2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
40 #include "opt_evdev.h"
42 #include <sys/stdint.h>
43 #include <sys/stddef.h>
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
61 #include <sys/fcntl.h>
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbhid.h>
70 #define USB_DEBUG_VAR ums_debug
71 #include <dev/usb/usb_debug.h>
73 #include <dev/usb/quirk/usb_quirk.h>
76 #include <dev/evdev/input.h>
77 #include <dev/evdev/evdev.h>
80 #include <sys/ioccom.h>
81 #include <sys/filio.h>
82 #include <sys/mouse.h>
85 static int ums_debug = 0;
87 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
89 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RWTUN,
90 &ums_debug, 0, "Debug level");
93 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
94 #define MOUSE_FLAGS (HIO_RELATIVE)
96 #define UMS_BUF_SIZE 8 /* bytes */
97 #define UMS_IFQ_MAXLEN 50 /* units */
98 #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
99 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
100 #define UMS_INFO_MAX 2 /* maximum number of HID sets */
108 struct hid_location sc_loc_w;
109 struct hid_location sc_loc_x;
110 struct hid_location sc_loc_y;
111 struct hid_location sc_loc_z;
112 struct hid_location sc_loc_t;
113 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
116 #define UMS_FLAG_X_AXIS 0x0001
117 #define UMS_FLAG_Y_AXIS 0x0002
118 #define UMS_FLAG_Z_AXIS 0x0004
119 #define UMS_FLAG_T_AXIS 0x0008
120 #define UMS_FLAG_SBU 0x0010 /* spurious button up events */
121 #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */
122 #define UMS_FLAG_W_AXIS 0x0040
129 uint8_t sc_iid_btn[UMS_BUTTON_MAX];
134 struct usb_fifo_sc sc_fifo;
136 struct usb_callout sc_callout;
137 struct ums_info sc_info[UMS_INFO_MAX];
141 mousestatus_t sc_status;
143 struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
149 #define UMS_EVDEV_OPENED 1
157 struct evdev_dev *sc_evdev;
161 static void ums_put_queue_timeout(void *__sc);
163 static usb_callback_t ums_intr_callback;
165 static device_probe_t ums_probe;
166 static device_attach_t ums_attach;
167 static device_detach_t ums_detach;
169 static usb_fifo_cmd_t ums_fifo_start_read;
170 static usb_fifo_cmd_t ums_fifo_stop_read;
171 static usb_fifo_open_t ums_fifo_open;
172 static usb_fifo_close_t ums_fifo_close;
173 static usb_fifo_ioctl_t ums_fifo_ioctl;
176 static evdev_open_t ums_ev_open;
177 static evdev_close_t ums_ev_close;
178 static void ums_evdev_push(struct ums_softc *, int32_t, int32_t,
179 int32_t, int32_t, int32_t);
182 static void ums_start_rx(struct ums_softc *);
183 static void ums_stop_rx(struct ums_softc *);
184 static void ums_put_queue(struct ums_softc *, int32_t, int32_t,
185 int32_t, int32_t, int32_t);
186 static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
188 static struct usb_fifo_methods ums_fifo_methods = {
189 .f_open = &ums_fifo_open,
190 .f_close = &ums_fifo_close,
191 .f_ioctl = &ums_fifo_ioctl,
192 .f_start_read = &ums_fifo_start_read,
193 .f_stop_read = &ums_fifo_stop_read,
194 .basename[0] = "ums",
198 static const struct evdev_methods ums_evdev_methods = {
199 .ev_open = &ums_ev_open,
200 .ev_close = &ums_ev_close,
205 ums_put_queue_timeout(void *__sc)
207 struct ums_softc *sc = __sc;
209 mtx_assert(&sc->sc_mtx, MA_OWNED);
211 ums_put_queue(sc, 0, 0, 0, 0, 0);
213 ums_evdev_push(sc, 0, 0, 0, 0, 0);
218 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
220 struct ums_softc *sc = usbd_xfer_softc(xfer);
221 struct ums_info *info = &sc->sc_info[0];
222 struct usb_page_cache *pc;
223 uint8_t *buf = sc->sc_temp;
225 int32_t buttons_found = 0;
227 int32_t buttons_reported = 0;
238 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
240 switch (USB_GET_STATE(xfer)) {
241 case USB_ST_TRANSFERRED:
242 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
244 if (len > (int)sizeof(sc->sc_temp)) {
245 DPRINTFN(6, "truncating large packet to %zu bytes\n",
246 sizeof(sc->sc_temp));
247 len = sizeof(sc->sc_temp);
252 pc = usbd_xfer_get_frame(xfer, 0);
253 usbd_copy_out(pc, 0, buf, len);
255 DPRINTFN(6, "data = %02x %02x %02x %02x "
256 "%02x %02x %02x %02x\n",
257 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
258 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
259 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
260 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
270 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
271 if ((*buf == 0x14) || (*buf == 0x15)) {
278 if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
279 (id == info->sc_iid_w))
280 dw += hid_get_data(buf, len, &info->sc_loc_w);
282 if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
283 (id == info->sc_iid_x))
284 dx += hid_get_data(buf, len, &info->sc_loc_x);
286 if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
287 (id == info->sc_iid_y))
288 dy -= hid_get_data(buf, len, &info->sc_loc_y);
290 if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
291 (id == info->sc_iid_z)) {
293 temp = hid_get_data(buf, len, &info->sc_loc_z);
294 if (info->sc_flags & UMS_FLAG_REVZ)
299 if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
300 (id == info->sc_iid_t)) {
301 dt += hid_get_data(buf, len, &info->sc_loc_t);
302 /* T-axis is translated into button presses */
303 buttons_found |= (1UL << 5) | (1UL << 6);
306 for (i = 0; i < info->sc_buttons; i++) {
308 mask = 1UL << UMS_BUT(i);
309 /* check for correct button ID */
310 if (id != info->sc_iid_btn[i])
312 /* check for button pressed */
313 if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
315 /* register button mask */
316 buttons_found |= mask;
319 if (++info != &sc->sc_info[UMS_INFO_MAX])
323 buttons_reported = buttons;
325 /* keep old button value(s) for non-detected buttons */
326 buttons |= sc->sc_status.button & ~buttons_found;
328 if (dx || dy || dz || dt || dw ||
329 (buttons != sc->sc_status.button)) {
331 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
332 dx, dy, dz, dt, dw, buttons);
334 /* translate T-axis into button presses until further */
336 ums_put_queue(sc, 0, 0, 0, 0, buttons);
339 ums_put_queue(sc, 0, 0, 0, 0, buttons);
343 sc->sc_status.button = buttons;
344 sc->sc_status.dx += dx;
345 sc->sc_status.dy += dy;
346 sc->sc_status.dz += dz;
348 * sc->sc_status.dt += dt;
349 * no way to export this yet
353 * The Qtronix keyboard has a built in PS/2
354 * port for a mouse. The firmware once in a
355 * while posts a spurious button up
356 * event. This event we ignore by doing a
357 * timeout for 50 msecs. If we receive
358 * dx=dy=dz=buttons=0 before we add the event
359 * to the queue. In any other case we delete
362 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
363 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
364 (dw == 0) && (buttons == 0)) {
366 usb_callout_reset(&sc->sc_callout, hz / 20,
367 &ums_put_queue_timeout, sc);
370 usb_callout_stop(&sc->sc_callout);
372 ums_put_queue(sc, dx, dy, dz, dt, buttons);
374 ums_evdev_push(sc, dx, dy, dz, dt,
382 /* check if we can put more data into the FIFO */
383 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) == 0) {
385 if (sc->sc_evflags == 0)
392 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
393 usbd_transfer_submit(xfer);
397 if (error != USB_ERR_CANCELLED) {
398 /* try clear stall first */
399 usbd_xfer_set_stall(xfer);
406 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
409 .type = UE_INTERRUPT,
410 .endpoint = UE_ADDR_ANY,
411 .direction = UE_DIR_IN,
412 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
413 .bufsize = 0, /* use wMaxPacketSize */
414 .callback = &ums_intr_callback,
418 /* A match on these entries will load ums */
419 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
420 {USB_IFACE_CLASS(UICLASS_HID),
421 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
422 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
426 ums_probe(device_t dev)
428 struct usb_attach_arg *uaa = device_get_ivars(dev);
435 if (uaa->usb_mode != USB_MODE_HOST)
438 if (uaa->info.bInterfaceClass != UICLASS_HID)
441 if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
444 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
445 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
446 return (BUS_PROBE_DEFAULT);
448 error = usbd_req_get_hid_desc(uaa->device, NULL,
449 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
454 if (hid_is_mouse(d_ptr, d_len))
455 error = BUS_PROBE_DEFAULT;
464 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
465 uint16_t len, uint8_t index)
467 struct ums_info *info = &sc->sc_info[index];
472 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
473 hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
475 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
476 info->sc_flags |= UMS_FLAG_X_AXIS;
479 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
480 hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
482 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
483 info->sc_flags |= UMS_FLAG_Y_AXIS;
486 /* Try the wheel first as the Z activator since it's tradition. */
487 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
488 HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
490 hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
491 HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
493 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
494 info->sc_flags |= UMS_FLAG_Z_AXIS;
497 * We might have both a wheel and Z direction, if so put
498 * put the Z on the W coordinate.
500 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
501 HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
504 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
505 info->sc_flags |= UMS_FLAG_W_AXIS;
508 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
509 HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
512 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
513 info->sc_flags |= UMS_FLAG_Z_AXIS;
517 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
518 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
519 * to know that the byte after the wheel is the tilt axis.
520 * There are no other HID axis descriptors other than X,Y and
523 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
524 HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
525 &flags, &info->sc_iid_t)) {
527 info->sc_loc_t.pos += 8;
529 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
530 info->sc_flags |= UMS_FLAG_T_AXIS;
532 } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
533 HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
534 &flags, &info->sc_iid_t)) {
536 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
537 info->sc_flags |= UMS_FLAG_T_AXIS;
539 /* figure out the number of buttons */
541 for (i = 0; i < UMS_BUTTON_MAX; i++) {
542 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
543 hid_input, index, &info->sc_loc_btn[i], NULL,
544 &info->sc_iid_btn[i])) {
549 /* detect other buttons */
551 for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
552 if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
553 hid_input, index, &info->sc_loc_btn[i], NULL,
554 &info->sc_iid_btn[i])) {
559 info->sc_buttons = i;
561 if (i > sc->sc_buttons)
564 if (info->sc_flags == 0)
567 /* announce information about the mouse */
568 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
570 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
571 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
572 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
573 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
574 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
579 ums_attach(device_t dev)
581 struct usb_attach_arg *uaa = device_get_ivars(dev);
582 struct ums_softc *sc = device_get_softc(dev);
583 struct ums_info *info;
593 DPRINTFN(11, "sc=%p\n", sc);
595 device_set_usb_desc(dev);
597 mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
599 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
602 * Force the report (non-boot) protocol.
604 * Mice without boot protocol support may choose not to implement
605 * Set_Protocol at all; Ignore any error.
607 err = usbd_req_set_protocol(uaa->device, NULL,
608 uaa->info.bIfaceIndex, 1);
610 err = usbd_transfer_setup(uaa->device,
611 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
612 UMS_N_TRANSFER, sc, &sc->sc_mtx);
615 DPRINTF("error=%s\n", usbd_errstr(err));
619 /* Get HID descriptor */
620 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
621 &d_len, M_TEMP, uaa->info.bIfaceIndex);
624 device_printf(dev, "error reading report description\n");
628 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
631 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
632 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
633 * all of its other button positions are all off. It also reports that
634 * it has two additional buttons and a tilt wheel.
636 if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
640 info = &sc->sc_info[0];
641 info->sc_flags = (UMS_FLAG_X_AXIS |
645 info->sc_buttons = 3;
647 /* 1st byte of descriptor report contains garbage */
648 info->sc_loc_x.pos = 16;
649 info->sc_loc_x.size = 8;
650 info->sc_loc_y.pos = 24;
651 info->sc_loc_y.size = 8;
652 info->sc_loc_z.pos = 32;
653 info->sc_loc_z.size = 8;
654 info->sc_loc_btn[0].pos = 8;
655 info->sc_loc_btn[0].size = 1;
656 info->sc_loc_btn[1].pos = 9;
657 info->sc_loc_btn[1].size = 1;
658 info->sc_loc_btn[2].pos = 10;
659 info->sc_loc_btn[2].size = 1;
661 /* Announce device */
662 device_printf(dev, "3 buttons and [XYZ] "
663 "coordinates ID=0\n");
666 /* Search the HID descriptor and announce device */
667 for (i = 0; i < UMS_INFO_MAX; i++) {
668 ums_hid_parse(sc, dev, d_ptr, d_len, i);
672 if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
673 info = &sc->sc_info[0];
674 /* Some wheels need the Z axis reversed. */
675 info->sc_flags |= UMS_FLAG_REVZ;
677 if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
678 DPRINTF("WARNING: report size, %d bytes, is larger "
679 "than interrupt size, %d bytes!\n", isize,
680 usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
686 for (j = 0; j < UMS_INFO_MAX; j++) {
687 info = &sc->sc_info[j];
689 DPRINTF("sc=%p, index=%d\n", sc, j);
690 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
691 info->sc_loc_x.size, info->sc_iid_x);
692 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
693 info->sc_loc_y.size, info->sc_iid_y);
694 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
695 info->sc_loc_z.size, info->sc_iid_z);
696 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
697 info->sc_loc_t.size, info->sc_iid_t);
698 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
699 info->sc_loc_w.size, info->sc_iid_w);
701 for (i = 0; i < info->sc_buttons; i++) {
702 DPRINTF("B%d\t%d/%d id=%d\n",
703 i + 1, info->sc_loc_btn[i].pos,
704 info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
707 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
710 err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
711 &ums_fifo_methods, &sc->sc_fifo,
712 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
713 UID_ROOT, GID_OPERATOR, 0644);
718 sc->sc_evdev = evdev_alloc();
719 evdev_set_name(sc->sc_evdev, device_get_desc(dev));
720 evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
721 evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor,
722 uaa->info.idProduct, 0);
723 evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device));
724 evdev_set_methods(sc->sc_evdev, sc, &ums_evdev_methods);
725 evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER);
726 evdev_support_event(sc->sc_evdev, EV_SYN);
727 evdev_support_event(sc->sc_evdev, EV_REL);
728 evdev_support_event(sc->sc_evdev, EV_KEY);
730 info = &sc->sc_info[0];
732 if (info->sc_flags & UMS_FLAG_X_AXIS)
733 evdev_support_rel(sc->sc_evdev, REL_X);
735 if (info->sc_flags & UMS_FLAG_Y_AXIS)
736 evdev_support_rel(sc->sc_evdev, REL_Y);
738 if (info->sc_flags & UMS_FLAG_Z_AXIS)
739 evdev_support_rel(sc->sc_evdev, REL_WHEEL);
741 if (info->sc_flags & UMS_FLAG_T_AXIS)
742 evdev_support_rel(sc->sc_evdev, REL_HWHEEL);
744 for (i = 0; i < info->sc_buttons; i++)
745 evdev_support_key(sc->sc_evdev, BTN_MOUSE + i);
747 err = evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx);
752 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
753 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
754 OID_AUTO, "parseinfo",
755 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
756 sc, 0, ums_sysctl_handler_parseinfo, "",
757 "Dump of parsed HID report descriptor");
770 ums_detach(device_t self)
772 struct ums_softc *sc = device_get_softc(self);
774 DPRINTF("sc=%p\n", sc);
776 usb_fifo_detach(&sc->sc_fifo);
779 evdev_free(sc->sc_evdev);
782 usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
784 usb_callout_drain(&sc->sc_callout);
786 mtx_destroy(&sc->sc_mtx);
792 ums_reset(struct ums_softc *sc)
795 /* reset all USB mouse parameters */
797 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
798 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
800 sc->sc_hw.buttons = sc->sc_buttons;
802 sc->sc_hw.iftype = MOUSE_IF_USB;
803 sc->sc_hw.type = MOUSE_MOUSE;
804 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
807 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
808 sc->sc_mode.rate = -1;
809 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
810 sc->sc_mode.accelfactor = 0;
811 sc->sc_mode.level = 0;
812 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
813 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
814 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
818 sc->sc_status.flags = 0;
819 sc->sc_status.button = 0;
820 sc->sc_status.obutton = 0;
821 sc->sc_status.dx = 0;
822 sc->sc_status.dy = 0;
823 sc->sc_status.dz = 0;
824 /* sc->sc_status.dt = 0; */
828 ums_start_rx(struct ums_softc *sc)
832 /* Check if we should override the default polling interval */
833 rate = sc->sc_pollrate;
834 /* Range check rate */
837 /* Check for set rate */
838 if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
839 DPRINTF("Setting pollrate = %d\n", rate);
840 /* Stop current transfer, if any */
841 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
842 /* Set new interval */
843 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
844 /* Only set pollrate once */
848 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
852 ums_stop_rx(struct ums_softc *sc)
854 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
855 usb_callout_stop(&sc->sc_callout);
859 ums_fifo_start_read(struct usb_fifo *fifo)
861 struct ums_softc *sc = usb_fifo_softc(fifo);
867 ums_fifo_stop_read(struct usb_fifo *fifo)
869 struct ums_softc *sc = usb_fifo_softc(fifo);
875 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
876 (MOUSE_MSC_PACKETSIZE != 5))
877 #error "Software assumptions are not met. Please update code."
881 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
882 int32_t dz, int32_t dt, int32_t buttons)
905 buf[0] = sc->sc_mode.syncmask[1];
906 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
909 buf[3] = dx - (dx >> 1);
910 buf[4] = dy - (dy >> 1);
912 if (sc->sc_mode.level == 1) {
914 buf[6] = dz - (dz >> 1);
915 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
917 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
918 sc->sc_mode.packetsize, 1);
920 DPRINTF("Buffer full, discarded packet\n");
926 ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy,
927 int32_t dz, int32_t dt, int32_t buttons)
929 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
930 /* Push evdev event */
931 evdev_push_rel(sc->sc_evdev, REL_X, dx);
932 evdev_push_rel(sc->sc_evdev, REL_Y, -dy);
933 evdev_push_rel(sc->sc_evdev, REL_WHEEL, -dz);
934 evdev_push_rel(sc->sc_evdev, REL_HWHEEL, dt);
935 evdev_push_mouse_btn(sc->sc_evdev,
936 (buttons & ~MOUSE_STDBUTTONS) |
937 (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) |
938 (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) |
939 (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0));
940 evdev_sync(sc->sc_evdev);
946 ums_reset_buf(struct ums_softc *sc)
948 /* reset read queue, must be called locked */
949 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
954 ums_ev_open(struct evdev_dev *evdev)
956 struct ums_softc *sc = evdev_get_softc(evdev);
958 mtx_assert(&sc->sc_mtx, MA_OWNED);
960 sc->sc_evflags = UMS_EVDEV_OPENED;
962 if (sc->sc_fflags == 0) {
971 ums_ev_close(struct evdev_dev *evdev)
973 struct ums_softc *sc = evdev_get_softc(evdev);
975 mtx_assert(&sc->sc_mtx, MA_OWNED);
979 if (sc->sc_fflags == 0)
987 ums_fifo_open(struct usb_fifo *fifo, int fflags)
989 struct ums_softc *sc = usb_fifo_softc(fifo);
993 /* check for duplicate open, should not happen */
994 if (sc->sc_fflags & fflags)
997 /* check for first open */
999 if (sc->sc_fflags == 0 && sc->sc_evflags == 0)
1002 if (sc->sc_fflags == 0)
1006 if (fflags & FREAD) {
1007 /* allocate RX buffer */
1008 if (usb_fifo_alloc_buffer(fifo,
1009 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1014 sc->sc_fflags |= fflags & (FREAD | FWRITE);
1019 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1021 struct ums_softc *sc = usb_fifo_softc(fifo);
1026 usb_fifo_free_buffer(fifo);
1028 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1032 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1034 struct ums_softc *sc = usb_fifo_softc(fifo);
1040 mtx_lock(&sc->sc_mtx);
1043 case MOUSE_GETHWINFO:
1044 *(mousehw_t *)addr = sc->sc_hw;
1048 *(mousemode_t *)addr = sc->sc_mode;
1052 mode = *(mousemode_t *)addr;
1054 if (mode.level == -1) {
1055 /* don't change the current setting */
1056 } else if ((mode.level < 0) || (mode.level > 1)) {
1060 sc->sc_mode.level = mode.level;
1063 /* store polling rate */
1064 sc->sc_pollrate = mode.rate;
1066 if (sc->sc_mode.level == 0) {
1067 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1068 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1070 sc->sc_hw.buttons = sc->sc_buttons;
1071 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1072 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1073 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1074 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1075 } else if (sc->sc_mode.level == 1) {
1076 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1077 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1079 sc->sc_hw.buttons = sc->sc_buttons;
1080 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1081 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1082 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1083 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1088 case MOUSE_GETLEVEL:
1089 *(int *)addr = sc->sc_mode.level;
1092 case MOUSE_SETLEVEL:
1093 if (*(int *)addr < 0 || *(int *)addr > 1) {
1097 sc->sc_mode.level = *(int *)addr;
1099 if (sc->sc_mode.level == 0) {
1100 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1101 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1103 sc->sc_hw.buttons = sc->sc_buttons;
1104 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1105 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1106 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1107 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1108 } else if (sc->sc_mode.level == 1) {
1109 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1110 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1112 sc->sc_hw.buttons = sc->sc_buttons;
1113 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1114 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1115 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1116 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1121 case MOUSE_GETSTATUS:{
1122 mousestatus_t *status = (mousestatus_t *)addr;
1124 *status = sc->sc_status;
1125 sc->sc_status.obutton = sc->sc_status.button;
1126 sc->sc_status.button = 0;
1127 sc->sc_status.dx = 0;
1128 sc->sc_status.dy = 0;
1129 sc->sc_status.dz = 0;
1130 /* sc->sc_status.dt = 0; */
1132 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1133 status->flags |= MOUSE_POSCHANGED;
1135 if (status->button != status->obutton) {
1136 status->flags |= MOUSE_BUTTONSCHANGED;
1145 mtx_unlock(&sc->sc_mtx);
1150 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1152 struct ums_softc *sc = arg1;
1153 struct ums_info *info;
1155 int i, j, err, had_output;
1157 sb = sbuf_new_auto();
1158 for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1159 info = &sc->sc_info[i];
1161 /* Don't emit empty info */
1162 if ((info->sc_flags &
1163 (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1164 UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1165 info->sc_buttons == 0)
1169 sbuf_printf(sb, "\n");
1171 sbuf_printf(sb, "i%d:", i + 1);
1172 if (info->sc_flags & UMS_FLAG_X_AXIS)
1173 sbuf_printf(sb, " X:r%d, p%d, s%d;",
1174 (int)info->sc_iid_x,
1175 (int)info->sc_loc_x.pos,
1176 (int)info->sc_loc_x.size);
1177 if (info->sc_flags & UMS_FLAG_Y_AXIS)
1178 sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1179 (int)info->sc_iid_y,
1180 (int)info->sc_loc_y.pos,
1181 (int)info->sc_loc_y.size);
1182 if (info->sc_flags & UMS_FLAG_Z_AXIS)
1183 sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1184 (int)info->sc_iid_z,
1185 (int)info->sc_loc_z.pos,
1186 (int)info->sc_loc_z.size);
1187 if (info->sc_flags & UMS_FLAG_T_AXIS)
1188 sbuf_printf(sb, " T:r%d, p%d, s%d;",
1189 (int)info->sc_iid_t,
1190 (int)info->sc_loc_t.pos,
1191 (int)info->sc_loc_t.size);
1192 if (info->sc_flags & UMS_FLAG_W_AXIS)
1193 sbuf_printf(sb, " W:r%d, p%d, s%d;",
1194 (int)info->sc_iid_w,
1195 (int)info->sc_loc_w.pos,
1196 (int)info->sc_loc_w.size);
1198 for (j = 0; j < info->sc_buttons; j++) {
1199 sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1200 (int)info->sc_iid_btn[j],
1201 (int)info->sc_loc_btn[j].pos,
1202 (int)info->sc_loc_btn[j].size);
1206 err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1212 static devclass_t ums_devclass;
1214 static device_method_t ums_methods[] = {
1215 DEVMETHOD(device_probe, ums_probe),
1216 DEVMETHOD(device_attach, ums_attach),
1217 DEVMETHOD(device_detach, ums_detach),
1222 static driver_t ums_driver = {
1224 .methods = ums_methods,
1225 .size = sizeof(struct ums_softc),
1228 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
1229 MODULE_DEPEND(ums, usb, 1, 1, 1);
1230 #ifdef EVDEV_SUPPORT
1231 MODULE_DEPEND(ums, evdev, 1, 1, 1);
1233 MODULE_VERSION(ums, 1);
1234 USB_PNP_HOST_INFO(ums_devs);