1 /* $OpenBSD: ubcmtp.c,v 1.23 2021/11/22 22:12:37 jcs Exp $ */ 2 3 /* 4 * Copyright (c) 2013-2014, joshua stein <jcs@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * Apple USB multitouch trackpad (Broadcom BCM5974) driver 21 * 22 * Protocol info/magic from bcm5974 Linux driver by Henrik Rydberg, et al. 23 */ 24 25 #include <sys/param.h> 26 #include <sys/device.h> 27 #include <sys/errno.h> 28 #include <sys/malloc.h> 29 30 #include <sys/ioctl.h> 31 #include <sys/systm.h> 32 #include <sys/tty.h> 33 34 #include <dev/usb/usb.h> 35 #include <dev/usb/usbdi.h> 36 #include <dev/usb/usbdevs.h> 37 #include <dev/usb/usbhid.h> 38 39 #include <dev/wscons/wsconsio.h> 40 #include <dev/wscons/wsmousevar.h> 41 42 /* #define UBCMTP_DEBUG */ 43 44 #ifdef UBCMTP_DEBUG 45 #define DPRINTF(x...) do { printf(x); } while (0); 46 #else 47 #define DPRINTF(x...) 48 #endif 49 50 /* magic to switch device from HID (default) mode into raw */ 51 #define UBCMTP_WELLSPRING_MODE_RAW 0x01 52 #define UBCMTP_WELLSPRING_MODE_HID 0x08 53 #define UBCMTP_WELLSPRING_MODE_LEN 8 54 #define UBCMTP_WELLSPRING9_MODE_RAW 0x01 55 #define UBCMTP_WELLSPRING9_MODE_HID 0x00 56 #define UBCMTP_WELLSPRING9_MODE_LEN 2 57 58 struct ubcmtp_button { 59 uint8_t unused; 60 uint8_t button; 61 uint8_t rel_x; 62 uint8_t rel_y; 63 }; 64 65 struct ubcmtp_finger { 66 uint16_t origin; 67 uint16_t abs_x; 68 uint16_t abs_y; 69 uint16_t rel_x; 70 uint16_t rel_y; 71 uint16_t tool_major; 72 uint16_t tool_minor; 73 uint16_t orientation; 74 uint16_t touch_major; 75 uint16_t touch_minor; 76 uint16_t unused[2]; 77 uint16_t pressure; 78 uint16_t multi; 79 } __packed __attribute((aligned(2))); 80 81 #define UBCMTP_MAX_FINGERS 16 82 #define UBCMTP_ALL_FINGER_SIZE (UBCMTP_MAX_FINGERS * sizeof(struct ubcmtp_finger)) 83 84 #define UBCMTP_TYPE1 1 85 #define UBCMTP_TYPE1_TPOFF (13 * sizeof(uint16_t)) 86 #define UBCMTP_TYPE1_TPLEN UBCMTP_TYPE1_TPOFF + UBCMTP_ALL_FINGER_SIZE 87 #define UBCMTP_TYPE1_TPIFACE 1 88 #define UBCMTP_TYPE1_BTIFACE 2 89 90 #define UBCMTP_TYPE2 2 91 #define UBCMTP_TYPE2_TPOFF (15 * sizeof(uint16_t)) 92 #define UBCMTP_TYPE2_TPLEN UBCMTP_TYPE2_TPOFF + UBCMTP_ALL_FINGER_SIZE 93 #define UBCMTP_TYPE2_TPIFACE 1 94 #define UBCMTP_TYPE2_BTOFF 15 95 96 #define UBCMTP_TYPE3 3 97 #define UBCMTP_TYPE3_TPOFF (19 * sizeof(uint16_t)) 98 #define UBCMTP_TYPE3_TPLEN UBCMTP_TYPE3_TPOFF + UBCMTP_ALL_FINGER_SIZE 99 #define UBCMTP_TYPE3_TPIFACE 2 100 #define UBCMTP_TYPE3_BTOFF 23 101 102 #define UBCMTP_TYPE4 4 103 #define UBCMTP_TYPE4_TPOFF (24 * sizeof(uint16_t)) 104 #define UBCMTP_TYPE4_TPLEN UBCMTP_TYPE4_TPOFF + UBCMTP_ALL_FINGER_SIZE 105 #define UBCMTP_TYPE4_TPIFACE 2 106 #define UBCMTP_TYPE4_BTOFF 31 107 #define UBCMTP_TYPE4_FINGERPAD (1 * sizeof(uint16_t)) 108 109 #define UBCMTP_FINGER_ORIENT 16384 110 #define UBCMTP_SN_PRESSURE 45 111 #define UBCMTP_SN_WIDTH 25 112 #define UBCMTP_SN_COORD 250 113 #define UBCMTP_SN_ORIENT 10 114 115 /* Identify clickpads in ubcmtp_configure. */ 116 #define IS_CLICKPAD(ubcmtp_type) (ubcmtp_type != UBCMTP_TYPE1) 117 118 /* Use a constant, synaptics-compatible pressure value for now. */ 119 #define DEFAULT_PRESSURE 40 120 121 struct ubcmtp_limit { 122 int limit; 123 int min; 124 int max; 125 }; 126 127 struct ubcmtp_dev { 128 int vendor; /* vendor */ 129 int ansi, iso, jis; /* 3 types of product */ 130 int type; /* 1 (normal) or 2 (integrated btn) */ 131 struct ubcmtp_limit l_pressure; /* finger pressure */ 132 struct ubcmtp_limit l_width; /* finger width */ 133 struct ubcmtp_limit l_x; 134 struct ubcmtp_limit l_y; 135 struct ubcmtp_limit l_orientation; 136 }; 137 138 static struct ubcmtp_dev ubcmtp_devices[] = { 139 /* type 1 devices with separate buttons */ 140 { 141 USB_VENDOR_APPLE, 142 /* MacbookAir */ 143 USB_PRODUCT_APPLE_WELLSPRING_ANSI, 144 USB_PRODUCT_APPLE_WELLSPRING_ISO, 145 USB_PRODUCT_APPLE_WELLSPRING_JIS, 146 UBCMTP_TYPE1, 147 { UBCMTP_SN_PRESSURE, 0, 256 }, 148 { UBCMTP_SN_WIDTH, 0, 2048 }, 149 { UBCMTP_SN_COORD, -4824, 5342 }, 150 { UBCMTP_SN_COORD, -172, 5820 }, 151 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 152 }, 153 { 154 USB_VENDOR_APPLE, 155 /* MacbookProPenryn */ 156 USB_PRODUCT_APPLE_WELLSPRING2_ANSI, 157 USB_PRODUCT_APPLE_WELLSPRING2_ISO, 158 USB_PRODUCT_APPLE_WELLSPRING2_JIS, 159 UBCMTP_TYPE1, 160 { UBCMTP_SN_PRESSURE, 0, 256 }, 161 { UBCMTP_SN_WIDTH, 0, 2048 }, 162 { UBCMTP_SN_COORD, -4824, 4824 }, 163 { UBCMTP_SN_COORD, -172, 4290 }, 164 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 165 }, 166 /* type 2 devices with integrated buttons */ 167 { 168 USB_VENDOR_APPLE, 169 /* Macbook5,1 */ 170 USB_PRODUCT_APPLE_WELLSPRING3_ANSI, 171 USB_PRODUCT_APPLE_WELLSPRING3_ISO, 172 USB_PRODUCT_APPLE_WELLSPRING3_JIS, 173 UBCMTP_TYPE2, 174 { UBCMTP_SN_PRESSURE, 0, 300 }, 175 { UBCMTP_SN_WIDTH, 0, 2048 }, 176 { UBCMTP_SN_COORD, -4460, 5166 }, 177 { UBCMTP_SN_COORD, -75, 6700 }, 178 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 179 }, 180 { 181 USB_VENDOR_APPLE, 182 /* MacbookAir3,1 */ 183 USB_PRODUCT_APPLE_WELLSPRING4A_ANSI, 184 USB_PRODUCT_APPLE_WELLSPRING4A_ISO, 185 USB_PRODUCT_APPLE_WELLSPRING4A_JIS, 186 UBCMTP_TYPE2, 187 { UBCMTP_SN_PRESSURE, 0, 300 }, 188 { UBCMTP_SN_WIDTH, 0, 2048 }, 189 { UBCMTP_SN_COORD, -4616, 5112 }, 190 { UBCMTP_SN_COORD, -142, 5234 }, 191 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 192 }, 193 { 194 USB_VENDOR_APPLE, 195 /* MacbookAir3,2 */ 196 USB_PRODUCT_APPLE_WELLSPRING4_ANSI, 197 USB_PRODUCT_APPLE_WELLSPRING4_ISO, 198 USB_PRODUCT_APPLE_WELLSPRING4_JIS, 199 UBCMTP_TYPE2, 200 { UBCMTP_SN_PRESSURE, 0, 300 }, 201 { UBCMTP_SN_WIDTH, 0, 2048 }, 202 { UBCMTP_SN_COORD, -4620, 5140 }, 203 { UBCMTP_SN_COORD, -150, 6600 }, 204 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 205 }, 206 { 207 USB_VENDOR_APPLE, 208 /* Macbook8 */ 209 USB_PRODUCT_APPLE_WELLSPRING5_ANSI, 210 USB_PRODUCT_APPLE_WELLSPRING5_ISO, 211 USB_PRODUCT_APPLE_WELLSPRING5_JIS, 212 UBCMTP_TYPE2, 213 { UBCMTP_SN_PRESSURE, 0, 300 }, 214 { UBCMTP_SN_WIDTH, 0, 2048 }, 215 { UBCMTP_SN_COORD, -4415, 5050 }, 216 { UBCMTP_SN_COORD, -55, 6680 }, 217 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 218 }, 219 { 220 USB_VENDOR_APPLE, 221 /* Macbook8,2 */ 222 USB_PRODUCT_APPLE_WELLSPRING5A_ANSI, 223 USB_PRODUCT_APPLE_WELLSPRING5A_ISO, 224 USB_PRODUCT_APPLE_WELLSPRING5A_JIS, 225 UBCMTP_TYPE2, 226 { UBCMTP_SN_PRESSURE, 0, 300 }, 227 { UBCMTP_SN_WIDTH, 0, 2048 }, 228 { UBCMTP_SN_COORD, -4750, 5280 }, 229 { UBCMTP_SN_COORD, -150, 6730 }, 230 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 231 }, 232 { 233 USB_VENDOR_APPLE, 234 /* MacbookAir4,2 */ 235 USB_PRODUCT_APPLE_WELLSPRING6_ANSI, 236 USB_PRODUCT_APPLE_WELLSPRING6_ISO, 237 USB_PRODUCT_APPLE_WELLSPRING6_JIS, 238 UBCMTP_TYPE2, 239 { UBCMTP_SN_PRESSURE, 0, 300 }, 240 { UBCMTP_SN_WIDTH, 0, 2048 }, 241 { UBCMTP_SN_COORD, -4620, 5140 }, 242 { UBCMTP_SN_COORD, -150, 6600 }, 243 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 244 }, 245 { 246 USB_VENDOR_APPLE, 247 /* MacbookAir4,1 */ 248 USB_PRODUCT_APPLE_WELLSPRING6A_ANSI, 249 USB_PRODUCT_APPLE_WELLSPRING6A_ISO, 250 USB_PRODUCT_APPLE_WELLSPRING6A_JIS, 251 UBCMTP_TYPE2, 252 { UBCMTP_SN_PRESSURE, 0, 300 }, 253 { UBCMTP_SN_WIDTH, 0, 2048 }, 254 { UBCMTP_SN_COORD, -4620, 5140 }, 255 { UBCMTP_SN_COORD, -150, 6600 }, 256 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 257 }, 258 { 259 USB_VENDOR_APPLE, 260 /* MacbookPro10,1 */ 261 USB_PRODUCT_APPLE_WELLSPRING7_ANSI, 262 USB_PRODUCT_APPLE_WELLSPRING7_ISO, 263 USB_PRODUCT_APPLE_WELLSPRING7_JIS, 264 UBCMTP_TYPE2, 265 { UBCMTP_SN_PRESSURE, 0, 300 }, 266 { UBCMTP_SN_WIDTH, 0, 2048 }, 267 { UBCMTP_SN_COORD, -4750, 5280 }, 268 { UBCMTP_SN_COORD, -150, 6730 }, 269 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 270 }, 271 { 272 USB_VENDOR_APPLE, 273 /* MacbookPro10,2 */ 274 USB_PRODUCT_APPLE_WELLSPRING7A_ANSI, 275 USB_PRODUCT_APPLE_WELLSPRING7A_ISO, 276 USB_PRODUCT_APPLE_WELLSPRING7A_JIS, 277 UBCMTP_TYPE2, 278 { UBCMTP_SN_PRESSURE, 0, 300 }, 279 { UBCMTP_SN_WIDTH, 0, 2048 }, 280 { UBCMTP_SN_COORD, -4750, 5280 }, 281 { UBCMTP_SN_COORD, -150, 6730 }, 282 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 283 }, 284 { 285 USB_VENDOR_APPLE, 286 /* MacbookAir6,1 */ 287 USB_PRODUCT_APPLE_WELLSPRING8_ANSI, 288 USB_PRODUCT_APPLE_WELLSPRING8_ISO, 289 USB_PRODUCT_APPLE_WELLSPRING8_JIS, 290 UBCMTP_TYPE3, 291 { UBCMTP_SN_PRESSURE, 0, 300 }, 292 { UBCMTP_SN_WIDTH, 0, 2048 }, 293 { UBCMTP_SN_COORD, -4620, 5140 }, 294 { UBCMTP_SN_COORD, -150, 6600 }, 295 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 296 }, 297 { 298 USB_VENDOR_APPLE, 299 /* MacbookPro12,1 */ 300 USB_PRODUCT_APPLE_WELLSPRING9_ANSI, 301 USB_PRODUCT_APPLE_WELLSPRING9_ISO, 302 USB_PRODUCT_APPLE_WELLSPRING9_JIS, 303 UBCMTP_TYPE4, 304 { UBCMTP_SN_PRESSURE, 0, 300 }, 305 { UBCMTP_SN_WIDTH, 0, 2048 }, 306 { UBCMTP_SN_COORD, -4828, 5345 }, 307 { UBCMTP_SN_COORD, -203, 6803 }, 308 { UBCMTP_SN_ORIENT, -UBCMTP_FINGER_ORIENT, UBCMTP_FINGER_ORIENT }, 309 }, 310 }; 311 312 struct ubcmtp_softc { 313 struct device sc_dev; /* base device */ 314 315 struct ubcmtp_dev *dev_type; 316 317 struct usbd_device *sc_udev; 318 struct device *sc_wsmousedev; 319 320 struct usbd_interface *sc_tp_iface; /* trackpad interface */ 321 struct usbd_pipe *sc_tp_pipe; /* trackpad pipe */ 322 int sc_tp_epaddr; /* endpoint addr */ 323 int tp_maxlen; /* max size of tp data */ 324 int tp_offset; /* finger offset into data */ 325 int tp_fingerpad; /* padding between finger data */ 326 uint8_t *tp_pkt; 327 328 struct usbd_interface *sc_bt_iface; /* button interface */ 329 struct usbd_pipe *sc_bt_pipe; /* button pipe */ 330 int sc_bt_epaddr; /* endpoint addr */ 331 int bt_maxlen; /* max size of button data */ 332 uint8_t *bt_pkt; 333 334 uint32_t sc_status; 335 #define UBCMTP_ENABLED 1 336 337 struct mtpoint frame[UBCMTP_MAX_FINGERS]; 338 int contacts; 339 int btn; 340 }; 341 342 int ubcmtp_enable(void *); 343 void ubcmtp_disable(void *); 344 int ubcmtp_ioctl(void *, unsigned long, caddr_t, int, struct proc *); 345 int ubcmtp_raw_mode(struct ubcmtp_softc *, int); 346 int ubcmtp_setup_pipes(struct ubcmtp_softc *); 347 void ubcmtp_bt_intr(struct usbd_xfer *, void *, usbd_status); 348 void ubcmtp_tp_intr(struct usbd_xfer *, void *, usbd_status); 349 350 int ubcmtp_match(struct device *, void *, void *); 351 void ubcmtp_attach(struct device *, struct device *, void *); 352 int ubcmtp_detach(struct device *, int); 353 int ubcmtp_activate(struct device *, int); 354 int ubcmtp_configure(struct ubcmtp_softc *); 355 356 const struct wsmouse_accessops ubcmtp_accessops = { 357 ubcmtp_enable, 358 ubcmtp_ioctl, 359 ubcmtp_disable, 360 }; 361 362 struct cfdriver ubcmtp_cd = { 363 NULL, "ubcmtp", DV_DULL 364 }; 365 366 const struct cfattach ubcmtp_ca = { 367 sizeof(struct ubcmtp_softc), ubcmtp_match, ubcmtp_attach, ubcmtp_detach, 368 ubcmtp_activate, 369 }; 370 371 int 372 ubcmtp_match(struct device *parent, void *match, void *aux) 373 { 374 struct usb_attach_arg *uaa = aux; 375 usb_interface_descriptor_t *id; 376 int i; 377 378 if (uaa->iface == NULL) 379 return (UMATCH_NONE); 380 381 for (i = 0; i < nitems(ubcmtp_devices); i++) { 382 if (uaa->vendor == ubcmtp_devices[i].vendor && ( 383 uaa->product == ubcmtp_devices[i].ansi || 384 uaa->product == ubcmtp_devices[i].iso || 385 uaa->product == ubcmtp_devices[i].jis)) { 386 if (uaa->nifaces < 2) 387 return (UMATCH_NONE); 388 if ((ubcmtp_devices[i].type != UBCMTP_TYPE2) && 389 (uaa->nifaces < 3)) 390 return (UMATCH_NONE); 391 392 /* 393 * The USB keyboard/mouse device will have one keyboard 394 * HID and two mouse HIDs, though only one will have a 395 * protocol of mouse -- we only want to take control of 396 * that one. 397 */ 398 id = usbd_get_interface_descriptor(uaa->iface); 399 if (id->bInterfaceProtocol == UIPROTO_BOOT_MOUSE) 400 return (UMATCH_VENDOR_PRODUCT_CONF_IFACE); 401 } 402 } 403 404 return (UMATCH_NONE); 405 } 406 407 void 408 ubcmtp_attach(struct device *parent, struct device *self, void *aux) 409 { 410 struct ubcmtp_softc *sc = (struct ubcmtp_softc *)self; 411 struct usb_attach_arg *uaa = aux; 412 struct usbd_device *dev = uaa->device; 413 struct wsmousedev_attach_args a; 414 usb_device_descriptor_t *udd; 415 int i; 416 417 sc->sc_udev = uaa->device; 418 sc->sc_status = 0; 419 sc->tp_fingerpad = 0; 420 421 if ((udd = usbd_get_device_descriptor(dev)) == NULL) { 422 printf("ubcmtp: failed getting device descriptor\n"); 423 return; 424 } 425 426 for (i = 0; i < nitems(ubcmtp_devices); i++) { 427 if (uaa->vendor == ubcmtp_devices[i].vendor && ( 428 uaa->product == ubcmtp_devices[i].ansi || 429 uaa->product == ubcmtp_devices[i].iso || 430 uaa->product == ubcmtp_devices[i].jis)) { 431 sc->dev_type = &ubcmtp_devices[i]; 432 DPRINTF("%s: attached to 0x%x/0x%x type %d\n", 433 sc->sc_dev.dv_xname, uaa->vendor, uaa->product, 434 sc->dev_type->type); 435 break; 436 } 437 } 438 439 if (sc->dev_type == NULL) { 440 /* how did we match then? */ 441 printf("%s: failed looking up device in table\n", 442 sc->sc_dev.dv_xname); 443 return; 444 } 445 446 switch (sc->dev_type->type) { 447 case UBCMTP_TYPE1: 448 sc->tp_maxlen = UBCMTP_TYPE1_TPLEN; 449 sc->tp_offset = UBCMTP_TYPE1_TPOFF; 450 sc->sc_tp_iface = uaa->ifaces[UBCMTP_TYPE1_TPIFACE]; 451 usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE1_TPIFACE); 452 453 /* button offsets */ 454 sc->bt_maxlen = sizeof(struct ubcmtp_button); 455 sc->sc_bt_iface = uaa->ifaces[UBCMTP_TYPE1_BTIFACE]; 456 usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE1_BTIFACE); 457 break; 458 459 case UBCMTP_TYPE2: 460 sc->tp_maxlen = UBCMTP_TYPE2_TPLEN; 461 sc->tp_offset = UBCMTP_TYPE2_TPOFF; 462 sc->sc_tp_iface = uaa->ifaces[UBCMTP_TYPE2_TPIFACE]; 463 usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE2_TPIFACE); 464 break; 465 466 case UBCMTP_TYPE3: 467 sc->tp_maxlen = UBCMTP_TYPE3_TPLEN; 468 sc->tp_offset = UBCMTP_TYPE3_TPOFF; 469 sc->sc_tp_iface = uaa->ifaces[UBCMTP_TYPE3_TPIFACE]; 470 usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE3_TPIFACE); 471 break; 472 473 case UBCMTP_TYPE4: 474 sc->tp_maxlen = UBCMTP_TYPE4_TPLEN; 475 sc->tp_offset = UBCMTP_TYPE4_TPOFF; 476 sc->sc_tp_iface = uaa->ifaces[UBCMTP_TYPE4_TPIFACE]; 477 sc->tp_fingerpad = UBCMTP_TYPE4_FINGERPAD; 478 usbd_claim_iface(sc->sc_udev, UBCMTP_TYPE4_TPIFACE); 479 break; 480 } 481 482 a.accessops = &ubcmtp_accessops; 483 a.accesscookie = sc; 484 485 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 486 if (sc->sc_wsmousedev != NULL && ubcmtp_configure(sc)) 487 ubcmtp_disable(sc); 488 } 489 490 int 491 ubcmtp_detach(struct device *self, int flags) 492 { 493 struct ubcmtp_softc *sc = (struct ubcmtp_softc *)self; 494 int ret = 0; 495 496 if (sc->sc_wsmousedev != NULL) 497 ret = config_detach(sc->sc_wsmousedev, flags); 498 499 return (ret); 500 } 501 502 int 503 ubcmtp_activate(struct device *self, int act) 504 { 505 struct ubcmtp_softc *sc = (struct ubcmtp_softc *)self; 506 int rv = 0; 507 508 if (act == DVACT_DEACTIVATE) { 509 if (sc->sc_wsmousedev != NULL) 510 rv = config_deactivate(sc->sc_wsmousedev); 511 usbd_deactivate(sc->sc_udev); 512 } 513 514 return (rv); 515 } 516 517 int 518 ubcmtp_configure(struct ubcmtp_softc *sc) 519 { 520 struct wsmousehw *hw = wsmouse_get_hw(sc->sc_wsmousedev); 521 522 hw->type = WSMOUSE_TYPE_TOUCHPAD; 523 hw->hw_type = (IS_CLICKPAD(sc->dev_type->type) 524 ? WSMOUSEHW_CLICKPAD : WSMOUSEHW_TOUCHPAD); 525 hw->x_min = sc->dev_type->l_x.min; 526 hw->x_max = sc->dev_type->l_x.max; 527 hw->y_min = sc->dev_type->l_y.min; 528 hw->y_max = sc->dev_type->l_y.max; 529 hw->mt_slots = UBCMTP_MAX_FINGERS; 530 hw->flags = WSMOUSEHW_MT_TRACKING; 531 532 return wsmouse_configure(sc->sc_wsmousedev, NULL, 0); 533 } 534 535 int 536 ubcmtp_enable(void *v) 537 { 538 struct ubcmtp_softc *sc = v; 539 540 if (sc->sc_status & UBCMTP_ENABLED) 541 return (EBUSY); 542 543 if (usbd_is_dying(sc->sc_udev)) 544 return (EIO); 545 546 if (ubcmtp_raw_mode(sc, 1) != 0) { 547 printf("%s: failed to enter raw mode\n", sc->sc_dev.dv_xname); 548 return (1); 549 } 550 551 if (ubcmtp_setup_pipes(sc) == 0) { 552 sc->sc_status |= UBCMTP_ENABLED; 553 return (0); 554 } else 555 return (1); 556 } 557 558 void 559 ubcmtp_disable(void *v) 560 { 561 struct ubcmtp_softc *sc = v; 562 563 if (usbd_is_dying(sc->sc_udev) || !(sc->sc_status & UBCMTP_ENABLED)) 564 return; 565 566 sc->sc_status &= ~UBCMTP_ENABLED; 567 568 ubcmtp_raw_mode(sc, 0); 569 570 if (sc->sc_tp_pipe != NULL) { 571 usbd_close_pipe(sc->sc_tp_pipe); 572 sc->sc_tp_pipe = NULL; 573 } 574 if (sc->sc_bt_pipe != NULL) { 575 usbd_close_pipe(sc->sc_bt_pipe); 576 sc->sc_bt_pipe = NULL; 577 } 578 579 if (sc->tp_pkt != NULL) { 580 free(sc->tp_pkt, M_USBDEV, sc->tp_maxlen); 581 sc->tp_pkt = NULL; 582 } 583 if (sc->bt_pkt != NULL) { 584 free(sc->bt_pkt, M_USBDEV, sc->bt_maxlen); 585 sc->bt_pkt = NULL; 586 } 587 } 588 589 int 590 ubcmtp_ioctl(void *v, unsigned long cmd, caddr_t data, int flag, struct proc *p) 591 { 592 struct ubcmtp_softc *sc = v; 593 struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data; 594 int wsmode; 595 596 DPRINTF("%s: in %s with cmd 0x%lx\n", sc->sc_dev.dv_xname, __func__, 597 cmd); 598 599 switch (cmd) { 600 case WSMOUSEIO_GTYPE: { 601 struct wsmousehw *hw = wsmouse_get_hw(sc->sc_wsmousedev); 602 *(u_int *)data = hw->type; 603 break; 604 } 605 606 case WSMOUSEIO_GCALIBCOORDS: 607 wsmc->minx = sc->dev_type->l_x.min; 608 wsmc->maxx = sc->dev_type->l_x.max; 609 wsmc->miny = sc->dev_type->l_y.min; 610 wsmc->maxy = sc->dev_type->l_y.max; 611 wsmc->swapxy = 0; 612 wsmc->resx = 0; 613 wsmc->resy = 0; 614 break; 615 616 case WSMOUSEIO_SETMODE: 617 wsmode = *(u_int *)data; 618 if (wsmode != WSMOUSE_COMPAT && wsmode != WSMOUSE_NATIVE) { 619 printf("%s: invalid mode %d\n", sc->sc_dev.dv_xname, 620 wsmode); 621 return (EINVAL); 622 } 623 wsmouse_set_mode(sc->sc_wsmousedev, wsmode); 624 625 DPRINTF("%s: changing mode to %s\n", 626 sc->sc_dev.dv_xname, (wsmode == WSMOUSE_COMPAT ? "compat" : 627 "native")); 628 629 break; 630 631 default: 632 return (-1); 633 } 634 635 return (0); 636 } 637 638 int 639 ubcmtp_raw_mode(struct ubcmtp_softc *sc, int enable) 640 { 641 usb_device_request_t r; 642 usbd_status err; 643 uint8_t buf[8]; 644 645 /* type 3 has no raw mode */ 646 if (sc->dev_type->type == UBCMTP_TYPE3) 647 return (0); 648 649 r.bRequest = UR_GET_REPORT; 650 r.bmRequestType = UT_READ_CLASS_INTERFACE; 651 if (sc->dev_type->type < UBCMTP_TYPE4) { 652 USETW2(r.wValue, UHID_FEATURE_REPORT, 0); 653 USETW(r.wIndex, 0); 654 USETW(r.wLength, UBCMTP_WELLSPRING_MODE_LEN); 655 } else { 656 USETW2(r.wValue, UHID_FEATURE_REPORT, 2); 657 USETW(r.wIndex, 2); 658 USETW(r.wLength, UBCMTP_WELLSPRING9_MODE_LEN); 659 } 660 661 err = usbd_do_request(sc->sc_udev, &r, buf); 662 if (err != USBD_NORMAL_COMPLETION) { 663 printf("%s: %s: failed to get feature report\n", 664 sc->sc_dev.dv_xname, __func__); 665 return (err); 666 } 667 668 /* toggle magic byte and write everything back */ 669 if (sc->dev_type->type < UBCMTP_TYPE4) 670 buf[0] = (enable ? UBCMTP_WELLSPRING_MODE_RAW : 671 UBCMTP_WELLSPRING_MODE_HID); 672 else 673 buf[1] = (enable ? UBCMTP_WELLSPRING9_MODE_RAW : 674 UBCMTP_WELLSPRING9_MODE_HID); 675 676 r.bRequest = UR_SET_REPORT; 677 r.bmRequestType = UT_WRITE_CLASS_INTERFACE; 678 if (sc->dev_type->type < UBCMTP_TYPE4) { 679 USETW2(r.wValue, UHID_FEATURE_REPORT, 0); 680 USETW(r.wIndex, 0); 681 USETW(r.wLength, UBCMTP_WELLSPRING_MODE_LEN); 682 } else { 683 USETW2(r.wValue, UHID_FEATURE_REPORT, 2); 684 USETW(r.wIndex, 2); 685 USETW(r.wLength, UBCMTP_WELLSPRING9_MODE_LEN); 686 } 687 688 err = usbd_do_request(sc->sc_udev, &r, buf); 689 if (err != USBD_NORMAL_COMPLETION) { 690 printf("%s: %s: failed to toggle raw mode\n", 691 sc->sc_dev.dv_xname, __func__); 692 return (err); 693 } 694 695 return (0); 696 } 697 698 int 699 ubcmtp_setup_pipes(struct ubcmtp_softc *sc) 700 { 701 usbd_status err; 702 usb_endpoint_descriptor_t *ed; 703 704 if (sc->dev_type->type == UBCMTP_TYPE1) { 705 /* setup physical button pipe */ 706 707 ed = usbd_interface2endpoint_descriptor(sc->sc_bt_iface, 0); 708 if (ed == NULL) { 709 printf("%s: failed getting button endpoint descriptor\n", 710 sc->sc_dev.dv_xname); 711 goto fail1; 712 } 713 sc->sc_bt_epaddr = ed->bEndpointAddress; 714 sc->bt_pkt = malloc(sc->bt_maxlen, M_USBDEV, M_WAITOK); 715 if (sc->bt_pkt == NULL) 716 goto fail1; 717 718 DPRINTF("%s: button iface at 0x%x, max size %d\n", 719 sc->sc_dev.dv_xname, sc->sc_bt_epaddr, sc->bt_maxlen); 720 721 err = usbd_open_pipe_intr(sc->sc_bt_iface, sc->sc_bt_epaddr, 722 USBD_SHORT_XFER_OK, &sc->sc_bt_pipe, sc, sc->bt_pkt, 723 sc->bt_maxlen, ubcmtp_bt_intr, USBD_DEFAULT_INTERVAL); 724 if (err != USBD_NORMAL_COMPLETION) { 725 printf("%s: failed opening button pipe\n", 726 sc->sc_dev.dv_xname); 727 goto fail1; 728 } 729 } 730 731 /* setup trackpad data pipe */ 732 733 ed = usbd_interface2endpoint_descriptor(sc->sc_tp_iface, 0); 734 if (ed == NULL) { 735 printf("%s: failed getting trackpad data endpoint descriptor\n", 736 sc->sc_dev.dv_xname); 737 goto fail2; 738 } 739 sc->sc_tp_epaddr = ed->bEndpointAddress; 740 sc->tp_pkt = malloc(sc->tp_maxlen, M_USBDEV, M_WAITOK); 741 if (sc->tp_pkt == NULL) 742 goto fail2; 743 744 DPRINTF("%s: trackpad data iface at 0x%x, max size %d\n", 745 sc->sc_dev.dv_xname, sc->sc_tp_epaddr, sc->tp_maxlen); 746 747 err = usbd_open_pipe_intr(sc->sc_tp_iface, sc->sc_tp_epaddr, 748 USBD_SHORT_XFER_OK, &sc->sc_tp_pipe, sc, sc->tp_pkt, sc->tp_maxlen, 749 ubcmtp_tp_intr, USBD_DEFAULT_INTERVAL); 750 if (err != USBD_NORMAL_COMPLETION) { 751 printf("%s: error opening trackpad data pipe\n", 752 sc->sc_dev.dv_xname); 753 goto fail2; 754 } 755 756 return (0); 757 758 fail2: 759 if (sc->sc_tp_pipe != NULL) 760 usbd_close_pipe(sc->sc_tp_pipe); 761 if (sc->tp_pkt != NULL) 762 free(sc->tp_pkt, M_USBDEV, sc->tp_maxlen); 763 fail1: 764 if (sc->sc_bt_pipe != NULL) 765 usbd_close_pipe(sc->sc_bt_pipe); 766 if (sc->bt_pkt != NULL) 767 free(sc->bt_pkt, M_USBDEV, sc->bt_maxlen); 768 769 return (1); 770 } 771 772 void 773 ubcmtp_tp_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 774 { 775 struct ubcmtp_softc *sc = priv; 776 struct ubcmtp_finger *finger; 777 u_int32_t pktlen; 778 int off, s, btn, contacts = 0; 779 780 if (usbd_is_dying(sc->sc_udev) || !(sc->sc_status & UBCMTP_ENABLED)) 781 return; 782 783 if (status != USBD_NORMAL_COMPLETION) { 784 DPRINTF("%s: %s with status 0x%x\n", sc->sc_dev.dv_xname, 785 __func__, status); 786 787 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 788 return; 789 if (status == USBD_STALLED) 790 usbd_clear_endpoint_stall_async(sc->sc_tp_pipe); 791 return; 792 } 793 794 usbd_get_xfer_status(xfer, NULL, NULL, &pktlen, NULL); 795 796 if (sc->tp_pkt == NULL || pktlen < sc->tp_offset) 797 return; 798 799 contacts = 0; 800 for (off = sc->tp_offset; off < pktlen; 801 off += (sizeof(struct ubcmtp_finger) + sc->tp_fingerpad)) { 802 finger = (struct ubcmtp_finger *)(sc->tp_pkt + off); 803 804 if ((int16_t)letoh16(finger->touch_major) == 0) 805 continue; /* finger lifted */ 806 807 sc->frame[contacts].x = (int16_t)letoh16(finger->abs_x); 808 sc->frame[contacts].y = (int16_t)letoh16(finger->abs_y); 809 sc->frame[contacts].pressure = DEFAULT_PRESSURE; 810 contacts++; 811 } 812 813 btn = sc->btn; 814 if (sc->dev_type->type == UBCMTP_TYPE2) 815 sc->btn = !!((int16_t)letoh16(sc->tp_pkt[UBCMTP_TYPE2_BTOFF])); 816 else if (sc->dev_type->type == UBCMTP_TYPE3) 817 sc->btn = !!((int16_t)letoh16(sc->tp_pkt[UBCMTP_TYPE3_BTOFF])); 818 else if (sc->dev_type->type == UBCMTP_TYPE4) 819 sc->btn = !!((int16_t)letoh16(sc->tp_pkt[UBCMTP_TYPE4_BTOFF])); 820 821 if (contacts || sc->contacts || sc->btn != btn) { 822 sc->contacts = contacts; 823 s = spltty(); 824 wsmouse_buttons(sc->sc_wsmousedev, sc->btn); 825 wsmouse_mtframe(sc->sc_wsmousedev, sc->frame, contacts); 826 wsmouse_input_sync(sc->sc_wsmousedev); 827 splx(s); 828 } 829 } 830 831 /* hardware button interrupt */ 832 void 833 ubcmtp_bt_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 834 { 835 struct ubcmtp_softc *sc = priv; 836 struct ubcmtp_button *pkt; 837 u_int32_t len; 838 839 if (usbd_is_dying(sc->sc_udev) || !(sc->sc_status & UBCMTP_ENABLED)) 840 return; 841 842 if (status != USBD_NORMAL_COMPLETION) { 843 DPRINTF("%s: %s with status 0x%x\n", sc->sc_dev.dv_xname, 844 __func__, status); 845 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 846 return; 847 if (status == USBD_STALLED) 848 usbd_clear_endpoint_stall_async(sc->sc_tp_pipe); 849 return; 850 } 851 852 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 853 854 if (sc->bt_pkt == NULL || len < sizeof(struct ubcmtp_button)) 855 return; 856 857 pkt = (struct ubcmtp_button *)(sc->bt_pkt); 858 859 DPRINTF("%s: button interrupt (%d, %d, %d, %d)", sc->sc_dev.dv_xname, 860 pkt->unused, pkt->button, pkt->rel_x, pkt->rel_y); 861 862 if (pkt->button != sc->btn) { 863 sc->btn = pkt->button; 864 wsmouse_buttons(sc->sc_wsmousedev, sc->btn); 865 wsmouse_input_sync(sc->sc_wsmousedev); 866 } 867 } 868