xref: /openbmc/linux/sound/usb/caiaq/audio.c (revision cd238eff)
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_alloc_vmalloc_buffer(sub,
174 						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 *cdev = snd_pcm_substream_chip(sub);
180 	deactivate_substream(cdev, sub);
181 	return snd_pcm_lib_free_vmalloc_buffer(sub);
182 }
183 
184 /* this should probably go upstream */
185 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
186 #error "Change this table"
187 #endif
188 
189 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
190 				48000, 64000, 88200, 96000, 176400, 192000 };
191 
192 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
193 {
194 	int bytes_per_sample, bpp, ret, i;
195 	int index = substream->number;
196 	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
197 	struct snd_pcm_runtime *runtime = substream->runtime;
198 	struct device *dev = caiaqdev_to_dev(cdev);
199 
200 	dev_dbg(dev, "%s(%p)\n", __func__, substream);
201 
202 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
203 		int out_pos;
204 
205 		switch (cdev->spec.data_alignment) {
206 		case 0:
207 		case 2:
208 			out_pos = BYTES_PER_SAMPLE + 1;
209 			break;
210 		case 3:
211 		default:
212 			out_pos = 0;
213 			break;
214 		}
215 
216 		cdev->period_out_count[index] = out_pos;
217 		cdev->audio_out_buf_pos[index] = out_pos;
218 	} else {
219 		int in_pos;
220 
221 		switch (cdev->spec.data_alignment) {
222 		case 0:
223 			in_pos = BYTES_PER_SAMPLE + 2;
224 			break;
225 		case 2:
226 			in_pos = BYTES_PER_SAMPLE;
227 			break;
228 		case 3:
229 		default:
230 			in_pos = 0;
231 			break;
232 		}
233 
234 		cdev->period_in_count[index] = in_pos;
235 		cdev->audio_in_buf_pos[index] = in_pos;
236 	}
237 
238 	if (cdev->streaming)
239 		return 0;
240 
241 	/* the first client that opens a stream defines the sample rate
242 	 * setting for all subsequent calls, until the last client closed. */
243 	for (i=0; i < ARRAY_SIZE(rates); i++)
244 		if (runtime->rate == rates[i])
245 			cdev->pcm_info.rates = 1 << i;
246 
247 	snd_pcm_limit_hw_rates(runtime);
248 
249 	bytes_per_sample = BYTES_PER_SAMPLE;
250 	if (cdev->spec.data_alignment >= 2)
251 		bytes_per_sample++;
252 
253 	bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
254 		* bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
255 
256 	if (bpp > MAX_ENDPOINT_SIZE)
257 		bpp = MAX_ENDPOINT_SIZE;
258 
259 	ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
260 					     runtime->sample_bits, bpp);
261 	if (ret)
262 		return ret;
263 
264 	ret = stream_start(cdev);
265 	if (ret)
266 		return ret;
267 
268 	cdev->output_running = 0;
269 	wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ);
270 	if (!cdev->output_running) {
271 		stream_stop(cdev);
272 		return -EPIPE;
273 	}
274 
275 	return 0;
276 }
277 
278 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
279 {
280 	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
281 	struct device *dev = caiaqdev_to_dev(cdev);
282 
283 	dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd);
284 
285 	switch (cmd) {
286 	case SNDRV_PCM_TRIGGER_START:
287 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
288 		activate_substream(cdev, sub);
289 		break;
290 	case SNDRV_PCM_TRIGGER_STOP:
291 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
292 		deactivate_substream(cdev, sub);
293 		break;
294 	default:
295 		return -EINVAL;
296 	}
297 
298 	return 0;
299 }
300 
301 static snd_pcm_uframes_t
302 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
303 {
304 	int index = sub->number;
305 	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
306 	snd_pcm_uframes_t ptr;
307 
308 	spin_lock(&cdev->spinlock);
309 
310 	if (cdev->input_panic || cdev->output_panic) {
311 		ptr = SNDRV_PCM_POS_XRUN;
312 		goto unlock;
313 	}
314 
315 	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
316 		ptr = bytes_to_frames(sub->runtime,
317 					cdev->audio_out_buf_pos[index]);
318 	else
319 		ptr = bytes_to_frames(sub->runtime,
320 					cdev->audio_in_buf_pos[index]);
321 
322 unlock:
323 	spin_unlock(&cdev->spinlock);
324 	return ptr;
325 }
326 
327 /* operators for both playback and capture */
328 static const struct snd_pcm_ops snd_usb_caiaq_ops = {
329 	.open =		snd_usb_caiaq_substream_open,
330 	.close =	snd_usb_caiaq_substream_close,
331 	.ioctl =	snd_pcm_lib_ioctl,
332 	.hw_params =	snd_usb_caiaq_pcm_hw_params,
333 	.hw_free =	snd_usb_caiaq_pcm_hw_free,
334 	.prepare =	snd_usb_caiaq_pcm_prepare,
335 	.trigger =	snd_usb_caiaq_pcm_trigger,
336 	.pointer =	snd_usb_caiaq_pcm_pointer,
337 	.page =		snd_pcm_lib_get_vmalloc_page,
338 };
339 
340 static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
341 				      struct snd_pcm_substream **subs)
342 {
343 	int stream, pb, *cnt;
344 	struct snd_pcm_substream *sub;
345 
346 	for (stream = 0; stream < cdev->n_streams; stream++) {
347 		sub = subs[stream];
348 		if (!sub)
349 			continue;
350 
351 		pb = snd_pcm_lib_period_bytes(sub);
352 		cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
353 					&cdev->period_out_count[stream] :
354 					&cdev->period_in_count[stream];
355 
356 		if (*cnt >= pb) {
357 			snd_pcm_period_elapsed(sub);
358 			*cnt %= pb;
359 		}
360 	}
361 }
362 
363 static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
364 			      const struct urb *urb,
365 			      const struct usb_iso_packet_descriptor *iso)
366 {
367 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
368 	struct snd_pcm_substream *sub;
369 	int stream, i;
370 
371 	if (all_substreams_zero(cdev->sub_capture))
372 		return;
373 
374 	for (i = 0; i < iso->actual_length;) {
375 		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
376 			sub = cdev->sub_capture[stream];
377 			if (sub) {
378 				struct snd_pcm_runtime *rt = sub->runtime;
379 				char *audio_buf = rt->dma_area;
380 				int sz = frames_to_bytes(rt, rt->buffer_size);
381 				audio_buf[cdev->audio_in_buf_pos[stream]++]
382 					= usb_buf[i];
383 				cdev->period_in_count[stream]++;
384 				if (cdev->audio_in_buf_pos[stream] == sz)
385 					cdev->audio_in_buf_pos[stream] = 0;
386 			}
387 		}
388 	}
389 }
390 
391 static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
392 			      const struct urb *urb,
393 			      const struct usb_iso_packet_descriptor *iso)
394 {
395 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
396 	unsigned char check_byte;
397 	struct snd_pcm_substream *sub;
398 	int stream, i;
399 
400 	for (i = 0; i < iso->actual_length;) {
401 		if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
402 			for (stream = 0;
403 			     stream < cdev->n_streams;
404 			     stream++, i++) {
405 				if (cdev->first_packet)
406 					continue;
407 
408 				check_byte = MAKE_CHECKBYTE(cdev, stream, i);
409 
410 				if ((usb_buf[i] & 0x3f) != check_byte)
411 					cdev->input_panic = 1;
412 
413 				if (usb_buf[i] & 0x80)
414 					cdev->output_panic = 1;
415 			}
416 		}
417 		cdev->first_packet = 0;
418 
419 		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
420 			sub = cdev->sub_capture[stream];
421 			if (cdev->input_panic)
422 				usb_buf[i] = 0;
423 
424 			if (sub) {
425 				struct snd_pcm_runtime *rt = sub->runtime;
426 				char *audio_buf = rt->dma_area;
427 				int sz = frames_to_bytes(rt, rt->buffer_size);
428 				audio_buf[cdev->audio_in_buf_pos[stream]++] =
429 					usb_buf[i];
430 				cdev->period_in_count[stream]++;
431 				if (cdev->audio_in_buf_pos[stream] == sz)
432 					cdev->audio_in_buf_pos[stream] = 0;
433 			}
434 		}
435 	}
436 }
437 
438 static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
439 			      const struct urb *urb,
440 			      const struct usb_iso_packet_descriptor *iso)
441 {
442 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
443 	struct device *dev = caiaqdev_to_dev(cdev);
444 	int stream, i;
445 
446 	/* paranoia check */
447 	if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
448 		return;
449 
450 	for (i = 0; i < iso->actual_length;) {
451 		for (stream = 0; stream < cdev->n_streams; stream++) {
452 			struct snd_pcm_substream *sub = cdev->sub_capture[stream];
453 			char *audio_buf = NULL;
454 			int c, n, sz = 0;
455 
456 			if (sub && !cdev->input_panic) {
457 				struct snd_pcm_runtime *rt = sub->runtime;
458 				audio_buf = rt->dma_area;
459 				sz = frames_to_bytes(rt, rt->buffer_size);
460 			}
461 
462 			for (c = 0; c < CHANNELS_PER_STREAM; c++) {
463 				/* 3 audio data bytes, followed by 1 check byte */
464 				if (audio_buf) {
465 					for (n = 0; n < BYTES_PER_SAMPLE; n++) {
466 						audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
467 
468 						if (cdev->audio_in_buf_pos[stream] == sz)
469 							cdev->audio_in_buf_pos[stream] = 0;
470 					}
471 
472 					cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
473 				}
474 
475 				i += BYTES_PER_SAMPLE;
476 
477 				if (usb_buf[i] != ((stream << 1) | c) &&
478 				    !cdev->first_packet) {
479 					if (!cdev->input_panic)
480 						dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
481 							 ((stream << 1) | c), usb_buf[i], c, stream, i);
482 					cdev->input_panic = 1;
483 				}
484 
485 				i++;
486 			}
487 		}
488 	}
489 
490 	if (cdev->first_packet > 0)
491 		cdev->first_packet--;
492 }
493 
494 static void read_in_urb(struct snd_usb_caiaqdev *cdev,
495 			const struct urb *urb,
496 			const struct usb_iso_packet_descriptor *iso)
497 {
498 	struct device *dev = caiaqdev_to_dev(cdev);
499 
500 	if (!cdev->streaming)
501 		return;
502 
503 	if (iso->actual_length < cdev->bpp)
504 		return;
505 
506 	switch (cdev->spec.data_alignment) {
507 	case 0:
508 		read_in_urb_mode0(cdev, urb, iso);
509 		break;
510 	case 2:
511 		read_in_urb_mode2(cdev, urb, iso);
512 		break;
513 	case 3:
514 		read_in_urb_mode3(cdev, urb, iso);
515 		break;
516 	}
517 
518 	if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
519 		dev_warn(dev, "streaming error detected %s %s\n",
520 				cdev->input_panic ? "(input)" : "",
521 				cdev->output_panic ? "(output)" : "");
522 		cdev->warned = 1;
523 	}
524 }
525 
526 static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
527 				struct urb *urb,
528 				const struct usb_iso_packet_descriptor *iso)
529 {
530 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
531 	struct snd_pcm_substream *sub;
532 	int stream, i;
533 
534 	for (i = 0; i < iso->length;) {
535 		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
536 			sub = cdev->sub_playback[stream];
537 			if (sub) {
538 				struct snd_pcm_runtime *rt = sub->runtime;
539 				char *audio_buf = rt->dma_area;
540 				int sz = frames_to_bytes(rt, rt->buffer_size);
541 				usb_buf[i] =
542 					audio_buf[cdev->audio_out_buf_pos[stream]];
543 				cdev->period_out_count[stream]++;
544 				cdev->audio_out_buf_pos[stream]++;
545 				if (cdev->audio_out_buf_pos[stream] == sz)
546 					cdev->audio_out_buf_pos[stream] = 0;
547 			} else
548 				usb_buf[i] = 0;
549 		}
550 
551 		/* fill in the check bytes */
552 		if (cdev->spec.data_alignment == 2 &&
553 		    i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
554 		        (cdev->n_streams * CHANNELS_PER_STREAM))
555 			for (stream = 0; stream < cdev->n_streams; stream++, i++)
556 				usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
557 	}
558 }
559 
560 static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
561 				struct urb *urb,
562 				const struct usb_iso_packet_descriptor *iso)
563 {
564 	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
565 	int stream, i;
566 
567 	for (i = 0; i < iso->length;) {
568 		for (stream = 0; stream < cdev->n_streams; stream++) {
569 			struct snd_pcm_substream *sub = cdev->sub_playback[stream];
570 			char *audio_buf = NULL;
571 			int c, n, sz = 0;
572 
573 			if (sub) {
574 				struct snd_pcm_runtime *rt = sub->runtime;
575 				audio_buf = rt->dma_area;
576 				sz = frames_to_bytes(rt, rt->buffer_size);
577 			}
578 
579 			for (c = 0; c < CHANNELS_PER_STREAM; c++) {
580 				for (n = 0; n < BYTES_PER_SAMPLE; n++) {
581 					if (audio_buf) {
582 						usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
583 
584 						if (cdev->audio_out_buf_pos[stream] == sz)
585 							cdev->audio_out_buf_pos[stream] = 0;
586 					} else {
587 						usb_buf[i+n] = 0;
588 					}
589 				}
590 
591 				if (audio_buf)
592 					cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
593 
594 				i += BYTES_PER_SAMPLE;
595 
596 				/* fill in the check byte pattern */
597 				usb_buf[i++] = (stream << 1) | c;
598 			}
599 		}
600 	}
601 }
602 
603 static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
604 				struct urb *urb,
605 				const struct usb_iso_packet_descriptor *iso)
606 {
607 	switch (cdev->spec.data_alignment) {
608 	case 0:
609 	case 2:
610 		fill_out_urb_mode_0(cdev, urb, iso);
611 		break;
612 	case 3:
613 		fill_out_urb_mode_3(cdev, urb, iso);
614 		break;
615 	}
616 }
617 
618 static void read_completed(struct urb *urb)
619 {
620 	struct snd_usb_caiaq_cb_info *info = urb->context;
621 	struct snd_usb_caiaqdev *cdev;
622 	struct device *dev;
623 	struct urb *out = NULL;
624 	int i, frame, len, send_it = 0, outframe = 0;
625 	unsigned long flags;
626 	size_t offset = 0;
627 
628 	if (urb->status || !info)
629 		return;
630 
631 	cdev = info->cdev;
632 	dev = caiaqdev_to_dev(cdev);
633 
634 	if (!cdev->streaming)
635 		return;
636 
637 	/* find an unused output urb that is unused */
638 	for (i = 0; i < N_URBS; i++)
639 		if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
640 			out = cdev->data_urbs_out[i];
641 			break;
642 		}
643 
644 	if (!out) {
645 		dev_err(dev, "Unable to find an output urb to use\n");
646 		goto requeue;
647 	}
648 
649 	/* read the recently received packet and send back one which has
650 	 * the same layout */
651 	for (frame = 0; frame < FRAMES_PER_URB; frame++) {
652 		if (urb->iso_frame_desc[frame].status)
653 			continue;
654 
655 		len = urb->iso_frame_desc[outframe].actual_length;
656 		out->iso_frame_desc[outframe].length = len;
657 		out->iso_frame_desc[outframe].actual_length = 0;
658 		out->iso_frame_desc[outframe].offset = offset;
659 		offset += len;
660 
661 		if (len > 0) {
662 			spin_lock_irqsave(&cdev->spinlock, flags);
663 			fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
664 			read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
665 			spin_unlock_irqrestore(&cdev->spinlock, flags);
666 			check_for_elapsed_periods(cdev, cdev->sub_playback);
667 			check_for_elapsed_periods(cdev, cdev->sub_capture);
668 			send_it = 1;
669 		}
670 
671 		outframe++;
672 	}
673 
674 	if (send_it) {
675 		out->number_of_packets = outframe;
676 		usb_submit_urb(out, GFP_ATOMIC);
677 	} else {
678 		struct snd_usb_caiaq_cb_info *oinfo = out->context;
679 		clear_bit(oinfo->index, &cdev->outurb_active_mask);
680 	}
681 
682 requeue:
683 	/* re-submit inbound urb */
684 	for (frame = 0; frame < FRAMES_PER_URB; frame++) {
685 		urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
686 		urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
687 		urb->iso_frame_desc[frame].actual_length = 0;
688 	}
689 
690 	urb->number_of_packets = FRAMES_PER_URB;
691 	usb_submit_urb(urb, GFP_ATOMIC);
692 }
693 
694 static void write_completed(struct urb *urb)
695 {
696 	struct snd_usb_caiaq_cb_info *info = urb->context;
697 	struct snd_usb_caiaqdev *cdev = info->cdev;
698 
699 	if (!cdev->output_running) {
700 		cdev->output_running = 1;
701 		wake_up(&cdev->prepare_wait_queue);
702 	}
703 
704 	clear_bit(info->index, &cdev->outurb_active_mask);
705 }
706 
707 static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
708 {
709 	int i, frame;
710 	struct urb **urbs;
711 	struct usb_device *usb_dev = cdev->chip.dev;
712 	unsigned int pipe;
713 
714 	pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
715 		usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
716 		usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
717 
718 	urbs = kmalloc_array(N_URBS, sizeof(*urbs), GFP_KERNEL);
719 	if (!urbs) {
720 		*ret = -ENOMEM;
721 		return NULL;
722 	}
723 
724 	for (i = 0; i < N_URBS; i++) {
725 		urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
726 		if (!urbs[i]) {
727 			*ret = -ENOMEM;
728 			return urbs;
729 		}
730 
731 		urbs[i]->transfer_buffer =
732 			kmalloc_array(BYTES_PER_FRAME, FRAMES_PER_URB,
733 				      GFP_KERNEL);
734 		if (!urbs[i]->transfer_buffer) {
735 			*ret = -ENOMEM;
736 			return urbs;
737 		}
738 
739 		for (frame = 0; frame < FRAMES_PER_URB; frame++) {
740 			struct usb_iso_packet_descriptor *iso =
741 				&urbs[i]->iso_frame_desc[frame];
742 
743 			iso->offset = BYTES_PER_FRAME * frame;
744 			iso->length = BYTES_PER_FRAME;
745 		}
746 
747 		urbs[i]->dev = usb_dev;
748 		urbs[i]->pipe = pipe;
749 		urbs[i]->transfer_buffer_length = FRAMES_PER_URB
750 						* BYTES_PER_FRAME;
751 		urbs[i]->context = &cdev->data_cb_info[i];
752 		urbs[i]->interval = 1;
753 		urbs[i]->number_of_packets = FRAMES_PER_URB;
754 		urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
755 					read_completed : write_completed;
756 	}
757 
758 	*ret = 0;
759 	return urbs;
760 }
761 
762 static void free_urbs(struct urb **urbs)
763 {
764 	int i;
765 
766 	if (!urbs)
767 		return;
768 
769 	for (i = 0; i < N_URBS; i++) {
770 		if (!urbs[i])
771 			continue;
772 
773 		usb_kill_urb(urbs[i]);
774 		kfree(urbs[i]->transfer_buffer);
775 		usb_free_urb(urbs[i]);
776 	}
777 
778 	kfree(urbs);
779 }
780 
781 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
782 {
783 	int i, ret;
784 	struct device *dev = caiaqdev_to_dev(cdev);
785 
786 	cdev->n_audio_in  = max(cdev->spec.num_analog_audio_in,
787 			       cdev->spec.num_digital_audio_in) /
788 				CHANNELS_PER_STREAM;
789 	cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
790 			       cdev->spec.num_digital_audio_out) /
791 				CHANNELS_PER_STREAM;
792 	cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
793 
794 	dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in);
795 	dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out);
796 	dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams);
797 
798 	if (cdev->n_streams > MAX_STREAMS) {
799 		dev_err(dev, "unable to initialize device, too many streams.\n");
800 		return -EINVAL;
801 	}
802 
803 	if (cdev->n_streams < 1) {
804 		dev_err(dev, "bogus number of streams: %d\n", cdev->n_streams);
805 		return -EINVAL;
806 	}
807 
808 	ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
809 			cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
810 
811 	if (ret < 0) {
812 		dev_err(dev, "snd_pcm_new() returned %d\n", ret);
813 		return ret;
814 	}
815 
816 	cdev->pcm->private_data = cdev;
817 	strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
818 
819 	memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
820 	memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
821 
822 	memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
823 			sizeof(snd_usb_caiaq_pcm_hardware));
824 
825 	/* setup samplerates */
826 	cdev->samplerates = cdev->pcm_info.rates;
827 	switch (cdev->chip.usb_id) {
828 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
829 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
830 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
831 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
832 		cdev->samplerates |= SNDRV_PCM_RATE_192000;
833 		/* fall thru */
834 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
835 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
836 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
837 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
838 		cdev->samplerates |= SNDRV_PCM_RATE_88200;
839 		break;
840 	}
841 
842 	snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
843 				&snd_usb_caiaq_ops);
844 	snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
845 				&snd_usb_caiaq_ops);
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