1 /* $NetBSD: uep.c,v 1.19 2013/09/15 15:07:06 martin Exp $ */ 2 3 /* 4 * Copyright (c) 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tyler C. Sarna (tsarna@netbsd.org). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * eGalax USB touchpanel controller driver. 34 */ 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: uep.c,v 1.19 2013/09/15 15:07:06 martin Exp $"); 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/malloc.h> 42 #include <sys/device.h> 43 #include <sys/ioctl.h> 44 #include <sys/vnode.h> 45 46 #include <dev/usb/usb.h> 47 #include <dev/usb/usbdi.h> 48 #include <dev/usb/usbdi_util.h> 49 #include <dev/usb/usbdevs.h> 50 #include <dev/usb/usb_quirks.h> 51 52 #include <dev/wscons/wsconsio.h> 53 #include <dev/wscons/wsmousevar.h> 54 #include <dev/wscons/tpcalibvar.h> 55 56 #define UIDSTR "eGalax USB SN000000" 57 58 struct uep_softc { 59 device_t sc_dev; 60 usbd_device_handle sc_udev; /* device */ 61 usbd_interface_handle sc_iface; /* interface */ 62 int sc_iface_number; 63 64 int sc_intr_number; /* interrupt number */ 65 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */ 66 u_char *sc_ibuf; 67 int sc_isize; 68 69 device_t sc_wsmousedev; /* wsmouse device */ 70 struct tpcalib_softc sc_tpcalib; /* calibration */ 71 72 u_char sc_enabled; 73 u_char sc_dying; 74 }; 75 76 static struct wsmouse_calibcoords default_calib = { 77 .minx = 0, 78 .miny = 0, 79 .maxx = 2047, 80 .maxy = 2047, 81 .samplelen = WSMOUSE_CALIBCOORDS_RESET, 82 }; 83 84 Static void uep_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 85 86 Static int uep_enable(void *); 87 Static void uep_disable(void *); 88 Static int uep_ioctl(void *, u_long, void *, int, struct lwp *); 89 90 const struct wsmouse_accessops uep_accessops = { 91 uep_enable, 92 uep_ioctl, 93 uep_disable, 94 }; 95 96 int uep_match(device_t, cfdata_t, void *); 97 void uep_attach(device_t, device_t, void *); 98 void uep_childdet(device_t, device_t); 99 int uep_detach(device_t, int); 100 int uep_activate(device_t, enum devact); 101 extern struct cfdriver uep_cd; 102 CFATTACH_DECL2_NEW(uep, sizeof(struct uep_softc), uep_match, uep_attach, 103 uep_detach, uep_activate, NULL, uep_childdet); 104 105 int 106 uep_match(device_t parent, cfdata_t match, void *aux) 107 { 108 struct usb_attach_arg *uaa = aux; 109 110 if ((uaa->vendor == USB_VENDOR_EGALAX) && ( 111 (uaa->product == USB_PRODUCT_EGALAX_TPANEL) 112 || (uaa->product == USB_PRODUCT_EGALAX_TPANEL2))) 113 return UMATCH_VENDOR_PRODUCT; 114 115 if ((uaa->vendor == USB_VENDOR_EGALAX2) 116 && (uaa->product == USB_PRODUCT_EGALAX2_TPANEL)) 117 return UMATCH_VENDOR_PRODUCT; 118 119 120 return UMATCH_NONE; 121 } 122 123 void 124 uep_attach(device_t parent, device_t self, void *aux) 125 { 126 struct uep_softc *sc = device_private(self); 127 struct usb_attach_arg *uaa = aux; 128 usbd_device_handle dev = uaa->device; 129 usb_config_descriptor_t *cdesc; 130 usb_interface_descriptor_t *id; 131 usb_endpoint_descriptor_t *ed; 132 usb_device_request_t req; 133 uByte act; 134 struct wsmousedev_attach_args a; 135 char *devinfop; 136 usbd_status err; 137 int i; 138 139 sc->sc_dev = self; 140 141 aprint_naive("\n"); 142 aprint_normal("\n"); 143 144 devinfop = usbd_devinfo_alloc(dev, 0); 145 aprint_normal_dev(self, "%s\n", devinfop); 146 usbd_devinfo_free(devinfop); 147 sc->sc_udev = dev; 148 sc->sc_intr_number = -1; 149 sc->sc_intr_pipe = NULL; 150 sc->sc_enabled = sc->sc_isize = 0; 151 152 /* Move the device into the configured state. */ 153 err = usbd_set_config_index(dev, 0, 1); 154 if (err) { 155 aprint_error("\n%s: failed to set configuration, err=%s\n", 156 device_xname(sc->sc_dev), usbd_errstr(err)); 157 sc->sc_dying = 1; 158 return; 159 } 160 161 /* get the config descriptor */ 162 cdesc = usbd_get_config_descriptor(sc->sc_udev); 163 if (cdesc == NULL) { 164 aprint_error_dev(self, 165 "failed to get configuration descriptor\n"); 166 sc->sc_dying = 1; 167 return; 168 } 169 170 /* get the interface */ 171 err = usbd_device2interface_handle(dev, 0, &sc->sc_iface); 172 if (err) { 173 aprint_error("\n%s: failed to get interface, err=%s\n", 174 device_xname(sc->sc_dev), usbd_errstr(err)); 175 sc->sc_dying = 1; 176 return; 177 } 178 179 /* Find the interrupt endpoint */ 180 id = usbd_get_interface_descriptor(sc->sc_iface); 181 sc->sc_iface_number = id->bInterfaceNumber; 182 183 for (i = 0; i < id->bNumEndpoints; i++) { 184 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 185 if (ed == NULL) { 186 aprint_error_dev(self, 187 "no endpoint descriptor for %d\n", i); 188 sc->sc_dying = 1; 189 return; 190 } 191 192 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 193 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 194 sc->sc_intr_number = ed->bEndpointAddress; 195 sc->sc_isize = UGETW(ed->wMaxPacketSize); 196 } 197 } 198 199 if (sc->sc_intr_number== -1) { 200 aprint_error_dev(self, "Could not find interrupt in\n"); 201 sc->sc_dying = 1; 202 return; 203 } 204 205 /* Newer controllers need an activation command */ 206 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 207 req.bRequest = 0x0a; 208 USETW(req.wValue, 'A'); 209 USETW(req.wIndex, 0); 210 USETW(req.wLength, 1); 211 usbd_do_request(dev, &req, &act); 212 213 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 214 215 a.accessops = &uep_accessops; 216 a.accesscookie = sc; 217 218 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 219 220 tpcalib_init(&sc->sc_tpcalib); 221 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS, 222 (void *)&default_calib, 0, 0); 223 224 return; 225 } 226 227 int 228 uep_detach(device_t self, int flags) 229 { 230 struct uep_softc *sc = device_private(self); 231 int rv = 0; 232 233 if (sc->sc_intr_pipe != NULL) { 234 usbd_abort_pipe(sc->sc_intr_pipe); 235 usbd_close_pipe(sc->sc_intr_pipe); 236 sc->sc_intr_pipe = NULL; 237 } 238 sc->sc_dying = 1; 239 240 /* save current calib as defaults */ 241 default_calib = sc->sc_tpcalib.sc_saved; 242 243 if (sc->sc_wsmousedev != NULL) 244 rv = config_detach(sc->sc_wsmousedev, flags); 245 246 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 247 return rv; 248 } 249 250 void 251 uep_childdet(device_t self, device_t child) 252 { 253 struct uep_softc *sc = device_private(self); 254 255 KASSERT(sc->sc_wsmousedev == child); 256 sc->sc_wsmousedev = NULL; 257 } 258 259 int 260 uep_activate(device_t self, enum devact act) 261 { 262 struct uep_softc *sc = device_private(self); 263 264 switch (act) { 265 case DVACT_DEACTIVATE: 266 sc->sc_dying = 1; 267 return 0; 268 default: 269 return EOPNOTSUPP; 270 } 271 } 272 273 Static int 274 uep_enable(void *v) 275 { 276 struct uep_softc *sc = v; 277 int err; 278 279 if (sc->sc_dying) 280 return EIO; 281 282 if (sc->sc_enabled) 283 return EBUSY; 284 285 if (sc->sc_isize == 0) 286 return 0; 287 288 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 289 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number, 290 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf, 291 sc->sc_isize, uep_intr, USBD_DEFAULT_INTERVAL); 292 if (err) { 293 free(sc->sc_ibuf, M_USBDEV); 294 sc->sc_intr_pipe = NULL; 295 return EIO; 296 } 297 298 sc->sc_enabled = 1; 299 300 return 0; 301 } 302 303 Static void 304 uep_disable(void *v) 305 { 306 struct uep_softc *sc = v; 307 308 if (!sc->sc_enabled) { 309 printf("uep_disable: already disabled!\n"); 310 return; 311 } 312 313 /* Disable interrupts. */ 314 if (sc->sc_intr_pipe != NULL) { 315 usbd_abort_pipe(sc->sc_intr_pipe); 316 usbd_close_pipe(sc->sc_intr_pipe); 317 sc->sc_intr_pipe = NULL; 318 } 319 320 if (sc->sc_ibuf != NULL) { 321 free(sc->sc_ibuf, M_USBDEV); 322 sc->sc_ibuf = NULL; 323 } 324 325 sc->sc_enabled = 0; 326 } 327 328 Static int 329 uep_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 330 { 331 struct uep_softc *sc = v; 332 struct wsmouse_id *id; 333 334 switch (cmd) { 335 case WSMOUSEIO_GTYPE: 336 *(u_int *)data = WSMOUSE_TYPE_TPANEL; 337 return 0; 338 339 case WSMOUSEIO_GETID: 340 /* 341 * return unique ID string 342 * "<vendor> <model> <serial number>" 343 * unfortunately we have no serial number... 344 */ 345 id = (struct wsmouse_id *)data; 346 if (id->type != WSMOUSE_ID_TYPE_UIDSTR) 347 return EINVAL; 348 349 strcpy(id->data, UIDSTR); 350 id->length = strlen(UIDSTR); 351 return 0; 352 353 case WSMOUSEIO_SCALIBCOORDS: 354 case WSMOUSEIO_GCALIBCOORDS: 355 return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l); 356 } 357 358 return EPASSTHROUGH; 359 } 360 361 void 362 uep_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 363 { 364 struct uep_softc *sc = addr; 365 u_char *p = sc->sc_ibuf; 366 u_char msk; 367 u_int32_t len; 368 int x = 0, y = 0, s; 369 370 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 371 372 if (status == USBD_CANCELLED) 373 return; 374 375 if (status != USBD_NORMAL_COMPLETION) { 376 aprint_error_dev(sc->sc_dev, "status %d\n", status); 377 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe); 378 return; 379 } 380 381 /* First bit is always set to 1 */ 382 if ((p[0] & 0x80) != 0x80) { 383 aprint_error_dev(sc->sc_dev, "bad input packet format\n"); 384 return; 385 } 386 387 if (sc->sc_wsmousedev != NULL) { 388 /* 389 * Each report package may contain 5 or 6 bytes as below: 390 * 391 * Byte 0 1ZM00HLT 392 * Byte 1 0AAAAAAA 393 * Byte 2 0AAAAAAA 394 * Byte 3 0BBBBBBB 395 * Byte 4 0BBBBBBB 396 * Byte 5 0PPPPPPP 397 * 398 * Z: 1=byte 5 is pressure information, 0=no pressure 399 * M: 1=byte 5 is play id, 0=no player id 400 * T: 1=touched, 0=not touched 401 * H,L: Resolution 402 * 0,0: 11 bits 403 * 0,1: 12 bits 404 * 1,0: 13 bits 405 * 1,1: 14 bits 406 * A: bits of axis A position, MSB to LSB 407 * B: bits of axis B position, MSB to LSB 408 * 409 * The packet has six bytes only if Z or M is set. 410 * Byte 5, if sent, is ignored. 411 * 412 * For the unit I have, A = Y and B = X. 413 * I don't know if units exist with A=X and B=Y, 414 * if so we'll cross that bridge when we come to it. 415 * 416 * The controller sends a stream of T=1 events while the 417 * panel is touched, followed by a single T=0 event. 418 */ 419 switch (p[0] & 0x06) { 420 case 0x02: 421 msk = 0x1f; 422 break; 423 case 0x04: 424 msk = 0x3f; 425 break; 426 case 0x06: 427 msk = 0x7f; 428 break; 429 default: 430 msk = 0x0f; /* H=0, L=0 */ 431 } 432 x = ((p[3] & msk) << 7) | p[4]; 433 y = ((p[1] & msk) << 7) | p[2]; 434 435 tpcalib_trans(&sc->sc_tpcalib, x, y, &x, &y); 436 437 s = spltty(); 438 wsmouse_input(sc->sc_wsmousedev, p[0] & 0x01, x, y, 0, 0, 439 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y); 440 splx(s); 441 } 442 } 443