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