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.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
45 #include <sys/stdint.h>
46 #include <sys/stddef.h>
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/types.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/linker_set.h>
54 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
65 #include <sys/fcntl.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbhid.h>
74 #define USB_DEBUG_VAR ums_debug
75 #include <dev/usb/usb_debug.h>
77 #include <dev/usb/quirk/usb_quirk.h>
79 #include <sys/ioccom.h>
80 #include <sys/filio.h>
82 #include <sys/mouse.h>
85 static int ums_debug = 0;
87 SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
88 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
89 &ums_debug, 0, "Debug level");
92 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
93 #define MOUSE_FLAGS (HIO_RELATIVE)
95 #define UMS_BUF_SIZE 8 /* bytes */
96 #define UMS_IFQ_MAXLEN 50 /* units */
97 #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
98 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
99 #define UMS_INFO_MAX 2 /* maximum number of HID sets */
107 struct hid_location sc_loc_w;
108 struct hid_location sc_loc_x;
109 struct hid_location sc_loc_y;
110 struct hid_location sc_loc_z;
111 struct hid_location sc_loc_t;
112 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
115 #define UMS_FLAG_X_AXIS 0x0001
116 #define UMS_FLAG_Y_AXIS 0x0002
117 #define UMS_FLAG_Z_AXIS 0x0004
118 #define UMS_FLAG_T_AXIS 0x0008
119 #define UMS_FLAG_SBU 0x0010 /* spurious button up events */
120 #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */
121 #define UMS_FLAG_W_AXIS 0x0040
128 uint8_t sc_iid_btn[UMS_BUTTON_MAX];
133 struct usb_fifo_sc sc_fifo;
135 struct usb_callout sc_callout;
136 struct ums_info sc_info[UMS_INFO_MAX];
140 mousestatus_t sc_status;
142 struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
151 static void ums_put_queue_timeout(void *__sc);
153 static usb_callback_t ums_intr_callback;
155 static device_probe_t ums_probe;
156 static device_attach_t ums_attach;
157 static device_detach_t ums_detach;
159 static usb_fifo_cmd_t ums_start_read;
160 static usb_fifo_cmd_t ums_stop_read;
161 static usb_fifo_open_t ums_open;
162 static usb_fifo_close_t ums_close;
163 static usb_fifo_ioctl_t ums_ioctl;
165 static void ums_put_queue(struct ums_softc *, int32_t, int32_t,
166 int32_t, int32_t, int32_t);
167 static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
169 static struct usb_fifo_methods ums_fifo_methods = {
171 .f_close = &ums_close,
172 .f_ioctl = &ums_ioctl,
173 .f_start_read = &ums_start_read,
174 .f_stop_read = &ums_stop_read,
175 .basename[0] = "ums",
179 ums_put_queue_timeout(void *__sc)
181 struct ums_softc *sc = __sc;
183 mtx_assert(&sc->sc_mtx, MA_OWNED);
185 ums_put_queue(sc, 0, 0, 0, 0, 0);
189 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
191 struct ums_softc *sc = usbd_xfer_softc(xfer);
192 struct ums_info *info = &sc->sc_info[0];
193 struct usb_page_cache *pc;
194 uint8_t *buf = sc->sc_temp;
196 int32_t buttons_found = 0;
206 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
208 switch (USB_GET_STATE(xfer)) {
209 case USB_ST_TRANSFERRED:
210 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
212 if (len > sizeof(sc->sc_temp)) {
213 DPRINTFN(6, "truncating large packet to %zu bytes\n",
214 sizeof(sc->sc_temp));
215 len = sizeof(sc->sc_temp);
220 pc = usbd_xfer_get_frame(xfer, 0);
221 usbd_copy_out(pc, 0, buf, len);
223 DPRINTFN(6, "data = %02x %02x %02x %02x "
224 "%02x %02x %02x %02x\n",
225 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
226 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
227 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
228 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
238 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
239 if ((*buf == 0x14) || (*buf == 0x15)) {
246 if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
247 (id == info->sc_iid_w))
248 dw += hid_get_data(buf, len, &info->sc_loc_w);
250 if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
251 (id == info->sc_iid_x))
252 dx += hid_get_data(buf, len, &info->sc_loc_x);
254 if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
255 (id == info->sc_iid_y))
256 dy = -hid_get_data(buf, len, &info->sc_loc_y);
258 if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
259 (id == info->sc_iid_z)) {
261 temp = hid_get_data(buf, len, &info->sc_loc_z);
262 if (info->sc_flags & UMS_FLAG_REVZ)
267 if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
268 (id == info->sc_iid_t))
269 dt -= hid_get_data(buf, len, &info->sc_loc_t);
271 for (i = 0; i < info->sc_buttons; i++) {
273 mask = 1UL << UMS_BUT(i);
274 /* check for correct button ID */
275 if (id != info->sc_iid_btn[i])
277 /* check for button pressed */
278 if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
280 /* register button mask */
281 buttons_found |= mask;
284 if (++info != &sc->sc_info[UMS_INFO_MAX])
287 /* keep old button value(s) for non-detected buttons */
288 buttons |= sc->sc_status.button & ~buttons_found;
290 if (dx || dy || dz || dt || dw ||
291 (buttons != sc->sc_status.button)) {
293 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
294 dx, dy, dz, dt, dw, buttons);
296 /* translate T-axis into button presses until further */
302 sc->sc_status.button = buttons;
303 sc->sc_status.dx += dx;
304 sc->sc_status.dy += dy;
305 sc->sc_status.dz += dz;
307 * sc->sc_status.dt += dt;
308 * no way to export this yet
312 * The Qtronix keyboard has a built in PS/2
313 * port for a mouse. The firmware once in a
314 * while posts a spurious button up
315 * event. This event we ignore by doing a
316 * timeout for 50 msecs. If we receive
317 * dx=dy=dz=buttons=0 before we add the event
318 * to the queue. In any other case we delete
321 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
322 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
323 (dw == 0) && (buttons == 0)) {
325 usb_callout_reset(&sc->sc_callout, hz / 20,
326 &ums_put_queue_timeout, sc);
329 usb_callout_stop(&sc->sc_callout);
331 ums_put_queue(sc, dx, dy, dz, dt, buttons);
336 /* check if we can put more data into the FIFO */
337 if (usb_fifo_put_bytes_max(
338 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
339 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
340 usbd_transfer_submit(xfer);
345 if (error != USB_ERR_CANCELLED) {
346 /* try clear stall first */
347 usbd_xfer_set_stall(xfer);
354 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
357 .type = UE_INTERRUPT,
358 .endpoint = UE_ADDR_ANY,
359 .direction = UE_DIR_IN,
360 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
361 .bufsize = 0, /* use wMaxPacketSize */
362 .callback = &ums_intr_callback,
367 ums_probe(device_t dev)
369 struct usb_attach_arg *uaa = device_get_ivars(dev);
376 if (uaa->usb_mode != USB_MODE_HOST)
379 if (uaa->info.bInterfaceClass != UICLASS_HID)
382 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
383 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
384 return (BUS_PROBE_GENERIC);
386 error = usbd_req_get_hid_desc(uaa->device, NULL,
387 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
392 if (hid_is_collection(d_ptr, d_len,
393 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
394 error = BUS_PROBE_GENERIC;
403 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
404 uint16_t len, uint8_t index)
406 struct ums_info *info = &sc->sc_info[index];
410 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
411 hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
413 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
414 info->sc_flags |= UMS_FLAG_X_AXIS;
417 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
418 hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
420 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
421 info->sc_flags |= UMS_FLAG_Y_AXIS;
424 /* Try the wheel first as the Z activator since it's tradition. */
425 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
426 HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
428 hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
429 HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
431 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
432 info->sc_flags |= UMS_FLAG_Z_AXIS;
435 * We might have both a wheel and Z direction, if so put
436 * put the Z on the W coordinate.
438 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
439 HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
442 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
443 info->sc_flags |= UMS_FLAG_W_AXIS;
446 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
447 HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
450 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
451 info->sc_flags |= UMS_FLAG_Z_AXIS;
455 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
456 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
457 * to know that the byte after the wheel is the tilt axis.
458 * There are no other HID axis descriptors other than X,Y and
461 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
462 HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
463 &flags, &info->sc_iid_t)) {
465 info->sc_loc_t.pos += 8;
467 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
468 info->sc_flags |= UMS_FLAG_T_AXIS;
470 } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
471 HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
472 &flags, &info->sc_iid_t)) {
474 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
475 info->sc_flags |= UMS_FLAG_T_AXIS;
477 /* figure out the number of buttons */
479 for (i = 0; i < UMS_BUTTON_MAX; i++) {
480 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
481 hid_input, index, &info->sc_loc_btn[i], NULL,
482 &info->sc_iid_btn[i])) {
486 info->sc_buttons = i;
488 if (i > sc->sc_buttons)
491 if (info->sc_flags == 0)
494 /* announce information about the mouse */
495 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
497 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
498 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
499 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
500 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
501 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
506 ums_attach(device_t dev)
508 struct usb_attach_arg *uaa = device_get_ivars(dev);
509 struct ums_softc *sc = device_get_softc(dev);
510 struct ums_info *info;
520 DPRINTFN(11, "sc=%p\n", sc);
522 device_set_usb_desc(dev);
524 mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
526 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
529 * Force the report (non-boot) protocol.
531 * Mice without boot protocol support may choose not to implement
532 * Set_Protocol at all; Ignore any error.
534 err = usbd_req_set_protocol(uaa->device, NULL,
535 uaa->info.bIfaceIndex, 1);
537 err = usbd_transfer_setup(uaa->device,
538 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
539 UMS_N_TRANSFER, sc, &sc->sc_mtx);
542 DPRINTF("error=%s\n", usbd_errstr(err));
546 /* Get HID descriptor */
547 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
548 &d_len, M_TEMP, uaa->info.bIfaceIndex);
551 device_printf(dev, "error reading report description\n");
555 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
558 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
559 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
560 * all of its other button positions are all off. It also reports that
561 * it has two addional buttons and a tilt wheel.
563 if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
567 info = &sc->sc_info[0];
568 info->sc_flags = (UMS_FLAG_X_AXIS |
572 info->sc_buttons = 3;
574 /* 1st byte of descriptor report contains garbage */
575 info->sc_loc_x.pos = 16;
576 info->sc_loc_x.size = 8;
577 info->sc_loc_y.pos = 24;
578 info->sc_loc_y.size = 8;
579 info->sc_loc_z.pos = 32;
580 info->sc_loc_z.size = 8;
581 info->sc_loc_btn[0].pos = 8;
582 info->sc_loc_btn[0].size = 1;
583 info->sc_loc_btn[1].pos = 9;
584 info->sc_loc_btn[1].size = 1;
585 info->sc_loc_btn[2].pos = 10;
586 info->sc_loc_btn[2].size = 1;
588 /* Announce device */
589 device_printf(dev, "3 buttons and [XYZ] "
590 "coordinates ID=0\n");
593 /* Search the HID descriptor and announce device */
594 for (i = 0; i < UMS_INFO_MAX; i++) {
595 ums_hid_parse(sc, dev, d_ptr, d_len, i);
599 if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
600 info = &sc->sc_info[0];
601 /* Some wheels need the Z axis reversed. */
602 info->sc_flags |= UMS_FLAG_REVZ;
604 if (isize > usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
605 DPRINTF("WARNING: report size, %d bytes, is larger "
606 "than interrupt size, %d bytes!\n", isize,
607 usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
613 for (j = 0; j < UMS_INFO_MAX; j++) {
614 info = &sc->sc_info[j];
616 DPRINTF("sc=%p, index=%d\n", sc, j);
617 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
618 info->sc_loc_x.size, info->sc_iid_x);
619 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
620 info->sc_loc_y.size, info->sc_iid_y);
621 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
622 info->sc_loc_z.size, info->sc_iid_z);
623 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
624 info->sc_loc_t.size, info->sc_iid_t);
625 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
626 info->sc_loc_w.size, info->sc_iid_w);
628 for (i = 0; i < info->sc_buttons; i++) {
629 DPRINTF("B%d\t%d/%d id=%d\n",
630 i + 1, info->sc_loc_btn[i].pos,
631 info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
634 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
637 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
638 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
640 sc->sc_hw.buttons = sc->sc_buttons;
642 sc->sc_hw.iftype = MOUSE_IF_USB;
643 sc->sc_hw.type = MOUSE_MOUSE;
644 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
647 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
648 sc->sc_mode.rate = -1;
649 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
650 sc->sc_mode.accelfactor = 0;
651 sc->sc_mode.level = 0;
652 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
653 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
654 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
656 err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
657 &ums_fifo_methods, &sc->sc_fifo,
658 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
659 UID_ROOT, GID_OPERATOR, 0644);
663 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
664 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
665 OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD,
666 sc, 0, ums_sysctl_handler_parseinfo,
667 "", "Dump UMS report parsing information");
680 ums_detach(device_t self)
682 struct ums_softc *sc = device_get_softc(self);
684 DPRINTF("sc=%p\n", sc);
686 usb_fifo_detach(&sc->sc_fifo);
688 usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
690 usb_callout_drain(&sc->sc_callout);
692 mtx_destroy(&sc->sc_mtx);
698 ums_start_read(struct usb_fifo *fifo)
700 struct ums_softc *sc = usb_fifo_softc(fifo);
703 /* Check if we should override the default polling interval */
704 rate = sc->sc_pollrate;
705 /* Range check rate */
708 /* Check for set rate */
709 if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
710 DPRINTF("Setting pollrate = %d\n", rate);
711 /* Stop current transfer, if any */
712 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
713 /* Set new interval */
714 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
715 /* Only set pollrate once */
719 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
723 ums_stop_read(struct usb_fifo *fifo)
725 struct ums_softc *sc = usb_fifo_softc(fifo);
727 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
728 usb_callout_stop(&sc->sc_callout);
732 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
733 (MOUSE_MSC_PACKETSIZE != 5))
734 #error "Software assumptions are not met. Please update code."
738 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
739 int32_t dz, int32_t dt, int32_t buttons)
762 buf[0] = sc->sc_mode.syncmask[1];
763 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
766 buf[3] = dx - (dx >> 1);
767 buf[4] = dy - (dy >> 1);
769 if (sc->sc_mode.level == 1) {
771 buf[6] = dz - (dz >> 1);
772 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
774 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
775 sc->sc_mode.packetsize, 1);
778 DPRINTF("Buffer full, discarded packet\n");
783 ums_reset_buf(struct ums_softc *sc)
785 /* reset read queue */
786 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
790 ums_open(struct usb_fifo *fifo, int fflags)
792 struct ums_softc *sc = usb_fifo_softc(fifo);
796 if (fflags & FREAD) {
800 sc->sc_status.flags = 0;
801 sc->sc_status.button = 0;
802 sc->sc_status.obutton = 0;
803 sc->sc_status.dx = 0;
804 sc->sc_status.dy = 0;
805 sc->sc_status.dz = 0;
806 /* sc->sc_status.dt = 0; */
808 if (usb_fifo_alloc_buffer(fifo,
809 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
817 ums_close(struct usb_fifo *fifo, int fflags)
819 if (fflags & FREAD) {
820 usb_fifo_free_buffer(fifo);
825 ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
827 struct ums_softc *sc = usb_fifo_softc(fifo);
833 mtx_lock(&sc->sc_mtx);
836 case MOUSE_GETHWINFO:
837 *(mousehw_t *)addr = sc->sc_hw;
841 *(mousemode_t *)addr = sc->sc_mode;
845 mode = *(mousemode_t *)addr;
847 if (mode.level == -1) {
848 /* don't change the current setting */
849 } else if ((mode.level < 0) || (mode.level > 1)) {
853 sc->sc_mode.level = mode.level;
856 /* store polling rate */
857 sc->sc_pollrate = mode.rate;
859 if (sc->sc_mode.level == 0) {
860 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
861 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
863 sc->sc_hw.buttons = sc->sc_buttons;
864 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
865 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
866 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
867 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
868 } else if (sc->sc_mode.level == 1) {
869 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
870 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
872 sc->sc_hw.buttons = sc->sc_buttons;
873 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
874 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
875 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
876 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
882 *(int *)addr = sc->sc_mode.level;
886 if (*(int *)addr < 0 || *(int *)addr > 1) {
890 sc->sc_mode.level = *(int *)addr;
892 if (sc->sc_mode.level == 0) {
893 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
894 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
896 sc->sc_hw.buttons = sc->sc_buttons;
897 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
898 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
899 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
900 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
901 } else if (sc->sc_mode.level == 1) {
902 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
903 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
905 sc->sc_hw.buttons = sc->sc_buttons;
906 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
907 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
908 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
909 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
914 case MOUSE_GETSTATUS:{
915 mousestatus_t *status = (mousestatus_t *)addr;
917 *status = sc->sc_status;
918 sc->sc_status.obutton = sc->sc_status.button;
919 sc->sc_status.button = 0;
920 sc->sc_status.dx = 0;
921 sc->sc_status.dy = 0;
922 sc->sc_status.dz = 0;
923 /* sc->sc_status.dt = 0; */
925 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
926 status->flags |= MOUSE_POSCHANGED;
928 if (status->button != status->obutton) {
929 status->flags |= MOUSE_BUTTONSCHANGED;
938 mtx_unlock(&sc->sc_mtx);
943 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
945 struct ums_softc *sc = arg1;
946 struct ums_info *info;
950 sb = sbuf_new_auto();
951 for (i = 0; i < UMS_INFO_MAX; i++) {
952 info = &sc->sc_info[i];
954 /* Don't emit empty info */
955 if ((info->sc_flags &
956 (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
957 UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
958 info->sc_buttons == 0)
961 sbuf_printf(sb, "i%d:", i + 1);
962 if (info->sc_flags & UMS_FLAG_X_AXIS)
963 sbuf_printf(sb, " X:r%d, p%d, s%d;",
965 (int)info->sc_loc_x.pos,
966 (int)info->sc_loc_x.size);
967 if (info->sc_flags & UMS_FLAG_Y_AXIS)
968 sbuf_printf(sb, " Y:r%d, p%d, s%d;",
970 (int)info->sc_loc_y.pos,
971 (int)info->sc_loc_y.size);
972 if (info->sc_flags & UMS_FLAG_Z_AXIS)
973 sbuf_printf(sb, " Z:r%d, p%d, s%d;",
975 (int)info->sc_loc_z.pos,
976 (int)info->sc_loc_z.size);
977 if (info->sc_flags & UMS_FLAG_T_AXIS)
978 sbuf_printf(sb, " T:r%d, p%d, s%d;",
980 (int)info->sc_loc_t.pos,
981 (int)info->sc_loc_t.size);
982 if (info->sc_flags & UMS_FLAG_W_AXIS)
983 sbuf_printf(sb, " W:r%d, p%d, s%d;",
985 (int)info->sc_loc_w.pos,
986 (int)info->sc_loc_w.size);
988 for (j = 0; j < info->sc_buttons; j++) {
989 sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
990 (int)info->sc_iid_btn[j],
991 (int)info->sc_loc_btn[j].pos,
992 (int)info->sc_loc_btn[j].size);
994 sbuf_printf(sb, "\n");
997 err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1003 static devclass_t ums_devclass;
1005 static device_method_t ums_methods[] = {
1006 DEVMETHOD(device_probe, ums_probe),
1007 DEVMETHOD(device_attach, ums_attach),
1008 DEVMETHOD(device_detach, ums_detach),
1012 static driver_t ums_driver = {
1014 .methods = ums_methods,
1015 .size = sizeof(struct ums_softc),
1018 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
1019 MODULE_DEPEND(ums, usb, 1, 1, 1);