Merge tag 'gfs2-merge-window' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2...
[firefly-linux-kernel-4.4.55.git] / sound / usb / line6 / toneport.c
index 9a4f5403569e3aa95fa7b16aaf10f8a35429134c..6d4c50c9b17d87175cac330703ebd2ff0aba4cc9 100644 (file)
 #include <linux/usb.h>
 #include <linux/slab.h>
 #include <linux/module.h>
+#include <linux/leds.h>
 #include <sound/core.h>
 #include <sound/control.h>
 
 #include "capture.h"
 #include "driver.h"
 #include "playback.h"
-#include "usbdefs.h"
 
 enum line6_device_type {
        LINE6_GUITARPORT,
@@ -32,36 +32,36 @@ enum line6_device_type {
        LINE6_TONEPORT_UX2,
 };
 
+struct usb_line6_toneport;
+
+struct toneport_led {
+       struct led_classdev dev;
+       char name[64];
+       struct usb_line6_toneport *toneport;
+       bool registered;
+};
+
 struct usb_line6_toneport {
-       /**
-               Generic Line 6 USB data.
-       */
+       /* Generic Line 6 USB data */
        struct usb_line6 line6;
 
-       /**
-               Source selector.
-       */
+       /* Source selector */
        int source;
 
-       /**
-               Serial number of device.
-       */
-       int serial_number;
+       /* Serial number of device */
+       u32 serial_number;
 
-       /**
-               Firmware version (x 100).
-       */
-       int firmware_version;
+       /* Firmware version (x 100) */
+       u8 firmware_version;
 
-       /**
-                Timer for delayed PCM startup.
-       */
+       /* Timer for delayed PCM startup */
        struct timer_list timer;
 
-       /**
-                Device type.
-       */
+       /* Device type */
        enum line6_device_type type;
+
+       /* LED instances */
+       struct toneport_led leds[2];
 };
 
 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
@@ -76,7 +76,7 @@ static struct snd_ratden toneport_ratden = {
 };
 
 static struct line6_pcm_properties toneport_pcm_properties = {
-       .snd_line6_playback_hw = {
+       .playback_hw = {
                                  .info = (SNDRV_PCM_INFO_MMAP |
                                           SNDRV_PCM_INFO_INTERLEAVED |
                                           SNDRV_PCM_INFO_BLOCK_TRANSFER |
@@ -94,7 +94,7 @@ static struct line6_pcm_properties toneport_pcm_properties = {
                                  .period_bytes_max = 8192,
                                  .periods_min = 1,
                                  .periods_max = 1024},
-       .snd_line6_capture_hw = {
+       .capture_hw = {
                                 .info = (SNDRV_PCM_INFO_MMAP |
                                          SNDRV_PCM_INFO_INTERLEAVED |
                                          SNDRV_PCM_INFO_BLOCK_TRANSFER |
@@ -111,21 +111,12 @@ static struct line6_pcm_properties toneport_pcm_properties = {
                                 .period_bytes_max = 8192,
                                 .periods_min = 1,
                                 .periods_max = 1024},
-       .snd_line6_rates = {
+       .rates = {
                            .nrats = 1,
                            .rats = &toneport_ratden},
        .bytes_per_frame = 4
 };
 
-/*
-       For the led on Guitarport.
-       Brightness goes from 0x00 to 0x26. Set a value above this to have led
-       blink.
-       (void cmd_0x02(byte red, byte green)
-*/
-static int led_red = 0x00;
-static int led_green = 0x26;
-
 static const struct {
        const char *name;
        int code;
@@ -136,62 +127,6 @@ static const struct {
        {"Inst & Mic", 0x0901}
 };
 
-static bool toneport_has_led(enum line6_device_type type)
-{
-       return
-           (type == LINE6_GUITARPORT) ||
-           (type == LINE6_TONEPORT_GX);
-       /* add your device here if you are missing support for the LEDs */
-}
-
-static void toneport_update_led(struct device *dev)
-{
-       struct usb_interface *interface = to_usb_interface(dev);
-       struct usb_line6_toneport *tp = usb_get_intfdata(interface);
-       struct usb_line6 *line6;
-
-       if (!tp)
-               return;
-
-       line6 = &tp->line6;
-       if (line6)
-               toneport_send_cmd(line6->usbdev, (led_red << 8) | 0x0002,
-                                 led_green);
-}
-
-static ssize_t toneport_set_led_red(struct device *dev,
-                                   struct device_attribute *attr,
-                                   const char *buf, size_t count)
-{
-       int retval;
-
-       retval = kstrtoint(buf, 10, &led_red);
-       if (retval)
-               return retval;
-
-       toneport_update_led(dev);
-       return count;
-}
-
-static ssize_t toneport_set_led_green(struct device *dev,
-                                     struct device_attribute *attr,
-                                     const char *buf, size_t count)
-{
-       int retval;
-
-       retval = kstrtoint(buf, 10, &led_green);
-       if (retval)
-               return retval;
-
-       toneport_update_led(dev);
-       return count;
-}
-
-static DEVICE_ATTR(led_red, S_IWUSR | S_IRUGO, line6_nop_read,
-                  toneport_set_led_red);
-static DEVICE_ATTR(led_green, S_IWUSR | S_IRUGO, line6_nop_read,
-                  toneport_set_led_green);
-
 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
 {
        int ret;
@@ -234,16 +169,23 @@ static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
                                    struct snd_ctl_elem_value *ucontrol)
 {
        struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
+       int err;
 
        if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
                return 0;
 
        line6pcm->volume_monitor = ucontrol->value.integer.value[0];
 
-       if (line6pcm->volume_monitor > 0)
-               line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_MONITOR);
-       else
-               line6_pcm_release(line6pcm, LINE6_BITS_PCM_MONITOR);
+       if (line6pcm->volume_monitor > 0) {
+               err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR);
+               if (err < 0) {
+                       line6pcm->volume_monitor = 0;
+                       line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
+                       return err;
+               }
+       } else {
+               line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
+       }
 
        return 1;
 }
@@ -304,7 +246,7 @@ static void toneport_start_pcm(unsigned long arg)
        struct usb_line6_toneport *toneport = (struct usb_line6_toneport *)arg;
        struct usb_line6 *line6 = &toneport->line6;
 
-       line6_pcm_acquire(line6->line6pcm, LINE6_BITS_PCM_MONITOR);
+       line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR);
 }
 
 /* control definition */
@@ -329,6 +271,97 @@ static struct snd_kcontrol_new toneport_control_source = {
        .put = snd_toneport_source_put
 };
 
+/*
+       For the led on Guitarport.
+       Brightness goes from 0x00 to 0x26. Set a value above this to have led
+       blink.
+       (void cmd_0x02(byte red, byte green)
+*/
+
+static bool toneport_has_led(struct usb_line6_toneport *toneport)
+{
+       switch (toneport->type) {
+       case LINE6_GUITARPORT:
+       case LINE6_TONEPORT_GX:
+       /* add your device here if you are missing support for the LEDs */
+               return true;
+
+       default:
+               return false;
+       }
+}
+
+static const char * const led_colors[2] = { "red", "green" };
+static const int led_init_vals[2] = { 0x00, 0x26 };
+
+static void toneport_update_led(struct usb_line6_toneport *toneport)
+{
+       toneport_send_cmd(toneport->line6.usbdev,
+                         (toneport->leds[0].dev.brightness << 8) | 0x0002,
+                         toneport->leds[1].dev.brightness);
+}
+
+static void toneport_led_brightness_set(struct led_classdev *led_cdev,
+                                       enum led_brightness brightness)
+{
+       struct toneport_led *leds =
+               container_of(led_cdev, struct toneport_led, dev);
+       toneport_update_led(leds->toneport);
+}
+
+static int toneport_init_leds(struct usb_line6_toneport *toneport)
+{
+       struct device *dev = &toneport->line6.usbdev->dev;
+       int i, err;
+
+       for (i = 0; i < 2; i++) {
+               struct toneport_led *led = &toneport->leds[i];
+               struct led_classdev *leddev = &led->dev;
+
+               led->toneport = toneport;
+               snprintf(led->name, sizeof(led->name), "%s::%s",
+                        dev_name(dev), led_colors[i]);
+               leddev->name = led->name;
+               leddev->brightness = led_init_vals[i];
+               leddev->max_brightness = 0x26;
+               leddev->brightness_set = toneport_led_brightness_set;
+               err = led_classdev_register(dev, leddev);
+               if (err)
+                       return err;
+               led->registered = true;
+       }
+
+       return 0;
+}
+
+static void toneport_remove_leds(struct usb_line6_toneport *toneport)
+{
+       struct toneport_led *led;
+       int i;
+
+       for (i = 0; i < 2; i++) {
+               led = &toneport->leds[i];
+               if (!led->registered)
+                       break;
+               led_classdev_unregister(&led->dev);
+               led->registered = false;
+       }
+}
+
+static bool toneport_has_source_select(struct usb_line6_toneport *toneport)
+{
+       switch (toneport->type) {
+       case LINE6_TONEPORT_UX1:
+       case LINE6_TONEPORT_UX2:
+       case LINE6_PODSTUDIO_UX1:
+       case LINE6_PODSTUDIO_UX2:
+               return true;
+
+       default:
+               return false;
+       }
+}
+
 /*
        Setup Toneport device.
 */
@@ -346,20 +379,13 @@ static void toneport_setup(struct usb_line6_toneport *toneport)
        toneport_send_cmd(usbdev, 0x0301, 0x0000);
 
        /* initialize source select: */
-       switch (toneport->type) {
-       case LINE6_TONEPORT_UX1:
-       case LINE6_TONEPORT_UX2:
-       case LINE6_PODSTUDIO_UX1:
-       case LINE6_PODSTUDIO_UX2:
+       if (toneport_has_source_select(toneport))
                toneport_send_cmd(usbdev,
                                  toneport_source_info[toneport->source].code,
                                  0x0000);
-       default:
-               break;
-       }
 
-       if (toneport_has_led(toneport->type))
-               toneport_update_led(&usbdev->dev);
+       if (toneport_has_led(toneport))
+               toneport_update_led(toneport);
 
        mod_timer(&toneport->timer, jiffies + TONEPORT_PCM_DELAY * HZ);
 }
@@ -367,29 +393,28 @@ static void toneport_setup(struct usb_line6_toneport *toneport)
 /*
        Toneport device disconnected.
 */
-static void line6_toneport_disconnect(struct usb_interface *interface)
+static void line6_toneport_disconnect(struct usb_line6 *line6)
 {
-       struct usb_line6_toneport *toneport;
+       struct usb_line6_toneport *toneport =
+               (struct usb_line6_toneport *)line6;
 
-       toneport = usb_get_intfdata(interface);
        del_timer_sync(&toneport->timer);
 
-       if (toneport_has_led(toneport->type)) {
-               device_remove_file(&interface->dev, &dev_attr_led_red);
-               device_remove_file(&interface->dev, &dev_attr_led_green);
-       }
+       if (toneport_has_led(toneport))
+               toneport_remove_leds(toneport);
 }
 
 
 /*
         Try to init Toneport device.
 */
-static int toneport_init(struct usb_interface *interface,
-                        struct usb_line6 *line6)
+static int toneport_init(struct usb_line6 *line6,
+                        const struct usb_device_id *id)
 {
        int err;
        struct usb_line6_toneport *toneport =  (struct usb_line6_toneport *) line6;
 
+       toneport->type = id->driver_info;
        setup_timer(&toneport->timer, toneport_start_pcm,
                    (unsigned long)toneport);
 
@@ -408,30 +433,20 @@ static int toneport_init(struct usb_interface *interface,
                return err;
 
        /* register source select control: */
-       switch (toneport->type) {
-       case LINE6_TONEPORT_UX1:
-       case LINE6_TONEPORT_UX2:
-       case LINE6_PODSTUDIO_UX1:
-       case LINE6_PODSTUDIO_UX2:
+       if (toneport_has_source_select(toneport)) {
                err =
                    snd_ctl_add(line6->card,
                                snd_ctl_new1(&toneport_control_source,
                                             line6->line6pcm));
                if (err < 0)
                        return err;
-
-       default:
-               break;
        }
 
        line6_read_serial_number(line6, &toneport->serial_number);
        line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
 
-       if (toneport_has_led(toneport->type)) {
-               err = device_create_file(&interface->dev, &dev_attr_led_red);
-               if (err < 0)
-                       return err;
-               err = device_create_file(&interface->dev, &dev_attr_led_green);
+       if (toneport_has_led(toneport)) {
+               err = toneport_init_leds(toneport);
                if (err < 0)
                        return err;
        }
@@ -542,15 +557,9 @@ static const struct line6_properties toneport_properties_table[] = {
 static int toneport_probe(struct usb_interface *interface,
                          const struct usb_device_id *id)
 {
-       struct usb_line6_toneport *toneport;
-
-       toneport = kzalloc(sizeof(*toneport), GFP_KERNEL);
-       if (!toneport)
-               return -ENODEV;
-       toneport->type = id->driver_info;
-       return line6_probe(interface, &toneport->line6,
+       return line6_probe(interface, id, "Line6-TonePort",
                           &toneport_properties_table[id->driver_info],
-                          toneport_init);
+                          toneport_init, sizeof(struct usb_line6_toneport));
 }
 
 static struct usb_driver toneport_driver = {