drm/linux: Port kfifo.h to DragonFly BSD
[dragonfly.git] / sys / bus / u4b / input / ums.c
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/priv.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
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
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 #ifdef EVDEV_SUPPORT
314                 buttons_reported = buttons;
315 #endif
316                 /* keep old button value(s) for non-detected buttons */
317                 buttons |= sc->sc_status.button & ~buttons_found;
318
319                 if (dx || dy || dz || dt || dw ||
320                     (buttons != sc->sc_status.button)) {
321
322                         DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
323                             dx, dy, dz, dt, dw, buttons);
324
325                         /* translate T-axis into button presses until further */
326                         if (dt > 0)
327                                 buttons |= 1UL << 3;
328                         else if (dt < 0)
329                                 buttons |= 1UL << 4;
330
331                         sc->sc_status.button = buttons;
332                         sc->sc_status.dx += dx;
333                         sc->sc_status.dy += dy;
334                         sc->sc_status.dz += dz;
335                         /*
336                          * sc->sc_status.dt += dt;
337                          * no way to export this yet
338                          */
339
340                         /*
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
348                          * the timeout event.
349                          */
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)) {
353
354                                 usb_callout_reset(&sc->sc_callout, hz / 20,
355                                     &ums_put_queue_timeout, sc);
356                         } else {
357
358                                 usb_callout_stop(&sc->sc_callout);
359
360                                 ums_put_queue(sc, dx, dy, dz, dt, buttons);
361 #ifdef EVDEV_SUPPORT
362                                 ums_evdev_push(sc, dx, dy, dz, dt,
363                                     buttons_reported);
364 #endif
365                         }
366                 }
367         case USB_ST_SETUP:
368 tr_setup:
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) {
371                         /*
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.
376                          */
377                         sc->sc_read_running = 0;
378 #ifdef EVDEV_SUPPORT
379                         /* XXX check logic. */
380                         if (sc->sc_evflags == 0)
381                                 break;
382 #else
383                         break;
384 #endif
385                 }
386
387                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
388                 usbd_transfer_submit(xfer);
389                 break;
390
391         default:                        /* Error */
392                 if (error != USB_ERR_CANCELLED) {
393                         /* try clear stall first */
394                         usbd_xfer_set_stall(xfer);
395                         goto tr_setup;
396                 }
397                 break;
398         }
399 }
400
401 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
402
403         [UMS_INTR_DT] = {
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,
410         },
411 };
412
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),},
418 };
419
420 static int
421 ums_probe(device_t dev)
422 {
423         struct usb_attach_arg *uaa = device_get_ivars(dev);
424         void *d_ptr;
425         int error;
426         uint16_t d_len;
427
428         DPRINTFN(11, "\n");
429
430         if (uaa->usb_mode != USB_MODE_HOST)
431                 return (ENXIO);
432
433         if (uaa->info.bInterfaceClass != UICLASS_HID)
434                 return (ENXIO);
435
436         if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
437                 return (ENXIO);
438
439         if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
440             (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
441                 return (BUS_PROBE_DEFAULT);
442
443         error = usbd_req_get_hid_desc(uaa->device, NULL,
444             &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
445
446         if (error)
447                 return (ENXIO);
448
449         if (hid_is_mouse(d_ptr, d_len))
450                 error = BUS_PROBE_DEFAULT;
451         else
452                 error = ENXIO;
453
454         kfree(d_ptr, M_TEMP);
455         return (error);
456 }
457
458 static void
459 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
460     uint16_t len, uint8_t index)
461 {
462         struct ums_info *info = &sc->sc_info[index];
463         uint32_t flags;
464         uint8_t i;
465         uint8_t j;
466
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)) {
469
470                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
471                         info->sc_flags |= UMS_FLAG_X_AXIS;
472                 }
473         }
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)) {
476
477                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
478                         info->sc_flags |= UMS_FLAG_Y_AXIS;
479                 }
480         }
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,
484             &info->sc_iid_z) ||
485             hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
486             HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
487             &info->sc_iid_z)) {
488                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
489                         info->sc_flags |= UMS_FLAG_Z_AXIS;
490                 }
491                 /*
492                  * We might have both a wheel and Z direction, if so put
493                  * put the Z on the W coordinate.
494                  */
495                 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
496                     HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
497                     &info->sc_iid_w)) {
498
499                         if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
500                                 info->sc_flags |= UMS_FLAG_W_AXIS;
501                         }
502                 }
503         } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
504             HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
505             &info->sc_iid_z)) {
506
507                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
508                         info->sc_flags |= UMS_FLAG_Z_AXIS;
509                 }
510         }
511         /*
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
516          * TWHEEL
517          */
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)) {
521
522                 info->sc_loc_t.pos += 8;
523
524                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
525                         info->sc_flags |= UMS_FLAG_T_AXIS;
526                 }
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)) {
530
531                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
532                         info->sc_flags |= UMS_FLAG_T_AXIS;
533         }
534         /* figure out the number of buttons */
535
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])) {
540                         break;
541                 }
542         }
543
544         /* detect other buttons */
545
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])) {
550                         break;
551                 }
552         }
553
554         info->sc_buttons = i;
555
556         if (i > sc->sc_buttons)
557                 sc->sc_buttons = i;
558
559         if (info->sc_flags == 0)
560                 return;
561
562         /* announce information about the mouse */
563         device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
564             (info->sc_buttons),
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" : "",
570             info->sc_iid_x);
571 }
572
573 static int
574 ums_attach(device_t dev)
575 {
576         struct usb_attach_arg *uaa = device_get_ivars(dev);
577         struct ums_softc *sc = device_get_softc(dev);
578         struct ums_info *info;
579         void *d_ptr = NULL;
580         int isize;
581         int err;
582         uint16_t d_len;
583         uint8_t i;
584 #ifdef USB_DEBUG
585         uint8_t j;
586 #endif
587
588         DPRINTFN(11, "sc=%p\n", sc);
589
590         device_set_usb_desc(dev);
591
592         lockinit(&sc->sc_lock, "ums lock", 0, LK_CANRECURSE);
593
594         usb_callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
595
596         /*
597          * Force the report (non-boot) protocol.
598          *
599          * Mice without boot protocol support may choose not to implement
600          * Set_Protocol at all; Ignore any error.
601          */
602         err = usbd_req_set_protocol(uaa->device, NULL,
603             uaa->info.bIfaceIndex, 1);
604
605         err = usbd_transfer_setup(uaa->device,
606             &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
607             UMS_N_TRANSFER, sc, &sc->sc_lock);
608
609         if (err) {
610                 DPRINTF("error=%s\n", usbd_errstr(err));
611                 goto detach;
612         }
613
614         /* Get HID descriptor */
615         err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
616             &d_len, M_TEMP, uaa->info.bIfaceIndex);
617
618         if (err) {
619                 device_printf(dev, "error reading report description\n");
620                 goto detach;
621         }
622
623         isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
624
625         /*
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.
630          */
631         if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
632
633                 sc->sc_iid = 0;
634
635                 info = &sc->sc_info[0];
636                 info->sc_flags = (UMS_FLAG_X_AXIS |
637                     UMS_FLAG_Y_AXIS |
638                     UMS_FLAG_Z_AXIS |
639                     UMS_FLAG_SBU);
640                 info->sc_buttons = 3;
641                 isize = 5;
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;
655
656                 /* Announce device */
657                 device_printf(dev, "3 buttons and [XYZ] "
658                     "coordinates ID=0\n");
659
660         } else {
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);
664                 }
665         }
666
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;
671         }
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]));
676         }
677         kfree(d_ptr, M_TEMP);
678         d_ptr = NULL;
679
680 #ifdef USB_DEBUG
681         for (j = 0; j < UMS_INFO_MAX; j++) {
682                 info = &sc->sc_info[j];
683
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);
695
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]);
700                 }
701         }
702         DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
703 #endif
704
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);
709         if (err)
710                 goto detach;
711
712 #ifdef EVDEV_SUPPORT
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);
724
725         info = &sc->sc_info[0];
726
727         if (info->sc_flags & UMS_FLAG_X_AXIS)
728                 evdev_support_rel(sc->sc_evdev, REL_X);
729
730         if (info->sc_flags & UMS_FLAG_Y_AXIS)
731                 evdev_support_rel(sc->sc_evdev, REL_Y);
732
733         if (info->sc_flags & UMS_FLAG_Z_AXIS)
734                 evdev_support_rel(sc->sc_evdev, REL_WHEEL);
735
736         if (info->sc_flags & UMS_FLAG_T_AXIS)
737                 evdev_support_rel(sc->sc_evdev, REL_HWHEEL);
738
739         for (i = 0; i < info->sc_buttons; i++)
740                 evdev_support_key(sc->sc_evdev, BTN_MOUSE + i);
741
742         err = evdev_register(sc->sc_evdev);
743         if (err)
744                 goto detach;
745 #endif
746
747 #if 0
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");
753 #endif
754
755         return (0);
756
757 detach:
758         if (d_ptr) {
759                 kfree(d_ptr, M_TEMP);
760         }
761         ums_detach(dev);
762         return (ENOMEM);
763 }
764
765 static int
766 ums_detach(device_t self)
767 {
768         struct ums_softc *sc = device_get_softc(self);
769
770         DPRINTF("sc=%p\n", sc);
771
772         usb_fifo_detach(&sc->sc_fifo);
773
774 #ifdef EVDEV_SUPPORT
775         evdev_free(sc->sc_evdev);
776 #endif
777
778         usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
779
780         usb_callout_drain(&sc->sc_callout);
781
782         lockuninit(&sc->sc_lock);
783
784         return (0);
785 }
786
787 static void
788 ums_reset(struct ums_softc *sc)
789 {
790
791         /* reset all USB mouse parameters */
792
793         if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
794                 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
795         else
796                 sc->sc_hw.buttons = sc->sc_buttons;
797
798         sc->sc_hw.iftype = MOUSE_IF_USB;
799         sc->sc_hw.type = MOUSE_MOUSE;
800         sc->sc_hw.model = MOUSE_MODEL_GENERIC;
801         sc->sc_hw.hwid = 0;
802
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;
811
812         /* reset status */
813
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; */
821 }
822
823 static void
824 ums_start_rx(struct ums_softc *sc)
825 {
826         int rate;
827
828         /* Check if we should override the default polling interval */
829         rate = sc->sc_pollrate;
830         /* Range check rate */
831         if (rate > 1000)
832                 rate = 1000;
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;
840                 }
841                 /* Set new interval */
842                 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
843                 /* Only set pollrate once */
844                 sc->sc_pollrate = 0;
845         }
846         if (sc->sc_read_running == 0) {
847                 sc->sc_read_running = 1;
848                 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
849         }
850 }
851
852 static void
853 ums_stop_rx(struct ums_softc *sc)
854 {
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);
859         }
860 }
861
862 static void
863 ums_fifo_start_read(struct usb_fifo *fifo)
864 {
865         struct ums_softc *sc = usb_fifo_softc(fifo);
866
867         ums_start_rx(sc);
868 }
869
870 static void
871 ums_fifo_stop_read(struct usb_fifo *fifo)
872 {
873         struct ums_softc *sc = usb_fifo_softc(fifo);
874
875         ums_stop_rx(sc);
876 }
877
878 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
879      (MOUSE_MSC_PACKETSIZE != 5))
880 #error "Software assumptions are not met. Please update code."
881 #endif
882
883 static void
884 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
885     int32_t dz, int32_t dt, int32_t buttons)
886 {
887         uint8_t buf[8];
888
889         if (1) {
890
891                 if (dx > 254)
892                         dx = 254;
893                 if (dx < -256)
894                         dx = -256;
895                 if (dy > 254)
896                         dy = 254;
897                 if (dy < -256)
898                         dy = -256;
899                 if (dz > 126)
900                         dz = 126;
901                 if (dz < -128)
902                         dz = -128;
903                 if (dt > 126)
904                         dt = 126;
905                 if (dt < -128)
906                         dt = -128;
907
908                 buf[0] = sc->sc_mode.syncmask[1];
909                 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
910                 buf[1] = dx >> 1;
911                 buf[2] = dy >> 1;
912                 buf[3] = dx - (dx >> 1);
913                 buf[4] = dy - (dy >> 1);
914
915                 if (sc->sc_mode.level == 1) {
916                         buf[5] = dz >> 1;
917                         buf[6] = dz - (dz >> 1);
918                         buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
919                 }
920                 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
921                     sc->sc_mode.packetsize, 1);
922
923         } else {
924                 DPRINTF("Buffer full, discarded packet\n");
925         }
926 }
927
928 #ifdef EVDEV_SUPPORT
929 static void
930 ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy,
931     int32_t dz, int32_t dt, int32_t buttons)
932 {
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);
945         }
946 }
947 #endif
948
949 static void
950 ums_reset_buf(struct ums_softc *sc)
951 {
952         /* reset read queue, must be called locked */
953         usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
954 }
955
956 #ifdef EVDEV_SUPPORT
957 static int
958 ums_ev_open(struct evdev_dev *evdev, void *ev_softc)
959 {
960         struct ums_softc *sc = (struct ums_softc *)ev_softc;
961
962         lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
963
964         sc->sc_evflags = UMS_EVDEV_OPENED;
965
966         if (sc->sc_fflags == 0) {
967                 ums_reset(sc);
968                 ums_start_rx(sc);
969         }
970
971         lockmgr(&sc->sc_lock, LK_RELEASE);
972
973         return (0);
974 }
975
976 static void
977 ums_ev_close(struct evdev_dev *evdev, void *ev_softc)
978 {
979         struct ums_softc *sc = (struct ums_softc *)ev_softc;
980
981         lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
982
983         sc->sc_evflags = 0;
984
985         if (sc->sc_fflags == 0)
986                 ums_stop_rx(sc);
987
988         lockmgr(&sc->sc_lock, LK_RELEASE);
989 }
990 #endif
991
992 static int
993 ums_fifo_open(struct usb_fifo *fifo, int fflags)
994 {
995         struct ums_softc *sc = usb_fifo_softc(fifo);
996
997         DPRINTFN(2, "\n");
998
999         /* check for duplicate open, should not happen */
1000         if (sc->sc_fflags & fflags)
1001                 return (EBUSY);
1002
1003         /* check for first open */
1004 #ifdef EVDEV_SUPPORT
1005         if (sc->sc_fflags == 0 && sc->sc_evflags == 0)
1006                 ums_reset(sc);
1007 #else
1008         if (sc->sc_fflags == 0)
1009                 ums_reset(sc);
1010 #endif
1011
1012         if (fflags & FREAD) {
1013                 /* allocate RX buffer */
1014                 if (usb_fifo_alloc_buffer(fifo,
1015                     UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1016                         return (ENOMEM);
1017                 }
1018         }
1019
1020         sc->sc_fflags |= fflags & (FREAD | FWRITE);
1021         return (0);
1022 }
1023
1024 static void
1025 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1026 {
1027         struct ums_softc *sc = usb_fifo_softc(fifo);
1028
1029         DPRINTFN(2, "\n");
1030
1031         if (fflags & FREAD)
1032                 usb_fifo_free_buffer(fifo);
1033
1034         sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1035 }
1036
1037 static int
1038 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1039 {
1040         struct ums_softc *sc = usb_fifo_softc(fifo);
1041         mousemode_t mode;
1042         int error = 0;
1043
1044         DPRINTFN(2, "\n");
1045
1046         lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
1047
1048         switch (cmd) {
1049         case MOUSE_GETHWINFO:
1050                 *(mousehw_t *)addr = sc->sc_hw;
1051                 break;
1052
1053         case MOUSE_GETMODE:
1054                 *(mousemode_t *)addr = sc->sc_mode;
1055                 break;
1056
1057         case MOUSE_SETMODE:
1058                 mode = *(mousemode_t *)addr;
1059
1060                 if (mode.level == -1) {
1061                         /* don't change the current setting */
1062                 } else if ((mode.level < 0) || (mode.level > 1)) {
1063                         error = EINVAL;
1064                         break;
1065                 } else {
1066                         sc->sc_mode.level = mode.level;
1067                 }
1068
1069                 /* store polling rate */
1070                 sc->sc_pollrate = mode.rate;
1071
1072                 if (sc->sc_mode.level == 0) {
1073                         if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1074                                 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1075                         else
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;
1084                         else
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;
1090                 }
1091                 ums_reset_buf(sc);
1092                 break;
1093
1094         case MOUSE_GETLEVEL:
1095                 *(int *)addr = sc->sc_mode.level;
1096                 break;
1097
1098         case MOUSE_SETLEVEL:
1099                 if (*(int *)addr < 0 || *(int *)addr > 1) {
1100                         error = EINVAL;
1101                         break;
1102                 }
1103                 sc->sc_mode.level = *(int *)addr;
1104
1105                 if (sc->sc_mode.level == 0) {
1106                         if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1107                                 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1108                         else
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;
1117                         else
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;
1123                 }
1124                 ums_reset_buf(sc);
1125                 break;
1126
1127         case MOUSE_GETSTATUS:{
1128                         mousestatus_t *status = (mousestatus_t *)addr;
1129
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; */
1137
1138                         if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1139                                 status->flags |= MOUSE_POSCHANGED;
1140                         }
1141                         if (status->button != status->obutton) {
1142                                 status->flags |= MOUSE_BUTTONSCHANGED;
1143                         }
1144                         break;
1145                 }
1146         default:
1147                 error = ENOTTY;
1148                 break;
1149         }
1150
1151         lockmgr(&sc->sc_lock, LK_RELEASE);
1152         return (error);
1153 }
1154
1155 #if 0 /* XXX */
1156 static int
1157 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1158 {
1159         struct ums_softc *sc = arg1;
1160         struct ums_info *info;
1161         struct sbuf *sb;
1162         int i, j, err, had_output;
1163
1164         sb = sbuf_new_auto();
1165         for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1166                 info = &sc->sc_info[i];
1167
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)
1173                         continue;
1174
1175                 if (had_output)
1176                         sbuf_printf(sb, "\n");
1177                 had_output = 1;
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);
1204
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);
1210                 }
1211         }
1212         sbuf_finish(sb);
1213         err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1214         sbuf_delete(sb);
1215
1216         return (err);
1217 }
1218 #endif
1219
1220 static devclass_t ums_devclass;
1221
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),
1226         DEVMETHOD_END
1227 };
1228
1229 static driver_t ums_driver = {
1230         .name = "ums",
1231         .methods = ums_methods,
1232         .size = sizeof(struct ums_softc),
1233 };
1234
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);
1239 #endif
1240 MODULE_VERSION(ums, 1);