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 = 100,
98 .scr_hor_threshold = 10,
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 */
404 int rdx; /* x axis remainder of divide by scale_factor */
405 int rdy; /* y axis remainder of divide by scale_factor */
406 int rdz; /* z axis remainder of divide by scale_factor */
408 uint8_t o_ntouch; /* old touch finger status */
409 uint8_t finger; /* 0 or 1 *, check which finger moving */
411 #define WSP_TAP_THRESHOLD 3
412 #define WSP_TAP_MAX_COUNT 20
413 int distance; /* the distance of 2 fingers */
414 #define MAX_DISTANCE 2500 /* the max allowed distance */
415 uint8_t ibtn; /* button status in tapping */
416 uint8_t ntaps; /* finger status in tapping */
417 uint8_t scr_mode; /* scroll status in movement */
418 #define WSP_SCR_NONE 0
419 #define WSP_SCR_VER 1
420 #define WSP_SCR_HOR 2
421 uint8_t tp_data[WSP_BUFFER_MAX] __aligned(4); /* trackpad transferred data */
424 typedef enum interface_mode {
425 RAW_SENSOR_MODE = 0x01,
430 * function prototypes
432 static usb_fifo_cmd_t wsp_start_read;
433 static usb_fifo_cmd_t wsp_stop_read;
434 static usb_fifo_open_t wsp_open;
435 static usb_fifo_close_t wsp_close;
436 static usb_fifo_ioctl_t wsp_ioctl;
438 static struct usb_fifo_methods wsp_fifo_methods = {
440 .f_close = &wsp_close,
441 .f_ioctl = &wsp_ioctl,
442 .f_start_read = &wsp_start_read,
443 .f_stop_read = &wsp_stop_read,
444 .basename[0] = WSP_DRIVER_NAME,
447 /* device initialization and shutdown */
448 static int wsp_enable(struct wsp_softc *sc);
449 static void wsp_disable(struct wsp_softc *sc);
452 static void wsp_reset_buf(struct wsp_softc *sc);
453 static void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t);
455 /* Device methods. */
456 static device_probe_t wsp_probe;
457 static device_attach_t wsp_attach;
458 static device_detach_t wsp_detach;
459 static usb_callback_t wsp_intr_callback;
461 static const struct usb_config wsp_config[WSP_N_TRANSFER] = {
463 .type = UE_INTERRUPT,
464 .endpoint = UE_ADDR_ANY,
465 .direction = UE_DIR_IN,
470 .bufsize = WSP_BUFFER_MAX,
471 .callback = &wsp_intr_callback,
476 wsp_set_device_mode(struct wsp_softc *sc, interface_mode mode)
478 uint8_t mode_bytes[8];
481 err = usbd_req_get_report(sc->sc_usb_device, NULL,
482 mode_bytes, sizeof(mode_bytes), 0,
485 if (err != USB_ERR_NORMAL_COMPLETION) {
486 DPRINTF("Failed to read device mode (%d)\n", err);
491 * XXX Need to wait at least 250ms for hardware to get
492 * ready. The device mode handling appears to be handled
493 * asynchronously and we should not issue these commands too
496 pause("WHW", hz / 4);
498 mode_bytes[0] = mode;
500 return (usbd_req_set_report(sc->sc_usb_device, NULL,
501 mode_bytes, sizeof(mode_bytes), 0,
506 wsp_enable(struct wsp_softc *sc)
509 memset(&sc->sc_status, 0, sizeof(sc->sc_status));
510 sc->sc_state |= WSP_ENABLED;
512 DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n");
517 wsp_disable(struct wsp_softc *sc)
519 sc->sc_state &= ~WSP_ENABLED;
520 DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n");
524 wsp_probe(device_t self)
526 struct usb_attach_arg *uaa = device_get_ivars(self);
528 if (uaa->usb_mode != USB_MODE_HOST)
531 if (uaa->info.bIfaceIndex != WSP_IFACE_INDEX)
534 if ((uaa->info.bInterfaceClass != UICLASS_HID) ||
535 (uaa->info.bInterfaceProtocol != 0))
538 return (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa));
542 wsp_attach(device_t dev)
544 struct wsp_softc *sc = device_get_softc(dev);
545 struct usb_attach_arg *uaa = device_get_ivars(dev);
550 DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc);
552 /* Get HID descriptor */
553 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
554 &d_len, M_TEMP, uaa->info.bIfaceIndex);
556 if (err == USB_ERR_NORMAL_COMPLETION) {
557 /* Get HID report descriptor length */
558 sc->tp_datalen = hid_report_size(d_ptr, d_len, hid_input, NULL);
561 if (sc->tp_datalen <= 0 || sc->tp_datalen > WSP_BUFFER_MAX) {
562 DPRINTF("Invalid datalength or too big "
563 "datalength: %d\n", sc->tp_datalen);
570 sc->sc_usb_device = uaa->device;
573 * By default the touchpad behaves like a HID device, sending
574 * packets with reportID = 8. Such reports contain only
575 * limited information. They encode movement deltas and button
576 * events, but do not include data from the pressure
577 * sensors. The device input mode can be switched from HID
578 * reports to raw sensor data using vendor-specific USB
583 * During re-enumeration of the device we need to force the
584 * device back into HID mode before switching it to RAW
585 * mode. Else the device does not work like expected.
587 err = wsp_set_device_mode(sc, HID_MODE);
588 if (err != USB_ERR_NORMAL_COMPLETION) {
589 DPRINTF("Failed to set mode to HID MODE (%d)\n", err);
593 err = wsp_set_device_mode(sc, RAW_SENSOR_MODE);
594 if (err != USB_ERR_NORMAL_COMPLETION) {
595 DPRINTF("failed to set mode to RAW MODE (%d)\n", err);
599 mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE);
601 /* get device specific configuration */
602 sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa);
604 err = usbd_transfer_setup(uaa->device,
605 &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config,
606 WSP_N_TRANSFER, sc, &sc->sc_mutex);
608 DPRINTF("error=%s\n", usbd_errstr(err));
611 if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex,
612 &wsp_fifo_methods, &sc->sc_fifo,
613 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
614 UID_ROOT, GID_OPERATOR, 0644)) {
617 device_set_usb_desc(dev);
619 sc->sc_hw.buttons = 3;
620 sc->sc_hw.iftype = MOUSE_IF_USB;
621 sc->sc_hw.type = MOUSE_PAD;
622 sc->sc_hw.model = MOUSE_MODEL_GENERIC;
623 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
624 sc->sc_mode.rate = -1;
625 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
626 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
627 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
628 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
630 sc->sc_touch = WSP_UNTOUCH;
631 sc->scr_mode = WSP_SCR_NONE;
641 wsp_detach(device_t dev)
643 struct wsp_softc *sc = device_get_softc(dev);
645 (void) wsp_set_device_mode(sc, HID_MODE);
647 mtx_lock(&sc->sc_mutex);
648 if (sc->sc_state & WSP_ENABLED)
650 mtx_unlock(&sc->sc_mutex);
652 usb_fifo_detach(&sc->sc_fifo);
654 usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER);
656 mtx_destroy(&sc->sc_mutex);
662 wsp_intr_callback(struct usb_xfer *xfer, usb_error_t error)
664 struct wsp_softc *sc = usbd_xfer_softc(xfer);
665 const struct wsp_dev_params *params = sc->sc_params;
666 struct usb_page_cache *pc;
669 struct wsp_tuning tun = wsp_tuning;
670 int ntouch = 0; /* the finger number in touch */
671 int ibt = 0; /* button status */
681 wsp_runing_rangecheck(&tun);
683 if (sc->dz_count == 0)
684 sc->dz_count = WSP_DZ_MAX_COUNT;
686 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
688 switch (USB_GET_STATE(xfer)) {
689 case USB_ST_TRANSFERRED:
691 /* copy out received data */
692 pc = usbd_xfer_get_frame(xfer, 0);
693 usbd_copy_out(pc, 0, sc->tp_data, len);
695 if (len < sc->tp_datalen) {
696 /* make sure we don't process old data */
697 memset(sc->tp_data + len, 0, sc->tp_datalen - len);
700 h = (struct tp_header *)(sc->tp_data);
702 if (params->tp_type == TYPE2) {
703 ibt = sc->tp_data[BUTTON_TYPE2];
704 ntouch = sc->tp_data[BUTTON_TYPE2 - 1];
705 } else if (params->tp_type == TYPE3) {
706 ibt = sc->tp_data[BUTTON_TYPE3];
707 ntouch = sc->tp_data[BUTTON_TYPE3 - 1];
712 else if (ntouch > MAX_FINGERS)
713 ntouch = MAX_FINGERS;
715 f = (struct tp_finger *)(sc->tp_data + params->tp_offset);
717 for (i = 0; i != ntouch; i++) {
718 /* swap endianness, if any */
719 if (le16toh(0x1234) != 0x1234) {
720 f[i].origin = le16toh((uint16_t)f[i].origin);
721 f[i].abs_x = le16toh((uint16_t)f[i].abs_x);
722 f[i].abs_y = le16toh((uint16_t)f[i].abs_y);
723 f[i].rel_x = le16toh((uint16_t)f[i].rel_x);
724 f[i].rel_y = le16toh((uint16_t)f[i].rel_y);
725 f[i].tool_major = le16toh((uint16_t)f[i].tool_major);
726 f[i].tool_minor = le16toh((uint16_t)f[i].tool_minor);
727 f[i].orientation = le16toh((uint16_t)f[i].orientation);
728 f[i].touch_major = le16toh((uint16_t)f[i].touch_major);
729 f[i].touch_minor = le16toh((uint16_t)f[i].touch_minor);
730 f[i].multi = le16toh((uint16_t)f[i].multi);
732 DPRINTFN(WSP_LLEVEL_INFO, "[%d]ibt=%d, taps=%d, u=%x, o=%4d, ax=%5d, ay=%5d, "
733 "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%5d, tchmaj=%4d, tchmin=%4d, m=%4x\n",
734 i, ibt, ntouch, h->q2,
735 f[i].origin, f[i].abs_x, f[i].abs_y, f[i].rel_x, f[i].rel_y,
736 f[i].tool_major, f[i].tool_minor, f[i].orientation,
737 f[i].touch_major, f[i].touch_minor, f[i].multi);
739 sc->pos_x[i] = f[i].abs_x;
740 sc->pos_y[i] = -f[i].abs_y;
741 sc->index[i] = &f[i];
744 sc->sc_status.flags &= ~MOUSE_POSCHANGED;
745 sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED;
746 sc->sc_status.obutton = sc->sc_status.button;
747 sc->sc_status.button = 0;
750 sc->sc_status.button |= MOUSE_BUTTON1DOWN;
756 if (sc->ntaps < ntouch) {
759 if (f[0].touch_major > tun.pressure_tap_threshold)
763 if (f[0].touch_major > tun.pressure_tap_threshold &&
764 f[1].touch_major > tun.pressure_tap_threshold)
768 if (f[0].touch_major > tun.pressure_tap_threshold &&
769 f[1].touch_major > tun.pressure_tap_threshold &&
770 f[2].touch_major > tun.pressure_tap_threshold)
778 sc->distance = max(sc->distance, max(
779 abs(sc->pos_x[0] - sc->pos_x[1]),
780 abs(sc->pos_y[0] - sc->pos_y[1])));
782 if (f[0].touch_major < tun.pressure_untouch_threshold &&
783 sc->sc_status.button == 0) {
784 sc->sc_touch = WSP_UNTOUCH;
785 if (sc->intr_count < WSP_TAP_MAX_COUNT &&
786 sc->intr_count > WSP_TAP_THRESHOLD &&
787 sc->ntaps && sc->ibtn == 0) {
789 * Add a pair of events (button-down and
794 if (!(params->caps & HAS_INTEGRATED_BUTTON)) {
795 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN);
796 DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n");
800 DPRINTFN(WSP_LLEVEL_INFO, "sum_x=%5d, sum_y=%5d\n",
801 sc->dx_sum, sc->dy_sum);
802 if (sc->distance < MAX_DISTANCE && abs(sc->dx_sum) < 5 &&
803 abs(sc->dy_sum) < 5) {
804 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN);
805 DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n");
809 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN);
812 /* we don't handle taps of more than three fingers */
815 wsp_add_to_queue(sc, 0, 0, 0, 0); /* button release */
817 if ((sc->dt_sum / tun.scr_hor_threshold) != 0 &&
818 sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) {
821 * translate T-axis into button presses
825 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3);
826 else if (sc->dt_sum < 0)
827 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4);
829 sc->dz_count = WSP_DZ_MAX_COUNT;
842 sc->scr_mode = WSP_SCR_NONE;
843 } else if (f[0].touch_major >= tun.pressure_touch_threshold &&
844 sc->sc_touch == WSP_UNTOUCH) { /* ignore first touch */
845 sc->sc_touch = WSP_FIRST_TOUCH;
846 } else if (f[0].touch_major >= tun.pressure_touch_threshold &&
847 sc->sc_touch == WSP_FIRST_TOUCH) { /* ignore second touch */
848 sc->sc_touch = WSP_SECOND_TOUCH;
849 DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n",
850 sc->pre_pos_x, sc->pre_pos_y);
852 if (sc->sc_touch == WSP_SECOND_TOUCH)
853 sc->sc_touch = WSP_TOUCHING;
857 f[0].touch_major >= tun.pressure_touch_threshold) {
858 dx = sc->pos_x[0] - sc->pre_pos_x;
859 dy = sc->pos_y[0] - sc->pre_pos_y;
861 /* Ignore movement from ibt=1 to ibt=0 */
862 if (sc->sc_status.obutton != 0 &&
863 sc->sc_status.button == 0) {
867 /* Ignore movement if ntouch changed */
868 if (sc->o_ntouch != ntouch) {
873 if (ntouch == 2 && sc->sc_status.button != 0) {
874 dx = sc->pos_x[sc->finger] - sc->pre_pos_x;
875 dy = sc->pos_y[sc->finger] - sc->pre_pos_y;
878 * Ignore movement of switch finger or
879 * movement from ibt=0 to ibt=1
881 if (f[0].origin == 0 || f[1].origin == 0 ||
882 sc->sc_status.obutton != sc->sc_status.button) {
887 if ((abs(f[0].rel_x) + abs(f[0].rel_y)) <
888 (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
890 sc->sc_touch = WSP_SECOND_TOUCH;
895 if ((abs(f[0].rel_x) + abs(f[0].rel_y)) >=
896 (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
898 sc->sc_touch = WSP_SECOND_TOUCH;
903 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n",
906 if (sc->dz_count--) {
907 rdz = (dy + sc->rdz) % tun.scale_factor;
908 sc->dz_sum -= (dy + sc->rdz) / tun.scale_factor;
911 if ((sc->dz_sum / tun.z_factor) != 0)
914 rdx = (dx + sc->rdx) % tun.scale_factor;
915 dx = (dx + sc->rdx) / tun.scale_factor;
918 rdy = (dy + sc->rdy) % tun.scale_factor;
919 dy = (dy + sc->rdy) / tun.scale_factor;
925 if (ntouch == 2 && sc->sc_status.button == 0) {
926 if (sc->scr_mode == WSP_SCR_NONE &&
927 abs(sc->dx_sum) + abs(sc->dy_sum) > tun.scr_hor_threshold)
928 sc->scr_mode = abs(sc->dx_sum) >
929 abs(sc->dy_sum) * 3 ? WSP_SCR_HOR :
931 DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n",
932 sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum);
933 if (sc->scr_mode == WSP_SCR_HOR)
940 if (sc->dz_count == 0)
941 dz = sc->dz_sum / tun.z_factor;
942 if (sc->scr_mode == WSP_SCR_HOR ||
943 abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE ||
944 abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE)
952 if (sc->intr_count < WSP_TAP_MAX_COUNT &&
953 abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3) {
956 sc->intr_count = WSP_TAP_MAX_COUNT;
958 sc->sc_status.flags |= MOUSE_POSCHANGED;
959 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n",
960 dx, dy, dz, sc->sc_touch, sc->sc_status.button);
961 sc->sc_status.dx += dx;
962 sc->sc_status.dy += dy;
963 sc->sc_status.dz += dz;
965 wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button);
966 if (sc->dz_count == 0) {
972 sc->pre_pos_x = sc->pos_x[0];
973 sc->pre_pos_y = sc->pos_y[0];
975 if (ntouch == 2 && sc->sc_status.button != 0) {
976 sc->pre_pos_x = sc->pos_x[sc->finger];
977 sc->pre_pos_y = sc->pos_y[sc->finger];
979 sc->o_ntouch = ntouch;
983 /* check if we can put more data into the FIFO */
984 if (usb_fifo_put_bytes_max(
985 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
986 usbd_xfer_set_frame_len(xfer, 0,
988 usbd_transfer_submit(xfer);
993 if (error != USB_ERR_CANCELLED) {
994 /* try clear stall first */
995 usbd_xfer_set_stall(xfer);
1003 wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz,
1004 uint32_t buttons_in)
1006 uint32_t buttons_out;
1010 dx = imax(dx, -256);
1012 dy = imax(dy, -256);
1014 dz = imax(dz, -128);
1016 buttons_out = MOUSE_MSC_BUTTONS;
1017 if (buttons_in & MOUSE_BUTTON1DOWN)
1018 buttons_out &= ~MOUSE_MSC_BUTTON1UP;
1019 else if (buttons_in & MOUSE_BUTTON2DOWN)
1020 buttons_out &= ~MOUSE_MSC_BUTTON2UP;
1021 else if (buttons_in & MOUSE_BUTTON3DOWN)
1022 buttons_out &= ~MOUSE_MSC_BUTTON3UP;
1024 /* Encode the mouse data in standard format; refer to mouse(4) */
1025 buf[0] = sc->sc_mode.syncmask[1];
1026 buf[0] |= buttons_out;
1029 buf[3] = dx - (dx >> 1);
1030 buf[4] = dy - (dy >> 1);
1031 /* Encode extra bytes for level 1 */
1032 if (sc->sc_mode.level == 1) {
1033 buf[5] = dz >> 1; /* dz / 2 */
1034 buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */
1035 buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS);
1037 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
1038 sc->sc_mode.packetsize, 1);
1042 wsp_reset_buf(struct wsp_softc *sc)
1044 /* reset read queue */
1045 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
1049 wsp_start_read(struct usb_fifo *fifo)
1051 struct wsp_softc *sc = usb_fifo_softc(fifo);
1054 /* Check if we should override the default polling interval */
1055 rate = sc->sc_pollrate;
1056 /* Range check rate */
1059 /* Check for set rate */
1060 if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) {
1061 /* Stop current transfer, if any */
1062 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1063 /* Set new interval */
1064 usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate);
1065 /* Only set pollrate once */
1066 sc->sc_pollrate = 0;
1068 usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]);
1072 wsp_stop_read(struct usb_fifo *fifo)
1074 struct wsp_softc *sc = usb_fifo_softc(fifo);
1076 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1081 wsp_open(struct usb_fifo *fifo, int fflags)
1083 DPRINTFN(WSP_LLEVEL_INFO, "\n");
1085 if (fflags & FREAD) {
1086 struct wsp_softc *sc = usb_fifo_softc(fifo);
1089 if (sc->sc_state & WSP_ENABLED)
1092 if (usb_fifo_alloc_buffer(fifo,
1093 WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) {
1096 rc = wsp_enable(sc);
1098 usb_fifo_free_buffer(fifo);
1106 wsp_close(struct usb_fifo *fifo, int fflags)
1108 if (fflags & FREAD) {
1109 struct wsp_softc *sc = usb_fifo_softc(fifo);
1112 usb_fifo_free_buffer(fifo);
1117 wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1119 struct wsp_softc *sc = usb_fifo_softc(fifo);
1123 mtx_lock(&sc->sc_mutex);
1126 case MOUSE_GETHWINFO:
1127 *(mousehw_t *)addr = sc->sc_hw;
1130 *(mousemode_t *)addr = sc->sc_mode;
1133 mode = *(mousemode_t *)addr;
1135 if (mode.level == -1)
1136 /* Don't change the current setting */
1138 else if ((mode.level < 0) || (mode.level > 1)) {
1142 sc->sc_mode.level = mode.level;
1143 sc->sc_pollrate = mode.rate;
1144 sc->sc_hw.buttons = 3;
1146 if (sc->sc_mode.level == 0) {
1147 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1148 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1149 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1150 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1151 } else if (sc->sc_mode.level == 1) {
1152 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1153 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1154 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1155 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1159 case MOUSE_GETLEVEL:
1160 *(int *)addr = sc->sc_mode.level;
1162 case MOUSE_SETLEVEL:
1163 if (*(int *)addr < 0 || *(int *)addr > 1) {
1167 sc->sc_mode.level = *(int *)addr;
1168 sc->sc_hw.buttons = 3;
1170 if (sc->sc_mode.level == 0) {
1171 sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1172 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1173 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1174 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1175 } else if (sc->sc_mode.level == 1) {
1176 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1177 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1178 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1179 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1183 case MOUSE_GETSTATUS:{
1184 mousestatus_t *status = (mousestatus_t *)addr;
1186 *status = sc->sc_status;
1187 sc->sc_status.obutton = sc->sc_status.button;
1188 sc->sc_status.button = 0;
1189 sc->sc_status.dx = 0;
1190 sc->sc_status.dy = 0;
1191 sc->sc_status.dz = 0;
1193 if (status->dx || status->dy || status->dz)
1194 status->flags |= MOUSE_POSCHANGED;
1195 if (status->button != status->obutton)
1196 status->flags |= MOUSE_BUTTONSCHANGED;
1204 mtx_unlock(&sc->sc_mutex);
1208 static device_method_t wsp_methods[] = {
1209 /* Device interface */
1210 DEVMETHOD(device_probe, wsp_probe),
1211 DEVMETHOD(device_attach, wsp_attach),
1212 DEVMETHOD(device_detach, wsp_detach),
1216 static driver_t wsp_driver = {
1217 .name = WSP_DRIVER_NAME,
1218 .methods = wsp_methods,
1219 .size = sizeof(struct wsp_softc)
1222 static devclass_t wsp_devclass;
1224 DRIVER_MODULE(wsp, uhub, wsp_driver, wsp_devclass, NULL, 0);
1225 MODULE_DEPEND(wsp, usb, 1, 1, 1);
1226 MODULE_VERSION(wsp, 1);