playback.c 14.1 KB
Newer Older
M
Markus Grabner 已提交
1
/*
2
 * Line 6 Linux USB driver
M
Markus Grabner 已提交
3
 *
4
 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
M
Markus Grabner 已提交
5 6 7 8 9 10 11
 *
 *	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, version 2.
 *
 */

12
#include <linux/slab.h>
M
Markus Grabner 已提交
13 14 15 16
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>

17 18
#include "capture.h"
#include "driver.h"
M
Markus Grabner 已提交
19
#include "pcm.h"
20
#include "playback.h"
M
Markus Grabner 已提交
21 22 23 24

/*
	Software stereo volume control.
*/
25 26
static void change_volume(struct urb *urb_out, int volume[],
			  int bytes_per_frame)
M
Markus Grabner 已提交
27 28 29
{
	int chn = 0;

30
	if (volume[0] == 256 && volume[1] == 256)
31
		return;		/* maximum volume - no change */
M
Markus Grabner 已提交
32

33
	if (bytes_per_frame == 4) {
M
Markus Grabner 已提交
34
		short *p, *buf_end;
35

M
Markus Grabner 已提交
36 37 38
		p = (short *)urb_out->transfer_buffer;
		buf_end = p + urb_out->transfer_buffer_length / sizeof(*p);

39
		for (; p < buf_end; ++p) {
M
Markus Grabner 已提交
40 41 42
			*p = (*p * volume[chn & 1]) >> 8;
			++chn;
		}
43
	} else if (bytes_per_frame == 6) {
M
Markus Grabner 已提交
44
		unsigned char *p, *buf_end;
45

M
Markus Grabner 已提交
46 47 48
		p = (unsigned char *)urb_out->transfer_buffer;
		buf_end = p + urb_out->transfer_buffer_length;

49 50
		for (; p < buf_end; p += 3) {
			int val;
51

52
			val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16);
M
Markus Grabner 已提交
53 54 55 56 57 58 59 60 61
			val = (val * volume[chn & 1]) >> 8;
			p[0] = val;
			p[1] = val >> 8;
			p[2] = val >> 16;
			++chn;
		}
	}
}

62 63 64 65 66 67 68 69 70
/*
	Create signal for impulse response test.
*/
static void create_impulse_test_signal(struct snd_line6_pcm *line6pcm,
				       struct urb *urb_out, int bytes_per_frame)
{
	int frames = urb_out->transfer_buffer_length / bytes_per_frame;

	if (bytes_per_frame == 4) {
71 72 73 74 75 76 77 78 79 80
		int i;
		short *pi = (short *)line6pcm->prev_fbuf;
		short *po = (short *)urb_out->transfer_buffer;

		for (i = 0; i < frames; ++i) {
			po[0] = pi[0];
			po[1] = 0;
			pi += 2;
			po += 2;
		}
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	} else if (bytes_per_frame == 6) {
		int i, j;
		unsigned char *pi = line6pcm->prev_fbuf;
		unsigned char *po = urb_out->transfer_buffer;

		for (i = 0; i < frames; ++i) {
			for (j = 0; j < bytes_per_frame / 2; ++j)
				po[j] = pi[j];

			for (; j < bytes_per_frame; ++j)
				po[j] = 0;

			pi += bytes_per_frame;
			po += bytes_per_frame;
		}
96 97 98 99 100 101
	}
	if (--line6pcm->impulse_count <= 0) {
		((unsigned char *)(urb_out->transfer_buffer))[bytes_per_frame -
							      1] =
		    line6pcm->impulse_volume;
		line6pcm->impulse_count = line6pcm->impulse_period;
102 103 104 105 106 107 108 109 110 111 112 113 114 115
	}
}

/*
	Add signal to buffer for software monitoring.
*/
static void add_monitor_signal(struct urb *urb_out, unsigned char *signal,
			       int volume, int bytes_per_frame)
{
	if (volume == 0)
		return;		/* zero volume - no change */

	if (bytes_per_frame == 4) {
		short *pi, *po, *buf_end;
116

117 118 119 120 121 122 123 124 125
		pi = (short *)signal;
		po = (short *)urb_out->transfer_buffer;
		buf_end = po + urb_out->transfer_buffer_length / sizeof(*po);

		for (; po < buf_end; ++pi, ++po)
			*po += (*pi * volume) >> 8;
	}

	/*
126 127 128
	   We don't need to handle devices with 6 bytes per frame here
	   since they all support hardware monitoring.
	 */
129 130
}

