xref: /openbmc/linux/sound/usb/caiaq/audio.c (revision f35e839a)
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/device.h>
20 #include <linux/spinlock.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/usb.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 
27 #include "device.h"
28 #include "audio.h"
29 
30 #define N_URBS			32
31 #define CLOCK_DRIFT_TOLERANCE	5
32 #define FRAMES_PER_URB		8
33 #define BYTES_PER_FRAME		512
34 #define CHANNELS_PER_STREAM	2
35 #define BYTES_PER_SAMPLE	3
36 #define BYTES_PER_SAMPLE_USB	4
37 #define MAX_BUFFER_SIZE		(128*1024)
38 #define MAX_ENDPOINT_SIZE	512
39 
40 #define ENDPOINT_CAPTURE	2
41 #define ENDPOINT_PLAYBACK	6
42 
43 #define MAKE_CHECKBYTE(cdev,stream,i) \
44 	(stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
45 
46 static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
47 	.info 		= (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
48 			   SNDRV_PCM_INFO_BLOCK_TRANSFER),
49 	.formats 	= SNDRV_PCM_FMTBIT_S24_3BE,
50 	.rates 		= (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
51 			   SNDRV_PCM_RATE_96000),
52 	.rate_min	= 44100,
53 	.rate_max	= 0, /* will overwrite later */
54 	.channels_min	= CHANNELS_PER_STREAM,
55 	.channels_max	= CHANNELS_PER_STREAM,
56 	.buffer_bytes_max = MAX_BUFFER_SIZE,
57 	.period_bytes_min = 128,
58 	.period_bytes_max = MAX_BUFFER_SIZE,
59 	.periods_min	= 1,
60 	.periods_max	= 1024,
61 };
62 
63 static void
64 activate_substream(struct snd_usb_caiaqdev *cdev,
65 	           struct snd_pcm_substream *sub)
66 {
67 	spin_lock(&cdev->spinlock);
68 
69 	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
70 		cdev->sub_playback[sub->number] = sub;
71 	else
72 		cdev->sub_capture[sub->number] = sub;
73 
74 	spin_unlock(&cdev->spinlock);
75 }
76 
77 static void
78 deactivate_substream(struct snd_usb_caiaqdev *cdev,
79 		     struct snd_pcm_substream *sub)
80 {
81 	unsigned long flags;
82 	spin_lock_irqsave(&cdev->spinlock, flags);
83 
84 	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
85 		cdev->sub_playback[sub->number] = NULL;
86 	else
87 		cdev->sub_capture[sub->number] = NULL;
88 
89 	spin_unlock_irqrestore(&cdev->spinlock, flags);
90 }
91 
92 static int
93 all_substreams_zero(struct snd_pcm_substream **subs)
94 {
95 	int i;
96 	for (i = 0; i < MAX_STREAMS; i++)
97 		if (subs[i] != NULL)
98 			return 0;
99 	return 1;
100 }
101 
102 static int stream_start(struct snd_usb_caiaqdev *cdev)
103 {
104 	int i, ret;
105 	struct device *dev = caiaqdev_to_dev(cdev);
106 
107 	dev_dbg(dev, "%s(%p)\n", __func__, cdev);
108 
109 	if (cdev->streaming)
110 		return -EINVAL;
111 
112 	memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
113 	memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
114 	cdev->input_panic = 0;
115 	cdev->output_panic = 0;
116 	cdev->first_packet = 4;
117 	cdev->streaming = 1;
118 	cdev->warned = 0;
119 
120 	for (i = 0; i < N_URBS; i++) {
121 		ret = usb_submit_urb(cdev->data_urbs_in[i], GFP_ATOMIC);
122 		if (ret) {
123 			dev_err(dev, "unable to trigger read #%d! (ret %d)\n",
124 				i, ret);
125 			cdev->streaming = 0;
126 			return -EPIPE;
127 		}
128 	}
129 
130 	return 0;
131 }
132 
133 static void stream_stop(struct snd_usb_caiaqdev *cdev)
134 {
135 	int i;
136 	struct device *dev = caiaqdev_to_dev(cdev);
137 
138 	dev_dbg(dev, "%s(%p)\n", __func__, cdev);
139 	if (!cdev->streaming)
140 		return;
141 
142 	cdev->streaming = 0;
143 
144 	for (i = 0; i < N_URBS; i++) {
145 		usb_kill_urb(cdev->data_urbs_in[i]);
146 
147 		if (test_bit(i, &cdev->outurb_active_mask))
148 			usb_kill_urb(cdev->data_urbs_out[i]);
149 	}
150 
151 	cdev->outurb_active_mask = 0;
152 }
153 
154 static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
155 {
156 	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
157 	struct device *dev = caiaqdev_to_dev(cdev);
158 
159 	dev_dbg(dev, "%s(%p)\n", __func__, substream);
160 	substream->runtime->hw = cdev->pcm_info;
161 	snd_pcm_limit_hw_rates(substream->runtime);
162 
163 	return 0;
164 }
165 
166 static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
167 {
168 	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
169 	struct device *dev = caiaqdev_to_dev(cdev);
170 
171 	dev_dbg(dev, "%s(%p)\n", __func__, substream);
172 	if (all_substreams_zero(cdev->sub_playback) &&
173 	    all_substreams_zero(cdev->sub_capture)) {
174 		/* when the last client has stopped streaming,
175 		 * all sample rates are allowed again */
176 		stream_stop(cdev);
177 		cdev->pcm_info.rates = cdev->samplerates;
178 	}
179 
180 	return 0;
181 }
182 
183 static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
184 				       struct snd_pcm_hw_params *hw_params)
185 {
186 	return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params));
187 }
188 
189 static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
190 {
191 	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
192 	deactivate_substream(cdev, sub);
193 	return snd_pcm_lib_free_pages(sub);
194 }
195 
196 /* this should probably go upstream */
197 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
198 #error "Change this table"
199 #endif
200 
201 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
202 				48000, 64000, 88200, 96000, 176400, 192000 };
203 
204 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
205 {
206 	int bytes_per_sample, bpp, ret, i;
207 	int index = substream->number;
208 	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
209 	struct snd_pcm_runtime *runtime = substream->runtime;
210 	struct device *dev = caiaqdev_to_dev(cdev);
211 
212 	dev_dbg(dev, "%s(%p)\n", __func__, substream);
213 
214 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
215 		int out_pos;
216 
217 		switch (cdev->spec.data_alignment) {
218 		case 0:
219 		case 2:
220 			out_pos = BYTES_PER_SAMPLE + 1;
221 			break;
222 		case 3:
223 		default:
224 			out_pos = 0;
225 			break;
226 		}
227 
228 		cdev->period_out_count[index] = out_pos;
229 		cdev->audio_out_buf_pos[index] = out_pos;
230 	} else {
231 		int in_pos;
232 
233 		switch (cdev->spec.data_alignment) {
234 		case 0:
235 			in_pos = BYTES_PER_SAMPLE + 2;
236 			break;
237 		case 2:
238 			in_pos = BYTES_PER_SAMPLE;
239 			break;
240 		case 3:
241 		default:
242 			in_pos = 0;
243 			break;
244 		}
245 
246 		cdev->period_in_count[index] = in_pos;
247 		cdev->audio_in_buf_pos[index] = in_pos;
248 	}
249 
250 	if (cdev->streaming)
251 		return 0;
252 
253 	/* the first client that opens a stream defines the sample rate
254 	 * setting for all subsequent calls, until the last client closed. */
255 	for (i=0; i < ARRAY_SIZE(rates); i++)
256 		if (runtime->rate == rates[i])
257 			cdev->pcm_info.rates = 1 << i;
258 
259 	snd_pcm_limit_hw_rates(runtime);
260 
261 	bytes_per_sample = BYTES_PER_SAMPLE;
262 	if (cdev->spec.data_alignment >= 2)
263 		bytes_per_sample++;
264 
265 	bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
266 		* bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
267 
268 	if (bpp > MAX_ENDPOINT_SIZE)
269 		bpp = MAX_ENDPOINT_SIZE;
270 
271 	ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
272 					     runtime->sample_bits, bpp);
273 	if (ret)
274 		return ret;
275 
276 	ret = stream_start(cdev);
277 	if (ret)
278 		return ret;
279 
280 	cdev->output_running = 0;
281 	wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ);
282 	if (!cdev->output_running) {
283 		stream_stop(cdev);
284 		return -EPIPE;
285 	}
286 
287 	return 0;
288 }
289 
290 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
291 {
292 	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
293 	struct device *dev = caiaqdev_to_dev(cdev);
294 
295 	dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd);
296 
297 	switch (cmd) {
298 	case SNDRV_PCM_TRIGGER_START:
299 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
300 		activate_substream(cdev, sub);
301 		break;
302 	case SNDRV_PCM_TRIGGER_STOP:
303 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
304 		deactivate_substream(cdev, sub);
305 		break;
306 	default:
307 		return -EINVAL;
308 	}
309 
310 	return 0;
311 }
312 
313 static snd_pcm_uframes_t
314 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
315 {
316 	int index = sub->number;
317 	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
318 	snd_pcm_uframes_t ptr;
319 
320 	spin_lock(&cdev->spinlock);
321 
322 	if (cdev->input_panic || cdev->output_panic) {
323 		ptr = SNDRV_PCM_POS_XRUN;
324 		goto unlock;
325 	}
326 
327 	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
328 		ptr = bytes_to_frames(sub->runtime,
329 					cdev->audio_out_buf_pos[index]);
330 	else
331 		ptr = bytes_to_frames(sub->runtime,
332 					cdev->audio_in_buf_pos[index]);
333 
334 unlock:
335 	spin_unlock(&cdev->spinlock);
336 	return ptr;
337 }
338 
339 /* operators for both playback and capture */
340 static struct snd_pcm_ops snd_usb_caiaq_ops = {
341 	.open =		snd_usb_caiaq_substream_open,
342 	.close =	snd_usb_caiaq_substream_close,
343 	.ioctl =	snd_pcm_lib_ioctl,
344 	.hw_params =	snd_usb_caiaq_pcm_hw_params,
345 	.hw_free =	snd_usb_caiaq_pcm_hw_free,
346 	.prepare =	snd_usb_caiaq_pcm_prepare,
347 	.trigger =	snd_usb_caiaq_pcm_trigger,
348 	.pointer =	snd_usb_caiaq_pcm_pointer
349 };
350 
351 static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
352 				      struct snd_pcm_substream **subs)
353 {
354 	int stream, pb, *cnt;
355 	struct snd_pcm_substream *sub;
356 
357 	for (stream = 0; stream < cdev->n_streams; stream++) {
358 		sub = subs[stream];
359 		if (!sub)
360 			continue;
361 
362 		pb = snd_pcm_lib_period_bytes(sub);
363 		cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
364 					&cdev->period_out_count[stream] :
365 					&cdev->period_in_count[stream];
366 
367 		if (*cnt >= pb) {
368 			snd_pcm_period_elapsed(sub);
369 			*cnt %= pb;
370 		}
371 	}
372 }
373 
374 static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
375 			      const struct urb *urb,
376 			      const struct usb_iso_packet_descriptor *iso)
377 {
378 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
379 	struct snd_pcm_substream *sub;
380 	int stream, i;
381 
382 	if (all_substreams_zero(cdev->sub_capture))
383 		return;
384 
385 	for (i = 0; i < iso->actual_length;) {
386 		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
387 			sub = cdev->sub_capture[stream];
388 			if (sub) {
389 				struct snd_pcm_runtime *rt = sub->runtime;
390 				char *audio_buf = rt->dma_area;
391 				int sz = frames_to_bytes(rt, rt->buffer_size);
392 				audio_buf[cdev->audio_in_buf_pos[stream]++]
393 					= usb_buf[i];
394 				cdev->period_in_count[stream]++;
395 				if (cdev->audio_in_buf_pos[stream] == sz)
396 					cdev->audio_in_buf_pos[stream] = 0;
397 			}
398 		}
399 	}
400 }
401 
402 static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
403 			      const struct urb *urb,
404 			      const struct usb_iso_packet_descriptor *iso)
405 {
406 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
407 	unsigned char check_byte;
408 	struct snd_pcm_substream *sub;
409 	int stream, i;
410 
411 	for (i = 0; i < iso->actual_length;) {
412 		if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
413 			for (stream = 0;
414 			     stream < cdev->n_streams;
415 			     stream++, i++) {
416 				if (cdev->first_packet)
417 					continue;
418 
419 				check_byte = MAKE_CHECKBYTE(cdev, stream, i);
420 
421 				if ((usb_buf[i] & 0x3f) != check_byte)
422 					cdev->input_panic = 1;
423 
424 				if (usb_buf[i] & 0x80)
425 					cdev->output_panic = 1;
426 			}
427 		}
428 		cdev->first_packet = 0;
429 
430 		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
431 			sub = cdev->sub_capture[stream];
432 			if (cdev->input_panic)
433 				usb_buf[i] = 0;
434 
435 			if (sub) {
436 				struct snd_pcm_runtime *rt = sub->runtime;
437 				char *audio_buf = rt->dma_area;
438 				int sz = frames_to_bytes(rt, rt->buffer_size);
439 				audio_buf[cdev->audio_in_buf_pos[stream]++] =
440 					usb_buf[i];
441 				cdev->period_in_count[stream]++;
442 				if (cdev->audio_in_buf_pos[stream] == sz)
443 					cdev->audio_in_buf_pos[stream] = 0;
444 			}
445 		}
446 	}
447 }
448 
449 static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
450 			      const struct urb *urb,
451 			      const struct usb_iso_packet_descriptor *iso)
452 {
453 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
454 	struct device *dev = caiaqdev_to_dev(cdev);
455 	int stream, i;
456 
457 	/* paranoia check */
458 	if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
459 		return;
460 
461 	for (i = 0; i < iso->actual_length;) {
462 		for (stream = 0; stream < cdev->n_streams; stream++) {
463 			struct snd_pcm_substream *sub = cdev->sub_capture[stream];
464 			char *audio_buf = NULL;
465 			int c, n, sz = 0;
466 
467 			if (sub && !cdev->input_panic) {
468 				struct snd_pcm_runtime *rt = sub->runtime;
469 				audio_buf = rt->dma_area;
470 				sz = frames_to_bytes(rt, rt->buffer_size);
471 			}
472 
473 			for (c = 0; c < CHANNELS_PER_STREAM; c++) {
474 				/* 3 audio data bytes, followed by 1 check byte */
475 				if (audio_buf) {
476 					for (n = 0; n < BYTES_PER_SAMPLE; n++) {
477 						audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
478 
479 						if (cdev->audio_in_buf_pos[stream] == sz)
480 							cdev->audio_in_buf_pos[stream] = 0;
481 					}
482 
483 					cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
484 				}
485 
486 				i += BYTES_PER_SAMPLE;
487 
488 				if (usb_buf[i] != ((stream << 1) | c) &&
489 				    !cdev->first_packet) {
490 					if (!cdev->input_panic)
491 						dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
492 							 ((stream << 1) | c), usb_buf[i], c, stream, i);
493 					cdev->input_panic = 1;
494 				}
495 
496 				i++;
497 			}
498 		}
499 	}
500 
501 	if (cdev->first_packet > 0)
502 		cdev->first_packet--;
503 }
504 
505 static void read_in_urb(struct snd_usb_caiaqdev *cdev,
506 			const struct urb *urb,
507 			const struct usb_iso_packet_descriptor *iso)
508 {
509 	struct device *dev = caiaqdev_to_dev(cdev);
510 
511 	if (!cdev->streaming)
512 		return;
513 
514 	if (iso->actual_length < cdev->bpp)
515 		return;
516 
517 	switch (cdev->spec.data_alignment) {
518 	case 0:
519 		read_in_urb_mode0(cdev, urb, iso);
520 		break;
521 	case 2:
522 		read_in_urb_mode2(cdev, urb, iso);
523 		break;
524 	case 3:
525 		read_in_urb_mode3(cdev, urb, iso);
526 		break;
527 	}
528 
529 	if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
530 		dev_warn(dev, "streaming error detected %s %s\n",
531 				cdev->input_panic ? "(input)" : "",
532 				cdev->output_panic ? "(output)" : "");
533 		cdev->warned = 1;
534 	}
535 }
536 
537 static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
538 				struct urb *urb,
539 				const struct usb_iso_packet_descriptor *iso)
540 {
541 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
542 	struct snd_pcm_substream *sub;
543 	int stream, i;
544 
545 	for (i = 0; i < iso->length;) {
546 		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
547 			sub = cdev->sub_playback[stream];
548 			if (sub) {
549 				struct snd_pcm_runtime *rt = sub->runtime;
550 				char *audio_buf = rt->dma_area;
551 				int sz = frames_to_bytes(rt, rt->buffer_size);
552 				usb_buf[i] =
553 					audio_buf[cdev->audio_out_buf_pos[stream]];
554 				cdev->period_out_count[stream]++;
555 				cdev->audio_out_buf_pos[stream]++;
556 				if (cdev->audio_out_buf_pos[stream] == sz)
557 					cdev->audio_out_buf_pos[stream] = 0;
558 			} else
559 				usb_buf[i] = 0;
560 		}
561 
562 		/* fill in the check bytes */
563 		if (cdev->spec.data_alignment == 2 &&
564 		    i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
565 		        (cdev->n_streams * CHANNELS_PER_STREAM))
566 			for (stream = 0; stream < cdev->n_streams; stream++, i++)
567 				usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
568 	}
569 }
570 
571 static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
572 				struct urb *urb,
573 				const struct usb_iso_packet_descriptor *iso)
574 {
575 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
576 	int stream, i;
577 
578 	for (i = 0; i < iso->length;) {
579 		for (stream = 0; stream < cdev->n_streams; stream++) {
580 			struct snd_pcm_substream *sub = cdev->sub_playback[stream];
581 			char *audio_buf = NULL;
582 			int c, n, sz = 0;
583 
584 			if (sub) {
585 				struct snd_pcm_runtime *rt = sub->runtime;
586 				audio_buf = rt->dma_area;
587 				sz = frames_to_bytes(rt, rt->buffer_size);
588 			}
589 
590 			for (c = 0; c < CHANNELS_PER_STREAM; c++) {
591 				for (n = 0; n < BYTES_PER_SAMPLE; n++) {
592 					if (audio_buf) {
593 						usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
594 
595 						if (cdev->audio_out_buf_pos[stream] == sz)
596 							cdev->audio_out_buf_pos[stream] = 0;
597 					} else {
598 						usb_buf[i+n] = 0;
599 					}
600 				}
601 
602 				if (audio_buf)
603 					cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
604 
605 				i += BYTES_PER_SAMPLE;
606 
607 				/* fill in the check byte pattern */
608 				usb_buf[i++] = (stream << 1) | c;
609 			}
610 		}
611 	}
612 }
613 
614 static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
615 				struct urb *urb,
616 				const struct usb_iso_packet_descriptor *iso)
617 {
618 	switch (cdev->spec.data_alignment) {
619 	case 0:
620 	case 2:
621 		fill_out_urb_mode_0(cdev, urb, iso);
622 		break;
623 	case 3:
624 		fill_out_urb_mode_3(cdev, urb, iso);
625 		break;
626 	}
627 }
628 
629 static void read_completed(struct urb *urb)
630 {
631 	struct snd_usb_caiaq_cb_info *info = urb->context;
632 	struct snd_usb_caiaqdev *cdev;
633 	struct device *dev;
634 	struct urb *out = NULL;
635 	int i, frame, len, send_it = 0, outframe = 0;
636 	size_t offset = 0;
637 
638 	if (urb->status || !info)
639 		return;
640 
641 	cdev = info->cdev;
642 	dev = caiaqdev_to_dev(cdev);
643 
644 	if (!cdev->streaming)
645 		return;
646 
647 	/* find an unused output urb that is unused */
648 	for (i = 0; i < N_URBS; i++)
649 		if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
650 			out = cdev->data_urbs_out[i];
651 			break;
652 		}
653 
654 	if (!out) {
655 		dev_err(dev, "Unable to find an output urb to use\n");
656 		goto requeue;
657 	}
658 
659 	/* read the recently received packet and send back one which has
660 	 * the same layout */
661 	for (frame = 0; frame < FRAMES_PER_URB; frame++) {
662 		if (urb->iso_frame_desc[frame].status)
663 			continue;
664 
665 		len = urb->iso_frame_desc[outframe].actual_length;
666 		out->iso_frame_desc[outframe].length = len;
667 		out->iso_frame_desc[outframe].actual_length = 0;
668 		out->iso_frame_desc[outframe].offset = offset;
669 		offset += len;
670 
671 		if (len > 0) {
672 			spin_lock(&cdev->spinlock);
673 			fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
674 			read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
675 			spin_unlock(&cdev->spinlock);
676 			check_for_elapsed_periods(cdev, cdev->sub_playback);
677 			check_for_elapsed_periods(cdev, cdev->sub_capture);
678 			send_it = 1;
679 		}
680 
681 		outframe++;
682 	}
683 
684 	if (send_it) {
685 		out->number_of_packets = outframe;
686 		usb_submit_urb(out, GFP_ATOMIC);
687 	} else {
688 		struct snd_usb_caiaq_cb_info *oinfo = out->context;
689 		clear_bit(oinfo->index, &cdev->outurb_active_mask);
690 	}
691 
692 requeue:
693 	/* re-submit inbound urb */
694 	for (frame = 0; frame < FRAMES_PER_URB; frame++) {
695 		urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
696 		urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
697 		urb->iso_frame_desc[frame].actual_length = 0;
698 	}
699 
700 	urb->number_of_packets = FRAMES_PER_URB;
701 	usb_submit_urb(urb, GFP_ATOMIC);
702 }
703 
704 static void write_completed(struct urb *urb)
705 {
706 	struct snd_usb_caiaq_cb_info *info = urb->context;
707 	struct snd_usb_caiaqdev *cdev = info->cdev;
708 
709 	if (!cdev->output_running) {
710 		cdev->output_running = 1;
711 		wake_up(&cdev->prepare_wait_queue);
712 	}
713 
714 	clear_bit(info->index, &cdev->outurb_active_mask);
715 }
716 
717 static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
718 {
719 	int i, frame;
720 	struct urb **urbs;
721 	struct usb_device *usb_dev = cdev->chip.dev;
722 	struct device *dev = caiaqdev_to_dev(cdev);
723 	unsigned int pipe;
724 
725 	pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
726 		usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
727 		usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
728 
729 	urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
730 	if (!urbs) {
731 		dev_err(dev, "unable to kmalloc() urbs, OOM!?\n");
732 		*ret = -ENOMEM;
733 		return NULL;
734 	}
735 
736 	for (i = 0; i < N_URBS; i++) {
737 		urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
738 		if (!urbs[i]) {
739 			dev_err(dev, "unable to usb_alloc_urb(), OOM!?\n");
740 			*ret = -ENOMEM;
741 			return urbs;
742 		}
743 
744 		urbs[i]->transfer_buffer =
745 			kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
746 		if (!urbs[i]->transfer_buffer) {
747 			dev_err(dev, "unable to kmalloc() transfer buffer, OOM!?\n");
748 			*ret = -ENOMEM;
749 			return urbs;
750 		}
751 
752 		for (frame = 0; frame < FRAMES_PER_URB; frame++) {
753 			struct usb_iso_packet_descriptor *iso =
754 				&urbs[i]->iso_frame_desc[frame];
755 
756 			iso->offset = BYTES_PER_FRAME * frame;
757 			iso->length = BYTES_PER_FRAME;
758 		}
759 
760 		urbs[i]->dev = usb_dev;
761 		urbs[i]->pipe = pipe;
762 		urbs[i]->transfer_buffer_length = FRAMES_PER_URB
763 						* BYTES_PER_FRAME;
764 		urbs[i]->context = &cdev->data_cb_info[i];
765 		urbs[i]->interval = 1;
766 		urbs[i]->number_of_packets = FRAMES_PER_URB;
767 		urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
768 					read_completed : write_completed;
769 	}
770 
771 	*ret = 0;
772 	return urbs;
773 }
774 
775 static void free_urbs(struct urb **urbs)
776 {
777 	int i;
778 
779 	if (!urbs)
780 		return;
781 
782 	for (i = 0; i < N_URBS; i++) {
783 		if (!urbs[i])
784 			continue;
785 
786 		usb_kill_urb(urbs[i]);
787 		kfree(urbs[i]->transfer_buffer);
788 		usb_free_urb(urbs[i]);
789 	}
790 
791 	kfree(urbs);
792 }
793 
794 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
795 {
796 	int i, ret;
797 	struct device *dev = caiaqdev_to_dev(cdev);
798 
799 	cdev->n_audio_in  = max(cdev->spec.num_analog_audio_in,
800 			       cdev->spec.num_digital_audio_in) /
801 				CHANNELS_PER_STREAM;
802 	cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
803 			       cdev->spec.num_digital_audio_out) /
804 				CHANNELS_PER_STREAM;
805 	cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
806 
807 	dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in);
808 	dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out);
809 	dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams);
810 
811 	if (cdev->n_streams > MAX_STREAMS) {
812 		dev_err(dev, "unable to initialize device, too many streams.\n");
813 		return -EINVAL;
814 	}
815 
816 	ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
817 			cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
818 
819 	if (ret < 0) {
820 		dev_err(dev, "snd_pcm_new() returned %d\n", ret);
821 		return ret;
822 	}
823 
824 	cdev->pcm->private_data = cdev;
825 	strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
826 
827 	memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
828 	memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
829 
830 	memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
831 			sizeof(snd_usb_caiaq_pcm_hardware));
832 
833 	/* setup samplerates */
834 	cdev->samplerates = cdev->pcm_info.rates;
835 	switch (cdev->chip.usb_id) {
836 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
837 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
838 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
839 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
840 		cdev->samplerates |= SNDRV_PCM_RATE_192000;
841 		/* fall thru */
842 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
843 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
844 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
845 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
846 		cdev->samplerates |= SNDRV_PCM_RATE_88200;
847 		break;
848 	}
849 
850 	snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
851 				&snd_usb_caiaq_ops);
852 	snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
853 				&snd_usb_caiaq_ops);
854 
855 	snd_pcm_lib_preallocate_pages_for_all(cdev->pcm,
856 					SNDRV_DMA_TYPE_CONTINUOUS,
857 					snd_dma_continuous_data(GFP_KERNEL),
858 					MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
859 
860 	cdev->data_cb_info =
861 		kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
862 					GFP_KERNEL);
863 
864 	if (!cdev->data_cb_info)
865 		return -ENOMEM;
866 
867 	cdev->outurb_active_mask = 0;
868 	BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8));
869 
870 	for (i = 0; i < N_URBS; i++) {
871 		cdev->data_cb_info[i].cdev = cdev;
872 		cdev->data_cb_info[i].index = i;
873 	}
874 
875 	cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret);
876 	if (ret < 0) {
877 		kfree(cdev->data_cb_info);
878 		free_urbs(cdev->data_urbs_in);
879 		return ret;
880 	}
881 
882 	cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
883 	if (ret < 0) {
884 		kfree(cdev->data_cb_info);
885 		free_urbs(cdev->data_urbs_in);
886 		free_urbs(cdev->data_urbs_out);
887 		return ret;
888 	}
889 
890 	return 0;
891 }
892 
893 void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev)
894 {
895 	struct device *dev = caiaqdev_to_dev(cdev);
896 
897 	dev_dbg(dev, "%s(%p)\n", __func__, cdev);
898 	stream_stop(cdev);
899 	free_urbs(cdev->data_urbs_in);
900 	free_urbs(cdev->data_urbs_out);
901 	kfree(cdev->data_cb_info);
902 }
903 
904