Linux Audio

Check our new training course

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