18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * HID driver for Holtek keyboard 48c2ecf20Sopenharmony_ci * Copyright (c) 2012 Tom Harwood 58c2ecf20Sopenharmony_ci*/ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci/* 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/device.h> 118c2ecf20Sopenharmony_ci#include <linux/hid.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/usb.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include "hid-ids.h" 168c2ecf20Sopenharmony_ci#include "usbhid/usbhid.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* Holtek based keyboards (USB ID 04d9:a055) have the following issues: 198c2ecf20Sopenharmony_ci * - The report descriptor specifies an excessively large number of consumer 208c2ecf20Sopenharmony_ci * usages (2^15), which is more than HID_MAX_USAGES. This prevents proper 218c2ecf20Sopenharmony_ci * parsing of the report descriptor. 228c2ecf20Sopenharmony_ci * - The report descriptor reports on caps/scroll/num lock key presses, but 238c2ecf20Sopenharmony_ci * doesn't have an LED output usage block. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * The replacement descriptor below fixes the number of consumer usages, 268c2ecf20Sopenharmony_ci * and provides an LED output usage block. LED output events are redirected 278c2ecf20Sopenharmony_ci * to the boot interface. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic __u8 holtek_kbd_rdesc_fixed[] = { 318c2ecf20Sopenharmony_ci /* Original report descriptor, with reduced number of consumer usages */ 328c2ecf20Sopenharmony_ci 0x05, 0x01, /* Usage Page (Desktop), */ 338c2ecf20Sopenharmony_ci 0x09, 0x80, /* Usage (Sys Control), */ 348c2ecf20Sopenharmony_ci 0xA1, 0x01, /* Collection (Application), */ 358c2ecf20Sopenharmony_ci 0x85, 0x01, /* Report ID (1), */ 368c2ecf20Sopenharmony_ci 0x19, 0x81, /* Usage Minimum (Sys Power Down), */ 378c2ecf20Sopenharmony_ci 0x29, 0x83, /* Usage Maximum (Sys Wake Up), */ 388c2ecf20Sopenharmony_ci 0x15, 0x00, /* Logical Minimum (0), */ 398c2ecf20Sopenharmony_ci 0x25, 0x01, /* Logical Maximum (1), */ 408c2ecf20Sopenharmony_ci 0x95, 0x03, /* Report Count (3), */ 418c2ecf20Sopenharmony_ci 0x75, 0x01, /* Report Size (1), */ 428c2ecf20Sopenharmony_ci 0x81, 0x02, /* Input (Variable), */ 438c2ecf20Sopenharmony_ci 0x95, 0x01, /* Report Count (1), */ 448c2ecf20Sopenharmony_ci 0x75, 0x05, /* Report Size (5), */ 458c2ecf20Sopenharmony_ci 0x81, 0x01, /* Input (Constant), */ 468c2ecf20Sopenharmony_ci 0xC0, /* End Collection, */ 478c2ecf20Sopenharmony_ci 0x05, 0x0C, /* Usage Page (Consumer), */ 488c2ecf20Sopenharmony_ci 0x09, 0x01, /* Usage (Consumer Control), */ 498c2ecf20Sopenharmony_ci 0xA1, 0x01, /* Collection (Application), */ 508c2ecf20Sopenharmony_ci 0x85, 0x02, /* Report ID (2), */ 518c2ecf20Sopenharmony_ci 0x19, 0x00, /* Usage Minimum (00h), */ 528c2ecf20Sopenharmony_ci 0x2A, 0xFF, 0x2F, /* Usage Maximum (0x2FFF), previously 0x7FFF */ 538c2ecf20Sopenharmony_ci 0x15, 0x00, /* Logical Minimum (0), */ 548c2ecf20Sopenharmony_ci 0x26, 0xFF, 0x2F, /* Logical Maximum (0x2FFF),previously 0x7FFF*/ 558c2ecf20Sopenharmony_ci 0x95, 0x01, /* Report Count (1), */ 568c2ecf20Sopenharmony_ci 0x75, 0x10, /* Report Size (16), */ 578c2ecf20Sopenharmony_ci 0x81, 0x00, /* Input, */ 588c2ecf20Sopenharmony_ci 0xC0, /* End Collection, */ 598c2ecf20Sopenharmony_ci 0x05, 0x01, /* Usage Page (Desktop), */ 608c2ecf20Sopenharmony_ci 0x09, 0x06, /* Usage (Keyboard), */ 618c2ecf20Sopenharmony_ci 0xA1, 0x01, /* Collection (Application), */ 628c2ecf20Sopenharmony_ci 0x85, 0x03, /* Report ID (3), */ 638c2ecf20Sopenharmony_ci 0x95, 0x38, /* Report Count (56), */ 648c2ecf20Sopenharmony_ci 0x75, 0x01, /* Report Size (1), */ 658c2ecf20Sopenharmony_ci 0x15, 0x00, /* Logical Minimum (0), */ 668c2ecf20Sopenharmony_ci 0x25, 0x01, /* Logical Maximum (1), */ 678c2ecf20Sopenharmony_ci 0x05, 0x07, /* Usage Page (Keyboard), */ 688c2ecf20Sopenharmony_ci 0x19, 0xE0, /* Usage Minimum (KB Leftcontrol), */ 698c2ecf20Sopenharmony_ci 0x29, 0xE7, /* Usage Maximum (KB Right GUI), */ 708c2ecf20Sopenharmony_ci 0x19, 0x00, /* Usage Minimum (None), */ 718c2ecf20Sopenharmony_ci 0x29, 0x2F, /* Usage Maximum (KB Lboxbracket And Lbrace),*/ 728c2ecf20Sopenharmony_ci 0x81, 0x02, /* Input (Variable), */ 738c2ecf20Sopenharmony_ci 0xC0, /* End Collection, */ 748c2ecf20Sopenharmony_ci 0x05, 0x01, /* Usage Page (Desktop), */ 758c2ecf20Sopenharmony_ci 0x09, 0x06, /* Usage (Keyboard), */ 768c2ecf20Sopenharmony_ci 0xA1, 0x01, /* Collection (Application), */ 778c2ecf20Sopenharmony_ci 0x85, 0x04, /* Report ID (4), */ 788c2ecf20Sopenharmony_ci 0x95, 0x38, /* Report Count (56), */ 798c2ecf20Sopenharmony_ci 0x75, 0x01, /* Report Size (1), */ 808c2ecf20Sopenharmony_ci 0x15, 0x00, /* Logical Minimum (0), */ 818c2ecf20Sopenharmony_ci 0x25, 0x01, /* Logical Maximum (1), */ 828c2ecf20Sopenharmony_ci 0x05, 0x07, /* Usage Page (Keyboard), */ 838c2ecf20Sopenharmony_ci 0x19, 0x30, /* Usage Minimum (KB Rboxbracket And Rbrace),*/ 848c2ecf20Sopenharmony_ci 0x29, 0x67, /* Usage Maximum (KP Equals), */ 858c2ecf20Sopenharmony_ci 0x81, 0x02, /* Input (Variable), */ 868c2ecf20Sopenharmony_ci 0xC0, /* End Collection */ 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci /* LED usage for the boot protocol interface */ 898c2ecf20Sopenharmony_ci 0x05, 0x01, /* Usage Page (Desktop), */ 908c2ecf20Sopenharmony_ci 0x09, 0x06, /* Usage (Keyboard), */ 918c2ecf20Sopenharmony_ci 0xA1, 0x01, /* Collection (Application), */ 928c2ecf20Sopenharmony_ci 0x05, 0x08, /* Usage Page (LED), */ 938c2ecf20Sopenharmony_ci 0x19, 0x01, /* Usage Minimum (01h), */ 948c2ecf20Sopenharmony_ci 0x29, 0x03, /* Usage Maximum (03h), */ 958c2ecf20Sopenharmony_ci 0x15, 0x00, /* Logical Minimum (0), */ 968c2ecf20Sopenharmony_ci 0x25, 0x01, /* Logical Maximum (1), */ 978c2ecf20Sopenharmony_ci 0x75, 0x01, /* Report Size (1), */ 988c2ecf20Sopenharmony_ci 0x95, 0x03, /* Report Count (3), */ 998c2ecf20Sopenharmony_ci 0x91, 0x02, /* Output (Variable), */ 1008c2ecf20Sopenharmony_ci 0x95, 0x05, /* Report Count (5), */ 1018c2ecf20Sopenharmony_ci 0x91, 0x01, /* Output (Constant), */ 1028c2ecf20Sopenharmony_ci 0xC0, /* End Collection */ 1038c2ecf20Sopenharmony_ci}; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistatic __u8 *holtek_kbd_report_fixup(struct hid_device *hdev, __u8 *rdesc, 1068c2ecf20Sopenharmony_ci unsigned int *rsize) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci struct usb_interface *intf = to_usb_interface(hdev->dev.parent); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci if (intf->cur_altsetting->desc.bInterfaceNumber == 1) { 1118c2ecf20Sopenharmony_ci rdesc = holtek_kbd_rdesc_fixed; 1128c2ecf20Sopenharmony_ci *rsize = sizeof(holtek_kbd_rdesc_fixed); 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci return rdesc; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int holtek_kbd_input_event(struct input_dev *dev, unsigned int type, 1188c2ecf20Sopenharmony_ci unsigned int code, 1198c2ecf20Sopenharmony_ci int value) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci struct hid_device *hid = input_get_drvdata(dev); 1228c2ecf20Sopenharmony_ci struct usb_device *usb_dev = hid_to_usb_dev(hid); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci /* Locate the boot interface, to receive the LED change events */ 1258c2ecf20Sopenharmony_ci struct usb_interface *boot_interface = usb_ifnum_to_if(usb_dev, 0); 1268c2ecf20Sopenharmony_ci struct hid_device *boot_hid; 1278c2ecf20Sopenharmony_ci struct hid_input *boot_hid_input; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci if (unlikely(boot_interface == NULL)) 1308c2ecf20Sopenharmony_ci return -ENODEV; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci boot_hid = usb_get_intfdata(boot_interface); 1338c2ecf20Sopenharmony_ci if (list_empty(&boot_hid->inputs)) { 1348c2ecf20Sopenharmony_ci hid_err(hid, "no inputs found\n"); 1358c2ecf20Sopenharmony_ci return -ENODEV; 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci boot_hid_input = list_first_entry(&boot_hid->inputs, 1388c2ecf20Sopenharmony_ci struct hid_input, list); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci return boot_hid_input->input->event(boot_hid_input->input, type, code, 1418c2ecf20Sopenharmony_ci value); 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic int holtek_kbd_probe(struct hid_device *hdev, 1458c2ecf20Sopenharmony_ci const struct hid_device_id *id) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci struct usb_interface *intf; 1488c2ecf20Sopenharmony_ci int ret; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci if (!hid_is_usb(hdev)) 1518c2ecf20Sopenharmony_ci return -EINVAL; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci ret = hid_parse(hdev); 1548c2ecf20Sopenharmony_ci if (!ret) 1558c2ecf20Sopenharmony_ci ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci intf = to_usb_interface(hdev->dev.parent); 1588c2ecf20Sopenharmony_ci if (!ret && intf->cur_altsetting->desc.bInterfaceNumber == 1) { 1598c2ecf20Sopenharmony_ci struct hid_input *hidinput; 1608c2ecf20Sopenharmony_ci list_for_each_entry(hidinput, &hdev->inputs, list) { 1618c2ecf20Sopenharmony_ci hidinput->input->event = holtek_kbd_input_event; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci return ret; 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic const struct hid_device_id holtek_kbd_devices[] = { 1698c2ecf20Sopenharmony_ci { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, 1708c2ecf20Sopenharmony_ci USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD) }, 1718c2ecf20Sopenharmony_ci { } 1728c2ecf20Sopenharmony_ci}; 1738c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, holtek_kbd_devices); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic struct hid_driver holtek_kbd_driver = { 1768c2ecf20Sopenharmony_ci .name = "holtek_kbd", 1778c2ecf20Sopenharmony_ci .id_table = holtek_kbd_devices, 1788c2ecf20Sopenharmony_ci .report_fixup = holtek_kbd_report_fixup, 1798c2ecf20Sopenharmony_ci .probe = holtek_kbd_probe 1808c2ecf20Sopenharmony_ci}; 1818c2ecf20Sopenharmony_cimodule_hid_driver(holtek_kbd_driver); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 184