Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.85
      1  1.85  christos /*	$NetBSD: ums.c,v 1.85 2013/01/05 01:30:17 christos Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4   1.1  augustss  * Copyright (c) 1998 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.85  christos __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.85 2013/01/05 01:30:17 christos Exp $");
     39  1.85  christos 
     40  1.85  christos #ifdef _KERNEL_OPT
     41  1.85  christos #include "opt_usb.h"
     42  1.85  christos #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/malloc.h>
     48   1.1  augustss #include <sys/device.h>
     49   1.1  augustss #include <sys/ioctl.h>
     50   1.1  augustss #include <sys/file.h>
     51   1.1  augustss #include <sys/select.h>
     52   1.1  augustss #include <sys/proc.h>
     53   1.1  augustss #include <sys/vnode.h>
     54   1.1  augustss #include <sys/poll.h>
     55   1.1  augustss 
     56   1.1  augustss #include <dev/usb/usb.h>
     57   1.1  augustss #include <dev/usb/usbhid.h>
     58   1.1  augustss 
     59   1.1  augustss #include <dev/usb/usbdi.h>
     60   1.1  augustss #include <dev/usb/usbdi_util.h>
     61   1.1  augustss #include <dev/usb/usbdevs.h>
     62   1.1  augustss #include <dev/usb/usb_quirks.h>
     63  1.53  augustss #include <dev/usb/uhidev.h>
     64   1.1  augustss #include <dev/usb/hid.h>
     65   1.1  augustss 
     66   1.3  augustss #include <dev/wscons/wsconsio.h>
     67   1.3  augustss #include <dev/wscons/wsmousevar.h>
     68   1.3  augustss 
     69   1.1  augustss #ifdef USB_DEBUG
     70  1.81    dyoung #define DPRINTF(x)	if (umsdebug) printf x
     71  1.81    dyoung #define DPRINTFN(n,x)	if (umsdebug>(n)) printf x
     72   1.1  augustss int	umsdebug = 0;
     73   1.1  augustss #else
     74   1.1  augustss #define DPRINTF(x)
     75   1.1  augustss #define DPRINTFN(n,x)
     76   1.1  augustss #endif
     77   1.1  augustss 
     78  1.27  augustss #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
     79  1.27  augustss 
     80  1.27  augustss #define UMSUNIT(s)	(minor(s))
     81  1.16  augustss 
     82  1.16  augustss #define PS2LBUTMASK	x01
     83  1.16  augustss #define PS2RBUTMASK	x02
     84  1.16  augustss #define PS2MBUTMASK	x04
     85   1.1  augustss #define PS2BUTMASK 0x0f
     86   1.1  augustss 
     87  1.61  jmcneill #define MAX_BUTTONS	31	/* must not exceed size of sc_buttons */
     88  1.53  augustss 
     89   1.1  augustss struct ums_softc {
     90  1.53  augustss 	struct uhidev sc_hdev;
     91  1.53  augustss 
     92  1.63  augustss 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z, sc_loc_w;
     93  1.53  augustss 	struct hid_location sc_loc_btn[MAX_BUTTONS];
     94   1.1  augustss 
     95  1.16  augustss 	int sc_enabled;
     96   1.3  augustss 
     97  1.84  christos 	u_int flags;		/* device configuration */
     98  1.84  christos #define UMS_Z			0x001	/* z direction available */
     99  1.84  christos #define UMS_SPUR_BUT_UP		0x002	/* spurious button up events */
    100  1.84  christos #define UMS_REVZ		0x004	/* Z-axis is reversed */
    101  1.84  christos #define UMS_W			0x008	/* w direction/tilt available */
    102  1.84  christos #define UMS_ABS			0x010	/* absolute position, touchpanel */
    103  1.84  christos #define UMS_TIP_SWITCH  	0x020	/* digitizer tip switch */
    104  1.84  christos #define UMS_SEC_TIP_SWITCH 	0x040	/* digitizer secondary tip switch */
    105  1.84  christos #define UMS_BARREL_SWITCH 	0x080	/* digitizer barrel switch */
    106  1.84  christos #define UMS_ERASER 		0x100	/* digitizer eraser */
    107  1.35  augustss 
    108  1.16  augustss 	int nbuttons;
    109  1.16  augustss 
    110  1.27  augustss 	u_int32_t sc_buttons;	/* mouse button status */
    111  1.71    dyoung 	device_t sc_wsmousedev;
    112  1.28  augustss 
    113  1.28  augustss 	char			sc_dying;
    114   1.1  augustss };
    115   1.1  augustss 
    116  1.84  christos static const struct {
    117  1.84  christos 	u_int feature;
    118  1.84  christos 	u_int flag;
    119  1.84  christos } digbut[] = {
    120  1.84  christos 	{ HUD_TIP_SWITCH, UMS_TIP_SWITCH },
    121  1.84  christos 	{ HUD_SEC_TIP_SWITCH, UMS_SEC_TIP_SWITCH },
    122  1.84  christos 	{ HUD_BARREL_SWITCH, UMS_BARREL_SWITCH },
    123  1.84  christos 	{ HUD_ERASER, UMS_ERASER },
    124  1.84  christos };
    125  1.84  christos 
    126   1.2  augustss #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
    127   1.2  augustss 
    128  1.53  augustss Static void ums_intr(struct uhidev *addr, void *ibuf, u_int len);
    129   1.1  augustss 
    130  1.44  augustss Static int	ums_enable(void *);
    131  1.44  augustss Static void	ums_disable(void *);
    132  1.68  christos Static int	ums_ioctl(void *, u_long, void *, int, struct lwp * );
    133   1.3  augustss 
    134   1.3  augustss const struct wsmouse_accessops ums_accessops = {
    135   1.3  augustss 	ums_enable,
    136   1.3  augustss 	ums_ioctl,
    137   1.3  augustss 	ums_disable,
    138   1.3  augustss };
    139   1.3  augustss 
    140  1.73      cube int ums_match(device_t, cfdata_t, void *);
    141  1.71    dyoung void ums_attach(device_t, device_t, void *);
    142  1.71    dyoung void ums_childdet(device_t, device_t);
    143  1.71    dyoung int ums_detach(device_t, int);
    144  1.71    dyoung int ums_activate(device_t, enum devact);
    145  1.71    dyoung extern struct cfdriver ums_cd;
    146  1.73      cube CFATTACH_DECL2_NEW(ums, sizeof(struct ums_softc), ums_match, ums_attach,
    147  1.71    dyoung     ums_detach, ums_activate, NULL, ums_childdet);
    148   1.1  augustss 
    149  1.53  augustss int
    150  1.73      cube ums_match(device_t parent, cfdata_t match, void *aux)
    151   1.1  augustss {
    152  1.53  augustss 	struct uhidev_attach_arg *uha = aux;
    153  1.53  augustss 	int size;
    154   1.1  augustss 	void *desc;
    155  1.57  augustss 
    156  1.79  jakllsch 	/*
    157  1.79  jakllsch 	 * Some (older) Griffin PowerMate knobs may masquerade as a
    158  1.79  jakllsch 	 * mouse, avoid treating them as such, they have only one axis.
    159  1.79  jakllsch 	 */
    160  1.79  jakllsch 	if (uha->uaa->vendor == USB_VENDOR_GRIFFIN &&
    161  1.79  jakllsch 	    uha->uaa->product == USB_PRODUCT_GRIFFIN_POWERMATE)
    162  1.79  jakllsch 		return (UMATCH_NONE);
    163  1.79  jakllsch 
    164  1.53  augustss 	uhidev_get_report_desc(uha->parent, &desc, &size);
    165  1.53  augustss 	if (!hid_is_collection(desc, size, uha->reportid,
    166  1.78  jakllsch 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)) &&
    167  1.78  jakllsch 	    !hid_is_collection(desc, size, uha->reportid,
    168  1.84  christos 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_POINTER)) &&
    169  1.84  christos 	    !hid_is_collection(desc, size, uha->reportid,
    170  1.84  christos                                HID_USAGE2(HUP_DIGITIZERS, 0x0002)))
    171   1.1  augustss 		return (UMATCH_NONE);
    172   1.1  augustss 
    173  1.53  augustss 	return (UMATCH_IFACECLASS);
    174   1.1  augustss }
    175   1.1  augustss 
    176  1.53  augustss void
    177  1.73      cube ums_attach(device_t parent, device_t self, void *aux)
    178   1.1  augustss {
    179  1.73      cube 	struct ums_softc *sc = device_private(self);
    180  1.53  augustss 	struct uhidev_attach_arg *uha = aux;
    181   1.3  augustss 	struct wsmousedev_attach_args a;
    182   1.1  augustss 	int size;
    183   1.1  augustss 	void *desc;
    184  1.35  augustss 	u_int32_t flags, quirks;
    185  1.75     rafal 	int i, hl;
    186  1.75     rafal 	struct hid_location *zloc;
    187  1.84  christos 	bool isdigitizer;
    188   1.1  augustss 
    189  1.69  jmcneill 	aprint_naive("\n");
    190  1.69  jmcneill 
    191  1.73      cube 	sc->sc_hdev.sc_dev = self;
    192  1.53  augustss 	sc->sc_hdev.sc_intr = ums_intr;
    193  1.53  augustss 	sc->sc_hdev.sc_parent = uha->parent;
    194  1.53  augustss 	sc->sc_hdev.sc_report_id = uha->reportid;
    195   1.1  augustss 
    196  1.53  augustss 	quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
    197  1.35  augustss 	if (quirks & UQ_MS_REVZ)
    198  1.35  augustss 		sc->flags |= UMS_REVZ;
    199  1.35  augustss 	if (quirks & UQ_SPUR_BUT_UP)
    200  1.35  augustss 		sc->flags |= UMS_SPUR_BUT_UP;
    201  1.24  augustss 
    202  1.53  augustss 	uhidev_get_report_desc(uha->parent, &desc, &size);
    203  1.16  augustss 
    204  1.84  christos 	isdigitizer = hid_is_collection(desc, size, uha->reportid,
    205  1.84  christos 	    HID_USAGE2(HUP_DIGITIZERS, 0x0002));
    206  1.84  christos 
    207  1.70  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    208  1.70  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    209  1.70  jmcneill 
    210   1.2  augustss 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    211  1.53  augustss 	       uha->reportid, hid_input, &sc->sc_loc_x, &flags)) {
    212  1.69  jmcneill 		aprint_error("\n%s: mouse has no X report\n",
    213  1.81    dyoung 		       device_xname(sc->sc_hdev.sc_dev));
    214  1.81    dyoung 		return;
    215  1.16  augustss 	}
    216  1.77   mbalmer 	switch (flags & MOUSE_FLAGS_MASK) {
    217  1.77   mbalmer 	case 0:
    218  1.77   mbalmer 		sc->flags |= UMS_ABS;
    219  1.77   mbalmer 		break;
    220  1.77   mbalmer 	case HIO_RELATIVE:
    221  1.77   mbalmer 		break;
    222  1.77   mbalmer 	default:
    223  1.69  jmcneill 		aprint_error("\n%s: X report 0x%04x not supported\n",
    224  1.81    dyoung 		       device_xname(sc->sc_hdev.sc_dev), flags);
    225  1.81    dyoung 		return;
    226   1.1  augustss 	}
    227  1.10  augustss 
    228   1.2  augustss 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    229  1.53  augustss 	       uha->reportid, hid_input, &sc->sc_loc_y, &flags)) {
    230  1.69  jmcneill 		aprint_error("\n%s: mouse has no Y report\n",
    231  1.81    dyoung 		       device_xname(sc->sc_hdev.sc_dev));
    232  1.81    dyoung 		return;
    233  1.16  augustss 	}
    234  1.77   mbalmer 	switch (flags & MOUSE_FLAGS_MASK) {
    235  1.77   mbalmer 	case 0:
    236  1.77   mbalmer 		sc->flags |= UMS_ABS;
    237  1.77   mbalmer 		break;
    238  1.77   mbalmer 	case HIO_RELATIVE:
    239  1.77   mbalmer 		break;
    240  1.77   mbalmer 	default:
    241  1.69  jmcneill 		aprint_error("\n%s: Y report 0x%04x not supported\n",
    242  1.81    dyoung 		       device_xname(sc->sc_hdev.sc_dev), flags);
    243  1.81    dyoung 		return;
    244   1.1  augustss 	}
    245  1.10  augustss 
    246  1.63  augustss 	/* Try the wheel first as the Z activator since it's tradition. */
    247  1.85  christos 	hl = hid_locate(desc,
    248  1.85  christos 			size,
    249  1.85  christos 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
    250  1.85  christos 			uha->reportid,
    251  1.85  christos 			hid_input,
    252  1.85  christos 			&sc->sc_loc_z,
    253  1.75     rafal 			&flags);
    254  1.75     rafal 
    255  1.75     rafal 	zloc = &sc->sc_loc_z;
    256  1.75     rafal 	if (hl) {
    257  1.77   mbalmer 		if ((flags & MOUSE_FLAGS_MASK) != HIO_RELATIVE) {
    258  1.69  jmcneill 			aprint_verbose("\n%s: Wheel report 0x%04x not "
    259  1.81    dyoung 			    "supported\n", device_xname(sc->sc_hdev.sc_dev),
    260  1.69  jmcneill 			    flags);
    261  1.16  augustss 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    262  1.16  augustss 		} else {
    263  1.16  augustss 			sc->flags |= UMS_Z;
    264  1.37  augustss 			/* Wheels need the Z axis reversed. */
    265  1.63  augustss 			sc->flags ^= UMS_REVZ;
    266  1.75     rafal 			/* Put Z on the W coordinate */
    267  1.75     rafal 			zloc = &sc->sc_loc_w;
    268  1.63  augustss 		}
    269  1.75     rafal 	}
    270  1.75     rafal 
    271  1.85  christos 	hl = hid_locate(desc,
    272  1.85  christos 			size,
    273  1.75     rafal 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    274  1.75     rafal 			uha->reportid,
    275  1.75     rafal 			hid_input,
    276  1.75     rafal 			zloc,
    277  1.75     rafal 			&flags);
    278  1.75     rafal 
    279  1.75     rafal 	/*
    280  1.75     rafal 	 * The horizontal component of the scrollball can also be given by
    281  1.75     rafal 	 * Application Control Pan in the Consumer page, so if we didnt see
    282  1.75     rafal 	 * any Z then check that.
    283  1.75     rafal 	 */
    284  1.75     rafal 	if (!hl) {
    285  1.85  christos 		hl = hid_locate(desc,
    286  1.85  christos 				size,
    287  1.85  christos 				HID_USAGE2(HUP_CONSUMER, HUC_AC_PAN),
    288  1.75     rafal 				uha->reportid,
    289  1.75     rafal 				hid_input,
    290  1.75     rafal 				zloc,
    291  1.75     rafal 				&flags);
    292  1.75     rafal 	}
    293  1.75     rafal 
    294  1.75     rafal 	if (hl) {
    295  1.77   mbalmer 		if ((flags & MOUSE_FLAGS_MASK) != HIO_RELATIVE) {
    296  1.69  jmcneill 			aprint_verbose("\n%s: Z report 0x%04x not supported\n",
    297  1.81    dyoung 			       device_xname(sc->sc_hdev.sc_dev), flags);
    298  1.75     rafal 			zloc->size = 0;	/* Bad Z coord, ignore it */
    299  1.63  augustss 		} else {
    300  1.75     rafal 			if (sc->flags & UMS_Z)
    301  1.75     rafal 				sc->flags |= UMS_W;
    302  1.75     rafal 			else
    303  1.75     rafal 				sc->flags |= UMS_Z;
    304  1.16  augustss 		}
    305   1.6  augustss 	}
    306   1.6  augustss 
    307  1.75     rafal 	/*
    308  1.75     rafal 	 * The Microsoft Wireless Laser Mouse 6000 v2.0 reports a bad
    309  1.75     rafal 	 * position for the wheel and wheel tilt controls -- should be
    310  1.75     rafal 	 * in bytes 3 & 4 of the report.  Fix this if necessary.
    311  1.75     rafal 	 */
    312  1.75     rafal 	if (uha->uaa->vendor == USB_VENDOR_MICROSOFT &&
    313  1.80  matthias 	    (uha->uaa->product == USB_PRODUCT_MICROSOFT_24GHZ_XCVR10 ||
    314  1.85  christos 	     uha->uaa->product == USB_PRODUCT_MICROSOFT_24GHZ_XCVR20)) {
    315  1.75     rafal 		if ((sc->flags & UMS_Z) && sc->sc_loc_z.pos == 0)
    316  1.75     rafal 			sc->sc_loc_z.pos = 24;
    317  1.75     rafal 		if ((sc->flags & UMS_W) && sc->sc_loc_w.pos == 0)
    318  1.75     rafal 			sc->sc_loc_w.pos = sc->sc_loc_z.pos + 8;
    319  1.75     rafal 	}
    320  1.63  augustss 
    321  1.21  augustss 	/* figure out the number of buttons */
    322  1.21  augustss 	for (i = 1; i <= MAX_BUTTONS; i++)
    323  1.16  augustss 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    324  1.84  christos 		    uha->reportid, hid_input, &sc->sc_loc_btn[i - 1], 0))
    325  1.16  augustss 			break;
    326  1.84  christos 
    327  1.84  christos 	if (isdigitizer) {
    328  1.84  christos 		for (size_t j = 0; j < __arraycount(digbut); j++) {
    329  1.85  christos 			if (hid_locate(desc, size, HID_USAGE2(HUP_DIGITIZERS,
    330  1.84  christos 			    digbut[j].feature), uha->reportid, hid_input,
    331  1.84  christos 			    &sc->sc_loc_btn[i - 1], 0)) {
    332  1.84  christos 				if (i <= MAX_BUTTONS) {
    333  1.84  christos 					i++;
    334  1.84  christos 					sc->flags |= digbut[j].flag;
    335  1.84  christos 				} else
    336  1.84  christos 					aprint_error_dev(self,
    337  1.84  christos 					    "ran out of buttons\n");
    338  1.84  christos 			}
    339  1.84  christos 		}
    340  1.84  christos 	}
    341  1.16  augustss 	sc->nbuttons = i - 1;
    342  1.16  augustss 
    343  1.84  christos 	aprint_normal(": %d button%s%s%s%s%s%s%s%s%s\n",
    344  1.45  augustss 	    sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
    345  1.75     rafal 	    sc->flags & UMS_W ? ", W" : "",
    346  1.75     rafal 	    sc->flags & UMS_Z ? " and Z dir" : "",
    347  1.84  christos 	    sc->flags & UMS_W ? "s" : "",
    348  1.84  christos 	    isdigitizer ? " digitizer"  : "",
    349  1.84  christos 	    sc->flags & UMS_TIP_SWITCH ? ", tip" : "",
    350  1.84  christos 	    sc->flags & UMS_SEC_TIP_SWITCH ? ", sec tip" : "",
    351  1.84  christos 	    sc->flags & UMS_BARREL_SWITCH ? ", barrel" : "",
    352  1.84  christos 	    sc->flags & UMS_ERASER ? ", eraser" : "");
    353   1.3  augustss 
    354  1.16  augustss #ifdef USB_DEBUG
    355  1.16  augustss 	DPRINTF(("ums_attach: sc=%p\n", sc));
    356  1.57  augustss 	DPRINTF(("ums_attach: X\t%d/%d\n",
    357  1.16  augustss 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    358  1.57  augustss 	DPRINTF(("ums_attach: Y\t%d/%d\n",
    359  1.48  augustss 		 sc->sc_loc_y.pos, sc->sc_loc_y.size));
    360  1.16  augustss 	if (sc->flags & UMS_Z)
    361  1.57  augustss 		DPRINTF(("ums_attach: Z\t%d/%d\n",
    362  1.16  augustss 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
    363  1.75     rafal 	if (sc->flags & UMS_W)
    364  1.75     rafal 		DPRINTF(("ums_attach: W\t%d/%d\n",
    365  1.75     rafal 			 sc->sc_loc_w.pos, sc->sc_loc_w.size));
    366  1.16  augustss 	for (i = 1; i <= sc->nbuttons; i++) {
    367  1.16  augustss 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
    368  1.16  augustss 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
    369  1.16  augustss 	}
    370  1.16  augustss #endif
    371  1.16  augustss 
    372   1.3  augustss 	a.accessops = &ums_accessops;
    373   1.3  augustss 	a.accesscookie = sc;
    374   1.3  augustss 
    375  1.47  augustss 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    376  1.40  augustss 
    377  1.81    dyoung 	return;
    378  1.16  augustss }
    379  1.16  augustss 
    380  1.25  augustss int
    381  1.81    dyoung ums_activate(device_t self, enum devact act)
    382  1.16  augustss {
    383  1.73      cube 	struct ums_softc *sc = device_private(self);
    384  1.28  augustss 
    385  1.28  augustss 	switch (act) {
    386  1.28  augustss 	case DVACT_DEACTIVATE:
    387  1.28  augustss 		sc->sc_dying = 1;
    388  1.76    dyoung 		return 0;
    389  1.76    dyoung 	default:
    390  1.76    dyoung 		return EOPNOTSUPP;
    391  1.28  augustss 	}
    392   1.1  augustss }
    393   1.1  augustss 
    394  1.71    dyoung void
    395  1.71    dyoung ums_childdet(device_t self, device_t child)
    396  1.71    dyoung {
    397  1.71    dyoung 	struct ums_softc *sc = device_private(self);
    398  1.71    dyoung 
    399  1.71    dyoung 	KASSERT(sc->sc_wsmousedev == child);
    400  1.71    dyoung 	sc->sc_wsmousedev = NULL;
    401  1.71    dyoung }
    402  1.71    dyoung 
    403  1.53  augustss int
    404  1.71    dyoung ums_detach(device_t self, int flags)
    405   1.1  augustss {
    406  1.71    dyoung 	struct ums_softc *sc = device_private(self);
    407  1.25  augustss 	int rv = 0;
    408   1.8  augustss 
    409  1.25  augustss 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    410  1.33  augustss 
    411  1.51  augustss 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    412  1.51  augustss 	if (sc->sc_wsmousedev != NULL)
    413  1.51  augustss 		rv = config_detach(sc->sc_wsmousedev, flags);
    414  1.40  augustss 
    415  1.70  jmcneill 	pmf_device_deregister(self);
    416  1.70  jmcneill 
    417  1.25  augustss 	return (rv);
    418   1.1  augustss }
    419   1.1  augustss 
    420   1.1  augustss void
    421  1.67  christos ums_intr(struct uhidev *addr, void *ibuf, u_int len)
    422   1.1  augustss {
    423  1.53  augustss 	struct ums_softc *sc = (struct ums_softc *)addr;
    424  1.63  augustss 	int dx, dy, dz, dw;
    425  1.27  augustss 	u_int32_t buttons = 0;
    426  1.77   mbalmer 	int i, flags, s;
    427  1.54  augustss 
    428  1.54  augustss 	DPRINTFN(5,("ums_intr: len=%d\n", len));
    429  1.21  augustss 
    430  1.77   mbalmer 	flags = WSMOUSE_INPUT_DELTA;	/* equals 0 */
    431  1.77   mbalmer 
    432   1.1  augustss 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    433  1.77   mbalmer 	if (sc->flags & UMS_ABS) {
    434  1.77   mbalmer 		flags |= (WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
    435  1.77   mbalmer 		dy = hid_get_data(ibuf, &sc->sc_loc_y);
    436  1.77   mbalmer 	} else
    437  1.77   mbalmer 		dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    438   1.6  augustss 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    439  1.63  augustss 	dw =  hid_get_data(ibuf, &sc->sc_loc_w);
    440  1.77   mbalmer 
    441  1.35  augustss 	if (sc->flags & UMS_REVZ)
    442  1.24  augustss 		dz = -dz;
    443  1.16  augustss 	for (i = 0; i < sc->nbuttons; i++)
    444  1.16  augustss 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    445  1.16  augustss 			buttons |= (1 << UMS_BUT(i));
    446   1.4  augustss 
    447  1.63  augustss 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 ||
    448  1.63  augustss 	    buttons != sc->sc_buttons) {
    449  1.63  augustss 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d w:%d buttons:0x%x\n",
    450  1.63  augustss 			dx, dy, dz, dw, buttons));
    451   1.4  augustss 		sc->sc_buttons = buttons;
    452  1.35  augustss 		if (sc->sc_wsmousedev != NULL) {
    453  1.23  augustss 			s = spltty();
    454  1.77   mbalmer 			wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz,
    455  1.77   mbalmer 			    dw, flags);
    456  1.23  augustss 			splx(s);
    457  1.23  augustss 		}
    458   1.1  augustss 	}
    459   1.1  augustss }
    460   1.3  augustss 
    461  1.42  augustss Static int
    462  1.44  augustss ums_enable(void *v)
    463   1.3  augustss {
    464   1.3  augustss 	struct ums_softc *sc = v;
    465  1.16  augustss 
    466  1.25  augustss 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    467  1.28  augustss 
    468  1.28  augustss 	if (sc->sc_dying)
    469  1.28  augustss 		return (EIO);
    470  1.28  augustss 
    471   1.3  augustss 	if (sc->sc_enabled)
    472  1.28  augustss 		return (EBUSY);
    473   1.3  augustss 
    474   1.3  augustss 	sc->sc_enabled = 1;
    475   1.4  augustss 	sc->sc_buttons = 0;
    476   1.3  augustss 
    477  1.53  augustss 	return (uhidev_open(&sc->sc_hdev));
    478   1.3  augustss }
    479   1.3  augustss 
    480  1.42  augustss Static void
    481  1.44  augustss ums_disable(void *v)
    482   1.3  augustss {
    483   1.3  augustss 	struct ums_softc *sc = v;
    484  1.25  augustss 
    485  1.25  augustss 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    486  1.25  augustss #ifdef DIAGNOSTIC
    487  1.25  augustss 	if (!sc->sc_enabled) {
    488  1.25  augustss 		printf("ums_disable: not enabled\n");
    489  1.25  augustss 		return;
    490  1.25  augustss 	}
    491  1.25  augustss #endif
    492   1.3  augustss 
    493   1.3  augustss 	sc->sc_enabled = 0;
    494  1.59    simonb 	uhidev_close(&sc->sc_hdev);
    495   1.3  augustss }
    496   1.3  augustss 
    497  1.42  augustss Static int
    498  1.68  christos ums_ioctl(void *v, u_long cmd, void *data, int flag,
    499  1.67  christos     struct lwp * p)
    500  1.16  augustss 
    501   1.3  augustss {
    502  1.77   mbalmer 	struct ums_softc *sc = v;
    503  1.77   mbalmer 
    504   1.3  augustss 	switch (cmd) {
    505   1.3  augustss 	case WSMOUSEIO_GTYPE:
    506  1.77   mbalmer 		if (sc->flags & UMS_ABS)
    507  1.77   mbalmer 			*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    508  1.77   mbalmer 		else
    509  1.77   mbalmer 			*(u_int *)data = WSMOUSE_TYPE_USB;
    510   1.3  augustss 		return (0);
    511   1.3  augustss 	}
    512  1.16  augustss 
    513  1.56    atatat 	return (EPASSTHROUGH);
    514   1.3  augustss }
    515