Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.96
      1  1.96  jmcneill /*	$NetBSD: ums.c,v 1.96 2020/01/03 12:39:39 jmcneill 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.96  jmcneill __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.96 2020/01/03 12:39:39 jmcneill 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.53  augustss 	struct uhidev sc_hdev;
     79  1.91    bouyer 	struct hidms sc_ms;
     80  1.53  augustss 
     81  1.96  jmcneill 	bool	sc_alwayson;
     82  1.96  jmcneill 
     83  1.91    bouyer 	int	sc_enabled;
     84  1.91    bouyer 	char	sc_dying;
     85  1.84  christos };
     86  1.84  christos 
     87  1.89     skrll Static void ums_intr(struct uhidev *, void *, u_int);
     88   1.1  augustss 
     89  1.44  augustss Static int	ums_enable(void *);
     90  1.44  augustss Static void	ums_disable(void *);
     91  1.89     skrll Static int	ums_ioctl(void *, u_long, void *, int, struct lwp *);
     92   1.3  augustss 
     93  1.95      maxv static const struct wsmouse_accessops ums_accessops = {
     94   1.3  augustss 	ums_enable,
     95   1.3  augustss 	ums_ioctl,
     96   1.3  augustss 	ums_disable,
     97   1.3  augustss };
     98   1.3  augustss 
     99  1.95      maxv static int ums_match(device_t, cfdata_t, void *);
    100  1.95      maxv static void ums_attach(device_t, device_t, void *);
    101  1.95      maxv static void ums_childdet(device_t, device_t);
    102  1.95      maxv static int ums_detach(device_t, int);
    103  1.95      maxv static int ums_activate(device_t, enum devact);
    104  1.93       mrg 
    105  1.73      cube CFATTACH_DECL2_NEW(ums, sizeof(struct ums_softc), ums_match, ums_attach,
    106  1.71    dyoung     ums_detach, ums_activate, NULL, ums_childdet);
    107   1.1  augustss 
    108  1.95      maxv static int
    109  1.73      cube ums_match(device_t parent, cfdata_t match, void *aux)
    110   1.1  augustss {
    111  1.53  augustss 	struct uhidev_attach_arg *uha = aux;
    112  1.53  augustss 	int size;
    113   1.1  augustss 	void *desc;
    114  1.57  augustss 
    115  1.79  jakllsch 	/*
    116  1.79  jakllsch 	 * Some (older) Griffin PowerMate knobs may masquerade as a
    117  1.79  jakllsch 	 * mouse, avoid treating them as such, they have only one axis.
    118  1.79  jakllsch 	 */
    119  1.89     skrll 	if (uha->uiaa->uiaa_vendor == USB_VENDOR_GRIFFIN &&
    120  1.89     skrll 	    uha->uiaa->uiaa_product == USB_PRODUCT_GRIFFIN_POWERMATE)
    121  1.89     skrll 		return UMATCH_NONE;
    122  1.79  jakllsch 
    123  1.53  augustss 	uhidev_get_report_desc(uha->parent, &desc, &size);
    124  1.53  augustss 	if (!hid_is_collection(desc, size, uha->reportid,
    125  1.78  jakllsch 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)) &&
    126  1.78  jakllsch 	    !hid_is_collection(desc, size, uha->reportid,
    127  1.84  christos 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_POINTER)) &&
    128  1.84  christos 	    !hid_is_collection(desc, size, uha->reportid,
    129  1.89     skrll 			       HID_USAGE2(HUP_DIGITIZERS, 0x0002)))
    130  1.89     skrll 		return UMATCH_NONE;
    131   1.1  augustss 
    132  1.89     skrll 	return UMATCH_IFACECLASS;
    133   1.1  augustss }
    134   1.1  augustss 
    135  1.95      maxv static void
    136  1.73      cube ums_attach(device_t parent, device_t self, void *aux)
    137   1.1  augustss {
    138  1.73      cube 	struct ums_softc *sc = device_private(self);
    139  1.53  augustss 	struct uhidev_attach_arg *uha = aux;
    140  1.96  jmcneill 	int size, error;
    141   1.1  augustss 	void *desc;
    142  1.91    bouyer 	uint32_t quirks;
    143   1.1  augustss 
    144  1.69  jmcneill 	aprint_naive("\n");
    145  1.69  jmcneill 
    146  1.73      cube 	sc->sc_hdev.sc_dev = self;
    147  1.53  augustss 	sc->sc_hdev.sc_intr = ums_intr;
    148  1.53  augustss 	sc->sc_hdev.sc_parent = uha->parent;
    149  1.53  augustss 	sc->sc_hdev.sc_report_id = uha->reportid;
    150   1.1  augustss 
    151  1.53  augustss 	quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
    152  1.35  augustss 	if (quirks & UQ_MS_REVZ)
    153  1.91    bouyer 		sc->sc_ms.flags |= HIDMS_REVZ;
    154  1.35  augustss 	if (quirks & UQ_SPUR_BUT_UP)
    155  1.91    bouyer 		sc->sc_ms.flags |= HIDMS_SPUR_BUT_UP;
    156  1.84  christos 
    157  1.70  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    158  1.70  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    159  1.70  jmcneill 
    160  1.91    bouyer 	uhidev_get_report_desc(uha->parent, &desc, &size);
    161  1.10  augustss 
    162  1.91    bouyer 	if (!hidms_setup(self, &sc->sc_ms, uha->reportid, desc, size))
    163  1.81    dyoung 		return;
    164   1.6  augustss 
    165  1.89     skrll 	if (uha->uiaa->uiaa_vendor == USB_VENDOR_MICROSOFT) {
    166  1.88  christos 		int fixpos;
    167  1.88  christos 		/*
    168  1.88  christos 		 * The Microsoft Wireless Laser Mouse 6000 v2.0 and the
    169  1.88  christos 		 * Microsoft Comfort Mouse 2.0 report a bad position for
    170  1.88  christos 		 * the wheel and wheel tilt controls -- should be in bytes
    171  1.88  christos 		 * 3 & 4 of the report. Fix this if necessary.
    172  1.88  christos 		 */
    173  1.89     skrll 		switch (uha->uiaa->uiaa_product) {
    174  1.88  christos 		case USB_PRODUCT_MICROSOFT_24GHZ_XCVR10:
    175  1.88  christos 		case USB_PRODUCT_MICROSOFT_24GHZ_XCVR20:
    176  1.92      maya 		case USB_PRODUCT_MICROSOFT_NATURAL_6000:
    177  1.88  christos 			fixpos = 24;
    178  1.88  christos 			break;
    179  1.88  christos 		case USB_PRODUCT_MICROSOFT_CM6000:
    180  1.88  christos 			fixpos = 40;
    181  1.88  christos 			break;
    182  1.88  christos 		default:
    183  1.88  christos 			fixpos = 0;
    184  1.88  christos 			break;
    185  1.88  christos 		}
    186  1.88  christos 		if (fixpos) {
    187  1.91    bouyer 			if ((sc->sc_ms.flags & HIDMS_Z) &&
    188  1.91    bouyer 			    sc->sc_ms.hidms_loc_z.pos == 0)
    189  1.91    bouyer 				sc->sc_ms.hidms_loc_z.pos = fixpos;
    190  1.91    bouyer 			if ((sc->sc_ms.flags & HIDMS_W) &&
    191  1.91    bouyer 			    sc->sc_ms.hidms_loc_w.pos == 0)
    192  1.91    bouyer 				sc->sc_ms.hidms_loc_w.pos =
    193  1.91    bouyer 				    sc->sc_ms.hidms_loc_z.pos + 8;
    194  1.88  christos 		}
    195  1.75     rafal 	}
    196  1.63  augustss 
    197  1.96  jmcneill 	if (uha->uiaa->uiaa_vendor == USB_VENDOR_HAILUCK &&
    198  1.96  jmcneill 	    uha->uiaa->uiaa_product == USB_PRODUCT_HAILUCK_KEYBOARD) {
    199  1.96  jmcneill 		/*
    200  1.96  jmcneill 		 * The HAILUCK USB Keyboard has a built-in touchpad, which
    201  1.96  jmcneill 		 * needs to be active for the keyboard to function properly.
    202  1.96  jmcneill 		 */
    203  1.96  jmcneill 		sc->sc_alwayson = true;
    204  1.96  jmcneill 	}
    205  1.96  jmcneill 
    206  1.94    yhardy 	tpcalib_init(&sc->sc_ms.sc_tpcalib);
    207  1.91    bouyer 	hidms_attach(self, &sc->sc_ms, &ums_accessops);
    208  1.96  jmcneill 
    209  1.96  jmcneill 	if (sc->sc_alwayson) {
    210  1.96  jmcneill 		error = uhidev_open(&sc->sc_hdev);
    211  1.96  jmcneill 		if (error != 0) {
    212  1.96  jmcneill 			aprint_error_dev(self,
    213  1.96  jmcneill 			    "WARNING: couldn't open always-on device\n");
    214  1.96  jmcneill 			sc->sc_alwayson = false;
    215  1.96  jmcneill 		}
    216  1.96  jmcneill 	}
    217  1.16  augustss }
    218  1.16  augustss 
    219  1.95      maxv static int
    220  1.81    dyoung ums_activate(device_t self, enum devact act)
    221  1.16  augustss {
    222  1.73      cube 	struct ums_softc *sc = device_private(self);
    223  1.28  augustss 
    224  1.28  augustss 	switch (act) {
    225  1.28  augustss 	case DVACT_DEACTIVATE:
    226  1.28  augustss 		sc->sc_dying = 1;
    227  1.76    dyoung 		return 0;
    228  1.76    dyoung 	default:
    229  1.76    dyoung 		return EOPNOTSUPP;
    230  1.28  augustss 	}
    231   1.1  augustss }
    232   1.1  augustss 
    233  1.95      maxv static void
    234  1.71    dyoung ums_childdet(device_t self, device_t child)
    235  1.71    dyoung {
    236  1.71    dyoung 	struct ums_softc *sc = device_private(self);
    237  1.71    dyoung 
    238  1.91    bouyer 	KASSERT(sc->sc_ms.hidms_wsmousedev == child);
    239  1.91    bouyer 	sc->sc_ms.hidms_wsmousedev = NULL;
    240  1.71    dyoung }
    241  1.71    dyoung 
    242  1.95      maxv static int
    243  1.71    dyoung ums_detach(device_t self, int flags)
    244   1.1  augustss {
    245  1.71    dyoung 	struct ums_softc *sc = device_private(self);
    246  1.25  augustss 	int rv = 0;
    247   1.8  augustss 
    248  1.25  augustss 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    249  1.33  augustss 
    250  1.96  jmcneill 	if (sc->sc_alwayson)
    251  1.96  jmcneill 		uhidev_close(&sc->sc_hdev);
    252  1.96  jmcneill 
    253  1.51  augustss 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    254  1.91    bouyer 	if (sc->sc_ms.hidms_wsmousedev != NULL)
    255  1.91    bouyer 		rv = config_detach(sc->sc_ms.hidms_wsmousedev, flags);
    256  1.40  augustss 
    257  1.70  jmcneill 	pmf_device_deregister(self);
    258  1.70  jmcneill 
    259  1.89     skrll 	return rv;
    260   1.1  augustss }
    261   1.1  augustss 
    262  1.95      maxv Static void
    263  1.67  christos ums_intr(struct uhidev *addr, void *ibuf, u_int len)
    264   1.1  augustss {
    265  1.53  augustss 	struct ums_softc *sc = (struct ums_softc *)addr;
    266  1.96  jmcneill 
    267  1.96  jmcneill 	if (sc->sc_enabled)
    268  1.96  jmcneill 		hidms_intr(&sc->sc_ms, ibuf, len);
    269   1.1  augustss }
    270   1.3  augustss 
    271  1.42  augustss Static int
    272  1.44  augustss ums_enable(void *v)
    273   1.3  augustss {
    274   1.3  augustss 	struct ums_softc *sc = v;
    275  1.96  jmcneill 	int error = 0;
    276  1.16  augustss 
    277  1.25  augustss 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    278  1.28  augustss 
    279  1.28  augustss 	if (sc->sc_dying)
    280  1.89     skrll 		return EIO;
    281  1.28  augustss 
    282   1.3  augustss 	if (sc->sc_enabled)
    283  1.89     skrll 		return EBUSY;
    284   1.3  augustss 
    285   1.3  augustss 	sc->sc_enabled = 1;
    286  1.91    bouyer 	sc->sc_ms.hidms_buttons = 0;
    287   1.3  augustss 
    288  1.96  jmcneill 	if (!sc->sc_alwayson) {
    289  1.96  jmcneill 		error = uhidev_open(&sc->sc_hdev);
    290  1.96  jmcneill 		if (error)
    291  1.96  jmcneill 			sc->sc_enabled = 0;
    292  1.96  jmcneill 	}
    293  1.87   mlelstv 
    294  1.87   mlelstv 	return error;
    295   1.3  augustss }
    296   1.3  augustss 
    297  1.42  augustss Static void
    298  1.44  augustss ums_disable(void *v)
    299   1.3  augustss {
    300   1.3  augustss 	struct ums_softc *sc = v;
    301  1.25  augustss 
    302  1.25  augustss 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    303  1.25  augustss #ifdef DIAGNOSTIC
    304  1.25  augustss 	if (!sc->sc_enabled) {
    305  1.25  augustss 		printf("ums_disable: not enabled\n");
    306  1.25  augustss 		return;
    307  1.25  augustss 	}
    308  1.25  augustss #endif
    309   1.3  augustss 
    310  1.87   mlelstv 	if (sc->sc_enabled) {
    311  1.87   mlelstv 		sc->sc_enabled = 0;
    312  1.96  jmcneill 		if (!sc->sc_alwayson)
    313  1.96  jmcneill 			uhidev_close(&sc->sc_hdev);
    314  1.87   mlelstv 	}
    315   1.3  augustss }
    316   1.3  augustss 
    317  1.42  augustss Static int
    318  1.68  christos ums_ioctl(void *v, u_long cmd, void *data, int flag,
    319  1.67  christos     struct lwp * p)
    320  1.16  augustss 
    321   1.3  augustss {
    322  1.77   mbalmer 	struct ums_softc *sc = v;
    323  1.77   mbalmer 
    324   1.3  augustss 	switch (cmd) {
    325   1.3  augustss 	case WSMOUSEIO_GTYPE:
    326  1.91    bouyer 		if (sc->sc_ms.flags & HIDMS_ABS)
    327  1.77   mbalmer 			*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    328  1.77   mbalmer 		else
    329  1.77   mbalmer 			*(u_int *)data = WSMOUSE_TYPE_USB;
    330  1.89     skrll 		return 0;
    331   1.3  augustss 	}
    332  1.16  augustss 
    333  1.89     skrll 	return EPASSTHROUGH;
    334   1.3  augustss }
    335