pcm.c 12.7 KB
Newer Older
M
Markus Grabner 已提交
1
/*
2
 * Line6 Linux USB driver - 0.9.1beta
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>
T
Takashi Iwai 已提交
13
#include <linux/export.h>
M
Markus Grabner 已提交
14 15 16 17 18 19
#include <sound/core.h>
#include <sound/control.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>

#include "capture.h"
20
#include "driver.h"
M
Markus Grabner 已提交
21 22
#include "playback.h"

23 24 25
/* impulse response volume controls */
static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_info *uinfo)
26
{
27 28 29 30 31
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 255;
	return 0;
32 33
}

34 35
static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
36
{
37 38 39 40
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);

	ucontrol->value.integer.value[0] = line6pcm->impulse_volume;
	return 0;
41 42
}

43 44
static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
45
{
46 47
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
	int value = ucontrol->value.integer.value[0];
48

49 50
	if (line6pcm->impulse_volume == value)
		return 0;
51

52
	line6pcm->impulse_volume = value;
53
	if (value > 0)
54
		line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
55
	else
56
		line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
57 58
	return 1;
}
59

60 61 62 63 64 65 66 67 68
/* impulse response period controls */
static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 2000;
	return 0;
69 70
}

71 72
static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
73
{
74 75 76 77
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);

	ucontrol->value.integer.value[0] = line6pcm->impulse_period;
	return 0;
78 79
}

80 81
static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
82
{
83 84
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
	int value = ucontrol->value.integer.value[0];
85

86 87
	if (line6pcm->impulse_period == value)
		return 0;
88

89 90
	line6pcm->impulse_period = value;
	return 1;
91 92
}

93 94 95 96 97 98
static bool test_flags(unsigned long flags0, unsigned long flags1,
		       unsigned long mask)
{
	return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
}

99
int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
100
{
101 102 103 104 105 106 107 108 109
	unsigned long flags_old, flags_new, flags_final;
	int err;

	do {
		flags_old = ACCESS_ONCE(line6pcm->flags);
		flags_new = flags_old | channels;
	} while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);

	flags_final = flags_old;
110

111
	line6pcm->prev_fbuf = NULL;
112

113
	if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
114
		/* Invoked multiple times in a row so allocate once only */
115 116 117 118 119 120 121 122 123 124 125 126 127 128
		if (!line6pcm->buffer_in) {
			line6pcm->buffer_in =
				kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
					line6pcm->max_packet_size, GFP_KERNEL);
			if (!line6pcm->buffer_in) {
				err = -ENOMEM;
				goto pcm_acquire_error;
			}

			flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
		}
	}

	if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
129
		/*
130 131 132 133
		   Waiting for completion of active URBs in the stop handler is
		   a bug, we therefore report an error if capturing is restarted
		   too soon.
		 */
134 135
		if (line6pcm->active_urb_in | line6pcm->unlink_urb_in) {
			dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
136
			return -EBUSY;
137 138
		}

139 140 141
		line6pcm->count_in = 0;
		line6pcm->prev_fsize = 0;
		err = line6_submit_audio_in_all_urbs(line6pcm);
142

143
		if (err < 0)
144 145 146
			goto pcm_acquire_error;

		flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
147
	}
148

149
	if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
150
		/* Invoked multiple times in a row so allocate once only */
151 152 153 154 155 156 157 158
		if (!line6pcm->buffer_out) {
			line6pcm->buffer_out =
				kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
					line6pcm->max_packet_size, GFP_KERNEL);
			if (!line6pcm->buffer_out) {
				err = -ENOMEM;
				goto pcm_acquire_error;
			}
159

160 161 162
			flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
		}
	}
163

164 165 166 167 168 169 170
	if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
		/*
		  See comment above regarding PCM restart.
		*/
		if (line6pcm->active_urb_out | line6pcm->unlink_urb_out) {
			dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
			return -EBUSY;
171 172
		}

173 174
		line6pcm->count_out = 0;
		err = line6_submit_audio_out_all_urbs(line6pcm);
175

176
		if (err < 0)
177 178 179
			goto pcm_acquire_error;

		flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
180
	}
181

182
	return 0;
183

184 185 186 187 188 189
pcm_acquire_error:
	/*
	   If not all requested resources/streams could be obtained, release
	   those which were successfully obtained (if any).
	*/
	line6_pcm_release(line6pcm, flags_final & channels);
190
	return err;
191
}
T
Takashi Iwai 已提交
192
EXPORT_SYMBOL_GPL(line6_pcm_acquire);
193

194
int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
195
{
196 197 198 199 200 201
	unsigned long flags_old, flags_new;

	do {
		flags_old = ACCESS_ONCE(line6pcm->flags);
		flags_new = flags_old & ~channels;
	} while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
202

203
	if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM))
204
		line6_unlink_audio_in_urbs(line6pcm);
205

206 207 208
	if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
		line6_wait_clear_audio_in_urbs(line6pcm);
		line6_free_capture_buffer(line6pcm);
209 210
	}

