audio.c 23.0 KB
Newer Older
1
/*
2
 *   Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/

19
#include <linux/device.h>
20
#include <linux/spinlock.h>
21
#include <linux/slab.h>
22 23 24 25 26
#include <linux/init.h>
#include <linux/usb.h>
#include <sound/core.h>
#include <sound/pcm.h>

27 28
#include "device.h"
#include "audio.h"
29 30 31 32 33 34 35 36 37

#define N_URBS			32
#define CLOCK_DRIFT_TOLERANCE	5
#define FRAMES_PER_URB		8
#define BYTES_PER_FRAME		512
#define CHANNELS_PER_STREAM	2
#define BYTES_PER_SAMPLE	3
#define BYTES_PER_SAMPLE_USB	4
#define MAX_BUFFER_SIZE		(128*1024)
38 39
#define MAX_ENDPOINT_SIZE	512

40 41 42
#define ENDPOINT_CAPTURE	2
#define ENDPOINT_PLAYBACK	6

43 44
#define MAKE_CHECKBYTE(cdev,stream,i) \
	(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 49
			   SNDRV_PCM_INFO_BLOCK_TRANSFER),
	.formats 	= SNDRV_PCM_FMTBIT_S24_3BE,
50
	.rates 		= (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
51 52 53 54 55 56
			   SNDRV_PCM_RATE_96000),
	.rate_min	= 44100,
	.rate_max	= 0, /* will overwrite later */
	.channels_min	= CHANNELS_PER_STREAM,
	.channels_max	= CHANNELS_PER_STREAM,
	.buffer_bytes_max = MAX_BUFFER_SIZE,
57
	.period_bytes_min = 128,
58 59 60 61 62 63
	.period_bytes_max = MAX_BUFFER_SIZE,
	.periods_min	= 1,
	.periods_max	= 1024,
};

static void
64
activate_substream(struct snd_usb_caiaqdev *cdev,
65 66
	           struct snd_pcm_substream *sub)
{
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 80
		     struct snd_pcm_substream *sub)
{
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 93 94 95 96 97 98 99 100 101
}

static int
all_substreams_zero(struct snd_pcm_substream **subs)
{
	int i;
	for (i = 0; i < MAX_STREAMS; i++)
		if (subs[i] != NULL)
			return 0;
	return 1;
}

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 111
		return -EINVAL;

112 113 114 115 116 117 118
	memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
	memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
	cdev->input_panic = 0;
	cdev->output_panic = 0;
	cdev->first_packet = 4;
	cdev->streaming = 1;
	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 124
			dev_err(dev, "unable to trigger read #%d! (ret %d)\n",
				i, ret);
125
			cdev->streaming = 0;
126 127 128
			return -EPIPE;
		}
	}
129

130 131 132
	return 0;
}

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 148
		if (test_bit(i, &cdev->outurb_active_mask))
			usb_kill_urb(cdev->data_urbs_out[i]);
149
	}
150

151
	cdev->outurb_active_mask = 0;
152 153 154 155
}

static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
{
156
	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
157 158 159
	struct device *dev = caiaqdev_to_dev(cdev);

	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 164 165 166 167
	return 0;
}

static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
{
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 173
	if (all_substreams_zero(cdev->sub_playback) &&
	    all_substreams_zero(cdev->sub_capture)) {
174
		/* when the last client has stopped streaming,
175
		 * all sample rates are allowed again */
176 177
		stream_stop(cdev);
		cdev->pcm_info.rates = cdev->samplerates;
178
	}
179

180 181 182 183
	return 0;
}

static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
184
				       struct snd_pcm_hw_params *hw_params)
185 186 187 188 189 190
{
	return snd_pcm_lib_malloc_pages(sub, params_buffer_bytes(hw_params));
}

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);
	deactivate_substream(cdev, sub);
193 194 195 196 197 198 199 200 201
	return snd_pcm_lib_free_pages(sub);
}

/* this should probably go upstream */
#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
#error "Change this table"
#endif

static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
202
				48000, 64000, 88200, 96000, 176400, 192000 };
203 204 205 206 207

