Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.47.2.1
      1  1.47.2.1   nathanw /*	$NetBSD: ums.c,v 1.47.2.1 2001/11/14 19:16:20 nathanw 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  * 3. All advertising materials mentioning features or use of this software
     20       1.1  augustss  *    must display the following acknowledgement:
     21       1.1  augustss  *        This product includes software developed by the NetBSD
     22       1.1  augustss  *        Foundation, Inc. and its contributors.
     23       1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1  augustss  *    contributors may be used to endorse or promote products derived
     25       1.1  augustss  *    from this software without specific prior written permission.
     26       1.1  augustss  *
     27       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38      1.20  augustss  */
     39      1.20  augustss 
     40      1.20  augustss /*
     41      1.46  augustss  * HID spec: http://www.usb.org/developers/data/devclass/hid1_1.pdf
     42       1.1  augustss  */
     43       1.1  augustss 
     44  1.47.2.1   nathanw #include <sys/cdefs.h>
     45  1.47.2.1   nathanw __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.47.2.1 2001/11/14 19:16:20 nathanw Exp $");
     46      1.35  augustss 
     47       1.1  augustss #include <sys/param.h>
     48       1.1  augustss #include <sys/systm.h>
     49       1.1  augustss #include <sys/kernel.h>
     50       1.1  augustss #include <sys/malloc.h>
     51       1.1  augustss #include <sys/device.h>
     52       1.1  augustss #include <sys/ioctl.h>
     53       1.1  augustss #include <sys/tty.h>
     54       1.1  augustss #include <sys/file.h>
     55       1.1  augustss #include <sys/select.h>
     56       1.1  augustss #include <sys/proc.h>
     57       1.1  augustss #include <sys/vnode.h>
     58       1.1  augustss #include <sys/poll.h>
     59       1.1  augustss 
     60       1.1  augustss #include <dev/usb/usb.h>
     61       1.1  augustss #include <dev/usb/usbhid.h>
     62       1.1  augustss 
     63       1.1  augustss #include <dev/usb/usbdi.h>
     64       1.1  augustss #include <dev/usb/usbdi_util.h>
     65       1.1  augustss #include <dev/usb/usbdevs.h>
     66       1.1  augustss #include <dev/usb/usb_quirks.h>
     67       1.1  augustss #include <dev/usb/hid.h>
     68       1.1  augustss 
     69       1.3  augustss #include <dev/wscons/wsconsio.h>
     70       1.3  augustss #include <dev/wscons/wsmousevar.h>
     71       1.3  augustss 
     72       1.1  augustss #ifdef USB_DEBUG
     73      1.26  augustss #define DPRINTF(x)	if (umsdebug) logprintf x
     74      1.26  augustss #define DPRINTFN(n,x)	if (umsdebug>(n)) logprintf x
     75       1.1  augustss int	umsdebug = 0;
     76       1.1  augustss #else
     77       1.1  augustss #define DPRINTF(x)
     78       1.1  augustss #define DPRINTFN(n,x)
     79       1.1  augustss #endif
     80       1.1  augustss 
     81      1.27  augustss #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
     82      1.27  augustss 
     83      1.27  augustss #define UMSUNIT(s)	(minor(s))
     84      1.16  augustss 
     85      1.16  augustss #define PS2LBUTMASK	x01
     86      1.16  augustss #define PS2RBUTMASK	x02
     87      1.16  augustss #define PS2MBUTMASK	x04
     88       1.1  augustss #define PS2BUTMASK 0x0f
     89       1.1  augustss 
     90       1.1  augustss struct ums_softc {
     91      1.31  augustss 	USBBASEDEVICE sc_dev;		/* base device */
     92      1.40  augustss 	usbd_device_handle sc_udev;
     93       1.1  augustss 	usbd_interface_handle sc_iface;	/* interface */
     94       1.1  augustss 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     95       1.1  augustss 	int sc_ep_addr;
     96       1.1  augustss 
     97       1.1  augustss 	u_char *sc_ibuf;
     98       1.1  augustss 	u_int8_t sc_iid;
     99       1.1  augustss 	int sc_isize;
    100      1.16  augustss 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
    101      1.16  augustss 	struct hid_location *sc_loc_btn;
    102       1.1  augustss 
    103      1.16  augustss 	int sc_enabled;
    104       1.3  augustss 
    105      1.16  augustss 	int flags;		/* device configuration */
    106      1.16  augustss #define UMS_Z		0x01	/* z direction available */
    107      1.35  augustss #define UMS_SPUR_BUT_UP	0x02	/* spurious button up events */
    108      1.35  augustss #define UMS_REVZ	0x04	/* Z-axis is reversed */
    109      1.35  augustss 
    110      1.16  augustss 	int nbuttons;
    111      1.27  augustss #define MAX_BUTTONS	31	/* chosen because sc_buttons is u_int32_t */
    112      1.16  augustss 
    113      1.27  augustss 	u_int32_t sc_buttons;	/* mouse button status */
    114       1.3  augustss 	struct device *sc_wsmousedev;
    115      1.28  augustss 
    116      1.28  augustss 	char			sc_dying;
    117       1.1  augustss };
    118       1.1  augustss 
    119       1.2  augustss #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
    120       1.2  augustss #define MOUSE_FLAGS (HIO_RELATIVE)
    121       1.2  augustss 
    122      1.44  augustss Static void ums_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    123       1.1  augustss 
    124      1.44  augustss Static int	ums_enable(void *);
    125      1.44  augustss Static void	ums_disable(void *);
    126      1.44  augustss Static int	ums_ioctl(void *, u_long, caddr_t, int, struct proc *);
    127       1.3  augustss 
    128       1.3  augustss const struct wsmouse_accessops ums_accessops = {
    129       1.3  augustss 	ums_enable,
    130       1.3  augustss 	ums_ioctl,
    131       1.3  augustss 	ums_disable,
    132       1.3  augustss };
    133       1.3  augustss 
    134      1.16  augustss USB_DECLARE_DRIVER(ums);
    135       1.1  augustss 
    136      1.16  augustss USB_MATCH(ums)
    137       1.1  augustss {
    138      1.16  augustss 	USB_MATCH_START(ums, uaa);
    139       1.1  augustss 	usb_interface_descriptor_t *id;
    140       1.1  augustss 	int size, ret;
    141       1.1  augustss 	void *desc;
    142      1.34  augustss 	usbd_status err;
    143       1.1  augustss 
    144      1.34  augustss 	if (uaa->iface == NULL)
    145       1.1  augustss 		return (UMATCH_NONE);
    146       1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    147      1.41  augustss 	if (id == NULL || id->bInterfaceClass != UICLASS_HID)
    148       1.1  augustss 		return (UMATCH_NONE);
    149       1.1  augustss 
    150  1.47.2.1   nathanw 	err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
    151      1.34  augustss 	if (err)
    152       1.1  augustss 		return (UMATCH_NONE);
    153       1.1  augustss 
    154       1.1  augustss 	if (hid_is_collection(desc, size,
    155       1.1  augustss 			      HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    156       1.1  augustss 		ret = UMATCH_IFACECLASS;
    157       1.1  augustss 	else
    158       1.1  augustss 		ret = UMATCH_NONE;
    159       1.1  augustss 
    160       1.1  augustss 	free(desc, M_TEMP);
    161       1.1  augustss 	return (ret);
    162       1.1  augustss }
    163       1.1  augustss 
    164      1.16  augustss USB_ATTACH(ums)
    165       1.1  augustss {
    166      1.16  augustss 	USB_ATTACH_START(ums, sc, uaa);
    167       1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    168       1.1  augustss 	usb_interface_descriptor_t *id;
    169       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    170       1.3  augustss 	struct wsmousedev_attach_args a;
    171       1.1  augustss 	int size;
    172       1.1  augustss 	void *desc;
    173      1.34  augustss 	usbd_status err;
    174       1.1  augustss 	char devinfo[1024];
    175      1.35  augustss 	u_int32_t flags, quirks;
    176      1.37  augustss 	int i, wheel;
    177      1.16  augustss 	struct hid_location loc_btn;
    178       1.1  augustss 
    179      1.40  augustss 	sc->sc_udev = uaa->device;
    180       1.1  augustss 	sc->sc_iface = iface;
    181       1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    182       1.1  augustss 	usbd_devinfo(uaa->device, 0, devinfo);
    183      1.16  augustss 	USB_ATTACH_SETUP;
    184      1.16  augustss 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    185      1.12  augustss 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    186       1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    187      1.34  augustss 	if (ed == NULL) {
    188       1.1  augustss 		printf("%s: could not read endpoint descriptor\n",
    189      1.16  augustss 		       USBDEVNAME(sc->sc_dev));
    190      1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    191       1.1  augustss 	}
    192       1.1  augustss 
    193      1.15  augustss 	DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
    194      1.15  augustss 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    195      1.15  augustss 		     " bInterval=%d\n",
    196      1.15  augustss 		     ed->bLength, ed->bDescriptorType,
    197      1.15  augustss 		     ed->bEndpointAddress & UE_ADDR,
    198      1.30  augustss 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    199      1.15  augustss 		     ed->bmAttributes & UE_XFERTYPE,
    200      1.15  augustss 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    201       1.1  augustss 
    202      1.30  augustss 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    203       1.1  augustss 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    204       1.1  augustss 		printf("%s: unexpected endpoint\n",
    205      1.16  augustss 		       USBDEVNAME(sc->sc_dev));
    206      1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    207       1.1  augustss 	}
    208       1.1  augustss 
    209      1.35  augustss 	quirks = usbd_get_quirks(uaa->device)->uq_flags;
    210      1.35  augustss 	if (quirks & UQ_MS_REVZ)
    211      1.35  augustss 		sc->flags |= UMS_REVZ;
    212      1.35  augustss 	if (quirks & UQ_SPUR_BUT_UP)
    213      1.35  augustss 		sc->flags |= UMS_SPUR_BUT_UP;
    214      1.24  augustss 
    215  1.47.2.1   nathanw 	err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
    216      1.34  augustss 	if (err)
    217      1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    218      1.16  augustss 
    219       1.2  augustss 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    220       1.2  augustss 		       hid_input, &sc->sc_loc_x, &flags)) {
    221      1.16  augustss 		printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
    222      1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    223      1.16  augustss 	}
    224      1.16  augustss 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    225      1.16  augustss 		printf("%s: X report 0x%04x not supported\n",
    226      1.16  augustss 		       USBDEVNAME(sc->sc_dev), flags);
    227      1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    228       1.1  augustss 	}
    229      1.10  augustss 
    230       1.2  augustss 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    231       1.2  augustss 		       hid_input, &sc->sc_loc_y, &flags)) {
    232      1.16  augustss 		printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
    233      1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    234      1.16  augustss 	}
    235      1.16  augustss 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    236      1.16  augustss 		printf("%s: Y report 0x%04x not supported\n",
    237      1.16  augustss 		       USBDEVNAME(sc->sc_dev), flags);
    238      1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    239       1.1  augustss 	}
    240      1.10  augustss 
    241      1.22  augustss 	/* Try to guess the Z activator: first check Z, then WHEEL. */
    242      1.37  augustss 	wheel = 0;
    243       1.6  augustss 	if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    244       1.6  augustss 		       hid_input, &sc->sc_loc_z, &flags) ||
    245      1.37  augustss 	    (wheel = hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
    246      1.37  augustss 						       HUG_WHEEL),
    247      1.37  augustss 		       hid_input, &sc->sc_loc_z, &flags))) {
    248      1.16  augustss 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    249      1.16  augustss 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    250      1.16  augustss 		} else {
    251      1.16  augustss 			sc->flags |= UMS_Z;
    252      1.37  augustss 			/* Wheels need the Z axis reversed. */
    253      1.37  augustss 			if (wheel)
    254      1.37  augustss 				sc->flags ^= UMS_REVZ;
    255      1.16  augustss 		}
    256       1.6  augustss 	}
    257       1.6  augustss 
    258      1.21  augustss 	/* figure out the number of buttons */
    259      1.21  augustss 	for (i = 1; i <= MAX_BUTTONS; i++)
    260      1.16  augustss 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    261      1.16  augustss 				hid_input, &loc_btn, 0))
    262      1.16  augustss 			break;
    263      1.16  augustss 	sc->nbuttons = i - 1;
    264      1.45  augustss 	sc->sc_loc_btn = malloc(sizeof(struct hid_location) * sc->nbuttons,
    265      1.16  augustss 				M_USBDEV, M_NOWAIT);
    266      1.22  augustss 	if (!sc->sc_loc_btn) {
    267      1.22  augustss 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    268      1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    269      1.22  augustss 	}
    270      1.16  augustss 
    271      1.45  augustss 	printf("%s: %d button%s%s\n", USBDEVNAME(sc->sc_dev),
    272      1.45  augustss 	    sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
    273      1.45  augustss 	    sc->flags & UMS_Z ? " and Z dir." : "");
    274      1.16  augustss 
    275      1.16  augustss 	for (i = 1; i <= sc->nbuttons; i++)
    276      1.16  augustss 		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    277      1.16  augustss 				hid_input, &sc->sc_loc_btn[i-1], 0);
    278       1.1  augustss 
    279       1.1  augustss 	sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
    280      1.25  augustss 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_NOWAIT);
    281      1.34  augustss 	if (sc->sc_ibuf == NULL) {
    282      1.22  augustss 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    283      1.25  augustss 		free(sc->sc_loc_btn, M_USBDEV);
    284      1.16  augustss 		USB_ATTACH_ERROR_RETURN;
    285      1.16  augustss 	}
    286       1.1  augustss 
    287       1.1  augustss 	sc->sc_ep_addr = ed->bEndpointAddress;
    288       1.1  augustss 	free(desc, M_TEMP);
    289       1.3  augustss 
    290      1.16  augustss #ifdef USB_DEBUG
    291      1.16  augustss 	DPRINTF(("ums_attach: sc=%p\n", sc));
    292      1.16  augustss 	DPRINTF(("ums_attach: X\t%d/%d\n",
    293      1.16  augustss 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    294      1.16  augustss 	DPRINTF(("ums_attach: Y\t%d/%d\n",
    295  1.47.2.1   nathanw 		 sc->sc_loc_y.pos, sc->sc_loc_y.size));
    296      1.16  augustss 	if (sc->flags & UMS_Z)
    297      1.16  augustss 		DPRINTF(("ums_attach: Z\t%d/%d\n",
    298      1.16  augustss 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
    299      1.16  augustss 	for (i = 1; i <= sc->nbuttons; i++) {
    300      1.16  augustss 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
    301      1.16  augustss 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
    302      1.16  augustss 	}
    303      1.16  augustss 	DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
    304      1.16  augustss #endif
    305      1.16  augustss 
    306       1.3  augustss 	a.accessops = &ums_accessops;
    307       1.3  augustss 	a.accesscookie = sc;
    308       1.3  augustss 
    309      1.40  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    310      1.40  augustss 			   USBDEV(sc->sc_dev));
    311      1.47  augustss 
    312      1.47  augustss 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    313      1.40  augustss 
    314      1.16  augustss 	USB_ATTACH_SUCCESS_RETURN;
    315      1.16  augustss }
    316      1.16  augustss 
    317      1.25  augustss int
    318      1.44  augustss ums_activate(device_ptr_t self, enum devact act)
    319      1.16  augustss {
    320      1.28  augustss 	struct ums_softc *sc = (struct ums_softc *)self;
    321      1.32  augustss 	int rv = 0;
    322      1.28  augustss 
    323      1.28  augustss 	switch (act) {
    324      1.28  augustss 	case DVACT_ACTIVATE:
    325      1.28  augustss 		return (EOPNOTSUPP);
    326      1.28  augustss 		break;
    327      1.28  augustss 
    328      1.28  augustss 	case DVACT_DEACTIVATE:
    329      1.34  augustss 		if (sc->sc_wsmousedev != NULL)
    330      1.32  augustss 			rv = config_deactivate(sc->sc_wsmousedev);
    331      1.28  augustss 		sc->sc_dying = 1;
    332      1.28  augustss 		break;
    333      1.28  augustss 	}
    334      1.32  augustss 	return (rv);
    335       1.1  augustss }
    336       1.1  augustss 
    337      1.33  augustss USB_DETACH(ums)
    338       1.1  augustss {
    339      1.33  augustss 	USB_DETACH_START(ums, sc);
    340      1.25  augustss 	int rv = 0;
    341       1.8  augustss 
    342      1.25  augustss 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
    343      1.33  augustss 
    344      1.25  augustss 	/* No need to do reference counting of ums, wsmouse has all the goo. */
    345      1.34  augustss 	if (sc->sc_wsmousedev != NULL)
    346      1.25  augustss 		rv = config_detach(sc->sc_wsmousedev, flags);
    347  1.47.2.1   nathanw #ifdef DIAGNOSTIC
    348  1.47.2.1   nathanw 	if (sc->sc_intrpipe != NULL) {
    349  1.47.2.1   nathanw 		printf("ums_disable: intr pipe still open\n");
    350  1.47.2.1   nathanw 		usbd_abort_pipe(sc->sc_intrpipe);
    351  1.47.2.1   nathanw 		usbd_close_pipe(sc->sc_intrpipe);
    352  1.47.2.1   nathanw 		sc->sc_intrpipe = NULL;
    353  1.47.2.1   nathanw 	}
    354  1.47.2.1   nathanw #endif
    355      1.25  augustss 	if (rv == 0) {
    356      1.25  augustss 		free(sc->sc_loc_btn, M_USBDEV);
    357      1.25  augustss 		free(sc->sc_ibuf, M_USBDEV);
    358      1.25  augustss 	}
    359      1.40  augustss 
    360      1.40  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    361      1.40  augustss 			   USBDEV(sc->sc_dev));
    362      1.40  augustss 
    363      1.25  augustss 	return (rv);
    364       1.1  augustss }
    365       1.1  augustss 
    366       1.1  augustss void
    367      1.44  augustss ums_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    368       1.1  augustss {
    369       1.1  augustss 	struct ums_softc *sc = addr;
    370       1.1  augustss 	u_char *ibuf;
    371       1.6  augustss 	int dx, dy, dz;
    372      1.27  augustss 	u_int32_t buttons = 0;
    373      1.16  augustss 	int i;
    374      1.23  augustss 	int s;
    375      1.21  augustss 
    376       1.1  augustss 	DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
    377       1.1  augustss 	DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
    378       1.1  augustss 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    379       1.1  augustss 
    380       1.1  augustss 	if (status == USBD_CANCELLED)
    381       1.1  augustss 		return;
    382       1.1  augustss 
    383      1.36  augustss 	if (status) {
    384       1.1  augustss 		DPRINTF(("ums_intr: status=%d\n", status));
    385       1.7  augustss 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    386       1.1  augustss 		return;
    387       1.1  augustss 	}
    388       1.1  augustss 
    389       1.1  augustss 	ibuf = sc->sc_ibuf;
    390      1.34  augustss 	if (sc->sc_iid != 0) {
    391       1.1  augustss 		if (*ibuf++ != sc->sc_iid)
    392       1.1  augustss 			return;
    393       1.1  augustss 	}
    394       1.1  augustss 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    395       1.1  augustss 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    396       1.6  augustss 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    397      1.35  augustss 	if (sc->flags & UMS_REVZ)
    398      1.24  augustss 		dz = -dz;
    399      1.16  augustss 	for (i = 0; i < sc->nbuttons; i++)
    400      1.16  augustss 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    401      1.16  augustss 			buttons |= (1 << UMS_BUT(i));
    402       1.4  augustss 
    403      1.35  augustss 	if (dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) {
    404      1.16  augustss 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
    405      1.16  augustss 			dx, dy, dz, buttons));
    406       1.4  augustss 		sc->sc_buttons = buttons;
    407      1.35  augustss 		if (sc->sc_wsmousedev != NULL) {
    408      1.23  augustss 			s = spltty();
    409      1.38  takemura 			wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz,
    410      1.38  takemura 				      WSMOUSE_INPUT_DELTA);
    411      1.23  augustss 			splx(s);
    412      1.23  augustss 		}
    413       1.1  augustss 	}
    414       1.1  augustss }
    415       1.3  augustss 
    416      1.42  augustss Static int
    417      1.44  augustss ums_enable(void *v)
    418       1.3  augustss {
    419       1.3  augustss 	struct ums_softc *sc = v;
    420      1.16  augustss 
    421      1.34  augustss 	usbd_status err;
    422       1.3  augustss 
    423      1.25  augustss 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
    424      1.28  augustss 
    425      1.28  augustss 	if (sc->sc_dying)
    426      1.28  augustss 		return (EIO);
    427      1.28  augustss 
    428       1.3  augustss 	if (sc->sc_enabled)
    429      1.28  augustss 		return (EBUSY);
    430       1.3  augustss 
    431       1.3  augustss 	sc->sc_enabled = 1;
    432       1.4  augustss 	sc->sc_buttons = 0;
    433       1.3  augustss 
    434       1.3  augustss 	/* Set up interrupt pipe. */
    435      1.34  augustss 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    436      1.34  augustss 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
    437      1.39  augustss 		  sc->sc_ibuf, sc->sc_isize, ums_intr, USBD_DEFAULT_INTERVAL);
    438      1.34  augustss 	if (err) {
    439       1.3  augustss 		DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
    440      1.34  augustss 			 err));
    441       1.3  augustss 		sc->sc_enabled = 0;
    442       1.3  augustss 		return (EIO);
    443       1.3  augustss 	}
    444       1.3  augustss 	return (0);
    445       1.3  augustss }
    446       1.3  augustss 
    447      1.42  augustss Static void
    448      1.44  augustss ums_disable(void *v)
    449       1.3  augustss {
    450       1.3  augustss 	struct ums_softc *sc = v;
    451      1.25  augustss 
    452      1.25  augustss 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
    453      1.25  augustss #ifdef DIAGNOSTIC
    454      1.25  augustss 	if (!sc->sc_enabled) {
    455      1.25  augustss 		printf("ums_disable: not enabled\n");
    456      1.25  augustss 		return;
    457      1.25  augustss 	}
    458      1.25  augustss #endif
    459       1.3  augustss 
    460       1.3  augustss 	/* Disable interrupts. */
    461       1.3  augustss 	usbd_abort_pipe(sc->sc_intrpipe);
    462       1.3  augustss 	usbd_close_pipe(sc->sc_intrpipe);
    463  1.47.2.1   nathanw 	sc->sc_intrpipe = NULL;
    464       1.3  augustss 
    465       1.3  augustss 	sc->sc_enabled = 0;
    466       1.3  augustss }
    467       1.3  augustss 
    468      1.42  augustss Static int
    469      1.44  augustss ums_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    470      1.16  augustss 
    471       1.3  augustss {
    472       1.3  augustss 	switch (cmd) {
    473       1.3  augustss 	case WSMOUSEIO_GTYPE:
    474      1.17  augustss 		*(u_int *)data = WSMOUSE_TYPE_USB;
    475       1.3  augustss 		return (0);
    476       1.3  augustss 	}
    477      1.16  augustss 
    478       1.3  augustss 	return (-1);
    479       1.3  augustss }
    480