M
Markus Grabner 已提交
131 132 133
/*
	Find a free URB, prepare audio data, and submit URB.
*/
134
static int submit_audio_out_urb(struct snd_line6_pcm *line6pcm)
M
Markus Grabner 已提交
135 136 137 138
{
	int index;
	unsigned long flags;
	int i, urb_size, urb_frames;
139
	int ret;
M
Markus Grabner 已提交
140
	const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
141 142 143 144 145
	const int frame_increment =
	    line6pcm->properties->snd_line6_rates.rats[0].num_min;
	const int frame_factor =
	    line6pcm->properties->snd_line6_rates.rats[0].den *
	    (USB_INTERVALS_PER_SECOND / LINE6_ISO_INTERVAL);
M
Markus Grabner 已提交
146 147 148
	struct urb *urb_out;

	spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
149 150
	index =
	    find_first_zero_bit(&line6pcm->active_urb_out, LINE6_ISO_BUFFERS);
M
Markus Grabner 已提交
151

152
	if (index < 0 || index >= LINE6_ISO_BUFFERS) {
M
Markus Grabner 已提交
153
		spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
154
		dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
M
Markus Grabner 已提交
155 156 157 158 159 160
		return -EINVAL;
	}

	urb_out = line6pcm->urb_audio_out[index];
	urb_size = 0;

161
	for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
M
Markus Grabner 已提交
162
		/* compute frame size for given sampling rate */
163
		int fsize = 0;
164 165
		struct usb_iso_packet_descriptor *fout =
		    &urb_out->iso_frame_desc[i];
166

167
		if (line6pcm->flags & LINE6_BITS_CAPTURE_STREAM)
168 169 170 171
			fsize = line6pcm->prev_fsize;

		if (fsize == 0) {
			int n;
172

173 174 175 176 177 178
			line6pcm->count_out += frame_increment;
			n = line6pcm->count_out / frame_factor;
			line6pcm->count_out -= n * frame_factor;
			fsize = n * bytes_per_frame;
		}

M
Markus Grabner 已提交
179
		fout->offset = urb_size;
180 181 182 183 184 185 186
		fout->length = fsize;
		urb_size += fsize;
	}

	if (urb_size == 0) {
		/* can't determine URB size */
		spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
187
		dev_err(line6pcm->line6->ifcdev, "driver bug: urb_size = 0\n");
188
		return -EINVAL;
M
Markus Grabner 已提交
189 190 191
	}

	urb_frames = urb_size / bytes_per_frame;
192 193
	urb_out->transfer_buffer =
	    line6pcm->buffer_out +
194
	    index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
195 196 197
	urb_out->transfer_buffer_length = urb_size;
	urb_out->context = line6pcm;

198 199
	if (test_bit(LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM, &line6pcm->flags) &&
	    !test_bit(LINE6_INDEX_PAUSE_PLAYBACK, &line6pcm->flags)) {
200 201
		struct snd_pcm_runtime *runtime =
		    get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK)->runtime;
M
Markus Grabner 已提交
202

203
		if (line6pcm->pos_out + urb_frames > runtime->buffer_size) {
M
Markus Grabner 已提交
204
			/*
205 206 207
			   The transferred area goes over buffer boundary,
			   copy the data to the temp buffer.
			 */
M
Markus Grabner 已提交
208
			int len;
209

M
Markus Grabner 已提交
210 211
			len = runtime->buffer_size - line6pcm->pos_out;

212
			if (len > 0) {
213
				memcpy(urb_out->transfer_buffer,
214 215 216
				       runtime->dma_area +
				       line6pcm->pos_out * bytes_per_frame,
				       len * bytes_per_frame);
217
				memcpy(urb_out->transfer_buffer +
218 219
				       len * bytes_per_frame, runtime->dma_area,
				       (urb_frames - len) * bytes_per_frame);
220
			} else
221 222
				dev_err(line6pcm->line6->ifcdev, "driver bug: len = %d\n",
					len);
223
		} else {
224 225 226 227
			memcpy(urb_out->transfer_buffer,
			       runtime->dma_area +
			       line6pcm->pos_out * bytes_per_frame,
			       urb_out->transfer_buffer_length);
M
Markus Grabner 已提交
228 229
		}