static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
{
	int bytes_per_sample, bpp, ret, i;
	int index = substream->number;
208
	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
209
	struct snd_pcm_runtime *runtime = substream->runtime;
210
	struct device *dev = caiaqdev_to_dev(cdev);
211

212
	dev_dbg(dev, "%s(%p)\n", __func__, substream);
213

214
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
215 216
		int out_pos;

217
		switch (cdev->spec.data_alignment) {
218 219 220 221 222 223 224 225 226 227
		case 0:
		case 2:
			out_pos = BYTES_PER_SAMPLE + 1;
			break;
		case 3:
		default:
			out_pos = 0;
			break;
		}

228 229
		cdev->period_out_count[index] = out_pos;
		cdev->audio_out_buf_pos[index] = out_pos;
230
	} else {
231 232
		int in_pos;

233
		switch (cdev->spec.data_alignment) {
234 235 236 237 238 239 240 241 242 243 244 245
		case 0:
			in_pos = BYTES_PER_SAMPLE + 2;
			break;
		case 2:
			in_pos = BYTES_PER_SAMPLE;
			break;
		case 3:
		default:
			in_pos = 0;
			break;
		}

246 247
		cdev->period_in_count[index] = in_pos;
		cdev->audio_in_buf_pos[index] = in_pos;
248 249
	}

250
	if (cdev->streaming)
251
		return 0;
252

253 254 255 256
	/* the first client that opens a stream defines the sample rate
	 * setting for all subsequent calls, until the last client closed. */
	for (i=0; i < ARRAY_SIZE(rates); i++)
		if (runtime->rate == rates[i])
257
			cdev->pcm_info.rates = 1 << i;
258

259 260 261
	snd_pcm_limit_hw_rates(runtime);

	bytes_per_sample = BYTES_PER_SAMPLE;
262
	if (cdev->spec.data_alignment >= 2)
263
		bytes_per_sample++;
264

265
	bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
266
		* bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
267 268 269 270

	if (bpp > MAX_ENDPOINT_SIZE)
		bpp = MAX_ENDPOINT_SIZE;

271
	ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
272 273 274 275
					     runtime->sample_bits, bpp);
	if (ret)
		return ret;

276
	ret = stream_start(cdev);
277 278
	if (ret)
		return ret;
279

280 281 282 283
	cdev->output_running = 0;
	wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ);
	if (!cdev->output_running) {
		stream_stop(cdev);
284 285 286 287 288 289 290 291
		return -EPIPE;
	}

	return 0;
}

static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
{
292
	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
293
	struct device *dev = caiaqdev_to_dev(cdev);
294

295
	dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd);
296

297 298 299
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
300
		activate_substream(cdev, sub);
301 302 303
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
304
		deactivate_substream(cdev, sub);
305 306 307 308 309 310 311 312 313 314 315 316
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static snd_pcm_uframes_t
snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
{
	int index = sub->number;
317
	struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
318 319
	snd_pcm_uframes_t ptr;

320
	spin_lock(&cdev->spinlock);
321

322
	if (cdev->input_panic || cdev->output_panic) {
323
		ptr = SNDRV_PCM_POS_XRUN;
324 325
		goto unlock;
	}
326 327

	if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
328
		ptr = bytes_to_frames(sub->runtime,
329
					cdev->audio_out_buf_pos[index]);
330
	else
331
		ptr = bytes_to_frames(sub->runtime,
332
					cdev->audio_in_buf_pos[index]);
333

334
unlock:
335
	spin_unlock(&cdev->spinlock);
336
	return ptr;
337 338 339 340 341 342 343 344 345 346 347 348 349
}

/* operators for both playback and capture */
static struct snd_pcm_ops snd_usb_caiaq_ops = {
	.open =		snd_usb_caiaq_substream_open,
	.close =	snd_usb_caiaq_substream_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_usb_caiaq_pcm_hw_params,
	.hw_free =	snd_usb_caiaq_pcm_hw_free,
	.prepare =	snd_usb_caiaq_pcm_prepare,
	.trigger =	snd_usb_caiaq_pcm_trigger,
	.pointer =	snd_usb_caiaq_pcm_pointer
};
350

351
static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
352 353 354 355 356
				      struct snd_pcm_substream **subs)
{
	int stream, pb, *cnt;
	struct snd_pcm_substream *sub;

357
	for (stream = 0; stream < cdev->n_streams; stream++) {
358 359 360 361
		sub = subs[stream];
		if (!sub)
			continue;

362
		pb = snd_pcm_lib_period_bytes(sub);
363
		cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
364 365
					&cdev->period_out_count[stream] :
					&cdev->period_in_count[stream];
366 367 368 369 370 371 372 373

		if (*cnt >= pb) {
			snd_pcm_period_elapsed(sub);
			*cnt %= pb;
		}
	}
}

