pcm.c 12.0 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>
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
	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);

109
	flags_final = 0;
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
		if (!line6pcm->in.buffer) {
			line6pcm->in.buffer =
117 118
				kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
					line6pcm->max_packet_size, GFP_KERNEL);
119
			if (!line6pcm->in.buffer) {
120 121 122 123
				err = -ENOMEM;
				goto pcm_acquire_error;
			}
		}
124 125

		flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
126 127 128
	}

	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
		if (line6pcm->in.active_urbs || line6pcm->in.unlink_urbs) {
135
			dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
136 137
			err = -EBUSY;
			goto pcm_acquire_error;
138 139
		}

140
		line6pcm->in.count = 0;
141 142
		line6pcm->prev_fsize = 0;
		err = line6_submit_audio_in_all_urbs(line6pcm);
143

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

		flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
148
	}
149

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

		flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
163
	}
164

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

174
		line6pcm->out.count = 0;
175
		err = line6_submit_audio_out_all_urbs(line6pcm);
176

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

		flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
181
	}
182

183
	return 0;
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).
	*/
190
	line6_pcm_release(line6pcm, flags_final);
191
	return err;
192
}
T
Takashi Iwai 已提交
193
EXPORT_SYMBOL_GPL(line6_pcm_acquire);
194

195
int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
196
{
197 198 199 200 201 202
	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);
203

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

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

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

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

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

M
Markus Grabner 已提交
224 225 226 227 228
/* 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;
229
	int err = 0;
M
Markus Grabner 已提交
230

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 241
			break;

		case SNDRV_PCM_STREAM_CAPTURE:
242
			err = snd_line6_capture_trigger(line6pcm, cmd);
M
Markus Grabner 已提交
243 244 245
			break;

		default:
246 247
			dev_err(line6pcm->line6->ifcdev,
				"Unknown stream direction %d\n", s->stream);
248 249
			err = -EINVAL;
			break;
M
Markus Grabner 已提交
250
		}
251 252
		if (err < 0)
			break;
M
Markus Grabner 已提交
253 254
	}

255
	return err;
M
Markus Grabner 已提交
256 257 258
}

/* control info callback */
259 260
static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_info *uinfo)
261
{
M
Markus Grabner 已提交
262 263 264 265 266 267 268 269
	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 */
270 271
static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *ucontrol)
272
{
M
Markus Grabner 已提交
273 274 275
	int i;
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);

T
Takashi Iwai 已提交
276
	for (i = 0; i < 2; i++)
277
		ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
M
Markus Grabner 已提交
278 279 280 281 282

	return 0;
}

/* control put callback */
283 284
static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *ucontrol)
285
{
M
Markus Grabner 已提交
286 287 288
	int i, changed = 0;
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);

T
Takashi Iwai 已提交
289
	for (i = 0; i < 2; i++)
290 291 292 293
		if (line6pcm->volume_playback[i] !=
		    ucontrol->value.integer.value[i]) {
			line6pcm->volume_playback[i] =
			    ucontrol->value.integer.value[i];
M
Markus Grabner 已提交
294 295 296 297 298 299 300
			changed = 1;
		}

	return changed;
}

/* control definition */
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
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 已提交
323 324 325 326 327 328 329 330 331 332
};

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

T
Takashi Iwai 已提交
333
	for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
334 335 336
		if (line6pcm->out.urbs[i]) {
			usb_kill_urb(line6pcm->out.urbs[i]);
			usb_free_urb(line6pcm->out.urbs[i]);
M
Markus Grabner 已提交
337
		}
338 339 340
		if (line6pcm->in.urbs[i]) {
			usb_kill_urb(line6pcm->in.urbs[i]);
			usb_free_urb(line6pcm->in.urbs[i]);
M
Markus Grabner 已提交
341 342
		}
	}
343
	kfree(line6pcm);
M
Markus Grabner 已提交
344 345 346
}

