xref: /openbmc/linux/sound/usb/line6/pcm.c (revision 84ac9bb12e8158e1affad4ae7718eb653fa5e36d)
1 /*
2  * Line6 Linux USB driver - 0.9.1beta
3  *
4  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License as
8  *	published by the Free Software Foundation, version 2.
9  *
10  */
11 
12 #include <linux/slab.h>
13 #include <linux/export.h>
14 #include <sound/core.h>
15 #include <sound/control.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 
19 #include "audio.h"
20 #include "capture.h"
21 #include "driver.h"
22 #include "playback.h"
23 
24 /* impulse response volume controls */
25 static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol,
26 					 struct snd_ctl_elem_info *uinfo)
27 {
28 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
29 	uinfo->count = 1;
30 	uinfo->value.integer.min = 0;
31 	uinfo->value.integer.max = 255;
32 	return 0;
33 }
34 
35 static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol,
36 					struct snd_ctl_elem_value *ucontrol)
37 {
38 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
39 
40 	ucontrol->value.integer.value[0] = line6pcm->impulse_volume;
41 	return 0;
42 }
43 
44 static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol,
45 					struct snd_ctl_elem_value *ucontrol)
46 {
47 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
48 	int value = ucontrol->value.integer.value[0];
49 
50 	if (line6pcm->impulse_volume == value)
51 		return 0;
52 
53 	line6pcm->impulse_volume = value;
54 	if (value > 0)
55 		line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
56 	else
57 		line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
58 	return 1;
59 }
60 
61 /* impulse response period controls */
62 static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
63 					 struct snd_ctl_elem_info *uinfo)
64 {
65 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
66 	uinfo->count = 1;
67 	uinfo->value.integer.min = 0;
68 	uinfo->value.integer.max = 2000;
69 	return 0;
70 }
71 
72 static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
73 					struct snd_ctl_elem_value *ucontrol)
74 {
75 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
76 
77 	ucontrol->value.integer.value[0] = line6pcm->impulse_period;
78 	return 0;
79 }
80 
81 static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
82 					struct snd_ctl_elem_value *ucontrol)
83 {
84 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
85 	int value = ucontrol->value.integer.value[0];
86 
87 	if (line6pcm->impulse_period == value)
88 		return 0;
89 
90 	line6pcm->impulse_period = value;
91 	return 1;
92 }
93 
94 static bool test_flags(unsigned long flags0, unsigned long flags1,
95 		       unsigned long mask)
96 {
97 	return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
98 }
99 
100 int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
101 {
102 	unsigned long flags_old, flags_new, flags_final;
103 	int err;
104 
105 	do {
106 		flags_old = ACCESS_ONCE(line6pcm->flags);
107 		flags_new = flags_old | channels;
108 	} while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
109 
110 	flags_final = flags_old;
111 
112 	line6pcm->prev_fbuf = NULL;
113 
114 	if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
115 		/* Invoked multiple times in a row so allocate once only */
116 		if (!line6pcm->buffer_in) {
117 			line6pcm->buffer_in =
118 				kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
119 					line6pcm->max_packet_size, GFP_KERNEL);
120 			if (!line6pcm->buffer_in) {
121 				err = -ENOMEM;
122 				goto pcm_acquire_error;
123 			}
124 
125 			flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
126 		}
127 	}
128 
129 	if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
130 		/*
131 		   Waiting for completion of active URBs in the stop handler is
132 		   a bug, we therefore report an error if capturing is restarted
133 		   too soon.
134 		 */
135 		if (line6pcm->active_urb_in | line6pcm->unlink_urb_in) {
136 			dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
137 			return -EBUSY;
138 		}
139 
140 		line6pcm->count_in = 0;
141 		line6pcm->prev_fsize = 0;
142 		err = line6_submit_audio_in_all_urbs(line6pcm);
143 
144 		if (err < 0)
145 			goto pcm_acquire_error;
146 
147 		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 		if (!line6pcm->buffer_out) {
153 			line6pcm->buffer_out =
154 				kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
155 					line6pcm->max_packet_size, GFP_KERNEL);
156 			if (!line6pcm->buffer_out) {
157 				err = -ENOMEM;
158 				goto pcm_acquire_error;
159 			}
160 
161 			flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
162 		}
163 	}
164 
165 	if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
166 		/*
167 		  See comment above regarding PCM restart.
168 		*/
169 		if (line6pcm->active_urb_out | line6pcm->unlink_urb_out) {
170 			dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
171 			return -EBUSY;
172 		}
173 
174 		line6pcm->count_out = 0;
175 		err = line6_submit_audio_out_all_urbs(line6pcm);
176 
177 		if (err < 0)
178 			goto pcm_acquire_error;
179 
180 		flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
181 	}
182 
183 	return 0;
184 
185 pcm_acquire_error:
186 	/*
187 	   If not all requested resources/streams could be obtained, release
188 	   those which were successfully obtained (if any).
189 	*/
190 	line6_pcm_release(line6pcm, flags_final & channels);
191 	return err;
192 }
193 EXPORT_SYMBOL_GPL(line6_pcm_acquire);
194 
195 int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
196 {
197 	unsigned long flags_old, flags_new;
198 
199 	do {
200 		flags_old = ACCESS_ONCE(line6pcm->flags);
201 		flags_new = flags_old & ~channels;
202 	} 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 	if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
208 		line6_wait_clear_audio_in_urbs(line6pcm);
209 		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 	if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
216 		line6_wait_clear_audio_out_urbs(line6pcm);
217 		line6_free_playback_buffer(line6pcm);
218 	}
219 
220 	return 0;
221 }
222 EXPORT_SYMBOL_GPL(line6_pcm_release);
223 
224 /* trigger callback */
225 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
226 {
227 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
228 	struct snd_pcm_substream *s;
229 	int err;
230 
231 	spin_lock(&line6pcm->lock_trigger);
232 	clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
233 
234 	snd_pcm_group_for_each_entry(s, substream) {
235 		if (s->pcm->card != substream->pcm->card)
236 			continue;
237 		switch (s->stream) {
238 		case SNDRV_PCM_STREAM_PLAYBACK:
239 			err = snd_line6_playback_trigger(line6pcm, cmd);
240 
241 			if (err < 0) {
242 				spin_unlock(&line6pcm->lock_trigger);
243 				return err;
244 			}
245 
246 			break;
247 
248 		case SNDRV_PCM_STREAM_CAPTURE:
249 			err = snd_line6_capture_trigger(line6pcm, cmd);
250 
251 			if (err < 0) {
252 				spin_unlock(&line6pcm->lock_trigger);
253 				return err;
254 			}
255 
256 			break;
257 
258 		default:
259 			dev_err(line6pcm->line6->ifcdev,
260 				"Unknown stream direction %d\n", s->stream);
261 		}
262 	}
263 
264 	spin_unlock(&line6pcm->lock_trigger);
265 	return 0;
266 }
267 
268 /* control info callback */
269 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
270 					   struct snd_ctl_elem_info *uinfo)
271 {
272 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
273 	uinfo->count = 2;
274 	uinfo->value.integer.min = 0;
275 	uinfo->value.integer.max = 256;
276 	return 0;
277 }
278 
279 /* control get callback */
280 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
281 					  struct snd_ctl_elem_value *ucontrol)
282 {
283 	int i;
284 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
285 
286 	for (i = 2; i--;)
287 		ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
288 
289 	return 0;
290 }
291 
292 /* control put callback */
293 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
294 					  struct snd_ctl_elem_value *ucontrol)
295 {
296 	int i, changed = 0;
297 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
298 
299 	for (i = 2; i--;)
300 		if (line6pcm->volume_playback[i] !=
301 		    ucontrol->value.integer.value[i]) {
302 			line6pcm->volume_playback[i] =
303 			    ucontrol->value.integer.value[i];
304 			changed = 1;
305 		}
306 
307 	return changed;
308 }
309 
310 /* control definition */
311 static struct snd_kcontrol_new line6_controls[] = {
312 	{
313 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
314 		.name = "PCM Playback Volume",
315 		.info = snd_line6_control_playback_info,
316 		.get = snd_line6_control_playback_get,
317 		.put = snd_line6_control_playback_put
318 	},
319 	{
320 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
321 		.name = "Impulse Response Volume",
322 		.info = snd_line6_impulse_volume_info,
323 		.get = snd_line6_impulse_volume_get,
324 		.put = snd_line6_impulse_volume_put
325 	},
326 	{
327 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
328 		.name = "Impulse Response Period",
329 		.info = snd_line6_impulse_period_info,
330 		.get = snd_line6_impulse_period_get,
331 		.put = snd_line6_impulse_period_put
332 	},
333 };
334 
335 /*
336 	Cleanup the PCM device.
337 */
338 static void line6_cleanup_pcm(struct snd_pcm *pcm)
339 {
340 	int i;
341 	struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
342 
343 	for (i = LINE6_ISO_BUFFERS; i--;) {
344 		if (line6pcm->urb_audio_out[i]) {
345 			usb_kill_urb(line6pcm->urb_audio_out[i]);
346 			usb_free_urb(line6pcm->urb_audio_out[i]);
347 		}
348 		if (line6pcm->urb_audio_in[i]) {
349 			usb_kill_urb(line6pcm->urb_audio_in[i]);
350 			usb_free_urb(line6pcm->urb_audio_in[i]);
351 		}
352 	}
353 	kfree(line6pcm);
354 }
355 
356 /* create a PCM device */
357 static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
358 {
359 	struct snd_pcm *pcm;
360 	int err;
361 
362 	err = snd_pcm_new(line6->card, (char *)line6->properties->name,
363 			  0, 1, 1, pcm_ret);
364 	if (err < 0)
365 		return err;
366 	pcm = *pcm_ret;
367 	strcpy(pcm->name, line6->properties->name);
368 
369 	/* set operators */
370 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
371 			&snd_line6_playback_ops);
372 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
373 
374 	/* pre-allocation of buffers */
375 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
376 					      snd_dma_continuous_data
377 					      (GFP_KERNEL), 64 * 1024,
378 					      128 * 1024);
379 	return 0;
380 }
381 
382 /*
383 	Stop substream if still running.
384 */
385 static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
386 {
387 	if (substream->runtime && snd_pcm_running(substream)) {
388 		snd_pcm_stream_lock_irq(substream);
389 		snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
390 		snd_pcm_stream_unlock_irq(substream);
391 	}
392 }
393 
394 /*
395 	Stop PCM stream.
396 */
397 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
398 {
399 	pcm_disconnect_substream(get_substream
400 				 (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
401 	pcm_disconnect_substream(get_substream
402 				 (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
403 	line6_unlink_wait_clear_audio_out_urbs(line6pcm);
404 	line6_unlink_wait_clear_audio_in_urbs(line6pcm);
405 }
406 EXPORT_SYMBOL_GPL(line6_pcm_disconnect);
407 
408 /*
409 	Create and register the PCM device and mixer entries.
410 	Create URBs for playback and capture.
411 */
412 int line6_init_pcm(struct usb_line6 *line6,
413 		   struct line6_pcm_properties *properties)
414 {
415 	int i, err;
416 	unsigned ep_read = line6->properties->ep_audio_r;
417 	unsigned ep_write = line6->properties->ep_audio_w;
418 	struct snd_pcm *pcm;
419 	struct snd_line6_pcm *line6pcm;
420 
421 	if (!(line6->properties->capabilities & LINE6_CAP_PCM))
422 		return 0;	/* skip PCM initialization and report success */
423 
424 	err = snd_line6_new_pcm(line6, &pcm);
425 	if (err < 0)
426 		return err;
427 
428 	line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
429 	if (!line6pcm)
430 		return -ENOMEM;
431 
432 	line6pcm->pcm = pcm;
433 	line6pcm->properties = properties;
434 	line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
435 	line6pcm->volume_monitor = 255;
436 	line6pcm->line6 = line6;
437 
438 	/* Read and write buffers are sized identically, so choose minimum */
439 	line6pcm->max_packet_size = min(
440 			usb_maxpacket(line6->usbdev,
441 				usb_rcvisocpipe(line6->usbdev, ep_read), 0),
442 			usb_maxpacket(line6->usbdev,
443 				usb_sndisocpipe(line6->usbdev, ep_write), 1));
444 
445 	spin_lock_init(&line6pcm->lock_audio_out);
446 	spin_lock_init(&line6pcm->lock_audio_in);
447 	spin_lock_init(&line6pcm->lock_trigger);
448 	line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
449 
450 	line6->line6pcm = line6pcm;
451 
452 	pcm->private_data = line6pcm;
453 	pcm->private_free = line6_cleanup_pcm;
454 
455 	err = line6_create_audio_out_urbs(line6pcm);
456 	if (err < 0)
457 		return err;
458 
459 	err = line6_create_audio_in_urbs(line6pcm);
460 	if (err < 0)
461 		return err;
462 
463 	/* mixer: */
464 	for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
465 		err = snd_ctl_add(line6->card,
466 				  snd_ctl_new1(&line6_controls[i], line6pcm));
467 		if (err < 0)
468 			return err;
469 	}
470 
471 	return 0;
472 }
473 EXPORT_SYMBOL_GPL(line6_init_pcm);
474 
475 /* prepare pcm callback */
476 int snd_line6_prepare(struct snd_pcm_substream *substream)
477 {
478 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
479 
480 	switch (substream->stream) {
481 	case SNDRV_PCM_STREAM_PLAYBACK:
482 		if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
483 			line6_unlink_wait_clear_audio_out_urbs(line6pcm);
484 
485 		break;
486 
487 	case SNDRV_PCM_STREAM_CAPTURE:
488 		if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
489 			line6_unlink_wait_clear_audio_in_urbs(line6pcm);
490 
491 		break;
492 
493 	default:
494 		MISSING_CASE;
495 	}
496 
497 	if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
498 		line6pcm->count_out = 0;
499 		line6pcm->pos_out = 0;
500 		line6pcm->pos_out_done = 0;
501 		line6pcm->bytes_out = 0;
502 		line6pcm->count_in = 0;
503 		line6pcm->pos_in_done = 0;
504 		line6pcm->bytes_in = 0;
505 	}
506 
507 	return 0;
508 }
509