Author: hselasky
Date: Wed Jan 29 10:42:01 2014
New Revision: 261260
URL: http://svnweb.freebsd.org/changeset/base/261260

Log:
  Add support for trackpads found in Apple MacBook products. While at it
  add some missing devd entries.
  
  Submitted by: Huang Wen Hui <huang...@gmail.com>
  MFC after:    1 week

Added:
  head/sys/dev/usb/input/wsp.c   (contents, props changed)
  head/sys/modules/usb/wsp/
  head/sys/modules/usb/wsp/Makefile   (contents, props changed)
Modified:
  head/etc/devd.conf
  head/sys/conf/NOTES
  head/sys/conf/files
  head/sys/dev/usb/usbdevs
  head/sys/modules/usb/Makefile

Modified: head/etc/devd.conf
==============================================================================
--- head/etc/devd.conf  Wed Jan 29 09:58:05 2014        (r261259)
+++ head/etc/devd.conf  Wed Jan 29 10:42:01 2014        (r261260)
@@ -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]+";
 

Modified: head/sys/conf/NOTES
==============================================================================
--- head/sys/conf/NOTES Wed Jan 29 09:58:05 2014        (r261259)
+++ head/sys/conf/NOTES Wed Jan 29 10:42:01 2014        (r261260)
@@ -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

Modified: head/sys/conf/files
==============================================================================
--- head/sys/conf/files Wed Jan 29 09:58:05 2014        (r261259)
+++ head/sys/conf/files Wed Jan 29 10:42:01 2014        (r261260)
@@ -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
 #

Added: head/sys/dev/usb/input/wsp.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/dev/usb/input/wsp.c        Wed Jan 29 10:42:01 2014        
(r261260)
@@ -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;

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to