1261260Shselasky/*-
2261260Shselasky * Copyright (c) 2012 Huang Wen Hui
3261260Shselasky * All rights reserved.
4261260Shselasky *
5261260Shselasky * Redistribution and use in source and binary forms, with or without
6261260Shselasky * modification, are permitted provided that the following conditions
7261260Shselasky * are met:
8261260Shselasky * 1. Redistributions of source code must retain the above copyright
9261260Shselasky *    notice, this list of conditions and the following disclaimer.
10261260Shselasky * 2. Redistributions in binary form must reproduce the above copyright
11261260Shselasky *    notice, this list of conditions and the following disclaimer in the
12261260Shselasky *    documentation and/or other materials provided with the distribution.
13261260Shselasky *
14261260Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15261260Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16261260Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17261260Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18261260Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19261260Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20261260Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21261260Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22261260Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23261260Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24261260Shselasky * SUCH DAMAGE.
25261260Shselasky */
26261260Shselasky
27261260Shselasky#include <sys/cdefs.h>
28261260Shselasky__FBSDID("$FreeBSD$");
29261260Shselasky
30261260Shselasky#include <sys/param.h>
31261260Shselasky#include <sys/systm.h>
32261260Shselasky#include <sys/kernel.h>
33261260Shselasky#include <sys/malloc.h>
34261260Shselasky#include <sys/module.h>
35261260Shselasky#include <sys/lock.h>
36261260Shselasky#include <sys/mutex.h>
37261260Shselasky#include <sys/bus.h>
38261260Shselasky#include <sys/conf.h>
39261260Shselasky#include <sys/fcntl.h>
40261260Shselasky#include <sys/file.h>
41261260Shselasky#include <sys/selinfo.h>
42261260Shselasky#include <sys/poll.h>
43261260Shselasky#include <sys/sysctl.h>
44261260Shselasky
45261260Shselasky#include <dev/usb/usb.h>
46261260Shselasky#include <dev/usb/usbdi.h>
47261260Shselasky#include <dev/usb/usbdi_util.h>
48261260Shselasky#include <dev/usb/usbhid.h>
49261260Shselasky
50261260Shselasky#include "usbdevs.h"
51261260Shselasky
52261260Shselasky#define	USB_DEBUG_VAR wsp_debug
53261260Shselasky#include <dev/usb/usb_debug.h>
54261260Shselasky
55261260Shselasky#include <sys/mouse.h>
56261260Shselasky
57261260Shselasky#define	WSP_DRIVER_NAME "wsp"
58262368Shselasky#define	WSP_BUFFER_MAX	1024
59261260Shselasky
60261260Shselasky#define	WSP_CLAMP(x,low,high) do {		\
61261260Shselasky	if ((x) < (low))			\
62261260Shselasky		(x) = (low);			\
63261260Shselasky	else if ((x) > (high))			\
64261260Shselasky		(x) = (high);			\
65261260Shselasky} while (0)
66261260Shselasky
67261260Shselasky/* Tunables */
68261260Shselaskystatic	SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW, 0, "USB wsp");
69261260Shselasky
70261260Shselasky#ifdef USB_DEBUG
71261260Shselaskyenum wsp_log_level {
72261260Shselasky	WSP_LLEVEL_DISABLED = 0,
73261260Shselasky	WSP_LLEVEL_ERROR,
74261260Shselasky	WSP_LLEVEL_DEBUG,		/* for troubleshooting */
75261260Shselasky	WSP_LLEVEL_INFO,		/* for diagnostics */
76261260Shselasky};
77261260Shselaskystatic int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */
78261260Shselasky
79261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RW,
80261260Shselasky    &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level");
81261260Shselasky#endif					/* USB_DEBUG */
82261260Shselasky
83261260Shselaskystatic struct wsp_tuning {
84261260Shselasky	int	scale_factor;
85261260Shselasky	int	z_factor;
86261260Shselasky	int	pressure_touch_threshold;
87261260Shselasky	int	pressure_untouch_threshold;
88261260Shselasky	int	pressure_tap_threshold;
89261260Shselasky	int	scr_hor_threshold;
90261260Shselasky}
91261260Shselasky	wsp_tuning =
92261260Shselasky{
93261260Shselasky	.scale_factor = 12,
94261260Shselasky	.z_factor = 5,
95261260Shselasky	.pressure_touch_threshold = 50,
96261260Shselasky	.pressure_untouch_threshold = 10,
97263071Shselasky	.pressure_tap_threshold = 100,
98263071Shselasky	.scr_hor_threshold = 10,
99261260Shselasky};
100261260Shselasky
101261260Shselaskystatic void
102261260Shselaskywsp_runing_rangecheck(struct wsp_tuning *ptun)
103261260Shselasky{
104261260Shselasky	WSP_CLAMP(ptun->scale_factor, 1, 63);
105261260Shselasky	WSP_CLAMP(ptun->z_factor, 1, 63);
106261260Shselasky	WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255);
107261260Shselasky	WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255);
108261260Shselasky	WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255);
109261260Shselasky	WSP_CLAMP(ptun->scr_hor_threshold, 1, 255);
110261260Shselasky}
111261260Shselasky
112261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RW,
113261260Shselasky    &wsp_tuning.scale_factor, 0, "movement scale factor");
114261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RW,
115261260Shselasky    &wsp_tuning.z_factor, 0, "Z-axis scale factor");
116261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RW,
117261260Shselasky    &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold");
118261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RW,
119261260Shselasky    &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold");
120261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RW,
121261260Shselasky    &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold");
122261260ShselaskySYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_hor_threshold, CTLFLAG_RW,
123261260Shselasky    &wsp_tuning.scr_hor_threshold, 0, "horizontal scrolling threshold");
124261260Shselasky
125261260Shselasky#define	WSP_IFACE_INDEX	1
126261260Shselasky
127261510Shselasky/*
128262368Shselasky * Some tables, structures, definitions and constant values for the
129262368Shselasky * touchpad protocol has been copied from Linux's
130261510Shselasky * "drivers/input/mouse/bcm5974.c" which has the following copyright
131261510Shselasky * holders under GPLv2. All device specific code in this driver has
132261510Shselasky * been written from scratch. The decoding algorithm is based on
133262368Shselasky * output from FreeBSD's usbdump.
134261510Shselasky *
135261510Shselasky * Copyright (C) 2008      Henrik Rydberg (rydberg@euromail.se)
136261510Shselasky * Copyright (C) 2008      Scott Shawcroft (scott.shawcroft@gmail.com)
137261510Shselasky * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
138261510Shselasky * Copyright (C) 2005      Johannes Berg (johannes@sipsolutions.net)
139261510Shselasky * Copyright (C) 2005      Stelian Pop (stelian@popies.net)
140261510Shselasky * Copyright (C) 2005      Frank Arnold (frank@scirocco-5v-turbo.de)
141261510Shselasky * Copyright (C) 2005      Peter Osterlund (petero2@telia.com)
142261510Shselasky * Copyright (C) 2005      Michael Hanselmann (linux-kernel@hansmi.ch)
143261510Shselasky * Copyright (C) 2006      Nicolas Boichat (nicolas@boichat.ch)
144261510Shselasky */
145261510Shselasky
146261260Shselasky/* button data structure */
147261260Shselaskystruct bt_data {
148261260Shselasky	uint8_t	unknown1;		/* constant */
149261260Shselasky	uint8_t	button;			/* left button */
150261260Shselasky	uint8_t	rel_x;			/* relative x coordinate */
151261260Shselasky	uint8_t	rel_y;			/* relative y coordinate */
152261260Shselasky} __packed;
153261260Shselasky
154261260Shselasky/* trackpad header types */
155261260Shselaskyenum tp_type {
156261260Shselasky	TYPE1,			/* plain trackpad */
157261260Shselasky	TYPE2,			/* button integrated in trackpad */
158261260Shselasky	TYPE3			/* additional header fields since June 2013 */
159261260Shselasky};
160261260Shselasky
161261260Shselasky/* trackpad finger data offsets, le16-aligned */
162261260Shselasky#define	FINGER_TYPE1		(13 * 2)
163261260Shselasky#define	FINGER_TYPE2		(15 * 2)
164261260Shselasky#define	FINGER_TYPE3		(19 * 2)
165261260Shselasky
166261260Shselasky/* trackpad button data offsets */
167261260Shselasky#define	BUTTON_TYPE2		15
168261260Shselasky#define	BUTTON_TYPE3		23
169261260Shselasky
170261260Shselasky/* list of device capability bits */
171261260Shselasky#define	HAS_INTEGRATED_BUTTON	1
172261260Shselasky
173261260Shselasky/* trackpad finger header - little endian */
174261260Shselaskystruct tp_header {
175261260Shselasky	uint8_t	flag;
176261260Shselasky	uint8_t	sn0;
177261260Shselasky	uint16_t wFixed0;
178261260Shselasky	uint32_t dwSn1;
179261260Shselasky	uint32_t dwFixed1;
180261260Shselasky	uint16_t wLength;
181261260Shselasky	uint8_t	nfinger;
182261260Shselasky	uint8_t	ibt;
183261260Shselasky	int16_t	wUnknown[6];
184261260Shselasky	uint8_t	q1;
185261260Shselasky	uint8_t	q2;
186261260Shselasky} __packed;
187261260Shselasky
188261260Shselasky/* trackpad finger structure - little endian */
189261260Shselaskystruct tp_finger {
190261260Shselasky	int16_t	origin;			/* zero when switching track finger */
191261260Shselasky	int16_t	abs_x;			/* absolute x coodinate */
192261260Shselasky	int16_t	abs_y;			/* absolute y coodinate */
193261260Shselasky	int16_t	rel_x;			/* relative x coodinate */
194261260Shselasky	int16_t	rel_y;			/* relative y coodinate */
195261260Shselasky	int16_t	tool_major;		/* tool area, major axis */
196261260Shselasky	int16_t	tool_minor;		/* tool area, minor axis */
197261260Shselasky	int16_t	orientation;		/* 16384 when point, else 15 bit angle */
198261260Shselasky	int16_t	touch_major;		/* touch area, major axis */
199261260Shselasky	int16_t	touch_minor;		/* touch area, minor axis */
200261260Shselasky	int16_t	unused[3];		/* zeros */
201261260Shselasky	int16_t	multi;			/* one finger: varies, more fingers:
202261260Shselasky					 * constant */
203261260Shselasky} __packed;
204261260Shselasky
205261260Shselasky/* trackpad finger data size, empirically at least ten fingers */
206261260Shselasky#define	MAX_FINGERS		16
207261260Shselasky#define	SIZEOF_FINGER		sizeof(struct tp_finger)
208261260Shselasky#define	SIZEOF_ALL_FINGERS	(MAX_FINGERS * SIZEOF_FINGER)
209261260Shselasky
210262368Shselasky#if (WSP_BUFFER_MAX < ((MAX_FINGERS * 14 * 2) + FINGER_TYPE3))
211262368Shselasky#error "WSP_BUFFER_MAX is too small"
212262368Shselasky#endif
213261260Shselasky
214261260Shselaskyenum {
215261260Shselasky	WSP_FLAG_WELLSPRING1,
216261260Shselasky	WSP_FLAG_WELLSPRING2,
217261260Shselasky	WSP_FLAG_WELLSPRING3,
218261260Shselasky	WSP_FLAG_WELLSPRING4,
219261260Shselasky	WSP_FLAG_WELLSPRING4A,
220261260Shselasky	WSP_FLAG_WELLSPRING5,
221261260Shselasky	WSP_FLAG_WELLSPRING6A,
222261260Shselasky	WSP_FLAG_WELLSPRING6,
223261260Shselasky	WSP_FLAG_WELLSPRING5A,
224261260Shselasky	WSP_FLAG_WELLSPRING7,
225261260Shselasky	WSP_FLAG_WELLSPRING7A,
226261260Shselasky	WSP_FLAG_WELLSPRING8,
227261260Shselasky	WSP_FLAG_MAX,
228261260Shselasky};
229261260Shselasky
230261260Shselasky/* device-specific configuration */
231261260Shselaskystruct wsp_dev_params {
232261260Shselasky	uint8_t	caps;			/* device capability bitmask */
233261260Shselasky	uint8_t	tp_type;		/* type of trackpad interface */
234261260Shselasky	uint8_t	tp_offset;		/* offset to trackpad finger data */
235261260Shselasky};
236261260Shselasky
237261260Shselaskystatic const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = {
238261260Shselasky	[WSP_FLAG_WELLSPRING1] = {
239261260Shselasky		.caps = 0,
240261260Shselasky		.tp_type = TYPE1,
241261260Shselasky		.tp_offset = FINGER_TYPE1,
242261260Shselasky	},
243261260Shselasky	[WSP_FLAG_WELLSPRING2] = {
244261260Shselasky		.caps = 0,
245261260Shselasky		.tp_type = TYPE1,
246261260Shselasky		.tp_offset = FINGER_TYPE1,
247261260Shselasky	},
248261260Shselasky	[WSP_FLAG_WELLSPRING3] = {
249261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
250261260Shselasky		.tp_type = TYPE2,
251261260Shselasky		.tp_offset = FINGER_TYPE2,
252261260Shselasky	},
253261260Shselasky	[WSP_FLAG_WELLSPRING4] = {
254261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
255261260Shselasky		.tp_type = TYPE2,
256261260Shselasky		.tp_offset = FINGER_TYPE2,
257261260Shselasky	},
258261260Shselasky	[WSP_FLAG_WELLSPRING4A] = {
259261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
260261260Shselasky		.tp_type = TYPE2,
261261260Shselasky		.tp_offset = FINGER_TYPE2,
262261260Shselasky	},
263261260Shselasky	[WSP_FLAG_WELLSPRING5] = {
264261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
265261260Shselasky		.tp_type = TYPE2,
266261260Shselasky		.tp_offset = FINGER_TYPE2,
267261260Shselasky	},
268261260Shselasky	[WSP_FLAG_WELLSPRING6] = {
269261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
270261260Shselasky		.tp_type = TYPE2,
271261260Shselasky		.tp_offset = FINGER_TYPE2,
272261260Shselasky	},
273261260Shselasky	[WSP_FLAG_WELLSPRING5A] = {
274261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
275261260Shselasky		.tp_type = TYPE2,
276261260Shselasky		.tp_offset = FINGER_TYPE2,
277261260Shselasky	},
278261260Shselasky	[WSP_FLAG_WELLSPRING6A] = {
279261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
280261260Shselasky		.tp_type = TYPE2,
281261260Shselasky		.tp_offset = FINGER_TYPE2,
282261260Shselasky	},
283261260Shselasky	[WSP_FLAG_WELLSPRING7] = {
284261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
285261260Shselasky		.tp_type = TYPE2,
286261260Shselasky		.tp_offset = FINGER_TYPE2,
287261260Shselasky	},
288261260Shselasky	[WSP_FLAG_WELLSPRING7A] = {
289261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
290261260Shselasky		.tp_type = TYPE2,
291261260Shselasky		.tp_offset = FINGER_TYPE2,
292261260Shselasky	},
293261260Shselasky	[WSP_FLAG_WELLSPRING8] = {
294261260Shselasky		.caps = HAS_INTEGRATED_BUTTON,
295261260Shselasky		.tp_type = TYPE3,
296261260Shselasky		.tp_offset = FINGER_TYPE3,
297261260Shselasky	},
298261260Shselasky};
299261260Shselasky
300261260Shselasky#define	WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
301261260Shselasky
302261260Shselaskystatic const STRUCT_USB_HOST_ID wsp_devs[] = {
303261260Shselasky	/* MacbookAir1.1 */
304261260Shselasky	WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1),
305261260Shselasky	WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1),
306261260Shselasky	WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1),
307261260Shselasky
308261260Shselasky	/* MacbookProPenryn, aka wellspring2 */
309261260Shselasky	WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2),
310261260Shselasky	WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2),
311261260Shselasky	WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2),
312261260Shselasky
313261260Shselasky	/* Macbook5,1 (unibody), aka wellspring3 */
314261260Shselasky	WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3),
315261260Shselasky	WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3),
316261260Shselasky	WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3),
317261260Shselasky
318261260Shselasky	/* MacbookAir3,2 (unibody), aka wellspring4 */
319261260Shselasky	WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4),
320261260Shselasky	WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4),
321261260Shselasky	WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4),
322261260Shselasky
323261260Shselasky	/* MacbookAir3,1 (unibody), aka wellspring4 */
324261260Shselasky	WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A),
325261260Shselasky	WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A),
326261260Shselasky	WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A),
327261260Shselasky
328261260Shselasky	/* Macbook8 (unibody, March 2011) */
329261260Shselasky	WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5),
330261260Shselasky	WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5),
331261260Shselasky	WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5),
332261260Shselasky
333261260Shselasky	/* MacbookAir4,1 (unibody, July 2011) */
334261260Shselasky	WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A),
335261260Shselasky	WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A),
336261260Shselasky	WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A),
337261260Shselasky
338261260Shselasky	/* MacbookAir4,2 (unibody, July 2011) */
339261260Shselasky	WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6),
340261260Shselasky	WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6),
341261260Shselasky	WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6),
342261260Shselasky
343261260Shselasky	/* Macbook8,2 (unibody) */
344261260Shselasky	WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A),
345261260Shselasky	WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A),
346261260Shselasky	WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A),
347261260Shselasky
348261260Shselasky	/* MacbookPro10,1 (unibody, June 2012) */
349261260Shselasky	/* MacbookPro11,? (unibody, June 2013) */
350261260Shselasky	WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7),
351261260Shselasky	WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7),
352261260Shselasky	WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7),
353261260Shselasky
354261260Shselasky	/* MacbookPro10,2 (unibody, October 2012) */
355261260Shselasky	WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A),
356261260Shselasky	WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A),
357261260Shselasky	WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A),
358261260Shselasky
359261260Shselasky	/* MacbookAir6,2 (unibody, June 2013) */
360261260Shselasky	WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8),
361261260Shselasky	WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8),
362261260Shselasky	WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8),
363261260Shselasky};
364261260Shselasky
365261260Shselasky#define	WSP_FIFO_BUF_SIZE	 8	/* bytes */
366261260Shselasky#define	WSP_FIFO_QUEUE_MAXLEN	50	/* units */
367261260Shselasky
368261260Shselaskyenum {
369261260Shselasky	WSP_INTR_DT,
370261260Shselasky	WSP_N_TRANSFER,
371261260Shselasky};
372261260Shselasky
373261260Shselaskystruct wsp_softc {
374261260Shselasky	struct usb_device *sc_usb_device;
375261260Shselasky	struct mtx sc_mutex;		/* for synchronization */
376261260Shselasky	struct usb_xfer *sc_xfer[WSP_N_TRANSFER];
377261260Shselasky	struct usb_fifo_sc sc_fifo;
378261260Shselasky
379261260Shselasky	const struct wsp_dev_params *sc_params;	/* device configuration */
380261260Shselasky
381261260Shselasky	mousehw_t sc_hw;
382261260Shselasky	mousemode_t sc_mode;
383261260Shselasky	u_int	sc_pollrate;
384261260Shselasky	mousestatus_t sc_status;
385261260Shselasky	u_int	sc_state;
386261260Shselasky#define	WSP_ENABLED	       0x01
387261260Shselasky
388261260Shselasky	struct tp_finger *index[MAX_FINGERS];	/* finger index data */
389261260Shselasky	int16_t	pos_x[MAX_FINGERS];	/* position array */
390261260Shselasky	int16_t	pos_y[MAX_FINGERS];	/* position array */
391261260Shselasky	u_int	sc_touch;		/* touch status */
392261260Shselasky#define	WSP_UNTOUCH		0x00
393261260Shselasky#define	WSP_FIRST_TOUCH		0x01
394261260Shselasky#define	WSP_SECOND_TOUCH	0x02
395261260Shselasky#define	WSP_TOUCHING		0x04
396261260Shselasky	int16_t	pre_pos_x;		/* previous position array */
397261260Shselasky	int16_t	pre_pos_y;		/* previous position array */
398261260Shselasky	int	dx_sum;			/* x axis cumulative movement */
399261260Shselasky	int	dy_sum;			/* y axis cumulative movement */
400261260Shselasky	int	dz_sum;			/* z axis cumulative movement */
401261260Shselasky	int	dz_count;
402261260Shselasky#define	WSP_DZ_MAX_COUNT	32
403261260Shselasky	int	dt_sum;			/* T-axis cumulative movement */
404263209Shselasky	int	rdx;			/* x axis remainder of divide by scale_factor */
405263209Shselasky	int	rdy;			/* y axis remainder of divide by scale_factor */
406263209Shselasky	int	rdz;			/* z axis remainder of divide by scale_factor */
407262368Shselasky	int	tp_datalen;
408261510Shselasky	uint8_t o_ntouch;		/* old touch finger status */
409261260Shselasky	uint8_t	finger;			/* 0 or 1 *, check which finger moving */
410261260Shselasky	uint16_t intr_count;
411261260Shselasky#define	WSP_TAP_THRESHOLD	3
412261260Shselasky#define	WSP_TAP_MAX_COUNT	20
413261260Shselasky	int	distance;		/* the distance of 2 fingers */
414261260Shselasky#define	MAX_DISTANCE		2500	/* the max allowed distance */
415261260Shselasky	uint8_t	ibtn;			/* button status in tapping */
416261260Shselasky	uint8_t	ntaps;			/* finger status in tapping */
417261260Shselasky	uint8_t	scr_mode;		/* scroll status in movement */
418261260Shselasky#define	WSP_SCR_NONE		0
419261260Shselasky#define	WSP_SCR_VER		1
420261260Shselasky#define	WSP_SCR_HOR		2
421262368Shselasky	uint8_t tp_data[WSP_BUFFER_MAX] __aligned(4);		/* trackpad transferred data */
422261260Shselasky};
423261260Shselasky
424261260Shselaskytypedef enum interface_mode {
425261260Shselasky	RAW_SENSOR_MODE = 0x01,
426261260Shselasky	HID_MODE = 0x08
427261260Shselasky} interface_mode;
428261260Shselasky
429261260Shselasky/*
430261260Shselasky * function prototypes
431261260Shselasky */
432261260Shselaskystatic usb_fifo_cmd_t wsp_start_read;
433261260Shselaskystatic usb_fifo_cmd_t wsp_stop_read;
434261260Shselaskystatic usb_fifo_open_t wsp_open;
435261260Shselaskystatic usb_fifo_close_t wsp_close;
436261260Shselaskystatic usb_fifo_ioctl_t wsp_ioctl;
437261260Shselasky
438261260Shselaskystatic struct usb_fifo_methods wsp_fifo_methods = {
439261260Shselasky	.f_open = &wsp_open,
440261260Shselasky	.f_close = &wsp_close,
441261260Shselasky	.f_ioctl = &wsp_ioctl,
442261260Shselasky	.f_start_read = &wsp_start_read,
443261260Shselasky	.f_stop_read = &wsp_stop_read,
444261260Shselasky	.basename[0] = WSP_DRIVER_NAME,
445261260Shselasky};
446261260Shselasky
447261260Shselasky/* device initialization and shutdown */
448261260Shselaskystatic int wsp_enable(struct wsp_softc *sc);
449261260Shselaskystatic void wsp_disable(struct wsp_softc *sc);
450261260Shselasky
451261260Shselasky/* updating fifo */
452261260Shselaskystatic void wsp_reset_buf(struct wsp_softc *sc);
453261260Shselaskystatic void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t);
454261260Shselasky
455261260Shselasky/* Device methods. */
456261260Shselaskystatic device_probe_t wsp_probe;
457261260Shselaskystatic device_attach_t wsp_attach;
458261260Shselaskystatic device_detach_t wsp_detach;
459261260Shselaskystatic usb_callback_t wsp_intr_callback;
460261260Shselasky
461262368Shselaskystatic const struct usb_config wsp_config[WSP_N_TRANSFER] = {
462261260Shselasky	[WSP_INTR_DT] = {
463261260Shselasky		.type = UE_INTERRUPT,
464261260Shselasky		.endpoint = UE_ADDR_ANY,
465261260Shselasky		.direction = UE_DIR_IN,
466261260Shselasky		.flags = {
467261260Shselasky			.pipe_bof = 0,
468261260Shselasky			.short_xfer_ok = 1,
469261260Shselasky		},
470262368Shselasky		.bufsize = WSP_BUFFER_MAX,
471261260Shselasky		.callback = &wsp_intr_callback,
472261260Shselasky	},
473261260Shselasky};
474261260Shselasky
475261510Shselaskystatic usb_error_t
476261510Shselaskywsp_set_device_mode(struct wsp_softc *sc, interface_mode mode)
477261260Shselasky{
478261510Shselasky	uint8_t	mode_bytes[8];
479261260Shselasky	usb_error_t err;
480261260Shselasky
481261510Shselasky	err = usbd_req_get_report(sc->sc_usb_device, NULL,
482261510Shselasky	    mode_bytes, sizeof(mode_bytes), 0,
483261510Shselasky	    0x03, 0x00);
484261260Shselasky
485261510Shselasky	if (err != USB_ERR_NORMAL_COMPLETION) {
486261510Shselasky		DPRINTF("Failed to read device mode (%d)\n", err);
487261510Shselasky		return (err);
488261510Shselasky	}
489261260Shselasky
490261510Shselasky	/*
491261510Shselasky	 * XXX Need to wait at least 250ms for hardware to get
492261510Shselasky	 * ready. The device mode handling appears to be handled
493261510Shselasky	 * asynchronously and we should not issue these commands too
494261510Shselasky	 * quickly.
495261510Shselasky	 */
496261510Shselasky	pause("WHW", hz / 4);
497261260Shselasky
498261510Shselasky	mode_bytes[0] = mode;
499261260Shselasky
500261510Shselasky	return (usbd_req_set_report(sc->sc_usb_device, NULL,
501261510Shselasky	    mode_bytes, sizeof(mode_bytes), 0,
502261510Shselasky	    0x03, 0x00));
503261260Shselasky}
504261260Shselasky
505261260Shselaskystatic int
506261260Shselaskywsp_enable(struct wsp_softc *sc)
507261260Shselasky{
508261260Shselasky	/* reset status */
509261260Shselasky	memset(&sc->sc_status, 0, sizeof(sc->sc_status));
510261260Shselasky	sc->sc_state |= WSP_ENABLED;
511261260Shselasky
512261260Shselasky	DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n");
513261260Shselasky	return (0);
514261260Shselasky}
515261260Shselasky
516261260Shselaskystatic void
517261260Shselaskywsp_disable(struct wsp_softc *sc)
518261260Shselasky{
519261260Shselasky	sc->sc_state &= ~WSP_ENABLED;
520261260Shselasky	DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n");
521261260Shselasky}
522261260Shselasky
523261260Shselaskystatic int
524261260Shselaskywsp_probe(device_t self)
525261260Shselasky{
526261260Shselasky	struct usb_attach_arg *uaa = device_get_ivars(self);
527261260Shselasky
528261260Shselasky	if (uaa->usb_mode != USB_MODE_HOST)
529261260Shselasky		return (ENXIO);
530261260Shselasky
531261260Shselasky	if (uaa->info.bIfaceIndex != WSP_IFACE_INDEX)
532261260Shselasky		return (ENXIO);
533261260Shselasky
534261260Shselasky	if ((uaa->info.bInterfaceClass != UICLASS_HID) ||
535261260Shselasky	    (uaa->info.bInterfaceProtocol != 0))
536261260Shselasky		return (ENXIO);
537261260Shselasky
538261260Shselasky	return (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa));
539261260Shselasky}
540261260Shselasky
541261260Shselaskystatic int
542261260Shselaskywsp_attach(device_t dev)
543261260Shselasky{
544261260Shselasky	struct wsp_softc *sc = device_get_softc(dev);
545261260Shselasky	struct usb_attach_arg *uaa = device_get_ivars(dev);
546261260Shselasky	usb_error_t err;
547262368Shselasky	void *d_ptr = NULL;
548262368Shselasky	uint16_t d_len;
549261260Shselasky
550261260Shselasky	DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc);
551261260Shselasky
552262368Shselasky	/* Get HID descriptor */
553262368Shselasky	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
554262368Shselasky	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
555262368Shselasky
556262368Shselasky	if (err == USB_ERR_NORMAL_COMPLETION) {
557262368Shselasky		/* Get HID report descriptor length */
558262368Shselasky		sc->tp_datalen = hid_report_size(d_ptr, d_len, hid_input, NULL);
559262368Shselasky		free(d_ptr, M_TEMP);
560262368Shselasky
561262368Shselasky		if (sc->tp_datalen <= 0 || sc->tp_datalen > WSP_BUFFER_MAX) {
562262368Shselasky			DPRINTF("Invalid datalength or too big "
563262368Shselasky			    "datalength: %d\n", sc->tp_datalen);
564262368Shselasky			return (ENXIO);
565262368Shselasky		}
566262368Shselasky	} else {
567262368Shselasky		return (ENXIO);
568262368Shselasky	}
569262368Shselasky
570261260Shselasky	sc->sc_usb_device = uaa->device;
571261260Shselasky
572261260Shselasky	/*
573261510Shselasky	 * By default the touchpad behaves like a HID device, sending
574261260Shselasky	 * packets with reportID = 8. Such reports contain only
575261260Shselasky	 * limited information. They encode movement deltas and button
576261260Shselasky	 * events, but do not include data from the pressure
577261260Shselasky	 * sensors. The device input mode can be switched from HID
578261260Shselasky	 * reports to raw sensor data using vendor-specific USB
579261510Shselasky	 * control commands:
580261260Shselasky	 */
581261510Shselasky
582261510Shselasky	/*
583261510Shselasky	 * During re-enumeration of the device we need to force the
584261510Shselasky	 * device back into HID mode before switching it to RAW
585261510Shselasky	 * mode. Else the device does not work like expected.
586261510Shselasky	 */
587261510Shselasky	err = wsp_set_device_mode(sc, HID_MODE);
588261260Shselasky	if (err != USB_ERR_NORMAL_COMPLETION) {
589261510Shselasky		DPRINTF("Failed to set mode to HID MODE (%d)\n", err);
590261260Shselasky		return (ENXIO);
591261260Shselasky	}
592261510Shselasky
593261510Shselasky	err = wsp_set_device_mode(sc, RAW_SENSOR_MODE);
594261510Shselasky	if (err != USB_ERR_NORMAL_COMPLETION) {
595261510Shselasky		DPRINTF("failed to set mode to RAW MODE (%d)\n", err);
596261260Shselasky		return (ENXIO);
597261260Shselasky	}
598261510Shselasky
599261260Shselasky	mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE);
600261260Shselasky
601261260Shselasky	/* get device specific configuration */
602261260Shselasky	sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa);
603261260Shselasky
604261260Shselasky	err = usbd_transfer_setup(uaa->device,
605261260Shselasky	    &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config,
606261260Shselasky	    WSP_N_TRANSFER, sc, &sc->sc_mutex);
607261260Shselasky	if (err) {
608261260Shselasky		DPRINTF("error=%s\n", usbd_errstr(err));
609261260Shselasky		goto detach;
610261260Shselasky	}
611261260Shselasky	if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex,
612261260Shselasky	    &wsp_fifo_methods, &sc->sc_fifo,
613261260Shselasky	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
614261260Shselasky	    UID_ROOT, GID_OPERATOR, 0644)) {
615261260Shselasky		goto detach;
616261260Shselasky	}
617261260Shselasky	device_set_usb_desc(dev);
618261260Shselasky
619261260Shselasky	sc->sc_hw.buttons = 3;
620261260Shselasky	sc->sc_hw.iftype = MOUSE_IF_USB;
621261260Shselasky	sc->sc_hw.type = MOUSE_PAD;
622261260Shselasky	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
623261260Shselasky	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
624261260Shselasky	sc->sc_mode.rate = -1;
625261260Shselasky	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
626261260Shselasky	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
627261260Shselasky	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
628261260Shselasky	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
629261260Shselasky
630261260Shselasky	sc->sc_touch = WSP_UNTOUCH;
631261260Shselasky	sc->scr_mode = WSP_SCR_NONE;
632261260Shselasky
633261260Shselasky	return (0);
634261260Shselasky
635261260Shselaskydetach:
636261260Shselasky	wsp_detach(dev);
637261260Shselasky	return (ENOMEM);
638261260Shselasky}
639261260Shselasky
640261260Shselaskystatic int
641261260Shselaskywsp_detach(device_t dev)
642261260Shselasky{
643261260Shselasky	struct wsp_softc *sc = device_get_softc(dev);
644261260Shselasky
645261510Shselasky	(void) wsp_set_device_mode(sc, HID_MODE);
646261260Shselasky
647261260Shselasky	mtx_lock(&sc->sc_mutex);
648261260Shselasky	if (sc->sc_state & WSP_ENABLED)
649261260Shselasky		wsp_disable(sc);
650261260Shselasky	mtx_unlock(&sc->sc_mutex);
651261260Shselasky
652261260Shselasky	usb_fifo_detach(&sc->sc_fifo);
653261260Shselasky
654261260Shselasky	usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER);
655261260Shselasky
656261260Shselasky	mtx_destroy(&sc->sc_mutex);
657261260Shselasky
658261260Shselasky	return (0);
659261260Shselasky}
660261260Shselasky
661261260Shselaskystatic void
662261260Shselaskywsp_intr_callback(struct usb_xfer *xfer, usb_error_t error)
663261260Shselasky{
664261260Shselasky	struct wsp_softc *sc = usbd_xfer_softc(xfer);
665261260Shselasky	const struct wsp_dev_params *params = sc->sc_params;
666261260Shselasky	struct usb_page_cache *pc;
667261260Shselasky	struct tp_finger *f;
668261260Shselasky	struct tp_header *h;
669261260Shselasky	struct wsp_tuning tun = wsp_tuning;
670261260Shselasky	int ntouch = 0;			/* the finger number in touch */
671261260Shselasky	int ibt = 0;			/* button status */
672261260Shselasky	int dx = 0;
673261260Shselasky	int dy = 0;
674261260Shselasky	int dz = 0;
675263209Shselasky	int rdx = 0;
676263209Shselasky	int rdy = 0;
677263209Shselasky	int rdz = 0;
678261260Shselasky	int len;
679261260Shselasky	int i;
680261260Shselasky
681261260Shselasky	wsp_runing_rangecheck(&tun);
682261260Shselasky
683261260Shselasky	if (sc->dz_count == 0)
684261260Shselasky		sc->dz_count = WSP_DZ_MAX_COUNT;
685261260Shselasky
686261260Shselasky	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
687261260Shselasky
688261260Shselasky	switch (USB_GET_STATE(xfer)) {
689261260Shselasky	case USB_ST_TRANSFERRED:
690261260Shselasky
691262368Shselasky		/* copy out received data */
692261260Shselasky		pc = usbd_xfer_get_frame(xfer, 0);
693261260Shselasky		usbd_copy_out(pc, 0, sc->tp_data, len);
694261260Shselasky
695262368Shselasky		if (len < sc->tp_datalen) {
696262368Shselasky			/* make sure we don't process old data */
697262368Shselasky			memset(sc->tp_data + len, 0, sc->tp_datalen - len);
698262368Shselasky		}
699262368Shselasky
700261260Shselasky		h = (struct tp_header *)(sc->tp_data);
701261260Shselasky
702261260Shselasky		if (params->tp_type == TYPE2) {
703261260Shselasky			ibt = sc->tp_data[BUTTON_TYPE2];
704261260Shselasky			ntouch = sc->tp_data[BUTTON_TYPE2 - 1];
705261260Shselasky		} else if (params->tp_type == TYPE3) {
706261260Shselasky			ibt = sc->tp_data[BUTTON_TYPE3];
707261260Shselasky			ntouch = sc->tp_data[BUTTON_TYPE3 - 1];
708261260Shselasky		}
709261260Shselasky		/* range check */
710261260Shselasky		if (ntouch < 0)
711261260Shselasky			ntouch = 0;
712261260Shselasky		else if (ntouch > MAX_FINGERS)
713261260Shselasky			ntouch = MAX_FINGERS;
714261260Shselasky
715261260Shselasky		f = (struct tp_finger *)(sc->tp_data + params->tp_offset);
716261260Shselasky
717261260Shselasky		for (i = 0; i != ntouch; i++) {
718261260Shselasky			/* swap endianness, if any */
719261260Shselasky			if (le16toh(0x1234) != 0x1234) {
720261260Shselasky				f[i].origin = le16toh((uint16_t)f[i].origin);
721261260Shselasky				f[i].abs_x = le16toh((uint16_t)f[i].abs_x);
722261260Shselasky				f[i].abs_y = le16toh((uint16_t)f[i].abs_y);
723261260Shselasky				f[i].rel_x = le16toh((uint16_t)f[i].rel_x);
724261260Shselasky				f[i].rel_y = le16toh((uint16_t)f[i].rel_y);
725261260Shselasky				f[i].tool_major = le16toh((uint16_t)f[i].tool_major);
726261260Shselasky				f[i].tool_minor = le16toh((uint16_t)f[i].tool_minor);
727261260Shselasky				f[i].orientation = le16toh((uint16_t)f[i].orientation);
728261260Shselasky				f[i].touch_major = le16toh((uint16_t)f[i].touch_major);
729261260Shselasky				f[i].touch_minor = le16toh((uint16_t)f[i].touch_minor);
730261260Shselasky				f[i].multi = le16toh((uint16_t)f[i].multi);
731261260Shselasky			}
732261260Shselasky			DPRINTFN(WSP_LLEVEL_INFO, "[%d]ibt=%d, taps=%d, u=%x, o=%4d, ax=%5d, ay=%5d, "
733261260Shselasky			    "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%5d, tchmaj=%4d, tchmin=%4d, m=%4x\n",
734261260Shselasky			    i, ibt, ntouch, h->q2,
735261260Shselasky			    f[i].origin, f[i].abs_x, f[i].abs_y, f[i].rel_x, f[i].rel_y,
736261260Shselasky			    f[i].tool_major, f[i].tool_minor, f[i].orientation,
737261260Shselasky			    f[i].touch_major, f[i].touch_minor, f[i].multi);
738261260Shselasky
739261510Shselasky			sc->pos_x[i] = f[i].abs_x;
740262368Shselasky			sc->pos_y[i] = -f[i].abs_y;
741261510Shselasky			sc->index[i] = &f[i];
742261260Shselasky		}
743261260Shselasky
744261260Shselasky		sc->sc_status.flags &= ~MOUSE_POSCHANGED;
745261260Shselasky		sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED;
746261260Shselasky		sc->sc_status.obutton = sc->sc_status.button;
747261260Shselasky		sc->sc_status.button = 0;
748261260Shselasky
749261260Shselasky		if (ibt != 0) {
750261260Shselasky			sc->sc_status.button |= MOUSE_BUTTON1DOWN;
751261260Shselasky			sc->ibtn = 1;
752261260Shselasky		}
753261260Shselasky		if (h->q2 == 4)
754261260Shselasky			sc->intr_count++;
755261260Shselasky
756261510Shselasky		if (sc->ntaps < ntouch) {
757261510Shselasky			switch (ntouch) {
758261260Shselasky			case 1:
759261260Shselasky				if (f[0].touch_major > tun.pressure_tap_threshold)
760261260Shselasky					sc->ntaps = 1;
761261260Shselasky				break;
762261260Shselasky			case 2:
763261260Shselasky				if (f[0].touch_major > tun.pressure_tap_threshold &&
764261260Shselasky				    f[1].touch_major > tun.pressure_tap_threshold)
765261260Shselasky					sc->ntaps = 2;
766261260Shselasky				break;
767261260Shselasky			case 3:
768261260Shselasky				if (f[0].touch_major > tun.pressure_tap_threshold &&
769261260Shselasky				    f[1].touch_major > tun.pressure_tap_threshold &&
770261260Shselasky				    f[2].touch_major > tun.pressure_tap_threshold)
771261260Shselasky					sc->ntaps = 3;
772261260Shselasky				break;
773261260Shselasky			default:
774261260Shselasky				break;
775261260Shselasky			}
776261260Shselasky		}
777261510Shselasky		if (ntouch == 2) {
778261260Shselasky			sc->distance = max(sc->distance, max(
779261260Shselasky			    abs(sc->pos_x[0] - sc->pos_x[1]),
780261260Shselasky			    abs(sc->pos_y[0] - sc->pos_y[1])));
781261260Shselasky		}
782261260Shselasky		if (f[0].touch_major < tun.pressure_untouch_threshold &&
783261260Shselasky		    sc->sc_status.button == 0) {
784261260Shselasky			sc->sc_touch = WSP_UNTOUCH;
785261260Shselasky			if (sc->intr_count < WSP_TAP_MAX_COUNT &&
786261260Shselasky			    sc->intr_count > WSP_TAP_THRESHOLD &&
787261260Shselasky			    sc->ntaps && sc->ibtn == 0) {
788261260Shselasky				/*
789261260Shselasky				 * Add a pair of events (button-down and
790261260Shselasky				 * button-up).
791261260Shselasky				 */
792261260Shselasky				switch (sc->ntaps) {
793261260Shselasky				case 1:
794262368Shselasky					if (!(params->caps & HAS_INTEGRATED_BUTTON)) {
795262368Shselasky						wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN);
796262368Shselasky						DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n");
797262368Shselasky					}
798261260Shselasky					break;
799261260Shselasky				case 2:
800263071Shselasky					DPRINTFN(WSP_LLEVEL_INFO, "sum_x=%5d, sum_y=%5d\n",
801263071Shselasky					    sc->dx_sum, sc->dy_sum);
802263071Shselasky					if (sc->distance < MAX_DISTANCE && abs(sc->dx_sum) < 5 &&
803263071Shselasky					    abs(sc->dy_sum) < 5) {
804261260Shselasky						wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN);
805263071Shselasky						DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n");
806263071Shselasky					}
807261260Shselasky					break;
808261260Shselasky				case 3:
809261260Shselasky					wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN);
810261260Shselasky					break;
811261260Shselasky				default:
812261260Shselasky					/* we don't handle taps of more than three fingers */
813261260Shselasky					break;
814261260Shselasky				}
815261260Shselasky				wsp_add_to_queue(sc, 0, 0, 0, 0);	/* button release */
816261260Shselasky			}
817263071Shselasky			if ((sc->dt_sum / tun.scr_hor_threshold) != 0 &&
818261260Shselasky			    sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) {
819261260Shselasky
820261260Shselasky				/*
821261260Shselasky				 * translate T-axis into button presses
822261260Shselasky				 * until further
823261260Shselasky				 */
824261260Shselasky				if (sc->dt_sum > 0)
825261260Shselasky					wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3);
826261260Shselasky				else if (sc->dt_sum < 0)
827261260Shselasky					wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4);
828261260Shselasky			}
829261260Shselasky			sc->dz_count = WSP_DZ_MAX_COUNT;
830261260Shselasky			sc->dz_sum = 0;
831261260Shselasky			sc->intr_count = 0;
832261260Shselasky			sc->ibtn = 0;
833261260Shselasky			sc->ntaps = 0;
834261260Shselasky			sc->finger = 0;
835261260Shselasky			sc->distance = 0;
836261260Shselasky			sc->dt_sum = 0;
837261260Shselasky			sc->dx_sum = 0;
838261260Shselasky			sc->dy_sum = 0;
839263209Shselasky			sc->rdx = 0;
840263209Shselasky			sc->rdy = 0;
841263209Shselasky			sc->rdz = 0;
842261260Shselasky			sc->scr_mode = WSP_SCR_NONE;
843261260Shselasky		} else if (f[0].touch_major >= tun.pressure_touch_threshold &&
844261260Shselasky		    sc->sc_touch == WSP_UNTOUCH) {	/* ignore first touch */
845261260Shselasky			sc->sc_touch = WSP_FIRST_TOUCH;
846261260Shselasky		} else if (f[0].touch_major >= tun.pressure_touch_threshold &&
847261260Shselasky		    sc->sc_touch == WSP_FIRST_TOUCH) {	/* ignore second touch */
848261260Shselasky			sc->sc_touch = WSP_SECOND_TOUCH;
849261260Shselasky			DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n",
850261260Shselasky			    sc->pre_pos_x, sc->pre_pos_y);
851261260Shselasky		} else {
852261260Shselasky			if (sc->sc_touch == WSP_SECOND_TOUCH)
853261260Shselasky				sc->sc_touch = WSP_TOUCHING;
854261260Shselasky
855261510Shselasky			if (ntouch != 0 &&
856261260Shselasky			    h->q2 == 4 &&
857261260Shselasky			    f[0].touch_major >= tun.pressure_touch_threshold) {
858261260Shselasky				dx = sc->pos_x[0] - sc->pre_pos_x;
859261260Shselasky				dy = sc->pos_y[0] - sc->pre_pos_y;
860261510Shselasky
861261510Shselasky				/* Ignore movement from ibt=1 to ibt=0 */
862261510Shselasky				if (sc->sc_status.obutton != 0 &&
863261510Shselasky				    sc->sc_status.button == 0) {
864261510Shselasky					dx = 0;
865261510Shselasky					dy = 0;
866261510Shselasky				}
867261510Shselasky				/* Ignore movement if ntouch changed */
868261510Shselasky				if (sc->o_ntouch != ntouch) {
869261510Shselasky					dx = 0;
870261510Shselasky					dy = 0;
871261510Shselasky				}
872261510Shselasky
873261510Shselasky				if (ntouch == 2 && sc->sc_status.button != 0) {
874261260Shselasky					dx = sc->pos_x[sc->finger] - sc->pre_pos_x;
875261260Shselasky					dy = sc->pos_y[sc->finger] - sc->pre_pos_y;
876261510Shselasky
877261510Shselasky					/*
878261510Shselasky					 * Ignore movement of switch finger or
879261510Shselasky					 * movement from ibt=0 to ibt=1
880261510Shselasky					 */
881261510Shselasky					if (f[0].origin == 0 || f[1].origin == 0 ||
882261510Shselasky					    sc->sc_status.obutton != sc->sc_status.button) {
883261260Shselasky						dx = 0;
884261260Shselasky						dy = 0;
885261260Shselasky						sc->finger = 0;
886261260Shselasky					}
887261260Shselasky					if ((abs(f[0].rel_x) + abs(f[0].rel_y)) <
888261260Shselasky					    (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
889261260Shselasky					    sc->finger == 0) {
890261260Shselasky						sc->sc_touch = WSP_SECOND_TOUCH;
891261260Shselasky						dx = 0;
892261260Shselasky						dy = 0;
893261260Shselasky						sc->finger = 1;
894261260Shselasky					}
895261260Shselasky					if ((abs(f[0].rel_x) + abs(f[0].rel_y)) >=
896261260Shselasky					    (abs(f[1].rel_x) + abs(f[1].rel_y)) &&
897261260Shselasky					    sc->finger == 1) {
898261260Shselasky						sc->sc_touch = WSP_SECOND_TOUCH;
899261260Shselasky						dx = 0;
900261260Shselasky						dy = 0;
901261260Shselasky						sc->finger = 0;
902261260Shselasky					}
903261260Shselasky					DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n",
904261260Shselasky					    dx, dy, sc->finger);
905261260Shselasky				}
906263209Shselasky				if (sc->dz_count--) {
907263209Shselasky					rdz = (dy + sc->rdz) % tun.scale_factor;
908263209Shselasky					sc->dz_sum -= (dy + sc->rdz) / tun.scale_factor;
909263209Shselasky					sc->rdz = rdz;
910263209Shselasky				}
911261260Shselasky				if ((sc->dz_sum / tun.z_factor) != 0)
912261260Shselasky					sc->dz_count = 0;
913261260Shselasky			}
914263209Shselasky			rdx = (dx + sc->rdx) % tun.scale_factor;
915263209Shselasky			dx = (dx + sc->rdx) / tun.scale_factor;
916263209Shselasky			sc->rdx = rdx;
917263209Shselasky
918263209Shselasky			rdy = (dy + sc->rdy) % tun.scale_factor;
919263209Shselasky			dy = (dy + sc->rdy) / tun.scale_factor;
920263209Shselasky			sc->rdy = rdy;
921263209Shselasky
922261260Shselasky			sc->dx_sum += dx;
923261260Shselasky			sc->dy_sum += dy;
924261260Shselasky
925261510Shselasky			if (ntouch == 2 && sc->sc_status.button == 0) {
926261260Shselasky				if (sc->scr_mode == WSP_SCR_NONE &&
927263071Shselasky				    abs(sc->dx_sum) + abs(sc->dy_sum) > tun.scr_hor_threshold)
928261260Shselasky					sc->scr_mode = abs(sc->dx_sum) >
929263209Shselasky					    abs(sc->dy_sum) * 3 ? WSP_SCR_HOR :
930261260Shselasky					    WSP_SCR_VER;
931261260Shselasky				DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n",
932261260Shselasky				    sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum);
933261260Shselasky				if (sc->scr_mode == WSP_SCR_HOR)
934261260Shselasky					sc->dt_sum += dx;
935261260Shselasky				else
936261260Shselasky					sc->dt_sum = 0;
937261260Shselasky
938261260Shselasky				dx = 0;
939261260Shselasky				dy = 0;
940261260Shselasky				if (sc->dz_count == 0)
941261260Shselasky					dz = sc->dz_sum / tun.z_factor;
942263071Shselasky				if (sc->scr_mode == WSP_SCR_HOR ||
943263071Shselasky				    abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE ||
944261260Shselasky				    abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE)
945261260Shselasky					dz = 0;
946261260Shselasky			}
947263071Shselasky			if (ntouch == 3) {
948263071Shselasky				dx = 0;
949263071Shselasky				dy = 0;
950263071Shselasky				dz = 0;
951263071Shselasky			}
952261260Shselasky			if (sc->intr_count < WSP_TAP_MAX_COUNT &&
953261260Shselasky			    abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3) {
954261260Shselasky				dx = dy = dz = 0;
955261260Shselasky			} else
956261260Shselasky				sc->intr_count = WSP_TAP_MAX_COUNT;
957261260Shselasky			if (dx || dy || dz)
958261260Shselasky				sc->sc_status.flags |= MOUSE_POSCHANGED;
959261260Shselasky			DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n",
960261260Shselasky			    dx, dy, dz, sc->sc_touch, sc->sc_status.button);
961261260Shselasky			sc->sc_status.dx += dx;
962261260Shselasky			sc->sc_status.dy += dy;
963261260Shselasky			sc->sc_status.dz += dz;
964261260Shselasky
965261260Shselasky			wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button);
966263209Shselasky			if (sc->dz_count == 0) {
967261260Shselasky				sc->dz_sum = 0;
968263209Shselasky				sc->rdz = 0;
969263209Shselasky			}
970261260Shselasky
971261260Shselasky		}
972261260Shselasky		sc->pre_pos_x = sc->pos_x[0];
973261260Shselasky		sc->pre_pos_y = sc->pos_y[0];
974261260Shselasky
975261510Shselasky		if (ntouch == 2 && sc->sc_status.button != 0) {
976261260Shselasky			sc->pre_pos_x = sc->pos_x[sc->finger];
977261260Shselasky			sc->pre_pos_y = sc->pos_y[sc->finger];
978261260Shselasky		}
979261510Shselasky		sc->o_ntouch = ntouch;
980261510Shselasky
981261260Shselasky	case USB_ST_SETUP:
982261260Shselaskytr_setup:
983261260Shselasky		/* check if we can put more data into the FIFO */
984261260Shselasky		if (usb_fifo_put_bytes_max(
985261260Shselasky		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
986261260Shselasky			usbd_xfer_set_frame_len(xfer, 0,
987262368Shselasky			    sc->tp_datalen);
988261260Shselasky			usbd_transfer_submit(xfer);
989261260Shselasky		}
990261260Shselasky		break;
991261260Shselasky
992261260Shselasky	default:			/* Error */
993261260Shselasky		if (error != USB_ERR_CANCELLED) {
994261260Shselasky			/* try clear stall first */
995261260Shselasky			usbd_xfer_set_stall(xfer);
996261260Shselasky			goto tr_setup;
997261260Shselasky		}
998261260Shselasky		break;
999261260Shselasky	}
1000261260Shselasky}
1001261260Shselasky
1002261260Shselaskystatic void
1003261260Shselaskywsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz,
1004261260Shselasky    uint32_t buttons_in)
1005261260Shselasky{
1006261260Shselasky	uint32_t buttons_out;
1007261260Shselasky	uint8_t buf[8];
1008261260Shselasky
1009261260Shselasky	dx = imin(dx, 254);
1010261260Shselasky	dx = imax(dx, -256);
1011261260Shselasky	dy = imin(dy, 254);
1012261260Shselasky	dy = imax(dy, -256);
1013261260Shselasky	dz = imin(dz, 126);
1014261260Shselasky	dz = imax(dz, -128);
1015261260Shselasky
1016261260Shselasky	buttons_out = MOUSE_MSC_BUTTONS;
1017261260Shselasky	if (buttons_in & MOUSE_BUTTON1DOWN)
1018261260Shselasky		buttons_out &= ~MOUSE_MSC_BUTTON1UP;
1019261260Shselasky	else if (buttons_in & MOUSE_BUTTON2DOWN)
1020261260Shselasky		buttons_out &= ~MOUSE_MSC_BUTTON2UP;
1021261260Shselasky	else if (buttons_in & MOUSE_BUTTON3DOWN)
1022261260Shselasky		buttons_out &= ~MOUSE_MSC_BUTTON3UP;
1023261260Shselasky
1024261260Shselasky	/* Encode the mouse data in standard format; refer to mouse(4) */
1025261260Shselasky	buf[0] = sc->sc_mode.syncmask[1];
1026261260Shselasky	buf[0] |= buttons_out;
1027261260Shselasky	buf[1] = dx >> 1;
1028261260Shselasky	buf[2] = dy >> 1;
1029261260Shselasky	buf[3] = dx - (dx >> 1);
1030261260Shselasky	buf[4] = dy - (dy >> 1);
1031261260Shselasky	/* Encode extra bytes for level 1 */
1032261260Shselasky	if (sc->sc_mode.level == 1) {
1033261260Shselasky		buf[5] = dz >> 1;	/* dz / 2 */
1034261260Shselasky		buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */
1035261260Shselasky		buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS);
1036261260Shselasky	}
1037261260Shselasky	usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
1038261260Shselasky	    sc->sc_mode.packetsize, 1);
1039261260Shselasky}
1040261260Shselasky
1041261260Shselaskystatic void
1042261260Shselaskywsp_reset_buf(struct wsp_softc *sc)
1043261260Shselasky{
1044261260Shselasky	/* reset read queue */
1045261260Shselasky	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
1046261260Shselasky}
1047261260Shselasky
1048261260Shselaskystatic void
1049261260Shselaskywsp_start_read(struct usb_fifo *fifo)
1050261260Shselasky{
1051261260Shselasky	struct wsp_softc *sc = usb_fifo_softc(fifo);
1052261260Shselasky	int rate;
1053261260Shselasky
1054261260Shselasky	/* Check if we should override the default polling interval */
1055261260Shselasky	rate = sc->sc_pollrate;
1056261260Shselasky	/* Range check rate */
1057261260Shselasky	if (rate > 1000)
1058261260Shselasky		rate = 1000;
1059261260Shselasky	/* Check for set rate */
1060261260Shselasky	if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) {
1061261260Shselasky		/* Stop current transfer, if any */
1062261260Shselasky		usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1063261260Shselasky		/* Set new interval */
1064261260Shselasky		usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate);
1065261260Shselasky		/* Only set pollrate once */
1066261260Shselasky		sc->sc_pollrate = 0;
1067261260Shselasky	}
1068261260Shselasky	usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]);
1069261260Shselasky}
1070261260Shselasky
1071261260Shselaskystatic void
1072261260Shselaskywsp_stop_read(struct usb_fifo *fifo)
1073261260Shselasky{
1074261260Shselasky	struct wsp_softc *sc = usb_fifo_softc(fifo);
1075261260Shselasky
1076261260Shselasky	usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]);
1077261260Shselasky}
1078261260Shselasky
1079261260Shselasky
1080261260Shselaskystatic int
1081261260Shselaskywsp_open(struct usb_fifo *fifo, int fflags)
1082261260Shselasky{
1083261260Shselasky	DPRINTFN(WSP_LLEVEL_INFO, "\n");
1084261260Shselasky
1085261260Shselasky	if (fflags & FREAD) {
1086261260Shselasky		struct wsp_softc *sc = usb_fifo_softc(fifo);
1087261260Shselasky		int rc;
1088261260Shselasky
1089261260Shselasky		if (sc->sc_state & WSP_ENABLED)
1090261260Shselasky			return (EBUSY);
1091261260Shselasky
1092261260Shselasky		if (usb_fifo_alloc_buffer(fifo,
1093261260Shselasky		    WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) {
1094261260Shselasky			return (ENOMEM);
1095261260Shselasky		}
1096261260Shselasky		rc = wsp_enable(sc);
1097261260Shselasky		if (rc != 0) {
1098261260Shselasky			usb_fifo_free_buffer(fifo);
1099261260Shselasky			return (rc);
1100261260Shselasky		}
1101261260Shselasky	}
1102261260Shselasky	return (0);
1103261260Shselasky}
1104261260Shselasky
1105261260Shselaskystatic void
1106261260Shselaskywsp_close(struct usb_fifo *fifo, int fflags)
1107261260Shselasky{
1108261260Shselasky	if (fflags & FREAD) {
1109261260Shselasky		struct wsp_softc *sc = usb_fifo_softc(fifo);
1110261260Shselasky
1111261260Shselasky		wsp_disable(sc);
1112261260Shselasky		usb_fifo_free_buffer(fifo);
1113261260Shselasky	}
1114261260Shselasky}
1115261260Shselasky
1116261260Shselaskyint
1117261260Shselaskywsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1118261260Shselasky{
1119261260Shselasky	struct wsp_softc *sc = usb_fifo_softc(fifo);
1120261260Shselasky	mousemode_t mode;
1121261260Shselasky	int error = 0;
1122261260Shselasky
1123261260Shselasky	mtx_lock(&sc->sc_mutex);
1124261260Shselasky
1125261260Shselasky	switch (cmd) {
1126261260Shselasky	case MOUSE_GETHWINFO:
1127261260Shselasky		*(mousehw_t *)addr = sc->sc_hw;
1128261260Shselasky		break;
1129261260Shselasky	case MOUSE_GETMODE:
1130261260Shselasky		*(mousemode_t *)addr = sc->sc_mode;
1131261260Shselasky		break;
1132261260Shselasky	case MOUSE_SETMODE:
1133261260Shselasky		mode = *(mousemode_t *)addr;
1134261260Shselasky
1135261260Shselasky		if (mode.level == -1)
1136261260Shselasky			/* Don't change the current setting */
1137261260Shselasky			;
1138261260Shselasky		else if ((mode.level < 0) || (mode.level > 1)) {
1139261260Shselasky			error = EINVAL;
1140261260Shselasky			goto done;
1141261260Shselasky		}
1142261260Shselasky		sc->sc_mode.level = mode.level;
1143261260Shselasky		sc->sc_pollrate = mode.rate;
1144261260Shselasky		sc->sc_hw.buttons = 3;
1145261260Shselasky
1146261260Shselasky		if (sc->sc_mode.level == 0) {
1147261260Shselasky			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1148261260Shselasky			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1149261260Shselasky			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1150261260Shselasky			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1151261260Shselasky		} else if (sc->sc_mode.level == 1) {
1152261260Shselasky			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1153261260Shselasky			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1154261260Shselasky			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1155261260Shselasky			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1156261260Shselasky		}
1157261260Shselasky		wsp_reset_buf(sc);
1158261260Shselasky		break;
1159261260Shselasky	case MOUSE_GETLEVEL:
1160261260Shselasky		*(int *)addr = sc->sc_mode.level;
1161261260Shselasky		break;
1162261260Shselasky	case MOUSE_SETLEVEL:
1163261260Shselasky		if (*(int *)addr < 0 || *(int *)addr > 1) {
1164261260Shselasky			error = EINVAL;
1165261260Shselasky			goto done;
1166261260Shselasky		}
1167261260Shselasky		sc->sc_mode.level = *(int *)addr;
1168261260Shselasky		sc->sc_hw.buttons = 3;
1169261260Shselasky
1170261260Shselasky		if (sc->sc_mode.level == 0) {
1171261260Shselasky			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1172261260Shselasky			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1173261260Shselasky			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1174261260Shselasky			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1175261260Shselasky		} else if (sc->sc_mode.level == 1) {
1176261260Shselasky			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1177261260Shselasky			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1178261260Shselasky			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1179261260Shselasky			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1180261260Shselasky		}
1181261260Shselasky		wsp_reset_buf(sc);
1182261260Shselasky		break;
1183261260Shselasky	case MOUSE_GETSTATUS:{
1184261260Shselasky			mousestatus_t *status = (mousestatus_t *)addr;
1185261260Shselasky
1186261260Shselasky			*status = sc->sc_status;
1187261260Shselasky			sc->sc_status.obutton = sc->sc_status.button;
1188261260Shselasky			sc->sc_status.button = 0;
1189261260Shselasky			sc->sc_status.dx = 0;
1190261260Shselasky			sc->sc_status.dy = 0;
1191261260Shselasky			sc->sc_status.dz = 0;
1192261260Shselasky
1193261260Shselasky			if (status->dx || status->dy || status->dz)
1194261260Shselasky				status->flags |= MOUSE_POSCHANGED;
1195261260Shselasky			if (status->button != status->obutton)
1196261260Shselasky				status->flags |= MOUSE_BUTTONSCHANGED;
1197261260Shselasky			break;
1198261260Shselasky		}
1199261260Shselasky	default:
1200261260Shselasky		error = ENOTTY;
1201261260Shselasky	}
1202261260Shselasky
1203261260Shselaskydone:
1204261260Shselasky	mtx_unlock(&sc->sc_mutex);
1205261260Shselasky	return (error);
1206261260Shselasky}
1207261260Shselasky
1208261260Shselaskystatic device_method_t wsp_methods[] = {
1209261260Shselasky	/* Device interface */
1210261260Shselasky	DEVMETHOD(device_probe, wsp_probe),
1211261260Shselasky	DEVMETHOD(device_attach, wsp_attach),
1212261260Shselasky	DEVMETHOD(device_detach, wsp_detach),
1213261260Shselasky	DEVMETHOD_END
1214261260Shselasky};
1215261260Shselasky
1216261260Shselaskystatic driver_t wsp_driver = {
1217261260Shselasky	.name = WSP_DRIVER_NAME,
1218261260Shselasky	.methods = wsp_methods,
1219261260Shselasky	.size = sizeof(struct wsp_softc)
1220261260Shselasky};
1221261260Shselasky
1222261260Shselaskystatic devclass_t wsp_devclass;
1223261260Shselasky
1224261260ShselaskyDRIVER_MODULE(wsp, uhub, wsp_driver, wsp_devclass, NULL, 0);
1225261260ShselaskyMODULE_DEPEND(wsp, usb, 1, 1, 1);
1226261260ShselaskyMODULE_VERSION(wsp, 1);
1227