xref: /freebsd/sys/dev/hid/xb360gp.c (revision e0c4386e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
5  * Copyright (c) 2020 Val Packett <val@packett.cool>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 /*
31  * XBox 360 gamepad driver thanks to the custom descriptor in usbhid.
32  *
33  * Tested on: SVEN GC-5070 in both XInput (XBox 360) and DirectInput modes
34  */
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/sysctl.h>
41 
42 #include <dev/evdev/input.h>
43 #include <dev/evdev/evdev.h>
44 
45 #include <dev/hid/hgame.h>
46 #include <dev/hid/hid.h>
47 #include <dev/hid/hidbus.h>
48 #include <dev/hid/hidmap.h>
49 #include <dev/hid/hidquirk.h>
50 #include <dev/hid/hidrdesc.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 
55 static const uint8_t	xb360gp_rdesc[] = {HID_XB360GP_REPORT_DESCR()};
56 
57 #define XB360GP_MAP_BUT(number, code)	\
58 	{ HIDMAP_KEY(HUP_BUTTON, number, code) }
59 #define XB360GP_MAP_ABS(usage, code)	\
60 	{ HIDMAP_ABS(HUP_GENERIC_DESKTOP, HUG_##usage, code) }
61 #define XB360GP_MAP_ABS_FLT(usage, code)	\
62 	{ HIDMAP_ABS(HUP_GENERIC_DESKTOP, HUG_##usage, code),	\
63 	    .fuzz = 16, .flat = 128 }
64 #define XB360GP_MAP_ABS_INV(usage, code)	\
65 	{ HIDMAP_ABS(HUP_GENERIC_DESKTOP, HUG_##usage, code),	\
66 	    .fuzz = 16, .flat = 128, .invert_value = true }
67 #define XB360GP_MAP_CRG(usage_from, usage_to, callback)	\
68 	{ HIDMAP_ANY_CB_RANGE(HUP_GENERIC_DESKTOP,	\
69 	    HUG_##usage_from, HUG_##usage_to, callback) }
70 #define XB360GP_FINALCB(cb)		\
71 	{ HIDMAP_FINAL_CB(&cb) }
72 
73 /* Customized to match usbhid's XBox 360 descriptor */
74 static const struct hidmap_item xb360gp_map[] = {
75 	XB360GP_MAP_BUT(1,		BTN_SOUTH),
76 	XB360GP_MAP_BUT(2,		BTN_EAST),
77 	XB360GP_MAP_BUT(3,		BTN_WEST),
78 	XB360GP_MAP_BUT(4,		BTN_NORTH),
79 	XB360GP_MAP_BUT(5,		BTN_TL),
80 	XB360GP_MAP_BUT(6,		BTN_TR),
81 	XB360GP_MAP_BUT(7,		BTN_SELECT),
82 	XB360GP_MAP_BUT(8,		BTN_START),
83 	XB360GP_MAP_BUT(9,		BTN_THUMBL),
84 	XB360GP_MAP_BUT(10,		BTN_THUMBR),
85 	XB360GP_MAP_BUT(11,		BTN_MODE),
86 	XB360GP_MAP_CRG(D_PAD_UP, D_PAD_LEFT, hgame_dpad_cb),
87 	XB360GP_MAP_ABS_FLT(X,		ABS_X),
88 	XB360GP_MAP_ABS_INV(Y,		ABS_Y),
89 	XB360GP_MAP_ABS(Z,		ABS_Z),
90 	XB360GP_MAP_ABS_FLT(RX,		ABS_RX),
91 	XB360GP_MAP_ABS_INV(RY,		ABS_RY),
92 	XB360GP_MAP_ABS(RZ,		ABS_RZ),
93 	XB360GP_FINALCB(		hgame_final_cb),
94 };
95 
96 static const STRUCT_USB_HOST_ID xb360gp_devs[] = {
97 	/* the Xbox 360 gamepad doesn't use the HID class */
98 	{USB_IFACE_CLASS(UICLASS_VENDOR),
99 	 USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER),
100 	 USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),},
101 };
102 
103 static void
104 xb360gp_identify(driver_t *driver, device_t parent)
105 {
106 	const struct hid_device_info *hw = hid_get_device_info(parent);
107 
108 	/* the Xbox 360 gamepad has no report descriptor */
109 	if (hid_test_quirk(hw, HQ_IS_XBOX360GP))
110 		hid_set_report_descr(parent, xb360gp_rdesc,
111 		    sizeof(xb360gp_rdesc));
112 }
113 
114 static int
115 xb360gp_probe(device_t dev)
116 {
117 	struct hgame_softc *sc = device_get_softc(dev);
118 	const struct hid_device_info *hw = hid_get_device_info(dev);
119 	int error;
120 
121 	if (!hid_test_quirk(hw, HQ_IS_XBOX360GP))
122 		return (ENXIO);
123 
124 	hidmap_set_dev(&sc->hm, dev);
125 
126 	error = HIDMAP_ADD_MAP(&sc->hm, xb360gp_map, NULL);
127 	if (error != 0)
128 		return (error);
129 
130 	device_set_desc(dev, "XBox 360 Gamepad");
131 
132 	return (BUS_PROBE_DEFAULT);
133 }
134 
135 static int
136 xb360gp_attach(device_t dev)
137 {
138 	struct hgame_softc *sc = device_get_softc(dev);
139 	int error;
140 
141 	/*
142 	 * Turn off the four LEDs on the gamepad which
143 	 * are blinking by default:
144 	 */
145 	static const uint8_t reportbuf[3] = {1, 3, 0};
146 	error = hid_set_report(dev, reportbuf, sizeof(reportbuf),
147 	    HID_OUTPUT_REPORT, 0);
148 	if (error)
149 		device_printf(dev, "set output report failed, error=%d "
150 		    "(ignored)\n", error);
151 
152 	return (hidmap_attach(&sc->hm));
153 }
154 
155 static int
156 xb360gp_detach(device_t dev)
157 {
158 	struct hgame_softc *sc = device_get_softc(dev);
159 
160 	return (hidmap_detach(&sc->hm));
161 }
162 
163 static device_method_t xb360gp_methods[] = {
164 	DEVMETHOD(device_identify,	xb360gp_identify),
165 	DEVMETHOD(device_probe,		xb360gp_probe),
166 	DEVMETHOD(device_attach,	xb360gp_attach),
167 	DEVMETHOD(device_detach,	xb360gp_detach),
168 	DEVMETHOD_END
169 };
170 
171 DEFINE_CLASS_0(xb360gp, xb360gp_driver, xb360gp_methods,
172     sizeof(struct hgame_softc));
173 DRIVER_MODULE(xb360gp, hidbus, xb360gp_driver, NULL, NULL);
174 MODULE_DEPEND(xb360gp, hid, 1, 1, 1);
175 MODULE_DEPEND(xb360gp, hidbus, 1, 1, 1);
176 MODULE_DEPEND(xb360gp, hidmap, 1, 1, 1);
177 MODULE_DEPEND(xb360gp, hgame, 1, 1, 1);
178 MODULE_DEPEND(xb360gp, evdev, 1, 1, 1);
179 MODULE_VERSION(xb360gp, 1);
180 USB_PNP_HOST_INFO(xb360gp_devs);
181