2 * Line 6 Linux USB driver
4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
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.
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>
23 /* impulse response volume controls */
24 static int snd_line6_impulse_volume_info(struct snd_kcontrol
*kcontrol
,
25 struct snd_ctl_elem_info
*uinfo
)
27 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
29 uinfo
->value
.integer
.min
= 0;
30 uinfo
->value
.integer
.max
= 255;
34 static int snd_line6_impulse_volume_get(struct snd_kcontrol
*kcontrol
,
35 struct snd_ctl_elem_value
*ucontrol
)
37 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
39 ucontrol
->value
.integer
.value
[0] = line6pcm
->impulse_volume
;
43 static int snd_line6_impulse_volume_put(struct snd_kcontrol
*kcontrol
,
44 struct snd_ctl_elem_value
*ucontrol
)
46 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
47 int value
= ucontrol
->value
.integer
.value
[0];
50 if (line6pcm
->impulse_volume
== value
)
53 line6pcm
->impulse_volume
= value
;
55 err
= line6_pcm_acquire(line6pcm
, LINE6_STREAM_IMPULSE
);
57 line6pcm
->impulse_volume
= 0;
58 line6_pcm_release(line6pcm
, LINE6_STREAM_IMPULSE
);
62 line6_pcm_release(line6pcm
, LINE6_STREAM_IMPULSE
);
67 /* impulse response period controls */
68 static int snd_line6_impulse_period_info(struct snd_kcontrol
*kcontrol
,
69 struct snd_ctl_elem_info
*uinfo
)
71 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
73 uinfo
->value
.integer
.min
= 0;
74 uinfo
->value
.integer
.max
= 2000;
78 static int snd_line6_impulse_period_get(struct snd_kcontrol
*kcontrol
,
79 struct snd_ctl_elem_value
*ucontrol
)
81 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
83 ucontrol
->value
.integer
.value
[0] = line6pcm
->impulse_period
;
87 static int snd_line6_impulse_period_put(struct snd_kcontrol
*kcontrol
,
88 struct snd_ctl_elem_value
*ucontrol
)
90 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
91 int value
= ucontrol
->value
.integer
.value
[0];
93 if (line6pcm
->impulse_period
== value
)
96 line6pcm
->impulse_period
= value
;
101 Unlink all currently active URBs.
103 static void line6_unlink_audio_urbs(struct snd_line6_pcm
*line6pcm
,
104 struct line6_pcm_stream
*pcms
)
108 for (i
= 0; i
< LINE6_ISO_BUFFERS
; i
++) {
109 if (test_bit(i
, &pcms
->active_urbs
)) {
110 if (!test_and_set_bit(i
, &pcms
->unlink_urbs
))
111 usb_unlink_urb(pcms
->urbs
[i
]);
117 Wait until unlinking of all currently active URBs has been finished.
119 static void line6_wait_clear_audio_urbs(struct snd_line6_pcm
*line6pcm
,
120 struct line6_pcm_stream
*pcms
)
128 for (i
= 0; i
< LINE6_ISO_BUFFERS
; i
++) {
129 if (test_bit(i
, &pcms
->active_urbs
))
134 set_current_state(TASK_UNINTERRUPTIBLE
);
136 } while (--timeout
> 0);
138 dev_err(line6pcm
->line6
->ifcdev
,
139 "timeout: still %d active urbs..\n", alive
);
142 static inline struct line6_pcm_stream
*
143 get_stream(struct snd_line6_pcm
*line6pcm
, int direction
)
145 return (direction
== SNDRV_PCM_STREAM_PLAYBACK
) ?
146 &line6pcm
->out
: &line6pcm
->in
;
149 /* allocate a buffer if not opened yet;
150 * call this in line6pcm.state_change mutex
152 static int line6_buffer_acquire(struct snd_line6_pcm
*line6pcm
,
153 struct line6_pcm_stream
*pstr
, int type
)
155 /* Invoked multiple times in a row so allocate once only */
156 if (!test_and_set_bit(type
, &pstr
->opened
) && !pstr
->buffer
) {
157 pstr
->buffer
= kmalloc(LINE6_ISO_BUFFERS
* LINE6_ISO_PACKETS
*
158 line6pcm
->max_packet_size
, GFP_KERNEL
);
165 /* free a buffer if all streams are closed;
166 * call this in line6pcm.state_change mutex
168 static void line6_buffer_release(struct snd_line6_pcm
*line6pcm
,
169 struct line6_pcm_stream
*pstr
, int type
)
172 clear_bit(type
, &pstr
->opened
);
174 line6_wait_clear_audio_urbs(line6pcm
, pstr
);
180 /* start a PCM stream */
181 static int line6_stream_start(struct snd_line6_pcm
*line6pcm
, int direction
,
185 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, direction
);
188 spin_lock_irqsave(&pstr
->lock
, flags
);
189 if (!test_and_set_bit(type
, &pstr
->running
)) {
190 if (pstr
->active_urbs
|| pstr
->unlink_urbs
) {
196 /* Submit all currently available URBs */
197 if (direction
== SNDRV_PCM_STREAM_PLAYBACK
)
198 ret
= line6_submit_audio_out_all_urbs(line6pcm
);
200 ret
= line6_submit_audio_in_all_urbs(line6pcm
);
204 clear_bit(type
, &pstr
->running
);
205 spin_unlock_irqrestore(&pstr
->lock
, flags
);
209 /* stop a PCM stream; this doesn't sync with the unlinked URBs */
210 static void line6_stream_stop(struct snd_line6_pcm
*line6pcm
, int direction
,
214 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, direction
);
216 spin_lock_irqsave(&pstr
->lock
, flags
);
217 clear_bit(type
, &pstr
->running
);
218 if (!pstr
->running
) {
219 line6_unlink_audio_urbs(line6pcm
, pstr
);
220 if (direction
== SNDRV_PCM_STREAM_CAPTURE
) {
221 line6pcm
->prev_fbuf
= NULL
;
222 line6pcm
->prev_fsize
= 0;
225 spin_unlock_irqrestore(&pstr
->lock
, flags
);
228 /* common PCM trigger callback */
229 int snd_line6_trigger(struct snd_pcm_substream
*substream
, int cmd
)
231 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
232 struct snd_pcm_substream
*s
;
235 clear_bit(LINE6_FLAG_PREPARED
, &line6pcm
->flags
);
237 snd_pcm_group_for_each_entry(s
, substream
) {
238 if (s
->pcm
->card
!= substream
->pcm
->card
)
242 case SNDRV_PCM_TRIGGER_START
:
243 case SNDRV_PCM_TRIGGER_RESUME
:
244 err
= line6_stream_start(line6pcm
, s
->stream
,
250 case SNDRV_PCM_TRIGGER_STOP
:
251 case SNDRV_PCM_TRIGGER_SUSPEND
:
252 line6_stream_stop(line6pcm
, s
->stream
,
256 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
257 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
259 set_bit(LINE6_FLAG_PAUSE_PLAYBACK
, &line6pcm
->flags
);
262 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
263 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
265 clear_bit(LINE6_FLAG_PAUSE_PLAYBACK
, &line6pcm
->flags
);
276 /* common PCM pointer callback */
277 snd_pcm_uframes_t
snd_line6_pointer(struct snd_pcm_substream
*substream
)
279 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
280 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, substream
->stream
);
282 return pstr
->pos_done
;
285 /* Acquire and start duplex streams:
286 * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR
288 int line6_pcm_acquire(struct snd_line6_pcm
*line6pcm
, int type
)
290 struct line6_pcm_stream
*pstr
;
293 mutex_lock(&line6pcm
->state_mutex
);
294 for (dir
= 0; dir
< 2; dir
++) {
295 pstr
= get_stream(line6pcm
, dir
);
296 ret
= line6_buffer_acquire(line6pcm
, pstr
, type
);
300 line6_wait_clear_audio_urbs(line6pcm
, pstr
);
302 for (dir
= 0; dir
< 2; dir
++) {
303 ret
= line6_stream_start(line6pcm
, dir
, type
);
308 mutex_unlock(&line6pcm
->state_mutex
);
310 line6_pcm_release(line6pcm
, type
);
313 EXPORT_SYMBOL_GPL(line6_pcm_acquire
);
315 /* Stop and release duplex streams */
316 void line6_pcm_release(struct snd_line6_pcm
*line6pcm
, int type
)
318 struct line6_pcm_stream
*pstr
;
321 mutex_lock(&line6pcm
->state_mutex
);
322 for (dir
= 0; dir
< 2; dir
++)
323 line6_stream_stop(line6pcm
, dir
, type
);
324 for (dir
= 0; dir
< 2; dir
++) {
325 pstr
= get_stream(line6pcm
, dir
);
326 line6_buffer_release(line6pcm
, pstr
, type
);
328 mutex_unlock(&line6pcm
->state_mutex
);
330 EXPORT_SYMBOL_GPL(line6_pcm_release
);
332 /* common PCM hw_params callback */
333 int snd_line6_hw_params(struct snd_pcm_substream
*substream
,
334 struct snd_pcm_hw_params
*hw_params
)
337 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
338 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, substream
->stream
);
340 mutex_lock(&line6pcm
->state_mutex
);
341 ret
= line6_buffer_acquire(line6pcm
, pstr
, LINE6_STREAM_PCM
);
345 ret
= snd_pcm_lib_malloc_pages(substream
,
346 params_buffer_bytes(hw_params
));
348 line6_buffer_release(line6pcm
, pstr
, LINE6_STREAM_PCM
);
352 pstr
->period
= params_period_bytes(hw_params
);
354 mutex_unlock(&line6pcm
->state_mutex
);
358 /* common PCM hw_free callback */
359 int snd_line6_hw_free(struct snd_pcm_substream
*substream
)
361 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
362 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, substream
->stream
);
364 mutex_lock(&line6pcm
->state_mutex
);
365 line6_buffer_release(line6pcm
, pstr
, LINE6_STREAM_PCM
);
366 mutex_unlock(&line6pcm
->state_mutex
);
367 return snd_pcm_lib_free_pages(substream
);
371 /* control info callback */
372 static int snd_line6_control_playback_info(struct snd_kcontrol
*kcontrol
,
373 struct snd_ctl_elem_info
*uinfo
)
375 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
377 uinfo
->value
.integer
.min
= 0;
378 uinfo
->value
.integer
.max
= 256;
382 /* control get callback */
383 static int snd_line6_control_playback_get(struct snd_kcontrol
*kcontrol
,
384 struct snd_ctl_elem_value
*ucontrol
)
387 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
389 for (i
= 0; i
< 2; i
++)
390 ucontrol
->value
.integer
.value
[i
] = line6pcm
->volume_playback
[i
];
395 /* control put callback */
396 static int snd_line6_control_playback_put(struct snd_kcontrol
*kcontrol
,
397 struct snd_ctl_elem_value
*ucontrol
)
400 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
402 for (i
= 0; i
< 2; i
++)
403 if (line6pcm
->volume_playback
[i
] !=
404 ucontrol
->value
.integer
.value
[i
]) {
405 line6pcm
->volume_playback
[i
] =
406 ucontrol
->value
.integer
.value
[i
];
413 /* control definition */
414 static struct snd_kcontrol_new line6_controls
[] = {
416 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
417 .name
= "PCM Playback Volume",
418 .info
= snd_line6_control_playback_info
,
419 .get
= snd_line6_control_playback_get
,
420 .put
= snd_line6_control_playback_put
423 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
424 .name
= "Impulse Response Volume",
425 .info
= snd_line6_impulse_volume_info
,
426 .get
= snd_line6_impulse_volume_get
,
427 .put
= snd_line6_impulse_volume_put
430 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
431 .name
= "Impulse Response Period",
432 .info
= snd_line6_impulse_period_info
,
433 .get
= snd_line6_impulse_period_get
,
434 .put
= snd_line6_impulse_period_put
439 Cleanup the PCM device.
441 static void cleanup_urbs(struct line6_pcm_stream
*pcms
)
445 for (i
= 0; i
< LINE6_ISO_BUFFERS
; i
++) {
447 usb_kill_urb(pcms
->urbs
[i
]);
448 usb_free_urb(pcms
->urbs
[i
]);
453 static void line6_cleanup_pcm(struct snd_pcm
*pcm
)
455 struct snd_line6_pcm
*line6pcm
= snd_pcm_chip(pcm
);
457 cleanup_urbs(&line6pcm
->out
);
458 cleanup_urbs(&line6pcm
->in
);
462 /* create a PCM device */
463 static int snd_line6_new_pcm(struct usb_line6
*line6
, struct snd_pcm
**pcm_ret
)
468 err
= snd_pcm_new(line6
->card
, (char *)line6
->properties
->name
,
473 strcpy(pcm
->name
, line6
->properties
->name
);
476 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
477 &snd_line6_playback_ops
);
478 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_line6_capture_ops
);
480 /* pre-allocation of buffers */
481 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
482 snd_dma_continuous_data
483 (GFP_KERNEL
), 64 * 1024,
489 Sync with PCM stream stops.
491 void line6_pcm_disconnect(struct snd_line6_pcm
*line6pcm
)
493 line6_unlink_audio_urbs(line6pcm
, &line6pcm
->out
);
494 line6_unlink_audio_urbs(line6pcm
, &line6pcm
->in
);
495 line6_wait_clear_audio_urbs(line6pcm
, &line6pcm
->out
);
496 line6_wait_clear_audio_urbs(line6pcm
, &line6pcm
->in
);
500 Create and register the PCM device and mixer entries.
501 Create URBs for playback and capture.
503 int line6_init_pcm(struct usb_line6
*line6
,
504 struct line6_pcm_properties
*properties
)
507 unsigned ep_read
= line6
->properties
->ep_audio_r
;
508 unsigned ep_write
= line6
->properties
->ep_audio_w
;
510 struct snd_line6_pcm
*line6pcm
;
512 if (!(line6
->properties
->capabilities
& LINE6_CAP_PCM
))
513 return 0; /* skip PCM initialization and report success */
515 err
= snd_line6_new_pcm(line6
, &pcm
);
519 line6pcm
= kzalloc(sizeof(*line6pcm
), GFP_KERNEL
);
523 mutex_init(&line6pcm
->state_mutex
);
525 line6pcm
->properties
= properties
;
526 line6pcm
->volume_playback
[0] = line6pcm
->volume_playback
[1] = 255;
527 line6pcm
->volume_monitor
= 255;
528 line6pcm
->line6
= line6
;
530 /* Read and write buffers are sized identically, so choose minimum */
531 line6pcm
->max_packet_size
= min(
532 usb_maxpacket(line6
->usbdev
,
533 usb_rcvisocpipe(line6
->usbdev
, ep_read
), 0),
534 usb_maxpacket(line6
->usbdev
,
535 usb_sndisocpipe(line6
->usbdev
, ep_write
), 1));
537 spin_lock_init(&line6pcm
->out
.lock
);
538 spin_lock_init(&line6pcm
->in
.lock
);
539 line6pcm
->impulse_period
= LINE6_IMPULSE_DEFAULT_PERIOD
;
541 line6
->line6pcm
= line6pcm
;
543 pcm
->private_data
= line6pcm
;
544 pcm
->private_free
= line6_cleanup_pcm
;
546 err
= line6_create_audio_out_urbs(line6pcm
);
550 err
= line6_create_audio_in_urbs(line6pcm
);
555 for (i
= 0; i
< ARRAY_SIZE(line6_controls
); i
++) {
556 err
= snd_ctl_add(line6
->card
,
557 snd_ctl_new1(&line6_controls
[i
], line6pcm
));
564 EXPORT_SYMBOL_GPL(line6_init_pcm
);
566 /* prepare pcm callback */
567 int snd_line6_prepare(struct snd_pcm_substream
*substream
)
569 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
570 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, substream
->stream
);
572 mutex_lock(&line6pcm
->state_mutex
);
574 line6_wait_clear_audio_urbs(line6pcm
, pstr
);
576 if (!test_and_set_bit(LINE6_FLAG_PREPARED
, &line6pcm
->flags
)) {
577 line6pcm
->out
.count
= 0;
578 line6pcm
->out
.pos
= 0;
579 line6pcm
->out
.pos_done
= 0;
580 line6pcm
->out
.bytes
= 0;
581 line6pcm
->in
.count
= 0;
582 line6pcm
->in
.pos_done
= 0;
583 line6pcm
->in
.bytes
= 0;
586 mutex_unlock(&line6pcm
->state_mutex
);