/* create a PCM device */
347
static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
M
Markus Grabner 已提交
348 349 350 351
{
	struct snd_pcm *pcm;
	int err;

352 353
	err = snd_pcm_new(line6->card, (char *)line6->properties->name,
			  0, 1, 1, pcm_ret);
354
	if (err < 0)
M
Markus Grabner 已提交
355
		return err;
356 357
	pcm = *pcm_ret;
	strcpy(pcm->name, line6->properties->name);
M
Markus Grabner 已提交
358 359

	/* set operators */
360 361
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
			&snd_line6_playback_ops);
362
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
M
Markus Grabner 已提交
363 364

	/* pre-allocation of buffers */
365
	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
366 367 368
					      snd_dma_continuous_data
					      (GFP_KERNEL), 64 * 1024,
					      128 * 1024);
M
Markus Grabner 已提交
369 370 371
	return 0;
}

372
/*
373
	Sync with PCM stream stops.
374 375 376 377 378 379 380
*/
void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
{
	line6_unlink_wait_clear_audio_out_urbs(line6pcm);
	line6_unlink_wait_clear_audio_in_urbs(line6pcm);
}

M
Markus Grabner 已提交
381 382 383 384
/*
	Create and register the PCM device and mixer entries.
	Create URBs for playback and capture.
*/
385 386
int line6_init_pcm(struct usb_line6 *line6,
		   struct line6_pcm_properties *properties)
M
Markus Grabner 已提交
387
{
388
	int i, err;
389 390
	unsigned ep_read = line6->properties->ep_audio_r;
	unsigned ep_write = line6->properties->ep_audio_w;
391
	struct snd_pcm *pcm;
M
Markus Grabner 已提交
392 393
	struct snd_line6_pcm *line6pcm;

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

397 398 399
	err = snd_line6_new_pcm(line6, &pcm);
	if (err < 0)
		return err;
M
Markus Grabner 已提交
400

401 402
	line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
	if (!line6pcm)
M
Markus Grabner 已提交
403 404
		return -ENOMEM;

405 406
	line6pcm->pcm = pcm;
	line6pcm->properties = properties;
407 408
	line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
	line6pcm->volume_monitor = 255;
M
Markus Grabner 已提交
409
	line6pcm->line6 = line6;
410 411 412 413 414 415 416 417

	/* 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));

418 419
	spin_lock_init(&line6pcm->out.lock);
	spin_lock_init(&line6pcm->in.lock);
420
	line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
M
Markus Grabner 已提交
421

422 423 424 425 426
	line6->line6pcm = line6pcm;

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

427
	err = line6_create_audio_out_urbs(line6pcm);
428
	if (err < 0)
M
Markus Grabner 已提交
429 430
		return err;

431
	err = line6_create_audio_in_urbs(line6pcm);
432
	if (err < 0)
M
Markus Grabner 已提交
433 434 435
		return err;

	/* mixer: */
436 437 438 439 440 441
	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;
	}
442

M
Markus Grabner 已提交
443 444
	return 0;
}
T
Takashi Iwai 已提交
445
EXPORT_SYMBOL_GPL(line6_init_pcm);
M
Markus Grabner 已提交
446 447 448 449 450 451

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

452 453
	switch (substream->stream) {
	case SNDRV_PCM_STREAM_PLAYBACK:
454
		if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
455 456
			line6_unlink_wait_clear_audio_out_urbs(line6pcm);

457 458 459
		break;

	case SNDRV_PCM_STREAM_CAPTURE:
460
		if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
461 462
			line6_unlink_wait_clear_audio_in_urbs(line6pcm);

463 464 465
		break;
	}

466
	if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
467 468 469 470 471 472 473
		line6pcm->out.count = 0;
		line6pcm->out.pos = 0;
		line6pcm->out.pos_done = 0;
		line6pcm->out.bytes = 0;
		line6pcm->in.count = 0;
		line6pcm->in.pos_done = 0;
		line6pcm->in.bytes = 0;
M
Markus Grabner 已提交
474 475 476 477
	}

	return 0;
}