374
static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
375 376 377 378 379 380 381
			      const struct urb *urb,
			      const struct usb_iso_packet_descriptor *iso)
{
	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
	struct snd_pcm_substream *sub;
	int stream, i;

382
	if (all_substreams_zero(cdev->sub_capture))
383 384 385
		return;

	for (i = 0; i < iso->actual_length;) {
386 387
		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
			sub = cdev->sub_capture[stream];
388 389 390 391
			if (sub) {
				struct snd_pcm_runtime *rt = sub->runtime;
				char *audio_buf = rt->dma_area;
				int sz = frames_to_bytes(rt, rt->buffer_size);
392
				audio_buf[cdev->audio_in_buf_pos[stream]++]
393
					= usb_buf[i];
394 395 396
				cdev->period_in_count[stream]++;
				if (cdev->audio_in_buf_pos[stream] == sz)
					cdev->audio_in_buf_pos[stream] = 0;
397 398 399 400 401
			}
		}
	}
}

402
static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
403 404 405 406 407 408 409 410 411
			      const struct urb *urb,
			      const struct usb_iso_packet_descriptor *iso)
{
	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
	unsigned char check_byte;
	struct snd_pcm_substream *sub;
	int stream, i;

	for (i = 0; i < iso->actual_length;) {
412
		if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
413
			for (stream = 0;
414
			     stream < cdev->n_streams;
415
			     stream++, i++) {
416
				if (cdev->first_packet)
417 418
					continue;

419
				check_byte = MAKE_CHECKBYTE(cdev, stream, i);
420

421
				if ((usb_buf[i] & 0x3f) != check_byte)
422
					cdev->input_panic = 1;
423 424

				if (usb_buf[i] & 0x80)
425
					cdev->output_panic = 1;
426 427
			}
		}
428
		cdev->first_packet = 0;
429

430 431 432
		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
			sub = cdev->sub_capture[stream];
			if (cdev->input_panic)
433 434
				usb_buf[i] = 0;

435 436 437 438
			if (sub) {
				struct snd_pcm_runtime *rt = sub->runtime;
				char *audio_buf = rt->dma_area;
				int sz = frames_to_bytes(rt, rt->buffer_size);
439
				audio_buf[cdev->audio_in_buf_pos[stream]++] =
440
					usb_buf[i];
441 442 443
				cdev->period_in_count[stream]++;
				if (cdev->audio_in_buf_pos[stream] == sz)
					cdev->audio_in_buf_pos[stream] = 0;
444 445 446 447 448
			}
		}
	}
}

449
static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
450 451 452 453
			      const struct urb *urb,
			      const struct usb_iso_packet_descriptor *iso)
{
	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
454
	struct device *dev = caiaqdev_to_dev(cdev);
455 456 457 458 459 460 461
	int stream, i;

	/* paranoia check */
	if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
		return;

	for (i = 0; i < iso->actual_length;) {
462 463
		for (stream = 0; stream < cdev->n_streams; stream++) {
			struct snd_pcm_substream *sub = cdev->sub_capture[stream];
464 465 466
			char *audio_buf = NULL;
			int c, n, sz = 0;

467
			if (sub && !cdev->input_panic) {
468 469 470 471 472 473 474 475 476
				struct snd_pcm_runtime *rt = sub->runtime;
				audio_buf = rt->dma_area;
				sz = frames_to_bytes(rt, rt->buffer_size);
			}

			for (c = 0; c < CHANNELS_PER_STREAM; c++) {
				/* 3 audio data bytes, followed by 1 check byte */
				if (audio_buf) {
					for (n = 0; n < BYTES_PER_SAMPLE; n++) {
477
						audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
478

479 480
						if (cdev->audio_in_buf_pos[stream] == sz)
							cdev->audio_in_buf_pos[stream] = 0;
481 482
					}

483
					cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
484 485 486 487 488
				}

				i += BYTES_PER_SAMPLE;

				if (usb_buf[i] != ((stream << 1) | c) &&
489 490
				    !cdev->first_packet) {
					if (!cdev->input_panic)
491 492
						dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
							 ((stream << 1) | c), usb_buf[i], c, stream, i);
493
					cdev->input_panic = 1;
494 495 496 497 498 499 500
				}

				i++;
			}
		}
	}

501 502
	if (cdev->first_packet > 0)
		cdev->first_packet--;
