2 * $FreeBSD: src/sys/dev/usb/ums.c,v 1.64 2003/11/09 09:17:22 tanimura Exp $
3 * $DragonFly: src/sys/dev/usbmisc/ums/ums.c,v 1.6 2003/12/30 01:01:47 dillon Exp $
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 * HID spec: http://www.usb.org/developers/data/devclass/hid1_1.pdf
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
53 #include <sys/ioccom.h>
57 #if __FreeBSD_version >= 500014
58 #include <sys/selinfo.h>
60 #include <sys/select.h>
62 #include <sys/vnode.h>
64 #include <sys/sysctl.h>
66 #include <bus/usb/usb.h>
67 #include <bus/usb/usbhid.h>
69 #include <bus/usb/usbdi.h>
70 #include <bus/usb/usbdi_util.h>
71 #include <bus/usb/usbdevs.h>
72 #include <bus/usb/usb_quirks.h>
73 #include <bus/usb/hid.h>
75 #if __FreeBSD_version >= 500000
76 #include <sys/mouse.h>
78 #include <machine/mouse.h>
82 #define DPRINTF(x) if (umsdebug) logprintf x
83 #define DPRINTFN(n,x) if (umsdebug>(n)) logprintf x
85 SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
86 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
87 &umsdebug, 0, "ums debug level");
93 #define UMSUNIT(s) (minor(s)&0x1f)
95 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
97 #define QUEUE_BUFSIZE 400 /* MUST be divisible by 5 _and_ 8 */
100 device_t sc_dev; /* base device */
101 usbd_interface_handle sc_iface; /* interface */
102 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
108 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
109 struct hid_location *sc_loc_btn;
111 usb_callout_t callout_handle; /* for spurious button ups */
114 int sc_disconnected; /* device is gone */
116 int flags; /* device configuration */
117 #define UMS_Z 0x01 /* z direction available */
118 #define UMS_SPUR_BUT_UP 0x02 /* spurious button up events */
120 #define MAX_BUTTONS 7 /* chosen because sc_buttons is u_char */
122 u_char qbuf[QUEUE_BUFSIZE]; /* must be divisable by 3&4 */
123 u_char dummy[100]; /* XXX just for safety and for now */
124 int qcount, qhead, qtail;
127 mousestatus_t status;
130 # define UMS_ASLEEP 0x01 /* readFromDevice is waiting */
131 # define UMS_SELECT 0x02 /* select is waiting */
132 struct selinfo rsel; /* process waiting in select */
134 dev_t dev; /* specfs */
137 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
138 #define MOUSE_FLAGS (HIO_RELATIVE)
140 Static void ums_intr(usbd_xfer_handle xfer,
141 usbd_private_handle priv, usbd_status status);
143 Static void ums_add_to_queue(struct ums_softc *sc,
144 int dx, int dy, int dz, int buttons);
145 Static void ums_add_to_queue_timeout(void *priv);
147 Static int ums_enable(void *);
148 Static void ums_disable(void *);
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;
156 #define UMS_CDEV_MAJOR 111
158 Static struct cdevsw ums_cdevsw = {
160 /* maj */ UMS_CDEV_MAJOR,
166 /* close */ ums_close,
169 /* ioctl */ ums_ioctl,
172 /* strategy */ nostrategy,
177 USB_DECLARE_DRIVER(ums);
181 USB_MATCH_START(ums, uaa);
182 usb_interface_descriptor_t *id;
188 return (UMATCH_NONE);
189 id = usbd_get_interface_descriptor(uaa->iface);
190 if (!id || id->bInterfaceClass != UICLASS_HID)
191 return (UMATCH_NONE);
193 err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
195 return (UMATCH_NONE);
197 if (hid_is_collection(desc, size,
198 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
199 ret = UMATCH_IFACECLASS;
209 USB_ATTACH_START(ums, sc, uaa);
210 usbd_interface_handle iface = uaa->iface;
211 usb_interface_descriptor_t *id;
212 usb_endpoint_descriptor_t *ed;
219 struct hid_location loc_btn;
221 sc->sc_disconnected = 1;
222 sc->sc_iface = iface;
223 id = usbd_get_interface_descriptor(iface);
224 usbd_devinfo(uaa->device, 0, devinfo);
226 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
227 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
228 ed = usbd_interface2endpoint_descriptor(iface, 0);
230 printf("%s: could not read endpoint descriptor\n",
231 USBDEVNAME(sc->sc_dev));
232 USB_ATTACH_ERROR_RETURN;
235 DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
236 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
238 ed->bLength, ed->bDescriptorType,
239 UE_GET_ADDR(ed->bEndpointAddress),
240 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
241 UE_GET_XFERTYPE(ed->bmAttributes),
242 UGETW(ed->wMaxPacketSize), ed->bInterval));
244 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
245 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
246 printf("%s: unexpected endpoint\n",
247 USBDEVNAME(sc->sc_dev));
248 USB_ATTACH_ERROR_RETURN;
251 err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
253 USB_ATTACH_ERROR_RETURN;
255 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
256 hid_input, &sc->sc_loc_x, &flags)) {
257 printf("%s: mouse has no X report\n", USBDEVNAME(sc->sc_dev));
258 USB_ATTACH_ERROR_RETURN;
260 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
261 printf("%s: X report 0x%04x not supported\n",
262 USBDEVNAME(sc->sc_dev), flags);
263 USB_ATTACH_ERROR_RETURN;
266 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
267 hid_input, &sc->sc_loc_y, &flags)) {
268 printf("%s: mouse has no Y report\n", USBDEVNAME(sc->sc_dev));
269 USB_ATTACH_ERROR_RETURN;
271 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
272 printf("%s: Y report 0x%04x not supported\n",
273 USBDEVNAME(sc->sc_dev), flags);
274 USB_ATTACH_ERROR_RETURN;
277 /* try to guess the Z activator: first check Z, then WHEEL */
278 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
279 hid_input, &sc->sc_loc_z, &flags) ||
280 hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
281 hid_input, &sc->sc_loc_z, &flags)) {
282 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
283 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
289 /* figure out the number of buttons */
290 for (i = 1; i <= MAX_BUTTONS; i++)
291 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
292 hid_input, &loc_btn, 0))
294 sc->nbuttons = i - 1;
295 sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
297 if (!sc->sc_loc_btn) {
298 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
299 USB_ATTACH_ERROR_RETURN;
302 printf("%s: %d buttons%s\n", USBDEVNAME(sc->sc_dev),
303 sc->nbuttons, sc->flags & UMS_Z? " and Z dir." : "");
305 for (i = 1; i <= sc->nbuttons; i++)
306 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
307 hid_input, &sc->sc_loc_btn[i-1], 0);
309 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
310 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
312 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
313 free(sc->sc_loc_btn, M_USB);
314 USB_ATTACH_ERROR_RETURN;
317 sc->sc_ep_addr = ed->bEndpointAddress;
318 sc->sc_disconnected = 0;
322 DPRINTF(("ums_attach: sc=%p\n", sc));
323 DPRINTF(("ums_attach: X\t%d/%d\n",
324 sc->sc_loc_x.pos, sc->sc_loc_x.size));
325 DPRINTF(("ums_attach: Y\t%d/%d\n",
326 sc->sc_loc_y.pos, sc->sc_loc_y.size));
327 if (sc->flags & UMS_Z)
328 DPRINTF(("ums_attach: Z\t%d/%d\n",
329 sc->sc_loc_z.pos, sc->sc_loc_z.size));
330 for (i = 1; i <= sc->nbuttons; i++) {
331 DPRINTF(("ums_attach: B%d\t%d/%d\n",
332 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
334 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
337 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
338 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
340 sc->hw.buttons = sc->nbuttons;
341 sc->hw.iftype = MOUSE_IF_USB;
342 sc->hw.type = MOUSE_MOUSE;
343 sc->hw.model = MOUSE_MODEL_GENERIC;
345 sc->mode.protocol = MOUSE_PROTO_MSC;
347 sc->mode.resolution = MOUSE_RES_UNKNOWN;
348 sc->mode.accelfactor = 0;
350 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
351 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
352 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
354 sc->status.flags = 0;
355 sc->status.button = sc->status.obutton = 0;
356 sc->status.dx = sc->status.dy = sc->status.dz = 0;
359 sc->rsel.si_flags = 0;
363 sc->dev = make_dev(&ums_cdevsw, device_get_unit(self),
364 UID_ROOT, GID_OPERATOR,
365 0644, "ums%d", device_get_unit(self));
367 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_SPUR_BUT_UP) {
368 DPRINTF(("%s: Spurious button up events\n",
369 USBDEVNAME(sc->sc_dev)));
370 sc->flags |= UMS_SPUR_BUT_UP;
373 USB_ATTACH_SUCCESS_RETURN;
378 ums_detach(device_t self)
380 struct ums_softc *sc = device_get_softc(self);
385 DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
387 free(sc->sc_loc_btn, M_USB);
388 free(sc->sc_ibuf, M_USB);
390 /* someone waiting for data */
392 * XXX If we wakeup the process here, the device will be gone by
393 * the time the process gets a chance to notice. *_close and friends
394 * should be fixed to handle this case.
395 * Or we should do a delayed detach for this.
396 * Does this delay now force tsleep to exit with an error?
398 if (sc->state & UMS_ASLEEP) {
399 sc->state &= ~UMS_ASLEEP;
402 if (sc->state & UMS_SELECT) {
403 sc->state &= ~UMS_SELECT;
404 selwakeuppri(&sc->rsel, PZERO);
407 destroy_dev(sc->dev);
413 ums_intr(xfer, addr, status)
414 usbd_xfer_handle xfer;
415 usbd_private_handle addr;
418 struct ums_softc *sc = addr;
424 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
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]));
430 if (status == USBD_CANCELLED)
433 if (status != USBD_NORMAL_COMPLETION) {
434 DPRINTF(("ums_intr: status=%d\n", status));
435 if (status == USBD_STALLED)
436 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
442 if (*ibuf++ != sc->sc_iid)
446 dx = hid_get_data(ibuf, &sc->sc_loc_x);
447 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
448 dz = -hid_get_data(ibuf, &sc->sc_loc_z);
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));
453 if (dx || dy || dz || (sc->flags & UMS_Z)
454 || buttons != sc->status.button) {
455 DPRINTFN(5, ("ums_intr: x:%d y:%d z:%d buttons:0x%x\n",
456 dx, dy, dz, buttons));
458 sc->status.button = buttons;
463 /* Discard data in case of full buffer */
464 if (sc->qcount == sizeof(sc->qbuf)) {
465 DPRINTF(("Buffer full, discarded packet"));
470 * The Qtronix keyboard has a built in PS/2 port for a mouse.
471 * The firmware once in a while posts a spurious button up
472 * event. This event we ignore by doing a timeout for 50 msecs.
473 * If we receive dx=dy=dz=buttons=0 before we add the event to
475 * In any other case we delete the timeout event.
477 if (sc->flags & UMS_SPUR_BUT_UP &&
478 dx == 0 && dy == 0 && dz == 0 && buttons == 0) {
479 usb_callout(sc->callout_handle, MS_TO_TICKS(50 /*msecs*/),
480 ums_add_to_queue_timeout, (void *) sc);
482 usb_uncallout(sc->callout_handle,
483 ums_add_to_queue_timeout, (void *) sc);
484 ums_add_to_queue(sc, dx, dy, dz, buttons);
490 ums_add_to_queue_timeout(void *priv)
492 struct ums_softc *sc = priv;
496 ums_add_to_queue(sc, 0, 0, 0, 0);
501 ums_add_to_queue(struct ums_softc *sc, int dx, int dy, int dz, int buttons)
503 /* Discard data in case of full buffer */
504 if (sc->qhead+sc->mode.packetsize > sizeof(sc->qbuf)) {
505 DPRINTF(("Buffer full, discarded packet"));
509 if (dx > 254) dx = 254;
510 if (dx < -256) dx = -256;
511 if (dy > 254) dy = 254;
512 if (dy < -256) dy = -256;
513 if (dz > 126) dz = 126;
514 if (dz < -128) dz = -128;
516 sc->qbuf[sc->qhead] = sc->mode.syncmask[1];
517 sc->qbuf[sc->qhead] |= ~buttons & MOUSE_MSC_BUTTONS;
518 sc->qbuf[sc->qhead+1] = dx >> 1;
519 sc->qbuf[sc->qhead+2] = dy >> 1;
520 sc->qbuf[sc->qhead+3] = dx - (dx >> 1);
521 sc->qbuf[sc->qhead+4] = dy - (dy >> 1);
523 if (sc->mode.level == 1) {
524 sc->qbuf[sc->qhead+5] = dz >> 1;
525 sc->qbuf[sc->qhead+6] = dz - (dz >> 1);
526 sc->qbuf[sc->qhead+7] = ((~buttons >> 3)
527 & MOUSE_SYS_EXTBUTTONS);
530 sc->qhead += sc->mode.packetsize;
531 sc->qcount += sc->mode.packetsize;
532 /* wrap round at end of buffer */
533 if (sc->qhead >= sizeof(sc->qbuf))
536 /* someone waiting for data */
537 if (sc->state & UMS_ASLEEP) {
538 sc->state &= ~UMS_ASLEEP;
541 if (sc->state & UMS_SELECT) {
542 sc->state &= ~UMS_SELECT;
543 selwakeuppri(&sc->rsel, PZERO);
550 struct ums_softc *sc = v;
559 sc->qhead = sc->qtail = 0;
560 sc->status.flags = 0;
561 sc->status.button = sc->status.obutton = 0;
562 sc->status.dx = sc->status.dy = sc->status.dz = 0;
564 callout_handle_init((struct callout_handle *)&sc->callout_handle);
566 /* Set up interrupt pipe. */
567 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
568 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
569 sc->sc_ibuf, sc->sc_isize, ums_intr,
570 USBD_DEFAULT_INTERVAL);
572 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
584 struct ums_softc *sc = priv;
586 usb_uncallout(sc->callout_handle, ums_add_to_queue_timeout, sc);
588 /* Disable interrupts. */
589 usbd_abort_pipe(sc->sc_intrpipe);
590 usbd_close_pipe(sc->sc_intrpipe);
595 DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
599 ums_open(dev_t dev, int flag, int fmt, usb_proc_ptr p)
601 struct ums_softc *sc;
603 USB_GET_SC_OPEN(ums, UMSUNIT(dev), sc);
605 return ums_enable(sc);
609 ums_close(dev_t dev, int flag, int fmt, usb_proc_ptr p)
611 struct ums_softc *sc;
613 USB_GET_SC(ums, UMSUNIT(dev), sc);
625 ums_read(dev_t dev, struct uio *uio, int flag)
627 struct ums_softc *sc;
629 char buf[sizeof(sc->qbuf)];
633 USB_GET_SC(ums, UMSUNIT(dev), sc);
641 while (sc->qcount == 0 ) {
642 if (flag & IO_NDELAY) { /* non-blocking I/O */
647 sc->state |= UMS_ASLEEP; /* blocking I/O */
648 error = tsleep(sc, PCATCH, "umsrea", 0);
652 } else if (!sc->sc_enabled) {
656 /* check whether the device is still there */
658 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
666 * XXX we could optimise the use of splx/splusb somewhat. The writer
667 * process only extends qcount and qtail. We could copy them and use the copies
668 * to do the copying out of the queue.
671 while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
672 l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
675 if (l > sizeof(sc->qbuf) - sc->qtail) /* transfer till end of buf */
676 l = sizeof(sc->qbuf) - sc->qtail;
679 uiomove(&sc->qbuf[sc->qtail], l, uio);
682 if ( sc->qcount - l < 0 ) {
683 DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
686 sc->qcount -= l; /* remove the bytes from the buffer */
687 sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
695 ums_poll(dev_t dev, int events, usb_proc_ptr p)
697 struct ums_softc *sc;
701 USB_GET_SC(ums, UMSUNIT(dev), sc);
707 if (events & (POLLIN | POLLRDNORM)) {
709 revents = events & (POLLIN | POLLRDNORM);
711 sc->state |= UMS_SELECT;
712 selrecord(p, &sc->rsel);
721 ums_ioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
723 struct ums_softc *sc;
728 USB_GET_SC(ums, UMSUNIT(dev), sc);
734 case MOUSE_GETHWINFO:
735 *(mousehw_t *)addr = sc->hw;
738 *(mousemode_t *)addr = sc->mode;
741 mode = *(mousemode_t *)addr;
743 if (mode.level == -1)
744 /* don't change the current setting */
746 else if ((mode.level < 0) || (mode.level > 1))
750 sc->mode.level = mode.level;
752 if (sc->mode.level == 0) {
753 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
754 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
756 sc->hw.buttons = sc->nbuttons;
757 sc->mode.protocol = MOUSE_PROTO_MSC;
758 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
759 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
760 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
761 } else if (sc->mode.level == 1) {
762 if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
763 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
765 sc->hw.buttons = sc->nbuttons;
766 sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
767 sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
768 sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
769 sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
772 bzero(sc->qbuf, sizeof(sc->qbuf));
773 sc->qhead = sc->qtail = sc->qcount = 0;
778 *(int *)addr = sc->mode.level;
781 if (*(int *)addr < 0 || *(int *)addr > 1)
785 sc->mode.level = *(int *)addr;
787 if (sc->mode.level == 0) {
788 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
789 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
791 sc->hw.buttons = sc->nbuttons;
792 sc->mode.protocol = MOUSE_PROTO_MSC;
793 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
794 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
795 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
796 } else if (sc->mode.level == 1) {
797 if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
798 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
800 sc->hw.buttons = sc->nbuttons;
801 sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
802 sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
803 sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
804 sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
807 bzero(sc->qbuf, sizeof(sc->qbuf));
808 sc->qhead = sc->qtail = sc->qcount = 0;
812 case MOUSE_GETSTATUS: {
813 mousestatus_t *status = (mousestatus_t *) addr;
816 *status = sc->status;
817 sc->status.obutton = sc->status.button;
818 sc->status.button = 0;
819 sc->status.dx = sc->status.dy = sc->status.dz = 0;
822 if (status->dx || status->dy || status->dz)
823 status->flags |= MOUSE_POSCHANGED;
824 if (status->button != status->obutton)
825 status->flags |= MOUSE_BUTTONSCHANGED;
835 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, usbd_driver_load, 0);