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_start(line6pcm, MASK_PCM_IMPULSE);
57 line6_pcm_stop(line6pcm, MASK_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 int line6_pcm_start(struct snd_line6_pcm *line6pcm, int channels)
91 unsigned long flags_old =
92 __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)) {
99 kmalloc(LINE6_ISO_PACKETS * line6pcm->max_packet_size,
102 if (!line6pcm->prev_fbuf) {
103 dev_err(line6pcm->line6->ifcdev,
104 "cannot malloc monitor buffer\n");
109 line6pcm->prev_fbuf = NULL;
112 if (((flags_old & MASK_CAPTURE) == 0) &&
113 ((flags_new & MASK_CAPTURE) != 0)) {
115 Waiting for completion of active URBs in the stop handler is
116 a bug, we therefore report an error if capturing is restarted
119 if (line6pcm->active_urb_in | line6pcm->unlink_urb_in)
122 line6pcm->buffer_in =
123 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
124 line6pcm->max_packet_size, GFP_KERNEL);
126 if (!line6pcm->buffer_in) {
127 dev_err(line6pcm->line6->ifcdev,
128 "cannot malloc capture buffer\n");
132 line6pcm->count_in = 0;
133 line6pcm->prev_fsize = 0;
134 err = line6_submit_audio_in_all_urbs(line6pcm);
137 __sync_fetch_and_and(&line6pcm->flags, ~channels);
142 if (((flags_old & MASK_PLAYBACK) == 0) &&
143 ((flags_new & MASK_PLAYBACK) != 0)) {
145 See comment above regarding PCM restart.
147 if (line6pcm->active_urb_out | line6pcm->unlink_urb_out)
150 line6pcm->buffer_out =
151 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
152 line6pcm->max_packet_size, GFP_KERNEL);
154 if (!line6pcm->buffer_out) {
155 dev_err(line6pcm->line6->ifcdev,
156 "cannot malloc playback buffer\n");
160 line6pcm->count_out = 0;
161 err = line6_submit_audio_out_all_urbs(line6pcm);
164 __sync_fetch_and_and(&line6pcm->flags, ~channels);
172 int line6_pcm_stop(struct snd_line6_pcm *line6pcm, int channels)
174 unsigned long flags_old =
175 __sync_fetch_and_and(&line6pcm->flags, ~channels);
176 unsigned long flags_new = flags_old & ~channels;
178 if (((flags_old & MASK_CAPTURE) != 0) &&
179 ((flags_new & MASK_CAPTURE) == 0)) {
180 line6_unlink_audio_in_urbs(line6pcm);
181 kfree(line6pcm->buffer_in);
182 line6pcm->buffer_in = NULL;
185 if (((flags_old & MASK_PLAYBACK) != 0) &&
186 ((flags_new & MASK_PLAYBACK) == 0)) {
187 line6_unlink_audio_out_urbs(line6pcm);
188 kfree(line6pcm->buffer_out);
189 line6pcm->buffer_out = NULL;
191 #if LINE6_BACKUP_MONITOR_SIGNAL
192 if (line6pcm->prev_fbuf != NULL)
193 kfree(line6pcm->prev_fbuf);
199 /* trigger callback */
200 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
202 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
203 struct snd_pcm_substream *s;
207 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
208 clear_bit(BIT_PREPARED, &line6pcm->flags);
210 snd_pcm_group_for_each_entry(s, substream) {
212 case SNDRV_PCM_STREAM_PLAYBACK:
213 err = snd_line6_playback_trigger(line6pcm, cmd);
216 spin_unlock_irqrestore(&line6pcm->lock_trigger,
223 case SNDRV_PCM_STREAM_CAPTURE:
224 err = snd_line6_capture_trigger(line6pcm, cmd);
227 spin_unlock_irqrestore(&line6pcm->lock_trigger,
235 dev_err(line6pcm->line6->ifcdev,
236 "Unknown stream direction %d\n", s->stream);
240 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
244 /* control info callback */
245 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
246 struct snd_ctl_elem_info *uinfo)
248 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
250 uinfo->value.integer.min = 0;
251 uinfo->value.integer.max = 256;
255 /* control get callback */
256 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
257 struct snd_ctl_elem_value *ucontrol)
260 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
263 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
268 /* control put callback */
269 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
270 struct snd_ctl_elem_value *ucontrol)
273 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
276 if (line6pcm->volume_playback[i] !=
277 ucontrol->value.integer.value[i]) {
278 line6pcm->volume_playback[i] =
279 ucontrol->value.integer.value[i];
286 /* control definition */
287 static struct snd_kcontrol_new line6_control_playback = {
288 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
289 .name = "PCM Playback Volume",
291 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
292 .info = snd_line6_control_playback_info,
293 .get = snd_line6_control_playback_get,
294 .put = snd_line6_control_playback_put
298 Cleanup the PCM device.
300 static void line6_cleanup_pcm(struct snd_pcm *pcm)
303 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
305 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
306 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
307 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
310 for (i = LINE6_ISO_BUFFERS; i--;) {
311 if (line6pcm->urb_audio_out[i]) {
312 usb_kill_urb(line6pcm->urb_audio_out[i]);
313 usb_free_urb(line6pcm->urb_audio_out[i]);
315 if (line6pcm->urb_audio_in[i]) {
316 usb_kill_urb(line6pcm->urb_audio_in[i]);
317 usb_free_urb(line6pcm->urb_audio_in[i]);
322 /* create a PCM device */
323 static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
328 err = snd_pcm_new(line6pcm->line6->card,
329 (char *)line6pcm->line6->properties->name,
334 pcm->private_data = line6pcm;
335 pcm->private_free = line6_cleanup_pcm;
337 strcpy(pcm->name, line6pcm->line6->properties->name);
340 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
341 &snd_line6_playback_ops);
342 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
344 /* pre-allocation of buffers */
345 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
346 snd_dma_continuous_data
347 (GFP_KERNEL), 64 * 1024,
353 /* PCM device destructor */
354 static int snd_line6_pcm_free(struct snd_device *device)
360 Stop substream if still running.
362 static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
364 if (substream->runtime && snd_pcm_running(substream))
365 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
371 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
373 pcm_disconnect_substream(get_substream
374 (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
375 pcm_disconnect_substream(get_substream
376 (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
377 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
378 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
382 Create and register the PCM device and mixer entries.
383 Create URBs for playback and capture.
385 int line6_init_pcm(struct usb_line6 *line6,
386 struct line6_pcm_properties *properties)
388 static struct snd_device_ops pcm_ops = {
389 .dev_free = snd_line6_pcm_free,
393 int ep_read = 0, ep_write = 0;
394 struct snd_line6_pcm *line6pcm;
396 if (!(line6->properties->capabilities & LINE6_BIT_PCM))
397 return 0; /* skip PCM initialization and report success */
399 /* initialize PCM subsystem based on product id: */
400 switch (line6->product) {
401 case LINE6_DEVID_BASSPODXT:
402 case LINE6_DEVID_BASSPODXTLIVE:
403 case LINE6_DEVID_BASSPODXTPRO:
404 case LINE6_DEVID_PODXT:
405 case LINE6_DEVID_PODXTLIVE:
406 case LINE6_DEVID_PODXTPRO:
411 case LINE6_DEVID_PODX3:
412 case LINE6_DEVID_PODX3LIVE:
417 case LINE6_DEVID_POCKETPOD:
422 case LINE6_DEVID_GUITARPORT:
423 case LINE6_DEVID_PODSTUDIO_GX:
424 case LINE6_DEVID_PODSTUDIO_UX1:
425 case LINE6_DEVID_PODSTUDIO_UX2:
426 case LINE6_DEVID_TONEPORT_GX:
427 case LINE6_DEVID_TONEPORT_UX1:
428 case LINE6_DEVID_TONEPORT_UX2:
433 /* this is for interface_number == 1:
434 case LINE6_DEVID_TONEPORT_UX2:
435 case LINE6_DEVID_PODSTUDIO_UX2:
445 line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
447 if (line6pcm == NULL)
450 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
451 line6pcm->volume_monitor = 255;
452 line6pcm->line6 = line6;
453 line6pcm->ep_audio_read = ep_read;
454 line6pcm->ep_audio_write = ep_write;
455 line6pcm->max_packet_size = usb_maxpacket(line6->usbdev,
456 usb_rcvintpipe(line6->usbdev,
458 line6pcm->properties = properties;
459 line6->line6pcm = line6pcm;
462 err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
466 snd_card_set_dev(line6->card, line6->ifcdev);
468 err = snd_line6_new_pcm(line6pcm);
472 spin_lock_init(&line6pcm->lock_audio_out);
473 spin_lock_init(&line6pcm->lock_audio_in);
474 spin_lock_init(&line6pcm->lock_trigger);
476 err = line6_create_audio_out_urbs(line6pcm);
480 err = line6_create_audio_in_urbs(line6pcm);
486 snd_ctl_add(line6->card,
487 snd_ctl_new1(&line6_control_playback, line6pcm));
491 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
492 /* impulse response test: */
493 err = device_create_file(line6->ifcdev, &dev_attr_impulse_volume);
497 err = device_create_file(line6->ifcdev, &dev_attr_impulse_period);
501 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
507 /* prepare pcm callback */
508 int snd_line6_prepare(struct snd_pcm_substream *substream)
510 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
512 if (!test_and_set_bit(BIT_PREPARED, &line6pcm->flags)) {
513 line6pcm->count_out = 0;
514 line6pcm->pos_out = 0;
515 line6pcm->pos_out_done = 0;
516 line6pcm->bytes_out = 0;
517 line6pcm->count_in = 0;
518 line6pcm->pos_in_done = 0;
519 line6pcm->bytes_in = 0;