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
);
51 int value
= simple_strtoul(buf
, NULL
, 10);
52 line6pcm
->impulse_volume
= value
;
55 line6_pcm_acquire(line6pcm
, LINE6_BITS_PCM_IMPULSE
);
57 line6_pcm_release(line6pcm
, LINE6_BITS_PCM_IMPULSE
);
63 "read" request on "impulse_period" special file.
65 static ssize_t
pcm_get_impulse_period(struct device
*dev
,
66 struct device_attribute
*attr
, char *buf
)
68 return sprintf(buf
, "%d\n", dev2pcm(dev
)->impulse_period
);
72 "write" request on "impulse_period" special file.
74 static ssize_t
pcm_set_impulse_period(struct device
*dev
,
75 struct device_attribute
*attr
,
76 const char *buf
, size_t count
)
78 dev2pcm(dev
)->impulse_period
= simple_strtoul(buf
, NULL
, 10);
82 static DEVICE_ATTR(impulse_volume
, S_IWUSR
| S_IRUGO
, pcm_get_impulse_volume
,
83 pcm_set_impulse_volume
);
84 static DEVICE_ATTR(impulse_period
, S_IWUSR
| S_IRUGO
, pcm_get_impulse_period
,
85 pcm_set_impulse_period
);
89 static bool test_flags(unsigned long flags0
, unsigned long flags1
,
92 return ((flags0
& mask
) == 0) && ((flags1
& mask
) != 0);
95 int line6_pcm_acquire(struct snd_line6_pcm
*line6pcm
, int channels
)
97 unsigned long flags_old
=
98 __sync_fetch_and_or(&line6pcm
->flags
, channels
);
99 unsigned long flags_new
= flags_old
| channels
;
100 unsigned long flags_final
= flags_old
;
103 line6pcm
->prev_fbuf
= NULL
;
105 if (test_flags(flags_old
, flags_new
, LINE6_BITS_CAPTURE_BUFFER
)) {
106 /* We may be invoked multiple times in a row so allocate once only */
107 if (!line6pcm
->buffer_in
) {
108 line6pcm
->buffer_in
=
109 kmalloc(LINE6_ISO_BUFFERS
* LINE6_ISO_PACKETS
*
110 line6pcm
->max_packet_size
, GFP_KERNEL
);
112 if (!line6pcm
->buffer_in
) {
113 dev_err(line6pcm
->line6
->ifcdev
,
114 "cannot malloc capture buffer\n");
116 goto pcm_acquire_error
;
119 flags_final
|= channels
& LINE6_BITS_CAPTURE_BUFFER
;
123 if (test_flags(flags_old
, flags_new
, LINE6_BITS_CAPTURE_STREAM
)) {
125 Waiting for completion of active URBs in the stop handler is
126 a bug, we therefore report an error if capturing is restarted
129 if (line6pcm
->active_urb_in
| line6pcm
->unlink_urb_in
) {
130 dev_err(line6pcm
->line6
->ifcdev
, "Device not yet ready\n");
134 line6pcm
->count_in
= 0;
135 line6pcm
->prev_fsize
= 0;
136 err
= line6_submit_audio_in_all_urbs(line6pcm
);
139 goto pcm_acquire_error
;
141 flags_final
|= channels
& LINE6_BITS_CAPTURE_STREAM
;
144 if (test_flags(flags_old
, flags_new
, LINE6_BITS_PLAYBACK_BUFFER
)) {
145 /* We may be invoked multiple times in a row so allocate once only */
146 if (!line6pcm
->buffer_out
) {
147 line6pcm
->buffer_out
=
148 kmalloc(LINE6_ISO_BUFFERS
* LINE6_ISO_PACKETS
*
149 line6pcm
->max_packet_size
, GFP_KERNEL
);
151 if (!line6pcm
->buffer_out
) {
152 dev_err(line6pcm
->line6
->ifcdev
,
153 "cannot malloc playback buffer\n");
155 goto pcm_acquire_error
;
158 flags_final
|= channels
& LINE6_BITS_PLAYBACK_BUFFER
;
162 if (test_flags(flags_old
, flags_new
, LINE6_BITS_PLAYBACK_STREAM
)) {
164 See comment above regarding PCM restart.
166 if (line6pcm
->active_urb_out
| line6pcm
->unlink_urb_out
) {
167 dev_err(line6pcm
->line6
->ifcdev
, "Device not yet ready\n");
171 line6pcm
->count_out
= 0;
172 err
= line6_submit_audio_out_all_urbs(line6pcm
);
175 goto pcm_acquire_error
;
177 flags_final
|= channels
& LINE6_BITS_PLAYBACK_STREAM
;
184 If not all requested resources/streams could be obtained, release
185 those which were successfully obtained (if any).
187 line6_pcm_release(line6pcm
, flags_final
& channels
);
191 int line6_pcm_release(struct snd_line6_pcm
*line6pcm
, int channels
)
193 unsigned long flags_old
=
194 __sync_fetch_and_and(&line6pcm
->flags
, ~channels
);
195 unsigned long flags_new
= flags_old
& ~channels
;
197 if (test_flags(flags_new
, flags_old
, LINE6_BITS_CAPTURE_STREAM
))
198 line6_unlink_audio_in_urbs(line6pcm
);
200 if (test_flags(flags_new
, flags_old
, LINE6_BITS_CAPTURE_BUFFER
)) {
201 line6_wait_clear_audio_in_urbs(line6pcm
);
202 line6_free_capture_buffer(line6pcm
);
205 if (test_flags(flags_new
, flags_old
, LINE6_BITS_PLAYBACK_STREAM
))
206 line6_unlink_audio_out_urbs(line6pcm
);
208 if (test_flags(flags_new
, flags_old
, LINE6_BITS_PLAYBACK_BUFFER
)) {
209 line6_wait_clear_audio_out_urbs(line6pcm
);
210 line6_free_playback_buffer(line6pcm
);
216 /* trigger callback */
217 int snd_line6_trigger(struct snd_pcm_substream
*substream
, int cmd
)
219 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
220 struct snd_pcm_substream
*s
;
224 spin_lock_irqsave(&line6pcm
->lock_trigger
, flags
);
225 clear_bit(LINE6_INDEX_PREPARED
, &line6pcm
->flags
);
227 snd_pcm_group_for_each_entry(s
, substream
) {
229 case SNDRV_PCM_STREAM_PLAYBACK
:
230 err
= snd_line6_playback_trigger(line6pcm
, cmd
);
233 spin_unlock_irqrestore(&line6pcm
->lock_trigger
,
240 case SNDRV_PCM_STREAM_CAPTURE
:
241 err
= snd_line6_capture_trigger(line6pcm
, cmd
);
244 spin_unlock_irqrestore(&line6pcm
->lock_trigger
,
252 dev_err(line6pcm
->line6
->ifcdev
,
253 "Unknown stream direction %d\n", s
->stream
);
257 spin_unlock_irqrestore(&line6pcm
->lock_trigger
, flags
);
261 /* control info callback */
262 static int snd_line6_control_playback_info(struct snd_kcontrol
*kcontrol
,
263 struct snd_ctl_elem_info
*uinfo
)
265 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
267 uinfo
->value
.integer
.min
= 0;
268 uinfo
->value
.integer
.max
= 256;
272 /* control get callback */
273 static int snd_line6_control_playback_get(struct snd_kcontrol
*kcontrol
,
274 struct snd_ctl_elem_value
*ucontrol
)
277 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
280 ucontrol
->value
.integer
.value
[i
] = line6pcm
->volume_playback
[i
];
285 /* control put callback */
286 static int snd_line6_control_playback_put(struct snd_kcontrol
*kcontrol
,
287 struct snd_ctl_elem_value
*ucontrol
)
290 struct snd_line6_pcm
*line6pcm
= snd_kcontrol_chip(kcontrol
);
293 if (line6pcm
->volume_playback
[i
] !=
294 ucontrol
->value
.integer
.value
[i
]) {
295 line6pcm
->volume_playback
[i
] =
296 ucontrol
->value
.integer
.value
[i
];
303 /* control definition */
304 static struct snd_kcontrol_new line6_control_playback
= {
305 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
306 .name
= "PCM Playback Volume",
308 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
309 .info
= snd_line6_control_playback_info
,
310 .get
= snd_line6_control_playback_get
,
311 .put
= snd_line6_control_playback_put
315 Cleanup the PCM device.
317 static void line6_cleanup_pcm(struct snd_pcm
*pcm
)
320 struct snd_line6_pcm
*line6pcm
= snd_pcm_chip(pcm
);
322 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
323 device_remove_file(line6pcm
->line6
->ifcdev
, &dev_attr_impulse_volume
);
324 device_remove_file(line6pcm
->line6
->ifcdev
, &dev_attr_impulse_period
);
327 for (i
= LINE6_ISO_BUFFERS
; i
--;) {
328 if (line6pcm
->urb_audio_out
[i
]) {
329 usb_kill_urb(line6pcm
->urb_audio_out
[i
]);
330 usb_free_urb(line6pcm
->urb_audio_out
[i
]);
332 if (line6pcm
->urb_audio_in
[i
]) {
333 usb_kill_urb(line6pcm
->urb_audio_in
[i
]);
334 usb_free_urb(line6pcm
->urb_audio_in
[i
]);
339 /* create a PCM device */
340 static int snd_line6_new_pcm(struct snd_line6_pcm
*line6pcm
)
345 err
= snd_pcm_new(line6pcm
->line6
->card
,
346 (char *)line6pcm
->line6
->properties
->name
,
351 pcm
->private_data
= line6pcm
;
352 pcm
->private_free
= line6_cleanup_pcm
;
354 strcpy(pcm
->name
, line6pcm
->line6
->properties
->name
);
357 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
358 &snd_line6_playback_ops
);
359 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_line6_capture_ops
);
361 /* pre-allocation of buffers */
362 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
363 snd_dma_continuous_data
364 (GFP_KERNEL
), 64 * 1024,
370 /* PCM device destructor */
371 static int snd_line6_pcm_free(struct snd_device
*device
)
377 Stop substream if still running.
379 static void pcm_disconnect_substream(struct snd_pcm_substream
*substream
)
381 if (substream
->runtime
&& snd_pcm_running(substream
))
382 snd_pcm_stop(substream
, SNDRV_PCM_STATE_DISCONNECTED
);
388 void line6_pcm_disconnect(struct snd_line6_pcm
*line6pcm
)
390 pcm_disconnect_substream(get_substream
391 (line6pcm
, SNDRV_PCM_STREAM_CAPTURE
));
392 pcm_disconnect_substream(get_substream
393 (line6pcm
, SNDRV_PCM_STREAM_PLAYBACK
));
394 line6_unlink_wait_clear_audio_out_urbs(line6pcm
);
395 line6_unlink_wait_clear_audio_in_urbs(line6pcm
);
399 Create and register the PCM device and mixer entries.
400 Create URBs for playback and capture.
402 int line6_init_pcm(struct usb_line6
*line6
,
403 struct line6_pcm_properties
*properties
)
405 static struct snd_device_ops pcm_ops
= {
406 .dev_free
= snd_line6_pcm_free
,
410 int ep_read
= 0, ep_write
= 0;
411 struct snd_line6_pcm
*line6pcm
;
413 if (!(line6
->properties
->capabilities
& LINE6_BIT_PCM
))
414 return 0; /* skip PCM initialization and report success */
416 /* initialize PCM subsystem based on product id: */
417 switch (line6
->product
) {
418 case LINE6_DEVID_BASSPODXT
:
419 case LINE6_DEVID_BASSPODXTLIVE
:
420 case LINE6_DEVID_BASSPODXTPRO
:
421 case LINE6_DEVID_PODXT
:
422 case LINE6_DEVID_PODXTLIVE
:
423 case LINE6_DEVID_PODXTPRO
:
424 case LINE6_DEVID_PODHD300
:
429 case LINE6_DEVID_PODHD500
:
430 case LINE6_DEVID_PODX3
:
431 case LINE6_DEVID_PODX3LIVE
:
436 case LINE6_DEVID_POCKETPOD
:
441 case LINE6_DEVID_GUITARPORT
:
442 case LINE6_DEVID_PODSTUDIO_GX
:
443 case LINE6_DEVID_PODSTUDIO_UX1
:
444 case LINE6_DEVID_PODSTUDIO_UX2
:
445 case LINE6_DEVID_TONEPORT_GX
:
446 case LINE6_DEVID_TONEPORT_UX1
:
447 case LINE6_DEVID_TONEPORT_UX2
:
452 /* this is for interface_number == 1:
453 case LINE6_DEVID_TONEPORT_UX2:
454 case LINE6_DEVID_PODSTUDIO_UX2:
464 line6pcm
= kzalloc(sizeof(struct snd_line6_pcm
), GFP_KERNEL
);
466 if (line6pcm
== NULL
)
469 line6pcm
->volume_playback
[0] = line6pcm
->volume_playback
[1] = 255;
470 line6pcm
->volume_monitor
= 255;
471 line6pcm
->line6
= line6
;
472 line6pcm
->ep_audio_read
= ep_read
;
473 line6pcm
->ep_audio_write
= ep_write
;
475 /* Read and write buffers are sized identically, so choose minimum */
476 line6pcm
->max_packet_size
= min(
477 usb_maxpacket(line6
->usbdev
,
478 usb_rcvisocpipe(line6
->usbdev
, ep_read
), 0),
479 usb_maxpacket(line6
->usbdev
,
480 usb_sndisocpipe(line6
->usbdev
, ep_write
), 1));
482 line6pcm
->properties
= properties
;
483 line6
->line6pcm
= line6pcm
;
486 err
= snd_device_new(line6
->card
, SNDRV_DEV_PCM
, line6
, &pcm_ops
);
490 snd_card_set_dev(line6
->card
, line6
->ifcdev
);
492 err
= snd_line6_new_pcm(line6pcm
);
496 spin_lock_init(&line6pcm
->lock_audio_out
);
497 spin_lock_init(&line6pcm
->lock_audio_in
);
498 spin_lock_init(&line6pcm
->lock_trigger
);
500 err
= line6_create_audio_out_urbs(line6pcm
);
504 err
= line6_create_audio_in_urbs(line6pcm
);
510 snd_ctl_add(line6
->card
,
511 snd_ctl_new1(&line6_control_playback
, line6pcm
));
515 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
516 /* impulse response test: */
517 err
= device_create_file(line6
->ifcdev
, &dev_attr_impulse_volume
);
521 err
= device_create_file(line6
->ifcdev
, &dev_attr_impulse_period
);
525 line6pcm
->impulse_period
= LINE6_IMPULSE_DEFAULT_PERIOD
;
531 /* prepare pcm callback */
532 int snd_line6_prepare(struct snd_pcm_substream
*substream
)
534 struct snd_line6_pcm
*line6pcm
= snd_pcm_substream_chip(substream
);
536 switch (substream
->stream
) {
537 case SNDRV_PCM_STREAM_PLAYBACK
:
538 if ((line6pcm
->flags
& LINE6_BITS_PLAYBACK_STREAM
) == 0)
539 line6_unlink_wait_clear_audio_out_urbs(line6pcm
);
543 case SNDRV_PCM_STREAM_CAPTURE
:
544 if ((line6pcm
->flags
& LINE6_BITS_CAPTURE_STREAM
) == 0)
545 line6_unlink_wait_clear_audio_in_urbs(line6pcm
);
553 if (!test_and_set_bit(LINE6_INDEX_PREPARED
, &line6pcm
->flags
)) {
554 line6pcm
->count_out
= 0;
555 line6pcm
->pos_out
= 0;
556 line6pcm
->pos_out_done
= 0;
557 line6pcm
->bytes_out
= 0;
558 line6pcm
->count_in
= 0;
559 line6pcm
->pos_in_done
= 0;
560 line6pcm
->bytes_in
= 0;