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/filio.h>
74 #include <sys/mouse.h>
77 static int ums_debug = 0;
79 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
80 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
81 &ums_debug, 0, "Debug level");
84 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
85 #define MOUSE_FLAGS (HIO_RELATIVE)
87 #define UMS_BUF_SIZE 8 /* bytes */
88 #define UMS_IFQ_MAXLEN 50 /* units */
89 #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
90 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
91 #define UMS_INFO_MAX 2 /* maximum number of HID sets */
99 struct hid_location sc_loc_w;
100 struct hid_location sc_loc_x;
101 struct hid_location sc_loc_y;
102 struct hid_location sc_loc_z;
103 struct hid_location sc_loc_t;
104 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
107 #define UMS_FLAG_X_AXIS 0x0001
108 #define UMS_FLAG_Y_AXIS 0x0002
109 #define UMS_FLAG_Z_AXIS 0x0004
110 #define UMS_FLAG_T_AXIS 0x0008
111 #define UMS_FLAG_SBU 0x0010 /* spurious button up events */
112 #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */
113 #define UMS_FLAG_W_AXIS 0x0040
120 uint8_t sc_iid_btn[UMS_BUTTON_MAX];
125 struct usb_fifo_sc sc_fifo;
127 struct usb_callout sc_callout;
128 struct ums_info sc_info[UMS_INFO_MAX];
132 mousestatus_t sc_status;
134 struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
141 #define UMS_EVDEV_OPENED 1
149 struct evdev_dev *sc_evdev;
153 static void ums_put_queue_timeout(void *__sc);
155 static usb_callback_t ums_intr_callback;
157 static device_probe_t ums_probe;
158 static device_attach_t ums_attach;
159 static device_detach_t ums_detach;
161 static usb_fifo_cmd_t ums_fifo_start_read;
162 static usb_fifo_cmd_t ums_fifo_stop_read;
163 static usb_fifo_open_t ums_fifo_open;
164 static usb_fifo_close_t ums_fifo_close;
165 static usb_fifo_ioctl_t ums_fifo_ioctl;
168 static evdev_open_t ums_ev_open;
169 static evdev_close_t ums_ev_close;
170 static void ums_evdev_push(struct ums_softc *, int32_t, int32_t,
171 int32_t, int32_t, int32_t);
174 static void ums_start_rx(struct ums_softc *);
175 static void ums_stop_rx(struct ums_softc *);
176 static void ums_put_queue(struct ums_softc *, int32_t, int32_t,
177 int32_t, int32_t, int32_t);
179 static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
182 static struct usb_fifo_methods ums_fifo_methods = {
183 .f_open = &ums_fifo_open,
184 .f_close = &ums_fifo_close,
185 .f_ioctl = &ums_fifo_ioctl,
186 .f_start_read = &ums_fifo_start_read,
187 .f_stop_read = &ums_fifo_stop_read,
188 .basename[0] = "ums",
192 static const struct evdev_methods ums_evdev_methods = {
193 .ev_open = &ums_ev_open,
194 .ev_close = &ums_ev_close,
199 ums_put_queue_timeout(void *__sc)
201 struct ums_softc *sc = __sc;
203 KKASSERT(lockowned(&sc->sc_lock));
205 ums_put_queue(sc, 0, 0, 0, 0, 0);
207 ums_evdev_push(sc, 0, 0, 0, 0, 0);
212 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
214 struct ums_softc *sc = usbd_xfer_softc(xfer);
215 struct ums_info *info = &sc->sc_info[0];
216 struct usb_page_cache *pc;
217 uint8_t *buf = sc->sc_temp;
219 int32_t buttons_found = 0;
221 int32_t buttons_reported = 0;
232 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
234 switch (USB_GET_STATE(xfer)) {
235 case USB_ST_TRANSFERRED:
236 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
238 if (len > (int)sizeof(sc->sc_temp)) {
239 DPRINTFN(6, "truncating large packet to %zu bytes\n",
240 sizeof(sc->sc_temp));
241 len = sizeof(sc->sc_temp);
246 pc = usbd_xfer_get_frame(xfer, 0);
247 usbd_copy_out(pc, 0, buf, len);
249 DPRINTFN(6, "data = %02x %02x %02x %02x "
250 "%02x %02x %02x %02x\n",
251 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
252 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
253 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
254 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
264 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
265 if ((*buf == 0x14) || (*buf == 0x15)) {
272 if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
273 (id == info->sc_iid_w))
274 dw += hid_get_data(buf, len, &info->sc_loc_w);
276 if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
277 (id == info->sc_iid_x))
278 dx += hid_get_data(buf, len, &info->sc_loc_x);
280 if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
281 (id == info->sc_iid_y))
282 dy = -hid_get_data(buf, len, &info->sc_loc_y);
284 if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
285 (id == info->sc_iid_z)) {
287 temp = hid_get_data(buf, len, &info->sc_loc_z);
288 if (info->sc_flags & UMS_FLAG_REVZ)
293 if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
294 (id == info->sc_iid_t))
295 dt -= hid_get_data(buf, len, &info->sc_loc_t);
297 for (i = 0; i < info->sc_buttons; i++) {
299 mask = 1UL << UMS_BUT(i);
300 /* check for correct button ID */
301 if (id != info->sc_iid_btn[i])
303 /* check for button pressed */
304 if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
306 /* register button mask */
307 buttons_found |= mask;
310 if (++info != &sc->sc_info[UMS_INFO_MAX])
314 buttons_reported = buttons;
316 /* keep old button value(s) for non-detected buttons */
317 buttons |= sc->sc_status.button & ~buttons_found;
319 if (dx || dy || dz || dt || dw ||
320 (buttons != sc->sc_status.button)) {
322 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
323 dx, dy, dz, dt, dw, buttons);
325 /* translate T-axis into button presses until further */
331 sc->sc_status.button = buttons;
332 sc->sc_status.dx += dx;
333 sc->sc_status.dy += dy;
334 sc->sc_status.dz += dz;
336 * sc->sc_status.dt += dt;
337 * no way to export this yet
341 * The Qtronix keyboard has a built in PS/2
342 * port for a mouse. The firmware once in a
343 * while posts a spurious button up
344 * event. This event we ignore by doing a
345 * timeout for 50 msecs. If we receive
346 * dx=dy=dz=buttons=0 before we add the event
347 * to the queue. In any other case we delete
350 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
351 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
352 (dw == 0) && (buttons == 0)) {
354 usb_callout_reset(&sc->sc_callout, hz / 20,
355 &ums_put_queue_timeout, sc);
358 usb_callout_stop(&sc->sc_callout);
360 ums_put_queue(sc, dx, dy, dz, dt, buttons);
362 ums_evdev_push(sc, dx, dy, dz, dt,
369 /* check if we can put more data into the FIFO */
370 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) == 0) {
372 * When the FIFO becomes full we stop cycling the
373 * poll and must clear sc_read_running. The next
374 * userland read/select/kqueue request will call
375 * f_start_read to get it going again.
377 sc->sc_read_running = 0;
379 /* XXX check logic. */
380 if (sc->sc_evflags == 0)
387 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
388 usbd_transfer_submit(xfer);
392 if (error != USB_ERR_CANCELLED) {
393 /* try clear stall first */
394 usbd_xfer_set_stall(xfer);
401 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
404 .type = UE_INTERRUPT,
405 .endpoint = UE_ADDR_ANY,
406 .direction = UE_DIR_IN,
407 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
408 .bufsize = 0, /* use wMaxPacketSize */
409 .callback = &ums_intr_callback,
413 /* A match on these entries will load ums */
414 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
415 {USB_IFACE_CLASS(UICLASS_HID),
416 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
417 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
421 ums_probe(device_t dev)
423 struct usb_attach_arg *uaa = device_get_ivars(dev);
430 if (uaa->usb_mode != USB_MODE_HOST)
433 if (uaa->info.bInterfaceClass != UICLASS_HID)
436 if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
439 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
440 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
441 return (BUS_PROBE_DEFAULT);
443 error = usbd_req_get_hid_desc(uaa->device, NULL,
444 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
449 if (hid_is_mouse(d_ptr, d_len))
450 error = BUS_PROBE_DEFAULT;
454 kfree(d_ptr, M_TEMP);
459 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
460 uint16_t len, uint8_t index)
462 struct ums_info *info = &sc->sc_info[index];
467 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
468 hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
470 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
471 info->sc_flags |= UMS_FLAG_X_AXIS;
474 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
475 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,
499 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
500 info->sc_flags |= UMS_FLAG_W_AXIS;
503 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
504 HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
507 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
508 info->sc_flags |= UMS_FLAG_Z_AXIS;
512 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
513 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
514 * to know that the byte after the wheel is the tilt axis.
515 * There are no other HID axis descriptors other than X,Y and
518 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
519 HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
520 &flags, &info->sc_iid_t)) {
522 info->sc_loc_t.pos += 8;
524 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
525 info->sc_flags |= UMS_FLAG_T_AXIS;
527 } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
528 HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
529 &flags, &info->sc_iid_t)) {
531 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
532 info->sc_flags |= UMS_FLAG_T_AXIS;
534 /* figure out the number of buttons */
536 for (i = 0; i < UMS_BUTTON_MAX; i++) {
537 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
538 hid_input, index, &info->sc_loc_btn[i], NULL,
539 &info->sc_iid_btn[i])) {
544 /* detect other buttons */
546 for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
547 if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
548 hid_input, index, &info->sc_loc_btn[i], NULL,
549 &info->sc_iid_btn[i])) {
554 info->sc_buttons = i;
556 if (i > sc->sc_buttons)
559 if (info->sc_flags == 0)
562 /* announce information about the mouse */
563 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
565 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
566 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
567 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
568 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
569 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
574 ums_attach(device_t dev)
576 struct usb_attach_arg *uaa = device_get_ivars(dev);
577 struct ums_softc *sc = device_get_softc(dev);
578 struct ums_info *info;
588 DPRINTFN(11, "sc=%p\n", sc);
590 device_set_usb_desc(dev);
592 lockinit(&sc->sc_lock, "ums lock", 0, LK_CANRECURSE);
594 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
597 * Force the report (non-boot) protocol.
599 * Mice without boot protocol support may choose not to implement
600 * Set_Protocol at all; Ignore any error.
602 err = usbd_req_set_protocol(uaa->device, NULL,
603 uaa->info.bIfaceIndex, 1);
605 err = usbd_transfer_setup(uaa->device,
606 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
607 UMS_N_TRANSFER, sc, &sc->sc_lock);
610 DPRINTF("error=%s\n", usbd_errstr(err));
614 /* Get HID descriptor */
615 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
616 &d_len, M_TEMP, uaa->info.bIfaceIndex);
619 device_printf(dev, "error reading report description\n");
623 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
626 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
627 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
628 * all of its other button positions are all off. It also reports that
629 * it has two addional buttons and a tilt wheel.
631 if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
635 info = &sc->sc_info[0];
636 info->sc_flags = (UMS_FLAG_X_AXIS |
640 info->sc_buttons = 3;
642 /* 1st byte of descriptor report contains garbage */
643 info->sc_loc_x.pos = 16;
644 info->sc_loc_x.size = 8;
645 info->sc_loc_y.pos = 24;
646 info->sc_loc_y.size = 8;
647 info->sc_loc_z.pos = 32;
648 info->sc_loc_z.size = 8;
649 info->sc_loc_btn[0].pos = 8;
650 info->sc_loc_btn[0].size = 1;
651 info->sc_loc_btn[1].pos = 9;
652 info->sc_loc_btn[1].size = 1;
653 info->sc_loc_btn[2].pos = 10;
654 info->sc_loc_btn[2].size = 1;
656 /* Announce device */
657 device_printf(dev, "3 buttons and [XYZ] "
658 "coordinates ID=0\n");
661 /* Search the HID descriptor and announce device */
662 for (i = 0; i < UMS_INFO_MAX; i++) {
663 ums_hid_parse(sc, dev, d_ptr, d_len, i);
667 if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
668 info = &sc->sc_info[0];
669 /* Some wheels need the Z axis reversed. */
670 info->sc_flags |= UMS_FLAG_REVZ;
672 if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
673 DPRINTF("WARNING: report size, %d bytes, is larger "
674 "than interrupt size, %d bytes!\n", isize,
675 usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
677 kfree(d_ptr, M_TEMP);
681 for (j = 0; j < UMS_INFO_MAX; j++) {
682 info = &sc->sc_info[j];
684 DPRINTF("sc=%p, index=%d\n", sc, j);
685 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
686 info->sc_loc_x.size, info->sc_iid_x);
687 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
688 info->sc_loc_y.size, info->sc_iid_y);
689 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
690 info->sc_loc_z.size, info->sc_iid_z);
691 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
692 info->sc_loc_t.size, info->sc_iid_t);
693 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
694 info->sc_loc_w.size, info->sc_iid_w);
696 for (i = 0; i < info->sc_buttons; i++) {
697 DPRINTF("B%d\t%d/%d id=%d\n",
698 i + 1, info->sc_loc_btn[i].pos,
699 info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
702 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
705 err = usb_fifo_attach(uaa->device, sc, &sc->sc_lock,
706 &ums_fifo_methods, &sc->sc_fifo,
707 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
708 UID_ROOT, GID_OPERATOR, 0644);
713 sc->sc_evdev = evdev_alloc();
714 evdev_set_name(sc->sc_evdev, device_get_desc(dev));
715 evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
716 evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor,
717 uaa->info.idProduct, 0);
718 evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device));
719 evdev_set_methods(sc->sc_evdev, sc, &ums_evdev_methods);
720 evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER);
721 evdev_support_event(sc->sc_evdev, EV_SYN);
722 evdev_support_event(sc->sc_evdev, EV_REL);
723 evdev_support_event(sc->sc_evdev, EV_KEY);
725 info = &sc->sc_info[0];
727 if (info->sc_flags & UMS_FLAG_X_AXIS)
728 evdev_support_rel(sc->sc_evdev, REL_X);
730 if (info->sc_flags & UMS_FLAG_Y_AXIS)
731 evdev_support_rel(sc->sc_evdev, REL_Y);
733 if (info->sc_flags & UMS_FLAG_Z_AXIS)
734 evdev_support_rel(sc->sc_evdev, REL_WHEEL);
736 if (info->sc_flags & UMS_FLAG_T_AXIS)
737 evdev_support_rel(sc->sc_evdev, REL_HWHEEL);
739 for (i = 0; i < info->sc_buttons; i++)
740 evdev_support_key(sc->sc_evdev, BTN_MOUSE + i);
742 err = evdev_register(sc->sc_evdev);
748 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
749 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
750 OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD,
751 sc, 0, ums_sysctl_handler_parseinfo,
752 "", "Dump of parsed HID report descriptor");
759 kfree(d_ptr, M_TEMP);
766 ums_detach(device_t self)
768 struct ums_softc *sc = device_get_softc(self);
770 DPRINTF("sc=%p\n", sc);
772 usb_fifo_detach(&sc->sc_fifo);
775 evdev_free(sc->sc_evdev);
778 usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
780 usb_callout_drain(&sc->sc_callout);
782 lockuninit(&sc->sc_lock);
788 ums_reset(struct ums_softc *sc)
791 /* reset all USB mouse parameters */
793 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
794 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
796 sc->sc_hw.buttons = sc->sc_buttons;
798 sc->sc_hw.iftype = MOUSE_IF_USB;
799 sc->sc_hw.type = MOUSE_MOUSE;
800 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
803 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
804 sc->sc_mode.rate = -1;
805 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
806 sc->sc_mode.accelfactor = 0;
807 sc->sc_mode.level = 0;
808 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
809 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
810 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
814 sc->sc_status.flags = 0;
815 sc->sc_status.button = 0;
816 sc->sc_status.obutton = 0;
817 sc->sc_status.dx = 0;
818 sc->sc_status.dy = 0;
819 sc->sc_status.dz = 0;
820 /* sc->sc_status.dt = 0; */
824 ums_start_rx(struct ums_softc *sc)
828 /* Check if we should override the default polling interval */
829 rate = sc->sc_pollrate;
830 /* Range check rate */
833 /* Check for set rate */
834 if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
835 DPRINTF("Setting pollrate = %d\n", rate);
836 /* Stop current transfer, if any */
837 if (sc->sc_read_running) {
838 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
839 sc->sc_read_running = 0;
841 /* Set new interval */
842 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
843 /* Only set pollrate once */
846 if (sc->sc_read_running == 0) {
847 sc->sc_read_running = 1;
848 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
853 ums_stop_rx(struct ums_softc *sc)
855 if (sc->sc_read_running) {
856 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
857 sc->sc_read_running = 0;
858 usb_callout_stop(&sc->sc_callout);
863 ums_fifo_start_read(struct usb_fifo *fifo)
865 struct ums_softc *sc = usb_fifo_softc(fifo);
871 ums_fifo_stop_read(struct usb_fifo *fifo)
873 struct ums_softc *sc = usb_fifo_softc(fifo);
878 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
879 (MOUSE_MSC_PACKETSIZE != 5))
880 #error "Software assumptions are not met. Please update code."
884 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
885 int32_t dz, int32_t dt, int32_t buttons)
908 buf[0] = sc->sc_mode.syncmask[1];
909 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
912 buf[3] = dx - (dx >> 1);
913 buf[4] = dy - (dy >> 1);
915 if (sc->sc_mode.level == 1) {
917 buf[6] = dz - (dz >> 1);
918 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
920 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
921 sc->sc_mode.packetsize, 1);
924 DPRINTF("Buffer full, discarded packet\n");
930 ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy,
931 int32_t dz, int32_t dt, int32_t buttons)
933 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
934 /* Push evdev event */
935 evdev_push_rel(sc->sc_evdev, REL_X, dx);
936 evdev_push_rel(sc->sc_evdev, REL_Y, -dy);
937 evdev_push_rel(sc->sc_evdev, REL_WHEEL, -dz);
938 evdev_push_rel(sc->sc_evdev, REL_HWHEEL, dt);
939 evdev_push_mouse_btn(sc->sc_evdev,
940 (buttons & ~MOUSE_STDBUTTONS) |
941 (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) |
942 (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) |
943 (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0));
944 evdev_sync(sc->sc_evdev);
950 ums_reset_buf(struct ums_softc *sc)
952 /* reset read queue, must be called locked */
953 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
958 ums_ev_open(struct evdev_dev *evdev, void *ev_softc)
960 struct ums_softc *sc = (struct ums_softc *)ev_softc;
962 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
964 sc->sc_evflags = UMS_EVDEV_OPENED;
966 if (sc->sc_fflags == 0) {
971 lockmgr(&sc->sc_lock, LK_RELEASE);
977 ums_ev_close(struct evdev_dev *evdev, void *ev_softc)
979 struct ums_softc *sc = (struct ums_softc *)ev_softc;
981 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
985 if (sc->sc_fflags == 0)
988 lockmgr(&sc->sc_lock, LK_RELEASE);
993 ums_fifo_open(struct usb_fifo *fifo, int fflags)
995 struct ums_softc *sc = usb_fifo_softc(fifo);
999 /* check for duplicate open, should not happen */
1000 if (sc->sc_fflags & fflags)
1003 /* check for first open */
1004 #ifdef EVDEV_SUPPORT
1005 if (sc->sc_fflags == 0 && sc->sc_evflags == 0)
1008 if (sc->sc_fflags == 0)
1012 if (fflags & FREAD) {
1013 /* allocate RX buffer */
1014 if (usb_fifo_alloc_buffer(fifo,
1015 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1020 sc->sc_fflags |= fflags & (FREAD | FWRITE);
1025 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1027 struct ums_softc *sc = usb_fifo_softc(fifo);
1032 usb_fifo_free_buffer(fifo);
1034 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1038 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1040 struct ums_softc *sc = usb_fifo_softc(fifo);
1046 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
1049 case MOUSE_GETHWINFO:
1050 *(mousehw_t *)addr = sc->sc_hw;
1054 *(mousemode_t *)addr = sc->sc_mode;
1058 mode = *(mousemode_t *)addr;
1060 if (mode.level == -1) {
1061 /* don't change the current setting */
1062 } else if ((mode.level < 0) || (mode.level > 1)) {
1066 sc->sc_mode.level = mode.level;
1069 /* store polling rate */
1070 sc->sc_pollrate = mode.rate;
1072 if (sc->sc_mode.level == 0) {
1073 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1074 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1076 sc->sc_hw.buttons = sc->sc_buttons;
1077 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1078 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1079 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1080 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1081 } else if (sc->sc_mode.level == 1) {
1082 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1083 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1085 sc->sc_hw.buttons = sc->sc_buttons;
1086 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1087 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1088 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1089 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1094 case MOUSE_GETLEVEL:
1095 *(int *)addr = sc->sc_mode.level;
1098 case MOUSE_SETLEVEL:
1099 if (*(int *)addr < 0 || *(int *)addr > 1) {
1103 sc->sc_mode.level = *(int *)addr;
1105 if (sc->sc_mode.level == 0) {
1106 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1107 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1109 sc->sc_hw.buttons = sc->sc_buttons;
1110 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1111 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1112 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1113 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1114 } else if (sc->sc_mode.level == 1) {
1115 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1116 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1118 sc->sc_hw.buttons = sc->sc_buttons;
1119 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1120 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1121 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1122 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1127 case MOUSE_GETSTATUS:{
1128 mousestatus_t *status = (mousestatus_t *)addr;
1130 *status = sc->sc_status;
1131 sc->sc_status.obutton = sc->sc_status.button;
1132 sc->sc_status.button = 0;
1133 sc->sc_status.dx = 0;
1134 sc->sc_status.dy = 0;
1135 sc->sc_status.dz = 0;
1136 /* sc->sc_status.dt = 0; */
1138 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1139 status->flags |= MOUSE_POSCHANGED;
1141 if (status->button != status->obutton) {
1142 status->flags |= MOUSE_BUTTONSCHANGED;
1151 lockmgr(&sc->sc_lock, LK_RELEASE);
1157 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1159 struct ums_softc *sc = arg1;
1160 struct ums_info *info;
1162 int i, j, err, had_output;
1164 sb = sbuf_new_auto();
1165 for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1166 info = &sc->sc_info[i];
1168 /* Don't emit empty info */
1169 if ((info->sc_flags &
1170 (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1171 UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1172 info->sc_buttons == 0)
1176 sbuf_printf(sb, "\n");
1178 sbuf_printf(sb, "i%d:", i + 1);
1179 if (info->sc_flags & UMS_FLAG_X_AXIS)
1180 sbuf_printf(sb, " X:r%d, p%d, s%d;",
1181 (int)info->sc_iid_x,
1182 (int)info->sc_loc_x.pos,
1183 (int)info->sc_loc_x.size);
1184 if (info->sc_flags & UMS_FLAG_Y_AXIS)
1185 sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1186 (int)info->sc_iid_y,
1187 (int)info->sc_loc_y.pos,
1188 (int)info->sc_loc_y.size);
1189 if (info->sc_flags & UMS_FLAG_Z_AXIS)
1190 sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1191 (int)info->sc_iid_z,
1192 (int)info->sc_loc_z.pos,
1193 (int)info->sc_loc_z.size);
1194 if (info->sc_flags & UMS_FLAG_T_AXIS)
1195 sbuf_printf(sb, " T:r%d, p%d, s%d;",
1196 (int)info->sc_iid_t,
1197 (int)info->sc_loc_t.pos,
1198 (int)info->sc_loc_t.size);
1199 if (info->sc_flags & UMS_FLAG_W_AXIS)
1200 sbuf_printf(sb, " W:r%d, p%d, s%d;",
1201 (int)info->sc_iid_w,
1202 (int)info->sc_loc_w.pos,
1203 (int)info->sc_loc_w.size);
1205 for (j = 0; j < info->sc_buttons; j++) {
1206 sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1207 (int)info->sc_iid_btn[j],
1208 (int)info->sc_loc_btn[j].pos,
1209 (int)info->sc_loc_btn[j].size);
1213 err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1220 static devclass_t ums_devclass;
1222 static device_method_t ums_methods[] = {
1223 DEVMETHOD(device_probe, ums_probe),
1224 DEVMETHOD(device_attach, ums_attach),
1225 DEVMETHOD(device_detach, ums_detach),
1229 static driver_t ums_driver = {
1231 .methods = ums_methods,
1232 .size = sizeof(struct ums_softc),
1235 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, NULL);
1236 MODULE_DEPEND(ums, usb, 1, 1, 1);
1237 #ifdef EVDEV_SUPPORT
1238 MODULE_DEPEND(ums, evdev, 1, 1, 1);
1240 MODULE_VERSION(ums, 1);