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