211
	if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
212
		line6_unlink_audio_out_urbs(line6pcm);
213

214 215 216
	if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
		line6_wait_clear_audio_out_urbs(line6pcm);
		line6_free_playback_buffer(line6pcm);
217 218 219 220
	}

	return 0;
}
T
Takashi Iwai 已提交
221
EXPORT_SYMBOL_GPL(line6_pcm_release);
222

M
Markus Grabner 已提交
223 224 225 226 227 228 229
/* trigger callback */
int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
{
	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *s;
	int err;

230
	spin_lock(&line6pcm->lock_trigger);
231
	clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
M
Markus Grabner 已提交
232 233

	snd_pcm_group_for_each_entry(s, substream) {
234 235
		if (s->pcm->card != substream->pcm->card)
			continue;
236
		switch (s->stream) {
M
Markus Grabner 已提交
237
		case SNDRV_PCM_STREAM_PLAYBACK:
238
			err = snd_line6_playback_trigger(line6pcm, cmd);
M
Markus Grabner 已提交
239

240
			if (err < 0) {
241
				spin_unlock(&line6pcm->lock_trigger);
M
Markus Grabner 已提交
242 243 244 245 246 247
				return err;
			}

			break;

		case SNDRV_PCM_STREAM_CAPTURE:
248
			err = snd_line6_capture_trigger(line6pcm, cmd);
M
Markus Grabner 已提交
249

250
			if (err < 0) {
251
				spin_unlock(&line6pcm->lock_trigger);
M
Markus Grabner 已提交
252 253 254 255 256 257
				return err;
			}

			break;

		default:
258 259
			dev_err(line6pcm->line6->ifcdev,
				"Unknown stream direction %d\n", s->stream);
M
Markus Grabner 已提交
260 261 262
		}
	}

263
	spin_unlock(&line6pcm->lock_trigger);
M
Markus Grabner 已提交
264 265 266 267
	return 0;
}

/* control info callback */
268 269
static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_info *uinfo)
270
{
M
Markus Grabner 已提交
271 272 273 274 275 276 277 278
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 256;
	return 0;
}

/* control get callback */
279 280
static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *ucontrol)
281
{
M
Markus Grabner 已提交
282 283 284
	int i;
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);

285
	for (i = 2; i--;)
286
		ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
M
Markus Grabner 已提交
287 288 289 290 291

	return 0;
}

/* control put callback */
292 293
static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *ucontrol)
294
{
M
Markus Grabner 已提交
295 296 297
	int i, changed = 0;
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);

298
	for (i = 2; i--;)
299 300 301 302
		if (line6pcm->volume_playback[i] !=
		    ucontrol->value.integer.value[i]) {
			line6pcm->volume_playback[i] =
			    ucontrol->value.integer.value[i];
M
Markus Grabner 已提交
303 304 305 306 307 308 309
			changed = 1;
		}

	return changed;
}

/* control definition */
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
static struct snd_kcontrol_new line6_controls[] = {
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "PCM Playback Volume",
		.info = snd_line6_control_playback_info,
		.get = snd_line6_control_playback_get,
		.put = snd_line6_control_playback_put
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Impulse Response Volume",
		.info = snd_line6_impulse_volume_info,
		.get = snd_line6_impulse_volume_get,
		.put = snd_line6_impulse_volume_put
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Impulse Response Period",
		.info = snd_line6_impulse_period_info,
		.get = snd_line6_impulse_period_get,
		.put = snd_line6_impulse_period_put
	},
M
Markus Grabner 已提交
332 333 334 335 336 337 338 339 340 341
};

/*
	Cleanup the PCM device.
*/
static void line6_cleanup_pcm(struct snd_pcm *pcm)
{
	int i;
	struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);

342 343
	for (i = LINE6_ISO_BUFFERS; i--;) {
		if (line6pcm->urb_audio_out[i]) {
M
Markus Grabner 已提交
344 345 346
			usb_kill_urb(line6pcm->urb_audio_out[i]);
			usb_free_urb(line6pcm->urb_audio_out[i]);
		}
347
		if (line6pcm->urb_audio_in[i]) {
M
Markus Grabner 已提交
348 349 350 351
			usb_kill_urb(line6pcm->urb_audio_in[i]);
			usb_free_urb(line6pcm->urb_audio_in[i]);
		}
	}
352
	kfree(line6pcm);
M
Markus Grabner 已提交
353 354 355
}

/* create a PCM device */
356
static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
M
Markus Grabner 已提交
357 358 359 360
{
	struct snd_pcm *pcm;
	int err;

361 362
	err = snd_pcm_new(line6->card, (char *)line6->properties->name,
			  0, 1, 1, pcm_ret);
363
	if (err < 0)
M
Markus Grabner 已提交
364
		return err;
365 366
	pcm = *pcm_ret;
	strcpy(pcm->name, line6->properties->name);
M
Markus Grabner 已提交
367 368

	/* set operators */
369 370
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
			&snd_line6_playback_ops);
