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