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
pcm_get_impulse_volume(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
pcm_set_impulse_volume(struct device
*dev
,
47 struct device_attribute
*attr
,
48 const char *buf
, size_t count
)
50 struct snd_line6_pcm
*line6pcm
= dev2pcm(dev
);
54 rv
= 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
);
69 "read" request on "impulse_period" special file.
71 static ssize_t
pcm_get_impulse_period(struct device
*dev
,
72 struct device_attribute
*attr
, char *buf
)
74 return sprintf(buf
, "%d\n", dev2pcm(dev
)->impulse_period
);
78 "write" request on "impulse_period" special file.
80 static ssize_t
pcm_set_impulse_period(struct device
*dev
,
81 struct device_attribute
*attr
,
82 const char *buf
, size_t count
)
84 dev2pcm(dev
)->impulse_period
= simple_strtoul(buf
, NULL
, 10);
88 static DEVICE_ATTR(impulse_volume
, S_IWUSR
| S_IRUGO
, pcm_get_impulse_volume
,
89 pcm_set_impulse_volume
);
90 static DEVICE_ATTR(impulse_period
, S_IWUSR
| S_IRUGO
, pcm_get_impulse_period
,
91 pcm_set_impulse_period
);
95 static bool test_flags(unsigned long flags0
, unsigned long flags1
,
98 return ((flags0
& mask
) == 0) && ((flags1
& mask
) != 0);
101 int line6_pcm_acquire(struct snd_line6_pcm
*line6pcm
, int channels
)
103 unsigned long flags_old
=
104 __sync_fetch_and_or(&line6pcm
->flags
, channels
);
105 unsigned long flags_new
= flags_old
| channels
;
106 unsigned long flags_final
= flags_old
;
109 line6pcm
->prev_fbuf
= NULL
;
111 if (test_flags(flags_old
, flags_new
, LINE6_BITS_CAPTURE_BUFFER
)) {
112 /* We may be invoked multiple times in a row so allocate once only */
113 if (!line6pcm
->buffer_in
) {
114 line6pcm
->buffer_in
=
115 kmalloc(LINE6_ISO_BUFFERS
* LINE6_ISO_PACKETS
*
116 line6pcm
->max_packet_size
, GFP_KERNEL
);
118 if (!line6pcm
->buffer_in
) {
119 dev_err(line6pcm
->line6
->ifcdev
,
120 "cannot malloc capture buffer\n");
122 goto pcm_acquire_error
;
125 flags_final
|= channels
& LINE6_BITS_CAPTURE_BUFFER
;
129 if (test_flags(flags_old
, flags_new
, LINE6_BITS_CAPTURE_STREAM
)) {
131 Waiting for completion of active URBs in the stop handler is
132 a bug, we therefore report an error if capturing is restarted
135 if (line6pcm
->active_urb_in
| line6pcm
->unlink_urb_in
) {
136 dev_err(line6pcm
->line6
->ifcdev
, "Device not yet ready\n");
140 line6pcm
->count_in
= 0;
141 line6pcm
->prev_fsize
= 0;
142 err
= line6_submit_audio_in_all_urbs(line6pcm
);
145 goto pcm_acquire_error
;
147 flags_final
|= channels
& LINE6_BITS_CAPTURE_STREAM
;
150 if (test_flags(flags_old
, flags_new
, LINE6_BITS_PLAYBACK_BUFFER
)) {
151 /* We may be 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
);
157 if (!line6pcm
->buffer_out
) {
158 dev_err(line6pcm
->line6
->ifcdev
,
159 "cannot malloc playback buffer\n");
161 goto pcm_acquire_error
;
164 flags_final
|= channels
& LINE6_BITS_PLAYBACK_BUFFER
;
168 if (test_flags(flags_old
, flags_new
, LINE6_BITS_PLAYBACK_STREAM
)) {
170 See comment above regarding PCM restart.
172 if (line6pcm
->active_urb_out
| line6pcm
->unlink_urb_out
) {
173 dev_err(line6pcm
->line6
->ifcdev
, "Device not yet ready\n");
177 line6pcm
->count_out
= 0;
178 err
= line6_submit_audio_out_all_urbs(line6pcm
);
181 goto pcm_acquire_error
;
183 flags_final
|= channels
& LINE6_BITS_PLAYBACK_STREAM
;
190 If not all requested resources/streams could be obtained, release
191 those which were successfully obtained (if any).
193 line6_pcm_release(line6pcm
, flags_final
& channels
);
197 int line6_pcm_release(struct snd_line6_pcm
*line6pcm
, int channels
)
199 unsigned long flags_old
=
200 __sync_fetch_and_and(&line6pcm
->flags
, ~channels
);
201 unsigned long flags_new
= flags_old
& ~channels
;
203 if (test_flags(flags_new
, flags_old
, LINE6_BITS_CAPTURE_STREAM
))
204 line6_unlink_audio_in_urbs(line6pcm
);
206 if (test_flags(flags_new
, flags_old
, LINE6_BITS_CAPTURE_BUFFER
)) {
207 line6_wait_clear_audio_in_urbs(line6pcm
);
208 line6_free_capture_buffer(line6pcm
);
211 if (test_flags(flags_new
, flags_old
, LINE6_BITS_PLAYBACK_STREAM
))
212 line6_unlink_audio_out_urbs(line6pcm
);
214 if (test_flags(flags_new
, flags_old
, LINE6_BITS_PLAYBACK_BUFFER
)) {
215 line6_wait_clear_audio_out_urbs(line6pcm
);
216 line6_free_playback_buffer(line6pcm
);
222 /* trigger callback */
223 int snd_line6_trigger(struct snd_pcm_substream
*substream
, int cmd
)
225 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
226 struct snd_pcm_substream
*s
;
230 spin_lock_irqsave(&line6pcm
->lock_trigger
, flags
);
231 clear_bit(LINE6_INDEX_PREPARED
, &line6pcm
->flags
);
233 snd_pcm_group_for_each_entry(s
, substream
) {
235 case SNDRV_PCM_STREAM_PLAYBACK
:
236 err
= snd_line6_playback_trigger(line6pcm
, cmd
);
239 spin_unlock_irqrestore(&line6pcm
->lock_trigger
,
246 case SNDRV_PCM_STREAM_CAPTURE
:
247 err
= snd_line6_capture_trigger(line6pcm
, cmd
);
250 spin_unlock_irqrestore(&line6pcm
->lock_trigger
,
258 dev_err(line6pcm
->line6
->ifcdev
,
259 "Unknown stream direction %d\n", s
->stream
);
263 spin_unlock_irqrestore(&line6pcm
->lock_trigger
, flags
);
267 /* control info callback */
268 static int snd_line6_control_playback_info(struct snd_kcontrol
*kcontrol
,
269 struct snd_ctl_elem_info
*uinfo
)
271 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
273 uinfo
->value
.integer
.min
= 0;
274 uinfo
->value
.integer
.max
= 256;
278 /* control get callback */
279 static int snd_line6_control_playback_get(struct snd_kcontrol
*kcontrol
,
280 struct snd_ctl_elem_value
*ucontrol
)
283 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
286 ucontrol
->value
.integer
.value
[i
] = line6pcm
->volume_playback
[i
];
291 /* control put callback */
292 static int snd_line6_control_playback_put(struct snd_kcontrol
*kcontrol
,
293 struct snd_ctl_elem_value
*ucontrol
)
296 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
299 if (line6pcm
->volume_playback
[i
] !=
300 ucontrol
->value
.integer
.value
[i
]) {
301 line6pcm
->volume_playback
[i
] =
302 ucontrol
->value
.integer
.value
[i
];
309 /* control definition */
310 static struct snd_kcontrol_new line6_control_playback
= {
311 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
312 .name
= "PCM Playback Volume",
314 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
315 .info
= snd_line6_control_playback_info
,
316 .get
= snd_line6_control_playback_get
,
317 .put
= snd_line6_control_playback_put
321 Cleanup the PCM device.
323 static void line6_cleanup_pcm(struct snd_pcm
*pcm
)
326 struct snd_line6_pcm
*line6pcm
= snd_pcm_chip(pcm
);
328 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
329 device_remove_file(line6pcm
->line6
->ifcdev
, &dev_attr_impulse_volume
);
330 device_remove_file(line6pcm
->line6
->ifcdev
, &dev_attr_impulse_period
);
333 for (i
= LINE6_ISO_BUFFERS
; i
--;) {
334 if (line6pcm
->urb_audio_out
[i
]) {
335 usb_kill_urb(line6pcm
->urb_audio_out
[i
]);
336 usb_free_urb(line6pcm
->urb_audio_out
[i
]);
338 if (line6pcm
->urb_audio_in
[i
]) {
339 usb_kill_urb(line6pcm
->urb_audio_in
[i
]);
340 usb_free_urb(line6pcm
->urb_audio_in
[i
]);
345 /* create a PCM device */
346 static int snd_line6_new_pcm(struct snd_line6_pcm
*line6pcm
)
351 err
= snd_pcm_new(line6pcm
->line6
->card
,
352 (char *)line6pcm
->line6
->properties
->name
,
357 pcm
->private_data
= line6pcm
;
358 pcm
->private_free
= line6_cleanup_pcm
;
360 strcpy(pcm
->name
, line6pcm
->line6
->properties
->name
);
363 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
364 &snd_line6_playback_ops
);
365 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_line6_capture_ops
);
367 /* pre-allocation of buffers */
368 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
369 snd_dma_continuous_data
370 (GFP_KERNEL
), 64 * 1024,
376 /* PCM device destructor */
377 static int snd_line6_pcm_free(struct snd_device
*device
)
383 Stop substream if still running.
385 static void pcm_disconnect_substream(struct snd_pcm_substream
*substream
)
387 if (substream
->runtime
&& snd_pcm_running(substream
))
388 snd_pcm_stop(substream
, SNDRV_PCM_STATE_DISCONNECTED
);
394 void line6_pcm_disconnect(struct snd_line6_pcm
*line6pcm
)
396 pcm_disconnect_substream(get_substream
397 (line6pcm
, SNDRV_PCM_STREAM_CAPTURE
));
398 pcm_disconnect_substream(get_substream
399 (line6pcm
, SNDRV_PCM_STREAM_PLAYBACK
));
400 line6_unlink_wait_clear_audio_out_urbs(line6pcm
);
401 line6_unlink_wait_clear_audio_in_urbs(line6pcm
);
405 Create and register the PCM device and mixer entries.
406 Create URBs for playback and capture.
408 int line6_init_pcm(struct usb_line6
*line6
,
409 struct line6_pcm_properties
*properties
)
411 static struct snd_device_ops pcm_ops
= {
412 .dev_free
= snd_line6_pcm_free
,
416 int ep_read
= 0, ep_write
= 0;
417 struct snd_line6_pcm
*line6pcm
;
419 if (!(line6
->properties
->capabilities
& LINE6_BIT_PCM
))
420 return 0; /* skip PCM initialization and report success */
422 /* initialize PCM subsystem based on product id: */
423 switch (line6
->product
) {
424 case LINE6_DEVID_BASSPODXT
:
425 case LINE6_DEVID_BASSPODXTLIVE
:
426 case LINE6_DEVID_BASSPODXTPRO
:
427 case LINE6_DEVID_PODXT
:
428 case LINE6_DEVID_PODXTLIVE
:
429 case LINE6_DEVID_PODXTPRO
:
430 case LINE6_DEVID_PODHD300
:
435 case LINE6_DEVID_PODHD500
:
436 case LINE6_DEVID_PODX3
:
437 case LINE6_DEVID_PODX3LIVE
:
442 case LINE6_DEVID_POCKETPOD
:
447 case LINE6_DEVID_GUITARPORT
:
448 case LINE6_DEVID_PODSTUDIO_GX
:
449 case LINE6_DEVID_PODSTUDIO_UX1
:
450 case LINE6_DEVID_PODSTUDIO_UX2
:
451 case LINE6_DEVID_TONEPORT_GX
:
452 case LINE6_DEVID_TONEPORT_UX1
:
453 case LINE6_DEVID_TONEPORT_UX2
:
458 /* this is for interface_number == 1:
459 case LINE6_DEVID_TONEPORT_UX2:
460 case LINE6_DEVID_PODSTUDIO_UX2:
470 line6pcm
= kzalloc(sizeof(struct snd_line6_pcm
), GFP_KERNEL
);
472 if (line6pcm
== NULL
)
475 line6pcm
->volume_playback
[0] = line6pcm
->volume_playback
[1] = 255;
476 line6pcm
->volume_monitor
= 255;
477 line6pcm
->line6
= line6
;
478 line6pcm
->ep_audio_read
= ep_read
;
479 line6pcm
->ep_audio_write
= ep_write
;
481 /* Read and write buffers are sized identically, so choose minimum */
482 line6pcm
->max_packet_size
= min(
483 usb_maxpacket(line6
->usbdev
,
484 usb_rcvisocpipe(line6
->usbdev
, ep_read
), 0),
485 usb_maxpacket(line6
->usbdev
,
486 usb_sndisocpipe(line6
->usbdev
, ep_write
), 1));
488 line6pcm
->properties
= properties
;
489 line6
->line6pcm
= line6pcm
;
492 err
= snd_device_new(line6
->card
, SNDRV_DEV_PCM
, line6
, &pcm_ops
);
496 snd_card_set_dev(line6
->card
, line6
->ifcdev
);
498 err
= snd_line6_new_pcm(line6pcm
);
502 spin_lock_init(&line6pcm
->lock_audio_out
);
503 spin_lock_init(&line6pcm
->lock_audio_in
);
504 spin_lock_init(&line6pcm
->lock_trigger
);
506 err
= line6_create_audio_out_urbs(line6pcm
);
510 err
= line6_create_audio_in_urbs(line6pcm
);
516 snd_ctl_add(line6
->card
,
517 snd_ctl_new1(&line6_control_playback
, line6pcm
));
521 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
522 /* impulse response test: */
523 err
= device_create_file(line6
->ifcdev
, &dev_attr_impulse_volume
);
527 err
= device_create_file(line6
->ifcdev
, &dev_attr_impulse_period
);
531 line6pcm
->impulse_period
= LINE6_IMPULSE_DEFAULT_PERIOD
;
537 /* prepare pcm callback */
538 int snd_line6_prepare(struct snd_pcm_substream
*substream
)
540 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
542 switch (substream
->stream
) {
543 case SNDRV_PCM_STREAM_PLAYBACK
:
544 if ((line6pcm
->flags
& LINE6_BITS_PLAYBACK_STREAM
) == 0)
545 line6_unlink_wait_clear_audio_out_urbs(line6pcm
);
549 case SNDRV_PCM_STREAM_CAPTURE
:
550 if ((line6pcm
->flags
& LINE6_BITS_CAPTURE_STREAM
) == 0)
551 line6_unlink_wait_clear_audio_in_urbs(line6pcm
);
559 if (!test_and_set_bit(LINE6_INDEX_PREPARED
, &line6pcm
->flags
)) {
560 line6pcm
->count_out
= 0;
561 line6pcm
->pos_out
= 0;
562 line6pcm
->pos_out_done
= 0;
563 line6pcm
->bytes_out
= 0;
564 line6pcm
->count_in
= 0;
565 line6pcm
->pos_in_done
= 0;
566 line6pcm
->bytes_in
= 0;