eeepc-wmi: add touchpad sysfs file
[firefly-linux-kernel-4.4.55.git] / drivers / platform / x86 / eeepc-wmi.c
index aa9e1d1719da3de088bf8a095e9483d2a5e33493..16c7f2d62515d850dd99a21b2310de14914bf3bd 100644 (file)
@@ -71,9 +71,19 @@ MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
 
 #define EEEPC_WMI_DEVID_WLAN           0x00010011
 #define EEEPC_WMI_DEVID_BLUETOOTH      0x00010013
+#define EEEPC_WMI_DEVID_WIMAX          0x00010017
 #define EEEPC_WMI_DEVID_WWAN3G         0x00010019
-#define EEEPC_WMI_DEVID_BACKLIGHT      0x00050012
-#define EEEPC_WMI_DEVID_TPDLED         0x00100011
+#define EEEPC_WMI_DEVID_BACKLIGHT      0x00050011
+#define EEEPC_WMI_DEVID_BRIGHTNESS     0x00050012
+#define EEEPC_WMI_DEVID_CAMERA         0x00060013
+#define EEEPC_WMI_DEVID_CARDREADER     0x00080013
+#define EEEPC_WMI_DEVID_TOUCHPAD       0x00100011
+#define EEEPC_WMI_DEVID_TOUCHPAD_LED   0x00100012
+
+#define EEEPC_WMI_DSTS_STATUS_BIT      0x00000001
+#define EEEPC_WMI_DSTS_PRESENCE_BIT    0x00010000
+#define EEEPC_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
+#define EEEPC_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
 
 static bool hotplug_wireless;
 
