Merge branch 'upstream' into for-linus
[safe/jmp/linux-2.6] / drivers / hid / hid-wacom.c
index f947d83..1e051f1 100644 (file)
 #include <linux/hid.h>
 #include <linux/module.h>
 #include <linux/slab.h>
+#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
+#include <linux/power_supply.h>
+#endif
 
 #include "hid-ids.h"
 
 struct wacom_data {
        __u16 tool;
        unsigned char butstate;
+       unsigned char high_speed;
+#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
+       int battery_capacity;
+       struct power_supply battery;
+       struct power_supply ac;
+#endif
 };
 
+#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
+/*percent of battery capacity, 0 means AC online*/
+static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
+
+static enum power_supply_property wacom_battery_props[] = {
+       POWER_SUPPLY_PROP_PRESENT,
+       POWER_SUPPLY_PROP_CAPACITY
+};
+
+static enum power_supply_property wacom_ac_props[] = {
+       POWER_SUPPLY_PROP_PRESENT,
+       POWER_SUPPLY_PROP_ONLINE
+};
+
+static int wacom_battery_get_property(struct power_supply *psy,
+                               enum power_supply_property psp,
+                               union power_supply_propval *val)
+{
+       struct wacom_data *wdata = container_of(psy,
+                                       struct wacom_data, battery);
+       int power_state = batcap[wdata->battery_capacity];
+       int ret = 0;
+
+       switch (psp) {
+       case POWER_SUPPLY_PROP_PRESENT:
+               val->intval = 1;
+               break;
+       case POWER_SUPPLY_PROP_CAPACITY:
+               /* show 100% battery capacity when charging */
+               if (power_state == 0)
+                       val->intval = 100;
+               else
+                       val->intval = power_state;
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+       return ret;
+}
+
+static int wacom_ac_get_property(struct power_supply *psy,
+                               enum power_supply_property psp,
+                               union power_supply_propval *val)
+{
+       struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
+       int power_state = batcap[wdata->battery_capacity];
+       int ret = 0;
+
+       switch (psp) {
+       case POWER_SUPPLY_PROP_PRESENT:
+               /* fall through */
+       case POWER_SUPPLY_PROP_ONLINE:
+               if (power_state == 0)
+                       val->intval = 1;
+               else
+                       val->intval = 0;
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+       return ret;
+}
+#endif
+
+static void wacom_poke(struct hid_device *hdev, u8 speed)
+{
+       struct wacom_data *wdata = hid_get_drvdata(hdev);
+       int limit, ret;
+       char rep_data[2];
+
+       rep_data[0] = 0x03 ; rep_data[1] = 0x00;
+       limit = 3;
+       do {
+               ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
+                               HID_FEATURE_REPORT);
+       } while (ret < 0 && limit-- > 0);
+
+       if (ret >= 0) {
+               if (speed == 0)
+                       rep_data[0] = 0x05;
+               else
+                       rep_data[0] = 0x06;
+
+               rep_data[1] = 0x00;
+               limit = 3;
+               do {
+                       ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
+                                       HID_FEATURE_REPORT);
+               } while (ret < 0 && limit-- > 0);
+
+               if (ret >= 0) {
+                       wdata->high_speed = speed;
+                       return;
+               }
+       }
+
+       /*
+        * Note that if the raw queries fail, it's not a hard failure and it
+        * is safe to continue
+        */
+       dev_warn(&hdev->dev, "failed to poke device, command %d, err %d\n",
+                               rep_data[0], ret);
+       return;
+}
+
+static ssize_t wacom_show_speed(struct device *dev,
+                               struct device_attribute
+                               *attr, char *buf)
+{
+       struct wacom_data *wdata = dev_get_drvdata(dev);
+
+       return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
+}
+
+static ssize_t wacom_store_speed(struct device *dev,
+                               struct device_attribute *attr,
+                               const char *buf, size_t count)
+{
+       struct hid_device *hdev = container_of(dev, struct hid_device, dev);
+       int new_speed;
+
+       if (sscanf(buf, "%1d", &new_speed ) != 1)
+               return -EINVAL;
+
+       if (new_speed == 0 || new_speed == 1) {
+               wacom_poke(hdev, new_speed);
+               return strnlen(buf, PAGE_SIZE);
+       } else
+               return -EINVAL;
+}
+
+static DEVICE_ATTR(speed, S_IRUGO | S_IWUGO,
+               wacom_show_speed, wacom_store_speed);
+
 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
                u8 *raw_data, int size)
 {
@@ -148,6 +293,12 @@ static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
                input_sync(input);
        }
 