503 504
}

505
static void read_in_urb(struct snd_usb_caiaqdev *cdev,
506 507 508
			const struct urb *urb,
			const struct usb_iso_packet_descriptor *iso)
{
509 510
	struct device *dev = caiaqdev_to_dev(cdev);

511
	if (!cdev->streaming)
512 513
		return;

514
	if (iso->actual_length < cdev->bpp)
515 516
		return;

517
	switch (cdev->spec.data_alignment) {
518
	case 0:
519
		read_in_urb_mode0(cdev, urb, iso);
520 521
		break;
	case 2:
522
		read_in_urb_mode2(cdev, urb, iso);
523
		break;
524
	case 3:
525
		read_in_urb_mode3(cdev, urb, iso);
526
		break;
527 528
	}

529
	if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
530
		dev_warn(dev, "streaming error detected %s %s\n",
531 532 533
				cdev->input_panic ? "(input)" : "",
				cdev->output_panic ? "(output)" : "");
		cdev->warned = 1;
534 535 536
	}
}

537
static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
538 539
				struct urb *urb,
				const struct usb_iso_packet_descriptor *iso)
540 541 542 543
{
	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
	struct snd_pcm_substream *sub;
	int stream, i;
544

545
	for (i = 0; i < iso->length;) {
546 547
		for (stream = 0; stream < cdev->n_streams; stream++, i++) {
			sub = cdev->sub_playback[stream];
548 549 550 551
			if (sub) {
				struct snd_pcm_runtime *rt = sub->runtime;
				char *audio_buf = rt->dma_area;
				int sz = frames_to_bytes(rt, rt->buffer_size);
552
				usb_buf[i] =
553 554 555 556 557
					audio_buf[cdev->audio_out_buf_pos[stream]];
				cdev->period_out_count[stream]++;
				cdev->audio_out_buf_pos[stream]++;
				if (cdev->audio_out_buf_pos[stream] == sz)
					cdev->audio_out_buf_pos[stream] = 0;
558
			} else
559 560
				usb_buf[i] = 0;
		}
561 562

		/* fill in the check bytes */
563 564 565 566 567
		if (cdev->spec.data_alignment == 2 &&
		    i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
		        (cdev->n_streams * CHANNELS_PER_STREAM))
			for (stream = 0; stream < cdev->n_streams; stream++, i++)
				usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
568 569 570
	}
}

571
static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
572 573 574 575 576 577 578
				struct urb *urb,
				const struct usb_iso_packet_descriptor *iso)
{
	unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
	int stream, i;

	for (i = 0; i < iso->length;) {
579 580
		for (stream = 0; stream < cdev->n_streams; stream++) {
			struct snd_pcm_substream *sub = cdev->sub_playback[stream];
581 582 583 584 585 586 587 588 589 590 591 592
			char *audio_buf = NULL;
			int c, n, sz = 0;

			if (sub) {
				struct snd_pcm_runtime *rt = sub->runtime;
				audio_buf = rt->dma_area;
				sz = frames_to_bytes(rt, rt->buffer_size);
			}

			for (c = 0; c < CHANNELS_PER_STREAM; c++) {
				for (n = 0; n < BYTES_PER_SAMPLE; n++) {
					if (audio_buf) {
593
						usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
594

595 596
						if (cdev->audio_out_buf_pos[stream] == sz)
							cdev->audio_out_buf_pos[stream] = 0;
597 598 599 600 601 602
					} else {
						usb_buf[i+n] = 0;
					}
				}

				if (audio_buf)
603
					cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
604 605 606 607 608 609 610 611 612 613

				i += BYTES_PER_SAMPLE;

				/* fill in the check byte pattern */
				usb_buf[i++] = (stream << 1) | c;
			}
		}
	}
}

614
static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
615 616 617
				struct urb *urb,
				const struct usb_iso_packet_descriptor *iso)
{
618
	switch (cdev->spec.data_alignment) {
619 620
	case 0:
	case 2:
621
		fill_out_urb_mode_0(cdev, urb, iso);
622 623
		break;
	case 3:
624
		fill_out_urb_mode_3(cdev, urb, iso);
625
		break;
626 627 628 629 630
	}
}

