Skip to content

Commit 5ac3d4a

Browse files
jigpuJiri Kosina
authored andcommitted
HID: wacom: generic: Refactor generic battery handling
Generic battery handling code is spread between the pen and pad codepaths since battery usages may appear in reports for either. This makes it difficult to concisely see the logic involved. Since battery data is not treated like other data (i.e., we report it through the power_supply subsystem rather than through the input subsystem), it makes reasonable sense to split the functionality out into its own functions. This commit has the generic battery handling duplicate the same pattern that is used by the pen, pad, and touch interfaces. A "mapping" function is provided to set up the battery, an "event" function is provided to update the battery data, and a "report" function is provided to notify the power_supply subsystem after all the data has been read. We look at the usage itself rather than its collection to determine if one of the battery functions should handle it. Additionally, we unconditionally call the "report" function since there is no particularly good way to know if a report contained a battery usage; 'wacom_notify_battery()' will filter out any duplicate updates, however. Signed-off-by: Jason Gerecke <[email protected]> Reviewed-by: Ping Cheng <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 16e4598 commit 5ac3d4a

File tree

2 files changed

+95
-71
lines changed

2 files changed

+95
-71
lines changed

drivers/hid/wacom_wac.c

Lines changed: 91 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,20 +1702,92 @@ static void wacom_map_usage(struct input_dev *input, struct hid_usage *usage,
17021702
}
17031703
}
17041704

1705-
static void wacom_wac_pad_usage_mapping(struct hid_device *hdev,
1705+
static void wacom_wac_battery_usage_mapping(struct hid_device *hdev,
17061706
struct hid_field *field, struct hid_usage *usage)
17071707
{
17081708
struct wacom *wacom = hid_get_drvdata(hdev);
17091709
struct wacom_wac *wacom_wac = &wacom->wacom_wac;
17101710
struct wacom_features *features = &wacom_wac->features;
1711-
struct input_dev *input = wacom_wac->pad_input;
17121711
unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
17131712

17141713
switch (equivalent_usage) {
1714+
case HID_DG_BATTERYSTRENGTH:
17151715
case WACOM_HID_WD_BATTERY_LEVEL:
17161716
case WACOM_HID_WD_BATTERY_CHARGING:
17171717
features->quirks |= WACOM_QUIRK_BATTERY;
17181718
break;
1719+
}
1720+
}
1721+
1722+
static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *field,
1723+
struct hid_usage *usage, __s32 value)
1724+
{
1725+
struct wacom *wacom = hid_get_drvdata(hdev);
1726+
struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1727+
unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1728+
1729+
switch (equivalent_usage) {
1730+
case HID_DG_BATTERYSTRENGTH:
1731+
if (value == 0) {
1732+
wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
1733+
}
1734+
else {
1735+
value = value * 100 / (field->logical_maximum - field->logical_minimum);
1736+
wacom_wac->hid_data.battery_capacity = value;
1737+
wacom_wac->hid_data.bat_connected = 1;
1738+
wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1739+
}
1740+
break;
1741+
case WACOM_HID_WD_BATTERY_LEVEL:
1742+
value = value * 100 / (field->logical_maximum - field->logical_minimum);
1743+
wacom_wac->hid_data.battery_capacity = value;
1744+
wacom_wac->hid_data.bat_connected = 1;
1745+
wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1746+
break;
1747+
case WACOM_HID_WD_BATTERY_CHARGING:
1748+
wacom_wac->hid_data.bat_charging = value;
1749+
wacom_wac->hid_data.ps_connected = value;
1750+
wacom_wac->hid_data.bat_connected = 1;
1751+
wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1752+
break;
1753+
}
1754+
}
1755+
1756+
static void wacom_wac_battery_pre_report(struct hid_device *hdev,
1757+
struct hid_report *report)
1758+
{
1759+
return;
1760+
}
1761+
1762+
static void wacom_wac_battery_report(struct hid_device *hdev,
1763+
struct hid_report *report)
1764+
{
1765+
struct wacom *wacom = hid_get_drvdata(hdev);
1766+
struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1767+
struct wacom_features *features = &wacom_wac->features;
1768+
1769+
if (features->quirks & WACOM_QUIRK_BATTERY) {
1770+
int status = wacom_wac->hid_data.bat_status;
1771+
int capacity = wacom_wac->hid_data.battery_capacity;
1772+
bool charging = wacom_wac->hid_data.bat_charging;
1773+
bool connected = wacom_wac->hid_data.bat_connected;
1774+
bool powered = wacom_wac->hid_data.ps_connected;
1775+
1776+
wacom_notify_battery(wacom_wac, status, capacity, charging,
1777+
connected, powered);
1778+
}
1779+
}
1780+
1781+
static void wacom_wac_pad_usage_mapping(struct hid_device *hdev,
1782+
struct hid_field *field, struct hid_usage *usage)
1783+
{
1784+
struct wacom *wacom = hid_get_drvdata(hdev);
1785+
struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1786+
struct wacom_features *features = &wacom_wac->features;
1787+
struct input_dev *input = wacom_wac->pad_input;
1788+
unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1789+
1790+
switch (equivalent_usage) {
17191791
case WACOM_HID_WD_ACCELEROMETER_X:
17201792
__set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
17211793
wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 0);
@@ -1809,29 +1881,6 @@ static void wacom_wac_pad_usage_mapping(struct hid_device *hdev,
18091881
}
18101882
}
18111883

