162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * HID driver for Xiaomi Mi Dual Mode Wireless Mouse Silent Edition
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2021 Ilya Skriblovsky
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/init.h>
962306a36Sopenharmony_ci#include <linux/module.h>
1062306a36Sopenharmony_ci#include <linux/hid.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include "hid-ids.h"
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/* Fixed Mi Silent Mouse report descriptor */
1562306a36Sopenharmony_ci/* Button's Usage Maximum changed from 3 to 5 to make side buttons work */
1662306a36Sopenharmony_ci#define MI_SILENT_MOUSE_ORIG_RDESC_LENGTH   87
1762306a36Sopenharmony_cistatic __u8 mi_silent_mouse_rdesc_fixed[] = {
1862306a36Sopenharmony_ci	0x05, 0x01,         /*  Usage Page (Desktop),               */
1962306a36Sopenharmony_ci	0x09, 0x02,         /*  Usage (Mouse),                      */
2062306a36Sopenharmony_ci	0xA1, 0x01,         /*  Collection (Application),           */
2162306a36Sopenharmony_ci	0x85, 0x03,         /*      Report ID (3),                  */
2262306a36Sopenharmony_ci	0x09, 0x01,         /*      Usage (Pointer),                */
2362306a36Sopenharmony_ci	0xA1, 0x00,         /*      Collection (Physical),          */
2462306a36Sopenharmony_ci	0x05, 0x09,         /*          Usage Page (Button),        */
2562306a36Sopenharmony_ci	0x19, 0x01,         /*          Usage Minimum (01h),        */
2662306a36Sopenharmony_ci	0x29, 0x05, /* X */ /*          Usage Maximum (05h),        */
2762306a36Sopenharmony_ci	0x15, 0x00,         /*          Logical Minimum (0),        */
2862306a36Sopenharmony_ci	0x25, 0x01,         /*          Logical Maximum (1),        */
2962306a36Sopenharmony_ci	0x75, 0x01,         /*          Report Size (1),            */
3062306a36Sopenharmony_ci	0x95, 0x05,         /*          Report Count (5),           */
3162306a36Sopenharmony_ci	0x81, 0x02,         /*          Input (Variable),           */
3262306a36Sopenharmony_ci	0x75, 0x03,         /*          Report Size (3),            */
3362306a36Sopenharmony_ci	0x95, 0x01,         /*          Report Count (1),           */
3462306a36Sopenharmony_ci	0x81, 0x01,         /*          Input (Constant),           */
3562306a36Sopenharmony_ci	0x05, 0x01,         /*          Usage Page (Desktop),       */
3662306a36Sopenharmony_ci	0x09, 0x30,         /*          Usage (X),                  */
3762306a36Sopenharmony_ci	0x09, 0x31,         /*          Usage (Y),                  */
3862306a36Sopenharmony_ci	0x15, 0x81,         /*          Logical Minimum (-127),     */
3962306a36Sopenharmony_ci	0x25, 0x7F,         /*          Logical Maximum (127),      */
4062306a36Sopenharmony_ci	0x75, 0x08,         /*          Report Size (8),            */
4162306a36Sopenharmony_ci	0x95, 0x02,         /*          Report Count (2),           */
4262306a36Sopenharmony_ci	0x81, 0x06,         /*          Input (Variable, Relative), */
4362306a36Sopenharmony_ci	0x09, 0x38,         /*          Usage (Wheel),              */
4462306a36Sopenharmony_ci	0x15, 0x81,         /*          Logical Minimum (-127),     */
4562306a36Sopenharmony_ci	0x25, 0x7F,         /*          Logical Maximum (127),      */
4662306a36Sopenharmony_ci	0x75, 0x08,         /*          Report Size (8),            */
4762306a36Sopenharmony_ci	0x95, 0x01,         /*          Report Count (1),           */
4862306a36Sopenharmony_ci	0x81, 0x06,         /*          Input (Variable, Relative), */
4962306a36Sopenharmony_ci	0xC0,               /*      End Collection,                 */
5062306a36Sopenharmony_ci	0xC0,               /*  End Collection,                     */
5162306a36Sopenharmony_ci	0x06, 0x01, 0xFF,   /*  Usage Page (FF01h),                 */
5262306a36Sopenharmony_ci	0x09, 0x01,         /*  Usage (01h),                        */
5362306a36Sopenharmony_ci	0xA1, 0x01,         /*  Collection (Application),           */
5462306a36Sopenharmony_ci	0x85, 0x05,         /*      Report ID (5),                  */
5562306a36Sopenharmony_ci	0x09, 0x05,         /*      Usage (05h),                    */
5662306a36Sopenharmony_ci	0x15, 0x00,         /*      Logical Minimum (0),            */
5762306a36Sopenharmony_ci	0x26, 0xFF, 0x00,   /*      Logical Maximum (255),          */
5862306a36Sopenharmony_ci	0x75, 0x08,         /*      Report Size (8),                */
5962306a36Sopenharmony_ci	0x95, 0x04,         /*      Report Count (4),               */
6062306a36Sopenharmony_ci	0xB1, 0x02,         /*      Feature (Variable),             */
6162306a36Sopenharmony_ci	0xC0                /*  End Collection                      */
6262306a36Sopenharmony_ci};
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistatic __u8 *xiaomi_report_fixup(struct hid_device *hdev, __u8 *rdesc,
6562306a36Sopenharmony_ci				 unsigned int *rsize)
6662306a36Sopenharmony_ci{
6762306a36Sopenharmony_ci	switch (hdev->product) {
6862306a36Sopenharmony_ci	case USB_DEVICE_ID_MI_SILENT_MOUSE:
6962306a36Sopenharmony_ci		if (*rsize == MI_SILENT_MOUSE_ORIG_RDESC_LENGTH) {
7062306a36Sopenharmony_ci			hid_info(hdev, "fixing up Mi Silent Mouse report descriptor\n");
7162306a36Sopenharmony_ci			rdesc = mi_silent_mouse_rdesc_fixed;
7262306a36Sopenharmony_ci			*rsize = sizeof(mi_silent_mouse_rdesc_fixed);
7362306a36Sopenharmony_ci		}
7462306a36Sopenharmony_ci		break;
7562306a36Sopenharmony_ci	}
7662306a36Sopenharmony_ci	return rdesc;
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistatic const struct hid_device_id xiaomi_devices[] = {
8062306a36Sopenharmony_ci	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_XIAOMI, USB_DEVICE_ID_MI_SILENT_MOUSE) },
8162306a36Sopenharmony_ci	{ }
8262306a36Sopenharmony_ci};
8362306a36Sopenharmony_ciMODULE_DEVICE_TABLE(hid, xiaomi_devices);
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistatic struct hid_driver xiaomi_driver = {
8662306a36Sopenharmony_ci	.name = "xiaomi",
8762306a36Sopenharmony_ci	.id_table = xiaomi_devices,
8862306a36Sopenharmony_ci	.report_fixup = xiaomi_report_fixup,
8962306a36Sopenharmony_ci};
9062306a36Sopenharmony_cimodule_hid_driver(xiaomi_driver);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciMODULE_LICENSE("GPL");
9362306a36Sopenharmony_ciMODULE_AUTHOR("Ilya Skriblovsky <IlyaSkriblovsky@gmail.com>");
9462306a36Sopenharmony_ciMODULE_DESCRIPTION("Fixing side buttons of Xiaomi Mi Silent Mouse");
95