ALSA: line6: Remove double line6_pcm_release() after failed acquire.
[deliverable/linux.git] / sound / usb / line6 / pcm.c
1 /*
2 * Line 6 Linux USB driver
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 "capture.h"
20 #include "driver.h"
21 #include "playback.h"
22
23 /* impulse response volume controls */
24 static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol,
25 struct snd_ctl_elem_info *uinfo)
26 {
27 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
28 uinfo->count = 1;
29 uinfo->value.integer.min = 0;
30 uinfo->value.integer.max = 255;
31 return 0;
32 }
33
34 static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol,
35 struct snd_ctl_elem_value *ucontrol)
36 {
37 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
38
39 ucontrol->value.integer.value[0] = line6pcm->impulse_volume;
40 return 0;
41 }
42
43 static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol,
44 struct snd_ctl_elem_value *ucontrol)
45 {
46 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
47 int value = ucontrol->value.integer.value[0];
48 int err;
49
50 if (line6pcm->impulse_volume == value)
51 return 0;
52
53 line6pcm->impulse_volume = value;
54 if (value > 0) {
55 err = line6_pcm_acquire(line6pcm, LINE6_STREAM_IMPULSE);
56 if (err < 0) {
57 line6pcm->impulse_volume = 0;
58 return err;
59 }
60 } else {
61 line6_pcm_release(line6pcm, LINE6_STREAM_IMPULSE);
62 }
63 return 1;
64 }
65
66 /* impulse response period controls */
67 static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
68 struct snd_ctl_elem_info *uinfo)
69 {
70 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
71 uinfo->count = 1;
72 uinfo->value.integer.min = 0;
73 uinfo->value.integer.max = 2000;
74 return 0;
75 }
76
77 static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
78 struct snd_ctl_elem_value *ucontrol)
79 {
80 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
81
82 ucontrol->value.integer.value[0] = line6pcm->impulse_period;
83 return 0;
84 }
85
86 static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
87 struct snd_ctl_elem_value *ucontrol)
88 {
89 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
90 int value = ucontrol->value.integer.value[0];
91
92 if (line6pcm->impulse_period == value)
93 return 0;
94
95 line6pcm->impulse_period = value;
96 return 1;
97 }
98
99 /*
100 Unlink all currently active URBs.
101 */
102 static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
103 struct line6_pcm_stream *pcms)
104 {
105 int i;
106
107 for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
108 if (test_bit(i, &pcms->active_urbs)) {
109 if (!test_and_set_bit(i, &pcms->unlink_urbs))
110 usb_unlink_urb(pcms->urbs[i]);
111 }
112 }
113 }
114
115 /*
116 Wait until unlinking of all currently active URBs has been finished.
117 */
118 static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
119 struct line6_pcm_stream *pcms)
120 {
121 int timeout = HZ;
122 int i;
123 int alive;
124
125 do {
126 alive = 0;
127 for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
128 if (test_bit(i, &pcms->active_urbs))
129 alive++;
130 }
131 if (!alive)
132 break;
133 set_current_state(TASK_UNINTERRUPTIBLE);
134 schedule_timeout(1);
135 } while (--timeout > 0);
136 if (alive)
137 dev_err(line6pcm->line6->ifcdev,
138 "timeout: still %d active urbs..\n", alive);
139 }
140
141 static inline struct line6_pcm_stream *
142 get_stream(struct snd_line6_pcm *line6pcm, int direction)
143 {
144 return (direction == SNDRV_PCM_STREAM_PLAYBACK) ?
145 &line6pcm->out : &line6pcm->in;
146 }
147
148 /* allocate a buffer if not opened yet;
149 * call this in line6pcm.state_change mutex
150 */
151 static int line6_buffer_acquire(struct snd_line6_pcm *line6pcm,
152 struct line6_pcm_stream *pstr, int type)
153 {
154 /* Invoked multiple times in a row so allocate once only */
155 if (!test_and_set_bit(type, &pstr->opened) && !pstr->buffer) {
156 pstr->buffer = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
157 line6pcm->max_packet_size, GFP_KERNEL);
158 if (!pstr->buffer)
159 return -ENOMEM;
160 }
161 return 0;
162 }
163
164 /* free a buffer if all streams are closed;
165 * call this in line6pcm.state_change mutex
166 */
167 static void line6_buffer_release(struct snd_line6_pcm *line6pcm,
168 struct line6_pcm_stream *pstr, int type)
169 {
170
171 clear_bit(type, &pstr->opened);
172 if (!pstr->opened) {
173 line6_wait_clear_audio_urbs(line6pcm, pstr);
174 kfree(pstr->buffer);
175 pstr->buffer = NULL;
176 }
177 }
178
179 /* start a PCM stream */
180 static int line6_stream_start(struct snd_line6_pcm *line6pcm, int direction,
181 int type)
182 {
183 unsigned long flags;
184 struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
185 int ret = 0;
186
187 spin_lock_irqsave(&pstr->lock, flags);
188 if (!test_and_set_bit(type, &pstr->running) &&
189 !(pstr->active_urbs || pstr->unlink_urbs)) {
190 pstr->count = 0;
191 /* Submit all currently available URBs */
192 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
193 ret = line6_submit_audio_out_all_urbs(line6pcm);
194 else
195 ret = line6_submit_audio_in_all_urbs(line6pcm);
196 }
197 if (ret < 0)
198 clear_bit(type, &pstr->running);
199 spin_unlock_irqrestore(&pstr->lock, flags);
200 return ret;
201 }
202
203 /* stop a PCM stream; this doesn't sync with the unlinked URBs */
204 static void line6_stream_stop(struct snd_line6_pcm *line6pcm, int direction,
205 int type)
206 {
207 unsigned long flags;
208 struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
209
210 spin_lock_irqsave(&pstr->lock, flags);
211 clear_bit(type, &pstr->running);
212 if (!pstr->running) {
213 line6_unlink_audio_urbs(line6pcm, pstr);
214 if (direction == SNDRV_PCM_STREAM_CAPTURE) {
215 line6pcm->prev_fbuf = NULL;
216 line6pcm->prev_fsize = 0;
217 }
218 }
219 spin_unlock_irqrestore(&pstr->lock, flags);
220 }
221
222 /* common PCM trigger callback */
223 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
224 {
225 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
226 struct snd_pcm_substream *s;
227 int err;
228
229 clear_bit(LINE6_FLAG_PREPARED, &line6pcm->flags);
230
231 snd_pcm_group_for_each_entry(s, substream) {
232 if (s->pcm->card != substream->pcm->card)
233 continue;
234
235 switch (cmd) {
236 case SNDRV_PCM_TRIGGER_START:
237 case SNDRV_PCM_TRIGGER_RESUME:
238 err = line6_stream_start(line6pcm, s->stream,
239 LINE6_STREAM_PCM);
240 if (err < 0)
241 return err;
242 break;
243
244 case SNDRV_PCM_TRIGGER_STOP:
245 case SNDRV_PCM_TRIGGER_SUSPEND:
246 line6_stream_stop(line6pcm, s->stream,
247 LINE6_STREAM_PCM);
248 break;
249
250 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
251 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
252 return -EINVAL;
253 set_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
254 break;
255
256 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
257 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
258 return -EINVAL;
259 clear_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
260 break;
261
262 default:
263 return -EINVAL;
264 }
265 }
266
267 return 0;
268 }
269
270 /* common PCM pointer callback */
271 snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream)
272 {
273 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
274 struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
275
276 return pstr->pos_done;
277 }
278
279 /* Acquire and start duplex streams:
280 * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR
281 */
282 int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type)
283 {
284 struct line6_pcm_stream *pstr;
285 int ret = 0, dir;
286
287 mutex_lock(&line6pcm->state_mutex);
288 for (dir = 0; dir < 2; dir++) {
289 pstr = get_stream(line6pcm, dir);
290 ret = line6_buffer_acquire(line6pcm, pstr, type);
291 if (ret < 0)
292 goto error;
293 if (!pstr->running)
294 line6_wait_clear_audio_urbs(line6pcm, pstr);
295 }
296 for (dir = 0; dir < 2; dir++) {
297 ret = line6_stream_start(line6pcm, dir, type);
298 if (ret < 0)
299 goto error;
300 }
301 error:
302 mutex_unlock(&line6pcm->state_mutex);
303 if (ret < 0)
304 line6_pcm_release(line6pcm, type);
305 return ret;
306 }
307 EXPORT_SYMBOL_GPL(line6_pcm_acquire);
308
309 /* Stop and release duplex streams */
310 void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
311 {
312 struct line6_pcm_stream *pstr;
313 int dir;
314
315 mutex_lock(&line6pcm->state_mutex);
316 for (dir = 0; dir < 2; dir++)
317 line6_stream_stop(line6pcm, dir, type);
318 for (dir = 0; dir < 2; dir++) {
319 pstr = get_stream(line6pcm, dir);
320 line6_buffer_release(line6pcm, pstr, type);
321 }
322 mutex_unlock(&line6pcm->state_mutex);
323 }
324 EXPORT_SYMBOL_GPL(line6_pcm_release);
325
326 /* common PCM hw_params callback */
327 int snd_line6_hw_params(struct snd_pcm_substream *substream,
328 struct snd_pcm_hw_params *hw_params)
329 {
330 int ret;
331 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
332 struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
333
334 mutex_lock(&line6pcm->state_mutex);
335 ret = line6_buffer_acquire(line6pcm, pstr, LINE6_STREAM_PCM);
336 if (ret < 0)
337 goto error;
338
339 ret = snd_pcm_lib_malloc_pages(substream,
340 params_buffer_bytes(hw_params));
341 if (ret < 0) {
342 line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
343 goto error;
344 }
345
346 pstr->period = params_period_bytes(hw_params);
347 error:
348 mutex_unlock(&line6pcm->state_mutex);
349 return ret;
350 }
351
352 /* common PCM hw_free callback */
353 int snd_line6_hw_free(struct snd_pcm_substream *substream)
354 {
355 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
356 struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
357
358 mutex_lock(&line6pcm->state_mutex);
359 line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
360 mutex_unlock(&line6pcm->state_mutex);
361 return snd_pcm_lib_free_pages(substream);
362 }
363
364
365 /* control info callback */
366 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
367 struct snd_ctl_elem_info *uinfo)
368 {
369 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
370 uinfo->count = 2;
371 uinfo->value.integer.min = 0;
372 uinfo->value.integer.max = 256;
373 return 0;
374 }
375
376 /* control get callback */
377 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
378 struct snd_ctl_elem_value *ucontrol)
379 {
380 int i;
381 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
382
383 for (i = 0; i < 2; i++)
384 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
385
386 return 0;
387 }
388
389 /* control put callback */
390 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
391 struct snd_ctl_elem_value *ucontrol)
392 {
393 int i, changed = 0;
394 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
395
396 for (i = 0; i < 2; i++)
397 if (line6pcm->volume_playback[i] !=
398 ucontrol->value.integer.value[i]) {
399 line6pcm->volume_playback[i] =
400 ucontrol->value.integer.value[i];
401 changed = 1;
402 }
403
404 return changed;
405 }
406
407 /* control definition */
408 static struct snd_kcontrol_new line6_controls[] = {
409 {
410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
411 .name = "PCM Playback Volume",
412 .info = snd_line6_control_playback_info,
413 .get = snd_line6_control_playback_get,
414 .put = snd_line6_control_playback_put
415 },
416 {
417 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
418 .name = "Impulse Response Volume",
419 .info = snd_line6_impulse_volume_info,
420 .get = snd_line6_impulse_volume_get,
421 .put = snd_line6_impulse_volume_put
422 },
423 {
424 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
425 .name = "Impulse Response Period",
426 .info = snd_line6_impulse_period_info,
427 .get = snd_line6_impulse_period_get,
428 .put = snd_line6_impulse_period_put
429 },
430 };
431
432 /*
433 Cleanup the PCM device.
434 */
435 static void cleanup_urbs(struct line6_pcm_stream *pcms)
436 {
437 int i;
438
439 for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
440 if (pcms->urbs[i]) {
441 usb_kill_urb(pcms->urbs[i]);
442 usb_free_urb(pcms->urbs[i]);
443 }
444 }
445 }
446
447 static void line6_cleanup_pcm(struct snd_pcm *pcm)
448 {
449 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
450
451 cleanup_urbs(&line6pcm->out);
452 cleanup_urbs(&line6pcm->in);
453 kfree(line6pcm);
454 }
455
456 /* create a PCM device */
457 static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
458 {
459 struct snd_pcm *pcm;
460 int err;
461
462 err = snd_pcm_new(line6->card, (char *)line6->properties->name,
463 0, 1, 1, pcm_ret);
464 if (err < 0)
465 return err;
466 pcm = *pcm_ret;
467 strcpy(pcm->name, line6->properties->name);
468
469 /* set operators */
470 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
471 &snd_line6_playback_ops);
472 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
473
474 /* pre-allocation of buffers */
475 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
476 snd_dma_continuous_data
477 (GFP_KERNEL), 64 * 1024,
478 128 * 1024);
479 return 0;
480 }
481
482 /*
483 Sync with PCM stream stops.
484 */
485 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
486 {
487 line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
488 line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
489 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
490 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
491 }
492
493 /*
494 Create and register the PCM device and mixer entries.
495 Create URBs for playback and capture.
496 */
497 int line6_init_pcm(struct usb_line6 *line6,
498 struct line6_pcm_properties *properties)
499 {
500 int i, err;
501 unsigned ep_read = line6->properties->ep_audio_r;
502 unsigned ep_write = line6->properties->ep_audio_w;
503 struct snd_pcm *pcm;
504 struct snd_line6_pcm *line6pcm;
505
506 if (!(line6->properties->capabilities & LINE6_CAP_PCM))
507 return 0; /* skip PCM initialization and report success */
508
509 err = snd_line6_new_pcm(line6, &pcm);
510 if (err < 0)
511 return err;
512
513 line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
514 if (!line6pcm)
515 return -ENOMEM;
516
517 mutex_init(&line6pcm->state_mutex);
518 line6pcm->pcm = pcm;
519 line6pcm->properties = properties;
520 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
521 line6pcm->volume_monitor = 255;
522 line6pcm->line6 = line6;
523
524 /* Read and write buffers are sized identically, so choose minimum */
525 line6pcm->max_packet_size = min(
526 usb_maxpacket(line6->usbdev,
527 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
528 usb_maxpacket(line6->usbdev,
529 usb_sndisocpipe(line6->usbdev, ep_write), 1));
530
531 spin_lock_init(&line6pcm->out.lock);
532 spin_lock_init(&line6pcm->in.lock);
533 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
534
535 line6->line6pcm = line6pcm;
536
537 pcm->private_data = line6pcm;
538 pcm->private_free = line6_cleanup_pcm;
539
540 err = line6_create_audio_out_urbs(line6pcm);
541 if (err < 0)
542 return err;
543
544 err = line6_create_audio_in_urbs(line6pcm);
545 if (err < 0)
546 return err;
547
548 /* mixer: */
549 for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
550 err = snd_ctl_add(line6->card,
551 snd_ctl_new1(&line6_controls[i], line6pcm));
552 if (err < 0)
553 return err;
554 }
555
556 return 0;
557 }
558 EXPORT_SYMBOL_GPL(line6_init_pcm);
559
560 /* prepare pcm callback */
561 int snd_line6_prepare(struct snd_pcm_substream *substream)
562 {
563 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
564 struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
565
566 mutex_lock(&line6pcm->state_mutex);
567 if (!pstr->running)
568 line6_wait_clear_audio_urbs(line6pcm, pstr);
569
570 if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) {
571 line6pcm->out.count = 0;
572 line6pcm->out.pos = 0;
573 line6pcm->out.pos_done = 0;
574 line6pcm->out.bytes = 0;
575 line6pcm->in.count = 0;
576 line6pcm->in.pos_done = 0;
577 line6pcm->in.bytes = 0;
578 }
579
580 mutex_unlock(&line6pcm->state_mutex);
581 return 0;
582 }
This page took 0.067756 seconds and 6 git commands to generate.