Home | History | Annotate | Line # | Download | only in usb
ums.c revision 1.19
      1 /*	$NetBSD: ums.c,v 1.19 1999/01/08 11:58:25 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/malloc.h>
     44 #if defined(__NetBSD__)
     45 #include <sys/device.h>
     46 #include <sys/ioctl.h>
     47 #elif defined(__FreeBSD__)
     48 #include <sys/module.h>
     49 #include <sys/bus.h>
     50 #include <sys/ioccom.h>
     51 #include <sys/conf.h>
     52 #endif
     53 #include <sys/tty.h>
     54 #include <sys/file.h>
     55 #include <sys/select.h>
     56 #include <sys/proc.h>
     57 #include <sys/vnode.h>
     58 #include <sys/poll.h>
     59 
     60 #include <dev/usb/usb.h>
     61 #include <dev/usb/usbhid.h>
     62 
     63 #include <dev/usb/usbdi.h>
     64 #include <dev/usb/usbdi_util.h>
     65 #include <dev/usb/usbdevs.h>
     66 #include <dev/usb/usb_quirks.h>
     67 #include <dev/usb/hid.h>
     68 
     69 #if defined(__NetBSD__)
     70 #include <dev/wscons/wsconsio.h>
     71 #include <dev/wscons/wsmousevar.h>
     72 #elif defined(__FreeBSD__)
     73 #include <machine/mouse.h>
     74 #endif
     75 
     76 #ifdef USB_DEBUG
     77 #define DPRINTF(x)	if (umsdebug) printf x
     78 #define DPRINTFN(n,x)	if (umsdebug>(n)) printf x
     79 int	umsdebug = 0;
     80 #else
     81 #define DPRINTF(x)
     82 #define DPRINTFN(n,x)
     83 #endif
     84 
     85 #define UMSUNIT(s)	(minor(s)&0x1f)
     86 
     87 #define PS2LBUTMASK	x01
     88 #define PS2RBUTMASK	x02
     89 #define PS2MBUTMASK	x04
     90 #define PS2BUTMASK 0x0f
     91 
     92 #define QUEUE_BUFSIZE	240	/* MUST be divisible by 3 _and_ 4 */
     93 
     94 struct ums_softc {
     95 	bdevice sc_dev;			/* base device */
     96 	usbd_interface_handle sc_iface;	/* interface */
     97 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     98 	int sc_ep_addr;
     99 
    100 	u_char *sc_ibuf;
    101 	u_int8_t sc_iid;
    102 	int sc_isize;
    103 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
    104 	struct hid_location *sc_loc_btn;
    105 
    106 	int sc_enabled;
    107 	int sc_disconnected;	/* device is gone */
    108 
    109 	int flags;		/* device configuration */
    110 #define UMS_Z		0x01	/* z direction available */
    111 	int nbuttons;
    112 
    113 #if defined(__NetBSD__)
    114 	u_char sc_buttons;	/* mouse button status */
    115 	struct device *sc_wsmousedev;
    116 #elif defined(__FreeBSD__)
    117 	u_char		qbuf[QUEUE_BUFSIZE];
    118 	u_char		dummy[100];		/* just for safety and for now */
    119 	int		qcount, qhead, qtail;
    120 	mousehw_t	hw;
    121 	mousemode_t	mode;
    122 	mousestatus_t	status;
    123 
    124 	int		state;
    125 #	  define	UMS_ASLEEP	0x01	/* readFromDevice is waiting */
    126 #	  define	UMS_SELECT	0x02	/* select is waiting */
    127 	struct selinfo	rsel;		/* process waiting in select */
    128 #endif
    129 };
    130 
    131 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
    132 #define MOUSE_FLAGS (HIO_RELATIVE)
    133 
    134 void ums_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
    135 void ums_disco __P((void *));
    136 
    137 static int	ums_enable __P((void *));
    138 static void	ums_disable __P((void *));
    139 
    140 #if defined(__NetBSD__)
    141 static int	ums_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    142 
    143 const struct wsmouse_accessops ums_accessops = {
    144 	ums_enable,
    145 	ums_ioctl,
    146 	ums_disable,
    147 };
    148 
    149 #elif defined(__FreeBSD__)
    150 static d_open_t ums_open;
    151 static d_close_t ums_close;
    152 static d_read_t ums_read;
    153 static d_ioctl_t ums_ioctl;
    154 static d_poll_t ums_poll;
    155 
    156 #define UMS_CDEV_MAJOR	111
    157 
    158 static struct  cdevsw ums_cdevsw = {
    159 	ums_open,	ums_close,	ums_read,	nowrite,
    160 	ums_ioctl,	nostop,		nullreset,	nodevtotty,
    161 	ums_poll,	nommap,
    162 	NULL,		"ums",		NULL,		-1
    163 };
    164 #endif
    165 
    166 USB_DECLARE_DRIVER(ums);
    167 
    168 USB_MATCH(ums)
    169 {
    170 	USB_MATCH_START(ums, uaa);
    171 	usb_interface_descriptor_t *id;
    172 	int size, ret;
    173 	void *desc;
    174 	usbd_status r;
    175 
    176 	if (!uaa->iface)
    177 		return (UMATCH_NONE);
    178 	id = usbd_get_interface_descriptor(uaa->iface);
    179 	if (!id || id->bInterfaceClass != UCLASS_HID)
    180 		return (UMATCH_NONE);
    181 
    182 	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
    183 	if (r != USBD_NORMAL_COMPLETION)
    184 		return (UMATCH_NONE);
    185 
    186 	if (hid_is_collection(desc, size,
    187 			      HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    188 		ret = UMATCH_IFACECLASS;
    189 	else
    190 		ret = UMATCH_NONE;
    191 
    192 	free(desc, M_TEMP);
    193 	return (ret);
    194 }
    195 
    196 USB_ATTACH(ums)
    197 {
    198 	USB_ATTACH_START(ums, sc, uaa);
    199 	usbd_interface_handle iface = uaa->iface;
    200 	usb_interface_descriptor_t *id;
    201 	usb_endpoint_descriptor_t *ed;
    202 #if defined(__NetBSD__)
    203 	struct wsmousedev_attach_args a;
    204 #endif
    205 	int size;
    206 	void *desc;
    207 	usbd_status r;
    208 	char devinfo[1024];
    209 	u_int32_t flags;
    210 	int i;
    211 	struct hid_location loc_btn;
    212 
    213 	sc->sc_disconnected = 1;
    214 	sc->sc_iface = iface;
    215 	id = usbd_get_interface_descriptor(iface);
    216 	usbd_devinfo(uaa->device, 0, devinfo);
    217 	USB_ATTACH_SETUP;
    218 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    219 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    220 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    221 	if (!ed) {
    222 		printf("%s: could not read endpoint descriptor\n",
    223 		       USBDEVNAME(sc->sc_dev));
    224 		USB_ATTACH_ERROR_RETURN;
    225 	}
    226 
    227 	DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
    228 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    229 		     " bInterval=%d\n",
    230 		     ed->bLength, ed->bDescriptorType,
    231 		     ed->bEndpointAddress & UE_ADDR,
    232 		     ed->bEndpointAddress & UE_IN ? "in" : "out",
    233 		     ed->bmAttributes & UE_XFERTYPE,
    234 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    235 
    236 	if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
    237 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    238 		printf("%s: unexpected endpoint\n",
    239 		       USBDEVNAME(sc->sc_dev));
    240 		USB_ATTACH_ERROR_RETURN;
    241 	}
    242 
    243 	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
    244 	if (r != USBD_NORMAL_COMPLETION)
    245 		USB_ATTACH_ERROR_RETURN;
    246 
    247 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    248 		       hid_input, &sc->sc_loc_x, &flags)) {
    249 		printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
    250 		USB_ATTACH_ERROR_RETURN;
    251 	}
    252 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    253 		printf("%s: X report 0x%04x not supported\n",
    254 		       USBDEVNAME(sc->sc_dev), flags);
    255 		USB_ATTACH_ERROR_RETURN;
    256 	}
    257 
    258 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    259 		       hid_input, &sc->sc_loc_y, &flags)) {
    260 		printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
    261 		USB_ATTACH_ERROR_RETURN;
    262 	}
    263 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    264 		printf("%s: Y report 0x%04x not supported\n",
    265 		       USBDEVNAME(sc->sc_dev), flags);
    266 		USB_ATTACH_ERROR_RETURN;
    267 	}
    268 
    269 	/* try to guess the Z activator: first check Z, then WHEEL */
    270 	if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    271 		       hid_input, &sc->sc_loc_z, &flags) ||
    272 	    hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
    273 		       hid_input, &sc->sc_loc_z, &flags)) {
    274 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
    275 			sc->sc_loc_z.size = 0;	/* Bad Z coord, ignore it */
    276 		} else {
    277 			sc->flags |= UMS_Z;
    278 		}
    279 	}
    280 
    281 	/* figure out the number of buttons, 7 is an arbitrary limit */
    282 	for (i = 1; i <= 7; i++)
    283 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    284 				hid_input, &loc_btn, 0))
    285 			break;
    286 	sc->nbuttons = i - 1;
    287 	sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
    288 				M_USBDEV, M_NOWAIT);
    289 	if (!sc->sc_loc_btn)
    290 		USB_ATTACH_ERROR_RETURN;
    291 
    292 	printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
    293 	       sc->nbuttons, (sc->flags & UMS_Z? " and Z dir." : ""));
    294 
    295 	for (i = 1; i <= sc->nbuttons; i++)
    296 		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
    297 				hid_input, &sc->sc_loc_btn[i-1], 0);
    298 
    299 	sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
    300 	sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
    301 	if (!sc->sc_ibuf) {
    302 		free(sc->sc_loc_btn, M_USB);
    303 		USB_ATTACH_ERROR_RETURN;
    304 	}
    305 
    306 	sc->sc_ep_addr = ed->bEndpointAddress;
    307 	sc->sc_disconnected = 0;
    308 	free(desc, M_TEMP);
    309 
    310 #ifdef USB_DEBUG
    311 	DPRINTF(("ums_attach: sc=%p\n", sc));
    312 	DPRINTF(("ums_attach: X\t%d/%d\n",
    313 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    314 	DPRINTF(("ums_attach: Y\t%d/%d\n",
    315 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
    316 	if (sc->flags & UMS_Z)
    317 		DPRINTF(("ums_attach: Z\t%d/%d\n",
    318 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
    319 	for (i = 1; i <= sc->nbuttons; i++) {
    320 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
    321 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
    322 	}
    323 	DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
    324 #endif
    325 
    326 #if defined(__NetBSD__)
    327 	a.accessops = &ums_accessops;
    328 	a.accesscookie = sc;
    329 
    330 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    331 #elif defined(__FreeBSD__)
    332 	sc->hw.buttons = 2;		/* XXX hw&mode values are bogus */
    333 	sc->hw.iftype = MOUSE_IF_PS2;
    334 	sc->hw.type = MOUSE_MOUSE;
    335 	if (sc->flags & UMS_Z)
    336 		sc->hw.model = MOUSE_MODEL_INTELLI;
    337 	else
    338 		sc->hw.model = MOUSE_MODEL_GENERIC;
    339 	sc->hw.hwid = 0;
    340 	sc->mode.protocol = MOUSE_PROTO_PS2;
    341 	sc->mode.rate = -1;
    342 	sc->mode.resolution = MOUSE_RES_DEFAULT;
    343 	sc->mode.accelfactor = 1;
    344 	sc->mode.level = 0;
    345 	if (sc->flags & UMS_Z) {
    346 		sc->mode.packetsize = MOUSE_INTELLI_PACKETSIZE;
    347 		sc->mode.syncmask[0] = 0xc8;
    348 	} else {
    349 		sc->mode.packetsize = MOUSE_PS2_PACKETSIZE;
    350 		sc->mode.syncmask[0] = 0xc0;
    351 	}
    352 	sc->mode.syncmask[1] = 0;
    353 
    354 	sc->status.flags = 0;
    355 	sc->status.button = sc->status.obutton = 0;
    356 	sc->status.dx = sc->status.dy = sc->status.dz = 0;
    357 
    358 	sc->rsel.si_flags = 0;
    359 	sc->rsel.si_pid = 0;
    360 #endif
    361 
    362 	USB_ATTACH_SUCCESS_RETURN;
    363 }
    364 
    365 
    366 #if defined(__FreeBSD__)
    367 static int
    368 ums_detach(device_t self)
    369 {
    370 	struct ums_softc *sc = device_get_softc(self);
    371 	char *devinfo = (char *) device_get_desc(self);
    372 
    373 	if (devinfo) {
    374 		device_set_desc(self, NULL);
    375 		free(devinfo, M_USB);
    376 	}
    377 	free(sc->sc_loc_btn, M_USB);
    378 	free(sc->sc_ibuf, M_USB);
    379 
    380 	return 0;
    381 }
    382 #endif
    383 
    384 void
    385 ums_disco(p)
    386 	void *p;
    387 {
    388 	struct ums_softc *sc = p;
    389 
    390 	DPRINTF(("ums_disco: sc=%p\n", sc));
    391 	usbd_abort_pipe(sc->sc_intrpipe);
    392 	sc->sc_disconnected = 1;
    393 }
    394 
    395 void
    396 ums_intr(reqh, addr, status)
    397 	usbd_request_handle reqh;
    398 	usbd_private_handle addr;
    399 	usbd_status status;
    400 {
    401 	struct ums_softc *sc = addr;
    402 	u_char *ibuf;
    403 	int dx, dy, dz;
    404 	u_char buttons = 0;
    405 	int i;
    406 #if defined(__NetBSD__)
    407 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
    408 #elif defined(__FreeBSD__)
    409 #define UMS_BUT(i) (i)
    410 #endif
    411 
    412 	DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
    413 	DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
    414 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    415 
    416 	if (status == USBD_CANCELLED)
    417 		return;
    418 
    419 	if (status != USBD_NORMAL_COMPLETION) {
    420 		DPRINTF(("ums_intr: status=%d\n", status));
    421 		usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
    422 		return;
    423 	}
    424 
    425 	ibuf = sc->sc_ibuf;
    426 	if (sc->sc_iid) {
    427 		if (*ibuf++ != sc->sc_iid)
    428 			return;
    429 	}
    430 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
    431 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
    432 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
    433 	/* NWH Why are you modifying the button assignments here?
    434 	 * That's the purpose of a high level mouse driver
    435 	 */
    436 	for (i = 0; i < sc->nbuttons; i++)
    437 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
    438 			buttons |= (1 << UMS_BUT(i));
    439 
    440 #if defined(__NetBSD__)
    441 	if (dx || dy || buttons != sc->sc_buttons) {
    442 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
    443 			dx, dy, dz, buttons));
    444 		sc->sc_buttons = buttons;
    445 		if (sc->sc_wsmousedev)
    446 			wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz);
    447 #elif defined(__FreeBSD__)
    448 	if (dx || dy || buttons != sc->status.button) {
    449 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
    450 			dx, dy, dz, buttons));
    451 
    452 		sc->status.button = buttons;
    453 		sc->status.dx += dx;
    454 		sc->status.dy += dy;
    455 		sc->status.dz += dz;
    456 
    457 		/* Discard data in case of full buffer */
    458 		if (sc->qcount == sizeof(sc->qbuf)) {
    459 			DPRINTF(("Buffer full, discarded packet"));
    460 			return;
    461 		}
    462 
    463 		sc->qbuf[sc->qhead] = MOUSE_PS2_SYNC;
    464 		if (dx < 0)
    465 			sc->qbuf[sc->qhead] |= MOUSE_PS2_XNEG;
    466 		if (dx > 255 || dx < -255)
    467 			sc->qbuf[sc->qhead] |= MOUSE_PS2_XOVERFLOW;
    468 		if (dy < 0)
    469 			sc->qbuf[sc->qhead] |= MOUSE_PS2_YNEG;
    470 		if (dy > 255 || dy < -255)
    471 			sc->qbuf[sc->qhead] |= MOUSE_PS2_YOVERFLOW;
    472 		sc->qbuf[sc->qhead++] |= buttons;
    473 		sc->qbuf[sc->qhead++] = dx;
    474 		sc->qbuf[sc->qhead++] = dy;
    475 		sc->qcount += 3;
    476 		if (sc->flags & UMS_Z) {
    477 			sc->qbuf[sc->qhead++] = dz;
    478 			sc->qcount++;
    479 		}
    480 #ifdef USB_DEBUG
    481 		if (sc->qhead > sizeof(sc->qbuf))
    482 			DPRINTF(("Buffer overrun! %d %d\n",
    483 				 sc->qhead, sizeof(sc->qbuf)));
    484 #endif
    485 		/* wrap round at end of buffer */
    486 		if (sc->qhead >= sizeof(sc->qbuf))
    487 			sc->qhead = 0;
    488 
    489 		/* someone waiting for data */
    490 		if (sc->state & UMS_ASLEEP)
    491 			wakeup(sc);
    492 		/* wake up any pending selects */
    493 		selwakeup(&sc->rsel);
    494 		sc->state &= ~UMS_SELECT;
    495 #endif
    496 	}
    497 }
    498 
    499 
    500 static int
    501 ums_enable(v)
    502 	void *v;
    503 {
    504 	struct ums_softc *sc = v;
    505 
    506 	usbd_status r;
    507 
    508 	if (sc->sc_enabled)
    509 		return EBUSY;
    510 
    511 	sc->sc_enabled = 1;
    512 #if defined(__NetBSD__)
    513 	sc->sc_buttons = 0;
    514 #elif defined(__FreeBSD__)
    515 	sc->qcount = 0;
    516 	sc->qhead = sc->qtail = 0;
    517 #ifdef USB_DEBUG
    518 	if (sizeof(sc->qbuf) % 4 || sizeof(sc->qbuf) % 3) {
    519 		DPRINTF(("Buffer size not divisible by 3 or 4\n"));
    520 		return ENXIO;
    521 	}
    522 #endif
    523 	sc->status.flags = 0;
    524 	sc->status.button = sc->status.obutton = 0;
    525 	sc->status.dx = sc->status.dy = sc->status.dz = 0;
    526 #endif
    527 
    528 	/* Set up interrupt pipe. */
    529 	r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    530 				USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
    531 				sc->sc_ibuf, sc->sc_isize, ums_intr);
    532 	if (r != USBD_NORMAL_COMPLETION) {
    533 		DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
    534 			 r));
    535 		sc->sc_enabled = 0;
    536 		return (EIO);
    537 	}
    538 	usbd_set_disco(sc->sc_intrpipe, ums_disco, sc);
    539 	return (0);
    540 }
    541 
    542 static void
    543 ums_disable(v)
    544 	void *v;
    545 {
    546 	struct ums_softc *sc = v;
    547 
    548 	/* Disable interrupts. */
    549 	usbd_abort_pipe(sc->sc_intrpipe);
    550 	usbd_close_pipe(sc->sc_intrpipe);
    551 
    552 	sc->sc_enabled = 0;
    553 
    554 #if defined(USBVERBOSE) && defined(__FreeBSD__)
    555 	if (sc->qcount != 0)
    556 		DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
    557 #endif
    558 }
    559 
    560 #if defined(__NetBSD__)
    561 static int
    562 ums_ioctl(v, cmd, data, flag, p)
    563 	void *v;
    564 	u_long cmd;
    565 	caddr_t data;
    566 	int flag;
    567 	struct proc *p;
    568 
    569 {
    570 	switch (cmd) {
    571 	case WSMOUSEIO_GTYPE:
    572 		*(u_int *)data = WSMOUSE_TYPE_USB;
    573 		return (0);
    574 	}
    575 
    576 	return (-1);
    577 }
    578 
    579 #elif defined(__FreeBSD__)
    580 static int
    581 ums_open(dev_t dev, int flag, int fmt, struct proc *p)
    582 {
    583 	struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
    584 
    585 	if (!sc) {
    586 		DPRINTF(("sc not found at open"));
    587 		return EINVAL;
    588 	}
    589 
    590 	return ums_enable(sc);
    591 }
    592 
    593 static int
    594 ums_close(dev_t dev, int flag, int fmt, struct proc *p)
    595 {
    596 	struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
    597 
    598 	if (!sc) {
    599 		DPRINTF(("sc not found at close"));
    600 		return EINVAL;
    601 	}
    602 
    603 	if (sc->sc_enabled)
    604 		ums_disable(sc);
    605 	return 0;
    606 }
    607 
    608 static int
    609 ums_read(dev_t dev, struct uio *uio, int flag)
    610 {
    611 	struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
    612 	int s;
    613 	char buf[sizeof(sc->qbuf)];
    614 	int l = 0;
    615 	int error;
    616 
    617 	if (!sc || !sc->sc_enabled) {
    618 		DPRINTF(("sc not found at read"));
    619 		return EINVAL;
    620 	}
    621 
    622 	s = splusb();
    623 	while (sc->qcount == 0 )  {
    624 		/* NWH XXX non blocking I/O ??
    625 		if (non blocking I/O ) {
    626 			splx(s);
    627 			return EWOULDBLOCK;
    628 		} else {
    629 		*/
    630 		sc->state |= UMS_ASLEEP;
    631 		error = tsleep(sc, PZERO | PCATCH, "umsrea", 0);
    632 		sc->state &= ~UMS_ASLEEP;
    633 		if (error) {
    634 			splx(s);
    635 			return error;
    636 		}
    637 	}
    638 
    639 	while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
    640 		l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
    641 		if (l > sizeof(buf))
    642 			l = sizeof(buf);
    643 		if (l > sizeof(sc->qbuf) - sc->qtail)		/* transfer till end of buf */
    644 			l = sizeof(sc->qbuf) - sc->qtail;
    645 
    646 		splx(s);
    647 		uiomove(&sc->qbuf[sc->qtail], l, uio);
    648 		s = splusb();
    649 
    650 		if ( sc->qcount - l < 0 ) {
    651 			DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
    652 			sc->qcount = l;
    653 		}
    654 		sc->qcount -= l;	/* remove the bytes from the buffer */
    655 		sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
    656 	}
    657 	splx(s);
    658 
    659 	return 0;
    660 }
    661 
    662 static int
    663 ums_poll(dev_t dev, int events, struct proc *p)
    664 {
    665 	struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
    666 	int revents = 0;
    667 	int s;
    668 
    669 	if (!sc) {
    670 		DPRINTF(("sc not found at poll"));
    671 		return 0;	/* just to make sure */
    672 	}
    673 
    674 	s = splusb();
    675 	if (events & (POLLIN | POLLRDNORM))
    676 		if (sc->qcount) {
    677 			revents = events & (POLLIN | POLLRDNORM);
    678 		} else {
    679 			sc->state |= UMS_SELECT;
    680 			selrecord(p, &sc->rsel);
    681 		}
    682 	splx(s);
    683 
    684 	return revents;
    685 }
    686 
    687 int
    688 ums_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
    689 {
    690 	struct ums_softc *sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
    691 	int error = 0;
    692 	int s;
    693 
    694 	if (!sc) {
    695 		DPRINTF(("sc not found at ioctl"));
    696 		return ENOENT;
    697 	}
    698 
    699 	switch(cmd) {
    700 	case MOUSE_GETHWINFO:
    701 		*(mousehw_t *)addr = sc->hw;
    702 		break;
    703 	case MOUSE_GETMODE:
    704 		*(mousemode_t *)addr = sc->mode;
    705 		break;
    706 	case MOUSE_GETLEVEL:
    707 		*(int *)addr = sc->mode.level;
    708 		break;
    709 	case MOUSE_GETSTATUS: {
    710 		mousestatus_t *status = (mousestatus_t *) addr;
    711 
    712 		s = splusb();
    713 		*status = sc->status;
    714 		sc->status.obutton = sc->status.button;
    715 		sc->status.button = 0;
    716 		sc->status.dx = sc->status.dy = sc->status.dz = 0;
    717 		splx(s);
    718 
    719 		if (status->dx || status->dy || status->dz)
    720 			status->flags |= MOUSE_POSCHANGED;
    721 		if (status->button != status->obutton)
    722 			status->flags |= MOUSE_BUTTONSCHANGED;
    723 		break;
    724 		}
    725 	default:
    726 		error = ENOTTY;
    727 	}
    728 
    729 	return error;
    730 }
    731 #endif
    732 
    733 #if defined(__FreeBSD__)
    734 CDEV_DRIVER_MODULE(ums, usb, ums_driver, ums_devclass,
    735 			UMS_CDEV_MAJOR, ums_cdevsw, usbd_driver_load, 0);
    736 #endif
    737 
    738