230 231
		line6pcm->pos_out += urb_frames;
		if (line6pcm->pos_out >= runtime->buffer_size)
232 233 234 235 236
			line6pcm->pos_out -= runtime->buffer_size;
	} else {
		memset(urb_out->transfer_buffer, 0,
		       urb_out->transfer_buffer_length);
	}
M
Markus Grabner 已提交
237

238
	change_volume(urb_out, line6pcm->volume_playback, bytes_per_frame);
M
Markus Grabner 已提交
239

240
	if (line6pcm->prev_fbuf != NULL) {
241
		if (line6pcm->flags & LINE6_BITS_PCM_IMPULSE) {
242 243
			create_impulse_test_signal(line6pcm, urb_out,
						   bytes_per_frame);
244 245
			if (line6pcm->flags &
			    LINE6_BIT_PCM_ALSA_CAPTURE_STREAM) {
246 247 248 249 250
				line6_capture_copy(line6pcm,
						   urb_out->transfer_buffer,
						   urb_out->
						   transfer_buffer_length);
				line6_capture_check_period(line6pcm,
251
					urb_out->transfer_buffer_length);
252 253 254
			}
		} else {
			if (!
255
			    (line6pcm->line6->
256
			     properties->capabilities & LINE6_CAP_HWMON)
257 258
			    && (line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM)
			    && (line6pcm->flags & LINE6_BITS_CAPTURE_STREAM))
259 260 261 262 263
				add_monitor_signal(urb_out, line6pcm->prev_fbuf,
						   line6pcm->volume_monitor,
						   bytes_per_frame);
		}
	}
M
Markus Grabner 已提交
264

265 266 267
	ret = usb_submit_urb(urb_out, GFP_ATOMIC);

	if (ret == 0)
M
Markus Grabner 已提交
268 269
		set_bit(index, &line6pcm->active_urb_out);
	else
270
		dev_err(line6pcm->line6->ifcdev,
271
			"URB out #%d submission failed (%d)\n", index, ret);
M
Markus Grabner 已提交
272 273 274 275 276 277 278 279

	spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
	return 0;
}

/*
	Submit all currently available playback URBs.
*/
280
int line6_submit_audio_out_all_urbs(struct snd_line6_pcm *line6pcm)
M
Markus Grabner 已提交
281 282 283
{
	int ret, i;

284
	for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
285
		ret = submit_audio_out_urb(line6pcm);
286
		if (ret < 0)
M
Markus Grabner 已提交
287
			return ret;
288
	}
M
Markus Grabner 已提交
289 290 291 292 293 294 295

	return 0;
}

/*
	Unlink all currently active playback URBs.
*/
296
void line6_unlink_audio_out_urbs(struct snd_line6_pcm *line6pcm)
M
Markus Grabner 已提交
297 298 299
{
	unsigned int i;

300 301 302
	for (i = LINE6_ISO_BUFFERS; i--;) {
		if (test_bit(i, &line6pcm->active_urb_out)) {
			if (!test_and_set_bit(i, &line6pcm->unlink_urb_out)) {
M
Markus Grabner 已提交
303
				struct urb *u = line6pcm->urb_audio_out[i];
304

M
Markus Grabner 已提交
305 306 307 308 309 310 311
				usb_unlink_urb(u);
			}
		}
	}
}

/*
312 313
	Wait until unlinking of all currently active playback URBs has been
	finished.
M
Markus Grabner 已提交
314
*/
315
void line6_wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
M
Markus Grabner 已提交
316 317 318 319 320 321 322 323 324 325 326
{
	int timeout = HZ;
	unsigned int i;
	int alive;

	do {
		alive = 0;
		for (i = LINE6_ISO_BUFFERS; i--;) {
			if (test_bit(i, &line6pcm->active_urb_out))
				alive++;
		}
327
		if (!alive)
M
Markus Grabner 已提交
328 329 330 331 332 333 334 335 336 337 338
			break;
		set_current_state(TASK_UNINTERRUPTIBLE);
		schedule_timeout(1);
	} while (--timeout > 0);
	if (alive)
		snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
}

/*
	Unlink all currently active playback URBs, and wait for finishing.
*/
339
void line6_unlink_wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
M
Markus Grabner 已提交
340
{
341
	line6_unlink_audio_out_urbs(line6pcm);
342
	line6_wait_clear_audio_out_urbs(line6pcm);
343 344 345 346 347 348 349 350
}

