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