1 // SPDX-License-Identifier: GPL-2.0-only
3 * Line 6 Linux USB driver
5 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
6 * Emil Myhrman (emil.myhrman@gmail.com)
9 #include <linux/wait.h>
10 #include <linux/usb.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/leds.h>
14 #include <sound/core.h>
15 #include <sound/control.h>
21 enum line6_device_type {
31 struct usb_line6_toneport;
34 struct led_classdev dev;
36 struct usb_line6_toneport *toneport;
40 struct usb_line6_toneport {
41 /* Generic Line 6 USB data */
42 struct usb_line6 line6;
47 /* Serial number of device */
50 /* Firmware version (x 100) */
54 enum line6_device_type type;
57 struct toneport_led leds[2];
60 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
62 #define TONEPORT_PCM_DELAY 1
64 static struct snd_ratden toneport_ratden = {
71 static struct line6_pcm_properties toneport_pcm_properties = {
73 .info = (SNDRV_PCM_INFO_MMAP |
74 SNDRV_PCM_INFO_INTERLEAVED |
75 SNDRV_PCM_INFO_BLOCK_TRANSFER |
76 SNDRV_PCM_INFO_MMAP_VALID |
77 SNDRV_PCM_INFO_PAUSE |
78 SNDRV_PCM_INFO_SYNC_START),
79 .formats = SNDRV_PCM_FMTBIT_S16_LE,
80 .rates = SNDRV_PCM_RATE_KNOT,
85 .buffer_bytes_max = 60000,
86 .period_bytes_min = 64,
87 .period_bytes_max = 8192,
91 .info = (SNDRV_PCM_INFO_MMAP |
92 SNDRV_PCM_INFO_INTERLEAVED |
93 SNDRV_PCM_INFO_BLOCK_TRANSFER |
94 SNDRV_PCM_INFO_MMAP_VALID |
95 SNDRV_PCM_INFO_SYNC_START),
96 .formats = SNDRV_PCM_FMTBIT_S16_LE,
97 .rates = SNDRV_PCM_RATE_KNOT,
102 .buffer_bytes_max = 60000,
103 .period_bytes_min = 64,
104 .period_bytes_max = 8192,
106 .periods_max = 1024},
109 .rats = &toneport_ratden},
110 .bytes_per_channel = 2
113 static const struct {
116 } toneport_source_info[] = {
117 {"Microphone", 0x0a01},
119 {"Instrument", 0x0b01},
120 {"Inst & Mic", 0x0901}
123 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
127 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
128 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
129 cmd1, cmd2, NULL, 0, LINE6_TIMEOUT * HZ);
132 dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
139 /* monitor info callback */
140 static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
141 struct snd_ctl_elem_info *uinfo)
143 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
145 uinfo->value.integer.min = 0;
146 uinfo->value.integer.max = 256;
150 /* monitor get callback */
151 static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
152 struct snd_ctl_elem_value *ucontrol)
154 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
156 ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
160 /* monitor put callback */
161 static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
162 struct snd_ctl_elem_value *ucontrol)
164 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
167 if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
170 line6pcm->volume_monitor = ucontrol->value.integer.value[0];
172 if (line6pcm->volume_monitor > 0) {
173 err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR, true);
175 line6pcm->volume_monitor = 0;
176 line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
180 line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
186 /* source info callback */
187 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
188 struct snd_ctl_elem_info *uinfo)
190 const int size = ARRAY_SIZE(toneport_source_info);
192 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
194 uinfo->value.enumerated.items = size;
196 if (uinfo->value.enumerated.item >= size)
197 uinfo->value.enumerated.item = size - 1;
199 strcpy(uinfo->value.enumerated.name,
200 toneport_source_info[uinfo->value.enumerated.item].name);
205 /* source get callback */
206 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
207 struct snd_ctl_elem_value *ucontrol)
209 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
210 struct usb_line6_toneport *toneport =
211 (struct usb_line6_toneport *)line6pcm->line6;
212 ucontrol->value.enumerated.item[0] = toneport->source;
216 /* source put callback */
217 static int snd_toneport_source_put(struct snd_kcontrol *kcontrol,
218 struct snd_ctl_elem_value *ucontrol)
220 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
221 struct usb_line6_toneport *toneport =
222 (struct usb_line6_toneport *)line6pcm->line6;
225 source = ucontrol->value.enumerated.item[0];
226 if (source >= ARRAY_SIZE(toneport_source_info))
228 if (source == toneport->source)
231 toneport->source = source;
232 toneport_send_cmd(toneport->line6.usbdev,
233 toneport_source_info[source].code, 0x0000);
237 static void toneport_startup(struct usb_line6 *line6)
239 line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR, true);
242 /* control definition */
243 static const struct snd_kcontrol_new toneport_control_monitor = {
244 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
245 .name = "Monitor Playback Volume",
247 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
248 .info = snd_toneport_monitor_info,
249 .get = snd_toneport_monitor_get,
250 .put = snd_toneport_monitor_put
253 /* source selector definition */
254 static const struct snd_kcontrol_new toneport_control_source = {
255 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
256 .name = "PCM Capture Source",
258 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
259 .info = snd_toneport_source_info,
260 .get = snd_toneport_source_get,
261 .put = snd_toneport_source_put
265 For the led on Guitarport.
266 Brightness goes from 0x00 to 0x26. Set a value above this to have led
268 (void cmd_0x02(byte red, byte green)
271 static bool toneport_has_led(struct usb_line6_toneport *toneport)
273 switch (toneport->type) {
274 case LINE6_GUITARPORT:
275 case LINE6_TONEPORT_GX:
276 /* add your device here if you are missing support for the LEDs */
284 static const char * const toneport_led_colors[2] = { "red", "green" };
285 static const int toneport_led_init_vals[2] = { 0x00, 0x26 };
287 static void toneport_update_led(struct usb_line6_toneport *toneport)
289 toneport_send_cmd(toneport->line6.usbdev,
290 (toneport->leds[0].dev.brightness << 8) | 0x0002,
291 toneport->leds[1].dev.brightness);
294 static void toneport_led_brightness_set(struct led_classdev *led_cdev,
295 enum led_brightness brightness)
297 struct toneport_led *leds =
298 container_of(led_cdev, struct toneport_led, dev);
299 toneport_update_led(leds->toneport);
302 static int toneport_init_leds(struct usb_line6_toneport *toneport)
304 struct device *dev = &toneport->line6.usbdev->dev;
307 for (i = 0; i < 2; i++) {
308 struct toneport_led *led = &toneport->leds[i];
309 struct led_classdev *leddev = &led->dev;
311 led->toneport = toneport;
312 snprintf(led->name, sizeof(led->name), "%s::%s",
313 dev_name(dev), toneport_led_colors[i]);
314 leddev->name = led->name;
315 leddev->brightness = toneport_led_init_vals[i];
316 leddev->max_brightness = 0x26;
317 leddev->brightness_set = toneport_led_brightness_set;
318 err = led_classdev_register(dev, leddev);
321 led->registered = true;
327 static void toneport_remove_leds(struct usb_line6_toneport *toneport)
329 struct toneport_led *led;
332 for (i = 0; i < 2; i++) {
333 led = &toneport->leds[i];
334 if (!led->registered)
336 led_classdev_unregister(&led->dev);
337 led->registered = false;
341 static bool toneport_has_source_select(struct usb_line6_toneport *toneport)
343 switch (toneport->type) {
344 case LINE6_TONEPORT_UX1:
345 case LINE6_TONEPORT_UX2:
346 case LINE6_PODSTUDIO_UX1:
347 case LINE6_PODSTUDIO_UX2:
356 Setup Toneport device.
358 static int toneport_setup(struct usb_line6_toneport *toneport)
361 struct usb_line6 *line6 = &toneport->line6;
362 struct usb_device *usbdev = line6->usbdev;
364 ticks = kmalloc(sizeof(*ticks), GFP_KERNEL);
368 /* sync time on device with host: */
369 /* note: 32-bit timestamps overflow in year 2106 */
370 *ticks = (u32)ktime_get_real_seconds();
371 line6_write_data(line6, 0x80c6, ticks, 4);
375 toneport_send_cmd(usbdev, 0x0301, 0x0000);
377 /* initialize source select: */
378 if (toneport_has_source_select(toneport))
379 toneport_send_cmd(usbdev,
380 toneport_source_info[toneport->source].code,
383 if (toneport_has_led(toneport))
384 toneport_update_led(toneport);
386 schedule_delayed_work(&toneport->line6.startup_work,
387 msecs_to_jiffies(TONEPORT_PCM_DELAY * 1000));
392 Toneport device disconnected.
394 static void line6_toneport_disconnect(struct usb_line6 *line6)
396 struct usb_line6_toneport *toneport =
397 (struct usb_line6_toneport *)line6;
399 if (toneport_has_led(toneport))
400 toneport_remove_leds(toneport);
405 Try to init Toneport device.
407 static int toneport_init(struct usb_line6 *line6,
408 const struct usb_device_id *id)
411 struct usb_line6_toneport *toneport = (struct usb_line6_toneport *) line6;
413 toneport->type = id->driver_info;
415 line6->disconnect = line6_toneport_disconnect;
416 line6->startup = toneport_startup;
418 /* initialize PCM subsystem: */
419 err = line6_init_pcm(line6, &toneport_pcm_properties);
423 /* register monitor control: */
424 err = snd_ctl_add(line6->card,
425 snd_ctl_new1(&toneport_control_monitor,
430 /* register source select control: */
431 if (toneport_has_source_select(toneport)) {
433 snd_ctl_add(line6->card,
434 snd_ctl_new1(&toneport_control_source,
440 line6_read_serial_number(line6, &toneport->serial_number);
441 line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
443 if (toneport_has_led(toneport)) {
444 err = toneport_init_leds(toneport);
449 err = toneport_setup(toneport);
453 /* register audio system: */
454 return snd_card_register(line6->card);
459 Resume Toneport device after reset.
461 static int toneport_reset_resume(struct usb_interface *interface)
465 err = toneport_setup(usb_get_intfdata(interface));
468 return line6_resume(interface);
472 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
473 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
475 /* table of devices that work with this driver */
476 static const struct usb_device_id toneport_id_table[] = {
477 { LINE6_DEVICE(0x4750), .driver_info = LINE6_GUITARPORT },
478 { LINE6_DEVICE(0x4153), .driver_info = LINE6_PODSTUDIO_GX },
479 { LINE6_DEVICE(0x4150), .driver_info = LINE6_PODSTUDIO_UX1 },
480 { LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 },
481 { LINE6_DEVICE(0x4147), .driver_info = LINE6_TONEPORT_GX },
482 { LINE6_DEVICE(0x4141), .driver_info = LINE6_TONEPORT_UX1 },
483 { LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 },
487 MODULE_DEVICE_TABLE(usb, toneport_id_table);
489 static const struct line6_properties toneport_properties_table[] = {
490 [LINE6_GUITARPORT] = {
492 .name = "GuitarPort",
493 .capabilities = LINE6_CAP_PCM,
494 .altsetting = 2, /* 1..4 seem to be ok */
495 /* no control channel */
499 [LINE6_PODSTUDIO_GX] = {
501 .name = "POD Studio GX",
502 .capabilities = LINE6_CAP_PCM,
503 .altsetting = 2, /* 1..4 seem to be ok */
504 /* no control channel */
508 [LINE6_PODSTUDIO_UX1] = {
509 .id = "PODStudioUX1",
510 .name = "POD Studio UX1",
511 .capabilities = LINE6_CAP_PCM,
512 .altsetting = 2, /* 1..4 seem to be ok */
513 /* no control channel */
517 [LINE6_PODSTUDIO_UX2] = {
518 .id = "PODStudioUX2",
519 .name = "POD Studio UX2",
520 .capabilities = LINE6_CAP_PCM,
521 .altsetting = 2, /* defaults to 44.1kHz, 16-bit */
522 /* no control channel */
526 [LINE6_TONEPORT_GX] = {
528 .name = "TonePort GX",
529 .capabilities = LINE6_CAP_PCM,
530 .altsetting = 2, /* 1..4 seem to be ok */
531 /* no control channel */
535 [LINE6_TONEPORT_UX1] = {
537 .name = "TonePort UX1",
538 .capabilities = LINE6_CAP_PCM,
539 .altsetting = 2, /* 1..4 seem to be ok */
540 /* no control channel */
544 [LINE6_TONEPORT_UX2] = {
546 .name = "TonePort UX2",
547 .capabilities = LINE6_CAP_PCM,
548 .altsetting = 2, /* defaults to 44.1kHz, 16-bit */
549 /* no control channel */
558 static int toneport_probe(struct usb_interface *interface,
559 const struct usb_device_id *id)
561 return line6_probe(interface, id, "Line6-TonePort",
562 &toneport_properties_table[id->driver_info],
563 toneport_init, sizeof(struct usb_line6_toneport));
566 static struct usb_driver toneport_driver = {
567 .name = KBUILD_MODNAME,
568 .probe = toneport_probe,
569 .disconnect = line6_disconnect,
571 .suspend = line6_suspend,
572 .resume = line6_resume,
573 .reset_resume = toneport_reset_resume,
575 .id_table = toneport_id_table,
578 module_usb_driver(toneport_driver);
580 MODULE_DESCRIPTION("TonePort USB driver");
581 MODULE_LICENSE("GPL");