1 /* $NetBSD: ums.c,v 1.100 2021/02/03 23:26:08 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2017 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.100 2021/02/03 23:26:08 thorpej Exp $"); 39 40 #ifdef _KERNEL_OPT 41 #include "opt_usb.h" 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/device.h> 48 #include <sys/ioctl.h> 49 #include <sys/file.h> 50 #include <sys/select.h> 51 #include <sys/proc.h> 52 #include <sys/vnode.h> 53 #include <sys/poll.h> 54 55 #include <dev/usb/usb.h> 56 #include <dev/usb/usbhid.h> 57 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usbdi_util.h> 60 #include <dev/usb/usbdevs.h> 61 #include <dev/usb/usb_quirks.h> 62 #include <dev/usb/uhidev.h> 63 #include <dev/hid/hid.h> 64 #include <dev/hid/hidms.h> 65 66 #ifdef UMS_DEBUG 67 #define DPRINTF(x) if (umsdebug) printf x 68 #define DPRINTFN(n,x) if (umsdebug>(n)) printf x 69 int umsdebug = 0; 70 #else 71 #define DPRINTF(x) 72 #define DPRINTFN(n,x) 73 #endif 74 75 #define UMSUNIT(s) (minor(s)) 76 77 struct ums_softc { 78 struct uhidev sc_hdev; 79 struct hidms sc_ms; 80 81 bool sc_alwayson; 82 83 int sc_enabled; 84 char sc_dying; 85 }; 86 87 Static void ums_intr(struct uhidev *, void *, u_int); 88 89 Static int ums_enable(void *); 90 Static void ums_disable(void *); 91 Static int ums_ioctl(void *, u_long, void *, int, struct lwp *); 92 93 static const struct wsmouse_accessops ums_accessops = { 94 ums_enable, 95 ums_ioctl, 96 ums_disable, 97 }; 98 99 static int ums_match(device_t, cfdata_t, void *); 100 static void ums_attach(device_t, device_t, void *); 101 static void ums_childdet(device_t, device_t); 102 static int ums_detach(device_t, int); 103 static int ums_activate(device_t, enum devact); 104 105 CFATTACH_DECL2_NEW(ums, sizeof(struct ums_softc), ums_match, ums_attach, 106 ums_detach, ums_activate, NULL, ums_childdet); 107 108 static int 109 ums_match(device_t parent, cfdata_t match, void *aux) 110 { 111 struct uhidev_attach_arg *uha = aux; 112 int size; 113 void *desc; 114 115 /* 116 * Some (older) Griffin PowerMate knobs may masquerade as a 117 * mouse, avoid treating them as such, they have only one axis. 118 */ 119 if (uha->uiaa->uiaa_vendor == USB_VENDOR_GRIFFIN && 120 uha->uiaa->uiaa_product == USB_PRODUCT_GRIFFIN_POWERMATE) 121 return UMATCH_NONE; 122 123 uhidev_get_report_desc(uha->parent, &desc, &size); 124 if (!hid_is_collection(desc, size, uha->reportid, 125 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)) && 126 !hid_is_collection(desc, size, uha->reportid, 127 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_POINTER)) && 128 !hid_is_collection(desc, size, uha->reportid, 129 HID_USAGE2(HUP_DIGITIZERS, 0x0002))) 130 return UMATCH_NONE; 131 132 return UMATCH_IFACECLASS; 133 } 134 135 static void 136 ums_attach(device_t parent, device_t self, void *aux) 137 { 138 struct ums_softc *sc = device_private(self); 139 struct uhidev_attach_arg *uha = aux; 140 struct hid_data *d; 141 struct hid_item item; 142 int size, error; 143 void *desc; 144 uint32_t quirks; 145 146 aprint_naive("\n"); 147 148 sc->sc_hdev.sc_dev = self; 149 sc->sc_hdev.sc_intr = ums_intr; 150 sc->sc_hdev.sc_parent = uha->parent; 151 sc->sc_hdev.sc_report_id = uha->reportid; 152 153 quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags; 154 if (quirks & UQ_MS_REVZ) 155 sc->sc_ms.flags |= HIDMS_REVZ; 156 if (quirks & UQ_SPUR_BUT_UP) 157 sc->sc_ms.flags |= HIDMS_SPUR_BUT_UP; 158 159 if (!pmf_device_register(self, NULL, NULL)) 160 aprint_error_dev(self, "couldn't establish power handler\n"); 161 162 uhidev_get_report_desc(uha->parent, &desc, &size); 163 164 if (!hidms_setup(self, &sc->sc_ms, uha->reportid, desc, size)) 165 return; 166 167 if (uha->uiaa->uiaa_vendor == USB_VENDOR_MICROSOFT) { 168 int fixpos; 169 int woffset = 8; 170 /* 171 * The Microsoft Wireless Laser Mouse 6000 v2.0 and the 172 * Microsoft Comfort Mouse 2.0 report a bad position for 173 * the wheel and wheel tilt controls -- should be in bytes 174 * 3 & 4 of the report. Fix this if necessary. 175 */ 176 switch (uha->uiaa->uiaa_product) { 177 case USB_PRODUCT_MICROSOFT_24GHZ_XCVR10: 178 case USB_PRODUCT_MICROSOFT_24GHZ_XCVR20: 179 case USB_PRODUCT_MICROSOFT_NATURAL_6000: 180 fixpos = 24; 181 break; 182 case USB_PRODUCT_MICROSOFT_24GHZ_XCVR80: 183 fixpos = 40; 184 woffset = sc->sc_ms.hidms_loc_z.size; 185 break; 186 case USB_PRODUCT_MICROSOFT_CM6000: 187 fixpos = 40; 188 break; 189 default: 190 fixpos = 0; 191 break; 192 } 193 if (fixpos) { 194 if ((sc->sc_ms.flags & HIDMS_Z) && 195 sc->sc_ms.hidms_loc_z.pos == 0) 196 sc->sc_ms.hidms_loc_z.pos = fixpos; 197 if ((sc->sc_ms.flags & HIDMS_W) && 198 sc->sc_ms.hidms_loc_w.pos == 0) 199 sc->sc_ms.hidms_loc_w.pos = 200 sc->sc_ms.hidms_loc_z.pos + woffset; 201 } 202 } 203 204 if (uha->uiaa->uiaa_vendor == USB_VENDOR_HAILUCK && 205 uha->uiaa->uiaa_product == USB_PRODUCT_HAILUCK_KEYBOARD) { 206 /* 207 * The HAILUCK USB Keyboard has a built-in touchpad, which 208 * needs to be active for the keyboard to function properly. 209 */ 210 sc->sc_alwayson = true; 211 } 212 213 if (uha->uiaa->uiaa_vendor == USB_VENDOR_CHICONY && 214 uha->uiaa->uiaa_product == USB_PRODUCT_CHICONY_OPTMOUSE0939) { 215 /* 216 * This cheap mouse will disconnect after 60 seconds, 217 * reconnect, and then disconnect again (ad nauseum) 218 * unless it's kept open. 219 */ 220 sc->sc_alwayson = true; 221 } 222 223 tpcalib_init(&sc->sc_ms.sc_tpcalib); 224 225 /* calibrate the pointer if it reports absolute events */ 226 if (sc->sc_ms.flags & HIDMS_ABS) { 227 memset(&sc->sc_ms.sc_calibcoords, 0, sizeof(sc->sc_ms.sc_calibcoords)); 228 sc->sc_ms.sc_calibcoords.maxx = 0; 229 sc->sc_ms.sc_calibcoords.maxy = 0; 230 sc->sc_ms.sc_calibcoords.samplelen = WSMOUSE_CALIBCOORDS_RESET; 231 d = hid_start_parse(desc, size, hid_input); 232 if (d != NULL) { 233 while (hid_get_item(d, &item)) { 234 if (item.kind != hid_input 235 || HID_GET_USAGE_PAGE(item.usage) != HUP_GENERIC_DESKTOP 236 || item.report_ID != sc->sc_hdev.sc_report_id) 237 continue; 238 if (HID_GET_USAGE(item.usage) == HUG_X) { 239 sc->sc_ms.sc_calibcoords.minx = item.logical_minimum; 240 sc->sc_ms.sc_calibcoords.maxx = item.logical_maximum; 241 } 242 if (HID_GET_USAGE(item.usage) == HUG_Y) { 243 sc->sc_ms.sc_calibcoords.miny = item.logical_minimum; 244 sc->sc_ms.sc_calibcoords.maxy = item.logical_maximum; 245 } 246 } 247 hid_end_parse(d); 248 } 249 tpcalib_ioctl(&sc->sc_ms.sc_tpcalib, WSMOUSEIO_SCALIBCOORDS, 250 (void *)&sc->sc_ms.sc_calibcoords, 0, 0); 251 } 252 253 hidms_attach(self, &sc->sc_ms, &ums_accessops); 254 255 if (sc->sc_alwayson) { 256 error = uhidev_open(&sc->sc_hdev); 257 if (error != 0) { 258 aprint_error_dev(self, 259 "WARNING: couldn't open always-on device\n"); 260 sc->sc_alwayson = false; 261 } 262 } 263 } 264 265 static int 266 ums_activate(device_t self, enum devact act) 267 { 268 struct ums_softc *sc = device_private(self); 269 270 switch (act) { 271 case DVACT_DEACTIVATE: 272 sc->sc_dying = 1; 273 return 0; 274 default: 275 return EOPNOTSUPP; 276 } 277 } 278 279 static void 280 ums_childdet(device_t self, device_t child) 281 { 282 struct ums_softc *sc = device_private(self); 283 284 KASSERT(sc->sc_ms.hidms_wsmousedev == child); 285 sc->sc_ms.hidms_wsmousedev = NULL; 286 } 287 288 static int 289 ums_detach(device_t self, int flags) 290 { 291 struct ums_softc *sc = device_private(self); 292 int rv = 0; 293 294 DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags)); 295 296 if (sc->sc_alwayson) 297 uhidev_close(&sc->sc_hdev); 298 299 /* No need to do reference counting of ums, wsmouse has all the goo. */ 300 if (sc->sc_ms.hidms_wsmousedev != NULL) 301 rv = config_detach(sc->sc_ms.hidms_wsmousedev, flags); 302 303 pmf_device_deregister(self); 304 305 return rv; 306 } 307 308 Static void 309 ums_intr(struct uhidev *addr, void *ibuf, u_int len) 310 { 311 struct ums_softc *sc = (struct ums_softc *)addr; 312 313 if (sc->sc_enabled) 314 hidms_intr(&sc->sc_ms, ibuf, len); 315 } 316 317 Static int 318 ums_enable(void *v) 319 { 320 struct ums_softc *sc = v; 321 int error = 0; 322 323 DPRINTFN(1,("ums_enable: sc=%p\n", sc)); 324 325 if (sc->sc_dying) 326 return EIO; 327 328 if (sc->sc_enabled) 329 return EBUSY; 330 331 sc->sc_enabled = 1; 332 sc->sc_ms.hidms_buttons = 0; 333 334 if (!sc->sc_alwayson) { 335 error = uhidev_open(&sc->sc_hdev); 336 if (error) 337 sc->sc_enabled = 0; 338 } 339 340 return error; 341 } 342 343 Static void 344 ums_disable(void *v) 345 { 346 struct ums_softc *sc = v; 347 348 DPRINTFN(1,("ums_disable: sc=%p\n", sc)); 349 #ifdef DIAGNOSTIC 350 if (!sc->sc_enabled) { 351 printf("ums_disable: not enabled\n"); 352 return; 353 } 354 #endif 355 356 if (sc->sc_enabled) { 357 sc->sc_enabled = 0; 358 if (!sc->sc_alwayson) 359 uhidev_close(&sc->sc_hdev); 360 } 361 } 362 363 Static int 364 ums_ioctl(void *v, u_long cmd, void *data, int flag, 365 struct lwp *l) 366 367 { 368 struct ums_softc *sc = v; 369 int error; 370 371 if (sc->sc_ms.flags & HIDMS_ABS) { 372 error = tpcalib_ioctl(&sc->sc_ms.sc_tpcalib, cmd, data, 373 flag, l); 374 if (error != EPASSTHROUGH) 375 return error; 376 } 377 378 switch (cmd) { 379 case WSMOUSEIO_GTYPE: 380 if (sc->sc_ms.flags & HIDMS_ABS) 381 *(u_int *)data = WSMOUSE_TYPE_TPANEL; 382 else 383 *(u_int *)data = WSMOUSE_TYPE_USB; 384 return 0; 385 } 386 387 return EPASSTHROUGH; 388 } 389