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