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