1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * HID driver for VRC-2 2-axis Car 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/*
13 * VID/PID are probably "borrowed", so keep them locally and
14 * do not populate hid-ids.h with those.
15 */
16#define USB_VENDOR_ID_VRC2	(0x07c0)
17#define USB_DEVICE_ID_VRC2	(0x1125)
18
19static __u8 vrc2_rdesc_fixed[] = {
20	0x05, 0x01,        // Usage Page (Generic Desktop Ctrls)
21	0x09, 0x04,        // Usage (Joystick)
22	0xA1, 0x01,        // Collection (Application)
23	0x09, 0x01,        //   Usage (Pointer)
24	0xA1, 0x00,        //   Collection (Physical)
25	0x09, 0x30,        //     Usage (X)
26	0x09, 0x31,        //     Usage (Y)
27	0x15, 0x00,        //     Logical Minimum (0)
28	0x26, 0xFF, 0x07,  //     Logical Maximum (2047)
29	0x35, 0x00,        //     Physical Minimum (0)
30	0x46, 0xFF, 0x00,  //     Physical Maximum (255)
31	0x75, 0x10,        //     Report Size (16)
32	0x95, 0x02,        //     Report Count (2)
33	0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
34	0xC0,              //   End Collection
35	0x75, 0x08,        //   Report Size (8)
36	0x95, 0x03,        //   Report Count (3)
37	0x81, 0x03,        //   Input (Cnst,Var,Abs)
38	0xC0,              // End Collection
39};
40
41static __u8 *vrc2_report_fixup(struct hid_device *hdev, __u8 *rdesc,
42				unsigned int *rsize)
43{
44	hid_info(hdev, "fixing up VRC-2 report descriptor\n");
45	*rsize = sizeof(vrc2_rdesc_fixed);
46	return vrc2_rdesc_fixed;
47}
48
49static int vrc2_probe(struct hid_device *hdev, const struct hid_device_id *id)
50{
51	int ret;
52
53	/*
54	 * The device gives us 2 separate USB endpoints.
55	 * One of those (the one with report descriptor size of 23) is just bogus so ignore it
56	 */
57	if (hdev->dev_rsize == 23)
58		return -ENODEV;
59
60	ret = hid_parse(hdev);
61	if (ret) {
62		hid_err(hdev, "parse failed\n");
63		return ret;
64	}
65
66	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
67	if (ret) {
68		hid_err(hdev, "hw start failed\n");
69		return ret;
70	}
71
72	return 0;
73}
74
75static const struct hid_device_id vrc2_devices[] = {
76	{ HID_USB_DEVICE(USB_VENDOR_ID_VRC2, USB_DEVICE_ID_VRC2) },
77	{ /* sentinel */ }
78};
79MODULE_DEVICE_TABLE(hid, vrc2_devices);
80
81static struct hid_driver vrc2_driver = {
82	.name = "vrc2",
83	.id_table = vrc2_devices,
84	.report_fixup = vrc2_report_fixup,
85	.probe = vrc2_probe,
86};
87module_hid_driver(vrc2_driver);
88
89MODULE_AUTHOR("Marcus Folkesson <marcus.folkesson@gmail.com>");
90MODULE_DESCRIPTION("HID driver for VRC-2 2-axis Car controller");
91MODULE_LICENSE("GPL");
92