]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/input/wsp.c
- Remove some dead code.
[FreeBSD/FreeBSD.git] / sys / dev / usb / input / wsp.c
1 /*-
2  * Copyright (c) 2012 Huang Wen Hui
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
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>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/fcntl.h>
40 #include <sys/file.h>
41 #include <sys/selinfo.h>
42 #include <sys/poll.h>
43 #include <sys/sysctl.h>
44
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>
49
50 #include "usbdevs.h"
51
52 #define USB_DEBUG_VAR wsp_debug
53 #include <dev/usb/usb_debug.h>
54
55 #include <sys/mouse.h>
56
57 #define WSP_DRIVER_NAME "wsp"
58
59 #define WSP_CLAMP(x,low,high) do {              \
60         if ((x) < (low))                        \
61                 (x) = (low);                    \
62         else if ((x) > (high))                  \
63                 (x) = (high);                   \
64 } while (0)
65
66 /* Tunables */
67 static  SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW, 0, "USB wsp");
68
69 #ifdef USB_DEBUG
70 enum wsp_log_level {
71         WSP_LLEVEL_DISABLED = 0,
72         WSP_LLEVEL_ERROR,
73         WSP_LLEVEL_DEBUG,               /* for troubleshooting */
74         WSP_LLEVEL_INFO,                /* for diagnostics */
75 };
76 static int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */
77
78 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RW,
79     &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level");
80 #endif                                  /* USB_DEBUG */
81
82 static struct wsp_tuning {
83         int     scale_factor;
84         int     z_factor;
85         int     pressure_touch_threshold;
86         int     pressure_untouch_threshold;
87         int     pressure_tap_threshold;
88         int     scr_hor_threshold;
89 }
90         wsp_tuning =
91 {
92         .scale_factor = 12,
93         .z_factor = 5,
94         .pressure_touch_threshold = 50,
95         .pressure_untouch_threshold = 10,
96         .pressure_tap_threshold = 120,
97         .scr_hor_threshold = 50,
98 };
99
100 static void
101 wsp_runing_rangecheck(struct wsp_tuning *ptun)
102 {
103         WSP_CLAMP(ptun->scale_factor, 1, 63);
104         WSP_CLAMP(ptun->z_factor, 1, 63);
105         WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255);
106         WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255);
107         WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255);
108         WSP_CLAMP(ptun->scr_hor_threshold, 1, 255);
109 }
110
111 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RW,
112     &wsp_tuning.scale_factor, 0, "movement scale factor");
113 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RW,
114     &wsp_tuning.z_factor, 0, "Z-axis scale factor");
115 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RW,
116     &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold");
117 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RW,
118     &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold");
119 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RW,
120     &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold");
121 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_hor_threshold, CTLFLAG_RW,
122     &wsp_tuning.scr_hor_threshold, 0, "horizontal scrolling threshold");
123
124 #define WSP_IFACE_INDEX 1
125
126 /* button data structure */
127 struct bt_data {
128         uint8_t unknown1;               /* constant */
129         uint8_t button;                 /* left button */
130         uint8_t rel_x;                  /* relative x coordinate */
131         uint8_t rel_y;                  /* relative y coordinate */
132 } __packed;
133
134 /* trackpad header types */
135 enum tp_type {
136         TYPE1,                  /* plain trackpad */
137         TYPE2,                  /* button integrated in trackpad */
138         TYPE3                   /* additional header fields since June 2013 */
139 };
140
141 /* trackpad finger data offsets, le16-aligned */
142 #define FINGER_TYPE1            (13 * 2)
143 #define FINGER_TYPE2            (15 * 2)
144 #define FINGER_TYPE3            (19 * 2)
145
146 /* trackpad button data offsets */
147 #define BUTTON_TYPE2            15
148 #define BUTTON_TYPE3            23
149
150 /* list of device capability bits */
151 #define HAS_INTEGRATED_BUTTON   1
152
153 /* trackpad finger header - little endian */
154 struct tp_header {
155         uint8_t flag;
156         uint8_t sn0;
157         uint16_t wFixed0;
158         uint32_t dwSn1;
159         uint32_t dwFixed1;
160         uint16_t wLength;
161         uint8_t nfinger;
162         uint8_t ibt;
163         int16_t wUnknown[6];
164         uint8_t q1;
165         uint8_t q2;
166 } __packed;
167
168 /* trackpad finger structure - little endian */
169 struct tp_finger {
170         int16_t origin;                 /* zero when switching track finger */
171         int16_t abs_x;                  /* absolute x coodinate */
172         int16_t abs_y;                  /* absolute y coodinate */
173         int16_t rel_x;                  /* relative x coodinate */
174         int16_t rel_y;                  /* relative y coodinate */
175         int16_t tool_major;             /* tool area, major axis */
176         int16_t tool_minor;             /* tool area, minor axis */
177         int16_t orientation;            /* 16384 when point, else 15 bit angle */
178         int16_t touch_major;            /* touch area, major axis */
179         int16_t touch_minor;            /* touch area, minor axis */
180         int16_t unused[3];              /* zeros */
181         int16_t multi;                  /* one finger: varies, more fingers:
182                                          * constant */
183 } __packed;
184
185 /* trackpad finger data size, empirically at least ten fingers */
186 #define MAX_FINGERS             16
187 #define SIZEOF_FINGER           sizeof(struct tp_finger)
188 #define SIZEOF_ALL_FINGERS      (MAX_FINGERS * SIZEOF_FINGER)
189 #define MAX_FINGER_ORIENTATION  16384
190
191 /* logical signal quality */
192 #define SN_PRESSURE     45              /* pressure signal-to-noise ratio */
193 #define SN_WIDTH        25              /* width signal-to-noise ratio */
194 #define SN_COORD        250             /* coordinate signal-to-noise ratio */
195 #define SN_ORIENT       10              /* orientation signal-to-noise ratio */
196
197 /* device-specific parameters */
198 struct wsp_param {
199         int     snratio;                /* signal-to-noise ratio */
200         int     min;                    /* device minimum reading */
201         int     max;                    /* device maximum reading */
202 };
203
204 enum {
205         WSP_FLAG_WELLSPRING1,
206         WSP_FLAG_WELLSPRING2,
207         WSP_FLAG_WELLSPRING3,
208         WSP_FLAG_WELLSPRING4,
209         WSP_FLAG_WELLSPRING4A,
210         WSP_FLAG_WELLSPRING5,
211         WSP_FLAG_WELLSPRING6A,
212         WSP_FLAG_WELLSPRING6,
213         WSP_FLAG_WELLSPRING5A,
214         WSP_FLAG_WELLSPRING7,
215         WSP_FLAG_WELLSPRING7A,
216         WSP_FLAG_WELLSPRING8,
217         WSP_FLAG_MAX,
218 };
219
220 /* device-specific configuration */
221 struct wsp_dev_params {
222         uint8_t caps;                   /* device capability bitmask */
223         uint16_t bt_datalen;            /* data length of the button interface */
224         uint8_t tp_type;                /* type of trackpad interface */
225         uint8_t tp_offset;              /* offset to trackpad finger data */
226         uint16_t tp_datalen;            /* data length of the trackpad
227                                          * interface */
228         struct wsp_param p;             /* finger pressure limits */
229         struct wsp_param w;             /* finger width limits */
230         struct wsp_param x;             /* horizontal limits */
231         struct wsp_param y;             /* vertical limits */
232         struct wsp_param o;             /* orientation limits */
233 };
234
235 static const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = {
236         [WSP_FLAG_WELLSPRING1] = {
237                 .caps = 0,
238                 .bt_datalen = sizeof(struct bt_data),
239                 .tp_type = TYPE1,
240                 .tp_offset = FINGER_TYPE1,
241                 .tp_datalen = FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
242                 .p = {
243                         SN_PRESSURE, 0, 256
244                 },
245                 .w = {
246                         SN_WIDTH, 0, 2048
247                 },
248                 .x = {
249                         SN_COORD, -4824, 5342
250                 },
251                 .y = {
252                         SN_COORD, -172, 5820
253                 },
254                 .o = {
255                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
256                 },
257         },
258         [WSP_FLAG_WELLSPRING2] = {
259                 .caps = 0,
260                 .bt_datalen = sizeof(struct bt_data),
261                 .tp_type = TYPE1,
262                 .tp_offset = FINGER_TYPE1,
263                 .tp_datalen = FINGER_TYPE1 + SIZEOF_ALL_FINGERS,
264                 .p = {
265                         SN_PRESSURE, 0, 256
266                 },
267                 .w = {
268                         SN_WIDTH, 0, 2048
269                 },
270                 .x = {
271                         SN_COORD, -4824, 4824
272                 },
273                 .y = {
274                         SN_COORD, -172, 4290
275                 },
276                 .o = {
277                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
278                 },
279         },
280         [WSP_FLAG_WELLSPRING3] = {
281                 .caps = HAS_INTEGRATED_BUTTON,
282                 .bt_datalen = sizeof(struct bt_data),
283                 .tp_type = TYPE2,
284                 .tp_offset = FINGER_TYPE2,
285                 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
286                 .p = {
287                         SN_PRESSURE, 0, 300
288                 },
289                 .w = {
290                         SN_WIDTH, 0, 2048
291                 },
292                 .x = {
293                         SN_COORD, -4460, 5166
294                 },
295                 .y = {
296                         SN_COORD, -75, 6700
297                 },
298                 .o = {
299                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
300                 },
301         },
302         [WSP_FLAG_WELLSPRING4] = {
303                 .caps = HAS_INTEGRATED_BUTTON,
304                 .bt_datalen = sizeof(struct bt_data),
305                 .tp_type = TYPE2,
306                 .tp_offset = FINGER_TYPE2,
307                 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
308                 .p = {
309                         SN_PRESSURE, 0, 300
310                 },
311                 .w = {
312                         SN_WIDTH, 0, 2048
313                 },
314                 .x = {
315                         SN_COORD, -4620, 5140
316                 },
317                 .y = {
318                         SN_COORD, -150, 6600
319                 },
320                 .o = {
321                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
322                 },
323         },
324         [WSP_FLAG_WELLSPRING4A] = {
325                 .caps = HAS_INTEGRATED_BUTTON,
326                 .bt_datalen = sizeof(struct bt_data),
327                 .tp_type = TYPE2,
328                 .tp_offset = FINGER_TYPE2,
329                 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
330                 .p = {
331                         SN_PRESSURE, 0, 300
332                 },
333                 .w = {
334                         SN_WIDTH, 0, 2048
335                 },
336                 .x = {
337                         SN_COORD, -4616, 5112
338                 },
339                 .y = {
340                         SN_COORD, -142, 5234
341                 },
342                 .o = {
343                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
344                 },
345         },
346         [WSP_FLAG_WELLSPRING5] = {
347                 .caps = HAS_INTEGRATED_BUTTON,
348                 .bt_datalen = sizeof(struct bt_data),
349                 .tp_type = TYPE2,
350                 .tp_offset = FINGER_TYPE2,
351                 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
352                 .p = {
353                         SN_PRESSURE, 0, 300
354                 },
355                 .w = {
356                         SN_WIDTH, 0, 2048
357                 },
358                 .x = {
359                         SN_COORD, -4415, 5050
360                 },
361                 .y = {
362                         SN_COORD, -55, 6680
363                 },
364                 .o = {
365                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
366                 },
367         },
368         [WSP_FLAG_WELLSPRING6] = {
369                 .caps = HAS_INTEGRATED_BUTTON,
370                 .bt_datalen = sizeof(struct bt_data),
371                 .tp_type = TYPE2,
372                 .tp_offset = FINGER_TYPE2,
373                 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
374                 .p = {
375                         SN_PRESSURE, 0, 300
376                 },
377                 .w = {
378                         SN_WIDTH, 0, 2048
379                 },
380                 .x = {
381                         SN_COORD, -4620, 5140
382                 },
383                 .y = {
384                         SN_COORD, -150, 6600
385                 },
386                 .o = {
387                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
388                 },
389         },
390         [WSP_FLAG_WELLSPRING5A] = {
391                 .caps = HAS_INTEGRATED_BUTTON,
392                 .bt_datalen = sizeof(struct bt_data),
393                 .tp_type = TYPE2,
394                 .tp_offset = FINGER_TYPE2,
395                 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
396                 .p = {
397                         SN_PRESSURE, 0, 300
398                 },
399                 .w = {
400                         SN_WIDTH, 0, 2048
401                 },
402                 .x = {
403                         SN_COORD, -4750, 5280
404                 },
405                 .y = {
406                         SN_COORD, -150, 6730
407                 },
408                 .o = {
409                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
410                 },
411         },
412         [WSP_FLAG_WELLSPRING6A] = {
413                 .caps = HAS_INTEGRATED_BUTTON,
414                 .bt_datalen = sizeof(struct bt_data),
415                 .tp_type = TYPE2,
416                 .tp_offset = FINGER_TYPE2,
417                 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
418                 .p = {
419                         SN_PRESSURE, 0, 300
420                 },
421                 .w = {
422                         SN_WIDTH, 0, 2048
423                 },
424                 .x = {
425                         SN_COORD, -4620, 5140
426                 },
427                 .y = {
428                         SN_COORD, -150, 6600
429                 },
430                 .o = {
431                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
432                 },
433         },
434         [WSP_FLAG_WELLSPRING7] = {
435                 .caps = HAS_INTEGRATED_BUTTON,
436                 .bt_datalen = sizeof(struct bt_data),
437                 .tp_type = TYPE2,
438                 .tp_offset = FINGER_TYPE2,
439                 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
440                 .p = {
441                         SN_PRESSURE, 0, 300
442                 },
443                 .w = {
444                         SN_WIDTH, 0, 2048
445                 },
446                 .x = {
447                         SN_COORD, -4750, 5280
448                 },
449                 .y = {
450                         SN_COORD, -150, 6730
451                 },
452                 .o = {
453                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
454                 },
455         },
456         [WSP_FLAG_WELLSPRING7A] = {
457                 .caps = HAS_INTEGRATED_BUTTON,
458                 .bt_datalen = sizeof(struct bt_data),
459                 .tp_type = TYPE2,
460                 .tp_offset = FINGER_TYPE2,
461                 .tp_datalen = FINGER_TYPE2 + SIZEOF_ALL_FINGERS,
462                 .p = {
463                         SN_PRESSURE, 0, 300
464                 },
465                 .w = {
466                         SN_WIDTH, 0, 2048
467                 },
468                 .x = {
469                         SN_COORD, -4750, 5280
470                 },
471                 .y = {
472                         SN_COORD, -150, 6730
473                 },
474                 .o = {
475                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
476                 },
477         },
478         [WSP_FLAG_WELLSPRING8] = {
479                 .caps = HAS_INTEGRATED_BUTTON,
480                 .bt_datalen = sizeof(struct bt_data),
481                 .tp_type = TYPE3,
482                 .tp_offset = FINGER_TYPE3,
483                 .tp_datalen = FINGER_TYPE3 + SIZEOF_ALL_FINGERS,
484                 .p = {
485                         SN_PRESSURE, 0, 300
486                 },
487                 .w = {
488                         SN_WIDTH, 0, 2048
489                 },
490                 .x = {
491                         SN_COORD, -4620, 5140
492                 },
493                 .y = {
494                         SN_COORD, -150, 6600
495                 },
496                 .o = {
497                         SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION
498                 },
499         },
500 };
501
502 #define WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
503
504 static const STRUCT_USB_HOST_ID wsp_devs[] = {
505         /* MacbookAir1.1 */
506         WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1),
507         WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1),
508         WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1),
509
510         /* MacbookProPenryn, aka wellspring2 */
511         WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2),
512         WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2),
513         WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2),
514
515         /* Macbook5,1 (unibody), aka wellspring3 */
516         WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3),
517         WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3),
518         WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3),
519
520         /* MacbookAir3,2 (unibody), aka wellspring4 */
521         WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4),
522         WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4),
523         WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4),
524
525         /* MacbookAir3,1 (unibody), aka wellspring4 */
526         WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A),
527         WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A),
528         WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A),
529
530         /* Macbook8 (unibody, March 2011) */
531         WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5),
532         WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5),
533         WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5),
534
535         /* MacbookAir4,1 (unibody, July 2011) */
536         WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A),
537         WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A),
538         WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A),
539
540         /* MacbookAir4,2 (unibody, July 2011) */
541         WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6),
542         WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6),
543         WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6),
544
545         /* Macbook8,2 (unibody) */
546         WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A),
547         WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A),
548         WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A),
549
550         /* MacbookPro10,1 (unibody, June 2012) */
551         /* MacbookPro11,? (unibody, June 2013) */
552         WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7),
553         WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7),
554         WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7),
555
556         /* MacbookPro10,2 (unibody, October 2012) */
557         WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A),
558         WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A),
559         WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A),
560
561         /* MacbookAir6,2 (unibody, June 2013) */
562         WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8),
563         WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8),
564         WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8),
565 };
566
567 #define WSP_FIFO_BUF_SIZE        8      /* bytes */
568 #define WSP_FIFO_QUEUE_MAXLEN   50      /* units */
569
570 enum {
571         WSP_INTR_DT,
572         WSP_N_TRANSFER,
573 };
574
575 struct wsp_softc {
576         struct usb_device *sc_usb_device;
577         struct mtx sc_mutex;            /* for synchronization */
578         struct usb_xfer *sc_xfer[WSP_N_TRANSFER];
579         struct usb_fifo_sc sc_fifo;
580
581         const struct wsp_dev_params *sc_params; /* device configuration */
582
583         mousehw_t sc_hw;
584         mousemode_t sc_mode;
585         u_int   sc_pollrate;
586         mousestatus_t sc_status;
587         u_int   sc_state;
588 #define WSP_ENABLED            0x01
589
590         struct bt_data *bt_data;        /* button transferred data */
591         uint8_t *tp_data;               /* trackpad transferred data */
592         struct tp_finger *index[MAX_FINGERS];   /* finger index data */
593         int16_t pos_x[MAX_FINGERS];     /* position array */
594         int16_t pos_y[MAX_FINGERS];     /* position array */
595         u_int   sc_touch;               /* touch status */
596 #define WSP_UNTOUCH             0x00
597 #define WSP_FIRST_TOUCH         0x01
598 #define WSP_SECOND_TOUCH        0x02
599 #define WSP_TOUCHING            0x04
600         int16_t pre_pos_x;              /* previous position array */
601         int16_t pre_pos_y;              /* previous position array */
602         int     dx_sum;                 /* x axis cumulative movement */
603         int     dy_sum;                 /* y axis cumulative movement */
604         int     dz_sum;                 /* z axis cumulative movement */
605         int     dz_count;
606 #define WSP_DZ_MAX_COUNT        32
607         int     dt_sum;                 /* T-axis cumulative movement */
608
609         uint8_t finger;                 /* 0 or 1 *, check which finger moving */
610         uint16_t intr_count;
611 #define WSP_TAP_THRESHOLD       3
612 #define WSP_TAP_MAX_COUNT       20
613         int     distance;               /* the distance of 2 fingers */
614 #define MAX_DISTANCE            2500    /* the max allowed distance */
615         uint8_t ibtn;                   /* button status in tapping */
616         uint8_t ntaps;                  /* finger status in tapping */
617         uint8_t scr_mode;               /* scroll status in movement */
618 #define WSP_SCR_NONE            0
619 #define WSP_SCR_VER             1
620 #define WSP_SCR_HOR             2
621 };
622
623 typedef enum interface_mode {
624         RAW_SENSOR_MODE = 0x01,
625         HID_MODE = 0x08
626 } interface_mode;
627
628 /*
629  * function prototypes
630  */
631 static usb_fifo_cmd_t wsp_start_read;
632 static usb_fifo_cmd_t wsp_stop_read;
633 static usb_fifo_open_t wsp_open;
634 static usb_fifo_close_t wsp_close;
635 static usb_fifo_ioctl_t wsp_ioctl;
636
637 static struct usb_fifo_methods wsp_fifo_methods = {
638         .f_open = &wsp_open,
639         .f_close = &wsp_close,
640         .f_ioctl = &wsp_ioctl,
641         .f_start_read = &wsp_start_read,
642         .f_stop_read = &wsp_stop_read,
643         .basename[0] = WSP_DRIVER_NAME,
644 };
645
646 /* device initialization and shutdown */
647 static int wsp_enable(struct wsp_softc *sc);
648 static void wsp_disable(struct wsp_softc *sc);
649
650 /* updating fifo */
651 static void wsp_reset_buf(struct wsp_softc *sc);
652 static void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t);
653
654 /* Device methods. */
655 static device_probe_t wsp_probe;
656 static device_attach_t wsp_attach;
657 static device_detach_t wsp_detach;
658 static usb_callback_t wsp_intr_callback;
659
660 static struct usb_config wsp_config[WSP_N_TRANSFER] = {
661         [WSP_INTR_DT] = {
662                 .type = UE_INTERRUPT,
663                 .endpoint = UE_ADDR_ANY,
664                 .direction = UE_DIR_IN,
665                 .flags = {
666                         .pipe_bof = 0,
667                         .short_xfer_ok = 1,
668                 },
669                 .bufsize = 0,           /* use wMaxPacketSize */
670                 .callback = &wsp_intr_callback,
671         },
672 };
673
674 static usb_error_t
675 wsp_set_device_mode(struct wsp_softc *sc, interface_mode mode)
676 {
677         uint8_t mode_bytes[8];
678         usb_error_t err;
679
680         err = usbd_req_get_report(sc->sc_usb_device, NULL,
681             mode_bytes, sizeof(mode_bytes), 0,
682             0x03, 0x00);
683
684         if (err != USB_ERR_NORMAL_COMPLETION) {
685                 DPRINTF("Failed to read device mode (%d)\n", err);
686                 return (err);
687         }
688
689         /*
690          * XXX Need to wait at least 250ms for hardware to get
691          * ready. The device mode handling appears to be handled
692          * asynchronously and we should not issue these commands too
693          * quickly.
694          */
695         pause("WHW", hz / 4);
696
697         mode_bytes[0] = mode;
698
699         return (usbd_req_set_report(sc->sc_usb_device, NULL,
700             mode_bytes, sizeof(mode_bytes), 0,
701             0x03, 0x00));
702 }
703
704 static int
705 wsp_enable(struct wsp_softc *sc)
706 {
707         const struct wsp_dev_params *params = sc->sc_params;
708
709         if (params == NULL || params->tp_datalen == 0) {
710                 DPRINTF("params uninitialized!\n");
711                 return (ENXIO);
712         }
713         /* Allocate the dynamic buffers */
714         sc->tp_data = malloc(params->tp_datalen, M_USB, M_WAITOK | M_ZERO);
715         if (sc->tp_data == NULL) {
716                 DPRINTF("Cannot allocate memory\n");
717                 return (ENXIO);
718         }
719
720         /* reset status */
721         memset(&sc->sc_status, 0, sizeof(sc->sc_status));
722         sc->sc_state |= WSP_ENABLED;
723
724         DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n");
725         return (0);
726 }
727
728 static void
729 wsp_disable(struct wsp_softc *sc)
730 {
731         free(sc->tp_data, M_USB);
732         sc->tp_data = NULL;
733
734         sc->sc_state &= ~WSP_ENABLED;
735         DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n");
736 }
737
738 static int
739 wsp_probe(device_t self)
740 {
741         struct usb_attach_arg *uaa = device_get_ivars(self);
742
743         if (uaa->usb_mode != USB_MODE_HOST)
744                 return (ENXIO);
745
746         if (uaa->info.bIfaceIndex != WSP_IFACE_INDEX)
747                 return (ENXIO);
748
749         if ((uaa->info.bInterfaceClass != UICLASS_HID) ||
750             (uaa->info.bInterfaceProtocol != 0))
751                 return (ENXIO);
752
753         return (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa));
754 }
755
756 static int
757 wsp_attach(device_t dev)
758 {
759         struct wsp_softc *sc = device_get_softc(dev);
760         struct usb_attach_arg *uaa = device_get_ivars(dev);
761         usb_error_t err;
762
763         DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc);
764
765         sc->sc_usb_device = uaa->device;
766
767         /*
768          * By default the touchpad behaves like a HID device, sending
769          * packets with reportID = 8. Such reports contain only
770          * limited information. They encode movement deltas and button
771          * events, but do not include data from the pressure
772          * sensors. The device input mode can be switched from HID
773          * reports to raw sensor data using vendor-specific USB
774          * control commands:
775          */
776
777         /*
778          * During re-enumeration of the device we need to force the
779          * device back into HID mode before switching it to RAW
780          * mode. Else the device does not work like expected.
781          */
782         err = wsp_set_device_mode(sc, HID_MODE);
783         if (err != USB_ERR_NORMAL_COMPLETION) {
784                 DPRINTF("Failed to set mode to HID MODE (%d)\n", err);
785                 return (ENXIO);
786         }
787
788         err = wsp_set_device_mode(sc, RAW_SENSOR_MODE);
789         if (err != USB_ERR_NORMAL_COMPLETION) {
790                 DPRINTF("failed to set mode to RAW MODE (%d)\n", err);
791                 return (ENXIO);
792         }
793
794         mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE);
795
796         /* get device specific configuration */
797         sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa);
798
799         /* set to 0 to use wMaxPacketSize is not enough */
800         wsp_config[0].bufsize = sc->sc_params->tp_datalen;
801
802         err = usbd_transfer_setup(uaa->device,
803             &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config,
804             WSP_N_TRANSFER, sc, &sc->sc_mutex);
805
806         if (err) {
807                 DPRINTF("error=%s\n", usbd_errstr(err));
808                 goto detach;
809         }
810         if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex,
811             &wsp_fifo_methods, &sc->sc_fifo,
812             device_get_unit(dev), -1, uaa->info.bIfaceIndex,
813             UID_ROOT, GID_OPERATOR, 0644)) {
814                 goto detach;
815         }
816         device_set_usb_desc(dev);
817
818         sc->sc_hw.buttons = 3;
819         sc->sc_hw.iftype = MOUSE_IF_USB;
820         sc->sc_hw.type = MOUSE_PAD;
821         sc->sc_hw.model = MOUSE_MODEL_GENERIC;
822         sc->sc_mode.protocol = MOUSE_PROTO_MSC;
823         sc->sc_mode.rate = -1;
824         sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
825         sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
826         sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
827         sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
828
829         sc->sc_touch = WSP_UNTOUCH;
830         sc->scr_mode = WSP_SCR_NONE;
831
832         return (0);
833
834 detach:
835         wsp_detach(dev);
836         return (ENOMEM);
837 }
838
839 static int
840 wsp_detach(device_t dev)
841 {
842         struct wsp_softc *sc = device_get_softc(dev);
843
844         (void) wsp_set_device_mode(sc, HID_MODE);
845
846         mtx_lock(&sc->sc_mutex);
847         if (sc->sc_state & WSP_ENABLED)
848                 wsp_disable(sc);
849         mtx_unlock(&sc->sc_mutex);
850
851         usb_fifo_detach(&sc->sc_fifo);
852
853         usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER);
854
855         mtx_destroy(&sc->sc_mutex);
856
857         return (0);
858 }
859
860 static void
861 wsp_intr_callback(struct usb_xfer *xfer, usb_error_t error)
862 {
863         struct wsp_softc *sc = usbd_xfer_softc(xfer);
864         const struct wsp_dev_params *params = sc->sc_params;
865         struct usb_page_cache *pc;
866         struct tp_finger *f;
867         struct tp_header *h;
868         struct wsp_tuning tun = wsp_tuning;
869         int ntouch = 0;                 /* the finger number in touch */
870         int ibt = 0;                    /* button status */
871         int dx = 0;
872         int dy = 0;
873         int dz = 0;
874         int n = 0;
875         int len;
876         int i;
877
878         wsp_runing_rangecheck(&tun);
879
880         if (sc->dz_count == 0)
881                 sc->dz_count = WSP_DZ_MAX_COUNT;
882
883         usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
884
885         switch (USB_GET_STATE(xfer)) {
886         case USB_ST_TRANSFERRED:
887                 if (len > (int)params->tp_datalen) {
888                         DPRINTFN(WSP_LLEVEL_ERROR,
889                             "truncating large packet from %u to %u bytes\n",
890                             len, params->tp_datalen);
891                         len = params->tp_datalen;
892                 } else {
893                         /* make sure we don't process old data */
894                         memset(sc->tp_data + len, 0, params->tp_datalen - len);
895                 }
896
897                 pc = usbd_xfer_get_frame(xfer, 0);
898                 usbd_copy_out(pc, 0, sc->tp_data, len);
899
900                 h = (struct tp_header *)(sc->tp_data);
901
902                 if (params->tp_type == TYPE2) {
903                         ibt = sc->tp_data[BUTTON_TYPE2];
904                         ntouch = sc->tp_data[BUTTON_TYPE2 - 1];
905                 } else if (params->tp_type == TYPE3) {
906                         ibt = sc->tp_data[BUTTON_TYPE3];
907                         ntouch = sc->tp_data[BUTTON_TYPE3 - 1];
908                 }
909                 /* range check */
910                 if (ntouch < 0)
911                         ntouch = 0;
912                 else if (ntouch > MAX_FINGERS)
913                         ntouch = MAX_FINGERS;
914
915                 f = (struct tp_finger *)(sc->tp_data + params->tp_offset);
916
917                 for (i = 0; i != ntouch; i++) {
918                         /* swap endianness, if any */
919                         if (le16toh(0x1234) != 0x1234) {
920                                 f[i].origin = le16toh((uint16_t)f[i].origin);
921                                 f[i].abs_x = le16toh((uint16_t)f[i].abs_x);
922                                 f[i].abs_y = le16toh((uint16_t)f[i].abs_y);
923                                 f[i].rel_x = le16toh((uint16_t)f[i].rel_x);
924                                 f[i].rel_y = le16toh((uint16_t)f[i].rel_y);
925                                 f[i].tool_major = le16toh((uint16_t)f[i].tool_major);
926                                 f[i].tool_minor = le16toh((uint16_t)f[i].tool_minor);
927                                 f[i].orientation = le16toh((uint16_t)f[i].orientation);
928                                 f[i].touch_major = le16toh((uint16_t)f[i].touch_major);
929                                 f[i].touch_minor = le16toh((uint16_t)f[i].touch_minor);
930                                 f[i].multi = le16toh((uint16_t)f[i].multi);
931                         }
932                         DPRINTFN(WSP_LLEVEL_INFO, "[%d]ibt=%d, taps=%d, u=%x, o=%4d, ax=%5d, ay=%5d, "
933                             "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%5d, tchmaj=%4d, tchmin=%4d, m=%4x\n",
934                             i, ibt, ntouch, h->q2,
935                             f[i].origin, f[i].abs_x, f[i].abs_y, f[i].rel_x, f[i].rel_y,
936                             f[i].tool_major, f[i].tool_minor, f[i].orientation,
937                             f[i].touch_major, f[i].touch_minor, f[i].multi);
938
939                         if (f[i].touch_major < tun.pressure_untouch_threshold)
940                                 continue;
941
942                         sc->pos_x[n] = f[i].abs_x;
943                         sc->pos_y[n] = params->y.min + params->y.max - f[i].abs_y;
944                         sc->index[n] = &f[i];
945                         n++;
946                 }
947
948                 sc->sc_status.flags &= ~MOUSE_POSCHANGED;
949                 sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED;
950                 sc->sc_status.obutton = sc->sc_status.button;
951                 sc->sc_status.button = 0;
952
953                 if (ibt != 0) {
954                         sc->sc_status.button |= MOUSE_BUTTON1DOWN;
955                         sc->ibtn = 1;
956                 }
957                 if (h->q2 == 4)
958                         sc->intr_count++;
959
960                 if (sc->ntaps < n) {
961                         switch (n) {
962                         case 1:
963                                 if (f[0].touch_major > tun.pressure_tap_threshold)
964                                         sc->ntaps = 1;
965                                 break;
966                         case 2:
967                                 if (f[0].touch_major > tun.pressure_tap_threshold &&
968                                     f[1].touch_major > tun.pressure_tap_threshold)
969                                         sc->ntaps = 2;
970                                 break;
971                         case 3:
972                                 if (f[0].touch_major > tun.pressure_tap_threshold &&
973                                     f[1].touch_major > tun.pressure_tap_threshold &&
974                                     f[2].touch_major > tun.pressure_tap_threshold)
975                                         sc->ntaps = 3;
976                                 break;
977                         default:
978                                 break;
979                         }
980                 }
981                 if (n == 2) {
982                         sc->distance = max(sc->distance, max(
983                             abs(sc->pos_x[0] - sc->pos_x[1]),
984                             abs(sc->pos_y[0] - sc->pos_y[1])));
985                 }
986                 if (f[0].touch_major < tun.pressure_untouch_threshold &&
987                     sc->sc_status.button == 0) {
988                         sc->sc_touch = WSP_UNTOUCH;
989                         if (sc->intr_count < WSP_TAP_MAX_COUNT &&
990                             sc->intr_count > WSP_TAP_THRESHOLD &&
991                             sc->ntaps && sc->ibtn == 0) {
992                                 /*
993                                  * Add a pair of events (button-down and
994                                  * button-up).
995                                  */
996                                 switch (sc->ntaps) {
997 #if 0
998                                 case 1:
999                                         wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN);
1000                                         DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n");
1001                                         break;
1002 #endif
1003                                 case 2:
1004                                         if (sc->distance < MAX_DISTANCE)
1005                                                 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN);
1006                                         DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n");
1007                                         break;
1008                                 case 3:
1009                                         wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN);
1010                                         break;
1011                                 default:
1012                                         /* we don't handle taps of more than three fingers */
1013                                         break;
1014                                 }
1015                                 wsp_add_to_queue(sc, 0, 0, 0, 0);       /* button release */
1016                         }
1017                         if (sc->intr_count >= WSP_TAP_MAX_COUNT &&
1018                             (sc->dt_sum / tun.scr_hor_threshold) != 0 &&
1019                             sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) {
1020
1021                                 /*
1022                                  * translate T-axis into button presses
1023                                  * until further
1024                                  */
1025                                 if (sc->dt_sum > 0)
1026                                         wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3);
1027                                 else if (sc->dt_sum < 0)
1028                                         wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4);
1029                         }
1030                         sc->dz_count = WSP_DZ_MAX_COUNT;
1031                         sc->dz_sum = 0;
1032                         sc->intr_count = 0;
1033                         sc->ibtn = 0;
1034                         sc->ntaps = 0;
1035                         sc->finger = 0;
1036                         sc->distance = 0;
1037                         sc->dt_sum = 0;
1038                         sc->dx_sum = 0;
1039                         sc->dy_sum = 0;
1040                         sc->scr_mode = WSP_SCR_NONE;
1041                 } else if (f[0].touch_major >= tun.pressure_touch_threshold &&
1042                     sc->sc_touch == WSP_UNTOUCH) {      /* ignore first touch */
1043                         sc->sc_touch = WSP_FIRST_TOUCH;
1044                 } else if (f[0].touch_major >= tun.pressure_touch_threshold &&
1045                     sc->sc_touch == WSP_FIRST_TOUCH) {  /* ignore second touch */
1046                         sc->sc_touch = WSP_SECOND_TOUCH;
1047                         DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n",
1048                             sc->pre_pos_x, sc->pre_pos_y);
1049                 } else {
1050                         if (sc->sc_touch == WSP_SECOND_TOUCH)
1051                                 sc->sc_touch = WSP_TOUCHING;
1052
1053                         if (n != 0 &&
1054                             h->q2 == 4 &&
1055                             f[0].touch_major >= tun.pressure_touch_threshold) {
1056                                 dx = sc->pos_x[0] - sc->pre_pos_x;
1057                                 dy = sc->pos_y[0] - sc->pre_pos_y;
1058                                 if (n == 2 && sc->sc_status.button != 0) {
1059                                         dx = sc->pos_x[sc->finger] - sc->pre_pos_x;
1060                                         dy = sc->pos_y[sc->finger] - sc->pre_pos_y;
1061                                         if (f[0].origin == 0 || f[1].origin == 0) {
1062                                                 dx = 0;
1063                                                 dy = 0;
1064                                                 sc->finger = 0;
1065                                         }
1066                                         if ((abs(f[0].rel_x) + abs(f[0].rel_y)) <
1067                                             (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
1068                                             sc->finger == 0) {
1069                                                 sc->sc_touch = WSP_SECOND_TOUCH;
1070                                                 dx = 0;
1071                                                 dy = 0;
1072                                                 sc->finger = 1;
1073                                         }
1074                                         if ((abs(f[0].rel_x) + abs(f[0].rel_y)) >=
1075                                             (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
1076                                             sc->finger == 1) {
1077                                                 sc->sc_touch = WSP_SECOND_TOUCH;
1078                                                 dx = 0;
1079                                                 dy = 0;
1080                                                 sc->finger = 0;
1081                                         }
1082                                         DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n",
1083                                             dx, dy, sc->finger);
1084                                 }
1085                                 if (sc->dz_count--)
1086                                         sc->dz_sum -= (dy / tun.scale_factor);
1087                                 if ((sc->dz_sum / tun.z_factor) != 0)
1088                                         sc->dz_count = 0;
1089                         }
1090                         dx /= tun.scale_factor;
1091                         dy /= tun.scale_factor;
1092                         sc->dx_sum += dx;
1093                         sc->dy_sum += dy;
1094
1095                         if (n == 2 && sc->sc_status.button == 0) {
1096                                 if (sc->scr_mode == WSP_SCR_NONE &&
1097                                     abs(sc->dx_sum) + abs(sc->dy_sum) > 50)
1098                                         sc->scr_mode = abs(sc->dx_sum) >
1099                                             abs(sc->dy_sum) ? WSP_SCR_HOR :
1100                                             WSP_SCR_VER;
1101                                 DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n",
1102                                     sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum);
1103                                 if (sc->scr_mode == WSP_SCR_HOR)
1104                                         sc->dt_sum += dx;
1105                                 else
1106                                         sc->dt_sum = 0;
1107
1108                                 dx = 0;
1109                                 dy = 0;
1110                                 if (sc->dz_count == 0)
1111                                         dz = sc->dz_sum / tun.z_factor;
1112                                 if (abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE ||
1113                                     abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE)
1114                                         dz = 0;
1115                         }
1116                         if (sc->intr_count < WSP_TAP_MAX_COUNT &&
1117                             abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3) {
1118                                 dx = dy = dz = 0;
1119                         } else
1120                                 sc->intr_count = WSP_TAP_MAX_COUNT;
1121                         if (dx || dy || dz)
1122                                 sc->sc_status.flags |= MOUSE_POSCHANGED;
1123                         DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n",
1124                             dx, dy, dz, sc->sc_touch, sc->sc_status.button);
1125                         sc->sc_status.dx += dx;
1126                         sc->sc_status.dy += dy;
1127                         sc->sc_status.dz += dz;
1128
1129                         wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button);
1130                         if (sc->dz_count == 0)
1131                                 sc->dz_sum = 0;
1132
1133                 }
1134                 sc->pre_pos_x = sc->pos_x[0];
1135                 sc->pre_pos_y = sc->pos_y[0];
1136
1137                 if (n == 2 && sc->sc_status.button != 0) {
1138                         sc->pre_pos_x = sc->pos_x[sc->finger];
1139                         sc->pre_pos_y = sc->pos_y[sc->finger];
1140                 }
1141         case USB_ST_SETUP:
1142 tr_setup:
1143                 /* check if we can put more data into the FIFO */
1144                 if (usb_fifo_put_bytes_max(
1145                     sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
1146                         usbd_xfer_set_frame_len(xfer, 0,
1147                             sc->sc_params->tp_datalen);
1148                         usbd_transfer_submit(xfer);
1149                 }
1150                 break;
1151
1152         default:                        /* Error */
1153                 if (error != USB_ERR_CANCELLED) {
1154                         /* try clear stall first */
1155                         usbd_xfer_set_stall(xfer);
1156                         goto tr_setup;
1157                 }
1158                 break;
1159         }
1160
1161         return;
1162 }
1163
1164 static void
1165 wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz,
1166     uint32_t buttons_in)
1167 {
1168         uint32_t buttons_out;
1169         uint8_t buf[8];
1170
1171         dx = imin(dx, 254);
1172         dx = imax(dx, -256);
1173         dy = imin(dy, 254);
1174         dy = imax(dy, -256);
1175         dz = imin(dz, 126);
1176         dz = imax(dz, -128);
1177
1178         buttons_out = MOUSE_MSC_BUTTONS;
1179         if (buttons_in & MOUSE_BUTTON1DOWN)
1180                 buttons_out &= ~MOUSE_MSC_BUTTON1UP;
1181         else if (buttons_in & MOUSE_BUTTON2DOWN)
1182                 buttons_out &= ~MOUSE_MSC_BUTTON2UP;
1183         else if (buttons_in & MOUSE_BUTTON3DOWN)
1184                 buttons_out &= ~MOUSE_MSC_BUTTON3UP;
1185
1186         /* Encode the mouse data in standard format; refer to mouse(4) */
1187         buf[0] = sc->sc_mode.syncmask[1];
1188         buf[0] |= buttons_out;
1189         buf[1] = dx >> 1;
1190         buf[2] = dy >> 1;
1191         buf[3] = dx - (dx >> 1);
1192         buf[4] = dy - (dy >> 1);
1193         /* Encode extra bytes for level 1 */
1194         if (sc->sc_mode.level == 1) {
1195                 buf[5] = dz >> 1;       /* dz / 2 */
1196                 buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */
1197                 buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS);
1198         }
1199         usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
1200             sc->sc_mode.packetsize, 1);
1201 }
1202
1203 static void
1204 wsp_reset_buf(struct wsp_softc *sc)
1205 {
1206         /* reset read queue */
1207         usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
1208 }
1209
1210 static void
1211 wsp_start_read(struct usb_fifo *fifo)
1212 {
1213         struct wsp_softc *sc = usb_fifo_softc(fifo);
1214         int rate;
1215
1216         /* Check if we should override the default polling interval */
1217         rate = sc->sc_pollrate;
1218         /* Range check rate */
1219         if (rate > 1000)
1220                 rate = 1000;
1221         /* Check for set rate */
1222         if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) {
1223                 /* Stop current transfer, if any */
1224                 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1225                 /* Set new interval */
1226                 usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate);
1227                 /* Only set pollrate once */
1228                 sc->sc_pollrate = 0;
1229         }
1230         usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]);
1231 }
1232
1233 static void
1234 wsp_stop_read(struct usb_fifo *fifo)
1235 {
1236         struct wsp_softc *sc = usb_fifo_softc(fifo);
1237
1238         usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1239 }
1240
1241
1242 static int
1243 wsp_open(struct usb_fifo *fifo, int fflags)
1244 {
1245         DPRINTFN(WSP_LLEVEL_INFO, "\n");
1246
1247         if (fflags & FREAD) {
1248                 struct wsp_softc *sc = usb_fifo_softc(fifo);
1249                 int rc;
1250
1251                 if (sc->sc_state & WSP_ENABLED)
1252                         return (EBUSY);
1253
1254                 if (usb_fifo_alloc_buffer(fifo,
1255                     WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) {
1256                         return (ENOMEM);
1257                 }
1258                 rc = wsp_enable(sc);
1259                 if (rc != 0) {
1260                         usb_fifo_free_buffer(fifo);
1261                         return (rc);
1262                 }
1263         }
1264         return (0);
1265 }
1266
1267 static void
1268 wsp_close(struct usb_fifo *fifo, int fflags)
1269 {
1270         if (fflags & FREAD) {
1271                 struct wsp_softc *sc = usb_fifo_softc(fifo);
1272
1273                 wsp_disable(sc);
1274                 usb_fifo_free_buffer(fifo);
1275         }
1276 }
1277
1278 int
1279 wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1280 {
1281         struct wsp_softc *sc = usb_fifo_softc(fifo);
1282         mousemode_t mode;
1283         int error = 0;
1284
1285         mtx_lock(&sc->sc_mutex);
1286
1287         switch (cmd) {
1288         case MOUSE_GETHWINFO:
1289                 *(mousehw_t *)addr = sc->sc_hw;
1290                 break;
1291         case MOUSE_GETMODE:
1292                 *(mousemode_t *)addr = sc->sc_mode;
1293                 break;
1294         case MOUSE_SETMODE:
1295                 mode = *(mousemode_t *)addr;
1296
1297                 if (mode.level == -1)
1298                         /* Don't change the current setting */
1299                         ;
1300                 else if ((mode.level < 0) || (mode.level > 1)) {
1301                         error = EINVAL;
1302                         goto done;
1303                 }
1304                 sc->sc_mode.level = mode.level;
1305                 sc->sc_pollrate = mode.rate;
1306                 sc->sc_hw.buttons = 3;
1307
1308                 if (sc->sc_mode.level == 0) {
1309                         sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1310                         sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1311                         sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1312                         sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1313                 } else if (sc->sc_mode.level == 1) {
1314                         sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1315                         sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1316                         sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1317                         sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1318                 }
1319                 wsp_reset_buf(sc);
1320                 break;
1321         case MOUSE_GETLEVEL:
1322                 *(int *)addr = sc->sc_mode.level;
1323                 break;
1324         case MOUSE_SETLEVEL:
1325                 if (*(int *)addr < 0 || *(int *)addr > 1) {
1326                         error = EINVAL;
1327                         goto done;
1328                 }
1329                 sc->sc_mode.level = *(int *)addr;
1330                 sc->sc_hw.buttons = 3;
1331
1332                 if (sc->sc_mode.level == 0) {
1333                         sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1334                         sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1335                         sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1336                         sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1337                 } else if (sc->sc_mode.level == 1) {
1338                         sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1339                         sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1340                         sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1341                         sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1342                 }
1343                 wsp_reset_buf(sc);
1344                 break;
1345         case MOUSE_GETSTATUS:{
1346                         mousestatus_t *status = (mousestatus_t *)addr;
1347
1348                         *status = sc->sc_status;
1349                         sc->sc_status.obutton = sc->sc_status.button;
1350                         sc->sc_status.button = 0;
1351                         sc->sc_status.dx = 0;
1352                         sc->sc_status.dy = 0;
1353                         sc->sc_status.dz = 0;
1354
1355                         if (status->dx || status->dy || status->dz)
1356                                 status->flags |= MOUSE_POSCHANGED;
1357                         if (status->button != status->obutton)
1358                                 status->flags |= MOUSE_BUTTONSCHANGED;
1359                         break;
1360                 }
1361         default:
1362                 error = ENOTTY;
1363         }
1364
1365 done:
1366         mtx_unlock(&sc->sc_mutex);
1367         return (error);
1368 }
1369
1370 static device_method_t wsp_methods[] = {
1371         /* Device interface */
1372         DEVMETHOD(device_probe, wsp_probe),
1373         DEVMETHOD(device_attach, wsp_attach),
1374         DEVMETHOD(device_detach, wsp_detach),
1375         DEVMETHOD_END
1376 };
1377
1378 static driver_t wsp_driver = {
1379         .name = WSP_DRIVER_NAME,
1380         .methods = wsp_methods,
1381         .size = sizeof(struct wsp_softc)
1382 };
1383
1384 static devclass_t wsp_devclass;
1385
1386 DRIVER_MODULE(wsp, uhub, wsp_driver, wsp_devclass, NULL, 0);
1387 MODULE_DEPEND(wsp, usb, 1, 1, 1);
1388 MODULE_VERSION(wsp, 1);