17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/slab.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
26 #include <asm/unaligned.h>
27 #include <asm/byteorder.h>
28 #include <linux/input.h>
29 #include <linux/wait.h>
31 #include <linux/sched.h>
34 #include <linux/hid.h>
35 #include <linux/hiddev.h>
37 #include <linux/hidraw.h>
45 #define DRIVER_DESC "HID core driver"
46 #define DRIVER_LICENSE "GPL"
53 static int hid_ignore_special_drivers = 0;
101 field = kzalloc((
sizeof(
struct hid_field) +
125 usage = parser->
local.usage[0];
132 if (parser->
device->maxcollection == parser->
device->collection_size) {
135 if (collection ==
NULL) {
136 hid_err(parser->
device,
"failed to reallocate collection array\n");
141 parser->
device->collection_size);
144 parser->
device->collection_size);
146 parser->
device->collection = collection;
147 parser->
device->collection_size *= 2;
151 parser->
device->maxcollection;
153 collection = parser->
device->collection +
154 parser->
device->maxcollection++;
160 parser->
device->maxapplication++;
169 static int close_collection(
struct hid_parser *parser)
184 static unsigned hid_lookup_collection(
struct hid_parser *parser,
unsigned type)
191 if (collection[index].type == type)
201 static int hid_add_usage(
struct hid_parser *parser,
unsigned usage)
208 parser->
local.collection_index[parser->
local.usage_index] =
211 parser->
local.usage_index++;
219 static int hid_add_field(
struct hid_parser *parser,
unsigned report_type,
unsigned flags)
234 if ((parser->
global.logical_minimum < 0 &&
235 parser->
global.logical_maximum <
236 parser->
global.logical_minimum) ||
237 (parser->
global.logical_minimum >= 0 &&
240 dbg_hid(
"logical range invalid 0x%x 0x%x\n",
241 parser->
global.logical_minimum,
242 parser->
global.logical_maximum);
246 offset = report->
size;
249 if (!parser->
local.usage_index)
252 usages =
max_t(
int, parser->
local.usage_index, parser->
global.report_count);
254 field = hid_register_field(report, usages, parser->
global.report_count);
262 for (i = 0; i < usages; i++) {
265 if (i >= parser->
local.usage_index)
266 j = parser->
local.usage_index - 1;
268 field->
usage[
i].collection_index =
269 parser->
local.collection_index[
j];
294 switch (item->
size) {
295 case 1:
return item->
data.
u8;
304 switch (item->
size) {
305 case 1:
return item->
data.
s8;
322 hid_err(parser->
device,
"global environment stack overflow\n");
333 hid_err(parser->
device,
"global environment stack underflow\n");
342 parser->
global.usage_page = item_udata(item);
346 parser->
global.logical_minimum = item_sdata(item);
350 if (parser->
global.logical_minimum < 0)
351 parser->
global.logical_maximum = item_sdata(item);
353 parser->
global.logical_maximum = item_udata(item);
357 parser->
global.physical_minimum = item_sdata(item);
361 if (parser->
global.physical_minimum < 0)
362 parser->
global.physical_maximum = item_sdata(item);
364 parser->
global.physical_maximum = item_udata(item);
368 parser->
global.unit_exponent = item_sdata(item);
372 parser->
global.unit = item_udata(item);
376 parser->
global.report_size = item_udata(item);
377 if (parser->
global.report_size > 128) {
379 parser->
global.report_size);
385 parser->
global.report_count = item_udata(item);
388 parser->
global.report_count);
394 parser->
global.report_id = item_udata(item);
395 if (parser->
global.report_id == 0) {
416 data = item_udata(item);
428 if (parser->
local.delimiter_depth != 0) {
432 parser->
local.delimiter_depth++;
433 parser->
local.delimiter_branch++;
435 if (parser->
local.delimiter_depth < 1) {
439 parser->
local.delimiter_depth--;
445 if (parser->
local.delimiter_branch > 1) {
446 dbg_hid(
"alternative usage ignored\n");
451 data = (parser->
global.usage_page << 16) + data;
453 return hid_add_usage(parser, data);
457 if (parser->
local.delimiter_branch > 1) {
458 dbg_hid(
"alternative usage ignored\n");
463 data = (parser->
global.usage_page << 16) + data;
470 if (parser->
local.delimiter_branch > 1) {
471 dbg_hid(
"alternative usage ignored\n");
476 data = (parser->
global.usage_page << 16) + data;
478 for (n = parser->
local.usage_minimum; n <= data; n++)
479 if (hid_add_usage(parser, n)) {
480 dbg_hid(
"hid_add_usage failed\n");
487 dbg_hid(
"unknown local item tag 0x%x\n", item->
tag);
502 data = item_udata(item);
506 ret = open_collection(parser, data & 0xff);
509 ret = close_collection(parser);
536 dbg_hid(
"reserved item type, tag 0x%x\n", item->
tag);
546 static void hid_free_report(
struct hid_report *report)
550 for (n = 0; n < report->
maxfield; n++)
566 for (j = 0; j < 256; j++) {
569 hid_free_report(report);
571 memset(report_enum, 0,
sizeof(*report_enum));
592 static void hid_device_release(
struct device *
dev)
596 hid_close_report(hid);
610 if ((end - start) <= 0)
615 item->
type = (b >> 2) & 3;
616 item->
tag = (b >> 4) & 15;
622 if ((end - start) < 2)
625 item->
size = *start++;
626 item->
tag = *start++;
628 if ((end - start) < item->
size)
639 switch (item->
size) {
644 if ((end - start) < 1)
650 if ((end - start) < 2)
652 item->
data.
u16 = get_unaligned_le16(start);
658 if ((end - start) < 4)
668 static void hid_scan_usage(
struct hid_device *hid,
u32 usage)
679 static int hid_scan_report(
struct hid_device *hid)
681 unsigned int page = 0, delim = 0;
684 unsigned int u, u_min = 0, u_max = 0;
688 while ((start = fetch_item(start, end, &item)) !=
NULL) {
693 page = item_udata(&item) << 16;
697 u = item_udata(&item);
705 hid_scan_usage(hid, u);
712 for (u = u_min; u <= u_max; u++)
713 hid_scan_usage(hid, u);
783 if (device->
driver->report_fixup)
784 start = device->
driver->report_fixup(device, buf, &size);
815 while ((start = fetch_item(start, end, &item)) !=
NULL) {
818 hid_err(device,
"unexpected long global item\n");
822 if (dispatch_type[item.
type](parser, &item)) {
823 hid_err(device,
"item %u %u %u %u parsing failed\n",
825 (
unsigned)item.
type, (
unsigned)item.
tag);
831 hid_err(device,
"unbalanced collection at end of report description\n");
834 if (parser->
local.delimiter_depth) {
835 hid_err(device,
"unbalanced delimiter at end of report description\n");
844 hid_err(device,
"item fetching failed at offset %d\n", (
int)(end - start));
847 hid_close_report(device);
861 case 8:
return ((
__s8)value);
862 case 16:
return ((
__s16)value);
863 case 32:
return ((
__s32)value);
865 return value & (1 << (n - 1)) ? value | (-1 <<
n) : value;
872 static u32 s32ton(
__s32 value,
unsigned n)
874 s32 a = value >> (n - 1);
876 return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
877 return value & ((1 <<
n) - 1);
893 unsigned offset,
unsigned n)
898 hid_warn(hid,
"extract() called with n (%d) > 32! (%s)\n",
901 report += offset >> 3;
904 x = (x >>
offset) & ((1ULL << n) - 1);
917 unsigned offset,
unsigned n,
__u32 value)
920 u64 m = (1ULL <<
n) - 1;
923 hid_warn(hid,
"%s() called with n (%d) > 32! (%s)\n",
927 hid_warn(hid,
"%s() called with too large value %d! (%s)\n",
928 __func__, value,
current->comm);
932 report += offset >> 3;
948 if (*array++ == value)
992 for (;
id->usage_type !=
HID_ANY_ID - 1;
id++)
1012 if (hdrv && hdrv->
event && hid_match_usage(hid, usage)) {
1013 ret = hdrv->
event(hid, field, usage, value);
1016 hid_err(hid,
"%s's event failed with %d\n",
1035 __u8 *data,
int interrupt)
1049 for (n = 0; n <
count; n++) {
1051 value[
n] = min < 0 ?
1052 snto32(extract(hid, data, offset + n * size, size),
1054 extract(hid, data, offset + n * size, size);
1058 value[n] >= min && value[n] <= max &&
1063 for (n = 0; n <
count; n++) {
1066 hid_process_event(hid, field, &field->
usage[n], value[n], interrupt);
1070 if (field->
value[n] >= min && field->
value[n] <= max
1073 hid_process_event(hid, field, &field->
usage[field->
value[n] - min], 0, interrupt);
1075 if (value[n] >= min && value[n] <= max
1076 && field->
usage[value[n] - min].hid
1078 hid_process_event(hid, field, &field->
usage[value[n] - min], 1, interrupt);
1090 static void hid_output_field(
const struct hid_device *hid,
1098 for (n = 0; n <
count; n++) {
1100 implement(hid, data, offset + n * size, size,
1101 s32ton(field->
value[n], size));
1103 implement(hid, data, offset + n * size, size,
1117 *data++ = report->
id;
1119 memset(data, 0, ((report->
size - 1) >> 3) + 1);
1120 for (n = 0; n < report->
maxfield; n++)
1121 hid_output_field(report->
device, report->
field[n], data);
1138 hid_err(field->
report->device,
"offset (%d) exceeds report_count (%d)\n",
1143 if (value != snto32(s32ton(value, size), size)) {
1144 hid_err(field->
report->device,
"value %d is out of range\n", value);
1165 dbg_hid(
"undefined report_id %u received\n", n);
1176 int rsize, csize =
size;
1180 report = hid_get_report(report_enum, data);
1189 rsize = ((report->
size - 1) >> 3) + 1;
1194 if (csize < rsize) {
1195 dbg_hid(
"report %d is too short, (%d < %d)\n", report->
id,
1197 memset(cdata + csize, 0, rsize - csize);
1209 for (a = 0; a < report->
maxfield; a++)
1210 hid_input_field(hid, report->
field[a], cdata, interrupt);
1269 snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1270 "\nreport (size %u) (%snumbered) = ", size, report_enum->
numbered ?
"" :
"un");
1273 for (i = 0; i <
size; i++) {
1274 snprintf(buf, HID_DEBUG_BUFSIZE - 1,
1282 report = hid_get_report(report_enum, data);
1289 if (hdrv && hdrv->
raw_event && hid_match_report(hid, report)) {
1290 ret = hdrv->
raw_event(hid, report, data, size);
1292 ret = ret < 0 ? ret : 0;
1305 static bool hid_match_one_id(
struct hid_device *hdev,
1317 for (;
id->bus;
id++)
1318 if (hid_match_one_id(hdev,
id))
1330 static bool hid_hiddev(
struct hid_device *hdev)
1337 read_report_descriptor(
struct file *filp,
struct kobject *kobj,
1339 char *
buf, loff_t off,
size_t count)
1344 if (off >= hdev->
rsize)
1347 if (off + count > hdev->
rsize)
1348 count = hdev->
rsize - off;
1356 .attr = { .name =
"report_descriptor", .mode = 0444 },
1357 .read = read_report_descriptor,
1363 static const char *types[] = {
"Device",
"Pointer",
"Mouse",
"Device",
1364 "Joystick",
"Gamepad",
"Keyboard",
"Keypad",
1365 "Multi-Axis Controller"
1379 if (hid_hiddev(hdev))
1396 hid_err(hdev,
"device has no listeners, quitting\n");
1406 len +=
sprintf(buf + len,
"input");
1408 len +=
sprintf(buf + len,
"%shiddev%d", len ?
"," :
"",
1411 len +=
sprintf(buf + len,
"%shidraw%d", len ?
"," :
"",
1420 type = types[col->
usage & 0xffff];
1425 switch (hdev->
bus) {
1439 "can't create sysfs report descriptor attribute err: %d\n", ret);
1441 hid_info(hdev,
"%s: %s HID v%x.%02x %s [%s] on %s\n",
1469 static const struct hid_device_id hid_have_special_driver[] = {
1586 #if IS_ENABLED(CONFIG_HID_LENOVO_TPKBD)
1614 #if IS_ENABLED(CONFIG_HID_LOGITECH_DJ)
1655 #if IS_ENABLED(CONFIG_HID_ROCCAT)
1744 ret =
sscanf(buf,
"%x %x %x %lx",
1745 &bus, &vendor, &product, &driver_data);
1753 dynid->
id.bus =
bus;
1757 dynid->
id.driver_data = driver_data;
1765 return ret ? :
count;
1769 static void hid_free_dynids(
struct hid_driver *hdrv)
1788 if (hid_match_one_id(hdev, &dynid->
id)) {
1798 static int hid_bus_match(
struct device *dev,
struct device_driver *drv)
1803 return hid_match_device(hdev, hdrv) !=
NULL;
1806 static int hid_device_probe(
struct device *dev)
1818 id = hid_match_device(hdev, hdrv);
1826 ret = hdrv->
probe(hdev,
id);
1833 hid_close_report(hdev);
1842 static int hid_device_remove(
struct device *dev)
1856 hid_close_report(hdev);
1905 static struct bus_type hid_bus_type = {
1907 .dev_attrs = hid_dev_attrs,
1908 .match = hid_bus_match,
1909 .probe = hid_device_probe,
1910 .remove = hid_device_remove,
1911 .uevent = hid_uevent,
2069 #if defined(CONFIG_MOUSE_SYNAPTICS_USB) || defined(CONFIG_MOUSE_SYNAPTICS_USB_MODULE)
2099 static const struct hid_device_id hid_mouse_ignore_list[] = {
2153 static bool hid_ignore(
struct hid_device *hdev)
2174 !
strcmp(hdev->
name,
"HOLTEK B-LINK USB Audio "))
2228 if (hid_ignore_special_drivers ||
2230 ret = hid_scan_report(hdev);
2232 hid_warn(hdev,
"bad device descriptor (%d)\n", ret);
2267 return ERR_PTR(ret);
2270 hdev->
dev.release = hid_device_release;
2271 hdev->
dev.bus = &hid_bus_type;
2273 hid_close_report(hdev);
2283 static void hid_remove_device(
struct hid_device *hdev)
2305 hid_remove_device(hdev);
2311 const char *mod_name)
2316 hdrv->
driver.bus = &hid_bus_type;
2318 hdrv->
driver.mod_name = mod_name;
2339 hid_free_dynids(hdrv);
2353 if (hidinput->
input->key[i])
2362 static int __init hid_init(
void)
2367 pr_warn(
"hid_debug is now used solely for parser and driver debugging.\n"
2368 "debugfs is now used for inspecting the device (report descriptor, reports)\n");
2372 pr_err(
"can't register hid bus\n");
2389 static void __exit hid_exit(
void)