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