xref: /openbmc/linux/drivers/hid/hid-xiaomi.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1*95157723SIlya Skriblovsky // SPDX-License-Identifier: GPL-2.0
2*95157723SIlya Skriblovsky /*
3*95157723SIlya Skriblovsky  * HID driver for Xiaomi Mi Dual Mode Wireless Mouse Silent Edition
4*95157723SIlya Skriblovsky  *
5*95157723SIlya Skriblovsky  * Copyright (c) 2021 Ilya Skriblovsky
6*95157723SIlya Skriblovsky  */
7*95157723SIlya Skriblovsky 
8*95157723SIlya Skriblovsky #include <linux/init.h>
9*95157723SIlya Skriblovsky #include <linux/module.h>
10*95157723SIlya Skriblovsky #include <linux/hid.h>
11*95157723SIlya Skriblovsky 
12*95157723SIlya Skriblovsky #include "hid-ids.h"
13*95157723SIlya Skriblovsky 
14*95157723SIlya Skriblovsky /* Fixed Mi Silent Mouse report descriptor */
15*95157723SIlya Skriblovsky /* Button's Usage Maximum changed from 3 to 5 to make side buttons work */
16*95157723SIlya Skriblovsky #define MI_SILENT_MOUSE_ORIG_RDESC_LENGTH   87
17*95157723SIlya Skriblovsky static __u8 mi_silent_mouse_rdesc_fixed[] = {
18*95157723SIlya Skriblovsky 	0x05, 0x01,         /*  Usage Page (Desktop),               */
19*95157723SIlya Skriblovsky 	0x09, 0x02,         /*  Usage (Mouse),                      */
20*95157723SIlya Skriblovsky 	0xA1, 0x01,         /*  Collection (Application),           */
21*95157723SIlya Skriblovsky 	0x85, 0x03,         /*      Report ID (3),                  */
22*95157723SIlya Skriblovsky 	0x09, 0x01,         /*      Usage (Pointer),                */
23*95157723SIlya Skriblovsky 	0xA1, 0x00,         /*      Collection (Physical),          */
24*95157723SIlya Skriblovsky 	0x05, 0x09,         /*          Usage Page (Button),        */
25*95157723SIlya Skriblovsky 	0x19, 0x01,         /*          Usage Minimum (01h),        */
26*95157723SIlya Skriblovsky 	0x29, 0x05, /* X */ /*          Usage Maximum (05h),        */
27*95157723SIlya Skriblovsky 	0x15, 0x00,         /*          Logical Minimum (0),        */
28*95157723SIlya Skriblovsky 	0x25, 0x01,         /*          Logical Maximum (1),        */
29*95157723SIlya Skriblovsky 	0x75, 0x01,         /*          Report Size (1),            */
30*95157723SIlya Skriblovsky 	0x95, 0x05,         /*          Report Count (5),           */
31*95157723SIlya Skriblovsky 	0x81, 0x02,         /*          Input (Variable),           */
32*95157723SIlya Skriblovsky 	0x75, 0x03,         /*          Report Size (3),            */
33*95157723SIlya Skriblovsky 	0x95, 0x01,         /*          Report Count (1),           */
34*95157723SIlya Skriblovsky 	0x81, 0x01,         /*          Input (Constant),           */
35*95157723SIlya Skriblovsky 	0x05, 0x01,         /*          Usage Page (Desktop),       */
36*95157723SIlya Skriblovsky 	0x09, 0x30,         /*          Usage (X),                  */
37*95157723SIlya Skriblovsky 	0x09, 0x31,         /*          Usage (Y),                  */
38*95157723SIlya Skriblovsky 	0x15, 0x81,         /*          Logical Minimum (-127),     */
39*95157723SIlya Skriblovsky 	0x25, 0x7F,         /*          Logical Maximum (127),      */
40*95157723SIlya Skriblovsky 	0x75, 0x08,         /*          Report Size (8),            */
41*95157723SIlya Skriblovsky 	0x95, 0x02,         /*          Report Count (2),           */
42*95157723SIlya Skriblovsky 	0x81, 0x06,         /*          Input (Variable, Relative), */
43*95157723SIlya Skriblovsky 	0x09, 0x38,         /*          Usage (Wheel),              */
44*95157723SIlya Skriblovsky 	0x15, 0x81,         /*          Logical Minimum (-127),     */
45*95157723SIlya Skriblovsky 	0x25, 0x7F,         /*          Logical Maximum (127),      */
46*95157723SIlya Skriblovsky 	0x75, 0x08,         /*          Report Size (8),            */
47*95157723SIlya Skriblovsky 	0x95, 0x01,         /*          Report Count (1),           */
48*95157723SIlya Skriblovsky 	0x81, 0x06,         /*          Input (Variable, Relative), */
49*95157723SIlya Skriblovsky 	0xC0,               /*      End Collection,                 */
50*95157723SIlya Skriblovsky 	0xC0,               /*  End Collection,                     */
51*95157723SIlya Skriblovsky 	0x06, 0x01, 0xFF,   /*  Usage Page (FF01h),                 */
52*95157723SIlya Skriblovsky 	0x09, 0x01,         /*  Usage (01h),                        */
53*95157723SIlya Skriblovsky 	0xA1, 0x01,         /*  Collection (Application),           */
54*95157723SIlya Skriblovsky 	0x85, 0x05,         /*      Report ID (5),                  */
55*95157723SIlya Skriblovsky 	0x09, 0x05,         /*      Usage (05h),                    */
56*95157723SIlya Skriblovsky 	0x15, 0x00,         /*      Logical Minimum (0),            */
57*95157723SIlya Skriblovsky 	0x26, 0xFF, 0x00,   /*      Logical Maximum (255),          */
58*95157723SIlya Skriblovsky 	0x75, 0x08,         /*      Report Size (8),                */
59*95157723SIlya Skriblovsky 	0x95, 0x04,         /*      Report Count (4),               */
60*95157723SIlya Skriblovsky 	0xB1, 0x02,         /*      Feature (Variable),             */
61*95157723SIlya Skriblovsky 	0xC0                /*  End Collection                      */
62*95157723SIlya Skriblovsky };
63*95157723SIlya Skriblovsky 
xiaomi_report_fixup(struct hid_device * hdev,__u8 * rdesc,unsigned int * rsize)64*95157723SIlya Skriblovsky static __u8 *xiaomi_report_fixup(struct hid_device *hdev, __u8 *rdesc,
65*95157723SIlya Skriblovsky 				 unsigned int *rsize)
66*95157723SIlya Skriblovsky {
67*95157723SIlya Skriblovsky 	switch (hdev->product) {
68*95157723SIlya Skriblovsky 	case USB_DEVICE_ID_MI_SILENT_MOUSE:
69*95157723SIlya Skriblovsky 		if (*rsize == MI_SILENT_MOUSE_ORIG_RDESC_LENGTH) {
70*95157723SIlya Skriblovsky 			hid_info(hdev, "fixing up Mi Silent Mouse report descriptor\n");
71*95157723SIlya Skriblovsky 			rdesc = mi_silent_mouse_rdesc_fixed;
72*95157723SIlya Skriblovsky 			*rsize = sizeof(mi_silent_mouse_rdesc_fixed);
73*95157723SIlya Skriblovsky 		}
74*95157723SIlya Skriblovsky 		break;
75*95157723SIlya Skriblovsky 	}
76*95157723SIlya Skriblovsky 	return rdesc;
77*95157723SIlya Skriblovsky }
78*95157723SIlya Skriblovsky 
79*95157723SIlya Skriblovsky static const struct hid_device_id xiaomi_devices[] = {
80*95157723SIlya Skriblovsky 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_XIAOMI, USB_DEVICE_ID_MI_SILENT_MOUSE) },
81*95157723SIlya Skriblovsky 	{ }
82*95157723SIlya Skriblovsky };
83*95157723SIlya Skriblovsky MODULE_DEVICE_TABLE(hid, xiaomi_devices);
84*95157723SIlya Skriblovsky 
85*95157723SIlya Skriblovsky static struct hid_driver xiaomi_driver = {
86*95157723SIlya Skriblovsky 	.name = "xiaomi",
87*95157723SIlya Skriblovsky 	.id_table = xiaomi_devices,
88*95157723SIlya Skriblovsky 	.report_fixup = xiaomi_report_fixup,
89*95157723SIlya Skriblovsky };
90*95157723SIlya Skriblovsky module_hid_driver(xiaomi_driver);
91*95157723SIlya Skriblovsky 
92*95157723SIlya Skriblovsky MODULE_LICENSE("GPL");
93*95157723SIlya Skriblovsky MODULE_AUTHOR("Ilya Skriblovsky <IlyaSkriblovsky@gmail.com>");
94*95157723SIlya Skriblovsky MODULE_DESCRIPTION("Fixing side buttons of Xiaomi Mi Silent Mouse");
95