[ALSA] usb/caiaq: decrease period_bytes_min
[linux-2.6] / sound / usb / caiaq / caiaq-audio.c
1 /*
2  *   Copyright (c) 2006,2007 Daniel Mack, Karsten Wiese
3  *
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.
8  *
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.
13  *
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
17 */
18
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/interrupt.h>
23 #include <linux/usb.h>
24 #include <linux/spinlock.h>
25 #include <sound/core.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
28 #include <sound/rawmidi.h>
29 #include <linux/input.h>
30
31 #include "caiaq-device.h"
32 #include "caiaq-audio.h"
33
34 #define N_URBS                  32
35 #define CLOCK_DRIFT_TOLERANCE   5
36 #define FRAMES_PER_URB          8
37 #define BYTES_PER_FRAME         512
38 #define CHANNELS_PER_STREAM     2
39 #define BYTES_PER_SAMPLE        3
40 #define BYTES_PER_SAMPLE_USB    4
41 #define MAX_BUFFER_SIZE         (128*1024)
42                                  
43 #define ENDPOINT_CAPTURE        2
44 #define ENDPOINT_PLAYBACK       6
45
46 #define MAKE_CHECKBYTE(dev,stream,i) \
47         (stream << 1) | (~(i / (dev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
48
49 static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
50         .info           = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 
51                            SNDRV_PCM_INFO_BLOCK_TRANSFER),
52         .formats        = SNDRV_PCM_FMTBIT_S24_3BE,
53         .rates          = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | 
54                            SNDRV_PCM_RATE_96000),
55         .rate_min       = 44100,
56         .rate_max       = 0, /* will overwrite later */
57         .channels_min   = CHANNELS_PER_STREAM,
58         .channels_max   = CHANNELS_PER_STREAM,
59         .buffer_bytes_max = MAX_BUFFER_SIZE,
60         .period_bytes_min = 128,
61         .period_bytes_max = MAX_BUFFER_SIZE,
62         .periods_min    = 1,
63         .periods_max    = 1024,
64 };
65
66 static void
67 activate_substream(struct snd_usb_caiaqdev *dev,
68                    struct snd_pcm_substream *sub)
69 {
70         if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
71                 dev->sub_playback[sub->number] = sub;
72         else
73                 dev->sub_capture[sub->number] = sub;
74 }
75
76 static void 
77 deactivate_substream(struct snd_usb_caiaqdev *dev,
78                      struct snd_pcm_substream *sub)
79 {
80         if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
81                 dev->sub_playback[sub->number] = NULL;
82         else
83                 dev->sub_capture[sub->number] = NULL;
84 }
85
86 static int
87 all_substreams_zero(struct snd_pcm_substream **subs)
88 {
89         int i;
90         for (i = 0; i < MAX_STREAMS; i++)
91                 if (subs[i] != NULL)
92                         return 0;
93         return 1;
94 }
95
96 static int stream_start(struct snd_usb_caiaqdev *dev)
97 {
98         int i, ret;
99
100         debug("stream_start(%p)\n", dev);
101         spin_lock_irq(&dev->spinlock);
102         if (dev->streaming) {
103                 spin_unlock_irq(&dev->spinlock);
104                 return -EINVAL;
105         }
106
107         dev->input_panic = 0;
108         dev->output_panic = 0;
109         dev->first_packet = 1;
110         dev->streaming = 1;
111
112         for (i = 0; i < N_URBS; i++) {
113                 ret = usb_submit_urb(dev->data_urbs_in[i], GFP_ATOMIC);
114                 if (ret) {
115                         log("unable to trigger initial read #%d! (ret = %d)\n",
116                                 i, ret);
117                         dev->streaming = 0;
118                         spin_unlock_irq(&dev->spinlock);
119                         return -EPIPE;
120                 }
121         }
122         
123         spin_unlock_irq(&dev->spinlock);
124         return 0;
125 }
126
127 static void stream_stop(struct snd_usb_caiaqdev *dev)
128 {
129         int i;
130         
131         debug("stream_stop(%p)\n", dev);
132         if (!dev->streaming)
133                 return;
134         
135         dev->streaming = 0;
136         for (i = 0; i < N_URBS; i++) {
137                 usb_unlink_urb(dev->data_urbs_in[i]);
138                 usb_unlink_urb(dev->data_urbs_out[i]);
139         }
140 }
141
142 static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
143 {
144         struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
145         debug("snd_usb_caiaq_substream_open(%p)\n", substream);
146         substream->runtime->hw = dev->pcm_info;
147         snd_pcm_limit_hw_rates(substream->runtime);
148         return 0;
149 }
150
151 static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
152 {
153         struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
154
155         debug("snd_usb_caiaq_substream_close(%p)\n", substream);
156         if (all_substreams_zero(dev->sub_playback) &&
157             all_substreams_zero(dev->sub_capture)) {
158                 /* when the last client has stopped streaming, 
159                  * all sample rates are allowed again */
160                 stream_stop(dev);
161                 dev->pcm_info.rates = dev->samplerates;
162         }
163         
164         return 0;
165 }
166
167 static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
168                                         struct snd_pcm_hw_params *hw_params)
169 {
170         debug("snd_usb_caiaq_pcm_hw_params(%p)\n", sub);
171         return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params));
172 }
173
174 static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
175 {
176         struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
177         debug("snd_usb_caiaq_pcm_hw_free(%p)\n", sub);
178         spin_lock_irq(&dev->spinlock);
179         deactivate_substream(dev, sub);
180         spin_unlock_irq(&dev->spinlock);
181         return snd_pcm_lib_free_pages(sub);
182 }
183
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"
187 #endif
188
189 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
190                                  48000, 64000, 88200, 96000, 176400, 192000 };
191
192 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
193 {
194         int bytes_per_sample, bpp, ret, i;
195         int index = substream->number;
196         struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(substream);
197         struct snd_pcm_runtime *runtime = substream->runtime;
198
199         debug("snd_usb_caiaq_pcm_prepare(%p)\n", substream);
200         
201         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
202                 dev->audio_out_buf_pos[index] = BYTES_PER_SAMPLE + 1;
203         else
204                 dev->audio_in_buf_pos[index] = 0;
205         
206         if (dev->streaming)
207                 return 0;
208         
209         /* the first client that opens a stream defines the sample rate
210          * setting for all subsequent calls, until the last client closed. */
211         for (i=0; i < ARRAY_SIZE(rates); i++)
212                 if (runtime->rate == rates[i])
213                         dev->pcm_info.rates = 1 << i;
214         
215         snd_pcm_limit_hw_rates(runtime);
216
217         bytes_per_sample = BYTES_PER_SAMPLE;
218         if (dev->spec.data_alignment == 2)
219                 bytes_per_sample++;
220         
221         bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
222                 * bytes_per_sample * CHANNELS_PER_STREAM * dev->n_streams;
223         
224         ret = snd_usb_caiaq_set_audio_params(dev, runtime->rate,
225                                              runtime->sample_bits, bpp);
226         if (ret)
227                 return ret;
228
229         ret = stream_start(dev);
230         if (ret)
231                 return ret;
232         
233         dev->output_running = 0;
234         wait_event_timeout(dev->prepare_wait_queue, dev->output_running, HZ);
235         if (!dev->output_running) {
236                 stream_stop(dev);
237                 return -EPIPE;
238         }
239
240         return 0;
241 }
242
243 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
244 {
245         struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
246
247         switch (cmd) {
248         case SNDRV_PCM_TRIGGER_START:
249         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
250                 spin_lock(&dev->spinlock);
251                 activate_substream(dev, sub);
252                 spin_unlock(&dev->spinlock);
253                 break;
254         case SNDRV_PCM_TRIGGER_STOP:
255         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
256                 spin_lock(&dev->spinlock);
257                 deactivate_substream(dev, sub);
258                 spin_unlock(&dev->spinlock);
259                 break;
260         default:
261                 return -EINVAL;
262         }
263
264         return 0;
265 }
266
267 static snd_pcm_uframes_t
268 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
269 {
270         int index = sub->number;
271         struct snd_usb_caiaqdev *dev = snd_pcm_substream_chip(sub);
272
273         if (dev->input_panic || dev->output_panic)
274                 return SNDRV_PCM_POS_XRUN;
275
276         if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
277                 return bytes_to_frames(sub->runtime, 
278                                         dev->audio_out_buf_pos[index]);
279         else
280                 return bytes_to_frames(sub->runtime,
281                                         dev->audio_in_buf_pos[index]);
282 }
283
284 /* operators for both playback and capture */
285 static struct snd_pcm_ops snd_usb_caiaq_ops = {
286         .open =         snd_usb_caiaq_substream_open,
287         .close =        snd_usb_caiaq_substream_close,
288         .ioctl =        snd_pcm_lib_ioctl,
289         .hw_params =    snd_usb_caiaq_pcm_hw_params,
290         .hw_free =      snd_usb_caiaq_pcm_hw_free,
291         .prepare =      snd_usb_caiaq_pcm_prepare,
292         .trigger =      snd_usb_caiaq_pcm_trigger,
293         .pointer =      snd_usb_caiaq_pcm_pointer
294 };
295         
296 static void check_for_elapsed_periods(struct snd_usb_caiaqdev *dev,
297                                       struct snd_pcm_substream **subs)
298 {
299         int stream, pb, *cnt;
300         struct snd_pcm_substream *sub;
301
302         for (stream = 0; stream < dev->n_streams; stream++) {
303                 sub = subs[stream];
304                 if (!sub)
305                         continue;
306
307                 pb = frames_to_bytes(sub->runtime, 
308                                      sub->runtime->period_size);
309                 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
310                                         &dev->period_out_count[stream] :
311                                         &dev->period_in_count[stream];
312
313                 if (*cnt >= pb) {
314                         snd_pcm_period_elapsed(sub);
315                         *cnt %= pb;
316                 }
317         }
318 }
319
320 static void read_in_urb_mode0(struct snd_usb_caiaqdev *dev,
321                               const struct urb *urb,
322                               const struct usb_iso_packet_descriptor *iso)
323 {
324         unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
325         struct snd_pcm_substream *sub;
326         int stream, i;
327
328         if (all_substreams_zero(dev->sub_capture))
329                 return;
330
331         spin_lock(&dev->spinlock);
332         
333         for (i = 0; i < iso->actual_length;) {
334                 for (stream = 0; stream < dev->n_streams; stream++, i++) {
335                         sub = dev->sub_capture[stream];
336                         if (sub) {
337                                 struct snd_pcm_runtime *rt = sub->runtime;
338                                 char *audio_buf = rt->dma_area;
339                                 int sz = frames_to_bytes(rt, rt->buffer_size);
340                                 audio_buf[dev->audio_in_buf_pos[stream]++] 
341                                         = usb_buf[i];
342                                 dev->period_in_count[stream]++;
343                                 if (dev->audio_in_buf_pos[stream] == sz)
344                                         dev->audio_in_buf_pos[stream] = 0;
345                         }
346                 }
347         }
348         
349         spin_unlock(&dev->spinlock);
350 }
351
352 static void read_in_urb_mode2(struct snd_usb_caiaqdev *dev,
353                               const struct urb *urb,
354                               const struct usb_iso_packet_descriptor *iso)
355 {
356         unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
357         unsigned char check_byte;
358         struct snd_pcm_substream *sub;
359         int stream, i;
360
361         spin_lock(&dev->spinlock);
362         
363         for (i = 0; i < iso->actual_length;) {
364                 if (i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
365                         for (stream = 0; 
366                              stream < dev->n_streams; 
367                              stream++, i++) {
368                                 if (dev->first_packet)
369                                         continue;
370
371                                 check_byte = MAKE_CHECKBYTE(dev, stream, i);
372                                 
373                                 if ((usb_buf[i] & 0x3f) != check_byte)
374                                         dev->input_panic = 1;
375
376                                 if (usb_buf[i] & 0x80)
377                                         dev->output_panic = 1;
378                         }
379                 }
380                 dev->first_packet = 0;
381
382                 for (stream = 0; stream < dev->n_streams; stream++, i++) {
383                         sub = dev->sub_capture[stream];
384                         if (sub) {
385                                 struct snd_pcm_runtime *rt = sub->runtime;
386                                 char *audio_buf = rt->dma_area;
387                                 int sz = frames_to_bytes(rt, rt->buffer_size);
388                                 audio_buf[dev->audio_in_buf_pos[stream]++] =
389                                         usb_buf[i];
390                                 dev->period_in_count[stream]++;
391                                 if (dev->audio_in_buf_pos[stream] == sz)
392                                         dev->audio_in_buf_pos[stream] = 0;
393                         }
394                 }
395         }
396
397         spin_unlock(&dev->spinlock);
398 }
399
400 static void read_in_urb(struct snd_usb_caiaqdev *dev,
401                         const struct urb *urb,
402                         const struct usb_iso_packet_descriptor *iso)
403 {
404         if (!dev->streaming)
405                 return;
406
407         switch (dev->spec.data_alignment) {
408         case 0:
409                 read_in_urb_mode0(dev, urb, iso);
410                 break;
411         case 2:
412                 read_in_urb_mode2(dev, urb, iso);
413                 break;
414         }
415
416         if (dev->input_panic || dev->output_panic) {
417                 debug("streaming error detected %s %s\n", 
418                                 dev->input_panic ? "(input)" : "",
419                                 dev->output_panic ? "(output)" : "");
420         }
421
422         check_for_elapsed_periods(dev, dev->sub_capture);
423 }
424
425 static void fill_out_urb(struct snd_usb_caiaqdev *dev, 
426                          struct urb *urb, 
427                          const struct usb_iso_packet_descriptor *iso)
428 {
429         unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
430         struct snd_pcm_substream *sub;
431         int stream, i;
432
433         spin_lock(&dev->spinlock);
434         
435         for (i = 0; i < iso->length;) {
436                 for (stream = 0; stream < dev->n_streams; stream++, i++) {
437                         sub = dev->sub_playback[stream];
438                         if (sub) {
439                                 struct snd_pcm_runtime *rt = sub->runtime;
440                                 char *audio_buf = rt->dma_area;
441                                 int sz = frames_to_bytes(rt, rt->buffer_size);
442                                 usb_buf[i] =
443                                         audio_buf[dev->audio_out_buf_pos[stream]];
444                                 dev->period_out_count[stream]++;
445                                 dev->audio_out_buf_pos[stream]++;
446                                 if (dev->audio_out_buf_pos[stream] == sz)
447                                         dev->audio_out_buf_pos[stream] = 0;
448                         } else
449                                 usb_buf[i] = 0;
450                 }
451
452                 /* fill in the check bytes */
453                 if (dev->spec.data_alignment == 2 &&
454                     i % (dev->n_streams * BYTES_PER_SAMPLE_USB) == 
455                         (dev->n_streams * CHANNELS_PER_STREAM))
456                     for (stream = 0; stream < dev->n_streams; stream++, i++)
457                         usb_buf[i] = MAKE_CHECKBYTE(dev, stream, i);
458         }
459
460         spin_unlock(&dev->spinlock);
461         check_for_elapsed_periods(dev, dev->sub_playback);
462 }
463
464 static void read_completed(struct urb *urb)
465 {
466         struct snd_usb_caiaq_cb_info *info = urb->context; 
467         struct snd_usb_caiaqdev *dev;
468         struct urb *out;
469         int frame, len, send_it = 0, outframe = 0;
470
471         if (urb->status || !info)
472                 return;
473
474         dev = info->dev;
475         if (!dev->streaming)
476                 return;
477
478         out = dev->data_urbs_out[info->index];
479
480         /* read the recently received packet and send back one which has
481          * the same layout */
482         for (frame = 0; frame < FRAMES_PER_URB; frame++) {
483                 if (urb->iso_frame_desc[frame].status)
484                         continue;
485
486                 len = urb->iso_frame_desc[outframe].actual_length;
487                 out->iso_frame_desc[outframe].length = len;
488                 out->iso_frame_desc[outframe].actual_length = 0;
489                 out->iso_frame_desc[outframe].offset = BYTES_PER_FRAME * frame;
490                 
491                 if (len > 0) {
492                         fill_out_urb(dev, out, &out->iso_frame_desc[outframe]);
493                         read_in_urb(dev, urb, &urb->iso_frame_desc[frame]);
494                         send_it = 1;
495                 }
496
497                 outframe++;
498         }
499
500         if (send_it) {
501                 out->number_of_packets = FRAMES_PER_URB;
502                 out->transfer_flags = URB_ISO_ASAP;
503                 usb_submit_urb(out, GFP_ATOMIC);
504         }
505         
506         /* re-submit inbound urb */
507         for (frame = 0; frame < FRAMES_PER_URB; frame++) {
508                 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
509                 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
510                 urb->iso_frame_desc[frame].actual_length = 0;
511         }
512         
513         urb->number_of_packets = FRAMES_PER_URB;
514         urb->transfer_flags = URB_ISO_ASAP;
515         usb_submit_urb(urb, GFP_ATOMIC);
516 }
517
518 static void write_completed(struct urb *urb)
519 {
520         struct snd_usb_caiaq_cb_info *info = urb->context;
521         struct snd_usb_caiaqdev *dev = info->dev;
522
523         if (!dev->output_running) {
524                 dev->output_running = 1;
525                 wake_up(&dev->prepare_wait_queue);
526         }
527 }
528
529 static struct urb **alloc_urbs(struct snd_usb_caiaqdev *dev, int dir, int *ret)
530 {
531         int i, frame;
532         struct urb **urbs;
533         struct usb_device *usb_dev = dev->chip.dev;
534         unsigned int pipe;
535
536         pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ? 
537                 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
538                 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
539
540         urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
541         if (!urbs) {
542                 log("unable to kmalloc() urbs, OOM!?\n");
543                 *ret = -ENOMEM;
544                 return NULL;
545         }
546
547         for (i = 0; i < N_URBS; i++) {
548                 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
549                 if (!urbs[i]) {
550                         log("unable to usb_alloc_urb(), OOM!?\n");
551                         *ret = -ENOMEM;
552                         return urbs;
553                 }
554
555                 urbs[i]->transfer_buffer = 
556                         kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
557                 if (!urbs[i]->transfer_buffer) {
558                         log("unable to kmalloc() transfer buffer, OOM!?\n");
559                         *ret = -ENOMEM;
560                         return urbs;
561                 }
562                 
563                 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
564                         struct usb_iso_packet_descriptor *iso = 
565                                 &urbs[i]->iso_frame_desc[frame];
566                         
567                         iso->offset = BYTES_PER_FRAME * frame;
568                         iso->length = BYTES_PER_FRAME;
569                 }
570                 
571                 urbs[i]->dev = usb_dev;
572                 urbs[i]->pipe = pipe;
573                 urbs[i]->transfer_buffer_length = FRAMES_PER_URB 
574                                                 * BYTES_PER_FRAME;
575                 urbs[i]->context = &dev->data_cb_info[i];
576                 urbs[i]->interval = 1;
577                 urbs[i]->transfer_flags = URB_ISO_ASAP;
578                 urbs[i]->number_of_packets = FRAMES_PER_URB;
579                 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
580                                         read_completed : write_completed;
581         }
582
583         *ret = 0;
584         return urbs;
585 }
586
587 static void free_urbs(struct urb **urbs)
588 {
589         int i;
590
591         if (!urbs)
592                 return;
593
594         for (i = 0; i < N_URBS; i++) {
595                 if (!urbs[i])
596                         continue;
597                 
598                 usb_kill_urb(urbs[i]);
599                 kfree(urbs[i]->transfer_buffer);
600                 usb_free_urb(urbs[i]);
601         }
602
603         kfree(urbs);
604 }
605
606 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *dev)
607 {
608         int i, ret;
609
610         dev->n_audio_in  = max(dev->spec.num_analog_audio_in, 
611                                dev->spec.num_digital_audio_in) / 
612                                 CHANNELS_PER_STREAM;
613         dev->n_audio_out = max(dev->spec.num_analog_audio_out,
614                                dev->spec.num_digital_audio_out) / 
615                                 CHANNELS_PER_STREAM;
616         dev->n_streams = max(dev->n_audio_in, dev->n_audio_out);
617
618         debug("dev->n_audio_in = %d\n", dev->n_audio_in);
619         debug("dev->n_audio_out = %d\n", dev->n_audio_out);
620         debug("dev->n_streams = %d\n", dev->n_streams);
621
622         if (dev->n_streams > MAX_STREAMS) {
623                 log("unable to initialize device, too many streams.\n");
624                 return -EINVAL;
625         }
626
627         ret = snd_pcm_new(dev->chip.card, dev->product_name, 0, 
628                         dev->n_audio_out, dev->n_audio_in, &dev->pcm);
629
630         if (ret < 0) {
631                 log("snd_pcm_new() returned %d\n", ret);
632                 return ret;
633         }
634
635         dev->pcm->private_data = dev;
636         strcpy(dev->pcm->name, dev->product_name);
637
638         memset(dev->sub_playback, 0, sizeof(dev->sub_playback));
639         memset(dev->sub_capture, 0, sizeof(dev->sub_capture));
640         
641         memcpy(&dev->pcm_info, &snd_usb_caiaq_pcm_hardware,
642                         sizeof(snd_usb_caiaq_pcm_hardware));
643
644         /* setup samplerates */
645         dev->samplerates = dev->pcm_info.rates;
646         switch (dev->chip.usb_id) {
647         case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
648         case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
649                 dev->samplerates |= SNDRV_PCM_RATE_88200;
650                 dev->samplerates |= SNDRV_PCM_RATE_192000;
651                 break;
652         case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
653                 dev->samplerates |= SNDRV_PCM_RATE_88200;
654                 break;
655         }
656
657         snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 
658                                 &snd_usb_caiaq_ops);
659         snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 
660                                 &snd_usb_caiaq_ops);
661
662         snd_pcm_lib_preallocate_pages_for_all(dev->pcm,
663                                         SNDRV_DMA_TYPE_CONTINUOUS,
664                                         snd_dma_continuous_data(GFP_KERNEL),
665                                         MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
666
667         dev->data_cb_info =
668                 kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS, 
669                                         GFP_KERNEL);
670
671         if (!dev->data_cb_info)
672                 return -ENOMEM;
673
674         for (i = 0; i < N_URBS; i++) {
675                 dev->data_cb_info[i].dev = dev;
676                 dev->data_cb_info[i].index = i;
677         }
678         
679         dev->data_urbs_in = alloc_urbs(dev, SNDRV_PCM_STREAM_CAPTURE, &ret);
680         if (ret < 0) {
681                 kfree(dev->data_cb_info);
682                 free_urbs(dev->data_urbs_in);
683                 return ret;
684         }
685         
686         dev->data_urbs_out = alloc_urbs(dev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
687         if (ret < 0) {
688                 kfree(dev->data_cb_info);
689                 free_urbs(dev->data_urbs_in);
690                 free_urbs(dev->data_urbs_out);
691                 return ret;
692         }
693
694         return 0;
695 }
696
697 void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *dev)
698 {
699         debug("snd_usb_caiaq_audio_free (%p)\n", dev);
700         stream_stop(dev);
701         free_urbs(dev->data_urbs_in);
702         free_urbs(dev->data_urbs_out);
703         kfree(dev->data_cb_info);
704 }
705