Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Line 6 Linux USB driver
0004  *
0005  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
0006  *                         Emil Myhrman (emil.myhrman@gmail.com)
0007  */
0008 
0009 #include <linux/wait.h>
0010 #include <linux/usb.h>
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013 #include <linux/leds.h>
0014 #include <sound/core.h>
0015 #include <sound/control.h>
0016 
0017 #include "capture.h"
0018 #include "driver.h"
0019 #include "playback.h"
0020 
0021 enum line6_device_type {
0022     LINE6_GUITARPORT,
0023     LINE6_PODSTUDIO_GX,
0024     LINE6_PODSTUDIO_UX1,
0025     LINE6_PODSTUDIO_UX2,
0026     LINE6_TONEPORT_GX,
0027     LINE6_TONEPORT_UX1,
0028     LINE6_TONEPORT_UX2,
0029 };
0030 
0031 struct usb_line6_toneport;
0032 
0033 struct toneport_led {
0034     struct led_classdev dev;
0035     char name[64];
0036     struct usb_line6_toneport *toneport;
0037     bool registered;
0038 };
0039 
0040 struct usb_line6_toneport {
0041     /* Generic Line 6 USB data */
0042     struct usb_line6 line6;
0043 
0044     /* Source selector */
0045     int source;
0046 
0047     /* Serial number of device */
0048     u32 serial_number;
0049 
0050     /* Firmware version (x 100) */
0051     u8 firmware_version;
0052 
0053     /* Device type */
0054     enum line6_device_type type;
0055 
0056     /* LED instances */
0057     struct toneport_led leds[2];
0058 };
0059 
0060 #define line6_to_toneport(x) container_of(x, struct usb_line6_toneport, line6)
0061 
0062 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
0063 
0064 #define TONEPORT_PCM_DELAY 1
0065 
0066 static const struct snd_ratden toneport_ratden = {
0067     .num_min = 44100,
0068     .num_max = 44100,
0069     .num_step = 1,
0070     .den = 1
0071 };
0072 
0073 static struct line6_pcm_properties toneport_pcm_properties = {
0074     .playback_hw = {
0075                   .info = (SNDRV_PCM_INFO_MMAP |
0076                        SNDRV_PCM_INFO_INTERLEAVED |
0077                        SNDRV_PCM_INFO_BLOCK_TRANSFER |
0078                        SNDRV_PCM_INFO_MMAP_VALID |
0079                        SNDRV_PCM_INFO_PAUSE |
0080                        SNDRV_PCM_INFO_SYNC_START),
0081                   .formats = SNDRV_PCM_FMTBIT_S16_LE,
0082                   .rates = SNDRV_PCM_RATE_KNOT,
0083                   .rate_min = 44100,
0084                   .rate_max = 44100,
0085                   .channels_min = 2,
0086                   .channels_max = 2,
0087                   .buffer_bytes_max = 60000,
0088                   .period_bytes_min = 64,
0089                   .period_bytes_max = 8192,
0090                   .periods_min = 1,
0091                   .periods_max = 1024},
0092     .capture_hw = {
0093                  .info = (SNDRV_PCM_INFO_MMAP |
0094                       SNDRV_PCM_INFO_INTERLEAVED |
0095                       SNDRV_PCM_INFO_BLOCK_TRANSFER |
0096                       SNDRV_PCM_INFO_MMAP_VALID |
0097                       SNDRV_PCM_INFO_SYNC_START),
0098                  .formats = SNDRV_PCM_FMTBIT_S16_LE,
0099                  .rates = SNDRV_PCM_RATE_KNOT,
0100                  .rate_min = 44100,
0101                  .rate_max = 44100,
0102                  .channels_min = 2,
0103                  .channels_max = 2,
0104                  .buffer_bytes_max = 60000,
0105                  .period_bytes_min = 64,
0106                  .period_bytes_max = 8192,
0107                  .periods_min = 1,
0108                  .periods_max = 1024},
0109     .rates = {
0110                 .nrats = 1,
0111                 .rats = &toneport_ratden},
0112     .bytes_per_channel = 2
0113 };
0114 
0115 static const struct {
0116     const char *name;
0117     int code;
0118 } toneport_source_info[] = {
0119     {"Microphone", 0x0a01},
0120     {"Line", 0x0801},
0121     {"Instrument", 0x0b01},
0122     {"Inst & Mic", 0x0901}
0123 };
0124 
0125 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
0126 {
0127     int ret;
0128 
0129     ret = usb_control_msg_send(usbdev, 0, 0x67,
0130                    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0131                    cmd1, cmd2, NULL, 0, LINE6_TIMEOUT,
0132                    GFP_KERNEL);
0133 
0134     if (ret) {
0135         dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
0136         return ret;
0137     }
0138 
0139     return 0;
0140 }
0141 
0142 /* monitor info callback */
0143 static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
0144                      struct snd_ctl_elem_info *uinfo)
0145 {
0146     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0147     uinfo->count = 1;
0148     uinfo->value.integer.min = 0;
0149     uinfo->value.integer.max = 256;
0150     return 0;
0151 }
0152 
0153 /* monitor get callback */
0154 static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
0155                     struct snd_ctl_elem_value *ucontrol)
0156 {
0157     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0158 
0159     ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
0160     return 0;
0161 }
0162 
0163 /* monitor put callback */
0164 static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
0165                     struct snd_ctl_elem_value *ucontrol)
0166 {
0167     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0168     int err;
0169 
0170     if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
0171         return 0;
0172 
0173     line6pcm->volume_monitor = ucontrol->value.integer.value[0];
0174 
0175     if (line6pcm->volume_monitor > 0) {
0176         err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR, true);
0177         if (err < 0) {
0178             line6pcm->volume_monitor = 0;
0179             line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
0180             return err;
0181         }
0182     } else {
0183         line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
0184     }
0185 
0186     return 1;
0187 }
0188 
0189 /* source info callback */
0190 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
0191                     struct snd_ctl_elem_info *uinfo)
0192 {
0193     const int size = ARRAY_SIZE(toneport_source_info);
0194 
0195     uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
0196     uinfo->count = 1;
0197     uinfo->value.enumerated.items = size;
0198 
0199     if (uinfo->value.enumerated.item >= size)
0200         uinfo->value.enumerated.item = size - 1;
0201 
0202     strcpy(uinfo->value.enumerated.name,
0203            toneport_source_info[uinfo->value.enumerated.item].name);
0204 
0205     return 0;
0206 }
0207 
0208 /* source get callback */
0209 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
0210                    struct snd_ctl_elem_value *ucontrol)
0211 {
0212     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0213     struct usb_line6_toneport *toneport = line6_to_toneport(line6pcm->line6);
0214 
0215     ucontrol->value.enumerated.item[0] = toneport->source;
0216     return 0;
0217 }
0218 
0219 /* source put callback */
0220 static int snd_toneport_source_put(struct snd_kcontrol *kcontrol,
0221                    struct snd_ctl_elem_value *ucontrol)
0222 {
0223     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0224     struct usb_line6_toneport *toneport = line6_to_toneport(line6pcm->line6);
0225     unsigned int source;
0226 
0227     source = ucontrol->value.enumerated.item[0];
0228     if (source >= ARRAY_SIZE(toneport_source_info))
0229         return -EINVAL;
0230     if (source == toneport->source)
0231         return 0;
0232 
0233     toneport->source = source;
0234     toneport_send_cmd(toneport->line6.usbdev,
0235               toneport_source_info[source].code, 0x0000);
0236     return 1;
0237 }
0238 
0239 static void toneport_startup(struct usb_line6 *line6)
0240 {
0241     line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR, true);
0242 }
0243 
0244 /* control definition */
0245 static const struct snd_kcontrol_new toneport_control_monitor = {
0246     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0247     .name = "Monitor Playback Volume",
0248     .index = 0,
0249     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0250     .info = snd_toneport_monitor_info,
0251     .get = snd_toneport_monitor_get,
0252     .put = snd_toneport_monitor_put
0253 };
0254 
0255 /* source selector definition */
0256 static const struct snd_kcontrol_new toneport_control_source = {
0257     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0258     .name = "PCM Capture Source",
0259     .index = 0,
0260     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0261     .info = snd_toneport_source_info,
0262     .get = snd_toneport_source_get,
0263     .put = snd_toneport_source_put
0264 };
0265 
0266 /*
0267     For the led on Guitarport.
0268     Brightness goes from 0x00 to 0x26. Set a value above this to have led
0269     blink.
0270     (void cmd_0x02(byte red, byte green)
0271 */
0272 
0273 static bool toneport_has_led(struct usb_line6_toneport *toneport)
0274 {
0275     switch (toneport->type) {
0276     case LINE6_GUITARPORT:
0277     case LINE6_TONEPORT_GX:
0278     /* add your device here if you are missing support for the LEDs */
0279         return true;
0280 
0281     default:
0282         return false;
0283     }
0284 }
0285 
0286 static const char * const toneport_led_colors[2] = { "red", "green" };
0287 static const int toneport_led_init_vals[2] = { 0x00, 0x26 };
0288 
0289 static void toneport_update_led(struct usb_line6_toneport *toneport)
0290 {
0291     toneport_send_cmd(toneport->line6.usbdev,
0292               (toneport->leds[0].dev.brightness << 8) | 0x0002,
0293               toneport->leds[1].dev.brightness);
0294 }
0295 
0296 static void toneport_led_brightness_set(struct led_classdev *led_cdev,
0297                     enum led_brightness brightness)
0298 {
0299     struct toneport_led *leds =
0300         container_of(led_cdev, struct toneport_led, dev);
0301     toneport_update_led(leds->toneport);
0302 }
0303 
0304 static int toneport_init_leds(struct usb_line6_toneport *toneport)
0305 {
0306     struct device *dev = &toneport->line6.usbdev->dev;
0307     int i, err;
0308 
0309     for (i = 0; i < 2; i++) {
0310         struct toneport_led *led = &toneport->leds[i];
0311         struct led_classdev *leddev = &led->dev;
0312 
0313         led->toneport = toneport;
0314         snprintf(led->name, sizeof(led->name), "%s::%s",
0315              dev_name(dev), toneport_led_colors[i]);
0316         leddev->name = led->name;
0317         leddev->brightness = toneport_led_init_vals[i];
0318         leddev->max_brightness = 0x26;
0319         leddev->brightness_set = toneport_led_brightness_set;
0320         err = led_classdev_register(dev, leddev);
0321         if (err)
0322             return err;
0323         led->registered = true;
0324     }
0325 
0326     return 0;
0327 }
0328 
0329 static void toneport_remove_leds(struct usb_line6_toneport *toneport)
0330 {
0331     struct toneport_led *led;
0332     int i;
0333 
0334     for (i = 0; i < 2; i++) {
0335         led = &toneport->leds[i];
0336         if (!led->registered)
0337             break;
0338         led_classdev_unregister(&led->dev);
0339         led->registered = false;
0340     }
0341 }
0342 
0343 static bool toneport_has_source_select(struct usb_line6_toneport *toneport)
0344 {
0345     switch (toneport->type) {
0346     case LINE6_TONEPORT_UX1:
0347     case LINE6_TONEPORT_UX2:
0348     case LINE6_PODSTUDIO_UX1:
0349     case LINE6_PODSTUDIO_UX2:
0350         return true;
0351 
0352     default:
0353         return false;
0354     }
0355 }
0356 
0357 /*
0358     Setup Toneport device.
0359 */
0360 static int toneport_setup(struct usb_line6_toneport *toneport)
0361 {
0362     u32 *ticks;
0363     struct usb_line6 *line6 = &toneport->line6;
0364     struct usb_device *usbdev = line6->usbdev;
0365 
0366     ticks = kmalloc(sizeof(*ticks), GFP_KERNEL);
0367     if (!ticks)
0368         return -ENOMEM;
0369 
0370     /* sync time on device with host: */
0371     /* note: 32-bit timestamps overflow in year 2106 */
0372     *ticks = (u32)ktime_get_real_seconds();
0373     line6_write_data(line6, 0x80c6, ticks, 4);
0374     kfree(ticks);
0375 
0376     /* enable device: */
0377     toneport_send_cmd(usbdev, 0x0301, 0x0000);
0378 
0379     /* initialize source select: */
0380     if (toneport_has_source_select(toneport))
0381         toneport_send_cmd(usbdev,
0382                   toneport_source_info[toneport->source].code,
0383                   0x0000);
0384 
0385     if (toneport_has_led(toneport))
0386         toneport_update_led(toneport);
0387 
0388     schedule_delayed_work(&toneport->line6.startup_work,
0389                   msecs_to_jiffies(TONEPORT_PCM_DELAY * 1000));
0390     return 0;
0391 }
0392 
0393 /*
0394     Toneport device disconnected.
0395 */
0396 static void line6_toneport_disconnect(struct usb_line6 *line6)
0397 {
0398     struct usb_line6_toneport *toneport = line6_to_toneport(line6);
0399 
0400     if (toneport_has_led(toneport))
0401         toneport_remove_leds(toneport);
0402 }
0403 
0404 
0405 /*
0406      Try to init Toneport device.
0407 */
0408 static int toneport_init(struct usb_line6 *line6,
0409              const struct usb_device_id *id)
0410 {
0411     int err;
0412     struct usb_line6_toneport *toneport = line6_to_toneport(line6);
0413 
0414     toneport->type = id->driver_info;
0415 
0416     line6->disconnect = line6_toneport_disconnect;
0417     line6->startup = toneport_startup;
0418 
0419     /* initialize PCM subsystem: */
0420     err = line6_init_pcm(line6, &toneport_pcm_properties);
0421     if (err < 0)
0422         return err;
0423 
0424     /* register monitor control: */
0425     err = snd_ctl_add(line6->card,
0426               snd_ctl_new1(&toneport_control_monitor,
0427                        line6->line6pcm));
0428     if (err < 0)
0429         return err;
0430 
0431     /* register source select control: */
0432     if (toneport_has_source_select(toneport)) {
0433         err =
0434             snd_ctl_add(line6->card,
0435                 snd_ctl_new1(&toneport_control_source,
0436                          line6->line6pcm));
0437         if (err < 0)
0438             return err;
0439     }
0440 
0441     line6_read_serial_number(line6, &toneport->serial_number);
0442     line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
0443 
0444     if (toneport_has_led(toneport)) {
0445         err = toneport_init_leds(toneport);
0446         if (err < 0)
0447             return err;
0448     }
0449 
0450     err = toneport_setup(toneport);
0451     if (err)
0452         return err;
0453 
0454     /* register audio system: */
0455     return snd_card_register(line6->card);
0456 }
0457 
0458 #ifdef CONFIG_PM
0459 /*
0460     Resume Toneport device after reset.
0461 */
0462 static int toneport_reset_resume(struct usb_interface *interface)
0463 {
0464     int err;
0465 
0466     err = toneport_setup(usb_get_intfdata(interface));
0467     if (err)
0468         return err;
0469     return line6_resume(interface);
0470 }
0471 #endif
0472 
0473 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
0474 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
0475 
0476 /* table of devices that work with this driver */
0477 static const struct usb_device_id toneport_id_table[] = {
0478     { LINE6_DEVICE(0x4750),    .driver_info = LINE6_GUITARPORT },
0479     { LINE6_DEVICE(0x4153),    .driver_info = LINE6_PODSTUDIO_GX },
0480     { LINE6_DEVICE(0x4150),    .driver_info = LINE6_PODSTUDIO_UX1 },
0481     { LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 },
0482     { LINE6_DEVICE(0x4147),    .driver_info = LINE6_TONEPORT_GX },
0483     { LINE6_DEVICE(0x4141),    .driver_info = LINE6_TONEPORT_UX1 },
0484     { LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 },
0485     {}
0486 };
0487 
0488 MODULE_DEVICE_TABLE(usb, toneport_id_table);
0489 
0490 static const struct line6_properties toneport_properties_table[] = {
0491     [LINE6_GUITARPORT] = {
0492         .id = "GuitarPort",
0493         .name = "GuitarPort",
0494         .capabilities   = LINE6_CAP_PCM,
0495         .altsetting = 2,  /* 1..4 seem to be ok */
0496         /* no control channel */
0497         .ep_audio_r = 0x82,
0498         .ep_audio_w = 0x01,
0499     },
0500     [LINE6_PODSTUDIO_GX] = {
0501         .id = "PODStudioGX",
0502         .name = "POD Studio GX",
0503         .capabilities   = LINE6_CAP_PCM,
0504         .altsetting = 2,  /* 1..4 seem to be ok */
0505         /* no control channel */
0506         .ep_audio_r = 0x82,
0507         .ep_audio_w = 0x01,
0508     },
0509     [LINE6_PODSTUDIO_UX1] = {
0510         .id = "PODStudioUX1",
0511         .name = "POD Studio UX1",
0512         .capabilities   = LINE6_CAP_PCM,
0513         .altsetting = 2,  /* 1..4 seem to be ok */
0514         /* no control channel */
0515         .ep_audio_r = 0x82,
0516         .ep_audio_w = 0x01,
0517     },
0518     [LINE6_PODSTUDIO_UX2] = {
0519         .id = "PODStudioUX2",
0520         .name = "POD Studio UX2",
0521         .capabilities   = LINE6_CAP_PCM,
0522         .altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
0523         /* no control channel */
0524         .ep_audio_r = 0x82,
0525         .ep_audio_w = 0x01,
0526     },
0527     [LINE6_TONEPORT_GX] = {
0528         .id = "TonePortGX",
0529         .name = "TonePort GX",
0530         .capabilities   = LINE6_CAP_PCM,
0531         .altsetting = 2,  /* 1..4 seem to be ok */
0532         /* no control channel */
0533         .ep_audio_r = 0x82,
0534         .ep_audio_w = 0x01,
0535     },
0536     [LINE6_TONEPORT_UX1] = {
0537         .id = "TonePortUX1",
0538         .name = "TonePort UX1",
0539         .capabilities   = LINE6_CAP_PCM,
0540         .altsetting = 2,  /* 1..4 seem to be ok */
0541         /* no control channel */
0542         .ep_audio_r = 0x82,
0543         .ep_audio_w = 0x01,
0544     },
0545     [LINE6_TONEPORT_UX2] = {
0546         .id = "TonePortUX2",
0547         .name = "TonePort UX2",
0548         .capabilities   = LINE6_CAP_PCM,
0549         .altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
0550         /* no control channel */
0551         .ep_audio_r = 0x82,
0552         .ep_audio_w = 0x01,
0553     },
0554 };
0555 
0556 /*
0557     Probe USB device.
0558 */
0559 static int toneport_probe(struct usb_interface *interface,
0560               const struct usb_device_id *id)
0561 {
0562     return line6_probe(interface, id, "Line6-TonePort",
0563                &toneport_properties_table[id->driver_info],
0564                toneport_init, sizeof(struct usb_line6_toneport));
0565 }
0566 
0567 static struct usb_driver toneport_driver = {
0568     .name = KBUILD_MODNAME,
0569     .probe = toneport_probe,
0570     .disconnect = line6_disconnect,
0571 #ifdef CONFIG_PM
0572     .suspend = line6_suspend,
0573     .resume = line6_resume,
0574     .reset_resume = toneport_reset_resume,
0575 #endif
0576     .id_table = toneport_id_table,
0577 };
0578 
0579 module_usb_driver(toneport_driver);
0580 
0581 MODULE_DESCRIPTION("TonePort USB driver");
0582 MODULE_LICENSE("GPL");