initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / sound / usb / caiaq / audio.c
blobe89017a7a2bdc0e4e8c2c0bb728d493b79cbe884
1 /*
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/spinlock.h>
20 #include <linux/init.h>
21 #include <linux/usb.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
25 #include "device.h"
26 #include "audio.h"
28 #define N_URBS 32
29 #define CLOCK_DRIFT_TOLERANCE 5
30 #define FRAMES_PER_URB 8
31 #define BYTES_PER_FRAME 512
32 #define CHANNELS_PER_STREAM 2
33 #define BYTES_PER_SAMPLE 3
34 #define BYTES_PER_SAMPLE_USB 4
35 #define MAX_BUFFER_SIZE (128*1024)
36 #define MAX_ENDPOINT_SIZE 512
38 #define ENDPOINT_CAPTURE 2
39 #define ENDPOINT_PLAYBACK 6
41 #define MAKE_CHECKBYTE(dev,stream,i) \
42 (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
44 static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
45 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
46 SNDRV_PCM_INFO_BLOCK_TRANSFER),
47 .formats = SNDRV_PCM_FMTBIT_S24_3BE,
48 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
49 SNDRV_PCM_RATE_96000),
50 .rate_min = 44100,
51 .rate_max = 0, /* will overwrite later */
52 .channels_min = CHANNELS_PER_STREAM,
53 .channels_max = CHANNELS_PER_STREAM,
54 .buffer_bytes_max = MAX_BUFFER_SIZE,
55 .period_bytes_min = 128,
56 .period_bytes_max = MAX_BUFFER_SIZE,
57 .periods_min = 1,
58 .periods_max = 1024,
61 static void
62 activate_substream(struct snd_usb_caiaqdev *dev,
63 struct snd_pcm_substream *sub)
65 spin_lock(&dev->spinlock);
67 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
68 dev->sub_playback[sub->number] = sub;
69 else
70 dev->sub_capture[sub->number] = sub;
72 spin_unlock(&dev->spinlock);
75 static void
76 deactivate_substream(struct snd_usb_caiaqdev *dev,
77 struct snd_pcm_substream *sub)
79 unsigned long flags;
80 spin_lock_irqsave(&dev->spinlock, flags);
82 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
83 dev->sub_playback[sub->number] = NULL;
84 else
85 dev->sub_capture[sub->number] = NULL;
87 spin_unlock_irqrestore(&dev->spinlock, flags);
90 static int
91 all_substreams_zero(struct snd_pcm_substream **subs)
93 int i;
94 for (i = 0; i < MAX_STREAMS; i++)
95 if (subs[i] != NULL)
96 return 0;
97 return 1;
100 static int stream_start(struct snd_usb_caiaqdev *dev)
102 int i, ret;
104 debug("%s(%p)\n", __func__, dev);
106 if (dev->streaming)
107 return -EINVAL;
109 memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
110 memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
111 dev->input_panic = 0;
112 dev->output_panic = 0;
113 dev->first_packet = 1;
114 dev->streaming = 1;
115 dev->warned = 0;
117 for (i = 0; i < N_URBS; i++) {
118 ret = usb_submit_urb(dev->data_urbs_in[i], GFP_ATOMIC);
119 if (ret) {
120 log("unable to trigger read #%d! (ret %d)\n", i, ret);
121 dev->streaming = 0;
122 return -EPIPE;
126 return 0;
129 static void stream_stop(struct snd_usb_caiaqdev *dev)
131 int i;
133 debug("%s(%p)\n", __func__, dev);
134 if (!dev->streaming)
135 return;
137 dev->streaming = 0;
139 for (i = 0; i < N_URBS; i++) {
140 usb_kill_urb(dev->data_urbs_in[i]);
142 if (test_bit(i, &dev->outurb_active_mask))
143 usb_kill_urb(dev->data_urbs_out[i]);
146 dev->outurb_active_mask = 0;
149 static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
151 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
152 debug("%s(%p)\n", __func__, substream);
153 substream->runtime->hw = dev->pcm_info;
154 snd_pcm_limit_hw_rates(substream->runtime);
155 return 0;
158 static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
160 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
162 debug("%s(%p)\n", __func__, substream);
163 if (all_substreams_zero(dev->sub_playback) &&
164 all_substreams_zero(dev->sub_capture)) {
165 /* when the last client has stopped streaming,
166 * all sample rates are allowed again */
167 stream_stop(dev);
168 dev->pcm_info.rates = dev->samplerates;
171 return 0;
174 static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
175 struct snd_pcm_hw_params *hw_params)
177 debug("%s(%p)\n", __func__, sub);
178 return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params));
181 static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
183 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
184 debug("%s(%p)\n", __func__, sub);
185 deactivate_substream(dev, sub);
186 return snd_pcm_lib_free_pages(sub);
189 /* this should probably go upstream */
190 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
191 #error "Change this table"
192 #endif
194 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
195 48000, 64000, 88200, 96000, 176400, 192000 };
197 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
199 int bytes_per_sample, bpp, ret, i;
200 int index = substream->number;
201 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
202 struct snd_pcm_runtime *runtime = substream->runtime;
204 debug("%s(%p)\n", __func__, substream);
206 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
207 dev->period_out_count[index] = BYTES_PER_SAMPLE + 1;
208 dev->audio_out_buf_pos[index] = BYTES_PER_SAMPLE + 1;
209 } else {
210 int in_pos = (dev->spec.data_alignment == 2) ? 0 : 2;
211 dev->period_in_count[index] = BYTES_PER_SAMPLE + in_pos;
212 dev->audio_in_buf_pos[index] = BYTES_PER_SAMPLE + in_pos;
215 if (dev->streaming)
216 return 0;
218 /* the first client that opens a stream defines the sample rate
219 * setting for all subsequent calls, until the last client closed. */
220 for (i=0; i < ARRAY_SIZE(rates); i++)
221 if (runtime->rate == rates[i])
222 dev->pcm_info.rates = 1 << i;
224 snd_pcm_limit_hw_rates(runtime);
226 bytes_per_sample = BYTES_PER_SAMPLE;
227 if (dev->spec.data_alignment == 2)
228 bytes_per_sample++;
230 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
231 * bytes_per_sample * CHANNELS_PER_STREAM * dev->n_streams;
233 if (bpp > MAX_ENDPOINT_SIZE)
234 bpp = MAX_ENDPOINT_SIZE;
236 ret = snd_usb_caiaq_set_audio_params(dev, runtime->rate,
237 runtime->sample_bits, bpp);
238 if (ret)
239 return ret;
241 ret = stream_start(dev);
242 if (ret)
243 return ret;
245 dev->output_running = 0;
246 wait_event_timeout(dev->prepare_wait_queue, dev->output_running, HZ);
247 if (!dev->output_running) {
248 stream_stop(dev);
249 return -EPIPE;
252 return 0;
255 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
257 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
259 switch (cmd) {
260 case SNDRV_PCM_TRIGGER_START:
261 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
262 activate_substream(dev, sub);
263 break;
264 case SNDRV_PCM_TRIGGER_STOP:
265 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
266 deactivate_substream(dev, sub);
267 break;
268 default:
269 return -EINVAL;
272 return 0;
275 static snd_pcm_uframes_t
276 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
278 int index = sub->number;
279 struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
280 snd_pcm_uframes_t ptr;
282 spin_lock(&dev->spinlock);
284 if (dev->input_panic || dev->output_panic)
285 ptr = SNDRV_PCM_POS_XRUN;
287 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
288 ptr = bytes_to_frames(sub->runtime,
289 dev->audio_out_buf_pos[index]);
290 else
291 ptr = bytes_to_frames(sub->runtime,
292 dev->audio_in_buf_pos[index]);
294 spin_unlock(&dev->spinlock);
295 return ptr;
298 /* operators for both playback and capture */
299 static struct snd_pcm_ops snd_usb_caiaq_ops = {
300 .open = snd_usb_caiaq_substream_open,
301 .close = snd_usb_caiaq_substream_close,
302 .ioctl = snd_pcm_lib_ioctl,
303 .hw_params = snd_usb_caiaq_pcm_hw_params,
304 .hw_free = snd_usb_caiaq_pcm_hw_free,
305 .prepare = snd_usb_caiaq_pcm_prepare,
306 .trigger = snd_usb_caiaq_pcm_trigger,
307 .pointer = snd_usb_caiaq_pcm_pointer
310 static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev,
311 struct snd_pcm_substream **subs)
313 int stream, pb, *cnt;
314 struct snd_pcm_substream *sub;
316 for (stream = 0; stream < dev->n_streams; stream++) {
317 sub = subs[stream];
318 if (!sub)
319 continue;
321 pb = snd_pcm_lib_period_bytes(sub);
322 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
323 &dev->period_out_count[stream] :
324 &dev->period_in_count[stream];
326 if (*cnt >= pb) {
327 snd_pcm_period_elapsed(sub);
328 *cnt %= pb;
333 static void read_in_urb_mode0(struct snd_usb_caiaqdev *dev,
334 const struct urb *urb,
335 const struct usb_iso_packet_descriptor *iso)
337 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
338 struct snd_pcm_substream *sub;
339 int stream, i;
341 if (all_substreams_zero(dev->sub_capture))
342 return;
344 for (i = 0; i < iso->actual_length;) {
345 for (stream = 0; stream < dev->n_streams; stream++, i++) {
346 sub = dev->sub_capture[stream];
347 if (sub) {
348 struct snd_pcm_runtime *rt = sub->runtime;
349 char *audio_buf = rt->dma_area;
350 int sz = frames_to_bytes(rt, rt->buffer_size);
351 audio_buf[dev->audio_in_buf_pos[stream]++]
352 = usb_buf[i];
353 dev->period_in_count[stream]++;
354 if (dev->audio_in_buf_pos[stream] == sz)
355 dev->audio_in_buf_pos[stream] = 0;
361 static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev,
362 const struct urb *urb,
363 const struct usb_iso_packet_descriptor *iso)
365 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
366 unsigned char check_byte;
367 struct snd_pcm_substream *sub;
368 int stream, i;
370 for (i = 0; i < iso->actual_length;) {
371 if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
372 for (stream = 0;
373 stream < dev->n_streams;
374 stream++, i++) {
375 if (dev->first_packet)
376 continue;
378 check_byte = MAKE_CHECKBYTE(dev, stream, i);
380 if ((usb_buf[i] & 0x3f) != check_byte)
381 dev->input_panic = 1;
383 if (usb_buf[i] & 0x80)
384 dev->output_panic = 1;
387 dev->first_packet = 0;
389 for (stream = 0; stream < dev->n_streams; stream++, i++) {
390 sub = dev->sub_capture[stream];
391 if (dev->input_panic)
392 usb_buf[i] = 0;
394 if (sub) {
395 struct snd_pcm_runtime *rt = sub->runtime;
396 char *audio_buf = rt->dma_area;
397 int sz = frames_to_bytes(rt, rt->buffer_size);
398 audio_buf[dev->audio_in_buf_pos[stream]++] =
399 usb_buf[i];
400 dev->period_in_count[stream]++;
401 if (dev->audio_in_buf_pos[stream] == sz)
402 dev->audio_in_buf_pos[stream] = 0;
408 static void read_in_urb(struct snd_usb_caiaqdev *dev,
409 const struct urb *urb,
410 const struct usb_iso_packet_descriptor *iso)
412 if (!dev->streaming)
413 return;
415 if (iso->actual_length < dev->bpp)
416 return;
418 switch (dev->spec.data_alignment) {
419 case 0:
420 read_in_urb_mode0(dev, urb, iso);
421 break;
422 case 2:
423 read_in_urb_mode2(dev, urb, iso);
424 break;
427 if ((dev->input_panic || dev->output_panic) && !dev->warned) {
428 debug("streaming error detected %s %s\n",
429 dev->input_panic ? "(input)" : "",
430 dev->output_panic ? "(output)" : "");
431 dev->warned = 1;
435 static void fill_out_urb(struct snd_usb_caiaqdev *dev,
436 struct urb *urb,
437 const struct usb_iso_packet_descriptor *iso)
439 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
440 struct snd_pcm_substream *sub;
441 int stream, i;
443 for (i = 0; i < iso->length;) {
444 for (stream = 0; stream < dev->n_streams; stream++, i++) {
445 sub = dev->sub_playback[stream];
446 if (sub) {
447 struct snd_pcm_runtime *rt = sub->runtime;
448 char *audio_buf = rt->dma_area;
449 int sz = frames_to_bytes(rt, rt->buffer_size);
450 usb_buf[i] =
451 audio_buf[dev->audio_out_buf_pos[stream]];
452 dev->period_out_count[stream]++;
453 dev->audio_out_buf_pos[stream]++;
454 if (dev->audio_out_buf_pos[stream] == sz)
455 dev->audio_out_buf_pos[stream] = 0;
456 } else
457 usb_buf[i] = 0;
460 /* fill in the check bytes */
461 if (dev->spec.data_alignment == 2 &&
462 i % (dev->n_streams * BYTES_PER_SAMPLE_USB) ==
463 (dev->n_streams * CHANNELS_PER_STREAM))
464 for (stream = 0; stream < dev->n_streams; stream++, i++)
465 usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i);
469 static void read_completed(struct urb *urb)
471 struct snd_usb_caiaq_cb_info *info = urb->context;
472 struct snd_usb_caiaqdev *dev;
473 struct urb *out = NULL;
474 int i, frame, len, send_it = 0, outframe = 0;
475 size_t offset = 0;
477 if (urb->status || !info)
478 return;
480 dev = info->dev;
482 if (!dev->streaming)
483 return;
485 /* find an unused output urb that is unused */
486 for (i = 0; i < N_URBS; i++)
487 if (test_and_set_bit(i, &dev->outurb_active_mask) == 0) {
488 out = dev->data_urbs_out[i];
489 break;
492 if (!out) {
493 log("Unable to find an output urb to use\n");
494 goto requeue;
497 /* read the recently received packet and send back one which has
498 * the same layout */
499 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
500 if (urb->iso_frame_desc[frame].status)
501 continue;
503 len = urb->iso_frame_desc[outframe].actual_length;
504 out->iso_frame_desc[outframe].length = len;
505 out->iso_frame_desc[outframe].actual_length = 0;
506 out->iso_frame_desc[outframe].offset = offset;
507 offset += len;
509 if (len > 0) {
510 spin_lock(&dev->spinlock);
511 fill_out_urb(dev, out, &out->iso_frame_desc[outframe]);
512 read_in_urb(dev, urb, &urb->iso_frame_desc[frame]);
513 spin_unlock(&dev->spinlock);
514 check_for_elapsed_periods(dev, dev->sub_playback);
515 check_for_elapsed_periods(dev, dev->sub_capture);
516 send_it = 1;
519 outframe++;
522 if (send_it) {
523 out->number_of_packets = outframe;
524 out->transfer_flags = URB_ISO_ASAP;
525 usb_submit_urb(out, GFP_ATOMIC);
526 } else {
527 struct snd_usb_caiaq_cb_info *oinfo = out->context;
528 clear_bit(oinfo->index, &dev->outurb_active_mask);
531 requeue:
532 /* re-submit inbound urb */
533 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
534 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
535 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
536 urb->iso_frame_desc[frame].actual_length = 0;
539 urb->number_of_packets = FRAMES_PER_URB;
540 urb->transfer_flags = URB_ISO_ASAP;
541 usb_submit_urb(urb, GFP_ATOMIC);
544 static void write_completed(struct urb *urb)
546 struct snd_usb_caiaq_cb_info *info = urb->context;
547 struct snd_usb_caiaqdev *dev = info->dev;
549 if (!dev->output_running) {
550 dev->output_running = 1;
551 wake_up(&dev->prepare_wait_queue);
554 clear_bit(info->index, &dev->outurb_active_mask);
557 static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret)
559 int i, frame;
560 struct urb **urbs;
561 struct usb_device *usb_dev = dev->chip.dev;
562 unsigned int pipe;
564 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
565 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
566 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
568 urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
569 if (!urbs) {
570 log("unable to kmalloc() urbs, OOM!?\n");
571 *ret = -ENOMEM;
572 return NULL;
575 for (i = 0; i < N_URBS; i++) {
576 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
577 if (!urbs[i]) {
578 log("unable to usb_alloc_urb(), OOM!?\n");
579 *ret = -ENOMEM;
580 return urbs;
583 urbs[i]->transfer_buffer =
584 kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
585 if (!urbs[i]->transfer_buffer) {
586 log("unable to kmalloc() transfer buffer, OOM!?\n");
587 *ret = -ENOMEM;
588 return urbs;
591 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
592 struct usb_iso_packet_descriptor *iso =
593 &urbs[i]->iso_frame_desc[frame];
595 iso->offset = BYTES_PER_FRAME * frame;
596 iso->length = BYTES_PER_FRAME;
599 urbs[i]->dev = usb_dev;
600 urbs[i]->pipe = pipe;
601 urbs[i]->transfer_buffer_length = FRAMES_PER_URB
602 * BYTES_PER_FRAME;
603 urbs[i]->context = &dev->data_cb_info[i];
604 urbs[i]->interval = 1;
605 urbs[i]->transfer_flags = URB_ISO_ASAP;
606 urbs[i]->number_of_packets = FRAMES_PER_URB;
607 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
608 read_completed : write_completed;
611 *ret = 0;
612 return urbs;
615 static void free_urbs(struct urb **urbs)
617 int i;
619 if (!urbs)
620 return;
622 for (i = 0; i < N_URBS; i++) {
623 if (!urbs[i])
624 continue;
626 usb_kill_urb(urbs[i]);
627 kfree(urbs[i]->transfer_buffer);
628 usb_free_urb(urbs[i]);
631 kfree(urbs);
634 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
636 int i, ret;
638 dev->n_audio_in = max(dev->spec.num_analog_audio_in,
639 dev->spec.num_digital_audio_in) /
640 CHANNELS_PER_STREAM;
641 dev->n_audio_out = max(dev->spec.num_analog_audio_out,
642 dev->spec.num_digital_audio_out) /
643 CHANNELS_PER_STREAM;
644 dev->n_streams = max(dev->n_audio_in, dev->n_audio_out);
646 debug("dev->n_audio_in = %d\n", dev->n_audio_in);
647 debug("dev->n_audio_out = %d\n", dev->n_audio_out);
648 debug("dev->n_streams = %d\n", dev->n_streams);
650 if (dev->n_streams > MAX_STREAMS) {
651 log("unable to initialize device, too many streams.\n");
652 return -EINVAL;
655 ret = snd_pcm_new(dev->chip.card, dev->product_name, 0,
656 dev->n_audio_out, dev->n_audio_in, &dev->pcm);
658 if (ret < 0) {
659 log("snd_pcm_new() returned %d\n", ret);
660 return ret;
663 dev->pcm->private_data = dev;
664 strlcpy(dev->pcm->name, dev->product_name, sizeof(dev->pcm->name));
666 memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
667 memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
669 memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware,
670 sizeof(snd_usb_caiaq_pcm_hardware));
672 /* setup samplerates */
673 dev->samplerates = dev->pcm_info.rates;
674 switch (dev->chip.usb_id) {
675 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
676 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
677 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
678 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
679 dev->samplerates |= SNDRV_PCM_RATE_192000;
680 /* fall thru */
681 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
682 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
683 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
684 dev->samplerates |= SNDRV_PCM_RATE_88200;
685 break;
688 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
689 &snd_usb_caiaq_ops);
690 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
691 &snd_usb_caiaq_ops);
693 snd_pcm_lib_preallocate_pages_for_all(dev->pcm,
694 SNDRV_DMA_TYPE_CONTINUOUS,
695 snd_dma_continuous_data(GFP_KERNEL),
696 MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
698 dev->data_cb_info =
699 kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
700 GFP_KERNEL);
702 if (!dev->data_cb_info)
703 return -ENOMEM;
705 dev->outurb_active_mask = 0;
706 BUILD_BUG_ON(N_URBS > (sizeof(dev->outurb_active_mask) * 8));
708 for (i = 0; i < N_URBS; i++) {
709 dev->data_cb_info[i].dev = dev;
710 dev->data_cb_info[i].index = i;
713 dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret);
714 if (ret < 0) {
715 kfree(dev->data_cb_info);
716 free_urbs(dev->data_urbs_in);
717 return ret;
720 dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
721 if (ret < 0) {
722 kfree(dev->data_cb_info);
723 free_urbs(dev->data_urbs_in);
724 free_urbs(dev->data_urbs_out);
725 return ret;
728 return 0;
731 void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev)
733 debug("%s(%p)\n", __func__, dev);
734 stream_stop(dev);
735 free_urbs(dev->data_urbs_in);
736 free_urbs(dev->data_urbs_out);
737 kfree(dev->data_cb_info);