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 "opt_hid.h" 36 37 #include <sys/param.h> 38 #include <sys/bus.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/sysctl.h> 43 44 #include <dev/evdev/input.h> 45 #include <dev/evdev/evdev.h> 46 47 #include <dev/hid/hid.h> 48 #include <dev/hid/hidbus.h> 49 #include <dev/hid/hidmap.h> 50 #include <dev/hid/hidquirk.h> 51 #include <dev/hid/hidrdesc.h> 52 53 static const uint8_t hms_boot_desc[] = { HID_MOUSE_BOOTPROTO_DESCR() }; 54 55 enum { 56 HMS_REL_X, 57 HMS_REL_Y, 58 HMS_REL_Z, 59 HMS_ABS_X, 60 HMS_ABS_Y, 61 HMS_ABS_Z, 62 HMS_HWHEEL, 63 HMS_BTN, 64 HMS_BTN_MS1, 65 HMS_BTN_MS2, 66 HMS_FINAL_CB, 67 }; 68 69 static hidmap_cb_t hms_final_cb; 70 #ifdef IICHID_SAMPLING 71 static hid_intr_t hms_intr; 72 #endif 73 74 #define HMS_MAP_BUT_RG(usage_from, usage_to, code) \ 75 { HIDMAP_KEY_RANGE(HUP_BUTTON, usage_from, usage_to, code) } 76 #define HMS_MAP_BUT_MS(usage, code) \ 77 { HIDMAP_KEY(HUP_MICROSOFT, usage, code) } 78 #define HMS_MAP_ABS(usage, code) \ 79 { HIDMAP_ABS(HUP_GENERIC_DESKTOP, usage, code) } 80 #define HMS_MAP_REL(usage, code) \ 81 { HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code) } 82 #define HMS_MAP_REL_REV(usage, code) \ 83 { HIDMAP_REL(HUP_GENERIC_DESKTOP, usage, code), .invert_value = true } 84 #define HMS_MAP_REL_CN(usage, code) \ 85 { HIDMAP_REL(HUP_CONSUMER, usage, code) } 86 #define HMS_FINAL_CB(cb) \ 87 { HIDMAP_FINAL_CB(&cb) } 88 89 static const struct hidmap_item hms_map[] = { 90 [HMS_REL_X] = HMS_MAP_REL(HUG_X, REL_X), 91 [HMS_REL_Y] = HMS_MAP_REL(HUG_Y, REL_Y), 92 [HMS_REL_Z] = HMS_MAP_REL(HUG_Z, REL_Z), 93 [HMS_ABS_X] = HMS_MAP_ABS(HUG_X, ABS_X), 94 [HMS_ABS_Y] = HMS_MAP_ABS(HUG_Y, ABS_Y), 95 [HMS_ABS_Z] = HMS_MAP_ABS(HUG_Z, ABS_Z), 96 [HMS_HWHEEL] = HMS_MAP_REL_CN(HUC_AC_PAN, REL_HWHEEL), 97 [HMS_BTN] = HMS_MAP_BUT_RG(1, 16, BTN_MOUSE), 98 [HMS_BTN_MS1] = HMS_MAP_BUT_MS(1, BTN_RIGHT), 99 [HMS_BTN_MS2] = HMS_MAP_BUT_MS(2, BTN_MIDDLE), 100 [HMS_FINAL_CB] = HMS_FINAL_CB(hms_final_cb), 101 }; 102 103 static const struct hidmap_item hms_map_wheel[] = { 104 HMS_MAP_REL(HUG_WHEEL, REL_WHEEL), 105 }; 106 static const struct hidmap_item hms_map_wheel_rev[] = { 107 HMS_MAP_REL_REV(HUG_WHEEL, REL_WHEEL), 108 }; 109 110 /* A match on these entries will load hms */ 111 static const struct hid_device_id hms_devs[] = { 112 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_POINTER) }, 113 { HID_TLC(HUP_GENERIC_DESKTOP, HUG_MOUSE) }, 114 }; 115 116 struct hms_softc { 117 struct hidmap hm; 118 HIDMAP_CAPS(caps, hms_map); 119 #ifdef IICHID_SAMPLING 120 bool iichid_sampling; 121 void *last_ir; 122 hid_size_t last_irsize; 123 hid_size_t isize; 124 uint32_t drift_cnt; 125 uint32_t drift_thresh; 126 #endif 127 }; 128 129 #ifdef IICHID_SAMPLING 130 static void 131 hms_intr(void *context, void *buf, hid_size_t len) 132 { 133 struct hidmap *hm = context; 134 struct hms_softc *sc = device_get_softc(hm->dev); 135 136 if (len > sc->isize) 137 len = sc->isize; 138 139 /* 140 * Many I2C "compatibility" mouse devices found on touchpads continue 141 * to return last report data in sampling mode even after touch has 142 * been ended. That results in cursor drift. Filter out such a 143 * reports through comparing with previous one. 144 */ 145 if (len == sc->last_irsize && memcmp(buf, sc->last_ir, len) == 0) { 146 sc->drift_cnt++; 147 if (sc->drift_thresh != 0 && sc->drift_cnt >= sc->drift_thresh) 148 return; 149 } else { 150 sc->drift_cnt = 0; 151 sc->last_irsize = len; 152 bcopy(buf, sc->last_ir, len); 153 } 154 155 hidmap_intr(context, buf, len); 156 } 157 #endif 158 159 static int 160 hms_final_cb(HIDMAP_CB_ARGS) 161 { 162 struct hms_softc *sc = HIDMAP_CB_GET_SOFTC(); 163 struct evdev_dev *evdev = HIDMAP_CB_GET_EVDEV(); 164 165 if (HIDMAP_CB_GET_STATE() == HIDMAP_CB_IS_ATTACHING) { 166 if (hidmap_test_cap(sc->caps, HMS_ABS_X) || 167 hidmap_test_cap(sc->caps, HMS_ABS_Y)) 168 evdev_support_prop(evdev, INPUT_PROP_DIRECT); 169 else 170 evdev_support_prop(evdev, INPUT_PROP_POINTER); 171 #ifdef IICHID_SAMPLING 172 /* Overload interrupt handler to skip identical reports */ 173 if (sc->iichid_sampling) 174 hidbus_set_intr(sc->hm.dev, hms_intr, &sc->hm); 175 #endif 176 } 177 178 /* Do not execute callback at interrupt handler and detach */ 179 return (ENOSYS); 180 } 181 182 static void 183 hms_identify(driver_t *driver, device_t parent) 184 { 185 const struct hid_device_info *hw = hid_get_device_info(parent); 186 void *d_ptr; 187 hid_size_t d_len; 188 int error; 189 190 /* 191 * If device claimed boot protocol support but do not have report 192 * descriptor, load one defined in "Appendix B.2" of HID1_11.pdf 193 */ 194 error = hid_get_report_descr(parent, &d_ptr, &d_len); 195 if ((error != 0 && hid_test_quirk(hw, HQ_HAS_MS_BOOTPROTO)) || 196 (error == 0 && hid_test_quirk(hw, HQ_MS_BOOTPROTO) && 197 hid_is_mouse(d_ptr, d_len))) 198 (void)hid_set_report_descr(parent, hms_boot_desc, 199 sizeof(hms_boot_desc)); 200 } 201 202 static int 203 hms_probe(device_t dev) 204 { 205 struct hms_softc *sc = device_get_softc(dev); 206 int error; 207 208 error = HIDBUS_LOOKUP_DRIVER_INFO(dev, hms_devs); 209 if (error != 0) 210 return (error); 211 212 hidmap_set_dev(&sc->hm, dev); 213 214 /* Check if report descriptor belongs to mouse */ 215 error = HIDMAP_ADD_MAP(&sc->hm, hms_map, sc->caps); 216 if (error != 0) 217 return (error); 218 219 /* There should be at least one X or Y axis */ 220 if (!hidmap_test_cap(sc->caps, HMS_REL_X) && 221 !hidmap_test_cap(sc->caps, HMS_REL_X) && 222 !hidmap_test_cap(sc->caps, HMS_ABS_X) && 223 !hidmap_test_cap(sc->caps, HMS_ABS_Y)) 224 return (ENXIO); 225 226 if (hidmap_test_cap(sc->caps, HMS_ABS_X) || 227 hidmap_test_cap(sc->caps, HMS_ABS_Y)) 228 hidbus_set_desc(dev, "Tablet"); 229 else 230 hidbus_set_desc(dev, "Mouse"); 231 232 return (BUS_PROBE_GENERIC); 233 } 234 235 static int 236 hms_attach(device_t dev) 237 { 238 struct hms_softc *sc = device_get_softc(dev); 239 const struct hid_device_info *hw = hid_get_device_info(dev); 240 struct hidmap_hid_item *hi; 241 HIDMAP_CAPS(cap_wheel, hms_map_wheel); 242 void *d_ptr; 243 hid_size_t d_len; 244 bool set_report_proto; 245 int error, nbuttons = 0; 246 247 /* 248 * Set the report (non-boot) protocol if report descriptor has not been 249 * overloaded with boot protocol report descriptor. 250 * 251 * Mice without boot protocol support may choose not to implement 252 * Set_Protocol at all; Ignore any error. 253 */ 254 error = hid_get_report_descr(dev, &d_ptr, &d_len); 255 set_report_proto = !(error == 0 && d_len == sizeof(hms_boot_desc) && 256 memcmp(d_ptr, hms_boot_desc, sizeof(hms_boot_desc)) == 0); 257 (void)hid_set_protocol(dev, set_report_proto ? 1 : 0); 258 259 if (hid_test_quirk(hw, HQ_MS_REVZ)) 260 HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel_rev, cap_wheel); 261 else 262 HIDMAP_ADD_MAP(&sc->hm, hms_map_wheel, cap_wheel); 263 264 #ifdef IICHID_SAMPLING 265 if (hid_test_quirk(hw, HQ_IICHID_SAMPLING) && 266 hidmap_test_cap(sc->caps, HMS_REL_X) && 267 hidmap_test_cap(sc->caps, HMS_REL_Y)) { 268 sc->iichid_sampling = true; 269 sc->isize = hid_report_size_max(d_ptr, d_len, hid_input, NULL); 270 sc->last_ir = malloc(sc->isize, M_DEVBUF, M_WAITOK | M_ZERO); 271 sc->drift_thresh = 2; 272 SYSCTL_ADD_U32(device_get_sysctl_ctx(dev), 273 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 274 "drift_thresh", CTLFLAG_RW, &sc->drift_thresh, 0, 275 "drift detection threshold"); 276 } 277 #endif 278 279 error = hidmap_attach(&sc->hm); 280 if (error) 281 return (error); 282 283 /* Count number of input usages of variable type mapped to buttons */ 284 for (hi = sc->hm.hid_items; 285 hi < sc->hm.hid_items + sc->hm.nhid_items; 286 hi++) 287 if (hi->type == HIDMAP_TYPE_VARIABLE && hi->evtype == EV_KEY) 288 nbuttons++; 289 290 /* announce information about the mouse */ 291 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n", 292 nbuttons, 293 (hidmap_test_cap(sc->caps, HMS_REL_X) || 294 hidmap_test_cap(sc->caps, HMS_ABS_X)) ? "X" : "", 295 (hidmap_test_cap(sc->caps, HMS_REL_Y) || 296 hidmap_test_cap(sc->caps, HMS_ABS_Y)) ? "Y" : "", 297 (hidmap_test_cap(sc->caps, HMS_REL_Z) || 298 hidmap_test_cap(sc->caps, HMS_ABS_Z)) ? "Z" : "", 299 hidmap_test_cap(cap_wheel, 0) ? "W" : "", 300 hidmap_test_cap(sc->caps, HMS_HWHEEL) ? "H" : "", 301 sc->hm.hid_items[0].id); 302 303 return (0); 304 } 305 306 static int 307 hms_detach(device_t dev) 308 { 309 struct hms_softc *sc = device_get_softc(dev); 310 int error; 311 312 error = hidmap_detach(&sc->hm); 313 #ifdef IICHID_SAMPLING 314 if (error == 0) 315 free(sc->last_ir, M_DEVBUF); 316 #endif 317 return (error); 318 } 319 320 static device_method_t hms_methods[] = { 321 DEVMETHOD(device_identify, hms_identify), 322 DEVMETHOD(device_probe, hms_probe), 323 DEVMETHOD(device_attach, hms_attach), 324 DEVMETHOD(device_detach, hms_detach), 325 326 DEVMETHOD_END 327 }; 328 329 DEFINE_CLASS_0(hms, hms_driver, hms_methods, sizeof(struct hms_softc)); 330 DRIVER_MODULE(hms, hidbus, hms_driver, NULL, NULL); 331 MODULE_DEPEND(hms, hid, 1, 1, 1); 332 MODULE_DEPEND(hms, hidbus, 1, 1, 1); 333 MODULE_DEPEND(hms, hidmap, 1, 1, 1); 334 MODULE_DEPEND(hms, evdev, 1, 1, 1); 335 MODULE_VERSION(hms, 1); 336 HID_PNP_INFO(hms_devs); 337