Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid

Pull HID updates from Jiri Kosina:

- rework of generic input handling which ultimately makes the
processing of tablet events more generic and reliable (Benjamin
Tissoires)

- fixes for handling unnumbered reports fully correctly in i2c-hid
(Angela Czubak, Dmitry Torokhov)

- untangling of intermingled code for sending and handling output
reports in i2c-hid (Dmitry Torokhov)

- Apple magic keyboard support improvements for newer models (José
Expósito)

- Apple T2 Macs support improvements (Aun-Ali Zaidi, Paul Pawlowski)

- driver for Razer Blackwidow keyboards (Jelle van der Waa)

- driver for SiGma Micro keyboards (Desmond Lim)

- integration of first part of DIGImend patches in order to ultimately
vastly improve Linux support of tablets (Nikolai Kondrashov, José
Expósito)

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid: (55 commits)
HID: intel-ish-hid: Use dma_alloc_coherent for firmware update
Input: docs: add more details on the use of BTN_TOOL
HID: input: accommodate priorities for slotted devices
HID: input: remove the need for HID_QUIRK_INVERT
HID: input: enforce Invert usage to be processed before InRange
HID: core: for input reports, process the usages by priority list
HID: compute an ordered list of input fields to process
HID: input: move up out-of-range processing of input values
HID: input: rework spaghetti code with switch statements
HID: input: tag touchscreens as such if the physical is not there
HID: core: split data fetching from processing in hid_input_field()
HID: core: de-duplicate some code in hid_input_field()
HID: core: statically allocate read buffers
HID: uclogic: Support multiple frame input devices
HID: uclogic: Define report IDs before their descriptors
HID: uclogic: Put version first in rdesc namespace
HID: uclogic: Use "frame" instead of "buttonpad"
HID: uclogic: Use different constants for frame report IDs
HID: uclogic: Specify total report size to buttonpad macro
HID: uclogic: Switch to matching subreport bytes
...

