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.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
38 #include <sys/stdint.h>
39 #include <sys/stddef.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
57 #include <sys/fcntl.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbhid.h>
66 #define USB_DEBUG_VAR ums_debug
67 #include <dev/usb/usb_debug.h>
69 #include <dev/usb/quirk/usb_quirk.h>
71 #include <sys/ioccom.h>
72 #include <sys/filio.h>
74 #include <sys/mouse.h>
77 static int ums_debug = 0;
79 SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
80 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
81 &ums_debug, 0, "Debug level");
84 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
85 #define MOUSE_FLAGS (HIO_RELATIVE)
87 #define UMS_BUF_SIZE 8 /* bytes */
88 #define UMS_IFQ_MAXLEN 50 /* units */
89 #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
90 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
91 #define UMS_INFO_MAX 2 /* maximum number of HID sets */
99 struct hid_location sc_loc_w;
100 struct hid_location sc_loc_x;
101 struct hid_location sc_loc_y;
102 struct hid_location sc_loc_z;
103 struct hid_location sc_loc_t;
104 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
107 #define UMS_FLAG_X_AXIS 0x0001
108 #define UMS_FLAG_Y_AXIS 0x0002
109 #define UMS_FLAG_Z_AXIS 0x0004
110 #define UMS_FLAG_T_AXIS 0x0008
111 #define UMS_FLAG_SBU 0x0010 /* spurious button up events */
112 #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */
113 #define UMS_FLAG_W_AXIS 0x0040
120 uint8_t sc_iid_btn[UMS_BUTTON_MAX];
125 struct usb_fifo_sc sc_fifo;
127 struct usb_callout sc_callout;
128 struct ums_info sc_info[UMS_INFO_MAX];
132 mousestatus_t sc_status;
134 struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
144 static void ums_put_queue_timeout(void *__sc);
146 static usb_callback_t ums_intr_callback;
148 static device_probe_t ums_probe;
149 static device_attach_t ums_attach;
150 static device_detach_t ums_detach;
152 static usb_fifo_cmd_t ums_start_read;
153 static usb_fifo_cmd_t ums_stop_read;
154 static usb_fifo_open_t ums_open;
155 static usb_fifo_close_t ums_close;
156 static usb_fifo_ioctl_t ums_ioctl;
158 static void ums_put_queue(struct ums_softc *, int32_t, int32_t,
159 int32_t, int32_t, int32_t);
160 static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
162 static struct usb_fifo_methods ums_fifo_methods = {
164 .f_close = &ums_close,
165 .f_ioctl = &ums_ioctl,
166 .f_start_read = &ums_start_read,
167 .f_stop_read = &ums_stop_read,
168 .basename[0] = "ums",
172 ums_put_queue_timeout(void *__sc)
174 struct ums_softc *sc = __sc;
176 mtx_assert(&sc->sc_mtx, MA_OWNED);
178 ums_put_queue(sc, 0, 0, 0, 0, 0);
182 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
184 struct ums_softc *sc = usbd_xfer_softc(xfer);
185 struct ums_info *info = &sc->sc_info[0];
186 struct usb_page_cache *pc;
187 uint8_t *buf = sc->sc_temp;
189 int32_t buttons_found = 0;
199 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
201 switch (USB_GET_STATE(xfer)) {
202 case USB_ST_TRANSFERRED:
203 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
205 if (len > (int)sizeof(sc->sc_temp)) {
206 DPRINTFN(6, "truncating large packet to %zu bytes\n",
207 sizeof(sc->sc_temp));
208 len = sizeof(sc->sc_temp);
213 pc = usbd_xfer_get_frame(xfer, 0);
214 usbd_copy_out(pc, 0, buf, len);
216 DPRINTFN(6, "data = %02x %02x %02x %02x "
217 "%02x %02x %02x %02x\n",
218 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
219 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
220 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
221 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
231 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
232 if ((*buf == 0x14) || (*buf == 0x15)) {
239 if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
240 (id == info->sc_iid_w))
241 dw += hid_get_data(buf, len, &info->sc_loc_w);
243 if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
244 (id == info->sc_iid_x))
245 dx += hid_get_data(buf, len, &info->sc_loc_x);
247 if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
248 (id == info->sc_iid_y))
249 dy = -hid_get_data(buf, len, &info->sc_loc_y);
251 if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
252 (id == info->sc_iid_z)) {
254 temp = hid_get_data(buf, len, &info->sc_loc_z);
255 if (info->sc_flags & UMS_FLAG_REVZ)
260 if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
261 (id == info->sc_iid_t)) {
262 dt -= hid_get_data(buf, len, &info->sc_loc_t);
263 /* T-axis is translated into button presses */
264 buttons_found |= (1UL << 5) | (1UL << 6);
267 for (i = 0; i < info->sc_buttons; i++) {
269 mask = 1UL << UMS_BUT(i);
270 /* check for correct button ID */
271 if (id != info->sc_iid_btn[i])
273 /* check for button pressed */
274 if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
276 /* register button mask */
277 buttons_found |= mask;
280 if (++info != &sc->sc_info[UMS_INFO_MAX])
283 /* keep old button value(s) for non-detected buttons */
284 buttons |= sc->sc_status.button & ~buttons_found;
286 if (dx || dy || dz || dt || dw ||
287 (buttons != sc->sc_status.button)) {
289 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
290 dx, dy, dz, dt, dw, buttons);
292 /* translate T-axis into button presses until further */
294 ums_put_queue(sc, 0, 0, 0, 0, buttons);
297 ums_put_queue(sc, 0, 0, 0, 0, buttons);
301 sc->sc_status.button = buttons;
302 sc->sc_status.dx += dx;
303 sc->sc_status.dy += dy;
304 sc->sc_status.dz += dz;
306 * sc->sc_status.dt += dt;
307 * no way to export this yet
311 * The Qtronix keyboard has a built in PS/2
312 * port for a mouse. The firmware once in a
313 * while posts a spurious button up
314 * event. This event we ignore by doing a
315 * timeout for 50 msecs. If we receive
316 * dx=dy=dz=buttons=0 before we add the event
317 * to the queue. In any other case we delete
320 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
321 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
322 (dw == 0) && (buttons == 0)) {
324 usb_callout_reset(&sc->sc_callout, hz / 20,
325 &ums_put_queue_timeout, sc);
328 usb_callout_stop(&sc->sc_callout);
330 ums_put_queue(sc, dx, dy, dz, dt, buttons);
335 /* check if we can put more data into the FIFO */
336 if (usb_fifo_put_bytes_max(
337 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
338 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
339 usbd_transfer_submit(xfer);
344 if (error != USB_ERR_CANCELLED) {
345 /* try clear stall first */
346 usbd_xfer_set_stall(xfer);
353 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
356 .type = UE_INTERRUPT,
357 .endpoint = UE_ADDR_ANY,
358 .direction = UE_DIR_IN,
359 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
360 .bufsize = 0, /* use wMaxPacketSize */
361 .callback = &ums_intr_callback,
365 /* A match on these entries will load ums */
366 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
367 {USB_IFACE_CLASS(UICLASS_HID),
368 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
369 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
373 ums_probe(device_t dev)
375 struct usb_attach_arg *uaa = device_get_ivars(dev);
382 if (uaa->usb_mode != USB_MODE_HOST)
385 if (uaa->info.bInterfaceClass != UICLASS_HID)
388 if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
391 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
392 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
393 return (BUS_PROBE_DEFAULT);
395 error = usbd_req_get_hid_desc(uaa->device, NULL,
396 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
401 if (hid_is_collection(d_ptr, d_len,
402 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
403 error = BUS_PROBE_DEFAULT;
412 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
413 uint16_t len, uint8_t index)
415 struct ums_info *info = &sc->sc_info[index];
420 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
421 hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
423 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
424 info->sc_flags |= UMS_FLAG_X_AXIS;
427 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
428 hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
430 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
431 info->sc_flags |= UMS_FLAG_Y_AXIS;
434 /* Try the wheel first as the Z activator since it's tradition. */
435 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
436 HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
438 hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
439 HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
441 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
442 info->sc_flags |= UMS_FLAG_Z_AXIS;
445 * We might have both a wheel and Z direction, if so put
446 * put the Z on the W coordinate.
448 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
449 HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
452 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
453 info->sc_flags |= UMS_FLAG_W_AXIS;
456 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
457 HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
460 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
461 info->sc_flags |= UMS_FLAG_Z_AXIS;
465 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
466 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
467 * to know that the byte after the wheel is the tilt axis.
468 * There are no other HID axis descriptors other than X,Y and
471 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
472 HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
473 &flags, &info->sc_iid_t)) {
475 info->sc_loc_t.pos += 8;
477 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
478 info->sc_flags |= UMS_FLAG_T_AXIS;
480 } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
481 HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
482 &flags, &info->sc_iid_t)) {
484 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
485 info->sc_flags |= UMS_FLAG_T_AXIS;
487 /* figure out the number of buttons */
489 for (i = 0; i < UMS_BUTTON_MAX; i++) {
490 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
491 hid_input, index, &info->sc_loc_btn[i], NULL,
492 &info->sc_iid_btn[i])) {
497 /* detect other buttons */
499 for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
500 if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
501 hid_input, index, &info->sc_loc_btn[i], NULL,
502 &info->sc_iid_btn[i])) {
507 info->sc_buttons = i;
509 if (i > sc->sc_buttons)
512 if (info->sc_flags == 0)
515 /* announce information about the mouse */
516 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
518 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
519 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
520 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
521 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
522 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
527 ums_attach(device_t dev)
529 struct usb_attach_arg *uaa = device_get_ivars(dev);
530 struct ums_softc *sc = device_get_softc(dev);
531 struct ums_info *info;
541 DPRINTFN(11, "sc=%p\n", sc);
543 device_set_usb_desc(dev);
545 mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
547 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
550 * Force the report (non-boot) protocol.
552 * Mice without boot protocol support may choose not to implement
553 * Set_Protocol at all; Ignore any error.
555 err = usbd_req_set_protocol(uaa->device, NULL,
556 uaa->info.bIfaceIndex, 1);
558 err = usbd_transfer_setup(uaa->device,
559 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
560 UMS_N_TRANSFER, sc, &sc->sc_mtx);
563 DPRINTF("error=%s\n", usbd_errstr(err));
567 /* Get HID descriptor */
568 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
569 &d_len, M_TEMP, uaa->info.bIfaceIndex);
572 device_printf(dev, "error reading report description\n");
576 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
579 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
580 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
581 * all of its other button positions are all off. It also reports that
582 * it has two addional buttons and a tilt wheel.
584 if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
588 info = &sc->sc_info[0];
589 info->sc_flags = (UMS_FLAG_X_AXIS |
593 info->sc_buttons = 3;
595 /* 1st byte of descriptor report contains garbage */
596 info->sc_loc_x.pos = 16;
597 info->sc_loc_x.size = 8;
598 info->sc_loc_y.pos = 24;
599 info->sc_loc_y.size = 8;
600 info->sc_loc_z.pos = 32;
601 info->sc_loc_z.size = 8;
602 info->sc_loc_btn[0].pos = 8;
603 info->sc_loc_btn[0].size = 1;
604 info->sc_loc_btn[1].pos = 9;
605 info->sc_loc_btn[1].size = 1;
606 info->sc_loc_btn[2].pos = 10;
607 info->sc_loc_btn[2].size = 1;
609 /* Announce device */
610 device_printf(dev, "3 buttons and [XYZ] "
611 "coordinates ID=0\n");
614 /* Search the HID descriptor and announce device */
615 for (i = 0; i < UMS_INFO_MAX; i++) {
616 ums_hid_parse(sc, dev, d_ptr, d_len, i);
620 if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
621 info = &sc->sc_info[0];
622 /* Some wheels need the Z axis reversed. */
623 info->sc_flags |= UMS_FLAG_REVZ;
625 if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
626 DPRINTF("WARNING: report size, %d bytes, is larger "
627 "than interrupt size, %d bytes!\n", isize,
628 usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
634 for (j = 0; j < UMS_INFO_MAX; j++) {
635 info = &sc->sc_info[j];
637 DPRINTF("sc=%p, index=%d\n", sc, j);
638 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
639 info->sc_loc_x.size, info->sc_iid_x);
640 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
641 info->sc_loc_y.size, info->sc_iid_y);
642 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
643 info->sc_loc_z.size, info->sc_iid_z);
644 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
645 info->sc_loc_t.size, info->sc_iid_t);
646 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
647 info->sc_loc_w.size, info->sc_iid_w);
649 for (i = 0; i < info->sc_buttons; i++) {
650 DPRINTF("B%d\t%d/%d id=%d\n",
651 i + 1, info->sc_loc_btn[i].pos,
652 info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
655 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
658 err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
659 &ums_fifo_methods, &sc->sc_fifo,
660 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
661 UID_ROOT, GID_OPERATOR, 0644);
665 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
666 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
667 OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD,
668 sc, 0, ums_sysctl_handler_parseinfo,
669 "", "Dump of parsed HID report descriptor");
682 ums_detach(device_t self)
684 struct ums_softc *sc = device_get_softc(self);
686 DPRINTF("sc=%p\n", sc);
688 usb_fifo_detach(&sc->sc_fifo);
690 usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
692 usb_callout_drain(&sc->sc_callout);
694 mtx_destroy(&sc->sc_mtx);
700 ums_start_read(struct usb_fifo *fifo)
702 struct ums_softc *sc = usb_fifo_softc(fifo);
705 /* Check if we should override the default polling interval */
706 rate = sc->sc_pollrate;
707 /* Range check rate */
710 /* Check for set rate */
711 if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
712 DPRINTF("Setting pollrate = %d\n", rate);
713 /* Stop current transfer, if any */
714 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
715 /* Set new interval */
716 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
717 /* Only set pollrate once */
721 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
725 ums_stop_read(struct usb_fifo *fifo)
727 struct ums_softc *sc = usb_fifo_softc(fifo);
729 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
730 usb_callout_stop(&sc->sc_callout);
734 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
735 (MOUSE_MSC_PACKETSIZE != 5))
736 #error "Software assumptions are not met. Please update code."
740 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
741 int32_t dz, int32_t dt, int32_t buttons)
764 buf[0] = sc->sc_mode.syncmask[1];
765 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
768 buf[3] = dx - (dx >> 1);
769 buf[4] = dy - (dy >> 1);
771 if (sc->sc_mode.level == 1) {
773 buf[6] = dz - (dz >> 1);
774 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
776 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
777 sc->sc_mode.packetsize, 1);
780 DPRINTF("Buffer full, discarded packet\n");
785 ums_reset_buf(struct ums_softc *sc)
787 /* reset read queue, must be called locked */
788 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
792 ums_open(struct usb_fifo *fifo, int fflags)
794 struct ums_softc *sc = usb_fifo_softc(fifo);
798 /* check for duplicate open, should not happen */
799 if (sc->sc_fflags & fflags)
802 /* check for first open */
803 if (sc->sc_fflags == 0) {
805 /* reset all USB mouse parameters */
807 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
808 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
810 sc->sc_hw.buttons = sc->sc_buttons;
812 sc->sc_hw.iftype = MOUSE_IF_USB;
813 sc->sc_hw.type = MOUSE_MOUSE;
814 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
817 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
818 sc->sc_mode.rate = -1;
819 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
820 sc->sc_mode.accelfactor = 0;
821 sc->sc_mode.level = 0;
822 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
823 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
824 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
828 sc->sc_status.flags = 0;
829 sc->sc_status.button = 0;
830 sc->sc_status.obutton = 0;
831 sc->sc_status.dx = 0;
832 sc->sc_status.dy = 0;
833 sc->sc_status.dz = 0;
834 /* sc->sc_status.dt = 0; */
837 if (fflags & FREAD) {
838 /* allocate RX buffer */
839 if (usb_fifo_alloc_buffer(fifo,
840 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
845 sc->sc_fflags |= fflags & (FREAD | FWRITE);
850 ums_close(struct usb_fifo *fifo, int fflags)
852 struct ums_softc *sc = usb_fifo_softc(fifo);
857 usb_fifo_free_buffer(fifo);
859 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
863 ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
865 struct ums_softc *sc = usb_fifo_softc(fifo);
871 mtx_lock(&sc->sc_mtx);
874 case MOUSE_GETHWINFO:
875 *(mousehw_t *)addr = sc->sc_hw;
879 *(mousemode_t *)addr = sc->sc_mode;
883 mode = *(mousemode_t *)addr;
885 if (mode.level == -1) {
886 /* don't change the current setting */
887 } else if ((mode.level < 0) || (mode.level > 1)) {
891 sc->sc_mode.level = mode.level;
894 /* store polling rate */
895 sc->sc_pollrate = mode.rate;
897 if (sc->sc_mode.level == 0) {
898 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
899 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
901 sc->sc_hw.buttons = sc->sc_buttons;
902 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
903 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
904 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
905 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
906 } else if (sc->sc_mode.level == 1) {
907 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
908 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
910 sc->sc_hw.buttons = sc->sc_buttons;
911 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
912 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
913 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
914 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
920 *(int *)addr = sc->sc_mode.level;
924 if (*(int *)addr < 0 || *(int *)addr > 1) {
928 sc->sc_mode.level = *(int *)addr;
930 if (sc->sc_mode.level == 0) {
931 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
932 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
934 sc->sc_hw.buttons = sc->sc_buttons;
935 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
936 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
937 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
938 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
939 } else if (sc->sc_mode.level == 1) {
940 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
941 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
943 sc->sc_hw.buttons = sc->sc_buttons;
944 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
945 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
946 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
947 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
952 case MOUSE_GETSTATUS:{
953 mousestatus_t *status = (mousestatus_t *)addr;
955 *status = sc->sc_status;
956 sc->sc_status.obutton = sc->sc_status.button;
957 sc->sc_status.button = 0;
958 sc->sc_status.dx = 0;
959 sc->sc_status.dy = 0;
960 sc->sc_status.dz = 0;
961 /* sc->sc_status.dt = 0; */
963 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
964 status->flags |= MOUSE_POSCHANGED;
966 if (status->button != status->obutton) {
967 status->flags |= MOUSE_BUTTONSCHANGED;
976 mtx_unlock(&sc->sc_mtx);
981 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
983 struct ums_softc *sc = arg1;
984 struct ums_info *info;
986 int i, j, err, had_output;
988 sb = sbuf_new_auto();
989 for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
990 info = &sc->sc_info[i];
992 /* Don't emit empty info */
993 if ((info->sc_flags &
994 (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
995 UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
996 info->sc_buttons == 0)
1000 sbuf_printf(sb, "\n");
1002 sbuf_printf(sb, "i%d:", i + 1);
1003 if (info->sc_flags & UMS_FLAG_X_AXIS)
1004 sbuf_printf(sb, " X:r%d, p%d, s%d;",
1005 (int)info->sc_iid_x,
1006 (int)info->sc_loc_x.pos,
1007 (int)info->sc_loc_x.size);
1008 if (info->sc_flags & UMS_FLAG_Y_AXIS)
1009 sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1010 (int)info->sc_iid_y,
1011 (int)info->sc_loc_y.pos,
1012 (int)info->sc_loc_y.size);
1013 if (info->sc_flags & UMS_FLAG_Z_AXIS)
1014 sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1015 (int)info->sc_iid_z,
1016 (int)info->sc_loc_z.pos,
1017 (int)info->sc_loc_z.size);
1018 if (info->sc_flags & UMS_FLAG_T_AXIS)
1019 sbuf_printf(sb, " T:r%d, p%d, s%d;",
1020 (int)info->sc_iid_t,
1021 (int)info->sc_loc_t.pos,
1022 (int)info->sc_loc_t.size);
1023 if (info->sc_flags & UMS_FLAG_W_AXIS)
1024 sbuf_printf(sb, " W:r%d, p%d, s%d;",
1025 (int)info->sc_iid_w,
1026 (int)info->sc_loc_w.pos,
1027 (int)info->sc_loc_w.size);
1029 for (j = 0; j < info->sc_buttons; j++) {
1030 sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1031 (int)info->sc_iid_btn[j],
1032 (int)info->sc_loc_btn[j].pos,
1033 (int)info->sc_loc_btn[j].size);
1037 err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1043 static devclass_t ums_devclass;
1045 static device_method_t ums_methods[] = {
1046 DEVMETHOD(device_probe, ums_probe),
1047 DEVMETHOD(device_attach, ums_attach),
1048 DEVMETHOD(device_detach, ums_detach),
1052 static driver_t ums_driver = {
1054 .methods = ums_methods,
1055 .size = sizeof(struct ums_softc),
1058 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
1059 MODULE_DEPEND(ums, usb, 1, 1, 1);
1060 MODULE_VERSION(ums, 1);