1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese
6 #include <linux/device.h>
7 #include <linux/spinlock.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10 #include <linux/usb.h>
11 #include <sound/core.h>
12 #include <sound/pcm.h>
18 #define CLOCK_DRIFT_TOLERANCE 5
19 #define FRAMES_PER_URB 8
20 #define BYTES_PER_FRAME 512
21 #define CHANNELS_PER_STREAM 2
22 #define BYTES_PER_SAMPLE 3
23 #define BYTES_PER_SAMPLE_USB 4
24 #define MAX_BUFFER_SIZE (128*1024)
25 #define MAX_ENDPOINT_SIZE 512
27 #define ENDPOINT_CAPTURE 2
28 #define ENDPOINT_PLAYBACK 6
30 #define MAKE_CHECKBYTE(cdev,stream,i) \
31 (stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
33 static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
34 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
35 SNDRV_PCM_INFO_BLOCK_TRANSFER),
36 .formats = SNDRV_PCM_FMTBIT_S24_3BE,
37 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
38 SNDRV_PCM_RATE_96000),
40 .rate_max = 0, /* will overwrite later */
41 .channels_min = CHANNELS_PER_STREAM,
42 .channels_max = CHANNELS_PER_STREAM,
43 .buffer_bytes_max = MAX_BUFFER_SIZE,
44 .period_bytes_min = 128,
45 .period_bytes_max = MAX_BUFFER_SIZE,
51 activate_substream(struct snd_usb_caiaqdev *cdev,
52 struct snd_pcm_substream *sub)
54 spin_lock(&cdev->spinlock);
56 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
57 cdev->sub_playback[sub->number] = sub;
59 cdev->sub_capture[sub->number] = sub;
61 spin_unlock(&cdev->spinlock);
65 deactivate_substream(struct snd_usb_caiaqdev *cdev,
66 struct snd_pcm_substream *sub)
69 spin_lock_irqsave(&cdev->spinlock, flags);
71 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
72 cdev->sub_playback[sub->number] = NULL;
74 cdev->sub_capture[sub->number] = NULL;
76 spin_unlock_irqrestore(&cdev->spinlock, flags);
80 all_substreams_zero(struct snd_pcm_substream **subs)
83 for (i = 0; i < MAX_STREAMS; i++)
89 static int stream_start(struct snd_usb_caiaqdev *cdev)
92 struct device *dev = caiaqdev_to_dev(cdev);
94 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
99 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
100 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
101 cdev->input_panic = 0;
102 cdev->output_panic = 0;
103 cdev->first_packet = 4;
107 for (i = 0; i < N_URBS; i++) {
108 ret = usb_submit_urb(cdev->data_urbs_in[i], GFP_ATOMIC);
110 dev_err(dev, "unable to trigger read #%d! (ret %d)\n",
120 static void stream_stop(struct snd_usb_caiaqdev *cdev)
123 struct device *dev = caiaqdev_to_dev(cdev);
125 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
126 if (!cdev->streaming)
131 for (i = 0; i < N_URBS; i++) {
132 usb_kill_urb(cdev->data_urbs_in[i]);
134 if (test_bit(i, &cdev->outurb_active_mask))
135 usb_kill_urb(cdev->data_urbs_out[i]);
138 cdev->outurb_active_mask = 0;
141 static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
143 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
144 struct device *dev = caiaqdev_to_dev(cdev);
146 dev_dbg(dev, "%s(%p)\n", __func__, substream);
147 substream->runtime->hw = cdev->pcm_info;
148 snd_pcm_limit_hw_rates(substream->runtime);
153 static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
155 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
156 struct device *dev = caiaqdev_to_dev(cdev);
158 dev_dbg(dev, "%s(%p)\n", __func__, substream);
159 if (all_substreams_zero(cdev->sub_playback) &&
160 all_substreams_zero(cdev->sub_capture)) {
161 /* when the last client has stopped streaming,
162 * all sample rates are allowed again */
164 cdev->pcm_info.rates = cdev->samplerates;
170 static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
171 struct snd_pcm_hw_params *hw_params)
173 return snd_pcm_lib_alloc_vmalloc_buffer(sub,
174 params_buffer_bytes(hw_params));
177 static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
179 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
180 deactivate_substream(cdev, sub);
181 return snd_pcm_lib_free_vmalloc_buffer(sub);
184 /* this should probably go upstream */
185 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
186 #error "Change this table"
189 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
190 48000, 64000, 88200, 96000, 176400, 192000 };
192 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
194 int bytes_per_sample, bpp, ret, i;
195 int index = substream->number;
196 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
197 struct snd_pcm_runtime *runtime = substream->runtime;
198 struct device *dev = caiaqdev_to_dev(cdev);
200 dev_dbg(dev, "%s(%p)\n", __func__, substream);
202 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
205 switch (cdev->spec.data_alignment) {
208 out_pos = BYTES_PER_SAMPLE + 1;
216 cdev->period_out_count[index] = out_pos;
217 cdev->audio_out_buf_pos[index] = out_pos;
221 switch (cdev->spec.data_alignment) {
223 in_pos = BYTES_PER_SAMPLE + 2;
226 in_pos = BYTES_PER_SAMPLE;
234 cdev->period_in_count[index] = in_pos;
235 cdev->audio_in_buf_pos[index] = in_pos;
241 /* the first client that opens a stream defines the sample rate
242 * setting for all subsequent calls, until the last client closed. */
243 for (i=0; i < ARRAY_SIZE(rates); i++)
244 if (runtime->rate == rates[i])
245 cdev->pcm_info.rates = 1 << i;
247 snd_pcm_limit_hw_rates(runtime);
249 bytes_per_sample = BYTES_PER_SAMPLE;
250 if (cdev->spec.data_alignment >= 2)
253 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
254 * bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
256 if (bpp > MAX_ENDPOINT_SIZE)
257 bpp = MAX_ENDPOINT_SIZE;
259 ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
260 runtime->sample_bits, bpp);
264 ret = stream_start(cdev);
268 cdev->output_running = 0;
269 wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ);
270 if (!cdev->output_running) {
278 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
280 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
281 struct device *dev = caiaqdev_to_dev(cdev);
283 dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd);
286 case SNDRV_PCM_TRIGGER_START:
287 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
288 activate_substream(cdev, sub);
290 case SNDRV_PCM_TRIGGER_STOP:
291 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
292 deactivate_substream(cdev, sub);
301 static snd_pcm_uframes_t
302 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
304 int index = sub->number;
305 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
306 snd_pcm_uframes_t ptr;
308 spin_lock(&cdev->spinlock);
310 if (cdev->input_panic || cdev->output_panic) {
311 ptr = SNDRV_PCM_POS_XRUN;
315 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
316 ptr = bytes_to_frames(sub->runtime,
317 cdev->audio_out_buf_pos[index]);
319 ptr = bytes_to_frames(sub->runtime,
320 cdev->audio_in_buf_pos[index]);
323 spin_unlock(&cdev->spinlock);
327 /* operators for both playback and capture */
328 static const struct snd_pcm_ops snd_usb_caiaq_ops = {
329 .open = snd_usb_caiaq_substream_open,
330 .close = snd_usb_caiaq_substream_close,
331 .ioctl = snd_pcm_lib_ioctl,
332 .hw_params = snd_usb_caiaq_pcm_hw_params,
333 .hw_free = snd_usb_caiaq_pcm_hw_free,
334 .prepare = snd_usb_caiaq_pcm_prepare,
335 .trigger = snd_usb_caiaq_pcm_trigger,
336 .pointer = snd_usb_caiaq_pcm_pointer,
337 .page = snd_pcm_lib_get_vmalloc_page,
340 static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
341 struct snd_pcm_substream **subs)
343 int stream, pb, *cnt;
344 struct snd_pcm_substream *sub;
346 for (stream = 0; stream < cdev->n_streams; stream++) {
351 pb = snd_pcm_lib_period_bytes(sub);
352 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
353 &cdev->period_out_count[stream] :
354 &cdev->period_in_count[stream];
357 snd_pcm_period_elapsed(sub);
363 static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
364 const struct urb *urb,
365 const struct usb_iso_packet_descriptor *iso)
367 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
368 struct snd_pcm_substream *sub;
371 if (all_substreams_zero(cdev->sub_capture))
374 for (i = 0; i < iso->actual_length;) {
375 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
376 sub = cdev->sub_capture[stream];
378 struct snd_pcm_runtime *rt = sub->runtime;
379 char *audio_buf = rt->dma_area;
380 int sz = frames_to_bytes(rt, rt->buffer_size);
381 audio_buf[cdev->audio_in_buf_pos[stream]++]
383 cdev->period_in_count[stream]++;
384 if (cdev->audio_in_buf_pos[stream] == sz)
385 cdev->audio_in_buf_pos[stream] = 0;
391 static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
392 const struct urb *urb,
393 const struct usb_iso_packet_descriptor *iso)
395 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
396 unsigned char check_byte;
397 struct snd_pcm_substream *sub;
400 for (i = 0; i < iso->actual_length;) {
401 if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
403 stream < cdev->n_streams;
405 if (cdev->first_packet)
408 check_byte = MAKE_CHECKBYTE(cdev, stream, i);
410 if ((usb_buf[i] & 0x3f) != check_byte)
411 cdev->input_panic = 1;
413 if (usb_buf[i] & 0x80)
414 cdev->output_panic = 1;
417 cdev->first_packet = 0;
419 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
420 sub = cdev->sub_capture[stream];
421 if (cdev->input_panic)
425 struct snd_pcm_runtime *rt = sub->runtime;
426 char *audio_buf = rt->dma_area;
427 int sz = frames_to_bytes(rt, rt->buffer_size);
428 audio_buf[cdev->audio_in_buf_pos[stream]++] =
430 cdev->period_in_count[stream]++;
431 if (cdev->audio_in_buf_pos[stream] == sz)
432 cdev->audio_in_buf_pos[stream] = 0;
438 static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
439 const struct urb *urb,
440 const struct usb_iso_packet_descriptor *iso)
442 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
443 struct device *dev = caiaqdev_to_dev(cdev);
447 if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
450 for (i = 0; i < iso->actual_length;) {
451 for (stream = 0; stream < cdev->n_streams; stream++) {
452 struct snd_pcm_substream *sub = cdev->sub_capture[stream];
453 char *audio_buf = NULL;
456 if (sub && !cdev->input_panic) {
457 struct snd_pcm_runtime *rt = sub->runtime;
458 audio_buf = rt->dma_area;
459 sz = frames_to_bytes(rt, rt->buffer_size);
462 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
463 /* 3 audio data bytes, followed by 1 check byte */
465 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
466 audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
468 if (cdev->audio_in_buf_pos[stream] == sz)
469 cdev->audio_in_buf_pos[stream] = 0;
472 cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
475 i += BYTES_PER_SAMPLE;
477 if (usb_buf[i] != ((stream << 1) | c) &&
478 !cdev->first_packet) {
479 if (!cdev->input_panic)
480 dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
481 ((stream << 1) | c), usb_buf[i], c, stream, i);
482 cdev->input_panic = 1;
490 if (cdev->first_packet > 0)
491 cdev->first_packet--;
494 static void read_in_urb(struct snd_usb_caiaqdev *cdev,
495 const struct urb *urb,
496 const struct usb_iso_packet_descriptor *iso)
498 struct device *dev = caiaqdev_to_dev(cdev);
500 if (!cdev->streaming)
503 if (iso->actual_length < cdev->bpp)
506 switch (cdev->spec.data_alignment) {
508 read_in_urb_mode0(cdev, urb, iso);
511 read_in_urb_mode2(cdev, urb, iso);
514 read_in_urb_mode3(cdev, urb, iso);
518 if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
519 dev_warn(dev, "streaming error detected %s %s\n",
520 cdev->input_panic ? "(input)" : "",
521 cdev->output_panic ? "(output)" : "");
526 static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
528 const struct usb_iso_packet_descriptor *iso)
530 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
531 struct snd_pcm_substream *sub;
534 for (i = 0; i < iso->length;) {
535 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
536 sub = cdev->sub_playback[stream];
538 struct snd_pcm_runtime *rt = sub->runtime;
539 char *audio_buf = rt->dma_area;
540 int sz = frames_to_bytes(rt, rt->buffer_size);
542 audio_buf[cdev->audio_out_buf_pos[stream]];
543 cdev->period_out_count[stream]++;
544 cdev->audio_out_buf_pos[stream]++;
545 if (cdev->audio_out_buf_pos[stream] == sz)
546 cdev->audio_out_buf_pos[stream] = 0;
551 /* fill in the check bytes */
552 if (cdev->spec.data_alignment == 2 &&
553 i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
554 (cdev->n_streams * CHANNELS_PER_STREAM))
555 for (stream = 0; stream < cdev->n_streams; stream++, i++)
556 usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
560 static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
562 const struct usb_iso_packet_descriptor *iso)
564 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
567 for (i = 0; i < iso->length;) {
568 for (stream = 0; stream < cdev->n_streams; stream++) {
569 struct snd_pcm_substream *sub = cdev->sub_playback[stream];
570 char *audio_buf = NULL;
574 struct snd_pcm_runtime *rt = sub->runtime;
575 audio_buf = rt->dma_area;
576 sz = frames_to_bytes(rt, rt->buffer_size);
579 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
580 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
582 usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
584 if (cdev->audio_out_buf_pos[stream] == sz)
585 cdev->audio_out_buf_pos[stream] = 0;
592 cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
594 i += BYTES_PER_SAMPLE;
596 /* fill in the check byte pattern */
597 usb_buf[i++] = (stream << 1) | c;
603 static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
605 const struct usb_iso_packet_descriptor *iso)
607 switch (cdev->spec.data_alignment) {
610 fill_out_urb_mode_0(cdev, urb, iso);
613 fill_out_urb_mode_3(cdev, urb, iso);
618 static void read_completed(struct urb *urb)
620 struct snd_usb_caiaq_cb_info *info = urb->context;
621 struct snd_usb_caiaqdev *cdev;
623 struct urb *out = NULL;
624 int i, frame, len, send_it = 0, outframe = 0;
628 if (urb->status || !info)
632 dev = caiaqdev_to_dev(cdev);
634 if (!cdev->streaming)
637 /* find an unused output urb that is unused */
638 for (i = 0; i < N_URBS; i++)
639 if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
640 out = cdev->data_urbs_out[i];
645 dev_err(dev, "Unable to find an output urb to use\n");
649 /* read the recently received packet and send back one which has
651 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
652 if (urb->iso_frame_desc[frame].status)
655 len = urb->iso_frame_desc[outframe].actual_length;
656 out->iso_frame_desc[outframe].length = len;
657 out->iso_frame_desc[outframe].actual_length = 0;
658 out->iso_frame_desc[outframe].offset = offset;
662 spin_lock_irqsave(&cdev->spinlock, flags);
663 fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
664 read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
665 spin_unlock_irqrestore(&cdev->spinlock, flags);
666 check_for_elapsed_periods(cdev, cdev->sub_playback);
667 check_for_elapsed_periods(cdev, cdev->sub_capture);
675 out->number_of_packets = outframe;
676 usb_submit_urb(out, GFP_ATOMIC);
678 struct snd_usb_caiaq_cb_info *oinfo = out->context;
679 clear_bit(oinfo->index, &cdev->outurb_active_mask);
683 /* re-submit inbound urb */
684 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
685 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
686 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
687 urb->iso_frame_desc[frame].actual_length = 0;
690 urb->number_of_packets = FRAMES_PER_URB;
691 usb_submit_urb(urb, GFP_ATOMIC);
694 static void write_completed(struct urb *urb)
696 struct snd_usb_caiaq_cb_info *info = urb->context;
697 struct snd_usb_caiaqdev *cdev = info->cdev;
699 if (!cdev->output_running) {
700 cdev->output_running = 1;
701 wake_up(&cdev->prepare_wait_queue);
704 clear_bit(info->index, &cdev->outurb_active_mask);
707 static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
711 struct usb_device *usb_dev = cdev->chip.dev;
714 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
715 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
716 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
718 urbs = kmalloc_array(N_URBS, sizeof(*urbs), GFP_KERNEL);
724 for (i = 0; i < N_URBS; i++) {
725 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
731 urbs[i]->transfer_buffer =
732 kmalloc_array(BYTES_PER_FRAME, FRAMES_PER_URB,
734 if (!urbs[i]->transfer_buffer) {
739 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
740 struct usb_iso_packet_descriptor *iso =
741 &urbs[i]->iso_frame_desc[frame];
743 iso->offset = BYTES_PER_FRAME * frame;
744 iso->length = BYTES_PER_FRAME;
747 urbs[i]->dev = usb_dev;
748 urbs[i]->pipe = pipe;
749 urbs[i]->transfer_buffer_length = FRAMES_PER_URB
751 urbs[i]->context = &cdev->data_cb_info[i];
752 urbs[i]->interval = 1;
753 urbs[i]->number_of_packets = FRAMES_PER_URB;
754 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
755 read_completed : write_completed;
762 static void free_urbs(struct urb **urbs)
769 for (i = 0; i < N_URBS; i++) {
773 usb_kill_urb(urbs[i]);
774 kfree(urbs[i]->transfer_buffer);
775 usb_free_urb(urbs[i]);
781 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
784 struct device *dev = caiaqdev_to_dev(cdev);
786 cdev->n_audio_in = max(cdev->spec.num_analog_audio_in,
787 cdev->spec.num_digital_audio_in) /
789 cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
790 cdev->spec.num_digital_audio_out) /
792 cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
794 dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in);
795 dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out);
796 dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams);
798 if (cdev->n_streams > MAX_STREAMS) {
799 dev_err(dev, "unable to initialize device, too many streams.\n");
803 if (cdev->n_streams < 1) {
804 dev_err(dev, "bogus number of streams: %d\n", cdev->n_streams);
808 ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
809 cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
812 dev_err(dev, "snd_pcm_new() returned %d\n", ret);
816 cdev->pcm->private_data = cdev;
817 strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
819 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
820 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
822 memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
823 sizeof(snd_usb_caiaq_pcm_hardware));
825 /* setup samplerates */
826 cdev->samplerates = cdev->pcm_info.rates;
827 switch (cdev->chip.usb_id) {
828 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
829 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
830 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
831 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
832 cdev->samplerates |= SNDRV_PCM_RATE_192000;
834 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
835 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
836 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
837 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
838 cdev->samplerates |= SNDRV_PCM_RATE_88200;
842 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
844 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
848 kmalloc_array(N_URBS, sizeof(struct snd_usb_caiaq_cb_info),
851 if (!cdev->data_cb_info)
854 cdev->outurb_active_mask = 0;
855 BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8));
857 for (i = 0; i < N_URBS; i++) {
858 cdev->data_cb_info[i].cdev = cdev;
859 cdev->data_cb_info[i].index = i;
862 cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret);
864 kfree(cdev->data_cb_info);
865 free_urbs(cdev->data_urbs_in);
869 cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
871 kfree(cdev->data_cb_info);
872 free_urbs(cdev->data_urbs_in);
873 free_urbs(cdev->data_urbs_out);
880 void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev)
882 struct device *dev = caiaqdev_to_dev(cdev);
884 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
886 free_urbs(cdev->data_urbs_in);
887 free_urbs(cdev->data_urbs_out);
888 kfree(cdev->data_cb_info);