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"
59 #define WSP_CLAMP(x,low,high) do { \
62 else if ((x) > (high)) \
67 static SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW, 0, "USB wsp");
71 WSP_LLEVEL_DISABLED = 0,
73 WSP_LLEVEL_DEBUG, /* for troubleshooting */
74 WSP_LLEVEL_INFO, /* for diagnostics */
76 static int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */
78 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RW,
79 &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level");
80 #endif /* USB_DEBUG */
82 static struct wsp_tuning {
85 int pressure_touch_threshold;
86 int pressure_untouch_threshold;
87 int pressure_tap_threshold;
88 int scr_hor_threshold;
94 .pressure_touch_threshold = 50,
95 .pressure_untouch_threshold = 10,
96 .pressure_tap_threshold = 120,
97 .scr_hor_threshold = 50,
101 wsp_runing_rangecheck(struct wsp_tuning *ptun)
103 WSP_CLAMP(ptun->scale_factor, 1, 63);
104 WSP_CLAMP(ptun->z_factor, 1, 63);
105 WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255);
106 WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255);
107 WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255);
108 WSP_CLAMP(ptun->scr_hor_threshold, 1, 255);
111 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RW,
112 &wsp_tuning.scale_factor, 0, "movement scale factor");
113 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RW,
114 &wsp_tuning.z_factor, 0, "Z-axis scale factor");
115 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RW,
116 &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold");
117 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RW,
118 &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold");
119 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RW,
120 &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold");
121 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_hor_threshold, CTLFLAG_RW,
122 &wsp_tuning.scr_hor_threshold, 0, "horizontal scrolling threshold");
124 #define WSP_IFACE_INDEX 1
126 /* button data structure */
128 uint8_t unknown1; /* constant */
129 uint8_t button; /* left button */
130 uint8_t rel_x; /* relative x coordinate */
131 uint8_t rel_y; /* relative y coordinate */
134 /* trackpad header types */
136 TYPE1, /* plain trackpad */
137 TYPE2, /* button integrated in trackpad */
138 TYPE3 /* additional header fields since June 2013 */
141 /* trackpad finger data offsets, le16-aligned */
142 #define FINGER_TYPE1 (13 * 2)
143 #define FINGER_TYPE2 (15 * 2)
144 #define FINGER_TYPE3 (19 * 2)
146 /* trackpad button data offsets */
147 #define BUTTON_TYPE2 15
148 #define BUTTON_TYPE3 23
150 /* list of device capability bits */
151 #define HAS_INTEGRATED_BUTTON 1
153 /* trackpad finger header - little endian */
168 /* trackpad finger structure - little endian */
170 int16_t origin; /* zero when switching track finger */
171 int16_t abs_x; /* absolute x coodinate */
172 int16_t abs_y; /* absolute y coodinate */
173 int16_t rel_x; /* relative x coodinate */
174 int16_t rel_y; /* relative y coodinate */
175 int16_t tool_major; /* tool area, major axis */
176 int16_t tool_minor; /* tool area, minor axis */
177 int16_t orientation; /* 16384 when point, else 15 bit angle */
178 int16_t touch_major; /* touch area, major axis */
179 int16_t touch_minor; /* touch area, minor axis */
180 int16_t unused[3]; /* zeros */
181 int16_t multi; /* one finger: varies, more fingers:
185 /* trackpad finger data size, empirically at least ten fingers */
186 #define MAX_FINGERS 16
187 #define SIZEOF_FINGER sizeof(struct tp_finger)
188 #define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER)
189 #define MAX_FINGER_ORIENTATION 16384
191 /* logical signal quality */
192 #define SN_PRESSURE 45 /* pressure signal-to-noise ratio */
193 #define SN_WIDTH 25 /* width signal-to-noise ratio */
194 #define SN_COORD 250 /* coordinate signal-to-noise ratio */
195 #define SN_ORIENT 10 /* orientation signal-to-noise ratio */
197 /* device-specific parameters */
199 int snratio; /* signal-to-noise ratio */
200 int min; /* device minimum reading */
201 int max; /* device maximum reading */
205 WSP_FLAG_WELLSPRING1,
206 WSP_FLAG_WELLSPRING2,
207 WSP_FLAG_WELLSPRING3,
208 WSP_FLAG_WELLSPRING4,
209 WSP_FLAG_WELLSPRING4A,
210 WSP_FLAG_WELLSPRING5,
211 WSP_FLAG_WELLSPRING6A,
212 WSP_FLAG_WELLSPRING6,
213 WSP_FLAG_WELLSPRING5A,
214 WSP_FLAG_WELLSPRING7,
215 WSP_FLAG_WELLSPRING7A,
216 WSP_FLAG_WELLSPRING8,
220 /* device-specific configuration */
221 struct wsp_dev_params {
222 uint8_t caps; /* device capability bitmask */
223 uint16_t bt_datalen; /* data length of the button interface */
224 uint8_t tp_type; /* type of trackpad interface */
225 uint8_t tp_offset; /* offset to trackpad finger data */
226 uint16_t tp_datalen; /* data length of the trackpad
228 struct wsp_param p; /* finger pressure limits */
229 struct wsp_param w; /* finger width limits */
230 struct wsp_param x; /* horizontal limits */
231 struct wsp_param y; /* vertical limits */
232 struct wsp_param o; /* orientation limits */
235 static const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = {
236 [WSP_FLAG_WELLSPRING1] = {
238 .bt_datalen = sizeof(struct bt_data),
240 .tp_offset = FINGER_TYPE1,
241 .tp_datalen = FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
249 SN_COORD, -4824, 5342
255 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
258 [WSP_FLAG_WELLSPRING2] = {
260 .bt_datalen = sizeof(struct bt_data),
262 .tp_offset = FINGER_TYPE1,
263 .tp_datalen = FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
271 SN_COORD, -4824, 4824
277 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
280 [WSP_FLAG_WELLSPRING3] = {
281 .caps = HAS_INTEGRATED_BUTTON,
282 .bt_datalen = sizeof(struct bt_data),
284 .tp_offset = FINGER_TYPE2,
285 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
293 SN_COORD, -4460, 5166
299 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
302 [WSP_FLAG_WELLSPRING4] = {
303 .caps = HAS_INTEGRATED_BUTTON,
304 .bt_datalen = sizeof(struct bt_data),
306 .tp_offset = FINGER_TYPE2,
307 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
315 SN_COORD, -4620, 5140
321 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
324 [WSP_FLAG_WELLSPRING4A] = {
325 .caps = HAS_INTEGRATED_BUTTON,
326 .bt_datalen = sizeof(struct bt_data),
328 .tp_offset = FINGER_TYPE2,
329 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
337 SN_COORD, -4616, 5112
343 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
346 [WSP_FLAG_WELLSPRING5] = {
347 .caps = HAS_INTEGRATED_BUTTON,
348 .bt_datalen = sizeof(struct bt_data),
350 .tp_offset = FINGER_TYPE2,
351 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
359 SN_COORD, -4415, 5050
365 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
368 [WSP_FLAG_WELLSPRING6] = {
369 .caps = HAS_INTEGRATED_BUTTON,
370 .bt_datalen = sizeof(struct bt_data),
372 .tp_offset = FINGER_TYPE2,
373 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
381 SN_COORD, -4620, 5140
387 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
390 [WSP_FLAG_WELLSPRING5A] = {
391 .caps = HAS_INTEGRATED_BUTTON,
392 .bt_datalen = sizeof(struct bt_data),
394 .tp_offset = FINGER_TYPE2,
395 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
403 SN_COORD, -4750, 5280
409 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
412 [WSP_FLAG_WELLSPRING6A] = {
413 .caps = HAS_INTEGRATED_BUTTON,
414 .bt_datalen = sizeof(struct bt_data),
416 .tp_offset = FINGER_TYPE2,
417 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
425 SN_COORD, -4620, 5140
431 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
434 [WSP_FLAG_WELLSPRING7] = {
435 .caps = HAS_INTEGRATED_BUTTON,
436 .bt_datalen = sizeof(struct bt_data),
438 .tp_offset = FINGER_TYPE2,
439 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
447 SN_COORD, -4750, 5280
453 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
456 [WSP_FLAG_WELLSPRING7A] = {
457 .caps = HAS_INTEGRATED_BUTTON,
458 .bt_datalen = sizeof(struct bt_data),
460 .tp_offset = FINGER_TYPE2,
461 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
469 SN_COORD, -4750, 5280
475 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
478 [WSP_FLAG_WELLSPRING8] = {
479 .caps = HAS_INTEGRATED_BUTTON,
480 .bt_datalen = sizeof(struct bt_data),
482 .tp_offset = FINGER_TYPE3,
483 .tp_datalen = FINGER_TYPE3 + SIZEOF_ALL_FINGERS,
491 SN_COORD, -4620, 5140
497 SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
502 #define WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
504 static const STRUCT_USB_HOST_ID wsp_devs[] = {
506 WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1),
507 WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1),
508 WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1),
510 /* MacbookProPenryn, aka wellspring2 */
511 WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2),
512 WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2),
513 WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2),
515 /* Macbook5,1 (unibody), aka wellspring3 */
516 WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3),
517 WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3),
518 WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3),
520 /* MacbookAir3,2 (unibody), aka wellspring4 */
521 WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4),
522 WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4),
523 WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4),
525 /* MacbookAir3,1 (unibody), aka wellspring4 */
526 WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A),
527 WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A),
528 WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A),
530 /* Macbook8 (unibody, March 2011) */
531 WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5),
532 WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5),
533 WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5),
535 /* MacbookAir4,1 (unibody, July 2011) */
536 WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A),
537 WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A),
538 WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A),
540 /* MacbookAir4,2 (unibody, July 2011) */
541 WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6),
542 WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6),
543 WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6),
545 /* Macbook8,2 (unibody) */
546 WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A),
547 WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A),
548 WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A),
550 /* MacbookPro10,1 (unibody, June 2012) */
551 /* MacbookPro11,? (unibody, June 2013) */
552 WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7),
553 WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7),
554 WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7),
556 /* MacbookPro10,2 (unibody, October 2012) */
557 WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A),
558 WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A),
559 WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A),
561 /* MacbookAir6,2 (unibody, June 2013) */
562 WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8),
563 WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8),
564 WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8),
567 #define WSP_FIFO_BUF_SIZE 8 /* bytes */
568 #define WSP_FIFO_QUEUE_MAXLEN 50 /* units */
576 struct usb_device *sc_usb_device;
577 struct mtx sc_mutex; /* for synchronization */
578 struct usb_xfer *sc_xfer[WSP_N_TRANSFER];
579 struct usb_fifo_sc sc_fifo;
581 const struct wsp_dev_params *sc_params; /* device configuration */
586 mousestatus_t sc_status;
588 #define WSP_ENABLED 0x01
590 struct bt_data *bt_data; /* button transferred data */
591 uint8_t *tp_data; /* trackpad transferred data */
592 struct tp_finger *index[MAX_FINGERS]; /* finger index data */
593 int16_t pos_x[MAX_FINGERS]; /* position array */
594 int16_t pos_y[MAX_FINGERS]; /* position array */
595 u_int sc_touch; /* touch status */
596 #define WSP_UNTOUCH 0x00
597 #define WSP_FIRST_TOUCH 0x01
598 #define WSP_SECOND_TOUCH 0x02
599 #define WSP_TOUCHING 0x04
600 int16_t pre_pos_x; /* previous position array */
601 int16_t pre_pos_y; /* previous position array */
602 int dx_sum; /* x axis cumulative movement */
603 int dy_sum; /* y axis cumulative movement */
604 int dz_sum; /* z axis cumulative movement */
606 #define WSP_DZ_MAX_COUNT 32
607 int dt_sum; /* T-axis cumulative movement */
609 uint8_t finger; /* 0 or 1 *, check which finger moving */
611 #define WSP_TAP_THRESHOLD 3
612 #define WSP_TAP_MAX_COUNT 20
613 int distance; /* the distance of 2 fingers */
614 #define MAX_DISTANCE 2500 /* the max allowed distance */
615 uint8_t ibtn; /* button status in tapping */
616 uint8_t ntaps; /* finger status in tapping */
617 uint8_t scr_mode; /* scroll status in movement */
618 #define WSP_SCR_NONE 0
619 #define WSP_SCR_VER 1
620 #define WSP_SCR_HOR 2
623 typedef enum interface_mode {
624 RAW_SENSOR_MODE = 0x01,
629 * function prototypes
631 static usb_fifo_cmd_t wsp_start_read;
632 static usb_fifo_cmd_t wsp_stop_read;
633 static usb_fifo_open_t wsp_open;
634 static usb_fifo_close_t wsp_close;
635 static usb_fifo_ioctl_t wsp_ioctl;
637 static struct usb_fifo_methods wsp_fifo_methods = {
639 .f_close = &wsp_close,
640 .f_ioctl = &wsp_ioctl,
641 .f_start_read = &wsp_start_read,
642 .f_stop_read = &wsp_stop_read,
643 .basename[0] = WSP_DRIVER_NAME,
646 /* device initialization and shutdown */
647 static int wsp_enable(struct wsp_softc *sc);
648 static void wsp_disable(struct wsp_softc *sc);
651 static void wsp_reset_buf(struct wsp_softc *sc);
652 static void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t);
654 /* Device methods. */
655 static device_probe_t wsp_probe;
656 static device_attach_t wsp_attach;
657 static device_detach_t wsp_detach;
658 static usb_callback_t wsp_intr_callback;
660 static struct usb_config wsp_config[WSP_N_TRANSFER] = {
662 .type = UE_INTERRUPT,
663 .endpoint = UE_ADDR_ANY,
664 .direction = UE_DIR_IN,
669 .bufsize = 0, /* use wMaxPacketSize */
670 .callback = &wsp_intr_callback,
675 wsp_set_device_mode(struct wsp_softc *sc, interface_mode mode)
677 uint8_t mode_bytes[8];
680 err = usbd_req_get_report(sc->sc_usb_device, NULL,
681 mode_bytes, sizeof(mode_bytes), 0,
684 if (err != USB_ERR_NORMAL_COMPLETION) {
685 DPRINTF("Failed to read device mode (%d)\n", err);
690 * XXX Need to wait at least 250ms for hardware to get
691 * ready. The device mode handling appears to be handled
692 * asynchronously and we should not issue these commands too
695 pause("WHW", hz / 4);
697 mode_bytes[0] = mode;
699 return (usbd_req_set_report(sc->sc_usb_device, NULL,
700 mode_bytes, sizeof(mode_bytes), 0,
705 wsp_enable(struct wsp_softc *sc)
707 const struct wsp_dev_params *params = sc->sc_params;
709 if (params == NULL || params->tp_datalen == 0) {
710 DPRINTF("params uninitialized!\n");
713 /* Allocate the dynamic buffers */
714 sc->tp_data = malloc(params->tp_datalen, M_USB, M_WAITOK | M_ZERO);
715 if (sc->tp_data == NULL) {
716 DPRINTF("Cannot allocate memory\n");
721 memset(&sc->sc_status, 0, sizeof(sc->sc_status));
722 sc->sc_state |= WSP_ENABLED;
724 DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n");
729 wsp_disable(struct wsp_softc *sc)
731 free(sc->tp_data, M_USB);
734 sc->sc_state &= ~WSP_ENABLED;
735 DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n");
739 wsp_probe(device_t self)
741 struct usb_attach_arg *uaa = device_get_ivars(self);
743 if (uaa->usb_mode != USB_MODE_HOST)
746 if (uaa->info.bIfaceIndex != WSP_IFACE_INDEX)
749 if ((uaa->info.bInterfaceClass != UICLASS_HID) ||
750 (uaa->info.bInterfaceProtocol != 0))
753 return (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa));
757 wsp_attach(device_t dev)
759 struct wsp_softc *sc = device_get_softc(dev);
760 struct usb_attach_arg *uaa = device_get_ivars(dev);
763 DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc);
765 sc->sc_usb_device = uaa->device;
768 * By default the touchpad behaves like a HID device, sending
769 * packets with reportID = 8. Such reports contain only
770 * limited information. They encode movement deltas and button
771 * events, but do not include data from the pressure
772 * sensors. The device input mode can be switched from HID
773 * reports to raw sensor data using vendor-specific USB
778 * During re-enumeration of the device we need to force the
779 * device back into HID mode before switching it to RAW
780 * mode. Else the device does not work like expected.
782 err = wsp_set_device_mode(sc, HID_MODE);
783 if (err != USB_ERR_NORMAL_COMPLETION) {
784 DPRINTF("Failed to set mode to HID MODE (%d)\n", err);
788 err = wsp_set_device_mode(sc, RAW_SENSOR_MODE);
789 if (err != USB_ERR_NORMAL_COMPLETION) {
790 DPRINTF("failed to set mode to RAW MODE (%d)\n", err);
794 mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE);
796 /* get device specific configuration */
797 sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa);
799 /* set to 0 to use wMaxPacketSize is not enough */
800 wsp_config[0].bufsize = sc->sc_params->tp_datalen;
802 err = usbd_transfer_setup(uaa->device,
803 &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config,
804 WSP_N_TRANSFER, sc, &sc->sc_mutex);
807 DPRINTF("error=%s\n", usbd_errstr(err));
810 if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex,
811 &wsp_fifo_methods, &sc->sc_fifo,
812 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
813 UID_ROOT, GID_OPERATOR, 0644)) {
816 device_set_usb_desc(dev);
818 sc->sc_hw.buttons = 3;
819 sc->sc_hw.iftype = MOUSE_IF_USB;
820 sc->sc_hw.type = MOUSE_PAD;
821 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
822 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
823 sc->sc_mode.rate = -1;
824 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
825 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
826 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
827 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
829 sc->sc_touch = WSP_UNTOUCH;
830 sc->scr_mode = WSP_SCR_NONE;
840 wsp_detach(device_t dev)
842 struct wsp_softc *sc = device_get_softc(dev);
844 (void) wsp_set_device_mode(sc, HID_MODE);
846 mtx_lock(&sc->sc_mutex);
847 if (sc->sc_state & WSP_ENABLED)
849 mtx_unlock(&sc->sc_mutex);
851 usb_fifo_detach(&sc->sc_fifo);
853 usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER);
855 mtx_destroy(&sc->sc_mutex);
861 wsp_intr_callback(struct usb_xfer *xfer, usb_error_t error)
863 struct wsp_softc *sc = usbd_xfer_softc(xfer);
864 const struct wsp_dev_params *params = sc->sc_params;
865 struct usb_page_cache *pc;
868 struct wsp_tuning tun = wsp_tuning;
869 int ntouch = 0; /* the finger number in touch */
870 int ibt = 0; /* button status */
878 wsp_runing_rangecheck(&tun);
880 if (sc->dz_count == 0)
881 sc->dz_count = WSP_DZ_MAX_COUNT;
883 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
885 switch (USB_GET_STATE(xfer)) {
886 case USB_ST_TRANSFERRED:
887 if (len > (int)params->tp_datalen) {
888 DPRINTFN(WSP_LLEVEL_ERROR,
889 "truncating large packet from %u to %u bytes\n",
890 len, params->tp_datalen);
891 len = params->tp_datalen;
893 /* make sure we don't process old data */
894 memset(sc->tp_data + len, 0, params->tp_datalen - len);
897 pc = usbd_xfer_get_frame(xfer, 0);
898 usbd_copy_out(pc, 0, sc->tp_data, len);
900 h = (struct tp_header *)(sc->tp_data);
902 if (params->tp_type == TYPE2) {
903 ibt = sc->tp_data[BUTTON_TYPE2];
904 ntouch = sc->tp_data[BUTTON_TYPE2 - 1];
905 } else if (params->tp_type == TYPE3) {
906 ibt = sc->tp_data[BUTTON_TYPE3];
907 ntouch = sc->tp_data[BUTTON_TYPE3 - 1];
912 else if (ntouch > MAX_FINGERS)
913 ntouch = MAX_FINGERS;
915 f = (struct tp_finger *)(sc->tp_data + params->tp_offset);
917 for (i = 0; i != ntouch; i++) {
918 /* swap endianness, if any */
919 if (le16toh(0x1234) != 0x1234) {
920 f[i].origin = le16toh((uint16_t)f[i].origin);
921 f[i].abs_x = le16toh((uint16_t)f[i].abs_x);
922 f[i].abs_y = le16toh((uint16_t)f[i].abs_y);
923 f[i].rel_x = le16toh((uint16_t)f[i].rel_x);
924 f[i].rel_y = le16toh((uint16_t)f[i].rel_y);
925 f[i].tool_major = le16toh((uint16_t)f[i].tool_major);
926 f[i].tool_minor = le16toh((uint16_t)f[i].tool_minor);
927 f[i].orientation = le16toh((uint16_t)f[i].orientation);
928 f[i].touch_major = le16toh((uint16_t)f[i].touch_major);
929 f[i].touch_minor = le16toh((uint16_t)f[i].touch_minor);
930 f[i].multi = le16toh((uint16_t)f[i].multi);
932 DPRINTFN(WSP_LLEVEL_INFO, "[%d]ibt=%d, taps=%d, u=%x, o=%4d, ax=%5d, ay=%5d, "
933 "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%5d, tchmaj=%4d, tchmin=%4d, m=%4x\n",
934 i, ibt, ntouch, h->q2,
935 f[i].origin, f[i].abs_x, f[i].abs_y, f[i].rel_x, f[i].rel_y,
936 f[i].tool_major, f[i].tool_minor, f[i].orientation,
937 f[i].touch_major, f[i].touch_minor, f[i].multi);
939 if (f[i].touch_major < tun.pressure_untouch_threshold)
942 sc->pos_x[n] = f[i].abs_x;
943 sc->pos_y[n] = params->y.min + params->y.max - f[i].abs_y;
944 sc->index[n] = &f[i];
948 sc->sc_status.flags &= ~MOUSE_POSCHANGED;
949 sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED;
950 sc->sc_status.obutton = sc->sc_status.button;
951 sc->sc_status.button = 0;
954 sc->sc_status.button |= MOUSE_BUTTON1DOWN;
963 if (f[0].touch_major > tun.pressure_tap_threshold)
967 if (f[0].touch_major > tun.pressure_tap_threshold &&
968 f[1].touch_major > tun.pressure_tap_threshold)
972 if (f[0].touch_major > tun.pressure_tap_threshold &&
973 f[1].touch_major > tun.pressure_tap_threshold &&
974 f[2].touch_major > tun.pressure_tap_threshold)
982 sc->distance = max(sc->distance, max(
983 abs(sc->pos_x[0] - sc->pos_x[1]),
984 abs(sc->pos_y[0] - sc->pos_y[1])));
986 if (f[0].touch_major < tun.pressure_untouch_threshold &&
987 sc->sc_status.button == 0) {
988 sc->sc_touch = WSP_UNTOUCH;
989 if (sc->intr_count < WSP_TAP_MAX_COUNT &&
990 sc->intr_count > WSP_TAP_THRESHOLD &&
991 sc->ntaps && sc->ibtn == 0) {
993 * Add a pair of events (button-down and
999 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN);
1000 DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n");
1004 if (sc->distance < MAX_DISTANCE)
1005 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN);
1006 DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n");
1009 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN);
1012 /* we don't handle taps of more than three fingers */
1015 wsp_add_to_queue(sc, 0, 0, 0, 0); /* button release */
1017 if (sc->intr_count >= WSP_TAP_MAX_COUNT &&
1018 (sc->dt_sum / tun.scr_hor_threshold) != 0 &&
1019 sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) {
1022 * translate T-axis into button presses
1026 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3);
1027 else if (sc->dt_sum < 0)
1028 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4);
1030 sc->dz_count = WSP_DZ_MAX_COUNT;
1040 sc->scr_mode = WSP_SCR_NONE;
1041 } else if (f[0].touch_major >= tun.pressure_touch_threshold &&
1042 sc->sc_touch == WSP_UNTOUCH) { /* ignore first touch */
1043 sc->sc_touch = WSP_FIRST_TOUCH;
1044 } else if (f[0].touch_major >= tun.pressure_touch_threshold &&
1045 sc->sc_touch == WSP_FIRST_TOUCH) { /* ignore second touch */
1046 sc->sc_touch = WSP_SECOND_TOUCH;
1047 DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n",
1048 sc->pre_pos_x, sc->pre_pos_y);
1050 if (sc->sc_touch == WSP_SECOND_TOUCH)
1051 sc->sc_touch = WSP_TOUCHING;
1055 f[0].touch_major >= tun.pressure_touch_threshold) {
1056 dx = sc->pos_x[0] - sc->pre_pos_x;
1057 dy = sc->pos_y[0] - sc->pre_pos_y;
1058 if (n == 2 && sc->sc_status.button != 0) {
1059 dx = sc->pos_x[sc->finger] - sc->pre_pos_x;
1060 dy = sc->pos_y[sc->finger] - sc->pre_pos_y;
1061 if (f[0].origin == 0 || f[1].origin == 0) {
1066 if ((abs(f[0].rel_x) + abs(f[0].rel_y)) <
1067 (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
1069 sc->sc_touch = WSP_SECOND_TOUCH;
1074 if ((abs(f[0].rel_x) + abs(f[0].rel_y)) >=
1075 (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
1077 sc->sc_touch = WSP_SECOND_TOUCH;
1082 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n",
1083 dx, dy, sc->finger);
1086 sc->dz_sum -= (dy / tun.scale_factor);
1087 if ((sc->dz_sum / tun.z_factor) != 0)
1090 dx /= tun.scale_factor;
1091 dy /= tun.scale_factor;
1095 if (n == 2 && sc->sc_status.button == 0) {
1096 if (sc->scr_mode == WSP_SCR_NONE &&
1097 abs(sc->dx_sum) + abs(sc->dy_sum) > 50)
1098 sc->scr_mode = abs(sc->dx_sum) >
1099 abs(sc->dy_sum) ? WSP_SCR_HOR :
1101 DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n",
1102 sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum);
1103 if (sc->scr_mode == WSP_SCR_HOR)
1110 if (sc->dz_count == 0)
1111 dz = sc->dz_sum / tun.z_factor;
1112 if (abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE ||
1113 abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE)
1116 if (sc->intr_count < WSP_TAP_MAX_COUNT &&
1117 abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3) {
1120 sc->intr_count = WSP_TAP_MAX_COUNT;
1122 sc->sc_status.flags |= MOUSE_POSCHANGED;
1123 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n",
1124 dx, dy, dz, sc->sc_touch, sc->sc_status.button);
1125 sc->sc_status.dx += dx;
1126 sc->sc_status.dy += dy;
1127 sc->sc_status.dz += dz;
1129 wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button);
1130 if (sc->dz_count == 0)
1134 sc->pre_pos_x = sc->pos_x[0];
1135 sc->pre_pos_y = sc->pos_y[0];
1137 if (n == 2 && sc->sc_status.button != 0) {
1138 sc->pre_pos_x = sc->pos_x[sc->finger];
1139 sc->pre_pos_y = sc->pos_y[sc->finger];
1143 /* check if we can put more data into the FIFO */
1144 if (usb_fifo_put_bytes_max(
1145 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
1146 usbd_xfer_set_frame_len(xfer, 0,
1147 sc->sc_params->tp_datalen);
1148 usbd_transfer_submit(xfer);
1152 default: /* Error */
1153 if (error != USB_ERR_CANCELLED) {
1154 /* try clear stall first */
1155 usbd_xfer_set_stall(xfer);
1165 wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz,
1166 uint32_t buttons_in)
1168 uint32_t buttons_out;
1172 dx = imax(dx, -256);
1174 dy = imax(dy, -256);
1176 dz = imax(dz, -128);
1178 buttons_out = MOUSE_MSC_BUTTONS;
1179 if (buttons_in & MOUSE_BUTTON1DOWN)
1180 buttons_out &= ~MOUSE_MSC_BUTTON1UP;
1181 else if (buttons_in & MOUSE_BUTTON2DOWN)
1182 buttons_out &= ~MOUSE_MSC_BUTTON2UP;
1183 else if (buttons_in & MOUSE_BUTTON3DOWN)
1184 buttons_out &= ~MOUSE_MSC_BUTTON3UP;
1186 /* Encode the mouse data in standard format; refer to mouse(4) */
1187 buf[0] = sc->sc_mode.syncmask[1];
1188 buf[0] |= buttons_out;
1191 buf[3] = dx - (dx >> 1);
1192 buf[4] = dy - (dy >> 1);
1193 /* Encode extra bytes for level 1 */
1194 if (sc->sc_mode.level == 1) {
1195 buf[5] = dz >> 1; /* dz / 2 */
1196 buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */
1197 buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS);
1199 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
1200 sc->sc_mode.packetsize, 1);
1204 wsp_reset_buf(struct wsp_softc *sc)
1206 /* reset read queue */
1207 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
1211 wsp_start_read(struct usb_fifo *fifo)
1213 struct wsp_softc *sc = usb_fifo_softc(fifo);
1216 /* Check if we should override the default polling interval */
1217 rate = sc->sc_pollrate;
1218 /* Range check rate */
1221 /* Check for set rate */
1222 if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) {
1223 /* Stop current transfer, if any */
1224 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1225 /* Set new interval */
1226 usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate);
1227 /* Only set pollrate once */
1228 sc->sc_pollrate = 0;
1230 usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]);
1234 wsp_stop_read(struct usb_fifo *fifo)
1236 struct wsp_softc *sc = usb_fifo_softc(fifo);
1238 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1243 wsp_open(struct usb_fifo *fifo, int fflags)
1245 DPRINTFN(WSP_LLEVEL_INFO, "\n");
1247 if (fflags & FREAD) {
1248 struct wsp_softc *sc = usb_fifo_softc(fifo);
1251 if (sc->sc_state & WSP_ENABLED)
1254 if (usb_fifo_alloc_buffer(fifo,
1255 WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) {
1258 rc = wsp_enable(sc);
1260 usb_fifo_free_buffer(fifo);
1268 wsp_close(struct usb_fifo *fifo, int fflags)
1270 if (fflags & FREAD) {
1271 struct wsp_softc *sc = usb_fifo_softc(fifo);
1274 usb_fifo_free_buffer(fifo);
1279 wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1281 struct wsp_softc *sc = usb_fifo_softc(fifo);
1285 mtx_lock(&sc->sc_mutex);
1288 case MOUSE_GETHWINFO:
1289 *(mousehw_t *)addr = sc->sc_hw;
1292 *(mousemode_t *)addr = sc->sc_mode;
1295 mode = *(mousemode_t *)addr;
1297 if (mode.level == -1)
1298 /* Don't change the current setting */
1300 else if ((mode.level < 0) || (mode.level > 1)) {
1304 sc->sc_mode.level = mode.level;
1305 sc->sc_pollrate = mode.rate;
1306 sc->sc_hw.buttons = 3;
1308 if (sc->sc_mode.level == 0) {
1309 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1310 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1311 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1312 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1313 } else if (sc->sc_mode.level == 1) {
1314 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1315 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1316 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1317 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1321 case MOUSE_GETLEVEL:
1322 *(int *)addr = sc->sc_mode.level;
1324 case MOUSE_SETLEVEL:
1325 if (*(int *)addr < 0 || *(int *)addr > 1) {
1329 sc->sc_mode.level = *(int *)addr;
1330 sc->sc_hw.buttons = 3;
1332 if (sc->sc_mode.level == 0) {
1333 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1334 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1335 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1336 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1337 } else if (sc->sc_mode.level == 1) {
1338 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1339 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1340 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1341 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1345 case MOUSE_GETSTATUS:{
1346 mousestatus_t *status = (mousestatus_t *)addr;
1348 *status = sc->sc_status;
1349 sc->sc_status.obutton = sc->sc_status.button;
1350 sc->sc_status.button = 0;
1351 sc->sc_status.dx = 0;
1352 sc->sc_status.dy = 0;
1353 sc->sc_status.dz = 0;
1355 if (status->dx || status->dy || status->dz)
1356 status->flags |= MOUSE_POSCHANGED;
1357 if (status->button != status->obutton)
1358 status->flags |= MOUSE_BUTTONSCHANGED;
1366 mtx_unlock(&sc->sc_mutex);
1370 static device_method_t wsp_methods[] = {
1371 /* Device interface */
1372 DEVMETHOD(device_probe, wsp_probe),
1373 DEVMETHOD(device_attach, wsp_attach),
1374 DEVMETHOD(device_detach, wsp_detach),
1378 static driver_t wsp_driver = {
1379 .name = WSP_DRIVER_NAME,
1380 .methods = wsp_methods,
1381 .size = sizeof(struct wsp_softc)
1384 static devclass_t wsp_devclass;
1386 DRIVER_MODULE(wsp, uhub, wsp_driver, wsp_devclass, NULL, 0);
1387 MODULE_DEPEND(wsp, usb, 1, 1, 1);
1388 MODULE_VERSION(wsp, 1);