xref: /freebsd/sys/dev/usb/input/wsp.c (revision b1f9167f94059fd55c630891d359bcff987bd7eb)
1 /*-
2  * Copyright (c) 2012 Huang Wen Hui
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/fcntl.h>
40 #include <sys/file.h>
41 #include <sys/selinfo.h>
42 #include <sys/poll.h>
43 #include <sys/sysctl.h>
44 
45 #include <dev/usb/usb.h>
46 #include <dev/usb/usbdi.h>
47 #include <dev/usb/usbdi_util.h>
48 #include <dev/usb/usbhid.h>
49 
50 #include "usbdevs.h"
51 
52 #define	USB_DEBUG_VAR wsp_debug
53 #include <dev/usb/usb_debug.h>
54 
55 #include <sys/mouse.h>
56 
57 #define	WSP_DRIVER_NAME "wsp"
58 #define	WSP_BUFFER_MAX	1024
59 
60 #define	WSP_CLAMP(x,low,high) do {		\
61 	if ((x) < (low))			\
62 		(x) = (low);			\
63 	else if ((x) > (high))			\
64 		(x) = (high);			\
65 } while (0)
66 
67 /* Tunables */
68 static	SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW, 0, "USB wsp");
69 
70 #ifdef USB_DEBUG
71 enum wsp_log_level {
72 	WSP_LLEVEL_DISABLED = 0,
73 	WSP_LLEVEL_ERROR,
74 	WSP_LLEVEL_DEBUG,		/* for troubleshooting */
75 	WSP_LLEVEL_INFO,		/* for diagnostics */
76 };
77 static int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */
78 
79 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RW,
80     &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level");
81 #endif					/* USB_DEBUG */
82 
83 static struct wsp_tuning {
84 	int	scale_factor;
85 	int	z_factor;
86 	int	pressure_touch_threshold;
87 	int	pressure_untouch_threshold;
88 	int	pressure_tap_threshold;
89 	int	scr_hor_threshold;
90 }
91 	wsp_tuning =
92 {
93 	.scale_factor = 12,
94 	.z_factor = 5,
95 	.pressure_touch_threshold = 50,
96 	.pressure_untouch_threshold = 10,
97 	.pressure_tap_threshold = 100,
98 	.scr_hor_threshold = 10,
99 };
100 
101 static void
102 wsp_runing_rangecheck(struct wsp_tuning *ptun)
103 {
104 	WSP_CLAMP(ptun->scale_factor, 1, 63);
105 	WSP_CLAMP(ptun->z_factor, 1, 63);
106 	WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255);
107 	WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255);
108 	WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255);
109 	WSP_CLAMP(ptun->scr_hor_threshold, 1, 255);
110 }
111 
112 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RW,
113     &wsp_tuning.scale_factor, 0, "movement scale factor");
114 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RW,
115     &wsp_tuning.z_factor, 0, "Z-axis scale factor");
116 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RW,
117     &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold");
118 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RW,
119     &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold");
120 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RW,
121     &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold");
122 SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_hor_threshold, CTLFLAG_RW,
123     &wsp_tuning.scr_hor_threshold, 0, "horizontal scrolling threshold");
124 
125 #define	WSP_IFACE_INDEX	1
126 
127 /*
128  * Some tables, structures, definitions and constant values for the
129  * touchpad protocol has been copied from Linux's
130  * "drivers/input/mouse/bcm5974.c" which has the following copyright
131  * holders under GPLv2. All device specific code in this driver has
132  * been written from scratch. The decoding algorithm is based on
133  * output from FreeBSD's usbdump.
134  *
135  * Copyright (C) 2008      Henrik Rydberg (rydberg@euromail.se)
136  * Copyright (C) 2008      Scott Shawcroft (scott.shawcroft@gmail.com)
137  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
138  * Copyright (C) 2005      Johannes Berg (johannes@sipsolutions.net)
139  * Copyright (C) 2005      Stelian Pop (stelian@popies.net)
140  * Copyright (C) 2005      Frank Arnold (frank@scirocco-5v-turbo.de)
141  * Copyright (C) 2005      Peter Osterlund (petero2@telia.com)
142  * Copyright (C) 2005      Michael Hanselmann (linux-kernel@hansmi.ch)
143  * Copyright (C) 2006      Nicolas Boichat (nicolas@boichat.ch)
144  */
145 
146 /* button data structure */
147 struct bt_data {
148 	uint8_t	unknown1;		/* constant */
149 	uint8_t	button;			/* left button */
150 	uint8_t	rel_x;			/* relative x coordinate */
151 	uint8_t	rel_y;			/* relative y coordinate */
152 } __packed;
153 
154 /* trackpad header types */
155 enum tp_type {
156 	TYPE1,			/* plain trackpad */
157 	TYPE2,			/* button integrated in trackpad */
158 	TYPE3			/* additional header fields since June 2013 */
159 };
160 
161 /* trackpad finger data offsets, le16-aligned */
162 #define	FINGER_TYPE1		(13 * 2)
163 #define	FINGER_TYPE2		(15 * 2)
164 #define	FINGER_TYPE3		(19 * 2)
165 
166 /* trackpad button data offsets */
167 #define	BUTTON_TYPE2		15
168 #define	BUTTON_TYPE3		23
169 
170 /* list of device capability bits */
171 #define	HAS_INTEGRATED_BUTTON	1
172 
173 /* trackpad finger header - little endian */
174 struct tp_header {
175 	uint8_t	flag;
176 	uint8_t	sn0;
177 	uint16_t wFixed0;
178 	uint32_t dwSn1;
179 	uint32_t dwFixed1;
180 	uint16_t wLength;
181 	uint8_t	nfinger;
182 	uint8_t	ibt;
183 	int16_t	wUnknown[6];
184 	uint8_t	q1;
185 	uint8_t	q2;
186 } __packed;
187 
188 /* trackpad finger structure - little endian */
189 struct tp_finger {
190 	int16_t	origin;			/* zero when switching track finger */
191 	int16_t	abs_x;			/* absolute x coodinate */
192 	int16_t	abs_y;			/* absolute y coodinate */
193 	int16_t	rel_x;			/* relative x coodinate */
194 	int16_t	rel_y;			/* relative y coodinate */
195 	int16_t	tool_major;		/* tool area, major axis */
196 	int16_t	tool_minor;		/* tool area, minor axis */
197 	int16_t	orientation;		/* 16384 when point, else 15 bit angle */
198 	int16_t	touch_major;		/* touch area, major axis */
199 	int16_t	touch_minor;		/* touch area, minor axis */
200 	int16_t	unused[3];		/* zeros */
201 	int16_t	multi;			/* one finger: varies, more fingers:
202 					 * constant */
203 } __packed;
204 
205 /* trackpad finger data size, empirically at least ten fingers */
206 #define	MAX_FINGERS		16
207 #define	SIZEOF_FINGER		sizeof(struct tp_finger)
208 #define	SIZEOF_ALL_FINGERS	(MAX_FINGERS * SIZEOF_FINGER)
209 
210 #if (WSP_BUFFER_MAX < ((MAX_FINGERS * 14 * 2) + FINGER_TYPE3))
211 #error "WSP_BUFFER_MAX is too small"
212 #endif
213 
214 enum {
215 	WSP_FLAG_WELLSPRING1,
216 	WSP_FLAG_WELLSPRING2,
217 	WSP_FLAG_WELLSPRING3,
218 	WSP_FLAG_WELLSPRING4,
219 	WSP_FLAG_WELLSPRING4A,
220 	WSP_FLAG_WELLSPRING5,
221 	WSP_FLAG_WELLSPRING6A,
222 	WSP_FLAG_WELLSPRING6,
223 	WSP_FLAG_WELLSPRING5A,
224 	WSP_FLAG_WELLSPRING7,
225 	WSP_FLAG_WELLSPRING7A,
226 	WSP_FLAG_WELLSPRING8,
227 	WSP_FLAG_MAX,
228 };
229 
230 /* device-specific configuration */
231 struct wsp_dev_params {
232 	uint8_t	caps;			/* device capability bitmask */
233 	uint8_t	tp_type;		/* type of trackpad interface */
234 	uint8_t	tp_offset;		/* offset to trackpad finger data */
235 };
236 
237 static const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = {
238 	[WSP_FLAG_WELLSPRING1] = {
239 		.caps = 0,
240 		.tp_type = TYPE1,
241 		.tp_offset = FINGER_TYPE1,
242 	},
243 	[WSP_FLAG_WELLSPRING2] = {
244 		.caps = 0,
245 		.tp_type = TYPE1,
246 		.tp_offset = FINGER_TYPE1,
247 	},
248 	[WSP_FLAG_WELLSPRING3] = {
249 		.caps = HAS_INTEGRATED_BUTTON,
250 		.tp_type = TYPE2,
251 		.tp_offset = FINGER_TYPE2,
252 	},
253 	[WSP_FLAG_WELLSPRING4] = {
254 		.caps = HAS_INTEGRATED_BUTTON,
255 		.tp_type = TYPE2,
256 		.tp_offset = FINGER_TYPE2,
257 	},
258 	[WSP_FLAG_WELLSPRING4A] = {
259 		.caps = HAS_INTEGRATED_BUTTON,
260 		.tp_type = TYPE2,
261 		.tp_offset = FINGER_TYPE2,
262 	},
263 	[WSP_FLAG_WELLSPRING5] = {
264 		.caps = HAS_INTEGRATED_BUTTON,
265 		.tp_type = TYPE2,
266 		.tp_offset = FINGER_TYPE2,
267 	},
268 	[WSP_FLAG_WELLSPRING6] = {
269 		.caps = HAS_INTEGRATED_BUTTON,
270 		.tp_type = TYPE2,
271 		.tp_offset = FINGER_TYPE2,
272 	},
273 	[WSP_FLAG_WELLSPRING5A] = {
274 		.caps = HAS_INTEGRATED_BUTTON,
275 		.tp_type = TYPE2,
276 		.tp_offset = FINGER_TYPE2,
277 	},
278 	[WSP_FLAG_WELLSPRING6A] = {
279 		.caps = HAS_INTEGRATED_BUTTON,
280 		.tp_type = TYPE2,
281 		.tp_offset = FINGER_TYPE2,
282 	},
283 	[WSP_FLAG_WELLSPRING7] = {
284 		.caps = HAS_INTEGRATED_BUTTON,
285 		.tp_type = TYPE2,
286 		.tp_offset = FINGER_TYPE2,
287 	},
288 	[WSP_FLAG_WELLSPRING7A] = {
289 		.caps = HAS_INTEGRATED_BUTTON,
290 		.tp_type = TYPE2,
291 		.tp_offset = FINGER_TYPE2,
292 	},
293 	[WSP_FLAG_WELLSPRING8] = {
294 		.caps = HAS_INTEGRATED_BUTTON,
295 		.tp_type = TYPE3,
296 		.tp_offset = FINGER_TYPE3,
297 	},
298 };
299 
300 #define	WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
301 
302 static const STRUCT_USB_HOST_ID wsp_devs[] = {
303 	/* MacbookAir1.1 */
304 	WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1),
305 	WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1),
306 	WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1),
307 
308 	/* MacbookProPenryn, aka wellspring2 */
309 	WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2),
310 	WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2),
311 	WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2),
312 
313 	/* Macbook5,1 (unibody), aka wellspring3 */
314 	WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3),
315 	WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3),
316 	WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3),
317 
318 	/* MacbookAir3,2 (unibody), aka wellspring4 */
319 	WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4),
320 	WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4),
321 	WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4),
322 
323 	/* MacbookAir3,1 (unibody), aka wellspring4 */
324 	WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A),
325 	WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A),
326 	WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A),
327 
328 	/* Macbook8 (unibody, March 2011) */
329 	WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5),
330 	WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5),
331 	WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5),
332 
333 	/* MacbookAir4,1 (unibody, July 2011) */
334 	WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A),
335 	WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A),
336 	WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A),
337 
338 	/* MacbookAir4,2 (unibody, July 2011) */
339 	WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6),
340 	WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6),
341 	WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6),
342 
343 	/* Macbook8,2 (unibody) */
344 	WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A),
345 	WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A),
346 	WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A),
347 
348 	/* MacbookPro10,1 (unibody, June 2012) */
349 	/* MacbookPro11,? (unibody, June 2013) */
350 	WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7),
351 	WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7),
352 	WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7),
353 
354 	/* MacbookPro10,2 (unibody, October 2012) */
355 	WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A),
356 	WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A),
357 	WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A),
358 
359 	/* MacbookAir6,2 (unibody, June 2013) */
360 	WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8),
361 	WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8),
362 	WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8),
363 };
364 
365 #define	WSP_FIFO_BUF_SIZE	 8	/* bytes */
366 #define	WSP_FIFO_QUEUE_MAXLEN	50	/* units */
367 
368 enum {
369 	WSP_INTR_DT,
370 	WSP_N_TRANSFER,
371 };
372 
373 struct wsp_softc {
374 	struct usb_device *sc_usb_device;
375 	struct mtx sc_mutex;		/* for synchronization */
376 	struct usb_xfer *sc_xfer[WSP_N_TRANSFER];
377 	struct usb_fifo_sc sc_fifo;
378 
379 	const struct wsp_dev_params *sc_params;	/* device configuration */
380 
381 	mousehw_t sc_hw;
382 	mousemode_t sc_mode;
383 	u_int	sc_pollrate;
384 	mousestatus_t sc_status;
385 	u_int	sc_state;
386 #define	WSP_ENABLED	       0x01
387 
388 	struct tp_finger *index[MAX_FINGERS];	/* finger index data */
389 	int16_t	pos_x[MAX_FINGERS];	/* position array */
390 	int16_t	pos_y[MAX_FINGERS];	/* position array */
391 	u_int	sc_touch;		/* touch status */
392 #define	WSP_UNTOUCH		0x00
393 #define	WSP_FIRST_TOUCH		0x01
394 #define	WSP_SECOND_TOUCH	0x02
395 #define	WSP_TOUCHING		0x04
396 	int16_t	pre_pos_x;		/* previous position array */
397 	int16_t	pre_pos_y;		/* previous position array */
398 	int	dx_sum;			/* x axis cumulative movement */
399 	int	dy_sum;			/* y axis cumulative movement */
400 	int	dz_sum;			/* z axis cumulative movement */
401 	int	dz_count;
402 #define	WSP_DZ_MAX_COUNT	32
403 	int	dt_sum;			/* T-axis cumulative movement */
404 	int	rdx;			/* x axis remainder of divide by scale_factor */
405 	int	rdy;			/* y axis remainder of divide by scale_factor */
406 	int	rdz;			/* z axis remainder of divide by scale_factor */
407 	int	tp_datalen;
408 	uint8_t o_ntouch;		/* old touch finger status */
409 	uint8_t	finger;			/* 0 or 1 *, check which finger moving */
410 	uint16_t intr_count;
411 #define	WSP_TAP_THRESHOLD	3
412 #define	WSP_TAP_MAX_COUNT	20
413 	int	distance;		/* the distance of 2 fingers */
414 #define	MAX_DISTANCE		2500	/* the max allowed distance */
415 	uint8_t	ibtn;			/* button status in tapping */
416 	uint8_t	ntaps;			/* finger status in tapping */
417 	uint8_t	scr_mode;		/* scroll status in movement */
418 #define	WSP_SCR_NONE		0
419 #define	WSP_SCR_VER		1
420 #define	WSP_SCR_HOR		2
421 	uint8_t tp_data[WSP_BUFFER_MAX] __aligned(4);		/* trackpad transferred data */
422 };
423 
424 typedef enum interface_mode {
425 	RAW_SENSOR_MODE = 0x01,
426 	HID_MODE = 0x08
427 } interface_mode;
428 
429 /*
430  * function prototypes
431  */
432 static usb_fifo_cmd_t wsp_start_read;
433 static usb_fifo_cmd_t wsp_stop_read;
434 static usb_fifo_open_t wsp_open;
435 static usb_fifo_close_t wsp_close;
436 static usb_fifo_ioctl_t wsp_ioctl;
437 
438 static struct usb_fifo_methods wsp_fifo_methods = {
439 	.f_open = &wsp_open,
440 	.f_close = &wsp_close,
441 	.f_ioctl = &wsp_ioctl,
442 	.f_start_read = &wsp_start_read,
443 	.f_stop_read = &wsp_stop_read,
444 	.basename[0] = WSP_DRIVER_NAME,
445 };
446 
447 /* device initialization and shutdown */
448 static int wsp_enable(struct wsp_softc *sc);
449 static void wsp_disable(struct wsp_softc *sc);
450 
451 /* updating fifo */
452 static void wsp_reset_buf(struct wsp_softc *sc);
453 static void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t);
454 
455 /* Device methods. */
456 static device_probe_t wsp_probe;
457 static device_attach_t wsp_attach;
458 static device_detach_t wsp_detach;
459 static usb_callback_t wsp_intr_callback;
460 
461 static const struct usb_config wsp_config[WSP_N_TRANSFER] = {
462 	[WSP_INTR_DT] = {
463 		.type = UE_INTERRUPT,
464 		.endpoint = UE_ADDR_ANY,
465 		.direction = UE_DIR_IN,
466 		.flags = {
467 			.pipe_bof = 0,
468 			.short_xfer_ok = 1,
469 		},
470 		.bufsize = WSP_BUFFER_MAX,
471 		.callback = &wsp_intr_callback,
472 	},
473 };
474 
475 static usb_error_t
476 wsp_set_device_mode(struct wsp_softc *sc, interface_mode mode)
477 {
478 	uint8_t	mode_bytes[8];
479 	usb_error_t err;
480 
481 	err = usbd_req_get_report(sc->sc_usb_device, NULL,
482 	    mode_bytes, sizeof(mode_bytes), 0,
483 	    0x03, 0x00);
484 
485 	if (err != USB_ERR_NORMAL_COMPLETION) {
486 		DPRINTF("Failed to read device mode (%d)\n", err);
487 		return (err);
488 	}
489 
490 	/*
491 	 * XXX Need to wait at least 250ms for hardware to get
492 	 * ready. The device mode handling appears to be handled
493 	 * asynchronously and we should not issue these commands too
494 	 * quickly.
495 	 */
496 	pause("WHW", hz / 4);
497 
498 	mode_bytes[0] = mode;
499 
500 	return (usbd_req_set_report(sc->sc_usb_device, NULL,
501 	    mode_bytes, sizeof(mode_bytes), 0,
502 	    0x03, 0x00));
503 }
504 
505 static int
506 wsp_enable(struct wsp_softc *sc)
507 {
508 	/* reset status */
509 	memset(&sc->sc_status, 0, sizeof(sc->sc_status));
510 	sc->sc_state |= WSP_ENABLED;
511 
512 	DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n");
513 	return (0);
514 }
515 
516 static void
517 wsp_disable(struct wsp_softc *sc)
518 {
519 	sc->sc_state &= ~WSP_ENABLED;
520 	DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n");
521 }
522 
523 static int
524 wsp_probe(device_t self)
525 {
526 	struct usb_attach_arg *uaa = device_get_ivars(self);
527 
528 	if (uaa->usb_mode != USB_MODE_HOST)
529 		return (ENXIO);
530 
531 	if (uaa->info.bIfaceIndex != WSP_IFACE_INDEX)
532 		return (ENXIO);
533 
534 	if ((uaa->info.bInterfaceClass != UICLASS_HID) ||
535 	    (uaa->info.bInterfaceProtocol != 0))
536 		return (ENXIO);
537 
538 	return (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa));
539 }
540 
541 static int
542 wsp_attach(device_t dev)
543 {
544 	struct wsp_softc *sc = device_get_softc(dev);
545 	struct usb_attach_arg *uaa = device_get_ivars(dev);
546 	usb_error_t err;
547 	void *d_ptr = NULL;
548 	uint16_t d_len;
549 
550 	DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc);
551 
552 	/* Get HID descriptor */
553 	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
554 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
555 
556 	if (err == USB_ERR_NORMAL_COMPLETION) {
557 		/* Get HID report descriptor length */
558 		sc->tp_datalen = hid_report_size(d_ptr, d_len, hid_input, NULL);
559 		free(d_ptr, M_TEMP);
560 
561 		if (sc->tp_datalen <= 0 || sc->tp_datalen > WSP_BUFFER_MAX) {
562 			DPRINTF("Invalid datalength or too big "
563 			    "datalength: %d\n", sc->tp_datalen);
564 			return (ENXIO);
565 		}
566 	} else {
567 		return (ENXIO);
568 	}
569 
570 	sc->sc_usb_device = uaa->device;
571 
572 	/*
573 	 * By default the touchpad behaves like a HID device, sending
574 	 * packets with reportID = 8. Such reports contain only
575 	 * limited information. They encode movement deltas and button
576 	 * events, but do not include data from the pressure
577 	 * sensors. The device input mode can be switched from HID
578 	 * reports to raw sensor data using vendor-specific USB
579 	 * control commands:
580 	 */
581 
582 	/*
583 	 * During re-enumeration of the device we need to force the
584 	 * device back into HID mode before switching it to RAW
585 	 * mode. Else the device does not work like expected.
586 	 */
587 	err = wsp_set_device_mode(sc, HID_MODE);
588 	if (err != USB_ERR_NORMAL_COMPLETION) {
589 		DPRINTF("Failed to set mode to HID MODE (%d)\n", err);
590 		return (ENXIO);
591 	}
592 
593 	err = wsp_set_device_mode(sc, RAW_SENSOR_MODE);
594 	if (err != USB_ERR_NORMAL_COMPLETION) {
595 		DPRINTF("failed to set mode to RAW MODE (%d)\n", err);
596 		return (ENXIO);
597 	}
598 
599 	mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE);
600 
601 	/* get device specific configuration */
602 	sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa);
603 
604 	err = usbd_transfer_setup(uaa->device,
605 	    &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config,
606 	    WSP_N_TRANSFER, sc, &sc->sc_mutex);
607 	if (err) {
608 		DPRINTF("error=%s\n", usbd_errstr(err));
609 		goto detach;
610 	}
611 	if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex,
612 	    &wsp_fifo_methods, &sc->sc_fifo,
613 	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
614 	    UID_ROOT, GID_OPERATOR, 0644)) {
615 		goto detach;
616 	}
617 	device_set_usb_desc(dev);
618 
619 	sc->sc_hw.buttons = 3;
620 	sc->sc_hw.iftype = MOUSE_IF_USB;
621 	sc->sc_hw.type = MOUSE_PAD;
622 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
623 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
624 	sc->sc_mode.rate = -1;
625 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
626 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
627 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
628 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
629 
630 	sc->sc_touch = WSP_UNTOUCH;
631 	sc->scr_mode = WSP_SCR_NONE;
632 
633 	return (0);
634 
635 detach:
636 	wsp_detach(dev);
637 	return (ENOMEM);
638 }
639 
640 static int
641 wsp_detach(device_t dev)
642 {
643 	struct wsp_softc *sc = device_get_softc(dev);
644 
645 	(void) wsp_set_device_mode(sc, HID_MODE);
646 
647 	mtx_lock(&sc->sc_mutex);
648 	if (sc->sc_state & WSP_ENABLED)
649 		wsp_disable(sc);
650 	mtx_unlock(&sc->sc_mutex);
651 
652 	usb_fifo_detach(&sc->sc_fifo);
653 
654 	usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER);
655 
656 	mtx_destroy(&sc->sc_mutex);
657 
658 	return (0);
659 }
660 
661 static void
662 wsp_intr_callback(struct usb_xfer *xfer, usb_error_t error)
663 {
664 	struct wsp_softc *sc = usbd_xfer_softc(xfer);
665 	const struct wsp_dev_params *params = sc->sc_params;
666 	struct usb_page_cache *pc;
667 	struct tp_finger *f;
668 	struct tp_header *h;
669 	struct wsp_tuning tun = wsp_tuning;
670 	int ntouch = 0;			/* the finger number in touch */
671 	int ibt = 0;			/* button status */
672 	int dx = 0;
673 	int dy = 0;
674 	int dz = 0;
675 	int rdx = 0;
676 	int rdy = 0;
677 	int rdz = 0;
678 	int len;
679 	int i;
680 
681 	wsp_runing_rangecheck(&tun);
682 
683 	if (sc->dz_count == 0)
684 		sc->dz_count = WSP_DZ_MAX_COUNT;
685 
686 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
687 
688 	switch (USB_GET_STATE(xfer)) {
689 	case USB_ST_TRANSFERRED:
690 
691 		/* copy out received data */
692 		pc = usbd_xfer_get_frame(xfer, 0);
693 		usbd_copy_out(pc, 0, sc->tp_data, len);
694 
695 		if (len < sc->tp_datalen) {
696 			/* make sure we don't process old data */
697 			memset(sc->tp_data + len, 0, sc->tp_datalen - len);
698 		}
699 
700 		h = (struct tp_header *)(sc->tp_data);
701 
702 		if (params->tp_type == TYPE2) {
703 			ibt = sc->tp_data[BUTTON_TYPE2];
704 			ntouch = sc->tp_data[BUTTON_TYPE2 - 1];
705 		} else if (params->tp_type == TYPE3) {
706 			ibt = sc->tp_data[BUTTON_TYPE3];
707 			ntouch = sc->tp_data[BUTTON_TYPE3 - 1];
708 		}
709 		/* range check */
710 		if (ntouch < 0)
711 			ntouch = 0;
712 		else if (ntouch > MAX_FINGERS)
713 			ntouch = MAX_FINGERS;
714 
715 		f = (struct tp_finger *)(sc->tp_data + params->tp_offset);
716 
717 		for (i = 0; i != ntouch; i++) {
718 			/* swap endianness, if any */
719 			if (le16toh(0x1234) != 0x1234) {
720 				f[i].origin = le16toh((uint16_t)f[i].origin);
721 				f[i].abs_x = le16toh((uint16_t)f[i].abs_x);
722 				f[i].abs_y = le16toh((uint16_t)f[i].abs_y);
723 				f[i].rel_x = le16toh((uint16_t)f[i].rel_x);
724 				f[i].rel_y = le16toh((uint16_t)f[i].rel_y);
725 				f[i].tool_major = le16toh((uint16_t)f[i].tool_major);
726 				f[i].tool_minor = le16toh((uint16_t)f[i].tool_minor);
727 				f[i].orientation = le16toh((uint16_t)f[i].orientation);
728 				f[i].touch_major = le16toh((uint16_t)f[i].touch_major);
729 				f[i].touch_minor = le16toh((uint16_t)f[i].touch_minor);
730 				f[i].multi = le16toh((uint16_t)f[i].multi);
731 			}
732 			DPRINTFN(WSP_LLEVEL_INFO, "[%d]ibt=%d, taps=%d, u=%x, o=%4d, ax=%5d, ay=%5d, "
733 			    "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%5d, tchmaj=%4d, tchmin=%4d, m=%4x\n",
734 			    i, ibt, ntouch, h->q2,
735 			    f[i].origin, f[i].abs_x, f[i].abs_y, f[i].rel_x, f[i].rel_y,
736 			    f[i].tool_major, f[i].tool_minor, f[i].orientation,
737 			    f[i].touch_major, f[i].touch_minor, f[i].multi);
738 
739 			sc->pos_x[i] = f[i].abs_x;
740 			sc->pos_y[i] = -f[i].abs_y;
741 			sc->index[i] = &f[i];
742 		}
743 
744 		sc->sc_status.flags &= ~MOUSE_POSCHANGED;
745 		sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED;
746 		sc->sc_status.obutton = sc->sc_status.button;
747 		sc->sc_status.button = 0;
748 
749 		if (ibt != 0) {
750 			sc->sc_status.button |= MOUSE_BUTTON1DOWN;
751 			sc->ibtn = 1;
752 		}
753 		if (h->q2 == 4)
754 			sc->intr_count++;
755 
756 		if (sc->ntaps < ntouch) {
757 			switch (ntouch) {
758 			case 1:
759 				if (f[0].touch_major > tun.pressure_tap_threshold)
760 					sc->ntaps = 1;
761 				break;
762 			case 2:
763 				if (f[0].touch_major > tun.pressure_tap_threshold &&
764 				    f[1].touch_major > tun.pressure_tap_threshold)
765 					sc->ntaps = 2;
766 				break;
767 			case 3:
768 				if (f[0].touch_major > tun.pressure_tap_threshold &&
769 				    f[1].touch_major > tun.pressure_tap_threshold &&
770 				    f[2].touch_major > tun.pressure_tap_threshold)
771 					sc->ntaps = 3;
772 				break;
773 			default:
774 				break;
775 			}
776 		}
777 		if (ntouch == 2) {
778 			sc->distance = max(sc->distance, max(
779 			    abs(sc->pos_x[0] - sc->pos_x[1]),
780 			    abs(sc->pos_y[0] - sc->pos_y[1])));
781 		}
782 		if (f[0].touch_major < tun.pressure_untouch_threshold &&
783 		    sc->sc_status.button == 0) {
784 			sc->sc_touch = WSP_UNTOUCH;
785 			if (sc->intr_count < WSP_TAP_MAX_COUNT &&
786 			    sc->intr_count > WSP_TAP_THRESHOLD &&
787 			    sc->ntaps && sc->ibtn == 0) {
788 				/*
789 				 * Add a pair of events (button-down and
790 				 * button-up).
791 				 */
792 				switch (sc->ntaps) {
793 				case 1:
794 					if (!(params->caps & HAS_INTEGRATED_BUTTON)) {
795 						wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN);
796 						DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n");
797 					}
798 					break;
799 				case 2:
800 					DPRINTFN(WSP_LLEVEL_INFO, "sum_x=%5d, sum_y=%5d\n",
801 					    sc->dx_sum, sc->dy_sum);
802 					if (sc->distance < MAX_DISTANCE && abs(sc->dx_sum) < 5 &&
803 					    abs(sc->dy_sum) < 5) {
804 						wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN);
805 						DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n");
806 					}
807 					break;
808 				case 3:
809 					wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN);
810 					break;
811 				default:
812 					/* we don't handle taps of more than three fingers */
813 					break;
814 				}
815 				wsp_add_to_queue(sc, 0, 0, 0, 0);	/* button release */
816 			}
817 			if ((sc->dt_sum / tun.scr_hor_threshold) != 0 &&
818 			    sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) {
819 
820 				/*
821 				 * translate T-axis into button presses
822 				 * until further
823 				 */
824 				if (sc->dt_sum > 0)
825 					wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3);
826 				else if (sc->dt_sum < 0)
827 					wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4);
828 			}
829 			sc->dz_count = WSP_DZ_MAX_COUNT;
830 			sc->dz_sum = 0;
831 			sc->intr_count = 0;
832 			sc->ibtn = 0;
833 			sc->ntaps = 0;
834 			sc->finger = 0;
835 			sc->distance = 0;
836 			sc->dt_sum = 0;
837 			sc->dx_sum = 0;
838 			sc->dy_sum = 0;
839 			sc->rdx = 0;
840 			sc->rdy = 0;
841 			sc->rdz = 0;
842 			sc->scr_mode = WSP_SCR_NONE;
843 		} else if (f[0].touch_major >= tun.pressure_touch_threshold &&
844 		    sc->sc_touch == WSP_UNTOUCH) {	/* ignore first touch */
845 			sc->sc_touch = WSP_FIRST_TOUCH;
846 		} else if (f[0].touch_major >= tun.pressure_touch_threshold &&
847 		    sc->sc_touch == WSP_FIRST_TOUCH) {	/* ignore second touch */
848 			sc->sc_touch = WSP_SECOND_TOUCH;
849 			DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n",
850 			    sc->pre_pos_x, sc->pre_pos_y);
851 		} else {
852 			if (sc->sc_touch == WSP_SECOND_TOUCH)
853 				sc->sc_touch = WSP_TOUCHING;
854 
855 			if (ntouch != 0 &&
856 			    h->q2 == 4 &&
857 			    f[0].touch_major >= tun.pressure_touch_threshold) {
858 				dx = sc->pos_x[0] - sc->pre_pos_x;
859 				dy = sc->pos_y[0] - sc->pre_pos_y;
860 
861 				/* Ignore movement from ibt=1 to ibt=0 */
862 				if (sc->sc_status.obutton != 0 &&
863 				    sc->sc_status.button == 0) {
864 					dx = 0;
865 					dy = 0;
866 				}
867 				/* Ignore movement if ntouch changed */
868 				if (sc->o_ntouch != ntouch) {
869 					dx = 0;
870 					dy = 0;
871 				}
872 
873 				if (ntouch == 2 && sc->sc_status.button != 0) {
874 					dx = sc->pos_x[sc->finger] - sc->pre_pos_x;
875 					dy = sc->pos_y[sc->finger] - sc->pre_pos_y;
876 
877 					/*
878 					 * Ignore movement of switch finger or
879 					 * movement from ibt=0 to ibt=1
880 					 */
881 					if (f[0].origin == 0 || f[1].origin == 0 ||
882 					    sc->sc_status.obutton != sc->sc_status.button) {
883 						dx = 0;
884 						dy = 0;
885 						sc->finger = 0;
886 					}
887 					if ((abs(f[0].rel_x) + abs(f[0].rel_y)) <
888 					    (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
889 					    sc->finger == 0) {
890 						sc->sc_touch = WSP_SECOND_TOUCH;
891 						dx = 0;
892 						dy = 0;
893 						sc->finger = 1;
894 					}
895 					if ((abs(f[0].rel_x) + abs(f[0].rel_y)) >=
896 					    (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
897 					    sc->finger == 1) {
898 						sc->sc_touch = WSP_SECOND_TOUCH;
899 						dx = 0;
900 						dy = 0;
901 						sc->finger = 0;
902 					}
903 					DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n",
904 					    dx, dy, sc->finger);
905 				}
906 				if (sc->dz_count--) {
907 					rdz = (dy + sc->rdz) % tun.scale_factor;
908 					sc->dz_sum -= (dy + sc->rdz) / tun.scale_factor;
909 					sc->rdz = rdz;
910 				}
911 				if ((sc->dz_sum / tun.z_factor) != 0)
912 					sc->dz_count = 0;
913 			}
914 			rdx = (dx + sc->rdx) % tun.scale_factor;
915 			dx = (dx + sc->rdx) / tun.scale_factor;
916 			sc->rdx = rdx;
917 
918 			rdy = (dy + sc->rdy) % tun.scale_factor;
919 			dy = (dy + sc->rdy) / tun.scale_factor;
920 			sc->rdy = rdy;
921 
922 			sc->dx_sum += dx;
923 			sc->dy_sum += dy;
924 
925 			if (ntouch == 2 && sc->sc_status.button == 0) {
926 				if (sc->scr_mode == WSP_SCR_NONE &&
927 				    abs(sc->dx_sum) + abs(sc->dy_sum) > tun.scr_hor_threshold)
928 					sc->scr_mode = abs(sc->dx_sum) >
929 					    abs(sc->dy_sum) * 3 ? WSP_SCR_HOR :
930 					    WSP_SCR_VER;
931 				DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n",
932 				    sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum);
933 				if (sc->scr_mode == WSP_SCR_HOR)
934 					sc->dt_sum += dx;
935 				else
936 					sc->dt_sum = 0;
937 
938 				dx = 0;
939 				dy = 0;
940 				if (sc->dz_count == 0)
941 					dz = sc->dz_sum / tun.z_factor;
942 				if (sc->scr_mode == WSP_SCR_HOR ||
943 				    abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE ||
944 				    abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE)
945 					dz = 0;
946 			}
947 			if (ntouch == 3) {
948 				dx = 0;
949 				dy = 0;
950 				dz = 0;
951 			}
952 			if (sc->intr_count < WSP_TAP_MAX_COUNT &&
953 			    abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3) {
954 				dx = dy = dz = 0;
955 			} else
956 				sc->intr_count = WSP_TAP_MAX_COUNT;
957 			if (dx || dy || dz)
958 				sc->sc_status.flags |= MOUSE_POSCHANGED;
959 			DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n",
960 			    dx, dy, dz, sc->sc_touch, sc->sc_status.button);
961 			sc->sc_status.dx += dx;
962 			sc->sc_status.dy += dy;
963 			sc->sc_status.dz += dz;
964 
965 			wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button);
966 			if (sc->dz_count == 0) {
967 				sc->dz_sum = 0;
968 				sc->rdz = 0;
969 			}
970 
971 		}
972 		sc->pre_pos_x = sc->pos_x[0];
973 		sc->pre_pos_y = sc->pos_y[0];
974 
975 		if (ntouch == 2 && sc->sc_status.button != 0) {
976 			sc->pre_pos_x = sc->pos_x[sc->finger];
977 			sc->pre_pos_y = sc->pos_y[sc->finger];
978 		}
979 		sc->o_ntouch = ntouch;
980 
981 	case USB_ST_SETUP:
982 tr_setup:
983 		/* check if we can put more data into the FIFO */
984 		if (usb_fifo_put_bytes_max(
985 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
986 			usbd_xfer_set_frame_len(xfer, 0,
987 			    sc->tp_datalen);
988 			usbd_transfer_submit(xfer);
989 		}
990 		break;
991 
992 	default:			/* Error */
993 		if (error != USB_ERR_CANCELLED) {
994 			/* try clear stall first */
995 			usbd_xfer_set_stall(xfer);
996 			goto tr_setup;
997 		}
998 		break;
999 	}
1000 }
1001 
1002 static void
1003 wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz,
1004     uint32_t buttons_in)
1005 {
1006 	uint32_t buttons_out;
1007 	uint8_t buf[8];
1008 
1009 	dx = imin(dx, 254);
1010 	dx = imax(dx, -256);
1011 	dy = imin(dy, 254);
1012 	dy = imax(dy, -256);
1013 	dz = imin(dz, 126);
1014 	dz = imax(dz, -128);
1015 
1016 	buttons_out = MOUSE_MSC_BUTTONS;
1017 	if (buttons_in & MOUSE_BUTTON1DOWN)
1018 		buttons_out &= ~MOUSE_MSC_BUTTON1UP;
1019 	else if (buttons_in & MOUSE_BUTTON2DOWN)
1020 		buttons_out &= ~MOUSE_MSC_BUTTON2UP;
1021 	else if (buttons_in & MOUSE_BUTTON3DOWN)
1022 		buttons_out &= ~MOUSE_MSC_BUTTON3UP;
1023 
1024 	/* Encode the mouse data in standard format; refer to mouse(4) */
1025 	buf[0] = sc->sc_mode.syncmask[1];
1026 	buf[0] |= buttons_out;
1027 	buf[1] = dx >> 1;
1028 	buf[2] = dy >> 1;
1029 	buf[3] = dx - (dx >> 1);
1030 	buf[4] = dy - (dy >> 1);
1031 	/* Encode extra bytes for level 1 */
1032 	if (sc->sc_mode.level == 1) {
1033 		buf[5] = dz >> 1;	/* dz / 2 */
1034 		buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */
1035 		buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS);
1036 	}
1037 	usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
1038 	    sc->sc_mode.packetsize, 1);
1039 }
1040 
1041 static void
1042 wsp_reset_buf(struct wsp_softc *sc)
1043 {
1044 	/* reset read queue */
1045 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
1046 }
1047 
1048 static void
1049 wsp_start_read(struct usb_fifo *fifo)
1050 {
1051 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1052 	int rate;
1053 
1054 	/* Check if we should override the default polling interval */
1055 	rate = sc->sc_pollrate;
1056 	/* Range check rate */
1057 	if (rate > 1000)
1058 		rate = 1000;
1059 	/* Check for set rate */
1060 	if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) {
1061 		/* Stop current transfer, if any */
1062 		usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1063 		/* Set new interval */
1064 		usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate);
1065 		/* Only set pollrate once */
1066 		sc->sc_pollrate = 0;
1067 	}
1068 	usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]);
1069 }
1070 
1071 static void
1072 wsp_stop_read(struct usb_fifo *fifo)
1073 {
1074 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1075 
1076 	usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1077 }
1078 
1079 
1080 static int
1081 wsp_open(struct usb_fifo *fifo, int fflags)
1082 {
1083 	DPRINTFN(WSP_LLEVEL_INFO, "\n");
1084 
1085 	if (fflags & FREAD) {
1086 		struct wsp_softc *sc = usb_fifo_softc(fifo);
1087 		int rc;
1088 
1089 		if (sc->sc_state & WSP_ENABLED)
1090 			return (EBUSY);
1091 
1092 		if (usb_fifo_alloc_buffer(fifo,
1093 		    WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) {
1094 			return (ENOMEM);
1095 		}
1096 		rc = wsp_enable(sc);
1097 		if (rc != 0) {
1098 			usb_fifo_free_buffer(fifo);
1099 			return (rc);
1100 		}
1101 	}
1102 	return (0);
1103 }
1104 
1105 static void
1106 wsp_close(struct usb_fifo *fifo, int fflags)
1107 {
1108 	if (fflags & FREAD) {
1109 		struct wsp_softc *sc = usb_fifo_softc(fifo);
1110 
1111 		wsp_disable(sc);
1112 		usb_fifo_free_buffer(fifo);
1113 	}
1114 }
1115 
1116 int
1117 wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1118 {
1119 	struct wsp_softc *sc = usb_fifo_softc(fifo);
1120 	mousemode_t mode;
1121 	int error = 0;
1122 
1123 	mtx_lock(&sc->sc_mutex);
1124 
1125 	switch (cmd) {
1126 	case MOUSE_GETHWINFO:
1127 		*(mousehw_t *)addr = sc->sc_hw;
1128 		break;
1129 	case MOUSE_GETMODE:
1130 		*(mousemode_t *)addr = sc->sc_mode;
1131 		break;
1132 	case MOUSE_SETMODE:
1133 		mode = *(mousemode_t *)addr;
1134 
1135 		if (mode.level == -1)
1136 			/* Don't change the current setting */
1137 			;
1138 		else if ((mode.level < 0) || (mode.level > 1)) {
1139 			error = EINVAL;
1140 			goto done;
1141 		}
1142 		sc->sc_mode.level = mode.level;
1143 		sc->sc_pollrate = mode.rate;
1144 		sc->sc_hw.buttons = 3;
1145 
1146 		if (sc->sc_mode.level == 0) {
1147 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1148 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1149 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1150 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1151 		} else if (sc->sc_mode.level == 1) {
1152 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1153 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1154 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1155 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1156 		}
1157 		wsp_reset_buf(sc);
1158 		break;
1159 	case MOUSE_GETLEVEL:
1160 		*(int *)addr = sc->sc_mode.level;
1161 		break;
1162 	case MOUSE_SETLEVEL:
1163 		if (*(int *)addr < 0 || *(int *)addr > 1) {
1164 			error = EINVAL;
1165 			goto done;
1166 		}
1167 		sc->sc_mode.level = *(int *)addr;
1168 		sc->sc_hw.buttons = 3;
1169 
1170 		if (sc->sc_mode.level == 0) {
1171 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1172 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1173 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1174 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1175 		} else if (sc->sc_mode.level == 1) {
1176 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1177 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1178 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1179 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1180 		}
1181 		wsp_reset_buf(sc);
1182 		break;
1183 	case MOUSE_GETSTATUS:{
1184 			mousestatus_t *status = (mousestatus_t *)addr;
1185 
1186 			*status = sc->sc_status;
1187 			sc->sc_status.obutton = sc->sc_status.button;
1188 			sc->sc_status.button = 0;
1189 			sc->sc_status.dx = 0;
1190 			sc->sc_status.dy = 0;
1191 			sc->sc_status.dz = 0;
1192 
1193 			if (status->dx || status->dy || status->dz)
1194 				status->flags |= MOUSE_POSCHANGED;
1195 			if (status->button != status->obutton)
1196 				status->flags |= MOUSE_BUTTONSCHANGED;
1197 			break;
1198 		}
1199 	default:
1200 		error = ENOTTY;
1201 	}
1202 
1203 done:
1204 	mtx_unlock(&sc->sc_mutex);
1205 	return (error);
1206 }
1207 
1208 static device_method_t wsp_methods[] = {
1209 	/* Device interface */
1210 	DEVMETHOD(device_probe, wsp_probe),
1211 	DEVMETHOD(device_attach, wsp_attach),
1212 	DEVMETHOD(device_detach, wsp_detach),
1213 	DEVMETHOD_END
1214 };
1215 
1216 static driver_t wsp_driver = {
1217 	.name = WSP_DRIVER_NAME,
1218 	.methods = wsp_methods,
1219 	.size = sizeof(struct wsp_softc)
1220 };
1221 
1222 static devclass_t wsp_devclass;
1223 
1224 DRIVER_MODULE(wsp, uhub, wsp_driver, wsp_devclass, NULL, 0);
1225 MODULE_DEPEND(wsp, usb, 1, 1, 1);
1226 MODULE_VERSION(wsp, 1);
1227