xref: /linux/drivers/hid/hid-huawei.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  HID driver for some huawei "special" devices
4  *
5  * Copyright (c) 2026 Miao Li <limiao@kylinos.cn>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/hid.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 
13 #include "hid-ids.h"
14 
15 static const __u8 huawei_cd30_kbd_rdesc_fixed[] = {
16 	0x05, 0x01,		/* Usage Page (Generic Desktop)		*/
17 	0x09, 0x80,		/* Usage (System Control)		*/
18 	0xa1, 0x01,		/* Collection (Application)		*/
19 	0x85, 0x01,		/*   Report ID (1)			*/
20 	0x19, 0x81,		/*   Usage Minimum (System Power Down)	*/
21 	0x29, 0x83,		/*   Usage Maximum (System Wake Up)	*/
22 	0x15, 0x00,		/*   Logical Minimum (0)		*/
23 	0x25, 0x01,		/*   Logical Maximum (1)		*/
24 	0x75, 0x01,		/*   Report Size (1 bit)		*/
25 	0x95, 0x03,		/*   Report Count (3)			*/
26 	0x81, 0x02,		/*   Input (Data,Var,Abs)		*/
27 	0x95, 0x05,		/*   Report Count (5)			*/
28 	0x81, 0x01,		/*   Input (Cnst,Ary,Abs)		*/
29 	0xc0,			/* End Collection			*/
30 	0x05, 0x0c,		/* Usage Page (Consumer)		*/
31 	0x09, 0x01,		/* Usage (Consumer Control)		*/
32 	0xa1, 0x01,		/* Collection (Application)		*/
33 	0x85, 0x02,		/*   Report ID (2)			*/
34 	0x19, 0x00,		/*   Usage Minimum (0)			*/
35 	0x2a, 0x3c, 0x02,	/*   Usage Maximum (0x023C)		*/
36 	0x15, 0x00,		/*   Logical Minimum (0)		*/
37 	0x26, 0x3c, 0x02,	/*   Logical Maximum (0x023C)		*/
38 	0x95, 0x01,		/*   Report Count (1)			*/
39 	0x75, 0x10,		/*   Report Size (16 bits)		*/
40 	0x81, 0x00,		/*   Input (Data,Ary,Abs)		*/
41 	0xc0			/* End Collection			*/
42 };
43 
44 static const __u8 *huawei_report_fixup(struct hid_device *hdev, __u8 *rdesc,
45 				  unsigned int *rsize)
46 {
47 	struct usb_interface *intf = hid_is_usb(hdev) ?
48 			to_usb_interface(hdev->dev.parent) : NULL;
49 
50 	switch (hdev->product) {
51 	case USB_DEVICE_ID_HUAWEI_CD30KBD:
52 		if (!intf || intf->cur_altsetting->desc.bInterfaceNumber == 1) {
53 			if (*rsize != sizeof(huawei_cd30_kbd_rdesc_fixed) ||
54 				memcmp(huawei_cd30_kbd_rdesc_fixed, rdesc,
55 					sizeof(huawei_cd30_kbd_rdesc_fixed)) != 0) {
56 				hid_info(hdev, "Replacing Huawei cd30 keyboard report descriptor.\n");
57 				*rsize = sizeof(huawei_cd30_kbd_rdesc_fixed);
58 				return huawei_cd30_kbd_rdesc_fixed;
59 			}
60 		}
61 		break;
62 	}
63 
64 	return rdesc;
65 }
66 
67 static const struct hid_device_id huawei_devices[] = {
68 	/* HUAWEI cd30 keyboard */
69 	{ HID_USB_DEVICE(USB_VENDOR_ID_HUAWEI, USB_DEVICE_ID_HUAWEI_CD30KBD)},
70 	{ }
71 };
72 MODULE_DEVICE_TABLE(hid, huawei_devices);
73 
74 static struct hid_driver huawei_driver = {
75 	.name = "huawei",
76 	.id_table = huawei_devices,
77 	.report_fixup = huawei_report_fixup,
78 };
79 module_hid_driver(huawei_driver);
80 
81 MODULE_LICENSE("GPL");
82 MODULE_AUTHOR("Miao Li <limiao@kylinos.cn>");
83 MODULE_DESCRIPTION("HID driver for some huawei \"special\" devices");
84