Skip to content

Commit dbaaf50

Browse files
committed
HID: uclogic: Handle wireless device reconnection
UGEEv2 tablets with battery can be connected using a USB cable or a USB Bluetooth dongle. When the Bluetooth dongle is used, the connection to that tablet can be lost because the tablet is out of the range of the receiver or because it was switched off using the switch placed in the back of the tablet's frame. After losing connection, the tablet is able to reconnect automatically and its firmware sends a special packet indicating that the device was reconnected. In response to this packet, the tablet needs to receive the same array of magic data it expects on probe to enable its interfaces. This patch implements a generic mechanism to hook raw events and schedule a work to perform any custom action. Tested-by: Mia Kanashi <chad@redpilled.dev> Tested-by: Andreas Grosse <andig.mail@t-online.de> Signed-off-by: José Expósito <jose.exposito89@gmail.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 7fa5d1e commit dbaaf50

5 files changed

Lines changed: 271 additions & 0 deletions

File tree

hid-uclogic-core-test.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
3+
/*
4+
* HID driver for UC-Logic devices not fully compliant with HID standard
5+
*
6+
* Copyright (c) 2022 José Expósito <jose.exposito89@gmail.com>
7+
*/
8+
9+
#include <kunit/test.h>
10+
#include "./hid-uclogic-params.h"
11+
12+
#define MAX_EVENT_SIZE 12
13+
14+
struct uclogic_raw_event_hook_test {
15+
u8 event[MAX_EVENT_SIZE];
16+
size_t size;
17+
bool expected;
18+
};
19+
20+
static struct uclogic_raw_event_hook_test hook_events[] = {
21+
{
22+
.event = { 0xA1, 0xB2, 0xC3, 0xD4 },
23+
.size = 4,
24+
},
25+
{
26+
.event = { 0x1F, 0x2E, 0x3D, 0x4C, 0x5B, 0x6A },
27+
.size = 6,
28+
},
29+
};
30+
31+
static struct uclogic_raw_event_hook_test test_events[] = {
32+
{
33+
.event = { 0xA1, 0xB2, 0xC3, 0xD4 },
34+
.size = 4,
35+
.expected = true,
36+
},
37+
{
38+
.event = { 0x1F, 0x2E, 0x3D, 0x4C, 0x5B, 0x6A },
39+
.size = 6,
40+
.expected = true,
41+
},
42+
{
43+
.event = { 0xA1, 0xB2, 0xC3 },
44+
.size = 3,
45+
.expected = false,
46+
},
47+
{
48+
.event = { 0xA1, 0xB2, 0xC3, 0xD4, 0x00 },
49+
.size = 5,
50+
.expected = false,
51+
},
52+
{
53+
.event = { 0x2E, 0x3D, 0x4C, 0x5B, 0x6A, 0x1F },
54+
.size = 6,
55+
.expected = false,
56+
},
57+
};
58+
59+
static void hid_test_uclogic_exec_event_hook_test(struct kunit *test)
60+
{
61+
struct uclogic_params p = {0, };
62+
struct uclogic_raw_event_hook *filter;
63+
bool res;
64+
int n;
65+
66+
/* Initialize the list of events to hook */
67+
p.event_hooks = kunit_kzalloc(test, sizeof(*p.event_hooks), GFP_KERNEL);
68+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p.event_hooks);
69+
INIT_LIST_HEAD(&p.event_hooks->list);
70+
71+
for (n = 0; n < ARRAY_SIZE(hook_events); n++) {
72+
filter = kunit_kzalloc(test, sizeof(*filter), GFP_KERNEL);
73+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filter);
74+
75+
filter->size = hook_events[n].size;
76+
filter->event = kunit_kzalloc(test, filter->size, GFP_KERNEL);
77+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filter->event);
78+
memcpy(filter->event, &hook_events[n].event[0], filter->size);
79+
80+
list_add_tail(&filter->list, &p.event_hooks->list);
81+
}
82+
83+
/* Test uclogic_exec_event_hook() */
84+
for (n = 0; n < ARRAY_SIZE(test_events); n++) {
85+
res = uclogic_exec_event_hook(&p, &test_events[n].event[0],
86+
test_events[n].size);
87+
KUNIT_ASSERT_EQ(test, res, test_events[n].expected);
88+
}
89+
}
90+
91+
static struct kunit_case hid_uclogic_core_test_cases[] = {
92+
KUNIT_CASE(hid_test_uclogic_exec_event_hook_test),
93+
{}
94+
};
95+
96+
static struct kunit_suite hid_uclogic_core_test_suite = {
97+
.name = "hid_uclogic_core_test",
98+
.test_cases = hid_uclogic_core_test_cases,
99+
};
100+
101+
kunit_test_suite(hid_uclogic_core_test_suite);
102+
103+
MODULE_DESCRIPTION("KUnit tests for the UC-Logic driver");
104+
MODULE_LICENSE("GPL");
105+
MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");

