Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * HID driver for Holtek keyboard
0004  * Copyright (c) 2012 Tom Harwood
0005 */
0006 
0007 /*
0008  */
0009 
0010 #include <linux/device.h>
0011 #include <linux/hid.h>
0012 #include <linux/module.h>
0013 #include <linux/usb.h>
0014 
0015 #include "hid-ids.h"
0016 #include "usbhid/usbhid.h"
0017 
0018 /* Holtek based keyboards (USB ID 04d9:a055) have the following issues:
0019  * - The report descriptor specifies an excessively large number of consumer
0020  *   usages (2^15), which is more than HID_MAX_USAGES. This prevents proper
0021  *   parsing of the report descriptor.
0022  * - The report descriptor reports on caps/scroll/num lock key presses, but
0023  *   doesn't have an LED output usage block.
0024  *
0025  * The replacement descriptor below fixes the number of consumer usages,
0026  * and provides an LED output usage block. LED output events are redirected
0027  * to the boot interface.
0028  */
0029 
0030 static __u8 holtek_kbd_rdesc_fixed[] = {
0031     /* Original report descriptor, with reduced number of consumer usages */
0032     0x05, 0x01,         /*  Usage Page (Desktop),                         */
0033     0x09, 0x80,         /*  Usage (Sys Control),                          */
0034     0xA1, 0x01,         /*  Collection (Application),                     */
0035     0x85, 0x01,         /*      Report ID (1),                            */
0036     0x19, 0x81,         /*      Usage Minimum (Sys Power Down),           */
0037     0x29, 0x83,         /*      Usage Maximum (Sys Wake Up),              */
0038     0x15, 0x00,         /*      Logical Minimum (0),                      */
0039     0x25, 0x01,         /*      Logical Maximum (1),                      */
0040     0x95, 0x03,         /*      Report Count (3),                         */
0041     0x75, 0x01,         /*      Report Size (1),                          */
0042     0x81, 0x02,         /*      Input (Variable),                         */
0043     0x95, 0x01,         /*      Report Count (1),                         */
0044     0x75, 0x05,         /*      Report Size (5),                          */
0045     0x81, 0x01,         /*      Input (Constant),                         */
0046     0xC0,               /*  End Collection,                               */
0047     0x05, 0x0C,         /*  Usage Page (Consumer),                        */
0048     0x09, 0x01,         /*  Usage (Consumer Control),                     */
0049     0xA1, 0x01,         /*  Collection (Application),                     */
0050     0x85, 0x02,         /*      Report ID (2),                            */
0051     0x19, 0x00,         /*      Usage Minimum (00h),                      */
0052     0x2A, 0xFF, 0x2F,   /*      Usage Maximum (0x2FFF), previously 0x7FFF */
0053     0x15, 0x00,         /*      Logical Minimum (0),                      */
0054     0x26, 0xFF, 0x2F,   /*      Logical Maximum (0x2FFF),previously 0x7FFF*/
0055     0x95, 0x01,         /*      Report Count (1),                         */
0056     0x75, 0x10,         /*      Report Size (16),                         */
0057     0x81, 0x00,         /*      Input,                                    */
0058     0xC0,               /*  End Collection,                               */
0059     0x05, 0x01,         /*  Usage Page (Desktop),                         */
0060     0x09, 0x06,         /*  Usage (Keyboard),                             */
0061     0xA1, 0x01,         /*  Collection (Application),                     */
0062     0x85, 0x03,         /*      Report ID (3),                            */
0063     0x95, 0x38,         /*      Report Count (56),                        */
0064     0x75, 0x01,         /*      Report Size (1),                          */
0065     0x15, 0x00,         /*      Logical Minimum (0),                      */
0066     0x25, 0x01,         /*      Logical Maximum (1),                      */
0067     0x05, 0x07,         /*      Usage Page (Keyboard),                    */
0068     0x19, 0xE0,         /*      Usage Minimum (KB Leftcontrol),           */
0069     0x29, 0xE7,         /*      Usage Maximum (KB Right GUI),             */
0070     0x19, 0x00,         /*      Usage Minimum (None),                     */
0071     0x29, 0x2F,         /*      Usage Maximum (KB Lboxbracket And Lbrace),*/
0072     0x81, 0x02,         /*      Input (Variable),                         */
0073     0xC0,               /*  End Collection,                               */
0074     0x05, 0x01,         /*  Usage Page (Desktop),                         */
0075     0x09, 0x06,         /*  Usage (Keyboard),                             */
0076     0xA1, 0x01,         /*  Collection (Application),                     */
0077     0x85, 0x04,         /*      Report ID (4),                            */
0078     0x95, 0x38,         /*      Report Count (56),                        */
0079     0x75, 0x01,         /*      Report Size (1),                          */
0080     0x15, 0x00,         /*      Logical Minimum (0),                      */
0081     0x25, 0x01,         /*      Logical Maximum (1),                      */
0082     0x05, 0x07,         /*      Usage Page (Keyboard),                    */
0083     0x19, 0x30,         /*      Usage Minimum (KB Rboxbracket And Rbrace),*/
0084     0x29, 0x67,         /*      Usage Maximum (KP Equals),                */
0085     0x81, 0x02,         /*      Input (Variable),                         */
0086     0xC0,               /*  End Collection                                */
0087 
0088     /* LED usage for the boot protocol interface */
0089     0x05, 0x01,         /*  Usage Page (Desktop),                         */
0090     0x09, 0x06,         /*  Usage (Keyboard),                             */
0091     0xA1, 0x01,         /*  Collection (Application),                     */
0092     0x05, 0x08,         /*      Usage Page (LED),                         */
0093     0x19, 0x01,         /*      Usage Minimum (01h),                      */
0094     0x29, 0x03,         /*      Usage Maximum (03h),                      */
0095     0x15, 0x00,         /*      Logical Minimum (0),                      */
0096     0x25, 0x01,         /*      Logical Maximum (1),                      */
0097     0x75, 0x01,         /*      Report Size (1),                          */
0098     0x95, 0x03,         /*      Report Count (3),                         */
0099     0x91, 0x02,         /*      Output (Variable),                        */
0100     0x95, 0x05,         /*      Report Count (5),                         */
0101     0x91, 0x01,         /*      Output (Constant),                        */
0102     0xC0,               /*  End Collection                                */
0103 };
0104 
0105 static __u8 *holtek_kbd_report_fixup(struct hid_device *hdev, __u8 *rdesc,
0106         unsigned int *rsize)
0107 {
0108     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0109 
0110     if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
0111         rdesc = holtek_kbd_rdesc_fixed;
0112         *rsize = sizeof(holtek_kbd_rdesc_fixed);
0113     }
0114     return rdesc;
0115 }
0116 
0117 static int holtek_kbd_input_event(struct input_dev *dev, unsigned int type,
0118         unsigned int code,
0119         int value)
0120 {
0121     struct hid_device *hid = input_get_drvdata(dev);
0122     struct usb_device *usb_dev = hid_to_usb_dev(hid);
0123 
0124     /* Locate the boot interface, to receive the LED change events */
0125     struct usb_interface *boot_interface = usb_ifnum_to_if(usb_dev, 0);
0126     struct hid_device *boot_hid;
0127     struct hid_input *boot_hid_input;
0128 
0129     if (unlikely(boot_interface == NULL))
0130         return -ENODEV;
0131 
0132     boot_hid = usb_get_intfdata(boot_interface);
0133     boot_hid_input = list_first_entry(&boot_hid->inputs,
0134         struct hid_input, list);
0135 
0136     return boot_hid_input->input->event(boot_hid_input->input, type, code,
0137             value);
0138 }
0139 
0140 static int holtek_kbd_probe(struct hid_device *hdev,
0141         const struct hid_device_id *id)
0142 {
0143     struct usb_interface *intf;
0144     int ret;
0145 
0146     if (!hid_is_usb(hdev))
0147         return -EINVAL;
0148 
0149     ret = hid_parse(hdev);
0150     if (!ret)
0151         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0152 
0153     intf = to_usb_interface(hdev->dev.parent);
0154     if (!ret && intf->cur_altsetting->desc.bInterfaceNumber == 1) {
0155         struct hid_input *hidinput;
0156         list_for_each_entry(hidinput, &hdev->inputs, list) {
0157             hidinput->input->event = holtek_kbd_input_event;
0158         }
0159     }
0160 
0161     return ret;
0162 }
0163 
0164 static const struct hid_device_id holtek_kbd_devices[] = {
0165     { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT,
0166             USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD) },
0167     { }
0168 };
0169 MODULE_DEVICE_TABLE(hid, holtek_kbd_devices);
0170 
0171 static struct hid_driver holtek_kbd_driver = {
0172     .name = "holtek_kbd",
0173     .id_table = holtek_kbd_devices,
0174     .report_fixup = holtek_kbd_report_fixup,
0175     .probe = holtek_kbd_probe
0176 };
0177 module_hid_driver(holtek_kbd_driver);
0178 
0179 MODULE_LICENSE("GPL");