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 static bool test_flags(unsigned long flags0, unsigned long flags1,
92 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
95 int line6_pcm_start(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;
102 line6pcm->prev_fbuf = NULL;
104 if (test_flags(flags_old, flags_new, MASK_CAPTURE)) {
106 Waiting for completion of active URBs in the stop handler is
107 a bug, we therefore report an error if capturing is restarted
110 if (line6pcm->active_urb_in | line6pcm->unlink_urb_in)
113 if (!(flags_new & MASK_PCM_ALSA_CAPTURE)) {
114 err = line6_alloc_capture_buffer(line6pcm);
117 goto pcm_start_error;
120 line6pcm->count_in = 0;
121 line6pcm->prev_fsize = 0;
122 err = line6_submit_audio_in_all_urbs(line6pcm);
125 goto pcm_start_error;
128 if (test_flags(flags_old, flags_new, MASK_PLAYBACK)) {
130 See comment above regarding PCM restart.
132 if (line6pcm->active_urb_out | line6pcm->unlink_urb_out)
135 if (!(flags_new & MASK_PCM_ALSA_PLAYBACK)) {
136 err = line6_alloc_playback_buffer(line6pcm);
139 goto pcm_start_error;
142 line6pcm->count_out = 0;
143 err = line6_submit_audio_out_all_urbs(line6pcm);
146 goto pcm_start_error;
152 __sync_fetch_and_and(&line6pcm->flags, ~channels);
156 int line6_pcm_stop(struct snd_line6_pcm *line6pcm, int channels)
158 unsigned long flags_old =
159 __sync_fetch_and_and(&line6pcm->flags, ~channels);
160 unsigned long flags_new = flags_old & ~channels;
162 if (test_flags(flags_new, flags_old, MASK_CAPTURE)) {
163 line6_unlink_audio_in_urbs(line6pcm);
165 if (!(flags_old & MASK_PCM_ALSA_CAPTURE))
166 line6_free_capture_buffer(line6pcm);
169 if (test_flags(flags_new, flags_old, MASK_PLAYBACK)) {
170 line6_unlink_audio_out_urbs(line6pcm);
172 if (!(flags_old & MASK_PCM_ALSA_PLAYBACK))
173 line6_free_playback_buffer(line6pcm);
179 /* trigger callback */
180 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
182 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
183 struct snd_pcm_substream *s;
187 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
188 clear_bit(BIT_PREPARED, &line6pcm->flags);
190 snd_pcm_group_for_each_entry(s, substream) {
192 case SNDRV_PCM_STREAM_PLAYBACK:
193 err = snd_line6_playback_trigger(line6pcm, cmd);
196 spin_unlock_irqrestore(&line6pcm->lock_trigger,
203 case SNDRV_PCM_STREAM_CAPTURE:
204 err = snd_line6_capture_trigger(line6pcm, cmd);
207 spin_unlock_irqrestore(&line6pcm->lock_trigger,
215 dev_err(line6pcm->line6->ifcdev,
216 "Unknown stream direction %d\n", s->stream);
220 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
224 /* control info callback */
225 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
226 struct snd_ctl_elem_info *uinfo)
228 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
230 uinfo->value.integer.min = 0;
231 uinfo->value.integer.max = 256;
235 /* control get callback */
236 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
237 struct snd_ctl_elem_value *ucontrol)
240 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
243 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
248 /* control put callback */
249 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
250 struct snd_ctl_elem_value *ucontrol)
253 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
256 if (line6pcm->volume_playback[i] !=
257 ucontrol->value.integer.value[i]) {
258 line6pcm->volume_playback[i] =
259 ucontrol->value.integer.value[i];
266 /* control definition */
267 static struct snd_kcontrol_new line6_control_playback = {
268 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
269 .name = "PCM Playback Volume",
271 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
272 .info = snd_line6_control_playback_info,
273 .get = snd_line6_control_playback_get,
274 .put = snd_line6_control_playback_put
278 Cleanup the PCM device.
280 static void line6_cleanup_pcm(struct snd_pcm *pcm)
283 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
285 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
286 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
287 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
290 for (i = LINE6_ISO_BUFFERS; i--;) {
291 if (line6pcm->urb_audio_out[i]) {
292 usb_kill_urb(line6pcm->urb_audio_out[i]);
293 usb_free_urb(line6pcm->urb_audio_out[i]);
295 if (line6pcm->urb_audio_in[i]) {
296 usb_kill_urb(line6pcm->urb_audio_in[i]);
297 usb_free_urb(line6pcm->urb_audio_in[i]);
302 /* create a PCM device */
303 static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
308 err = snd_pcm_new(line6pcm->line6->card,
309 (char *)line6pcm->line6->properties->name,
314 pcm->private_data = line6pcm;
315 pcm->private_free = line6_cleanup_pcm;
317 strcpy(pcm->name, line6pcm->line6->properties->name);
320 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
321 &snd_line6_playback_ops);
322 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
324 /* pre-allocation of buffers */
325 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
326 snd_dma_continuous_data
327 (GFP_KERNEL), 64 * 1024,
333 /* PCM device destructor */
334 static int snd_line6_pcm_free(struct snd_device *device)
340 Stop substream if still running.
342 static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
344 if (substream->runtime && snd_pcm_running(substream))
345 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
351 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
353 pcm_disconnect_substream(get_substream
354 (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
355 pcm_disconnect_substream(get_substream
356 (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
357 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
358 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
362 Create and register the PCM device and mixer entries.
363 Create URBs for playback and capture.
365 int line6_init_pcm(struct usb_line6 *line6,
366 struct line6_pcm_properties *properties)
368 static struct snd_device_ops pcm_ops = {
369 .dev_free = snd_line6_pcm_free,
373 int ep_read = 0, ep_write = 0;
374 struct snd_line6_pcm *line6pcm;
376 if (!(line6->properties->capabilities & LINE6_BIT_PCM))
377 return 0; /* skip PCM initialization and report success */
379 /* initialize PCM subsystem based on product id: */
380 switch (line6->product) {
381 case LINE6_DEVID_BASSPODXT:
382 case LINE6_DEVID_BASSPODXTLIVE:
383 case LINE6_DEVID_BASSPODXTPRO:
384 case LINE6_DEVID_PODXT:
385 case LINE6_DEVID_PODXTLIVE:
386 case LINE6_DEVID_PODXTPRO:
387 case LINE6_DEVID_PODHD300:
392 case LINE6_DEVID_PODHD500:
393 case LINE6_DEVID_PODX3:
394 case LINE6_DEVID_PODX3LIVE:
399 case LINE6_DEVID_POCKETPOD:
404 case LINE6_DEVID_GUITARPORT:
405 case LINE6_DEVID_PODSTUDIO_GX:
406 case LINE6_DEVID_PODSTUDIO_UX1:
407 case LINE6_DEVID_PODSTUDIO_UX2:
408 case LINE6_DEVID_TONEPORT_GX:
409 case LINE6_DEVID_TONEPORT_UX1:
410 case LINE6_DEVID_TONEPORT_UX2:
415 /* this is for interface_number == 1:
416 case LINE6_DEVID_TONEPORT_UX2:
417 case LINE6_DEVID_PODSTUDIO_UX2:
427 line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
429 if (line6pcm == NULL)
432 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
433 line6pcm->volume_monitor = 255;
434 line6pcm->line6 = line6;
435 line6pcm->ep_audio_read = ep_read;
436 line6pcm->ep_audio_write = ep_write;
438 /* Read and write buffers are sized identically, so choose minimum */
439 line6pcm->max_packet_size = min(
440 usb_maxpacket(line6->usbdev,
441 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
442 usb_maxpacket(line6->usbdev,
443 usb_sndisocpipe(line6->usbdev, ep_write), 1));
445 line6pcm->properties = properties;
446 line6->line6pcm = line6pcm;
449 err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
453 snd_card_set_dev(line6->card, line6->ifcdev);
455 err = snd_line6_new_pcm(line6pcm);
459 spin_lock_init(&line6pcm->lock_audio_out);
460 spin_lock_init(&line6pcm->lock_audio_in);
461 spin_lock_init(&line6pcm->lock_trigger);
463 err = line6_create_audio_out_urbs(line6pcm);
467 err = line6_create_audio_in_urbs(line6pcm);
473 snd_ctl_add(line6->card,
474 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 switch (substream->stream) {
500 case SNDRV_PCM_STREAM_PLAYBACK:
501 if ((line6pcm->flags & MASK_PLAYBACK) == 0)
502 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
506 case SNDRV_PCM_STREAM_CAPTURE:
507 if ((line6pcm->flags & MASK_CAPTURE) == 0)
508 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
516 if (!test_and_set_bit(BIT_PREPARED, &line6pcm->flags)) {
517 line6pcm->count_out = 0;
518 line6pcm->pos_out = 0;
519 line6pcm->pos_out_done = 0;
520 line6pcm->bytes_out = 0;
521 line6pcm->count_in = 0;
522 line6pcm->pos_in_done = 0;
523 line6pcm->bytes_in = 0;