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