static void read_completed(struct urb *urb)
{
631
	struct snd_usb_caiaq_cb_info *info = urb->context;
632
	struct snd_usb_caiaqdev *cdev;
633
	struct device *dev;
634 635
	struct urb *out = NULL;
	int i, frame, len, send_it = 0, outframe = 0;
636
	size_t offset = 0;
637 638 639 640

	if (urb->status || !info)
		return;

641
	cdev = info->cdev;
642
	dev = caiaqdev_to_dev(cdev);
643

644
	if (!cdev->streaming)
645 646
		return;

647 648
	/* find an unused output urb that is unused */
	for (i = 0; i < N_URBS; i++)
649 650
		if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
			out = cdev->data_urbs_out[i];
651 652 653 654
			break;
		}

	if (!out) {
655
		dev_err(dev, "Unable to find an output urb to use\n");
656 657
		goto requeue;
	}
658 659 660 661 662 663 664 665 666 667

	/* read the recently received packet and send back one which has
	 * the same layout */
	for (frame = 0; frame < FRAMES_PER_URB; frame++) {
		if (urb->iso_frame_desc[frame].status)
			continue;

		len = urb->iso_frame_desc[outframe].actual_length;
		out->iso_frame_desc[outframe].length = len;
		out->iso_frame_desc[outframe].actual_length = 0;
668 669
		out->iso_frame_desc[outframe].offset = offset;
		offset += len;
670

671
		if (len > 0) {
672 673 674 675 676 677
			spin_lock(&cdev->spinlock);
			fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
			read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
			spin_unlock(&cdev->spinlock);
			check_for_elapsed_periods(cdev, cdev->sub_playback);
			check_for_elapsed_periods(cdev, cdev->sub_capture);
678 679 680 681 682 683 684
			send_it = 1;
		}

		outframe++;
	}

	if (send_it) {
685
		out->number_of_packets = outframe;
686 687
		out->transfer_flags = URB_ISO_ASAP;
		usb_submit_urb(out, GFP_ATOMIC);
688 689
	} else {
		struct snd_usb_caiaq_cb_info *oinfo = out->context;
690
		clear_bit(oinfo->index, &cdev->outurb_active_mask);
691
	}
692

693
requeue:
694 695 696 697 698 699
	/* re-submit inbound urb */
	for (frame = 0; frame < FRAMES_PER_URB; frame++) {
		urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
		urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
		urb->iso_frame_desc[frame].actual_length = 0;
	}
700

701 702 703 704 705 706 707 708
	urb->number_of_packets = FRAMES_PER_URB;
	urb->transfer_flags = URB_ISO_ASAP;
	usb_submit_urb(urb, GFP_ATOMIC);
}

static void write_completed(struct urb *urb)
{
	struct snd_usb_caiaq_cb_info *info = urb->context;
709
	struct snd_usb_caiaqdev *cdev = info->cdev;
710

711 712 713
	if (!cdev->output_running) {
		cdev->output_running = 1;
		wake_up(&cdev->prepare_wait_queue);
714
	}
715

716
	clear_bit(info->index, &cdev->outurb_active_mask);
717 718
}

719
static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
720 721 722
{
	int i, frame;
	struct urb **urbs;
723
	struct usb_device *usb_dev = cdev->chip.dev;
724
	struct device *dev = caiaqdev_to_dev(cdev);
725 726
	unsigned int pipe;

727
	pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
728 729 730 731 732
		usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
		usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);

	urbs = kmalloc(N_URBS * sizeof(*urbs), GFP_KERNEL);
	if (!urbs) {
733
		dev_err(dev, "unable to kmalloc() urbs, OOM!?\n");
734 735 736 737 738 739 740
		*ret = -ENOMEM;
		return NULL;
	}

	for (i = 0; i < N_URBS; i++) {
		urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
		if (!urbs[i]) {
741
			dev_err(dev, "unable to usb_alloc_urb(), OOM!?\n");
742 743 744 745
			*ret = -ENOMEM;
			return urbs;
		}

746
		urbs[i]->transfer_buffer =
747 748
			kmalloc(FRAMES_PER_URB * BYTES_PER_FRAME, GFP_KERNEL);
		if (!urbs[i]->transfer_buffer) {
749
			dev_err(dev, "unable to kmalloc() transfer buffer, OOM!?\n");
750 751 752
			*ret = -ENOMEM;
			return urbs;
		}
753

754
		for (frame = 0; frame < FRAMES_PER_URB; frame++) {
755
			struct usb_iso_packet_descriptor *iso =
756
				&urbs[i]->iso_frame_desc[frame];
757

758 759 760
			iso->offset = BYTES_PER_FRAME * frame;
			iso->length = BYTES_PER_FRAME;
		}
761

762 763
		urbs[i]->dev = usb_dev;
		urbs[i]->pipe = pipe;
764
		urbs[i]->transfer_buffer_length = FRAMES_PER_URB
765
						* BYTES_PER_FRAME;
766
		urbs[i]->context = &cdev->data_cb_info[i];
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
		urbs[i]->interval = 1;
		urbs[i]->transfer_flags = URB_ISO_ASAP;
		urbs[i]->number_of_packets = FRAMES_PER_URB;
		urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
					read_completed : write_completed;
	}

	*ret = 0;
	return urbs;
}

