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