]> git.karo-electronics.de Git - linux-beck.git/commitdiff
HID: wacom: Centralize updating of wacom_wac battery status
authorJason Gerecke <killertofu@gmail.com>
Fri, 6 Mar 2015 19:47:39 +0000 (11:47 -0800)
committerJiri Kosina <jkosina@suse.cz>
Wed, 11 Mar 2015 15:46:11 +0000 (11:46 -0400)
Has the 'wacom_notify_battery' function take on the job of detecting if
updating the power supply is necessary to remove multiple
nearly-identical 'if' blocks.

Signed-off-by: Jason Gerecke <killertofu@gmail.com>
Acked-by: Ping Cheng <pingc@wacom.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
drivers/hid/wacom.h
drivers/hid/wacom_wac.c

index 7db432809e9ebd12c96a74c065b7a82f536c94fe..b8344b14076065c94dee00cefe7e95752a61d5f3 100644 (file)
@@ -129,13 +129,6 @@ static inline void wacom_schedule_work(struct wacom_wac *wacom_wac)
        schedule_work(&wacom->work);
 }
 
-static inline void wacom_notify_battery(struct wacom_wac *wacom_wac)
-{
-       struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
-
-       power_supply_changed(&wacom->battery);
-}
-
 extern const struct hid_device_id wacom_ids[];
 
 void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len);
index cb308c5f441a2fec6594c65aa0436e6fa8c911f9..5d57fec177a8a72e7ea0d6a313cd57c91044e0e7 100644 (file)
@@ -45,6 +45,24 @@ 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_wac *wacom_wac,
+       int bat_capacity, bool bat_charging, bool ps_connected)
+{
+       struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
+       bool changed = wacom_wac->battery_capacity != bat_capacity  ||
+                      wacom_wac->bat_charging     != bat_charging  ||
+                      wacom_wac->ps_connected     != ps_connected;
+
+       if (changed) {
+               wacom_wac->battery_capacity = bat_capacity;
+               wacom_wac->bat_charging = bat_charging;
+               wacom_wac->ps_connected = ps_connected;
+
+               if (wacom->battery.dev)
+                       power_supply_changed(&wacom->battery);
+       }
+}
+
 static int wacom_penpartner_irq(struct wacom_wac *wacom)
 {
        unsigned char *data = wacom->data;
@@ -419,12 +437,8 @@ static int wacom_graphire_irq(struct wacom_wac *wacom)
                rw = (data[7] >> 2 & 0x07);
                battery_capacity = batcap_gr[rw];
                ps_connected = rw == 7;
-               if ((wacom->battery_capacity != battery_capacity) ||
-                   (wacom->ps_connected != ps_connected)) {
-                       wacom->battery_capacity = battery_capacity;
-                       wacom->ps_connected = ps_connected;
-                       wacom_notify_battery(wacom);
-               }
+               wacom_notify_battery(wacom, battery_capacity, ps_connected,
+                                    ps_connected);
        }
 exit:
        return retval;
@@ -1014,15 +1028,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];
-               if ((wacom->battery_capacity != battery_capacity) ||
-                   (wacom->bat_charging != bat_charging) ||
-                   (wacom->ps_connected != ps_connected)) {
-                       wacom->battery_capacity = battery_capacity;
-                       wacom->bat_charging = bat_charging;
-                       wacom->ps_connected = ps_connected;
-                       wacom_notify_battery(wacom);
-               }
-
+               wacom_notify_battery(wacom, battery_capacity, bat_charging,
+                                    ps_connected);
                break;
        default:
                dev_dbg(wacom->input->dev.parent,
@@ -1910,7 +1917,7 @@ static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
 
        connected = data[1] & 0x01;
        if (connected) {
-               int pid, battery, ps_connected;
+               int pid, battery, ps_connected, charging;
 
                if ((wacom->shared->type == INTUOSHT) &&
                    wacom->shared->touch_input &&
@@ -1923,27 +1930,21 @@ static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
                pid = get_unaligned_be16(&data[6]);
                battery = (data[5] & 0x3f) * 100 / 31;
                ps_connected = !!(data[5] & 0x80);
+               charging = ps_connected && wacom->battery_capacity < 100;
                if (wacom->pid != pid) {
                        wacom->pid = pid;
                        wacom_schedule_work(wacom);
                }
 
-               if (wacom->shared->type &&
-                   (battery != wacom->battery_capacity ||
-                    ps_connected != wacom->ps_connected)) {
-                       wacom->battery_capacity = battery;
-                       wacom->ps_connected = ps_connected;
-                       wacom->bat_charging = ps_connected &&
-                                               wacom->battery_capacity < 100;
-                       wacom_notify_battery(wacom);
-               }
+               if (wacom->shared->type)
+                       wacom_notify_battery(wacom, battery, charging,
+                                            ps_connected);
+
        } else if (wacom->pid != 0) {
                /* disconnected while previously connected */
                wacom->pid = 0;
                wacom_schedule_work(wacom);
-               wacom->battery_capacity = 0;
-               wacom->bat_charging = 0;
-               wacom->ps_connected = 0;
+               wacom_notify_battery(wacom, 0, 0, 0);
        }
 
        return 0;