xref: /linux/drivers/hid/hid-rapoo.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <asm-generic/errno-base.h>
3 #include <asm-generic/int-ll64.h>
4 #include <linux/bitops.h>
5 #include <linux/input.h>
6 #include <linux/input-event-codes.h>
7 #include <linux/module.h>
8 #include <linux/hid.h>
9 #include <linux/usb.h>
10 
11 #include "hid-ids.h"
12 
13 #define RAPOO_BTN_BACK			0x08
14 #define RAPOO_BTN_FORWARD		0x10
15 
16 static const struct hid_device_id rapoo_devices[] = {
17 	{ HID_USB_DEVICE(USB_VENDOR_ID_RAPOO, USB_DEVICE_ID_RAPOO_2_4G_RECEIVER) },
18 	{ }
19 };
20 MODULE_DEVICE_TABLE(hid, rapoo_devices);
21 
22 static int rapoo_probe(struct hid_device *hdev, const struct hid_device_id *id)
23 {
24 	int ret;
25 	struct input_dev *input;
26 
27 	ret = hid_parse(hdev);
28 	if (ret) {
29 		hid_err(hdev, "parse failed\n");
30 		return ret;
31 	}
32 
33 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
34 	if (ret) {
35 		hid_err(hdev, "start failed\n");
36 		return ret;
37 	}
38 
39 	if (hdev->bus == BUS_USB) {
40 		struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
41 
42 		if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
43 			return 0;
44 	}
45 
46 	input = devm_input_allocate_device(&hdev->dev);
47 	if (!input)
48 		return -ENOMEM;
49 
50 	input->name = "Rapoo 2.4G Wireless Mouse";
51 	input->phys = "rapoo/input1";
52 	input->id.bustype = hdev->bus;
53 	input->id.vendor = hdev->vendor;
54 	input->id.product = hdev->product;
55 	input->id.version = hdev->version;
56 
57 	__set_bit(EV_KEY, input->evbit);
58 	__set_bit(KEY_BACK, input->keybit);
59 	__set_bit(KEY_FORWARD, input->keybit);
60 
61 	ret = input_register_device(input);
62 	if (ret)
63 		return ret;
64 
65 	hid_set_drvdata(hdev, input);
66 
67 	return ret;
68 }
69 
70 static int rapoo_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
71 {
72 	struct input_dev *input = hid_get_drvdata(hdev);
73 
74 	if (!input)
75 		return 0;
76 
77 	if (report->id == 1 && size >= 2) {
78 		u8 btn = data[1];
79 
80 		input_report_key(input, KEY_BACK, btn & RAPOO_BTN_BACK);
81 		input_report_key(input, KEY_FORWARD, btn & RAPOO_BTN_FORWARD);
82 		input_sync(input);
83 		return 1;
84 	}
85 
86 	return 0;
87 }
88 
89 static struct hid_driver rapoo_driver = {
90 	.name = "hid-rapoo",
91 	.id_table = rapoo_devices,
92 	.probe = rapoo_probe,
93 	.raw_event = rapoo_raw_event,
94 };
95 
96 module_hid_driver(rapoo_driver);
97 
98 MODULE_LICENSE("GPL");
99 MODULE_AUTHOR("Nguyen Dinh Dang Duong <dangduong31205@gmail.com>");
100 MODULE_DESCRIPTION("RAPOO 2.4G Wireless Device Driver");
101 
102