Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.103
      1  1.103  riastrad /*	$NetBSD: ums.c,v 1.103 2022/03/28 12:44:17 riastradh Exp $	*/
      2    1.1  augustss 
      3    1.1  augustss /*
      4   1.91    bouyer  * Copyright (c) 1998, 2017 The NetBSD Foundation, Inc.
      5    1.1  augustss  * All rights reserved.
      6    1.1  augustss  *
      7   1.11  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.43  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9   1.11  augustss  * Carlstedt Research & Technology.
     10    1.1  augustss  *
     11    1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12    1.1  augustss  * modification, are permitted provided that the following conditions
     13    1.1  augustss  * are met:
     14    1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15    1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16    1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17    1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18    1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19    1.1  augustss  *
     20    1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21    1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22    1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23    1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24    1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25    1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26    1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27    1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28    1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29    1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30    1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31   1.20  augustss  */
     32   1.20  augustss 
     33   1.20  augustss /*
     34   1.60  augustss  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
     35    1.1  augustss  */
     36   1.52     lukem 
     37   1.52     lukem #include <sys/cdefs.h>
     38  1.103  riastrad __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.103 2022/03/28 12:44:17 riastradh Exp $");
     39   1.90  jakllsch 
     40   1.90  jakllsch #ifdef _KERNEL_OPT
     41   1.90  jakllsch #include "opt_usb.h"
     42   1.90  jakllsch #endif
     43    1.1  augustss 
     44    1.1  augustss #include <sys/param.h>
     45    1.1  augustss #include <sys/systm.h>
     46    1.1  augustss #include <sys/kernel.h>
     47    1.1  augustss #include <sys/device.h>
     48    1.1  augustss #include <sys/ioctl.h>
     49    1.1  augustss #include <sys/file.h>
     50    1.1  augustss #include <sys/select.h>
     51    1.1  augustss #include <sys/proc.h>
     52    1.1  augustss #include <sys/vnode.h>
     53    1.1  augustss #include <sys/poll.h>
     54    1.1  augustss 
     55    1.1  augustss #include <dev/usb/usb.h>
     56    1.1  augustss #include <dev/usb/usbhid.h>
     57    1.1  augustss 
     58    1.1  augustss #include <dev/usb/usbdi.h>
     59    1.1  augustss #include <dev/usb/usbdi_util.h>
     60    1.1  augustss #include <dev/usb/usbdevs.h>
     61    1.1  augustss #include <dev/usb/usb_quirks.h>
     62   1.53  augustss #include <dev/usb/uhidev.h>
     63   1.91    bouyer #include <dev/hid/hid.h>
     64   1.91    bouyer #include <dev/hid/hidms.h>
     65    1.3  augustss 
     66   1.86  christos #ifdef UMS_DEBUG
     67   1.81    dyoung #define DPRINTF(x)	if (umsdebug) printf x
     68   1.81    dyoung #define DPRINTFN(n,x)	if (umsdebug>(n)) printf x
     69    1.1  augustss int	umsdebug = 0;
     70    1.1  augustss #else
     71    1.1  augustss #define DPRINTF(x)
     72    1.1  augustss #define DPRINTFN(n,x)
     73    1.1  augustss #endif
     74    1.1  augustss 
     75   1.27  augustss #define UMSUNIT(s)	(minor(s))
     76   1.16  augustss 
     77    1.1  augustss struct ums_softc {
     78  1.103  riastrad 	struct uhidev *sc_hdev;
     79  1.102  riastrad 	struct usbd_device *sc_udev;
     80   1.91    bouyer 	struct hidms sc_ms;
     81   1.53  augustss 
     82   1.96  jmcneill 	bool	sc_alwayson;
     83   1.96  jmcneill 
     84   1.91    bouyer 	int	sc_enabled;
     85   1.91    bouyer 	char	sc_dying;
     86   1.84  christos };
     87   1.84  christos 
     88  1.103  riastrad Static void ums_intr(void *, void *, u_int);
     89    1.1  augustss 
     90   1.44  augustss Static int	ums_enable(void *);
     91   1.44  augustss Static void	ums_disable(void *);
     92   1.89     skrll Static int	ums_ioctl(void *, u_long, void *, int, struct lwp *);
     93    1.3  augustss 
     94   1.95      maxv static const struct wsmouse_accessops ums_accessops = {
     95    1.3  augustss 	ums_enable,
     96    1.3  augustss 	ums_ioctl,
     97    1.3  augustss 	ums_disable,
     98    1.3  augustss };
     99    1.3  augustss 
    100   1.95      maxv static int ums_match(device_t, cfdata_t, void *);
    101   1.95      maxv static void ums_attach(device_t, device_t, void *);
    102   1.95      maxv static void ums_childdet(device_t, device_t);
    103   1.95      maxv static int ums_detach(device_t, int);
    104   1.95      maxv static int ums_activate(device_t, enum devact);
    105   1.93       mrg 
    106   1.73      cube CFATTACH_DECL2_NEW(ums, sizeof(struct ums_softc), ums_match, ums_attach,
    107   1.71    dyoung     ums_detach, ums_activate, NULL, ums_childdet);
    108    1.1  augustss 
    109   1.95      maxv static int
    110   1.73      cube ums_match(device_t parent, cfdata_t match, void *aux)
    111    1.1  augustss {
    112   1.53  augustss 	struct uhidev_attach_arg *uha = aux;
    113   1.53  augustss 	int size;
    114    1.1  augustss 	void *desc;
    115   1.57  augustss 
    116   1.79  jakllsch 	/*
    117   1.79  jakllsch 	 * Some (older) Griffin PowerMate knobs may masquerade as a
    118   1.79  jakllsch 	 * mouse, avoid treating them as such, they have only one axis.
    119   1.79  jakllsch 	 */
    120   1.89     skrll 	if (uha->uiaa->uiaa_vendor == USB_VENDOR_GRIFFIN &&
    121   1.89     skrll 	    uha->uiaa->uiaa_product == USB_PRODUCT_GRIFFIN_POWERMATE)
    122   1.89     skrll 		return UMATCH_NONE;
    123   1.79  jakllsch 
    124   1.53  augustss 	uhidev_get_report_desc(uha->parent, &desc, &size);
    125   1.53  augustss 	if (!hid_is_collection(desc, size, uha->reportid,
    126   1.78  jakllsch 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)) &&
    127   1.78  jakllsch 	    !hid_is_collection(desc, size, uha->reportid,
    128   1.84  christos 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_POINTER)) &&
    129   1.84  christos 	    !hid_is_collection(desc, size, uha->reportid,
    130   1.89     skrll 			       HID_USAGE2(HUP_DIGITIZERS, 0x0002)))
    131   1.89     skrll 		return UMATCH_NONE;
    132    1.1  augustss 
    133   1.89     skrll 	return UMATCH_IFACECLASS;
    134    1.1  augustss }
    135    1.1  augustss 
    136   1.95      maxv static void
    137   1.73      cube ums_attach(device_t parent, device_t self, void *aux)
    138    1.1  augustss {
    139   1.73      cube 	struct ums_softc *sc = device_private(self);
    140   1.53  augustss 	struct uhidev_attach_arg *uha = aux;
    141   1.99  jmcneill 	struct hid_data *d;
    142   1.99  jmcneill 	struct hid_item item;
    143   1.96  jmcneill 	int size, error;
    144    1.1  augustss 	void *desc;
    145   1.91    bouyer 	uint32_t quirks;
    146    1.1  augustss 
    147   1.69  jmcneill 	aprint_naive("\n");
    148   1.69  jmcneill 
    149  1.103  riastrad 	sc->sc_hdev = uha->parent;
    150  1.102  riastrad 	sc->sc_udev = uha->uiaa->uiaa_device;
    151    1.1  augustss 
    152  1.102  riastrad 	quirks = usbd_get_quirks(sc->sc_udev)->uq_flags;
    153   1.35  augustss 	if (quirks & UQ_MS_REVZ)
    154   1.91    bouyer 		sc->sc_ms.flags |= HIDMS_REVZ;
    155   1.35  augustss 	if (quirks & UQ_SPUR_BUT_UP)
    156   1.91    bouyer 		sc->sc_ms.flags |= HIDMS_SPUR_BUT_UP;
    157  1.101  macallan 	if (quirks & UQ_ALWAYS_ON)
    158  1.101  macallan 		sc->sc_alwayson = true;
    159   1.84  christos 
    160   1.70  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    161   1.70  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    162   1.70  jmcneill 
    163   1.91    bouyer 	uhidev_get_report_desc(uha->parent, &desc, &size);
    164   1.10  augustss 
    165   1.91    bouyer 	if (!hidms_setup(self, &sc->sc_ms, uha->reportid, desc, size))
    166   1.81    dyoung 		return;
    167    1.6  augustss 
    168   1.89     skrll 	if (uha->uiaa->uiaa_vendor == USB_VENDOR_MICROSOFT) {
    169   1.88  christos 		int fixpos;
    170   1.98  jdolecek 		int woffset = 8;
    171   1.88  christos 		/*
    172   1.88  christos 		 * The Microsoft Wireless Laser Mouse 6000 v2.0 and the
    173   1.88  christos 		 * Microsoft Comfort Mouse 2.0 report a bad position for
    174   1.88  christos 		 * the wheel and wheel tilt controls -- should be in bytes
    175   1.88  christos 		 * 3 & 4 of the report. Fix this if necessary.
    176   1.88  christos 		 */
    177   1.89     skrll 		switch (uha->uiaa->uiaa_product) {
    178   1.88  christos 		case USB_PRODUCT_MICROSOFT_24GHZ_XCVR10:
    179   1.88  christos 		case USB_PRODUCT_MICROSOFT_24GHZ_XCVR20:
    180   1.92      maya 		case USB_PRODUCT_MICROSOFT_NATURAL_6000:
    181   1.88  christos 			fixpos = 24;
    182   1.88  christos 			break;
    183   1.98  jdolecek 		case USB_PRODUCT_MICROSOFT_24GHZ_XCVR80:
    184   1.98  jdolecek 			fixpos = 40;
    185   1.98  jdolecek 			woffset = sc->sc_ms.hidms_loc_z.size;
    186   1.98  jdolecek 			break;
    187   1.88  christos 		case USB_PRODUCT_MICROSOFT_CM6000:
    188   1.88  christos 			fixpos = 40;
    189   1.88  christos 			break;
    190   1.88  christos 		default:
    191   1.88  christos 			fixpos = 0;
    192   1.88  christos 			break;
    193   1.88  christos 		}
    194   1.88  christos 		if (fixpos) {
    195   1.91    bouyer 			if ((sc->sc_ms.flags & HIDMS_Z) &&
    196   1.91    bouyer 			    sc->sc_ms.hidms_loc_z.pos == 0)
    197   1.91    bouyer 				sc->sc_ms.hidms_loc_z.pos = fixpos;
    198   1.91    bouyer 			if ((sc->sc_ms.flags & HIDMS_W) &&
    199   1.91    bouyer 			    sc->sc_ms.hidms_loc_w.pos == 0)
    200   1.91    bouyer 				sc->sc_ms.hidms_loc_w.pos =
    201   1.98  jdolecek 				    sc->sc_ms.hidms_loc_z.pos + woffset;
    202   1.88  christos 		}
    203   1.75     rafal 	}
    204   1.63  augustss 
    205   1.94    yhardy 	tpcalib_init(&sc->sc_ms.sc_tpcalib);
    206   1.99  jmcneill 
    207   1.99  jmcneill 	/* calibrate the pointer if it reports absolute events */
    208   1.99  jmcneill 	if (sc->sc_ms.flags & HIDMS_ABS) {
    209   1.99  jmcneill 		memset(&sc->sc_ms.sc_calibcoords, 0, sizeof(sc->sc_ms.sc_calibcoords));
    210   1.99  jmcneill 		sc->sc_ms.sc_calibcoords.maxx = 0;
    211   1.99  jmcneill 		sc->sc_ms.sc_calibcoords.maxy = 0;
    212   1.99  jmcneill 		sc->sc_ms.sc_calibcoords.samplelen = WSMOUSE_CALIBCOORDS_RESET;
    213   1.99  jmcneill 		d = hid_start_parse(desc, size, hid_input);
    214   1.99  jmcneill 		if (d != NULL) {
    215   1.99  jmcneill 			while (hid_get_item(d, &item)) {
    216   1.99  jmcneill 				if (item.kind != hid_input
    217   1.99  jmcneill 				    || HID_GET_USAGE_PAGE(item.usage) != HUP_GENERIC_DESKTOP
    218  1.103  riastrad 				    || item.report_ID != uha->reportid)
    219   1.99  jmcneill 					continue;
    220   1.99  jmcneill 				if (HID_GET_USAGE(item.usage) == HUG_X) {
    221   1.99  jmcneill 					sc->sc_ms.sc_calibcoords.minx = item.logical_minimum;
    222   1.99  jmcneill 					sc->sc_ms.sc_calibcoords.maxx = item.logical_maximum;
    223   1.99  jmcneill 				}
    224   1.99  jmcneill 				if (HID_GET_USAGE(item.usage) == HUG_Y) {
    225   1.99  jmcneill 					sc->sc_ms.sc_calibcoords.miny = item.logical_minimum;
    226   1.99  jmcneill 					sc->sc_ms.sc_calibcoords.maxy = item.logical_maximum;
    227   1.99  jmcneill 				}
    228   1.99  jmcneill 			}
    229   1.99  jmcneill 			hid_end_parse(d);
    230   1.99  jmcneill 		}
    231   1.99  jmcneill         	tpcalib_ioctl(&sc->sc_ms.sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    232   1.99  jmcneill         	    (void *)&sc->sc_ms.sc_calibcoords, 0, 0);
    233   1.99  jmcneill 	}
    234   1.99  jmcneill 
    235   1.91    bouyer 	hidms_attach(self, &sc->sc_ms, &ums_accessops);
    236   1.96  jmcneill 
    237   1.96  jmcneill 	if (sc->sc_alwayson) {
    238  1.103  riastrad 		error = uhidev_open(sc->sc_hdev, &ums_intr, sc);
    239   1.96  jmcneill 		if (error != 0) {
    240   1.96  jmcneill 			aprint_error_dev(self,
    241   1.96  jmcneill 			    "WARNING: couldn't open always-on device\n");
    242   1.96  jmcneill 			sc->sc_alwayson = false;
    243   1.96  jmcneill 		}
    244   1.96  jmcneill 	}
    245   1.16  augustss }
    246   1.16  augustss 
    247   1.95      maxv static int
    248   1.81    dyoung ums_activate(device_t self, enum devact act)
    249   1.16  augustss {
    250   1.73      cube 	struct ums_softc *sc = device_private(self);
    251   1.28  augustss 
    252   1.28  augustss 	switch (act) {
    253   1.28  augustss 	case DVACT_DEACTIVATE:
    254   1.28  augustss 		sc->sc_dying = 1;
    255   1.76    dyoung 		return 0;
    256   1.76    dyoung 	default:
    257   1.76    dyoung 		return EOPNOTSUPP;
    258   1.28  augustss 	}
    259    1.1  augustss }
    260    1.1  augustss 
    261   1.95      maxv static void
    262   1.71    dyoung ums_childdet(device_t self, device_t child)
    263   1.71    dyoung {
    264   1.71    dyoung 	struct ums_softc *sc = device_private(self);
    265   1.71    dyoung 
    266   1.91    bouyer 	KASSERT(sc->sc_ms.hidms_wsmousedev == child);
    267   1.91    bouyer 	sc->sc_ms.hidms_wsmousedev = NULL;
    268   1.71    dyoung }
    269   1.71    dyoung 
    270   1.95      maxv static int
    271   1.71    dyoung ums_detach(device_t self, int flags)
    272    1.1  augustss {
    273   1.71    dyoung 	struct ums_softc *sc = device_private(self);
    274   1.25  augustss 	int rv = 0;
    275    1.8  augustss 
    276   1.25  augustss 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    277   1.33  augustss 
    278   1.96  jmcneill 	if (sc->sc_alwayson)
    279  1.103  riastrad 		uhidev_close(sc->sc_hdev);
    280   1.96  jmcneill 
    281   1.51  augustss 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    282   1.91    bouyer 	if (sc->sc_ms.hidms_wsmousedev != NULL)
    283   1.91    bouyer 		rv = config_detach(sc->sc_ms.hidms_wsmousedev, flags);
    284   1.40  augustss 
    285   1.70  jmcneill 	pmf_device_deregister(self);
    286   1.70  jmcneill 
    287   1.89     skrll 	return rv;
    288    1.1  augustss }
    289    1.1  augustss 
    290   1.95      maxv Static void
    291  1.103  riastrad ums_intr(void *cookie, void *ibuf, u_int len)
    292    1.1  augustss {
    293  1.103  riastrad 	struct ums_softc *sc = cookie;
    294   1.96  jmcneill 
    295   1.96  jmcneill 	if (sc->sc_enabled)
    296   1.96  jmcneill 		hidms_intr(&sc->sc_ms, ibuf, len);
    297    1.1  augustss }
    298    1.3  augustss 
    299   1.42  augustss Static int
    300   1.44  augustss ums_enable(void *v)
    301    1.3  augustss {
    302    1.3  augustss 	struct ums_softc *sc = v;
    303   1.96  jmcneill 	int error = 0;
    304   1.16  augustss 
    305   1.25  augustss 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    306   1.28  augustss 
    307   1.28  augustss 	if (sc->sc_dying)
    308   1.89     skrll 		return EIO;
    309   1.28  augustss 
    310    1.3  augustss 	if (sc->sc_enabled)
    311   1.89     skrll 		return EBUSY;
    312    1.3  augustss 
    313    1.3  augustss 	sc->sc_enabled = 1;
    314   1.91    bouyer 	sc->sc_ms.hidms_buttons = 0;
    315    1.3  augustss 
    316   1.96  jmcneill 	if (!sc->sc_alwayson) {
    317  1.103  riastrad 		error = uhidev_open(sc->sc_hdev, &ums_intr, sc);
    318   1.96  jmcneill 		if (error)
    319   1.96  jmcneill 			sc->sc_enabled = 0;
    320   1.96  jmcneill 	}
    321   1.87   mlelstv 
    322   1.87   mlelstv 	return error;
    323    1.3  augustss }
    324    1.3  augustss 
    325   1.42  augustss Static void
    326   1.44  augustss ums_disable(void *v)
    327    1.3  augustss {
    328    1.3  augustss 	struct ums_softc *sc = v;
    329   1.25  augustss 
    330   1.25  augustss 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    331   1.25  augustss #ifdef DIAGNOSTIC
    332   1.25  augustss 	if (!sc->sc_enabled) {
    333   1.25  augustss 		printf("ums_disable: not enabled\n");
    334   1.25  augustss 		return;
    335   1.25  augustss 	}
    336   1.25  augustss #endif
    337    1.3  augustss 
    338   1.87   mlelstv 	if (sc->sc_enabled) {
    339   1.87   mlelstv 		sc->sc_enabled = 0;
    340   1.96  jmcneill 		if (!sc->sc_alwayson)
    341  1.103  riastrad 			uhidev_close(sc->sc_hdev);
    342   1.87   mlelstv 	}
    343    1.3  augustss }
    344    1.3  augustss 
    345   1.42  augustss Static int
    346   1.68  christos ums_ioctl(void *v, u_long cmd, void *data, int flag,
    347   1.99  jmcneill     struct lwp *l)
    348   1.16  augustss 
    349    1.3  augustss {
    350   1.77   mbalmer 	struct ums_softc *sc = v;
    351   1.99  jmcneill 	int error;
    352   1.99  jmcneill 
    353   1.99  jmcneill 	if (sc->sc_ms.flags & HIDMS_ABS) {
    354   1.99  jmcneill 		error = tpcalib_ioctl(&sc->sc_ms.sc_tpcalib, cmd, data,
    355   1.99  jmcneill 		    flag, l);
    356   1.99  jmcneill 		if (error != EPASSTHROUGH)
    357   1.99  jmcneill 			return error;
    358   1.99  jmcneill 	}
    359   1.77   mbalmer 
    360    1.3  augustss 	switch (cmd) {
    361    1.3  augustss 	case WSMOUSEIO_GTYPE:
    362   1.91    bouyer 		if (sc->sc_ms.flags & HIDMS_ABS)
    363   1.77   mbalmer 			*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    364   1.77   mbalmer 		else
    365   1.77   mbalmer 			*(u_int *)data = WSMOUSE_TYPE_USB;
    366   1.89     skrll 		return 0;
    367    1.3  augustss 	}
    368   1.16  augustss 
    369   1.89     skrll 	return EPASSTHROUGH;
    370    1.3  augustss }
    371