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_malloc_pages(sub, params_buffer_bytes(hw_params));
176 static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
178 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
179 deactivate_substream(cdev, sub);
180 return snd_pcm_lib_free_pages(sub);
183 /* this should probably go upstream */
184 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
185 #error "Change this table"
188 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
189 48000, 64000, 88200, 96000, 176400, 192000 };
191 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
193 int bytes_per_sample, bpp, ret, i;
194 int index = substream->number;
195 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
196 struct snd_pcm_runtime *runtime = substream->runtime;
197 struct device *dev = caiaqdev_to_dev(cdev);
199 dev_dbg(dev, "%s(%p)\n", __func__, substream);
201 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
204 switch (cdev->spec.data_alignment) {
207 out_pos = BYTES_PER_SAMPLE + 1;
215 cdev->period_out_count[index] = out_pos;
216 cdev->audio_out_buf_pos[index] = out_pos;
220 switch (cdev->spec.data_alignment) {
222 in_pos = BYTES_PER_SAMPLE + 2;
225 in_pos = BYTES_PER_SAMPLE;
233 cdev->period_in_count[index] = in_pos;
234 cdev->audio_in_buf_pos[index] = in_pos;
240 /* the first client that opens a stream defines the sample rate
241 * setting for all subsequent calls, until the last client closed. */
242 for (i=0; i < ARRAY_SIZE(rates); i++)
243 if (runtime->rate == rates[i])
244 cdev->pcm_info.rates = 1 << i;
246 snd_pcm_limit_hw_rates(runtime);
248 bytes_per_sample = BYTES_PER_SAMPLE;
249 if (cdev->spec.data_alignment >= 2)
252 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
253 * bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
255 if (bpp > MAX_ENDPOINT_SIZE)
256 bpp = MAX_ENDPOINT_SIZE;
258 ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
259 runtime->sample_bits, bpp);
263 ret = stream_start(cdev);
267 cdev->output_running = 0;
268 wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ);
269 if (!cdev->output_running) {
277 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
279 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
280 struct device *dev = caiaqdev_to_dev(cdev);
282 dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd);
285 case SNDRV_PCM_TRIGGER_START:
286 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
287 activate_substream(cdev, sub);
289 case SNDRV_PCM_TRIGGER_STOP:
290 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
291 deactivate_substream(cdev, sub);
300 static snd_pcm_uframes_t
301 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
303 int index = sub->number;
304 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
305 snd_pcm_uframes_t ptr;
307 spin_lock(&cdev->spinlock);
309 if (cdev->input_panic || cdev->output_panic) {
310 ptr = SNDRV_PCM_POS_XRUN;
314 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
315 ptr = bytes_to_frames(sub->runtime,
316 cdev->audio_out_buf_pos[index]);
318 ptr = bytes_to_frames(sub->runtime,
319 cdev->audio_in_buf_pos[index]);
322 spin_unlock(&cdev->spinlock);
326 /* operators for both playback and capture */
327 static const struct snd_pcm_ops snd_usb_caiaq_ops = {
328 .open = snd_usb_caiaq_substream_open,
329 .close = snd_usb_caiaq_substream_close,
330 .ioctl = snd_pcm_lib_ioctl,
331 .hw_params = snd_usb_caiaq_pcm_hw_params,
332 .hw_free = snd_usb_caiaq_pcm_hw_free,
333 .prepare = snd_usb_caiaq_pcm_prepare,
334 .trigger = snd_usb_caiaq_pcm_trigger,
335 .pointer = snd_usb_caiaq_pcm_pointer,
338 static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
339 struct snd_pcm_substream **subs)
341 int stream, pb, *cnt;
342 struct snd_pcm_substream *sub;
344 for (stream = 0; stream < cdev->n_streams; stream++) {
349 pb = snd_pcm_lib_period_bytes(sub);
350 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
351 &cdev->period_out_count[stream] :
352 &cdev->period_in_count[stream];
355 snd_pcm_period_elapsed(sub);
361 static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
362 const struct urb *urb,
363 const struct usb_iso_packet_descriptor *iso)
365 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
366 struct snd_pcm_substream *sub;
369 if (all_substreams_zero(cdev->sub_capture))
372 for (i = 0; i < iso->actual_length;) {
373 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
374 sub = cdev->sub_capture[stream];
376 struct snd_pcm_runtime *rt = sub->runtime;
377 char *audio_buf = rt->dma_area;
378 int sz = frames_to_bytes(rt, rt->buffer_size);
379 audio_buf[cdev->audio_in_buf_pos[stream]++]
381 cdev->period_in_count[stream]++;
382 if (cdev->audio_in_buf_pos[stream] == sz)
383 cdev->audio_in_buf_pos[stream] = 0;
389 static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
390 const struct urb *urb,
391 const struct usb_iso_packet_descriptor *iso)
393 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
394 unsigned char check_byte;
395 struct snd_pcm_substream *sub;
398 for (i = 0; i < iso->actual_length;) {
399 if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
401 stream < cdev->n_streams;
403 if (cdev->first_packet)
406 check_byte = MAKE_CHECKBYTE(cdev, stream, i);
408 if ((usb_buf[i] & 0x3f) != check_byte)
409 cdev->input_panic = 1;
411 if (usb_buf[i] & 0x80)
412 cdev->output_panic = 1;
415 cdev->first_packet = 0;
417 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
418 sub = cdev->sub_capture[stream];
419 if (cdev->input_panic)
423 struct snd_pcm_runtime *rt = sub->runtime;
424 char *audio_buf = rt->dma_area;
425 int sz = frames_to_bytes(rt, rt->buffer_size);
426 audio_buf[cdev->audio_in_buf_pos[stream]++] =
428 cdev->period_in_count[stream]++;
429 if (cdev->audio_in_buf_pos[stream] == sz)
430 cdev->audio_in_buf_pos[stream] = 0;
436 static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
437 const struct urb *urb,
438 const struct usb_iso_packet_descriptor *iso)
440 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
441 struct device *dev = caiaqdev_to_dev(cdev);
445 if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
448 for (i = 0; i < iso->actual_length;) {
449 for (stream = 0; stream < cdev->n_streams; stream++) {
450 struct snd_pcm_substream *sub = cdev->sub_capture[stream];
451 char *audio_buf = NULL;
454 if (sub && !cdev->input_panic) {
455 struct snd_pcm_runtime *rt = sub->runtime;
456 audio_buf = rt->dma_area;
457 sz = frames_to_bytes(rt, rt->buffer_size);
460 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
461 /* 3 audio data bytes, followed by 1 check byte */
463 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
464 audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
466 if (cdev->audio_in_buf_pos[stream] == sz)
467 cdev->audio_in_buf_pos[stream] = 0;
470 cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
473 i += BYTES_PER_SAMPLE;
475 if (usb_buf[i] != ((stream << 1) | c) &&
476 !cdev->first_packet) {
477 if (!cdev->input_panic)
478 dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
479 ((stream << 1) | c), usb_buf[i], c, stream, i);
480 cdev->input_panic = 1;
488 if (cdev->first_packet > 0)
489 cdev->first_packet--;
492 static void read_in_urb(struct snd_usb_caiaqdev *cdev,
493 const struct urb *urb,
494 const struct usb_iso_packet_descriptor *iso)
496 struct device *dev = caiaqdev_to_dev(cdev);
498 if (!cdev->streaming)
501 if (iso->actual_length < cdev->bpp)
504 switch (cdev->spec.data_alignment) {
506 read_in_urb_mode0(cdev, urb, iso);
509 read_in_urb_mode2(cdev, urb, iso);
512 read_in_urb_mode3(cdev, urb, iso);
516 if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
517 dev_warn(dev, "streaming error detected %s %s\n",
518 cdev->input_panic ? "(input)" : "",
519 cdev->output_panic ? "(output)" : "");
524 static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
526 const struct usb_iso_packet_descriptor *iso)
528 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
529 struct snd_pcm_substream *sub;
532 for (i = 0; i < iso->length;) {
533 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
534 sub = cdev->sub_playback[stream];
536 struct snd_pcm_runtime *rt = sub->runtime;
537 char *audio_buf = rt->dma_area;
538 int sz = frames_to_bytes(rt, rt->buffer_size);
540 audio_buf[cdev->audio_out_buf_pos[stream]];
541 cdev->period_out_count[stream]++;
542 cdev->audio_out_buf_pos[stream]++;
543 if (cdev->audio_out_buf_pos[stream] == sz)
544 cdev->audio_out_buf_pos[stream] = 0;
549 /* fill in the check bytes */
550 if (cdev->spec.data_alignment == 2 &&
551 i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
552 (cdev->n_streams * CHANNELS_PER_STREAM))
553 for (stream = 0; stream < cdev->n_streams; stream++, i++)
554 usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
558 static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
560 const struct usb_iso_packet_descriptor *iso)
562 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
565 for (i = 0; i < iso->length;) {
566 for (stream = 0; stream < cdev->n_streams; stream++) {
567 struct snd_pcm_substream *sub = cdev->sub_playback[stream];
568 char *audio_buf = NULL;
572 struct snd_pcm_runtime *rt = sub->runtime;
573 audio_buf = rt->dma_area;
574 sz = frames_to_bytes(rt, rt->buffer_size);
577 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
578 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
580 usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
582 if (cdev->audio_out_buf_pos[stream] == sz)
583 cdev->audio_out_buf_pos[stream] = 0;
590 cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
592 i += BYTES_PER_SAMPLE;
594 /* fill in the check byte pattern */
595 usb_buf[i++] = (stream << 1) | c;
601 static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
603 const struct usb_iso_packet_descriptor *iso)
605 switch (cdev->spec.data_alignment) {
608 fill_out_urb_mode_0(cdev, urb, iso);
611 fill_out_urb_mode_3(cdev, urb, iso);
616 static void read_completed(struct urb *urb)
618 struct snd_usb_caiaq_cb_info *info = urb->context;
619 struct snd_usb_caiaqdev *cdev;
621 struct urb *out = NULL;
622 int i, frame, len, send_it = 0, outframe = 0;
626 if (urb->status || !info)
630 dev = caiaqdev_to_dev(cdev);
632 if (!cdev->streaming)
635 /* find an unused output urb that is unused */
636 for (i = 0; i < N_URBS; i++)
637 if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
638 out = cdev->data_urbs_out[i];
643 dev_err(dev, "Unable to find an output urb to use\n");
647 /* read the recently received packet and send back one which has
649 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
650 if (urb->iso_frame_desc[frame].status)
653 len = urb->iso_frame_desc[outframe].actual_length;
654 out->iso_frame_desc[outframe].length = len;
655 out->iso_frame_desc[outframe].actual_length = 0;
656 out->iso_frame_desc[outframe].offset = offset;
660 spin_lock_irqsave(&cdev->spinlock, flags);
661 fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
662 read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
663 spin_unlock_irqrestore(&cdev->spinlock, flags);
664 check_for_elapsed_periods(cdev, cdev->sub_playback);
665 check_for_elapsed_periods(cdev, cdev->sub_capture);
673 out->number_of_packets = outframe;
674 usb_submit_urb(out, GFP_ATOMIC);
676 struct snd_usb_caiaq_cb_info *oinfo = out->context;
677 clear_bit(oinfo->index, &cdev->outurb_active_mask);
681 /* re-submit inbound urb */
682 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
683 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
684 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
685 urb->iso_frame_desc[frame].actual_length = 0;
688 urb->number_of_packets = FRAMES_PER_URB;
689 usb_submit_urb(urb, GFP_ATOMIC);
692 static void write_completed(struct urb *urb)
694 struct snd_usb_caiaq_cb_info *info = urb->context;
695 struct snd_usb_caiaqdev *cdev = info->cdev;
697 if (!cdev->output_running) {
698 cdev->output_running = 1;
699 wake_up(&cdev->prepare_wait_queue);
702 clear_bit(info->index, &cdev->outurb_active_mask);
705 static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
709 struct usb_device *usb_dev = cdev->chip.dev;
712 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
713 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
714 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
716 urbs = kmalloc_array(N_URBS, sizeof(*urbs), GFP_KERNEL);
722 for (i = 0; i < N_URBS; i++) {
723 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
729 urbs[i]->transfer_buffer =
730 kmalloc_array(BYTES_PER_FRAME, FRAMES_PER_URB,
732 if (!urbs[i]->transfer_buffer) {
737 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
738 struct usb_iso_packet_descriptor *iso =
739 &urbs[i]->iso_frame_desc[frame];
741 iso->offset = BYTES_PER_FRAME * frame;
742 iso->length = BYTES_PER_FRAME;
745 urbs[i]->dev = usb_dev;
746 urbs[i]->pipe = pipe;
747 urbs[i]->transfer_buffer_length = FRAMES_PER_URB
749 urbs[i]->context = &cdev->data_cb_info[i];
750 urbs[i]->interval = 1;
751 urbs[i]->number_of_packets = FRAMES_PER_URB;
752 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
753 read_completed : write_completed;
760 static void free_urbs(struct urb **urbs)
767 for (i = 0; i < N_URBS; i++) {
771 usb_kill_urb(urbs[i]);
772 kfree(urbs[i]->transfer_buffer);
773 usb_free_urb(urbs[i]);
779 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
782 struct device *dev = caiaqdev_to_dev(cdev);
784 cdev->n_audio_in = max(cdev->spec.num_analog_audio_in,
785 cdev->spec.num_digital_audio_in) /
787 cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
788 cdev->spec.num_digital_audio_out) /
790 cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
792 dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in);
793 dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out);
794 dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams);
796 if (cdev->n_streams > MAX_STREAMS) {
797 dev_err(dev, "unable to initialize device, too many streams.\n");
801 if (cdev->n_streams < 1) {
802 dev_err(dev, "bogus number of streams: %d\n", cdev->n_streams);
806 ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
807 cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
810 dev_err(dev, "snd_pcm_new() returned %d\n", ret);
814 cdev->pcm->private_data = cdev;
815 strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
817 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
818 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
820 memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
821 sizeof(snd_usb_caiaq_pcm_hardware));
823 /* setup samplerates */
824 cdev->samplerates = cdev->pcm_info.rates;
825 switch (cdev->chip.usb_id) {
826 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
827 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
828 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
829 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
830 cdev->samplerates |= SNDRV_PCM_RATE_192000;
832 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
833 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
834 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
835 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
836 cdev->samplerates |= SNDRV_PCM_RATE_88200;
840 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
842 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
844 snd_pcm_lib_preallocate_pages_for_all(cdev->pcm, SNDRV_DMA_TYPE_VMALLOC,
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);