xref: /freebsd/sys/dev/usb/input/wsp.c (revision e47161e5f1f01ef300c6e7efdb9c92e3a6c497ff)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Huang Wen Hui
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_evdev.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/fcntl.h>
42 #include <sys/file.h>
43 #include <sys/selinfo.h>
44 #include <sys/poll.h>
45 #include <sys/sysctl.h>
46 
47 #include <dev/hid/hid.h>
48 
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usbhid.h>
53 
54 #include "usbdevs.h"
55 
56 #define	USB_DEBUG_VAR wsp_debug
57 #include <dev/usb/usb_debug.h>
58 
59 #ifdef EVDEV_SUPPORT
60 #include <dev/evdev/input.h>
61 #include <dev/evdev/evdev.h>
62 #endif
63 
64 #include <sys/mouse.h>
65 
66 #define	WSP_DRIVER_NAME "wsp"
67 #define	WSP_BUFFER_MAX	1024
68 
69 #define	WSP_CLAMP(x,low,high) do {		\
70 	if ((x) < (low))			\
71 		(x) = (low);			\
72 	else if ((x) > (high))			\
73 		(x) = (high);			\
74 } while (0)
75 
76 /* Tunables */
77 static	SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
78     "USB wsp");
79 
80 #ifdef USB_DEBUG
81 enum wsp_log_level {
82 	WSP_LLEVEL_DISABLED = 0,
83 	WSP_LLEVEL_ERROR,
84 	WSP_LLEVEL_DEBUG,		/* for troubleshooting */
85 	WSP_LLEVEL_INFO,		/* for diagnostics */
86 };
87 static int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */
88 
89 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RWTUN,
90     &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level");
91 #endif					/* USB_DEBUG */
92 
93 static struct wsp_tuning {
94 	int	scale_factor;
95 	int	z_factor;
96 	int	z_invert;
97 	int	pressure_touch_threshold;
98 	int	pressure_untouch_threshold;
99 	int	pressure_tap_threshold;
100 	int	scr_hor_threshold;
101 	int	max_finger_area;
102 	int	max_double_tap_distance;
103 	int	enable_single_tap_clicks;
104 	int	enable_single_tap_movement;
105 }
106 	wsp_tuning =
107 {
108 	.scale_factor = 12,
109 	.z_factor = 5,
110 	.z_invert = 0,
111 	.pressure_touch_threshold = 50,
112 	.pressure_untouch_threshold = 10,
113 	.pressure_tap_threshold = 120,
114 	.scr_hor_threshold = 20,
115 	.max_finger_area = 1900,
116 	.max_double_tap_distance = 2500,
117 	.enable_single_tap_clicks = 1,
118 	.enable_single_tap_movement = 1,
119 };
120 
121 static void
122 wsp_runing_rangecheck(struct wsp_tuning *ptun)
123 {
124 	WSP_CLAMP(ptun->scale_factor, 1, 63);
125 	WSP_CLAMP(ptun->z_factor, 1, 63);
126 	WSP_CLAMP(ptun->z_invert, 0, 1);
127 	WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255);
128 	WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255);
129 	WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255);
130 	WSP_CLAMP(ptun->max_finger_area, 1, 2400);
131 	WSP_CLAMP(ptun->max_double_tap_distance, 1, 16384);
132 	WSP_CLAMP(ptun->scr_hor_threshold, 1, 255);
133 	WSP_CLAMP(ptun->enable_single_tap_clicks, 0, 1);
134 	WSP_CLAMP(ptun->enable_single_tap_movement, 0, 1);
135 }
136 
137 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RWTUN,
138     &wsp_tuning.scale_factor, 0, "movement scale factor");
139 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RWTUN,
140     &wsp_tuning.z_factor, 0, "Z-axis scale factor");
141 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_invert, CTLFLAG_RWTUN,
142     &wsp_tuning.z_invert, 0, "enable Z-axis inversion");
143 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RWTUN,
144     &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold");
145 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RWTUN,
146     &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold");
147 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RWTUN,
148     &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold");
149 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, max_finger_area, CTLFLAG_RWTUN,
150     &wsp_tuning.max_finger_area, 0, "maximum finger area");
151 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, max_double_tap_distance, CTLFLAG_RWTUN,
152     &wsp_tuning.max_double_tap_distance, 0, "maximum double-finger click distance");
153 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_hor_threshold, CTLFLAG_RWTUN,
154     &wsp_tuning.scr_hor_threshold, 0, "horizontal scrolling threshold");
155 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, enable_single_tap_clicks, CTLFLAG_RWTUN,
156     &wsp_tuning.enable_single_tap_clicks, 0, "enable single tap clicks");
157 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, enable_single_tap_movement, CTLFLAG_RWTUN,
158     &wsp_tuning.enable_single_tap_movement, 0, "enable single tap movement");
159 
160 
161 /*
162  * Some tables, structures, definitions and constant values for the
163  * touchpad protocol has been copied from Linux's
164  * "drivers/input/mouse/bcm5974.c" which has the following copyright
165  * holders under GPLv2. All device specific code in this driver has
166  * been written from scratch. The decoding algorithm is based on
167  * output from FreeBSD's usbdump.
168  *
169  * Copyright (C) 2008      Henrik Rydberg (rydberg@euromail.se)
170  * Copyright (C) 2008      Scott Shawcroft (scott.shawcroft@gmail.com)
171  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
172  * Copyright (C) 2005      Johannes Berg (johannes@sipsolutions.net)
173  * Copyright (C) 2005      Stelian Pop (stelian@popies.net)
174  * Copyright (C) 2005      Frank Arnold (frank@scirocco-5v-turbo.de)
175  * Copyright (C) 2005      Peter Osterlund (petero2@telia.com)
176  * Copyright (C) 2005      Michael Hanselmann (linux-kernel@hansmi.ch)
177  * Copyright (C) 2006      Nicolas Boichat (nicolas@boichat.ch)
178  */
179 
180 /* button data structure */
181 struct bt_data {
182 	uint8_t	unknown1;		/* constant */
183 	uint8_t	button;			/* left button */
184 	uint8_t	rel_x;			/* relative x coordinate */
185 	uint8_t	rel_y;			/* relative y coordinate */
186 } __packed;
187 
188 /* trackpad header types */
189 enum tp_type {
190 	TYPE1,			/* plain trackpad */
191 	TYPE2,			/* button integrated in trackpad */
192 	TYPE3,			/* additional header fields since June 2013 */
193 	TYPE4,                  /* additional header field for pressure data */
194 	TYPE_CNT
195 };
196 
197 /* trackpad finger data offsets, le16-aligned */
198 #define	FINGER_TYPE1		(13 * 2)
199 #define	FINGER_TYPE2		(15 * 2)
200 #define	FINGER_TYPE3		(19 * 2)
201 #define	FINGER_TYPE4		(23 * 2)
202 
203 /* trackpad button data offsets */
204 #define	BUTTON_TYPE2		15
205 #define	BUTTON_TYPE3		23
206 #define	BUTTON_TYPE4		31
207 
208 /* list of device capability bits */
209 #define	HAS_INTEGRATED_BUTTON	1
210 
211 /* trackpad finger data block size */
212 #define FSIZE_TYPE1             (14 * 2)
213 #define FSIZE_TYPE2             (14 * 2)
214 #define FSIZE_TYPE3             (14 * 2)
215 #define FSIZE_TYPE4             (15 * 2)
216 
217 struct wsp_tp {
218 	uint8_t	caps;			/* device capability bitmask */
219 	uint8_t	button;			/* offset to button data */
220 	uint8_t	offset;			/* offset to trackpad finger data */
221 	uint8_t fsize;			/* bytes in single finger block */
222 	uint8_t delta;			/* offset from header to finger struct */
223 	uint8_t iface_index;
224 	uint8_t um_size;		/* usb control message length */
225 	uint8_t um_req_idx;		/* usb control message index */
226 	uint8_t um_switch_idx;		/* usb control message mode switch index */
227 	uint8_t um_switch_on;		/* usb control message mode switch on */
228 	uint8_t um_switch_off;		/* usb control message mode switch off */
229 } const static wsp_tp[TYPE_CNT] = {
230 	[TYPE1] = {
231 		.caps = 0,
232 		.button = 0,
233 		.offset = FINGER_TYPE1,
234 		.fsize = FSIZE_TYPE1,
235 		.delta = 0,
236 		.iface_index = 0,
237 		.um_size = 8,
238 		.um_req_idx = 0x00,
239 		.um_switch_idx = 0,
240 		.um_switch_on = 0x01,
241 		.um_switch_off = 0x08,
242 	},
243 	[TYPE2] = {
244 		.caps = HAS_INTEGRATED_BUTTON,
245 		.button = BUTTON_TYPE2,
246 		.offset = FINGER_TYPE2,
247 		.fsize = FSIZE_TYPE2,
248 		.delta = 0,
249 		.iface_index = 0,
250 		.um_size = 8,
251 		.um_req_idx = 0x00,
252 		.um_switch_idx = 0,
253 		.um_switch_on = 0x01,
254 		.um_switch_off = 0x08,
255 	},
256 	[TYPE3] = {
257 		.caps = HAS_INTEGRATED_BUTTON,
258 		.button = BUTTON_TYPE3,
259 		.offset = FINGER_TYPE3,
260 		.fsize = FSIZE_TYPE3,
261 		.delta = 0,
262 	},
263 	[TYPE4] = {
264 		.caps = HAS_INTEGRATED_BUTTON,
265 		.button = BUTTON_TYPE4,
266 		.offset = FINGER_TYPE4,
267 		.fsize = FSIZE_TYPE4,
268 		.delta = 2,
269 		.iface_index = 2,
270 		.um_size = 2,
271 		.um_req_idx = 0x02,
272 		.um_switch_idx = 1,
273 		.um_switch_on = 0x01,
274 		.um_switch_off = 0x00,
275 	},
276 };
277 
278 /* trackpad finger header - little endian */
279 struct tp_header {
280 	uint8_t	flag;
281 	uint8_t	sn0;
282 	uint16_t wFixed0;
283 	uint32_t dwSn1;
284 	uint32_t dwFixed1;
285 	uint16_t wLength;
286 	uint8_t	nfinger;
287 	uint8_t	ibt;
288 	int16_t	wUnknown[6];
289 	uint8_t	q1;
290 	uint8_t	q2;
291 } __packed;
292 
293 /* trackpad finger structure - little endian */
294 struct tp_finger {
295 	int16_t	origin;			/* zero when switching track finger */
296 	int16_t	abs_x;			/* absolute x coodinate */
297 	int16_t	abs_y;			/* absolute y coodinate */
298 	int16_t	rel_x;			/* relative x coodinate */
299 	int16_t	rel_y;			/* relative y coodinate */
300 	int16_t	tool_major;		/* tool area, major axis */
301 	int16_t	tool_minor;		/* tool area, minor axis */
302 	int16_t	orientation;		/* 16384 when point, else 15 bit angle */
303 	int16_t	touch_major;		/* touch area, major axis */
304 	int16_t	touch_minor;		/* touch area, minor axis */
305 	int16_t	unused[2];		/* zeros */
306 	int16_t pressure;		/* pressure on forcetouch touchpad */
307 	int16_t	multi;			/* one finger: varies, more fingers:
308 				 	 * constant */
309 } __packed;
310 
311 /* trackpad finger data size, empirically at least ten fingers */
312 #ifdef EVDEV_SUPPORT
313 #define	MAX_FINGERS		MAX_MT_SLOTS
314 #else
315 #define	MAX_FINGERS		16
316 #endif
317 #define	SIZEOF_FINGER		sizeof(struct tp_finger)
318 #define	SIZEOF_ALL_FINGERS	(MAX_FINGERS * SIZEOF_FINGER)
319 #define	MAX_FINGER_ORIENTATION	16384
320 
321 #if (WSP_BUFFER_MAX < ((MAX_FINGERS * FSIZE_TYPE4) + FINGER_TYPE4))
322 #error "WSP_BUFFER_MAX is too small"
323 #endif
324 
325 enum {
326 	WSP_FLAG_WELLSPRING1,
327 	WSP_FLAG_WELLSPRING2,
328 	WSP_FLAG_WELLSPRING3,
329 	WSP_FLAG_WELLSPRING4,
330 	WSP_FLAG_WELLSPRING4A,
331 	WSP_FLAG_WELLSPRING5,
332 	WSP_FLAG_WELLSPRING6A,
333 	WSP_FLAG_WELLSPRING6,
334 	WSP_FLAG_WELLSPRING5A,
335 	WSP_FLAG_WELLSPRING7,
336 	WSP_FLAG_WELLSPRING7A,
337 	WSP_FLAG_WELLSPRING8,
338 	WSP_FLAG_WELLSPRING9,
339 	WSP_FLAG_MAX,
340 };
341 
342 /* device-specific parameters */
343 struct wsp_param {
344 	int snratio;			/* signal-to-noise ratio */
345 	int min;			/* device minimum reading */
346 	int max;			/* device maximum reading */
347 	int size;			/* physical size, mm */
348 };
349 
350 /* device-specific configuration */
351 struct wsp_dev_params {
352 	const struct wsp_tp* tp;
353 	struct wsp_param p;		/* finger pressure limits */
354 	struct wsp_param w;		/* finger width limits */
355 	struct wsp_param x;		/* horizontal limits */
356 	struct wsp_param y;		/* vertical limits */
357 	struct wsp_param o;		/* orientation limits */
358 };
359 
360 /* logical signal quality */
361 #define	SN_PRESSURE	45		/* pressure signal-to-noise ratio */
362 #define	SN_WIDTH	25		/* width signal-to-noise ratio */
363 #define	SN_COORD	250		/* coordinate signal-to-noise ratio */
364 #define	SN_ORIENT	10		/* orientation signal-to-noise ratio */
365 
366 static const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = {
367 	[WSP_FLAG_WELLSPRING1] = {
368 		.tp = wsp_tp + TYPE1,
369 		.p = { SN_PRESSURE, 0, 256, 0 },
370 		.w = { SN_WIDTH, 0, 2048, 0 },
371 		.x = { SN_COORD, -4824, 5342, 105 },
372 		.y = { SN_COORD, -172, 5820, 75 },
373 		.o = { SN_ORIENT,
374 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
375 	},
376 	[WSP_FLAG_WELLSPRING2] = {
377 		.tp = wsp_tp + TYPE1,
378 		.p = { SN_PRESSURE, 0, 256, 0 },
379 		.w = { SN_WIDTH, 0, 2048, 0 },
380 		.x = { SN_COORD, -4824, 4824, 105 },
381 		.y = { SN_COORD, -172, 4290, 75 },
382 		.o = { SN_ORIENT,
383 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
384 	},
385 	[WSP_FLAG_WELLSPRING3] = {
386 		.tp = wsp_tp + TYPE2,
387 		.p = { SN_PRESSURE, 0, 300, 0 },
388 		.w = { SN_WIDTH, 0, 2048, 0 },
389 		.x = { SN_COORD, -4460, 5166, 105 },
390 		.y = { SN_COORD, -75, 6700, 75 },
391 		.o = { SN_ORIENT,
392 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
393 	},
394 	[WSP_FLAG_WELLSPRING4] = {
395 		.tp = wsp_tp + TYPE2,
396 		.p = { SN_PRESSURE, 0, 300, 0 },
397 		.w = { SN_WIDTH, 0, 2048, 0 },
398 		.x = { SN_COORD, -4620, 5140, 105 },
399 		.y = { SN_COORD, -150, 6600, 75 },
400 		.o = { SN_ORIENT,
401 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
402 	},
403 	[WSP_FLAG_WELLSPRING4A] = {
404 		.tp = wsp_tp + TYPE2,
405 		.p = { SN_PRESSURE, 0, 300, 0 },
406 		.w = { SN_WIDTH, 0, 2048, 0 },
407 		.x = { SN_COORD, -4616, 5112, 105 },
408 		.y = { SN_COORD, -142, 5234, 75 },
409 		.o = { SN_ORIENT,
410 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
411 	},
412 	[WSP_FLAG_WELLSPRING5] = {
413 		.tp = wsp_tp + TYPE2,
414 		.p = { SN_PRESSURE, 0, 300, 0 },
415 		.w = { SN_WIDTH, 0, 2048, 0 },
416 		.x = { SN_COORD, -4415, 5050, 105 },
417 		.y = { SN_COORD, -55, 6680, 75 },
418 		.o = { SN_ORIENT,
419 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
420 	},
421 	[WSP_FLAG_WELLSPRING6] = {
422 		.tp = wsp_tp + TYPE2,
423 		.p = { SN_PRESSURE, 0, 300, 0 },
424 		.w = { SN_WIDTH, 0, 2048, 0 },
425 		.x = { SN_COORD, -4620, 5140, 105 },
426 		.y = { SN_COORD, -150, 6600, 75 },
427 		.o = { SN_ORIENT,
428 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
429 	},
430 	[WSP_FLAG_WELLSPRING5A] = {
431 		.tp = wsp_tp + TYPE2,
432 		.p = { SN_PRESSURE, 0, 300, 0 },
433 		.w = { SN_WIDTH, 0, 2048, 0 },
434 		.x = { SN_COORD, -4750, 5280, 105 },
435 		.y = { SN_COORD, -150, 6730, 75 },
436 		.o = { SN_ORIENT,
437 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
438 	},
439 	[WSP_FLAG_WELLSPRING6A] = {
440 		.tp = wsp_tp + TYPE2,
441 		.p = { SN_PRESSURE, 0, 300, 0 },
442 		.w = { SN_WIDTH, 0, 2048, 0 },
443 		.x = { SN_COORD, -4620, 5140, 105 },
444 		.y = { SN_COORD, -150, 6600, 75 },
445 		.o = { SN_ORIENT,
446 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
447 	},
448 	[WSP_FLAG_WELLSPRING7] = {
449 		.tp = wsp_tp + TYPE2,
450 		.p = { SN_PRESSURE, 0, 300, 0 },
451 		.w = { SN_WIDTH, 0, 2048, 0 },
452 		.x = { SN_COORD, -4750, 5280, 105 },
453 		.y = { SN_COORD, -150, 6730, 75 },
454 		.o = { SN_ORIENT,
455 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
456 	},
457 	[WSP_FLAG_WELLSPRING7A] = {
458 		.tp = wsp_tp + TYPE2,
459 		.p = { SN_PRESSURE, 0, 300, 0 },
460 		.w = { SN_WIDTH, 0, 2048, 0 },
461 		.x = { SN_COORD, -4750, 5280, 105 },
462 		.y = { SN_COORD, -150, 6730, 75 },
463 		.o = { SN_ORIENT,
464 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
465 	},
466 	[WSP_FLAG_WELLSPRING8] = {
467 		.tp = wsp_tp + TYPE3,
468 		.p = { SN_PRESSURE, 0, 300, 0 },
469 		.w = { SN_WIDTH, 0, 2048, 0 },
470 		.x = { SN_COORD, -4620, 5140, 105 },
471 		.y = { SN_COORD, -150, 6600, 75 },
472 		.o = { SN_ORIENT,
473 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
474 	},
475 	[WSP_FLAG_WELLSPRING9] = {
476 		.tp = wsp_tp + TYPE4,
477 		.p = { SN_PRESSURE, 0, 300, 0 },
478 		.w = { SN_WIDTH, 0, 2048, 0 },
479 		.x = { SN_COORD, -4828, 5345, 105 },
480 		.y = { SN_COORD, -203, 6803, 75 },
481 		.o = { SN_ORIENT,
482 		    -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, 0 },
483 	},
484 };
485 #define	WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
486 
487 static const STRUCT_USB_HOST_ID wsp_devs[] = {
488 	/* MacbookAir1.1 */
489 	WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1),
490 	WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1),
491 	WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1),
492 
493 	/* MacbookProPenryn, aka wellspring2 */
494 	WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2),
495 	WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2),
496 	WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2),
497 
498 	/* Macbook5,1 (unibody), aka wellspring3 */
499 	WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3),
500 	WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3),
501 	WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3),
502 
503 	/* MacbookAir3,2 (unibody), aka wellspring4 */
504 	WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4),
505 	WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4),
506 	WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4),
507 
508 	/* MacbookAir3,1 (unibody), aka wellspring4 */
509 	WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A),
510 	WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A),
511 	WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A),
512 
513 	/* Macbook8 (unibody, March 2011) */
514 	WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5),
515 	WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5),
516 	WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5),
517 
518 	/* MacbookAir4,1 (unibody, July 2011) */
519 	WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A),
520 	WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A),
521 	WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A),
522 
523 	/* MacbookAir4,2 (unibody, July 2011) */
524 	WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6),
525 	WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6),
526 	WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6),
527 
528 	/* Macbook8,2 (unibody) */
529 	WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A),
530 	WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A),
531 	WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A),
532 
533 	/* MacbookPro10,1 (unibody, June 2012) */
534 	/* MacbookPro11,1-3 (unibody, June 2013) */
535 	WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7),
536 	WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7),
537 	WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7),
538 
539 	/* MacbookPro10,2 (unibody, October 2012) */
540 	WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A),
541 	WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A),
542 	WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A),
543 
544 	/* MacbookAir6,2 (unibody, June 2013) */
545 	WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8),
546 	WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8),
547 	WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8),
548 
549 	/* MacbookPro12,1 MacbookPro11,4 */
550 	WSP_DEV(APPLE, WELLSPRING9_ANSI, WSP_FLAG_WELLSPRING9),
551 	WSP_DEV(APPLE, WELLSPRING9_ISO, WSP_FLAG_WELLSPRING9),
552 	WSP_DEV(APPLE, WELLSPRING9_JIS, WSP_FLAG_WELLSPRING9),
553 };
554 
555 #define	WSP_FIFO_BUF_SIZE	 8	/* bytes */
556 #define	WSP_FIFO_QUEUE_MAXLEN	50	/* units */
557 
558 enum {
559 	WSP_INTR_DT,
560 	WSP_N_TRANSFER,
561 };
562 
563 struct wsp_softc {
564 	struct usb_device *sc_usb_device;
565 	struct mtx sc_mutex;		/* for synchronization */
566 	struct usb_xfer *sc_xfer[WSP_N_TRANSFER];
567 	struct usb_fifo_sc sc_fifo;
568 
569 	const struct wsp_dev_params *sc_params;	/* device configuration */
570 
571 #ifdef EVDEV_SUPPORT
572 	struct evdev_dev *sc_evdev;
573 #endif
574 	mousehw_t sc_hw;
575 	mousemode_t sc_mode;
576 	u_int	sc_pollrate;
577 	mousestatus_t sc_status;
578 	int	sc_fflags;
579 	u_int	sc_state;
580 #define	WSP_ENABLED		0x01
581 #define	WSP_EVDEV_OPENED	0x02
582 
583 	struct tp_finger *index[MAX_FINGERS];	/* finger index data */
584 	int16_t	pos_x[MAX_FINGERS];	/* position array */
585 	int16_t	pos_y[MAX_FINGERS];	/* position array */
586 	int16_t pre_pos_x[MAX_FINGERS];	/* previous position array */
587 	int16_t pre_pos_y[MAX_FINGERS]; /* previous position array */
588 	u_int	sc_touch;		/* touch status */
589 #define	WSP_UNTOUCH		0x00
590 #define	WSP_FIRST_TOUCH		0x01
591 #define	WSP_SECOND_TOUCH	0x02
592 #define	WSP_TOUCHING		0x04
593 	int	dx_sum;			/* x axis cumulative movement */
594 	int	dy_sum;			/* y axis cumulative movement */
595 	int	dz_sum;			/* z axis cumulative movement */
596 	int	dz_count;
597 #define	WSP_DZ_MAX_COUNT	32
598 	int	dt_sum;			/* T-axis cumulative movement */
599 	int	rdx;			/* x axis remainder of divide by scale_factor */
600 	int	rdy;			/* y axis remainder of divide by scale_factor */
601 	int	rdz;			/* z axis remainder of divide by scale_factor */
602 	int	tp_datalen;
603 	uint8_t o_ntouch;		/* old touch finger status */
604 	uint8_t	finger;			/* 0 or 1 *, check which finger moving */
605 	uint16_t intr_count;
606 #define	WSP_TAP_THRESHOLD	3
607 #define	WSP_TAP_MAX_COUNT	20
608 	int	distance;		/* the distance of 2 fingers */
609 	uint8_t	ibtn;			/* button status in tapping */
610 	uint8_t	ntaps;			/* finger status in tapping */
611 	uint8_t	scr_mode;		/* scroll status in movement */
612 #define	WSP_SCR_NONE		0
613 #define	WSP_SCR_VER		1
614 #define	WSP_SCR_HOR		2
615 	uint8_t tp_data[WSP_BUFFER_MAX] __aligned(4);		/* trackpad transferred data */
616 };
617 
618 /*
619  * function prototypes
620  */
621 static usb_fifo_cmd_t wsp_fifo_start_read;
622 static usb_fifo_cmd_t wsp_fifo_stop_read;
623 static usb_fifo_open_t wsp_open;
624 static usb_fifo_close_t wsp_close;
625 static usb_fifo_ioctl_t wsp_ioctl;
626 
627 static struct usb_fifo_methods wsp_fifo_methods = {
628 	.f_open = &wsp_open,
629 	.f_close = &wsp_close,
630 	.f_ioctl = &wsp_ioctl,
631 	.f_start_read = &wsp_fifo_start_read,
632 	.f_stop_read = &wsp_fifo_stop_read,
633 	.basename[0] = WSP_DRIVER_NAME,
634 };
635 
636 #ifdef EVDEV_SUPPORT
637 static evdev_open_t wsp_ev_open;
638 static evdev_close_t wsp_ev_close;
639 static const struct evdev_methods wsp_evdev_methods = {
640 	.ev_open = &wsp_ev_open,
641 	.ev_close = &wsp_ev_close,
642 };
643 #endif
644 
645 /* device initialization and shutdown */
646 static int wsp_enable(struct wsp_softc *sc);
647 static void wsp_disable(struct wsp_softc *sc);
648 
649 /* updating fifo */
650 static void wsp_reset_buf(struct wsp_softc *sc);
651 static void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t);
652 
653 /* Device methods. */
654 static device_probe_t wsp_probe;
655 static device_attach_t wsp_attach;
656 static device_detach_t wsp_detach;
657 static usb_callback_t wsp_intr_callback;
658 
659 static const struct usb_config wsp_config[WSP_N_TRANSFER] = {
660 	[WSP_INTR_DT] = {
661 		.type = UE_INTERRUPT,
662 		.endpoint = UE_ADDR_ANY,
663 		.direction = UE_DIR_IN,
664 		.flags = {
665 			.pipe_bof = 0,
666 			.short_xfer_ok = 1,
667 		},
668 		.bufsize = WSP_BUFFER_MAX,
669 		.callback = &wsp_intr_callback,
670 	},
671 };
672 
673 static usb_error_t
674 wsp_set_device_mode(struct wsp_softc *sc, uint8_t on)
675 {
676 	const struct wsp_dev_params *params = sc->sc_params;
677 	uint8_t	mode_bytes[8];
678 	usb_error_t err;
679 
680 	/* Type 3 does not require a mode switch */
681 	if (params->tp == wsp_tp + TYPE3)
682 		return 0;
683 
684 	err = usbd_req_get_report(sc->sc_usb_device, NULL,
685 	    mode_bytes, params->tp->um_size, params->tp->iface_index,
686 	    UHID_FEATURE_REPORT, params->tp->um_req_idx);
687 
688 	if (err != USB_ERR_NORMAL_COMPLETION) {
689 		DPRINTF("Failed to read device mode (%d)\n", err);
690 		return (err);
691 	}
692 
693 	/*
694 	 * XXX Need to wait at least 250ms for hardware to get
695 	 * ready. The device mode handling appears to be handled
696 	 * asynchronously and we should not issue these commands too
697 	 * quickly.
698 	 */
699 	pause("WHW", hz / 4);
700 
701 	mode_bytes[params->tp->um_switch_idx] =
702 	    on ? params->tp->um_switch_on : params->tp->um_switch_off;
703 
704 	return (usbd_req_set_report(sc->sc_usb_device, NULL,
705 	    mode_bytes, params->tp->um_size, params->tp->iface_index,
706 	    UHID_FEATURE_REPORT, params->tp->um_req_idx));
707 }
708 
709 static int
710 wsp_enable(struct wsp_softc *sc)
711 {
712 	/* reset status */
713 	memset(&sc->sc_status, 0, sizeof(sc->sc_status));
714 	sc->sc_state |= WSP_ENABLED;
715 
716 	DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n");
717 	return (0);
718 }
719 
720 static void
721 wsp_disable(struct wsp_softc *sc)
722 {
723 	sc->sc_state &= ~WSP_ENABLED;
724 	DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n");
725 }
726 
727 static int
728 wsp_probe(device_t self)
729 {
730 	struct usb_attach_arg *uaa = device_get_ivars(self);
731 	struct usb_interface_descriptor *id;
732 	struct usb_interface *iface;
733 	uint8_t i;
734 
735 	if (uaa->usb_mode != USB_MODE_HOST)
736 		return (ENXIO);
737 
738 	/* figure out first interface matching */
739 	for (i = 1;; i++) {
740 		iface = usbd_get_iface(uaa->device, i);
741 		if (iface == NULL || i == 3)
742 			return (ENXIO);
743 		id = iface->idesc;
744 		if ((id == NULL) ||
745 		    (id->bInterfaceClass != UICLASS_HID) ||
746 		    (id->bInterfaceProtocol != 0 &&
747 		    id->bInterfaceProtocol != UIPROTO_MOUSE))
748 			continue;
749 		break;
750 	}
751 	/* check if we are attaching to the first match */
752 	if (uaa->info.bIfaceIndex != i)
753 		return (ENXIO);
754 	if (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa) != 0)
755 		return (ENXIO);
756 
757 	return (BUS_PROBE_DEFAULT);
758 }
759 
760 static int
761 wsp_attach(device_t dev)
762 {
763 	struct wsp_softc *sc = device_get_softc(dev);
764 	struct usb_attach_arg *uaa = device_get_ivars(dev);
765 	usb_error_t err;
766 	void *d_ptr = NULL;
767 	uint16_t d_len;
768 
769 	DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc);
770 
771 	/* Get HID descriptor */
772 	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
773 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
774 
775 	if (err == USB_ERR_NORMAL_COMPLETION) {
776 		/* Get HID report descriptor length */
777 		sc->tp_datalen = hid_report_size_max(d_ptr, d_len, hid_input,
778 		    NULL);
779 		free(d_ptr, M_TEMP);
780 
781 		if (sc->tp_datalen <= 0 || sc->tp_datalen > WSP_BUFFER_MAX) {
782 			DPRINTF("Invalid datalength or too big "
783 			    "datalength: %d\n", sc->tp_datalen);
784 			return (ENXIO);
785 		}
786 	} else {
787 		return (ENXIO);
788 	}
789 
790 	sc->sc_usb_device = uaa->device;
791 
792 	/* get device specific configuration */
793 	sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa);
794 
795 	/*
796 	 * By default the touchpad behaves like a HID device, sending
797 	 * packets with reportID = 8. Such reports contain only
798 	 * limited information. They encode movement deltas and button
799 	 * events, but do not include data from the pressure
800 	 * sensors. The device input mode can be switched from HID
801 	 * reports to raw sensor data using vendor-specific USB
802 	 * control commands:
803 	 */
804 
805 	/*
806 	 * During re-enumeration of the device we need to force the
807 	 * device back into HID mode before switching it to RAW
808 	 * mode. Else the device does not work like expected.
809 	 */
810 	err = wsp_set_device_mode(sc, 0);
811 	if (err != USB_ERR_NORMAL_COMPLETION) {
812 		DPRINTF("Failed to set mode to HID MODE (%d)\n", err);
813 		return (ENXIO);
814 	}
815 
816 	err = wsp_set_device_mode(sc, 1);
817 	if (err != USB_ERR_NORMAL_COMPLETION) {
818 		DPRINTF("failed to set mode to RAW MODE (%d)\n", err);
819 		return (ENXIO);
820 	}
821 
822 	mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE);
823 
824 	err = usbd_transfer_setup(uaa->device,
825 	    &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config,
826 	    WSP_N_TRANSFER, sc, &sc->sc_mutex);
827 	if (err) {
828 		DPRINTF("error=%s\n", usbd_errstr(err));
829 		goto detach;
830 	}
831 	if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex,
832 	    &wsp_fifo_methods, &sc->sc_fifo,
833 	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
834 	    UID_ROOT, GID_OPERATOR, 0644)) {
835 		goto detach;
836 	}
837 	device_set_usb_desc(dev);
838 
839 	sc->sc_hw.buttons = 3;
840 	sc->sc_hw.iftype = MOUSE_IF_USB;
841 	sc->sc_hw.type = MOUSE_PAD;
842 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
843 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
844 	sc->sc_mode.rate = -1;
845 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
846 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
847 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
848 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
849 
850 	sc->sc_touch = WSP_UNTOUCH;
851 	sc->scr_mode = WSP_SCR_NONE;
852 
853 #ifdef EVDEV_SUPPORT
854 	sc->sc_evdev = evdev_alloc();
855 	evdev_set_name(sc->sc_evdev, device_get_desc(dev));
856 	evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
857 	evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor,
858 	    uaa->info.idProduct, 0);
859 	evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device));
860 	evdev_set_methods(sc->sc_evdev, sc, &wsp_evdev_methods);
861 	evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER);
862 	evdev_support_event(sc->sc_evdev, EV_SYN);
863 	evdev_support_event(sc->sc_evdev, EV_ABS);
864 	evdev_support_event(sc->sc_evdev, EV_KEY);
865 
866 #define WSP_SUPPORT_ABS(evdev, code, param)				\
867 	evdev_support_abs((evdev), (code), (param).min, (param).max,	\
868 	((param).max - (param).min) / (param).snratio, 0,		\
869 	(param).size != 0 ? ((param).max - (param).min) / (param).size : 0);
870 
871 	/* finger position */
872 	WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_POSITION_X, sc->sc_params->x);
873 	WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_POSITION_Y, sc->sc_params->y);
874 	/* finger pressure */
875 	WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_PRESSURE, sc->sc_params->p);
876 	/* finger touch area */
877 	WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_TOUCH_MAJOR, sc->sc_params->w);
878 	WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_TOUCH_MINOR, sc->sc_params->w);
879 	/* finger approach area */
880 	WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_WIDTH_MAJOR, sc->sc_params->w);
881 	WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_WIDTH_MINOR, sc->sc_params->w);
882 	/* finger orientation */
883 	WSP_SUPPORT_ABS(sc->sc_evdev, ABS_MT_ORIENTATION, sc->sc_params->o);
884 	/* button properties */
885 	evdev_support_key(sc->sc_evdev, BTN_LEFT);
886 	if ((sc->sc_params->tp->caps & HAS_INTEGRATED_BUTTON) != 0)
887 		evdev_support_prop(sc->sc_evdev, INPUT_PROP_BUTTONPAD);
888 	/* Enable automatic touch assignment for type B MT protocol */
889 	evdev_support_abs(sc->sc_evdev, ABS_MT_SLOT,
890 	    0, MAX_FINGERS - 1, 0, 0, 0);
891 	evdev_support_abs(sc->sc_evdev, ABS_MT_TRACKING_ID,
892 	    -1, MAX_FINGERS - 1, 0, 0, 0);
893 	evdev_set_flag(sc->sc_evdev, EVDEV_FLAG_MT_TRACK);
894 	evdev_set_flag(sc->sc_evdev, EVDEV_FLAG_MT_AUTOREL);
895 	/* Synaptics compatibility events */
896 	evdev_set_flag(sc->sc_evdev, EVDEV_FLAG_MT_STCOMPAT);
897 
898 	err = evdev_register(sc->sc_evdev);
899 	if (err)
900 		goto detach;
901 #endif
902 
903 	return (0);
904 
905 detach:
906 	wsp_detach(dev);
907 	return (ENOMEM);
908 }
909 
910 static int
911 wsp_detach(device_t dev)
912 {
913 	struct wsp_softc *sc = device_get_softc(dev);
914 
915 	(void) wsp_set_device_mode(sc, 0);
916 
917 	mtx_lock(&sc->sc_mutex);
918 	if (sc->sc_state & WSP_ENABLED)
919 		wsp_disable(sc);
920 	mtx_unlock(&sc->sc_mutex);
921 
922 	usb_fifo_detach(&sc->sc_fifo);
923 
924 #ifdef EVDEV_SUPPORT
925 	evdev_free(sc->sc_evdev);
926 #endif
927 
928 	usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER);
929 
930 	mtx_destroy(&sc->sc_mutex);
931 
932 	return (0);
933 }
934 
935 static void
936 wsp_intr_callback(struct usb_xfer *xfer, usb_error_t error)
937 {
938 	struct wsp_softc *sc = usbd_xfer_softc(xfer);
939 	const struct wsp_dev_params *params = sc->sc_params;
940 	struct usb_page_cache *pc;
941 	struct tp_finger *f;
942 	struct wsp_tuning tun = wsp_tuning;
943 	int ntouch = 0;			/* the finger number in touch */
944 	int ibt = 0;			/* button status */
945 	int dx = 0;
946 	int dy = 0;
947 	int dz = 0;
948 	int rdx = 0;
949 	int rdy = 0;
950 	int rdz = 0;
951 	int len;
952 	int i;
953 #ifdef EVDEV_SUPPORT
954 	int slot = 0;
955 #endif
956 
957 	wsp_runing_rangecheck(&tun);
958 
959 	if (sc->dz_count == 0)
960 		sc->dz_count = WSP_DZ_MAX_COUNT;
961 
962 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
963 
964 	switch (USB_GET_STATE(xfer)) {
965 	case USB_ST_TRANSFERRED:
966 
967 		/* copy out received data */
968 		pc = usbd_xfer_get_frame(xfer, 0);
969 		usbd_copy_out(pc, 0, sc->tp_data, len);
970 
971 		if ((len < params->tp->offset + params->tp->fsize) ||
972 		    ((len - params->tp->offset) % params->tp->fsize) != 0) {
973 			DPRINTFN(WSP_LLEVEL_INFO, "Invalid length: %d, %x, %x\n",
974 			    len, sc->tp_data[0], sc->tp_data[1]);
975 			goto tr_setup;
976 		}
977 
978 		if (len < sc->tp_datalen) {
979 			/* make sure we don't process old data */
980 			memset(sc->tp_data + len, 0, sc->tp_datalen - len);
981 		}
982 
983 		if (params->tp != wsp_tp + TYPE1) {
984 			ibt = sc->tp_data[params->tp->button];
985 			ntouch = sc->tp_data[params->tp->button - 1];
986 		} else
987 			ntouch = (len - params->tp->offset) / params->tp->fsize;
988 
989 		/* range check */
990 		if (ntouch < 0)
991 			ntouch = 0;
992 		else if (ntouch > MAX_FINGERS)
993 			ntouch = MAX_FINGERS;
994 
995 		for (i = 0; i != ntouch; i++) {
996 			f = (struct tp_finger *)(sc->tp_data + params->tp->offset + params->tp->delta + i * params->tp->fsize);
997 			/* swap endianness, if any */
998 			if (le16toh(0x1234) != 0x1234) {
999 				f->origin = le16toh((uint16_t)f->origin);
1000 				f->abs_x = le16toh((uint16_t)f->abs_x);
1001 				f->abs_y = le16toh((uint16_t)f->abs_y);
1002 				f->rel_x = le16toh((uint16_t)f->rel_x);
1003 				f->rel_y = le16toh((uint16_t)f->rel_y);
1004 				f->tool_major = le16toh((uint16_t)f->tool_major);
1005 				f->tool_minor = le16toh((uint16_t)f->tool_minor);
1006 				f->orientation = le16toh((uint16_t)f->orientation);
1007 				f->touch_major = le16toh((uint16_t)f->touch_major);
1008 				f->touch_minor = le16toh((uint16_t)f->touch_minor);
1009 				f->pressure = le16toh((uint16_t)f->pressure);
1010 				f->multi = le16toh((uint16_t)f->multi);
1011 			}
1012 			DPRINTFN(WSP_LLEVEL_INFO,
1013 			    "[%d]ibt=%d, taps=%d, o=%4d, ax=%5d, ay=%5d, "
1014 			    "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%4x, "
1015 			    "tchmaj=%4d, tchmin=%4d, presure=%4d, m=%4x\n",
1016 			    i, ibt, ntouch, f->origin, f->abs_x, f->abs_y,
1017 			    f->rel_x, f->rel_y, f->tool_major, f->tool_minor, f->orientation,
1018 			    f->touch_major, f->touch_minor, f->pressure, f->multi);
1019 			sc->pos_x[i] = f->abs_x;
1020 			sc->pos_y[i] = -f->abs_y;
1021 			sc->index[i] = f;
1022 #ifdef EVDEV_SUPPORT
1023 			if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE && f->touch_major != 0) {
1024 				union evdev_mt_slot slot_data = {
1025 					.id = slot,
1026 					.x = f->abs_x,
1027 					.y = params->y.min + params->y.max - f->abs_y,
1028 					.p = f->pressure,
1029 					.maj = f->touch_major << 1,
1030 					.min = f->touch_minor << 1,
1031 					.w_maj = f->tool_major << 1,
1032 					.w_min = f->tool_minor << 1,
1033 					.ori = params->o.max - f->orientation,
1034 				};
1035 				evdev_mt_push_slot(sc->sc_evdev, slot, &slot_data);
1036 				slot++;
1037 			}
1038 #endif
1039 		}
1040 
1041 #ifdef EVDEV_SUPPORT
1042 		if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
1043 			evdev_push_key(sc->sc_evdev, BTN_LEFT, ibt);
1044 			evdev_sync(sc->sc_evdev);
1045 		}
1046 #endif
1047 		sc->sc_status.flags &= ~MOUSE_POSCHANGED;
1048 		sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED;
1049 		sc->sc_status.obutton = sc->sc_status.button;
1050 		sc->sc_status.button = 0;
1051 
1052 		if (ntouch == 2) {
1053 			sc->distance = max(sc->distance, max(
1054 			    abs(sc->pos_x[0] - sc->pos_x[1]),
1055 			    abs(sc->pos_y[0] - sc->pos_y[1])));
1056 		}
1057 
1058 		if (ibt != 0) {
1059 			if (params->tp->caps & HAS_INTEGRATED_BUTTON) {
1060 				switch (ntouch) {
1061 				case 1:
1062 					sc->sc_status.button |= MOUSE_BUTTON1DOWN;
1063 					break;
1064 				case 2:
1065 					if (sc->distance < tun.max_double_tap_distance && abs(sc->dx_sum) < 5 &&
1066 					    abs(sc->dy_sum) < 5)
1067 						sc->sc_status.button |= MOUSE_BUTTON3DOWN;
1068 					else
1069 						sc->sc_status.button |= MOUSE_BUTTON1DOWN;
1070 					break;
1071 				case 3:
1072 					sc->sc_status.button |= MOUSE_BUTTON2DOWN;
1073 					break;
1074 				default:
1075 					break;
1076 				}
1077 			} else {
1078 				sc->sc_status.button |= MOUSE_BUTTON1DOWN;
1079 			}
1080 
1081 			sc->ibtn = 1;
1082 		}
1083 		sc->intr_count++;
1084 
1085 		if (sc->ntaps < ntouch) {
1086 			switch (ntouch) {
1087 			case 1:
1088 				if (sc->index[0]->touch_major > tun.pressure_tap_threshold &&
1089 				    sc->index[0]->tool_major <= tun.max_finger_area)
1090 					sc->ntaps = 1;
1091 				break;
1092 			case 2:
1093 				if (sc->index[0]->touch_major > tun.pressure_tap_threshold-30 &&
1094 				    sc->index[1]->touch_major > tun.pressure_tap_threshold-30)
1095 					sc->ntaps = 2;
1096 				break;
1097 			case 3:
1098 				if (sc->index[0]->touch_major > tun.pressure_tap_threshold-40 &&
1099 				    sc->index[1]->touch_major > tun.pressure_tap_threshold-40 &&
1100 				    sc->index[2]->touch_major > tun.pressure_tap_threshold-40)
1101 					sc->ntaps = 3;
1102 				break;
1103 			default:
1104 				break;
1105 			}
1106 		}
1107 
1108 		if (sc->index[0]->touch_major < tun.pressure_untouch_threshold &&
1109 		    sc->sc_status.button == 0) {
1110 			sc->sc_touch = WSP_UNTOUCH;
1111 			if (sc->intr_count < WSP_TAP_MAX_COUNT &&
1112 			    sc->intr_count > WSP_TAP_THRESHOLD &&
1113 			    sc->ntaps && sc->ibtn == 0) {
1114 				/*
1115 				 * Add a pair of events (button-down and
1116 				 * button-up).
1117 				 */
1118 				switch (sc->ntaps) {
1119 				case 1:
1120 					if (!(params->tp->caps & HAS_INTEGRATED_BUTTON) || tun.enable_single_tap_clicks) {
1121 						wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN);
1122 						DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n");
1123 					}
1124 					break;
1125 				case 2:
1126 					DPRINTFN(WSP_LLEVEL_INFO, "sum_x=%5d, sum_y=%5d\n",
1127 					    sc->dx_sum, sc->dy_sum);
1128 					if (sc->distance < tun.max_double_tap_distance && abs(sc->dx_sum) < 5 &&
1129 					    abs(sc->dy_sum) < 5) {
1130 						wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN);
1131 						DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n");
1132 					}
1133 					break;
1134 				case 3:
1135 					wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN);
1136 					break;
1137 				default:
1138 					/* we don't handle taps of more than three fingers */
1139 					break;
1140 				}
1141 				wsp_add_to_queue(sc, 0, 0, 0, 0);	/* button release */
1142 			}
1143 			if ((sc->dt_sum / tun.scr_hor_threshold) != 0 &&
1144 			    sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) {
1145 				/*
1146 				 * translate T-axis into button presses
1147 				 * until further
1148 				 */
1149 				if (sc->dt_sum > 0)
1150 					wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3);
1151 				else if (sc->dt_sum < 0)
1152 					wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4);
1153 			}
1154 			sc->dz_count = WSP_DZ_MAX_COUNT;
1155 			sc->dz_sum = 0;
1156 			sc->intr_count = 0;
1157 			sc->ibtn = 0;
1158 			sc->ntaps = 0;
1159 			sc->finger = 0;
1160 			sc->distance = 0;
1161 			sc->dt_sum = 0;
1162 			sc->dx_sum = 0;
1163 			sc->dy_sum = 0;
1164 			sc->rdx = 0;
1165 			sc->rdy = 0;
1166 			sc->rdz = 0;
1167 			sc->scr_mode = WSP_SCR_NONE;
1168 		} else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold &&
1169 		    sc->sc_touch == WSP_UNTOUCH) {	/* ignore first touch */
1170 			sc->sc_touch = WSP_FIRST_TOUCH;
1171 		} else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold &&
1172 		    sc->sc_touch == WSP_FIRST_TOUCH) {	/* ignore second touch */
1173 			sc->sc_touch = WSP_SECOND_TOUCH;
1174 			DPRINTFN(WSP_LLEVEL_INFO, "First pre_x[0]=%5d, pre_y[0]=%5d\n",
1175 			    sc->pre_pos_x[0], sc->pre_pos_y[0]);
1176 		} else {
1177 			if (sc->sc_touch == WSP_SECOND_TOUCH)
1178 				sc->sc_touch = WSP_TOUCHING;
1179 
1180 			if (ntouch != 0 &&
1181 			    sc->index[0]->touch_major >= tun.pressure_touch_threshold) {
1182 				dx = sc->pos_x[0] - sc->pre_pos_x[0];
1183 				dy = sc->pos_y[0] - sc->pre_pos_y[0];
1184 
1185 				/* Optionally ignore movement during button is releasing */
1186 				if (tun.enable_single_tap_movement != 1 && sc->ibtn != 0 && sc->sc_status.button == 0)
1187 					dx = dy = 0;
1188 
1189 				/* Ignore movement if ntouch changed */
1190 				if (sc->o_ntouch != ntouch)
1191 					dx = dy = 0;
1192 
1193 				/* Ignore unexpected movement when typing (palm detection) */
1194 				if (ntouch == 1 && sc->index[0]->tool_major > tun.max_finger_area)
1195 					dx = dy = 0;
1196 
1197 				if (sc->ibtn != 0 && ntouch == 1 &&
1198 				    sc->intr_count < WSP_TAP_MAX_COUNT &&
1199 				    abs(sc->dx_sum) < 1 && abs(sc->dy_sum) < 1 )
1200 					dx = dy = 0;
1201 
1202 				if (ntouch == 2 && sc->sc_status.button != 0) {
1203 					dx = sc->pos_x[sc->finger] - sc->pre_pos_x[sc->finger];
1204 					dy = sc->pos_y[sc->finger] - sc->pre_pos_y[sc->finger];
1205 
1206 					/*
1207 					 * Ignore movement of switch finger or
1208 					 * movement from ibt=0 to ibt=1
1209 					 */
1210 					if (sc->index[0]->origin == 0 || sc->index[1]->origin == 0 ||
1211 					    sc->sc_status.obutton != sc->sc_status.button) {
1212 						dx = dy = 0;
1213 						sc->finger = 0;
1214 					}
1215 					if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) <
1216 					    (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) &&
1217 					    sc->finger == 0) {
1218 						sc->sc_touch = WSP_SECOND_TOUCH;
1219 						dx = dy = 0;
1220 						sc->finger = 1;
1221 					}
1222 					if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) >=
1223 					    (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) &&
1224 					    sc->finger == 1) {
1225 						sc->sc_touch = WSP_SECOND_TOUCH;
1226 						dx = dy = 0;
1227 						sc->finger = 0;
1228 					}
1229 					DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n",
1230 					    dx, dy, sc->finger);
1231 				}
1232 				if (sc->dz_count--) {
1233 					rdz = (dy + sc->rdz) % tun.scale_factor;
1234 					sc->dz_sum -= (dy + sc->rdz) / tun.scale_factor;
1235 					sc->rdz = rdz;
1236 				}
1237 				if ((sc->dz_sum / tun.z_factor) != 0)
1238 					sc->dz_count = 0;
1239 			}
1240 			rdx = (dx + sc->rdx) % tun.scale_factor;
1241 			dx = (dx + sc->rdx) / tun.scale_factor;
1242 			sc->rdx = rdx;
1243 
1244 			rdy = (dy + sc->rdy) % tun.scale_factor;
1245 			dy = (dy + sc->rdy) / tun.scale_factor;
1246 			sc->rdy = rdy;
1247 
1248 			sc->dx_sum += dx;
1249 			sc->dy_sum += dy;
1250 
1251 			if (ntouch == 2 && sc->sc_status.button == 0) {
1252 				if (sc->scr_mode == WSP_SCR_NONE &&
1253 				    abs(sc->dx_sum) + abs(sc->dy_sum) > tun.scr_hor_threshold)
1254 					sc->scr_mode = abs(sc->dx_sum) >
1255 					    abs(sc->dy_sum) * 2 ? WSP_SCR_HOR : WSP_SCR_VER;
1256 				DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n",
1257 				    sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum);
1258 				if (sc->scr_mode == WSP_SCR_HOR)
1259 					sc->dt_sum += dx;
1260 				else
1261 					sc->dt_sum = 0;
1262 
1263 				dx = dy = 0;
1264 				if (sc->dz_count == 0)
1265 					dz = (sc->dz_sum / tun.z_factor) * (tun.z_invert ? -1 : 1);
1266 				if (sc->scr_mode == WSP_SCR_HOR || sc->distance > tun.max_double_tap_distance)
1267 					dz = 0;
1268 			}
1269 			if (ntouch == 3)
1270 				dx = dy = dz = 0;
1271 			if (sc->intr_count < WSP_TAP_MAX_COUNT &&
1272 			    abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3)
1273 				dx = dy = dz = 0;
1274 			else
1275 				sc->intr_count = WSP_TAP_MAX_COUNT;
1276 			if (dx || dy || dz)
1277 				sc->sc_status.flags |= MOUSE_POSCHANGED;
1278 			DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n",
1279 			    dx, dy, dz, sc->sc_touch, sc->sc_status.button);
1280 			sc->sc_status.dx += dx;
1281 			sc->sc_status.dy += dy;
1282 			sc->sc_status.dz += dz;
1283 
1284 			wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button);
1285 			if (sc->dz_count == 0) {
1286 				sc->dz_sum = 0;
1287 				sc->rdz = 0;
1288 			}
1289 		}
1290 		sc->pre_pos_x[0] = sc->pos_x[0];
1291 		sc->pre_pos_y[0] = sc->pos_y[0];
1292 
1293 		if (ntouch == 2 && sc->sc_status.button != 0) {
1294 			sc->pre_pos_x[sc->finger] = sc->pos_x[sc->finger];
1295 			sc->pre_pos_y[sc->finger] = sc->pos_y[sc->finger];
1296 		}
1297 		sc->o_ntouch = ntouch;
1298 
1299 	case USB_ST_SETUP:
1300 tr_setup:
1301 		/* check if we can put more data into the FIFO */
1302 		if (usb_fifo_put_bytes_max(
1303 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
1304 			usbd_xfer_set_frame_len(xfer, 0,
1305 			    sc->tp_datalen);
1306 			usbd_transfer_submit(xfer);
1307 		}
1308 		break;
1309 
1310 	default:			/* Error */
1311 		if (error != USB_ERR_CANCELLED) {
1312 			/* try clear stall first */
1313 			usbd_xfer_set_stall(xfer);
1314 			goto tr_setup;
1315 		}
1316 		break;
1317 	}
1318 }
1319 
1320 static void
1321 wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz,
1322     uint32_t buttons_in)
1323 {
1324 	uint32_t buttons_out;
1325 	uint8_t buf[8];
1326 
1327 	dx = imin(dx, 254);
1328 	dx = imax(dx, -256);
1329 	dy = imin(dy, 254);
1330 	dy = imax(dy, -256);
1331 	dz = imin(dz, 126);
1332 	dz = imax(dz, -128);
1333 
1334 	buttons_out = MOUSE_MSC_BUTTONS;
1335 	if (buttons_in & MOUSE_BUTTON1DOWN)
1336 		buttons_out &= ~MOUSE_MSC_BUTTON1UP;
1337 	else if (buttons_in & MOUSE_BUTTON2DOWN)
1338 		buttons_out &= ~MOUSE_MSC_BUTTON2UP;
1339 	else if (buttons_in & MOUSE_BUTTON3DOWN)
1340 		buttons_out &= ~MOUSE_MSC_BUTTON3UP;
1341 
1342 	/* Encode the mouse data in standard format; refer to mouse(4) */
1343 	buf[0] = sc->sc_mode.syncmask[1];
1344 	buf[0] |= buttons_out;
1345 	buf[1] = dx >> 1;
1346 	buf[2] = dy >> 1;
1347 	buf[3] = dx - (dx >> 1);
1348 	buf[4] = dy - (dy >> 1);
1349 	/* Encode extra bytes for level 1 */
1350 	if (sc->sc_mode.level == 1) {
1351 		buf[5] = dz >> 1;	/* dz / 2 */
1352 		buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */
1353 		buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS);
1354 	}
1355 	usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
1356 	    sc->sc_mode.packetsize, 1);
1357 }
1358 
1359 static void
1360 wsp_reset_buf(struct wsp_softc *sc)
1361 {
1362 	/* reset read queue */
1363 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
1364 }
1365 
1366 static void
1367 wsp_start_read(struct wsp_softc *sc)
1368 {
1369 	int rate;
1370 
1371 	/* Check if we should override the default polling interval */
1372 	rate = sc->sc_pollrate;
1373 	/* Range check rate */
1374 	if (rate > 1000)
1375 		rate = 1000;
1376 	/* Check for set rate */
1377 	if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) {
1378 		/* Stop current transfer, if any */
1379 		usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1380 		/* Set new interval */
1381 		usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate);
1382 		/* Only set pollrate once */
1383 		sc->sc_pollrate = 0;
1384 	}
1385 	usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]);
1386 }
1387 
1388 static void
1389 wsp_stop_read(struct wsp_softc *sc)
1390 {
1391 	usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1392 }
1393 
1394 static int
1395 wsp_open(struct usb_fifo *fifo, int fflags)
1396 {
1397 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1398 	int rc = 0;
1399 
1400 	DPRINTFN(WSP_LLEVEL_INFO, "\n");
1401 
1402 	if (sc->sc_fflags & fflags)
1403 		return (EBUSY);
1404 
1405 	if (fflags & FREAD) {
1406 		if (usb_fifo_alloc_buffer(fifo,
1407 		    WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) {
1408 			return (ENOMEM);
1409 		}
1410 #ifdef EVDEV_SUPPORT
1411 		if ((sc->sc_state & WSP_EVDEV_OPENED) == 0)
1412 #endif
1413 			rc = wsp_enable(sc);
1414 		if (rc != 0) {
1415 			usb_fifo_free_buffer(fifo);
1416 			return (rc);
1417 		}
1418 	}
1419 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
1420 	return (0);
1421 }
1422 
1423 static void
1424 wsp_close(struct usb_fifo *fifo, int fflags)
1425 {
1426 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1427 
1428 	if (fflags & FREAD) {
1429 #ifdef EVDEV_SUPPORT
1430 		if ((sc->sc_state & WSP_EVDEV_OPENED) == 0)
1431 #endif
1432 			wsp_disable(sc);
1433 		usb_fifo_free_buffer(fifo);
1434 	}
1435 
1436 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1437 }
1438 
1439 static void
1440 wsp_fifo_start_read(struct usb_fifo *fifo)
1441 {
1442 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1443 
1444 	wsp_start_read(sc);
1445 }
1446 
1447 static void
1448 wsp_fifo_stop_read(struct usb_fifo *fifo)
1449 {
1450 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1451 
1452 #ifdef EVDEV_SUPPORT
1453 	if ((sc->sc_state & WSP_EVDEV_OPENED) == 0)
1454 #endif
1455 		wsp_stop_read(sc);
1456 }
1457 
1458 #ifdef EVDEV_SUPPORT
1459 static int
1460 wsp_ev_open(struct evdev_dev *evdev)
1461 {
1462 	struct wsp_softc *sc = evdev_get_softc(evdev);
1463 	int rc = 0;
1464 
1465 	mtx_lock(&sc->sc_mutex);
1466 	if (sc->sc_fflags == 0)
1467 		rc = wsp_enable(sc);
1468 	if (rc == 0) {
1469 		wsp_start_read(sc);
1470 		sc->sc_state |= WSP_EVDEV_OPENED;
1471 	}
1472 	mtx_unlock(&sc->sc_mutex);
1473 
1474 	return (rc);
1475 }
1476 
1477 static int
1478 wsp_ev_close(struct evdev_dev *evdev)
1479 {
1480 	struct wsp_softc *sc = evdev_get_softc(evdev);
1481 
1482 	mtx_lock(&sc->sc_mutex);
1483 	sc->sc_state &= ~WSP_EVDEV_OPENED;
1484 	if (sc->sc_fflags == 0)
1485 		wsp_stop_read(sc);
1486 	mtx_unlock(&sc->sc_mutex);
1487 
1488 	return (0);
1489 }
1490 #endif
1491 
1492 int
1493 wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1494 {
1495 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1496 	mousemode_t mode;
1497 	int error = 0;
1498 
1499 	mtx_lock(&sc->sc_mutex);
1500 
1501 	switch (cmd) {
1502 	case MOUSE_GETHWINFO:
1503 		*(mousehw_t *)addr = sc->sc_hw;
1504 		break;
1505 	case MOUSE_GETMODE:
1506 		*(mousemode_t *)addr = sc->sc_mode;
1507 		break;
1508 	case MOUSE_SETMODE:
1509 		mode = *(mousemode_t *)addr;
1510 
1511 		if (mode.level == -1)
1512 			/* Don't change the current setting */
1513 			;
1514 		else if ((mode.level < 0) || (mode.level > 1)) {
1515 			error = EINVAL;
1516 			goto done;
1517 		}
1518 		sc->sc_mode.level = mode.level;
1519 		sc->sc_pollrate = mode.rate;
1520 		sc->sc_hw.buttons = 3;
1521 
1522 		if (sc->sc_mode.level == 0) {
1523 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1524 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1525 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1526 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1527 		} else if (sc->sc_mode.level == 1) {
1528 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1529 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1530 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1531 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1532 		}
1533 		wsp_reset_buf(sc);
1534 		break;
1535 	case MOUSE_GETLEVEL:
1536 		*(int *)addr = sc->sc_mode.level;
1537 		break;
1538 	case MOUSE_SETLEVEL:
1539 		if (*(int *)addr < 0 || *(int *)addr > 1) {
1540 			error = EINVAL;
1541 			goto done;
1542 		}
1543 		sc->sc_mode.level = *(int *)addr;
1544 		sc->sc_hw.buttons = 3;
1545 
1546 		if (sc->sc_mode.level == 0) {
1547 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1548 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1549 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1550 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1551 		} else if (sc->sc_mode.level == 1) {
1552 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1553 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1554 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1555 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1556 		}
1557 		wsp_reset_buf(sc);
1558 		break;
1559 	case MOUSE_GETSTATUS:{
1560 			mousestatus_t *status = (mousestatus_t *)addr;
1561 
1562 			*status = sc->sc_status;
1563 			sc->sc_status.obutton = sc->sc_status.button;
1564 			sc->sc_status.button = 0;
1565 			sc->sc_status.dx = 0;
1566 			sc->sc_status.dy = 0;
1567 			sc->sc_status.dz = 0;
1568 
1569 			if (status->dx || status->dy || status->dz)
1570 				status->flags |= MOUSE_POSCHANGED;
1571 			if (status->button != status->obutton)
1572 				status->flags |= MOUSE_BUTTONSCHANGED;
1573 			break;
1574 		}
1575 	default:
1576 		error = ENOTTY;
1577 	}
1578 
1579 done:
1580 	mtx_unlock(&sc->sc_mutex);
1581 	return (error);
1582 }
1583 
1584 static device_method_t wsp_methods[] = {
1585 	/* Device interface */
1586 	DEVMETHOD(device_probe, wsp_probe),
1587 	DEVMETHOD(device_attach, wsp_attach),
1588 	DEVMETHOD(device_detach, wsp_detach),
1589 	DEVMETHOD_END
1590 };
1591 
1592 static driver_t wsp_driver = {
1593 	.name = WSP_DRIVER_NAME,
1594 	.methods = wsp_methods,
1595 	.size = sizeof(struct wsp_softc)
1596 };
1597 
1598 DRIVER_MODULE(wsp, uhub, wsp_driver, NULL, NULL);
1599 MODULE_DEPEND(wsp, usb, 1, 1, 1);
1600 MODULE_DEPEND(wsp, hid, 1, 1, 1);
1601 #ifdef EVDEV_SUPPORT
1602 MODULE_DEPEND(wsp, evdev, 1, 1, 1);
1603 #endif
1604 MODULE_VERSION(wsp, 1);
1605 USB_PNP_HOST_INFO(wsp_devs);
1606