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