+1846 -760
+5 -1
Documentation/input/event-codes.rst
··· 137 137 code should be set to a value of 1. When the tool is no longer interacting 138 138 with the input device, the BTN_TOOL_<name> code should be reset to 0. All 139 139 trackpads, tablets, and touchscreens should use at least one BTN_TOOL_<name> 140 - code when events are generated. 140 + code when events are generated. Likewise all trackpads, tablets, and 141 + touchscreens should export only one BTN_TOOL_<name> at a time. To not break 142 + existing userspace, it is recommended to not switch tool in one EV_SYN frame 143 + but first emitting the old BTN_TOOL_<name> at 0, then emit one SYN_REPORT 144 + and then set the new BTN_TOOL_<name> at 1. 141 145 142 146 * BTN_TOUCH: 143 147
+19
drivers/hid/Kconfig
··· 128 128 config HID_APPLE 129 129 tristate "Apple {i,Power,Mac}Books" 130 130 depends on HID 131 + depends on LEDS_CLASS 132 + depends on NEW_LEDS 131 133 default !EXPERT 132 134 help 133 135 Support for some Apple devices which less or more break ··· 931 929 Say Y here if you would like to enable force feedback support for 932 930 PlayStation game controllers. 933 931 932 + config HID_RAZER 933 + tristate "Razer non-fully HID-compliant devices" 934 + depends on HID 935 + help 936 + Support for Razer devices that are not fully compliant with the 937 + HID standard. 938 + 934 939 config HID_PRIMAX 935 940 tristate "Primax non-fully HID-compliant devices" 936 941 depends on HID ··· 992 983 - Tronsmart TK09R 993 984 - Woo-dy 994 985 - X-Bows Nature/Knight 986 + 987 + config HID_SIGMAMICRO 988 + tristate "SiGma Micro-based keyboards" 989 + depends on USB_HID 990 + help 991 + Support for keyboards that use the SiGma Micro (a.k.a SigmaChip) IC. 992 + 993 + Supported devices: 994 + - Landslides KR-700 995 + - Rapoo V500 995 996 996 997 config HID_SONY 997 998 tristate "Sony PS2/3/4 accessories"
+2
drivers/hid/Makefile
··· 99 99 obj-$(CONFIG_HID_PLANTRONICS) += hid-plantronics.o 100 100 obj-$(CONFIG_HID_PLAYSTATION) += hid-playstation.o 101 101 obj-$(CONFIG_HID_PRIMAX) += hid-primax.o 102 + obj-$(CONFIG_HID_RAZER) += hid-razer.o 102 103 obj-$(CONFIG_HID_REDRAGON) += hid-redragon.o 103 104 obj-$(CONFIG_HID_RETRODE) += hid-retrode.o 104 105 obj-$(CONFIG_HID_ROCCAT) += hid-roccat.o hid-roccat-common.o \ ··· 110 109 obj-$(CONFIG_HID_SAITEK) += hid-saitek.o 111 110 obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o 112 111 obj-$(CONFIG_HID_SEMITEK) += hid-semitek.o 112 + obj-$(CONFIG_HID_SIGMAMICRO) += hid-sigmamicro.o 113 113 obj-$(CONFIG_HID_SMARTJOYPLUS) += hid-sjoy.o 114 114 obj-$(CONFIG_HID_SONY) += hid-sony.o 115 115 obj-$(CONFIG_HID_SPEEDLINK) += hid-speedlink.o
+2 -5
drivers/hid/amd-sfh-hid/amd_sfh_pcie.c
··· 301 301 pci_set_master(pdev); 302 302 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 303 303 if (rc) { 304 - rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 305 - if (rc) { 306 - dev_err(&pdev->dev, "failed to set DMA mask\n"); 307 - return rc; 308 - } 304 + dev_err(&pdev->dev, "failed to set DMA mask\n"); 305 + return rc; 309 306 } 310 307 311 308 privdata->cl_data = devm_kzalloc(&pdev->dev, sizeof(struct amdtp_cl_data), GFP_KERNEL);
+277 -26
drivers/hid/hid-apple.c
··· 7 7 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc 8 8 * Copyright (c) 2006-2007 Jiri Kosina 9 9 * Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com> 10 + * Copyright (c) 2019 Paul Pawlowski <paul@mrarm.io> 10 11 */ 11 12 12 13 /* ··· 34 33 /* BIT(7) reserved, was: APPLE_IGNORE_HIDINPUT */ 35 34 #define APPLE_NUMLOCK_EMULATION BIT(8) 36 35 #define APPLE_RDESC_BATTERY BIT(9) 36 + #define APPLE_BACKLIGHT_CTL BIT(10) 37 37 38 38 #define APPLE_FLAG_FKEY 0x01 39 39 ··· 63 61 "(For people who want to keep PC keyboard muscle memory. " 64 62 "[0] = as-is, Mac layout, 1 = swapped, PC layout)"); 65 63 64 + struct apple_sc_backlight { 65 + struct led_classdev cdev; 66 + struct hid_device *hdev; 67 + unsigned short backlight_off, backlight_on_min, backlight_on_max; 68 + }; 69 + 66 70 struct apple_sc { 67 71 struct hid_device *hdev; 68 72 unsigned long quirks; ··· 76 68 unsigned int fn_found; 77 69 DECLARE_BITMAP(pressed_numlock, KEY_CNT); 78 70 struct timer_list battery_timer; 71 + struct apple_sc_backlight *backlight; 79 72 }; 80 73 81 74 struct apple_key_translation { ··· 84 75 u16 to; 85 76 u8 flags; 86 77 }; 78 + 79 + static const struct apple_key_translation magic_keyboard_alu_fn_keys[] = { 80 + { KEY_BACKSPACE, KEY_DELETE }, 81 + { KEY_ENTER, KEY_INSERT }, 82 + { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY }, 83 + { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY }, 84 + { KEY_F3, KEY_SCALE, APPLE_FLAG_FKEY }, 85 + { KEY_F4, KEY_DASHBOARD, APPLE_FLAG_FKEY }, 86 + { KEY_F6, KEY_NUMLOCK, APPLE_FLAG_FKEY }, 87 + { KEY_F7, KEY_PREVIOUSSONG, APPLE_FLAG_FKEY }, 88 + { KEY_F8, KEY_PLAYPAUSE, APPLE_FLAG_FKEY }, 89 + { KEY_F9, KEY_NEXTSONG, APPLE_FLAG_FKEY }, 90 + { KEY_F10, KEY_MUTE, APPLE_FLAG_FKEY }, 91 + { KEY_F11, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY }, 92 + { KEY_F12, KEY_VOLUMEUP, APPLE_FLAG_FKEY }, 93 + { KEY_UP, KEY_PAGEUP }, 94 + { KEY_DOWN, KEY_PAGEDOWN }, 95 + { KEY_LEFT, KEY_HOME }, 96 + { KEY_RIGHT, KEY_END }, 97 + { } 98 + }; 99 + 100 + static const struct apple_key_translation magic_keyboard_2015_fn_keys[] = { 101 + { KEY_BACKSPACE, KEY_DELETE }, 102 + { KEY_ENTER, KEY_INSERT }, 103 + { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY }, 104 + { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY }, 105 + { KEY_F3, KEY_SCALE, APPLE_FLAG_FKEY }, 106 + { KEY_F4, KEY_DASHBOARD, APPLE_FLAG_FKEY }, 107 + { KEY_F7, KEY_PREVIOUSSONG, APPLE_FLAG_FKEY }, 108 + { KEY_F8, KEY_PLAYPAUSE, APPLE_FLAG_FKEY }, 109 + { KEY_F9, KEY_NEXTSONG, APPLE_FLAG_FKEY }, 110 + { KEY_F10, KEY_MUTE, APPLE_FLAG_FKEY }, 111 + { KEY_F11, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY }, 112 + { KEY_F12, KEY_VOLUMEUP, APPLE_FLAG_FKEY }, 113 + { KEY_UP, KEY_PAGEUP }, 114 + { KEY_DOWN, KEY_PAGEDOWN }, 115 + { KEY_LEFT, KEY_HOME }, 116 + { KEY_RIGHT, KEY_END }, 117 + { } 118 + }; 119 + 120 + struct apple_backlight_config_report { 121 + u8 report_id; 122 + u8 version; 123 + u16 backlight_off, backlight_on_min, backlight_on_max; 124 + }; 125 + 126 + struct apple_backlight_set_report { 127 + u8 report_id; 128 + u8 version; 129 + u16 backlight; 130 + u16 rate; 131 + }; 132 + 87 133 88 134 static const struct apple_key_translation apple2021_fn_keys[] = { 89 135 { KEY_BACKSPACE, KEY_DELETE }, ··· 176 112 { KEY_F10, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY }, 177 113 { KEY_F11, KEY_VOLUMEUP, APPLE_FLAG_FKEY }, 178 114 { KEY_F12, KEY_EJECTCD, APPLE_FLAG_FKEY }, 115 + { KEY_UP, KEY_PAGEUP }, 116 + { KEY_DOWN, KEY_PAGEDOWN }, 117 + { KEY_LEFT, KEY_HOME }, 118 + { KEY_RIGHT, KEY_END }, 119 + { } 120 + }; 121 + 122 + static const struct apple_key_translation macbookpro_no_esc_fn_keys[] = { 123 + { KEY_BACKSPACE, KEY_DELETE }, 124 + { KEY_ENTER, KEY_INSERT }, 125 + { KEY_GRAVE, KEY_ESC }, 126 + { KEY_1, KEY_F1 }, 127 + { KEY_2, KEY_F2 }, 128 + { KEY_3, KEY_F3 }, 129 + { KEY_4, KEY_F4 }, 130 + { KEY_5, KEY_F5 }, 131 + { KEY_6, KEY_F6 }, 132 + { KEY_7, KEY_F7 }, 133 + { KEY_8, KEY_F8 }, 134 + { KEY_9, KEY_F9 }, 135 + { KEY_0, KEY_F10 }, 136 + { KEY_MINUS, KEY_F11 }, 137 + { KEY_EQUAL, KEY_F12 }, 138 + { KEY_UP, KEY_PAGEUP }, 139 + { KEY_DOWN, KEY_PAGEDOWN }, 140 + { KEY_LEFT, KEY_HOME }, 141 + { KEY_RIGHT, KEY_END }, 142 + { } 143 + }; 144 + 145 + static const struct apple_key_translation macbookpro_dedicated_esc_fn_keys[] = { 146 + { KEY_BACKSPACE, KEY_DELETE }, 147 + { KEY_ENTER, KEY_INSERT }, 148 + { KEY_1, KEY_F1 }, 149 + { KEY_2, KEY_F2 }, 150 + { KEY_3, KEY_F3 }, 151 + { KEY_4, KEY_F4 }, 152 + { KEY_5, KEY_F5 }, 153 + { KEY_6, KEY_F6 }, 154 + { KEY_7, KEY_F7 }, 155 + { KEY_8, KEY_F8 }, 156 + { KEY_9, KEY_F9 }, 157 + { KEY_0, KEY_F10 }, 158 + { KEY_MINUS, KEY_F11 }, 159 + { KEY_EQUAL, KEY_F12 }, 179 160 { KEY_UP, KEY_PAGEUP }, 180 161 { KEY_DOWN, KEY_PAGEDOWN }, 181 162 { KEY_LEFT, KEY_HOME }, ··· 311 202 { } 312 203 }; 313 204 205 + static inline void apple_setup_key_translation(struct input_dev *input, 206 + const struct apple_key_translation *table) 207 + { 208 + const struct apple_key_translation *trans; 209 + 210 + for (trans = table; trans->from; trans++) 211 + set_bit(trans->to, input->keybit); 212 + } 213 + 314 214 static const struct apple_key_translation *apple_find_translation( 315 215 const struct apple_key_translation *table, u16 from) 316 216 { ··· 360 242 } 361 243 362 244 if (fnmode) { 363 - if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021 || 364 - hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 || 365 - hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021) 245 + if (hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI || 246 + hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO || 247 + hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS || 248 + hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI || 249 + hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO || 250 + hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS || 251 + hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI || 252 + hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO || 253 + hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS) 254 + table = magic_keyboard_alu_fn_keys; 255 + else if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015 || 256 + hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015) 257 + table = magic_keyboard_2015_fn_keys; 258 + else if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021 || 259 + hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 || 260 + hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021) 366 261 table = apple2021_fn_keys; 262 + else if (hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132 || 263 + hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680 || 264 + hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213) 265 + table = macbookpro_no_esc_fn_keys; 266 + else if (hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K || 267 + hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223 || 268 + hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F) 269 + table = macbookpro_dedicated_esc_fn_keys; 270 + else if (hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K || 271 + hid->product == USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K) 272 + table = apple_fn_keys; 367 273 else if (hid->product >= USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI && 368 274 hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS) 369 275 table = macbookair_fn_keys; ··· 594 452 595 453 static void apple_setup_input(struct input_dev *input) 596 454 { 597 - const struct apple_key_translation *trans; 598 - 599 455 set_bit(KEY_NUMLOCK, input->keybit); 600 456 601 457 /* Enable all needed keys */ 602 - for (trans = apple_fn_keys; trans->from; trans++) 603 - set_bit(trans->to, input->keybit); 458 + apple_setup_key_translation(input, apple_fn_keys); 459 + apple_setup_key_translation(input, powerbook_fn_keys); 460 + apple_setup_key_translation(input, powerbook_numlock_keys); 461 + apple_setup_key_translation(input, apple_iso_keyboard); 462 + apple_setup_key_translation(input, magic_keyboard_alu_fn_keys); 463 + apple_setup_key_translation(input, magic_keyboard_2015_fn_keys); 464 + apple_setup_key_translation(input, apple2021_fn_keys); 465 + apple_setup_key_translation(input, macbookpro_no_esc_fn_keys); 466 + apple_setup_key_translation(input, macbookpro_dedicated_esc_fn_keys); 604 467 605 - for (trans = powerbook_fn_keys; trans->from; trans++) 606 - set_bit(trans->to, input->keybit); 607 - 608 - for (trans = powerbook_numlock_keys; trans->from; trans++) 609 - set_bit(trans->to, input->keybit); 610 - 611 - for (trans = apple_iso_keyboard; trans->from; trans++) 612 - set_bit(trans->to, input->keybit); 613 - 614 - for (trans = apple2021_fn_keys; trans->from; trans++) 615 - set_bit(trans->to, input->keybit); 616 - 617 - if (swap_fn_leftctrl) { 618 - for (trans = swapped_fn_leftctrl_keys; trans->from; trans++) 619 - set_bit(trans->to, input->keybit); 620 - } 468 + if (swap_fn_leftctrl) 469 + apple_setup_key_translation(input, swapped_fn_leftctrl_keys); 621 470 } 622 471 623 472 static int apple_input_mapping(struct hid_device *hdev, struct hid_input *hi, ··· 663 530 return 0; 664 531 } 665 532 533 + static bool apple_backlight_check_support(struct hid_device *hdev) 534 + { 535 + int i; 536 + unsigned int hid; 537 + struct hid_report *report; 538 + 539 + list_for_each_entry(report, &hdev->report_enum[HID_INPUT_REPORT].report_list, list) { 540 + for (i = 0; i < report->maxfield; i++) { 541 + hid = report->field[i]->usage->hid; 542 + if ((hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR && (hid & HID_USAGE) == 0xf) 543 + return true; 544 + } 545 + } 546 + 547 + return false; 548 + } 549 + 550 + static int apple_backlight_set(struct hid_device *hdev, u16 value, u16 rate) 551 + { 552 + int ret = 0; 553 + struct apple_backlight_set_report *rep; 554 + 555 + rep = kmalloc(sizeof(*rep), GFP_KERNEL); 556 + if (rep == NULL) 557 + return -ENOMEM; 558 + 559 + rep->report_id = 0xB0; 560 + rep->version = 1; 561 + rep->backlight = value; 562 + rep->rate = rate; 563 + 564 + ret = hid_hw_raw_request(hdev, 0xB0u, (u8 *) rep, sizeof(*rep), 565 + HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); 566 + 567 + kfree(rep); 568 + return ret; 569 + } 570 + 571 + static int apple_backlight_led_set(struct led_classdev *led_cdev, 572 + enum led_brightness brightness) 573 + { 574 + struct apple_sc_backlight *backlight = container_of(led_cdev, 575 + struct apple_sc_backlight, cdev); 576 + 577 + return apple_backlight_set(backlight->hdev, brightness, 0); 578 + } 579 + 580 + static int apple_backlight_init(struct hid_device *hdev) 581 + { 582 + int ret; 583 + struct apple_sc *asc = hid_get_drvdata(hdev); 584 + struct apple_backlight_config_report *rep; 585 + 586 + if (!apple_backlight_check_support(hdev)) 587 + return -EINVAL; 588 + 589 + rep = kmalloc(0x200, GFP_KERNEL); 590 + if (rep == NULL) 591 + return -ENOMEM; 592 + 593 + ret = hid_hw_raw_request(hdev, 0xBFu, (u8 *) rep, sizeof(*rep), 594 + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 595 + if (ret < 0) { 596 + hid_err(hdev, "backlight request failed: %d\n", ret); 597 + goto cleanup_and_exit; 598 + } 599 + if (ret < 8 || rep->version != 1) { 600 + hid_err(hdev, "backlight config struct: bad version %i\n", rep->version); 601 + ret = -EINVAL; 602 + goto cleanup_and_exit; 603 + } 604 + 605 + hid_dbg(hdev, "backlight config: off=%u, on_min=%u, on_max=%u\n", 606 + rep->backlight_off, rep->backlight_on_min, rep->backlight_on_max); 607 + 608 + asc->backlight = devm_kzalloc(&hdev->dev, sizeof(*asc->backlight), GFP_KERNEL); 609 + if (!asc->backlight) { 610 + ret = -ENOMEM; 611 + goto cleanup_and_exit; 612 + } 613 + 614 + asc->backlight->hdev = hdev; 615 + asc->backlight->cdev.name = "apple::kbd_backlight"; 616 + asc->backlight->cdev.max_brightness = rep->backlight_on_max; 617 + asc->backlight->cdev.brightness_set_blocking = apple_backlight_led_set; 618 + 619 + ret = apple_backlight_set(hdev, 0, 0); 620 + if (ret < 0) { 621 + hid_err(hdev, "backlight set request failed: %d\n", ret); 622 + goto cleanup_and_exit; 623 + } 624 + 625 + ret = devm_led_classdev_register(&hdev->dev, &asc->backlight->cdev); 626 + 627 + cleanup_and_exit: 628 + kfree(rep); 629 + return ret; 630 + } 631 + 666 632 static int apple_probe(struct hid_device *hdev, 667 633 const struct hid_device_id *id) 668 634 { ··· 796 564 mod_timer(&asc->battery_timer, 797 565 jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS)); 798 566 apple_fetch_battery(hdev); 567 + 568 + if (quirks & APPLE_BACKLIGHT_CTL) 569 + apple_backlight_init(hdev); 799 570 800 571 return 0; 801 572 } ··· 971 736 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK }, 972 737 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS), 973 738 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS }, 739 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K), 740 + .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL }, 741 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132), 742 + .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL }, 743 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680), 744 + .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL }, 745 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213), 746 + .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL }, 747 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K), 748 + .driver_data = APPLE_HAS_FN }, 749 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223), 750 + .driver_data = APPLE_HAS_FN }, 751 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K), 752 + .driver_data = APPLE_HAS_FN }, 753 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F), 754 + .driver_data = APPLE_HAS_FN }, 974 755 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI), 975 756 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN }, 976 757 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO), ··· 999 748 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY), 1000 749 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN }, 1001 750 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021), 1002 - .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK }, 751 + .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY }, 1003 752 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021), 1004 753 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK }, 1005 754 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021), 1006 - .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK }, 755 + .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY }, 1007 756 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021), 1008 757 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK }, 1009 758 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021), 1010 - .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK }, 759 + .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK | APPLE_RDESC_BATTERY }, 1011 760 { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021), 1012 761 .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK }, 1013 762
+249 -35
drivers/hid/hid-core.c
··· 81 81 report_enum->report_id_hash[id] = report; 82 82 83 83 list_add_tail(&report->list, &report_enum->report_list); 84 + INIT_LIST_HEAD(&report->field_entry_list); 84 85 85 86 return report; 86 87 } ··· 102 101 103 102 field = kzalloc((sizeof(struct hid_field) + 104 103 usages * sizeof(struct hid_usage) + 105 - usages * sizeof(unsigned)), GFP_KERNEL); 104 + 3 * usages * sizeof(unsigned int)), GFP_KERNEL); 106 105 if (!field) 107 106 return NULL; 108 107 ··· 110 109 report->field[field->index] = field; 111 110 field->usage = (struct hid_usage *)(field + 1); 112 111 field->value = (s32 *)(field->usage + usages); 112 + field->new_value = (s32 *)(field->value + usages); 113 + field->usages_priorities = (s32 *)(field->new_value + usages); 113 114 field->report = report; 114 115 115 116 return field; ··· 658 655 static void hid_free_report(struct hid_report *report) 659 656 { 660 657 unsigned n; 658 + 659 + kfree(report->field_entries); 661 660 662 661 for (n = 0; n < report->maxfield; n++) 663 662 kfree(report->field[n]); ··· 1530 1525 } 1531 1526 1532 1527 /* 1533 - * Analyse a received field, and fetch the data from it. The field 1534 - * content is stored for next report processing (we do differential 1535 - * reporting to the layer). 1528 + * Checks if the given value is valid within this field 1536 1529 */ 1530 + static inline int hid_array_value_is_valid(struct hid_field *field, 1531 + __s32 value) 1532 + { 1533 + __s32 min = field->logical_minimum; 1537 1534 1538 - static void hid_input_field(struct hid_device *hid, struct hid_field *field, 1539 - __u8 *data, int interrupt) 1535 + /* 1536 + * Value needs to be between logical min and max, and 1537 + * (value - min) is used as an index in the usage array. 1538 + * This array is of size field->maxusage 1539 + */ 1540 + return value >= min && 1541 + value <= field->logical_maximum && 1542 + value - min < field->maxusage; 1543 + } 1544 + 1545 + /* 1546 + * Fetch the field from the data. The field content is stored for next 1547 + * report processing (we do differential reporting to the layer). 1548 + */ 1549 + static void hid_input_fetch_field(struct hid_device *hid, 1550 + struct hid_field *field, 1551 + __u8 *data) 1540 1552 { 1541 1553 unsigned n; 1542 1554 unsigned count = field->report_count; 1543 1555 unsigned offset = field->report_offset; 1544 1556 unsigned size = field->report_size; 1545 1557 __s32 min = field->logical_minimum; 1546 - __s32 max = field->logical_maximum; 1547 1558 __s32 *value; 1548 1559 1549 - value = kmalloc_array(count, sizeof(__s32), GFP_ATOMIC); 1550 - if (!value) 1551 - return; 1560 + value = field->new_value; 1561 + memset(value, 0, count * sizeof(__s32)); 1562 + field->ignored = false; 1552 1563 1553 1564 for (n = 0; n < count; n++) { 1554 1565 ··· 1575 1554 1576 1555 /* Ignore report if ErrorRollOver */ 1577 1556 if (!(field->flags & HID_MAIN_ITEM_VARIABLE) && 1578 - value[n] >= min && value[n] <= max && 1579 - value[n] - min < field->maxusage && 1580 - field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1) 1581 - goto exit; 1557 + hid_array_value_is_valid(field, value[n]) && 1558 + field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1) { 1559 + field->ignored = true; 1560 + return; 1561 + } 1582 1562 } 1563 + } 1564 + 1565 + /* 1566 + * Process a received variable field. 1567 + */ 1568 + 1569 + static void hid_input_var_field(struct hid_device *hid, 1570 + struct hid_field *field, 1571 + int interrupt) 1572 + { 1573 + unsigned int count = field->report_count; 1574 + __s32 *value = field->new_value; 1575 + unsigned int n; 1576 + 1577 + for (n = 0; n < count; n++) 1578 + hid_process_event(hid, 1579 + field, 1580 + &field->usage[n], 1581 + value[n], 1582 + interrupt); 1583 + 1584 + memcpy(field->value, value, count * sizeof(__s32)); 1585 + } 1586 + 1587 + /* 1588 + * Process a received array field. The field content is stored for 1589 + * next report processing (we do differential reporting to the layer). 1590 + */ 1591 + 1592 + static void hid_input_array_field(struct hid_device *hid, 1593 + struct hid_field *field, 1594 + int interrupt) 1595 + { 1596 + unsigned int n; 1597 + unsigned int count = field->report_count; 1598 + __s32 min = field->logical_minimum; 1599 + __s32 *value; 1600 + 1601 + value = field->new_value; 1602 + 1603 + /* ErrorRollOver */ 1604 + if (field->ignored) 1605 + return; 1583 1606 1584 1607 for (n = 0; n < count; n++) { 1608 + if (hid_array_value_is_valid(field, field->value[n]) && 1609 + search(value, field->value[n], count)) 1610 + hid_process_event(hid, 1611 + field, 1612 + &field->usage[field->value[n] - min], 1613 + 0, 1614 + interrupt); 1585 1615 1586 - if (HID_MAIN_ITEM_VARIABLE & field->flags) { 1587 - hid_process_event(hid, field, &field->usage[n], value[n], interrupt); 1588 - continue; 1589 - } 1590 - 1591 - if (field->value[n] >= min && field->value[n] <= max 1592 - && field->value[n] - min < field->maxusage 1593 - && field->usage[field->value[n] - min].hid 1594 - && search(value, field->value[n], count)) 1595 - hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt); 1596 - 1597 - if (value[n] >= min && value[n] <= max 1598 - && value[n] - min < field->maxusage 1599 - && field->usage[value[n] - min].hid 1600 - && search(field->value, value[n], count)) 1601 - hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt); 1616 + if (hid_array_value_is_valid(field, value[n]) && 1617 + search(field->value, value[n], count)) 1618 + hid_process_event(hid, 1619 + field, 1620 + &field->usage[value[n] - min], 1621 + 1, 1622 + interrupt); 1602 1623 } 1603 1624 1604 1625 memcpy(field->value, value, count * sizeof(__s32)); 1605 - exit: 1606 - kfree(value); 1626 + } 1627 + 1628 + /* 1629 + * Analyse a received report, and fetch the data from it. The field 1630 + * content is stored for next report processing (we do differential 1631 + * reporting to the layer). 1632 + */ 1633 + static void hid_process_report(struct hid_device *hid, 1634 + struct hid_report *report, 1635 + __u8 *data, 1636 + int interrupt) 1637 + { 1638 + unsigned int a; 1639 + struct hid_field_entry *entry; 1640 + struct hid_field *field; 1641 + 1642 + /* first retrieve all incoming values in data */ 1643 + for (a = 0; a < report->maxfield; a++) 1644 + hid_input_fetch_field(hid, field = report->field[a], data); 1645 + 1646 + if (!list_empty(&report->field_entry_list)) { 1647 + /* INPUT_REPORT, we have a priority list of fields */ 1648 + list_for_each_entry(entry, 1649 + &report->field_entry_list, 1650 + list) { 1651 + field = entry->field; 1652 + 1653 + if (field->flags & HID_MAIN_ITEM_VARIABLE) 1654 + hid_process_event(hid, 1655 + field, 1656 + &field->usage[entry->index], 1657 + field->new_value[entry->index], 1658 + interrupt); 1659 + else 1660 + hid_input_array_field(hid, field, interrupt); 1661 + } 1662 + 1663 + /* we need to do the memcpy at the end for var items */ 1664 + for (a = 0; a < report->maxfield; a++) { 1665 + field = report->field[a]; 1666 + 1667 + if (field->flags & HID_MAIN_ITEM_VARIABLE) 1668 + memcpy(field->value, field->new_value, 1669 + field->report_count * sizeof(__s32)); 1670 + } 1671 + } else { 1672 + /* FEATURE_REPORT, regular processing */ 1673 + for (a = 0; a < report->maxfield; a++) { 1674 + field = report->field[a]; 1675 + 1676 + if (field->flags & HID_MAIN_ITEM_VARIABLE) 1677 + hid_input_var_field(hid, field, interrupt); 1678 + else 1679 + hid_input_array_field(hid, field, interrupt); 1680 + } 1681 + } 1682 + } 1683 + 1684 + /* 1685 + * Insert a given usage_index in a field in the list 1686 + * of processed usages in the report. 1687 + * 1688 + * The elements of lower priority score are processed 1689 + * first. 1690 + */ 1691 + static void __hid_insert_field_entry(struct hid_device *hid, 1692 + struct hid_report *report, 1693 + struct hid_field_entry *entry, 1694 + struct hid_field *field, 1695 + unsigned int usage_index) 1696 + { 1697 + struct hid_field_entry *next; 1698 + 1699 + entry->field = field; 1700 + entry->index = usage_index; 1701 + entry->priority = field->usages_priorities[usage_index]; 1702 + 1703 + /* insert the element at the correct position */ 1704 + list_for_each_entry(next, 1705 + &report->field_entry_list, 1706 + list) { 1707 + /* 1708 + * the priority of our element is strictly higher 1709 + * than the next one, insert it before 1710 + */ 1711 + if (entry->priority > next->priority) { 1712 + list_add_tail(&entry->list, &next->list); 1713 + return; 1714 + } 1715 + } 1716 + 1717 + /* lowest priority score: insert at the end */ 1718 + list_add_tail(&entry->list, &report->field_entry_list); 1719 + } 1720 + 1721 + static void hid_report_process_ordering(struct hid_device *hid, 1722 + struct hid_report *report) 1723 + { 1724 + struct hid_field *field; 1725 + struct hid_field_entry *entries; 1726 + unsigned int a, u, usages; 1727 + unsigned int count = 0; 1728 + 1729 + /* count the number of individual fields in the report */ 1730 + for (a = 0; a < report->maxfield; a++) { 1731 + field = report->field[a]; 1732 + 1733 + if (field->flags & HID_MAIN_ITEM_VARIABLE) 1734 + count += field->report_count; 1735 + else 1736 + count++; 1737 + } 1738 + 1739 + /* allocate the memory to process the fields */ 1740 + entries = kcalloc(count, sizeof(*entries), GFP_KERNEL); 1741 + if (!entries) 1742 + return; 1743 + 1744 + report->field_entries = entries; 1745 + 1746 + /* 1747 + * walk through all fields in the report and 1748 + * store them by priority order in report->field_entry_list 1749 + * 1750 + * - Var elements are individualized (field + usage_index) 1751 + * - Arrays are taken as one, we can not chose an order for them 1752 + */ 1753 + usages = 0; 1754 + for (a = 0; a < report->maxfield; a++) { 1755 + field = report->field[a]; 1756 + 1757 + if (field->flags & HID_MAIN_ITEM_VARIABLE) { 1758 + for (u = 0; u < field->report_count; u++) { 1759 + __hid_insert_field_entry(hid, report, 1760 + &entries[usages], 1761 + field, u); 1762 + usages++; 1763 + } 1764 + } else { 1765 + __hid_insert_field_entry(hid, report, &entries[usages], 1766 + field, 0); 1767 + usages++; 1768 + } 1769 + } 1770 + } 1771 + 1772 + static void hid_process_ordering(struct hid_device *hid) 1773 + { 1774 + struct hid_report *report; 1775 + struct hid_report_enum *report_enum = &hid->report_enum[HID_INPUT_REPORT]; 1776 + 1777 + list_for_each_entry(report, &report_enum->report_list, list) 1778 + hid_report_process_ordering(hid, report); 1607 1779 } 1608 1780 1609 1781 /* ··· 1960 1746 struct hid_report_enum *report_enum = hid->report_enum + type; 1961 1747 struct hid_report *report; 1962 1748 struct hid_driver *hdrv; 1963 - unsigned int a; 1964 1749 u32 rsize, csize = size; 1965 1750 u8 *cdata = data; 1966 1751 int ret = 0; ··· 1995 1782 } 1996 1783 1997 1784 if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) { 1998 - for (a = 0; a < report->maxfield; a++) 1999 - hid_input_field(hid, report->field[a], cdata, interrupt); 1785 + hid_process_report(hid, report, cdata, interrupt); 2000 1786 hdrv = hid->driver; 2001 1787 if (hdrv && hdrv->report) 2002 1788 hdrv->report(hid, report); ··· 2181 1969 hid_err(hdev, "device has no listeners, quitting\n"); 2182 1970 return -ENODEV; 2183 1971 } 1972 + 1973 + hid_process_ordering(hdev); 2184 1974 2185 1975 if ((hdev->claimed & HID_CLAIMED_INPUT) && 2186 1976 (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
+1 -1
drivers/hid/hid-google-hammer.c
··· 58 58 struct cros_ec_command *msg; 59 59 int ret; 60 60 61 - msg = kzalloc(sizeof(*msg) + max(sizeof(u32), sizeof(*params)), 61 + msg = kzalloc(struct_size(msg, data, max(sizeof(u32), sizeof(*params))), 62 62 GFP_KERNEL); 63 63 if (!msg) 64 64 return -ENOMEM;
+13 -1
drivers/hid/hid-ids.h
··· 167 167 #define USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI 0x0272 168 168 #define USB_DEVICE_ID_APPLE_WELLSPRING9_ISO 0x0273 169 169 #define USB_DEVICE_ID_APPLE_WELLSPRING9_JIS 0x0274 170 + #define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K 0x027a 171 + #define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132 0x027b 172 + #define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680 0x027c 173 + #define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213 0x027d 174 + #define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K 0x027e 175 + #define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223 0x027f 176 + #define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K 0x0280 177 + #define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F 0x0340 170 178 #define USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY 0x030a 171 179 #define USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY 0x030b 172 180 #define USB_DEVICE_ID_APPLE_IRCONTROL 0x8240 ··· 614 606 615 607 #define USB_VENDOR_ID_HUION 0x256c 616 608 #define USB_DEVICE_ID_HUION_TABLET 0x006e 617 - #define USB_DEVICE_ID_HUION_HS64 0x006d 609 + #define USB_DEVICE_ID_HUION_TABLET2 0x006d 618 610 619 611 #define USB_VENDOR_ID_IBM 0x04b3 620 612 #define USB_DEVICE_ID_IBM_SCROLLPOINT_III 0x3100 ··· 1038 1030 #define I2C_PRODUCT_ID_RAYDIUM_3118 0x3118 1039 1031 1040 1032 #define USB_VENDOR_ID_RAZER 0x1532 1033 + #define USB_DEVICE_ID_RAZER_BLACKWIDOW_ULTIMATE 0x010D 1034 + #define USB_DEVICE_ID_RAZER_BLACKWIDOW 0x010e 1035 + #define USB_DEVICE_ID_RAZER_BLACKWIDOW_CLASSIC 0x011b 1041 1036 #define USB_DEVICE_ID_RAZER_BLADE_14 0x011D 1042 1037 1043 1038 #define USB_VENDOR_ID_REALTEK 0x0bda ··· 1103 1092 1104 1093 #define USB_VENDOR_ID_SIGMA_MICRO 0x1c4f 1105 1094 #define USB_DEVICE_ID_SIGMA_MICRO_KEYBOARD 0x0002 1095 + #define USB_DEVICE_ID_SIGMA_MICRO_KEYBOARD2 0x0059 1106 1096 1107 1097 #define USB_VENDOR_ID_SIGMATEL 0x066F 1108 1098 #define USB_DEVICE_ID_SIGMATEL_STMP3780 0x3780
+295 -69
drivers/hid/hid-input.c
··· 48 48 __s32 y; 49 49 } hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; 50 50 51 + struct usage_priority { 52 + __u32 usage; /* the HID usage associated */ 53 + bool global; /* we assume all usages to be slotted, 54 + * unless global 55 + */ 56 + unsigned int slot_overwrite; /* for globals: allows to set the usage 57 + * before or after the slots 58 + */ 59 + }; 60 + 61 + /* 62 + * hid-input will convert this list into priorities: 63 + * the first element will have the highest priority 64 + * (the length of the following array) and the last 65 + * element the lowest (1). 66 + * 67 + * hid-input will then shift the priority by 8 bits to leave some space 68 + * in case drivers want to interleave other fields. 69 + * 70 + * To accommodate slotted devices, the slot priority is 71 + * defined in the next 8 bits (defined by 0xff - slot). 72 + * 73 + * If drivers want to add fields before those, hid-input will 74 + * leave out the first 8 bits of the priority value. 75 + * 76 + * This still leaves us 65535 individual priority values. 77 + */ 78 + static const struct usage_priority hidinput_usages_priorities[] = { 79 + { /* Eraser (eraser touching) must always come before tipswitch */ 80 + .usage = HID_DG_ERASER, 81 + }, 82 + { /* Invert must always come before In Range */ 83 + .usage = HID_DG_INVERT, 84 + }, 85 + { /* Is the tip of the tool touching? */ 86 + .usage = HID_DG_TIPSWITCH, 87 + }, 88 + { /* Tip Pressure might emulate tip switch */ 89 + .usage = HID_DG_TIPPRESSURE, 90 + }, 91 + { /* In Range needs to come after the other tool states */ 92 + .usage = HID_DG_INRANGE, 93 + }, 94 + }; 95 + 51 96 #define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c)) 52 97 #define map_rel(c) hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c)) 53 98 #define map_key(c) hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c)) ··· 631 586 } 632 587 633 588 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field, 634 - struct hid_usage *usage) 589 + struct hid_usage *usage, unsigned int usage_index) 635 590 { 636 591 struct input_dev *input = hidinput->input; 637 592 struct hid_device *device = input_get_drvdata(input); 593 + const struct usage_priority *usage_priority = NULL; 638 594 int max = 0, code; 595 + unsigned int i = 0; 639 596 unsigned long *bit = NULL; 640 597 641 598 field->hidinput = hidinput; ··· 654 607 (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) { 655 608 goto ignore; 656 609 } 610 + 611 + /* assign a priority based on the static list declared here */ 612 + for (i = 0; i < ARRAY_SIZE(hidinput_usages_priorities); i++) { 613 + if (usage->hid == hidinput_usages_priorities[i].usage) { 614 + usage_priority = &hidinput_usages_priorities[i]; 615 + 616 + field->usages_priorities[usage_index] = 617 + (ARRAY_SIZE(hidinput_usages_priorities) - i) << 8; 618 + break; 619 + } 620 + } 621 + 622 + /* 623 + * For slotted devices, we need to also add the slot index 624 + * in the priority. 625 + */ 626 + if (usage_priority && usage_priority->global) 627 + field->usages_priorities[usage_index] |= 628 + usage_priority->slot_overwrite; 629 + else 630 + field->usages_priorities[usage_index] |= 631 + (0xff - field->slot_idx) << 16; 657 632 658 633 if (device->driver->input_mapping) { 659 634 int ret = device->driver->input_mapping(device, hidinput, field, ··· 897 828 break; 898 829 899 830 case 0x32: /* InRange */ 900 - switch (field->physical & 0xff) { 901 - case 0x21: map_key(BTN_TOOL_MOUSE); break; 902 - case 0x22: map_key(BTN_TOOL_FINGER); break; 903 - default: map_key(BTN_TOOL_PEN); break; 831 + switch (field->physical) { 832 + case HID_DG_PUCK: 833 + map_key(BTN_TOOL_MOUSE); 834 + break; 835 + case HID_DG_FINGER: 836 + map_key(BTN_TOOL_FINGER); 837 + break; 838 + default: 839 + /* 840 + * If the physical is not given, 841 + * rely on the application. 842 + */ 843 + if (!field->physical) { 844 + switch (field->application) { 845 + case HID_DG_TOUCHSCREEN: 846 + case HID_DG_TOUCHPAD: 847 + map_key_clear(BTN_TOOL_FINGER); 848 + break; 849 + default: 850 + map_key_clear(BTN_TOOL_PEN); 851 + } 852 + } else { 853 + map_key(BTN_TOOL_PEN); 854 + } 855 + break; 904 856 } 905 857 break; 906 858 ··· 1408 1318 input_event(input, EV_REL, usage->code, hi_res); 1409 1319 } 1410 1320 1321 + static void hid_report_release_tool(struct hid_report *report, struct input_dev *input, 1322 + unsigned int tool) 1323 + { 1324 + /* if the given tool is not currently reported, ignore */ 1325 + if (!test_bit(tool, input->key)) 1326 + return; 1327 + 1328 + /* 1329 + * if the given tool was previously set, release it, 1330 + * release any TOUCH and send an EV_SYN 1331 + */ 1332 + input_event(input, EV_KEY, BTN_TOUCH, 0); 1333 + input_event(input, EV_KEY, tool, 0); 1334 + input_event(input, EV_SYN, SYN_REPORT, 0); 1335 + 1336 + report->tool = 0; 1337 + } 1338 + 1339 + static void hid_report_set_tool(struct hid_report *report, struct input_dev *input, 1340 + unsigned int new_tool) 1341 + { 1342 + if (report->tool != new_tool) 1343 + hid_report_release_tool(report, input, report->tool); 1344 + 1345 + input_event(input, EV_KEY, new_tool, 1); 1346 + report->tool = new_tool; 1347 + } 1348 + 1411 1349 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value) 1412 1350 { 1413 1351 struct input_dev *input; 1352 + struct hid_report *report = field->report; 1414 1353 unsigned *quirks = &hid->quirks; 1415 1354 1416 1355 if (!usage->type) ··· 1455 1336 1456 1337 input = field->hidinput->input; 1457 1338 1458 - if (usage->type == EV_ABS && 1459 - (((*quirks & HID_QUIRK_X_INVERT) && usage->code == ABS_X) || 1460 - ((*quirks & HID_QUIRK_Y_INVERT) && usage->code == ABS_Y))) { 1461 - value = field->logical_maximum - value; 1462 - } 1463 - 1464 1339 if (usage->hat_min < usage->hat_max || usage->hat_dir) { 1465 1340 int hat_dir = usage->hat_dir; 1466 1341 if (!hat_dir) ··· 1462 1349 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0; 1463 1350 input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x); 1464 1351 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y); 1465 - return; 1466 - } 1467 - 1468 - if (usage->hid == HID_DG_INVERT) { 1469 - *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT); 1470 - return; 1471 - } 1472 - 1473 - if (usage->hid == HID_DG_INRANGE) { 1474 - if (value) { 1475 - input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1); 1476 - return; 1477 - } 1478 - input_event(input, usage->type, usage->code, 0); 1479 - input_event(input, usage->type, BTN_TOOL_RUBBER, 0); 1480 - return; 1481 - } 1482 - 1483 - if (usage->hid == HID_DG_TIPPRESSURE && (*quirks & HID_QUIRK_NOTOUCH)) { 1484 - int a = field->logical_minimum; 1485 - int b = field->logical_maximum; 1486 - input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3)); 1487 - } 1488 - 1489 - if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */ 1490 - dbg_hid("Maximum Effects - %d\n",value); 1491 - return; 1492 - } 1493 - 1494 - if (usage->hid == (HID_UP_PID | 0x7fUL)) { 1495 - dbg_hid("PID Pool Report\n"); 1496 - return; 1497 - } 1498 - 1499 - if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */ 1500 - return; 1501 - 1502 - if ((usage->type == EV_REL) && (usage->code == REL_WHEEL_HI_RES || 1503 - usage->code == REL_HWHEEL_HI_RES)) { 1504 - hidinput_handle_scroll(usage, input, value); 1505 - return; 1506 - } 1507 - 1508 - if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) && 1509 - (usage->code == ABS_VOLUME)) { 1510 - int count = abs(value); 1511 - int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN; 1512 - int i; 1513 - 1514 - for (i = 0; i < count; i++) { 1515 - input_event(input, EV_KEY, direction, 1); 1516 - input_sync(input); 1517 - input_event(input, EV_KEY, direction, 0); 1518 - input_sync(input); 1519 - } 1520 1352 return; 1521 1353 } 1522 1354 ··· 1477 1419 * don't specify logical min and max. 1478 1420 */ 1479 1421 if ((field->flags & HID_MAIN_ITEM_VARIABLE) && 1480 - (field->logical_minimum < field->logical_maximum)) { 1422 + field->logical_minimum < field->logical_maximum) { 1481 1423 if (field->flags & HID_MAIN_ITEM_NULL_STATE && 1482 1424 (value < field->logical_minimum || 1483 1425 value > field->logical_maximum)) { ··· 1487 1429 value = clamp(value, 1488 1430 field->logical_minimum, 1489 1431 field->logical_maximum); 1432 + } 1433 + 1434 + switch (usage->hid) { 1435 + case HID_DG_ERASER: 1436 + report->tool_active |= !!value; 1437 + 1438 + /* 1439 + * if eraser is set, we must enforce BTN_TOOL_RUBBER 1440 + * to accommodate for devices not following the spec. 1441 + */ 1442 + if (value) 1443 + hid_report_set_tool(report, input, BTN_TOOL_RUBBER); 1444 + else if (report->tool != BTN_TOOL_RUBBER) 1445 + /* value is off, tool is not rubber, ignore */ 1446 + return; 1447 + 1448 + /* let hid-input set BTN_TOUCH */ 1449 + break; 1450 + 1451 + case HID_DG_INVERT: 1452 + report->tool_active |= !!value; 1453 + 1454 + /* 1455 + * If invert is set, we store BTN_TOOL_RUBBER. 1456 + */ 1457 + if (value) 1458 + hid_report_set_tool(report, input, BTN_TOOL_RUBBER); 1459 + else if (!report->tool_active) 1460 + /* tool_active not set means Invert and Eraser are not set */ 1461 + hid_report_release_tool(report, input, BTN_TOOL_RUBBER); 1462 + 1463 + /* no further processing */ 1464 + return; 1465 + 1466 + case HID_DG_INRANGE: 1467 + report->tool_active |= !!value; 1468 + 1469 + if (report->tool_active) { 1470 + /* 1471 + * if tool is not set but is marked as active, 1472 + * assume ours 1473 + */ 1474 + if (!report->tool) 1475 + hid_report_set_tool(report, input, usage->code); 1476 + } else { 1477 + hid_report_release_tool(report, input, usage->code); 1478 + } 1479 + 1480 + /* reset tool_active for the next event */ 1481 + report->tool_active = false; 1482 + 1483 + /* no further processing */ 1484 + return; 1485 + 1486 + case HID_DG_TIPSWITCH: 1487 + report->tool_active |= !!value; 1488 + 1489 + /* if tool is set to RUBBER we should ignore the current value */ 1490 + if (report->tool == BTN_TOOL_RUBBER) 1491 + return; 1492 + 1493 + break; 1494 + 1495 + case HID_DG_TIPPRESSURE: 1496 + if (*quirks & HID_QUIRK_NOTOUCH) { 1497 + int a = field->logical_minimum; 1498 + int b = field->logical_maximum; 1499 + 1500 + if (value > a + ((b - a) >> 3)) { 1501 + input_event(input, EV_KEY, BTN_TOUCH, 1); 1502 + report->tool_active = true; 1503 + } 1504 + } 1505 + break; 1506 + 1507 + case HID_UP_PID | 0x83UL: /* Simultaneous Effects Max */ 1508 + dbg_hid("Maximum Effects - %d\n",value); 1509 + return; 1510 + 1511 + case HID_UP_PID | 0x7fUL: 1512 + dbg_hid("PID Pool Report\n"); 1513 + return; 1514 + } 1515 + 1516 + switch (usage->type) { 1517 + case EV_KEY: 1518 + if (usage->code == 0) /* Key 0 is "unassigned", not KEY_UNKNOWN */ 1519 + return; 1520 + break; 1521 + 1522 + case EV_REL: 1523 + if (usage->code == REL_WHEEL_HI_RES || 1524 + usage->code == REL_HWHEEL_HI_RES) { 1525 + hidinput_handle_scroll(usage, input, value); 1526 + return; 1527 + } 1528 + break; 1529 + 1530 + case EV_ABS: 1531 + if ((field->flags & HID_MAIN_ITEM_RELATIVE) && 1532 + usage->code == ABS_VOLUME) { 1533 + int count = abs(value); 1534 + int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN; 1535 + int i; 1536 + 1537 + for (i = 0; i < count; i++) { 1538 + input_event(input, EV_KEY, direction, 1); 1539 + input_sync(input); 1540 + input_event(input, EV_KEY, direction, 0); 1541 + input_sync(input); 1542 + } 1543 + return; 1544 + 1545 + } else if (((*quirks & HID_QUIRK_X_INVERT) && usage->code == ABS_X) || 1546 + ((*quirks & HID_QUIRK_Y_INVERT) && usage->code == ABS_Y)) 1547 + value = field->logical_maximum - value; 1548 + break; 1490 1549 } 1491 1550 1492 1551 /* ··· 2108 1933 static inline void hidinput_configure_usages(struct hid_input *hidinput, 2109 1934 struct hid_report *report) 2110 1935 { 2111 - int i, j; 1936 + int i, j, k; 1937 + int first_field_index = 0; 1938 + int slot_collection_index = -1; 1939 + int prev_collection_index = -1; 1940 + unsigned int slot_idx = 0; 1941 + struct hid_field *field; 1942 + 1943 + /* 1944 + * First tag all the fields that are part of a slot, 1945 + * a slot needs to have one Contact ID in the collection 1946 + */ 1947 + for (i = 0; i < report->maxfield; i++) { 1948 + field = report->field[i]; 1949 + 1950 + /* ignore fields without usage */ 1951 + if (field->maxusage < 1) 1952 + continue; 1953 + 1954 + /* 1955 + * janitoring when collection_index changes 1956 + */ 1957 + if (prev_collection_index != field->usage->collection_index) { 1958 + prev_collection_index = field->usage->collection_index; 1959 + first_field_index = i; 1960 + } 1961 + 1962 + /* 1963 + * if we already found a Contact ID in the collection, 1964 + * tag and continue to the next. 1965 + */ 1966 + if (slot_collection_index == field->usage->collection_index) { 1967 + field->slot_idx = slot_idx; 1968 + continue; 1969 + } 1970 + 1971 + /* check if the current field has Contact ID */ 1972 + for (j = 0; j < field->maxusage; j++) { 1973 + if (field->usage[j].hid == HID_DG_CONTACTID) { 1974 + slot_collection_index = field->usage->collection_index; 1975 + slot_idx++; 1976 + 1977 + /* 1978 + * mark all previous fields and this one in the 1979 + * current collection to be slotted. 1980 + */ 1981 + for (k = first_field_index; k <= i; k++) 1982 + report->field[k]->slot_idx = slot_idx; 1983 + break; 1984 + } 1985 + } 1986 + } 2112 1987 2113 1988 for (i = 0; i < report->maxfield; i++) 2114 1989 for (j = 0; j < report->field[i]->maxusage; j++) 2115 1990 hidinput_configure_usage(hidinput, report->field[i], 2116 - report->field[i]->usage + j); 1991 + report->field[i]->usage + j, 1992 + j); 2117 1993 } 2118 1994 2119 1995 /*
+16
drivers/hid/hid-quirks.c
··· 295 295 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI) }, 296 296 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ISO) }, 297 297 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS) }, 298 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K) }, 299 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132) }, 300 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680) }, 301 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213) }, 302 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K) }, 303 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223) }, 304 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K) }, 305 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F) }, 298 306 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI) }, 299 307 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO) }, 300 308 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS) }, ··· 938 930 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI) }, 939 931 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ISO) }, 940 932 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS) }, 933 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K) }, 934 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132) }, 935 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680) }, 936 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213) }, 937 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K) }, 938 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223) }, 939 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K) }, 940 + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F) }, 941 941 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) }, 942 942 { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) }, 943 943 { }
+125
drivers/hid/hid-razer.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * HID driver for gaming keys on Razer Blackwidow gaming keyboards 4 + * Macro Key Keycodes: M1 = 191, M2 = 192, M3 = 193, M4 = 194, M5 = 195 5 + * 6 + * Copyright (c) 2021 Jelle van der Waa <jvanderwaa@redhat.com> 7 + */ 8 + 9 + #include <linux/device.h> 10 + #include <linux/hid.h> 11 + #include <linux/module.h> 12 + #include <linux/random.h> 13 + #include <linux/sched.h> 14 + #include <linux/usb.h> 15 + #include <linux/wait.h> 16 + 17 + #include "hid-ids.h" 18 + 19 + #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c)) 20 + 21 + #define RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE 91 22 + 23 + static bool macro_key_remapping = 1; 24 + module_param(macro_key_remapping, bool, 0644); 25 + MODULE_PARM_DESC(macro_key_remapping, " on (Y) off (N)"); 26 + 27 + 28 + static unsigned char blackwidow_init[RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE] = { 29 + 0x00, 30 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 31 + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 32 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 33 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 34 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 35 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 37 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 39 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 + 0x04, 0x00 42 + }; 43 + 44 + static int razer_input_mapping(struct hid_device *hdev, 45 + struct hid_input *hi, struct hid_field *field, 46 + struct hid_usage *usage, unsigned long **bit, int *max) 47 + { 48 + 49 + if (!macro_key_remapping) 50 + return 0; 51 + 52 + if ((usage->hid & HID_UP_KEYBOARD) != HID_UP_KEYBOARD) 53 + return 0; 54 + 55 + switch (usage->hid & ~HID_UP_KEYBOARD) { 56 + case 0x68: 57 + map_key_clear(KEY_MACRO1); 58 + return 1; 59 + case 0x69: 60 + map_key_clear(KEY_MACRO2); 61 + return 1; 62 + case 0x6a: 63 + map_key_clear(KEY_MACRO3); 64 + return 1; 65 + case 0x6b: 66 + map_key_clear(KEY_MACRO4); 67 + return 1; 68 + case 0x6c: 69 + map_key_clear(KEY_MACRO5); 70 + return 1; 71 + } 72 + 73 + return 0; 74 + } 75 + 76 + static int razer_probe(struct hid_device *hdev, const struct hid_device_id *id) 77 + { 78 + char *buf; 79 + int ret = 0; 80 + 81 + ret = hid_parse(hdev); 82 + if (ret) 83 + return ret; 84 + 85 + /* 86 + * Only send the enable macro keys command for the third device 87 + * identified as mouse input. 88 + */ 89 + if (hdev->type == HID_TYPE_USBMOUSE) { 90 + buf = kmemdup(blackwidow_init, RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE, GFP_KERNEL); 91 + if (buf == NULL) 92 + return -ENOMEM; 93 + 94 + ret = hid_hw_raw_request(hdev, 0, buf, RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE, 95 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 96 + if (ret != RAZER_BLACKWIDOW_TRANSFER_BUF_SIZE) 97 + hid_err(hdev, "failed to enable macro keys: %d\n", ret); 98 + 99 + kfree(buf); 100 + } 101 + 102 + return hid_hw_start(hdev, HID_CONNECT_DEFAULT); 103 + } 104 + 105 + static const struct hid_device_id razer_devices[] = { 106 + { HID_USB_DEVICE(USB_VENDOR_ID_RAZER, 107 + USB_DEVICE_ID_RAZER_BLACKWIDOW) }, 108 + { HID_USB_DEVICE(USB_VENDOR_ID_RAZER, 109 + USB_DEVICE_ID_RAZER_BLACKWIDOW_CLASSIC) }, 110 + { HID_USB_DEVICE(USB_VENDOR_ID_RAZER, 111 + USB_DEVICE_ID_RAZER_BLACKWIDOW_ULTIMATE) }, 112 + { } 113 + }; 114 + MODULE_DEVICE_TABLE(hid, razer_devices); 115 + 116 + static struct hid_driver razer_driver = { 117 + .name = "razer", 118 + .id_table = razer_devices, 119 + .input_mapping = razer_input_mapping, 120 + .probe = razer_probe, 121 + }; 122 + module_hid_driver(razer_driver); 123 + 124 + MODULE_AUTHOR("Jelle van der Waa <jvanderwaa@redhat.com>"); 125 + MODULE_LICENSE("GPL");
+130
drivers/hid/hid-sigmamicro.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * HID driver for SiGma Micro-based keyboards 4 + * 5 + * Copyright (c) 2016 Kinglong Mee 6 + * Copyright (c) 2021 Desmond Lim 7 + */ 8 + 9 + #include <linux/device.h> 10 + #include <linux/hid.h> 11 + #include <linux/module.h> 12 + 13 + #include "hid-ids.h" 14 + 15 + static const __u8 sm_0059_rdesc[] = { 16 + 0x05, 0x0c, /* Usage Page (Consumer Devices) 0 */ 17 + 0x09, 0x01, /* Usage (Consumer Control) 2 */ 18 + 0xa1, 0x01, /* Collection (Application) 4 */ 19 + 0x85, 0x01, /* Report ID (1) 6 */ 20 + 0x19, 0x00, /* Usage Minimum (0) 8 */ 21 + 0x2a, 0x3c, 0x02, /* Usage Maximum (572) 10 */ 22 + 0x15, 0x00, /* Logical Minimum (0) 13 */ 23 + 0x26, 0x3c, 0x02, /* Logical Maximum (572) 15 */ 24 + 0x95, 0x01, /* Report Count (1) 18 */ 25 + 0x75, 0x10, /* Report Size (16) 20 */ 26 + 0x81, 0x00, /* Input (Data,Arr,Abs) 22 */ 27 + 0xc0, /* End Collection 24 */ 28 + 0x05, 0x01, /* Usage Page (Generic Desktop) 25 */ 29 + 0x09, 0x80, /* Usage (System Control) 27 */ 30 + 0xa1, 0x01, /* Collection (Application) 29 */ 31 + 0x85, 0x02, /* Report ID (2) 31 */ 32 + 0x19, 0x81, /* Usage Minimum (129) 33 */ 33 + 0x29, 0x83, /* Usage Maximum (131) 35 */ 34 + 0x25, 0x01, /* Logical Maximum (1) 37 */ 35 + 0x75, 0x01, /* Report Size (1) 39 */ 36 + 0x95, 0x03, /* Report Count (3) 41 */ 37 + 0x81, 0x02, /* Input (Data,Var,Abs) 43 */ 38 + 0x95, 0x05, /* Report Count (5) 45 */ 39 + 0x81, 0x01, /* Input (Cnst,Arr,Abs) 47 */ 40 + 0xc0, /* End Collection 49 */ 41 + 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) 50 */ 42 + 0x09, 0x01, /* Usage (Vendor Usage 1) 53 */ 43 + 0xa1, 0x01, /* Collection (Application) 55 */ 44 + 0x85, 0x03, /* Report ID (3) 57 */ 45 + 0x1a, 0xf1, 0x00, /* Usage Minimum (241) 59 */ 46 + 0x2a, 0xf8, 0x00, /* Usage Maximum (248) 62 */ 47 + 0x15, 0x00, /* Logical Minimum (0) 65 */ 48 + 0x25, 0x01, /* Logical Maximum (1) 67 */ 49 + 0x75, 0x01, /* Report Size (1) 69 */ 50 + 0x95, 0x08, /* Report Count (8) 71 */ 51 + 0x81, 0x02, /* Input (Data,Var,Abs) 73 */ 52 + 0xc0, /* End Collection 75 */ 53 + 0x05, 0x01, /* Usage Page (Generic Desktop) 76 */ 54 + 0x09, 0x06, /* Usage (Keyboard) 78 */ 55 + 0xa1, 0x01, /* Collection (Application) 80 */ 56 + 0x85, 0x04, /* Report ID (4) 82 */ 57 + 0x05, 0x07, /* Usage Page (Keyboard) 84 */ 58 + 0x19, 0xe0, /* Usage Minimum (224) 86 */ 59 + 0x29, 0xe7, /* Usage Maximum (231) 88 */ 60 + 0x15, 0x00, /* Logical Minimum (0) 90 */ 61 + 0x25, 0x01, /* Logical Maximum (1) 92 */ 62 + 0x75, 0x01, /* Report Size (1) 94 */ 63 + 0x95, 0x08, /* Report Count (8) 96 */ 64 + 0x81, 0x00, /* Input (Data,Arr,Abs) 98 */ 65 + 0x95, 0x30, /* Report Count (48) 100 */ 66 + 0x75, 0x01, /* Report Size (1) 102 */ 67 + 0x15, 0x00, /* Logical Minimum (0) 104 */ 68 + 0x25, 0x01, /* Logical Maximum (1) 106 */ 69 + 0x05, 0x07, /* Usage Page (Keyboard) 108 */ 70 + 0x19, 0x00, /* Usage Minimum (0) 110 */ 71 + 0x29, 0x2f, /* Usage Maximum (47) 112 */ 72 + 0x81, 0x02, /* Input (Data,Var,Abs) 114 */ 73 + 0xc0, /* End Collection 116 */ 74 + 0x05, 0x01, /* Usage Page (Generic Desktop) 117 */ 75 + 0x09, 0x06, /* Usage (Keyboard) 119 */ 76 + 0xa1, 0x01, /* Collection (Application) 121 */ 77 + 0x85, 0x05, /* Report ID (5) 123 */ 78 + 0x95, 0x38, /* Report Count (56) 125 */ 79 + 0x75, 0x01, /* Report Size (1) 127 */ 80 + 0x15, 0x00, /* Logical Minimum (0) 129 */ 81 + 0x25, 0x01, /* Logical Maximum (1) 131 */ 82 + 0x05, 0x07, /* Usage Page (Keyboard) 133 */ 83 + 0x19, 0x30, /* Usage Minimum (48) 135 */ 84 + 0x29, 0x67, /* Usage Maximum (103) 137 */ 85 + 0x81, 0x02, /* Input (Data,Var,Abs) 139 */ 86 + 0xc0, /* End Collection 141 */ 87 + 0x05, 0x01, /* Usage Page (Generic Desktop) 142 */ 88 + 0x09, 0x06, /* Usage (Keyboard) 144 */ 89 + 0xa1, 0x01, /* Collection (Application) 146 */ 90 + 0x85, 0x06, /* Report ID (6) 148 */ 91 + 0x95, 0x38, /* Report Count (56) 150 */ 92 + 0x75, 0x01, /* Report Size (1) 152 */ 93 + 0x15, 0x00, /* Logical Minimum (0) 154 */ 94 + 0x25, 0x01, /* Logical Maximum (1) 156 */ 95 + 0x05, 0x07, /* Usage Page (Keyboard) 158 */ 96 + 0x19, 0x68, /* Usage Minimum (104) 160 */ 97 + 0x29, 0x9f, /* Usage Maximum (159) 162 */ 98 + 0x81, 0x02, /* Input (Data,Var,Abs) 164 */ 99 + 0xc0, /* End Collection 166 */ 100 + }; 101 + 102 + static __u8 *sm_report_fixup(struct hid_device *hdev, __u8 *rdesc, 103 + unsigned int *rsize) 104 + { 105 + if (*rsize == sizeof(sm_0059_rdesc) && 106 + !memcmp(sm_0059_rdesc, rdesc, *rsize)) { 107 + hid_info(hdev, "Fixing up SiGma Micro report descriptor\n"); 108 + rdesc[99] = 0x02; 109 + } 110 + return rdesc; 111 + } 112 + 113 + static const struct hid_device_id sm_devices[] = { 114 + { HID_USB_DEVICE(USB_VENDOR_ID_SIGMA_MICRO, 115 + USB_DEVICE_ID_SIGMA_MICRO_KEYBOARD2) }, 116 + { } 117 + }; 118 + MODULE_DEVICE_TABLE(hid, sm_devices); 119 + 120 + static struct hid_driver sm_driver = { 121 + .name = "sigmamicro", 122 + .id_table = sm_devices, 123 + .report_fixup = sm_report_fixup, 124 + }; 125 + module_hid_driver(sm_driver); 126 + 127 + MODULE_AUTHOR("Kinglong Mee <kinglongmee@gmail.com>"); 128 + MODULE_AUTHOR("Desmond Lim <peckishrine@gmail.com>"); 129 + MODULE_DESCRIPTION("SiGma Micro HID driver"); 130 + MODULE_LICENSE("GPL");
+156 -104
drivers/hid/hid-uclogic-core.c
··· 81 81 return rdesc; 82 82 } 83 83 84 - static int uclogic_input_mapping(struct hid_device *hdev, 85 - struct hid_input *hi, 86 - struct hid_field *field, 87 - struct hid_usage *usage, 88 - unsigned long **bit, 89 - int *max) 90 - { 91 - struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 92 - struct uclogic_params *params = &drvdata->params; 93 - 94 - /* discard the unused pen interface */ 95 - if (params->pen_unused && (field->application == HID_DG_PEN)) 96 - return -1; 97 - 98 - /* let hid-core decide what to do */ 99 - return 0; 100 - } 101 - 102 84 static int uclogic_input_configured(struct hid_device *hdev, 103 85 struct hid_input *hi) 104 86 { ··· 228 246 } 229 247 #endif 230 248 249 + /** 250 + * uclogic_raw_event_pen - handle raw pen events (pen HID reports). 251 + * 252 + * @drvdata: Driver data. 253 + * @data: Report data buffer, can be modified. 254 + * @size: Report data size, bytes. 255 + * 256 + * Returns: 257 + * Negative value on error (stops event delivery), zero for success. 258 + */ 259 + static int uclogic_raw_event_pen(struct uclogic_drvdata *drvdata, 260 + u8 *data, int size) 261 + { 262 + struct uclogic_params_pen *pen = &drvdata->params.pen; 263 + 264 + WARN_ON(drvdata == NULL); 265 + WARN_ON(data == NULL && size != 0); 266 + 267 + /* If in-range reports are inverted */ 268 + if (pen->inrange == 269 + UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) { 270 + /* Invert the in-range bit */ 271 + data[1] ^= 0x40; 272 + } 273 + /* 274 + * If report contains fragmented high-resolution pen 275 + * coordinates 276 + */ 277 + if (size >= 10 && pen->fragmented_hires) { 278 + u8 pressure_low_byte; 279 + u8 pressure_high_byte; 280 + 281 + /* Lift pressure bytes */ 282 + pressure_low_byte = data[6]; 283 + pressure_high_byte = data[7]; 284 + /* 285 + * Move Y coord to make space for high-order X 286 + * coord byte 287 + */ 288 + data[6] = data[5]; 289 + data[5] = data[4]; 290 + /* Move high-order X coord byte */ 291 + data[4] = data[8]; 292 + /* Move high-order Y coord byte */ 293 + data[7] = data[9]; 294 + /* Place pressure bytes */ 295 + data[8] = pressure_low_byte; 296 + data[9] = pressure_high_byte; 297 + } 298 + /* If we need to emulate in-range detection */ 299 + if (pen->inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) { 300 + /* Set in-range bit */ 301 + data[1] |= 0x40; 302 + /* (Re-)start in-range timeout */ 303 + mod_timer(&drvdata->inrange_timer, 304 + jiffies + msecs_to_jiffies(100)); 305 + } 306 + /* If we report tilt and Y direction is flipped */ 307 + if (size >= 12 && pen->tilt_y_flipped) 308 + data[11] = -data[11]; 309 + 310 + return 0; 311 + } 312 + 313 + /** 314 + * uclogic_raw_event_frame - handle raw frame events (frame HID reports). 315 + * 316 + * @drvdata: Driver data. 317 + * @frame: The parameters of the frame controls to handle. 318 + * @data: Report data buffer, can be modified. 319 + * @size: Report data size, bytes. 320 + * 321 + * Returns: 322 + * Negative value on error (stops event delivery), zero for success. 323 + */ 324 + static int uclogic_raw_event_frame( 325 + struct uclogic_drvdata *drvdata, 326 + const struct uclogic_params_frame *frame, 327 + u8 *data, int size) 328 + { 329 + WARN_ON(drvdata == NULL); 330 + WARN_ON(data == NULL && size != 0); 331 + 332 + /* If need to, and can, set pad device ID for Wacom drivers */ 333 + if (frame->dev_id_byte > 0 && frame->dev_id_byte < size) { 334 + data[frame->dev_id_byte] = 0xf; 335 + } 336 + /* If need to, and can, read rotary encoder state change */ 337 + if (frame->re_lsb > 0 && frame->re_lsb / 8 < size) { 338 + unsigned int byte = frame->re_lsb / 8; 339 + unsigned int bit = frame->re_lsb % 8; 340 + 341 + u8 change; 342 + u8 prev_state = drvdata->re_state; 343 + /* Read Gray-coded state */ 344 + u8 state = (data[byte] >> bit) & 0x3; 345 + /* Encode state change into 2-bit signed integer */ 346 + if ((prev_state == 1 && state == 0) || 347 + (prev_state == 2 && state == 3)) { 348 + change = 1; 349 + } else if ((prev_state == 2 && state == 0) || 350 + (prev_state == 1 && state == 3)) { 351 + change = 3; 352 + } else { 353 + change = 0; 354 + } 355 + /* Write change */ 356 + data[byte] = (data[byte] & ~((u8)3 << bit)) | 357 + (change << bit); 358 + /* Remember state */ 359 + drvdata->re_state = state; 360 + } 361 + 362 + return 0; 363 + } 364 + 231 365 static int uclogic_raw_event(struct hid_device *hdev, 232 366 struct hid_report *report, 233 367 u8 *data, int size) 234 368 { 369 + unsigned int report_id = report->id; 235 370 struct uclogic_drvdata *drvdata = hid_get_drvdata(hdev); 236 371 struct uclogic_params *params = &drvdata->params; 372 + struct uclogic_params_pen_subreport *subreport; 373 + struct uclogic_params_pen_subreport *subreport_list_end; 374 + size_t i; 237 375 238 - /* Tweak pen reports, if necessary */ 239 - if (!params->pen_unused && 240 - (report->type == HID_INPUT_REPORT) && 241 - (report->id == params->pen.id) && 242 - (size >= 2)) { 243 - /* If it's the "virtual" frame controls report */ 244 - if (params->frame.id != 0 && 245 - data[1] & params->pen_frame_flag) { 246 - /* Change to virtual frame controls report ID */ 247 - data[0] = params->frame.id; 248 - return 0; 249 - } 250 - /* If in-range reports are inverted */ 251 - if (params->pen.inrange == 252 - UCLOGIC_PARAMS_PEN_INRANGE_INVERTED) { 253 - /* Invert the in-range bit */ 254 - data[1] ^= 0x40; 255 - } 256 - /* 257 - * If report contains fragmented high-resolution pen 258 - * coordinates 259 - */ 260 - if (size >= 10 && params->pen.fragmented_hires) { 261 - u8 pressure_low_byte; 262 - u8 pressure_high_byte; 376 + /* Do not handle anything but input reports */ 377 + if (report->type != HID_INPUT_REPORT) 378 + return 0; 263 379 264 - /* Lift pressure bytes */ 265 - pressure_low_byte = data[6]; 266 - pressure_high_byte = data[7]; 267 - /* 268 - * Move Y coord to make space for high-order X 269 - * coord byte 270 - */ 271 - data[6] = data[5]; 272 - data[5] = data[4]; 273 - /* Move high-order X coord byte */ 274 - data[4] = data[8]; 275 - /* Move high-order Y coord byte */ 276 - data[7] = data[9]; 277 - /* Place pressure bytes */ 278 - data[8] = pressure_low_byte; 279 - data[9] = pressure_high_byte; 280 - } 281 - /* If we need to emulate in-range detection */ 282 - if (params->pen.inrange == UCLOGIC_PARAMS_PEN_INRANGE_NONE) { 283 - /* Set in-range bit */ 284 - data[1] |= 0x40; 285 - /* (Re-)start in-range timeout */ 286 - mod_timer(&drvdata->inrange_timer, 287 - jiffies + msecs_to_jiffies(100)); 288 - } 289 - } 290 - 291 - /* Tweak frame control reports, if necessary */ 292 - if ((report->type == HID_INPUT_REPORT) && 293 - (report->id == params->frame.id)) { 294 - /* If need to, and can, set pad device ID for Wacom drivers */ 295 - if (params->frame.dev_id_byte > 0 && 296 - params->frame.dev_id_byte < size) { 297 - data[params->frame.dev_id_byte] = 0xf; 298 - } 299 - /* If need to, and can, read rotary encoder state change */ 300 - if (params->frame.re_lsb > 0 && 301 - params->frame.re_lsb / 8 < size) { 302 - unsigned int byte = params->frame.re_lsb / 8; 303 - unsigned int bit = params->frame.re_lsb % 8; 304 - 305 - u8 change; 306 - u8 prev_state = drvdata->re_state; 307 - /* Read Gray-coded state */ 308 - u8 state = (data[byte] >> bit) & 0x3; 309 - /* Encode state change into 2-bit signed integer */ 310 - if ((prev_state == 1 && state == 0) || 311 - (prev_state == 2 && state == 3)) { 312 - change = 1; 313 - } else if ((prev_state == 2 && state == 0) || 314 - (prev_state == 1 && state == 3)) { 315 - change = 3; 316 - } else { 317 - change = 0; 380 + while (true) { 381 + /* Tweak pen reports, if necessary */ 382 + if ((report_id == params->pen.id) && (size >= 2)) { 383 + subreport_list_end = 384 + params->pen.subreport_list + 385 + ARRAY_SIZE(params->pen.subreport_list); 386 + /* Try to match a subreport */ 387 + for (subreport = params->pen.subreport_list; 388 + subreport < subreport_list_end; subreport++) { 389 + if (subreport->value != 0 && 390 + subreport->value == data[1]) { 391 + break; 392 + } 318 393 } 319 - /* Write change */ 320 - data[byte] = (data[byte] & ~((u8)3 << bit)) | 321 - (change << bit); 322 - /* Remember state */ 323 - drvdata->re_state = state; 394 + /* If a subreport matched */ 395 + if (subreport < subreport_list_end) { 396 + /* Change to subreport ID, and restart */ 397 + report_id = data[0] = subreport->id; 398 + continue; 399 + } else { 400 + return uclogic_raw_event_pen(drvdata, data, size); 401 + } 324 402 } 403 + 404 + /* Tweak frame control reports, if necessary */ 405 + for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) { 406 + if (report_id == params->frame_list[i].id) { 407 + return uclogic_raw_event_frame( 408 + drvdata, &params->frame_list[i], 409 + data, size); 410 + } 411 + } 412 + 413 + break; 325 414 } 326 415 327 416 return 0; ··· 426 373 { HID_USB_DEVICE(USB_VENDOR_ID_HUION, 427 374 USB_DEVICE_ID_HUION_TABLET) }, 428 375 { HID_USB_DEVICE(USB_VENDOR_ID_HUION, 429 - USB_DEVICE_ID_HUION_HS64) }, 376 + USB_DEVICE_ID_HUION_TABLET2) }, 430 377 { HID_USB_DEVICE(USB_VENDOR_ID_TRUST, 431 378 USB_DEVICE_ID_TRUST_PANORA_TABLET) }, 432 379 { HID_USB_DEVICE(USB_VENDOR_ID_UCLOGIC, ··· 468 415 .remove = uclogic_remove, 469 416 .report_fixup = uclogic_report_fixup, 470 417 .raw_event = uclogic_raw_event, 471 - .input_mapping = uclogic_input_mapping, 472 418 .input_configured = uclogic_input_configured, 473 419 #ifdef CONFIG_PM 474 420 .resume = uclogic_resume,
+90 -110
drivers/hid/hid-uclogic-params.c
··· 207 207 * Generate pen report descriptor 208 208 */ 209 209 desc_ptr = uclogic_rdesc_template_apply( 210 - uclogic_rdesc_pen_v1_template_arr, 211 - uclogic_rdesc_pen_v1_template_size, 210 + uclogic_rdesc_v1_pen_template_arr, 211 + uclogic_rdesc_v1_pen_template_size, 212 212 desc_params, ARRAY_SIZE(desc_params)); 213 213 if (desc_ptr == NULL) { 214 214 rc = -ENOMEM; ··· 221 221 memset(pen, 0, sizeof(*pen)); 222 222 pen->desc_ptr = desc_ptr; 223 223 desc_ptr = NULL; 224 - pen->desc_size = uclogic_rdesc_pen_v1_template_size; 225 - pen->id = UCLOGIC_RDESC_PEN_V1_ID; 224 + pen->desc_size = uclogic_rdesc_v1_pen_template_size; 225 + pen->id = UCLOGIC_RDESC_V1_PEN_ID; 226 226 pen->inrange = UCLOGIC_PARAMS_PEN_INRANGE_INVERTED; 227 227 found = true; 228 228 finish: ··· 351 351 * Generate pen report descriptor 352 352 */ 353 353 desc_ptr = uclogic_rdesc_template_apply( 354 - uclogic_rdesc_pen_v2_template_arr, 355 - uclogic_rdesc_pen_v2_template_size, 354 + uclogic_rdesc_v2_pen_template_arr, 355 + uclogic_rdesc_v2_pen_template_size, 356 356 desc_params, ARRAY_SIZE(desc_params)); 357 357 if (desc_ptr == NULL) { 358 358 rc = -ENOMEM; ··· 365 365 memset(pen, 0, sizeof(*pen)); 366 366 pen->desc_ptr = desc_ptr; 367 367 desc_ptr = NULL; 368 - pen->desc_size = uclogic_rdesc_pen_v2_template_size; 369 - pen->id = UCLOGIC_RDESC_PEN_V2_ID; 368 + pen->desc_size = uclogic_rdesc_v2_pen_template_size; 369 + pen->id = UCLOGIC_RDESC_V2_PEN_ID; 370 370 pen->inrange = UCLOGIC_PARAMS_PEN_INRANGE_NONE; 371 371 pen->fragmented_hires = true; 372 + pen->tilt_y_flipped = true; 372 373 found = true; 373 374 finish: 374 375 *pfound = found; ··· 431 430 } 432 431 433 432 /** 434 - * uclogic_params_frame_init_v1_buttonpad() - initialize abstract buttonpad 435 - * on a v1 tablet interface. 433 + * uclogic_params_frame_init_v1() - initialize v1 tablet interface frame 434 + * controls. 436 435 * 437 436 * @frame: Pointer to the frame parameters to initialize (to be cleaned 438 437 * up with uclogic_params_frame_cleanup()). Not modified in case ··· 446 445 * Returns: 447 446 * Zero, if successful. A negative errno code on error. 448 447 */ 449 - static int uclogic_params_frame_init_v1_buttonpad( 450 - struct uclogic_params_frame *frame, 448 + static int uclogic_params_frame_init_v1(struct uclogic_params_frame *frame, 451 449 bool *pfound, 452 450 struct hid_device *hdev) 453 451 { ··· 487 487 hid_dbg(hdev, "generic buttons enabled\n"); 488 488 rc = uclogic_params_frame_init_with_desc( 489 489 frame, 490 - uclogic_rdesc_buttonpad_v1_arr, 491 - uclogic_rdesc_buttonpad_v1_size, 492 - UCLOGIC_RDESC_BUTTONPAD_V1_ID); 490 + uclogic_rdesc_v1_frame_arr, 491 + uclogic_rdesc_v1_frame_size, 492 + UCLOGIC_RDESC_V1_FRAME_ID); 493 493 if (rc != 0) 494 494 goto cleanup; 495 495 found = true; ··· 512 512 void uclogic_params_cleanup(struct uclogic_params *params) 513 513 { 514 514 if (!params->invalid) { 515 + size_t i; 515 516 kfree(params->desc_ptr); 516 - if (!params->pen_unused) 517 - uclogic_params_pen_cleanup(&params->pen); 518 - uclogic_params_frame_cleanup(&params->frame); 517 + uclogic_params_pen_cleanup(&params->pen); 518 + for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) 519 + uclogic_params_frame_cleanup(&params->frame_list[i]); 520 + 519 521 memset(params, 0, sizeof(*params)); 520 522 } 521 523 } ··· 545 543 __u8 **pdesc, 546 544 unsigned int *psize) 547 545 { 548 - bool common_present; 549 - bool pen_present; 550 - bool frame_present; 551 - unsigned int size; 546 + int rc = -ENOMEM; 547 + bool present = false; 548 + unsigned int size = 0; 552 549 __u8 *desc = NULL; 550 + size_t i; 553 551 554 552 /* Check arguments */ 555 553 if (params == NULL || pdesc == NULL || psize == NULL) 556 554 return -EINVAL; 557 555 558 - size = 0; 556 + /* Concatenate descriptors */ 557 + #define ADD_DESC(_desc_ptr, _desc_size) \ 558 + do { \ 559 + unsigned int new_size; \ 560 + __u8 *new_desc; \ 561 + if ((_desc_ptr) == NULL) { \ 562 + break; \ 563 + } \ 564 + new_size = size + (_desc_size); \ 565 + new_desc = krealloc(desc, new_size, GFP_KERNEL); \ 566 + if (new_desc == NULL) { \ 567 + goto cleanup; \ 568 + } \ 569 + memcpy(new_desc + size, (_desc_ptr), (_desc_size)); \ 570 + desc = new_desc; \ 571 + size = new_size; \ 572 + present = true; \ 573 + } while (0) 559 574 560 - common_present = (params->desc_ptr != NULL); 561 - pen_present = (!params->pen_unused && params->pen.desc_ptr != NULL); 562 - frame_present = (params->frame.desc_ptr != NULL); 563 - 564 - if (common_present) 565 - size += params->desc_size; 566 - if (pen_present) 567 - size += params->pen.desc_size; 568 - if (frame_present) 569 - size += params->frame.desc_size; 570 - 571 - if (common_present || pen_present || frame_present) { 572 - __u8 *p; 573 - 574 - desc = kmalloc(size, GFP_KERNEL); 575 - if (desc == NULL) 576 - return -ENOMEM; 577 - p = desc; 578 - 579 - if (common_present) { 580 - memcpy(p, params->desc_ptr, 581 - params->desc_size); 582 - p += params->desc_size; 583 - } 584 - if (pen_present) { 585 - memcpy(p, params->pen.desc_ptr, 586 - params->pen.desc_size); 587 - p += params->pen.desc_size; 588 - } 589 - if (frame_present) { 590 - memcpy(p, params->frame.desc_ptr, 591 - params->frame.desc_size); 592 - p += params->frame.desc_size; 593 - } 594 - 595 - WARN_ON(p != desc + size); 596 - 597 - *psize = size; 575 + ADD_DESC(params->desc_ptr, params->desc_size); 576 + ADD_DESC(params->pen.desc_ptr, params->pen.desc_size); 577 + for (i = 0; i < ARRAY_SIZE(params->frame_list); i++) { 578 + ADD_DESC(params->frame_list[i].desc_ptr, 579 + params->frame_list[i].desc_size); 598 580 } 599 581 600 - *pdesc = desc; 601 - return 0; 582 + #undef ADD_DESC 583 + 584 + if (present) { 585 + *pdesc = desc; 586 + *psize = size; 587 + desc = NULL; 588 + } 589 + rc = 0; 590 + cleanup: 591 + kfree(desc); 592 + return rc; 602 593 } 603 594 604 595 /** ··· 675 680 } 676 681 677 682 /** 678 - * uclogic_params_init_with_pen_unused() - initialize tablet interface 679 - * parameters preserving original reports and generic HID processing, but 680 - * disabling pen usage. 681 - * 682 - * @params: Parameters to initialize (to be cleaned with 683 - * uclogic_params_cleanup()). Not modified in case of 684 - * error. Cannot be NULL. 685 - */ 686 - static void uclogic_params_init_with_pen_unused(struct uclogic_params *params) 687 - { 688 - memset(params, 0, sizeof(*params)); 689 - params->pen_unused = true; 690 - } 691 - 692 - /** 693 683 * uclogic_params_huion_init() - initialize a Huion tablet interface and discover 694 684 * its parameters. 695 685 * ··· 713 733 714 734 /* If it's not a pen interface */ 715 735 if (bInterfaceNumber != 0) { 716 - /* TODO: Consider marking the interface invalid */ 717 - uclogic_params_init_with_pen_unused(&p); 736 + uclogic_params_init_invalid(&p); 718 737 goto output; 719 738 } 720 739 ··· 745 766 goto cleanup; 746 767 } else if (found) { 747 768 hid_dbg(hdev, "pen v2 parameters found\n"); 748 - /* Create v2 buttonpad parameters */ 769 + /* Create v2 frame parameters */ 749 770 rc = uclogic_params_frame_init_with_desc( 750 - &p.frame, 751 - uclogic_rdesc_buttonpad_v2_arr, 752 - uclogic_rdesc_buttonpad_v2_size, 753 - UCLOGIC_RDESC_BUTTONPAD_V2_ID); 771 + &p.frame_list[0], 772 + uclogic_rdesc_v2_frame_arr, 773 + uclogic_rdesc_v2_frame_size, 774 + UCLOGIC_RDESC_V2_FRAME_ID); 754 775 if (rc != 0) { 755 776 hid_err(hdev, 756 - "failed creating v2 buttonpad parameters: %d\n", 777 + "failed creating v2 frame parameters: %d\n", 757 778 rc); 758 779 goto cleanup; 759 780 } 760 - /* Set bitmask marking frame reports in pen reports */ 761 - p.pen_frame_flag = 0x20; 781 + /* Link frame button subreports from pen reports */ 782 + p.pen.subreport_list[0].value = 0xe0; 783 + p.pen.subreport_list[0].id = 784 + UCLOGIC_RDESC_V2_FRAME_ID; 762 785 goto output; 763 786 } 764 787 hid_dbg(hdev, "pen v2 parameters not found\n"); ··· 774 793 goto cleanup; 775 794 } else if (found) { 776 795 hid_dbg(hdev, "pen v1 parameters found\n"); 777 - /* Try to probe v1 buttonpad */ 778 - rc = uclogic_params_frame_init_v1_buttonpad( 779 - &p.frame, 780 - &found, hdev); 796 + /* Try to probe v1 frame */ 797 + rc = uclogic_params_frame_init_v1(&p.frame_list[0], 798 + &found, hdev); 781 799 if (rc != 0) { 782 - hid_err(hdev, "v1 buttonpad probing failed: %d\n", rc); 800 + hid_err(hdev, "v1 frame probing failed: %d\n", rc); 783 801 goto cleanup; 784 802 } 785 - hid_dbg(hdev, "buttonpad v1 parameters%s found\n", 803 + hid_dbg(hdev, "frame v1 parameters%s found\n", 786 804 (found ? "" : " not")); 787 805 if (found) { 788 - /* Set bitmask marking frame reports */ 789 - p.pen_frame_flag = 0x20; 806 + /* Link frame button subreports from pen reports */ 807 + p.pen.subreport_list[0].value = 0xe0; 808 + p.pen.subreport_list[0].id = 809 + UCLOGIC_RDESC_V1_FRAME_ID; 790 810 } 791 811 goto output; 792 812 } ··· 974 992 case VID_PID(USB_VENDOR_ID_HUION, 975 993 USB_DEVICE_ID_HUION_TABLET): 976 994 case VID_PID(USB_VENDOR_ID_HUION, 977 - USB_DEVICE_ID_HUION_HS64): 995 + USB_DEVICE_ID_HUION_TABLET2): 978 996 case VID_PID(USB_VENDOR_ID_UCLOGIC, 979 997 USB_DEVICE_ID_HUION_TABLET): 980 998 case VID_PID(USB_VENDOR_ID_UCLOGIC, ··· 1014 1032 uclogic_params_init_invalid(&p); 1015 1033 } 1016 1034 } else { 1017 - /* TODO: Consider marking the interface invalid */ 1018 - uclogic_params_init_with_pen_unused(&p); 1035 + uclogic_params_init_invalid(&p); 1019 1036 } 1020 1037 break; 1021 1038 case VID_PID(USB_VENDOR_ID_UGEE, ··· 1029 1048 } 1030 1049 /* Initialize frame parameters */ 1031 1050 rc = uclogic_params_frame_init_with_desc( 1032 - &p.frame, 1051 + &p.frame_list[0], 1033 1052 uclogic_rdesc_xppen_deco01_frame_arr, 1034 1053 uclogic_rdesc_xppen_deco01_frame_size, 1035 1054 0); 1036 1055 if (rc != 0) 1037 1056 goto cleanup; 1038 1057 } else { 1039 - /* TODO: Consider marking the interface invalid */ 1040 - uclogic_params_init_with_pen_unused(&p); 1058 + uclogic_params_init_invalid(&p); 1041 1059 } 1042 1060 break; 1043 1061 case VID_PID(USB_VENDOR_ID_TRUST, ··· 1055 1075 goto cleanup; 1056 1076 } else if (found) { 1057 1077 rc = uclogic_params_frame_init_with_desc( 1058 - &p.frame, 1078 + &p.frame_list[0], 1059 1079 uclogic_rdesc_ugee_g5_frame_arr, 1060 1080 uclogic_rdesc_ugee_g5_frame_size, 1061 1081 UCLOGIC_RDESC_UGEE_G5_FRAME_ID); 1062 1082 if (rc != 0) { 1063 1083 hid_err(hdev, 1064 - "failed creating buttonpad parameters: %d\n", 1084 + "failed creating frame parameters: %d\n", 1065 1085 rc); 1066 1086 goto cleanup; 1067 1087 } 1068 - p.frame.re_lsb = 1088 + p.frame_list[0].re_lsb = 1069 1089 UCLOGIC_RDESC_UGEE_G5_FRAME_RE_LSB; 1070 - p.frame.dev_id_byte = 1090 + p.frame_list[0].dev_id_byte = 1071 1091 UCLOGIC_RDESC_UGEE_G5_FRAME_DEV_ID_BYTE; 1072 1092 } else { 1073 1093 hid_warn(hdev, "pen parameters not found"); ··· 1089 1109 goto cleanup; 1090 1110 } else if (found) { 1091 1111 rc = uclogic_params_frame_init_with_desc( 1092 - &p.frame, 1093 - uclogic_rdesc_ugee_ex07_buttonpad_arr, 1094 - uclogic_rdesc_ugee_ex07_buttonpad_size, 1112 + &p.frame_list[0], 1113 + uclogic_rdesc_ugee_ex07_frame_arr, 1114 + uclogic_rdesc_ugee_ex07_frame_size, 1095 1115 0); 1096 1116 if (rc != 0) { 1097 1117 hid_err(hdev, 1098 - "failed creating buttonpad parameters: %d\n", 1118 + "failed creating frame parameters: %d\n", 1099 1119 rc); 1100 1120 goto cleanup; 1101 1121 }
+54 -38
drivers/hid/hid-uclogic-params.h
··· 33 33 extern const char *uclogic_params_pen_inrange_to_str( 34 34 enum uclogic_params_pen_inrange inrange); 35 35 36 + 37 + /* 38 + * Pen report's subreport data. 39 + */ 40 + struct uclogic_params_pen_subreport { 41 + /* 42 + * The value of the second byte of the pen report indicating this 43 + * subreport. If zero, the subreport should be considered invalid and 44 + * not matched. 45 + */ 46 + __u8 value; 47 + 48 + /* 49 + * The ID to be assigned to the report, if the second byte of the pen 50 + * report is equal to "value". Only valid if "value" is not zero. 51 + */ 52 + __u8 id; 53 + }; 54 + 36 55 /* 37 56 * Tablet interface's pen input parameters. 38 57 * ··· 73 54 unsigned int desc_size; 74 55 /* Report ID, if reports should be tweaked, zero if not */ 75 56 unsigned int id; 57 + /* The list of subreports */ 58 + struct uclogic_params_pen_subreport subreport_list[1]; 76 59 /* Type of in-range reporting, only valid if "id" is not zero */ 77 60 enum uclogic_params_pen_inrange inrange; 78 61 /* ··· 83 62 * Only valid if "id" is not zero. 84 63 */ 85 64 bool fragmented_hires; 65 + /* 66 + * True if the pen reports tilt in bytes at offset 10 (X) and 11 (Y), 67 + * and the Y tilt direction is flipped. 68 + * Only valid if "id" is not zero. 69 + */ 70 + bool tilt_y_flipped; 86 71 }; 87 72 88 73 /* ··· 160 133 */ 161 134 unsigned int desc_size; 162 135 /* 163 - * True, if pen usage in report descriptor is invalid, when present. 164 - * Only valid, if "invalid" is false. 165 - */ 166 - bool pen_unused; 167 - /* 168 136 * Pen parameters and optional report descriptor part. 169 - * Only valid if "pen_unused" is valid and false. 137 + * Only valid, if "invalid" is false. 170 138 */ 171 139 struct uclogic_params_pen pen; 172 140 /* 173 - * Frame control parameters and optional report descriptor part. 174 - * Only valid, if "invalid" is false. 141 + * The list of frame control parameters and optional report descriptor 142 + * parts. Only valid, if "invalid" is false. 175 143 */ 176 - struct uclogic_params_frame frame; 177 - /* 178 - * Bitmask matching frame controls "sub-report" flag in the second 179 - * byte of the pen report, or zero if it's not expected. 180 - * Only valid if both "pen" and "frame" are valid, and "frame.id" is 181 - * not zero. 182 - */ 183 - __u8 pen_frame_flag; 144 + struct uclogic_params_frame frame_list[1]; 184 145 }; 185 146 186 147 /* Initialize a tablet interface and discover its parameters */ ··· 177 162 178 163 /* Tablet interface parameters *printf format string */ 179 164 #define UCLOGIC_PARAMS_FMT_STR \ 180 - ".invalid = %s\n" \ 181 - ".desc_ptr = %p\n" \ 182 - ".desc_size = %u\n" \ 183 - ".pen_unused = %s\n" \ 184 - ".pen.desc_ptr = %p\n" \ 185 - ".pen.desc_size = %u\n" \ 186 - ".pen.id = %u\n" \ 187 - ".pen.inrange = %s\n" \ 188 - ".pen.fragmented_hires = %s\n" \ 189 - ".frame.desc_ptr = %p\n" \ 190 - ".frame.desc_size = %u\n" \ 191 - ".frame.id = %u\n" \ 192 - ".frame.re_lsb = %u\n" \ 193 - ".frame.dev_id_byte = %u\n" \ 194 - ".pen_frame_flag = 0x%02x\n" 165 + ".invalid = %s\n" \ 166 + ".desc_ptr = %p\n" \ 167 + ".desc_size = %u\n" \ 168 + ".pen.desc_ptr = %p\n" \ 169 + ".pen.desc_size = %u\n" \ 170 + ".pen.id = %u\n" \ 171 + ".pen.subreport_list[0] = {0x%02hhx, %hhu}\n" \ 172 + ".pen.inrange = %s\n" \ 173 + ".pen.fragmented_hires = %s\n" \ 174 + ".pen.tilt_y_flipped = %s\n" \ 175 + ".frame_list[0].desc_ptr = %p\n" \ 176 + ".frame_list[0].desc_size = %u\n" \ 177 + ".frame_list[0].id = %u\n" \ 178 + ".frame_list[0].re_lsb = %u\n" \ 179 + ".frame_list[0].dev_id_byte = %u\n" 195 180 196 181 /* Tablet interface parameters *printf format arguments */ 197 182 #define UCLOGIC_PARAMS_FMT_ARGS(_params) \ 198 183 ((_params)->invalid ? "true" : "false"), \ 199 184 (_params)->desc_ptr, \ 200 185 (_params)->desc_size, \ 201 - ((_params)->pen_unused ? "true" : "false"), \ 202 186 (_params)->pen.desc_ptr, \ 203 187 (_params)->pen.desc_size, \ 204 188 (_params)->pen.id, \ 189 + (_params)->pen.subreport_list[0].value, \ 190 + (_params)->pen.subreport_list[0].id, \ 205 191 uclogic_params_pen_inrange_to_str((_params)->pen.inrange), \ 206 192 ((_params)->pen.fragmented_hires ? "true" : "false"), \ 207 - (_params)->frame.desc_ptr, \ 208 - (_params)->frame.desc_size, \ 209 - (_params)->frame.id, \ 210 - (_params)->frame.re_lsb, \ 211 - (_params)->frame.dev_id_byte, \ 212 - (_params)->pen_frame_flag 193 + ((_params)->pen.tilt_y_flipped ? "true" : "false"), \ 194 + (_params)->frame_list[0].desc_ptr, \ 195 + (_params)->frame_list[0].desc_size, \ 196 + (_params)->frame_list[0].id, \ 197 + (_params)->frame_list[0].re_lsb, \ 198 + (_params)->frame_list[0].dev_id_byte 213 199 214 200 /* Get a replacement report descriptor for a tablet's interface. */ 215 201 extern int uclogic_params_get_desc(const struct uclogic_params *params,
+40 -29
drivers/hid/hid-uclogic-rdesc.c
··· 532 532 sizeof(uclogic_rdesc_twha60_fixed1_arr); 533 533 534 534 /* Fixed report descriptor template for (tweaked) v1 pen reports */ 535 - const __u8 uclogic_rdesc_pen_v1_template_arr[] = { 535 + const __u8 uclogic_rdesc_v1_pen_template_arr[] = { 536 536 0x05, 0x0D, /* Usage Page (Digitizer), */ 537 537 0x09, 0x02, /* Usage (Pen), */ 538 538 0xA1, 0x01, /* Collection (Application), */ ··· 582 582 0xC0 /* End Collection */ 583 583 }; 584 584 585 - const size_t uclogic_rdesc_pen_v1_template_size = 586 - sizeof(uclogic_rdesc_pen_v1_template_arr); 585 + const size_t uclogic_rdesc_v1_pen_template_size = 586 + sizeof(uclogic_rdesc_v1_pen_template_arr); 587 587 588 588 /* Fixed report descriptor template for (tweaked) v2 pen reports */ 589 - const __u8 uclogic_rdesc_pen_v2_template_arr[] = { 589 + const __u8 uclogic_rdesc_v2_pen_template_arr[] = { 590 590 0x05, 0x0D, /* Usage Page (Digitizer), */ 591 591 0x09, 0x02, /* Usage (Pen), */ 592 592 0xA1, 0x01, /* Collection (Application), */ ··· 633 633 0x27, UCLOGIC_RDESC_PEN_PH(PRESSURE_LM), 634 634 /* Logical Maximum (PLACEHOLDER), */ 635 635 0x81, 0x02, /* Input (Variable), */ 636 - 0x81, 0x03, /* Input (Constant, Variable), */ 636 + 0x54, /* Unit Exponent (0), */ 637 + 0x65, 0x14, /* Unit (Degrees), */ 638 + 0x35, 0xC4, /* Physical Minimum (-60), */ 639 + 0x45, 0x3C, /* Physical Maximum (60), */ 640 + 0x15, 0xC4, /* Logical Minimum (-60), */ 641 + 0x25, 0x3C, /* Logical Maximum (60), */ 642 + 0x75, 0x08, /* Report Size (8), */ 643 + 0x95, 0x02, /* Report Count (2), */ 644 + 0x09, 0x3D, /* Usage (X Tilt), */ 645 + 0x09, 0x3E, /* Usage (Y Tilt), */ 646 + 0x81, 0x02, /* Input (Variable), */ 637 647 0xC0, /* End Collection, */ 638 648 0xC0 /* End Collection */ 639 649 }; 640 650 641 - const size_t uclogic_rdesc_pen_v2_template_size = 642 - sizeof(uclogic_rdesc_pen_v2_template_arr); 651 + const size_t uclogic_rdesc_v2_pen_template_size = 652 + sizeof(uclogic_rdesc_v2_pen_template_arr); 643 653 644 654 /* 645 - * Expand to the contents of a generic buttonpad report descriptor. 655 + * Expand to the contents of a generic frame report descriptor. 646 656 * 647 - * @_padding: Padding from the end of button bits at bit 44, until 648 - * the end of the report, in bits. 657 + * @_id: The report ID to use. 658 + * @_size: Size of the report to pad to, including report ID, bytes. 649 659 */ 650 - #define UCLOGIC_RDESC_BUTTONPAD_BYTES(_padding) \ 660 + #define UCLOGIC_RDESC_FRAME_BYTES(_id, _size) \ 651 661 0x05, 0x01, /* Usage Page (Desktop), */ \ 652 662 0x09, 0x07, /* Usage (Keypad), */ \ 653 663 0xA1, 0x01, /* Collection (Application), */ \ 654 - 0x85, 0xF7, /* Report ID (247), */ \ 664 + 0x85, (_id), /* Report ID (_id), */ \ 655 665 0x14, /* Logical Minimum (0), */ \ 656 666 0x25, 0x01, /* Logical Maximum (1), */ \ 657 667 0x75, 0x01, /* Report Size (1), */ \ ··· 689 679 0xA0, /* Collection (Physical), */ \ 690 680 0x05, 0x09, /* Usage Page (Button), */ \ 691 681 0x19, 0x01, /* Usage Minimum (01h), */ \ 692 - 0x29, 0x02, /* Usage Maximum (02h), */ \ 693 - 0x95, 0x02, /* Report Count (2), */ \ 682 + 0x29, 0x03, /* Usage Maximum (03h), */ \ 683 + 0x95, 0x03, /* Report Count (3), */ \ 694 684 0x81, 0x02, /* Input (Variable), */ \ 695 - 0x95, _padding, /* Report Count (_padding), */ \ 685 + 0x95, ((_size) * 8 - 45), \ 686 + /* Report Count (padding), */ \ 696 687 0x81, 0x01, /* Input (Constant), */ \ 697 688 0xC0, /* End Collection, */ \ 698 689 0xC0 /* End Collection */ 699 690 700 - /* Fixed report descriptor for (tweaked) v1 buttonpad reports */ 701 - const __u8 uclogic_rdesc_buttonpad_v1_arr[] = { 702 - UCLOGIC_RDESC_BUTTONPAD_BYTES(20) 691 + /* Fixed report descriptor for (tweaked) v1 frame reports */ 692 + const __u8 uclogic_rdesc_v1_frame_arr[] = { 693 + UCLOGIC_RDESC_FRAME_BYTES(UCLOGIC_RDESC_V1_FRAME_ID, 8) 703 694 }; 704 - const size_t uclogic_rdesc_buttonpad_v1_size = 705 - sizeof(uclogic_rdesc_buttonpad_v1_arr); 695 + const size_t uclogic_rdesc_v1_frame_size = 696 + sizeof(uclogic_rdesc_v1_frame_arr); 706 697 707 - /* Fixed report descriptor for (tweaked) v2 buttonpad reports */ 708 - const __u8 uclogic_rdesc_buttonpad_v2_arr[] = { 709 - UCLOGIC_RDESC_BUTTONPAD_BYTES(52) 698 + /* Fixed report descriptor for (tweaked) v2 frame reports */ 699 + const __u8 uclogic_rdesc_v2_frame_arr[] = { 700 + UCLOGIC_RDESC_FRAME_BYTES(UCLOGIC_RDESC_V2_FRAME_ID, 12) 710 701 }; 711 - const size_t uclogic_rdesc_buttonpad_v2_size = 712 - sizeof(uclogic_rdesc_buttonpad_v2_arr); 702 + const size_t uclogic_rdesc_v2_frame_size = 703 + sizeof(uclogic_rdesc_v2_frame_arr); 713 704 714 - /* Fixed report descriptor for Ugee EX07 buttonpad */ 715 - const __u8 uclogic_rdesc_ugee_ex07_buttonpad_arr[] = { 705 + /* Fixed report descriptor for Ugee EX07 frame */ 706 + const __u8 uclogic_rdesc_ugee_ex07_frame_arr[] = { 716 707 0x05, 0x01, /* Usage Page (Desktop), */ 717 708 0x09, 0x07, /* Usage (Keypad), */ 718 709 0xA1, 0x01, /* Collection (Application), */ ··· 736 725 0xC0, /* End Collection, */ 737 726 0xC0 /* End Collection */ 738 727 }; 739 - const size_t uclogic_rdesc_ugee_ex07_buttonpad_size = 740 - sizeof(uclogic_rdesc_ugee_ex07_buttonpad_arr); 728 + const size_t uclogic_rdesc_ugee_ex07_frame_size = 729 + sizeof(uclogic_rdesc_ugee_ex07_frame_arr); 741 730 742 731 /* Fixed report descriptor for Ugee G5 frame controls */ 743 732 const __u8 uclogic_rdesc_ugee_g5_frame_arr[] = {
+19 -19
drivers/hid/hid-uclogic-rdesc.h
··· 104 104 UCLOGIC_RDESC_PH_HEAD, UCLOGIC_RDESC_PEN_PH_ID_##_ID 105 105 106 106 /* Report ID for v1 pen reports */ 107 - #define UCLOGIC_RDESC_PEN_V1_ID 0x07 107 + #define UCLOGIC_RDESC_V1_PEN_ID 0x07 108 108 109 109 /* Fixed report descriptor template for (tweaked) v1 pen reports */ 110 - extern const __u8 uclogic_rdesc_pen_v1_template_arr[]; 111 - extern const size_t uclogic_rdesc_pen_v1_template_size; 110 + extern const __u8 uclogic_rdesc_v1_pen_template_arr[]; 111 + extern const size_t uclogic_rdesc_v1_pen_template_size; 112 112 113 113 /* Report ID for v2 pen reports */ 114 - #define UCLOGIC_RDESC_PEN_V2_ID 0x08 114 + #define UCLOGIC_RDESC_V2_PEN_ID 0x08 115 115 116 116 /* Fixed report descriptor template for (tweaked) v2 pen reports */ 117 - extern const __u8 uclogic_rdesc_pen_v2_template_arr[]; 118 - extern const size_t uclogic_rdesc_pen_v2_template_size; 117 + extern const __u8 uclogic_rdesc_v2_pen_template_arr[]; 118 + extern const size_t uclogic_rdesc_v2_pen_template_size; 119 119 120 - /* Fixed report descriptor for (tweaked) v1 buttonpad reports */ 121 - extern const __u8 uclogic_rdesc_buttonpad_v1_arr[]; 122 - extern const size_t uclogic_rdesc_buttonpad_v1_size; 120 + /* Report ID for tweaked v1 frame reports */ 121 + #define UCLOGIC_RDESC_V1_FRAME_ID 0xf7 123 122 124 - /* Report ID for tweaked v1 buttonpad reports */ 125 - #define UCLOGIC_RDESC_BUTTONPAD_V1_ID 0xf7 123 + /* Fixed report descriptor for (tweaked) v1 frame reports */ 124 + extern const __u8 uclogic_rdesc_v1_frame_arr[]; 125 + extern const size_t uclogic_rdesc_v1_frame_size; 126 126 127 - /* Fixed report descriptor for (tweaked) v2 buttonpad reports */ 128 - extern const __u8 uclogic_rdesc_buttonpad_v2_arr[]; 129 - extern const size_t uclogic_rdesc_buttonpad_v2_size; 127 + /* Report ID for tweaked v2 frame reports */ 128 + #define UCLOGIC_RDESC_V2_FRAME_ID 0xf7 130 129 131 - /* Report ID for tweaked v2 buttonpad reports */ 132 - #define UCLOGIC_RDESC_BUTTONPAD_V2_ID 0xf7 130 + /* Fixed report descriptor for (tweaked) v2 frame reports */ 131 + extern const __u8 uclogic_rdesc_v2_frame_arr[]; 132 + extern const size_t uclogic_rdesc_v2_frame_size; 133 133 134 - /* Fixed report descriptor for Ugee EX07 buttonpad */ 135 - extern const __u8 uclogic_rdesc_ugee_ex07_buttonpad_arr[]; 136 - extern const size_t uclogic_rdesc_ugee_ex07_buttonpad_size; 134 + /* Fixed report descriptor for Ugee EX07 frame */ 135 + extern const __u8 uclogic_rdesc_ugee_ex07_frame_arr[]; 136 + extern const size_t uclogic_rdesc_ugee_ex07_frame_size; 137 137 138 138 /* Fixed report descriptor for XP-Pen Deco 01 frame controls */ 139 139 extern const __u8 uclogic_rdesc_xppen_deco01_frame_arr[];
+329 -294
drivers/hid/i2c-hid/i2c-hid-core.c
··· 35 35 #include <linux/kernel.h> 36 36 #include <linux/hid.h> 37 37 #include <linux/mutex.h> 38 + #include <asm/unaligned.h> 38 39 39 40 #include "../hid-ids.h" 40 41 #include "i2c-hid.h" ··· 48 47 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6) 49 48 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7) 50 49 50 + /* Command opcodes */ 51 + #define I2C_HID_OPCODE_RESET 0x01 52 + #define I2C_HID_OPCODE_GET_REPORT 0x02 53 + #define I2C_HID_OPCODE_SET_REPORT 0x03 54 + #define I2C_HID_OPCODE_GET_IDLE 0x04 55 + #define I2C_HID_OPCODE_SET_IDLE 0x05 56 + #define I2C_HID_OPCODE_GET_PROTOCOL 0x06 57 + #define I2C_HID_OPCODE_SET_PROTOCOL 0x07 58 + #define I2C_HID_OPCODE_SET_POWER 0x08 51 59 52 60 /* flags */ 53 61 #define I2C_HID_STARTED 0 ··· 94 84 __le32 reserved; 95 85 } __packed; 96 86 97 - struct i2c_hid_cmd { 98 - unsigned int registerIndex; 99 - __u8 opcode; 100 - unsigned int length; 101 - bool wait; 102 - }; 103 - 104 - union command { 105 - u8 data[0]; 106 - struct cmd { 107 - __le16 reg; 108 - __u8 reportTypeID; 109 - __u8 opcode; 110 - } __packed c; 111 - }; 112 - 113 - #define I2C_HID_CMD(opcode_) \ 114 - .opcode = opcode_, .length = 4, \ 115 - .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister) 116 - 117 - /* fetch HID descriptor */ 118 - static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 }; 119 - /* fetch report descriptors */ 120 - static const struct i2c_hid_cmd hid_report_descr_cmd = { 121 - .registerIndex = offsetof(struct i2c_hid_desc, 122 - wReportDescRegister), 123 - .opcode = 0x00, 124 - .length = 2 }; 125 - /* commands */ 126 - static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01), 127 - .wait = true }; 128 - static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) }; 129 - static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) }; 130 - static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; 131 - static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 }; 132 - 133 - /* 134 - * These definitions are not used here, but are defined by the spec. 135 - * Keeping them here for documentation purposes. 136 - * 137 - * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) }; 138 - * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) }; 139 - * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) }; 140 - * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; 141 - */ 142 - 143 87 /* The main device structure */ 144 88 struct i2c_hid { 145 89 struct i2c_client *client; /* i2c client */ 146 90 struct hid_device *hid; /* pointer to corresponding HID dev */ 147 - union { 148 - __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)]; 149 - struct i2c_hid_desc hdesc; /* the HID Descriptor */ 150 - }; 91 + struct i2c_hid_desc hdesc; /* the HID Descriptor */ 151 92 __le16 wHIDDescRegister; /* location of the i2c 152 93 * register of the HID 153 94 * descriptor. */ ··· 106 145 u8 *inbuf; /* Input buffer */ 107 146 u8 *rawbuf; /* Raw Input buffer */ 108 147 u8 *cmdbuf; /* Command buffer */ 109 - u8 *argsbuf; /* Command arguments buffer */ 110 148 111 149 unsigned long flags; /* device flags */ 112 150 unsigned long quirks; /* Various quirks */ ··· 167 207 return quirks; 168 208 } 169 209 170 - static int __i2c_hid_command(struct i2c_client *client, 171 - const struct i2c_hid_cmd *command, u8 reportID, 172 - u8 reportType, u8 *args, int args_len, 173 - unsigned char *buf_recv, int data_len) 210 + static int i2c_hid_xfer(struct i2c_hid *ihid, 211 + u8 *send_buf, int send_len, u8 *recv_buf, int recv_len) 174 212 { 175 - struct i2c_hid *ihid = i2c_get_clientdata(client); 176 - union command *cmd = (union command *)ihid->cmdbuf; 213 + struct i2c_client *client = ihid->client; 214 + struct i2c_msg msgs[2] = { 0 }; 215 + int n = 0; 177 216 int ret; 178 - struct i2c_msg msg[2]; 179 - int msg_num = 1; 180 217 181 - int length = command->length; 182 - bool wait = command->wait; 183 - unsigned int registerIndex = command->registerIndex; 218 + if (send_len) { 219 + i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", 220 + __func__, send_len, send_buf); 184 221 185 - /* special case for hid_descr_cmd */ 186 - if (command == &hid_descr_cmd) { 187 - cmd->c.reg = ihid->wHIDDescRegister; 188 - } else { 189 - cmd->data[0] = ihid->hdesc_buffer[registerIndex]; 190 - cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1]; 222 + msgs[n].addr = client->addr; 223 + msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE; 224 + msgs[n].len = send_len; 225 + msgs[n].buf = send_buf; 226 + n++; 191 227 } 192 228 193 - if (length > 2) { 194 - cmd->c.opcode = command->opcode; 195 - cmd->c.reportTypeID = reportID | reportType << 4; 196 - } 229 + if (recv_len) { 230 + msgs[n].addr = client->addr; 231 + msgs[n].flags = (client->flags & I2C_M_TEN) | 232 + I2C_M_RD | I2C_M_DMA_SAFE; 233 + msgs[n].len = recv_len; 234 + msgs[n].buf = recv_buf; 235 + n++; 197 236 198 - memcpy(cmd->data + length, args, args_len); 199 - length += args_len; 200 - 201 - i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data); 202 - 203 - msg[0].addr = client->addr; 204 - msg[0].flags = client->flags & I2C_M_TEN; 205 - msg[0].len = length; 206 - msg[0].buf = cmd->data; 207 - if (data_len > 0) { 208 - msg[1].addr = client->addr; 209 - msg[1].flags = client->flags & I2C_M_TEN; 210 - msg[1].flags |= I2C_M_RD; 211 - msg[1].len = data_len; 212 - msg[1].buf = buf_recv; 213 - msg_num = 2; 214 237 set_bit(I2C_HID_READ_PENDING, &ihid->flags); 215 238 } 216 239 217 - if (wait) 218 - set_bit(I2C_HID_RESET_PENDING, &ihid->flags); 240 + ret = i2c_transfer(client->adapter, msgs, n); 219 241 220 - ret = i2c_transfer(client->adapter, msg, msg_num); 221 - 222 - if (data_len > 0) 242 + if (recv_len) 223 243 clear_bit(I2C_HID_READ_PENDING, &ihid->flags); 224 244 225 - if (ret != msg_num) 245 + if (ret != n) 226 246 return ret < 0 ? ret : -EIO; 227 - 228 - ret = 0; 229 - 230 - if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) { 231 - msleep(100); 232 - } else if (wait) { 233 - i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); 234 - if (!wait_event_timeout(ihid->wait, 235 - !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), 236 - msecs_to_jiffies(5000))) 237 - ret = -ENODATA; 238 - i2c_hid_dbg(ihid, "%s: finished.\n", __func__); 239 - } 240 - 241 - return ret; 242 - } 243 - 244 - static int i2c_hid_command(struct i2c_client *client, 245 - const struct i2c_hid_cmd *command, 246 - unsigned char *buf_recv, int data_len) 247 - { 248 - return __i2c_hid_command(client, command, 0, 0, NULL, 0, 249 - buf_recv, data_len); 250 - } 251 - 252 - static int i2c_hid_get_report(struct i2c_client *client, u8 reportType, 253 - u8 reportID, unsigned char *buf_recv, int data_len) 254 - { 255 - struct i2c_hid *ihid = i2c_get_clientdata(client); 256 - u8 args[3]; 257 - int ret; 258 - int args_len = 0; 259 - u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister); 260 - 261 - i2c_hid_dbg(ihid, "%s\n", __func__); 262 - 263 - if (reportID >= 0x0F) { 264 - args[args_len++] = reportID; 265 - reportID = 0x0F; 266 - } 267 - 268 - args[args_len++] = readRegister & 0xFF; 269 - args[args_len++] = readRegister >> 8; 270 - 271 - ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID, 272 - reportType, args, args_len, buf_recv, data_len); 273 - if (ret) { 274 - dev_err(&client->dev, 275 - "failed to retrieve report from device.\n"); 276 - return ret; 277 - } 278 247 279 248 return 0; 280 249 } 281 250 251 + static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg, 252 + void *buf, size_t len) 253 + { 254 + *(__le16 *)ihid->cmdbuf = reg; 255 + 256 + return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len); 257 + } 258 + 259 + static size_t i2c_hid_encode_command(u8 *buf, u8 opcode, 260 + int report_type, int report_id) 261 + { 262 + size_t length = 0; 263 + 264 + if (report_id < 0x0F) { 265 + buf[length++] = report_type << 4 | report_id; 266 + buf[length++] = opcode; 267 + } else { 268 + buf[length++] = report_type << 4 | 0x0F; 269 + buf[length++] = opcode; 270 + buf[length++] = report_id; 271 + } 272 + 273 + return length; 274 + } 275 + 276 + static int i2c_hid_get_report(struct i2c_hid *ihid, 277 + u8 report_type, u8 report_id, 278 + u8 *recv_buf, size_t recv_len) 279 + { 280 + size_t length = 0; 281 + size_t ret_count; 282 + int error; 283 + 284 + i2c_hid_dbg(ihid, "%s\n", __func__); 285 + 286 + /* Command register goes first */ 287 + *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 288 + length += sizeof(__le16); 289 + /* Next is GET_REPORT command */ 290 + length += i2c_hid_encode_command(ihid->cmdbuf + length, 291 + I2C_HID_OPCODE_GET_REPORT, 292 + report_type, report_id); 293 + /* 294 + * Device will send report data through data register. Because 295 + * command can be either 2 or 3 bytes destination for the data 296 + * register may be not aligned. 297 + */ 298 + put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister), 299 + ihid->cmdbuf + length); 300 + length += sizeof(__le16); 301 + 302 + /* 303 + * In addition to report data device will supply data length 304 + * in the first 2 bytes of the response, so adjust . 305 + */ 306 + error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, 307 + ihid->rawbuf, recv_len + sizeof(__le16)); 308 + if (error) { 309 + dev_err(&ihid->client->dev, 310 + "failed to set a report to device: %d\n", error); 311 + return error; 312 + } 313 + 314 + /* The buffer is sufficiently aligned */ 315 + ret_count = le16_to_cpup((__le16 *)ihid->rawbuf); 316 + 317 + /* Check for empty report response */ 318 + if (ret_count <= sizeof(__le16)) 319 + return 0; 320 + 321 + recv_len = min(recv_len, ret_count - sizeof(__le16)); 322 + memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len); 323 + 324 + if (report_id && recv_len != 0 && recv_buf[0] != report_id) { 325 + dev_err(&ihid->client->dev, 326 + "device returned incorrect report (%d vs %d expected)\n", 327 + recv_buf[0], report_id); 328 + return -EINVAL; 329 + } 330 + 331 + return recv_len; 332 + } 333 + 334 + static size_t i2c_hid_format_report(u8 *buf, int report_id, 335 + const u8 *data, size_t size) 336 + { 337 + size_t length = sizeof(__le16); /* reserve space to store size */ 338 + 339 + if (report_id) 340 + buf[length++] = report_id; 341 + 342 + memcpy(buf + length, data, size); 343 + length += size; 344 + 345 + /* Store overall size in the beginning of the buffer */ 346 + put_unaligned_le16(length, buf); 347 + 348 + return length; 349 + } 350 + 282 351 /** 283 352 * i2c_hid_set_or_send_report: forward an incoming report to the device 284 - * @client: the i2c_client of the device 285 - * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT 286 - * @reportID: the report ID 353 + * @ihid: the i2c hid device 354 + * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT 355 + * @report_id: the report ID 287 356 * @buf: the actual data to transfer, without the report ID 288 357 * @data_len: size of buf 289 - * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report 358 + * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report 290 359 */ 291 - static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType, 292 - u8 reportID, unsigned char *buf, size_t data_len, bool use_data) 360 + static int i2c_hid_set_or_send_report(struct i2c_hid *ihid, 361 + u8 report_type, u8 report_id, 362 + const u8 *buf, size_t data_len, 363 + bool do_set) 293 364 { 294 - struct i2c_hid *ihid = i2c_get_clientdata(client); 295 - u8 *args = ihid->argsbuf; 296 - const struct i2c_hid_cmd *hidcmd; 297 - int ret; 298 - u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister); 299 - u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister); 300 - u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength); 301 - u16 size; 302 - int args_len; 303 - int index = 0; 365 + size_t length = 0; 366 + int error; 304 367 305 368 i2c_hid_dbg(ihid, "%s\n", __func__); 306 369 307 370 if (data_len > ihid->bufsize) 308 371 return -EINVAL; 309 372 310 - size = 2 /* size */ + 311 - (reportID ? 1 : 0) /* reportID */ + 312 - data_len /* buf */; 313 - args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ + 314 - 2 /* dataRegister */ + 315 - size /* args */; 316 - 317 - if (!use_data && maxOutputLength == 0) 373 + if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0) 318 374 return -ENOSYS; 319 375 320 - if (reportID >= 0x0F) { 321 - args[index++] = reportID; 322 - reportID = 0x0F; 323 - } 324 - 325 - /* 326 - * use the data register for feature reports or if the device does not 327 - * support the output register 328 - */ 329 - if (use_data) { 330 - args[index++] = dataRegister & 0xFF; 331 - args[index++] = dataRegister >> 8; 332 - hidcmd = &hid_set_report_cmd; 376 + if (do_set) { 377 + /* Command register goes first */ 378 + *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 379 + length += sizeof(__le16); 380 + /* Next is SET_REPORT command */ 381 + length += i2c_hid_encode_command(ihid->cmdbuf + length, 382 + I2C_HID_OPCODE_SET_REPORT, 383 + report_type, report_id); 384 + /* 385 + * Report data will go into the data register. Because 386 + * command can be either 2 or 3 bytes destination for 387 + * the data register may be not aligned. 388 + */ 389 + put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister), 390 + ihid->cmdbuf + length); 391 + length += sizeof(__le16); 333 392 } else { 334 - args[index++] = outputRegister & 0xFF; 335 - args[index++] = outputRegister >> 8; 336 - hidcmd = &hid_no_cmd; 393 + /* 394 + * With simple "send report" all data goes into the output 395 + * register. 396 + */ 397 + *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister; 398 + length += sizeof(__le16); 337 399 } 338 400 339 - args[index++] = size & 0xFF; 340 - args[index++] = size >> 8; 401 + length += i2c_hid_format_report(ihid->cmdbuf + length, 402 + report_id, buf, data_len); 341 403 342 - if (reportID) 343 - args[index++] = reportID; 344 - 345 - memcpy(&args[index], buf, data_len); 346 - 347 - ret = __i2c_hid_command(client, hidcmd, reportID, 348 - reportType, args, args_len, NULL, 0); 349 - if (ret) { 350 - dev_err(&client->dev, "failed to set a report to device.\n"); 351 - return ret; 404 + error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 405 + if (error) { 406 + dev_err(&ihid->client->dev, 407 + "failed to set a report to device: %d\n", error); 408 + return error; 352 409 } 353 410 354 411 return data_len; 355 412 } 356 413 357 - static int i2c_hid_set_power(struct i2c_client *client, int power_state) 414 + static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state) 358 415 { 359 - struct i2c_hid *ihid = i2c_get_clientdata(client); 416 + size_t length; 417 + 418 + /* SET_POWER uses command register */ 419 + *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 420 + length = sizeof(__le16); 421 + 422 + /* Now the command itself */ 423 + length += i2c_hid_encode_command(ihid->cmdbuf + length, 424 + I2C_HID_OPCODE_SET_POWER, 425 + 0, power_state); 426 + 427 + return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 428 + } 429 + 430 + static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state) 431 + { 360 432 int ret; 361 433 362 434 i2c_hid_dbg(ihid, "%s\n", __func__); ··· 400 408 */ 401 409 if (power_state == I2C_HID_PWR_ON && 402 410 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) { 403 - ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0); 411 + ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON); 404 412 405 413 /* Device was already activated */ 406 414 if (!ret) 407 415 goto set_pwr_exit; 408 416 } 409 417 410 - ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state, 411 - 0, NULL, 0, NULL, 0); 412 - 418 + ret = i2c_hid_set_power_command(ihid, power_state); 413 419 if (ret) 414 - dev_err(&client->dev, "failed to change power setting.\n"); 420 + dev_err(&ihid->client->dev, 421 + "failed to change power setting.\n"); 415 422 416 423 set_pwr_exit: 417 424 ··· 429 438 return ret; 430 439 } 431 440 432 - static int i2c_hid_hwreset(struct i2c_client *client) 441 + static int i2c_hid_execute_reset(struct i2c_hid *ihid) 433 442 { 434 - struct i2c_hid *ihid = i2c_get_clientdata(client); 443 + size_t length = 0; 444 + int ret; 445 + 446 + i2c_hid_dbg(ihid, "resetting...\n"); 447 + 448 + /* Prepare reset command. Command register goes first. */ 449 + *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 450 + length += sizeof(__le16); 451 + /* Next is RESET command itself */ 452 + length += i2c_hid_encode_command(ihid->cmdbuf + length, 453 + I2C_HID_OPCODE_RESET, 0, 0); 454 + 455 + set_bit(I2C_HID_RESET_PENDING, &ihid->flags); 456 + 457 + ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 458 + if (ret) { 459 + dev_err(&ihid->client->dev, "failed to reset device.\n"); 460 + goto out; 461 + } 462 + 463 + if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) { 464 + msleep(100); 465 + goto out; 466 + } 467 + 468 + i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); 469 + if (!wait_event_timeout(ihid->wait, 470 + !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), 471 + msecs_to_jiffies(5000))) { 472 + ret = -ENODATA; 473 + goto out; 474 + } 475 + i2c_hid_dbg(ihid, "%s: finished.\n", __func__); 476 + 477 + out: 478 + clear_bit(I2C_HID_RESET_PENDING, &ihid->flags); 479 + return ret; 480 + } 481 + 482 + static int i2c_hid_hwreset(struct i2c_hid *ihid) 483 + { 435 484 int ret; 436 485 437 486 i2c_hid_dbg(ihid, "%s\n", __func__); ··· 483 452 */ 484 453 mutex_lock(&ihid->reset_lock); 485 454 486 - ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); 455 + ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 487 456 if (ret) 488 457 goto out_unlock; 489 458 490 - i2c_hid_dbg(ihid, "resetting...\n"); 491 - 492 - ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0); 459 + ret = i2c_hid_execute_reset(ihid); 493 460 if (ret) { 494 - dev_err(&client->dev, "failed to reset device.\n"); 495 - i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); 461 + dev_err(&ihid->client->dev, 462 + "failed to reset device: %d\n", ret); 463 + i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 496 464 goto out_unlock; 497 465 } 498 466 499 467 /* At least some SIS devices need this after reset */ 500 468 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET)) 501 - ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); 469 + ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 502 470 503 471 out_unlock: 504 472 mutex_unlock(&ihid->reset_lock); ··· 506 476 507 477 static void i2c_hid_get_input(struct i2c_hid *ihid) 508 478 { 479 + u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength); 480 + u16 ret_size; 509 481 int ret; 510 - u32 ret_size; 511 - int size = le16_to_cpu(ihid->hdesc.wMaxInputLength); 512 482 513 483 if (size > ihid->bufsize) 514 484 size = ihid->bufsize; ··· 523 493 return; 524 494 } 525 495 526 - ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8; 527 - 496 + /* Receiving buffer is properly aligned */ 497 + ret_size = le16_to_cpup((__le16 *)ihid->inbuf); 528 498 if (!ret_size) { 529 499 /* host or device initiated RESET completed */ 530 500 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) ··· 532 502 return; 533 503 } 534 504 535 - if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) { 536 - dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but " 537 - "there's no data\n", __func__); 505 + if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) { 506 + dev_warn_once(&ihid->client->dev, 507 + "%s: IRQ triggered but there's no data\n", 508 + __func__); 538 509 return; 539 510 } 540 511 541 - if ((ret_size > size) || (ret_size < 2)) { 512 + if (ret_size > size || ret_size < sizeof(__le16)) { 542 513 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) { 543 - ihid->inbuf[0] = size & 0xff; 544 - ihid->inbuf[1] = size >> 8; 514 + *(__le16 *)ihid->inbuf = cpu_to_le16(size); 545 515 ret_size = size; 546 516 } else { 547 - dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n", 517 + dev_err(&ihid->client->dev, 518 + "%s: incomplete report (%d/%d)\n", 548 519 __func__, size, ret_size); 549 520 return; 550 521 } ··· 556 525 if (test_bit(I2C_HID_STARTED, &ihid->flags)) { 557 526 pm_wakeup_event(&ihid->client->dev, 0); 558 527 559 - hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2, 560 - ret_size - 2, 1); 528 + hid_input_report(ihid->hid, HID_INPUT_REPORT, 529 + ihid->inbuf + sizeof(__le16), 530 + ret_size - sizeof(__le16), 1); 561 531 } 562 532 563 533 return; ··· 604 572 { 605 573 kfree(ihid->inbuf); 606 574 kfree(ihid->rawbuf); 607 - kfree(ihid->argsbuf); 608 575 kfree(ihid->cmdbuf); 609 576 ihid->inbuf = NULL; 610 577 ihid->rawbuf = NULL; 611 578 ihid->cmdbuf = NULL; 612 - ihid->argsbuf = NULL; 613 579 ihid->bufsize = 0; 614 580 } 615 581 616 582 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) 617 583 { 618 - /* the worst case is computed from the set_report command with a 619 - * reportID > 15 and the maximum report length */ 620 - int args_len = sizeof(__u8) + /* ReportID */ 621 - sizeof(__u8) + /* optional ReportID byte */ 622 - sizeof(__u16) + /* data register */ 623 - sizeof(__u16) + /* size of the report */ 624 - report_size; /* report */ 584 + /* 585 + * The worst case is computed from the set_report command with a 586 + * reportID > 15 and the maximum report length. 587 + */ 588 + int cmd_len = sizeof(__le16) + /* command register */ 589 + sizeof(u8) + /* encoded report type/ID */ 590 + sizeof(u8) + /* opcode */ 591 + sizeof(u8) + /* optional 3rd byte report ID */ 592 + sizeof(__le16) + /* data register */ 593 + sizeof(__le16) + /* report data size */ 594 + sizeof(u8) + /* report ID if numbered report */ 595 + report_size; 625 596 626 597 ihid->inbuf = kzalloc(report_size, GFP_KERNEL); 627 598 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL); 628 - ihid->argsbuf = kzalloc(args_len, GFP_KERNEL); 629 - ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL); 599 + ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL); 630 600 631 - if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) { 601 + if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) { 632 602 i2c_hid_free_buffers(ihid); 633 603 return -ENOMEM; 634 604 } ··· 641 607 } 642 608 643 609 static int i2c_hid_get_raw_report(struct hid_device *hid, 644 - unsigned char report_number, __u8 *buf, size_t count, 645 - unsigned char report_type) 610 + u8 report_type, u8 report_id, 611 + u8 *buf, size_t count) 646 612 { 647 613 struct i2c_client *client = hid->driver_data; 648 614 struct i2c_hid *ihid = i2c_get_clientdata(client); 649 - size_t ret_count, ask_count; 650 - int ret; 615 + int ret_count; 651 616 652 617 if (report_type == HID_OUTPUT_REPORT) 653 618 return -EINVAL; 654 619 655 - /* +2 bytes to include the size of the reply in the query buffer */ 656 - ask_count = min(count + 2, (size_t)ihid->bufsize); 620 + /* 621 + * In case of unnumbered reports the response from the device will 622 + * not have the report ID that the upper layers expect, so we need 623 + * to stash it the buffer ourselves and adjust the data size. 624 + */ 625 + if (!report_id) { 626 + buf[0] = 0; 627 + buf++; 628 + count--; 629 + } 657 630 658 - ret = i2c_hid_get_report(client, 631 + ret_count = i2c_hid_get_report(ihid, 659 632 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, 660 - report_number, ihid->rawbuf, ask_count); 633 + report_id, buf, count); 661 634 662 - if (ret < 0) 663 - return ret; 635 + if (ret_count > 0 && !report_id) 636 + ret_count++; 664 637 665 - ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8); 666 - 667 - if (ret_count <= 2) 668 - return 0; 669 - 670 - ret_count = min(ret_count, ask_count); 671 - 672 - /* The query buffer contains the size, dropping it in the reply */ 673 - count = min(count, ret_count - 2); 674 - memcpy(buf, ihid->rawbuf + 2, count); 675 - 676 - return count; 638 + return ret_count; 677 639 } 678 640 679 - static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf, 680 - size_t count, unsigned char report_type, bool use_data) 641 + static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type, 642 + const u8 *buf, size_t count, bool do_set) 681 643 { 682 644 struct i2c_client *client = hid->driver_data; 683 645 struct i2c_hid *ihid = i2c_get_clientdata(client); ··· 685 655 686 656 mutex_lock(&ihid->reset_lock); 687 657 688 - if (report_id) { 689 - buf++; 690 - count--; 691 - } 692 - 693 - ret = i2c_hid_set_or_send_report(client, 658 + /* 659 + * Note that both numbered and unnumbered reports passed here 660 + * are supposed to have report ID stored in the 1st byte of the 661 + * buffer, so we strip it off unconditionally before passing payload 662 + * to i2c_hid_set_or_send_report which takes care of encoding 663 + * everything properly. 664 + */ 665 + ret = i2c_hid_set_or_send_report(ihid, 694 666 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, 695 - report_id, buf, count, use_data); 667 + report_id, buf + 1, count - 1, do_set); 696 668 697 - if (report_id && ret >= 0) 698 - ret++; /* add report_id to the number of transfered bytes */ 669 + if (ret >= 0) 670 + ret++; /* add report_id to the number of transferred bytes */ 699 671 700 672 mutex_unlock(&ihid->reset_lock); 701 673 702 674 return ret; 703 675 } 704 676 705 - static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf, 706 - size_t count) 677 + static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count) 707 678 { 708 - return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT, 709 - false); 679 + return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count, 680 + false); 710 681 } 711 682 712 683 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, ··· 716 685 { 717 686 switch (reqtype) { 718 687 case HID_REQ_GET_REPORT: 719 - return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype); 688 + return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len); 720 689 case HID_REQ_SET_REPORT: 721 690 if (buf[0] != reportnum) 722 691 return -EINVAL; 723 - return i2c_hid_output_raw_report(hid, buf, len, rtype, true); 692 + return i2c_hid_output_raw_report(hid, rtype, buf, len, true); 724 693 default: 725 694 return -EIO; 726 695 } ··· 746 715 } 747 716 748 717 do { 749 - ret = i2c_hid_hwreset(client); 718 + ret = i2c_hid_hwreset(ihid); 750 719 if (ret) 751 720 msleep(1000); 752 721 } while (tries-- > 0 && ret); ··· 770 739 771 740 i2c_hid_dbg(ihid, "asking HID report descriptor\n"); 772 741 773 - ret = i2c_hid_command(client, &hid_report_descr_cmd, 774 - rdesc, rsize); 742 + ret = i2c_hid_read_register(ihid, 743 + ihid->hdesc.wReportDescRegister, 744 + rdesc, rsize); 775 745 if (ret) { 776 746 hid_err(hid, "reading report descriptor failed\n"); 777 747 kfree(rdesc); ··· 882 850 struct i2c_client *client = ihid->client; 883 851 struct i2c_hid_desc *hdesc = &ihid->hdesc; 884 852 unsigned int dsize; 885 - int ret; 853 + int error; 886 854 887 855 /* i2c hid fetch using a fixed descriptor size (30 bytes) */ 888 856 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) { ··· 891 859 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name); 892 860 } else { 893 861 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); 894 - ret = i2c_hid_command(client, &hid_descr_cmd, 895 - ihid->hdesc_buffer, 896 - sizeof(struct i2c_hid_desc)); 897 - if (ret) { 898 - dev_err(&client->dev, "hid_descr_cmd failed\n"); 862 + error = i2c_hid_read_register(ihid, 863 + ihid->wHIDDescRegister, 864 + &ihid->hdesc, 865 + sizeof(ihid->hdesc)); 866 + if (error) { 867 + dev_err(&ihid->client->dev, 868 + "failed to fetch HID descriptor: %d\n", 869 + error); 899 870 return -ENODEV; 900 871 } 901 872 } ··· 908 873 * bytes 2-3 -> bcdVersion (has to be 1.00) */ 909 874 /* check bcdVersion == 1.0 */ 910 875 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { 911 - dev_err(&client->dev, 876 + dev_err(&ihid->client->dev, 912 877 "unexpected HID descriptor bcdVersion (0x%04hx)\n", 913 878 le16_to_cpu(hdesc->bcdVersion)); 914 879 return -ENODEV; ··· 917 882 /* Descriptor length should be 30 bytes as per the specification */ 918 883 dsize = le16_to_cpu(hdesc->wHIDDescLength); 919 884 if (dsize != sizeof(struct i2c_hid_desc)) { 920 - dev_err(&client->dev, "weird size of HID descriptor (%u)\n", 921 - dsize); 885 + dev_err(&ihid->client->dev, 886 + "weird size of HID descriptor (%u)\n", dsize); 922 887 return -ENODEV; 923 888 } 924 - i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer); 889 + i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc); 925 890 return 0; 926 891 } 927 892 ··· 1087 1052 { 1088 1053 struct i2c_hid *ihid = i2c_get_clientdata(client); 1089 1054 1090 - i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); 1055 + i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 1091 1056 free_irq(client->irq, ihid); 1092 1057 1093 1058 i2c_hid_core_shutdown_tail(ihid); ··· 1108 1073 return ret; 1109 1074 1110 1075 /* Save some power */ 1111 - i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); 1076 + i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 1112 1077 1113 1078 disable_irq(client->irq); 1114 1079 ··· 1156 1121 * let's still reset them here. 1157 1122 */ 1158 1123 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME) 1159 - ret = i2c_hid_hwreset(client); 1124 + ret = i2c_hid_hwreset(ihid); 1160 1125 else 1161 - ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); 1126 + ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 1162 1127 1163 1128 if (ret) 1164 1129 return ret;
+3 -26
drivers/hid/intel-ish-hid/ishtp-fw-loader.c
··· 661 661 */ 662 662 payload_max_size &= ~(L1_CACHE_BYTES - 1); 663 663 664 - dma_buf = kmalloc(payload_max_size, GFP_KERNEL | GFP_DMA32); 664 + dma_buf = dma_alloc_coherent(devc, payload_max_size, &dma_buf_phy, GFP_KERNEL); 665 665 if (!dma_buf) { 666 666 client_data->flag_retry = true; 667 667 return -ENOMEM; 668 - } 669 - 670 - dma_buf_phy = dma_map_single(devc, dma_buf, payload_max_size, 671 - DMA_TO_DEVICE); 672 - if (dma_mapping_error(devc, dma_buf_phy)) { 673 - dev_err(cl_data_to_dev(client_data), "DMA map failed\n"); 674 - client_data->flag_retry = true; 675 - rv = -ENOMEM; 676 - goto end_err_dma_buf_release; 677 668 } 678 669 679 670 ldr_xfer_dma_frag.fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT; ··· 686 695 ldr_xfer_dma_frag.fragment.size = fragment_size; 687 696 memcpy(dma_buf, &fw->data[fragment_offset], fragment_size); 688 697 689 - dma_sync_single_for_device(devc, dma_buf_phy, 690 - payload_max_size, 691 - DMA_TO_DEVICE); 692 - 693 - /* 694 - * Flush cache here because the dma_sync_single_for_device() 695 - * does not do for x86. 696 - */ 698 + /* Flush cache to be sure the data is in main memory. */ 697 699 clflush_cache_range(dma_buf, payload_max_size); 698 700 699 701 dev_dbg(cl_data_to_dev(client_data), ··· 709 725 fragment_offset += fragment_size; 710 726 } 711 727 712 - dma_unmap_single(devc, dma_buf_phy, payload_max_size, DMA_TO_DEVICE); 713 - kfree(dma_buf); 714 - return 0; 715 - 716 728 end_err_resp_buf_release: 717 - /* Free ISH buffer if not done already, in error case */ 718 - dma_unmap_single(devc, dma_buf_phy, payload_max_size, DMA_TO_DEVICE); 719 - end_err_dma_buf_release: 720 - kfree(dma_buf); 729 + dma_free_coherent(devc, payload_max_size, dma_buf, dma_buf_phy); 721 730 return rv; 722 731 } 723 732
+21 -2
include/linux/hid.h
··· 342 342 * HID device quirks. 343 343 */ 344 344 345 - /* 345 + /* 346 346 * Increase this if you need to configure more HID quirks at module load time 347 347 */ 348 348 #define MAX_USBHID_BOOT_QUIRKS 4 349 349 350 - #define HID_QUIRK_INVERT BIT(0) 350 + /* BIT(0) reserved for backward compatibility, was HID_QUIRK_INVERT */ 351 351 #define HID_QUIRK_NOTOUCH BIT(1) 352 352 #define HID_QUIRK_IGNORE BIT(2) 353 353 #define HID_QUIRK_NOGET BIT(3) ··· 476 476 unsigned report_count; /* number of this field in the report */ 477 477 unsigned report_type; /* (input,output,feature) */ 478 478 __s32 *value; /* last known value(s) */ 479 + __s32 *new_value; /* newly read value(s) */ 480 + __s32 *usages_priorities; /* priority of each usage when reading the report 481 + * bits 8-16 are reserved for hid-input usage 482 + */ 479 483 __s32 logical_minimum; 480 484 __s32 logical_maximum; 481 485 __s32 physical_minimum; 482 486 __s32 physical_maximum; 483 487 __s32 unit_exponent; 484 488 unsigned unit; 489 + bool ignored; /* this field is ignored in this event */ 485 490 struct hid_report *report; /* associated report */ 486 491 unsigned index; /* index into report->field[] */ 487 492 /* hidinput data */ 488 493 struct hid_input *hidinput; /* associated input structure */ 489 494 __u16 dpad; /* dpad input code */ 495 + unsigned int slot_idx; /* slot index in a report */ 490 496 }; 491 497 492 498 #define HID_MAX_FIELDS 256 493 499 500 + struct hid_field_entry { 501 + struct list_head list; 502 + struct hid_field *field; 503 + unsigned int index; 504 + __s32 priority; 505 + }; 506 + 494 507 struct hid_report { 495 508 struct list_head list; 496 509 struct list_head hidinput_list; 510 + struct list_head field_entry_list; /* ordered list of input fields */ 497 511 unsigned int id; /* id of this report */ 498 512 unsigned int type; /* report type */ 499 513 unsigned int application; /* application usage for this report */ 500 514 struct hid_field *field[HID_MAX_FIELDS]; /* fields of the report */ 515 + struct hid_field_entry *field_entries; /* allocated memory of input field_entry */ 501 516 unsigned maxfield; /* maximum valid field index */ 502 517 unsigned size; /* size of the report (bits) */ 503 518 struct hid_device *device; /* associated device */ 519 + 520 + /* tool related state */ 521 + bool tool_active; /* whether the current tool is active */ 522 + unsigned int tool; /* BTN_TOOL_* */ 504 523 }; 505 524 506 525 #define HID_MAX_IDS 256