void line6_free_playback_buffer(struct snd_line6_pcm *line6pcm)
{
	kfree(line6pcm->buffer_out);
	line6pcm->buffer_out = NULL;
}

M
Markus Grabner 已提交
351 352 353
/*
	Callback for completed playback URB.
*/
354
static void audio_out_callback(struct urb *urb)
M
Markus Grabner 已提交
355 356 357
{
	int i, index, length = 0, shutdown = 0;
	unsigned long flags;
358
	struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
359
	struct snd_pcm_substream *substream =
360 361 362 363 364 365 366
	    get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK);

#if USE_CLEAR_BUFFER_WORKAROUND
	memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
#endif

	line6pcm->last_frame_out = urb->start_frame;
M
Markus Grabner 已提交
367 368

	/* find index of URB */
369 370
	for (index = LINE6_ISO_BUFFERS; index--;)
		if (urb == line6pcm->urb_audio_out[index])
M
Markus Grabner 已提交
371 372
			break;

373
	if (index < 0)
374
		return;		/* URB has been unlinked asynchronously */
M
Markus Grabner 已提交
375

376
	for (i = LINE6_ISO_PACKETS; i--;)
M
Markus Grabner 已提交
377 378 379 380
		length += urb->iso_frame_desc[i].length;

	spin_lock_irqsave(&line6pcm->lock_audio_out, flags);

381
	if (test_bit(LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM, &line6pcm->flags)) {
382
		struct snd_pcm_runtime *runtime = substream->runtime;
383

384 385 386 387 388 389
		line6pcm->pos_out_done +=
		    length / line6pcm->properties->bytes_per_frame;

		if (line6pcm->pos_out_done >= runtime->buffer_size)
			line6pcm->pos_out_done -= runtime->buffer_size;
	}
M
Markus Grabner 已提交
390 391 392

	clear_bit(index, &line6pcm->active_urb_out);

393
	for (i = LINE6_ISO_PACKETS; i--;)
394
		if (urb->iso_frame_desc[i].status == -EXDEV) {
M
Markus Grabner 已提交
395 396 397 398
			shutdown = 1;
			break;
		}

399
	if (test_and_clear_bit(index, &line6pcm->unlink_urb_out))
M
Markus Grabner 已提交
400 401 402 403
		shutdown = 1;

	spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);

404
	if (!shutdown) {
405
		submit_audio_out_urb(line6pcm);
M
Markus Grabner 已提交
406

407 408
		if (test_bit(LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM,
			     &line6pcm->flags)) {
409 410
			line6pcm->bytes_out += length;
			if (line6pcm->bytes_out >= line6pcm->period_out) {
411 412 413
				line6pcm->bytes_out %= line6pcm->period_out;
				snd_pcm_period_elapsed(substream);
			}
M
Markus Grabner 已提交
414 415 416 417 418 419 420 421 422 423 424
		}
	}
}

/* open playback callback */
static int snd_line6_playback_open(struct snd_pcm_substream *substream)
{
	int err;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);

425
	err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
426 427
					    (&line6pcm->
					     properties->snd_line6_rates));
428
	if (err < 0)
M
Markus Grabner 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441
		return err;

	runtime->hw = line6pcm->properties->snd_line6_playback_hw;
	return 0;
}

/* close playback callback */
static int snd_line6_playback_close(struct snd_pcm_substream *substream)
{
	return 0;
}

/* hw_params playback callback */
442 443
static int snd_line6_playback_hw_params(struct snd_pcm_substream *substream,
					struct snd_pcm_hw_params *hw_params)
M
Markus Grabner 已提交
444 445 446 447 448 449
{
	int ret;
	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);

	/* -- Florian Demski [FD] */
	/* don't ask me why, but this fixes the bug on my machine */
450 451
	if (line6pcm == NULL) {
		if (substream->pcm == NULL)
M
Markus Grabner 已提交
452
			return -ENOMEM;
453
		if (substream->pcm->private_data == NULL)
M
Markus Grabner 已提交
454 455
			return -ENOMEM;
		substream->private_data = substream->pcm->private_data;
456
		line6pcm = snd_pcm_substream_chip(substream);
M
Markus Grabner 已提交
457 458 459
	}
	/* -- [FD] end */

