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