]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/input/ums.c
MFC r212129
[FreeBSD/stable/8.git] / sys / dev / usb / 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  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 /*
42  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
43  */
44
45 #include <sys/stdint.h>
46 #include <sys/stddef.h>
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/types.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/bus.h>
53 #include <sys/linker_set.h>
54 #include <sys/module.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/sx.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
63 #include <sys/priv.h>
64 #include <sys/conf.h>
65 #include <sys/fcntl.h>
66 #include <sys/sbuf.h>
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbhid.h>
72 #include "usbdevs.h"
73
74 #define USB_DEBUG_VAR ums_debug
75 #include <dev/usb/usb_debug.h>
76
77 #include <dev/usb/quirk/usb_quirk.h>
78
79 #include <sys/ioccom.h>
80 #include <sys/filio.h>
81 #include <sys/tty.h>
82 #include <sys/mouse.h>
83
84 #ifdef USB_DEBUG
85 static int ums_debug = 0;
86
87 SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
88 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
89     &ums_debug, 0, "Debug level");
90 #endif
91
92 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
93 #define MOUSE_FLAGS (HIO_RELATIVE)
94
95 #define UMS_BUF_SIZE      8             /* bytes */
96 #define UMS_IFQ_MAXLEN   50             /* units */
97 #define UMS_BUTTON_MAX   31             /* exclusive, must be less than 32 */
98 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
99 #define UMS_INFO_MAX      2             /* maximum number of HID sets */
100
101 enum {
102         UMS_INTR_DT,
103         UMS_N_TRANSFER,
104 };
105
106 struct ums_info {
107         struct hid_location sc_loc_w;
108         struct hid_location sc_loc_x;
109         struct hid_location sc_loc_y;
110         struct hid_location sc_loc_z;
111         struct hid_location sc_loc_t;
112         struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
113
114         uint32_t sc_flags;
115 #define UMS_FLAG_X_AXIS     0x0001
116 #define UMS_FLAG_Y_AXIS     0x0002
117 #define UMS_FLAG_Z_AXIS     0x0004
118 #define UMS_FLAG_T_AXIS     0x0008
119 #define UMS_FLAG_SBU        0x0010      /* spurious button up events */
120 #define UMS_FLAG_REVZ       0x0020      /* Z-axis is reversed */
121 #define UMS_FLAG_W_AXIS     0x0040
122
123         uint8_t sc_iid_w;
124         uint8_t sc_iid_x;
125         uint8_t sc_iid_y;
126         uint8_t sc_iid_z;
127         uint8_t sc_iid_t;
128         uint8_t sc_iid_btn[UMS_BUTTON_MAX];
129         uint8_t sc_buttons;
130 };
131
132 struct ums_softc {
133         struct usb_fifo_sc sc_fifo;
134         struct mtx sc_mtx;
135         struct usb_callout sc_callout;
136         struct ums_info sc_info[UMS_INFO_MAX];
137
138         mousehw_t sc_hw;
139         mousemode_t sc_mode;
140         mousestatus_t sc_status;
141
142         struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
143
144         int sc_pollrate;
145
146         uint8_t sc_buttons;
147         uint8_t sc_iid;
148         uint8_t sc_temp[64];
149 };
150
151 static void ums_put_queue_timeout(void *__sc);
152
153 static usb_callback_t ums_intr_callback;
154
155 static device_probe_t ums_probe;
156 static device_attach_t ums_attach;
157 static device_detach_t ums_detach;
158
159 static usb_fifo_cmd_t ums_start_read;
160 static usb_fifo_cmd_t ums_stop_read;
161 static usb_fifo_open_t ums_open;
162 static usb_fifo_close_t ums_close;
163 static usb_fifo_ioctl_t ums_ioctl;
164
165 static void     ums_put_queue(struct ums_softc *, int32_t, int32_t,
166                     int32_t, int32_t, int32_t);
167 static int      ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
168
169 static struct usb_fifo_methods ums_fifo_methods = {
170         .f_open = &ums_open,
171         .f_close = &ums_close,
172         .f_ioctl = &ums_ioctl,
173         .f_start_read = &ums_start_read,
174         .f_stop_read = &ums_stop_read,
175         .basename[0] = "ums",
176 };
177
178 static void
179 ums_put_queue_timeout(void *__sc)
180 {
181         struct ums_softc *sc = __sc;
182
183         mtx_assert(&sc->sc_mtx, MA_OWNED);
184
185         ums_put_queue(sc, 0, 0, 0, 0, 0);
186 }
187
188 static void
189 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
190 {
191         struct ums_softc *sc = usbd_xfer_softc(xfer);
192         struct ums_info *info = &sc->sc_info[0];
193         struct usb_page_cache *pc;
194         uint8_t *buf = sc->sc_temp;
195         int32_t buttons = 0;
196         int32_t buttons_found = 0;
197         int32_t dw = 0;
198         int32_t dx = 0;
199         int32_t dy = 0;
200         int32_t dz = 0;
201         int32_t dt = 0;
202         uint8_t i;
203         uint8_t id;
204         int len;
205
206         usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
207
208         switch (USB_GET_STATE(xfer)) {
209         case USB_ST_TRANSFERRED:
210                 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
211
212                 if (len > sizeof(sc->sc_temp)) {
213                         DPRINTFN(6, "truncating large packet to %zu bytes\n",
214                             sizeof(sc->sc_temp));
215                         len = sizeof(sc->sc_temp);
216                 }
217                 if (len == 0)
218                         goto tr_setup;
219
220                 pc = usbd_xfer_get_frame(xfer, 0);
221                 usbd_copy_out(pc, 0, buf, len);
222
223                 DPRINTFN(6, "data = %02x %02x %02x %02x "
224                     "%02x %02x %02x %02x\n",
225                     (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
226                     (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
227                     (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
228                     (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
229
230                 if (sc->sc_iid) {
231                         id = *buf;
232
233                         len--;
234                         buf++;
235
236                 } else {
237                         id = 0;
238                         if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
239                                 if ((*buf == 0x14) || (*buf == 0x15)) {
240                                         goto tr_setup;
241                                 }
242                         }
243                 }
244
245         repeat:
246                 if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
247                     (id == info->sc_iid_w))
248                         dw += hid_get_data(buf, len, &info->sc_loc_w);
249
250                 if ((info->sc_flags & UMS_FLAG_X_AXIS) && 
251                     (id == info->sc_iid_x))
252                         dx += hid_get_data(buf, len, &info->sc_loc_x);
253
254                 if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
255                     (id == info->sc_iid_y))
256                         dy = -hid_get_data(buf, len, &info->sc_loc_y);
257
258                 if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
259                     (id == info->sc_iid_z)) {
260                         int32_t temp;
261                         temp = hid_get_data(buf, len, &info->sc_loc_z);
262                         if (info->sc_flags & UMS_FLAG_REVZ)
263                                 temp = -temp;
264                         dz -= temp;
265                 }
266
267                 if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
268                     (id == info->sc_iid_t))
269                         dt -= hid_get_data(buf, len, &info->sc_loc_t);
270
271                 for (i = 0; i < info->sc_buttons; i++) {
272                         uint32_t mask;
273                         mask = 1UL << UMS_BUT(i);
274                         /* check for correct button ID */
275                         if (id != info->sc_iid_btn[i])
276                                 continue;
277                         /* check for button pressed */
278                         if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
279                                 buttons |= mask;
280                         /* register button mask */
281                         buttons_found |= mask;
282                 }
283
284                 if (++info != &sc->sc_info[UMS_INFO_MAX])
285                         goto repeat;
286
287                 /* keep old button value(s) for non-detected buttons */
288                 buttons |= sc->sc_status.button & ~buttons_found;
289
290                 if (dx || dy || dz || dt || dw ||
291                     (buttons != sc->sc_status.button)) {
292
293                         DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
294                             dx, dy, dz, dt, dw, buttons);
295
296                         /* translate T-axis into button presses until further */
297                         if (dt > 0)
298                                 buttons |= 1UL << 3;
299                         else if (dt < 0)
300                                 buttons |= 1UL << 4;
301
302                         sc->sc_status.button = buttons;
303                         sc->sc_status.dx += dx;
304                         sc->sc_status.dy += dy;
305                         sc->sc_status.dz += dz;
306                         /*
307                          * sc->sc_status.dt += dt;
308                          * no way to export this yet
309                          */
310
311                         /*
312                          * The Qtronix keyboard has a built in PS/2
313                          * port for a mouse.  The firmware once in a
314                          * while posts a spurious button up
315                          * event. This event we ignore by doing a
316                          * timeout for 50 msecs.  If we receive
317                          * dx=dy=dz=buttons=0 before we add the event
318                          * to the queue.  In any other case we delete
319                          * the timeout event.
320                          */
321                         if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
322                             (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
323                             (dw == 0) && (buttons == 0)) {
324
325                                 usb_callout_reset(&sc->sc_callout, hz / 20,
326                                     &ums_put_queue_timeout, sc);
327                         } else {
328
329                                 usb_callout_stop(&sc->sc_callout);
330
331                                 ums_put_queue(sc, dx, dy, dz, dt, buttons);
332                         }
333                 }
334         case USB_ST_SETUP:
335 tr_setup:
336                 /* check if we can put more data into the FIFO */
337                 if (usb_fifo_put_bytes_max(
338                     sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
339                         usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
340                         usbd_transfer_submit(xfer);
341                 }
342                 break;
343
344         default:                        /* Error */
345                 if (error != USB_ERR_CANCELLED) {
346                         /* try clear stall first */
347                         usbd_xfer_set_stall(xfer);
348                         goto tr_setup;
349                 }
350                 break;
351         }
352 }
353
354 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
355
356         [UMS_INTR_DT] = {
357                 .type = UE_INTERRUPT,
358                 .endpoint = UE_ADDR_ANY,
359                 .direction = UE_DIR_IN,
360                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
361                 .bufsize = 0,   /* use wMaxPacketSize */
362                 .callback = &ums_intr_callback,
363         },
364 };
365
366 static int
367 ums_probe(device_t dev)
368 {
369         struct usb_attach_arg *uaa = device_get_ivars(dev);
370         void *d_ptr;
371         int error;
372         uint16_t d_len;
373
374         DPRINTFN(11, "\n");
375
376         if (uaa->usb_mode != USB_MODE_HOST)
377                 return (ENXIO);
378
379         if (uaa->info.bInterfaceClass != UICLASS_HID)
380                 return (ENXIO);
381
382         if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
383             (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
384                 return (BUS_PROBE_GENERIC);
385
386         error = usbd_req_get_hid_desc(uaa->device, NULL,
387             &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
388
389         if (error)
390                 return (ENXIO);
391
392         if (hid_is_collection(d_ptr, d_len,
393             HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
394                 error = BUS_PROBE_GENERIC;
395         else
396                 error = ENXIO;
397
398         free(d_ptr, M_TEMP);
399         return (error);
400 }
401
402 static void
403 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
404     uint16_t len, uint8_t index)
405 {
406         struct ums_info *info = &sc->sc_info[index];
407         uint32_t flags;
408         uint8_t i;
409         uint8_t j;
410
411         if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
412             hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
413
414                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
415                         info->sc_flags |= UMS_FLAG_X_AXIS;
416                 }
417         }
418         if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
419             hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
420
421                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
422                         info->sc_flags |= UMS_FLAG_Y_AXIS;
423                 }
424         }
425         /* Try the wheel first as the Z activator since it's tradition. */
426         if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
427             HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
428             &info->sc_iid_z) ||
429             hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
430             HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
431             &info->sc_iid_z)) {
432                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
433                         info->sc_flags |= UMS_FLAG_Z_AXIS;
434                 }
435                 /*
436                  * We might have both a wheel and Z direction, if so put
437                  * put the Z on the W coordinate.
438                  */
439                 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
440                     HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
441                     &info->sc_iid_w)) {
442
443                         if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
444                                 info->sc_flags |= UMS_FLAG_W_AXIS;
445                         }
446                 }
447         } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
448             HUG_Z), hid_input, index, &info->sc_loc_z, &flags, 
449             &info->sc_iid_z)) {
450
451                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
452                         info->sc_flags |= UMS_FLAG_Z_AXIS;
453                 }
454         }
455         /*
456          * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
457          * using 0x0048, which is HUG_TWHEEL, and seems to expect you
458          * to know that the byte after the wheel is the tilt axis.
459          * There are no other HID axis descriptors other than X,Y and
460          * TWHEEL
461          */
462         if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
463             HUG_TWHEEL), hid_input, index, &info->sc_loc_t, 
464             &flags, &info->sc_iid_t)) {
465
466                 info->sc_loc_t.pos += 8;
467
468                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
469                         info->sc_flags |= UMS_FLAG_T_AXIS;
470                 }
471         } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
472                 HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
473                 &flags, &info->sc_iid_t)) {
474
475                 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
476                         info->sc_flags |= UMS_FLAG_T_AXIS;
477         }
478         /* figure out the number of buttons */
479
480         for (i = 0; i < UMS_BUTTON_MAX; i++) {
481                 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
482                     hid_input, index, &info->sc_loc_btn[i], NULL, 
483                     &info->sc_iid_btn[i])) {
484                         break;
485                 }
486         }
487
488         /* detect other buttons */
489
490         for (j = 0; (i < UMS_BUTTONS_MAX) && (j < 2); i++, j++) {
491                 if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
492                     hid_input, index, &info->sc_loc_btn[i], NULL, 
493                     &info->sc_iid_btn[i])) {
494                         break;
495                 }
496         }
497
498         info->sc_buttons = i;
499
500         if (i > sc->sc_buttons)
501                 sc->sc_buttons = i;
502
503         if (info->sc_flags == 0)
504                 return;
505
506         /* announce information about the mouse */
507         device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
508             (info->sc_buttons),
509             (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
510             (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
511             (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
512             (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
513             (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
514             info->sc_iid_x);
515 }
516
517 static int
518 ums_attach(device_t dev)
519 {
520         struct usb_attach_arg *uaa = device_get_ivars(dev);
521         struct ums_softc *sc = device_get_softc(dev);
522         struct ums_info *info;
523         void *d_ptr = NULL;
524         int isize;
525         int err;
526         uint16_t d_len;
527         uint8_t i;
528 #ifdef USB_DEBUG
529         uint8_t j;
530 #endif
531
532         DPRINTFN(11, "sc=%p\n", sc);
533
534         device_set_usb_desc(dev);
535
536         mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
537
538         usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
539
540         /*
541          * Force the report (non-boot) protocol.
542          *
543          * Mice without boot protocol support may choose not to implement
544          * Set_Protocol at all; Ignore any error.
545          */
546         err = usbd_req_set_protocol(uaa->device, NULL,
547             uaa->info.bIfaceIndex, 1);
548
549         err = usbd_transfer_setup(uaa->device,
550             &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
551             UMS_N_TRANSFER, sc, &sc->sc_mtx);
552
553         if (err) {
554                 DPRINTF("error=%s\n", usbd_errstr(err));
555                 goto detach;
556         }
557
558         /* Get HID descriptor */
559         err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
560             &d_len, M_TEMP, uaa->info.bIfaceIndex);
561
562         if (err) {
563                 device_printf(dev, "error reading report description\n");
564                 goto detach;
565         }
566
567         isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
568
569         /*
570          * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
571          * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
572          * all of its other button positions are all off. It also reports that
573          * it has two addional buttons and a tilt wheel.
574          */
575         if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
576
577                 sc->sc_iid = 0;
578
579                 info = &sc->sc_info[0];
580                 info->sc_flags = (UMS_FLAG_X_AXIS |
581                     UMS_FLAG_Y_AXIS |
582                     UMS_FLAG_Z_AXIS |
583                     UMS_FLAG_SBU);
584                 info->sc_buttons = 3;
585                 isize = 5;
586                 /* 1st byte of descriptor report contains garbage */
587                 info->sc_loc_x.pos = 16;
588                 info->sc_loc_x.size = 8;
589                 info->sc_loc_y.pos = 24;
590                 info->sc_loc_y.size = 8;
591                 info->sc_loc_z.pos = 32;
592                 info->sc_loc_z.size = 8;
593                 info->sc_loc_btn[0].pos = 8;
594                 info->sc_loc_btn[0].size = 1;
595                 info->sc_loc_btn[1].pos = 9;
596                 info->sc_loc_btn[1].size = 1;
597                 info->sc_loc_btn[2].pos = 10;
598                 info->sc_loc_btn[2].size = 1;
599
600                 /* Announce device */
601                 device_printf(dev, "3 buttons and [XYZ] "
602                     "coordinates ID=0\n");
603
604         } else {
605                 /* Search the HID descriptor and announce device */
606                 for (i = 0; i < UMS_INFO_MAX; i++) {
607                         ums_hid_parse(sc, dev, d_ptr, d_len, i);
608                 }
609         }
610
611         if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
612                 info = &sc->sc_info[0];
613                 /* Some wheels need the Z axis reversed. */
614                 info->sc_flags |= UMS_FLAG_REVZ;
615         }
616         if (isize > usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
617                 DPRINTF("WARNING: report size, %d bytes, is larger "
618                     "than interrupt size, %d bytes!\n", isize,
619                     usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
620         }
621         free(d_ptr, M_TEMP);
622         d_ptr = NULL;
623
624 #ifdef USB_DEBUG
625         for (j = 0; j < UMS_INFO_MAX; j++) {
626                 info = &sc->sc_info[j];
627
628                 DPRINTF("sc=%p, index=%d\n", sc, j);
629                 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
630                     info->sc_loc_x.size, info->sc_iid_x);
631                 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
632                     info->sc_loc_y.size, info->sc_iid_y);
633                 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
634                     info->sc_loc_z.size, info->sc_iid_z);
635                 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
636                     info->sc_loc_t.size, info->sc_iid_t);
637                 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
638                     info->sc_loc_w.size, info->sc_iid_w);
639
640                 for (i = 0; i < info->sc_buttons; i++) {
641                         DPRINTF("B%d\t%d/%d id=%d\n",
642                             i + 1, info->sc_loc_btn[i].pos,
643                             info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
644                 }
645         }
646         DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
647 #endif
648
649         if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
650                 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
651         else
652                 sc->sc_hw.buttons = sc->sc_buttons;
653
654         sc->sc_hw.iftype = MOUSE_IF_USB;
655         sc->sc_hw.type = MOUSE_MOUSE;
656         sc->sc_hw.model = MOUSE_MODEL_GENERIC;
657         sc->sc_hw.hwid = 0;
658
659         sc->sc_mode.protocol = MOUSE_PROTO_MSC;
660         sc->sc_mode.rate = -1;
661         sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
662         sc->sc_mode.accelfactor = 0;
663         sc->sc_mode.level = 0;
664         sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
665         sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
666         sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
667
668         err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
669             &ums_fifo_methods, &sc->sc_fifo,
670             device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
671             UID_ROOT, GID_OPERATOR, 0644);
672         if (err) {
673                 goto detach;
674         }
675         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
676             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
677             OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD,
678             sc, 0, ums_sysctl_handler_parseinfo,
679             "", "Dump UMS report parsing information");
680
681         return (0);
682
683 detach:
684         if (d_ptr) {
685                 free(d_ptr, M_TEMP);
686         }
687         ums_detach(dev);
688         return (ENOMEM);
689 }
690
691 static int
692 ums_detach(device_t self)
693 {
694         struct ums_softc *sc = device_get_softc(self);
695
696         DPRINTF("sc=%p\n", sc);
697
698         usb_fifo_detach(&sc->sc_fifo);
699
700         usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
701
702         usb_callout_drain(&sc->sc_callout);
703
704         mtx_destroy(&sc->sc_mtx);
705
706         return (0);
707 }
708
709 static void
710 ums_start_read(struct usb_fifo *fifo)
711 {
712         struct ums_softc *sc = usb_fifo_softc(fifo);
713         int rate;
714
715         /* Check if we should override the default polling interval */
716         rate = sc->sc_pollrate;
717         /* Range check rate */
718         if (rate > 1000)
719                 rate = 1000;
720         /* Check for set rate */
721         if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
722                 DPRINTF("Setting pollrate = %d\n", rate);
723                 /* Stop current transfer, if any */
724                 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
725                 /* Set new interval */
726                 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
727                 /* Only set pollrate once */
728                 sc->sc_pollrate = 0;
729         }
730
731         usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
732 }
733
734 static void
735 ums_stop_read(struct usb_fifo *fifo)
736 {
737         struct ums_softc *sc = usb_fifo_softc(fifo);
738
739         usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
740         usb_callout_stop(&sc->sc_callout);
741 }
742
743
744 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
745      (MOUSE_MSC_PACKETSIZE != 5))
746 #error "Software assumptions are not met. Please update code."
747 #endif
748
749 static void
750 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
751     int32_t dz, int32_t dt, int32_t buttons)
752 {
753         uint8_t buf[8];
754
755         if (1) {
756
757                 if (dx > 254)
758                         dx = 254;
759                 if (dx < -256)
760                         dx = -256;
761                 if (dy > 254)
762                         dy = 254;
763                 if (dy < -256)
764                         dy = -256;
765                 if (dz > 126)
766                         dz = 126;
767                 if (dz < -128)
768                         dz = -128;
769                 if (dt > 126)
770                         dt = 126;
771                 if (dt < -128)
772                         dt = -128;
773
774                 buf[0] = sc->sc_mode.syncmask[1];
775                 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
776                 buf[1] = dx >> 1;
777                 buf[2] = dy >> 1;
778                 buf[3] = dx - (dx >> 1);
779                 buf[4] = dy - (dy >> 1);
780
781                 if (sc->sc_mode.level == 1) {
782                         buf[5] = dz >> 1;
783                         buf[6] = dz - (dz >> 1);
784                         buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
785                 }
786                 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
787                     sc->sc_mode.packetsize, 1);
788
789         } else {
790                 DPRINTF("Buffer full, discarded packet\n");
791         }
792 }
793
794 static void
795 ums_reset_buf(struct ums_softc *sc)
796 {
797         /* reset read queue */
798         usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
799 }
800
801 static int
802 ums_open(struct usb_fifo *fifo, int fflags)
803 {
804         struct ums_softc *sc = usb_fifo_softc(fifo);
805
806         DPRINTFN(2, "\n");
807
808         if (fflags & FREAD) {
809
810                 /* reset status */
811
812                 sc->sc_status.flags = 0;
813                 sc->sc_status.button = 0;
814                 sc->sc_status.obutton = 0;
815                 sc->sc_status.dx = 0;
816                 sc->sc_status.dy = 0;
817                 sc->sc_status.dz = 0;
818                 /* sc->sc_status.dt = 0; */
819
820                 if (usb_fifo_alloc_buffer(fifo,
821                     UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
822                         return (ENOMEM);
823                 }
824         }
825         return (0);
826 }
827
828 static void
829 ums_close(struct usb_fifo *fifo, int fflags)
830 {
831         if (fflags & FREAD) {
832                 usb_fifo_free_buffer(fifo);
833         }
834 }
835
836 static int
837 ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
838 {
839         struct ums_softc *sc = usb_fifo_softc(fifo);
840         mousemode_t mode;
841         int error = 0;
842
843         DPRINTFN(2, "\n");
844
845         mtx_lock(&sc->sc_mtx);
846
847         switch (cmd) {
848         case MOUSE_GETHWINFO:
849                 *(mousehw_t *)addr = sc->sc_hw;
850                 break;
851
852         case MOUSE_GETMODE:
853                 *(mousemode_t *)addr = sc->sc_mode;
854                 break;
855
856         case MOUSE_SETMODE:
857                 mode = *(mousemode_t *)addr;
858
859                 if (mode.level == -1) {
860                         /* don't change the current setting */
861                 } else if ((mode.level < 0) || (mode.level > 1)) {
862                         error = EINVAL;
863                         goto done;
864                 } else {
865                         sc->sc_mode.level = mode.level;
866                 }
867
868                 /* store polling rate */
869                 sc->sc_pollrate = mode.rate;
870
871                 if (sc->sc_mode.level == 0) {
872                         if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
873                                 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
874                         else
875                                 sc->sc_hw.buttons = sc->sc_buttons;
876                         sc->sc_mode.protocol = MOUSE_PROTO_MSC;
877                         sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
878                         sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
879                         sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
880                 } else if (sc->sc_mode.level == 1) {
881                         if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
882                                 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
883                         else
884                                 sc->sc_hw.buttons = sc->sc_buttons;
885                         sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
886                         sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
887                         sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
888                         sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
889                 }
890                 ums_reset_buf(sc);
891                 break;
892
893         case MOUSE_GETLEVEL:
894                 *(int *)addr = sc->sc_mode.level;
895                 break;
896
897         case MOUSE_SETLEVEL:
898                 if (*(int *)addr < 0 || *(int *)addr > 1) {
899                         error = EINVAL;
900                         goto done;
901                 }
902                 sc->sc_mode.level = *(int *)addr;
903
904                 if (sc->sc_mode.level == 0) {
905                         if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
906                                 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
907                         else
908                                 sc->sc_hw.buttons = sc->sc_buttons;
909                         sc->sc_mode.protocol = MOUSE_PROTO_MSC;
910                         sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
911                         sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
912                         sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
913                 } else if (sc->sc_mode.level == 1) {
914                         if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
915                                 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
916                         else
917                                 sc->sc_hw.buttons = sc->sc_buttons;
918                         sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
919                         sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
920                         sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
921                         sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
922                 }
923                 ums_reset_buf(sc);
924                 break;
925
926         case MOUSE_GETSTATUS:{
927                         mousestatus_t *status = (mousestatus_t *)addr;
928
929                         *status = sc->sc_status;
930                         sc->sc_status.obutton = sc->sc_status.button;
931                         sc->sc_status.button = 0;
932                         sc->sc_status.dx = 0;
933                         sc->sc_status.dy = 0;
934                         sc->sc_status.dz = 0;
935                         /* sc->sc_status.dt = 0; */
936
937                         if (status->dx || status->dy || status->dz /* || status->dt */ ) {
938                                 status->flags |= MOUSE_POSCHANGED;
939                         }
940                         if (status->button != status->obutton) {
941                                 status->flags |= MOUSE_BUTTONSCHANGED;
942                         }
943                         break;
944                 }
945         default:
946                 error = ENOTTY;
947         }
948
949 done:
950         mtx_unlock(&sc->sc_mtx);
951         return (error);
952 }
953
954 static int
955 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
956 {
957         struct ums_softc *sc = arg1;
958         struct ums_info *info;
959         struct sbuf *sb;
960         int i, j, err;
961
962         sb = sbuf_new_auto();
963         for (i = 0; i < UMS_INFO_MAX; i++) {
964                 info = &sc->sc_info[i];
965
966                 /* Don't emit empty info */
967                 if ((info->sc_flags &
968                     (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
969                      UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
970                     info->sc_buttons == 0)
971                         continue;
972
973                 sbuf_printf(sb, "i%d:", i + 1);
974                 if (info->sc_flags & UMS_FLAG_X_AXIS)
975                         sbuf_printf(sb, " X:r%d, p%d, s%d;",
976                             (int)info->sc_iid_x,
977                             (int)info->sc_loc_x.pos,
978                             (int)info->sc_loc_x.size);
979                 if (info->sc_flags & UMS_FLAG_Y_AXIS)
980                         sbuf_printf(sb, " Y:r%d, p%d, s%d;",
981                             (int)info->sc_iid_y,
982                             (int)info->sc_loc_y.pos,
983                             (int)info->sc_loc_y.size);
984                 if (info->sc_flags & UMS_FLAG_Z_AXIS)
985                         sbuf_printf(sb, " Z:r%d, p%d, s%d;",
986                             (int)info->sc_iid_z,
987                             (int)info->sc_loc_z.pos,
988                             (int)info->sc_loc_z.size);
989                 if (info->sc_flags & UMS_FLAG_T_AXIS)
990                         sbuf_printf(sb, " T:r%d, p%d, s%d;",
991                             (int)info->sc_iid_t,
992                             (int)info->sc_loc_t.pos,
993                             (int)info->sc_loc_t.size);
994                 if (info->sc_flags & UMS_FLAG_W_AXIS)
995                         sbuf_printf(sb, " W:r%d, p%d, s%d;",
996                             (int)info->sc_iid_w,
997                             (int)info->sc_loc_w.pos,
998                             (int)info->sc_loc_w.size);
999
1000                 for (j = 0; j < info->sc_buttons; j++) {
1001                         sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1002                             (int)info->sc_iid_btn[j],
1003                             (int)info->sc_loc_btn[j].pos,
1004                             (int)info->sc_loc_btn[j].size);
1005                 }
1006                 sbuf_printf(sb, "\n");
1007         }
1008         sbuf_finish(sb);
1009         err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1010         sbuf_delete(sb);
1011
1012         return (err);
1013 }
1014
1015 static devclass_t ums_devclass;
1016
1017 static device_method_t ums_methods[] = {
1018         DEVMETHOD(device_probe, ums_probe),
1019         DEVMETHOD(device_attach, ums_attach),
1020         DEVMETHOD(device_detach, ums_detach),
1021         {0, 0}
1022 };
1023
1024 static driver_t ums_driver = {
1025         .name = "ums",
1026         .methods = ums_methods,
1027         .size = sizeof(struct ums_softc),
1028 };
1029
1030 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
1031 MODULE_DEPEND(ums, usb, 1, 1, 1);
1032 MODULE_VERSION(ums, 1);