HID: wacom: Add ability to provide explicit battery status info
authorJason Gerecke <killertofu@gmail.com>
Fri, 28 Apr 2017 16:25:33 +0000 (09:25 -0700)
committerJiri Kosina <jkosina@suse.cz>
Fri, 5 May 2017 19:46:10 +0000 (21:46 +0200)
At the moment, our driver relies on 'wacom_battery_get_property()' to
determine the most likely battery state (e.g charging, discharging, or
full) based on the information available. It is not always possible
for the function to properly determine this, however. For instance,
whenever an AES pen leaves proximity the battery state becomes
indeterminite. This commit adds the ability to provide it with explict
state information if desired. Whenever explicit state is not required
(the majority of circumstances), WACOM_POWER_SUPPLY_STATUS_AUTO can
be used in its place.

Three uses of explicit battery status are added: two wireless disconnect
paths and the AES case mentioned above.

Signed-off-by: Jason Gerecke <jason.gerecke@wacom.com>
Reviewed-by: Ping Cheng <ping.cheng@wacom.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
drivers/hid/wacom.h
drivers/hid/wacom_sys.c
drivers/hid/wacom_wac.c
drivers/hid/wacom_wac.h

index c7b9ab1907d87aa6ddd8d09829e112105ca2e0d3..3c37c3cbf6f10013b928e18c1ef245f9eac3d295 100644 (file)
@@ -138,6 +138,7 @@ struct wacom_battery {
        struct power_supply_desc bat_desc;
        struct power_supply *battery;
        char bat_name[WACOM_NAME_MAX];
+       int bat_status;
        int battery_capacity;
        int bat_charging;
        int bat_connected;
index 0022c0dac88a20c79e4d080cbbdd35850ab36712..838c1ebfffa9779d6f95128361249bb14deb0f7c 100644 (file)
@@ -1547,7 +1547,9 @@ static int wacom_battery_get_property(struct power_supply *psy,
                        val->intval = battery->battery_capacity;
                        break;
                case POWER_SUPPLY_PROP_STATUS:
-                       if (battery->bat_charging)
+                       if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
+                               val->intval = battery->bat_status;
+                       else if (battery->bat_charging)
                                val->intval = POWER_SUPPLY_STATUS_CHARGING;
                        else if (battery->battery_capacity == 100 &&
                                    battery->ps_connected)
index 3e034506778f7f7efbd2ccc6fa2f7079942d4536..08a865f733fad3acbe0854d41d03dc7a86370ce7 100644 (file)
@@ -57,15 +57,18 @@ static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
 static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
 
 static void __wacom_notify_battery(struct wacom_battery *battery,
-                                  int bat_capacity, bool bat_charging,
-                                  bool bat_connected, bool ps_connected)
+                                  int bat_status, int bat_capacity,
+                                  bool bat_charging, bool bat_connected,
+                                  bool ps_connected)
 {
-       bool changed = battery->battery_capacity != bat_capacity  ||
+       bool changed = battery->bat_status       != bat_status    ||
+                      battery->battery_capacity != bat_capacity  ||
                       battery->bat_charging     != bat_charging  ||
                       battery->bat_connected    != bat_connected ||
                       battery->ps_connected     != ps_connected;
 
        if (changed) {
+               battery->bat_status = bat_status;
                battery->battery_capacity = bat_capacity;
                battery->bat_charging = bat_charging;
                battery->bat_connected = bat_connected;
@@ -77,13 +80,13 @@ static void __wacom_notify_battery(struct wacom_battery *battery,
 }
 
 static void wacom_notify_battery(struct wacom_wac *wacom_wac,
-       int bat_capacity, bool bat_charging, bool bat_connected,
-       bool ps_connected)
+       int bat_status, int bat_capacity, bool bat_charging,
+       bool bat_connected, bool ps_connected)
 {
        struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
 
-       __wacom_notify_battery(&wacom->battery, bat_capacity, bat_charging,
-                              bat_connected, ps_connected);
+       __wacom_notify_battery(&wacom->battery, bat_status, bat_capacity,
+                              bat_charging, bat_connected, ps_connected);
 }
 
 static int wacom_penpartner_irq(struct wacom_wac *wacom)
@@ -448,8 +451,9 @@ static int wacom_graphire_irq(struct wacom_wac *wacom)
                rw = (data[7] >> 2 & 0x07);
                battery_capacity = batcap_gr[rw];
                ps_connected = rw == 7;
-               wacom_notify_battery(wacom, battery_capacity, ps_connected,
-                                    1, ps_connected);
+               wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
+                                    battery_capacity, ps_connected, 1,
+                                    ps_connected);
        }
 exit:
        return retval;
@@ -1071,7 +1075,8 @@ static int wacom_remote_irq(struct wacom_wac *wacom_wac, size_t len)
                        wacom->led.groups[i].select = touch_ring_mode;
        }
 
