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 "opt_evdev.h"
37 #include <sys/stdint.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
53 #include <sys/fcntl.h>
56 #include <bus/u4b/usb.h>
57 #include <bus/u4b/usbdi.h>
58 #include <bus/u4b/usbdi_util.h>
59 #include <bus/u4b/usbhid.h>
62 #define USB_DEBUG_VAR ums_debug
63 #include <bus/u4b/usb_debug.h>
65 #include <bus/u4b/quirk/usb_quirk.h>
68 #include <dev/misc/evdev/input.h>
69 #include <dev/misc/evdev/evdev.h>
72 #include <sys/ioccom.h>
73 #include <sys/filio.h>
75 #include <sys/mouse.h>
78 static int ums_debug = 0;
80 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
81 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
82 &ums_debug, 0, "Debug level");
85 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
86 #define MOUSE_FLAGS (HIO_RELATIVE)
88 #define UMS_BUF_SIZE 8 /* bytes */
89 #define UMS_IFQ_MAXLEN 50 /* units */
90 #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
91 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
92 #define UMS_INFO_MAX 2 /* maximum number of HID sets */
100 struct hid_location sc_loc_w;
101 struct hid_location sc_loc_x;
102 struct hid_location sc_loc_y;
103 struct hid_location sc_loc_z;
104 struct hid_location sc_loc_t;
105 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
108 #define UMS_FLAG_X_AXIS 0x0001
109 #define UMS_FLAG_Y_AXIS 0x0002
110 #define UMS_FLAG_Z_AXIS 0x0004
111 #define UMS_FLAG_T_AXIS 0x0008
112 #define UMS_FLAG_SBU 0x0010 /* spurious button up events */
113 #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */
114 #define UMS_FLAG_W_AXIS 0x0040
121 uint8_t sc_iid_btn[UMS_BUTTON_MAX];
126 struct usb_fifo_sc sc_fifo;
128 struct usb_callout sc_callout;
129 struct ums_info sc_info[UMS_INFO_MAX];
133 mousestatus_t sc_status;
135 struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
142 #define UMS_EVDEV_OPENED 1
150 struct evdev_dev *sc_evdev;
154 static void ums_put_queue_timeout(void *__sc);
156 static usb_callback_t ums_intr_callback;
158 static device_probe_t ums_probe;
159 static device_attach_t ums_attach;
160 static device_detach_t ums_detach;
162 static usb_fifo_cmd_t ums_fifo_start_read;
163 static usb_fifo_cmd_t ums_fifo_stop_read;
164 static usb_fifo_open_t ums_fifo_open;
165 static usb_fifo_close_t ums_fifo_close;
166 static usb_fifo_ioctl_t ums_fifo_ioctl;
169 static evdev_open_t ums_ev_open;
170 static evdev_close_t ums_ev_close;
171 static void ums_evdev_push(struct ums_softc *, int32_t, int32_t,
172 int32_t, int32_t, int32_t);
175 static void ums_start_rx(struct ums_softc *);
176 static void ums_stop_rx(struct ums_softc *);
177 static void ums_put_queue(struct ums_softc *, int32_t, int32_t,
178 int32_t, int32_t, int32_t);
180 static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
183 static struct usb_fifo_methods ums_fifo_methods = {
184 .f_open = &ums_fifo_open,
185 .f_close = &ums_fifo_close,
186 .f_ioctl = &ums_fifo_ioctl,
187 .f_start_read = &ums_fifo_start_read,
188 .f_stop_read = &ums_fifo_stop_read,
189 .basename[0] = "ums",
193 static const struct evdev_methods ums_evdev_methods = {
194 .ev_open = &ums_ev_open,
195 .ev_close = &ums_ev_close,
200 ums_put_queue_timeout(void *__sc)
202 struct ums_softc *sc = __sc;
204 KKASSERT(lockowned(&sc->sc_lock));
206 ums_put_queue(sc, 0, 0, 0, 0, 0);
208 ums_evdev_push(sc, 0, 0, 0, 0, 0);
213 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
215 struct ums_softc *sc = usbd_xfer_softc(xfer);
216 struct ums_info *info = &sc->sc_info[0];
217 struct usb_page_cache *pc;
218 uint8_t *buf = sc->sc_temp;
220 int32_t buttons_found = 0;
222 int32_t buttons_reported = 0;
233 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
235 switch (USB_GET_STATE(xfer)) {
236 case USB_ST_TRANSFERRED:
237 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
239 if (len > (int)sizeof(sc->sc_temp)) {
240 DPRINTFN(6, "truncating large packet to %zu bytes\n",
241 sizeof(sc->sc_temp));
242 len = sizeof(sc->sc_temp);
247 pc = usbd_xfer_get_frame(xfer, 0);
248 usbd_copy_out(pc, 0, buf, len);
250 DPRINTFN(6, "data = %02x %02x %02x %02x "
251 "%02x %02x %02x %02x\n",
252 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
253 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
254 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
255 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
265 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
266 if ((*buf == 0x14) || (*buf == 0x15)) {
273 if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
274 (id == info->sc_iid_w))
275 dw += hid_get_data(buf, len, &info->sc_loc_w);
277 if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
278 (id == info->sc_iid_x))
279 dx += hid_get_data(buf, len, &info->sc_loc_x);
281 if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
282 (id == info->sc_iid_y))
283 dy = -hid_get_data(buf, len, &info->sc_loc_y);
285 if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
286 (id == info->sc_iid_z)) {
288 temp = hid_get_data(buf, len, &info->sc_loc_z);
289 if (info->sc_flags & UMS_FLAG_REVZ)
294 if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
295 (id == info->sc_iid_t))
296 dt -= hid_get_data(buf, len, &info->sc_loc_t);
298 for (i = 0; i < info->sc_buttons; i++) {
300 mask = 1UL << UMS_BUT(i);
301 /* check for correct button ID */
302 if (id != info->sc_iid_btn[i])
304 /* check for button pressed */
305 if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
307 /* register button mask */
308 buttons_found |= mask;
311 if (++info != &sc->sc_info[UMS_INFO_MAX])
315 buttons_reported = buttons;
317 /* keep old button value(s) for non-detected buttons */
318 buttons |= sc->sc_status.button & ~buttons_found;
320 if (dx || dy || dz || dt || dw ||
321 (buttons != sc->sc_status.button)) {
323 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
324 dx, dy, dz, dt, dw, buttons);
326 /* translate T-axis into button presses until further */
332 sc->sc_status.button = buttons;
333 sc->sc_status.dx += dx;
334 sc->sc_status.dy += dy;
335 sc->sc_status.dz += dz;
337 * sc->sc_status.dt += dt;
338 * no way to export this yet
342 * The Qtronix keyboard has a built in PS/2
343 * port for a mouse. The firmware once in a
344 * while posts a spurious button up
345 * event. This event we ignore by doing a
346 * timeout for 50 msecs. If we receive
347 * dx=dy=dz=buttons=0 before we add the event
348 * to the queue. In any other case we delete
351 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
352 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
353 (dw == 0) && (buttons == 0)) {
355 usb_callout_reset(&sc->sc_callout, hz / 20,
356 &ums_put_queue_timeout, sc);
359 usb_callout_stop(&sc->sc_callout);
361 ums_put_queue(sc, dx, dy, dz, dt, buttons);
363 ums_evdev_push(sc, dx, dy, dz, dt,
370 /* check if we can put more data into the FIFO */
371 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) == 0) {
373 * When the FIFO becomes full we stop cycling the
374 * poll and must clear sc_read_running. The next
375 * userland read/select/kqueue request will call
376 * f_start_read to get it going again.
378 sc->sc_read_running = 0;
380 /* XXX check logic. */
381 if (sc->sc_evflags == 0)
388 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
389 usbd_transfer_submit(xfer);
393 if (error != USB_ERR_CANCELLED) {
394 /* try clear stall first */
395 usbd_xfer_set_stall(xfer);
402 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
405 .type = UE_INTERRUPT,
406 .endpoint = UE_ADDR_ANY,
407 .direction = UE_DIR_IN,
408 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
409 .bufsize = 0, /* use wMaxPacketSize */
410 .callback = &ums_intr_callback,
414 /* A match on these entries will load ums */
415 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
416 {USB_IFACE_CLASS(UICLASS_HID),
417 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
418 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
422 ums_probe(device_t dev)
424 struct usb_attach_arg *uaa = device_get_ivars(dev);
431 if (uaa->usb_mode != USB_MODE_HOST)
434 if (uaa->info.bInterfaceClass != UICLASS_HID)
437 if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
440 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
441 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
442 return (BUS_PROBE_DEFAULT);
444 error = usbd_req_get_hid_desc(uaa->device, NULL,
445 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
450 if (hid_is_mouse(d_ptr, d_len))
451 error = BUS_PROBE_DEFAULT;
455 kfree(d_ptr, M_TEMP);
460 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
461 uint16_t len, uint8_t index)
463 struct ums_info *info = &sc->sc_info[index];
468 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
469 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)) {
478 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
479 info->sc_flags |= UMS_FLAG_Y_AXIS;
482 /* Try the wheel first as the Z activator since it's tradition. */
483 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
484 HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
486 hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
487 HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
489 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
490 info->sc_flags |= UMS_FLAG_Z_AXIS;
493 * We might have both a wheel and Z direction, if so put
494 * put the Z on the W coordinate.
496 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
497 HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
500 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
501 info->sc_flags |= UMS_FLAG_W_AXIS;
504 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
505 HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
508 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
509 info->sc_flags |= UMS_FLAG_Z_AXIS;
513 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
514 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
515 * to know that the byte after the wheel is the tilt axis.
516 * There are no other HID axis descriptors other than X,Y and
519 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
520 HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
521 &flags, &info->sc_iid_t)) {
523 info->sc_loc_t.pos += 8;
525 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
526 info->sc_flags |= UMS_FLAG_T_AXIS;
528 } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
529 HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
530 &flags, &info->sc_iid_t)) {
532 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
533 info->sc_flags |= UMS_FLAG_T_AXIS;
535 /* figure out the number of buttons */
537 for (i = 0; i < UMS_BUTTON_MAX; i++) {
538 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
539 hid_input, index, &info->sc_loc_btn[i], NULL,
540 &info->sc_iid_btn[i])) {
545 /* detect other buttons */
547 for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
548 if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
549 hid_input, index, &info->sc_loc_btn[i], NULL,
550 &info->sc_iid_btn[i])) {
555 info->sc_buttons = i;
557 if (i > sc->sc_buttons)
560 if (info->sc_flags == 0)
563 /* announce information about the mouse */
564 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
566 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
567 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
568 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
569 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
570 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
575 ums_attach(device_t dev)
577 struct usb_attach_arg *uaa = device_get_ivars(dev);
578 struct ums_softc *sc = device_get_softc(dev);
579 struct ums_info *info;
589 DPRINTFN(11, "sc=%p\n", sc);
591 device_set_usb_desc(dev);
593 lockinit(&sc->sc_lock, "ums lock", 0, LK_CANRECURSE);
595 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
598 * Force the report (non-boot) protocol.
600 * Mice without boot protocol support may choose not to implement
601 * Set_Protocol at all; Ignore any error.
603 err = usbd_req_set_protocol(uaa->device, NULL,
604 uaa->info.bIfaceIndex, 1);
606 err = usbd_transfer_setup(uaa->device,
607 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
608 UMS_N_TRANSFER, sc, &sc->sc_lock);
611 DPRINTF("error=%s\n", usbd_errstr(err));
615 /* Get HID descriptor */
616 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
617 &d_len, M_TEMP, uaa->info.bIfaceIndex);
620 device_printf(dev, "error reading report description\n");
624 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
627 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
628 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
629 * all of its other button positions are all off. It also reports that
630 * it has two addional buttons and a tilt wheel.
632 if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
636 info = &sc->sc_info[0];
637 info->sc_flags = (UMS_FLAG_X_AXIS |
641 info->sc_buttons = 3;
643 /* 1st byte of descriptor report contains garbage */
644 info->sc_loc_x.pos = 16;
645 info->sc_loc_x.size = 8;
646 info->sc_loc_y.pos = 24;
647 info->sc_loc_y.size = 8;
648 info->sc_loc_z.pos = 32;
649 info->sc_loc_z.size = 8;
650 info->sc_loc_btn[0].pos = 8;
651 info->sc_loc_btn[0].size = 1;
652 info->sc_loc_btn[1].pos = 9;
653 info->sc_loc_btn[1].size = 1;
654 info->sc_loc_btn[2].pos = 10;
655 info->sc_loc_btn[2].size = 1;
657 /* Announce device */
658 device_printf(dev, "3 buttons and [XYZ] "
659 "coordinates ID=0\n");
662 /* Search the HID descriptor and announce device */
663 for (i = 0; i < UMS_INFO_MAX; i++) {
664 ums_hid_parse(sc, dev, d_ptr, d_len, i);
668 if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
669 info = &sc->sc_info[0];
670 /* Some wheels need the Z axis reversed. */
671 info->sc_flags |= UMS_FLAG_REVZ;
673 if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
674 DPRINTF("WARNING: report size, %d bytes, is larger "
675 "than interrupt size, %d bytes!\n", isize,
676 usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
678 kfree(d_ptr, M_TEMP);
682 for (j = 0; j < UMS_INFO_MAX; j++) {
683 info = &sc->sc_info[j];
685 DPRINTF("sc=%p, index=%d\n", sc, j);
686 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
687 info->sc_loc_x.size, info->sc_iid_x);
688 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
689 info->sc_loc_y.size, info->sc_iid_y);
690 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
691 info->sc_loc_z.size, info->sc_iid_z);
692 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
693 info->sc_loc_t.size, info->sc_iid_t);
694 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
695 info->sc_loc_w.size, info->sc_iid_w);
697 for (i = 0; i < info->sc_buttons; i++) {
698 DPRINTF("B%d\t%d/%d id=%d\n",
699 i + 1, info->sc_loc_btn[i].pos,
700 info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
703 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
706 err = usb_fifo_attach(uaa->device, sc, &sc->sc_lock,
707 &ums_fifo_methods, &sc->sc_fifo,
708 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
709 UID_ROOT, GID_OPERATOR, 0644);
714 sc->sc_evdev = evdev_alloc();
715 evdev_set_name(sc->sc_evdev, device_get_desc(dev));
716 evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
717 evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor,
718 uaa->info.idProduct, 0);
719 evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device));
720 evdev_set_methods(sc->sc_evdev, sc, &ums_evdev_methods);
721 evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER);
722 evdev_support_event(sc->sc_evdev, EV_SYN);
723 evdev_support_event(sc->sc_evdev, EV_REL);
724 evdev_support_event(sc->sc_evdev, EV_KEY);
726 info = &sc->sc_info[0];
728 if (info->sc_flags & UMS_FLAG_X_AXIS)
729 evdev_support_rel(sc->sc_evdev, REL_X);
731 if (info->sc_flags & UMS_FLAG_Y_AXIS)
732 evdev_support_rel(sc->sc_evdev, REL_Y);
734 if (info->sc_flags & UMS_FLAG_Z_AXIS)
735 evdev_support_rel(sc->sc_evdev, REL_WHEEL);
737 if (info->sc_flags & UMS_FLAG_T_AXIS)
738 evdev_support_rel(sc->sc_evdev, REL_HWHEEL);
740 for (i = 0; i < info->sc_buttons; i++)
741 evdev_support_key(sc->sc_evdev, BTN_MOUSE + i);
743 err = evdev_register_mtx(sc->sc_evdev, &sc->sc_lock);
749 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
750 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
751 OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD,
752 sc, 0, ums_sysctl_handler_parseinfo,
753 "", "Dump of parsed HID report descriptor");
760 kfree(d_ptr, M_TEMP);
767 ums_detach(device_t self)
769 struct ums_softc *sc = device_get_softc(self);
771 DPRINTF("sc=%p\n", sc);
773 usb_fifo_detach(&sc->sc_fifo);
776 evdev_free(sc->sc_evdev);
779 usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
781 usb_callout_drain(&sc->sc_callout);
783 lockuninit(&sc->sc_lock);
789 ums_reset(struct ums_softc *sc)
792 /* reset all USB mouse parameters */
794 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
795 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
797 sc->sc_hw.buttons = sc->sc_buttons;
799 sc->sc_hw.iftype = MOUSE_IF_USB;
800 sc->sc_hw.type = MOUSE_MOUSE;
801 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
804 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
805 sc->sc_mode.rate = -1;
806 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
807 sc->sc_mode.accelfactor = 0;
808 sc->sc_mode.level = 0;
809 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
810 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
811 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
815 sc->sc_status.flags = 0;
816 sc->sc_status.button = 0;
817 sc->sc_status.obutton = 0;
818 sc->sc_status.dx = 0;
819 sc->sc_status.dy = 0;
820 sc->sc_status.dz = 0;
821 /* sc->sc_status.dt = 0; */
825 ums_start_rx(struct ums_softc *sc)
829 /* Check if we should override the default polling interval */
830 rate = sc->sc_pollrate;
831 /* Range check rate */
834 /* Check for set rate */
835 if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
836 DPRINTF("Setting pollrate = %d\n", rate);
837 /* Stop current transfer, if any */
838 if (sc->sc_read_running) {
839 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
840 sc->sc_read_running = 0;
842 /* Set new interval */
843 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
844 /* Only set pollrate once */
847 if (sc->sc_read_running == 0) {
848 sc->sc_read_running = 1;
849 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
854 ums_stop_rx(struct ums_softc *sc)
856 if (sc->sc_read_running) {
857 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
858 sc->sc_read_running = 0;
859 usb_callout_stop(&sc->sc_callout);
864 ums_fifo_start_read(struct usb_fifo *fifo)
866 struct ums_softc *sc = usb_fifo_softc(fifo);
872 ums_fifo_stop_read(struct usb_fifo *fifo)
874 struct ums_softc *sc = usb_fifo_softc(fifo);
879 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
880 (MOUSE_MSC_PACKETSIZE != 5))
881 #error "Software assumptions are not met. Please update code."
885 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
886 int32_t dz, int32_t dt, int32_t buttons)
909 buf[0] = sc->sc_mode.syncmask[1];
910 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
913 buf[3] = dx - (dx >> 1);
914 buf[4] = dy - (dy >> 1);
916 if (sc->sc_mode.level == 1) {
918 buf[6] = dz - (dz >> 1);
919 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
921 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
922 sc->sc_mode.packetsize, 1);
925 DPRINTF("Buffer full, discarded packet\n");
931 ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy,
932 int32_t dz, int32_t dt, int32_t buttons)
934 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
935 /* Push evdev event */
936 evdev_push_rel(sc->sc_evdev, REL_X, dx);
937 evdev_push_rel(sc->sc_evdev, REL_Y, -dy);
938 evdev_push_rel(sc->sc_evdev, REL_WHEEL, -dz);
939 evdev_push_rel(sc->sc_evdev, REL_HWHEEL, dt);
940 evdev_push_mouse_btn(sc->sc_evdev,
941 (buttons & ~MOUSE_STDBUTTONS) |
942 (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) |
943 (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) |
944 (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0));
945 evdev_sync(sc->sc_evdev);
951 ums_reset_buf(struct ums_softc *sc)
953 /* reset read queue, must be called locked */
954 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
959 ums_ev_open(struct evdev_dev *evdev, void *ev_softc)
961 struct ums_softc *sc = (struct ums_softc *)ev_softc;
963 KKASSERT(lockowned(&sc->sc_lock));
965 sc->sc_evflags = UMS_EVDEV_OPENED;
967 if (sc->sc_fflags == 0) {
976 ums_ev_close(struct evdev_dev *evdev, void *ev_softc)
978 struct ums_softc *sc = (struct ums_softc *)ev_softc;
980 KKASSERT(lockowned(&sc->sc_lock));
984 if (sc->sc_fflags == 0)
990 ums_fifo_open(struct usb_fifo *fifo, int fflags)
992 struct ums_softc *sc = usb_fifo_softc(fifo);
996 /* check for duplicate open, should not happen */
997 if (sc->sc_fflags & fflags)
1000 /* check for first open */
1001 #ifdef EVDEV_SUPPORT
1002 if (sc->sc_fflags == 0 && sc->sc_evflags == 0)
1005 if (sc->sc_fflags == 0)
1009 if (fflags & FREAD) {
1010 /* allocate RX buffer */
1011 if (usb_fifo_alloc_buffer(fifo,
1012 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1017 sc->sc_fflags |= fflags & (FREAD | FWRITE);
1022 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1024 struct ums_softc *sc = usb_fifo_softc(fifo);
1029 usb_fifo_free_buffer(fifo);
1031 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1035 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1037 struct ums_softc *sc = usb_fifo_softc(fifo);
1043 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
1046 case MOUSE_GETHWINFO:
1047 *(mousehw_t *)addr = sc->sc_hw;
1051 *(mousemode_t *)addr = sc->sc_mode;
1055 mode = *(mousemode_t *)addr;
1057 if (mode.level == -1) {
1058 /* don't change the current setting */
1059 } else if ((mode.level < 0) || (mode.level > 1)) {
1063 sc->sc_mode.level = mode.level;
1066 /* store polling rate */
1067 sc->sc_pollrate = mode.rate;
1069 if (sc->sc_mode.level == 0) {
1070 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1071 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1073 sc->sc_hw.buttons = sc->sc_buttons;
1074 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1075 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1076 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1077 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1078 } else if (sc->sc_mode.level == 1) {
1079 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1080 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1082 sc->sc_hw.buttons = sc->sc_buttons;
1083 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1084 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1085 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1086 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1091 case MOUSE_GETLEVEL:
1092 *(int *)addr = sc->sc_mode.level;
1095 case MOUSE_SETLEVEL:
1096 if (*(int *)addr < 0 || *(int *)addr > 1) {
1100 sc->sc_mode.level = *(int *)addr;
1102 if (sc->sc_mode.level == 0) {
1103 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1104 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1106 sc->sc_hw.buttons = sc->sc_buttons;
1107 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1108 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1109 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1110 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1111 } else if (sc->sc_mode.level == 1) {
1112 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1113 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1115 sc->sc_hw.buttons = sc->sc_buttons;
1116 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1117 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1118 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1119 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1124 case MOUSE_GETSTATUS:{
1125 mousestatus_t *status = (mousestatus_t *)addr;
1127 *status = sc->sc_status;
1128 sc->sc_status.obutton = sc->sc_status.button;
1129 sc->sc_status.button = 0;
1130 sc->sc_status.dx = 0;
1131 sc->sc_status.dy = 0;
1132 sc->sc_status.dz = 0;
1133 /* sc->sc_status.dt = 0; */
1135 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1136 status->flags |= MOUSE_POSCHANGED;
1138 if (status->button != status->obutton) {
1139 status->flags |= MOUSE_BUTTONSCHANGED;
1148 lockmgr(&sc->sc_lock, LK_RELEASE);
1154 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1156 struct ums_softc *sc = arg1;
1157 struct ums_info *info;
1159 int i, j, err, had_output;
1161 sb = sbuf_new_auto();
1162 for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1163 info = &sc->sc_info[i];
1165 /* Don't emit empty info */
1166 if ((info->sc_flags &
1167 (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1168 UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1169 info->sc_buttons == 0)
1173 sbuf_printf(sb, "\n");
1175 sbuf_printf(sb, "i%d:", i + 1);
1176 if (info->sc_flags & UMS_FLAG_X_AXIS)
1177 sbuf_printf(sb, " X:r%d, p%d, s%d;",
1178 (int)info->sc_iid_x,
1179 (int)info->sc_loc_x.pos,
1180 (int)info->sc_loc_x.size);
1181 if (info->sc_flags & UMS_FLAG_Y_AXIS)
1182 sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1183 (int)info->sc_iid_y,
1184 (int)info->sc_loc_y.pos,
1185 (int)info->sc_loc_y.size);
1186 if (info->sc_flags & UMS_FLAG_Z_AXIS)
1187 sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1188 (int)info->sc_iid_z,
1189 (int)info->sc_loc_z.pos,
1190 (int)info->sc_loc_z.size);
1191 if (info->sc_flags & UMS_FLAG_T_AXIS)
1192 sbuf_printf(sb, " T:r%d, p%d, s%d;",
1193 (int)info->sc_iid_t,
1194 (int)info->sc_loc_t.pos,
1195 (int)info->sc_loc_t.size);
1196 if (info->sc_flags & UMS_FLAG_W_AXIS)
1197 sbuf_printf(sb, " W:r%d, p%d, s%d;",
1198 (int)info->sc_iid_w,
1199 (int)info->sc_loc_w.pos,
1200 (int)info->sc_loc_w.size);
1202 for (j = 0; j < info->sc_buttons; j++) {
1203 sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1204 (int)info->sc_iid_btn[j],
1205 (int)info->sc_loc_btn[j].pos,
1206 (int)info->sc_loc_btn[j].size);
1210 err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1217 static devclass_t ums_devclass;
1219 static device_method_t ums_methods[] = {
1220 DEVMETHOD(device_probe, ums_probe),
1221 DEVMETHOD(device_attach, ums_attach),
1222 DEVMETHOD(device_detach, ums_detach),
1226 static driver_t ums_driver = {
1228 .methods = ums_methods,
1229 .size = sizeof(struct ums_softc),
1232 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, NULL);
1233 MODULE_DEPEND(ums, usb, 1, 1, 1);
1234 #ifdef EVDEV_SUPPORT
1235 MODULE_DEPEND(ums, evdev, 1, 1, 1);
1237 MODULE_VERSION(ums, 1);