1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  USB HID driver for Glorious PC Gaming Race
4  *  Glorious Model O, O- and D mice.
5  *
6  *  Copyright (c) 2020 Samuel Čavoj <sammko@sammserver.com>
7  */
8 
9 /*
10  */
11 
12 #include <linux/hid.h>
13 #include <linux/module.h>
14 
15 #include "hid-ids.h"
16 
17 MODULE_AUTHOR("Samuel Čavoj <sammko@sammserver.com>");
18 MODULE_DESCRIPTION("HID driver for Glorious PC Gaming Race mice");
19 
20 /*
21  * Glorious Model O and O- specify the const flag in the consumer input
22  * report descriptor, which leads to inputs being ignored. Fix this
23  * by patching the descriptor.
24  */
glorious_report_fixup(struct hid_device * hdev,__u8 * rdesc,unsigned int * rsize)25 static __u8 *glorious_report_fixup(struct hid_device *hdev, __u8 *rdesc,
26 		unsigned int *rsize)
27 {
28 	if (*rsize == 213 &&
29 		rdesc[84] == 129 && rdesc[112] == 129 && rdesc[140] == 129 &&
30 		rdesc[85] == 3   && rdesc[113] == 3   && rdesc[141] == 3) {
31 		hid_info(hdev, "patching Glorious Model O consumer control report descriptor\n");
32 		rdesc[85] = rdesc[113] = rdesc[141] = \
33 			HID_MAIN_ITEM_VARIABLE | HID_MAIN_ITEM_RELATIVE;
34 	}
35 	return rdesc;
36 }
37 
glorious_update_name(struct hid_device * hdev)38 static void glorious_update_name(struct hid_device *hdev)
39 {
40 	const char *model = "Device";
41 
42 	switch (hdev->product) {
43 	case USB_DEVICE_ID_GLORIOUS_MODEL_O:
44 		model = "Model O"; break;
45 	case USB_DEVICE_ID_GLORIOUS_MODEL_D:
46 		model = "Model D"; break;
47 	}
48 
49 	snprintf(hdev->name, sizeof(hdev->name), "%s %s", "Glorious", model);
50 }
51 
glorious_probe(struct hid_device * hdev,const struct hid_device_id * id)52 static int glorious_probe(struct hid_device *hdev,
53 		const struct hid_device_id *id)
54 {
55 	int ret;
56 
57 	hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
58 
59 	ret = hid_parse(hdev);
60 	if (ret)
61 		return ret;
62 
63 	glorious_update_name(hdev);
64 
65 	return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
66 }
67 
68 static const struct hid_device_id glorious_devices[] = {
69 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLORIOUS,
70 		USB_DEVICE_ID_GLORIOUS_MODEL_O) },
71 	{ HID_USB_DEVICE(USB_VENDOR_ID_GLORIOUS,
72 		USB_DEVICE_ID_GLORIOUS_MODEL_D) },
73 	{ }
74 };
75 MODULE_DEVICE_TABLE(hid, glorious_devices);
76 
77 static struct hid_driver glorious_driver = {
78 	.name = "glorious",
79 	.id_table = glorious_devices,
80 	.probe = glorious_probe,
81 	.report_fixup = glorious_report_fixup
82 };
83 
84 module_hid_driver(glorious_driver);
85 
86 MODULE_LICENSE("GPL");
87