^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * HID driver for Holtek keyboard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2012 Tom Harwood
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "usbhid/usbhid.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* Holtek based keyboards (USB ID 04d9:a055) have the following issues:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * - The report descriptor specifies an excessively large number of consumer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * usages (2^15), which is more than HID_MAX_USAGES. This prevents proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * parsing of the report descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * - The report descriptor reports on caps/scroll/num lock key presses, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * doesn't have an LED output usage block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * The replacement descriptor below fixes the number of consumer usages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * and provides an LED output usage block. LED output events are redirected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * to the boot interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static __u8 holtek_kbd_rdesc_fixed[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* Original report descriptor, with reduced number of consumer usages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) 0x05, 0x01, /* Usage Page (Desktop), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) 0x09, 0x80, /* Usage (Sys Control), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) 0xA1, 0x01, /* Collection (Application), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) 0x85, 0x01, /* Report ID (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) 0x19, 0x81, /* Usage Minimum (Sys Power Down), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) 0x29, 0x83, /* Usage Maximum (Sys Wake Up), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) 0x15, 0x00, /* Logical Minimum (0), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) 0x25, 0x01, /* Logical Maximum (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) 0x95, 0x03, /* Report Count (3), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) 0x75, 0x01, /* Report Size (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) 0x81, 0x02, /* Input (Variable), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) 0x95, 0x01, /* Report Count (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) 0x75, 0x05, /* Report Size (5), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) 0x81, 0x01, /* Input (Constant), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) 0xC0, /* End Collection, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) 0x05, 0x0C, /* Usage Page (Consumer), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) 0x09, 0x01, /* Usage (Consumer Control), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) 0xA1, 0x01, /* Collection (Application), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) 0x85, 0x02, /* Report ID (2), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) 0x19, 0x00, /* Usage Minimum (00h), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) 0x2A, 0xFF, 0x2F, /* Usage Maximum (0x2FFF), previously 0x7FFF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) 0x15, 0x00, /* Logical Minimum (0), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) 0x26, 0xFF, 0x2F, /* Logical Maximum (0x2FFF),previously 0x7FFF*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) 0x95, 0x01, /* Report Count (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) 0x75, 0x10, /* Report Size (16), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) 0x81, 0x00, /* Input, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) 0xC0, /* End Collection, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) 0x05, 0x01, /* Usage Page (Desktop), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) 0x09, 0x06, /* Usage (Keyboard), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) 0xA1, 0x01, /* Collection (Application), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) 0x85, 0x03, /* Report ID (3), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) 0x95, 0x38, /* Report Count (56), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) 0x75, 0x01, /* Report Size (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) 0x15, 0x00, /* Logical Minimum (0), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) 0x25, 0x01, /* Logical Maximum (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) 0x05, 0x07, /* Usage Page (Keyboard), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) 0x19, 0xE0, /* Usage Minimum (KB Leftcontrol), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) 0x29, 0xE7, /* Usage Maximum (KB Right GUI), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) 0x19, 0x00, /* Usage Minimum (None), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) 0x29, 0x2F, /* Usage Maximum (KB Lboxbracket And Lbrace),*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) 0x81, 0x02, /* Input (Variable), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) 0xC0, /* End Collection, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) 0x05, 0x01, /* Usage Page (Desktop), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) 0x09, 0x06, /* Usage (Keyboard), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) 0xA1, 0x01, /* Collection (Application), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) 0x85, 0x04, /* Report ID (4), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) 0x95, 0x38, /* Report Count (56), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) 0x75, 0x01, /* Report Size (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) 0x15, 0x00, /* Logical Minimum (0), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) 0x25, 0x01, /* Logical Maximum (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) 0x05, 0x07, /* Usage Page (Keyboard), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) 0x19, 0x30, /* Usage Minimum (KB Rboxbracket And Rbrace),*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) 0x29, 0x67, /* Usage Maximum (KP Equals), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) 0x81, 0x02, /* Input (Variable), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) 0xC0, /* End Collection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* LED usage for the boot protocol interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) 0x05, 0x01, /* Usage Page (Desktop), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) 0x09, 0x06, /* Usage (Keyboard), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) 0xA1, 0x01, /* Collection (Application), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) 0x05, 0x08, /* Usage Page (LED), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) 0x19, 0x01, /* Usage Minimum (01h), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) 0x29, 0x03, /* Usage Maximum (03h), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) 0x15, 0x00, /* Logical Minimum (0), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) 0x25, 0x01, /* Logical Maximum (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) 0x75, 0x01, /* Report Size (1), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) 0x95, 0x03, /* Report Count (3), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) 0x91, 0x02, /* Output (Variable), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 0x95, 0x05, /* Report Count (5), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 0x91, 0x01, /* Output (Constant), */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 0xC0, /* End Collection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static __u8 *holtek_kbd_report_fixup(struct hid_device *hdev, __u8 *rdesc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) unsigned int *rsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) rdesc = holtek_kbd_rdesc_fixed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) *rsize = sizeof(holtek_kbd_rdesc_fixed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return rdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int holtek_kbd_input_event(struct input_dev *dev, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned int code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct hid_device *hid = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct usb_device *usb_dev = hid_to_usb_dev(hid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* Locate the boot interface, to receive the LED change events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct usb_interface *boot_interface = usb_ifnum_to_if(usb_dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct hid_device *boot_hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct hid_input *boot_hid_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (unlikely(boot_interface == NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) boot_hid = usb_get_intfdata(boot_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) boot_hid_input = list_first_entry(&boot_hid->inputs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct hid_input, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return boot_hid_input->input->event(boot_hid_input->input, type, code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int holtek_kbd_probe(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct usb_interface *intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (!hid_is_usb(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ret = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) intf = to_usb_interface(hdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!ret && intf->cur_altsetting->desc.bInterfaceNumber == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct hid_input *hidinput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) list_for_each_entry(hidinput, &hdev->inputs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) hidinput->input->event = holtek_kbd_input_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static const struct hid_device_id holtek_kbd_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODULE_DEVICE_TABLE(hid, holtek_kbd_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static struct hid_driver holtek_kbd_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) .name = "holtek_kbd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .id_table = holtek_kbd_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .report_fixup = holtek_kbd_report_fixup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .probe = holtek_kbd_probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) module_hid_driver(holtek_kbd_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) MODULE_LICENSE("GPL");