1 /* $NetBSD: uts.c,v 1.2 2013/01/05 01:30:18 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Pierre Pronchery (khorben@defora.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 * USB generic Touch Screen driver. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: uts.c,v 1.2 2013/01/05 01:30:18 christos Exp $"); 38 39 #ifdef _KERNEL_OPT 40 #include "opt_usb.h" 41 #endif 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/device.h> 48 #include <sys/ioctl.h> 49 #include <sys/vnode.h> 50 51 #include <dev/usb/usb.h> 52 #include <dev/usb/usbhid.h> 53 54 #include <dev/usb/usbdi.h> 55 #include <dev/usb/usbdi_util.h> 56 #include <dev/usb/usbdevs.h> 57 #include <dev/usb/usb_quirks.h> 58 #include <dev/usb/uhidev.h> 59 #include <dev/usb/hid.h> 60 61 #include <dev/wscons/wsconsio.h> 62 #include <dev/wscons/wsmousevar.h> 63 #include <dev/wscons/tpcalibvar.h> 64 65 #ifdef USB_DEBUG 66 #define DPRINTF(x) if (utsdebug) printf x 67 #define DPRINTFN(n,x) if (utsdebug>(n)) printf x 68 int utsdebug = 0; 69 #else 70 #define DPRINTF(x) 71 #define DPRINTFN(n,x) 72 #endif 73 74 75 struct uts_softc { 76 struct uhidev sc_hdev; 77 78 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z; 79 struct hid_location sc_loc_btn; 80 81 int sc_enabled; 82 83 int flags; /* device configuration */ 84 #define UTS_ABS 0x1 /* absolute position */ 85 86 u_int32_t sc_buttons; /* touchscreen button status */ 87 device_t sc_wsmousedev; 88 struct tpcalib_softc sc_tpcalib; /* calibration */ 89 struct wsmouse_calibcoords sc_calibcoords; 90 91 char sc_dying; 92 }; 93 94 #define TSCREEN_FLAGS_MASK (HIO_CONST|HIO_RELATIVE) 95 96 Static void uts_intr(struct uhidev *addr, void *ibuf, u_int len); 97 98 Static int uts_enable(void *); 99 Static void uts_disable(void *); 100 Static int uts_ioctl(void *, u_long, void *, int, struct lwp *); 101 102 Static const struct wsmouse_accessops uts_accessops = { 103 uts_enable, 104 uts_ioctl, 105 uts_disable, 106 }; 107 108 Static int uts_match(device_t, cfdata_t, void *); 109 Static void uts_attach(device_t, device_t, void *); 110 Static void uts_childdet(device_t, device_t); 111 Static int uts_detach(device_t, int); 112 Static int uts_activate(device_t, enum devact); 113 114 extern struct cfdriver uts_cd; 115 116 CFATTACH_DECL2_NEW(uts, sizeof(struct uts_softc), uts_match, uts_attach, 117 uts_detach, uts_activate, NULL, uts_childdet); 118 119 Static int 120 uts_match(device_t parent, cfdata_t match, void *aux) 121 { 122 struct uhidev_attach_arg *uha = aux; 123 int size; 124 void *desc; 125 126 uhidev_get_report_desc(uha->parent, &desc, &size); 127 if (!hid_is_collection(desc, size, uha->reportid, 128 HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCH_SCREEN)) && 129 !hid_is_collection(desc, size, uha->reportid, 130 HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER))) 131 return UMATCH_NONE; 132 133 return UMATCH_IFACECLASS; 134 } 135 136 Static void 137 uts_attach(device_t parent, device_t self, void *aux) 138 { 139 struct uts_softc *sc = device_private(self); 140 struct uhidev_attach_arg *uha = aux; 141 struct wsmousedev_attach_args a; 142 int size; 143 void *desc; 144 u_int32_t flags; 145 struct hid_data * d; 146 struct hid_item item; 147 148 aprint_naive("\n"); 149 150 sc->sc_hdev.sc_dev = self; 151 sc->sc_hdev.sc_intr = uts_intr; 152 sc->sc_hdev.sc_parent = uha->parent; 153 sc->sc_hdev.sc_report_id = uha->reportid; 154 155 uhidev_get_report_desc(uha->parent, &desc, &size); 156 157 if (!pmf_device_register(self, NULL, NULL)) 158 aprint_error_dev(self, "couldn't establish power handler\n"); 159 160 /* requires HID usage Generic_Desktop:X */ 161 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 162 uha->reportid, hid_input, &sc->sc_loc_x, &flags)) { 163 aprint_error_dev(sc->sc_hdev.sc_dev, 164 "touchscreen has no X report\n"); 165 return; 166 } 167 switch (flags & TSCREEN_FLAGS_MASK) { 168 case 0: 169 sc->flags |= UTS_ABS; 170 break; 171 case HIO_RELATIVE: 172 break; 173 default: 174 aprint_error_dev(sc->sc_hdev.sc_dev, 175 "X report 0x%04x not supported\n", flags); 176 return; 177 } 178 179 /* requires HID usage Generic_Desktop:Y */ 180 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 181 uha->reportid, hid_input, &sc->sc_loc_y, &flags)) { 182 aprint_error_dev(sc->sc_hdev.sc_dev, 183 "touchscreen has no Y report\n"); 184 return; 185 } 186 switch (flags & TSCREEN_FLAGS_MASK) { 187 case 0: 188 sc->flags |= UTS_ABS; 189 break; 190 case HIO_RELATIVE: 191 break; 192 default: 193 aprint_error_dev(sc->sc_hdev.sc_dev, 194 "Y report 0x%04x not supported\n", flags); 195 return; 196 } 197 198 /* requires HID usage Digitizer:Tip_Switch */ 199 if (!hid_locate(desc, size, HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_SWITCH), 200 uha->reportid, hid_input, &sc->sc_loc_btn, 0)) { 201 aprint_error_dev(sc->sc_hdev.sc_dev, 202 "touchscreen has no tip switch report\n"); 203 return; 204 } 205 206 /* requires HID usage Digitizer:In_Range */ 207 if (!hid_locate(desc, size, HID_USAGE2(HUP_DIGITIZERS, HUD_IN_RANGE), 208 uha->reportid, hid_input, &sc->sc_loc_z, &flags)) { 209 aprint_error_dev(sc->sc_hdev.sc_dev, 210 "touchscreen has no range report\n"); 211 return; 212 } 213 214 /* multi-touch support would need HUD_CONTACTID and HUD_CONTACTMAX */ 215 216 #ifdef USB_DEBUG 217 DPRINTF(("uts_attach: sc=%p\n", sc)); 218 DPRINTF(("uts_attach: X\t%d/%d\n", 219 sc->sc_loc_x.pos, sc->sc_loc_x.size)); 220 DPRINTF(("uts_attach: Y\t%d/%d\n", 221 sc->sc_loc_y.pos, sc->sc_loc_y.size)); 222 DPRINTF(("uts_attach: Z\t%d/%d\n", 223 sc->sc_loc_z.pos, sc->sc_loc_z.size)); 224 #endif 225 226 a.accessops = &uts_accessops; 227 a.accesscookie = sc; 228 229 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 230 231 /* calibrate the touchscreen */ 232 memset(&sc->sc_calibcoords, 0, sizeof(sc->sc_calibcoords)); 233 sc->sc_calibcoords.maxx = 4095; 234 sc->sc_calibcoords.maxy = 4095; 235 sc->sc_calibcoords.samplelen = WSMOUSE_CALIBCOORDS_RESET; 236 d = hid_start_parse(desc, size, hid_input); 237 while (hid_get_item(d, &item)) { 238 if (item.kind != hid_input 239 || HID_GET_USAGE_PAGE(item.usage) != HUP_GENERIC_DESKTOP 240 || item.report_ID != sc->sc_hdev.sc_report_id) 241 continue; 242 if (HID_GET_USAGE(item.usage) == HUG_X) { 243 sc->sc_calibcoords.minx = item.logical_minimum; 244 sc->sc_calibcoords.maxx = item.logical_maximum; 245 } 246 if (HID_GET_USAGE(item.usage) == HUG_Y) { 247 sc->sc_calibcoords.miny = item.logical_minimum; 248 sc->sc_calibcoords.maxy = item.logical_maximum; 249 } 250 } 251 hid_end_parse(d); 252 253 tpcalib_init(&sc->sc_tpcalib); 254 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS, 255 (void *)&sc->sc_calibcoords, 0, 0); 256 257 return; 258 } 259 260 Static int 261 uts_detach(device_t self, int flags) 262 { 263 struct uts_softc *sc = device_private(self); 264 int rv = 0; 265 266 DPRINTF(("uts_detach: sc=%p flags=%d\n", sc, flags)); 267 268 if (sc->sc_wsmousedev != NULL) 269 rv = config_detach(sc->sc_wsmousedev, flags); 270 271 pmf_device_deregister(self); 272 273 return rv; 274 } 275 276 Static void 277 uts_childdet(device_t self, device_t child) 278 { 279 struct uts_softc *sc = device_private(self); 280 281 KASSERT(sc->sc_wsmousedev == child); 282 sc->sc_wsmousedev = NULL; 283 } 284 285 Static int 286 uts_activate(device_t self, enum devact act) 287 { 288 struct uts_softc *sc = device_private(self); 289 290 switch (act) { 291 case DVACT_DEACTIVATE: 292 sc->sc_dying = 1; 293 return 0; 294 default: 295 return EOPNOTSUPP; 296 } 297 } 298 299 Static int 300 uts_enable(void *v) 301 { 302 struct uts_softc *sc = v; 303 304 DPRINTFN(1,("uts_enable: sc=%p\n", sc)); 305 306 if (sc->sc_dying) 307 return EIO; 308 309 if (sc->sc_enabled) 310 return EBUSY; 311 312 sc->sc_enabled = 1; 313 sc->sc_buttons = 0; 314 315 return uhidev_open(&sc->sc_hdev); 316 } 317 318 Static void 319 uts_disable(void *v) 320 { 321 struct uts_softc *sc = v; 322 323 DPRINTFN(1,("uts_disable: sc=%p\n", sc)); 324 #ifdef DIAGNOSTIC 325 if (!sc->sc_enabled) { 326 printf("uts_disable: not enabled\n"); 327 return; 328 } 329 #endif 330 331 sc->sc_enabled = 0; 332 uhidev_close(&sc->sc_hdev); 333 } 334 335 Static int 336 uts_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 337 { 338 struct uts_softc *sc = v; 339 340 switch (cmd) { 341 case WSMOUSEIO_GTYPE: 342 if (sc->flags & UTS_ABS) 343 *(u_int *)data = WSMOUSE_TYPE_TPANEL; 344 else 345 *(u_int *)data = WSMOUSE_TYPE_USB; 346 return 0; 347 case WSMOUSEIO_SCALIBCOORDS: 348 case WSMOUSEIO_GCALIBCOORDS: 349 return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l); 350 } 351 352 return EPASSTHROUGH; 353 } 354 355 Static void 356 uts_intr(struct uhidev *addr, void *ibuf, u_int len) 357 { 358 struct uts_softc *sc = (struct uts_softc *)addr; 359 int dx, dy, dz; 360 u_int32_t buttons = 0; 361 int flags, s; 362 363 DPRINTFN(5,("uts_intr: len=%d\n", len)); 364 365 flags = WSMOUSE_INPUT_DELTA | WSMOUSE_INPUT_ABSOLUTE_Z; 366 367 dx = hid_get_data(ibuf, &sc->sc_loc_x); 368 if (sc->flags & UTS_ABS) { 369 flags |= (WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y); 370 dy = hid_get_data(ibuf, &sc->sc_loc_y); 371 tpcalib_trans(&sc->sc_tpcalib, dx, dy, &dx, &dy); 372 } else 373 dy = -hid_get_data(ibuf, &sc->sc_loc_y); 374 375 dz = hid_get_data(ibuf, &sc->sc_loc_z); 376 377 if (hid_get_data(ibuf, &sc->sc_loc_btn)) 378 buttons |= 1; 379 380 if (dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) { 381 DPRINTFN(10,("uts_intr: x:%d y:%d z:%d buttons:0x%x\n", 382 dx, dy, dz, buttons)); 383 sc->sc_buttons = buttons; 384 if (sc->sc_wsmousedev != NULL) { 385 s = spltty(); 386 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz, 0, 387 flags); 388 splx(s); 389 } 390 } 391 } 392