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