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