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.
32 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
35 #include <sys/stdint.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <sys/fcntl.h>
54 #include <bus/u4b/usb.h>
55 #include <bus/u4b/usbdi.h>
56 #include <bus/u4b/usbdi_util.h>
57 #include <bus/u4b/usbhid.h>
60 #define USB_DEBUG_VAR ums_debug
61 #include <bus/u4b/usb_debug.h>
63 #include <bus/u4b/quirk/usb_quirk.h>
65 #include <sys/ioccom.h>
66 #include <sys/filio.h>
68 #include <sys/mouse.h>
71 static int ums_debug = 0;
73 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
74 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
75 &ums_debug, 0, "Debug level");
78 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
79 #define MOUSE_FLAGS (HIO_RELATIVE)
81 #define UMS_BUF_SIZE 8 /* bytes */
82 #define UMS_IFQ_MAXLEN 50 /* units */
83 #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
84 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
85 #define UMS_INFO_MAX 2 /* maximum number of HID sets */
93 struct hid_location sc_loc_w;
94 struct hid_location sc_loc_x;
95 struct hid_location sc_loc_y;
96 struct hid_location sc_loc_z;
97 struct hid_location sc_loc_t;
98 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
101 #define UMS_FLAG_X_AXIS 0x0001
102 #define UMS_FLAG_Y_AXIS 0x0002
103 #define UMS_FLAG_Z_AXIS 0x0004
104 #define UMS_FLAG_T_AXIS 0x0008
105 #define UMS_FLAG_SBU 0x0010 /* spurious button up events */
106 #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */
107 #define UMS_FLAG_W_AXIS 0x0040
114 uint8_t sc_iid_btn[UMS_BUTTON_MAX];
119 struct usb_fifo_sc sc_fifo;
121 struct usb_callout sc_callout;
122 struct ums_info sc_info[UMS_INFO_MAX];
126 mousestatus_t sc_status;
128 struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
139 static void ums_put_queue_timeout(void *__sc);
141 static usb_callback_t ums_intr_callback;
143 static device_probe_t ums_probe;
144 static device_attach_t ums_attach;
145 static device_detach_t ums_detach;
147 static usb_fifo_cmd_t ums_start_read;
148 static usb_fifo_cmd_t ums_stop_read;
149 static usb_fifo_open_t ums_open;
150 static usb_fifo_close_t ums_close;
151 static usb_fifo_ioctl_t ums_ioctl;
153 static void ums_put_queue(struct ums_softc *, int32_t, int32_t,
154 int32_t, int32_t, int32_t);
156 static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
159 static struct usb_fifo_methods ums_fifo_methods = {
161 .f_close = &ums_close,
162 .f_ioctl = &ums_ioctl,
163 .f_start_read = &ums_start_read,
164 .f_stop_read = &ums_stop_read,
165 .basename[0] = "ums",
169 ums_put_queue_timeout(void *__sc)
171 struct ums_softc *sc = __sc;
173 KKASSERT(lockowned(&sc->sc_lock));
175 ums_put_queue(sc, 0, 0, 0, 0, 0);
179 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
181 struct ums_softc *sc = usbd_xfer_softc(xfer);
182 struct ums_info *info = &sc->sc_info[0];
183 struct usb_page_cache *pc;
184 uint8_t *buf = sc->sc_temp;
186 int32_t buttons_found = 0;
196 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
198 switch (USB_GET_STATE(xfer)) {
199 case USB_ST_TRANSFERRED:
200 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
202 if (len > (int)sizeof(sc->sc_temp)) {
203 DPRINTFN(6, "truncating large packet to %zu bytes\n",
204 sizeof(sc->sc_temp));
205 len = sizeof(sc->sc_temp);
210 pc = usbd_xfer_get_frame(xfer, 0);
211 usbd_copy_out(pc, 0, buf, len);
213 DPRINTFN(6, "data = %02x %02x %02x %02x "
214 "%02x %02x %02x %02x\n",
215 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
216 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
217 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
218 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
228 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
229 if ((*buf == 0x14) || (*buf == 0x15)) {
236 if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
237 (id == info->sc_iid_w))
238 dw += hid_get_data(buf, len, &info->sc_loc_w);
240 if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
241 (id == info->sc_iid_x))
242 dx += hid_get_data(buf, len, &info->sc_loc_x);
244 if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
245 (id == info->sc_iid_y))
246 dy = -hid_get_data(buf, len, &info->sc_loc_y);
248 if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
249 (id == info->sc_iid_z)) {
251 temp = hid_get_data(buf, len, &info->sc_loc_z);
252 if (info->sc_flags & UMS_FLAG_REVZ)
257 if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
258 (id == info->sc_iid_t))
259 dt -= hid_get_data(buf, len, &info->sc_loc_t);
261 for (i = 0; i < info->sc_buttons; i++) {
263 mask = 1UL << UMS_BUT(i);
264 /* check for correct button ID */
265 if (id != info->sc_iid_btn[i])
267 /* check for button pressed */
268 if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
270 /* register button mask */
271 buttons_found |= mask;
274 if (++info != &sc->sc_info[UMS_INFO_MAX])
277 /* keep old button value(s) for non-detected buttons */
278 buttons |= sc->sc_status.button & ~buttons_found;
280 if (dx || dy || dz || dt || dw ||
281 (buttons != sc->sc_status.button)) {
282 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
283 dx, dy, dz, dt, dw, buttons);
285 /* translate T-axis into button presses until further */
291 sc->sc_status.button = buttons;
292 sc->sc_status.dx += dx;
293 sc->sc_status.dy += dy;
294 sc->sc_status.dz += dz;
296 * sc->sc_status.dt += dt;
297 * no way to export this yet
301 * The Qtronix keyboard has a built in PS/2
302 * port for a mouse. The firmware once in a
303 * while posts a spurious button up
304 * event. This event we ignore by doing a
305 * timeout for 50 msecs. If we receive
306 * dx=dy=dz=buttons=0 before we add the event
307 * to the queue. In any other case we delete
310 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
311 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
312 (dw == 0) && (buttons == 0)) {
314 usb_callout_reset(&sc->sc_callout, hz / 20,
315 &ums_put_queue_timeout, sc);
318 usb_callout_stop(&sc->sc_callout);
320 ums_put_queue(sc, dx, dy, dz, dt, buttons);
325 /* check if we can put more data into the FIFO */
326 if (usb_fifo_put_bytes_max(
327 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
328 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
329 usbd_transfer_submit(xfer);
332 * When the FIFO becomes full we stop cycling the
333 * poll and must clear sc_read_running. The next
334 * userland read/select/kqueue request will call
335 * f_start_read to get it going again.
337 sc->sc_read_running = 0;
342 if (error != USB_ERR_CANCELLED) {
343 /* try clear stall first */
344 usbd_xfer_set_stall(xfer);
351 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
354 .type = UE_INTERRUPT,
355 .endpoint = UE_ADDR_ANY,
356 .direction = UE_DIR_IN,
357 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
358 .bufsize = 0, /* use wMaxPacketSize */
359 .callback = &ums_intr_callback,
363 /* A match on these entries will load ums */
364 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
365 {USB_IFACE_CLASS(UICLASS_HID),
366 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
367 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
371 ums_probe(device_t dev)
373 struct usb_attach_arg *uaa = device_get_ivars(dev);
380 if (uaa->usb_mode != USB_MODE_HOST)
383 if (uaa->info.bInterfaceClass != UICLASS_HID)
386 if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
389 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
390 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
391 return (BUS_PROBE_DEFAULT);
393 error = usbd_req_get_hid_desc(uaa->device, NULL,
394 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
399 if (hid_is_mouse(d_ptr, d_len))
400 error = BUS_PROBE_DEFAULT;
404 kfree(d_ptr, M_TEMP);
409 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
410 uint16_t len, uint8_t index)
412 struct ums_info *info = &sc->sc_info[index];
417 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
418 hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
420 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
421 info->sc_flags |= UMS_FLAG_X_AXIS;
424 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
425 hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
427 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
428 info->sc_flags |= UMS_FLAG_Y_AXIS;
431 /* Try the wheel first as the Z activator since it's tradition. */
432 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
433 HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
435 hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
436 HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
438 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
439 info->sc_flags |= UMS_FLAG_Z_AXIS;
442 * We might have both a wheel and Z direction, if so put
443 * put the Z on the W coordinate.
445 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
446 HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
449 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
450 info->sc_flags |= UMS_FLAG_W_AXIS;
453 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
454 HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
457 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
458 info->sc_flags |= UMS_FLAG_Z_AXIS;
462 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
463 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
464 * to know that the byte after the wheel is the tilt axis.
465 * There are no other HID axis descriptors other than X,Y and
468 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
469 HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
470 &flags, &info->sc_iid_t)) {
472 info->sc_loc_t.pos += 8;
474 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
475 info->sc_flags |= UMS_FLAG_T_AXIS;
477 } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
478 HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
479 &flags, &info->sc_iid_t)) {
481 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
482 info->sc_flags |= UMS_FLAG_T_AXIS;
484 /* figure out the number of buttons */
486 for (i = 0; i < UMS_BUTTON_MAX; i++) {
487 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
488 hid_input, index, &info->sc_loc_btn[i], NULL,
489 &info->sc_iid_btn[i])) {
494 /* detect other buttons */
496 for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
497 if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
498 hid_input, index, &info->sc_loc_btn[i], NULL,
499 &info->sc_iid_btn[i])) {
504 info->sc_buttons = i;
506 if (i > sc->sc_buttons)
509 if (info->sc_flags == 0)
512 /* announce information about the mouse */
513 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
515 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
516 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
517 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
518 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
519 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
524 ums_attach(device_t dev)
526 struct usb_attach_arg *uaa = device_get_ivars(dev);
527 struct ums_softc *sc = device_get_softc(dev);
528 struct ums_info *info;
538 DPRINTFN(11, "sc=%p\n", sc);
540 device_set_usb_desc(dev);
542 lockinit(&sc->sc_lock, "ums lock", 0, LK_CANRECURSE);
544 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
547 * Force the report (non-boot) protocol.
549 * Mice without boot protocol support may choose not to implement
550 * Set_Protocol at all; Ignore any error.
552 err = usbd_req_set_protocol(uaa->device, NULL,
553 uaa->info.bIfaceIndex, 1);
555 err = usbd_transfer_setup(uaa->device,
556 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
557 UMS_N_TRANSFER, sc, &sc->sc_lock);
560 DPRINTF("error=%s\n", usbd_errstr(err));
564 /* Get HID descriptor */
565 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
566 &d_len, M_TEMP, uaa->info.bIfaceIndex);
569 device_printf(dev, "error reading report description\n");
573 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
576 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
577 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
578 * all of its other button positions are all off. It also reports that
579 * it has two addional buttons and a tilt wheel.
581 if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
585 info = &sc->sc_info[0];
586 info->sc_flags = (UMS_FLAG_X_AXIS |
590 info->sc_buttons = 3;
592 /* 1st byte of descriptor report contains garbage */
593 info->sc_loc_x.pos = 16;
594 info->sc_loc_x.size = 8;
595 info->sc_loc_y.pos = 24;
596 info->sc_loc_y.size = 8;
597 info->sc_loc_z.pos = 32;
598 info->sc_loc_z.size = 8;
599 info->sc_loc_btn[0].pos = 8;
600 info->sc_loc_btn[0].size = 1;
601 info->sc_loc_btn[1].pos = 9;
602 info->sc_loc_btn[1].size = 1;
603 info->sc_loc_btn[2].pos = 10;
604 info->sc_loc_btn[2].size = 1;
606 /* Announce device */
607 device_printf(dev, "3 buttons and [XYZ] "
608 "coordinates ID=0\n");
611 /* Search the HID descriptor and announce device */
612 for (i = 0; i < UMS_INFO_MAX; i++) {
613 ums_hid_parse(sc, dev, d_ptr, d_len, i);
617 if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
618 info = &sc->sc_info[0];
619 /* Some wheels need the Z axis reversed. */
620 info->sc_flags |= UMS_FLAG_REVZ;
622 if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
623 DPRINTF("WARNING: report size, %d bytes, is larger "
624 "than interrupt size, %d bytes!\n", isize,
625 usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
627 kfree(d_ptr, M_TEMP);
631 for (j = 0; j < UMS_INFO_MAX; j++) {
632 info = &sc->sc_info[j];
634 DPRINTF("sc=%p, index=%d\n", sc, j);
635 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
636 info->sc_loc_x.size, info->sc_iid_x);
637 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
638 info->sc_loc_y.size, info->sc_iid_y);
639 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
640 info->sc_loc_z.size, info->sc_iid_z);
641 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
642 info->sc_loc_t.size, info->sc_iid_t);
643 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
644 info->sc_loc_w.size, info->sc_iid_w);
646 for (i = 0; i < info->sc_buttons; i++) {
647 DPRINTF("B%d\t%d/%d id=%d\n",
648 i + 1, info->sc_loc_btn[i].pos,
649 info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
652 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
655 err = usb_fifo_attach(uaa->device, sc, &sc->sc_lock,
656 &ums_fifo_methods, &sc->sc_fifo,
657 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
658 UID_ROOT, GID_OPERATOR, 0644);
662 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
663 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
664 OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD,
665 sc, 0, ums_sysctl_handler_parseinfo,
666 "", "Dump of parsed HID report descriptor");
673 kfree(d_ptr, M_TEMP);
680 ums_detach(device_t self)
682 struct ums_softc *sc = device_get_softc(self);
684 DPRINTF("sc=%p\n", sc);
686 usb_fifo_detach(&sc->sc_fifo);
688 usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
690 usb_callout_drain(&sc->sc_callout);
692 lockuninit(&sc->sc_lock);
698 ums_start_read(struct usb_fifo *fifo)
700 struct ums_softc *sc = usb_fifo_softc(fifo);
703 /* Check if we should override the default polling interval */
704 rate = sc->sc_pollrate;
705 /* Range check rate */
708 /* Check for set rate */
709 if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
710 DPRINTF("Setting pollrate = %d\n", rate);
711 /* Stop current transfer, if any */
712 if (sc->sc_read_running) {
713 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
714 sc->sc_read_running = 0;
716 /* Set new interval */
717 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
718 /* Only set pollrate once */
721 if (sc->sc_read_running == 0) {
722 sc->sc_read_running = 1;
723 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
728 ums_stop_read(struct usb_fifo *fifo)
730 struct ums_softc *sc = usb_fifo_softc(fifo);
732 if (sc->sc_read_running) {
733 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
734 sc->sc_read_running = 0;
735 usb_callout_stop(&sc->sc_callout);
740 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
741 (MOUSE_MSC_PACKETSIZE != 5))
742 #error "Software assumptions are not met. Please update code."
746 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
747 int32_t dz, int32_t dt, int32_t buttons)
770 buf[0] = sc->sc_mode.syncmask[1];
771 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
774 buf[3] = dx - (dx >> 1);
775 buf[4] = dy - (dy >> 1);
777 if (sc->sc_mode.level == 1) {
779 buf[6] = dz - (dz >> 1);
780 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
782 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
783 sc->sc_mode.packetsize, 1);
786 DPRINTF("Buffer full, discarded packet\n");
791 ums_reset_buf(struct ums_softc *sc)
793 /* reset read queue, must be called locked */
794 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
798 ums_open(struct usb_fifo *fifo, int fflags)
800 struct ums_softc *sc = usb_fifo_softc(fifo);
804 /* check for duplicate open, should not happen */
805 if (sc->sc_fflags & fflags)
808 /* check for first open */
809 if (sc->sc_fflags == 0) {
811 /* reset all USB mouse parameters */
813 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
814 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
816 sc->sc_hw.buttons = sc->sc_buttons;
818 sc->sc_hw.iftype = MOUSE_IF_USB;
819 sc->sc_hw.type = MOUSE_MOUSE;
820 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
823 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
824 sc->sc_mode.rate = -1;
825 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
826 sc->sc_mode.accelfactor = 0;
827 sc->sc_mode.level = 0;
828 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
829 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
830 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
834 sc->sc_status.flags = 0;
835 sc->sc_status.button = 0;
836 sc->sc_status.obutton = 0;
837 sc->sc_status.dx = 0;
838 sc->sc_status.dy = 0;
839 sc->sc_status.dz = 0;
840 /* sc->sc_status.dt = 0; */
843 if (fflags & FREAD) {
844 /* allocate RX buffer */
845 if (usb_fifo_alloc_buffer(fifo,
846 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
851 sc->sc_fflags |= fflags & (FREAD | FWRITE);
856 ums_close(struct usb_fifo *fifo, int fflags)
858 struct ums_softc *sc = usb_fifo_softc(fifo);
863 usb_fifo_free_buffer(fifo);
865 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
869 ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
871 struct ums_softc *sc = usb_fifo_softc(fifo);
877 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
880 case MOUSE_GETHWINFO:
881 *(mousehw_t *)addr = sc->sc_hw;
885 *(mousemode_t *)addr = sc->sc_mode;
889 mode = *(mousemode_t *)addr;
891 if (mode.level == -1) {
892 /* don't change the current setting */
893 } else if ((mode.level < 0) || (mode.level > 1)) {
897 sc->sc_mode.level = mode.level;
900 /* store polling rate */
901 sc->sc_pollrate = mode.rate;
903 if (sc->sc_mode.level == 0) {
904 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
905 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
907 sc->sc_hw.buttons = sc->sc_buttons;
908 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
909 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
910 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
911 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
912 } else if (sc->sc_mode.level == 1) {
913 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
914 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
916 sc->sc_hw.buttons = sc->sc_buttons;
917 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
918 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
919 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
920 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
926 *(int *)addr = sc->sc_mode.level;
930 if (*(int *)addr < 0 || *(int *)addr > 1) {
934 sc->sc_mode.level = *(int *)addr;
936 if (sc->sc_mode.level == 0) {
937 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
938 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
940 sc->sc_hw.buttons = sc->sc_buttons;
941 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
942 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
943 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
944 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
945 } else if (sc->sc_mode.level == 1) {
946 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
947 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
949 sc->sc_hw.buttons = sc->sc_buttons;
950 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
951 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
952 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
953 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
958 case MOUSE_GETSTATUS:{
959 mousestatus_t *status = (mousestatus_t *)addr;
961 *status = sc->sc_status;
962 sc->sc_status.obutton = sc->sc_status.button;
963 sc->sc_status.button = 0;
964 sc->sc_status.dx = 0;
965 sc->sc_status.dy = 0;
966 sc->sc_status.dz = 0;
967 /* sc->sc_status.dt = 0; */
969 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
970 status->flags |= MOUSE_POSCHANGED;
972 if (status->button != status->obutton) {
973 status->flags |= MOUSE_BUTTONSCHANGED;
982 lockmgr(&sc->sc_lock, LK_RELEASE);
988 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
990 struct ums_softc *sc = arg1;
991 struct ums_info *info;
993 int i, j, err, had_output;
995 sb = sbuf_new_auto();
996 for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
997 info = &sc->sc_info[i];
999 /* Don't emit empty info */
1000 if ((info->sc_flags &
1001 (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1002 UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1003 info->sc_buttons == 0)
1007 sbuf_printf(sb, "\n");
1009 sbuf_printf(sb, "i%d:", i + 1);
1010 if (info->sc_flags & UMS_FLAG_X_AXIS)
1011 sbuf_printf(sb, " X:r%d, p%d, s%d;",
1012 (int)info->sc_iid_x,
1013 (int)info->sc_loc_x.pos,
1014 (int)info->sc_loc_x.size);
1015 if (info->sc_flags & UMS_FLAG_Y_AXIS)
1016 sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1017 (int)info->sc_iid_y,
1018 (int)info->sc_loc_y.pos,
1019 (int)info->sc_loc_y.size);
1020 if (info->sc_flags & UMS_FLAG_Z_AXIS)
1021 sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1022 (int)info->sc_iid_z,
1023 (int)info->sc_loc_z.pos,
1024 (int)info->sc_loc_z.size);
1025 if (info->sc_flags & UMS_FLAG_T_AXIS)
1026 sbuf_printf(sb, " T:r%d, p%d, s%d;",
1027 (int)info->sc_iid_t,
1028 (int)info->sc_loc_t.pos,
1029 (int)info->sc_loc_t.size);
1030 if (info->sc_flags & UMS_FLAG_W_AXIS)
1031 sbuf_printf(sb, " W:r%d, p%d, s%d;",
1032 (int)info->sc_iid_w,
1033 (int)info->sc_loc_w.pos,
1034 (int)info->sc_loc_w.size);
1036 for (j = 0; j < info->sc_buttons; j++) {
1037 sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1038 (int)info->sc_iid_btn[j],
1039 (int)info->sc_loc_btn[j].pos,
1040 (int)info->sc_loc_btn[j].size);
1044 err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1051 static devclass_t ums_devclass;
1053 static device_method_t ums_methods[] = {
1054 DEVMETHOD(device_probe, ums_probe),
1055 DEVMETHOD(device_attach, ums_attach),
1056 DEVMETHOD(device_detach, ums_detach),
1060 static driver_t ums_driver = {
1062 .methods = ums_methods,
1063 .size = sizeof(struct ums_softc),
1066 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, NULL);
1067 MODULE_DEPEND(ums, usb, 1, 1, 1);
1068 MODULE_VERSION(ums, 1);