2 * Line6 Linux USB driver - 0.9.0
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>
25 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
27 static struct snd_line6_pcm* dev2pcm(struct device *dev)
29 struct usb_interface *interface = to_usb_interface(dev);
30 struct usb_line6 *line6 = usb_get_intfdata(interface);
31 struct snd_line6_pcm *line6pcm = line6->line6pcm;
36 "read" request on "impulse_volume" special file.
38 static ssize_t pcm_get_impulse_volume(struct device *dev,
39 struct device_attribute *attr,
42 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_volume);
46 "write" request on "impulse_volume" special file.
48 static ssize_t pcm_set_impulse_volume(struct device *dev,
49 struct device_attribute *attr,
50 const char *buf, size_t count)
52 struct snd_line6_pcm *line6pcm = dev2pcm(dev);
53 int value = simple_strtoul(buf, NULL, 10);
54 line6pcm->impulse_volume = value;
57 line6_pcm_start(line6pcm, MASK_PCM_IMPULSE);
59 line6_pcm_stop(line6pcm, MASK_PCM_IMPULSE);
65 "read" request on "impulse_period" special file.
67 static ssize_t pcm_get_impulse_period(struct device *dev,
68 struct device_attribute *attr,
71 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_period);
75 "write" request on "impulse_period" special file.
77 static ssize_t pcm_set_impulse_period(struct device *dev,
78 struct device_attribute *attr,
79 const char *buf, size_t count)
81 dev2pcm(dev)->impulse_period = simple_strtoul(buf, NULL, 10);
85 static DEVICE_ATTR(impulse_volume, S_IWUGO | S_IRUGO, pcm_get_impulse_volume, pcm_set_impulse_volume);
86 static DEVICE_ATTR(impulse_period, S_IWUGO | S_IRUGO, pcm_get_impulse_period, pcm_set_impulse_period);
90 int line6_pcm_start(struct snd_line6_pcm *line6pcm, int channels)
92 unsigned long flags_old = __sync_fetch_and_or(&line6pcm->flags, channels);
93 unsigned long flags_new = flags_old | channels;
96 #if LINE6_BACKUP_MONITOR_SIGNAL
97 if (!(line6pcm->line6->properties->capabilities & LINE6_BIT_HWMON)) {
98 line6pcm->prev_fbuf = kmalloc(LINE6_ISO_PACKETS * line6pcm->max_packet_size, GFP_KERNEL);
100 if (!line6pcm->prev_fbuf) {
101 dev_err(line6pcm->line6->ifcdev, "cannot malloc monitor buffer\n");
106 line6pcm->prev_fbuf = NULL;
109 if (((flags_old & MASK_CAPTURE) == 0) &&
110 ((flags_new & MASK_CAPTURE) != 0)) {
112 Waiting for completion of active URBs in the stop handler is
113 a bug, we therefore report an error if capturing is restarted
116 if(line6pcm->active_urb_in | line6pcm->unlink_urb_in)
119 line6pcm->buffer_in = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS * line6pcm->max_packet_size, GFP_KERNEL);
121 if (!line6pcm->buffer_in) {
122 dev_err(line6pcm->line6->ifcdev, "cannot malloc capture buffer\n");
126 line6pcm->count_in = 0;
127 line6pcm->prev_fsize = 0;
128 err = line6_submit_audio_in_all_urbs(line6pcm);
131 __sync_fetch_and_and(&line6pcm->flags, ~channels);
136 if (((flags_old & MASK_PLAYBACK) == 0) &&
137 ((flags_new & MASK_PLAYBACK) != 0)) {
139 See comment above regarding PCM restart.
141 if(line6pcm->active_urb_out | line6pcm->unlink_urb_out)
144 line6pcm->buffer_out = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS * line6pcm->max_packet_size, GFP_KERNEL);
146 if (!line6pcm->buffer_out) {
147 dev_err(line6pcm->line6->ifcdev, "cannot malloc playback buffer\n");
151 line6pcm->count_out = 0;
152 err = line6_submit_audio_out_all_urbs(line6pcm);
155 __sync_fetch_and_and(&line6pcm->flags, ~channels);
163 int line6_pcm_stop(struct snd_line6_pcm *line6pcm, int channels)
165 unsigned long flags_old = __sync_fetch_and_and(&line6pcm->flags, ~channels);
166 unsigned long flags_new = flags_old & ~channels;
168 if (((flags_old & MASK_CAPTURE) != 0) &&
169 ((flags_new & MASK_CAPTURE) == 0)) {
170 line6_unlink_audio_in_urbs(line6pcm);
171 kfree(line6pcm->buffer_in);
172 line6pcm->buffer_in = NULL;
175 if (((flags_old & MASK_PLAYBACK) != 0) &&
176 ((flags_new & MASK_PLAYBACK) == 0)) {
177 line6_unlink_audio_out_urbs(line6pcm);
178 kfree(line6pcm->buffer_out);
179 line6pcm->buffer_out = NULL;
182 #if LINE6_BACKUP_MONITOR_SIGNAL
183 if (line6pcm->prev_fbuf != NULL)
184 kfree(line6pcm->prev_fbuf);
190 /* trigger callback */
191 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
193 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
194 struct snd_pcm_substream *s;
198 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
199 clear_bit(BIT_PREPARED, &line6pcm->flags);
201 snd_pcm_group_for_each_entry(s, substream) {
203 case SNDRV_PCM_STREAM_PLAYBACK:
204 err = snd_line6_playback_trigger(line6pcm, cmd);
207 spin_unlock_irqrestore(&line6pcm->lock_trigger,
214 case SNDRV_PCM_STREAM_CAPTURE:
215 err = snd_line6_capture_trigger(line6pcm, cmd);
218 spin_unlock_irqrestore(&line6pcm->lock_trigger,
226 dev_err(line6pcm->line6->ifcdev, "Unknown stream direction %d\n",
231 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
235 /* control info callback */
236 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
237 struct snd_ctl_elem_info *uinfo)
239 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
241 uinfo->value.integer.min = 0;
242 uinfo->value.integer.max = 256;
246 /* control get callback */
247 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
248 struct snd_ctl_elem_value *ucontrol)
251 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
254 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
259 /* control put callback */
260 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
261 struct snd_ctl_elem_value *ucontrol)
264 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
267 if (line6pcm->volume_playback[i] != ucontrol->value.integer.value[i]) {
268 line6pcm->volume_playback[i] = ucontrol->value.integer.value[i];
275 /* control definition */
276 static struct snd_kcontrol_new line6_control_playback = {
277 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
278 .name = "PCM Playback Volume",
280 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
281 .info = snd_line6_control_playback_info,
282 .get = snd_line6_control_playback_get,
283 .put = snd_line6_control_playback_put
287 Cleanup the PCM device.
289 static void line6_cleanup_pcm(struct snd_pcm *pcm)
292 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
294 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
295 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
296 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
299 for (i = LINE6_ISO_BUFFERS; i--;) {
300 if (line6pcm->urb_audio_out[i]) {
301 usb_kill_urb(line6pcm->urb_audio_out[i]);
302 usb_free_urb(line6pcm->urb_audio_out[i]);
304 if (line6pcm->urb_audio_in[i]) {
305 usb_kill_urb(line6pcm->urb_audio_in[i]);
306 usb_free_urb(line6pcm->urb_audio_in[i]);
311 /* create a PCM device */
312 static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
317 err = snd_pcm_new(line6pcm->line6->card,
318 (char *)line6pcm->line6->properties->name,
323 pcm->private_data = line6pcm;
324 pcm->private_free = line6_cleanup_pcm;
326 strcpy(pcm->name, line6pcm->line6->properties->name);
329 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
330 &snd_line6_playback_ops);
331 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
332 &snd_line6_capture_ops);
334 /* pre-allocation of buffers */
335 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
336 snd_dma_continuous_data(GFP_KERNEL),
337 64 * 1024, 128 * 1024);
342 /* PCM device destructor */
343 static int snd_line6_pcm_free(struct snd_device *device)
349 Stop substream if still running.
351 static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
353 if(substream->runtime && snd_pcm_running(substream)) {
354 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
361 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
363 pcm_disconnect_substream(get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE));
364 pcm_disconnect_substream(get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
365 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
366 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
370 Create and register the PCM device and mixer entries.
371 Create URBs for playback and capture.
373 int line6_init_pcm(struct usb_line6 *line6,
374 struct line6_pcm_properties *properties)
376 static struct snd_device_ops pcm_ops = {
377 .dev_free = snd_line6_pcm_free,
381 int ep_read = 0, ep_write = 0;
382 struct snd_line6_pcm *line6pcm;
384 if (!(line6->properties->capabilities & LINE6_BIT_PCM))
385 return 0; /* skip PCM initialization and report success */
387 /* initialize PCM subsystem based on product id: */
388 switch (line6->product) {
389 case LINE6_DEVID_BASSPODXT:
390 case LINE6_DEVID_BASSPODXTLIVE:
391 case LINE6_DEVID_BASSPODXTPRO:
392 case LINE6_DEVID_PODXT:
393 case LINE6_DEVID_PODXTLIVE:
394 case LINE6_DEVID_PODXTPRO:
399 case LINE6_DEVID_PODX3:
400 case LINE6_DEVID_PODX3LIVE:
405 case LINE6_DEVID_POCKETPOD:
410 case LINE6_DEVID_GUITARPORT:
411 case LINE6_DEVID_PODSTUDIO_GX:
412 case LINE6_DEVID_PODSTUDIO_UX1:
413 case LINE6_DEVID_PODSTUDIO_UX2:
414 case LINE6_DEVID_TONEPORT_GX:
415 case LINE6_DEVID_TONEPORT_UX1:
416 case LINE6_DEVID_TONEPORT_UX2:
421 /* this is for interface_number == 1:
422 case LINE6_DEVID_TONEPORT_UX2:
423 case LINE6_DEVID_PODSTUDIO_UX2:
433 line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
435 if (line6pcm == NULL)
438 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
439 line6pcm->volume_monitor = 255;
440 line6pcm->line6 = line6;
441 line6pcm->ep_audio_read = ep_read;
442 line6pcm->ep_audio_write = ep_write;
443 line6pcm->max_packet_size = usb_maxpacket(line6->usbdev,
444 usb_rcvintpipe(line6->usbdev,
447 line6pcm->properties = properties;
448 line6->line6pcm = line6pcm;
451 err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
455 snd_card_set_dev(line6->card, line6->ifcdev);
457 err = snd_line6_new_pcm(line6pcm);
461 spin_lock_init(&line6pcm->lock_audio_out);
462 spin_lock_init(&line6pcm->lock_audio_in);
463 spin_lock_init(&line6pcm->lock_trigger);
465 err = line6_create_audio_out_urbs(line6pcm);
469 err = line6_create_audio_in_urbs(line6pcm);
474 err = snd_ctl_add(line6->card, snd_ctl_new1(&line6_control_playback, line6pcm));
478 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
479 /* impulse response test: */
480 err = device_create_file(line6->ifcdev, &dev_attr_impulse_volume);
484 err = device_create_file(line6->ifcdev, &dev_attr_impulse_period);
488 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
494 /* prepare pcm callback */
495 int snd_line6_prepare(struct snd_pcm_substream *substream)
497 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
499 if (!test_and_set_bit(BIT_PREPARED, &line6pcm->flags)) {
500 line6pcm->count_out = 0;
501 line6pcm->pos_out = 0;
502 line6pcm->pos_out_done = 0;
503 line6pcm->bytes_out = 0;
504 line6pcm->count_in = 0;
505 line6pcm->pos_in_done = 0;
506 line6pcm->bytes_in = 0;