2 * Line 6 Linux USB driver
4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
5 * Emil Myhrman (emil.myhrman@gmail.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2.
13 #include <linux/wait.h>
14 #include <linux/usb.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/leds.h>
18 #include <sound/core.h>
19 #include <sound/control.h>
25 enum line6_device_type {
35 struct usb_line6_toneport;
38 struct led_classdev dev;
40 struct usb_line6_toneport *toneport;
44 struct usb_line6_toneport {
45 /* Generic Line 6 USB data */
46 struct usb_line6 line6;
51 /* Serial number of device */
54 /* Firmware version (x 100) */
57 /* Timer for delayed PCM startup */
58 struct timer_list timer;
61 enum line6_device_type type;
64 struct toneport_led leds[2];
67 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
69 #define TONEPORT_PCM_DELAY 1
71 static struct snd_ratden toneport_ratden = {
78 static struct line6_pcm_properties toneport_pcm_properties = {
80 .info = (SNDRV_PCM_INFO_MMAP |
81 SNDRV_PCM_INFO_INTERLEAVED |
82 SNDRV_PCM_INFO_BLOCK_TRANSFER |
83 SNDRV_PCM_INFO_MMAP_VALID |
84 SNDRV_PCM_INFO_PAUSE |
85 SNDRV_PCM_INFO_SYNC_START),
86 .formats = SNDRV_PCM_FMTBIT_S16_LE,
87 .rates = SNDRV_PCM_RATE_KNOT,
92 .buffer_bytes_max = 60000,
93 .period_bytes_min = 64,
94 .period_bytes_max = 8192,
98 .info = (SNDRV_PCM_INFO_MMAP |
99 SNDRV_PCM_INFO_INTERLEAVED |
100 SNDRV_PCM_INFO_BLOCK_TRANSFER |
101 SNDRV_PCM_INFO_MMAP_VALID |
102 SNDRV_PCM_INFO_SYNC_START),
103 .formats = SNDRV_PCM_FMTBIT_S16_LE,
104 .rates = SNDRV_PCM_RATE_KNOT,
109 .buffer_bytes_max = 60000,
110 .period_bytes_min = 64,
111 .period_bytes_max = 8192,
113 .periods_max = 1024},
116 .rats = &toneport_ratden},
117 .bytes_per_channel = 2
120 static const struct {
123 } toneport_source_info[] = {
124 {"Microphone", 0x0a01},
126 {"Instrument", 0x0b01},
127 {"Inst & Mic", 0x0901}
130 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
134 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
135 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
136 cmd1, cmd2, NULL, 0, LINE6_TIMEOUT);
139 dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
146 /* monitor info callback */
147 static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
148 struct snd_ctl_elem_info *uinfo)
150 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
152 uinfo->value.integer.min = 0;
153 uinfo->value.integer.max = 256;
157 /* monitor get callback */
158 static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
159 struct snd_ctl_elem_value *ucontrol)
161 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
163 ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
167 /* monitor put callback */
168 static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
169 struct snd_ctl_elem_value *ucontrol)
171 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
174 if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
177 line6pcm->volume_monitor = ucontrol->value.integer.value[0];
179 if (line6pcm->volume_monitor > 0) {
180 err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR, true);
182 line6pcm->volume_monitor = 0;
183 line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
187 line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
193 /* source info callback */
194 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
195 struct snd_ctl_elem_info *uinfo)
197 const int size = ARRAY_SIZE(toneport_source_info);
199 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
201 uinfo->value.enumerated.items = size;
203 if (uinfo->value.enumerated.item >= size)
204 uinfo->value.enumerated.item = size - 1;
206 strcpy(uinfo->value.enumerated.name,
207 toneport_source_info[uinfo->value.enumerated.item].name);
212 /* source get callback */
213 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_value *ucontrol)
216 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
217 struct usb_line6_toneport *toneport =
218 (struct usb_line6_toneport *)line6pcm->line6;
219 ucontrol->value.enumerated.item[0] = toneport->source;
223 /* source put callback */
224 static int snd_toneport_source_put(struct snd_kcontrol *kcontrol,
225 struct snd_ctl_elem_value *ucontrol)
227 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
228 struct usb_line6_toneport *toneport =
229 (struct usb_line6_toneport *)line6pcm->line6;
232 source = ucontrol->value.enumerated.item[0];
233 if (source >= ARRAY_SIZE(toneport_source_info))
235 if (source == toneport->source)
238 toneport->source = source;
239 toneport_send_cmd(toneport->line6.usbdev,
240 toneport_source_info[source].code, 0x0000);
244 static void toneport_start_pcm(unsigned long arg)
246 struct usb_line6_toneport *toneport = (struct usb_line6_toneport *)arg;
247 struct usb_line6 *line6 = &toneport->line6;
249 line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR, true);
252 /* control definition */
253 static struct snd_kcontrol_new toneport_control_monitor = {
254 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
255 .name = "Monitor Playback Volume",
257 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
258 .info = snd_toneport_monitor_info,
259 .get = snd_toneport_monitor_get,
260 .put = snd_toneport_monitor_put
263 /* source selector definition */
264 static struct snd_kcontrol_new toneport_control_source = {
265 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
266 .name = "PCM Capture Source",
268 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
269 .info = snd_toneport_source_info,
270 .get = snd_toneport_source_get,
271 .put = snd_toneport_source_put
275 For the led on Guitarport.
276 Brightness goes from 0x00 to 0x26. Set a value above this to have led
278 (void cmd_0x02(byte red, byte green)
281 static bool toneport_has_led(struct usb_line6_toneport *toneport)
283 switch (toneport->type) {
284 case LINE6_GUITARPORT:
285 case LINE6_TONEPORT_GX:
286 /* add your device here if you are missing support for the LEDs */
294 static const char * const led_colors[2] = { "red", "green" };
295 static const int led_init_vals[2] = { 0x00, 0x26 };
297 static void toneport_update_led(struct usb_line6_toneport *toneport)
299 toneport_send_cmd(toneport->line6.usbdev,
300 (toneport->leds[0].dev.brightness << 8) | 0x0002,
301 toneport->leds[1].dev.brightness);
304 static void toneport_led_brightness_set(struct led_classdev *led_cdev,
305 enum led_brightness brightness)
307 struct toneport_led *leds =
308 container_of(led_cdev, struct toneport_led, dev);
309 toneport_update_led(leds->toneport);
312 static int toneport_init_leds(struct usb_line6_toneport *toneport)
314 struct device *dev = &toneport->line6.usbdev->dev;
317 for (i = 0; i < 2; i++) {
318 struct toneport_led *led = &toneport->leds[i];
319 struct led_classdev *leddev = &led->dev;
321 led->toneport = toneport;
322 snprintf(led->name, sizeof(led->name), "%s::%s",
323 dev_name(dev), led_colors[i]);
324 leddev->name = led->name;
325 leddev->brightness = led_init_vals[i];
326 leddev->max_brightness = 0x26;
327 leddev->brightness_set = toneport_led_brightness_set;
328 err = led_classdev_register(dev, leddev);
331 led->registered = true;
337 static void toneport_remove_leds(struct usb_line6_toneport *toneport)
339 struct toneport_led *led;
342 for (i = 0; i < 2; i++) {
343 led = &toneport->leds[i];
344 if (!led->registered)
346 led_classdev_unregister(&led->dev);
347 led->registered = false;
351 static bool toneport_has_source_select(struct usb_line6_toneport *toneport)
353 switch (toneport->type) {
354 case LINE6_TONEPORT_UX1:
355 case LINE6_TONEPORT_UX2:
356 case LINE6_PODSTUDIO_UX1:
357 case LINE6_PODSTUDIO_UX2:
366 Setup Toneport device.
368 static int toneport_setup(struct usb_line6_toneport *toneport)
371 struct usb_line6 *line6 = &toneport->line6;
372 struct usb_device *usbdev = line6->usbdev;
374 ticks = kmalloc(sizeof(*ticks), GFP_KERNEL);
378 /* sync time on device with host: */
379 *ticks = (int)get_seconds();
380 line6_write_data(line6, 0x80c6, ticks, 4);
384 toneport_send_cmd(usbdev, 0x0301, 0x0000);
386 /* initialize source select: */
387 if (toneport_has_source_select(toneport))
388 toneport_send_cmd(usbdev,
389 toneport_source_info[toneport->source].code,
392 if (toneport_has_led(toneport))
393 toneport_update_led(toneport);
395 mod_timer(&toneport->timer, jiffies + TONEPORT_PCM_DELAY * HZ);
400 Toneport device disconnected.
402 static void line6_toneport_disconnect(struct usb_line6 *line6)
404 struct usb_line6_toneport *toneport =
405 (struct usb_line6_toneport *)line6;
407 del_timer_sync(&toneport->timer);
409 if (toneport_has_led(toneport))
410 toneport_remove_leds(toneport);
415 Try to init Toneport device.
417 static int toneport_init(struct usb_line6 *line6,
418 const struct usb_device_id *id)
421 struct usb_line6_toneport *toneport = (struct usb_line6_toneport *) line6;
423 toneport->type = id->driver_info;
424 setup_timer(&toneport->timer, toneport_start_pcm,
425 (unsigned long)toneport);
427 line6->disconnect = line6_toneport_disconnect;
429 /* initialize PCM subsystem: */
430 err = line6_init_pcm(line6, &toneport_pcm_properties);
434 /* register monitor control: */
435 err = snd_ctl_add(line6->card,
436 snd_ctl_new1(&toneport_control_monitor,
441 /* register source select control: */
442 if (toneport_has_source_select(toneport)) {
444 snd_ctl_add(line6->card,
445 snd_ctl_new1(&toneport_control_source,
451 line6_read_serial_number(line6, &toneport->serial_number);
452 line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
454 if (toneport_has_led(toneport)) {
455 err = toneport_init_leds(toneport);
460 err = toneport_setup(toneport);
464 /* register audio system: */
465 return snd_card_register(line6->card);
470 Resume Toneport device after reset.
472 static int toneport_reset_resume(struct usb_interface *interface)
476 err = toneport_setup(usb_get_intfdata(interface));
479 return line6_resume(interface);
483 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
484 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
486 /* table of devices that work with this driver */
487 static const struct usb_device_id toneport_id_table[] = {
488 { LINE6_DEVICE(0x4750), .driver_info = LINE6_GUITARPORT },
489 { LINE6_DEVICE(0x4153), .driver_info = LINE6_PODSTUDIO_GX },
490 { LINE6_DEVICE(0x4150), .driver_info = LINE6_PODSTUDIO_UX1 },
491 { LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 },
492 { LINE6_DEVICE(0x4147), .driver_info = LINE6_TONEPORT_GX },
493 { LINE6_DEVICE(0x4141), .driver_info = LINE6_TONEPORT_UX1 },
494 { LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 },
498 MODULE_DEVICE_TABLE(usb, toneport_id_table);
500 static const struct line6_properties toneport_properties_table[] = {
501 [LINE6_GUITARPORT] = {
503 .name = "GuitarPort",
504 .capabilities = LINE6_CAP_PCM,
505 .altsetting = 2, /* 1..4 seem to be ok */
506 /* no control channel */
510 [LINE6_PODSTUDIO_GX] = {
512 .name = "POD Studio GX",
513 .capabilities = LINE6_CAP_PCM,
514 .altsetting = 2, /* 1..4 seem to be ok */
515 /* no control channel */
519 [LINE6_PODSTUDIO_UX1] = {
520 .id = "PODStudioUX1",
521 .name = "POD Studio UX1",
522 .capabilities = LINE6_CAP_PCM,
523 .altsetting = 2, /* 1..4 seem to be ok */
524 /* no control channel */
528 [LINE6_PODSTUDIO_UX2] = {
529 .id = "PODStudioUX2",
530 .name = "POD Studio UX2",
531 .capabilities = LINE6_CAP_PCM,
532 .altsetting = 2, /* defaults to 44.1kHz, 16-bit */
533 /* no control channel */
537 [LINE6_TONEPORT_GX] = {
539 .name = "TonePort GX",
540 .capabilities = LINE6_CAP_PCM,
541 .altsetting = 2, /* 1..4 seem to be ok */
542 /* no control channel */
546 [LINE6_TONEPORT_UX1] = {
548 .name = "TonePort UX1",
549 .capabilities = LINE6_CAP_PCM,
550 .altsetting = 2, /* 1..4 seem to be ok */
551 /* no control channel */
555 [LINE6_TONEPORT_UX2] = {
557 .name = "TonePort UX2",
558 .capabilities = LINE6_CAP_PCM,
559 .altsetting = 2, /* defaults to 44.1kHz, 16-bit */
560 /* no control channel */
569 static int toneport_probe(struct usb_interface *interface,
570 const struct usb_device_id *id)
572 return line6_probe(interface, id, "Line6-TonePort",
573 &toneport_properties_table[id->driver_info],
574 toneport_init, sizeof(struct usb_line6_toneport));
577 static struct usb_driver toneport_driver = {
578 .name = KBUILD_MODNAME,
579 .probe = toneport_probe,
580 .disconnect = line6_disconnect,
582 .suspend = line6_suspend,
583 .resume = line6_resume,
584 .reset_resume = toneport_reset_resume,
586 .id_table = toneport_id_table,
589 module_usb_driver(toneport_driver);
591 MODULE_DESCRIPTION("TonePort USB driver");
592 MODULE_LICENSE("GPL");