2 * Line6 Linux USB driver - 0.9.1beta
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 <sound/core.h>
14 #include <sound/control.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
24 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
26 static struct snd_line6_pcm
*dev2pcm(struct device
*dev
)
28 struct usb_interface
*interface
= to_usb_interface(dev
);
29 struct usb_line6
*line6
= usb_get_intfdata(interface
);
30 struct snd_line6_pcm
*line6pcm
= line6
->line6pcm
;
35 "read" request on "impulse_volume" special file.
37 static ssize_t
impulse_volume_show(struct device
*dev
,
38 struct device_attribute
*attr
, char *buf
)
40 return sprintf(buf
, "%d\n", dev2pcm(dev
)->impulse_volume
);
44 "write" request on "impulse_volume" special file.
46 static ssize_t
impulse_volume_store(struct device
*dev
,
47 struct device_attribute
*attr
,
48 const char *buf
, size_t count
)
50 struct snd_line6_pcm
*line6pcm
= dev2pcm(dev
);
54 ret
= kstrtoint(buf
, 10, &value
);
58 line6pcm
->impulse_volume
= value
;
61 line6_pcm_acquire(line6pcm
, LINE6_BITS_PCM_IMPULSE
);
63 line6_pcm_release(line6pcm
, LINE6_BITS_PCM_IMPULSE
);
67 static DEVICE_ATTR_RW(impulse_volume
);
70 "read" request on "impulse_period" special file.
72 static ssize_t
impulse_period_show(struct device
*dev
,
73 struct device_attribute
*attr
, char *buf
)
75 return sprintf(buf
, "%d\n", dev2pcm(dev
)->impulse_period
);
79 "write" request on "impulse_period" special file.
81 static ssize_t
impulse_period_store(struct device
*dev
,
82 struct device_attribute
*attr
,
83 const char *buf
, size_t count
)
88 ret
= kstrtoint(buf
, 10, &value
);
92 dev2pcm(dev
)->impulse_period
= value
;
95 static DEVICE_ATTR_RW(impulse_period
);
99 static bool test_flags(unsigned long flags0
, unsigned long flags1
,
102 return ((flags0
& mask
) == 0) && ((flags1
& mask
) != 0);
105 int line6_pcm_acquire(struct snd_line6_pcm
*line6pcm
, int channels
)
107 unsigned long flags_old
, flags_new
, flags_final
;
111 flags_old
= ACCESS_ONCE(line6pcm
->flags
);
112 flags_new
= flags_old
| channels
;
113 } while (cmpxchg(&line6pcm
->flags
, flags_old
, flags_new
) != flags_old
);
115 flags_final
= flags_old
;
117 line6pcm
->prev_fbuf
= NULL
;
119 if (test_flags(flags_old
, flags_new
, LINE6_BITS_CAPTURE_BUFFER
)) {
120 /* Invoked multiple times in a row so allocate once only */
121 if (!line6pcm
->buffer_in
) {
122 line6pcm
->buffer_in
=
123 kmalloc(LINE6_ISO_BUFFERS
* LINE6_ISO_PACKETS
*
124 line6pcm
->max_packet_size
, GFP_KERNEL
);
125 if (!line6pcm
->buffer_in
) {
127 goto pcm_acquire_error
;
130 flags_final
|= channels
& LINE6_BITS_CAPTURE_BUFFER
;
134 if (test_flags(flags_old
, flags_new
, LINE6_BITS_CAPTURE_STREAM
)) {
136 Waiting for completion of active URBs in the stop handler is
137 a bug, we therefore report an error if capturing is restarted
140 if (line6pcm
->active_urb_in
| line6pcm
->unlink_urb_in
) {
141 dev_err(line6pcm
->line6
->ifcdev
, "Device not yet ready\n");
145 line6pcm
->count_in
= 0;
146 line6pcm
->prev_fsize
= 0;
147 err
= line6_submit_audio_in_all_urbs(line6pcm
);
150 goto pcm_acquire_error
;
152 flags_final
|= channels
& LINE6_BITS_CAPTURE_STREAM
;
155 if (test_flags(flags_old
, flags_new
, LINE6_BITS_PLAYBACK_BUFFER
)) {
156 /* Invoked multiple times in a row so allocate once only */
157 if (!line6pcm
->buffer_out
) {
158 line6pcm
->buffer_out
=
159 kmalloc(LINE6_ISO_BUFFERS
* LINE6_ISO_PACKETS
*
160 line6pcm
->max_packet_size
, GFP_KERNEL
);
161 if (!line6pcm
->buffer_out
) {
163 goto pcm_acquire_error
;
166 flags_final
|= channels
& LINE6_BITS_PLAYBACK_BUFFER
;
170 if (test_flags(flags_old
, flags_new
, LINE6_BITS_PLAYBACK_STREAM
)) {
172 See comment above regarding PCM restart.
174 if (line6pcm
->active_urb_out
| line6pcm
->unlink_urb_out
) {
175 dev_err(line6pcm
->line6
->ifcdev
, "Device not yet ready\n");
179 line6pcm
->count_out
= 0;
180 err
= line6_submit_audio_out_all_urbs(line6pcm
);
183 goto pcm_acquire_error
;
185 flags_final
|= channels
& LINE6_BITS_PLAYBACK_STREAM
;
192 If not all requested resources/streams could be obtained, release
193 those which were successfully obtained (if any).
195 line6_pcm_release(line6pcm
, flags_final
& channels
);
199 int line6_pcm_release(struct snd_line6_pcm
*line6pcm
, int channels
)
201 unsigned long flags_old
, flags_new
;
204 flags_old
= ACCESS_ONCE(line6pcm
->flags
);
205 flags_new
= flags_old
& ~channels
;
206 } while (cmpxchg(&line6pcm
->flags
, flags_old
, flags_new
) != flags_old
);
208 if (test_flags(flags_new
, flags_old
, LINE6_BITS_CAPTURE_STREAM
))
209 line6_unlink_audio_in_urbs(line6pcm
);
211 if (test_flags(flags_new
, flags_old
, LINE6_BITS_CAPTURE_BUFFER
)) {
212 line6_wait_clear_audio_in_urbs(line6pcm
);
213 line6_free_capture_buffer(line6pcm
);
216 if (test_flags(flags_new
, flags_old
, LINE6_BITS_PLAYBACK_STREAM
))
217 line6_unlink_audio_out_urbs(line6pcm
);
219 if (test_flags(flags_new
, flags_old
, LINE6_BITS_PLAYBACK_BUFFER
)) {
220 line6_wait_clear_audio_out_urbs(line6pcm
);
221 line6_free_playback_buffer(line6pcm
);
227 /* 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
;
235 spin_lock_irqsave(&line6pcm
->lock_trigger
, flags
);
236 clear_bit(LINE6_INDEX_PREPARED
, &line6pcm
->flags
);
238 snd_pcm_group_for_each_entry(s
, substream
) {
240 case SNDRV_PCM_STREAM_PLAYBACK
:
241 err
= snd_line6_playback_trigger(line6pcm
, cmd
);
244 spin_unlock_irqrestore(&line6pcm
->lock_trigger
,
251 case SNDRV_PCM_STREAM_CAPTURE
:
252 err
= snd_line6_capture_trigger(line6pcm
, cmd
);
255 spin_unlock_irqrestore(&line6pcm
->lock_trigger
,
263 dev_err(line6pcm
->line6
->ifcdev
,
264 "Unknown stream direction %d\n", s
->stream
);
268 spin_unlock_irqrestore(&line6pcm
->lock_trigger
, flags
);
272 /* control info callback */
273 static int snd_line6_control_playback_info(struct snd_kcontrol
*kcontrol
,
274 struct snd_ctl_elem_info
*uinfo
)
276 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
278 uinfo
->value
.integer
.min
= 0;
279 uinfo
->value
.integer
.max
= 256;
283 /* control get callback */
284 static int snd_line6_control_playback_get(struct snd_kcontrol
*kcontrol
,
285 struct snd_ctl_elem_value
*ucontrol
)
288 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
291 ucontrol
->value
.integer
.value
[i
] = line6pcm
->volume_playback
[i
];
296 /* control put callback */
297 static int snd_line6_control_playback_put(struct snd_kcontrol
*kcontrol
,
298 struct snd_ctl_elem_value
*ucontrol
)
301 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
304 if (line6pcm
->volume_playback
[i
] !=
305 ucontrol
->value
.integer
.value
[i
]) {
306 line6pcm
->volume_playback
[i
] =
307 ucontrol
->value
.integer
.value
[i
];
314 /* control definition */
315 static struct snd_kcontrol_new line6_control_playback
= {
316 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
317 .name
= "PCM Playback Volume",
319 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
320 .info
= snd_line6_control_playback_info
,
321 .get
= snd_line6_control_playback_get
,
322 .put
= snd_line6_control_playback_put
326 Cleanup the PCM device.
328 static void line6_cleanup_pcm(struct snd_pcm
*pcm
)
331 struct snd_line6_pcm
*line6pcm
= snd_pcm_chip(pcm
);
333 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
334 device_remove_file(line6pcm
->line6
->ifcdev
, &dev_attr_impulse_volume
);
335 device_remove_file(line6pcm
->line6
->ifcdev
, &dev_attr_impulse_period
);
338 for (i
= LINE6_ISO_BUFFERS
; i
--;) {
339 if (line6pcm
->urb_audio_out
[i
]) {
340 usb_kill_urb(line6pcm
->urb_audio_out
[i
]);
341 usb_free_urb(line6pcm
->urb_audio_out
[i
]);
343 if (line6pcm
->urb_audio_in
[i
]) {
344 usb_kill_urb(line6pcm
->urb_audio_in
[i
]);
345 usb_free_urb(line6pcm
->urb_audio_in
[i
]);
350 /* create a PCM device */
351 static int snd_line6_new_pcm(struct snd_line6_pcm
*line6pcm
)
356 err
= snd_pcm_new(line6pcm
->line6
->card
,
357 (char *)line6pcm
->line6
->properties
->name
,
362 pcm
->private_data
= line6pcm
;
363 pcm
->private_free
= line6_cleanup_pcm
;
365 strcpy(pcm
->name
, line6pcm
->line6
->properties
->name
);
368 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
369 &snd_line6_playback_ops
);
370 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_line6_capture_ops
);
372 /* pre-allocation of buffers */
373 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
374 snd_dma_continuous_data
375 (GFP_KERNEL
), 64 * 1024,
381 /* PCM device destructor */
382 static int snd_line6_pcm_free(struct snd_device
*device
)
388 Stop substream if still running.
390 static void pcm_disconnect_substream(struct snd_pcm_substream
*substream
)
392 if (substream
->runtime
&& snd_pcm_running(substream
)) {
393 snd_pcm_stream_lock_irq(substream
);
394 snd_pcm_stop(substream
, SNDRV_PCM_STATE_DISCONNECTED
);
395 snd_pcm_stream_unlock_irq(substream
);
402 void line6_pcm_disconnect(struct snd_line6_pcm
*line6pcm
)
404 pcm_disconnect_substream(get_substream
405 (line6pcm
, SNDRV_PCM_STREAM_CAPTURE
));
406 pcm_disconnect_substream(get_substream
407 (line6pcm
, SNDRV_PCM_STREAM_PLAYBACK
));
408 line6_unlink_wait_clear_audio_out_urbs(line6pcm
);
409 line6_unlink_wait_clear_audio_in_urbs(line6pcm
);
413 Create and register the PCM device and mixer entries.
414 Create URBs for playback and capture.
416 int line6_init_pcm(struct usb_line6
*line6
,
417 struct line6_pcm_properties
*properties
)
419 static struct snd_device_ops pcm_ops
= {
420 .dev_free
= snd_line6_pcm_free
,
424 int ep_read
= 0, ep_write
= 0;
425 struct snd_line6_pcm
*line6pcm
;
427 if (!(line6
->properties
->capabilities
& LINE6_BIT_PCM
))
428 return 0; /* skip PCM initialization and report success */
430 /* initialize PCM subsystem based on product id: */
431 switch (line6
->product
) {
432 case LINE6_DEVID_BASSPODXT
:
433 case LINE6_DEVID_BASSPODXTLIVE
:
434 case LINE6_DEVID_BASSPODXTPRO
:
435 case LINE6_DEVID_PODXT
:
436 case LINE6_DEVID_PODXTLIVE
:
437 case LINE6_DEVID_PODXTPRO
:
438 case LINE6_DEVID_PODHD300
:
439 case LINE6_DEVID_PODHD400
:
444 case LINE6_DEVID_PODHD500
:
445 case LINE6_DEVID_PODX3
:
446 case LINE6_DEVID_PODX3LIVE
:
451 case LINE6_DEVID_POCKETPOD
:
456 case LINE6_DEVID_GUITARPORT
:
457 case LINE6_DEVID_PODSTUDIO_GX
:
458 case LINE6_DEVID_PODSTUDIO_UX1
:
459 case LINE6_DEVID_PODSTUDIO_UX2
:
460 case LINE6_DEVID_TONEPORT_GX
:
461 case LINE6_DEVID_TONEPORT_UX1
:
462 case LINE6_DEVID_TONEPORT_UX2
:
467 /* this is for interface_number == 1:
468 case LINE6_DEVID_TONEPORT_UX2:
469 case LINE6_DEVID_PODSTUDIO_UX2:
478 line6pcm
= kzalloc(sizeof(struct snd_line6_pcm
), GFP_KERNEL
);
480 if (line6pcm
== NULL
)
483 line6pcm
->volume_playback
[0] = line6pcm
->volume_playback
[1] = 255;
484 line6pcm
->volume_monitor
= 255;
485 line6pcm
->line6
= line6
;
486 line6pcm
->ep_audio_read
= ep_read
;
487 line6pcm
->ep_audio_write
= ep_write
;
489 /* Read and write buffers are sized identically, so choose minimum */
490 line6pcm
->max_packet_size
= min(
491 usb_maxpacket(line6
->usbdev
,
492 usb_rcvisocpipe(line6
->usbdev
, ep_read
), 0),
493 usb_maxpacket(line6
->usbdev
,
494 usb_sndisocpipe(line6
->usbdev
, ep_write
), 1));
496 line6pcm
->properties
= properties
;
497 line6
->line6pcm
= line6pcm
;
500 err
= snd_device_new(line6
->card
, SNDRV_DEV_PCM
, line6
, &pcm_ops
);
504 snd_card_set_dev(line6
->card
, line6
->ifcdev
);
506 err
= snd_line6_new_pcm(line6pcm
);
510 spin_lock_init(&line6pcm
->lock_audio_out
);
511 spin_lock_init(&line6pcm
->lock_audio_in
);
512 spin_lock_init(&line6pcm
->lock_trigger
);
514 err
= line6_create_audio_out_urbs(line6pcm
);
518 err
= line6_create_audio_in_urbs(line6pcm
);
524 snd_ctl_add(line6
->card
,
525 snd_ctl_new1(&line6_control_playback
, line6pcm
));
529 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
530 /* impulse response test: */
531 err
= device_create_file(line6
->ifcdev
, &dev_attr_impulse_volume
);
535 err
= device_create_file(line6
->ifcdev
, &dev_attr_impulse_period
);
539 line6pcm
->impulse_period
= LINE6_IMPULSE_DEFAULT_PERIOD
;
545 /* prepare pcm callback */
546 int snd_line6_prepare(struct snd_pcm_substream
*substream
)
548 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
550 switch (substream
->stream
) {
551 case SNDRV_PCM_STREAM_PLAYBACK
:
552 if ((line6pcm
->flags
& LINE6_BITS_PLAYBACK_STREAM
) == 0)
553 line6_unlink_wait_clear_audio_out_urbs(line6pcm
);
557 case SNDRV_PCM_STREAM_CAPTURE
:
558 if ((line6pcm
->flags
& LINE6_BITS_CAPTURE_STREAM
) == 0)
559 line6_unlink_wait_clear_audio_in_urbs(line6pcm
);
567 if (!test_and_set_bit(LINE6_INDEX_PREPARED
, &line6pcm
->flags
)) {
568 line6pcm
->count_out
= 0;
569 line6pcm
->pos_out
= 0;
570 line6pcm
->pos_out_done
= 0;
571 line6pcm
->bytes_out
= 0;
572 line6pcm
->count_in
= 0;
573 line6pcm
->pos_in_done
= 0;
574 line6pcm
->bytes_in
= 0;