-       __wacom_notify_battery(&remote->remotes[index].battery, bat_percent,
+       __wacom_notify_battery(&remote->remotes[index].battery,
+                               WACOM_POWER_SUPPLY_STATUS_AUTO, bat_percent,
                                bat_charging, 1, bat_charging);
 
 out:
@@ -1157,7 +1162,8 @@ static int wacom_intuos_bt_irq(struct wacom_wac *wacom, size_t len)
                bat_charging = (power_raw & 0x08) ? 1 : 0;
                ps_connected = (power_raw & 0x10) ? 1 : 0;
                battery_capacity = batcap_i4[power_raw & 0x07];
-               wacom_notify_battery(wacom, battery_capacity, bat_charging,
+               wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
+                                    battery_capacity, bat_charging,
                                     battery_capacity || bat_charging,
                                     ps_connected);
                break;
@@ -1334,7 +1340,8 @@ static void wacom_intuos_pro2_bt_battery(struct wacom_wac *wacom)
        bool chg = data[284] & 0x80;
        int battery_status = data[284] & 0x7F;
 
-       wacom_notify_battery(wacom, battery_status, chg, 1, chg);
+       wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
+                            battery_status, chg, 1, chg);
 }
 
 static int wacom_intuos_pro2_bt_irq(struct wacom_wac *wacom, size_t len)
@@ -1814,6 +1821,7 @@ static void wacom_wac_pad_battery_event(struct hid_device *hdev, struct hid_fiel
                value = value * 100 / (field->logical_maximum - field->logical_minimum);
                wacom_wac->hid_data.battery_capacity = value;
                wacom_wac->hid_data.bat_connected = 1;
+               wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
                break;
 
        case WACOM_HID_WD_BATTERY_CHARGING:
@@ -1905,13 +1913,14 @@ static void wacom_wac_pad_battery_report(struct hid_device *hdev,
        struct wacom_features *features = &wacom_wac->features;
 
        if (features->quirks & WACOM_QUIRK_BATTERY) {
+               int status = wacom_wac->hid_data.bat_status;
                int capacity = wacom_wac->hid_data.battery_capacity;
                bool charging = wacom_wac->hid_data.bat_charging;
                bool connected = wacom_wac->hid_data.bat_connected;
                bool powered = wacom_wac->hid_data.ps_connected;
 
-               wacom_notify_battery(wacom_wac, capacity, charging,
-                                    connected, powered);
+               wacom_notify_battery(wacom_wac, status, capacity,
+                                    charging, connected, powered);
        }
 }
 
@@ -2036,11 +2045,15 @@ static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field
                        wacom_wac->hid_data.sense_state = value;
                return;
        case HID_DG_BATTERYSTRENGTH:
-               if (value == 0) /* "not available" */
-                       break;
-               value = value * 100 / (field->logical_maximum - field->logical_minimum);
-               wacom_wac->hid_data.battery_capacity = value;
-               wacom_wac->hid_data.bat_connected = 1;
+               if (value == 0) {
+                       wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
+               }
+               else {
+                       value = value * 100 / (field->logical_maximum - field->logical_minimum);
+                       wacom_wac->hid_data.battery_capacity = value;
+                       wacom_wac->hid_data.bat_connected = 1;
+                       wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
+               }
                break;
        case HID_DG_INVERT:
                wacom_wac->hid_data.invert_state = value;
@@ -2818,13 +2831,14 @@ static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
                        wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
                }
 
-               wacom_notify_battery(wacom, battery, charging, 1, 0);
+               wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
+                                    battery, charging, 1, 0);
 
        } else if (wacom->pid != 0) {
                /* disconnected while previously connected */
                wacom->pid = 0;
                wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
-               wacom_notify_battery(wacom, 0, 0, 0, 0);
+               wacom_notify_battery(wacom, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
        }
 
        return 0;
@@ -2852,8 +2866,8 @@ static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
                int battery = (data[8] & 0x3f) * 100 / 31;
                bool charging = !!(data[8] & 0x80);
 
-               wacom_notify_battery(wacom_wac, battery, charging,
-                                    battery || charging, 1);
+               wacom_notify_battery(wacom_wac, WACOM_POWER_SUPPLY_STATUS_AUTO,
+                                    battery, charging, battery || charging, 1);
 
                if (!wacom->battery.battery &&
                    !(features->quirks & WACOM_QUIRK_BATTERY)) {
@@ -2865,7 +2879,7 @@ static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
                 wacom->battery.battery) {
                features->quirks &= ~WACOM_QUIRK_BATTERY;
                wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
-               wacom_notify_battery(wacom_wac, 0, 0, 0, 0);
+               wacom_notify_battery(wacom_wac, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
        }
        return 0;
 }
index 570d29582b8206c2c0ba530029916b70500baaa0..1824b530bcb5e40a5ee8504ea6a0391a4cde4b3d 100644 (file)
@@ -96,6 +96,8 @@
 #define WACOM_DEVICETYPE_WL_MONITOR     0x0008
 #define WACOM_DEVICETYPE_DIRECT         0x0010
 
+#define WACOM_POWER_SUPPLY_STATUS_AUTO  -1
+
 #define WACOM_HID_UP_WACOMDIGITIZER     0xff0d0000
 #define WACOM_HID_SP_PAD                0x00040000
 #define WACOM_HID_SP_BUTTON             0x00090000
@@ -297,6 +299,7 @@ struct hid_data {
        int last_slot_field;
        int num_expected;
        int num_received;
+       int bat_status;
        int battery_capacity;
        int bat_charging;
        int bat_connected;