static void free_urbs(struct urb **urbs)
{
	int i;

	if (!urbs)
		return;

	for (i = 0; i < N_URBS; i++) {
		if (!urbs[i])
			continue;
788

789 790 791 792 793 794 795 796
		usb_kill_urb(urbs[i]);
		kfree(urbs[i]->transfer_buffer);
		usb_free_urb(urbs[i]);
	}

	kfree(urbs);
}

797
int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
798 799
{
	int i, ret;
800
	struct device *dev = caiaqdev_to_dev(cdev);
801

802 803
	cdev->n_audio_in  = max(cdev->spec.num_analog_audio_in,
			       cdev->spec.num_digital_audio_in) /
804
				CHANNELS_PER_STREAM;
805 806
	cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
			       cdev->spec.num_digital_audio_out) /
807
				CHANNELS_PER_STREAM;
808
	cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
809

810 811 812
	dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in);
	dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out);
	dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams);
813

814
	if (cdev->n_streams > MAX_STREAMS) {
815
		dev_err(dev, "unable to initialize device, too many streams.\n");
816 817 818
		return -EINVAL;
	}

819 820
	ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
			cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
821 822

	if (ret < 0) {
823
		dev_err(dev, "snd_pcm_new() returned %d\n", ret);
824 825 826
		return ret;
	}

827 828
	cdev->pcm->private_data = cdev;
	strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
829

830 831
	memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
	memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
832

833
	memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
834 835 836
			sizeof(snd_usb_caiaq_pcm_hardware));

	/* setup samplerates */
837 838
	cdev->samplerates = cdev->pcm_info.rates;
	switch (cdev->chip.usb_id) {
839
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
840
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
841
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
842
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
843
		cdev->samplerates |= SNDRV_PCM_RATE_192000;
844
		/* fall thru */
845
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
846
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
847
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
848
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
849
		cdev->samplerates |= SNDRV_PCM_RATE_88200;
850 851 852
		break;
	}

853
	snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
854
				&snd_usb_caiaq_ops);
855
	snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
856 857
				&snd_usb_caiaq_ops);

858
	snd_pcm_lib_preallocate_pages_for_all(cdev->pcm,
859 860 861 862
					SNDRV_DMA_TYPE_CONTINUOUS,
					snd_dma_continuous_data(GFP_KERNEL),
					MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);

863
	cdev->data_cb_info =
864
		kmalloc(sizeof(struct snd_usb_caiaq_cb_info) * N_URBS,
865 866
					GFP_KERNEL);

867
	if (!cdev->data_cb_info)
868 869
		return -ENOMEM;

870 871
	cdev->outurb_active_mask = 0;
	BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8));
872

873
	for (i = 0; i < N_URBS; i++) {
874 875
		cdev->data_cb_info[i].cdev = cdev;
		cdev->data_cb_info[i].index = i;
876
	}
877

878
	cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret);
879
	if (ret < 0) {
880 881
		kfree(cdev->data_cb_info);
		free_urbs(cdev->data_urbs_in);
882 883
		return ret;
	}
884

885
	cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
886
	if (ret < 0) {
887 888 889
		kfree(cdev->data_cb_info);
		free_urbs(cdev->data_urbs_in);
		free_urbs(cdev->data_urbs_out);
890 891 892 893 894 895
		return ret;
	}

	return 0;
}

896
void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev)
897
{
898 899 900
	struct device *dev = caiaqdev_to_dev(cdev);

	dev_dbg(dev, "%s(%p)\n", __func__, cdev);
901 902 903 904
	stream_stop(cdev);
	free_urbs(cdev->data_urbs_in);
	free_urbs(cdev->data_urbs_out);
	kfree(cdev->data_cb_info);
905 906
}