Skip to content

Commit 87562fc

Browse files
bentissJiri Kosina
authored andcommitted
HID: input: remove the need for HID_QUIRK_INVERT
HID_QUIRK_INVERT is kind of complex to deal with and was bogus. Furthermore, it didn't make sense to use a global per struct hid_device quirk for something dynamic as the current state. Store the current tool information in the report itself, and re-order the processing of the fields to enforce having all the tablet "state" fields before getting to In Range and other input fields. This way, we now have all the information whether a tool is present or not while processing In Range. This new behavior enforces that only one tool gets forwarded to userspace at the same time, and that if either eraser or invert is set, we enforce BTN_TOOL_RUBBER. Note that the release of the previous tool now happens in its own EV_SYN report so userspace doesn't get confused by having 2 tools. These changes are tested in the following hid-tools regression tests: https://gitlab.freedesktop.org/libevdev/hid-tools/-/merge_requests/127 Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Reviewed-by: Ping Cheng <ping.cheng@wacom.com> Acked-by: Peter Hutterer <peter.hutterer@who-t.net> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 048cddf commit 87562fc

2 files changed

Lines changed: 95 additions & 9 deletions

File tree

drivers/hid/hid-input.c

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ static const struct {
6363
* This still leaves us 65535 individual priority values.
6464
*/
6565
static const __u32 hidinput_usages_priorities[] = {
66+
HID_DG_ERASER, /* Eraser (eraser touching) must always come before tipswitch */
6667
HID_DG_INVERT, /* Invert must always come before In Range */
67-
HID_DG_INRANGE,
68+
HID_DG_TIPSWITCH, /* Is the tip of the tool touching? */
69+
HID_DG_TIPPRESSURE, /* Tip Pressure might emulate tip switch */
70+
HID_DG_INRANGE, /* In Range needs to come after the other tool states */
6871
};
6972

7073
#define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
@@ -1365,9 +1368,38 @@ static void hidinput_handle_scroll(struct hid_usage *usage,
13651368
input_event(input, EV_REL, usage->code, hi_res);
13661369
}
13671370

1371+
static void hid_report_release_tool(struct hid_report *report, struct input_dev *input,
1372+
unsigned int tool)
1373+
{
1374+
/* if the given tool is not currently reported, ignore */
1375+
if (!test_bit(tool, input->key))
1376+
return;
1377+
1378+
/*
1379+
* if the given tool was previously set, release it,
1380+
* release any TOUCH and send an EV_SYN
1381+
*/
1382+
input_event(input, EV_KEY, BTN_TOUCH, 0);
1383+
input_event(input, EV_KEY, tool, 0);
1384+
input_event(input, EV_SYN, SYN_REPORT, 0);
1385+
1386+
report->tool = 0;
1387+
}
1388+
1389+
static void hid_report_set_tool(struct hid_report *report, struct input_dev *input,
1390+
unsigned int new_tool)
1391+
{
1392+
if (report->tool != new_tool)
1393+
hid_report_release_tool(report, input, report->tool);
1394+
1395+
input_event(input, EV_KEY, new_tool, 1);
1396+
report->tool = new_tool;
1397+
}
1398+
13681399
void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
13691400
{
13701401
struct input_dev *input;
1402+
struct hid_report *report = field->report;
13711403
unsigned *quirks = &hid->quirks;
13721404

13731405
if (!usage->type)
@@ -1418,25 +1450,75 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
14181450
}
14191451

14201452
switch (usage->hid) {
1453+
case HID_DG_ERASER:
1454+
report->tool_active |= !!value;
1455+
1456+
/*
1457+
* if eraser is set, we must enforce BTN_TOOL_RUBBER
1458+
* to accommodate for devices not following the spec.
1459+
*/
1460+
if (value)
1461+
hid_report_set_tool(report, input, BTN_TOOL_RUBBER);
1462+
else if (report->tool != BTN_TOOL_RUBBER)
1463+
/* value is off, tool is not rubber, ignore */
1464+
return;
1465+
1466+
/* let hid-input set BTN_TOUCH */
1467+
break;
1468+
14211469
case HID_DG_INVERT:
1422-
*quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
1470+
report->tool_active |= !!value;
1471+
1472+
/*
1473+
* If invert is set, we store BTN_TOOL_RUBBER.
1474+
*/
1475+
if (value)
1476+
hid_report_set_tool(report, input, BTN_TOOL_RUBBER);
1477+
else if (!report->tool_active)
1478+
/* tool_active not set means Invert and Eraser are not set */
1479+
hid_report_release_tool(report, input, BTN_TOOL_RUBBER);
1480+
1481+
/* no further processing */
14231482
return;
14241483

14251484
case HID_DG_INRANGE:
1426-
if (value) {
1427-
input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
1428-
return;
1485+
report->tool_active |= !!value;
1486+
1487+
if (report->tool_active) {
1488+
/*
1489+
* if tool is not set but is marked as active,
1490+
* assume ours
1491+
*/
1492+
if (!report->tool)
1493+
hid_report_set_tool(report, input, usage->code);
1494+
} else {
1495+
hid_report_release_tool(report, input, usage->code);
14291496
}
1430-
input_event(input, usage->type, usage->code, 0);
1431-
input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
1497+
1498+
/* reset tool_active for the next event */
1499+
report->tool_active = false;
1500+
1501+
/* no further processing */
14321502
return;
14331503

1504+
case HID_DG_TIPSWITCH:
1505+
report->tool_active |= !!value;
1506+
1507+
/* if tool is set to RUBBER we should ignore the current value */
1508+
if (report->tool == BTN_TOOL_RUBBER)
1509+
return;
1510+
1511+
break;
1512+
14341513
case HID_DG_TIPPRESSURE:
14351514
if (*quirks & HID_QUIRK_NOTOUCH) {
14361515
int a = field->logical_minimum;
14371516
int b = field->logical_maximum;
14381517

1439-
input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
1518+
if (value > a + ((b - a) >> 3)) {
1519+
input_event(input, EV_KEY, BTN_TOUCH, 1);
1520+
report->tool_active = true;
1521+
}
14401522
}
14411523
break;
14421524

include/linux/hid.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ struct hid_item {
347347
*/
348348
#define MAX_USBHID_BOOT_QUIRKS 4
349349

350-
#define HID_QUIRK_INVERT BIT(0)
350+
/* BIT(0) reserved for backward compatibility, was HID_QUIRK_INVERT */
351351
#define HID_QUIRK_NOTOUCH BIT(1)
352352
#define HID_QUIRK_IGNORE BIT(2)
353353
#define HID_QUIRK_NOGET BIT(3)
@@ -515,6 +515,10 @@ struct hid_report {
515515
unsigned maxfield; /* maximum valid field index */
516516
unsigned size; /* size of the report (bits) */
517517
struct hid_device *device; /* associated device */
518+
519+
/* tool related state */
520+
bool tool_active; /* whether the current tool is active */
521+
unsigned int tool; /* BTN_TOOL_* */
518522
};
519523

520524
#define HID_MAX_IDS 256

0 commit comments

Comments
 (0)