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