2 * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/device.h>
20 #include <linux/spinlock.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/usb.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
31 #define CLOCK_DRIFT_TOLERANCE 5
32 #define FRAMES_PER_URB 8
33 #define BYTES_PER_FRAME 512
34 #define CHANNELS_PER_STREAM 2
35 #define BYTES_PER_SAMPLE 3
36 #define BYTES_PER_SAMPLE_USB 4
37 #define MAX_BUFFER_SIZE (128*1024)
38 #define MAX_ENDPOINT_SIZE 512
40 #define ENDPOINT_CAPTURE 2
41 #define ENDPOINT_PLAYBACK 6
43 #define MAKE_CHECKBYTE(cdev,stream,i) \
44 (stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
46 static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware
= {
47 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
48 SNDRV_PCM_INFO_BLOCK_TRANSFER
),
49 .formats
= SNDRV_PCM_FMTBIT_S24_3BE
,
50 .rates
= (SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
|
51 SNDRV_PCM_RATE_96000
),
53 .rate_max
= 0, /* will overwrite later */
54 .channels_min
= CHANNELS_PER_STREAM
,
55 .channels_max
= CHANNELS_PER_STREAM
,
56 .buffer_bytes_max
= MAX_BUFFER_SIZE
,
57 .period_bytes_min
= 128,
58 .period_bytes_max
= MAX_BUFFER_SIZE
,
64 activate_substream(struct snd_usb_caiaqdev
*cdev
,
65 struct snd_pcm_substream
*sub
)
67 spin_lock(&cdev
->spinlock
);
69 if (sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
70 cdev
->sub_playback
[sub
->number
] = sub
;
72 cdev
->sub_capture
[sub
->number
] = sub
;
74 spin_unlock(&cdev
->spinlock
);
78 deactivate_substream(struct snd_usb_caiaqdev
*cdev
,
79 struct snd_pcm_substream
*sub
)
82 spin_lock_irqsave(&cdev
->spinlock
, flags
);
84 if (sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
85 cdev
->sub_playback
[sub
->number
] = NULL
;
87 cdev
->sub_capture
[sub
->number
] = NULL
;
89 spin_unlock_irqrestore(&cdev
->spinlock
, flags
);
93 all_substreams_zero(struct snd_pcm_substream
**subs
)
96 for (i
= 0; i
< MAX_STREAMS
; i
++)
102 static int stream_start(struct snd_usb_caiaqdev
*cdev
)
105 struct device
*dev
= caiaqdev_to_dev(cdev
);
107 dev_dbg(dev
, "%s(%p)\n", __func__
, cdev
);
112 memset(cdev
->sub_playback
, 0, sizeof(cdev
->sub_playback
));
113 memset(cdev
->sub_capture
, 0, sizeof(cdev
->sub_capture
));
114 cdev
->input_panic
= 0;
115 cdev
->output_panic
= 0;
116 cdev
->first_packet
= 4;
120 for (i
= 0; i
< N_URBS
; i
++) {
121 ret
= usb_submit_urb(cdev
->data_urbs_in
[i
], GFP_ATOMIC
);
123 dev_err(dev
, "unable to trigger read #%d! (ret %d)\n",
133 static void stream_stop(struct snd_usb_caiaqdev
*cdev
)
136 struct device
*dev
= caiaqdev_to_dev(cdev
);
138 dev_dbg(dev
, "%s(%p)\n", __func__
, cdev
);
139 if (!cdev
->streaming
)
144 for (i
= 0; i
< N_URBS
; i
++) {
145 usb_kill_urb(cdev
->data_urbs_in
[i
]);
147 if (test_bit(i
, &cdev
->outurb_active_mask
))
148 usb_kill_urb(cdev
->data_urbs_out
[i
]);
151 cdev
->outurb_active_mask
= 0;
154 static int snd_usb_caiaq_substream_open(struct snd_pcm_substream
*substream
)
156 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(substream
);
157 struct device
*dev
= caiaqdev_to_dev(cdev
);
159 dev_dbg(dev
, "%s(%p)\n", __func__
, substream
);
160 substream
->runtime
->hw
= cdev
->pcm_info
;
161 snd_pcm_limit_hw_rates(substream
->runtime
);
166 static int snd_usb_caiaq_substream_close(struct snd_pcm_substream
*substream
)
168 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(substream
);
169 struct device
*dev
= caiaqdev_to_dev(cdev
);
171 dev_dbg(dev
, "%s(%p)\n", __func__
, substream
);
172 if (all_substreams_zero(cdev
->sub_playback
) &&
173 all_substreams_zero(cdev
->sub_capture
)) {
174 /* when the last client has stopped streaming,
175 * all sample rates are allowed again */
177 cdev
->pcm_info
.rates
= cdev
->samplerates
;
183 static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream
*sub
,
184 struct snd_pcm_hw_params
*hw_params
)
186 return snd_pcm_lib_malloc_pages(sub
, params_buffer_bytes(hw_params
));
189 static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream
*sub
)
191 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(sub
);
192 deactivate_substream(cdev
, sub
);
193 return snd_pcm_lib_free_pages(sub
);
196 /* this should probably go upstream */
197 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
198 #error "Change this table"
201 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
202 48000, 64000, 88200, 96000, 176400, 192000 };
204 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream
*substream
)
206 int bytes_per_sample
, bpp
, ret
, i
;
207 int index
= substream
->number
;
208 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(substream
);
209 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
210 struct device
*dev
= caiaqdev_to_dev(cdev
);
212 dev_dbg(dev
, "%s(%p)\n", __func__
, substream
);
214 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
217 switch (cdev
->spec
.data_alignment
) {
220 out_pos
= BYTES_PER_SAMPLE
+ 1;
228 cdev
->period_out_count
[index
] = out_pos
;
229 cdev
->audio_out_buf_pos
[index
] = out_pos
;
233 switch (cdev
->spec
.data_alignment
) {
235 in_pos
= BYTES_PER_SAMPLE
+ 2;
238 in_pos
= BYTES_PER_SAMPLE
;
246 cdev
->period_in_count
[index
] = in_pos
;
247 cdev
->audio_in_buf_pos
[index
] = in_pos
;
253 /* the first client that opens a stream defines the sample rate
254 * setting for all subsequent calls, until the last client closed. */
255 for (i
=0; i
< ARRAY_SIZE(rates
); i
++)
256 if (runtime
->rate
== rates
[i
])
257 cdev
->pcm_info
.rates
= 1 << i
;
259 snd_pcm_limit_hw_rates(runtime
);
261 bytes_per_sample
= BYTES_PER_SAMPLE
;
262 if (cdev
->spec
.data_alignment
>= 2)
265 bpp
= ((runtime
->rate
/ 8000) + CLOCK_DRIFT_TOLERANCE
)
266 * bytes_per_sample
* CHANNELS_PER_STREAM
* cdev
->n_streams
;
268 if (bpp
> MAX_ENDPOINT_SIZE
)
269 bpp
= MAX_ENDPOINT_SIZE
;
271 ret
= snd_usb_caiaq_set_audio_params(cdev
, runtime
->rate
,
272 runtime
->sample_bits
, bpp
);
276 ret
= stream_start(cdev
);
280 cdev
->output_running
= 0;
281 wait_event_timeout(cdev
->prepare_wait_queue
, cdev
->output_running
, HZ
);
282 if (!cdev
->output_running
) {
290 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream
*sub
, int cmd
)
292 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(sub
);
293 struct device
*dev
= caiaqdev_to_dev(cdev
);
295 dev_dbg(dev
, "%s(%p) cmd %d\n", __func__
, sub
, cmd
);
298 case SNDRV_PCM_TRIGGER_START
:
299 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
300 activate_substream(cdev
, sub
);
302 case SNDRV_PCM_TRIGGER_STOP
:
303 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
304 deactivate_substream(cdev
, sub
);
313 static snd_pcm_uframes_t
314 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream
*sub
)
316 int index
= sub
->number
;
317 struct snd_usb_caiaqdev
*cdev
= snd_pcm_substream_chip(sub
);
318 snd_pcm_uframes_t ptr
;
320 spin_lock(&cdev
->spinlock
);
322 if (cdev
->input_panic
|| cdev
->output_panic
) {
323 ptr
= SNDRV_PCM_POS_XRUN
;
327 if (sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
328 ptr
= bytes_to_frames(sub
->runtime
,
329 cdev
->audio_out_buf_pos
[index
]);
331 ptr
= bytes_to_frames(sub
->runtime
,
332 cdev
->audio_in_buf_pos
[index
]);
335 spin_unlock(&cdev
->spinlock
);
339 /* operators for both playback and capture */
340 static struct snd_pcm_ops snd_usb_caiaq_ops
= {
341 .open
= snd_usb_caiaq_substream_open
,
342 .close
= snd_usb_caiaq_substream_close
,
343 .ioctl
= snd_pcm_lib_ioctl
,
344 .hw_params
= snd_usb_caiaq_pcm_hw_params
,
345 .hw_free
= snd_usb_caiaq_pcm_hw_free
,
346 .prepare
= snd_usb_caiaq_pcm_prepare
,
347 .trigger
= snd_usb_caiaq_pcm_trigger
,
348 .pointer
= snd_usb_caiaq_pcm_pointer
351 static void check_for_elapsed_periods(struct snd_usb_caiaqdev
*cdev
,
352 struct snd_pcm_substream
**subs
)
354 int stream
, pb
, *cnt
;
355 struct snd_pcm_substream
*sub
;
357 for (stream
= 0; stream
< cdev
->n_streams
; stream
++) {
362 pb
= snd_pcm_lib_period_bytes(sub
);
363 cnt
= (sub
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
364 &cdev
->period_out_count
[stream
] :
365 &cdev
->period_in_count
[stream
];
368 snd_pcm_period_elapsed(sub
);
374 static void read_in_urb_mode0(struct snd_usb_caiaqdev
*cdev
,
375 const struct urb
*urb
,
376 const struct usb_iso_packet_descriptor
*iso
)
378 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
379 struct snd_pcm_substream
*sub
;
382 if (all_substreams_zero(cdev
->sub_capture
))
385 for (i
= 0; i
< iso
->actual_length
;) {
386 for (stream
= 0; stream
< cdev
->n_streams
; stream
++, i
++) {
387 sub
= cdev
->sub_capture
[stream
];
389 struct snd_pcm_runtime
*rt
= sub
->runtime
;
390 char *audio_buf
= rt
->dma_area
;
391 int sz
= frames_to_bytes(rt
, rt
->buffer_size
);
392 audio_buf
[cdev
->audio_in_buf_pos
[stream
]++]
394 cdev
->period_in_count
[stream
]++;
395 if (cdev
->audio_in_buf_pos
[stream
] == sz
)
396 cdev
->audio_in_buf_pos
[stream
] = 0;
402 static void read_in_urb_mode2(struct snd_usb_caiaqdev
*cdev
,
403 const struct urb
*urb
,
404 const struct usb_iso_packet_descriptor
*iso
)
406 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
407 unsigned char check_byte
;
408 struct snd_pcm_substream
*sub
;
411 for (i
= 0; i
< iso
->actual_length
;) {
412 if (i
% (cdev
->n_streams
* BYTES_PER_SAMPLE_USB
) == 0) {
414 stream
< cdev
->n_streams
;
416 if (cdev
->first_packet
)
419 check_byte
= MAKE_CHECKBYTE(cdev
, stream
, i
);
421 if ((usb_buf
[i
] & 0x3f) != check_byte
)
422 cdev
->input_panic
= 1;
424 if (usb_buf
[i
] & 0x80)
425 cdev
->output_panic
= 1;
428 cdev
->first_packet
= 0;
430 for (stream
= 0; stream
< cdev
->n_streams
; stream
++, i
++) {
431 sub
= cdev
->sub_capture
[stream
];
432 if (cdev
->input_panic
)
436 struct snd_pcm_runtime
*rt
= sub
->runtime
;
437 char *audio_buf
= rt
->dma_area
;
438 int sz
= frames_to_bytes(rt
, rt
->buffer_size
);
439 audio_buf
[cdev
->audio_in_buf_pos
[stream
]++] =
441 cdev
->period_in_count
[stream
]++;
442 if (cdev
->audio_in_buf_pos
[stream
] == sz
)
443 cdev
->audio_in_buf_pos
[stream
] = 0;
449 static void read_in_urb_mode3(struct snd_usb_caiaqdev
*cdev
,
450 const struct urb
*urb
,
451 const struct usb_iso_packet_descriptor
*iso
)
453 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
454 struct device
*dev
= caiaqdev_to_dev(cdev
);
458 if (iso
->actual_length
% (BYTES_PER_SAMPLE_USB
* CHANNELS_PER_STREAM
))
461 for (i
= 0; i
< iso
->actual_length
;) {
462 for (stream
= 0; stream
< cdev
->n_streams
; stream
++) {
463 struct snd_pcm_substream
*sub
= cdev
->sub_capture
[stream
];
464 char *audio_buf
= NULL
;
467 if (sub
&& !cdev
->input_panic
) {
468 struct snd_pcm_runtime
*rt
= sub
->runtime
;
469 audio_buf
= rt
->dma_area
;
470 sz
= frames_to_bytes(rt
, rt
->buffer_size
);
473 for (c
= 0; c
< CHANNELS_PER_STREAM
; c
++) {
474 /* 3 audio data bytes, followed by 1 check byte */
476 for (n
= 0; n
< BYTES_PER_SAMPLE
; n
++) {
477 audio_buf
[cdev
->audio_in_buf_pos
[stream
]++] = usb_buf
[i
+n
];
479 if (cdev
->audio_in_buf_pos
[stream
] == sz
)
480 cdev
->audio_in_buf_pos
[stream
] = 0;
483 cdev
->period_in_count
[stream
] += BYTES_PER_SAMPLE
;
486 i
+= BYTES_PER_SAMPLE
;
488 if (usb_buf
[i
] != ((stream
<< 1) | c
) &&
489 !cdev
->first_packet
) {
490 if (!cdev
->input_panic
)
491 dev_warn(dev
, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
492 ((stream
<< 1) | c
), usb_buf
[i
], c
, stream
, i
);
493 cdev
->input_panic
= 1;
501 if (cdev
->first_packet
> 0)
502 cdev
->first_packet
--;
505 static void read_in_urb(struct snd_usb_caiaqdev
*cdev
,
506 const struct urb
*urb
,
507 const struct usb_iso_packet_descriptor
*iso
)
509 struct device
*dev
= caiaqdev_to_dev(cdev
);
511 if (!cdev
->streaming
)
514 if (iso
->actual_length
< cdev
->bpp
)
517 switch (cdev
->spec
.data_alignment
) {
519 read_in_urb_mode0(cdev
, urb
, iso
);
522 read_in_urb_mode2(cdev
, urb
, iso
);
525 read_in_urb_mode3(cdev
, urb
, iso
);
529 if ((cdev
->input_panic
|| cdev
->output_panic
) && !cdev
->warned
) {
530 dev_warn(dev
, "streaming error detected %s %s\n",
531 cdev
->input_panic
? "(input)" : "",
532 cdev
->output_panic
? "(output)" : "");
537 static void fill_out_urb_mode_0(struct snd_usb_caiaqdev
*cdev
,
539 const struct usb_iso_packet_descriptor
*iso
)
541 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
542 struct snd_pcm_substream
*sub
;
545 for (i
= 0; i
< iso
->length
;) {
546 for (stream
= 0; stream
< cdev
->n_streams
; stream
++, i
++) {
547 sub
= cdev
->sub_playback
[stream
];
549 struct snd_pcm_runtime
*rt
= sub
->runtime
;
550 char *audio_buf
= rt
->dma_area
;
551 int sz
= frames_to_bytes(rt
, rt
->buffer_size
);
553 audio_buf
[cdev
->audio_out_buf_pos
[stream
]];
554 cdev
->period_out_count
[stream
]++;
555 cdev
->audio_out_buf_pos
[stream
]++;
556 if (cdev
->audio_out_buf_pos
[stream
] == sz
)
557 cdev
->audio_out_buf_pos
[stream
] = 0;
562 /* fill in the check bytes */
563 if (cdev
->spec
.data_alignment
== 2 &&
564 i
% (cdev
->n_streams
* BYTES_PER_SAMPLE_USB
) ==
565 (cdev
->n_streams
* CHANNELS_PER_STREAM
))
566 for (stream
= 0; stream
< cdev
->n_streams
; stream
++, i
++)
567 usb_buf
[i
] = MAKE_CHECKBYTE(cdev
, stream
, i
);
571 static void fill_out_urb_mode_3(struct snd_usb_caiaqdev
*cdev
,
573 const struct usb_iso_packet_descriptor
*iso
)
575 unsigned char *usb_buf
= urb
->transfer_buffer
+ iso
->offset
;
578 for (i
= 0; i
< iso
->length
;) {
579 for (stream
= 0; stream
< cdev
->n_streams
; stream
++) {
580 struct snd_pcm_substream
*sub
= cdev
->sub_playback
[stream
];
581 char *audio_buf
= NULL
;
585 struct snd_pcm_runtime
*rt
= sub
->runtime
;
586 audio_buf
= rt
->dma_area
;
587 sz
= frames_to_bytes(rt
, rt
->buffer_size
);
590 for (c
= 0; c
< CHANNELS_PER_STREAM
; c
++) {
591 for (n
= 0; n
< BYTES_PER_SAMPLE
; n
++) {
593 usb_buf
[i
+n
] = audio_buf
[cdev
->audio_out_buf_pos
[stream
]++];
595 if (cdev
->audio_out_buf_pos
[stream
] == sz
)
596 cdev
->audio_out_buf_pos
[stream
] = 0;
603 cdev
->period_out_count
[stream
] += BYTES_PER_SAMPLE
;
605 i
+= BYTES_PER_SAMPLE
;
607 /* fill in the check byte pattern */
608 usb_buf
[i
++] = (stream
<< 1) | c
;
614 static inline void fill_out_urb(struct snd_usb_caiaqdev
*cdev
,
616 const struct usb_iso_packet_descriptor
*iso
)
618 switch (cdev
->spec
.data_alignment
) {
621 fill_out_urb_mode_0(cdev
, urb
, iso
);
624 fill_out_urb_mode_3(cdev
, urb
, iso
);
629 static void read_completed(struct urb
*urb
)
631 struct snd_usb_caiaq_cb_info
*info
= urb
->context
;
632 struct snd_usb_caiaqdev
*cdev
;
634 struct urb
*out
= NULL
;
635 int i
, frame
, len
, send_it
= 0, outframe
= 0;
638 if (urb
->status
|| !info
)
642 dev
= caiaqdev_to_dev(cdev
);
644 if (!cdev
->streaming
)
647 /* find an unused output urb that is unused */
648 for (i
= 0; i
< N_URBS
; i
++)
649 if (test_and_set_bit(i
, &cdev
->outurb_active_mask
) == 0) {
650 out
= cdev
->data_urbs_out
[i
];
655 dev_err(dev
, "Unable to find an output urb to use\n");
659 /* read the recently received packet and send back one which has
661 for (frame
= 0; frame
< FRAMES_PER_URB
; frame
++) {
662 if (urb
->iso_frame_desc
[frame
].status
)
665 len
= urb
->iso_frame_desc
[outframe
].actual_length
;
666 out
->iso_frame_desc
[outframe
].length
= len
;
667 out
->iso_frame_desc
[outframe
].actual_length
= 0;
668 out
->iso_frame_desc
[outframe
].offset
= offset
;
672 spin_lock(&cdev
->spinlock
);
673 fill_out_urb(cdev
, out
, &out
->iso_frame_desc
[outframe
]);
674 read_in_urb(cdev
, urb
, &urb
->iso_frame_desc
[frame
]);
675 spin_unlock(&cdev
->spinlock
);
676 check_for_elapsed_periods(cdev
, cdev
->sub_playback
);
677 check_for_elapsed_periods(cdev
, cdev
->sub_capture
);
685 out
->number_of_packets
= outframe
;
686 usb_submit_urb(out
, GFP_ATOMIC
);
688 struct snd_usb_caiaq_cb_info
*oinfo
= out
->context
;
689 clear_bit(oinfo
->index
, &cdev
->outurb_active_mask
);
693 /* re-submit inbound urb */
694 for (frame
= 0; frame
< FRAMES_PER_URB
; frame
++) {
695 urb
->iso_frame_desc
[frame
].offset
= BYTES_PER_FRAME
* frame
;
696 urb
->iso_frame_desc
[frame
].length
= BYTES_PER_FRAME
;
697 urb
->iso_frame_desc
[frame
].actual_length
= 0;
700 urb
->number_of_packets
= FRAMES_PER_URB
;
701 usb_submit_urb(urb
, GFP_ATOMIC
);
704 static void write_completed(struct urb
*urb
)
706 struct snd_usb_caiaq_cb_info
*info
= urb
->context
;
707 struct snd_usb_caiaqdev
*cdev
= info
->cdev
;
709 if (!cdev
->output_running
) {
710 cdev
->output_running
= 1;
711 wake_up(&cdev
->prepare_wait_queue
);
714 clear_bit(info
->index
, &cdev
->outurb_active_mask
);
717 static struct urb
**alloc_urbs(struct snd_usb_caiaqdev
*cdev
, int dir
, int *ret
)
721 struct usb_device
*usb_dev
= cdev
->chip
.dev
;
722 struct device
*dev
= caiaqdev_to_dev(cdev
);
725 pipe
= (dir
== SNDRV_PCM_STREAM_PLAYBACK
) ?
726 usb_sndisocpipe(usb_dev
, ENDPOINT_PLAYBACK
) :
727 usb_rcvisocpipe(usb_dev
, ENDPOINT_CAPTURE
);
729 urbs
= kmalloc(N_URBS
* sizeof(*urbs
), GFP_KERNEL
);
731 dev_err(dev
, "unable to kmalloc() urbs, OOM!?\n");
736 for (i
= 0; i
< N_URBS
; i
++) {
737 urbs
[i
] = usb_alloc_urb(FRAMES_PER_URB
, GFP_KERNEL
);
739 dev_err(dev
, "unable to usb_alloc_urb(), OOM!?\n");
744 urbs
[i
]->transfer_buffer
=
745 kmalloc(FRAMES_PER_URB
* BYTES_PER_FRAME
, GFP_KERNEL
);
746 if (!urbs
[i
]->transfer_buffer
) {
747 dev_err(dev
, "unable to kmalloc() transfer buffer, OOM!?\n");
752 for (frame
= 0; frame
< FRAMES_PER_URB
; frame
++) {
753 struct usb_iso_packet_descriptor
*iso
=
754 &urbs
[i
]->iso_frame_desc
[frame
];
756 iso
->offset
= BYTES_PER_FRAME
* frame
;
757 iso
->length
= BYTES_PER_FRAME
;
760 urbs
[i
]->dev
= usb_dev
;
761 urbs
[i
]->pipe
= pipe
;
762 urbs
[i
]->transfer_buffer_length
= FRAMES_PER_URB
764 urbs
[i
]->context
= &cdev
->data_cb_info
[i
];
765 urbs
[i
]->interval
= 1;
766 urbs
[i
]->number_of_packets
= FRAMES_PER_URB
;
767 urbs
[i
]->complete
= (dir
== SNDRV_PCM_STREAM_CAPTURE
) ?
768 read_completed
: write_completed
;
775 static void free_urbs(struct urb
**urbs
)
782 for (i
= 0; i
< N_URBS
; i
++) {
786 usb_kill_urb(urbs
[i
]);
787 kfree(urbs
[i
]->transfer_buffer
);
788 usb_free_urb(urbs
[i
]);
794 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev
*cdev
)
797 struct device
*dev
= caiaqdev_to_dev(cdev
);
799 cdev
->n_audio_in
= max(cdev
->spec
.num_analog_audio_in
,
800 cdev
->spec
.num_digital_audio_in
) /
802 cdev
->n_audio_out
= max(cdev
->spec
.num_analog_audio_out
,
803 cdev
->spec
.num_digital_audio_out
) /
805 cdev
->n_streams
= max(cdev
->n_audio_in
, cdev
->n_audio_out
);
807 dev_dbg(dev
, "cdev->n_audio_in = %d\n", cdev
->n_audio_in
);
808 dev_dbg(dev
, "cdev->n_audio_out = %d\n", cdev
->n_audio_out
);
809 dev_dbg(dev
, "cdev->n_streams = %d\n", cdev
->n_streams
);
811 if (cdev
->n_streams
> MAX_STREAMS
) {
812 dev_err(dev
, "unable to initialize device, too many streams.\n");
816 ret
= snd_pcm_new(cdev
->chip
.card
, cdev
->product_name
, 0,
817 cdev
->n_audio_out
, cdev
->n_audio_in
, &cdev
->pcm
);
820 dev_err(dev
, "snd_pcm_new() returned %d\n", ret
);
824 cdev
->pcm
->private_data
= cdev
;
825 strlcpy(cdev
->pcm
->name
, cdev
->product_name
, sizeof(cdev
->pcm
->name
));
827 memset(cdev
->sub_playback
, 0, sizeof(cdev
->sub_playback
));
828 memset(cdev
->sub_capture
, 0, sizeof(cdev
->sub_capture
));
830 memcpy(&cdev
->pcm_info
, &snd_usb_caiaq_pcm_hardware
,
831 sizeof(snd_usb_caiaq_pcm_hardware
));
833 /* setup samplerates */
834 cdev
->samplerates
= cdev
->pcm_info
.rates
;
835 switch (cdev
->chip
.usb_id
) {
836 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AK1
):
837 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_RIGKONTROL3
):
838 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_SESSIONIO
):
839 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_GUITARRIGMOBILE
):
840 cdev
->samplerates
|= SNDRV_PCM_RATE_192000
;
842 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO2DJ
):
843 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO4DJ
):
844 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO8DJ
):
845 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_TRAKTORAUDIO2
):
846 cdev
->samplerates
|= SNDRV_PCM_RATE_88200
;
850 snd_pcm_set_ops(cdev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
852 snd_pcm_set_ops(cdev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
,
855 snd_pcm_lib_preallocate_pages_for_all(cdev
->pcm
,
856 SNDRV_DMA_TYPE_CONTINUOUS
,
857 snd_dma_continuous_data(GFP_KERNEL
),
858 MAX_BUFFER_SIZE
, MAX_BUFFER_SIZE
);
861 kmalloc(sizeof(struct snd_usb_caiaq_cb_info
) * N_URBS
,
864 if (!cdev
->data_cb_info
)
867 cdev
->outurb_active_mask
= 0;
868 BUILD_BUG_ON(N_URBS
> (sizeof(cdev
->outurb_active_mask
) * 8));
870 for (i
= 0; i
< N_URBS
; i
++) {
871 cdev
->data_cb_info
[i
].cdev
= cdev
;
872 cdev
->data_cb_info
[i
].index
= i
;
875 cdev
->data_urbs_in
= alloc_urbs(cdev
, SNDRV_PCM_STREAM_CAPTURE
, &ret
);
877 kfree(cdev
->data_cb_info
);
878 free_urbs(cdev
->data_urbs_in
);
882 cdev
->data_urbs_out
= alloc_urbs(cdev
, SNDRV_PCM_STREAM_PLAYBACK
, &ret
);
884 kfree(cdev
->data_cb_info
);
885 free_urbs(cdev
->data_urbs_in
);
886 free_urbs(cdev
->data_urbs_out
);
893 void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev
*cdev
)
895 struct device
*dev
= caiaqdev_to_dev(cdev
);
897 dev_dbg(dev
, "%s(%p)\n", __func__
, cdev
);
899 free_urbs(cdev
->data_urbs_in
);
900 free_urbs(cdev
->data_urbs_out
);
901 kfree(cdev
->data_cb_info
);