2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (lennart@augustsson.net) at
7 * Carlstedt Research & Technology.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
51 #include <sys/ioccom.h>
53 #include <sys/fcntl.h>
56 #include <sys/selinfo.h>
58 #include <sys/sysctl.h>
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbhid.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usb_quirks.h>
68 #include <dev/usb/hid.h>
70 #include <sys/mouse.h>
73 #define DPRINTF(x) if (umsdebug) printf x
74 #define DPRINTFN(n,x) if (umsdebug>(n)) printf x
76 SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
77 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
78 &umsdebug, 0, "ums debug level");
84 #define UMSUNIT(s) (minor(s)&0x1f)
86 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
88 #define QUEUE_BUFSIZE 400 /* MUST be divisible by 5 _and_ 8 */
91 device_t sc_dev; /* base device */
92 usbd_interface_handle sc_iface; /* interface */
93 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
99 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z, sc_loc_t, sc_loc_w;
100 struct hid_location *sc_loc_btn;
102 struct callout callout_handle; /* for spurious button ups */
105 int sc_disconnected; /* device is gone */
107 int flags; /* device configuration */
108 #define UMS_Z 0x01 /* z direction available */
109 #define UMS_SPUR_BUT_UP 0x02 /* spurious button up events */
110 #define UMS_T 0x04 /* aa direction available (tilt) */
111 #define UMS_REVZ 0x08 /* Z-axis is reversed */
113 #define MAX_BUTTONS 31 /* chosen because sc_buttons is int */
115 u_char qbuf[QUEUE_BUFSIZE]; /* must be divisable by 3&4 */
116 u_char dummy[100]; /* XXX just for safety and for now */
117 int qcount, qhead, qtail;
120 mousestatus_t status;
123 # define UMS_ASLEEP 0x01 /* readFromDevice is waiting */
124 # define UMS_SELECT 0x02 /* select is waiting */
125 struct selinfo rsel; /* process waiting in select */
127 struct cdev *dev; /* specfs */
130 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
131 #define MOUSE_FLAGS (HIO_RELATIVE)
133 static void ums_intr(usbd_xfer_handle xfer,
134 usbd_private_handle priv, usbd_status status);
136 static void ums_add_to_queue(struct ums_softc *sc,
137 int dx, int dy, int dz, int dt, int buttons);
138 static void ums_add_to_queue_timeout(void *priv);
140 static int ums_enable(void *);
141 static void ums_disable(void *);
143 static d_open_t ums_open;
144 static d_close_t ums_close;
145 static d_read_t ums_read;
146 static d_ioctl_t ums_ioctl;
147 static d_poll_t ums_poll;
150 static struct cdevsw ums_cdevsw = {
151 .d_version = D_VERSION,
152 .d_flags = D_NEEDGIANT,
154 .d_close = ums_close,
156 .d_ioctl = ums_ioctl,
161 static device_probe_t ums_match;
162 static device_attach_t ums_attach;
163 static device_detach_t ums_detach;
165 static device_method_t ums_methods[] = {
166 /* Device interface */
167 DEVMETHOD(device_probe, ums_match),
168 DEVMETHOD(device_attach, ums_attach),
169 DEVMETHOD(device_detach, ums_detach),
174 static driver_t ums_driver = {
177 sizeof(struct ums_softc)
180 static devclass_t ums_devclass;
183 ums_match(device_t self)
185 struct usb_attach_arg *uaa = device_get_ivars(self);
186 usb_interface_descriptor_t *id;
192 return (UMATCH_NONE);
193 id = usbd_get_interface_descriptor(uaa->iface);
194 if (!id || id->bInterfaceClass != UICLASS_HID)
195 return (UMATCH_NONE);
197 err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
199 return (UMATCH_NONE);
201 if (hid_is_collection(desc, size,
202 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
203 ret = UMATCH_IFACECLASS;
204 else if (id->bInterfaceClass == UICLASS_HID &&
205 id->bInterfaceSubClass == UISUBCLASS_BOOT &&
206 id->bInterfaceProtocol == UIPROTO_MOUSE)
207 ret = UMATCH_IFACECLASS;
216 ums_attach(device_t self)
218 struct ums_softc *sc = device_get_softc(self);
219 struct usb_attach_arg *uaa = device_get_ivars(self);
220 usbd_interface_handle iface = uaa->iface;
221 usb_interface_descriptor_t *id;
222 usb_endpoint_descriptor_t *ed;
228 struct hid_location loc_btn;
230 sc->sc_disconnected = 1;
231 sc->sc_iface = iface;
232 id = usbd_get_interface_descriptor(iface);
234 ed = usbd_interface2endpoint_descriptor(iface, 0);
236 printf("%s: could not read endpoint descriptor\n",
237 device_get_nameunit(sc->sc_dev));
241 DPRINTFN(10,("ums_attach: bLength=%d bDescriptorType=%d "
242 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
244 ed->bLength, ed->bDescriptorType,
245 UE_GET_ADDR(ed->bEndpointAddress),
246 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in":"out",
247 UE_GET_XFERTYPE(ed->bmAttributes),
248 UGETW(ed->wMaxPacketSize), ed->bInterval));
250 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
251 UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT) {
252 printf("%s: unexpected endpoint\n",
253 device_get_nameunit(sc->sc_dev));
257 err = usbd_read_report_desc(uaa->iface, &desc, &size, M_TEMP);
261 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
262 hid_input, &sc->sc_loc_x, &flags)) {
263 printf("%s: mouse has no X report\n", device_get_nameunit(sc->sc_dev));
266 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
267 printf("%s: X report 0x%04x not supported\n",
268 device_get_nameunit(sc->sc_dev), flags);
272 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
273 hid_input, &sc->sc_loc_y, &flags)) {
274 printf("%s: mouse has no Y report\n", device_get_nameunit(sc->sc_dev));
277 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
278 printf("%s: Y report 0x%04x not supported\n",
279 device_get_nameunit(sc->sc_dev), flags);
283 /* Try the wheel first as the Z activator since it's tradition. */
284 wheel = hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
286 hid_input, &sc->sc_loc_z, &flags) ||
287 hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
289 hid_input, &sc->sc_loc_z, &flags);
292 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
293 printf("\n%s: Wheel report 0x%04x not supported\n",
294 device_get_nameunit(sc->sc_dev), flags);
295 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
298 if (usbd_get_quirks(uaa->device)->uq_flags &
300 /* Some wheels need the Z axis reversed. */
301 sc->flags |= UMS_REVZ;
306 * We might have both a wheel and Z direction, if so put
307 * put the Z on the W coordinate.
309 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
311 hid_input, &sc->sc_loc_w, &flags)) {
312 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
313 printf("\n%s: Z report 0x%04x not supported\n",
314 device_get_nameunit(sc->sc_dev), flags);
315 sc->sc_loc_w.size = 0; /* Bad Z, ignore */
318 } else if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP,
320 hid_input, &sc->sc_loc_z, &flags)) {
321 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
322 printf("\n%s: Z report 0x%04x not supported\n",
323 device_get_nameunit(sc->sc_dev), flags);
324 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
331 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
332 * using 0x0048 (i've called it HUG_TWHEEL) and seems to expect
333 * you to know that the byte after the wheel is the tilt axis.
334 * There are no other HID axis descriptors other than X,Y and
337 if (hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_TWHEEL),
338 hid_input, &sc->sc_loc_t, &flags)) {
339 sc->sc_loc_t.pos = sc->sc_loc_t.pos + 8;
343 /* figure out the number of buttons */
344 for (i = 1; i <= MAX_BUTTONS; i++)
345 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
346 hid_input, &loc_btn, 0))
348 sc->nbuttons = i - 1;
349 sc->sc_loc_btn = malloc(sizeof(struct hid_location)*sc->nbuttons,
351 if (!sc->sc_loc_btn) {
352 printf("%s: no memory\n", device_get_nameunit(sc->sc_dev));
356 printf("%s: %d buttons%s%s.\n", device_get_nameunit(sc->sc_dev),
357 sc->nbuttons, sc->flags & UMS_Z? " and Z dir" : "",
358 sc->flags & UMS_T?" and a TILT dir": "");
360 for (i = 1; i <= sc->nbuttons; i++)
361 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
362 hid_input, &sc->sc_loc_btn[i-1], 0);
364 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
367 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
368 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
369 * all of its other button positions are all off. It also reports that
370 * it has two addional buttons and a tilt wheel.
372 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_MS_BAD_CLASS) {
374 sc->flags |= UMS_SPUR_BUT_UP;
378 /* 1st byte of descriptor report contains garbage */
379 sc->sc_loc_x.pos = 16;
380 sc->sc_loc_y.pos = 24;
381 sc->sc_loc_z.pos = 32;
382 sc->sc_loc_btn[0].pos = 8;
383 sc->sc_loc_btn[1].pos = 9;
384 sc->sc_loc_btn[2].pos = 10;
388 * The Microsoft Wireless Notebook Optical Mouse 3000 Model 1049 has
389 * five Report IDs: 19 23 24 17 18 (in the order they appear in report
390 * descriptor), it seems that report id 17 contains the necessary
391 * mouse information(3-buttons,X,Y,wheel) so we specify it manually.
393 if (uaa->vendor == USB_VENDOR_MICROSOFT &&
394 uaa->product == USB_PRODUCT_MICROSOFT_WLNOTEBOOK3) {
399 sc->sc_loc_x.pos = 8;
400 sc->sc_loc_y.pos = 16;
401 sc->sc_loc_z.pos = 24;
402 sc->sc_loc_btn[0].pos = 0;
403 sc->sc_loc_btn[1].pos = 1;
404 sc->sc_loc_btn[2].pos = 2;
407 sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_NOWAIT);
409 printf("%s: no memory\n", device_get_nameunit(sc->sc_dev));
410 free(sc->sc_loc_btn, M_USB);
414 sc->sc_ep_addr = ed->bEndpointAddress;
415 sc->sc_disconnected = 0;
419 DPRINTF(("ums_attach: sc=%p\n", sc));
420 DPRINTF(("ums_attach: X\t%d/%d\n",
421 sc->sc_loc_x.pos, sc->sc_loc_x.size));
422 DPRINTF(("ums_attach: Y\t%d/%d\n",
423 sc->sc_loc_y.pos, sc->sc_loc_y.size));
424 if (sc->flags & UMS_Z)
425 DPRINTF(("ums_attach: Z\t%d/%d\n",
426 sc->sc_loc_z.pos, sc->sc_loc_z.size));
427 for (i = 1; i <= sc->nbuttons; i++) {
428 DPRINTF(("ums_attach: B%d\t%d/%d\n",
429 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
431 DPRINTF(("ums_attach: size=%d, id=%d\n", sc->sc_isize, sc->sc_iid));
434 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
435 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
437 sc->hw.buttons = sc->nbuttons;
438 sc->hw.iftype = MOUSE_IF_USB;
439 sc->hw.type = MOUSE_MOUSE;
440 sc->hw.model = MOUSE_MODEL_GENERIC;
442 sc->mode.protocol = MOUSE_PROTO_MSC;
444 sc->mode.resolution = MOUSE_RES_UNKNOWN;
445 sc->mode.accelfactor = 0;
447 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
448 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
449 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
451 sc->status.flags = 0;
452 sc->status.button = sc->status.obutton = 0;
453 sc->status.dx = sc->status.dy = sc->status.dz = 0;
455 sc->dev = make_dev(&ums_cdevsw, device_get_unit(self),
456 UID_ROOT, GID_OPERATOR,
457 0644, "ums%d", device_get_unit(self));
459 callout_init(&sc->callout_handle, 0);
460 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_SPUR_BUT_UP) {
461 DPRINTF(("%s: Spurious button up events\n",
462 device_get_nameunit(sc->sc_dev)));
463 sc->flags |= UMS_SPUR_BUT_UP;
471 ums_detach(device_t self)
473 struct ums_softc *sc = device_get_softc(self);
478 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
480 free(sc->sc_loc_btn, M_USB);
481 free(sc->sc_ibuf, M_USB);
483 /* someone waiting for data */
485 * XXX If we wakeup the process here, the device will be gone by
486 * the time the process gets a chance to notice. *_close and friends
487 * should be fixed to handle this case.
488 * Or we should do a delayed detach for this.
489 * Does this delay now force tsleep to exit with an error?
491 if (sc->state & UMS_ASLEEP) {
492 sc->state &= ~UMS_ASLEEP;
495 if (sc->state & UMS_SELECT) {
496 sc->state &= ~UMS_SELECT;
497 selwakeuppri(&sc->rsel, PZERO);
500 destroy_dev(sc->dev);
506 ums_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
508 struct ums_softc *sc = addr;
510 int dx, dy, dz, dw, dt;
514 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
516 DPRINTFN(5, ("ums_intr: sc=%p status=%d\n", sc, status));
517 DPRINTFN(5, ("ums_intr: data ="));
518 for (i = 0; i < sc->sc_isize; i++)
519 DPRINTFN(5, (" %02x", sc->sc_ibuf[i]));
522 if (status == USBD_CANCELLED)
525 if (status != USBD_NORMAL_COMPLETION) {
526 DPRINTF(("ums_intr: status=%d\n", status));
527 if (status == USBD_STALLED)
528 usbd_clear_endpoint_stall_async(sc->sc_intrpipe);
529 if(status != USBD_IOERROR)
535 * The M$ Wireless Intellimouse 2.0 sends 1 extra leading byte of
536 * data compared to most USB mice. This byte frequently switches
537 * from 0x01 (usual state) to 0x02. I assume it is to allow
538 * extra, non-standard, reporting (say battery-life). However
539 * at the same time it generates a left-click message on the button
540 * byte which causes spurious left-click's where there shouldn't be.
541 * This should sort that.
542 * Currently it's the only user of UMS_T so use it as an identifier.
543 * We probably should switch to some more official quirk.
545 * UPDATE: This problem affects the M$ Wireless Notebook Optical Mouse,
546 * too. However, the leading byte for this mouse is normally 0x11,
547 * and the phantom mouse click occurs when its 0x14.
549 if (sc->flags & UMS_T) {
554 } else if (sc->flags & UMS_SPUR_BUT_UP) {
555 DPRINTFN(5, ("ums_intr: #### ibuf[0] =3D %d ####\n", *ibuf));
556 if (*ibuf == 0x14 || *ibuf == 0x15)
560 if (*ibuf++ != sc->sc_iid)
565 dx = hid_get_data(ibuf, &sc->sc_loc_x);
566 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
567 dz = -hid_get_data(ibuf, &sc->sc_loc_z);
568 dw = hid_get_data(ibuf, &sc->sc_loc_w);
569 if (sc->flags & UMS_REVZ)
571 if (sc->flags & UMS_T)
572 dt = -hid_get_data(ibuf, &sc->sc_loc_t);
575 for (i = 0; i < sc->nbuttons; i++)
576 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
577 buttons |= (1 << UMS_BUT(i));
579 if (dx || dy || dz || dt || dw || (sc->flags & UMS_Z)
580 || buttons != sc->status.button) {
581 DPRINTFN(5, ("ums_intr: x:%d y:%d z:%d w:%d t:%d buttons:0x%x\n",
582 dx, dy, dz, dw, dt, buttons));
584 sc->status.button = buttons;
588 /* sc->status.dt += dt; */ /* no way to export this yet */
589 /* sc->status.dw += dw; */ /* idem */
591 /* Discard data in case of full buffer */
592 if (sc->qcount == sizeof(sc->qbuf)) {
593 DPRINTF(("Buffer full, discarded packet"));
598 * The Qtronix keyboard has a built in PS/2 port for a mouse.
599 * The firmware once in a while posts a spurious button up
600 * event. This event we ignore by doing a timeout for 50 msecs.
601 * If we receive dx=dy=dz=buttons=0 before we add the event to
603 * In any other case we delete the timeout event.
605 if (sc->flags & UMS_SPUR_BUT_UP &&
606 dx == 0 && dy == 0 && dz == 0 && dt == 0 && buttons == 0) {
607 callout_reset(&sc->callout_handle, MS_TO_TICKS(50),
608 ums_add_to_queue_timeout, (void *) sc);
610 callout_stop(&sc->callout_handle);
611 ums_add_to_queue(sc, dx, dy, dz, dt, buttons);
617 ums_add_to_queue_timeout(void *priv)
619 struct ums_softc *sc = priv;
623 ums_add_to_queue(sc, 0, 0, 0, 0, 0);
628 ums_add_to_queue(struct ums_softc *sc, int dx, int dy, int dz, int dt, int buttons)
630 /* Discard data in case of full buffer */
631 if (sc->qhead+sc->mode.packetsize > sizeof(sc->qbuf)) {
632 DPRINTF(("Buffer full, discarded packet"));
636 if (dx > 254) dx = 254;
637 if (dx < -256) dx = -256;
638 if (dy > 254) dy = 254;
639 if (dy < -256) dy = -256;
640 if (dz > 126) dz = 126;
641 if (dz < -128) dz = -128;
642 if (dt > 126) dt = 126;
643 if (dt < -128) dt = -128;
645 sc->qbuf[sc->qhead] = sc->mode.syncmask[1];
646 sc->qbuf[sc->qhead] |= ~buttons & MOUSE_MSC_BUTTONS;
647 sc->qbuf[sc->qhead+1] = dx >> 1;
648 sc->qbuf[sc->qhead+2] = dy >> 1;
649 sc->qbuf[sc->qhead+3] = dx - (dx >> 1);
650 sc->qbuf[sc->qhead+4] = dy - (dy >> 1);
652 if (sc->mode.level == 1) {
653 sc->qbuf[sc->qhead+5] = dz >> 1;
654 sc->qbuf[sc->qhead+6] = dz - (dz >> 1);
655 sc->qbuf[sc->qhead+7] = ((~buttons >> 3)
656 & MOUSE_SYS_EXTBUTTONS);
659 sc->qhead += sc->mode.packetsize;
660 sc->qcount += sc->mode.packetsize;
661 /* wrap round at end of buffer */
662 if (sc->qhead >= sizeof(sc->qbuf))
665 /* someone waiting for data */
666 if (sc->state & UMS_ASLEEP) {
667 sc->state &= ~UMS_ASLEEP;
670 if (sc->state & UMS_SELECT) {
671 sc->state &= ~UMS_SELECT;
672 selwakeuppri(&sc->rsel, PZERO);
679 struct ums_softc *sc = v;
688 sc->qhead = sc->qtail = 0;
689 sc->status.flags = 0;
690 sc->status.button = sc->status.obutton = 0;
691 sc->status.dx = sc->status.dy = sc->status.dz /* = sc->status.dt */ = 0;
693 callout_handle_init((struct callout_handle *)&sc->callout_handle);
696 * Force the report (non-boot) protocol.
698 * Mice without boot protocol support may choose not to implement
699 * Set_Protocol at all; do not check for error.
701 usbd_set_protocol(sc->sc_iface, 1);
703 /* Set up interrupt pipe. */
704 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
705 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc,
706 sc->sc_ibuf, sc->sc_isize, ums_intr,
707 USBD_DEFAULT_INTERVAL);
709 DPRINTF(("ums_enable: usbd_open_pipe_intr failed, error=%d\n",
721 struct ums_softc *sc = priv;
723 callout_stop(&sc->callout_handle);
725 /* Disable interrupts. */
726 usbd_abort_pipe(sc->sc_intrpipe);
727 usbd_close_pipe(sc->sc_intrpipe);
732 DPRINTF(("Discarded %d bytes in queue\n", sc->qcount));
736 ums_open(struct cdev *dev, int flag, int fmt, struct thread *p)
738 struct ums_softc *sc;
740 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
744 return ums_enable(sc);
748 ums_close(struct cdev *dev, int flag, int fmt, struct thread *p)
750 struct ums_softc *sc;
752 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
763 ums_read(struct cdev *dev, struct uio *uio, int flag)
765 struct ums_softc *sc;
767 char buf[sizeof(sc->qbuf)];
771 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
778 while (sc->qcount == 0 ) {
779 if (flag & O_NONBLOCK) { /* non-blocking I/O */
784 sc->state |= UMS_ASLEEP; /* blocking I/O */
785 error = tsleep(sc, PZERO | PCATCH, "umsrea", 0);
789 } else if (!sc->sc_enabled) {
793 /* check whether the device is still there */
795 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
803 * XXX we could optimise the use of splx/splusb somewhat. The writer
804 * process only extends qcount and qtail. We could copy them and use the copies
805 * to do the copying out of the queue.
808 while ((sc->qcount > 0) && (uio->uio_resid > 0)) {
809 l = (sc->qcount < uio->uio_resid? sc->qcount:uio->uio_resid);
812 if (l > sizeof(sc->qbuf) - sc->qtail) /* transfer till end of buf */
813 l = sizeof(sc->qbuf) - sc->qtail;
816 uiomove(&sc->qbuf[sc->qtail], l, uio);
819 if ( sc->qcount - l < 0 ) {
820 DPRINTF(("qcount below 0, count=%d l=%d\n", sc->qcount, l));
823 sc->qcount -= l; /* remove the bytes from the buffer */
824 sc->qtail = (sc->qtail + l) % sizeof(sc->qbuf);
832 ums_poll(struct cdev *dev, int events, struct thread *p)
834 struct ums_softc *sc;
838 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
843 if (events & (POLLIN | POLLRDNORM)) {
845 revents = events & (POLLIN | POLLRDNORM);
847 sc->state |= UMS_SELECT;
848 selrecord(p, &sc->rsel);
857 ums_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *p)
859 struct ums_softc *sc;
864 sc = devclass_get_softc(ums_devclass, UMSUNIT(dev));
869 case MOUSE_GETHWINFO:
870 *(mousehw_t *)addr = sc->hw;
873 *(mousemode_t *)addr = sc->mode;
876 mode = *(mousemode_t *)addr;
878 if (mode.level == -1)
879 /* don't change the current setting */
881 else if ((mode.level < 0) || (mode.level > 1))
885 sc->mode.level = mode.level;
887 if (sc->mode.level == 0) {
888 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
889 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
891 sc->hw.buttons = sc->nbuttons;
892 sc->mode.protocol = MOUSE_PROTO_MSC;
893 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
894 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
895 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
896 } else if (sc->mode.level == 1) {
897 if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
898 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
900 sc->hw.buttons = sc->nbuttons;
901 sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
902 sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
903 sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
904 sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
907 bzero(sc->qbuf, sizeof(sc->qbuf));
908 sc->qhead = sc->qtail = sc->qcount = 0;
913 *(int *)addr = sc->mode.level;
916 if (*(int *)addr < 0 || *(int *)addr > 1)
920 sc->mode.level = *(int *)addr;
922 if (sc->mode.level == 0) {
923 if (sc->nbuttons > MOUSE_MSC_MAXBUTTON)
924 sc->hw.buttons = MOUSE_MSC_MAXBUTTON;
926 sc->hw.buttons = sc->nbuttons;
927 sc->mode.protocol = MOUSE_PROTO_MSC;
928 sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
929 sc->mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
930 sc->mode.syncmask[1] = MOUSE_MSC_SYNC;
931 } else if (sc->mode.level == 1) {
932 if (sc->nbuttons > MOUSE_SYS_MAXBUTTON)
933 sc->hw.buttons = MOUSE_SYS_MAXBUTTON;
935 sc->hw.buttons = sc->nbuttons;
936 sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
937 sc->mode.packetsize = MOUSE_SYS_PACKETSIZE;
938 sc->mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
939 sc->mode.syncmask[1] = MOUSE_SYS_SYNC;
942 bzero(sc->qbuf, sizeof(sc->qbuf));
943 sc->qhead = sc->qtail = sc->qcount = 0;
947 case MOUSE_GETSTATUS: {
948 mousestatus_t *status = (mousestatus_t *) addr;
951 *status = sc->status;
952 sc->status.obutton = sc->status.button;
953 sc->status.button = 0;
954 sc->status.dx = sc->status.dy
955 = sc->status.dz = /* sc->status.dt = */ 0;
958 if (status->dx || status->dy || status->dz /* || status->dt */)
959 status->flags |= MOUSE_POSCHANGED;
960 if (status->button != status->obutton)
961 status->flags |= MOUSE_BUTTONSCHANGED;
971 MODULE_DEPEND(ums, usb, 1, 1, 1);
972 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, usbd_driver_load, 0);