diff options
author | hselasky <hselasky@FreeBSD.org> | 2014-01-29 10:42:01 +0000 |
---|---|---|
committer | hselasky <hselasky@FreeBSD.org> | 2014-01-29 10:42:01 +0000 |
commit | fdb5e5e5abccd369618e13d70075d2c5e5d5a70f (patch) | |
tree | 2097a6a726283d882836849adf243945388ce2d8 | |
parent | f8038f60cdbb0cfc0737893790a54ac9024416b7 (diff) | |
download | FreeBSD-src-fdb5e5e5abccd369618e13d70075d2c5e5d5a70f.zip FreeBSD-src-fdb5e5e5abccd369618e13d70075d2c5e5d5a70f.tar.gz |
Add support for trackpads found in Apple MacBook products. While at it
add some missing devd entries.
Submitted by: Huang Wen Hui <huanghwh@gmail.com>
MFC after: 1 week
-rw-r--r-- | etc/devd.conf | 18 | ||||
-rw-r--r-- | sys/conf/NOTES | 3 | ||||
-rw-r--r-- | sys/conf/files | 1 | ||||
-rw-r--r-- | sys/dev/usb/input/wsp.c | 1438 | ||||
-rw-r--r-- | sys/dev/usb/usbdevs | 54 | ||||
-rw-r--r-- | sys/modules/usb/Makefile | 2 | ||||
-rw-r--r-- | sys/modules/usb/wsp/Makefile | 36 |
7 files changed, 1545 insertions, 7 deletions
diff --git a/etc/devd.conf b/etc/devd.conf index 5339bae..0c70817 100644 --- a/etc/devd.conf +++ b/etc/devd.conf @@ -119,6 +119,15 @@ notify 100 { match "system" "DEVFS"; match "subsystem" "CDEV"; match "type" "CREATE"; + match "cdev" "atp[0-9]+"; + + action "/etc/rc.d/moused quietstart $cdev"; +}; + +notify 100 { + match "system" "DEVFS"; + match "subsystem" "CDEV"; + match "type" "CREATE"; match "cdev" "ums[0-9]+"; action "/etc/rc.d/moused quietstart $cdev"; @@ -127,6 +136,15 @@ notify 100 { notify 100 { match "system" "DEVFS"; match "subsystem" "CDEV"; + match "type" "CREATE"; + match "cdev" "wsp[0-9]+"; + + action "/etc/rc.d/moused quietstart $cdev"; +}; + +notify 100 { + match "system" "DEVFS"; + match "subsystem" "CDEV"; match "type" "DESTROY"; match "cdev" "ums[0-9]+"; diff --git a/sys/conf/NOTES b/sys/conf/NOTES index 9d74130..8095c0e 100644 --- a/sys/conf/NOTES +++ b/sys/conf/NOTES @@ -2660,6 +2660,9 @@ device umct device umodem # USB mouse device ums +# USB touchpad(s) +device atp +device wsp # eGalax USB touch screen device uep # Diamond Rio 500 MP3 player diff --git a/sys/conf/files b/sys/conf/files index 33fc75d..46e669f 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -2408,6 +2408,7 @@ dev/usb/input/uep.c optional uep dev/usb/input/uhid.c optional uhid dev/usb/input/ukbd.c optional ukbd dev/usb/input/ums.c optional ums +dev/usb/input/wsp.c optional wsp # # USB quirks # diff --git a/sys/dev/usb/input/wsp.c b/sys/dev/usb/input/wsp.c new file mode 100644 index 0000000..b415ce1 --- /dev/null +++ b/sys/dev/usb/input/wsp.c @@ -0,0 +1,1438 @@ +/*- + * Copyright (c) 2012 Huang Wen Hui + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/malloc.h> +#include <sys/module.h> +#include <sys/lock.h> +#include <sys/mutex.h> +#include <sys/bus.h> +#include <sys/conf.h> +#include <sys/fcntl.h> +#include <sys/file.h> +#include <sys/selinfo.h> +#include <sys/poll.h> +#include <sys/sysctl.h> + +#include <dev/usb/usb.h> +#include <dev/usb/usbdi.h> +#include <dev/usb/usbdi_util.h> +#include <dev/usb/usbhid.h> + +#include "usbdevs.h" + +#define USB_DEBUG_VAR wsp_debug +#include <dev/usb/usb_debug.h> + +#include <sys/mouse.h> + +#define WSP_DRIVER_NAME "wsp" + +#define WSP_CLAMP(x,low,high) do { \ + if ((x) < (low)) \ + (x) = (low); \ + else if ((x) > (high)) \ + (x) = (high); \ +} while (0) + +/* Tunables */ +static SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW, 0, "USB wsp"); + +#ifdef USB_DEBUG +enum wsp_log_level { + WSP_LLEVEL_DISABLED = 0, + WSP_LLEVEL_ERROR, + WSP_LLEVEL_DEBUG, /* for troubleshooting */ + WSP_LLEVEL_INFO, /* for diagnostics */ +}; +static int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */ + +SYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RW, + &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level"); +#endif /* USB_DEBUG */ + +static struct wsp_tuning { + int scale_factor; + int z_factor; + int pressure_touch_threshold; + int pressure_untouch_threshold; + int pressure_tap_threshold; + int scr_hor_threshold; +} + wsp_tuning = +{ + .scale_factor = 12, + .z_factor = 5, + .pressure_touch_threshold = 50, + .pressure_untouch_threshold = 10, + .pressure_tap_threshold = 120, + .scr_hor_threshold = 50, +}; + +static void +wsp_runing_rangecheck(struct wsp_tuning *ptun) +{ + WSP_CLAMP(ptun->scale_factor, 1, 63); + WSP_CLAMP(ptun->z_factor, 1, 63); + WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255); + WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255); + WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255); + WSP_CLAMP(ptun->scr_hor_threshold, 1, 255); +} + +SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RW, + &wsp_tuning.scale_factor, 0, "movement scale factor"); +SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RW, + &wsp_tuning.z_factor, 0, "Z-axis scale factor"); +SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RW, + &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold"); +SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RW, + &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold"); +SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RW, + &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold"); +SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_hor_threshold, CTLFLAG_RW, + &wsp_tuning.scr_hor_threshold, 0, "horizontal scrolling threshold"); + +#define WSP_IFACE_INDEX 1 + +/* button data structure */ +struct bt_data { + uint8_t unknown1; /* constant */ + uint8_t button; /* left button */ + uint8_t rel_x; /* relative x coordinate */ + uint8_t rel_y; /* relative y coordinate */ +} __packed; + +/* trackpad header types */ +enum tp_type { + TYPE1, /* plain trackpad */ + TYPE2, /* button integrated in trackpad */ + TYPE3 /* additional header fields since June 2013 */ +}; + +/* trackpad finger data offsets, le16-aligned */ +#define FINGER_TYPE1 (13 * 2) +#define FINGER_TYPE2 (15 * 2) +#define FINGER_TYPE3 (19 * 2) + +/* trackpad button data offsets */ +#define BUTTON_TYPE2 15 +#define BUTTON_TYPE3 23 + +/* list of device capability bits */ +#define HAS_INTEGRATED_BUTTON 1 + +/* trackpad finger header - little endian */ +struct tp_header { + uint8_t flag; + uint8_t sn0; + uint16_t wFixed0; + uint32_t dwSn1; + uint32_t dwFixed1; + uint16_t wLength; + uint8_t nfinger; + uint8_t ibt; + int16_t wUnknown[6]; + uint8_t q1; + uint8_t q2; +} __packed; + +/* trackpad finger structure - little endian */ +struct tp_finger { + int16_t origin; /* zero when switching track finger */ + int16_t abs_x; /* absolute x coodinate */ + int16_t abs_y; /* absolute y coodinate */ + int16_t rel_x; /* relative x coodinate */ + int16_t rel_y; /* relative y coodinate */ + int16_t tool_major; /* tool area, major axis */ + int16_t tool_minor; /* tool area, minor axis */ + int16_t orientation; /* 16384 when point, else 15 bit angle */ + int16_t touch_major; /* touch area, major axis */ + int16_t touch_minor; /* touch area, minor axis */ + int16_t unused[3]; /* zeros */ + int16_t multi; /* one finger: varies, more fingers: + * constant */ +} __packed; + +/* trackpad finger data size, empirically at least ten fingers */ +#define MAX_FINGERS 16 +#define SIZEOF_FINGER sizeof(struct tp_finger) +#define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER) +#define MAX_FINGER_ORIENTATION 16384 + +/* logical signal quality */ +#define SN_PRESSURE 45 /* pressure signal-to-noise ratio */ +#define SN_WIDTH 25 /* width signal-to-noise ratio */ +#define SN_COORD 250 /* coordinate signal-to-noise ratio */ +#define SN_ORIENT 10 /* orientation signal-to-noise ratio */ + +/* device-specific parameters */ +struct wsp_param { + int snratio; /* signal-to-noise ratio */ + int min; /* device minimum reading */ + int max; /* device maximum reading */ +}; + +enum { + WSP_FLAG_WELLSPRING1, + WSP_FLAG_WELLSPRING2, + WSP_FLAG_WELLSPRING3, + WSP_FLAG_WELLSPRING4, + WSP_FLAG_WELLSPRING4A, + WSP_FLAG_WELLSPRING5, + WSP_FLAG_WELLSPRING6A, + WSP_FLAG_WELLSPRING6, + WSP_FLAG_WELLSPRING5A, + WSP_FLAG_WELLSPRING7, + WSP_FLAG_WELLSPRING7A, + WSP_FLAG_WELLSPRING8, + WSP_FLAG_MAX, +}; + +/* device-specific configuration */ +struct wsp_dev_params { + uint8_t caps; /* device capability bitmask */ + uint16_t bt_datalen; /* data length of the button interface */ + uint8_t tp_type; /* type of trackpad interface */ + uint8_t tp_offset; /* offset to trackpad finger data */ + uint16_t tp_datalen; /* data length of the trackpad + * interface */ + struct wsp_param p; /* finger pressure limits */ + struct wsp_param w; /* finger width limits */ + struct wsp_param x; /* horizontal limits */ + struct wsp_param y; /* vertical limits */ + struct wsp_param o; /* orientation limits */ +}; + +static const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = { + [WSP_FLAG_WELLSPRING1] = { + .caps = 0, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE1, + .tp_offset = FINGER_TYPE1, + .tp_datalen = FINGER_TYPE1 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 256 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4824, 5342 + }, + .y = { + SN_COORD, -172, 5820 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING2] = { + .caps = 0, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE1, + .tp_offset = FINGER_TYPE1, + .tp_datalen = FINGER_TYPE1 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 256 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4824, 4824 + }, + .y = { + SN_COORD, -172, 4290 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING3] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE2, + .tp_offset = FINGER_TYPE2, + .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4460, 5166 + }, + .y = { + SN_COORD, -75, 6700 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING4] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE2, + .tp_offset = FINGER_TYPE2, + .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4620, 5140 + }, + .y = { + SN_COORD, -150, 6600 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING4A] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE2, + .tp_offset = FINGER_TYPE2, + .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4616, 5112 + }, + .y = { + SN_COORD, -142, 5234 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING5] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE2, + .tp_offset = FINGER_TYPE2, + .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4415, 5050 + }, + .y = { + SN_COORD, -55, 6680 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING6] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE2, + .tp_offset = FINGER_TYPE2, + .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4620, 5140 + }, + .y = { + SN_COORD, -150, 6600 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING5A] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE2, + .tp_offset = FINGER_TYPE2, + .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4750, 5280 + }, + .y = { + SN_COORD, -150, 6730 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING6A] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE2, + .tp_offset = FINGER_TYPE2, + .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4620, 5140 + }, + .y = { + SN_COORD, -150, 6600 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING7] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE2, + .tp_offset = FINGER_TYPE2, + .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4750, 5280 + }, + .y = { + SN_COORD, -150, 6730 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING7A] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE2, + .tp_offset = FINGER_TYPE2, + .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4750, 5280 + }, + .y = { + SN_COORD, -150, 6730 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, + [WSP_FLAG_WELLSPRING8] = { + .caps = HAS_INTEGRATED_BUTTON, + .bt_datalen = sizeof(struct bt_data), + .tp_type = TYPE3, + .tp_offset = FINGER_TYPE3, + .tp_datalen = FINGER_TYPE3 + SIZEOF_ALL_FINGERS, + .p = { + SN_PRESSURE, 0, 300 + }, + .w = { + SN_WIDTH, 0, 2048 + }, + .x = { + SN_COORD, -4620, 5140 + }, + .y = { + SN_COORD, -150, 6600 + }, + .o = { + SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION + }, + }, +}; + +#define WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) } + +static const STRUCT_USB_HOST_ID wsp_devs[] = { + /* MacbookAir1.1 */ + WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1), + WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1), + WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1), + + /* MacbookProPenryn, aka wellspring2 */ + WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2), + WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2), + WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2), + + /* Macbook5,1 (unibody), aka wellspring3 */ + WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3), + WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3), + WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3), + + /* MacbookAir3,2 (unibody), aka wellspring4 */ + WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4), + WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4), + WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4), + + /* MacbookAir3,1 (unibody), aka wellspring4 */ + WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A), + WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A), + WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A), + + /* Macbook8 (unibody, March 2011) */ + WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5), + WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5), + WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5), + + /* MacbookAir4,1 (unibody, July 2011) */ + WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A), + WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A), + WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A), + + /* MacbookAir4,2 (unibody, July 2011) */ + WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6), + WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6), + WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6), + + /* Macbook8,2 (unibody) */ + WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A), + WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A), + WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A), + + /* MacbookPro10,1 (unibody, June 2012) */ + /* MacbookPro11,? (unibody, June 2013) */ + WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7), + WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7), + WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7), + + /* MacbookPro10,2 (unibody, October 2012) */ + WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A), + WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A), + WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A), + + /* MacbookAir6,2 (unibody, June 2013) */ + WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8), + WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8), + WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8), +}; + +#define WSP_FIFO_BUF_SIZE 8 /* bytes */ +#define WSP_FIFO_QUEUE_MAXLEN 50 /* units */ + +enum { + WSP_INTR_DT, + WSP_RESET, + WSP_N_TRANSFER, +}; + +struct wsp_softc { + device_t sc_dev; + struct usb_device *sc_usb_device; +#define MODE_LENGTH 8 + uint8_t sc_mode_bytes[MODE_LENGTH]; /* device mode */ + struct mtx sc_mutex; /* for synchronization */ + struct usb_xfer *sc_xfer[WSP_N_TRANSFER]; + struct usb_fifo_sc sc_fifo; + + const struct wsp_dev_params *sc_params; /* device configuration */ + + mousehw_t sc_hw; + mousemode_t sc_mode; + u_int sc_pollrate; + mousestatus_t sc_status; + u_int sc_state; +#define WSP_ENABLED 0x01 + + struct bt_data *bt_data; /* button transferred data */ + uint8_t *tp_data; /* trackpad transferred data */ + struct tp_finger *index[MAX_FINGERS]; /* finger index data */ + int16_t pos_x[MAX_FINGERS]; /* position array */ + int16_t pos_y[MAX_FINGERS]; /* position array */ + u_int sc_touch; /* touch status */ +#define WSP_UNTOUCH 0x00 +#define WSP_FIRST_TOUCH 0x01 +#define WSP_SECOND_TOUCH 0x02 +#define WSP_TOUCHING 0x04 + int16_t pre_pos_x; /* previous position array */ + int16_t pre_pos_y; /* previous position array */ + int dx_sum; /* x axis cumulative movement */ + int dy_sum; /* y axis cumulative movement */ + int dz_sum; /* z axis cumulative movement */ + int dz_count; +#define WSP_DZ_MAX_COUNT 32 + int dt_sum; /* T-axis cumulative movement */ + + uint8_t finger; /* 0 or 1 *, check which finger moving */ + uint16_t intr_count; +#define WSP_TAP_THRESHOLD 3 +#define WSP_TAP_MAX_COUNT 20 + int distance; /* the distance of 2 fingers */ +#define MAX_DISTANCE 2500 /* the max allowed distance */ + uint8_t ibtn; /* button status in tapping */ + uint8_t ntaps; /* finger status in tapping */ + uint8_t scr_mode; /* scroll status in movement */ +#define WSP_SCR_NONE 0 +#define WSP_SCR_VER 1 +#define WSP_SCR_HOR 2 +}; + +typedef enum interface_mode { + RAW_SENSOR_MODE = 0x01, + HID_MODE = 0x08 +} interface_mode; + +/* + * function prototypes + */ +static usb_fifo_cmd_t wsp_start_read; +static usb_fifo_cmd_t wsp_stop_read; +static usb_fifo_open_t wsp_open; +static usb_fifo_close_t wsp_close; +static usb_fifo_ioctl_t wsp_ioctl; + +static struct usb_fifo_methods wsp_fifo_methods = { + .f_open = &wsp_open, + .f_close = &wsp_close, + .f_ioctl = &wsp_ioctl, + .f_start_read = &wsp_start_read, + .f_stop_read = &wsp_stop_read, + .basename[0] = WSP_DRIVER_NAME, +}; + +/* device initialization and shutdown */ +static usb_error_t wsp_req_get_report(struct usb_device *udev, void *data); +static int wsp_set_device_mode(device_t dev, interface_mode mode); +static int wsp_enable(struct wsp_softc *sc); +static void wsp_disable(struct wsp_softc *sc); + +/* updating fifo */ +static void wsp_reset_buf(struct wsp_softc *sc); +static void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t); + +/* Device methods. */ +static device_probe_t wsp_probe; +static device_attach_t wsp_attach; +static device_detach_t wsp_detach; +static usb_callback_t wsp_intr_callback; +static usb_callback_t wsp_reset_callback; + +static struct usb_config wsp_config[WSP_N_TRANSFER] = { + [WSP_INTR_DT] = { + .type = UE_INTERRUPT, + .endpoint = UE_ADDR_ANY, + .direction = UE_DIR_IN, + .flags = { + .pipe_bof = 0, + .short_xfer_ok = 1, + }, + .bufsize = 0, /* use wMaxPacketSize */ + .callback = &wsp_intr_callback, + }, + [WSP_RESET] = { + .type = UE_CONTROL, + .endpoint = 0, /* Control pipe */ + .direction = UE_DIR_ANY, + .bufsize = sizeof(struct usb_device_request) + MODE_LENGTH, + .callback = &wsp_reset_callback, + .interval = 0, /* no pre-delay */ + }, +}; + +usb_error_t +wsp_req_get_report(struct usb_device *udev, void *data) +{ + struct usb_device_request req; + + req.bmRequestType = UT_READ_CLASS_INTERFACE; + req.bRequest = UR_GET_REPORT; + USETW2(req.wValue, (uint8_t)0x03 /* type */ , (uint8_t)0x00 /* id */ ); + USETW(req.wIndex, 0); + USETW(req.wLength, MODE_LENGTH); + + return (usbd_do_request(udev, NULL /* mutex */ , &req, data)); +} + +static int +wsp_set_device_mode(device_t dev, interface_mode mode) +{ + struct wsp_softc *sc; + usb_device_request_t req; + usb_error_t err; + + if ((mode != RAW_SENSOR_MODE) && (mode != HID_MODE)) + return (ENXIO); + + sc = device_get_softc(dev); + + sc->sc_mode_bytes[0] = mode; + req.bmRequestType = UT_WRITE_CLASS_INTERFACE; + req.bRequest = UR_SET_REPORT; + USETW2(req.wValue, (uint8_t)0x03 /* type */ , (uint8_t)0x00 /* id */ ); + USETW(req.wIndex, 0); + USETW(req.wLength, MODE_LENGTH); + err = usbd_do_request(sc->sc_usb_device, NULL, &req, sc->sc_mode_bytes); + if (err != USB_ERR_NORMAL_COMPLETION) + return (ENXIO); + + return (0); +} + +static void +wsp_reset_callback(struct usb_xfer *xfer, usb_error_t error) +{ + usb_device_request_t req; + struct usb_page_cache *pc; + struct wsp_softc *sc = usbd_xfer_softc(xfer); + + switch (USB_GET_STATE(xfer)) { + case USB_ST_SETUP: + sc->sc_mode_bytes[0] = RAW_SENSOR_MODE; + req.bmRequestType = UT_WRITE_CLASS_INTERFACE; + req.bRequest = UR_SET_REPORT; + USETW2(req.wValue, + (uint8_t)0x03 /* type */ , (uint8_t)0x00 /* id */ ); + USETW(req.wIndex, 0); + USETW(req.wLength, MODE_LENGTH); + + pc = usbd_xfer_get_frame(xfer, 0); + usbd_copy_in(pc, 0, &req, sizeof(req)); + pc = usbd_xfer_get_frame(xfer, 1); + usbd_copy_in(pc, 0, sc->sc_mode_bytes, MODE_LENGTH); + + usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); + usbd_xfer_set_frame_len(xfer, 1, MODE_LENGTH); + usbd_xfer_set_frames(xfer, 2); + usbd_transfer_submit(xfer); + break; + + case USB_ST_TRANSFERRED: + default: + break; + } +} + +static int +wsp_enable(struct wsp_softc *sc) +{ + const struct wsp_dev_params *params = sc->sc_params; + + if (params == NULL || params->tp_datalen == 0) { + DPRINTF("params uninitialized!\n"); + return (ENXIO); + } + /* Allocate the dynamic buffers */ + sc->tp_data = malloc(params->tp_datalen, M_USB, M_WAITOK | M_ZERO); + if (sc->tp_data == NULL) { + DPRINTF("Cannot allocate memory\n"); + return (ENXIO); + } + + /* reset status */ + memset(&sc->sc_status, 0, sizeof(sc->sc_status)); + sc->sc_state |= WSP_ENABLED; + + DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n"); + return (0); +} + +static void +wsp_disable(struct wsp_softc *sc) +{ + free(sc->tp_data, M_USB); + sc->tp_data = NULL; + + sc->sc_state &= ~WSP_ENABLED; + DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n"); +} + +static int +wsp_probe(device_t self) +{ + struct usb_attach_arg *uaa = device_get_ivars(self); + + if (uaa->usb_mode != USB_MODE_HOST) + return (ENXIO); + + if (uaa->info.bIfaceIndex != WSP_IFACE_INDEX) + return (ENXIO); + + if ((uaa->info.bInterfaceClass != UICLASS_HID) || + (uaa->info.bInterfaceProtocol != 0)) + return (ENXIO); + + return (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa)); +} + +static int +wsp_attach(device_t dev) +{ + struct wsp_softc *sc = device_get_softc(dev); + struct usb_attach_arg *uaa = device_get_ivars(dev); + usb_error_t err; + + DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc); + + sc->sc_dev = dev; + sc->sc_usb_device = uaa->device; + + /* + * By default the touchpad behaves like an HID device, sending + * packets with reportID = 8. Such reports contain only + * limited information. They encode movement deltas and button + * events, but do not include data from the pressure + * sensors. The device input mode can be switched from HID + * reports to raw sensor data using vendor-specific USB + * control commands; but first the mode must be read. + */ + err = wsp_req_get_report(sc->sc_usb_device, sc->sc_mode_bytes); + if (err != USB_ERR_NORMAL_COMPLETION) { + DPRINTF("failed to read device mode (%d)\n", err); + return (ENXIO); + } + if (wsp_set_device_mode(dev, RAW_SENSOR_MODE) != 0) { + DPRINTF("failed to set mode to 'RAW_SENSOR' (%d)\n", err); + return (ENXIO); + } + mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE); + + /* get device specific configuration */ + sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa); + + /* set to 0 to use wMaxPacketSize is not enough */ + wsp_config[0].bufsize = sc->sc_params->tp_datalen; + + err = usbd_transfer_setup(uaa->device, + &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config, + WSP_N_TRANSFER, sc, &sc->sc_mutex); + + if (err) { + DPRINTF("error=%s\n", usbd_errstr(err)); + goto detach; + } + if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex, + &wsp_fifo_methods, &sc->sc_fifo, + device_get_unit(dev), -1, uaa->info.bIfaceIndex, + UID_ROOT, GID_OPERATOR, 0644)) { + goto detach; + } + device_set_usb_desc(dev); + + sc->sc_hw.buttons = 3; + sc->sc_hw.iftype = MOUSE_IF_USB; + sc->sc_hw.type = MOUSE_PAD; + sc->sc_hw.model = MOUSE_MODEL_GENERIC; + sc->sc_mode.protocol = MOUSE_PROTO_MSC; + sc->sc_mode.rate = -1; + sc->sc_mode.resolution = MOUSE_RES_UNKNOWN; + sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; + sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; + sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; + + sc->sc_touch = WSP_UNTOUCH; + sc->scr_mode = WSP_SCR_NONE; + + return (0); + +detach: + wsp_detach(dev); + return (ENOMEM); +} + +static int +wsp_detach(device_t dev) +{ + struct wsp_softc *sc = device_get_softc(dev); + + wsp_set_device_mode(dev, HID_MODE); + + mtx_lock(&sc->sc_mutex); + if (sc->sc_state & WSP_ENABLED) + wsp_disable(sc); + mtx_unlock(&sc->sc_mutex); + + usb_fifo_detach(&sc->sc_fifo); + + usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER); + + mtx_destroy(&sc->sc_mutex); + + return (0); +} + +static void +wsp_intr_callback(struct usb_xfer *xfer, usb_error_t error) +{ + struct wsp_softc *sc = usbd_xfer_softc(xfer); + const struct wsp_dev_params *params = sc->sc_params; + struct usb_page_cache *pc; + struct tp_finger *f; + struct tp_header *h; + struct wsp_tuning tun = wsp_tuning; + int ntouch = 0; /* the finger number in touch */ + int ibt = 0; /* button status */ + int dx = 0; + int dy = 0; + int dz = 0; + int n = 0; + int len; + int i; + + wsp_runing_rangecheck(&tun); + + if (sc->dz_count == 0) + sc->dz_count = WSP_DZ_MAX_COUNT; + + usbd_xfer_status(xfer, &len, NULL, NULL, NULL); + + switch (USB_GET_STATE(xfer)) { + case USB_ST_TRANSFERRED: + if (len > (int)params->tp_datalen) { + DPRINTFN(WSP_LLEVEL_ERROR, + "truncating large packet from %u to %u bytes\n", + len, params->tp_datalen); + len = params->tp_datalen; + } else { + /* make sure we don't process old data */ + memset(sc->tp_data + len, 0, params->tp_datalen - len); + } + + pc = usbd_xfer_get_frame(xfer, 0); + usbd_copy_out(pc, 0, sc->tp_data, len); + + h = (struct tp_header *)(sc->tp_data); + + if (params->tp_type == TYPE2) { + ibt = sc->tp_data[BUTTON_TYPE2]; + ntouch = sc->tp_data[BUTTON_TYPE2 - 1]; + } else if (params->tp_type == TYPE3) { + ibt = sc->tp_data[BUTTON_TYPE3]; + ntouch = sc->tp_data[BUTTON_TYPE3 - 1]; + } + /* range check */ + if (ntouch < 0) + ntouch = 0; + else if (ntouch > MAX_FINGERS) + ntouch = MAX_FINGERS; + + f = (struct tp_finger *)(sc->tp_data + params->tp_offset); + + for (i = 0; i != ntouch; i++) { + /* swap endianness, if any */ + if (le16toh(0x1234) != 0x1234) { + f[i].origin = le16toh((uint16_t)f[i].origin); + f[i].abs_x = le16toh((uint16_t)f[i].abs_x); + f[i].abs_y = le16toh((uint16_t)f[i].abs_y); + f[i].rel_x = le16toh((uint16_t)f[i].rel_x); + f[i].rel_y = le16toh((uint16_t)f[i].rel_y); + f[i].tool_major = le16toh((uint16_t)f[i].tool_major); + f[i].tool_minor = le16toh((uint16_t)f[i].tool_minor); + f[i].orientation = le16toh((uint16_t)f[i].orientation); + f[i].touch_major = le16toh((uint16_t)f[i].touch_major); + f[i].touch_minor = le16toh((uint16_t)f[i].touch_minor); + f[i].multi = le16toh((uint16_t)f[i].multi); + } + DPRINTFN(WSP_LLEVEL_INFO, "[%d]ibt=%d, taps=%d, u=%x, o=%4d, ax=%5d, ay=%5d, " + "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%5d, tchmaj=%4d, tchmin=%4d, m=%4x\n", + i, ibt, ntouch, h->q2, + f[i].origin, f[i].abs_x, f[i].abs_y, f[i].rel_x, f[i].rel_y, + f[i].tool_major, f[i].tool_minor, f[i].orientation, + f[i].touch_major, f[i].touch_minor, f[i].multi); + + if (f[i].touch_major < tun.pressure_untouch_threshold) + continue; + + sc->pos_x[n] = f[i].abs_x; + sc->pos_y[n] = params->y.min + params->y.max - f[i].abs_y; + sc->index[n] = &f[i]; + n++; + } + + sc->sc_status.flags &= ~MOUSE_POSCHANGED; + sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED; + sc->sc_status.obutton = sc->sc_status.button; + sc->sc_status.button = 0; + + if (ibt != 0) { + sc->sc_status.button |= MOUSE_BUTTON1DOWN; + sc->ibtn = 1; + } + if (h->q2 == 4) + sc->intr_count++; + + if (sc->ntaps < n) { + switch (n) { + case 1: + if (f[0].touch_major > tun.pressure_tap_threshold) + sc->ntaps = 1; + break; + case 2: + if (f[0].touch_major > tun.pressure_tap_threshold && + f[1].touch_major > tun.pressure_tap_threshold) + sc->ntaps = 2; + break; + case 3: + if (f[0].touch_major > tun.pressure_tap_threshold && + f[1].touch_major > tun.pressure_tap_threshold && + f[2].touch_major > tun.pressure_tap_threshold) + sc->ntaps = 3; + break; + default: + break; + } + } + if (n == 2) { + sc->distance = max(sc->distance, max( + abs(sc->pos_x[0] - sc->pos_x[1]), + abs(sc->pos_y[0] - sc->pos_y[1]))); + } + if (f[0].touch_major < tun.pressure_untouch_threshold && + sc->sc_status.button == 0) { + sc->sc_touch = WSP_UNTOUCH; + if (sc->intr_count < WSP_TAP_MAX_COUNT && + sc->intr_count > WSP_TAP_THRESHOLD && + sc->ntaps && sc->ibtn == 0) { + /* + * Add a pair of events (button-down and + * button-up). + */ + switch (sc->ntaps) { +#if 0 + case 1: + wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN); + DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n"); + break; +#endif + case 2: + if (sc->distance < MAX_DISTANCE) + wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN); + DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n"); + break; + case 3: + wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN); + break; + default: + /* we don't handle taps of more than three fingers */ + break; + } + wsp_add_to_queue(sc, 0, 0, 0, 0); /* button release */ + } + if (sc->intr_count >= WSP_TAP_MAX_COUNT && + (sc->dt_sum / tun.scr_hor_threshold) != 0 && + sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) { + + /* + * translate T-axis into button presses + * until further + */ + if (sc->dt_sum > 0) + wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3); + else if (sc->dt_sum < 0) + wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4); + } + sc->dz_count = WSP_DZ_MAX_COUNT; + sc->dz_sum = 0; + sc->intr_count = 0; + sc->ibtn = 0; + sc->ntaps = 0; + sc->finger = 0; + sc->distance = 0; + sc->dt_sum = 0; + sc->dx_sum = 0; + sc->dy_sum = 0; + sc->scr_mode = WSP_SCR_NONE; + } else if (f[0].touch_major >= tun.pressure_touch_threshold && + sc->sc_touch == WSP_UNTOUCH) { /* ignore first touch */ + sc->sc_touch = WSP_FIRST_TOUCH; + } else if (f[0].touch_major >= tun.pressure_touch_threshold && + sc->sc_touch == WSP_FIRST_TOUCH) { /* ignore second touch */ + sc->sc_touch = WSP_SECOND_TOUCH; + DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n", + sc->pre_pos_x, sc->pre_pos_y); + } else { + if (sc->sc_touch == WSP_SECOND_TOUCH) + sc->sc_touch = WSP_TOUCHING; + + if (n != 0 && + h->q2 == 4 && + f[0].touch_major >= tun.pressure_touch_threshold) { + dx = sc->pos_x[0] - sc->pre_pos_x; + dy = sc->pos_y[0] - sc->pre_pos_y; + if (n == 2 && sc->sc_status.button != 0) { + dx = sc->pos_x[sc->finger] - sc->pre_pos_x; + dy = sc->pos_y[sc->finger] - sc->pre_pos_y; + if (f[0].origin == 0 || f[1].origin == 0) { + dx = 0; + dy = 0; + sc->finger = 0; + } + if ((abs(f[0].rel_x) + abs(f[0].rel_y)) < + (abs(f[1].rel_x) + abs(f[1].rel_y)) && + sc->finger == 0) { + sc->sc_touch = WSP_SECOND_TOUCH; + dx = 0; + dy = 0; + sc->finger = 1; + } + if ((abs(f[0].rel_x) + abs(f[0].rel_y)) >= + (abs(f[1].rel_x) + abs(f[1].rel_y)) && + sc->finger == 1) { + sc->sc_touch = WSP_SECOND_TOUCH; + dx = 0; + dy = 0; + sc->finger = 0; + } + DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n", + dx, dy, sc->finger); + } + if (sc->dz_count--) + sc->dz_sum -= (dy / tun.scale_factor); + if ((sc->dz_sum / tun.z_factor) != 0) + sc->dz_count = 0; + } + dx /= tun.scale_factor; + dy /= tun.scale_factor; + sc->dx_sum += dx; + sc->dy_sum += dy; + + if (n == 2 && sc->sc_status.button == 0) { + if (sc->scr_mode == WSP_SCR_NONE && + abs(sc->dx_sum) + abs(sc->dy_sum) > 50) + sc->scr_mode = abs(sc->dx_sum) > + abs(sc->dy_sum) ? WSP_SCR_HOR : + WSP_SCR_VER; + DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n", + sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum); + if (sc->scr_mode == WSP_SCR_HOR) + sc->dt_sum += dx; + else + sc->dt_sum = 0; + + dx = 0; + dy = 0; + if (sc->dz_count == 0) + dz = sc->dz_sum / tun.z_factor; + if (abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE || + abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE) + dz = 0; + } + if (sc->intr_count < WSP_TAP_MAX_COUNT && + abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3) { + dx = dy = dz = 0; + } else + sc->intr_count = WSP_TAP_MAX_COUNT; + if (dx || dy || dz) + sc->sc_status.flags |= MOUSE_POSCHANGED; + DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n", + dx, dy, dz, sc->sc_touch, sc->sc_status.button); + sc->sc_status.dx += dx; + sc->sc_status.dy += dy; + sc->sc_status.dz += dz; + + wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button); + if (sc->dz_count == 0) + sc->dz_sum = 0; + + } + sc->pre_pos_x = sc->pos_x[0]; + sc->pre_pos_y = sc->pos_y[0]; + + if (n == 2 && sc->sc_status.button != 0) { + sc->pre_pos_x = sc->pos_x[sc->finger]; + sc->pre_pos_y = sc->pos_y[sc->finger]; + } + case USB_ST_SETUP: +tr_setup: + /* check if we can put more data into the FIFO */ + if (usb_fifo_put_bytes_max( + sc->sc_fifo.fp[USB_FIFO_RX]) != 0) { + usbd_xfer_set_frame_len(xfer, 0, + sc->sc_params->tp_datalen); + usbd_transfer_submit(xfer); + } + break; + + default: /* Error */ + if (error != USB_ERR_CANCELLED) { + /* try clear stall first */ + usbd_xfer_set_stall(xfer); + goto tr_setup; + } + break; + } + + return; +} + +static void +wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz, + uint32_t buttons_in) +{ + uint32_t buttons_out; + uint8_t buf[8]; + + dx = imin(dx, 254); + dx = imax(dx, -256); + dy = imin(dy, 254); + dy = imax(dy, -256); + dz = imin(dz, 126); + dz = imax(dz, -128); + + buttons_out = MOUSE_MSC_BUTTONS; + if (buttons_in & MOUSE_BUTTON1DOWN) + buttons_out &= ~MOUSE_MSC_BUTTON1UP; + else if (buttons_in & MOUSE_BUTTON2DOWN) + buttons_out &= ~MOUSE_MSC_BUTTON2UP; + else if (buttons_in & MOUSE_BUTTON3DOWN) + buttons_out &= ~MOUSE_MSC_BUTTON3UP; + + /* Encode the mouse data in standard format; refer to mouse(4) */ + buf[0] = sc->sc_mode.syncmask[1]; + buf[0] |= buttons_out; + buf[1] = dx >> 1; + buf[2] = dy >> 1; + buf[3] = dx - (dx >> 1); + buf[4] = dy - (dy >> 1); + /* Encode extra bytes for level 1 */ + if (sc->sc_mode.level == 1) { + buf[5] = dz >> 1; /* dz / 2 */ + buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */ + buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS); + } + usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf, + sc->sc_mode.packetsize, 1); +} + +static void +wsp_reset_buf(struct wsp_softc *sc) +{ + /* reset read queue */ + usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]); +} + +static void +wsp_start_read(struct usb_fifo *fifo) +{ + struct wsp_softc *sc = usb_fifo_softc(fifo); + int rate; + + /* Check if we should override the default polling interval */ + rate = sc->sc_pollrate; + /* Range check rate */ + if (rate > 1000) + rate = 1000; + /* Check for set rate */ + if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) { + /* Stop current transfer, if any */ + usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]); + /* Set new interval */ + usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate); + /* Only set pollrate once */ + sc->sc_pollrate = 0; + } + usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]); +} + +static void +wsp_stop_read(struct usb_fifo *fifo) +{ + struct wsp_softc *sc = usb_fifo_softc(fifo); + + usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]); +} + + +static int +wsp_open(struct usb_fifo *fifo, int fflags) +{ + DPRINTFN(WSP_LLEVEL_INFO, "\n"); + + if (fflags & FREAD) { + struct wsp_softc *sc = usb_fifo_softc(fifo); + int rc; + + if (sc->sc_state & WSP_ENABLED) + return (EBUSY); + + if (usb_fifo_alloc_buffer(fifo, + WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) { + return (ENOMEM); + } + rc = wsp_enable(sc); + if (rc != 0) { + usb_fifo_free_buffer(fifo); + return (rc); + } + } + return (0); +} + +static void +wsp_close(struct usb_fifo *fifo, int fflags) +{ + if (fflags & FREAD) { + struct wsp_softc *sc = usb_fifo_softc(fifo); + + wsp_disable(sc); + usb_fifo_free_buffer(fifo); + } +} + +int +wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) +{ + struct wsp_softc *sc = usb_fifo_softc(fifo); + mousemode_t mode; + int error = 0; + + mtx_lock(&sc->sc_mutex); + + switch (cmd) { + case MOUSE_GETHWINFO: + *(mousehw_t *)addr = sc->sc_hw; + break; + case MOUSE_GETMODE: + *(mousemode_t *)addr = sc->sc_mode; + break; + case MOUSE_SETMODE: + mode = *(mousemode_t *)addr; + + if (mode.level == -1) + /* Don't change the current setting */ + ; + else if ((mode.level < 0) || (mode.level > 1)) { + error = EINVAL; + goto done; + } + sc->sc_mode.level = mode.level; + sc->sc_pollrate = mode.rate; + sc->sc_hw.buttons = 3; + + if (sc->sc_mode.level == 0) { + sc->sc_mode.protocol = MOUSE_PROTO_MSC; + sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; + sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; + sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; + } else if (sc->sc_mode.level == 1) { + sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; + sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; + sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; + sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; + } + wsp_reset_buf(sc); + break; + case MOUSE_GETLEVEL: + *(int *)addr = sc->sc_mode.level; + break; + case MOUSE_SETLEVEL: + if (*(int *)addr < 0 || *(int *)addr > 1) { + error = EINVAL; + goto done; + } + sc->sc_mode.level = *(int *)addr; + sc->sc_hw.buttons = 3; + + if (sc->sc_mode.level == 0) { + sc->sc_mode.protocol = MOUSE_PROTO_MSC; + sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; + sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; + sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; + } else if (sc->sc_mode.level == 1) { + sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; + sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; + sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; + sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; + } + wsp_reset_buf(sc); + break; + case MOUSE_GETSTATUS:{ + mousestatus_t *status = (mousestatus_t *)addr; + + *status = sc->sc_status; + sc->sc_status.obutton = sc->sc_status.button; + sc->sc_status.button = 0; + sc->sc_status.dx = 0; + sc->sc_status.dy = 0; + sc->sc_status.dz = 0; + + if (status->dx || status->dy || status->dz) + status->flags |= MOUSE_POSCHANGED; + if (status->button != status->obutton) + status->flags |= MOUSE_BUTTONSCHANGED; + break; + } + default: + error = ENOTTY; + } + +done: + mtx_unlock(&sc->sc_mutex); + return (error); +} + +static device_method_t wsp_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, wsp_probe), + DEVMETHOD(device_attach, wsp_attach), + DEVMETHOD(device_detach, wsp_detach), + DEVMETHOD_END +}; + +static driver_t wsp_driver = { + .name = WSP_DRIVER_NAME, + .methods = wsp_methods, + .size = sizeof(struct wsp_softc) +}; + +static devclass_t wsp_devclass; + +DRIVER_MODULE(wsp, uhub, wsp_driver, wsp_devclass, NULL, 0); +MODULE_DEPEND(wsp, usb, 1, 1, 1); +MODULE_VERSION(wsp, 1); diff --git a/sys/dev/usb/usbdevs b/sys/dev/usb/usbdevs index 586c81d..68ec00b 100644 --- a/sys/dev/usb/usbdevs +++ b/sys/dev/usb/usbdevs @@ -1077,12 +1077,54 @@ product APPLE DUMMY 0x0000 Dummy product product APPLE IMAC_KBD 0x0201 USB iMac Keyboard product APPLE KBD 0x0202 USB Keyboard M2452 product APPLE EXT_KBD 0x020c Apple Extended USB Keyboard -product APPLE KBD_TP_ANSI 0x0223 Apple Internal Keyboard/Trackpad (Wellspring/ANSI) -product APPLE KBD_TP_ISO 0x0224 Apple Internal Keyboard/Trackpad (Wellspring/ISO) -product APPLE KBD_TP_JIS 0x0225 Apple Internal Keyboard/Trackpad (Wellspring/JIS) -product APPLE KBD_TP_ANSI2 0x0230 Apple Internal Keyboard/Trackpad (Wellspring2/ANSI) -product APPLE KBD_TP_ISO2 0x0231 Apple Internal Keyboard/Trackpad (Wellspring2/ISO) -product APPLE KBD_TP_JIS2 0x0232 Apple Internal Keyboard/Trackpad (Wellspring2/JIS) +/* MacbookAir, aka wellspring */ +product APPLE WELLSPRING_ANSI 0x0223 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING_ISO 0x0224 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING_JIS 0x0225 Apple Internal Keyboard/Trackpad +/* MacbookProPenryn, aka wellspring2 */ +product APPLE WELLSPRING2_ANSI 0x0230 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING2_ISO 0x0231 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING2_JIS 0x0232 Apple Internal Keyboard/Trackpad +/* Macbook5,1 (unibody), aka wellspring3 */ +product APPLE WELLSPRING3_ANSI 0x0236 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING3_ISO 0x0237 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING3_JIS 0x0238 Apple Internal Keyboard/Trackpad +/* MacbookAir3,2 (unibody), aka wellspring4 */ +product APPLE WELLSPRING4_ANSI 0x023f Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING4_ISO 0x0240 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING4_JIS 0x0241 Apple Internal Keyboard/Trackpad +/* MacbookAir3,1 (unibody), aka wellspring4 */ +product APPLE WELLSPRING4A_ANSI 0x0242 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING4A_ISO 0x0243 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING4A_JIS 0x0244 Apple Internal Keyboard/Trackpad +/* Macbook8 (unibody, March 2011) */ +product APPLE WELLSPRING5_ANSI 0x0245 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING5_ISO 0x0246 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING5_JIS 0x0247 Apple Internal Keyboard/Trackpad +/* MacbookAir4,1 (unibody, July 2011) */ +product APPLE WELLSPRING6A_ANSI 0x0249 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING6A_ISO 0x024a Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING6A_JIS 0x024b Apple Internal Keyboard/Trackpad +/* MacbookAir4,2 (unibody, July 2011) */ +product APPLE WELLSPRING6_ANSI 0x024c Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING6_ISO 0x024d Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING6_JIS 0x024e Apple Internal Keyboard/Trackpad +/* Macbook8,2 (unibody) */ +product APPLE WELLSPRING5A_ANSI 0x0252 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING5A_ISO 0x0253 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING5A_JIS 0x0254 Apple Internal Keyboard/Trackpad +/* MacbookPro10,1 (unibody, June 2012) */ +product APPLE WELLSPRING7_ANSI 0x0262 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING7_ISO 0x0263 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING7_JIS 0x0264 Apple Internal Keyboard/Trackpad +/* MacbookPro10,2 (unibody, October 2012) */ +product APPLE WELLSPRING7A_ANSI 0x0259 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING7A_ISO 0x025a Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING7A_JIS 0x025b Apple Internal Keyboard/Trackpad +/* MacbookAir6,2 (unibody, June 2013) */ +product APPLE WELLSPRING8_ANSI 0x0290 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING8_ISO 0x0291 Apple Internal Keyboard/Trackpad +product APPLE WELLSPRING8_JIS 0x0292 Apple Internal Keyboard/Trackpad product APPLE MOUSE 0x0301 Mouse M4848 product APPLE OPTMOUSE 0x0302 Optical mouse product APPLE MIGHTYMOUSE 0x0304 Mighty Mouse diff --git a/sys/modules/usb/Makefile b/sys/modules/usb/Makefile index 24847ca..626f4b4 100644 --- a/sys/modules/usb/Makefile +++ b/sys/modules/usb/Makefile @@ -47,7 +47,7 @@ SUBDIR += ${_dwc_otg} ehci ${_musb} ohci uhci xhci ${_uss820dci} ${_at91dci} \ ${_atmegadci} ${_avr32dci} ${_rsu} ${_rsufw} SUBDIR += ${_rum} ${_run} ${_runfw} ${_uath} upgt usie ural ${_zyd} ${_urtw} SUBDIR += ${_urtwn} ${_urtwnfw} -SUBDIR += atp uhid ukbd ums udbp ufm uep +SUBDIR += atp uhid ukbd ums udbp ufm uep wsp SUBDIR += ucom u3g uark ubsa ubser uchcom ucycom ufoma uftdi ugensa uipaq ulpt \ umct umcs umodem umoscom uplcom uslcom uvisor uvscom SUBDIR += uether aue axe axge cdce cue ${_kue} mos rue smsc udav uhso ipheth diff --git a/sys/modules/usb/wsp/Makefile b/sys/modules/usb/wsp/Makefile new file mode 100644 index 0000000..9f85b6a --- /dev/null +++ b/sys/modules/usb/wsp/Makefile @@ -0,0 +1,36 @@ +# +# $FreeBSD$ +# +# Copyright (c) 2012 Huang Wen Hui. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# + +S= ${.CURDIR}/../../.. + +.PATH: $S/dev/usb/input + +KMOD= wsp +SRCS= opt_bus.h opt_usb.h device_if.h bus_if.h usb_if.h vnode_if.h usbdevs.h \ + wsp.c + +.include <bsd.kmod.mk> |