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/hid/hid.h>
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include <dev/usb/usbhid.h>
72 #define USB_DEBUG_VAR ums_debug
73 #include <dev/usb/usb_debug.h>
75 #include <dev/usb/quirk/usb_quirk.h>
78 #include <dev/evdev/input.h>
79 #include <dev/evdev/evdev.h>
82 #include <sys/ioccom.h>
83 #include <sys/filio.h>
84 #include <sys/mouse.h>
87 static int ums_debug = 0;
89 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
91 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RWTUN,
92 &ums_debug, 0, "Debug level");
95 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
96 #define MOUSE_FLAGS (HIO_RELATIVE)
98 #define UMS_BUF_SIZE 8 /* bytes */
99 #define UMS_IFQ_MAXLEN 50 /* units */
100 #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
101 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
102 #define UMS_INFO_MAX 2 /* maximum number of HID sets */
110 struct hid_location sc_loc_w;
111 struct hid_location sc_loc_x;
112 struct hid_location sc_loc_y;
113 struct hid_location sc_loc_z;
114 struct hid_location sc_loc_t;
115 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
118 #define UMS_FLAG_X_AXIS 0x0001
119 #define UMS_FLAG_Y_AXIS 0x0002
120 #define UMS_FLAG_Z_AXIS 0x0004
121 #define UMS_FLAG_T_AXIS 0x0008
122 #define UMS_FLAG_SBU 0x0010 /* spurious button up events */
123 #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */
124 #define UMS_FLAG_W_AXIS 0x0040
131 uint8_t sc_iid_btn[UMS_BUTTON_MAX];
136 struct usb_fifo_sc sc_fifo;
138 struct usb_callout sc_callout;
139 struct ums_info sc_info[UMS_INFO_MAX];
143 mousestatus_t sc_status;
145 struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
151 #define UMS_EVDEV_OPENED 1
159 struct evdev_dev *sc_evdev;
163 static void ums_put_queue_timeout(void *__sc);
165 static usb_callback_t ums_intr_callback;
167 static device_probe_t ums_probe;
168 static device_attach_t ums_attach;
169 static device_detach_t ums_detach;
171 static usb_fifo_cmd_t ums_fifo_start_read;
172 static usb_fifo_cmd_t ums_fifo_stop_read;
173 static usb_fifo_open_t ums_fifo_open;
174 static usb_fifo_close_t ums_fifo_close;
175 static usb_fifo_ioctl_t ums_fifo_ioctl;
178 static evdev_open_t ums_ev_open;
179 static evdev_close_t ums_ev_close;
180 static void ums_evdev_push(struct ums_softc *, int32_t, int32_t,
181 int32_t, int32_t, int32_t);
184 static void ums_start_rx(struct ums_softc *);
185 static void ums_stop_rx(struct ums_softc *);
186 static void ums_put_queue(struct ums_softc *, int32_t, int32_t,
187 int32_t, int32_t, int32_t);
188 static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
190 static struct usb_fifo_methods ums_fifo_methods = {
191 .f_open = &ums_fifo_open,
192 .f_close = &ums_fifo_close,
193 .f_ioctl = &ums_fifo_ioctl,
194 .f_start_read = &ums_fifo_start_read,
195 .f_stop_read = &ums_fifo_stop_read,
196 .basename[0] = "ums",
200 static const struct evdev_methods ums_evdev_methods = {
201 .ev_open = &ums_ev_open,
202 .ev_close = &ums_ev_close,
207 ums_put_queue_timeout(void *__sc)
209 struct ums_softc *sc = __sc;
211 mtx_assert(&sc->sc_mtx, MA_OWNED);
213 ums_put_queue(sc, 0, 0, 0, 0, 0);
215 ums_evdev_push(sc, 0, 0, 0, 0, 0);
220 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
222 struct ums_softc *sc = usbd_xfer_softc(xfer);
223 struct ums_info *info = &sc->sc_info[0];
224 struct usb_page_cache *pc;
225 uint8_t *buf = sc->sc_temp;
227 int32_t buttons_found = 0;
229 int32_t buttons_reported = 0;
240 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
242 switch (USB_GET_STATE(xfer)) {
243 case USB_ST_TRANSFERRED:
244 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
246 if (len > (int)sizeof(sc->sc_temp)) {
247 DPRINTFN(6, "truncating large packet to %zu bytes\n",
248 sizeof(sc->sc_temp));
249 len = sizeof(sc->sc_temp);
254 pc = usbd_xfer_get_frame(xfer, 0);
255 usbd_copy_out(pc, 0, buf, len);
257 DPRINTFN(6, "data = %02x %02x %02x %02x "
258 "%02x %02x %02x %02x\n",
259 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
260 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
261 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
262 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
272 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
273 if ((*buf == 0x14) || (*buf == 0x15)) {
280 if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
281 (id == info->sc_iid_w))
282 dw += hid_get_data(buf, len, &info->sc_loc_w);
284 if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
285 (id == info->sc_iid_x))
286 dx += hid_get_data(buf, len, &info->sc_loc_x);
288 if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
289 (id == info->sc_iid_y))
290 dy -= hid_get_data(buf, len, &info->sc_loc_y);
292 if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
293 (id == info->sc_iid_z)) {
295 temp = hid_get_data(buf, len, &info->sc_loc_z);
296 if (info->sc_flags & UMS_FLAG_REVZ)
301 if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
302 (id == info->sc_iid_t)) {
303 dt += hid_get_data(buf, len, &info->sc_loc_t);
304 /* T-axis is translated into button presses */
305 buttons_found |= (1UL << 5) | (1UL << 6);
308 for (i = 0; i < info->sc_buttons; i++) {
310 mask = 1UL << UMS_BUT(i);
311 /* check for correct button ID */
312 if (id != info->sc_iid_btn[i])
314 /* check for button pressed */
315 if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
317 /* register button mask */
318 buttons_found |= mask;
321 if (++info != &sc->sc_info[UMS_INFO_MAX])
325 buttons_reported = buttons;
327 /* keep old button value(s) for non-detected buttons */
328 buttons |= sc->sc_status.button & ~buttons_found;
330 if (dx || dy || dz || dt || dw ||
331 (buttons != sc->sc_status.button)) {
332 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
333 dx, dy, dz, dt, dw, buttons);
335 /* translate T-axis into button presses until further */
337 ums_put_queue(sc, 0, 0, 0, 0, buttons);
340 ums_put_queue(sc, 0, 0, 0, 0, buttons);
344 sc->sc_status.button = buttons;
345 sc->sc_status.dx += dx;
346 sc->sc_status.dy += dy;
347 sc->sc_status.dz += dz;
349 * sc->sc_status.dt += dt;
350 * no way to export this yet
354 * The Qtronix keyboard has a built in PS/2
355 * port for a mouse. The firmware once in a
356 * while posts a spurious button up
357 * event. This event we ignore by doing a
358 * timeout for 50 msecs. If we receive
359 * dx=dy=dz=buttons=0 before we add the event
360 * to the queue. In any other case we delete
363 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
364 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
365 (dw == 0) && (buttons == 0)) {
366 usb_callout_reset(&sc->sc_callout, hz / 20,
367 &ums_put_queue_timeout, sc);
369 usb_callout_stop(&sc->sc_callout);
371 ums_put_queue(sc, dx, dy, dz, dt, buttons);
373 ums_evdev_push(sc, dx, dy, dz, dt,
380 /* check if we can put more data into the FIFO */
381 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) == 0) {
383 if (sc->sc_evflags == 0)
390 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
391 usbd_transfer_submit(xfer);
395 if (error != USB_ERR_CANCELLED) {
396 /* try clear stall first */
397 usbd_xfer_set_stall(xfer);
404 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
406 .type = UE_INTERRUPT,
407 .endpoint = UE_ADDR_ANY,
408 .direction = UE_DIR_IN,
409 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
410 .bufsize = 0, /* use wMaxPacketSize */
411 .callback = &ums_intr_callback,
415 /* A match on these entries will load ums */
416 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
417 {USB_IFACE_CLASS(UICLASS_HID),
418 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
419 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
423 ums_probe(device_t dev)
425 struct usb_attach_arg *uaa = device_get_ivars(dev);
432 if (uaa->usb_mode != USB_MODE_HOST)
435 if (uaa->info.bInterfaceClass != UICLASS_HID)
438 if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
441 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
442 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
443 return (BUS_PROBE_DEFAULT);
445 error = usbd_req_get_hid_desc(uaa->device, NULL,
446 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
451 if (hid_is_mouse(d_ptr, d_len))
452 error = BUS_PROBE_DEFAULT;
461 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
462 uint16_t len, uint8_t index)
464 struct ums_info *info = &sc->sc_info[index];
469 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
470 hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
471 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
472 info->sc_flags |= UMS_FLAG_X_AXIS;
475 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
476 hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
477 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
478 info->sc_flags |= UMS_FLAG_Y_AXIS;
481 /* Try the wheel first as the Z activator since it's tradition. */
482 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
483 HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
485 hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
486 HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
488 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
489 info->sc_flags |= UMS_FLAG_Z_AXIS;
492 * We might have both a wheel and Z direction, if so put
493 * put the Z on the W coordinate.
495 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
496 HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
498 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
499 info->sc_flags |= UMS_FLAG_W_AXIS;
502 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
503 HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
505 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
506 info->sc_flags |= UMS_FLAG_Z_AXIS;
510 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
511 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
512 * to know that the byte after the wheel is the tilt axis.
513 * There are no other HID axis descriptors other than X,Y and
516 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
517 HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
518 &flags, &info->sc_iid_t)) {
519 info->sc_loc_t.pos += 8;
521 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
522 info->sc_flags |= UMS_FLAG_T_AXIS;
524 } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
525 HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
526 &flags, &info->sc_iid_t)) {
527 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
528 info->sc_flags |= UMS_FLAG_T_AXIS;
530 /* figure out the number of buttons */
532 for (i = 0; i < UMS_BUTTON_MAX; i++) {
533 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
534 hid_input, index, &info->sc_loc_btn[i], NULL,
535 &info->sc_iid_btn[i])) {
540 /* detect other buttons */
542 for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
543 if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
544 hid_input, index, &info->sc_loc_btn[i], NULL,
545 &info->sc_iid_btn[i])) {
550 info->sc_buttons = i;
552 if (i > sc->sc_buttons)
555 if (info->sc_flags == 0)
558 /* announce information about the mouse */
559 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
561 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
562 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
563 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
564 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
565 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
570 ums_attach(device_t dev)
572 struct usb_attach_arg *uaa = device_get_ivars(dev);
573 struct ums_softc *sc = device_get_softc(dev);
574 struct ums_info *info;
584 DPRINTFN(11, "sc=%p\n", sc);
586 device_set_usb_desc(dev);
588 mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
590 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
593 * Force the report (non-boot) protocol.
595 * Mice without boot protocol support may choose not to implement
596 * Set_Protocol at all; Ignore any error.
598 err = usbd_req_set_protocol(uaa->device, NULL,
599 uaa->info.bIfaceIndex, 1);
601 err = usbd_transfer_setup(uaa->device,
602 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
603 UMS_N_TRANSFER, sc, &sc->sc_mtx);
606 DPRINTF("error=%s\n", usbd_errstr(err));
610 /* Get HID descriptor */
611 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
612 &d_len, M_TEMP, uaa->info.bIfaceIndex);
615 device_printf(dev, "error reading report description\n");
619 isize = hid_report_size_max(d_ptr, d_len, hid_input, &sc->sc_iid);
622 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
623 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
624 * all of its other button positions are all off. It also reports that
625 * it has two additional buttons and a tilt wheel.
627 if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
630 info = &sc->sc_info[0];
631 info->sc_flags = (UMS_FLAG_X_AXIS |
635 info->sc_buttons = 3;
637 /* 1st byte of descriptor report contains garbage */
638 info->sc_loc_x.pos = 16;
639 info->sc_loc_x.size = 8;
640 info->sc_loc_y.pos = 24;
641 info->sc_loc_y.size = 8;
642 info->sc_loc_z.pos = 32;
643 info->sc_loc_z.size = 8;
644 info->sc_loc_btn[0].pos = 8;
645 info->sc_loc_btn[0].size = 1;
646 info->sc_loc_btn[1].pos = 9;
647 info->sc_loc_btn[1].size = 1;
648 info->sc_loc_btn[2].pos = 10;
649 info->sc_loc_btn[2].size = 1;
651 /* Announce device */
652 device_printf(dev, "3 buttons and [XYZ] "
653 "coordinates ID=0\n");
656 /* Search the HID descriptor and announce device */
657 for (i = 0; i < UMS_INFO_MAX; i++) {
658 ums_hid_parse(sc, dev, d_ptr, d_len, i);
662 if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
663 info = &sc->sc_info[0];
664 /* Some wheels need the Z axis reversed. */
665 info->sc_flags |= UMS_FLAG_REVZ;
667 if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
668 DPRINTF("WARNING: report size, %d bytes, is larger "
669 "than interrupt size, %d bytes!\n", isize,
670 usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
676 for (j = 0; j < UMS_INFO_MAX; j++) {
677 info = &sc->sc_info[j];
679 DPRINTF("sc=%p, index=%d\n", sc, j);
680 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
681 info->sc_loc_x.size, info->sc_iid_x);
682 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
683 info->sc_loc_y.size, info->sc_iid_y);
684 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
685 info->sc_loc_z.size, info->sc_iid_z);
686 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
687 info->sc_loc_t.size, info->sc_iid_t);
688 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
689 info->sc_loc_w.size, info->sc_iid_w);
691 for (i = 0; i < info->sc_buttons; i++) {
692 DPRINTF("B%d\t%d/%d id=%d\n",
693 i + 1, info->sc_loc_btn[i].pos,
694 info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
697 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
700 err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
701 &ums_fifo_methods, &sc->sc_fifo,
702 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
703 UID_ROOT, GID_OPERATOR, 0644);
708 sc->sc_evdev = evdev_alloc();
709 evdev_set_name(sc->sc_evdev, device_get_desc(dev));
710 evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
711 evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor,
712 uaa->info.idProduct, 0);
713 evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device));
714 evdev_set_methods(sc->sc_evdev, sc, &ums_evdev_methods);
715 evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER);
716 evdev_support_event(sc->sc_evdev, EV_SYN);
717 evdev_support_event(sc->sc_evdev, EV_REL);
718 evdev_support_event(sc->sc_evdev, EV_KEY);
720 info = &sc->sc_info[0];
722 if (info->sc_flags & UMS_FLAG_X_AXIS)
723 evdev_support_rel(sc->sc_evdev, REL_X);
725 if (info->sc_flags & UMS_FLAG_Y_AXIS)
726 evdev_support_rel(sc->sc_evdev, REL_Y);
728 if (info->sc_flags & UMS_FLAG_Z_AXIS)
729 evdev_support_rel(sc->sc_evdev, REL_WHEEL);
731 if (info->sc_flags & UMS_FLAG_T_AXIS)
732 evdev_support_rel(sc->sc_evdev, REL_HWHEEL);
734 for (i = 0; i < info->sc_buttons; i++)
735 evdev_support_key(sc->sc_evdev, BTN_MOUSE + i);
737 err = evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx);
742 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
743 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
744 OID_AUTO, "parseinfo",
745 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
746 sc, 0, ums_sysctl_handler_parseinfo, "",
747 "Dump of parsed HID report descriptor");
760 ums_detach(device_t self)
762 struct ums_softc *sc = device_get_softc(self);
764 DPRINTF("sc=%p\n", sc);
766 usb_fifo_detach(&sc->sc_fifo);
769 evdev_free(sc->sc_evdev);
772 usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
774 usb_callout_drain(&sc->sc_callout);
776 mtx_destroy(&sc->sc_mtx);
782 ums_reset(struct ums_softc *sc)
785 /* reset all USB mouse parameters */
787 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
788 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
790 sc->sc_hw.buttons = sc->sc_buttons;
792 sc->sc_hw.iftype = MOUSE_IF_USB;
793 sc->sc_hw.type = MOUSE_MOUSE;
794 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
797 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
798 sc->sc_mode.rate = -1;
799 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
800 sc->sc_mode.accelfactor = 0;
801 sc->sc_mode.level = 0;
802 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
803 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
804 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
808 sc->sc_status.flags = 0;
809 sc->sc_status.button = 0;
810 sc->sc_status.obutton = 0;
811 sc->sc_status.dx = 0;
812 sc->sc_status.dy = 0;
813 sc->sc_status.dz = 0;
814 /* sc->sc_status.dt = 0; */
818 ums_start_rx(struct ums_softc *sc)
822 /* Check if we should override the default polling interval */
823 rate = sc->sc_pollrate;
824 /* Range check rate */
827 /* Check for set rate */
828 if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
829 DPRINTF("Setting pollrate = %d\n", rate);
830 /* Stop current transfer, if any */
831 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
832 /* Set new interval */
833 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
834 /* Only set pollrate once */
838 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
842 ums_stop_rx(struct ums_softc *sc)
844 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
845 usb_callout_stop(&sc->sc_callout);
849 ums_fifo_start_read(struct usb_fifo *fifo)
851 struct ums_softc *sc = usb_fifo_softc(fifo);
857 ums_fifo_stop_read(struct usb_fifo *fifo)
859 struct ums_softc *sc = usb_fifo_softc(fifo);
864 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
865 (MOUSE_MSC_PACKETSIZE != 5))
866 #error "Software assumptions are not met. Please update code."
870 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
871 int32_t dz, int32_t dt, int32_t buttons)
893 buf[0] = sc->sc_mode.syncmask[1];
894 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
897 buf[3] = dx - (dx >> 1);
898 buf[4] = dy - (dy >> 1);
900 if (sc->sc_mode.level == 1) {
902 buf[6] = dz - (dz >> 1);
903 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
905 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
906 sc->sc_mode.packetsize, 1);
908 DPRINTF("Buffer full, discarded packet\n");
914 ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy,
915 int32_t dz, int32_t dt, int32_t buttons)
917 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
918 /* Push evdev event */
919 evdev_push_rel(sc->sc_evdev, REL_X, dx);
920 evdev_push_rel(sc->sc_evdev, REL_Y, -dy);
921 evdev_push_rel(sc->sc_evdev, REL_WHEEL, -dz);
922 evdev_push_rel(sc->sc_evdev, REL_HWHEEL, dt);
923 evdev_push_mouse_btn(sc->sc_evdev,
924 (buttons & ~MOUSE_STDBUTTONS) |
925 (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) |
926 (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) |
927 (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0));
928 evdev_sync(sc->sc_evdev);
934 ums_reset_buf(struct ums_softc *sc)
936 /* reset read queue, must be called locked */
937 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
942 ums_ev_open(struct evdev_dev *evdev)
944 struct ums_softc *sc = evdev_get_softc(evdev);
946 mtx_assert(&sc->sc_mtx, MA_OWNED);
948 sc->sc_evflags = UMS_EVDEV_OPENED;
950 if (sc->sc_fflags == 0) {
959 ums_ev_close(struct evdev_dev *evdev)
961 struct ums_softc *sc = evdev_get_softc(evdev);
963 mtx_assert(&sc->sc_mtx, MA_OWNED);
967 if (sc->sc_fflags == 0)
975 ums_fifo_open(struct usb_fifo *fifo, int fflags)
977 struct ums_softc *sc = usb_fifo_softc(fifo);
981 /* check for duplicate open, should not happen */
982 if (sc->sc_fflags & fflags)
985 /* check for first open */
987 if (sc->sc_fflags == 0 && sc->sc_evflags == 0)
990 if (sc->sc_fflags == 0)
994 if (fflags & FREAD) {
995 /* allocate RX buffer */
996 if (usb_fifo_alloc_buffer(fifo,
997 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1002 sc->sc_fflags |= fflags & (FREAD | FWRITE);
1007 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1009 struct ums_softc *sc = usb_fifo_softc(fifo);
1014 usb_fifo_free_buffer(fifo);
1016 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1020 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1022 struct ums_softc *sc = usb_fifo_softc(fifo);
1028 mtx_lock(&sc->sc_mtx);
1031 case MOUSE_GETHWINFO:
1032 *(mousehw_t *)addr = sc->sc_hw;
1036 *(mousemode_t *)addr = sc->sc_mode;
1040 mode = *(mousemode_t *)addr;
1042 if (mode.level == -1) {
1043 /* don't change the current setting */
1044 } else if ((mode.level < 0) || (mode.level > 1)) {
1048 sc->sc_mode.level = mode.level;
1051 /* store polling rate */
1052 sc->sc_pollrate = mode.rate;
1054 if (sc->sc_mode.level == 0) {
1055 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1056 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1058 sc->sc_hw.buttons = sc->sc_buttons;
1059 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1060 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1061 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1062 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1063 } else if (sc->sc_mode.level == 1) {
1064 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1065 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1067 sc->sc_hw.buttons = sc->sc_buttons;
1068 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1069 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1070 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1071 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1076 case MOUSE_GETLEVEL:
1077 *(int *)addr = sc->sc_mode.level;
1080 case MOUSE_SETLEVEL:
1081 if (*(int *)addr < 0 || *(int *)addr > 1) {
1085 sc->sc_mode.level = *(int *)addr;
1087 if (sc->sc_mode.level == 0) {
1088 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1089 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1091 sc->sc_hw.buttons = sc->sc_buttons;
1092 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1093 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1094 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1095 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1096 } else if (sc->sc_mode.level == 1) {
1097 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1098 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1100 sc->sc_hw.buttons = sc->sc_buttons;
1101 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1102 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1103 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1104 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1109 case MOUSE_GETSTATUS:{
1110 mousestatus_t *status = (mousestatus_t *)addr;
1112 *status = sc->sc_status;
1113 sc->sc_status.obutton = sc->sc_status.button;
1114 sc->sc_status.button = 0;
1115 sc->sc_status.dx = 0;
1116 sc->sc_status.dy = 0;
1117 sc->sc_status.dz = 0;
1118 /* sc->sc_status.dt = 0; */
1120 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1121 status->flags |= MOUSE_POSCHANGED;
1123 if (status->button != status->obutton) {
1124 status->flags |= MOUSE_BUTTONSCHANGED;
1133 mtx_unlock(&sc->sc_mtx);
1138 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1140 struct ums_softc *sc = arg1;
1141 struct ums_info *info;
1143 int i, j, err, had_output;
1145 sb = sbuf_new_auto();
1146 for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1147 info = &sc->sc_info[i];
1149 /* Don't emit empty info */
1150 if ((info->sc_flags &
1151 (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1152 UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1153 info->sc_buttons == 0)
1157 sbuf_printf(sb, "\n");
1159 sbuf_printf(sb, "i%d:", i + 1);
1160 if (info->sc_flags & UMS_FLAG_X_AXIS)
1161 sbuf_printf(sb, " X:r%d, p%d, s%d;",
1162 (int)info->sc_iid_x,
1163 (int)info->sc_loc_x.pos,
1164 (int)info->sc_loc_x.size);
1165 if (info->sc_flags & UMS_FLAG_Y_AXIS)
1166 sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1167 (int)info->sc_iid_y,
1168 (int)info->sc_loc_y.pos,
1169 (int)info->sc_loc_y.size);
1170 if (info->sc_flags & UMS_FLAG_Z_AXIS)
1171 sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1172 (int)info->sc_iid_z,
1173 (int)info->sc_loc_z.pos,
1174 (int)info->sc_loc_z.size);
1175 if (info->sc_flags & UMS_FLAG_T_AXIS)
1176 sbuf_printf(sb, " T:r%d, p%d, s%d;",
1177 (int)info->sc_iid_t,
1178 (int)info->sc_loc_t.pos,
1179 (int)info->sc_loc_t.size);
1180 if (info->sc_flags & UMS_FLAG_W_AXIS)
1181 sbuf_printf(sb, " W:r%d, p%d, s%d;",
1182 (int)info->sc_iid_w,
1183 (int)info->sc_loc_w.pos,
1184 (int)info->sc_loc_w.size);
1186 for (j = 0; j < info->sc_buttons; j++) {
1187 sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1188 (int)info->sc_iid_btn[j],
1189 (int)info->sc_loc_btn[j].pos,
1190 (int)info->sc_loc_btn[j].size);
1194 err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1200 static devclass_t ums_devclass;
1202 static device_method_t ums_methods[] = {
1203 DEVMETHOD(device_probe, ums_probe),
1204 DEVMETHOD(device_attach, ums_attach),
1205 DEVMETHOD(device_detach, ums_detach),
1210 static driver_t ums_driver = {
1212 .methods = ums_methods,
1213 .size = sizeof(struct ums_softc),
1216 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
1217 MODULE_DEPEND(ums, usb, 1, 1, 1);
1218 MODULE_DEPEND(ums, hid, 1, 1, 1);
1219 #ifdef EVDEV_SUPPORT
1220 MODULE_DEPEND(ums, evdev, 1, 1, 1);
1222 MODULE_VERSION(ums, 1);
1223 #ifndef USBHID_ENABLED
1224 USB_PNP_HOST_INFO(ums_devs);