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