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