1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2020 Vladimir Kondratyev <wulf@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * HID spec: https://www.usb.org/sites/default/files/documents/hid1_11.pdf 33 */ 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.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/hid.h> 46 #include <dev/hid/hidbus.h> 47 #include <dev/hid/hidmap.h> 48 #include <dev/hid/hidquirk.h> 49 #include <dev/hid/hidrdesc.h> 50 51 static const uint8_t hms_boot_desc[] = { HID_MOUSE_BOOTPROTO_DESCR() }; 52 53 enum { 54 HMS_REL_X, 55 HMS_REL_Y, 56 HMS_REL_Z, 57 HMS_ABS_X, 58 HMS_ABS_Y, 59 HMS_ABS_Z, 60 HMS_HWHEEL, 61 HMS_BTN, 62 HMS_BTN_MS1, 63 HMS_BTN_MS2, 64 HMS_FINAL_CB, 65 }; 66 67 static hidmap_cb_t hms_final_cb; 68 69 #define HMS_MAP_BUT_RG(usage_from, usage_to, code) \ 70 { HIDMAP_KEY_RANGE(HUP_BUTTON, usage_from, usage_to, code) } 71 #define HMS_MAP_BUT_MS(usage, code) \ 72 { HIDMAP_KEY(HUP_MICROSOFT, usage, code) } 73 #define HMS_MAP_ABS(usage, code) \ 74 { HIDMAP_ABS(HUP_GENERIC_DESKTOP, usage, code) } 75 #define HMS_MAP_REL(usage, code) \ 76 { HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code) } 77 #define HMS_MAP_REL_REV(usage, code) \ 78 { HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code), .invert_value = true } 79 #define HMS_MAP_REL_CN(usage, code) \ 80 { HIDMAP_REL(HUP_CONSUMER, usage, code) } 81 #define HMS_FINAL_CB(cb) \ 82 { HIDMAP_FINAL_CB(&cb) } 83 84 static const struct hidmap_item hms_map[] = { 85 [HMS_REL_X] = HMS_MAP_REL(HUG_X, REL_X), 86 [HMS_REL_Y] = HMS_MAP_REL(HUG_Y, REL_Y), 87 [HMS_REL_Z] = HMS_MAP_REL(HUG_Z, REL_Z), 88 [HMS_ABS_X] = HMS_MAP_ABS(HUG_X, ABS_X), 89 [HMS_ABS_Y] = HMS_MAP_ABS(HUG_Y, ABS_Y), 90 [HMS_ABS_Z] = HMS_MAP_ABS(HUG_Z, ABS_Z), 91 [HMS_HWHEEL] = HMS_MAP_REL_CN(HUC_AC_PAN, REL_HWHEEL), 92 [HMS_BTN] = HMS_MAP_BUT_RG(1, 16, BTN_MOUSE), 93 [HMS_BTN_MS1] = HMS_MAP_BUT_MS(1, BTN_RIGHT), 94 [HMS_BTN_MS2] = HMS_MAP_BUT_MS(2, BTN_MIDDLE), 95 [HMS_FINAL_CB] = HMS_FINAL_CB(hms_final_cb), 96 }; 97 98 static const struct hidmap_item hms_map_wheel[] = { 99 HMS_MAP_REL(HUG_WHEEL, REL_WHEEL), 100 }; 101 static const struct hidmap_item hms_map_wheel_rev[] = { 102 HMS_MAP_REL_REV(HUG_WHEEL, REL_WHEEL), 103 }; 104 105 /* A match on these entries will load hms */ 106 static const struct hid_device_id hms_devs[] = { 107 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_MOUSE) }, 108 }; 109 110 struct hms_softc { 111 struct hidmap hm; 112 HIDMAP_CAPS(caps, hms_map); 113 }; 114 115 static int 116 hms_final_cb(HIDMAP_CB_ARGS) 117 { 118 struct hms_softc *sc = HIDMAP_CB_GET_SOFTC(); 119 struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV(); 120 121 if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) { 122 if (hidmap_test_cap(sc->caps, HMS_ABS_X) || 123 hidmap_test_cap(sc->caps, HMS_ABS_Y)) 124 evdev_support_prop(evdev, INPUT_PROP_DIRECT); 125 else 126 evdev_support_prop(evdev, INPUT_PROP_POINTER); 127 } 128 129 /* Do not execute callback at interrupt handler and detach */ 130 return (ENOSYS); 131 } 132 133 static void 134 hms_identify(driver_t *driver, device_t parent) 135 { 136 const struct hid_device_info *hw = hid_get_device_info(parent); 137 void *d_ptr; 138 hid_size_t d_len; 139 int error; 140 141 /* 142 * If device claimed boot protocol support but do not have report 143 * descriptor, load one defined in "Appendix B.2" of HID1_11.pdf 144 */ 145 error = hid_get_report_descr(parent, &d_ptr, &d_len); 146 if ((error != 0 && hid_test_quirk(hw, HQ_HAS_MS_BOOTPROTO)) || 147 (error == 0 && hid_test_quirk(hw, HQ_MS_BOOTPROTO) && 148 hid_is_mouse(d_ptr, d_len))) 149 (void)hid_set_report_descr(parent, hms_boot_desc, 150 sizeof(hms_boot_desc)); 151 } 152 153 static int 154 hms_probe(device_t dev) 155 { 156 struct hms_softc *sc = device_get_softc(dev); 157 int error; 158 159 error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hms_devs); 160 if (error != 0) 161 return (error); 162 163 hidmap_set_dev(&sc->hm, dev); 164 165 /* Check if report descriptor belongs to mouse */ 166 error = HIDMAP_ADD_MAP(&sc->hm, hms_map, sc->caps); 167 if (error != 0) 168 return (error); 169 170 /* There should be at least one X or Y axis */ 171 if (!hidmap_test_cap(sc->caps, HMS_REL_X) && 172 !hidmap_test_cap(sc->caps, HMS_REL_X) && 173 !hidmap_test_cap(sc->caps, HMS_ABS_X) && 174 !hidmap_test_cap(sc->caps, HMS_ABS_Y)) 175 return (ENXIO); 176 177 if (hidmap_test_cap(sc->caps, HMS_ABS_X) || 178 hidmap_test_cap(sc->caps, HMS_ABS_Y)) 179 hidbus_set_desc(dev, "Tablet"); 180 else 181 hidbus_set_desc(dev, "Mouse"); 182 183 return (BUS_PROBE_DEFAULT); 184 } 185 186 static int 187 hms_attach(device_t dev) 188 { 189 struct hms_softc *sc = device_get_softc(dev); 190 const struct hid_device_info *hw = hid_get_device_info(dev); 191 struct hidmap_hid_item *hi; 192 HIDMAP_CAPS(cap_wheel, hms_map_wheel); 193 void *d_ptr; 194 hid_size_t d_len; 195 bool set_report_proto; 196 int error, nbuttons = 0; 197 198 /* 199 * Set the report (non-boot) protocol if report descriptor has not been 200 * overloaded with boot protocol report descriptor. 201 * 202 * Mice without boot protocol support may choose not to implement 203 * Set_Protocol at all; Ignore any error. 204 */ 205 error = hid_get_report_descr(dev, &d_ptr, &d_len); 206 set_report_proto = !(error == 0 && d_len == sizeof(hms_boot_desc) && 207 memcmp(d_ptr, hms_boot_desc, sizeof(hms_boot_desc)) == 0); 208 (void)hid_set_protocol(dev, set_report_proto ? 1 : 0); 209 210 if (hid_test_quirk(hw, HQ_MS_REVZ)) 211 HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel_rev, cap_wheel); 212 else 213 HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel, cap_wheel); 214 215 error = hidmap_attach(&sc->hm); 216 if (error) 217 return (error); 218 219 /* Count number of input usages of variable type mapped to buttons */ 220 for (hi = sc->hm.hid_items; 221 hi < sc->hm.hid_items + sc->hm.nhid_items; 222 hi++) 223 if (hi->type == HIDMAP_TYPE_VARIABLE && hi->evtype == EV_KEY) 224 nbuttons++; 225 226 /* announce information about the mouse */ 227 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n", 228 nbuttons, 229 (hidmap_test_cap(sc->caps, HMS_REL_X) || 230 hidmap_test_cap(sc->caps, HMS_ABS_X)) ? "X" : "", 231 (hidmap_test_cap(sc->caps, HMS_REL_Y) || 232 hidmap_test_cap(sc->caps, HMS_ABS_Y)) ? "Y" : "", 233 (hidmap_test_cap(sc->caps, HMS_REL_Z) || 234 hidmap_test_cap(sc->caps, HMS_ABS_Z)) ? "Z" : "", 235 hidmap_test_cap(cap_wheel, 0) ? "W" : "", 236 hidmap_test_cap(sc->caps, HMS_HWHEEL) ? "H" : "", 237 sc->hm.hid_items[0].id); 238 239 return (0); 240 } 241 242 static int 243 hms_detach(device_t dev) 244 { 245 struct hms_softc *sc = device_get_softc(dev); 246 247 return (hidmap_detach(&sc->hm)); 248 } 249 250 static devclass_t hms_devclass; 251 static device_method_t hms_methods[] = { 252 DEVMETHOD(device_identify, hms_identify), 253 DEVMETHOD(device_probe, hms_probe), 254 DEVMETHOD(device_attach, hms_attach), 255 DEVMETHOD(device_detach, hms_detach), 256 257 DEVMETHOD_END 258 }; 259 260 DEFINE_CLASS_0(hms, hms_driver, hms_methods, sizeof(struct hms_softc)); 261 DRIVER_MODULE(hms, hidbus, hms_driver, hms_devclass, NULL, 0); 262 MODULE_DEPEND(hms, hid, 1, 1, 1); 263 MODULE_DEPEND(hms, hidbus, 1, 1, 1); 264 MODULE_DEPEND(hms, hidmap, 1, 1, 1); 265 MODULE_DEPEND(hms, evdev, 1, 1, 1); 266 MODULE_VERSION(hms, 1); 267 HID_PNP_INFO(hms_devs); 268