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