1 /* $FreeBSD: src/sys/dev/usb/ums.c,v 1.36.2.6 2002/11/06 20:23:50 joe Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
41 * HID spec: http://www.usb.org/developers/data/usbhid10.pdf
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
50 #include <sys/ioccom.h>
54 #include <sys/select.h>
56 #include <sys/vnode.h>
58 #include <sys/sysctl.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbhid.h>
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>
69 #include <machine/mouse.h>
72 #define DPRINTF(x) if (umsdebug) logprintf x
73 #define DPRINTFN(n,x) if (umsdebug>(n)) logprintf x
75 SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
76 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
77 &umsdebug, 0, "ums debug level");
83 #define UMSUNIT(s) (minor(s)&0x1f)
85 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
87 #define QUEUE_BUFSIZE 400 /* MUST be divisible by 5 _and_ 8 */
90 device_t sc_dev; /* base device */
91 usbd_interface_handle sc_iface; /* interface */
92 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
98 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
99 struct hid_location *sc_loc_btn;
101 struct callout_handle timeout_handle; /* for spurious button ups */
104 int sc_disconnected; /* device is gone */
106 int flags; /* device configuration */
107 #define UMS_Z 0x01 /* z direction available */
108 #define UMS_SPUR_BUT_UP 0x02 /* spurious button up events */
110 #define MAX_BUTTONS 7 /* chosen because sc_buttons is u_char */
112 u_char qbuf[QUEUE_BUFSIZE]; /* must be divisable by 3&4 */
113 u_char dummy[100]; /* XXX just for safety and for now */
114 int qcount, qhead, qtail;
117 mousestatus_t status;
120 # define UMS_ASLEEP 0x01 /* readFromDevice is waiting */
121 # define UMS_SELECT 0x02 /* select is waiting */
122 struct selinfo rsel; /* process waiting in select */
124 dev_t dev; /* specfs */
127 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
128 #define MOUSE_FLAGS (HIO_RELATIVE)
130 Static void ums_intr(usbd_xfer_handle xfer,
131 usbd_private_handle priv, usbd_status status);
133 Static void ums_add_to_queue(struct ums_softc *sc,
134 int dx, int dy, int dz, int buttons);
135 Static void ums_add_to_queue_timeout(void *priv);
137 Static int ums_enable(void *);
138 Static void ums_disable(void *);
140 Static d_open_t ums_open;
141 Static d_close_t ums_close;
142 Static d_read_t ums_read;
143 Static d_ioctl_t ums_ioctl;
144 Static d_poll_t ums_poll;
146 #define UMS_CDEV_MAJOR 111
148 Static struct cdevsw ums_cdevsw = {
150 /* close */ ums_close,
153 /* ioctl */ ums_ioctl,
156 /* strategy */ nostrategy,
158 /* maj */ UMS_CDEV_MAJOR,
165 USB_DECLARE_DRIVER(ums);
169 USB_MATCH_START(ums, uaa);
170 usb_interface_descriptor_t *id;
176 return (UMATCH_NONE);
177 id = usbd_get_interface_descriptor(uaa->iface);
178 if (!id || id->bInterfaceClass != UICLASS_HID)
179 return (UMATCH_NONE);
181 err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
183 return (UMATCH_NONE);
185 if (hid_is_collection(desc, size,
186 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
187 ret = UMATCH_IFACECLASS;
197 USB_ATTACH_START(ums, sc, uaa);
198 usbd_interface_handle iface = uaa->iface;
199 usb_interface_descriptor_t *id;
200 usb_endpoint_descriptor_t *ed;
207 struct hid_location loc_btn;
209 sc->sc_disconnected = 1;
210 sc->sc_iface = iface;
211 id = usbd_get_interface_descriptor(iface);
212 usbd_devinfo(uaa->device, 0, devinfo);
214 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
215 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
216 ed = usbd_interface2endpoint_descriptor(iface, 0);
218 printf("%s: could not read endpoint descriptor\n",
219 USBDEVNAME(sc->sc_dev));
220 USB_ATTACH_ERROR_RETURN;
223 DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
224 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
226 ed->bLength, ed->bDescriptorType,
227 UE_GET_ADDR(ed->bEndpointAddress),
228 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
229 UE_GET_XFERTYPE(ed->bmAttributes),
230 UGETW(ed->wMaxPacketSize), ed->bInterval));
232 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
233 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
234 printf("%s: unexpected endpoint\n",
235 USBDEVNAME(sc->sc_dev));
236 USB_ATTACH_ERROR_RETURN;
239 err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_TEMP);
241 USB_ATTACH_ERROR_RETURN;
243 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
244 hid_input, &sc->sc_loc_x, &flags)) {
245 printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
246 USB_ATTACH_ERROR_RETURN;
248 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
249 printf("%s: X report 0x%04x not supported\n",
250 USBDEVNAME(sc->sc_dev), flags);
251 USB_ATTACH_ERROR_RETURN;
254 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
255 hid_input, &sc->sc_loc_y, &flags)) {
256 printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
257 USB_ATTACH_ERROR_RETURN;
259 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
260 printf("%s: Y report 0x%04x not supported\n",
261 USBDEVNAME(sc->sc_dev), flags);
262 USB_ATTACH_ERROR_RETURN;
265 /* try to guess the Z activator: first check Z, then WHEEL */
266 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
267 hid_input, &sc->sc_loc_z, &flags) ||
268 hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
269 hid_input, &sc->sc_loc_z, &flags)) {
270 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
271 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
277 /* figure out the number of buttons */
278 for (i = 1; i <= MAX_BUTTONS; i++)
279 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
280 hid_input, &loc_btn, 0))
282 sc->nbuttons = i - 1;
283 sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
285 if (!sc->sc_loc_btn) {
286 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
287 USB_ATTACH_ERROR_RETURN;
290 printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
291 sc->nbuttons, sc->flags & UMS_Z? " and Z dir." : "");
293 for (i = 1; i <= sc->nbuttons; i++)
294 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
295 hid_input, &sc->sc_loc_btn[i-1], 0);
297 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
298 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
300 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
301 free(sc->sc_loc_btn, M_USB);
302 USB_ATTACH_ERROR_RETURN;
305 sc->sc_ep_addr = ed->bEndpointAddress;
306 sc->sc_disconnected = 0;
310 DPRINTF(("ums_attach: sc=%p\n", sc));
311 DPRINTF(("ums_attach: X\t%d/%d\n",
312 sc->sc_loc_x.pos, sc->sc_loc_x.size));
313 DPRINTF(("ums_attach: Y\t%d/%d\n",
314 sc->sc_loc_y.pos, sc->sc_loc_y.size));
315 if (sc->flags & UMS_Z)
316 DPRINTF(("ums_attach: Z\t%d/%d\n",
317 sc->sc_loc_z.pos, sc->sc_loc_z.size));
318 for (i = 1; i <= sc->nbuttons; i++) {
319 DPRINTF(("ums_attach: B%d\t%d/%d\n",
320 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
322 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
325 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
326 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
328 sc->hw.buttons = sc->nbuttons;
329 sc->hw.iftype = MOUSE_IF_USB;
330 sc->hw.type = MOUSE_MOUSE;
331 sc->hw.model = MOUSE_MODEL_GENERIC;
333 sc->mode.protocol = MOUSE_PROTO_MSC;
335 sc->mode.resolution = MOUSE_RES_UNKNOWN;
336 sc->mode.accelfactor = 0;
338 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
339 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
340 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
342 sc->status.flags = 0;
343 sc->status.button = sc->status.obutton = 0;
344 sc->status.dx = sc->status.dy = sc->status.dz = 0;
346 sc->rsel.si_flags = 0;
349 sc->dev = make_dev(&ums_cdevsw, device_get_unit(self),
350 UID_ROOT, GID_OPERATOR,
351 0644, "ums%d", device_get_unit(self));
353 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_SPUR_BUT_UP) {
354 DPRINTF(("%s: Spurious button up events\n",
355 USBDEVNAME(sc->sc_dev)));
356 sc->flags |= UMS_SPUR_BUT_UP;
359 USB_ATTACH_SUCCESS_RETURN;
364 ums_detach(device_t self)
366 struct ums_softc *sc = device_get_softc(self);
372 DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
374 free(sc->sc_loc_btn, M_USB);
375 free(sc->sc_ibuf, M_USB);
377 vp = SLIST_FIRST(&sc->dev->si_hlist);
379 VOP_REVOKE(vp, REVOKEALL);
381 /* someone waiting for data */
383 * XXX If we wakeup the process here, the device will be gone by
384 * the time the process gets a chance to notice. *_close and friends
385 * should be fixed to handle this case.
386 * Or we should do a delayed detach for this.
387 * Does this delay now force tsleep to exit with an error?
389 if (sc->state & UMS_ASLEEP) {
390 sc->state &= ~UMS_ASLEEP;
393 if (sc->state & UMS_SELECT) {
394 sc->state &= ~UMS_SELECT;
395 selwakeup(&sc->rsel);
398 destroy_dev(sc->dev);
404 ums_intr(xfer, addr, status)
405 usbd_xfer_handle xfer;
406 usbd_private_handle addr;
409 struct ums_softc *sc = addr;
415 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
417 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
418 DPRINTFN(5, ("ums_intr: data = %02x %02x %02x\n",
419 sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
421 if (status == USBD_CANCELLED)
424 if (status != USBD_NORMAL_COMPLETION) {
425 DPRINTF(("ums_intr: status=%d\n", status));
426 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
432 if (*ibuf++ != sc->sc_iid)
436 dx = hid_get_data(ibuf, &sc->sc_loc_x);
437 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
438 dz = -hid_get_data(ibuf, &sc->sc_loc_z);
439 for (i = 0; i < sc->nbuttons; i++)
440 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
441 buttons |= (1 << UMS_BUT(i));
443 if (dx || dy || dz || (sc->flags & UMS_Z)
444 || buttons != sc->status.button) {
445 DPRINTFN(5, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
446 dx, dy, dz, buttons));
448 sc->status.button = buttons;
453 /* Discard data in case of full buffer */
454 if (sc->qcount == sizeof(sc->qbuf)) {
455 DPRINTF(("Buffer full, discarded packet"));
460 * The Qtronix keyboard has a built in PS/2 port for a mouse.
461 * The firmware once in a while posts a spurious button up
462 * event. This event we ignore by doing a timeout for 50 msecs.
463 * If we receive dx=dy=dz=buttons=0 before we add the event to
465 * In any other case we delete the timeout event.
467 if (sc->flags & UMS_SPUR_BUT_UP &&
468 dx == 0 && dy == 0 && dz == 0 && buttons == 0) {
469 usb_timeout(ums_add_to_queue_timeout, (void *) sc,
470 MS_TO_TICKS(50 /*msecs*/), sc->timeout_handle);
472 usb_untimeout(ums_add_to_queue_timeout, (void *) sc,
475 ums_add_to_queue(sc, dx, dy, dz, buttons);
481 ums_add_to_queue_timeout(void *priv)
483 struct ums_softc *sc = priv;
487 ums_add_to_queue(sc, 0, 0, 0, 0);
492 ums_add_to_queue(struct ums_softc *sc, int dx, int dy, int dz, int buttons)
494 /* Discard data in case of full buffer */
495 if (sc->qhead+sc->mode.packetsize > sizeof(sc->qbuf)) {
496 DPRINTF(("Buffer full, discarded packet"));
500 if (dx > 254) dx = 254;
501 if (dx < -256) dx = -256;
502 if (dy > 254) dy = 254;
503 if (dy < -256) dy = -256;
504 if (dz > 126) dz = 126;
505 if (dz < -128) dz = -128;
507 sc->qbuf[sc->qhead] = sc->mode.syncmask[1];
508 sc->qbuf[sc->qhead] |= ~buttons & MOUSE_MSC_BUTTONS;
509 sc->qbuf[sc->qhead+1] = dx >> 1;
510 sc->qbuf[sc->qhead+2] = dy >> 1;
511 sc->qbuf[sc->qhead+3] = dx - (dx >> 1);
512 sc->qbuf[sc->qhead+4] = dy - (dy >> 1);
514 if (sc->mode.level == 1) {
515 sc->qbuf[sc->qhead+5] = dz >> 1;
516 sc->qbuf[sc->qhead+6] = dz - (dz >> 1);
517 sc->qbuf[sc->qhead+7] = ((~buttons >> 3)
518 & MOUSE_SYS_EXTBUTTONS);
521 sc->qhead += sc->mode.packetsize;
522 sc->qcount += sc->mode.packetsize;
523 /* wrap round at end of buffer */
524 if (sc->qhead >= sizeof(sc->qbuf))
527 /* someone waiting for data */
528 if (sc->state & UMS_ASLEEP) {
529 sc->state &= ~UMS_ASLEEP;
532 if (sc->state & UMS_SELECT) {
533 sc->state &= ~UMS_SELECT;
534 selwakeup(&sc->rsel);
541 struct ums_softc *sc = v;
550 sc->qhead = sc->qtail = 0;
551 sc->status.flags = 0;
552 sc->status.button = sc->status.obutton = 0;
553 sc->status.dx = sc->status.dy = sc->status.dz = 0;
555 callout_handle_init(&sc->timeout_handle);
557 /* Set up interrupt pipe. */
558 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
559 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
560 sc->sc_ibuf, sc->sc_isize, ums_intr,
561 USBD_DEFAULT_INTERVAL);
563 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
575 struct ums_softc *sc = priv;
577 usb_untimeout(ums_add_to_queue_timeout, sc, sc->timeout_handle);
579 /* Disable interrupts. */
580 usbd_abort_pipe(sc->sc_intrpipe);
581 usbd_close_pipe(sc->sc_intrpipe);
586 DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
590 ums_open(dev_t dev, int flag, int fmt, usb_proc_ptr p)
592 struct ums_softc *sc;
594 USB_GET_SC_OPEN(ums, UMSUNIT(dev), sc);
596 return ums_enable(sc);
600 ums_close(dev_t dev, int flag, int fmt, usb_proc_ptr p)
602 struct ums_softc *sc;
604 USB_GET_SC(ums, UMSUNIT(dev), sc);
616 ums_read(dev_t dev, struct uio *uio, int flag)
618 struct ums_softc *sc;
620 char buf[sizeof(sc->qbuf)];
624 USB_GET_SC(ums, UMSUNIT(dev), sc);
632 while (sc->qcount == 0 ) {
633 if (flag & IO_NDELAY) { /* non-blocking I/O */
638 sc->state |= UMS_ASLEEP; /* blocking I/O */
639 error = tsleep(sc, PZERO | PCATCH, "umsrea", 0);
643 } else if (!sc->sc_enabled) {
647 /* check whether the device is still there */
649 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
657 * XXX we could optimise the use of splx/splusb somewhat. The writer
658 * process only extends qcount and qtail. We could copy them and use the copies
659 * to do the copying out of the queue.
662 while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
663 l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
666 if (l > sizeof(sc->qbuf) - sc->qtail) /* transfer till end of buf */
667 l = sizeof(sc->qbuf) - sc->qtail;
670 uiomove(&sc->qbuf[sc->qtail], l, uio);
673 if ( sc->qcount - l < 0 ) {
674 DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
677 sc->qcount -= l; /* remove the bytes from the buffer */
678 sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
686 ums_poll(dev_t dev, int events, usb_proc_ptr p)
688 struct ums_softc *sc;
692 USB_GET_SC(ums, UMSUNIT(dev), sc);
698 if (events & (POLLIN | POLLRDNORM)) {
700 revents = events & (POLLIN | POLLRDNORM);
702 sc->state |= UMS_SELECT;
703 selrecord(p, &sc->rsel);
712 ums_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
714 struct ums_softc *sc;
719 USB_GET_SC(ums, UMSUNIT(dev), sc);
725 case MOUSE_GETHWINFO:
726 *(mousehw_t *)addr = sc->hw;
729 *(mousemode_t *)addr = sc->mode;
732 mode = *(mousemode_t *)addr;
734 if (mode.level == -1)
735 /* don't change the current setting */
737 else if ((mode.level < 0) || (mode.level > 1))
741 sc->mode.level = mode.level;
743 if (sc->mode.level == 0) {
744 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
745 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
747 sc->hw.buttons = sc->nbuttons;
748 sc->mode.protocol = MOUSE_PROTO_MSC;
749 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
750 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
751 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
752 } else if (sc->mode.level == 1) {
753 if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
754 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
756 sc->hw.buttons = sc->nbuttons;
757 sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
758 sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
759 sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
760 sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
763 bzero(sc->qbuf, sizeof(sc->qbuf));
764 sc->qhead = sc->qtail = sc->qcount = 0;
769 *(int *)addr = sc->mode.level;
772 if (*(int *)addr < 0 || *(int *)addr > 1)
776 sc->mode.level = *(int *)addr;
778 if (sc->mode.level == 0) {
779 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
780 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
782 sc->hw.buttons = sc->nbuttons;
783 sc->mode.protocol = MOUSE_PROTO_MSC;
784 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
785 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
786 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
787 } else if (sc->mode.level == 1) {
788 if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
789 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
791 sc->hw.buttons = sc->nbuttons;
792 sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
793 sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
794 sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
795 sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
798 bzero(sc->qbuf, sizeof(sc->qbuf));
799 sc->qhead = sc->qtail = sc->qcount = 0;
803 case MOUSE_GETSTATUS: {
804 mousestatus_t *status = (mousestatus_t *) addr;
807 *status = sc->status;
808 sc->status.obutton = sc->status.button;
809 sc->status.button = 0;
810 sc->status.dx = sc->status.dy = sc->status.dz = 0;
813 if (status->dx || status->dy || status->dz)
814 status->flags |= MOUSE_POSCHANGED;
815 if (status->button != status->obutton)
816 status->flags |= MOUSE_BUTTONSCHANGED;
826 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, usbd_driver_load, 0);