// SPDX-License-Identifier: GPL-2.0-or-later1/*2* HID driver for Holtek keyboard3* Copyright (c) 2012 Tom Harwood4*/56/*7*/89#include <linux/device.h>10#include <linux/hid.h>11#include <linux/module.h>12#include <linux/usb.h>1314#include "hid-ids.h"15#include "usbhid/usbhid.h"1617/* Holtek based keyboards (USB ID 04d9:a055) have the following issues:18* - The report descriptor specifies an excessively large number of consumer19* usages (2^15), which is more than HID_MAX_USAGES. This prevents proper20* parsing of the report descriptor.21* - The report descriptor reports on caps/scroll/num lock key presses, but22* doesn't have an LED output usage block.23*24* The replacement descriptor below fixes the number of consumer usages,25* and provides an LED output usage block. LED output events are redirected26* to the boot interface.27*/2829static const __u8 holtek_kbd_rdesc_fixed[] = {30/* Original report descriptor, with reduced number of consumer usages */310x05, 0x01, /* Usage Page (Desktop), */320x09, 0x80, /* Usage (Sys Control), */330xA1, 0x01, /* Collection (Application), */340x85, 0x01, /* Report ID (1), */350x19, 0x81, /* Usage Minimum (Sys Power Down), */360x29, 0x83, /* Usage Maximum (Sys Wake Up), */370x15, 0x00, /* Logical Minimum (0), */380x25, 0x01, /* Logical Maximum (1), */390x95, 0x03, /* Report Count (3), */400x75, 0x01, /* Report Size (1), */410x81, 0x02, /* Input (Variable), */420x95, 0x01, /* Report Count (1), */430x75, 0x05, /* Report Size (5), */440x81, 0x01, /* Input (Constant), */450xC0, /* End Collection, */460x05, 0x0C, /* Usage Page (Consumer), */470x09, 0x01, /* Usage (Consumer Control), */480xA1, 0x01, /* Collection (Application), */490x85, 0x02, /* Report ID (2), */500x19, 0x00, /* Usage Minimum (00h), */510x2A, 0xFF, 0x2F, /* Usage Maximum (0x2FFF), previously 0x7FFF */520x15, 0x00, /* Logical Minimum (0), */530x26, 0xFF, 0x2F, /* Logical Maximum (0x2FFF),previously 0x7FFF*/540x95, 0x01, /* Report Count (1), */550x75, 0x10, /* Report Size (16), */560x81, 0x00, /* Input, */570xC0, /* End Collection, */580x05, 0x01, /* Usage Page (Desktop), */590x09, 0x06, /* Usage (Keyboard), */600xA1, 0x01, /* Collection (Application), */610x85, 0x03, /* Report ID (3), */620x95, 0x38, /* Report Count (56), */630x75, 0x01, /* Report Size (1), */640x15, 0x00, /* Logical Minimum (0), */650x25, 0x01, /* Logical Maximum (1), */660x05, 0x07, /* Usage Page (Keyboard), */670x19, 0xE0, /* Usage Minimum (KB Leftcontrol), */680x29, 0xE7, /* Usage Maximum (KB Right GUI), */690x19, 0x00, /* Usage Minimum (None), */700x29, 0x2F, /* Usage Maximum (KB Lboxbracket And Lbrace),*/710x81, 0x02, /* Input (Variable), */720xC0, /* End Collection, */730x05, 0x01, /* Usage Page (Desktop), */740x09, 0x06, /* Usage (Keyboard), */750xA1, 0x01, /* Collection (Application), */760x85, 0x04, /* Report ID (4), */770x95, 0x38, /* Report Count (56), */780x75, 0x01, /* Report Size (1), */790x15, 0x00, /* Logical Minimum (0), */800x25, 0x01, /* Logical Maximum (1), */810x05, 0x07, /* Usage Page (Keyboard), */820x19, 0x30, /* Usage Minimum (KB Rboxbracket And Rbrace),*/830x29, 0x67, /* Usage Maximum (KP Equals), */840x81, 0x02, /* Input (Variable), */850xC0, /* End Collection */8687/* LED usage for the boot protocol interface */880x05, 0x01, /* Usage Page (Desktop), */890x09, 0x06, /* Usage (Keyboard), */900xA1, 0x01, /* Collection (Application), */910x05, 0x08, /* Usage Page (LED), */920x19, 0x01, /* Usage Minimum (01h), */930x29, 0x03, /* Usage Maximum (03h), */940x15, 0x00, /* Logical Minimum (0), */950x25, 0x01, /* Logical Maximum (1), */960x75, 0x01, /* Report Size (1), */970x95, 0x03, /* Report Count (3), */980x91, 0x02, /* Output (Variable), */990x95, 0x05, /* Report Count (5), */1000x91, 0x01, /* Output (Constant), */1010xC0, /* End Collection */102};103104static const __u8 *holtek_kbd_report_fixup(struct hid_device *hdev, __u8 *rdesc,105unsigned int *rsize)106{107struct usb_interface *intf = to_usb_interface(hdev->dev.parent);108109if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {110*rsize = sizeof(holtek_kbd_rdesc_fixed);111return holtek_kbd_rdesc_fixed;112}113return rdesc;114}115116static int holtek_kbd_input_event(struct input_dev *dev, unsigned int type,117unsigned int code,118int value)119{120struct hid_device *hid = input_get_drvdata(dev);121struct usb_device *usb_dev = hid_to_usb_dev(hid);122123/* Locate the boot interface, to receive the LED change events */124struct usb_interface *boot_interface = usb_ifnum_to_if(usb_dev, 0);125struct hid_device *boot_hid;126struct hid_input *boot_hid_input;127128if (unlikely(boot_interface == NULL))129return -ENODEV;130131boot_hid = usb_get_intfdata(boot_interface);132if (list_empty(&boot_hid->inputs)) {133hid_err(hid, "no inputs found\n");134return -ENODEV;135}136boot_hid_input = list_first_entry(&boot_hid->inputs,137struct hid_input, list);138139return boot_hid_input->input->event(boot_hid_input->input, type, code,140value);141}142143static int holtek_kbd_probe(struct hid_device *hdev,144const struct hid_device_id *id)145{146struct usb_interface *intf;147int ret;148149if (!hid_is_usb(hdev))150return -EINVAL;151152ret = hid_parse(hdev);153if (!ret)154ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);155156intf = to_usb_interface(hdev->dev.parent);157if (!ret && intf->cur_altsetting->desc.bInterfaceNumber == 1) {158struct hid_input *hidinput;159list_for_each_entry(hidinput, &hdev->inputs, list) {160hidinput->input->event = holtek_kbd_input_event;161}162}163164return ret;165}166167static const struct hid_device_id holtek_kbd_devices[] = {168{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT,169USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD) },170{ }171};172MODULE_DEVICE_TABLE(hid, holtek_kbd_devices);173174static struct hid_driver holtek_kbd_driver = {175.name = "holtek_kbd",176.id_table = holtek_kbd_devices,177.report_fixup = holtek_kbd_report_fixup,178.probe = holtek_kbd_probe179};180module_hid_driver(holtek_kbd_driver);181182MODULE_DESCRIPTION("HID driver for Holtek keyboard");183MODULE_LICENSE("GPL");184185186