xref: /OK3568_Linux_fs/kernel/drivers/hid/hid-maltron.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * HID driver for Maltron L90
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 1999 Andreas Gal
6*4882a593Smuzhiyun  * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
7*4882a593Smuzhiyun  * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
8*4882a593Smuzhiyun  * Copyright (c) 2008 Jiri Slaby
9*4882a593Smuzhiyun  * Copyright (c) 2012 David Dillow <dave@thedillows.org>
10*4882a593Smuzhiyun  * Copyright (c) 2006-2013 Jiri Kosina
11*4882a593Smuzhiyun  * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
12*4882a593Smuzhiyun  * Copyright (c) 2014-2016 Frank Praznik <frank.praznik@gmail.com>
13*4882a593Smuzhiyun  * Copyright (c) 2010 Richard Nauber <Richard.Nauber@gmail.com>
14*4882a593Smuzhiyun  * Copyright (c) 2016 Yuxuan Shui <yshuiv7@gmail.com>
15*4882a593Smuzhiyun  * Copyright (c) 2018 William Whistler <wtbw@wtbw.co.uk>
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/device.h>
19*4882a593Smuzhiyun #include <linux/hid.h>
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "hid-ids.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /* The original buggy USB descriptor */
25*4882a593Smuzhiyun static u8 maltron_rdesc_o[] = {
26*4882a593Smuzhiyun 	0x05, 0x01,        /* Usage Page (Generic Desktop Ctrls) */
27*4882a593Smuzhiyun 	0x09, 0x80,        /* Usage (Sys Control)                */
28*4882a593Smuzhiyun 	0xA1, 0x01,        /* Collection (Application)           */
29*4882a593Smuzhiyun 	0x85, 0x02,        /*   Report ID (2)                    */
30*4882a593Smuzhiyun 	0x75, 0x01,        /*   Report Size (1)                  */
31*4882a593Smuzhiyun 	0x95, 0x01,        /*   Report Count (1)                 */
32*4882a593Smuzhiyun 	0x15, 0x00,        /*   Logical Minimum (0)              */
33*4882a593Smuzhiyun 	0x25, 0x01,        /*   Logical Maximum (1)              */
34*4882a593Smuzhiyun 	0x09, 0x82,        /*   Usage (Sys Sleep)                */
35*4882a593Smuzhiyun 	0x81, 0x06,        /*   Input (Data,Var,Rel)             */
36*4882a593Smuzhiyun 	0x09, 0x82,        /*   Usage (Sys Sleep)                */
37*4882a593Smuzhiyun 	0x81, 0x06,        /*   Input (Data,Var,Rel)             */
38*4882a593Smuzhiyun 	0x09, 0x83,        /*   Usage (Sys Wake Up)              */
39*4882a593Smuzhiyun 	0x81, 0x06,        /*   Input (Data,Var,Rel)             */
40*4882a593Smuzhiyun 	0x75, 0x05,        /*   Report Size (5)                  */
41*4882a593Smuzhiyun 	0x81, 0x01,        /*   Input (Const,Array,Abs)          */
42*4882a593Smuzhiyun 	0xC0,              /* End Collection                     */
43*4882a593Smuzhiyun 	0x05, 0x0C,        /* Usage Page (Consumer)              */
44*4882a593Smuzhiyun 	0x09, 0x01,        /* Usage (Consumer Control)           */
45*4882a593Smuzhiyun 	0xA1, 0x01,        /* Collection (Application)           */
46*4882a593Smuzhiyun 	0x85, 0x03,        /*   Report ID (3)                    */
47*4882a593Smuzhiyun 	0x95, 0x01,        /*   Report Count (1)                 */
48*4882a593Smuzhiyun 	0x75, 0x10,        /*   Report Size (16)                 */
49*4882a593Smuzhiyun 	0x19, 0x00,        /*   Usage Minimum (Unassigned)       */
50*4882a593Smuzhiyun 	0x2A, 0xFF, 0x7F,  /*   Usage Maximum (0x7FFF)           */
51*4882a593Smuzhiyun 	0x81, 0x00,        /*   Input (Data,Array,Abs)           */
52*4882a593Smuzhiyun 	0xC0,              /* End Collection                     */
53*4882a593Smuzhiyun 	0x06, 0x7F, 0xFF,  /* Usage Page (Vendor Defined 0xFF7F) */
54*4882a593Smuzhiyun 	0x09, 0x01,        /* Usage (0x01)                       */
55*4882a593Smuzhiyun 	0xA1, 0x01,        /* Collection (Application)           */
56*4882a593Smuzhiyun 	0x85, 0x04,        /*   Report ID (4)                    */
57*4882a593Smuzhiyun 	0x95, 0x01,        /*   Report Count (1)                 */
58*4882a593Smuzhiyun 	0x75, 0x10,        /*   Report Size (16)                 */
59*4882a593Smuzhiyun 	0x19, 0x00,        /*   Usage Minimum (0x00)             */
60*4882a593Smuzhiyun 	0x2A, 0xFF, 0x7F,  /*   Usage Maximum (0x7FFF)           */
61*4882a593Smuzhiyun 	0x81, 0x00,        /*   Input (Data,Array,Abs)           */
62*4882a593Smuzhiyun 	0x75, 0x02,        /*   Report Size (2)                  */
63*4882a593Smuzhiyun 	0x25, 0x02,        /*   Logical Maximum (2)              */
64*4882a593Smuzhiyun 	0x09, 0x90,        /*   Usage (0x90)                     */
65*4882a593Smuzhiyun 	0xB1, 0x02,        /*   Feature (Data,Var,Abs)           */
66*4882a593Smuzhiyun 	0x75, 0x06,        /*   Report Size (6)                  */
67*4882a593Smuzhiyun 	0xB1, 0x01,        /*   Feature (Const,Array,Abs)        */
68*4882a593Smuzhiyun 	0x75, 0x01,        /*   Report Size (1)                  */
69*4882a593Smuzhiyun 	0x25, 0x01,        /*   Logical Maximum (1)              */
70*4882a593Smuzhiyun 	0x05, 0x08,        /*   Usage Page (LEDs)                */
71*4882a593Smuzhiyun 	0x09, 0x2A,        /*   Usage (On-Line)                  */
72*4882a593Smuzhiyun 	0x91, 0x02,        /*   Output (Data,Var,Abs)            */
73*4882a593Smuzhiyun 	0x09, 0x4B,        /*   Usage (Generic Indicator)        */
74*4882a593Smuzhiyun 	0x91, 0x02,        /*   Output (Data,Var,Abs)            */
75*4882a593Smuzhiyun 	0x75, 0x06,        /*   Report Size (6)                  */
76*4882a593Smuzhiyun 	0x95, 0x01,        /*   Report Count (1)                 */
77*4882a593Smuzhiyun 	0x91, 0x01,        /*   Output (Const,Array,Abs)         */
78*4882a593Smuzhiyun 	0xC0               /* End Collection                     */
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /* The patched descriptor, allowing media key events to be accepted as valid */
82*4882a593Smuzhiyun static u8 maltron_rdesc[] = {
83*4882a593Smuzhiyun 	0x05, 0x01,        /* Usage Page (Generic Desktop Ctrls) */
84*4882a593Smuzhiyun 	0x09, 0x80,        /* Usage (Sys Control)                */
85*4882a593Smuzhiyun 	0xA1, 0x01,        /* Collection (Application)           */
86*4882a593Smuzhiyun 	0x85, 0x02,        /*   Report ID (2)                    */
87*4882a593Smuzhiyun 	0x75, 0x01,        /*   Report Size (1)                  */
88*4882a593Smuzhiyun 	0x95, 0x01,        /*   Report Count (1)                 */
89*4882a593Smuzhiyun 	0x15, 0x00,        /*   Logical Minimum (0)              */
90*4882a593Smuzhiyun 	0x25, 0x01,        /*   Logical Maximum (1)              */
91*4882a593Smuzhiyun 	0x09, 0x82,        /*   Usage (Sys Sleep)                */
92*4882a593Smuzhiyun 	0x81, 0x06,        /*   Input (Data,Var,Rel)             */
93*4882a593Smuzhiyun 	0x09, 0x82,        /*   Usage (Sys Sleep)                */
94*4882a593Smuzhiyun 	0x81, 0x06,        /*   Input (Data,Var,Rel)             */
95*4882a593Smuzhiyun 	0x09, 0x83,        /*   Usage (Sys Wake Up)              */
96*4882a593Smuzhiyun 	0x81, 0x06,        /*   Input (Data,Var,Rel)             */
97*4882a593Smuzhiyun 	0x75, 0x05,        /*   Report Size (5)                  */
98*4882a593Smuzhiyun 	0x81, 0x01,        /*   Input (Const,Array,Abs)          */
99*4882a593Smuzhiyun 	0xC0,              /* End Collection                     */
100*4882a593Smuzhiyun 	0x05, 0x0C,        /* Usage Page (Consumer)              */
101*4882a593Smuzhiyun 	0x09, 0x01,        /* Usage (Consumer Control)           */
102*4882a593Smuzhiyun 	0xA1, 0x01,        /* Collection (Application)           */
103*4882a593Smuzhiyun 	0x85, 0x03,        /*   Report ID (3)                    */
104*4882a593Smuzhiyun 	0x15, 0x00,        /*   Logical Minimum (0)              - changed */
105*4882a593Smuzhiyun 	0x26, 0xFF, 0x7F,  /*   Logical Maximum (32767)          - changed */
106*4882a593Smuzhiyun 	0x95, 0x01,        /*   Report Count (1)                 */
107*4882a593Smuzhiyun 	0x75, 0x10,        /*   Report Size (16)                 */
108*4882a593Smuzhiyun 	0x19, 0x00,        /*   Usage Minimum (Unassigned)       */
109*4882a593Smuzhiyun 	0x2A, 0xFF, 0x7F,  /*   Usage Maximum (0x7FFF)           */
110*4882a593Smuzhiyun 	0x81, 0x00,        /*   Input (Data,Array,Abs)           */
111*4882a593Smuzhiyun 	0xC0,              /* End Collection                     */
112*4882a593Smuzhiyun 	0x06, 0x7F, 0xFF,  /* Usage Page (Vendor Defined 0xFF7F) */
113*4882a593Smuzhiyun 	0x09, 0x01,        /* Usage (0x01)                       */
114*4882a593Smuzhiyun 	0xA1, 0x01,        /* Collection (Application)           */
115*4882a593Smuzhiyun 	0x85, 0x04,        /*   Report ID (4)                    */
116*4882a593Smuzhiyun 	0x95, 0x01,        /*   Report Count (1)                 */
117*4882a593Smuzhiyun 	0x75, 0x10,        /*   Report Size (16)                 */
118*4882a593Smuzhiyun 	0x19, 0x00,        /*   Usage Minimum (0x00)             */
119*4882a593Smuzhiyun 	0x2A, 0xFF, 0x7F,  /*   Usage Maximum (0x7FFF)           */
120*4882a593Smuzhiyun 	0x81, 0x00,        /*   Input (Data,Array,Abs)           */
121*4882a593Smuzhiyun 	0x75, 0x02,        /*   Report Size (2)                  */
122*4882a593Smuzhiyun 	0x25, 0x02,        /*   Logical Maximum (2)              */
123*4882a593Smuzhiyun 	0x09, 0x90,        /*   Usage (0x90)                     */
124*4882a593Smuzhiyun 	0xB1, 0x02,        /*   Feature (Data,Var,Abs)           */
125*4882a593Smuzhiyun 	0x75, 0x06,        /*   Report Size (6)                  */
126*4882a593Smuzhiyun 	0xB1, 0x01,        /*   Feature (Const,Array,Abs)        */
127*4882a593Smuzhiyun 	0x75, 0x01,        /*   Report Size (1)                  */
128*4882a593Smuzhiyun 	0x25, 0x01,        /*   Logical Maximum (1)              */
129*4882a593Smuzhiyun 	0x05, 0x08,        /*   Usage Page (LEDs)                */
130*4882a593Smuzhiyun 	0x09, 0x2A,        /*   Usage (On-Line)                  */
131*4882a593Smuzhiyun 	0x91, 0x02,        /*   Output (Data,Var,Abs)            */
132*4882a593Smuzhiyun 	0x09, 0x4B,        /*   Usage (Generic Indicator)        */
133*4882a593Smuzhiyun 	0x91, 0x02,        /*   Output (Data,Var,Abs)            */
134*4882a593Smuzhiyun 	0x75, 0x06,        /*   Report Size (6)                  */
135*4882a593Smuzhiyun 	0x95, 0x01,        /*   Report Count (1)                 */
136*4882a593Smuzhiyun 	0x91, 0x01,        /*   Output (Const,Array,Abs)         */
137*4882a593Smuzhiyun 	0xC0               /* End Collection                     */
138*4882a593Smuzhiyun };
139*4882a593Smuzhiyun 
maltron_report_fixup(struct hid_device * hdev,__u8 * rdesc,unsigned int * rsize)140*4882a593Smuzhiyun static __u8 *maltron_report_fixup(struct hid_device *hdev, __u8 *rdesc,
141*4882a593Smuzhiyun 				  unsigned int *rsize)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	if (*rsize == sizeof(maltron_rdesc_o) &&
144*4882a593Smuzhiyun 	    !memcmp(maltron_rdesc_o, rdesc, sizeof(maltron_rdesc_o))) {
145*4882a593Smuzhiyun 		hid_info(hdev, "Replacing Maltron L90 keyboard report descriptor\n");
146*4882a593Smuzhiyun 		*rsize = sizeof(maltron_rdesc);
147*4882a593Smuzhiyun 		return maltron_rdesc;
148*4882a593Smuzhiyun 	}
149*4882a593Smuzhiyun 	return rdesc;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun static const struct hid_device_id maltron_devices[] = {
153*4882a593Smuzhiyun 	{ HID_USB_DEVICE(USB_VENDOR_ID_ALCOR, USB_DEVICE_ID_ALCOR_MALTRON_KB)},
154*4882a593Smuzhiyun 	{ }
155*4882a593Smuzhiyun };
156*4882a593Smuzhiyun MODULE_DEVICE_TABLE(hid, maltron_devices);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun static struct hid_driver maltron_driver = {
159*4882a593Smuzhiyun 	.name = "maltron",
160*4882a593Smuzhiyun 	.id_table = maltron_devices,
161*4882a593Smuzhiyun 	.report_fixup = maltron_report_fixup
162*4882a593Smuzhiyun };
163*4882a593Smuzhiyun module_hid_driver(maltron_driver);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun MODULE_LICENSE("GPL");
166