+#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
+       /* Store current battery capacity */
+       rw = (data[7] >> 2 & 0x07);
+       if (rw != wdata->battery_capacity)
+               wdata->battery_capacity = rw;
+#endif
        return 1;
 }
 
@@ -157,9 +308,7 @@ static int wacom_probe(struct hid_device *hdev,
        struct hid_input *hidinput;
        struct input_dev *input;
        struct wacom_data *wdata;
-       char rep_data[2];
        int ret;
-       int limit;
 
        wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
        if (wdata == NULL) {
@@ -182,31 +331,53 @@ static int wacom_probe(struct hid_device *hdev,
                goto err_free;
        }
 
-       /*
-        * Note that if the raw queries fail, it's not a hard failure and it
-        * is safe to continue
-        */
+       ret = device_create_file(&hdev->dev, &dev_attr_speed);
+       if (ret)
+               dev_warn(&hdev->dev,
+                       "can't create sysfs speed attribute err: %d\n", ret);
 
-       /* Set Wacom mode2 */
-       rep_data[0] = 0x03; rep_data[1] = 0x00;
-       limit = 3;
-       do {
-               ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
-                               HID_FEATURE_REPORT);
-       } while (ret < 0 && limit-- > 0);
-       if (ret < 0)
-               dev_warn(&hdev->dev, "failed to poke device #1, %d\n", ret);
+       /* Set Wacom mode 2 with high reporting speed */
+       wacom_poke(hdev, 1);
 
-       /* 0x06 - high reporting speed, 0x05 - low speed */
-       rep_data[0] = 0x06; rep_data[1] = 0x00;
-       limit = 3;
-       do {
-               ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
-                               HID_FEATURE_REPORT);
-       } while (ret < 0 && limit-- > 0);
-       if (ret < 0)
-               dev_warn(&hdev->dev, "failed to poke device #2, %d\n", ret);
+#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
+       wdata->battery.properties = wacom_battery_props;
+       wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
+       wdata->battery.get_property = wacom_battery_get_property;
+       wdata->battery.name = "wacom_battery";
+       wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
+       wdata->battery.use_for_apm = 0;
 
+       ret = power_supply_register(&hdev->dev, &wdata->battery);
+       if (ret) {
+               dev_warn(&hdev->dev,
+                       "can't create sysfs battery attribute, err: %d\n", ret);
+               /*
+                * battery attribute is not critical for the tablet, but if it
+                * failed then there is no need to create ac attribute
+                */
+               goto move_on;
+       }
+
+       wdata->ac.properties = wacom_ac_props;
+       wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
+       wdata->ac.get_property = wacom_ac_get_property;
+       wdata->ac.name = "wacom_ac";
+       wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
+       wdata->ac.use_for_apm = 0;
+
+       ret = power_supply_register(&hdev->dev, &wdata->ac);
+       if (ret) {
+               dev_warn(&hdev->dev,
+                       "can't create ac battery attribute, err: %d\n", ret);
+               /*
+                * ac attribute is not critical for the tablet, but if it
+                * failed then we don't want to battery attribute to exist
+                */
+               power_supply_unregister(&wdata->battery);
+       }
+
+move_on:
+#endif
        hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
        input = hidinput->input;
 
@@ -251,13 +422,21 @@ err_free:
 
 static void wacom_remove(struct hid_device *hdev)
 {
+#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
+       struct wacom_data *wdata = hid_get_drvdata(hdev);
+#endif
        hid_hw_stop(hdev);
+
+#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
+       power_supply_unregister(&wdata->battery);
+       power_supply_unregister(&wdata->ac);
+#endif
        kfree(hid_get_drvdata(hdev));
 }
 
 static const struct hid_device_id wacom_devices[] = {
        { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
-
+       { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
        { }
 };
 MODULE_DEVICE_TABLE(hid, wacom_devices);