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