xref: /openbmc/linux/drivers/hid/hid-pxrc.c (revision 7cffcade)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HID driver for PhoenixRC 8-axis flight controller
4  *
5  * Copyright (C) 2022 Marcus Folkesson <marcus.folkesson@gmail.com>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/hid.h>
10 #include <linux/module.h>
11 
12 #include "hid-ids.h"
13 
14 struct pxrc_priv {
15 	u8 slider;
16 	u8 dial;
17 	bool alternate;
18 };
19 
20 static __u8 pxrc_rdesc_fixed[] = {
21 	0x05, 0x01,        // Usage Page (Generic Desktop Ctrls)
22 	0x09, 0x04,        // Usage (Joystick)
23 	0xA1, 0x01,        // Collection (Application)
24 	0x09, 0x01,        //   Usage (Pointer)
25 	0xA1, 0x00,        //   Collection (Physical)
26 	0x09, 0x30,        //     Usage (X)
27 	0x09, 0x36,        //     Usage (Slider)
28 	0x09, 0x31,        //     Usage (Y)
29 	0x09, 0x32,        //     Usage (Z)
30 	0x09, 0x33,        //     Usage (Rx)
31 	0x09, 0x34,        //     Usage (Ry)
32 	0x09, 0x35,        //     Usage (Rz)
33 	0x09, 0x37,        //     Usage (Dial)
34 	0x15, 0x00,        //     Logical Minimum (0)
35 	0x26, 0xFF, 0x00,  //     Logical Maximum (255)
36 	0x35, 0x00,        //     Physical Minimum (0)
37 	0x46, 0xFF, 0x00,  //     Physical Maximum (255)
38 	0x75, 0x08,        //     Report Size (8)
39 	0x95, 0x08,        //     Report Count (8)
40 	0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
41 	0xC0,              //   End Collection
42 	0xC0,              // End Collection
43 };
44 
45 static __u8 *pxrc_report_fixup(struct hid_device *hdev, __u8 *rdesc,
46 				unsigned int *rsize)
47 {
48 	hid_info(hdev, "fixing up PXRC report descriptor\n");
49 	*rsize = sizeof(pxrc_rdesc_fixed);
50 	return pxrc_rdesc_fixed;
51 }
52 
53 static int pxrc_raw_event(struct hid_device *hdev, struct hid_report *report,
54 	 u8 *data, int size)
55 {
56 	struct pxrc_priv *priv = hid_get_drvdata(hdev);
57 
58 	if (priv->alternate)
59 		priv->slider = data[7];
60 	else
61 		priv->dial = data[7];
62 
63 	data[1] = priv->slider;
64 	data[7] = priv->dial;
65 
66 	priv->alternate = !priv->alternate;
67 	return 0;
68 }
69 
70 static int pxrc_probe(struct hid_device *hdev, const struct hid_device_id *id)
71 {
72 	int ret;
73 	struct pxrc_priv *priv;
74 
75 	priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
76 	if (!priv)
77 		return -ENOMEM;
78 	hid_set_drvdata(hdev, priv);
79 
80 	ret = hid_parse(hdev);
81 	if (ret) {
82 		hid_err(hdev, "parse failed\n");
83 		return ret;
84 	}
85 
86 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
87 	if (ret) {
88 		hid_err(hdev, "hw start failed\n");
89 		return ret;
90 	}
91 
92 	return 0;
93 }
94 
95 static const struct hid_device_id pxrc_devices[] = {
96 	{ HID_USB_DEVICE(USB_VENDOR_ID_MULTIPLE_1781, USB_DEVICE_ID_PHOENIXRC) },
97 	{ /* sentinel */ }
98 };
99 MODULE_DEVICE_TABLE(hid, pxrc_devices);
100 
101 static struct hid_driver pxrc_driver = {
102 	.name = "hid-pxrc",
103 	.id_table = pxrc_devices,
104 	.report_fixup = pxrc_report_fixup,
105 	.probe = pxrc_probe,
106 	.raw_event = pxrc_raw_event,
107 };
108 module_hid_driver(pxrc_driver);
109 
110 MODULE_AUTHOR("Marcus Folkesson <marcus.folkesson@gmail.com>");
111 MODULE_DESCRIPTION("HID driver for PXRC 8-axis flight controller");
112 MODULE_LICENSE("GPL");
113