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