1812-
static void wacom_wac_pad_battery_event(struct hid_device *hdev, struct hid_field *field,
1813-
struct hid_usage *usage, __s32 value)
1814-
{
1815-
struct wacom *wacom = hid_get_drvdata(hdev);
1816-
struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1817-
unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1818-
1819-
switch (equivalent_usage) {
1820-
case WACOM_HID_WD_BATTERY_LEVEL:
1821-
value = value * 100 / (field->logical_maximum - field->logical_minimum);
1822-
wacom_wac->hid_data.battery_capacity = value;
1823-
wacom_wac->hid_data.bat_connected = 1;
1824-
wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1825-
break;
1826-
1827-
case WACOM_HID_WD_BATTERY_CHARGING:
1828-
wacom_wac->hid_data.bat_charging = value;
1829-
wacom_wac->hid_data.ps_connected = value;
1830-
wacom_wac->hid_data.bat_connected = 1;
1831-
break;
1832-
}
1833-
}
1834-
18351884
static void wacom_wac_pad_event(struct hid_device *hdev, struct hid_field *field,
18361885
struct hid_usage *usage, __s32 value)
18371886
{
@@ -1905,25 +1954,6 @@ static void wacom_wac_pad_pre_report(struct hid_device *hdev,
19051954
wacom_wac->hid_data.inrange_state = 0;
19061955
}
19071956

1908-
static void wacom_wac_pad_battery_report(struct hid_device *hdev,
1909-
struct hid_report *report)
1910-
{
1911-
struct wacom *wacom = hid_get_drvdata(hdev);
1912-
struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1913-
struct wacom_features *features = &wacom_wac->features;
1914-
1915-
if (features->quirks & WACOM_QUIRK_BATTERY) {
1916-
int status = wacom_wac->hid_data.bat_status;
1917-
int capacity = wacom_wac->hid_data.battery_capacity;
1918-
bool charging = wacom_wac->hid_data.bat_charging;
1919-
bool connected = wacom_wac->hid_data.bat_connected;
1920-
bool powered = wacom_wac->hid_data.ps_connected;
1921-
1922-
wacom_notify_battery(wacom_wac, status, capacity,
1923-
charging, connected, powered);
1924-
}
1925-
}
1926-
19271957
static void wacom_wac_pad_report(struct hid_device *hdev,
19281958
struct hid_report *report)
19291959
{
@@ -1969,9 +1999,6 @@ static void wacom_wac_pen_usage_mapping(struct hid_device *hdev,
19691999
case HID_DG_INRANGE:
19702000
wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
19712001
break;
1972-
case HID_DG_BATTERYSTRENGTH:
1973-
features->quirks |= WACOM_QUIRK_BATTERY;
1974-
break;
19752002
case HID_DG_INVERT:
19762003
wacom_map_usage(input, usage, field, EV_KEY,
19772004
BTN_TOOL_RUBBER, 0);
@@ -2044,17 +2071,6 @@ static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field
20442071
if (!(features->quirks & WACOM_QUIRK_SENSE))
20452072
wacom_wac->hid_data.sense_state = value;
20462073
return;
2047-
case HID_DG_BATTERYSTRENGTH:
2048-
if (value == 0) {
2049-
wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
2050-
}
2051-
else {
2052-
value = value * 100 / (field->logical_maximum - field->logical_minimum);
2053-
wacom_wac->hid_data.battery_capacity = value;
2054-
wacom_wac->hid_data.bat_connected = 1;
2055-
wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
2056-
}
2057-
break;
20582074
case HID_DG_INVERT:
20592075
wacom_wac->hid_data.invert_state = value;
20602076
return;
@@ -2190,8 +2206,6 @@ static void wacom_wac_pen_report(struct hid_device *hdev,
21902206
input_sync(input);
21912207
}
21922208

2193-
wacom_wac_pad_battery_report(hdev, report);
2194-
21952209
if (!prox) {
21962210
wacom_wac->tool[0] = 0;
21972211
wacom_wac->id[0] = 0;
@@ -2413,7 +2427,10 @@ void wacom_wac_usage_mapping(struct hid_device *hdev,
24132427
if (WACOM_DIRECT_DEVICE(field))
24142428
features->device_type |= WACOM_DEVICETYPE_DIRECT;
24152429

2416-
if (WACOM_PAD_FIELD(field))
2430+
/* usage tests must precede field tests */
2431+
if (WACOM_BATTERY_USAGE(usage))
2432+
wacom_wac_battery_usage_mapping(hdev, field, usage);
2433+
else if (WACOM_PAD_FIELD(field))
24172434
wacom_wac_pad_usage_mapping(hdev, field, usage);
24182435
else if (WACOM_PEN_FIELD(field))
24192436
wacom_wac_pen_usage_mapping(hdev, field, usage);
@@ -2432,11 +2449,12 @@ void wacom_wac_event(struct hid_device *hdev, struct hid_field *field,
24322449
if (value > field->logical_maximum || value < field->logical_minimum)
24332450
return;
24342451

2435-
if (WACOM_PAD_FIELD(field)) {
2436-
wacom_wac_pad_battery_event(hdev, field, usage, value);
2437-
if (wacom->wacom_wac.pad_input)
2438-
wacom_wac_pad_event(hdev, field, usage, value);
2439-
} else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2452+
/* usage tests must precede field tests */
2453+
if (WACOM_BATTERY_USAGE(usage))
2454+
wacom_wac_battery_event(hdev, field, usage, value);
2455+
else if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2456+
wacom_wac_pad_event(hdev, field, usage, value);
2457+
else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
24402458
wacom_wac_pen_event(hdev, field, usage, value);
24412459
else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
24422460
wacom_wac_finger_event(hdev, field, usage, value);
@@ -2470,6 +2488,8 @@ void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
24702488
if (wacom_wac->features.type != HID_GENERIC)
24712489
return;
24722490

2491+
wacom_wac_battery_pre_report(hdev, report);
2492+
24732493
if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
24742494
wacom_wac_pad_pre_report(hdev, report);
24752495
else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
@@ -2489,11 +2509,11 @@ void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
24892509
if (report->type != HID_INPUT_REPORT)
24902510
return;
24912511

2492-
if (WACOM_PAD_FIELD(field)) {
2493-
wacom_wac_pad_battery_report(hdev, report);
2494-
if (wacom->wacom_wac.pad_input)
2495-
wacom_wac_pad_report(hdev, report);
2496-
} else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2512+
wacom_wac_battery_report(hdev, report);
2513+
2514+
if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2515+
wacom_wac_pad_report(hdev, report);
2516+
else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
24972517
wacom_wac_pen_report(hdev, report);
24982518
else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
24992519
wacom_wac_finger_report(hdev, report);

drivers/hid/wacom_wac.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@
153153
#define WACOM_HID_WT_X (WACOM_HID_UP_WACOMTOUCH | 0x130)
154154
#define WACOM_HID_WT_Y (WACOM_HID_UP_WACOMTOUCH | 0x131)
155155

156+
#define WACOM_BATTERY_USAGE(f) (((f)->hid == HID_DG_BATTERYSTRENGTH) || \
157+
((f)->hid == WACOM_HID_WD_BATTERY_CHARGING) || \
158+
((f)->hid == WACOM_HID_WD_BATTERY_LEVEL))
159+
156160
#define WACOM_PAD_FIELD(f) (((f)->physical == HID_DG_TABLETFUNCTIONKEY) || \
157161
((f)->physical == WACOM_HID_WD_DIGITIZERFNKEYS) || \
158162
((f)->physical == WACOM_HID_WD_DIGITIZERINFO))

0 commit comments

Comments
 (0)