xref: /freebsd/sys/dev/hid/hidquirk.c (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
5  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
6  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7  * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49 
50 #include <dev/evdev/input.h>
51 
52 #define	HID_DEBUG_VAR hid_debug
53 #include <dev/hid/hid.h>
54 #include <dev/hid/hidquirk.h>
55 #include "usbdevs.h"
56 
57 
58 MODULE_DEPEND(hidquirk, hid, 1, 1, 1);
59 MODULE_VERSION(hidquirk, 1);
60 
61 #define	HID_DEV_QUIRKS_MAX 384
62 #define	HID_SUB_QUIRKS_MAX 8
63 #define	HID_QUIRK_ENVROOT "hw.hid.quirk."
64 
65 struct hidquirk_entry {
66 	uint16_t bus;
67 	uint16_t vid;
68 	uint16_t pid;
69 	uint16_t lo_rev;
70 	uint16_t hi_rev;
71 	uint16_t quirks[HID_SUB_QUIRKS_MAX];
72 };
73 
74 static struct mtx hidquirk_mtx;
75 
76 #define	HID_QUIRK_VP(b,v,p,l,h,...) \
77   { .bus = (b), .vid = (v), .pid = (p), .lo_rev = (l), .hi_rev = (h), \
78     .quirks = { __VA_ARGS__ } }
79 #define	USB_QUIRK(v,p,l,h,...) \
80   HID_QUIRK_VP(BUS_USB, USB_VENDOR_##v, USB_PRODUCT_##v##_##p, l, h, __VA_ARGS__)
81 
82 static struct hidquirk_entry hidquirks[HID_DEV_QUIRKS_MAX] = {
83 	USB_QUIRK(ASUS, LCM, 0x0000, 0xffff, HQ_HID_IGNORE),
84 	USB_QUIRK(QTRONIX, 980N, 0x110, 0x110, HQ_SPUR_BUT_UP),
85 	USB_QUIRK(ALCOR2, KBD_HUB, 0x001, 0x001, HQ_SPUR_BUT_UP),
86 	USB_QUIRK(LOGITECH, G510S, 0x0000, 0xFFFF, HQ_KBD_BOOTPROTO),
87 	/* Devices which should be ignored by usbhid */
88 	USB_QUIRK(APC, UPS, 0x0000, 0xffff, HQ_HID_IGNORE),
89 	USB_QUIRK(BELKIN, F6H375USB, 0x0000, 0xffff, HQ_HID_IGNORE),
90 	USB_QUIRK(BELKIN, F6C550AVR, 0x0000, 0xffff, HQ_HID_IGNORE),
91 	USB_QUIRK(BELKIN, F6C1250TWRK, 0x0000, 0xffff, HQ_HID_IGNORE),
92 	USB_QUIRK(BELKIN, F6C1500TWRK, 0x0000, 0xffff, HQ_HID_IGNORE),
93 	USB_QUIRK(BELKIN, F6C900UNV, 0x0000, 0xffff, HQ_HID_IGNORE),
94 	USB_QUIRK(BELKIN, F6C100UNV, 0x0000, 0xffff, HQ_HID_IGNORE),
95 	USB_QUIRK(BELKIN, F6C120UNV, 0x0000, 0xffff, HQ_HID_IGNORE),
96 	USB_QUIRK(BELKIN, F6C800UNV, 0x0000, 0xffff, HQ_HID_IGNORE),
97 	USB_QUIRK(BELKIN, F6C1100UNV, 0x0000, 0xffff, HQ_HID_IGNORE),
98 	USB_QUIRK(CYBERPOWER, BC900D, 0x0000, 0xffff, HQ_HID_IGNORE),
99 	USB_QUIRK(CYBERPOWER, 1500CAVRLCD, 0x0000, 0xffff, HQ_HID_IGNORE),
100 	USB_QUIRK(CYBERPOWER, OR2200LCDRM2U, 0x0000, 0xffff, HQ_HID_IGNORE),
101 	USB_QUIRK(DELL2, VARIOUS_UPS, 0x0000, 0xffff, HQ_HID_IGNORE),
102 	USB_QUIRK(CYPRESS, SILVERSHIELD, 0x0000, 0xffff, HQ_HID_IGNORE),
103 	USB_QUIRK(DELORME, EARTHMATE, 0x0000, 0xffff, HQ_HID_IGNORE),
104 	USB_QUIRK(DREAMLINK, DL100B, 0x0000, 0xffff, HQ_HID_IGNORE),
105 	USB_QUIRK(MICROCHIP, PICOLCD20X2, 0x0000, 0xffff, HQ_HID_IGNORE),
106 	USB_QUIRK(MICROCHIP, PICOLCD4X20, 0x0000, 0xffff, HQ_HID_IGNORE),
107 	USB_QUIRK(LIEBERT, POWERSURE_PXT, 0x0000, 0xffff, HQ_HID_IGNORE),
108 	USB_QUIRK(LIEBERT2, PSI1000, 0x0000, 0xffff, HQ_HID_IGNORE),
109 	USB_QUIRK(LIEBERT2, POWERSURE_PSA, 0x0000, 0xffff, HQ_HID_IGNORE),
110 	USB_QUIRK(MGE, UPS1, 0x0000, 0xffff, HQ_HID_IGNORE),
111 	USB_QUIRK(MGE, UPS2, 0x0000, 0xffff, HQ_HID_IGNORE),
112 	USB_QUIRK(POWERCOM, IMPERIAL_SERIES, 0x0000, 0xffff, HQ_HID_IGNORE),
113 	USB_QUIRK(POWERCOM, SMART_KING_PRO, 0x0000, 0xffff, HQ_HID_IGNORE),
114 	USB_QUIRK(POWERCOM, WOW, 0x0000, 0xffff, HQ_HID_IGNORE),
115 	USB_QUIRK(POWERCOM, VANGUARD, 0x0000, 0xffff, HQ_HID_IGNORE),
116 	USB_QUIRK(POWERCOM, BLACK_KNIGHT_PRO, 0x0000, 0xffff, HQ_HID_IGNORE),
117 	USB_QUIRK(TRIPPLITE2, AVR550U, 0x0000, 0xffff, HQ_HID_IGNORE),
118 	USB_QUIRK(TRIPPLITE2, AVR750U, 0x0000, 0xffff, HQ_HID_IGNORE),
119 	USB_QUIRK(TRIPPLITE2, ECO550UPS, 0x0000, 0xffff, HQ_HID_IGNORE),
120 	USB_QUIRK(TRIPPLITE2, T750_INTL, 0x0000, 0xffff, HQ_HID_IGNORE),
121 	USB_QUIRK(TRIPPLITE2, RT_2200_INTL, 0x0000, 0xffff, HQ_HID_IGNORE),
122 	USB_QUIRK(TRIPPLITE2, OMNI1000LCD, 0x0000, 0xffff, HQ_HID_IGNORE),
123 	USB_QUIRK(TRIPPLITE2, OMNI900LCD, 0x0000, 0xffff, HQ_HID_IGNORE),
124 	USB_QUIRK(TRIPPLITE2, SMART_2200RMXL2U, 0x0000, 0xffff, HQ_HID_IGNORE),
125 	USB_QUIRK(TRIPPLITE2, UPS_3014, 0x0000, 0xffff, HQ_HID_IGNORE),
126 	USB_QUIRK(TRIPPLITE2, SU1500RTXL2UA, 0x0000, 0xffff, HQ_HID_IGNORE),
127 	USB_QUIRK(TRIPPLITE2, SU6000RT4U, 0x0000, 0xffff, HQ_HID_IGNORE),
128 	USB_QUIRK(TRIPPLITE2, SU1500RTXL2UA_2, 0x0000, 0xffff, HQ_HID_IGNORE),
129 	USB_QUIRK(APPLE, IPHONE, 0x0000, 0xffff, HQ_HID_IGNORE),
130 	USB_QUIRK(APPLE, IPHONE_3G, 0x0000, 0xffff, HQ_HID_IGNORE),
131 	USB_QUIRK(MEGATEC, UPS, 0x0000, 0xffff, HQ_HID_IGNORE),
132 	/* Devices which should be ignored by both ukbd and uhid */
133 	USB_QUIRK(CYPRESS, WISPY1A, 0x0000, 0xffff, HQ_HID_IGNORE),
134 	USB_QUIRK(METAGEEK, WISPY1B, 0x0000, 0xffff, HQ_HID_IGNORE),
135 	USB_QUIRK(METAGEEK, WISPY24X, 0x0000, 0xffff, HQ_HID_IGNORE),
136 	USB_QUIRK(METAGEEK2, WISPYDBX, 0x0000, 0xffff, HQ_HID_IGNORE),
137 	/* MS keyboards do weird things */
138 	USB_QUIRK(MICROSOFT, NATURAL4000, 0x0000, 0xFFFF, HQ_KBD_BOOTPROTO),
139 	USB_QUIRK(MICROSOFT, WLINTELLIMOUSE, 0x0000, 0xffff, HQ_MS_LEADING_BYTE),
140 	/* Quirk for Corsair Vengeance K60 keyboard */
141 	USB_QUIRK(CORSAIR, K60, 0x0000, 0xffff, HQ_KBD_BOOTPROTO),
142 	/* Quirk for Corsair Gaming K68 keyboard */
143 	USB_QUIRK(CORSAIR, K68, 0x0000, 0xffff, HQ_KBD_BOOTPROTO),
144 	/* Quirk for Corsair Vengeance K70 keyboard */
145 	USB_QUIRK(CORSAIR, K70, 0x0000, 0xffff, HQ_KBD_BOOTPROTO),
146 	/* Quirk for Corsair K70 RGB keyboard */
147 	USB_QUIRK(CORSAIR, K70_RGB, 0x0000, 0xffff, HQ_KBD_BOOTPROTO),
148 	/* Quirk for Corsair STRAFE Gaming keyboard */
149 	USB_QUIRK(CORSAIR, STRAFE, 0x0000, 0xffff, HQ_KBD_BOOTPROTO),
150 	USB_QUIRK(CORSAIR, STRAFE2, 0x0000, 0xffff, HQ_KBD_BOOTPROTO),
151 	/* Holtek USB gaming keyboard */
152 	USB_QUIRK(HOLTEK, F85, 0x0000, 0xffff, HQ_KBD_BOOTPROTO),
153 	/* Quirk for Kensington Slimblade Trackball */
154 	USB_QUIRK(KENSINGTON, SLIMBLADE, 0x0000, 0xffff, HQ_MS_VENDOR_BTN),
155 };
156 #undef HID_QUIRK_VP
157 #undef USB_QUIRK
158 
159 /* hidquirk.h exposes only HID_QUIRK_LIST macro when HQ() is defined */
160 #define	HQ(x)	[HQ_##x] = "HQ_"#x
161 #include "hidquirk.h"
162 static const char *hidquirk_str[HID_QUIRK_MAX] = { HID_QUIRK_LIST() };
163 #undef HQ
164 
165 static hid_test_quirk_t hid_test_quirk_by_info;
166 
167 /*------------------------------------------------------------------------*
168  *	hidquirkstr
169  *
170  * This function converts an USB quirk code into a string.
171  *------------------------------------------------------------------------*/
172 static const char *
173 hidquirkstr(uint16_t quirk)
174 {
175 	return ((quirk < HID_QUIRK_MAX && hidquirk_str[quirk] != NULL) ?
176 	    hidquirk_str[quirk] : "HQ_UNKNOWN");
177 }
178 
179 /*------------------------------------------------------------------------*
180  *	hid_strquirk
181  *
182  * This function converts a string into a HID quirk code.
183  *
184  * Returns:
185  * Less than HID_QUIRK_MAX: Quirk code
186  * Else: Quirk code not found
187  *------------------------------------------------------------------------*/
188 static uint16_t
189 hid_strquirk(const char *str, size_t len)
190 {
191 	const char *quirk;
192 	uint16_t x;
193 
194 	for (x = 0; x != HID_QUIRK_MAX; x++) {
195 		quirk = hidquirkstr(x);
196 		if (strncmp(str, quirk, len) == 0 &&
197 		    quirk[len] == 0)
198 			break;
199 	}
200 	return (x);
201 }
202 
203 /*------------------------------------------------------------------------*
204  *	hid_test_quirk_by_info
205  *
206  * Returns:
207  * false: Quirk not found
208  * true: Quirk found
209  *------------------------------------------------------------------------*/
210 bool
211 hid_test_quirk_by_info(const struct hid_device_info *info, uint16_t quirk)
212 {
213 	uint16_t x;
214 	uint16_t y;
215 
216 	if (quirk == HQ_NONE)
217 		goto done;
218 
219 	mtx_lock(&hidquirk_mtx);
220 
221 	for (x = 0; x != HID_DEV_QUIRKS_MAX; x++) {
222 		/* see if quirk information does not match */
223 		if ((hidquirks[x].bus != info->idBus) ||
224 		    (hidquirks[x].vid != info->idVendor) ||
225 		    (hidquirks[x].lo_rev > info->idVersion) ||
226 		    (hidquirks[x].hi_rev < info->idVersion)) {
227 			continue;
228 		}
229 		/* see if quirk only should match vendor ID */
230 		if (hidquirks[x].pid != info->idProduct) {
231 			if (hidquirks[x].pid != 0)
232 				continue;
233 
234 			for (y = 0; y != HID_SUB_QUIRKS_MAX; y++) {
235 				if (hidquirks[x].quirks[y] == HQ_MATCH_VENDOR_ONLY)
236 					break;
237 			}
238 			if (y == HID_SUB_QUIRKS_MAX)
239 				continue;
240 		}
241 		/* lookup quirk */
242 		for (y = 0; y != HID_SUB_QUIRKS_MAX; y++) {
243 			if (hidquirks[x].quirks[y] == quirk) {
244 				mtx_unlock(&hidquirk_mtx);
245 				DPRINTF("Found quirk '%s'.\n", hidquirkstr(quirk));
246 				return (true);
247 			}
248 		}
249 	}
250 	mtx_unlock(&hidquirk_mtx);
251 done:
252 	return (false);			/* no quirk match */
253 }
254 
255 static struct hidquirk_entry *
256 hidquirk_get_entry(uint16_t bus, uint16_t vid, uint16_t pid,
257     uint16_t lo_rev, uint16_t hi_rev, uint8_t do_alloc)
258 {
259 	uint16_t x;
260 
261 	mtx_assert(&hidquirk_mtx, MA_OWNED);
262 
263 	if ((bus | vid | pid | lo_rev | hi_rev) == 0) {
264 		/* all zero - special case */
265 		return (hidquirks + HID_DEV_QUIRKS_MAX - 1);
266 	}
267 	/* search for an existing entry */
268 	for (x = 0; x != HID_DEV_QUIRKS_MAX; x++) {
269 		/* see if quirk information does not match */
270 		if ((hidquirks[x].bus != bus) ||
271 		    (hidquirks[x].vid != vid) ||
272 		    (hidquirks[x].pid != pid) ||
273 		    (hidquirks[x].lo_rev != lo_rev) ||
274 		    (hidquirks[x].hi_rev != hi_rev)) {
275 			continue;
276 		}
277 		return (hidquirks + x);
278 	}
279 
280 	if (do_alloc == 0) {
281 		/* no match */
282 		return (NULL);
283 	}
284 	/* search for a free entry */
285 	for (x = 0; x != HID_DEV_QUIRKS_MAX; x++) {
286 		/* see if quirk information does not match */
287 		if ((hidquirks[x].bus |
288 		    hidquirks[x].vid |
289 		    hidquirks[x].pid |
290 		    hidquirks[x].lo_rev |
291 		    hidquirks[x].hi_rev) != 0) {
292 			continue;
293 		}
294 		hidquirks[x].bus = bus;
295 		hidquirks[x].vid = vid;
296 		hidquirks[x].pid = pid;
297 		hidquirks[x].lo_rev = lo_rev;
298 		hidquirks[x].hi_rev = hi_rev;
299 
300 		return (hidquirks + x);
301 	}
302 
303 	/* no entry found */
304 	return (NULL);
305 }
306 
307 /*------------------------------------------------------------------------*
308  *	usb_quirk_strtou16
309  *
310  * Helper function to scan a 16-bit integer.
311  *------------------------------------------------------------------------*/
312 static uint16_t
313 hidquirk_strtou16(const char **pptr, const char *name, const char *what)
314 {
315 	unsigned long value;
316 	char *end;
317 
318 	value = strtoul(*pptr, &end, 0);
319 	if (value > 65535 || *pptr == end || (*end != ' ' && *end != '\t')) {
320 		printf("%s: %s 16-bit %s value set to zero\n",
321 		    name, what, *end == 0 ? "incomplete" : "invalid");
322 		return (0);
323 	}
324 	*pptr = end + 1;
325 	return ((uint16_t)value);
326 }
327 
328 /*------------------------------------------------------------------------*
329  *	usb_quirk_add_entry_from_str
330  *
331  * Add a USB quirk entry from string.
332  *     "VENDOR PRODUCT LO_REV HI_REV QUIRK[,QUIRK[,...]]"
333  *------------------------------------------------------------------------*/
334 static void
335 hidquirk_add_entry_from_str(const char *name, const char *env)
336 {
337 	struct hidquirk_entry entry = { };
338 	struct hidquirk_entry *new;
339 	uint16_t quirk_idx;
340 	uint16_t quirk;
341 	const char *end;
342 
343 	/* check for invalid environment variable */
344 	if (name == NULL || env == NULL)
345 		return;
346 
347 	if (bootverbose)
348 		printf("Adding HID QUIRK '%s' = '%s'\n", name, env);
349 
350 	/* parse device information */
351 	entry.bus = hidquirk_strtou16(&env, name, "Bus ID");
352 	entry.vid = hidquirk_strtou16(&env, name, "Vendor ID");
353 	entry.pid = hidquirk_strtou16(&env, name, "Product ID");
354 	entry.lo_rev = hidquirk_strtou16(&env, name, "Low revision");
355 	entry.hi_rev = hidquirk_strtou16(&env, name, "High revision");
356 
357 	/* parse quirk information */
358 	quirk_idx = 0;
359 	while (*env != 0 && quirk_idx != HID_SUB_QUIRKS_MAX) {
360 		/* skip whitespace before quirks */
361 		while (*env == ' ' || *env == '\t')
362 			env++;
363 
364 		/* look for quirk separation character */
365 		end = strchr(env, ',');
366 		if (end == NULL)
367 			end = env + strlen(env);
368 
369 		/* lookup quirk in string table */
370 		quirk = hid_strquirk(env, end - env);
371 		if (quirk < HID_QUIRK_MAX) {
372 			entry.quirks[quirk_idx++] = quirk;
373 		} else {
374 			printf("%s: unknown HID quirk '%.*s' (skipped)\n",
375 			    name, (int)(end - env), env);
376 		}
377 		env = end;
378 
379 		/* skip quirk delimiter, if any */
380 		if (*env != 0)
381 			env++;
382 	}
383 
384 	/* register quirk */
385 	if (quirk_idx != 0) {
386 		if (*env != 0) {
387 			printf("%s: Too many HID quirks, only %d allowed!\n",
388 			    name, HID_SUB_QUIRKS_MAX);
389 		}
390 		mtx_lock(&hidquirk_mtx);
391 		new = hidquirk_get_entry(entry.bus, entry.vid, entry.pid,
392 		    entry.lo_rev, entry.hi_rev, 1);
393 		if (new == NULL)
394 			printf("%s: HID quirks table is full!\n", name);
395 		else
396 			memcpy(new->quirks, entry.quirks, sizeof(entry.quirks));
397 		mtx_unlock(&hidquirk_mtx);
398 	} else {
399 		printf("%s: No USB quirks found!\n", name);
400 	}
401 }
402 
403 static void
404 hidquirk_init(void *arg)
405 {
406 	char envkey[sizeof(HID_QUIRK_ENVROOT) + 2];	/* 2 digits max, 0 to 99 */
407 	int i;
408 
409 	/* initialize mutex */
410 	mtx_init(&hidquirk_mtx, "HID quirk", NULL, MTX_DEF);
411 
412 	/* look for quirks defined by the environment variable */
413 	for (i = 0; i != 100; i++) {
414 		snprintf(envkey, sizeof(envkey), HID_QUIRK_ENVROOT "%d", i);
415 
416 		/* Stop at first undefined var */
417 		if (!testenv(envkey))
418 			break;
419 
420 		/* parse environment variable */
421 		hidquirk_add_entry_from_str(envkey, kern_getenv(envkey));
422 	}
423 
424 	/* register our function */
425 	hid_test_quirk_p = &hid_test_quirk_by_info;
426 }
427 
428 static void
429 hidquirk_uninit(void *arg)
430 {
431 	hid_quirk_unload(arg);
432 
433 	/* destroy mutex */
434 	mtx_destroy(&hidquirk_mtx);
435 }
436 
437 SYSINIT(hidquirk_init, SI_SUB_LOCK, SI_ORDER_FIRST, hidquirk_init, NULL);
438 SYSUNINIT(hidquirk_uninit, SI_SUB_LOCK, SI_ORDER_ANY, hidquirk_uninit, NULL);
439