2 * Copyright (c) 2012 Huang Wen Hui
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
36 #include <sys/mutex.h>
39 #include <sys/fcntl.h>
41 #include <sys/selinfo.h>
43 #include <sys/sysctl.h>
45 #include <dev/usb/usb.h>
46 #include <dev/usb/usbdi.h>
47 #include <dev/usb/usbdi_util.h>
48 #include <dev/usb/usbhid.h>
52 #define USB_DEBUG_VAR wsp_debug
53 #include <dev/usb/usb_debug.h>
55 #include <sys/mouse.h>
57 #define WSP_DRIVER_NAME "wsp"
58 #define WSP_BUFFER_MAX 1024
60 #define WSP_CLAMP(x,low,high) do { \
63 else if ((x) > (high)) \
68 static SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW, 0, "USB wsp");
72 WSP_LLEVEL_DISABLED = 0,
74 WSP_LLEVEL_DEBUG, /* for troubleshooting */
75 WSP_LLEVEL_INFO, /* for diagnostics */
77 static int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */
79 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RW,
80 &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level");
81 #endif /* USB_DEBUG */
83 static struct wsp_tuning {
86 int pressure_touch_threshold;
87 int pressure_untouch_threshold;
88 int pressure_tap_threshold;
89 int scr_hor_threshold;
95 .pressure_touch_threshold = 50,
96 .pressure_untouch_threshold = 10,
97 .pressure_tap_threshold = 120,
98 .scr_hor_threshold = 50,
102 wsp_runing_rangecheck(struct wsp_tuning *ptun)
104 WSP_CLAMP(ptun->scale_factor, 1, 63);
105 WSP_CLAMP(ptun->z_factor, 1, 63);
106 WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255);
107 WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255);
108 WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255);
109 WSP_CLAMP(ptun->scr_hor_threshold, 1, 255);
112 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RW,
113 &wsp_tuning.scale_factor, 0, "movement scale factor");
114 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RW,
115 &wsp_tuning.z_factor, 0, "Z-axis scale factor");
116 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RW,
117 &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold");
118 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RW,
119 &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold");
120 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RW,
121 &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold");
122 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_hor_threshold, CTLFLAG_RW,
123 &wsp_tuning.scr_hor_threshold, 0, "horizontal scrolling threshold");
125 #define WSP_IFACE_INDEX 1
128 * Some tables, structures, definitions and constant values for the
129 * touchpad protocol has been copied from Linux's
130 * "drivers/input/mouse/bcm5974.c" which has the following copyright
131 * holders under GPLv2. All device specific code in this driver has
132 * been written from scratch. The decoding algorithm is based on
133 * output from FreeBSD's usbdump.
135 * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se)
136 * Copyright (C) 2008 Scott Shawcroft (scott.shawcroft@gmail.com)
137 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
138 * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net)
139 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
140 * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de)
141 * Copyright (C) 2005 Peter Osterlund (petero2@telia.com)
142 * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch)
143 * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch)
146 /* button data structure */
148 uint8_t unknown1; /* constant */
149 uint8_t button; /* left button */
150 uint8_t rel_x; /* relative x coordinate */
151 uint8_t rel_y; /* relative y coordinate */
154 /* trackpad header types */
156 TYPE1, /* plain trackpad */
157 TYPE2, /* button integrated in trackpad */
158 TYPE3 /* additional header fields since June 2013 */
161 /* trackpad finger data offsets, le16-aligned */
162 #define FINGER_TYPE1 (13 * 2)
163 #define FINGER_TYPE2 (15 * 2)
164 #define FINGER_TYPE3 (19 * 2)
166 /* trackpad button data offsets */
167 #define BUTTON_TYPE2 15
168 #define BUTTON_TYPE3 23
170 /* list of device capability bits */
171 #define HAS_INTEGRATED_BUTTON 1
173 /* trackpad finger header - little endian */
188 /* trackpad finger structure - little endian */
190 int16_t origin; /* zero when switching track finger */
191 int16_t abs_x; /* absolute x coodinate */
192 int16_t abs_y; /* absolute y coodinate */
193 int16_t rel_x; /* relative x coodinate */
194 int16_t rel_y; /* relative y coodinate */
195 int16_t tool_major; /* tool area, major axis */
196 int16_t tool_minor; /* tool area, minor axis */
197 int16_t orientation; /* 16384 when point, else 15 bit angle */
198 int16_t touch_major; /* touch area, major axis */
199 int16_t touch_minor; /* touch area, minor axis */
200 int16_t unused[3]; /* zeros */
201 int16_t multi; /* one finger: varies, more fingers:
205 /* trackpad finger data size, empirically at least ten fingers */
206 #define MAX_FINGERS 16
207 #define SIZEOF_FINGER sizeof(struct tp_finger)
208 #define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER)
210 #if (WSP_BUFFER_MAX < ((MAX_FINGERS * 14 * 2) + FINGER_TYPE3))
211 #error "WSP_BUFFER_MAX is too small"
215 WSP_FLAG_WELLSPRING1,
216 WSP_FLAG_WELLSPRING2,
217 WSP_FLAG_WELLSPRING3,
218 WSP_FLAG_WELLSPRING4,
219 WSP_FLAG_WELLSPRING4A,
220 WSP_FLAG_WELLSPRING5,
221 WSP_FLAG_WELLSPRING6A,
222 WSP_FLAG_WELLSPRING6,
223 WSP_FLAG_WELLSPRING5A,
224 WSP_FLAG_WELLSPRING7,
225 WSP_FLAG_WELLSPRING7A,
226 WSP_FLAG_WELLSPRING8,
230 /* device-specific configuration */
231 struct wsp_dev_params {
232 uint8_t caps; /* device capability bitmask */
233 uint8_t tp_type; /* type of trackpad interface */
234 uint8_t tp_offset; /* offset to trackpad finger data */
237 static const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = {
238 [WSP_FLAG_WELLSPRING1] = {
241 .tp_offset = FINGER_TYPE1,
243 [WSP_FLAG_WELLSPRING2] = {
246 .tp_offset = FINGER_TYPE1,
248 [WSP_FLAG_WELLSPRING3] = {
249 .caps = HAS_INTEGRATED_BUTTON,
251 .tp_offset = FINGER_TYPE2,
253 [WSP_FLAG_WELLSPRING4] = {
254 .caps = HAS_INTEGRATED_BUTTON,
256 .tp_offset = FINGER_TYPE2,
258 [WSP_FLAG_WELLSPRING4A] = {
259 .caps = HAS_INTEGRATED_BUTTON,
261 .tp_offset = FINGER_TYPE2,
263 [WSP_FLAG_WELLSPRING5] = {
264 .caps = HAS_INTEGRATED_BUTTON,
266 .tp_offset = FINGER_TYPE2,
268 [WSP_FLAG_WELLSPRING6] = {
269 .caps = HAS_INTEGRATED_BUTTON,
271 .tp_offset = FINGER_TYPE2,
273 [WSP_FLAG_WELLSPRING5A] = {
274 .caps = HAS_INTEGRATED_BUTTON,
276 .tp_offset = FINGER_TYPE2,
278 [WSP_FLAG_WELLSPRING6A] = {
279 .caps = HAS_INTEGRATED_BUTTON,
281 .tp_offset = FINGER_TYPE2,
283 [WSP_FLAG_WELLSPRING7] = {
284 .caps = HAS_INTEGRATED_BUTTON,
286 .tp_offset = FINGER_TYPE2,
288 [WSP_FLAG_WELLSPRING7A] = {
289 .caps = HAS_INTEGRATED_BUTTON,
291 .tp_offset = FINGER_TYPE2,
293 [WSP_FLAG_WELLSPRING8] = {
294 .caps = HAS_INTEGRATED_BUTTON,
296 .tp_offset = FINGER_TYPE3,
300 #define WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
302 static const STRUCT_USB_HOST_ID wsp_devs[] = {
304 WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1),
305 WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1),
306 WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1),
308 /* MacbookProPenryn, aka wellspring2 */
309 WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2),
310 WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2),
311 WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2),
313 /* Macbook5,1 (unibody), aka wellspring3 */
314 WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3),
315 WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3),
316 WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3),
318 /* MacbookAir3,2 (unibody), aka wellspring4 */
319 WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4),
320 WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4),
321 WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4),
323 /* MacbookAir3,1 (unibody), aka wellspring4 */
324 WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A),
325 WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A),
326 WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A),
328 /* Macbook8 (unibody, March 2011) */
329 WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5),
330 WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5),
331 WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5),
333 /* MacbookAir4,1 (unibody, July 2011) */
334 WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A),
335 WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A),
336 WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A),
338 /* MacbookAir4,2 (unibody, July 2011) */
339 WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6),
340 WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6),
341 WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6),
343 /* Macbook8,2 (unibody) */
344 WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A),
345 WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A),
346 WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A),
348 /* MacbookPro10,1 (unibody, June 2012) */
349 /* MacbookPro11,? (unibody, June 2013) */
350 WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7),
351 WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7),
352 WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7),
354 /* MacbookPro10,2 (unibody, October 2012) */
355 WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A),
356 WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A),
357 WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A),
359 /* MacbookAir6,2 (unibody, June 2013) */
360 WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8),
361 WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8),
362 WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8),
365 #define WSP_FIFO_BUF_SIZE 8 /* bytes */
366 #define WSP_FIFO_QUEUE_MAXLEN 50 /* units */
374 struct usb_device *sc_usb_device;
375 struct mtx sc_mutex; /* for synchronization */
376 struct usb_xfer *sc_xfer[WSP_N_TRANSFER];
377 struct usb_fifo_sc sc_fifo;
379 const struct wsp_dev_params *sc_params; /* device configuration */
384 mousestatus_t sc_status;
386 #define WSP_ENABLED 0x01
388 struct tp_finger *index[MAX_FINGERS]; /* finger index data */
389 int16_t pos_x[MAX_FINGERS]; /* position array */
390 int16_t pos_y[MAX_FINGERS]; /* position array */
391 u_int sc_touch; /* touch status */
392 #define WSP_UNTOUCH 0x00
393 #define WSP_FIRST_TOUCH 0x01
394 #define WSP_SECOND_TOUCH 0x02
395 #define WSP_TOUCHING 0x04
396 int16_t pre_pos_x; /* previous position array */
397 int16_t pre_pos_y; /* previous position array */
398 int dx_sum; /* x axis cumulative movement */
399 int dy_sum; /* y axis cumulative movement */
400 int dz_sum; /* z axis cumulative movement */
402 #define WSP_DZ_MAX_COUNT 32
403 int dt_sum; /* T-axis cumulative movement */
405 uint8_t o_ntouch; /* old touch finger status */
406 uint8_t finger; /* 0 or 1 *, check which finger moving */
408 #define WSP_TAP_THRESHOLD 3
409 #define WSP_TAP_MAX_COUNT 20
410 int distance; /* the distance of 2 fingers */
411 #define MAX_DISTANCE 2500 /* the max allowed distance */
412 uint8_t ibtn; /* button status in tapping */
413 uint8_t ntaps; /* finger status in tapping */
414 uint8_t scr_mode; /* scroll status in movement */
415 #define WSP_SCR_NONE 0
416 #define WSP_SCR_VER 1
417 #define WSP_SCR_HOR 2
418 uint8_t tp_data[WSP_BUFFER_MAX] __aligned(4); /* trackpad transferred data */
421 typedef enum interface_mode {
422 RAW_SENSOR_MODE = 0x01,
427 * function prototypes
429 static usb_fifo_cmd_t wsp_start_read;
430 static usb_fifo_cmd_t wsp_stop_read;
431 static usb_fifo_open_t wsp_open;
432 static usb_fifo_close_t wsp_close;
433 static usb_fifo_ioctl_t wsp_ioctl;
435 static struct usb_fifo_methods wsp_fifo_methods = {
437 .f_close = &wsp_close,
438 .f_ioctl = &wsp_ioctl,
439 .f_start_read = &wsp_start_read,
440 .f_stop_read = &wsp_stop_read,
441 .basename[0] = WSP_DRIVER_NAME,
444 /* device initialization and shutdown */
445 static int wsp_enable(struct wsp_softc *sc);
446 static void wsp_disable(struct wsp_softc *sc);
449 static void wsp_reset_buf(struct wsp_softc *sc);
450 static void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t);
452 /* Device methods. */
453 static device_probe_t wsp_probe;
454 static device_attach_t wsp_attach;
455 static device_detach_t wsp_detach;
456 static usb_callback_t wsp_intr_callback;
458 static const struct usb_config wsp_config[WSP_N_TRANSFER] = {
460 .type = UE_INTERRUPT,
461 .endpoint = UE_ADDR_ANY,
462 .direction = UE_DIR_IN,
467 .bufsize = WSP_BUFFER_MAX,
468 .callback = &wsp_intr_callback,
473 wsp_set_device_mode(struct wsp_softc *sc, interface_mode mode)
475 uint8_t mode_bytes[8];
478 err = usbd_req_get_report(sc->sc_usb_device, NULL,
479 mode_bytes, sizeof(mode_bytes), 0,
482 if (err != USB_ERR_NORMAL_COMPLETION) {
483 DPRINTF("Failed to read device mode (%d)\n", err);
488 * XXX Need to wait at least 250ms for hardware to get
489 * ready. The device mode handling appears to be handled
490 * asynchronously and we should not issue these commands too
493 pause("WHW", hz / 4);
495 mode_bytes[0] = mode;
497 return (usbd_req_set_report(sc->sc_usb_device, NULL,
498 mode_bytes, sizeof(mode_bytes), 0,
503 wsp_enable(struct wsp_softc *sc)
506 memset(&sc->sc_status, 0, sizeof(sc->sc_status));
507 sc->sc_state |= WSP_ENABLED;
509 DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n");
514 wsp_disable(struct wsp_softc *sc)
516 sc->sc_state &= ~WSP_ENABLED;
517 DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n");
521 wsp_probe(device_t self)
523 struct usb_attach_arg *uaa = device_get_ivars(self);
525 if (uaa->usb_mode != USB_MODE_HOST)
528 if (uaa->info.bIfaceIndex != WSP_IFACE_INDEX)
531 if ((uaa->info.bInterfaceClass != UICLASS_HID) ||
532 (uaa->info.bInterfaceProtocol != 0))
535 return (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa));
539 wsp_attach(device_t dev)
541 struct wsp_softc *sc = device_get_softc(dev);
542 struct usb_attach_arg *uaa = device_get_ivars(dev);
547 DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc);
549 /* Get HID descriptor */
550 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
551 &d_len, M_TEMP, uaa->info.bIfaceIndex);
553 if (err == USB_ERR_NORMAL_COMPLETION) {
554 /* Get HID report descriptor length */
555 sc->tp_datalen = hid_report_size(d_ptr, d_len, hid_input, NULL);
558 if (sc->tp_datalen <= 0 || sc->tp_datalen > WSP_BUFFER_MAX) {
559 DPRINTF("Invalid datalength or too big "
560 "datalength: %d\n", sc->tp_datalen);
567 sc->sc_usb_device = uaa->device;
570 * By default the touchpad behaves like a HID device, sending
571 * packets with reportID = 8. Such reports contain only
572 * limited information. They encode movement deltas and button
573 * events, but do not include data from the pressure
574 * sensors. The device input mode can be switched from HID
575 * reports to raw sensor data using vendor-specific USB
580 * During re-enumeration of the device we need to force the
581 * device back into HID mode before switching it to RAW
582 * mode. Else the device does not work like expected.
584 err = wsp_set_device_mode(sc, HID_MODE);
585 if (err != USB_ERR_NORMAL_COMPLETION) {
586 DPRINTF("Failed to set mode to HID MODE (%d)\n", err);
590 err = wsp_set_device_mode(sc, RAW_SENSOR_MODE);
591 if (err != USB_ERR_NORMAL_COMPLETION) {
592 DPRINTF("failed to set mode to RAW MODE (%d)\n", err);
596 mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE);
598 /* get device specific configuration */
599 sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa);
601 err = usbd_transfer_setup(uaa->device,
602 &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config,
603 WSP_N_TRANSFER, sc, &sc->sc_mutex);
605 DPRINTF("error=%s\n", usbd_errstr(err));
608 if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex,
609 &wsp_fifo_methods, &sc->sc_fifo,
610 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
611 UID_ROOT, GID_OPERATOR, 0644)) {
614 device_set_usb_desc(dev);
616 sc->sc_hw.buttons = 3;
617 sc->sc_hw.iftype = MOUSE_IF_USB;
618 sc->sc_hw.type = MOUSE_PAD;
619 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
620 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
621 sc->sc_mode.rate = -1;
622 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
623 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
624 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
625 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
627 sc->sc_touch = WSP_UNTOUCH;
628 sc->scr_mode = WSP_SCR_NONE;
638 wsp_detach(device_t dev)
640 struct wsp_softc *sc = device_get_softc(dev);
642 (void) wsp_set_device_mode(sc, HID_MODE);
644 mtx_lock(&sc->sc_mutex);
645 if (sc->sc_state & WSP_ENABLED)
647 mtx_unlock(&sc->sc_mutex);
649 usb_fifo_detach(&sc->sc_fifo);
651 usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER);
653 mtx_destroy(&sc->sc_mutex);
659 wsp_intr_callback(struct usb_xfer *xfer, usb_error_t error)
661 struct wsp_softc *sc = usbd_xfer_softc(xfer);
662 const struct wsp_dev_params *params = sc->sc_params;
663 struct usb_page_cache *pc;
666 struct wsp_tuning tun = wsp_tuning;
667 int ntouch = 0; /* the finger number in touch */
668 int ibt = 0; /* button status */
675 wsp_runing_rangecheck(&tun);
677 if (sc->dz_count == 0)
678 sc->dz_count = WSP_DZ_MAX_COUNT;
680 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
682 switch (USB_GET_STATE(xfer)) {
683 case USB_ST_TRANSFERRED:
685 /* copy out received data */
686 pc = usbd_xfer_get_frame(xfer, 0);
687 usbd_copy_out(pc, 0, sc->tp_data, len);
689 if (len < sc->tp_datalen) {
690 /* make sure we don't process old data */
691 memset(sc->tp_data + len, 0, sc->tp_datalen - len);
694 h = (struct tp_header *)(sc->tp_data);
696 if (params->tp_type == TYPE2) {
697 ibt = sc->tp_data[BUTTON_TYPE2];
698 ntouch = sc->tp_data[BUTTON_TYPE2 - 1];
699 } else if (params->tp_type == TYPE3) {
700 ibt = sc->tp_data[BUTTON_TYPE3];
701 ntouch = sc->tp_data[BUTTON_TYPE3 - 1];
706 else if (ntouch > MAX_FINGERS)
707 ntouch = MAX_FINGERS;
709 f = (struct tp_finger *)(sc->tp_data + params->tp_offset);
711 for (i = 0; i != ntouch; i++) {
712 /* swap endianness, if any */
713 if (le16toh(0x1234) != 0x1234) {
714 f[i].origin = le16toh((uint16_t)f[i].origin);
715 f[i].abs_x = le16toh((uint16_t)f[i].abs_x);
716 f[i].abs_y = le16toh((uint16_t)f[i].abs_y);
717 f[i].rel_x = le16toh((uint16_t)f[i].rel_x);
718 f[i].rel_y = le16toh((uint16_t)f[i].rel_y);
719 f[i].tool_major = le16toh((uint16_t)f[i].tool_major);
720 f[i].tool_minor = le16toh((uint16_t)f[i].tool_minor);
721 f[i].orientation = le16toh((uint16_t)f[i].orientation);
722 f[i].touch_major = le16toh((uint16_t)f[i].touch_major);
723 f[i].touch_minor = le16toh((uint16_t)f[i].touch_minor);
724 f[i].multi = le16toh((uint16_t)f[i].multi);
726 DPRINTFN(WSP_LLEVEL_INFO, "[%d]ibt=%d, taps=%d, u=%x, o=%4d, ax=%5d, ay=%5d, "
727 "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%5d, tchmaj=%4d, tchmin=%4d, m=%4x\n",
728 i, ibt, ntouch, h->q2,
729 f[i].origin, f[i].abs_x, f[i].abs_y, f[i].rel_x, f[i].rel_y,
730 f[i].tool_major, f[i].tool_minor, f[i].orientation,
731 f[i].touch_major, f[i].touch_minor, f[i].multi);
733 sc->pos_x[i] = f[i].abs_x;
734 sc->pos_y[i] = -f[i].abs_y;
735 sc->index[i] = &f[i];
738 sc->sc_status.flags &= ~MOUSE_POSCHANGED;
739 sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED;
740 sc->sc_status.obutton = sc->sc_status.button;
741 sc->sc_status.button = 0;
744 sc->sc_status.button |= MOUSE_BUTTON1DOWN;
750 if (sc->ntaps < ntouch) {
753 if (f[0].touch_major > tun.pressure_tap_threshold)
757 if (f[0].touch_major > tun.pressure_tap_threshold &&
758 f[1].touch_major > tun.pressure_tap_threshold)
762 if (f[0].touch_major > tun.pressure_tap_threshold &&
763 f[1].touch_major > tun.pressure_tap_threshold &&
764 f[2].touch_major > tun.pressure_tap_threshold)
772 sc->distance = max(sc->distance, max(
773 abs(sc->pos_x[0] - sc->pos_x[1]),
774 abs(sc->pos_y[0] - sc->pos_y[1])));
776 if (f[0].touch_major < tun.pressure_untouch_threshold &&
777 sc->sc_status.button == 0) {
778 sc->sc_touch = WSP_UNTOUCH;
779 if (sc->intr_count < WSP_TAP_MAX_COUNT &&
780 sc->intr_count > WSP_TAP_THRESHOLD &&
781 sc->ntaps && sc->ibtn == 0) {
783 * Add a pair of events (button-down and
788 if (!(params->caps & HAS_INTEGRATED_BUTTON)) {
789 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN);
790 DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n");
794 if (sc->distance < MAX_DISTANCE)
795 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN);
796 DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n");
799 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN);
802 /* we don't handle taps of more than three fingers */
805 wsp_add_to_queue(sc, 0, 0, 0, 0); /* button release */
807 if (sc->intr_count >= WSP_TAP_MAX_COUNT &&
808 (sc->dt_sum / tun.scr_hor_threshold) != 0 &&
809 sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) {
812 * translate T-axis into button presses
816 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3);
817 else if (sc->dt_sum < 0)
818 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4);
820 sc->dz_count = WSP_DZ_MAX_COUNT;
830 sc->scr_mode = WSP_SCR_NONE;
831 } else if (f[0].touch_major >= tun.pressure_touch_threshold &&
832 sc->sc_touch == WSP_UNTOUCH) { /* ignore first touch */
833 sc->sc_touch = WSP_FIRST_TOUCH;
834 } else if (f[0].touch_major >= tun.pressure_touch_threshold &&
835 sc->sc_touch == WSP_FIRST_TOUCH) { /* ignore second touch */
836 sc->sc_touch = WSP_SECOND_TOUCH;
837 DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n",
838 sc->pre_pos_x, sc->pre_pos_y);
840 if (sc->sc_touch == WSP_SECOND_TOUCH)
841 sc->sc_touch = WSP_TOUCHING;
845 f[0].touch_major >= tun.pressure_touch_threshold) {
846 dx = sc->pos_x[0] - sc->pre_pos_x;
847 dy = sc->pos_y[0] - sc->pre_pos_y;
849 /* Ignore movement from ibt=1 to ibt=0 */
850 if (sc->sc_status.obutton != 0 &&
851 sc->sc_status.button == 0) {
855 /* Ignore movement if ntouch changed */
856 if (sc->o_ntouch != ntouch) {
861 if (ntouch == 2 && sc->sc_status.button != 0) {
862 dx = sc->pos_x[sc->finger] - sc->pre_pos_x;
863 dy = sc->pos_y[sc->finger] - sc->pre_pos_y;
866 * Ignore movement of switch finger or
867 * movement from ibt=0 to ibt=1
869 if (f[0].origin == 0 || f[1].origin == 0 ||
870 sc->sc_status.obutton != sc->sc_status.button) {
875 if ((abs(f[0].rel_x) + abs(f[0].rel_y)) <
876 (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
878 sc->sc_touch = WSP_SECOND_TOUCH;
883 if ((abs(f[0].rel_x) + abs(f[0].rel_y)) >=
884 (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
886 sc->sc_touch = WSP_SECOND_TOUCH;
891 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n",
895 sc->dz_sum -= (dy / tun.scale_factor);
896 if ((sc->dz_sum / tun.z_factor) != 0)
899 dx /= tun.scale_factor;
900 dy /= tun.scale_factor;
904 if (ntouch == 2 && sc->sc_status.button == 0) {
905 if (sc->scr_mode == WSP_SCR_NONE &&
906 abs(sc->dx_sum) + abs(sc->dy_sum) > 50)
907 sc->scr_mode = abs(sc->dx_sum) >
908 abs(sc->dy_sum) ? WSP_SCR_HOR :
910 DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n",
911 sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum);
912 if (sc->scr_mode == WSP_SCR_HOR)
919 if (sc->dz_count == 0)
920 dz = sc->dz_sum / tun.z_factor;
921 if (abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE ||
922 abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE)
925 if (sc->intr_count < WSP_TAP_MAX_COUNT &&
926 abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3) {
929 sc->intr_count = WSP_TAP_MAX_COUNT;
931 sc->sc_status.flags |= MOUSE_POSCHANGED;
932 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n",
933 dx, dy, dz, sc->sc_touch, sc->sc_status.button);
934 sc->sc_status.dx += dx;
935 sc->sc_status.dy += dy;
936 sc->sc_status.dz += dz;
938 wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button);
939 if (sc->dz_count == 0)
943 sc->pre_pos_x = sc->pos_x[0];
944 sc->pre_pos_y = sc->pos_y[0];
946 if (ntouch == 2 && sc->sc_status.button != 0) {
947 sc->pre_pos_x = sc->pos_x[sc->finger];
948 sc->pre_pos_y = sc->pos_y[sc->finger];
950 sc->o_ntouch = ntouch;
954 /* check if we can put more data into the FIFO */
955 if (usb_fifo_put_bytes_max(
956 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
957 usbd_xfer_set_frame_len(xfer, 0,
959 usbd_transfer_submit(xfer);
964 if (error != USB_ERR_CANCELLED) {
965 /* try clear stall first */
966 usbd_xfer_set_stall(xfer);
974 wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz,
977 uint32_t buttons_out;
987 buttons_out = MOUSE_MSC_BUTTONS;
988 if (buttons_in & MOUSE_BUTTON1DOWN)
989 buttons_out &= ~MOUSE_MSC_BUTTON1UP;
990 else if (buttons_in & MOUSE_BUTTON2DOWN)
991 buttons_out &= ~MOUSE_MSC_BUTTON2UP;
992 else if (buttons_in & MOUSE_BUTTON3DOWN)
993 buttons_out &= ~MOUSE_MSC_BUTTON3UP;
995 /* Encode the mouse data in standard format; refer to mouse(4) */
996 buf[0] = sc->sc_mode.syncmask[1];
997 buf[0] |= buttons_out;
1000 buf[3] = dx - (dx >> 1);
1001 buf[4] = dy - (dy >> 1);
1002 /* Encode extra bytes for level 1 */
1003 if (sc->sc_mode.level == 1) {
1004 buf[5] = dz >> 1; /* dz / 2 */
1005 buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */
1006 buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS);
1008 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
1009 sc->sc_mode.packetsize, 1);
1013 wsp_reset_buf(struct wsp_softc *sc)
1015 /* reset read queue */
1016 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
1020 wsp_start_read(struct usb_fifo *fifo)
1022 struct wsp_softc *sc = usb_fifo_softc(fifo);
1025 /* Check if we should override the default polling interval */
1026 rate = sc->sc_pollrate;
1027 /* Range check rate */
1030 /* Check for set rate */
1031 if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) {
1032 /* Stop current transfer, if any */
1033 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1034 /* Set new interval */
1035 usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate);
1036 /* Only set pollrate once */
1037 sc->sc_pollrate = 0;
1039 usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]);
1043 wsp_stop_read(struct usb_fifo *fifo)
1045 struct wsp_softc *sc = usb_fifo_softc(fifo);
1047 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1052 wsp_open(struct usb_fifo *fifo, int fflags)
1054 DPRINTFN(WSP_LLEVEL_INFO, "\n");
1056 if (fflags & FREAD) {
1057 struct wsp_softc *sc = usb_fifo_softc(fifo);
1060 if (sc->sc_state & WSP_ENABLED)
1063 if (usb_fifo_alloc_buffer(fifo,
1064 WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) {
1067 rc = wsp_enable(sc);
1069 usb_fifo_free_buffer(fifo);
1077 wsp_close(struct usb_fifo *fifo, int fflags)
1079 if (fflags & FREAD) {
1080 struct wsp_softc *sc = usb_fifo_softc(fifo);
1083 usb_fifo_free_buffer(fifo);
1088 wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1090 struct wsp_softc *sc = usb_fifo_softc(fifo);
1094 mtx_lock(&sc->sc_mutex);
1097 case MOUSE_GETHWINFO:
1098 *(mousehw_t *)addr = sc->sc_hw;
1101 *(mousemode_t *)addr = sc->sc_mode;
1104 mode = *(mousemode_t *)addr;
1106 if (mode.level == -1)
1107 /* Don't change the current setting */
1109 else if ((mode.level < 0) || (mode.level > 1)) {
1113 sc->sc_mode.level = mode.level;
1114 sc->sc_pollrate = mode.rate;
1115 sc->sc_hw.buttons = 3;
1117 if (sc->sc_mode.level == 0) {
1118 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1119 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1120 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1121 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1122 } else if (sc->sc_mode.level == 1) {
1123 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1124 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1125 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1126 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1130 case MOUSE_GETLEVEL:
1131 *(int *)addr = sc->sc_mode.level;
1133 case MOUSE_SETLEVEL:
1134 if (*(int *)addr < 0 || *(int *)addr > 1) {
1138 sc->sc_mode.level = *(int *)addr;
1139 sc->sc_hw.buttons = 3;
1141 if (sc->sc_mode.level == 0) {
1142 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1143 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1144 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1145 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1146 } else if (sc->sc_mode.level == 1) {
1147 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1148 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1149 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1150 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1154 case MOUSE_GETSTATUS:{
1155 mousestatus_t *status = (mousestatus_t *)addr;
1157 *status = sc->sc_status;
1158 sc->sc_status.obutton = sc->sc_status.button;
1159 sc->sc_status.button = 0;
1160 sc->sc_status.dx = 0;
1161 sc->sc_status.dy = 0;
1162 sc->sc_status.dz = 0;
1164 if (status->dx || status->dy || status->dz)
1165 status->flags |= MOUSE_POSCHANGED;
1166 if (status->button != status->obutton)
1167 status->flags |= MOUSE_BUTTONSCHANGED;
1175 mtx_unlock(&sc->sc_mutex);
1179 static device_method_t wsp_methods[] = {
1180 /* Device interface */
1181 DEVMETHOD(device_probe, wsp_probe),
1182 DEVMETHOD(device_attach, wsp_attach),
1183 DEVMETHOD(device_detach, wsp_detach),
1187 static driver_t wsp_driver = {
1188 .name = WSP_DRIVER_NAME,
1189 .methods = wsp_methods,
1190 .size = sizeof(struct wsp_softc)
1193 static devclass_t wsp_devclass;
1195 DRIVER_MODULE(wsp, uhub, wsp_driver, wsp_devclass, NULL, 0);
1196 MODULE_DEPEND(wsp, usb, 1, 1, 1);
1197 MODULE_VERSION(wsp, 1);