1 // SPDX-License-Identifier: GPL-2.0-only
3 * Line 6 Linux USB driver
5 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
8 #include <linux/slab.h>
9 #include <linux/export.h>
10 #include <sound/core.h>
11 #include <sound/control.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
19 /* impulse response volume controls */
20 static int snd_line6_impulse_volume_info(struct snd_kcontrol
*kcontrol
,
21 struct snd_ctl_elem_info
*uinfo
)
23 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
25 uinfo
->value
.integer
.min
= 0;
26 uinfo
->value
.integer
.max
= 255;
30 static int snd_line6_impulse_volume_get(struct snd_kcontrol
*kcontrol
,
31 struct snd_ctl_elem_value
*ucontrol
)
33 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
35 ucontrol
->value
.integer
.value
[0] = line6pcm
->impulse_volume
;
39 static int snd_line6_impulse_volume_put(struct snd_kcontrol
*kcontrol
,
40 struct snd_ctl_elem_value
*ucontrol
)
42 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
43 int value
= ucontrol
->value
.integer
.value
[0];
46 if (line6pcm
->impulse_volume
== value
)
49 line6pcm
->impulse_volume
= value
;
51 err
= line6_pcm_acquire(line6pcm
, LINE6_STREAM_IMPULSE
, true);
53 line6pcm
->impulse_volume
= 0;
57 line6_pcm_release(line6pcm
, LINE6_STREAM_IMPULSE
);
62 /* impulse response period controls */
63 static int snd_line6_impulse_period_info(struct snd_kcontrol
*kcontrol
,
64 struct snd_ctl_elem_info
*uinfo
)
66 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
68 uinfo
->value
.integer
.min
= 0;
69 uinfo
->value
.integer
.max
= 2000;
73 static int snd_line6_impulse_period_get(struct snd_kcontrol
*kcontrol
,
74 struct snd_ctl_elem_value
*ucontrol
)
76 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
78 ucontrol
->value
.integer
.value
[0] = line6pcm
->impulse_period
;
82 static int snd_line6_impulse_period_put(struct snd_kcontrol
*kcontrol
,
83 struct snd_ctl_elem_value
*ucontrol
)
85 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
86 int value
= ucontrol
->value
.integer
.value
[0];
88 if (line6pcm
->impulse_period
== value
)
91 line6pcm
->impulse_period
= value
;
96 Unlink all currently active URBs.
98 static void line6_unlink_audio_urbs(struct snd_line6_pcm
*line6pcm
,
99 struct line6_pcm_stream
*pcms
)
103 for (i
= 0; i
< line6pcm
->line6
->iso_buffers
; i
++) {
104 if (test_bit(i
, &pcms
->active_urbs
)) {
105 if (!test_and_set_bit(i
, &pcms
->unlink_urbs
))
106 usb_unlink_urb(pcms
->urbs
[i
]);
112 Wait until unlinking of all currently active URBs has been finished.
114 static void line6_wait_clear_audio_urbs(struct snd_line6_pcm
*line6pcm
,
115 struct line6_pcm_stream
*pcms
)
123 for (i
= 0; i
< line6pcm
->line6
->iso_buffers
; i
++) {
124 if (test_bit(i
, &pcms
->active_urbs
))
129 set_current_state(TASK_UNINTERRUPTIBLE
);
131 } while (--timeout
> 0);
133 dev_err(line6pcm
->line6
->ifcdev
,
134 "timeout: still %d active urbs..\n", alive
);
137 static inline struct line6_pcm_stream
*
138 get_stream(struct snd_line6_pcm
*line6pcm
, int direction
)
140 return (direction
== SNDRV_PCM_STREAM_PLAYBACK
) ?
141 &line6pcm
->out
: &line6pcm
->in
;
144 /* allocate a buffer if not opened yet;
145 * call this in line6pcm.state_mutex
147 static int line6_buffer_acquire(struct snd_line6_pcm
*line6pcm
,
148 struct line6_pcm_stream
*pstr
, int direction
, int type
)
151 (direction
== SNDRV_PCM_STREAM_PLAYBACK
) ?
152 line6pcm
->max_packet_size_out
:
153 line6pcm
->max_packet_size_in
;
155 /* Invoked multiple times in a row so allocate once only */
156 if (!test_and_set_bit(type
, &pstr
->opened
) && !pstr
->buffer
) {
158 kmalloc(array3_size(line6pcm
->line6
->iso_buffers
,
159 LINE6_ISO_PACKETS
, pkt_size
),
167 /* free a buffer if all streams are closed;
168 * call this in line6pcm.state_mutex
170 static void line6_buffer_release(struct snd_line6_pcm
*line6pcm
,
171 struct line6_pcm_stream
*pstr
, int type
)
173 clear_bit(type
, &pstr
->opened
);
175 line6_wait_clear_audio_urbs(line6pcm
, pstr
);
181 /* start a PCM stream */
182 static int line6_stream_start(struct snd_line6_pcm
*line6pcm
, int direction
,
186 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, direction
);
189 spin_lock_irqsave(&pstr
->lock
, flags
);
190 if (!test_and_set_bit(type
, &pstr
->running
) &&
191 !(pstr
->active_urbs
|| pstr
->unlink_urbs
)) {
193 /* Submit all currently available URBs */
194 if (direction
== SNDRV_PCM_STREAM_PLAYBACK
)
195 ret
= line6_submit_audio_out_all_urbs(line6pcm
);
197 ret
= line6_submit_audio_in_all_urbs(line6pcm
);
201 clear_bit(type
, &pstr
->running
);
202 spin_unlock_irqrestore(&pstr
->lock
, flags
);
206 /* stop a PCM stream; this doesn't sync with the unlinked URBs */
207 static void line6_stream_stop(struct snd_line6_pcm
*line6pcm
, int direction
,
211 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, direction
);
213 spin_lock_irqsave(&pstr
->lock
, flags
);
214 clear_bit(type
, &pstr
->running
);
215 if (!pstr
->running
) {
216 spin_unlock_irqrestore(&pstr
->lock
, flags
);
217 line6_unlink_audio_urbs(line6pcm
, pstr
);
218 spin_lock_irqsave(&pstr
->lock
, flags
);
219 if (direction
== SNDRV_PCM_STREAM_CAPTURE
) {
220 line6pcm
->prev_fbuf
= NULL
;
221 line6pcm
->prev_fsize
= 0;
224 spin_unlock_irqrestore(&pstr
->lock
, flags
);
227 /* common PCM trigger callback */
228 int snd_line6_trigger(struct snd_pcm_substream
*substream
, int cmd
)
230 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
231 struct snd_pcm_substream
*s
;
234 clear_bit(LINE6_FLAG_PREPARED
, &line6pcm
->flags
);
236 snd_pcm_group_for_each_entry(s
, substream
) {
237 if (s
->pcm
->card
!= substream
->pcm
->card
)
241 case SNDRV_PCM_TRIGGER_START
:
242 case SNDRV_PCM_TRIGGER_RESUME
:
243 if (s
->stream
== SNDRV_PCM_STREAM_CAPTURE
&&
244 (line6pcm
->line6
->properties
->capabilities
&
245 LINE6_CAP_IN_NEEDS_OUT
)) {
246 err
= line6_stream_start(line6pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
247 LINE6_STREAM_CAPTURE_HELPER
);
251 err
= line6_stream_start(line6pcm
, s
->stream
,
257 case SNDRV_PCM_TRIGGER_STOP
:
258 case SNDRV_PCM_TRIGGER_SUSPEND
:
259 if (s
->stream
== SNDRV_PCM_STREAM_CAPTURE
&&
260 (line6pcm
->line6
->properties
->capabilities
&
261 LINE6_CAP_IN_NEEDS_OUT
)) {
262 line6_stream_stop(line6pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
263 LINE6_STREAM_CAPTURE_HELPER
);
265 line6_stream_stop(line6pcm
, s
->stream
,
269 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
270 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
272 set_bit(LINE6_FLAG_PAUSE_PLAYBACK
, &line6pcm
->flags
);
275 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
276 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
278 clear_bit(LINE6_FLAG_PAUSE_PLAYBACK
, &line6pcm
->flags
);
289 /* common PCM pointer callback */
290 snd_pcm_uframes_t
snd_line6_pointer(struct snd_pcm_substream
*substream
)
292 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
293 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, substream
->stream
);
295 return pstr
->pos_done
;
298 /* Acquire and optionally start duplex streams:
299 * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR
301 int line6_pcm_acquire(struct snd_line6_pcm
*line6pcm
, int type
, bool start
)
303 struct line6_pcm_stream
*pstr
;
306 /* TODO: We should assert SNDRV_PCM_STREAM_PLAYBACK/CAPTURE == 0/1 */
307 mutex_lock(&line6pcm
->state_mutex
);
308 for (dir
= 0; dir
< 2; dir
++) {
309 pstr
= get_stream(line6pcm
, dir
);
310 ret
= line6_buffer_acquire(line6pcm
, pstr
, dir
, type
);
314 line6_wait_clear_audio_urbs(line6pcm
, pstr
);
317 for (dir
= 0; dir
< 2; dir
++) {
318 ret
= line6_stream_start(line6pcm
, dir
, type
);
324 mutex_unlock(&line6pcm
->state_mutex
);
326 line6_pcm_release(line6pcm
, type
);
329 EXPORT_SYMBOL_GPL(line6_pcm_acquire
);
331 /* Stop and release duplex streams */
332 void line6_pcm_release(struct snd_line6_pcm
*line6pcm
, int type
)
334 struct line6_pcm_stream
*pstr
;
337 mutex_lock(&line6pcm
->state_mutex
);
338 for (dir
= 0; dir
< 2; dir
++)
339 line6_stream_stop(line6pcm
, dir
, type
);
340 for (dir
= 0; dir
< 2; dir
++) {
341 pstr
= get_stream(line6pcm
, dir
);
342 line6_buffer_release(line6pcm
, pstr
, type
);
344 mutex_unlock(&line6pcm
->state_mutex
);
346 EXPORT_SYMBOL_GPL(line6_pcm_release
);
348 /* common PCM hw_params callback */
349 int snd_line6_hw_params(struct snd_pcm_substream
*substream
,
350 struct snd_pcm_hw_params
*hw_params
)
353 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
354 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, substream
->stream
);
356 mutex_lock(&line6pcm
->state_mutex
);
357 ret
= line6_buffer_acquire(line6pcm
, pstr
, substream
->stream
,
362 ret
= snd_pcm_lib_malloc_pages(substream
,
363 params_buffer_bytes(hw_params
));
365 line6_buffer_release(line6pcm
, pstr
, LINE6_STREAM_PCM
);
369 pstr
->period
= params_period_bytes(hw_params
);
371 mutex_unlock(&line6pcm
->state_mutex
);
375 /* common PCM hw_free callback */
376 int snd_line6_hw_free(struct snd_pcm_substream
*substream
)
378 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
379 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, substream
->stream
);
381 mutex_lock(&line6pcm
->state_mutex
);
382 line6_buffer_release(line6pcm
, pstr
, LINE6_STREAM_PCM
);
383 mutex_unlock(&line6pcm
->state_mutex
);
384 return snd_pcm_lib_free_pages(substream
);
388 /* control info callback */
389 static int snd_line6_control_playback_info(struct snd_kcontrol
*kcontrol
,
390 struct snd_ctl_elem_info
*uinfo
)
392 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
394 uinfo
->value
.integer
.min
= 0;
395 uinfo
->value
.integer
.max
= 256;
399 /* control get callback */
400 static int snd_line6_control_playback_get(struct snd_kcontrol
*kcontrol
,
401 struct snd_ctl_elem_value
*ucontrol
)
404 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
406 for (i
= 0; i
< 2; i
++)
407 ucontrol
->value
.integer
.value
[i
] = line6pcm
->volume_playback
[i
];
412 /* control put callback */
413 static int snd_line6_control_playback_put(struct snd_kcontrol
*kcontrol
,
414 struct snd_ctl_elem_value
*ucontrol
)
417 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
419 for (i
= 0; i
< 2; i
++)
420 if (line6pcm
->volume_playback
[i
] !=
421 ucontrol
->value
.integer
.value
[i
]) {
422 line6pcm
->volume_playback
[i
] =
423 ucontrol
->value
.integer
.value
[i
];
430 /* control definition */
431 static const struct snd_kcontrol_new line6_controls
[] = {
433 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
434 .name
= "PCM Playback Volume",
435 .info
= snd_line6_control_playback_info
,
436 .get
= snd_line6_control_playback_get
,
437 .put
= snd_line6_control_playback_put
440 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
441 .name
= "Impulse Response Volume",
442 .info
= snd_line6_impulse_volume_info
,
443 .get
= snd_line6_impulse_volume_get
,
444 .put
= snd_line6_impulse_volume_put
447 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
448 .name
= "Impulse Response Period",
449 .info
= snd_line6_impulse_period_info
,
450 .get
= snd_line6_impulse_period_get
,
451 .put
= snd_line6_impulse_period_put
456 Cleanup the PCM device.
458 static void cleanup_urbs(struct line6_pcm_stream
*pcms
, int iso_buffers
)
462 /* Most likely impossible in current code... */
463 if (pcms
->urbs
== NULL
)
466 for (i
= 0; i
< iso_buffers
; i
++) {
468 usb_kill_urb(pcms
->urbs
[i
]);
469 usb_free_urb(pcms
->urbs
[i
]);
476 static void line6_cleanup_pcm(struct snd_pcm
*pcm
)
478 struct snd_line6_pcm
*line6pcm
= snd_pcm_chip(pcm
);
480 cleanup_urbs(&line6pcm
->out
, line6pcm
->line6
->iso_buffers
);
481 cleanup_urbs(&line6pcm
->in
, line6pcm
->line6
->iso_buffers
);
485 /* create a PCM device */
486 static int snd_line6_new_pcm(struct usb_line6
*line6
, struct snd_pcm
**pcm_ret
)
491 err
= snd_pcm_new(line6
->card
, (char *)line6
->properties
->name
,
496 strcpy(pcm
->name
, line6
->properties
->name
);
499 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
500 &snd_line6_playback_ops
);
501 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_line6_capture_ops
);
503 /* pre-allocation of buffers */
504 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
505 NULL
, 64 * 1024, 128 * 1024);
510 Sync with PCM stream stops.
512 void line6_pcm_disconnect(struct snd_line6_pcm
*line6pcm
)
514 line6_unlink_audio_urbs(line6pcm
, &line6pcm
->out
);
515 line6_unlink_audio_urbs(line6pcm
, &line6pcm
->in
);
516 line6_wait_clear_audio_urbs(line6pcm
, &line6pcm
->out
);
517 line6_wait_clear_audio_urbs(line6pcm
, &line6pcm
->in
);
521 Create and register the PCM device and mixer entries.
522 Create URBs for playback and capture.
524 int line6_init_pcm(struct usb_line6
*line6
,
525 struct line6_pcm_properties
*properties
)
528 unsigned ep_read
= line6
->properties
->ep_audio_r
;
529 unsigned ep_write
= line6
->properties
->ep_audio_w
;
531 struct snd_line6_pcm
*line6pcm
;
533 if (!(line6
->properties
->capabilities
& LINE6_CAP_PCM
))
534 return 0; /* skip PCM initialization and report success */
536 err
= snd_line6_new_pcm(line6
, &pcm
);
540 line6pcm
= kzalloc(sizeof(*line6pcm
), GFP_KERNEL
);
544 mutex_init(&line6pcm
->state_mutex
);
546 line6pcm
->properties
= properties
;
547 line6pcm
->volume_playback
[0] = line6pcm
->volume_playback
[1] = 255;
548 line6pcm
->volume_monitor
= 255;
549 line6pcm
->line6
= line6
;
551 spin_lock_init(&line6pcm
->out
.lock
);
552 spin_lock_init(&line6pcm
->in
.lock
);
553 line6pcm
->impulse_period
= LINE6_IMPULSE_DEFAULT_PERIOD
;
555 line6
->line6pcm
= line6pcm
;
557 pcm
->private_data
= line6pcm
;
558 pcm
->private_free
= line6_cleanup_pcm
;
560 line6pcm
->max_packet_size_in
=
561 usb_maxpacket(line6
->usbdev
,
562 usb_rcvisocpipe(line6
->usbdev
, ep_read
), 0);
563 line6pcm
->max_packet_size_out
=
564 usb_maxpacket(line6
->usbdev
,
565 usb_sndisocpipe(line6
->usbdev
, ep_write
), 1);
566 if (!line6pcm
->max_packet_size_in
|| !line6pcm
->max_packet_size_out
) {
567 dev_err(line6pcm
->line6
->ifcdev
,
568 "cannot get proper max packet size\n");
572 err
= line6_create_audio_out_urbs(line6pcm
);
576 err
= line6_create_audio_in_urbs(line6pcm
);
581 for (i
= 0; i
< ARRAY_SIZE(line6_controls
); i
++) {
582 err
= snd_ctl_add(line6
->card
,
583 snd_ctl_new1(&line6_controls
[i
], line6pcm
));
590 EXPORT_SYMBOL_GPL(line6_init_pcm
);
592 /* prepare pcm callback */
593 int snd_line6_prepare(struct snd_pcm_substream
*substream
)
595 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
596 struct line6_pcm_stream
*pstr
= get_stream(line6pcm
, substream
->stream
);
598 mutex_lock(&line6pcm
->state_mutex
);
600 line6_wait_clear_audio_urbs(line6pcm
, pstr
);
602 if (!test_and_set_bit(LINE6_FLAG_PREPARED
, &line6pcm
->flags
)) {
603 line6pcm
->out
.count
= 0;
604 line6pcm
->out
.pos
= 0;
605 line6pcm
->out
.pos_done
= 0;
606 line6pcm
->out
.bytes
= 0;
607 line6pcm
->in
.count
= 0;
608 line6pcm
->in
.pos_done
= 0;
609 line6pcm
->in
.bytes
= 0;
612 mutex_unlock(&line6pcm
->state_mutex
);