hid-uclogic-core.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,34 @@ static int uclogic_resume(struct hid_device *hdev)
260260
}
261261
#endif
262262

263+
/**
264+
* uclogic_exec_event_hook - if the received event is hooked schedules the
265+
* associated work.
266+
*
267+
* @p: Tablet interface report parameters.
268+
* @event: Raw event.
269+
* @size: The size of event.
270+
*
271+
* Returns:
272+
* Whether the event was hooked or not.
273+
*/
274+
static bool uclogic_exec_event_hook(struct uclogic_params *p, u8 *event, int size)
275+
{
276+
struct uclogic_raw_event_hook *curr;
277+
278+
if (!p->event_hooks)
279+
return false;
280+
281+
list_for_each_entry(curr, &p->event_hooks->list, list) {
282+
if (curr->size == size && memcmp(curr->event, event, size) == 0) {
283+
schedule_work(&curr->work);
284+
return true;
285+
}
286+
}
287+
288+
return false;
289+
}
290+
263291
/**
264292
* uclogic_raw_event_pen - handle raw pen events (pen HID reports).
265293
*
@@ -418,6 +446,9 @@ static int uclogic_raw_event(struct hid_device *hdev,
418446
if (report->type != HID_INPUT_REPORT)
419447
return 0;
420448

449+
if (uclogic_exec_event_hook(params, data, size))
450+
return 0;
451+
421452
while (true) {
422453
/* Tweak pen reports, if necessary */
423454
if ((report_id == params->pen.id) && (size >= 2)) {
@@ -550,3 +581,7 @@ MODULE_AUTHOR("Martin Rusko");
550581
MODULE_AUTHOR("Nikolai Kondrashov");
551582
MODULE_LICENSE("GPL");
552583
MODULE_VERSION("11");
584+
585+
#ifdef CONFIG_HID_KUNIT_TEST
586+
#include "hid-uclogic-core-test.c"
587+
#endif

hid-uclogic-params-test.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,25 @@ static void hid_test_uclogic_parse_ugee_v2_desc(struct kunit *test)
174174
KUNIT_EXPECT_EQ(test, params->frame_type, frame_type);
175175
}
176176

177+
static void hid_test_uclogic_params_cleanup_event_hooks(struct kunit *test)
178+
{
179+
int res, n;
180+
struct uclogic_params p = {0, };
181+
182+
res = uclogic_params_ugee_v2_init_event_hooks(NULL, &p);
183+
KUNIT_ASSERT_EQ(test, res, 0);
184+
185+
/* Check that the function can be called repeatedly */
186+
for (n = 0; n < 4; n++) {
187+
uclogic_params_cleanup_event_hooks(&p);
188+
KUNIT_EXPECT_PTR_EQ(test, p.event_hooks, NULL);
189+
}
190+
}
191+
177192
static struct kunit_case hid_uclogic_params_test_cases[] = {
178193
KUNIT_CASE_PARAM(hid_test_uclogic_parse_ugee_v2_desc,
179194
uclogic_parse_ugee_v2_desc_gen_params),
195+
KUNIT_CASE(hid_test_uclogic_params_cleanup_event_hooks),
180196
{}
181197
};
182198

hid-uclogic-params.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,31 @@ static int uclogic_params_frame_init_v1(struct uclogic_params_frame *frame,
613613
return rc;
614614
}
615615

