Deleted Added
full compact
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: head/sys/dev/usb/input/ums.c 192499 2009-05-21 00:04:17Z thompsa $");
39__FBSDID("$FreeBSD: head/sys/dev/usb/input/ums.c 192502 2009-05-21 01:48:42Z thompsa $");
40
41/*
42 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
43 */
44
45#include "usbdevs.h"
46#include <dev/usb/usb.h>
47#include <dev/usb/usb_mfunc.h>
48#include <dev/usb/usb_error.h>
49#include <dev/usb/usbhid.h>
50
51#define USB_DEBUG_VAR ums_debug
52
53#include <dev/usb/usb_core.h>
54#include <dev/usb/usb_util.h>
55#include <dev/usb/usb_debug.h>
56#include <dev/usb/usb_busdma.h>
57#include <dev/usb/usb_process.h>
58#include <dev/usb/usb_transfer.h>
59#include <dev/usb/usb_request.h>
60#include <dev/usb/usb_dynamic.h>
61#include <dev/usb/usb_mbuf.h>
62#include <dev/usb/usb_dev.h>
63#include <dev/usb/usb_hid.h>
64
65#include <dev/usb/quirk/usb_quirk.h>
66
67#include <sys/ioccom.h>
68#include <sys/filio.h>
69#include <sys/tty.h>
70#include <sys/mouse.h>
71
72#if USB_DEBUG
73static int ums_debug = 0;
74
75SYSCTL_NODE(_hw_usb2, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
76SYSCTL_INT(_hw_usb2_ums, OID_AUTO, debug, CTLFLAG_RW,
75SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
76SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
77 &ums_debug, 0, "Debug level");
78#endif
79
80#define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
81#define MOUSE_FLAGS (HIO_RELATIVE)
82
83#define UMS_BUF_SIZE 8 /* bytes */
84#define UMS_IFQ_MAXLEN 50 /* units */
85#define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */
86#define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
87#define UMS_INFO_MAX 2 /* maximum number of HID sets */
88
89enum {
90 UMS_INTR_DT,
91 UMS_N_TRANSFER,
92};
93
94struct ums_info {
95 struct hid_location sc_loc_w;
96 struct hid_location sc_loc_x;
97 struct hid_location sc_loc_y;
98 struct hid_location sc_loc_z;
99 struct hid_location sc_loc_t;
100 struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
101
102 uint32_t sc_flags;
103#define UMS_FLAG_X_AXIS 0x0001
104#define UMS_FLAG_Y_AXIS 0x0002
105#define UMS_FLAG_Z_AXIS 0x0004
106#define UMS_FLAG_T_AXIS 0x0008
107#define UMS_FLAG_SBU 0x0010 /* spurious button up events */
108#define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */
109#define UMS_FLAG_W_AXIS 0x0040
110
111 uint8_t sc_iid_w;
112 uint8_t sc_iid_x;
113 uint8_t sc_iid_y;
114 uint8_t sc_iid_z;
115 uint8_t sc_iid_t;
116 uint8_t sc_iid_btn[UMS_BUTTON_MAX];
117 uint8_t sc_buttons;
118};
119
120struct ums_softc {
121 struct usb2_fifo_sc sc_fifo;
122 struct mtx sc_mtx;
123 struct usb2_callout sc_callout;
124 struct ums_info sc_info[UMS_INFO_MAX];
125
126 mousehw_t sc_hw;
127 mousemode_t sc_mode;
128 mousestatus_t sc_status;
129
130 struct usb2_xfer *sc_xfer[UMS_N_TRANSFER];
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 usb2_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 usb2_fifo_cmd_t ums_start_read;
146static usb2_fifo_cmd_t ums_stop_read;
147static usb2_fifo_open_t ums_open;
148static usb2_fifo_close_t ums_close;
149static usb2_fifo_ioctl_t ums_ioctl;
150
151static void ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, int32_t dz, int32_t dt, int32_t buttons);
152
153static struct usb2_fifo_methods ums_fifo_methods = {
154 .f_open = &ums_open,
155 .f_close = &ums_close,
156 .f_ioctl = &ums_ioctl,
157 .f_start_read = &ums_start_read,
158 .f_stop_read = &ums_stop_read,
159 .basename[0] = "ums",
160};
161
162static void
163ums_put_queue_timeout(void *__sc)
164{
165 struct ums_softc *sc = __sc;
166
167 mtx_assert(&sc->sc_mtx, MA_OWNED);
168
169 ums_put_queue(sc, 0, 0, 0, 0, 0);
170}
171
172static void
173ums_intr_callback(struct usb2_xfer *xfer)
174{
175 struct ums_softc *sc = xfer->priv_sc;
176 struct ums_info *info = &sc->sc_info[0];
177 uint8_t *buf = sc->sc_temp;
178 uint16_t len = xfer->actlen;
179 int32_t buttons = 0;
180 int32_t dw = 0;
181 int32_t dx = 0;
182 int32_t dy = 0;
183 int32_t dz = 0;
184 int32_t dt = 0;
185 uint8_t i;
186 uint8_t id;
187
188 switch (USB_GET_STATE(xfer)) {
189 case USB_ST_TRANSFERRED:
190 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
191
192 if (len > sizeof(sc->sc_temp)) {
193 DPRINTFN(6, "truncating large packet to %zu bytes\n",
194 sizeof(sc->sc_temp));
195 len = sizeof(sc->sc_temp);
196 }
197 if (len == 0)
198 goto tr_setup;
199
200 usb2_copy_out(xfer->frbuffers, 0, buf, len);
201
202 DPRINTFN(6, "data = %02x %02x %02x %02x "
203 "%02x %02x %02x %02x\n",
204 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
205 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
206 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
207 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
208
209 if (sc->sc_iid) {
210 id = *buf;
211
212 len--;
213 buf++;
214
215 } else {
216 id = 0;
217 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
218 if ((*buf == 0x14) || (*buf == 0x15)) {
219 goto tr_setup;
220 }
221 }
222 }
223
224 repeat:
225 if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
226 (id == info->sc_iid_w))
227 dw += hid_get_data(buf, len, &info->sc_loc_w);
228
229 if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
230 (id == info->sc_iid_x))
231 dx += hid_get_data(buf, len, &info->sc_loc_x);
232
233 if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
234 (id == info->sc_iid_y))
235 dy = -hid_get_data(buf, len, &info->sc_loc_y);
236
237 if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
238 (id == info->sc_iid_z)) {
239 int32_t temp;
240 temp = hid_get_data(buf, len, &info->sc_loc_z);
241 if (info->sc_flags & UMS_FLAG_REVZ)
242 temp = -temp;
243 dz -= temp;
244 }
245
246 if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
247 (id == info->sc_iid_t))
248 dt -= hid_get_data(buf, len, &info->sc_loc_t);
249
250 for (i = 0; i < info->sc_buttons; i++) {
251 if (id != info->sc_iid_btn[i])
252 continue;
253 if (hid_get_data(buf, len, &info->sc_loc_btn[i])) {
254 buttons |= (1 << UMS_BUT(i));
255 }
256 }
257
258 if (++info != &sc->sc_info[UMS_INFO_MAX])
259 goto repeat;
260
261 if (dx || dy || dz || dt || dw ||
262 (buttons != sc->sc_status.button)) {
263
264 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
265 dx, dy, dz, dt, dw, buttons);
266
267 sc->sc_status.button = buttons;
268 sc->sc_status.dx += dx;
269 sc->sc_status.dy += dy;
270 sc->sc_status.dz += dz;
271 /*
272 * sc->sc_status.dt += dt;
273 * no way to export this yet
274 */
275
276 /*
277 * The Qtronix keyboard has a built in PS/2
278 * port for a mouse. The firmware once in a
279 * while posts a spurious button up
280 * event. This event we ignore by doing a
281 * timeout for 50 msecs. If we receive
282 * dx=dy=dz=buttons=0 before we add the event
283 * to the queue. In any other case we delete
284 * the timeout event.
285 */
286 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
287 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
288 (dw == 0) && (buttons == 0)) {
289
290 usb2_callout_reset(&sc->sc_callout, hz / 20,
291 &ums_put_queue_timeout, sc);
292 } else {
293
294 usb2_callout_stop(&sc->sc_callout);
295
296 ums_put_queue(sc, dx, dy, dz, dt, buttons);
297 }
298 }
299 case USB_ST_SETUP:
300tr_setup:
301 /* check if we can put more data into the FIFO */
302 if (usb2_fifo_put_bytes_max(
303 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
304 xfer->frlengths[0] = xfer->max_data_length;
305 usb2_start_hardware(xfer);
306 }
307 break;
308
309 default: /* Error */
310 if (xfer->error != USB_ERR_CANCELLED) {
311 /* try clear stall first */
312 xfer->flags.stall_pipe = 1;
313 goto tr_setup;
314 }
315 break;
316 }
317}
318
319static const struct usb2_config ums_config[UMS_N_TRANSFER] = {
320
321 [UMS_INTR_DT] = {
322 .type = UE_INTERRUPT,
323 .endpoint = UE_ADDR_ANY,
324 .direction = UE_DIR_IN,
325 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
326 .bufsize = 0, /* use wMaxPacketSize */
327 .callback = &ums_intr_callback,
328 },
329};
330
331static int
332ums_probe(device_t dev)
333{
334 struct usb2_attach_arg *uaa = device_get_ivars(dev);
335 struct usb2_interface_descriptor *id;
336 void *d_ptr;
337 int error;
338 uint16_t d_len;
339
340 DPRINTFN(11, "\n");
341
342 if (uaa->usb_mode != USB_MODE_HOST)
343 return (ENXIO);
344
345 id = usb2_get_interface_descriptor(uaa->iface);
346
347 if ((id == NULL) ||
348 (id->bInterfaceClass != UICLASS_HID))
349 return (ENXIO);
350
351 error = usb2_req_get_hid_desc(uaa->device, NULL,
352 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
353
354 if (error)
355 return (ENXIO);
356
357 if (hid_is_collection(d_ptr, d_len,
358 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
359 error = 0;
360 else if ((id->bInterfaceSubClass == UISUBCLASS_BOOT) &&
361 (id->bInterfaceProtocol == UIPROTO_MOUSE))
362 error = 0;
363 else
364 error = ENXIO;
365
366 free(d_ptr, M_TEMP);
367 return (error);
368}
369
370static void
371ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
372 uint16_t len, uint8_t index)
373{
374 struct ums_info *info = &sc->sc_info[index];
375 uint32_t flags;
376 uint8_t i;
377
378 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
379 hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
380
381 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
382 info->sc_flags |= UMS_FLAG_X_AXIS;
383 }
384 }
385 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
386 hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
387
388 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
389 info->sc_flags |= UMS_FLAG_Y_AXIS;
390 }
391 }
392 /* Try the wheel first as the Z activator since it's tradition. */
393 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
394 HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
395 &info->sc_iid_z) ||
396 hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
397 HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
398 &info->sc_iid_z)) {
399 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
400 info->sc_flags |= UMS_FLAG_Z_AXIS;
401 }
402 /*
403 * We might have both a wheel and Z direction, if so put
404 * put the Z on the W coordinate.
405 */
406 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
407 HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
408 &info->sc_iid_w)) {
409
410 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
411 info->sc_flags |= UMS_FLAG_W_AXIS;
412 }
413 }
414 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
415 HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
416 &info->sc_iid_z)) {
417
418 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
419 info->sc_flags |= UMS_FLAG_Z_AXIS;
420 }
421 }
422 /*
423 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
424 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
425 * to know that the byte after the wheel is the tilt axis.
426 * There are no other HID axis descriptors other than X,Y and
427 * TWHEEL
428 */
429 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
430 HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
431 &flags, &info->sc_iid_t)) {
432
433 info->sc_loc_t.pos += 8;
434
435 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
436 info->sc_flags |= UMS_FLAG_T_AXIS;
437 }
438 }
439 /* figure out the number of buttons */
440
441 for (i = 0; i < UMS_BUTTON_MAX; i++) {
442 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
443 hid_input, index, &info->sc_loc_btn[i], NULL,
444 &info->sc_iid_btn[i])) {
445 break;
446 }
447 }
448 info->sc_buttons = i;
449
450 if (i > sc->sc_buttons)
451 sc->sc_buttons = i;
452
453 if (info->sc_flags == 0)
454 return;
455
456 /* announce information about the mouse */
457 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
458 (info->sc_buttons),
459 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
460 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
461 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
462 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
463 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
464 info->sc_iid_x);
465}
466
467static int
468ums_attach(device_t dev)
469{
470 struct usb2_attach_arg *uaa = device_get_ivars(dev);
471 struct ums_softc *sc = device_get_softc(dev);
472 struct ums_info *info;
473 void *d_ptr = NULL;
474 int isize;
475 int err;
476 uint16_t d_len;
477 uint8_t i;
478 uint8_t j;
479
480 DPRINTFN(11, "sc=%p\n", sc);
481
482 device_set_usb2_desc(dev);
483
484 mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
485
486 usb2_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
487
488 /*
489 * Force the report (non-boot) protocol.
490 *
491 * Mice without boot protocol support may choose not to implement
492 * Set_Protocol at all; Ignore any error.
493 */
494 err = usb2_req_set_protocol(uaa->device, NULL,
495 uaa->info.bIfaceIndex, 1);
496
497 err = usb2_transfer_setup(uaa->device,
498 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
499 UMS_N_TRANSFER, sc, &sc->sc_mtx);
500
501 if (err) {
502 DPRINTF("error=%s\n", usb2_errstr(err));
503 goto detach;
504 }
505 err = usb2_req_get_hid_desc(uaa->device, NULL, &d_ptr,
506 &d_len, M_TEMP, uaa->info.bIfaceIndex);
507
508 if (err) {
509 device_printf(dev, "error reading report description\n");
510 goto detach;
511 }
512
513 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
514
515 /*
516 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
517 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
518 * all of its other button positions are all off. It also reports that
519 * it has two addional buttons and a tilt wheel.
520 */
521 if (usb2_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
522 info = &sc->sc_info[0];
523 info->sc_flags = (UMS_FLAG_X_AXIS |
524 UMS_FLAG_Y_AXIS |
525 UMS_FLAG_Z_AXIS |
526 UMS_FLAG_SBU);
527 info->sc_buttons = 3;
528 isize = 5;
529 /* 1st byte of descriptor report contains garbage */
530 info->sc_loc_x.pos = 16;
531 info->sc_loc_y.pos = 24;
532 info->sc_loc_z.pos = 32;
533 info->sc_loc_btn[0].pos = 8;
534 info->sc_loc_btn[1].pos = 9;
535 info->sc_loc_btn[2].pos = 10;
536
537 /* Announce device */
538 device_printf(dev, "3 buttons and [XYZ] "
539 "coordinates ID=0\n");
540
541 } else {
542 /* Search the HID descriptor and announce device */
543 for (i = 0; i < UMS_INFO_MAX; i++) {
544 ums_hid_parse(sc, dev, d_ptr, d_len, i);
545 }
546 }
547
548 if (usb2_test_quirk(uaa, UQ_MS_REVZ)) {
549 info = &sc->sc_info[0];
550 /* Some wheels need the Z axis reversed. */
551 info->sc_flags |= UMS_FLAG_REVZ;
552 }
553 if (isize > sc->sc_xfer[UMS_INTR_DT]->max_frame_size) {
554 DPRINTF("WARNING: report size, %d bytes, is larger "
555 "than interrupt size, %d bytes!\n",
556 isize, sc->sc_xfer[UMS_INTR_DT]->max_frame_size);
557 }
558 free(d_ptr, M_TEMP);
559 d_ptr = NULL;
560
561#if USB_DEBUG
562 for (j = 0; j < UMS_INFO_MAX; j++) {
563 info = &sc->sc_info[j];
564
565 DPRINTF("sc=%p, index=%d\n", sc, j);
566 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
567 info->sc_loc_x.size, info->sc_iid_x);
568 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
569 info->sc_loc_y.size, info->sc_iid_y);
570 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
571 info->sc_loc_z.size, info->sc_iid_z);
572 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
573 info->sc_loc_t.size, info->sc_iid_t);
574 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
575 info->sc_loc_w.size, info->sc_iid_w);
576
577 for (i = 0; i < info->sc_buttons; i++) {
578 DPRINTF("B%d\t%d/%d id=%d\n",
579 i + 1, info->sc_loc_btn[i].pos,
580 info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
581 }
582 }
583 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
584#endif
585
586 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
587 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
588 else
589 sc->sc_hw.buttons = sc->sc_buttons;
590
591 sc->sc_hw.iftype = MOUSE_IF_USB;
592 sc->sc_hw.type = MOUSE_MOUSE;
593 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
594 sc->sc_hw.hwid = 0;
595
596 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
597 sc->sc_mode.rate = -1;
598 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
599 sc->sc_mode.accelfactor = 0;
600 sc->sc_mode.level = 0;
601 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
602 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
603 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
604
605 err = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx,
606 &ums_fifo_methods, &sc->sc_fifo,
607 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
608 UID_ROOT, GID_OPERATOR, 0644);
609 if (err) {
610 goto detach;
611 }
612 return (0);
613
614detach:
615 if (d_ptr) {
616 free(d_ptr, M_TEMP);
617 }
618 ums_detach(dev);
619 return (ENOMEM);
620}
621
622static int
623ums_detach(device_t self)
624{
625 struct ums_softc *sc = device_get_softc(self);
626
627 DPRINTF("sc=%p\n", sc);
628
629 usb2_fifo_detach(&sc->sc_fifo);
630
631 usb2_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
632
633 usb2_callout_drain(&sc->sc_callout);
634
635 mtx_destroy(&sc->sc_mtx);
636
637 return (0);
638}
639
640static void
641ums_start_read(struct usb2_fifo *fifo)
642{
643 struct ums_softc *sc = fifo->priv_sc0;
644
645 usb2_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
646}
647
648static void
649ums_stop_read(struct usb2_fifo *fifo)
650{
651 struct ums_softc *sc = fifo->priv_sc0;
652
653 usb2_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
654 usb2_callout_stop(&sc->sc_callout);
655}
656
657
658#if ((MOUSE_SYS_PACKETSIZE != 8) || \
659 (MOUSE_MSC_PACKETSIZE != 5))
660#error "Software assumptions are not met. Please update code."
661#endif
662
663static void
664ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
665 int32_t dz, int32_t dt, int32_t buttons)
666{
667 uint8_t buf[8];
668
669 if (1) {
670
671 if (dx > 254)
672 dx = 254;
673 if (dx < -256)
674 dx = -256;
675 if (dy > 254)
676 dy = 254;
677 if (dy < -256)
678 dy = -256;
679 if (dz > 126)
680 dz = 126;
681 if (dz < -128)
682 dz = -128;
683 if (dt > 126)
684 dt = 126;
685 if (dt < -128)
686 dt = -128;
687
688 buf[0] = sc->sc_mode.syncmask[1];
689 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
690 buf[1] = dx >> 1;
691 buf[2] = dy >> 1;
692 buf[3] = dx - (dx >> 1);
693 buf[4] = dy - (dy >> 1);
694
695 if (sc->sc_mode.level == 1) {
696 buf[5] = dz >> 1;
697 buf[6] = dz - (dz >> 1);
698 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
699 }
700 usb2_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
701 sc->sc_mode.packetsize, 1);
702
703 } else {
704 DPRINTF("Buffer full, discarded packet\n");
705 }
706}
707
708static void
709ums_reset_buf(struct ums_softc *sc)
710{
711 /* reset read queue */
712 usb2_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
713}
714
715static int
716ums_open(struct usb2_fifo *fifo, int fflags)
717{
718 struct ums_softc *sc = fifo->priv_sc0;
719
720 DPRINTFN(2, "\n");
721
722 if (fflags & FREAD) {
723
724 /* reset status */
725
726 sc->sc_status.flags = 0;
727 sc->sc_status.button = 0;
728 sc->sc_status.obutton = 0;
729 sc->sc_status.dx = 0;
730 sc->sc_status.dy = 0;
731 sc->sc_status.dz = 0;
732 /* sc->sc_status.dt = 0; */
733
734 if (usb2_fifo_alloc_buffer(fifo,
735 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
736 return (ENOMEM);
737 }
738 }
739 return (0);
740}
741
742static void
743ums_close(struct usb2_fifo *fifo, int fflags)
744{
745 if (fflags & FREAD) {
746 usb2_fifo_free_buffer(fifo);
747 }
748}
749
750static int
751ums_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr, int fflags)
752{
753 struct ums_softc *sc = fifo->priv_sc0;
754 mousemode_t mode;
755 int error = 0;
756
757 DPRINTFN(2, "\n");
758
759 mtx_lock(&sc->sc_mtx);
760
761 switch (cmd) {
762 case MOUSE_GETHWINFO:
763 *(mousehw_t *)addr = sc->sc_hw;
764 break;
765
766 case MOUSE_GETMODE:
767 *(mousemode_t *)addr = sc->sc_mode;
768 break;
769
770 case MOUSE_SETMODE:
771 mode = *(mousemode_t *)addr;
772
773 if (mode.level == -1) {
774 /* don't change the current setting */
775 } else if ((mode.level < 0) || (mode.level > 1)) {
776 error = EINVAL;
777 goto done;
778 } else {
779 sc->sc_mode.level = mode.level;
780 }
781
782 if (sc->sc_mode.level == 0) {
783 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
784 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
785 else
786 sc->sc_hw.buttons = sc->sc_buttons;
787 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
788 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
789 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
790 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
791 } else if (sc->sc_mode.level == 1) {
792 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
793 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
794 else
795 sc->sc_hw.buttons = sc->sc_buttons;
796 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
797 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
798 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
799 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
800 }
801 ums_reset_buf(sc);
802 break;
803
804 case MOUSE_GETLEVEL:
805 *(int *)addr = sc->sc_mode.level;
806 break;
807
808 case MOUSE_SETLEVEL:
809 if (*(int *)addr < 0 || *(int *)addr > 1) {
810 error = EINVAL;
811 goto done;
812 }
813 sc->sc_mode.level = *(int *)addr;
814
815 if (sc->sc_mode.level == 0) {
816 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
817 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
818 else
819 sc->sc_hw.buttons = sc->sc_buttons;
820 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
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 } else if (sc->sc_mode.level == 1) {
825 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
826 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
827 else
828 sc->sc_hw.buttons = sc->sc_buttons;
829 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
830 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
831 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
832 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
833 }
834 ums_reset_buf(sc);
835 break;
836
837 case MOUSE_GETSTATUS:{
838 mousestatus_t *status = (mousestatus_t *)addr;
839
840 *status = sc->sc_status;
841 sc->sc_status.obutton = sc->sc_status.button;
842 sc->sc_status.button = 0;
843 sc->sc_status.dx = 0;
844 sc->sc_status.dy = 0;
845 sc->sc_status.dz = 0;
846 /* sc->sc_status.dt = 0; */
847
848 if (status->dx || status->dy || status->dz /* || status->dt */ ) {
849 status->flags |= MOUSE_POSCHANGED;
850 }
851 if (status->button != status->obutton) {
852 status->flags |= MOUSE_BUTTONSCHANGED;
853 }
854 break;
855 }
856 default:
857 error = ENOTTY;
858 }
859
860done:
861 mtx_unlock(&sc->sc_mtx);
862 return (error);
863}
864
865static devclass_t ums_devclass;
866
867static device_method_t ums_methods[] = {
868 DEVMETHOD(device_probe, ums_probe),
869 DEVMETHOD(device_attach, ums_attach),
870 DEVMETHOD(device_detach, ums_detach),
871 {0, 0}
872};
873
874static driver_t ums_driver = {
875 .name = "ums",
876 .methods = ums_methods,
877 .size = sizeof(struct ums_softc),
878};
879
880DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
881MODULE_DEPEND(ums, usb, 1, 1, 1);