xref: /dragonfly/sys/bus/u4b/input/ums.c (revision a285bb00fb0722e1c2533849d83a52a0b1744afe)
1 /*-
2  * Copyright (c) 1998 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Lennart Augustsson (lennart@augustsson.net) at
7  * Carlstedt Research & Technology.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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  *
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.
29  */
30 
31 /*
32  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
33  */
34 
35 #include "opt_evdev.h"
36 
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>
43 #include <sys/bus.h>
44 #include <sys/module.h>
45 #include <sys/lock.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>
51 #include <sys/caps.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/sbuf.h>
55 
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>
60 #include "usbdevs.h"
61 
62 #define   USB_DEBUG_VAR ums_debug
63 #include <bus/u4b/usb_debug.h>
64 
65 #include <bus/u4b/quirk/usb_quirk.h>
66 
67 #ifdef EVDEV_SUPPORT
68 #include <dev/misc/evdev/input.h>
69 #include <dev/misc/evdev/evdev.h>
70 #endif
71 
72 #include <sys/filio.h>
73 #include <sys/tty.h>
74 #include <sys/mouse.h>
75 
76 #ifdef USB_DEBUG
77 static int ums_debug = 0;
78 
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");
82 #endif
83 
84 #define   MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
85 #define   MOUSE_FLAGS (HIO_RELATIVE)
86 
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 */
92 
93 enum {
94           UMS_INTR_DT,
95           UMS_N_TRANSFER,
96 };
97 
98 struct ums_info {
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];
105 
106           uint32_t sc_flags;
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
114 
115           uint8_t   sc_iid_w;
116           uint8_t   sc_iid_x;
117           uint8_t   sc_iid_y;
118           uint8_t   sc_iid_z;
119           uint8_t   sc_iid_t;
120           uint8_t   sc_iid_btn[UMS_BUTTON_MAX];
121           uint8_t   sc_buttons;
122 };
123 
124 struct ums_softc {
125           struct usb_fifo_sc sc_fifo;
126           struct lock sc_lock;
127           struct usb_callout sc_callout;
128           struct ums_info sc_info[UMS_INFO_MAX];
129 
130           mousehw_t sc_hw;
131           mousemode_t sc_mode;
132           mousestatus_t sc_status;
133 
134           struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
135 
136           int sc_pollrate;
137           int sc_fflags;
138           int sc_read_running;
139 #ifdef EVDEV_SUPPORT
140           int sc_evflags;
141 #define   UMS_EVDEV_OPENED    1
142 #endif
143 
144           uint8_t   sc_buttons;
145           uint8_t   sc_iid;
146           uint8_t   sc_temp[64];
147 
148 #ifdef EVDEV_SUPPORT
149           struct evdev_dev *sc_evdev;
150 #endif
151 };
152 
153 static void ums_put_queue_timeout(void *__sc);
154 
155 static usb_callback_t ums_intr_callback;
156 
157 static device_probe_t ums_probe;
158 static device_attach_t ums_attach;
159 static device_detach_t ums_detach;
160 
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;
166 
167 #ifdef EVDEV_SUPPORT
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);
172 #endif
173 
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);
178 #if 0 /* XXX */
179 static int          ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
180 #endif
181 
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",
189 };
190 
191 #ifdef EVDEV_SUPPORT
192 static const struct evdev_methods ums_evdev_methods = {
193           .ev_open = &ums_ev_open,
194           .ev_close = &ums_ev_close,
195 };
196 #endif
197 
198 static void
ums_put_queue_timeout(void * __sc)199 ums_put_queue_timeout(void *__sc)
200 {
201           struct ums_softc *sc = __sc;
202 
203           KKASSERT(lockowned(&sc->sc_lock));
204 
205           ums_put_queue(sc, 0, 0, 0, 0, 0);
206 #ifdef EVDEV_SUPPORT
207           ums_evdev_push(sc, 0, 0, 0, 0, 0);
208 #endif
209 }
210 
211 static void
ums_intr_callback(struct usb_xfer * xfer,usb_error_t error)212 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
213 {
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;
218           int32_t buttons = 0;
219           int32_t buttons_found = 0;
220 #ifdef EVDEV_SUPPORT
221           int32_t buttons_reported = 0;
222 #endif
223           int32_t dw = 0;
224           int32_t dx = 0;
225           int32_t dy = 0;
226           int32_t dz = 0;
227           int32_t dt = 0;
228           uint8_t i;
229           uint8_t id;
230           int len;
231 
232           usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
233 
234           switch (USB_GET_STATE(xfer)) {
235           case USB_ST_TRANSFERRED:
236                     DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
237 
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);
242                     }
243                     if (len == 0)
244                               goto tr_setup;
245 
246                     pc = usbd_xfer_get_frame(xfer, 0);
247                     usbd_copy_out(pc, 0, buf, len);
248 
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);
255 
256                     if (sc->sc_iid) {
257                               id = *buf;
258 
259                               len--;
260                               buf++;
261 
262                     } else {
263                               id = 0;
264                               if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
265                                         if ((*buf == 0x14) || (*buf == 0x15)) {
266                                                   goto tr_setup;
267                                         }
268                               }
269                     }
270 
271           repeat:
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);
275 
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);
279 
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);
283 
284                     if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
285                         (id == info->sc_iid_z)) {
286                               int32_t temp;
287                               temp = hid_get_data(buf, len, &info->sc_loc_z);
288                               if (info->sc_flags & UMS_FLAG_REVZ)
289                                         temp = -temp;
290                               dz -= temp;
291                     }
292 
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);
296 
297                     for (i = 0; i < info->sc_buttons; i++) {
298                               uint32_t mask;
299                               mask = 1UL << UMS_BUT(i);
300                               /* check for correct button ID */
301                               if (id != info->sc_iid_btn[i])
302                                         continue;
303                               /* check for button pressed */
304                               if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
305                                         buttons |= mask;
306                               /* register button mask */
307                               buttons_found |= mask;
308                     }
309 
310                     if (++info != &sc->sc_info[UMS_INFO_MAX])
311                               goto repeat;
312 
313                     /* keep old button value(s) for non-detected buttons */
314                     buttons |= sc->sc_status.button & ~buttons_found;
315 
316 #ifdef EVDEV_SUPPORT
317                     buttons_reported = buttons;
318 #endif
319 
320                     if (dx || dy || dz || dt || dw ||
321                         (buttons != sc->sc_status.button)) {
322 
323                               DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
324                                   dx, dy, dz, dt, dw, buttons);
325 
326                               /* translate T-axis into button presses until further */
327                               if (dt > 0)
328                                         buttons |= 1UL << 3;
329                               else if (dt < 0)
330                                         buttons |= 1UL << 4;
331 
332                               sc->sc_status.button = buttons;
333                               sc->sc_status.dx += dx;
334                               sc->sc_status.dy += dy;
335                               sc->sc_status.dz += dz;
336                               /*
337                                * sc->sc_status.dt += dt;
338                                * no way to export this yet
339                                */
340 
341                               /*
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
349                              * the timeout event.
350                              */
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)) {
354 
355                                         usb_callout_reset(&sc->sc_callout, hz / 20,
356                                             &ums_put_queue_timeout, sc);
357                               } else {
358 
359                                         usb_callout_stop(&sc->sc_callout);
360 
361                                         ums_put_queue(sc, dx, dy, dz, dt, buttons);
362 #ifdef EVDEV_SUPPORT
363                                         ums_evdev_push(sc, dx, dy, dz, dt,
364                                             buttons_reported);
365 #endif
366                               }
367                     }
368           case USB_ST_SETUP:
369 tr_setup:
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) {
372                               /*
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.
377                                */
378                               sc->sc_read_running = 0;
379 #ifdef EVDEV_SUPPORT
380                               /* XXX check logic. */
381                               if (sc->sc_evflags == 0)
382                                         break;
383 #else
384                               break;
385 #endif
386                     }
387 
388                     usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
389                     usbd_transfer_submit(xfer);
390                     break;
391 
392           default:                      /* Error */
393                     if (error != USB_ERR_CANCELLED) {
394                               /* try clear stall first */
395                               usbd_xfer_set_stall(xfer);
396                               goto tr_setup;
397                     }
398                     break;
399           }
400 }
401 
402 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
403 
404           [UMS_INTR_DT] = {
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,
411           },
412 };
413 
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),},
419 };
420 
421 static int
ums_probe(device_t dev)422 ums_probe(device_t dev)
423 {
424           struct usb_attach_arg *uaa = device_get_ivars(dev);
425           void *d_ptr;
426           int error;
427           uint16_t d_len;
428 
429           DPRINTFN(11, "\n");
430 
431           if (uaa->usb_mode != USB_MODE_HOST)
432                     return (ENXIO);
433 
434           if (uaa->info.bInterfaceClass != UICLASS_HID)
435                     return (ENXIO);
436 
437           if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
438                     return (ENXIO);
439 
440           if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
441               (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
442                     return (BUS_PROBE_DEFAULT);
443 
444           error = usbd_req_get_hid_desc(uaa->device, NULL,
445               &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
446 
447           if (error)
448                     return (ENXIO);
449 
450           if (hid_is_mouse(d_ptr, d_len))
451                     error = BUS_PROBE_DEFAULT;
452           else
453                     error = ENXIO;
454 
455           kfree(d_ptr, M_TEMP);
456           return (error);
457 }
458 
459 static void
ums_hid_parse(struct ums_softc * sc,device_t dev,const uint8_t * buf,uint16_t len,uint8_t index)460 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
461     uint16_t len, uint8_t index)
462 {
463           struct ums_info *info = &sc->sc_info[index];
464           uint32_t flags;
465           uint8_t i;
466           uint8_t j;
467 
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)) {
470 
471                     if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
472                               info->sc_flags |= UMS_FLAG_X_AXIS;
473                     }
474           }
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 
478                     if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
479                               info->sc_flags |= UMS_FLAG_Y_AXIS;
480                     }
481           }
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,
485               &info->sc_iid_z) ||
486               hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
487               HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
488               &info->sc_iid_z)) {
489                     if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
490                               info->sc_flags |= UMS_FLAG_Z_AXIS;
491                     }
492                     /*
493                      * We might have both a wheel and Z direction, if so put
494                      * put the Z on the W coordinate.
495                      */
496                     if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
497                         HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
498                         &info->sc_iid_w)) {
499 
500                               if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
501                                         info->sc_flags |= UMS_FLAG_W_AXIS;
502                               }
503                     }
504           } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
505               HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
506               &info->sc_iid_z)) {
507 
508                     if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
509                               info->sc_flags |= UMS_FLAG_Z_AXIS;
510                     }
511           }
512           /*
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
517            * TWHEEL
518            */
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)) {
522 
523                     info->sc_loc_t.pos += 8;
524 
525                     if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
526                               info->sc_flags |= UMS_FLAG_T_AXIS;
527                     }
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)) {
531 
532                     if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
533                               info->sc_flags |= UMS_FLAG_T_AXIS;
534           }
535           /* figure out the number of buttons */
536 
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])) {
541                               break;
542                     }
543           }
544 
545           /* detect other buttons */
546 
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])) {
551                               break;
552                     }
553           }
554 
555           info->sc_buttons = i;
556 
557           if (i > sc->sc_buttons)
558                     sc->sc_buttons = i;
559 
560           if (info->sc_flags == 0)
561                     return;
562 
563           /* announce information about the mouse */
564           device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
565               (info->sc_buttons),
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" : "",
571               info->sc_iid_x);
572 }
573 
574 static int
ums_attach(device_t dev)575 ums_attach(device_t dev)
576 {
577           struct usb_attach_arg *uaa = device_get_ivars(dev);
578           struct ums_softc *sc = device_get_softc(dev);
579           struct ums_info *info;
580           void *d_ptr = NULL;
581           int isize;
582           int err;
583           uint16_t d_len;
584           uint8_t i;
585 #ifdef USB_DEBUG
586           uint8_t j;
587 #endif
588 
589           DPRINTFN(11, "sc=%p\n", sc);
590 
591           device_set_usb_desc(dev);
592 
593           lockinit(&sc->sc_lock, "ums lock", 0, LK_CANRECURSE);
594 
595           usb_callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
596 
597           /*
598          * Force the report (non-boot) protocol.
599          *
600          * Mice without boot protocol support may choose not to implement
601          * Set_Protocol at all; Ignore any error.
602          */
603           err = usbd_req_set_protocol(uaa->device, NULL,
604               uaa->info.bIfaceIndex, 1);
605 
606           err = usbd_transfer_setup(uaa->device,
607               &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
608               UMS_N_TRANSFER, sc, &sc->sc_lock);
609 
610           if (err) {
611                     DPRINTF("error=%s\n", usbd_errstr(err));
612                     goto detach;
613           }
614 
615           /* Get HID descriptor */
616           err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
617               &d_len, M_TEMP, uaa->info.bIfaceIndex);
618 
619           if (err) {
620                     device_printf(dev, "error reading report description\n");
621                     goto detach;
622           }
623 
624           isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
625 
626           /*
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.
631            */
632           if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
633 
634                     sc->sc_iid = 0;
635 
636                     info = &sc->sc_info[0];
637                     info->sc_flags = (UMS_FLAG_X_AXIS |
638                         UMS_FLAG_Y_AXIS |
639                         UMS_FLAG_Z_AXIS |
640                         UMS_FLAG_SBU);
641                     info->sc_buttons = 3;
642                     isize = 5;
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;
656 
657                     /* Announce device */
658                     device_printf(dev, "3 buttons and [XYZ] "
659                         "coordinates ID=0\n");
660 
661           } else {
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);
665                     }
666           }
667 
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;
672           }
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]));
677           }
678           kfree(d_ptr, M_TEMP);
679           d_ptr = NULL;
680 
681 #ifdef USB_DEBUG
682           for (j = 0; j < UMS_INFO_MAX; j++) {
683                     info = &sc->sc_info[j];
684 
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);
696 
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]);
701                     }
702           }
703           DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
704 #endif
705 
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);
710           if (err)
711                     goto detach;
712 
713 #ifdef EVDEV_SUPPORT
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);
725 
726           info = &sc->sc_info[0];
727 
728           if (info->sc_flags & UMS_FLAG_X_AXIS)
729                     evdev_support_rel(sc->sc_evdev, REL_X);
730 
731           if (info->sc_flags & UMS_FLAG_Y_AXIS)
732                     evdev_support_rel(sc->sc_evdev, REL_Y);
733 
734           if (info->sc_flags & UMS_FLAG_Z_AXIS)
735                     evdev_support_rel(sc->sc_evdev, REL_WHEEL);
736 
737           if (info->sc_flags & UMS_FLAG_T_AXIS)
738                     evdev_support_rel(sc->sc_evdev, REL_HWHEEL);
739 
740           for (i = 0; i < info->sc_buttons; i++)
741                     evdev_support_key(sc->sc_evdev, BTN_MOUSE + i);
742 
743           err = evdev_register(sc->sc_evdev);
744           if (err)
745                     goto detach;
746 #endif
747 
748 #if 0
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");
754 #endif
755 
756           return (0);
757 
758 detach:
759           if (d_ptr) {
760                     kfree(d_ptr, M_TEMP);
761           }
762           ums_detach(dev);
763           return (ENOMEM);
764 }
765 
766 static int
ums_detach(device_t self)767 ums_detach(device_t self)
768 {
769           struct ums_softc *sc = device_get_softc(self);
770 
771           DPRINTF("sc=%p\n", sc);
772 
773           usb_fifo_detach(&sc->sc_fifo);
774 
775 #ifdef EVDEV_SUPPORT
776           evdev_free(sc->sc_evdev);
777           sc->sc_evdev = NULL;
778 #endif
779 
780           usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
781 
782           usb_callout_drain(&sc->sc_callout);
783 
784           lockuninit(&sc->sc_lock);
785 
786           return (0);
787 }
788 
789 static void
ums_reset(struct ums_softc * sc)790 ums_reset(struct ums_softc *sc)
791 {
792 
793           /* reset all USB mouse parameters */
794 
795           if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
796                     sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
797           else
798                     sc->sc_hw.buttons = sc->sc_buttons;
799 
800           sc->sc_hw.iftype = MOUSE_IF_USB;
801           sc->sc_hw.type = MOUSE_MOUSE;
802           sc->sc_hw.model = MOUSE_MODEL_GENERIC;
803           sc->sc_hw.hwid = 0;
804 
805           sc->sc_mode.protocol = MOUSE_PROTO_MSC;
806           sc->sc_mode.rate = -1;
807           sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
808           sc->sc_mode.accelfactor = 0;
809           sc->sc_mode.level = 0;
810           sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
811           sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
812           sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
813 
814           /* reset status */
815 
816           sc->sc_status.flags = 0;
817           sc->sc_status.button = 0;
818           sc->sc_status.obutton = 0;
819           sc->sc_status.dx = 0;
820           sc->sc_status.dy = 0;
821           sc->sc_status.dz = 0;
822           /* sc->sc_status.dt = 0; */
823 }
824 
825 static void
ums_start_rx(struct ums_softc * sc)826 ums_start_rx(struct ums_softc *sc)
827 {
828           int rate;
829 
830           /* Check if we should override the default polling interval */
831           rate = sc->sc_pollrate;
832           /* Range check rate */
833           if (rate > 1000)
834                     rate = 1000;
835           /* Check for set rate */
836           if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
837                     DPRINTF("Setting pollrate = %d\n", rate);
838                     /* Stop current transfer, if any */
839                     if (sc->sc_read_running) {
840                               usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
841                               sc->sc_read_running = 0;
842                     }
843                     /* Set new interval */
844                     usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
845                     /* Only set pollrate once */
846                     sc->sc_pollrate = 0;
847           }
848           if (sc->sc_read_running == 0) {
849                     sc->sc_read_running = 1;
850                     usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
851           }
852 }
853 
854 static void
ums_stop_rx(struct ums_softc * sc)855 ums_stop_rx(struct ums_softc *sc)
856 {
857           if (sc->sc_read_running) {
858                     usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
859                     sc->sc_read_running = 0;
860                     usb_callout_stop(&sc->sc_callout);
861           }
862 }
863 
864 static void
ums_fifo_start_read(struct usb_fifo * fifo)865 ums_fifo_start_read(struct usb_fifo *fifo)
866 {
867           struct ums_softc *sc = usb_fifo_softc(fifo);
868 
869           ums_start_rx(sc);
870 }
871 
872 static void
ums_fifo_stop_read(struct usb_fifo * fifo)873 ums_fifo_stop_read(struct usb_fifo *fifo)
874 {
875           struct ums_softc *sc = usb_fifo_softc(fifo);
876 
877           ums_stop_rx(sc);
878 }
879 
880 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
881      (MOUSE_MSC_PACKETSIZE != 5))
882 #error "Software assumptions are not met. Please update code."
883 #endif
884 
885 static void
ums_put_queue(struct ums_softc * sc,int32_t dx,int32_t dy,int32_t dz,int32_t dt,int32_t buttons)886 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
887     int32_t dz, int32_t dt, int32_t buttons)
888 {
889           uint8_t buf[8];
890 
891           if (1) {
892 
893                     if (dx > 254)
894                               dx = 254;
895                     if (dx < -256)
896                               dx = -256;
897                     if (dy > 254)
898                               dy = 254;
899                     if (dy < -256)
900                               dy = -256;
901                     if (dz > 126)
902                               dz = 126;
903                     if (dz < -128)
904                               dz = -128;
905                     if (dt > 126)
906                               dt = 126;
907                     if (dt < -128)
908                               dt = -128;
909 
910                     buf[0] = sc->sc_mode.syncmask[1];
911                     buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
912                     buf[1] = dx >> 1;
913                     buf[2] = dy >> 1;
914                     buf[3] = dx - (dx >> 1);
915                     buf[4] = dy - (dy >> 1);
916 
917                     if (sc->sc_mode.level == 1) {
918                               buf[5] = dz >> 1;
919                               buf[6] = dz - (dz >> 1);
920                               buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
921                     }
922                     usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
923                         sc->sc_mode.packetsize, 1);
924 
925           } else {
926                     DPRINTF("Buffer full, discarded packet\n");
927           }
928 }
929 
930 #ifdef EVDEV_SUPPORT
931 static void
ums_evdev_push(struct ums_softc * sc,int32_t dx,int32_t dy,int32_t dz,int32_t dt,int32_t buttons)932 ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy,
933     int32_t dz, int32_t dt, int32_t buttons)
934 {
935           if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
936                     /* Push evdev event */
937                     evdev_push_rel(sc->sc_evdev, REL_X, dx);
938                     evdev_push_rel(sc->sc_evdev, REL_Y, -dy);
939                     evdev_push_rel(sc->sc_evdev, REL_WHEEL, -dz);
940                     evdev_push_rel(sc->sc_evdev, REL_HWHEEL, dt);
941                     evdev_push_mouse_btn(sc->sc_evdev,
942                         (buttons & ~MOUSE_STDBUTTONS) |
943                         (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) |
944                         (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) |
945                         (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0));
946                     evdev_sync(sc->sc_evdev);
947           }
948 }
949 #endif
950 
951 static void
ums_reset_buf(struct ums_softc * sc)952 ums_reset_buf(struct ums_softc *sc)
953 {
954           /* reset read queue, must be called locked */
955           usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
956 }
957 
958 #ifdef EVDEV_SUPPORT
959 static int
ums_ev_open(struct evdev_dev * evdev)960 ums_ev_open(struct evdev_dev *evdev)
961 {
962           struct ums_softc *sc = (struct ums_softc *)evdev_get_softc(evdev);
963 
964           lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
965 
966           sc->sc_evflags = UMS_EVDEV_OPENED;
967 
968           if (sc->sc_fflags == 0) {
969                     ums_reset(sc);
970                     ums_start_rx(sc);
971           }
972 
973           lockmgr(&sc->sc_lock, LK_RELEASE);
974 
975           return (0);
976 }
977 
978 static int
ums_ev_close(struct evdev_dev * evdev)979 ums_ev_close(struct evdev_dev *evdev)
980 {
981           struct ums_softc *sc = (struct ums_softc *)evdev_get_softc(evdev);
982 
983           lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
984 
985           sc->sc_evflags = 0;
986 
987           if (sc->sc_fflags == 0)
988                     ums_stop_rx(sc);
989 
990           lockmgr(&sc->sc_lock, LK_RELEASE);
991 
992           return (0);
993 }
994 #endif
995 
996 static int
ums_fifo_open(struct usb_fifo * fifo,int fflags)997 ums_fifo_open(struct usb_fifo *fifo, int fflags)
998 {
999           struct ums_softc *sc = usb_fifo_softc(fifo);
1000 
1001           DPRINTFN(2, "\n");
1002 
1003           /* check for duplicate open, should not happen */
1004           if (sc->sc_fflags & fflags)
1005                     return (EBUSY);
1006 
1007           /* check for first open */
1008 #ifdef EVDEV_SUPPORT
1009           if (sc->sc_fflags == 0 && sc->sc_evflags == 0)
1010                     ums_reset(sc);
1011 #else
1012           if (sc->sc_fflags == 0)
1013                     ums_reset(sc);
1014 #endif
1015 
1016           if (fflags & FREAD) {
1017                     /* allocate RX buffer */
1018                     if (usb_fifo_alloc_buffer(fifo,
1019                         UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1020                               return (ENOMEM);
1021                     }
1022           }
1023 
1024           sc->sc_fflags |= fflags & (FREAD | FWRITE);
1025           return (0);
1026 }
1027 
1028 static void
ums_fifo_close(struct usb_fifo * fifo,int fflags)1029 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1030 {
1031           struct ums_softc *sc = usb_fifo_softc(fifo);
1032 
1033           DPRINTFN(2, "\n");
1034 
1035           if (fflags & FREAD)
1036                     usb_fifo_free_buffer(fifo);
1037 
1038           sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1039 }
1040 
1041 static int
ums_fifo_ioctl(struct usb_fifo * fifo,u_long cmd,void * addr,int fflags)1042 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1043 {
1044           struct ums_softc *sc = usb_fifo_softc(fifo);
1045           mousemode_t mode;
1046           int error = 0;
1047 
1048           DPRINTFN(2, "\n");
1049 
1050           lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
1051 
1052           switch (cmd) {
1053           case MOUSE_GETHWINFO:
1054                     *(mousehw_t *)addr = sc->sc_hw;
1055                     break;
1056 
1057           case MOUSE_GETMODE:
1058                     *(mousemode_t *)addr = sc->sc_mode;
1059                     break;
1060 
1061           case MOUSE_SETMODE:
1062                     mode = *(mousemode_t *)addr;
1063 
1064                     if (mode.level == -1) {
1065                               /* don't change the current setting */
1066                     } else if ((mode.level < 0) || (mode.level > 1)) {
1067                               error = EINVAL;
1068                               break;
1069                     } else {
1070                               sc->sc_mode.level = mode.level;
1071                     }
1072 
1073                     /* store polling rate */
1074                     sc->sc_pollrate = mode.rate;
1075 
1076                     if (sc->sc_mode.level == 0) {
1077                               if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1078                                         sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1079                               else
1080                                         sc->sc_hw.buttons = sc->sc_buttons;
1081                               sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1082                               sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1083                               sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1084                               sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1085                     } else if (sc->sc_mode.level == 1) {
1086                               if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1087                                         sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1088                               else
1089                                         sc->sc_hw.buttons = sc->sc_buttons;
1090                               sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1091                               sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1092                               sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1093                               sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1094                     }
1095                     ums_reset_buf(sc);
1096                     break;
1097 
1098           case MOUSE_GETLEVEL:
1099                     *(int *)addr = sc->sc_mode.level;
1100                     break;
1101 
1102           case MOUSE_SETLEVEL:
1103                     if (*(int *)addr < 0 || *(int *)addr > 1) {
1104                               error = EINVAL;
1105                               break;
1106                     }
1107                     sc->sc_mode.level = *(int *)addr;
1108 
1109                     if (sc->sc_mode.level == 0) {
1110                               if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1111                                         sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1112                               else
1113                                         sc->sc_hw.buttons = sc->sc_buttons;
1114                               sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1115                               sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1116                               sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1117                               sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1118                     } else if (sc->sc_mode.level == 1) {
1119                               if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1120                                         sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1121                               else
1122                                         sc->sc_hw.buttons = sc->sc_buttons;
1123                               sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1124                               sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1125                               sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1126                               sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1127                     }
1128                     ums_reset_buf(sc);
1129                     break;
1130 
1131           case MOUSE_GETSTATUS:{
1132                               mousestatus_t *status = (mousestatus_t *)addr;
1133 
1134                               *status = sc->sc_status;
1135                               sc->sc_status.obutton = sc->sc_status.button;
1136                               sc->sc_status.button = 0;
1137                               sc->sc_status.dx = 0;
1138                               sc->sc_status.dy = 0;
1139                               sc->sc_status.dz = 0;
1140                               /* sc->sc_status.dt = 0; */
1141 
1142                               if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1143                                         status->flags |= MOUSE_POSCHANGED;
1144                               }
1145                               if (status->button != status->obutton) {
1146                                         status->flags |= MOUSE_BUTTONSCHANGED;
1147                               }
1148                               break;
1149                     }
1150           default:
1151                     error = ENOTTY;
1152                     break;
1153           }
1154 
1155           lockmgr(&sc->sc_lock, LK_RELEASE);
1156           return (error);
1157 }
1158 
1159 #if 0 /* XXX */
1160 static int
1161 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1162 {
1163           struct ums_softc *sc = arg1;
1164           struct ums_info *info;
1165           struct sbuf *sb;
1166           int i, j, err, had_output;
1167 
1168           sb = sbuf_new_auto();
1169           for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1170                     info = &sc->sc_info[i];
1171 
1172                     /* Don't emit empty info */
1173                     if ((info->sc_flags &
1174                         (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1175                          UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1176                         info->sc_buttons == 0)
1177                               continue;
1178 
1179                     if (had_output)
1180                               sbuf_printf(sb, "\n");
1181                     had_output = 1;
1182                     sbuf_printf(sb, "i%d:", i + 1);
1183                     if (info->sc_flags & UMS_FLAG_X_AXIS)
1184                               sbuf_printf(sb, " X:r%d, p%d, s%d;",
1185                                   (int)info->sc_iid_x,
1186                                   (int)info->sc_loc_x.pos,
1187                                   (int)info->sc_loc_x.size);
1188                     if (info->sc_flags & UMS_FLAG_Y_AXIS)
1189                               sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1190                                   (int)info->sc_iid_y,
1191                                   (int)info->sc_loc_y.pos,
1192                                   (int)info->sc_loc_y.size);
1193                     if (info->sc_flags & UMS_FLAG_Z_AXIS)
1194                               sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1195                                   (int)info->sc_iid_z,
1196                                   (int)info->sc_loc_z.pos,
1197                                   (int)info->sc_loc_z.size);
1198                     if (info->sc_flags & UMS_FLAG_T_AXIS)
1199                               sbuf_printf(sb, " T:r%d, p%d, s%d;",
1200                                   (int)info->sc_iid_t,
1201                                   (int)info->sc_loc_t.pos,
1202                                   (int)info->sc_loc_t.size);
1203                     if (info->sc_flags & UMS_FLAG_W_AXIS)
1204                               sbuf_printf(sb, " W:r%d, p%d, s%d;",
1205                                   (int)info->sc_iid_w,
1206                                   (int)info->sc_loc_w.pos,
1207                                   (int)info->sc_loc_w.size);
1208 
1209                     for (j = 0; j < info->sc_buttons; j++) {
1210                               sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1211                                   (int)info->sc_iid_btn[j],
1212                                   (int)info->sc_loc_btn[j].pos,
1213                                   (int)info->sc_loc_btn[j].size);
1214                     }
1215           }
1216           sbuf_finish(sb);
1217           err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1218           sbuf_delete(sb);
1219 
1220           return (err);
1221 }
1222 #endif
1223 
1224 static devclass_t ums_devclass;
1225 
1226 static device_method_t ums_methods[] = {
1227           DEVMETHOD(device_probe, ums_probe),
1228           DEVMETHOD(device_attach, ums_attach),
1229           DEVMETHOD(device_detach, ums_detach),
1230           DEVMETHOD_END
1231 };
1232 
1233 static driver_t ums_driver = {
1234           .name = "ums",
1235           .methods = ums_methods,
1236           .size = sizeof(struct ums_softc),
1237 };
1238 
1239 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, NULL);
1240 MODULE_DEPEND(ums, usb, 1, 1, 1);
1241 #ifdef EVDEV_SUPPORT
1242 MODULE_DEPEND(ums, evdev, 1, 1, 1);
1243 #endif
1244 MODULE_VERSION(ums, 1);
1245