616+
/**
617+
* uclogic_params_cleanup_event_hooks - free resources used by the list of raw
618+
* event hooks.
619+
* Can be called repeatedly.
620+
*
621+
* @params: Input parameters to cleanup. Cannot be NULL.
622+
*/
623+
static void uclogic_params_cleanup_event_hooks(struct uclogic_params *params)
624+
{
625+
struct uclogic_raw_event_hook *curr, *n;
626+
627+
if (!params || !params->event_hooks)
628+
return;
629+
630+
list_for_each_entry_safe(curr, n, &params->event_hooks->list, list) {
631+
cancel_work_sync(&curr->work);
632+
list_del(&curr->list);
633+
kfree(curr->event);
634+
kfree(curr);
635+
}
636+
637+
kfree(params->event_hooks);
638+
params->event_hooks = NULL;
639+
}
640+
616641
/**
617642
* uclogic_params_cleanup - free resources used by struct uclogic_params
618643
* (tablet interface's parameters).
@@ -629,6 +654,7 @@ void uclogic_params_cleanup(struct uclogic_params *params)
629654
for (i = 0; i < ARRAY_SIZE(params->frame_list); i++)
630655
uclogic_params_frame_cleanup(&params->frame_list[i]);
631656

657+
uclogic_params_cleanup_event_hooks(params);
632658
memset(params, 0, sizeof(*params));
633659
}
634660
}
@@ -1278,6 +1304,72 @@ static int uclogic_params_ugee_v2_init_battery(struct hid_device *hdev,
12781304
return rc;
12791305
}
12801306

1307+
/**
1308+
* uclogic_params_ugee_v2_reconnect_work() - When a wireless tablet looses
1309+
* connection to the USB dongle and reconnects, either because of its physical
1310+
* distance or because it was switches off and on using the frame's switch,
1311+
* uclogic_probe_interface() needs to be called again to enable the tablet.
1312+
*
1313+
* @work: The work that triggered this function.
1314+
*/
1315+
static void uclogic_params_ugee_v2_reconnect_work(struct work_struct *work)
1316+
{
1317+
struct uclogic_raw_event_hook *event_hook;
1318+
1319+
event_hook = container_of(work, struct uclogic_raw_event_hook, work);
1320+
uclogic_probe_interface(event_hook->hdev, uclogic_ugee_v2_probe_arr,
1321+
uclogic_ugee_v2_probe_size,
1322+
uclogic_ugee_v2_probe_endpoint);
1323+
}
1324+
1325+
/**
1326+
* uclogic_params_ugee_v2_init_event_hooks() - initialize the list of events
1327+
* to be hooked for UGEE v2 devices.
1328+
* @hdev: The HID device of the tablet interface to initialize and get
1329+
* parameters from.
1330+
* @p: Parameters to fill in, cannot be NULL.
1331+
*
1332+
* Returns:
1333+
* Zero, if successful. A negative errno code on error.
1334+
*/
1335+
static int uclogic_params_ugee_v2_init_event_hooks(struct hid_device *hdev,
1336+
struct uclogic_params *p)
1337+
{
1338+
struct uclogic_raw_event_hook *event_hook;
1339+
__u8 reconnect_event[] = {
1340+
/* Event received on wireless tablet reconnection */
1341+
0x02, 0xF8, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1342+
};
1343+
1344+
if (!p)
1345+
return -EINVAL;
1346+
1347+
/* The reconnection event is only received if the tablet has battery */
1348+
if (!uclogic_params_ugee_v2_has_battery(hdev))
1349+
return 0;
1350+
1351+
p->event_hooks = kzalloc(sizeof(*p->event_hooks), GFP_KERNEL);
1352+
if (!p->event_hooks)
1353+
return -ENOMEM;
1354+
1355+
INIT_LIST_HEAD(&p->event_hooks->list);
1356+
1357+
event_hook = kzalloc(sizeof(*event_hook), GFP_KERNEL);
1358+
if (!event_hook)
1359+
return -ENOMEM;
1360+
1361+
INIT_WORK(&event_hook->work, uclogic_params_ugee_v2_reconnect_work);
1362+
event_hook->hdev = hdev;
1363+
event_hook->size = ARRAY_SIZE(reconnect_event);
1364+
event_hook->event = kmemdup(reconnect_event, event_hook->size, GFP_KERNEL);
1365+
if (!event_hook->event)
1366+
return -ENOMEM;
1367+
1368+
list_add_tail(&event_hook->list, &p->event_hooks->list);
1369+
1370+
return 0;
1371+
}
1372+
12811373
/**
12821374
* uclogic_params_ugee_v2_init() - initialize a UGEE graphics tablets by
12831375
* discovering their parameters.
@@ -1414,6 +1506,13 @@ static int uclogic_params_ugee_v2_init(struct uclogic_params *params,
14141506
}
14151507
}
14161508

1509+
/* Create a list of raw events to be ignored */
1510+
rc = uclogic_params_ugee_v2_init_event_hooks(hdev, &p);
1511+
if (rc) {
1512+
hid_err(hdev, "error initializing event hook list: %d\n", rc);
1513+
goto cleanup;
1514+
}
1515+
14171516
output:
14181517
/* Output parameters */
14191518
memcpy(params, &p, sizeof(*params));

hid-uclogic-params.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <linux/usb.h>
2020
#include <linux/hid.h>
21+
#include <linux/list.h>
2122

2223
#define UCLOGIC_MOUSE_FRAME_QUIRK BIT(0)
2324
#define UCLOGIC_BATTERY_QUIRK BIT(1)
@@ -176,6 +177,17 @@ struct uclogic_params_frame {
176177
unsigned int bitmap_dial_byte;
177178
};
178179

180+
/*
181+
* List of works to be performed when a certain raw event is received.
182+
*/
183+
struct uclogic_raw_event_hook {
184+
struct hid_device *hdev;
185+
__u8 *event;
186+
size_t size;
187+
struct work_struct work;
188+
struct list_head list;
189+
};
190+
179191
/*
180192
* Tablet interface report parameters.
181193
*
@@ -216,6 +228,10 @@ struct uclogic_params {
216228
* parts. Only valid, if "invalid" is false.
217229
*/
218230
struct uclogic_params_frame frame_list[3];
231+
/*
232+
* List of event hooks.
233+
*/
234+
struct uclogic_raw_event_hook *event_hooks;
219235
};
220236

221237
/* Driver data */

0 commit comments

Comments
 (0)