Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.91.4.3
      1  1.91.4.2    martin /*	$NetBSD: ums.c,v 1.91.4.3 2020/04/21 18:42:38 martin 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.91.4.2    martin __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.91.4.3 2020/04/21 18:42:38 martin 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.91.4.2    martin 	bool	sc_alwayson;
     82  1.91.4.2    martin 
     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.91.4.2    martin 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.91.4.2    martin static int ums_match(device_t, cfdata_t, void *);
    100  1.91.4.2    martin static void ums_attach(device_t, device_t, void *);
    101  1.91.4.2    martin static void ums_childdet(device_t, device_t);
    102  1.91.4.2    martin static int ums_detach(device_t, int);
    103  1.91.4.2    martin static int ums_activate(device_t, enum devact);
    104  1.91.4.1  christos 
    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.91.4.2    martin 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.91.4.2    martin 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.91.4.2    martin 	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.91.4.3    martin 		int woffset = 8;
    168      1.88  christos 		/*
    169      1.88  christos 		 * The Microsoft Wireless Laser Mouse 6000 v2.0 and the
    170      1.88  christos 		 * Microsoft Comfort Mouse 2.0 report a bad position for
    171      1.88  christos 		 * the wheel and wheel tilt controls -- should be in bytes
    172      1.88  christos 		 * 3 & 4 of the report. Fix this if necessary.
    173      1.88  christos 		 */
    174      1.89     skrll 		switch (uha->uiaa->uiaa_product) {
    175      1.88  christos 		case USB_PRODUCT_MICROSOFT_24GHZ_XCVR10:
    176      1.88  christos 		case USB_PRODUCT_MICROSOFT_24GHZ_XCVR20:
    177  1.91.4.1  christos 		case USB_PRODUCT_MICROSOFT_NATURAL_6000:
    178      1.88  christos 			fixpos = 24;
    179      1.88  christos 			break;
    180  1.91.4.3    martin 		case USB_PRODUCT_MICROSOFT_24GHZ_XCVR80:
    181  1.91.4.3    martin 			fixpos = 40;
    182  1.91.4.3    martin 			woffset = sc->sc_ms.hidms_loc_z.size;
    183  1.91.4.3    martin 			break;
    184      1.88  christos 		case USB_PRODUCT_MICROSOFT_CM6000:
    185      1.88  christos 			fixpos = 40;
    186      1.88  christos 			break;
    187      1.88  christos 		default:
    188      1.88  christos 			fixpos = 0;
    189      1.88  christos 			break;
    190      1.88  christos 		}
    191      1.88  christos 		if (fixpos) {
    192      1.91    bouyer 			if ((sc->sc_ms.flags & HIDMS_Z) &&
    193      1.91    bouyer 			    sc->sc_ms.hidms_loc_z.pos == 0)
    194      1.91    bouyer 				sc->sc_ms.hidms_loc_z.pos = fixpos;
    195      1.91    bouyer 			if ((sc->sc_ms.flags & HIDMS_W) &&
    196      1.91    bouyer 			    sc->sc_ms.hidms_loc_w.pos == 0)
    197      1.91    bouyer 				sc->sc_ms.hidms_loc_w.pos =
    198  1.91.4.3    martin 				    sc->sc_ms.hidms_loc_z.pos + woffset;
    199      1.88  christos 		}
    200      1.75     rafal 	}
    201      1.63  augustss 
    202  1.91.4.2    martin 	if (uha->uiaa->uiaa_vendor == USB_VENDOR_HAILUCK &&
    203  1.91.4.2    martin 	    uha->uiaa->uiaa_product == USB_PRODUCT_HAILUCK_KEYBOARD) {
    204  1.91.4.2    martin 		/*
    205  1.91.4.2    martin 		 * The HAILUCK USB Keyboard has a built-in touchpad, which
    206  1.91.4.2    martin 		 * needs to be active for the keyboard to function properly.
    207  1.91.4.2    martin 		 */
    208  1.91.4.2    martin 		sc->sc_alwayson = true;
    209  1.91.4.2    martin 	}
    210  1.91.4.2    martin 
    211  1.91.4.2    martin 	tpcalib_init(&sc->sc_ms.sc_tpcalib);
    212      1.91    bouyer 	hidms_attach(self, &sc->sc_ms, &ums_accessops);
    213  1.91.4.2    martin 
    214  1.91.4.2    martin 	if (sc->sc_alwayson) {
    215  1.91.4.2    martin 		error = uhidev_open(&sc->sc_hdev);
    216  1.91.4.2    martin 		if (error != 0) {
    217  1.91.4.2    martin 			aprint_error_dev(self,
    218  1.91.4.2    martin 			    "WARNING: couldn't open always-on device\n");
    219  1.91.4.2    martin 			sc->sc_alwayson = false;
    220  1.91.4.2    martin 		}
    221  1.91.4.2    martin 	}
    222      1.16  augustss }
    223      1.16  augustss 
    224  1.91.4.2    martin static int
    225      1.81    dyoung ums_activate(device_t self, enum devact act)
    226      1.16  augustss {
    227      1.73      cube 	struct ums_softc *sc = device_private(self);
    228      1.28  augustss 
    229      1.28  augustss 	switch (act) {
    230      1.28  augustss 	case DVACT_DEACTIVATE:
    231      1.28  augustss 		sc->sc_dying = 1;
    232      1.76    dyoung 		return 0;
    233      1.76    dyoung 	default:
    234      1.76    dyoung 		return EOPNOTSUPP;
    235      1.28  augustss 	}
    236       1.1  augustss }
    237       1.1  augustss 
    238  1.91.4.2    martin static void
    239      1.71    dyoung ums_childdet(device_t self, device_t child)
    240      1.71    dyoung {
    241      1.71    dyoung 	struct ums_softc *sc = device_private(self);
    242      1.71    dyoung 
    243      1.91    bouyer 	KASSERT(sc->sc_ms.hidms_wsmousedev == child);
    244      1.91    bouyer 	sc->sc_ms.hidms_wsmousedev = NULL;
    245      1.71    dyoung }
    246      1.71    dyoung 
    247  1.91.4.2    martin static int
    248      1.71    dyoung ums_detach(device_t self, int flags)
    249       1.1  augustss {
    250      1.71    dyoung 	struct ums_softc *sc = device_private(self);
    251      1.25  augustss 	int rv = 0;
    252       1.8  augustss 
    253      1.25  augustss 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    254      1.33  augustss 
    255  1.91.4.2    martin 	if (sc->sc_alwayson)
    256  1.91.4.2    martin 		uhidev_close(&sc->sc_hdev);
    257  1.91.4.2    martin 
    258      1.51  augustss 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    259      1.91    bouyer 	if (sc->sc_ms.hidms_wsmousedev != NULL)
    260      1.91    bouyer 		rv = config_detach(sc->sc_ms.hidms_wsmousedev, flags);
    261      1.40  augustss 
    262      1.70  jmcneill 	pmf_device_deregister(self);
    263      1.70  jmcneill 
    264      1.89     skrll 	return rv;
    265       1.1  augustss }
    266       1.1  augustss 
    267  1.91.4.2    martin Static void
    268      1.67  christos ums_intr(struct uhidev *addr, void *ibuf, u_int len)
    269       1.1  augustss {
    270      1.53  augustss 	struct ums_softc *sc = (struct ums_softc *)addr;
    271  1.91.4.2    martin 
    272  1.91.4.2    martin 	if (sc->sc_enabled)
    273  1.91.4.2    martin 		hidms_intr(&sc->sc_ms, ibuf, len);
    274       1.1  augustss }
    275       1.3  augustss 
    276      1.42  augustss Static int
    277      1.44  augustss ums_enable(void *v)
    278       1.3  augustss {
    279       1.3  augustss 	struct ums_softc *sc = v;
    280  1.91.4.2    martin 	int error = 0;
    281      1.16  augustss 
    282      1.25  augustss 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    283      1.28  augustss 
    284      1.28  augustss 	if (sc->sc_dying)
    285      1.89     skrll 		return EIO;
    286      1.28  augustss 
    287       1.3  augustss 	if (sc->sc_enabled)
    288      1.89     skrll 		return EBUSY;
    289       1.3  augustss 
    290       1.3  augustss 	sc->sc_enabled = 1;
    291      1.91    bouyer 	sc->sc_ms.hidms_buttons = 0;
    292       1.3  augustss 
    293  1.91.4.2    martin 	if (!sc->sc_alwayson) {
    294  1.91.4.2    martin 		error = uhidev_open(&sc->sc_hdev);
    295  1.91.4.2    martin 		if (error)
    296  1.91.4.2    martin 			sc->sc_enabled = 0;
    297  1.91.4.2    martin 	}
    298      1.87   mlelstv 
    299      1.87   mlelstv 	return error;
    300       1.3  augustss }
    301       1.3  augustss 
    302      1.42  augustss Static void
    303      1.44  augustss ums_disable(void *v)
    304       1.3  augustss {
    305       1.3  augustss 	struct ums_softc *sc = v;
    306      1.25  augustss 
    307      1.25  augustss 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    308      1.25  augustss #ifdef DIAGNOSTIC
    309      1.25  augustss 	if (!sc->sc_enabled) {
    310      1.25  augustss 		printf("ums_disable: not enabled\n");
    311      1.25  augustss 		return;
    312      1.25  augustss 	}
    313      1.25  augustss #endif
    314       1.3  augustss 
    315      1.87   mlelstv 	if (sc->sc_enabled) {
    316      1.87   mlelstv 		sc->sc_enabled = 0;
    317  1.91.4.2    martin 		if (!sc->sc_alwayson)
    318  1.91.4.2    martin 			uhidev_close(&sc->sc_hdev);
    319      1.87   mlelstv 	}
    320       1.3  augustss }
    321       1.3  augustss 
    322      1.42  augustss Static int
    323      1.68  christos ums_ioctl(void *v, u_long cmd, void *data, int flag,
    324      1.67  christos     struct lwp * p)
    325      1.16  augustss 
    326       1.3  augustss {
    327      1.77   mbalmer 	struct ums_softc *sc = v;
    328      1.77   mbalmer 
    329       1.3  augustss 	switch (cmd) {
    330       1.3  augustss 	case WSMOUSEIO_GTYPE:
    331      1.91    bouyer 		if (sc->sc_ms.flags & HIDMS_ABS)
    332      1.77   mbalmer 			*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    333      1.77   mbalmer 		else
    334      1.77   mbalmer 			*(u_int *)data = WSMOUSE_TYPE_USB;
    335      1.89     skrll 		return 0;
    336       1.3  augustss 	}
    337      1.16  augustss 
    338      1.89     skrll 	return EPASSTHROUGH;
    339       1.3  augustss }
    340