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  */
0007 
0008 #include <linux/slab.h>
0009 #include <linux/wait.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/module.h>
0012 #include <linux/usb.h>
0013 
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 /*
0022     Locate name in binary program dump
0023 */
0024 #define POD_NAME_OFFSET 0
0025 #define POD_NAME_LENGTH 16
0026 
0027 /*
0028     Other constants
0029 */
0030 #define POD_CONTROL_SIZE 0x80
0031 #define POD_BUFSIZE_DUMPREQ 7
0032 #define POD_STARTUP_DELAY 1000
0033 
0034 /*
0035     Stages of POD startup procedure
0036 */
0037 enum {
0038     POD_STARTUP_VERSIONREQ,
0039     POD_STARTUP_SETUP,
0040     POD_STARTUP_DONE,
0041 };
0042 
0043 enum {
0044     LINE6_BASSPODXT,
0045     LINE6_BASSPODXTLIVE,
0046     LINE6_BASSPODXTPRO,
0047     LINE6_POCKETPOD,
0048     LINE6_PODXT,
0049     LINE6_PODXTLIVE_POD,
0050     LINE6_PODXTPRO,
0051 };
0052 
0053 struct usb_line6_pod {
0054     /* Generic Line 6 USB data */
0055     struct usb_line6 line6;
0056 
0057     /* Instrument monitor level */
0058     int monitor_level;
0059 
0060     /* Current progress in startup procedure */
0061     int startup_progress;
0062 
0063     /* Serial number of device */
0064     u32 serial_number;
0065 
0066     /* Firmware version (x 100) */
0067     int firmware_version;
0068 
0069     /* Device ID */
0070     int device_id;
0071 };
0072 
0073 #define line6_to_pod(x)     container_of(x, struct usb_line6_pod, line6)
0074 
0075 #define POD_SYSEX_CODE 3
0076 
0077 /* *INDENT-OFF* */
0078 
0079 enum {
0080     POD_SYSEX_SAVE      = 0x24,
0081     POD_SYSEX_SYSTEM    = 0x56,
0082     POD_SYSEX_SYSTEMREQ = 0x57,
0083     /* POD_SYSEX_UPDATE    = 0x6c, */  /* software update! */
0084     POD_SYSEX_STORE     = 0x71,
0085     POD_SYSEX_FINISH    = 0x72,
0086     POD_SYSEX_DUMPMEM   = 0x73,
0087     POD_SYSEX_DUMP      = 0x74,
0088     POD_SYSEX_DUMPREQ   = 0x75
0089 
0090     /* dumps entire internal memory of PODxt Pro */
0091     /* POD_SYSEX_DUMPMEM2  = 0x76 */
0092 };
0093 
0094 enum {
0095     POD_MONITOR_LEVEL  = 0x04,
0096     POD_SYSTEM_INVALID = 0x10000
0097 };
0098 
0099 /* *INDENT-ON* */
0100 
0101 enum {
0102     POD_DUMP_MEMORY = 2
0103 };
0104 
0105 enum {
0106     POD_BUSY_READ,
0107     POD_BUSY_WRITE,
0108     POD_CHANNEL_DIRTY,
0109     POD_SAVE_PRESSED,
0110     POD_BUSY_MIDISEND
0111 };
0112 
0113 static const struct snd_ratden pod_ratden = {
0114     .num_min = 78125,
0115     .num_max = 78125,
0116     .num_step = 1,
0117     .den = 2
0118 };
0119 
0120 static struct line6_pcm_properties pod_pcm_properties = {
0121     .playback_hw = {
0122                   .info = (SNDRV_PCM_INFO_MMAP |
0123                        SNDRV_PCM_INFO_INTERLEAVED |
0124                        SNDRV_PCM_INFO_BLOCK_TRANSFER |
0125                        SNDRV_PCM_INFO_MMAP_VALID |
0126                        SNDRV_PCM_INFO_PAUSE |
0127                        SNDRV_PCM_INFO_SYNC_START),
0128                   .formats = SNDRV_PCM_FMTBIT_S24_3LE,
0129                   .rates = SNDRV_PCM_RATE_KNOT,
0130                   .rate_min = 39062,
0131                   .rate_max = 39063,
0132                   .channels_min = 2,
0133                   .channels_max = 2,
0134                   .buffer_bytes_max = 60000,
0135                   .period_bytes_min = 64,
0136                   .period_bytes_max = 8192,
0137                   .periods_min = 1,
0138                   .periods_max = 1024},
0139     .capture_hw = {
0140                  .info = (SNDRV_PCM_INFO_MMAP |
0141                       SNDRV_PCM_INFO_INTERLEAVED |
0142                       SNDRV_PCM_INFO_BLOCK_TRANSFER |
0143                       SNDRV_PCM_INFO_MMAP_VALID |
0144                       SNDRV_PCM_INFO_SYNC_START),
0145                  .formats = SNDRV_PCM_FMTBIT_S24_3LE,
0146                  .rates = SNDRV_PCM_RATE_KNOT,
0147                  .rate_min = 39062,
0148                  .rate_max = 39063,
0149                  .channels_min = 2,
0150                  .channels_max = 2,
0151                  .buffer_bytes_max = 60000,
0152                  .period_bytes_min = 64,
0153                  .period_bytes_max = 8192,
0154                  .periods_min = 1,
0155                  .periods_max = 1024},
0156     .rates = {
0157                 .nrats = 1,
0158                 .rats = &pod_ratden},
0159     .bytes_per_channel = 3 /* SNDRV_PCM_FMTBIT_S24_3LE */
0160 };
0161 
0162 static const char pod_version_header[] = {
0163     0xf2, 0x7e, 0x7f, 0x06, 0x02
0164 };
0165 
0166 static char *pod_alloc_sysex_buffer(struct usb_line6_pod *pod, int code,
0167                     int size)
0168 {
0169     return line6_alloc_sysex_buffer(&pod->line6, POD_SYSEX_CODE, code,
0170                     size);
0171 }
0172 
0173 /*
0174     Process a completely received message.
0175 */
0176 static void line6_pod_process_message(struct usb_line6 *line6)
0177 {
0178     struct usb_line6_pod *pod = line6_to_pod(line6);
0179     const unsigned char *buf = pod->line6.buffer_message;
0180 
0181     if (memcmp(buf, pod_version_header, sizeof(pod_version_header)) == 0) {
0182         pod->firmware_version = buf[13] * 100 + buf[14] * 10 + buf[15];
0183         pod->device_id = ((int)buf[8] << 16) | ((int)buf[9] << 8) |
0184                  (int) buf[10];
0185         if (pod->startup_progress == POD_STARTUP_VERSIONREQ) {
0186             pod->startup_progress = POD_STARTUP_SETUP;
0187             schedule_delayed_work(&line6->startup_work, 0);
0188         }
0189         return;
0190     }
0191 
0192     /* Only look for sysex messages from this device */
0193     if (buf[0] != (LINE6_SYSEX_BEGIN | LINE6_CHANNEL_DEVICE) &&
0194         buf[0] != (LINE6_SYSEX_BEGIN | LINE6_CHANNEL_UNKNOWN)) {
0195         return;
0196     }
0197     if (memcmp(buf + 1, line6_midi_id, sizeof(line6_midi_id)) != 0)
0198         return;
0199 
0200     if (buf[5] == POD_SYSEX_SYSTEM && buf[6] == POD_MONITOR_LEVEL) {
0201         short value = ((int)buf[7] << 12) | ((int)buf[8] << 8) |
0202                   ((int)buf[9] << 4) | (int)buf[10];
0203         pod->monitor_level = value;
0204     }
0205 }
0206 
0207 /*
0208     Send system parameter (from integer).
0209 */
0210 static int pod_set_system_param_int(struct usb_line6_pod *pod, int value,
0211                     int code)
0212 {
0213     char *sysex;
0214     static const int size = 5;
0215 
0216     sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEM, size);
0217     if (!sysex)
0218         return -ENOMEM;
0219     sysex[SYSEX_DATA_OFS] = code;
0220     sysex[SYSEX_DATA_OFS + 1] = (value >> 12) & 0x0f;
0221     sysex[SYSEX_DATA_OFS + 2] = (value >> 8) & 0x0f;
0222     sysex[SYSEX_DATA_OFS + 3] = (value >> 4) & 0x0f;
0223     sysex[SYSEX_DATA_OFS + 4] = (value) & 0x0f;
0224     line6_send_sysex_message(&pod->line6, sysex, size);
0225     kfree(sysex);
0226     return 0;
0227 }
0228 
0229 /*
0230     "read" request on "serial_number" special file.
0231 */
0232 static ssize_t serial_number_show(struct device *dev,
0233                   struct device_attribute *attr, char *buf)
0234 {
0235     struct snd_card *card = dev_to_snd_card(dev);
0236     struct usb_line6_pod *pod = card->private_data;
0237 
0238     return sysfs_emit(buf, "%u\n", pod->serial_number);
0239 }
0240 
0241 /*
0242     "read" request on "firmware_version" special file.
0243 */
0244 static ssize_t firmware_version_show(struct device *dev,
0245                      struct device_attribute *attr, char *buf)
0246 {
0247     struct snd_card *card = dev_to_snd_card(dev);
0248     struct usb_line6_pod *pod = card->private_data;
0249 
0250     return sysfs_emit(buf, "%d.%02d\n", pod->firmware_version / 100,
0251               pod->firmware_version % 100);
0252 }
0253 
0254 /*
0255     "read" request on "device_id" special file.
0256 */
0257 static ssize_t device_id_show(struct device *dev,
0258                   struct device_attribute *attr, char *buf)
0259 {
0260     struct snd_card *card = dev_to_snd_card(dev);
0261     struct usb_line6_pod *pod = card->private_data;
0262 
0263     return sysfs_emit(buf, "%d\n", pod->device_id);
0264 }
0265 
0266 /*
0267     POD startup procedure.
0268     This is a sequence of functions with special requirements (e.g., must
0269     not run immediately after initialization, must not run in interrupt
0270     context). After the last one has finished, the device is ready to use.
0271 */
0272 
0273 static void pod_startup(struct usb_line6 *line6)
0274 {
0275     struct usb_line6_pod *pod = line6_to_pod(line6);
0276 
0277     switch (pod->startup_progress) {
0278     case POD_STARTUP_VERSIONREQ:
0279         /* request firmware version: */
0280         line6_version_request_async(line6);
0281         break;
0282     case POD_STARTUP_SETUP:
0283         /* serial number: */
0284         line6_read_serial_number(&pod->line6, &pod->serial_number);
0285 
0286         /* ALSA audio interface: */
0287         if (snd_card_register(line6->card))
0288             dev_err(line6->ifcdev, "Failed to register POD card.\n");
0289         pod->startup_progress = POD_STARTUP_DONE;
0290         break;
0291     default:
0292         break;
0293     }
0294 }
0295 
0296 /* POD special files: */
0297 static DEVICE_ATTR_RO(device_id);
0298 static DEVICE_ATTR_RO(firmware_version);
0299 static DEVICE_ATTR_RO(serial_number);
0300 
0301 static struct attribute *pod_dev_attrs[] = {
0302     &dev_attr_device_id.attr,
0303     &dev_attr_firmware_version.attr,
0304     &dev_attr_serial_number.attr,
0305     NULL
0306 };
0307 
0308 static const struct attribute_group pod_dev_attr_group = {
0309     .name = "pod",
0310     .attrs = pod_dev_attrs,
0311 };
0312 
0313 /* control info callback */
0314 static int snd_pod_control_monitor_info(struct snd_kcontrol *kcontrol,
0315                     struct snd_ctl_elem_info *uinfo)
0316 {
0317     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0318     uinfo->count = 1;
0319     uinfo->value.integer.min = 0;
0320     uinfo->value.integer.max = 65535;
0321     return 0;
0322 }
0323 
0324 /* control get callback */
0325 static int snd_pod_control_monitor_get(struct snd_kcontrol *kcontrol,
0326                        struct snd_ctl_elem_value *ucontrol)
0327 {
0328     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0329     struct usb_line6_pod *pod = line6_to_pod(line6pcm->line6);
0330 
0331     ucontrol->value.integer.value[0] = pod->monitor_level;
0332     return 0;
0333 }
0334 
0335 /* control put callback */
0336 static int snd_pod_control_monitor_put(struct snd_kcontrol *kcontrol,
0337                        struct snd_ctl_elem_value *ucontrol)
0338 {
0339     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0340     struct usb_line6_pod *pod = line6_to_pod(line6pcm->line6);
0341 
0342     if (ucontrol->value.integer.value[0] == pod->monitor_level)
0343         return 0;
0344 
0345     pod->monitor_level = ucontrol->value.integer.value[0];
0346     pod_set_system_param_int(pod, ucontrol->value.integer.value[0],
0347                  POD_MONITOR_LEVEL);
0348     return 1;
0349 }
0350 
0351 /* control definition */
0352 static const struct snd_kcontrol_new pod_control_monitor = {
0353     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0354     .name = "Monitor Playback Volume",
0355     .index = 0,
0356     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0357     .info = snd_pod_control_monitor_info,
0358     .get = snd_pod_control_monitor_get,
0359     .put = snd_pod_control_monitor_put
0360 };
0361 
0362 /*
0363      Try to init POD device.
0364 */
0365 static int pod_init(struct usb_line6 *line6,
0366             const struct usb_device_id *id)
0367 {
0368     int err;
0369     struct usb_line6_pod *pod = line6_to_pod(line6);
0370 
0371     line6->process_message = line6_pod_process_message;
0372     line6->startup = pod_startup;
0373 
0374     /* create sysfs entries: */
0375     err = snd_card_add_dev_attr(line6->card, &pod_dev_attr_group);
0376     if (err < 0)
0377         return err;
0378 
0379     /* initialize PCM subsystem: */
0380     err = line6_init_pcm(line6, &pod_pcm_properties);
0381     if (err < 0)
0382         return err;
0383 
0384     /* register monitor control: */
0385     err = snd_ctl_add(line6->card,
0386               snd_ctl_new1(&pod_control_monitor, line6->line6pcm));
0387     if (err < 0)
0388         return err;
0389 
0390     /*
0391        When the sound card is registered at this point, the PODxt Live
0392        displays "Invalid Code Error 07", so we do it later in the event
0393        handler.
0394      */
0395 
0396     if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL) {
0397         pod->monitor_level = POD_SYSTEM_INVALID;
0398 
0399         /* initiate startup procedure: */
0400         schedule_delayed_work(&line6->startup_work,
0401                       msecs_to_jiffies(POD_STARTUP_DELAY));
0402     }
0403 
0404     return 0;
0405 }
0406 
0407 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
0408 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
0409 
0410 /* table of devices that work with this driver */
0411 static const struct usb_device_id pod_id_table[] = {
0412     { LINE6_DEVICE(0x4250),    .driver_info = LINE6_BASSPODXT },
0413     { LINE6_DEVICE(0x4642),    .driver_info = LINE6_BASSPODXTLIVE },
0414     { LINE6_DEVICE(0x4252),    .driver_info = LINE6_BASSPODXTPRO },
0415     { LINE6_IF_NUM(0x5051, 1), .driver_info = LINE6_POCKETPOD },
0416     { LINE6_DEVICE(0x5044),    .driver_info = LINE6_PODXT },
0417     { LINE6_IF_NUM(0x4650, 0), .driver_info = LINE6_PODXTLIVE_POD },
0418     { LINE6_DEVICE(0x5050),    .driver_info = LINE6_PODXTPRO },
0419     {}
0420 };
0421 
0422 MODULE_DEVICE_TABLE(usb, pod_id_table);
0423 
0424 static const struct line6_properties pod_properties_table[] = {
0425     [LINE6_BASSPODXT] = {
0426         .id = "BassPODxt",
0427         .name = "BassPODxt",
0428         .capabilities   = LINE6_CAP_CONTROL
0429                 | LINE6_CAP_CONTROL_MIDI
0430                 | LINE6_CAP_PCM
0431                 | LINE6_CAP_HWMON,
0432         .altsetting = 5,
0433         .ep_ctrl_r = 0x84,
0434         .ep_ctrl_w = 0x03,
0435         .ep_audio_r = 0x82,
0436         .ep_audio_w = 0x01,
0437     },
0438     [LINE6_BASSPODXTLIVE] = {
0439         .id = "BassPODxtLive",
0440         .name = "BassPODxt Live",
0441         .capabilities   = LINE6_CAP_CONTROL
0442                 | LINE6_CAP_CONTROL_MIDI
0443                 | LINE6_CAP_PCM
0444                 | LINE6_CAP_HWMON,
0445         .altsetting = 1,
0446         .ep_ctrl_r = 0x84,
0447         .ep_ctrl_w = 0x03,
0448         .ep_audio_r = 0x82,
0449         .ep_audio_w = 0x01,
0450     },
0451     [LINE6_BASSPODXTPRO] = {
0452         .id = "BassPODxtPro",
0453         .name = "BassPODxt Pro",
0454         .capabilities   = LINE6_CAP_CONTROL
0455                 | LINE6_CAP_CONTROL_MIDI
0456                 | LINE6_CAP_PCM
0457                 | LINE6_CAP_HWMON,
0458         .altsetting = 5,
0459         .ep_ctrl_r = 0x84,
0460         .ep_ctrl_w = 0x03,
0461         .ep_audio_r = 0x82,
0462         .ep_audio_w = 0x01,
0463     },
0464     [LINE6_POCKETPOD] = {
0465         .id = "PocketPOD",
0466         .name = "Pocket POD",
0467         .capabilities   = LINE6_CAP_CONTROL
0468                 | LINE6_CAP_CONTROL_MIDI,
0469         .altsetting = 0,
0470         .ep_ctrl_r = 0x82,
0471         .ep_ctrl_w = 0x02,
0472         /* no audio channel */
0473     },
0474     [LINE6_PODXT] = {
0475         .id = "PODxt",
0476         .name = "PODxt",
0477         .capabilities   = LINE6_CAP_CONTROL
0478                 | LINE6_CAP_CONTROL_MIDI
0479                 | LINE6_CAP_PCM
0480                 | LINE6_CAP_HWMON,
0481         .altsetting = 5,
0482         .ep_ctrl_r = 0x84,
0483         .ep_ctrl_w = 0x03,
0484         .ep_audio_r = 0x82,
0485         .ep_audio_w = 0x01,
0486     },
0487     [LINE6_PODXTLIVE_POD] = {
0488         .id = "PODxtLive",
0489         .name = "PODxt Live",
0490         .capabilities   = LINE6_CAP_CONTROL
0491                 | LINE6_CAP_CONTROL_MIDI
0492                 | LINE6_CAP_PCM
0493                 | LINE6_CAP_HWMON,
0494         .altsetting = 1,
0495         .ep_ctrl_r = 0x84,
0496         .ep_ctrl_w = 0x03,
0497         .ep_audio_r = 0x82,
0498         .ep_audio_w = 0x01,
0499     },
0500     [LINE6_PODXTPRO] = {
0501         .id = "PODxtPro",
0502         .name = "PODxt Pro",
0503         .capabilities   = LINE6_CAP_CONTROL
0504                 | LINE6_CAP_CONTROL_MIDI
0505                 | LINE6_CAP_PCM
0506                 | LINE6_CAP_HWMON,
0507         .altsetting = 5,
0508         .ep_ctrl_r = 0x84,
0509         .ep_ctrl_w = 0x03,
0510         .ep_audio_r = 0x82,
0511         .ep_audio_w = 0x01,
0512     },
0513 };
0514 
0515 /*
0516     Probe USB device.
0517 */
0518 static int pod_probe(struct usb_interface *interface,
0519              const struct usb_device_id *id)
0520 {
0521     return line6_probe(interface, id, "Line6-POD",
0522                &pod_properties_table[id->driver_info],
0523                pod_init, sizeof(struct usb_line6_pod));
0524 }
0525 
0526 static struct usb_driver pod_driver = {
0527     .name = KBUILD_MODNAME,
0528     .probe = pod_probe,
0529     .disconnect = line6_disconnect,
0530 #ifdef CONFIG_PM
0531     .suspend = line6_suspend,
0532     .resume = line6_resume,
0533     .reset_resume = line6_resume,
0534 #endif
0535     .id_table = pod_id_table,
0536 };
0537 
0538 module_usb_driver(pod_driver);
0539 
0540 MODULE_DESCRIPTION("Line 6 POD USB driver");
0541 MODULE_LICENSE("GPL");