@@ -90,14 +100,14 @@ static const struct key_entry eeepc_wmi_keymap[] = {
        { KE_KEY, 0x30, { KEY_VOLUMEUP } },
        { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
        { KE_KEY, 0x32, { KEY_MUTE } },
-       { KE_KEY, 0x5c, { KEY_F15 } },
+       { KE_KEY, 0x5c, { KEY_F15 } }, /* Power Gear key */
        { KE_KEY, 0x5d, { KEY_WLAN } },
        { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
        { KE_KEY, 0x88, { KEY_WLAN } },
        { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
-       { KE_KEY, 0xe0, { KEY_PROG1 } },
-       { KE_KEY, 0xe1, { KEY_F14 } },
-       { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
+       { KE_KEY, 0xe0, { KEY_PROG1 } }, /* Task Manager */
+       { KE_KEY, 0xe1, { KEY_F14 } }, /* Change Resolution */
+       { KE_KEY, 0xe9, { KEY_BRIGHTNESS_ZERO } },
        { KE_END, 0},
 };
 
@@ -133,6 +143,7 @@ struct eeepc_wmi {
 
        struct rfkill *wlan_rfkill;
        struct rfkill *bluetooth_rfkill;
+       struct rfkill *wimax_rfkill;
        struct rfkill *wwan3g_rfkill;
 
        struct hotplug_slot *hotplug_slot;
@@ -255,7 +266,7 @@ static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
 }
 
 /* Helper for special devices with magic return codes */
-static int eeepc_wmi_get_devstate_simple(u32 dev_id)
+static int eeepc_wmi_get_devstate_bits(u32 dev_id, u32 mask)
 {
        u32 retval = 0;
        acpi_status status;
@@ -265,16 +276,15 @@ static int eeepc_wmi_get_devstate_simple(u32 dev_id)
        if (ACPI_FAILURE(status))
                return -EINVAL;
 
-       /* If the device is present, DSTS will always set some bits
-        * 0x00070000 - 1110000000000000000 - device supported
-        * 0x00060000 - 1100000000000000000 - not supported
-        * 0x00020000 - 0100000000000000000 - device supported
-        * 0x00010000 - 0010000000000000000 - not supported / special mode ?
-        */
-       if (!retval || retval == 0x00060000)
+       if (!(retval & EEEPC_WMI_DSTS_PRESENCE_BIT))
                return -ENODEV;
 
-       return retval & 0x1;
+       return retval & mask;
+}
+
+static int eeepc_wmi_get_devstate_simple(u32 dev_id)
+{
+       return eeepc_wmi_get_devstate_bits(dev_id, EEEPC_WMI_DSTS_STATUS_BIT);
 }
 
 /*
@@ -294,7 +304,7 @@ static void tpd_led_update(struct work_struct *work)
        eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
 
        ctrl_param = eeepc->tpd_led_wk;
-       eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
+       eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
 }
 
 static void tpd_led_set(struct led_classdev *led_cdev,
@@ -308,9 +318,9 @@ static void tpd_led_set(struct led_classdev *led_cdev,
        queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
 }
 
-static int read_tpd_state(struct eeepc_wmi *eeepc)
+static int read_tpd_led_state(struct eeepc_wmi *eeepc)
 {
-       return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TPDLED);
+       return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TOUCHPAD_LED);
 }
 
 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
@@ -319,14 +329,14 @@ static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
 
        eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
 
-       return read_tpd_state(eeepc);
+       return read_tpd_led_state(eeepc);
 }
 
 static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
 {
        int rv;
 
-       if (read_tpd_state(eeepc) < 0)
+       if (read_tpd_led_state(eeepc) < 0)
                return 0;
 
        eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
@@ -692,6 +702,11 @@ static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
                rfkill_destroy(eeepc->bluetooth_rfkill);
                eeepc->bluetooth_rfkill = NULL;
        }
+       if (eeepc->wimax_rfkill) {
+               rfkill_unregister(eeepc->wimax_rfkill);
+               rfkill_destroy(eeepc->wimax_rfkill);
+               eeepc->wimax_rfkill = NULL;
+       }
        if (eeepc->wwan3g_rfkill) {
                rfkill_unregister(eeepc->wwan3g_rfkill);
                rfkill_destroy(eeepc->wwan3g_rfkill);
@@ -720,6 +735,13 @@ static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
        if (result && result != -ENODEV)
                goto exit;
 
+       result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
+                                 "eeepc-wimax", RFKILL_TYPE_WIMAX,
+                                 EEEPC_WMI_DEVID_WIMAX);
+
+       if (result && result != -ENODEV)
+               goto exit;
+
        result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
                                  "eeepc-wwan3g", RFKILL_TYPE_WWAN,
                                  EEEPC_WMI_DEVID_WWAN3G);
@@ -727,6 +749,9 @@ static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
        if (result && result != -ENODEV)
                goto exit;
 
+       if (!eeepc->hotplug_wireless)
+               goto exit;
+
        result = eeepc_setup_pci_hotplug(eeepc);
        /*
         * If we get -EBUSY then something else is handling the PCI hotplug -
@@ -757,34 +782,53 @@ exit:
 /*
  * Backlight
  */
+static int read_backlight_power(void)
+{
+       int ret = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BACKLIGHT);
+
+       if (ret < 0)
+               return ret;
+
+       return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
+}
+
 static int read_brightness(struct backlight_device *bd)
 {
        u32 retval;
        acpi_status status;
 
-       status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
+       status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BRIGHTNESS, &retval);
 
        if (ACPI_FAILURE(status))
-               return -1;
+               return -EIO;
        else
-               return retval & 0xFF;
+               return retval & EEEPC_WMI_DSTS_BRIGHTNESS_MASK;
 }
 
 static int update_bl_status(struct backlight_device *bd)
 {
-
        u32 ctrl_param;
        acpi_status status;
+       int power;
 
        ctrl_param = bd->props.brightness;
 
-       status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
+       status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BRIGHTNESS,
                                        ctrl_param, NULL);
 
        if (ACPI_FAILURE(status))
-               return -1;
-       else
-               return 0;
+               return -EIO;
+
+       power = read_backlight_power();
+       if (power != -ENODEV && bd->props.power != power) {
+               ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
+               status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
+                                               ctrl_param, NULL);
+
+               if (ACPI_FAILURE(status))
+                       return -EIO;
+       }
+       return 0;
 }
 
 static const struct backlight_ops eeepc_wmi_bl_ops = {
@@ -814,9 +858,29 @@ static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
 {
        struct backlight_device *bd;
        struct backlight_properties props;
+       int max;
+       int power;
+
+       max = eeepc_wmi_get_devstate_bits(EEEPC_WMI_DEVID_BRIGHTNESS,
+                                         EEEPC_WMI_DSTS_MAX_BRIGTH_MASK);
+       power = read_backlight_power();
+
+       if (max < 0 && power < 0) {
+               /* Try to keep the original error */
+               if (max == -ENODEV && power == -ENODEV)
+                       return -ENODEV;
+               if (max != -ENODEV)
+                       return max;
+               else
+                       return power;
+       }
+       if (max == -ENODEV)
+               max = 0;
+       if (power == -ENODEV)
+               power = FB_BLANK_UNBLANK;
 
        memset(&props, 0, sizeof(struct backlight_properties));
-       props.max_brightness = 15;
+       props.max_brightness = max;
        bd = backlight_device_register(EEEPC_WMI_FILE,
                                       &eeepc->platform_device->dev, eeepc,
                                       &eeepc_wmi_bl_ops, &props);
@@ -828,7 +892,7 @@ static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
        eeepc->backlight_device = bd;
 
        bd->props.brightness = read_brightness(bd);
-       bd->props.power = FB_BLANK_UNBLANK;
+       bd->props.power = power;
        backlight_update_status(bd);
 
        return 0;
@@ -882,6 +946,71 @@ static void eeepc_wmi_notify(u32 value, void *context)
        kfree(obj);
 }
 
+/*
+ * Sys helpers
+ */
+static int parse_arg(const char *buf, unsigned long count, int *val)
+{
+       if (!count)
+               return 0;
+       if (sscanf(buf, "%i", val) != 1)
+               return -EINVAL;
+       return count;
+}
+
+static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
+{
+       acpi_status status;
+       u32 retval;
+       int rv, value;
+
+       value = eeepc_wmi_get_devstate_simple(devid);
+       if (value == -ENODEV) /* Check device presence */
+               return value;
+
+       rv = parse_arg(buf, count, &value);
+       status = eeepc_wmi_set_devstate(devid, value, &retval);
+
+       if (ACPI_FAILURE(status))
+               return -EIO;
+       return rv;
+}
+
+static ssize_t show_sys_wmi(int devid, char *buf)
+{
+       int value = eeepc_wmi_get_devstate_simple(devid);
+
+       if (value < 0)
+               return value;
+
+       return sprintf(buf, "%d\n", value);
+}
+
+#define EEEPC_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)                        \
+       static ssize_t show_##_name(struct device *dev,                 \
+                                   struct device_attribute *attr,      \
+                                   char *buf)                          \
+       {                                                               \
+               return show_sys_wmi(_cm, buf);                          \
+       }                                                               \
+       static ssize_t store_##_name(struct device *dev,                \
+                                    struct device_attribute *attr,     \
+                                    const char *buf, size_t count)     \
+       {                                                               \
+               return store_sys_wmi(_cm, buf, count);                  \
+       }                                                               \
+       static struct device_attribute dev_attr_##_name = {             \
+               .attr = {                                               \
+                       .name = __stringify(_name),                     \
+                       .mode = _mode },                                \
+               .show   = show_##_name,                                 \
+               .store  = store_##_name,                                \
+       }
+
+EEEPC_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, EEEPC_WMI_DEVID_TOUCHPAD);
+EEEPC_WMI_CREATE_DEVICE_ATTR(camera, 0644, EEEPC_WMI_DEVID_CAMERA);
+EEEPC_WMI_CREATE_DEVICE_ATTR(cardr, 0644, EEEPC_WMI_DEVID_CARDREADER);
+
 static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
                           const char *buf, size_t count)
 {
@@ -907,11 +1036,35 @@ static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
 
 static struct attribute *platform_attributes[] = {
        &dev_attr_cpufv.attr,
+       &dev_attr_camera.attr,
+       &dev_attr_cardr.attr,
+       &dev_attr_touchpad.attr,
        NULL
 };
 
+static mode_t eeepc_sysfs_is_visible(struct kobject *kobj,
+                                    struct attribute *attr,
+                                    int idx)
+{
+       bool supported = true;
+       int devid = -1;
+
+       if (attr == &dev_attr_camera.attr)
+               devid = EEEPC_WMI_DEVID_CAMERA;
+       else if (attr == &dev_attr_cardr.attr)
+               devid = EEEPC_WMI_DEVID_CARDREADER;
+       else if (attr == &dev_attr_touchpad.attr)
+               devid = EEEPC_WMI_DEVID_TOUCHPAD;
+
+       if (devid != -1)
+               supported = eeepc_wmi_get_devstate_simple(devid) != -ENODEV;
+
+       return supported ? attr->mode : 0;
+}
+
 static struct attribute_group platform_attribute_group = {
-       .attrs = platform_attributes
+       .is_visible     = eeepc_sysfs_is_visible,
+       .attrs          = platform_attributes
 };
 
 static void eeepc_wmi_sysfs_exit(struct platform_device *device)
@@ -1104,7 +1257,7 @@ static int __init eeepc_wmi_add(struct platform_device *pdev)
 
        if (!acpi_video_backlight_support()) {
                err = eeepc_wmi_backlight_init(eeepc);
-               if (err)
+               if (err && err != -ENODEV)
                        goto fail_backlight;
        } else
                pr_info("Backlight controlled by ACPI video driver\n");
@@ -1192,7 +1345,11 @@ static int eeepc_hotk_restore(struct device *device)
        if (eeepc->bluetooth_rfkill) {
                bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BLUETOOTH);
                rfkill_set_sw_state(eeepc->bluetooth_rfkill, bl);
-}
+       }
+       if (eeepc->wimax_rfkill) {
+               bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WIMAX);
+               rfkill_set_sw_state(eeepc->wimax_rfkill, bl);
+       }
        if (eeepc->wwan3g_rfkill) {
                bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WWAN3G);
                rfkill_set_sw_state(eeepc->wwan3g_rfkill, bl);