460
	ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER);
461

462 463
	if (ret < 0)
		return ret;
464

465 466
	ret = snd_pcm_lib_malloc_pages(substream,
				       params_buffer_bytes(hw_params));
467 468
	if (ret < 0) {
		line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER);
M
Markus Grabner 已提交
469
		return ret;
470
	}
M
Markus Grabner 已提交
471 472 473 474 475 476 477 478

	line6pcm->period_out = params_period_bytes(hw_params);
	return 0;
}

/* hw_free playback callback */
static int snd_line6_playback_hw_free(struct snd_pcm_substream *substream)
{
479
	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
480

481
	line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER);
M
Markus Grabner 已提交
482 483 484 485
	return snd_pcm_lib_free_pages(substream);
}

/* trigger playback callback */
486
int snd_line6_playback_trigger(struct snd_line6_pcm *line6pcm, int cmd)
M
Markus Grabner 已提交
487 488 489
{
	int err;

490
	switch (cmd) {
M
Markus Grabner 已提交
491
	case SNDRV_PCM_TRIGGER_START:
492
	case SNDRV_PCM_TRIGGER_RESUME:
493 494
		err = line6_pcm_acquire(line6pcm,
					LINE6_BIT_PCM_ALSA_PLAYBACK_STREAM);
M
Markus Grabner 已提交
495

496 497
		if (err < 0)
			return err;
M
Markus Grabner 已提交
498 499 500 501

		break;

	case SNDRV_PCM_TRIGGER_STOP:
502
	case SNDRV_PCM_TRIGGER_SUSPEND:
503 504
		err = line6_pcm_release(line6pcm,
					LINE6_BIT_PCM_ALSA_PLAYBACK_STREAM);
505 506 507

		if (err < 0)
			return err;
M
Markus Grabner 已提交
508 509 510 511

		break;

	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
512
		set_bit(LINE6_INDEX_PAUSE_PLAYBACK, &line6pcm->flags);
M
Markus Grabner 已提交
513 514 515
		break;

	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
516
		clear_bit(LINE6_INDEX_PAUSE_PLAYBACK, &line6pcm->flags);
M
Markus Grabner 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530
		break;

	default:
		return -EINVAL;
	}

	return 0;
}

/* playback pointer callback */
static snd_pcm_uframes_t
snd_line6_playback_pointer(struct snd_pcm_substream *substream)
{
	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
531

M
Markus Grabner 已提交
532 533 534 535 536
	return line6pcm->pos_out_done;
}

/* playback operators */
struct snd_pcm_ops snd_line6_playback_ops = {
537 538 539 540 541 542 543 544
	.open = snd_line6_playback_open,
	.close = snd_line6_playback_close,
	.ioctl = snd_pcm_lib_ioctl,
	.hw_params = snd_line6_playback_hw_params,
	.hw_free = snd_line6_playback_hw_free,
	.prepare = snd_line6_prepare,
	.trigger = snd_line6_trigger,
	.pointer = snd_line6_playback_pointer,
M
Markus Grabner 已提交
545 546
};

547
int line6_create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
M
Markus Grabner 已提交
548
{
549
	struct usb_line6 *line6 = line6pcm->line6;
M
Markus Grabner 已提交
550 551 552
	int i;

	/* create audio URBs and fill in constant values: */
553
	for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
M
Markus Grabner 已提交
554 555 556
		struct urb *urb;

		/* URB for audio out: */
557 558
		urb = line6pcm->urb_audio_out[i] =
		    usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
M
Markus Grabner 已提交
559

560
		if (urb == NULL)
M
Markus Grabner 已提交
561 562
			return -ENOMEM;

563
		urb->dev = line6->usbdev;
564
		urb->pipe =
565 566
		    usb_sndisocpipe(line6->usbdev,
				    line6->properties->ep_audio_w &
567
				    USB_ENDPOINT_NUMBER_MASK);
M
Markus Grabner 已提交
568 569 570 571 572 573 574 575 576 577
		urb->transfer_flags = URB_ISO_ASAP;
		urb->start_frame = -1;
		urb->number_of_packets = LINE6_ISO_PACKETS;
		urb->interval = LINE6_ISO_INTERVAL;
		urb->error_count = 0;
		urb->complete = audio_out_callback;
	}

	return 0;
}