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