xref: /freebsd/sys/dev/usb/input/wsp.c (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
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 wsp_tuning tun = wsp_tuning;
930 	int ntouch = 0;			/* the finger number in touch */
931 	int ibt = 0;			/* button status */
932 	int dx = 0;
933 	int dy = 0;
934 	int dz = 0;
935 	int rdx = 0;
936 	int rdy = 0;
937 	int rdz = 0;
938 	int len;
939 	int i;
940 #ifdef EVDEV_SUPPORT
941 	int slot = 0;
942 #endif
943 
944 	wsp_runing_rangecheck(&tun);
945 
946 	if (sc->dz_count == 0)
947 		sc->dz_count = WSP_DZ_MAX_COUNT;
948 
949 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
950 
951 	switch (USB_GET_STATE(xfer)) {
952 	case USB_ST_TRANSFERRED:
953 
954 		/* copy out received data */
955 		pc = usbd_xfer_get_frame(xfer, 0);
956 		usbd_copy_out(pc, 0, sc->tp_data, len);
957 
958 		if ((len < params->tp->offset + params->tp->fsize) ||
959 		    ((len - params->tp->offset) % params->tp->fsize) != 0) {
960 			DPRINTFN(WSP_LLEVEL_INFO, "Invalid length: %d, %x, %x\n",
961 			    len, sc->tp_data[0], sc->tp_data[1]);
962 			goto tr_setup;
963 		}
964 
965 		if (len < sc->tp_datalen) {
966 			/* make sure we don't process old data */
967 			memset(sc->tp_data + len, 0, sc->tp_datalen - len);
968 		}
969 
970 		if (params->tp != wsp_tp + TYPE1) {
971 			ibt = sc->tp_data[params->tp->button];
972 			ntouch = sc->tp_data[params->tp->button - 1];
973 		} else
974 			ntouch = (len - params->tp->offset) / params->tp->fsize;
975 
976 		/* range check */
977 		if (ntouch < 0)
978 			ntouch = 0;
979 		else if (ntouch > MAX_FINGERS)
980 			ntouch = MAX_FINGERS;
981 
982 		for (i = 0; i != ntouch; i++) {
983 			f = (struct tp_finger *)(sc->tp_data + params->tp->offset + params->tp->delta + i * params->tp->fsize);
984 			/* swap endianness, if any */
985 			if (le16toh(0x1234) != 0x1234) {
986 				f->origin = le16toh((uint16_t)f->origin);
987 				f->abs_x = le16toh((uint16_t)f->abs_x);
988 				f->abs_y = le16toh((uint16_t)f->abs_y);
989 				f->rel_x = le16toh((uint16_t)f->rel_x);
990 				f->rel_y = le16toh((uint16_t)f->rel_y);
991 				f->tool_major = le16toh((uint16_t)f->tool_major);
992 				f->tool_minor = le16toh((uint16_t)f->tool_minor);
993 				f->orientation = le16toh((uint16_t)f->orientation);
994 				f->touch_major = le16toh((uint16_t)f->touch_major);
995 				f->touch_minor = le16toh((uint16_t)f->touch_minor);
996 				f->pressure = le16toh((uint16_t)f->pressure);
997 				f->multi = le16toh((uint16_t)f->multi);
998 			}
999 			DPRINTFN(WSP_LLEVEL_INFO,
1000 			    "[%d]ibt=%d, taps=%d, o=%4d, ax=%5d, ay=%5d, "
1001 			    "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%4x, "
1002 			    "tchmaj=%4d, tchmin=%4d, presure=%4d, m=%4x\n",
1003 			    i, ibt, ntouch, f->origin, f->abs_x, f->abs_y,
1004 			    f->rel_x, f->rel_y, f->tool_major, f->tool_minor, f->orientation,
1005 			    f->touch_major, f->touch_minor, f->pressure, f->multi);
1006 			sc->pos_x[i] = f->abs_x;
1007 			sc->pos_y[i] = -f->abs_y;
1008 			sc->index[i] = f;
1009 #ifdef EVDEV_SUPPORT
1010 			if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE && f->touch_major != 0) {
1011 				union evdev_mt_slot slot_data = {
1012 					.id = slot,
1013 					.x = f->abs_x,
1014 					.y = params->y.min + params->y.max - f->abs_y,
1015 					.p = f->pressure,
1016 					.maj = f->touch_major << 1,
1017 					.min = f->touch_minor << 1,
1018 					.w_maj = f->tool_major << 1,
1019 					.w_min = f->tool_minor << 1,
1020 					.ori = params->o.max - f->orientation,
1021 				};
1022 				evdev_mt_push_slot(sc->sc_evdev, slot, &slot_data);
1023 				slot++;
1024 			}
1025 #endif
1026 		}
1027 
1028 #ifdef EVDEV_SUPPORT
1029 		if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
1030 			evdev_push_key(sc->sc_evdev, BTN_LEFT, ibt);
1031 			evdev_sync(sc->sc_evdev);
1032 		}
1033 #endif
1034 		sc->sc_status.flags &= ~MOUSE_POSCHANGED;
1035 		sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED;
1036 		sc->sc_status.obutton = sc->sc_status.button;
1037 		sc->sc_status.button = 0;
1038 
1039 		if (ibt != 0) {
1040 			if ((params->tp->caps & HAS_INTEGRATED_BUTTON) && ntouch == 2)
1041 				sc->sc_status.button |= MOUSE_BUTTON3DOWN;
1042 			else if ((params->tp->caps & HAS_INTEGRATED_BUTTON) && ntouch == 3)
1043 				sc->sc_status.button |= MOUSE_BUTTON2DOWN;
1044 			else
1045 				sc->sc_status.button |= MOUSE_BUTTON1DOWN;
1046 			sc->ibtn = 1;
1047 		}
1048 		sc->intr_count++;
1049 
1050 		if (sc->ntaps < ntouch) {
1051 			switch (ntouch) {
1052 			case 1:
1053 				if (sc->index[0]->touch_major > tun.pressure_tap_threshold &&
1054 				    sc->index[0]->tool_major <= 1200)
1055 					sc->ntaps = 1;
1056 				break;
1057 			case 2:
1058 				if (sc->index[0]->touch_major > tun.pressure_tap_threshold-30 &&
1059 				    sc->index[1]->touch_major > tun.pressure_tap_threshold-30)
1060 					sc->ntaps = 2;
1061 				break;
1062 			case 3:
1063 				if (sc->index[0]->touch_major > tun.pressure_tap_threshold-40 &&
1064 				    sc->index[1]->touch_major > tun.pressure_tap_threshold-40 &&
1065 				    sc->index[2]->touch_major > tun.pressure_tap_threshold-40)
1066 					sc->ntaps = 3;
1067 				break;
1068 			default:
1069 				break;
1070 			}
1071 		}
1072 		if (ntouch == 2) {
1073 			sc->distance = max(sc->distance, max(
1074 			    abs(sc->pos_x[0] - sc->pos_x[1]),
1075 			    abs(sc->pos_y[0] - sc->pos_y[1])));
1076 		}
1077 		if (sc->index[0]->touch_major < tun.pressure_untouch_threshold &&
1078 		    sc->sc_status.button == 0) {
1079 			sc->sc_touch = WSP_UNTOUCH;
1080 			if (sc->intr_count < WSP_TAP_MAX_COUNT &&
1081 			    sc->intr_count > WSP_TAP_THRESHOLD &&
1082 			    sc->ntaps && sc->ibtn == 0) {
1083 				/*
1084 				 * Add a pair of events (button-down and
1085 				 * button-up).
1086 				 */
1087 				switch (sc->ntaps) {
1088 				case 1:
1089 					if (!(params->tp->caps & HAS_INTEGRATED_BUTTON) || tun.enable_single_tap_clicks) {
1090 						wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN);
1091 						DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n");
1092 					}
1093 					break;
1094 				case 2:
1095 					DPRINTFN(WSP_LLEVEL_INFO, "sum_x=%5d, sum_y=%5d\n",
1096 					    sc->dx_sum, sc->dy_sum);
1097 					if (sc->distance < MAX_DISTANCE && abs(sc->dx_sum) < 5 &&
1098 					    abs(sc->dy_sum) < 5) {
1099 						wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN);
1100 						DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n");
1101 					}
1102 					break;
1103 				case 3:
1104 					wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN);
1105 					break;
1106 				default:
1107 					/* we don't handle taps of more than three fingers */
1108 					break;
1109 				}
1110 				wsp_add_to_queue(sc, 0, 0, 0, 0);	/* button release */
1111 			}
1112 			if ((sc->dt_sum / tun.scr_hor_threshold) != 0 &&
1113 			    sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) {
1114 				/*
1115 				 * translate T-axis into button presses
1116 				 * until further
1117 				 */
1118 				if (sc->dt_sum > 0)
1119 					wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3);
1120 				else if (sc->dt_sum < 0)
1121 					wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4);
1122 			}
1123 			sc->dz_count = WSP_DZ_MAX_COUNT;
1124 			sc->dz_sum = 0;
1125 			sc->intr_count = 0;
1126 			sc->ibtn = 0;
1127 			sc->ntaps = 0;
1128 			sc->finger = 0;
1129 			sc->distance = 0;
1130 			sc->dt_sum = 0;
1131 			sc->dx_sum = 0;
1132 			sc->dy_sum = 0;
1133 			sc->rdx = 0;
1134 			sc->rdy = 0;
1135 			sc->rdz = 0;
1136 			sc->scr_mode = WSP_SCR_NONE;
1137 		} else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold &&
1138 		    sc->sc_touch == WSP_UNTOUCH) {	/* ignore first touch */
1139 			sc->sc_touch = WSP_FIRST_TOUCH;
1140 		} else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold &&
1141 		    sc->sc_touch == WSP_FIRST_TOUCH) {	/* ignore second touch */
1142 			sc->sc_touch = WSP_SECOND_TOUCH;
1143 			DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n",
1144 			    sc->pre_pos_x, sc->pre_pos_y);
1145 		} else {
1146 			if (sc->sc_touch == WSP_SECOND_TOUCH)
1147 				sc->sc_touch = WSP_TOUCHING;
1148 
1149 			if (ntouch != 0 &&
1150 			    sc->index[0]->touch_major >= tun.pressure_touch_threshold) {
1151 				dx = sc->pos_x[0] - sc->pre_pos_x;
1152 				dy = sc->pos_y[0] - sc->pre_pos_y;
1153 
1154 				/* Ignore movement during button is releasing */
1155 				if (sc->ibtn != 0 && sc->sc_status.button == 0)
1156 					dx = dy = 0;
1157 
1158 				/* Ignore movement if ntouch changed */
1159 				if (sc->o_ntouch != ntouch)
1160 					dx = dy = 0;
1161 
1162 				/* Ignore unexpeted movement when typing */
1163 				if (ntouch == 1 && sc->index[0]->tool_major > 1200)
1164 					dx = dy = 0;
1165 
1166 				if (sc->ibtn != 0 && ntouch == 1 &&
1167 				    sc->intr_count < WSP_TAP_MAX_COUNT &&
1168 				    abs(sc->dx_sum) < 1 && abs(sc->dy_sum) < 1 )
1169 					dx = dy = 0;
1170 
1171 				if (ntouch == 2 && sc->sc_status.button != 0) {
1172 					dx = sc->pos_x[sc->finger] - sc->pre_pos_x;
1173 					dy = sc->pos_y[sc->finger] - sc->pre_pos_y;
1174 
1175 					/*
1176 					 * Ignore movement of switch finger or
1177 					 * movement from ibt=0 to ibt=1
1178 					 */
1179 					if (sc->index[0]->origin == 0 || sc->index[1]->origin == 0 ||
1180 					    sc->sc_status.obutton != sc->sc_status.button) {
1181 						dx = dy = 0;
1182 						sc->finger = 0;
1183 					}
1184 					if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) <
1185 					    (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) &&
1186 					    sc->finger == 0) {
1187 						sc->sc_touch = WSP_SECOND_TOUCH;
1188 						dx = dy = 0;
1189 						sc->finger = 1;
1190 					}
1191 					if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) >=
1192 					    (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) &&
1193 					    sc->finger == 1) {
1194 						sc->sc_touch = WSP_SECOND_TOUCH;
1195 						dx = dy = 0;
1196 						sc->finger = 0;
1197 					}
1198 					DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n",
1199 					    dx, dy, sc->finger);
1200 				}
1201 				if (sc->dz_count--) {
1202 					rdz = (dy + sc->rdz) % tun.scale_factor;
1203 					sc->dz_sum -= (dy + sc->rdz) / tun.scale_factor;
1204 					sc->rdz = rdz;
1205 				}
1206 				if ((sc->dz_sum / tun.z_factor) != 0)
1207 					sc->dz_count = 0;
1208 			}
1209 			rdx = (dx + sc->rdx) % tun.scale_factor;
1210 			dx = (dx + sc->rdx) / tun.scale_factor;
1211 			sc->rdx = rdx;
1212 
1213 			rdy = (dy + sc->rdy) % tun.scale_factor;
1214 			dy = (dy + sc->rdy) / tun.scale_factor;
1215 			sc->rdy = rdy;
1216 
1217 			sc->dx_sum += dx;
1218 			sc->dy_sum += dy;
1219 
1220 			if (ntouch == 2 && sc->sc_status.button == 0) {
1221 				if (sc->scr_mode == WSP_SCR_NONE &&
1222 				    abs(sc->dx_sum) + abs(sc->dy_sum) > tun.scr_hor_threshold)
1223 					sc->scr_mode = abs(sc->dx_sum) >
1224 					    abs(sc->dy_sum) * 2 ? WSP_SCR_HOR : WSP_SCR_VER;
1225 				DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n",
1226 				    sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum);
1227 				if (sc->scr_mode == WSP_SCR_HOR)
1228 					sc->dt_sum += dx;
1229 				else
1230 					sc->dt_sum = 0;
1231 
1232 				dx = dy = 0;
1233 				if (sc->dz_count == 0)
1234 					dz = (sc->dz_sum / tun.z_factor) * (tun.z_invert ? -1 : 1);
1235 				if (sc->scr_mode == WSP_SCR_HOR ||
1236 				    abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE ||
1237 				    abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE)
1238 					dz = 0;
1239 			}
1240 			if (ntouch == 3)
1241 				dx = dy = dz = 0;
1242 			if (sc->intr_count < WSP_TAP_MAX_COUNT &&
1243 			    abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3)
1244 				dx = dy = dz = 0;
1245 			else
1246 				sc->intr_count = WSP_TAP_MAX_COUNT;
1247 			if (dx || dy || dz)
1248 				sc->sc_status.flags |= MOUSE_POSCHANGED;
1249 			DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n",
1250 			    dx, dy, dz, sc->sc_touch, sc->sc_status.button);
1251 			sc->sc_status.dx += dx;
1252 			sc->sc_status.dy += dy;
1253 			sc->sc_status.dz += dz;
1254 
1255 			wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button);
1256 			if (sc->dz_count == 0) {
1257 				sc->dz_sum = 0;
1258 				sc->rdz = 0;
1259 			}
1260 		}
1261 		sc->pre_pos_x = sc->pos_x[0];
1262 		sc->pre_pos_y = sc->pos_y[0];
1263 
1264 		if (ntouch == 2 && sc->sc_status.button != 0) {
1265 			sc->pre_pos_x = sc->pos_x[sc->finger];
1266 			sc->pre_pos_y = sc->pos_y[sc->finger];
1267 		}
1268 		sc->o_ntouch = ntouch;
1269 
1270 	case USB_ST_SETUP:
1271 tr_setup:
1272 		/* check if we can put more data into the FIFO */
1273 		if (usb_fifo_put_bytes_max(
1274 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
1275 			usbd_xfer_set_frame_len(xfer, 0,
1276 			    sc->tp_datalen);
1277 			usbd_transfer_submit(xfer);
1278 		}
1279 		break;
1280 
1281 	default:			/* Error */
1282 		if (error != USB_ERR_CANCELLED) {
1283 			/* try clear stall first */
1284 			usbd_xfer_set_stall(xfer);
1285 			goto tr_setup;
1286 		}
1287 		break;
1288 	}
1289 }
1290 
1291 static void
1292 wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz,
1293     uint32_t buttons_in)
1294 {
1295 	uint32_t buttons_out;
1296 	uint8_t buf[8];
1297 
1298 	dx = imin(dx, 254);
1299 	dx = imax(dx, -256);
1300 	dy = imin(dy, 254);
1301 	dy = imax(dy, -256);
1302 	dz = imin(dz, 126);
1303 	dz = imax(dz, -128);
1304 
1305 	buttons_out = MOUSE_MSC_BUTTONS;
1306 	if (buttons_in & MOUSE_BUTTON1DOWN)
1307 		buttons_out &= ~MOUSE_MSC_BUTTON1UP;
1308 	else if (buttons_in & MOUSE_BUTTON2DOWN)
1309 		buttons_out &= ~MOUSE_MSC_BUTTON2UP;
1310 	else if (buttons_in & MOUSE_BUTTON3DOWN)
1311 		buttons_out &= ~MOUSE_MSC_BUTTON3UP;
1312 
1313 	/* Encode the mouse data in standard format; refer to mouse(4) */
1314 	buf[0] = sc->sc_mode.syncmask[1];
1315 	buf[0] |= buttons_out;
1316 	buf[1] = dx >> 1;
1317 	buf[2] = dy >> 1;
1318 	buf[3] = dx - (dx >> 1);
1319 	buf[4] = dy - (dy >> 1);
1320 	/* Encode extra bytes for level 1 */
1321 	if (sc->sc_mode.level == 1) {
1322 		buf[5] = dz >> 1;	/* dz / 2 */
1323 		buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */
1324 		buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS);
1325 	}
1326 	usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
1327 	    sc->sc_mode.packetsize, 1);
1328 }
1329 
1330 static void
1331 wsp_reset_buf(struct wsp_softc *sc)
1332 {
1333 	/* reset read queue */
1334 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
1335 }
1336 
1337 static void
1338 wsp_start_read(struct wsp_softc *sc)
1339 {
1340 	int rate;
1341 
1342 	/* Check if we should override the default polling interval */
1343 	rate = sc->sc_pollrate;
1344 	/* Range check rate */
1345 	if (rate > 1000)
1346 		rate = 1000;
1347 	/* Check for set rate */
1348 	if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) {
1349 		/* Stop current transfer, if any */
1350 		usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1351 		/* Set new interval */
1352 		usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate);
1353 		/* Only set pollrate once */
1354 		sc->sc_pollrate = 0;
1355 	}
1356 	usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]);
1357 }
1358 
1359 static void
1360 wsp_stop_read(struct wsp_softc *sc)
1361 {
1362 	usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1363 }
1364 
1365 static int
1366 wsp_open(struct usb_fifo *fifo, int fflags)
1367 {
1368 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1369 	int rc = 0;
1370 
1371 	DPRINTFN(WSP_LLEVEL_INFO, "\n");
1372 
1373 	if (sc->sc_fflags & fflags)
1374 		return (EBUSY);
1375 
1376 	if (fflags & FREAD) {
1377 		if (usb_fifo_alloc_buffer(fifo,
1378 		    WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) {
1379 			return (ENOMEM);
1380 		}
1381 #ifdef EVDEV_SUPPORT
1382 		if ((sc->sc_state & WSP_EVDEV_OPENED) == 0)
1383 #endif
1384 			rc = wsp_enable(sc);
1385 		if (rc != 0) {
1386 			usb_fifo_free_buffer(fifo);
1387 			return (rc);
1388 		}
1389 	}
1390 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
1391 	return (0);
1392 }
1393 
1394 static void
1395 wsp_close(struct usb_fifo *fifo, int fflags)
1396 {
1397 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1398 
1399 	if (fflags & FREAD) {
1400 #ifdef EVDEV_SUPPORT
1401 		if ((sc->sc_state & WSP_EVDEV_OPENED) == 0)
1402 #endif
1403 			wsp_disable(sc);
1404 		usb_fifo_free_buffer(fifo);
1405 	}
1406 
1407 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1408 }
1409 
1410 static void
1411 wsp_fifo_start_read(struct usb_fifo *fifo)
1412 {
1413 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1414 
1415 	wsp_start_read(sc);
1416 }
1417 
1418 static void
1419 wsp_fifo_stop_read(struct usb_fifo *fifo)
1420 {
1421 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1422 
1423 #ifdef EVDEV_SUPPORT
1424 	if ((sc->sc_state & WSP_EVDEV_OPENED) == 0)
1425 #endif
1426 		wsp_stop_read(sc);
1427 }
1428 
1429 #ifdef EVDEV_SUPPORT
1430 static int
1431 wsp_ev_open(struct evdev_dev *evdev)
1432 {
1433 	struct wsp_softc *sc = evdev_get_softc(evdev);
1434 	int rc = 0;
1435 
1436 	mtx_lock(&sc->sc_mutex);
1437 	if (sc->sc_fflags == 0)
1438 		rc = wsp_enable(sc);
1439 	if (rc == 0) {
1440 		wsp_start_read(sc);
1441 		sc->sc_state |= WSP_EVDEV_OPENED;
1442 	}
1443 	mtx_unlock(&sc->sc_mutex);
1444 
1445 	return (rc);
1446 }
1447 
1448 static int
1449 wsp_ev_close(struct evdev_dev *evdev)
1450 {
1451 	struct wsp_softc *sc = evdev_get_softc(evdev);
1452 
1453 	mtx_lock(&sc->sc_mutex);
1454 	sc->sc_state &= ~WSP_EVDEV_OPENED;
1455 	if (sc->sc_fflags == 0)
1456 		wsp_stop_read(sc);
1457 	mtx_unlock(&sc->sc_mutex);
1458 
1459 	return (0);
1460 }
1461 #endif
1462 
1463 int
1464 wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1465 {
1466 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1467 	mousemode_t mode;
1468 	int error = 0;
1469 
1470 	mtx_lock(&sc->sc_mutex);
1471 
1472 	switch (cmd) {
1473 	case MOUSE_GETHWINFO:
1474 		*(mousehw_t *)addr = sc->sc_hw;
1475 		break;
1476 	case MOUSE_GETMODE:
1477 		*(mousemode_t *)addr = sc->sc_mode;
1478 		break;
1479 	case MOUSE_SETMODE:
1480 		mode = *(mousemode_t *)addr;
1481 
1482 		if (mode.level == -1)
1483 			/* Don't change the current setting */
1484 			;
1485 		else if ((mode.level < 0) || (mode.level > 1)) {
1486 			error = EINVAL;
1487 			goto done;
1488 		}
1489 		sc->sc_mode.level = mode.level;
1490 		sc->sc_pollrate = mode.rate;
1491 		sc->sc_hw.buttons = 3;
1492 
1493 		if (sc->sc_mode.level == 0) {
1494 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1495 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1496 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1497 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1498 		} else if (sc->sc_mode.level == 1) {
1499 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1500 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1501 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1502 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1503 		}
1504 		wsp_reset_buf(sc);
1505 		break;
1506 	case MOUSE_GETLEVEL:
1507 		*(int *)addr = sc->sc_mode.level;
1508 		break;
1509 	case MOUSE_SETLEVEL:
1510 		if (*(int *)addr < 0 || *(int *)addr > 1) {
1511 			error = EINVAL;
1512 			goto done;
1513 		}
1514 		sc->sc_mode.level = *(int *)addr;
1515 		sc->sc_hw.buttons = 3;
1516 
1517 		if (sc->sc_mode.level == 0) {
1518 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1519 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1520 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1521 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1522 		} else if (sc->sc_mode.level == 1) {
1523 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1524 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1525 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1526 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1527 		}
1528 		wsp_reset_buf(sc);
1529 		break;
1530 	case MOUSE_GETSTATUS:{
1531 			mousestatus_t *status = (mousestatus_t *)addr;
1532 
1533 			*status = sc->sc_status;
1534 			sc->sc_status.obutton = sc->sc_status.button;
1535 			sc->sc_status.button = 0;
1536 			sc->sc_status.dx = 0;
1537 			sc->sc_status.dy = 0;
1538 			sc->sc_status.dz = 0;
1539 
1540 			if (status->dx || status->dy || status->dz)
1541 				status->flags |= MOUSE_POSCHANGED;
1542 			if (status->button != status->obutton)
1543 				status->flags |= MOUSE_BUTTONSCHANGED;
1544 			break;
1545 		}
1546 	default:
1547 		error = ENOTTY;
1548 	}
1549 
1550 done:
1551 	mtx_unlock(&sc->sc_mutex);
1552 	return (error);
1553 }
1554 
1555 static device_method_t wsp_methods[] = {
1556 	/* Device interface */
1557 	DEVMETHOD(device_probe, wsp_probe),
1558 	DEVMETHOD(device_attach, wsp_attach),
1559 	DEVMETHOD(device_detach, wsp_detach),
1560 	DEVMETHOD_END
1561 };
1562 
1563 static driver_t wsp_driver = {
1564 	.name = WSP_DRIVER_NAME,
1565 	.methods = wsp_methods,
1566 	.size = sizeof(struct wsp_softc)
1567 };
1568 
1569 DRIVER_MODULE(wsp, uhub, wsp_driver, NULL, NULL);
1570 MODULE_DEPEND(wsp, usb, 1, 1, 1);
1571 MODULE_DEPEND(wsp, hid, 1, 1, 1);
1572 #ifdef EVDEV_SUPPORT
1573 MODULE_DEPEND(wsp, evdev, 1, 1, 1);
1574 #endif
1575 MODULE_VERSION(wsp, 1);
1576 USB_PNP_HOST_INFO(wsp_devs);
1577