371
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
M
Markus Grabner 已提交
372 373

	/* pre-allocation of buffers */
374
	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
375 376 377
					      snd_dma_continuous_data
					      (GFP_KERNEL), 64 * 1024,
					      128 * 1024);
M
Markus Grabner 已提交
378 379 380
	return 0;
}

381 382 383 384 385
/*
	Stop substream if still running.
*/
static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
{
386 387
	if (substream->runtime && snd_pcm_running(substream)) {
		snd_pcm_stream_lock_irq(substream);
388
		snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
389 390
		snd_pcm_stream_unlock_irq(substream);
	}
391 392 393 394 395 396 397
}

/*
	Stop PCM stream.
*/
void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
{
398 399 400 401
	pcm_disconnect_substream(get_substream
				 (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
	pcm_disconnect_substream(get_substream
				 (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
402 403 404
	line6_unlink_wait_clear_audio_out_urbs(line6pcm);
	line6_unlink_wait_clear_audio_in_urbs(line6pcm);
}
T
Takashi Iwai 已提交
405
EXPORT_SYMBOL_GPL(line6_pcm_disconnect);
406

M
Markus Grabner 已提交
407 408 409 410
/*
	Create and register the PCM device and mixer entries.
	Create URBs for playback and capture.
*/
411 412
int line6_init_pcm(struct usb_line6 *line6,
		   struct line6_pcm_properties *properties)
M
Markus Grabner 已提交
413
{
414
	int i, err;
415 416
	unsigned ep_read = line6->properties->ep_audio_r;
	unsigned ep_write = line6->properties->ep_audio_w;
417
	struct snd_pcm *pcm;
M
Markus Grabner 已提交
418 419
	struct snd_line6_pcm *line6pcm;

420
	if (!(line6->properties->capabilities & LINE6_CAP_PCM))
421
		return 0;	/* skip PCM initialization and report success */
M
Markus Grabner 已提交
422

423 424 425
	err = snd_line6_new_pcm(line6, &pcm);
	if (err < 0)
		return err;
M
Markus Grabner 已提交
426

427 428
	line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
	if (!line6pcm)
M
Markus Grabner 已提交
429 430
		return -ENOMEM;

431 432
	line6pcm->pcm = pcm;
	line6pcm->properties = properties;
433 434
	line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
	line6pcm->volume_monitor = 255;
M
Markus Grabner 已提交
435
	line6pcm->line6 = line6;
436 437 438 439 440 441 442 443

	/* Read and write buffers are sized identically, so choose minimum */
	line6pcm->max_packet_size = min(
			usb_maxpacket(line6->usbdev,
				usb_rcvisocpipe(line6->usbdev, ep_read), 0),
			usb_maxpacket(line6->usbdev,
				usb_sndisocpipe(line6->usbdev, ep_write), 1));

M
Markus Grabner 已提交
444 445 446
	spin_lock_init(&line6pcm->lock_audio_out);
	spin_lock_init(&line6pcm->lock_audio_in);
	spin_lock_init(&line6pcm->lock_trigger);
447
	line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
M
Markus Grabner 已提交
448

449 450 451 452 453
	line6->line6pcm = line6pcm;

	pcm->private_data = line6pcm;
	pcm->private_free = line6_cleanup_pcm;

454
	err = line6_create_audio_out_urbs(line6pcm);
455
	if (err < 0)
M
Markus Grabner 已提交
456 457
		return err;

458
	err = line6_create_audio_in_urbs(line6pcm);
459
	if (err < 0)
M
Markus Grabner 已提交
460 461 462
		return err;

	/* mixer: */
463 464 465 466 467 468
	for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
		err = snd_ctl_add(line6->card,
				  snd_ctl_new1(&line6_controls[i], line6pcm));
		if (err < 0)
			return err;
	}
469

M
Markus Grabner 已提交
470 471
	return 0;
}
T
Takashi Iwai 已提交
472
EXPORT_SYMBOL_GPL(line6_init_pcm);
M
Markus Grabner 已提交
473 474 475 476 477 478

/* prepare pcm callback */
int snd_line6_prepare(struct snd_pcm_substream *substream)
{
	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);

479 480
	switch (substream->stream) {
	case SNDRV_PCM_STREAM_PLAYBACK:
481
		if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
482 483
			line6_unlink_wait_clear_audio_out_urbs(line6pcm);

484 485 486
		break;

	case SNDRV_PCM_STREAM_CAPTURE:
487
		if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
488 489
			line6_unlink_wait_clear_audio_in_urbs(line6pcm);

490 491 492
		break;
	}

493
	if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
494
		line6pcm->count_out = 0;
M
Markus Grabner 已提交
495 496 497
		line6pcm->pos_out = 0;
		line6pcm->pos_out_done = 0;
		line6pcm->bytes_out = 0;
498
		line6pcm->count_in = 0;
M
Markus Grabner 已提交
499 500 501 502 503 504
		line6pcm->pos_in_done = 0;
		line6pcm->bytes_in = 0;
	}

	return 0;
}