xref: /freebsd/sys/dev/atkbdc/psm.c (revision 788ca347b816afd83b2885e0c79aeeb88649b2ab)
1 /*-
2  * Copyright (c) 1992, 1993 Erik Forsberg.
3  * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
13  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
15  * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  */
23 /*
24  *  Ported to 386bsd Oct 17, 1992
25  *  Sandi Donno, Computer Science, University of Cape Town, South Africa
26  *  Please send bug reports to sandi@cs.uct.ac.za
27  *
28  *  Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
29  *  although I was only partially successful in getting the alpha release
30  *  of his "driver for the Logitech and ATI Inport Bus mice for use with
31  *  386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
32  *  found his code to be an invaluable reference when porting this driver
33  *  to 386bsd.
34  *
35  *  Further modifications for latest 386BSD+patchkit and port to NetBSD,
36  *  Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
37  *
38  *  Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
39  *  Andrew Herbert - 12 June 1993
40  *
41  *  Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
42  *  - 13 June 1993
43  *
44  *  Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
45  *  - 24 October 1993
46  *
47  *  Hardware access routines and probe logic rewritten by
48  *  Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
49  *  - 3, 14, 22 October 1996.
50  *  - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
51  *  - 14, 30 November 1996. Uses `kbdio.c'.
52  *  - 13 December 1996. Uses queuing version of `kbdio.c'.
53  *  - January/February 1997. Tweaked probe logic for
54  *    HiNote UltraII/Latitude/Armada laptops.
55  *  - 30 July 1997. Added APM support.
56  *  - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
57  *    Improved sync check logic.
58  *    Vendor specific support routines.
59  */
60 
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
63 
64 #include "opt_isa.h"
65 #include "opt_psm.h"
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/module.h>
71 #include <sys/bus.h>
72 #include <sys/conf.h>
73 #include <sys/filio.h>
74 #include <sys/poll.h>
75 #include <sys/sigio.h>
76 #include <sys/signalvar.h>
77 #include <sys/syslog.h>
78 #include <machine/bus.h>
79 #include <sys/rman.h>
80 #include <sys/selinfo.h>
81 #include <sys/sysctl.h>
82 #include <sys/time.h>
83 #include <sys/uio.h>
84 
85 #include <sys/limits.h>
86 #include <sys/mouse.h>
87 #include <machine/resource.h>
88 
89 #ifdef DEV_ISA
90 #include <isa/isavar.h>
91 #endif
92 
93 #include <dev/atkbdc/atkbdcreg.h>
94 #include <dev/atkbdc/psm.h>
95 
96 /*
97  * Driver specific options: the following options may be set by
98  * `options' statements in the kernel configuration file.
99  */
100 
101 /* debugging */
102 #ifndef PSM_DEBUG
103 #define	PSM_DEBUG	0	/*
104 				 * logging: 0: none, 1: brief, 2: verbose
105 				 *          3: sync errors, 4: all packets
106 				 */
107 #endif
108 #define	VLOG(level, args)	do {	\
109 	if (verbose >= level)		\
110 		log args;		\
111 } while (0)
112 
113 #ifndef PSM_INPUT_TIMEOUT
114 #define	PSM_INPUT_TIMEOUT	2000000	/* 2 sec */
115 #endif
116 
117 #ifndef PSM_TAP_TIMEOUT
118 #define	PSM_TAP_TIMEOUT		125000
119 #endif
120 
121 #ifndef PSM_TAP_THRESHOLD
122 #define	PSM_TAP_THRESHOLD	25
123 #endif
124 
125 /* end of driver specific options */
126 
127 #define	PSMCPNP_DRIVER_NAME	"psmcpnp"
128 
129 /* input queue */
130 #define	PSM_BUFSIZE		960
131 #define	PSM_SMALLBUFSIZE	240
132 
133 /* operation levels */
134 #define	PSM_LEVEL_BASE		0
135 #define	PSM_LEVEL_STANDARD	1
136 #define	PSM_LEVEL_NATIVE	2
137 #define	PSM_LEVEL_MIN		PSM_LEVEL_BASE
138 #define	PSM_LEVEL_MAX		PSM_LEVEL_NATIVE
139 
140 /* Logitech PS2++ protocol */
141 #define	MOUSE_PS2PLUS_CHECKBITS(b)	\
142     ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
143 #define	MOUSE_PS2PLUS_PACKET_TYPE(b)	\
144     (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
145 
146 /* ring buffer */
147 typedef struct ringbuf {
148 	int		count;	/* # of valid elements in the buffer */
149 	int		head;	/* head pointer */
150 	int		tail;	/* tail poiner */
151 	u_char buf[PSM_BUFSIZE];
152 } ringbuf_t;
153 
154 /* data buffer */
155 typedef struct packetbuf {
156 	u_char	ipacket[16];	/* interim input buffer */
157 	int	inputbytes;	/* # of bytes in the input buffer */
158 } packetbuf_t;
159 
160 #ifndef PSM_PACKETQUEUE
161 #define	PSM_PACKETQUEUE	128
162 #endif
163 
164 enum {
165 	SYNAPTICS_SYSCTL_MIN_PRESSURE,
166 	SYNAPTICS_SYSCTL_MAX_PRESSURE,
167 	SYNAPTICS_SYSCTL_MAX_WIDTH,
168 	SYNAPTICS_SYSCTL_MARGIN_TOP,
169 	SYNAPTICS_SYSCTL_MARGIN_RIGHT,
170 	SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
171 	SYNAPTICS_SYSCTL_MARGIN_LEFT,
172 	SYNAPTICS_SYSCTL_NA_TOP,
173 	SYNAPTICS_SYSCTL_NA_RIGHT,
174 	SYNAPTICS_SYSCTL_NA_BOTTOM,
175 	SYNAPTICS_SYSCTL_NA_LEFT,
176 	SYNAPTICS_SYSCTL_WINDOW_MIN,
177 	SYNAPTICS_SYSCTL_WINDOW_MAX,
178 	SYNAPTICS_SYSCTL_MULTIPLICATOR,
179 	SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
180 	SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
181 	SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
182 	SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
183 	SYNAPTICS_SYSCTL_DIV_MIN,
184 	SYNAPTICS_SYSCTL_DIV_MAX,
185 	SYNAPTICS_SYSCTL_DIV_MAX_NA,
186 	SYNAPTICS_SYSCTL_DIV_LEN,
187 	SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
188 	SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
189 	SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
190 	SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
191 	SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
192 	SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
193 	SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
194 	SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
195 	SYNAPTICS_SYSCTL_TOUCHPAD_OFF
196 };
197 
198 typedef struct synapticsinfo {
199 	struct sysctl_ctx_list	 sysctl_ctx;
200 	struct sysctl_oid	*sysctl_tree;
201 	int			 directional_scrolls;
202 	int			 two_finger_scroll;
203 	int			 min_pressure;
204 	int			 max_pressure;
205 	int			 max_width;
206 	int			 margin_top;
207 	int			 margin_right;
208 	int			 margin_bottom;
209 	int			 margin_left;
210 	int			 na_top;
211 	int			 na_right;
212 	int			 na_bottom;
213 	int			 na_left;
214 	int			 window_min;
215 	int			 window_max;
216 	int			 multiplicator;
217 	int			 weight_current;
218 	int			 weight_previous;
219 	int			 weight_previous_na;
220 	int			 weight_len_squared;
221 	int			 div_min;
222 	int			 div_max;
223 	int			 div_max_na;
224 	int			 div_len;
225 	int			 tap_max_delta;
226 	int			 tap_min_queue;
227 	int			 taphold_timeout;
228 	int			 vscroll_ver_area;
229 	int			 vscroll_hor_area;
230 	int			 vscroll_min_delta;
231 	int			 vscroll_div_min;
232 	int			 vscroll_div_max;
233 	int			 touchpad_off;
234 } synapticsinfo_t;
235 
236 typedef struct synapticspacket {
237 	int			x;
238 	int			y;
239 } synapticspacket_t;
240 
241 #define	SYNAPTICS_PACKETQUEUE 10
242 #define SYNAPTICS_QUEUE_CURSOR(x)					\
243 	(x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
244 
245 #define	SYNAPTICS_VERSION_GE(synhw, major, minor)			\
246     ((synhw).infoMajor > (major) ||					\
247      ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
248 
249 typedef struct synapticsaction {
250 	synapticspacket_t	queue[SYNAPTICS_PACKETQUEUE];
251 	int			queue_len;
252 	int			queue_cursor;
253 	int			window_min;
254 	int			start_x;
255 	int			start_y;
256 	int			avg_dx;
257 	int			avg_dy;
258 	int			squelch_x;
259 	int			squelch_y;
260 	int			fingers_nb;
261 	int			tap_button;
262 	int			in_taphold;
263 	int			in_vscroll;
264 } synapticsaction_t;
265 
266 enum {
267 	TRACKPOINT_SYSCTL_SENSITIVITY,
268 	TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
269 	TRACKPOINT_SYSCTL_UPPER_PLATEAU,
270 	TRACKPOINT_SYSCTL_BACKUP_RANGE,
271 	TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
272 	TRACKPOINT_SYSCTL_MINIMUM_DRAG,
273 	TRACKPOINT_SYSCTL_UP_THRESHOLD,
274 	TRACKPOINT_SYSCTL_THRESHOLD,
275 	TRACKPOINT_SYSCTL_JENKS_CURVATURE,
276 	TRACKPOINT_SYSCTL_Z_TIME,
277 	TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
278 	TRACKPOINT_SYSCTL_SKIP_BACKUPS
279 };
280 
281 typedef struct trackpointinfo {
282 	struct sysctl_ctx_list sysctl_ctx;
283 	struct sysctl_oid *sysctl_tree;
284 	int	sensitivity;
285 	int	inertia;
286 	int	uplateau;
287 	int	reach;
288 	int	draghys;
289 	int	mindrag;
290 	int	upthresh;
291 	int	threshold;
292 	int	jenks;
293 	int	ztime;
294 	int	pts;
295 	int	skipback;
296 } trackpointinfo_t;
297 
298 /* driver control block */
299 struct psm_softc {		/* Driver status information */
300 	int		unit;
301 	struct selinfo	rsel;		/* Process selecting for Input */
302 	u_char		state;		/* Mouse driver state */
303 	int		config;		/* driver configuration flags */
304 	int		flags;		/* other flags */
305 	KBDC		kbdc;		/* handle to access kbd controller */
306 	struct resource	*intr;		/* IRQ resource */
307 	void		*ih;		/* interrupt handle */
308 	mousehw_t	hw;		/* hardware information */
309 	synapticshw_t	synhw;		/* Synaptics hardware information */
310 	synapticsinfo_t	syninfo;	/* Synaptics configuration */
311 	synapticsaction_t synaction;	/* Synaptics action context */
312 	int		tphw;		/* TrackPoint hardware information */
313 	trackpointinfo_t tpinfo;	/* TrackPoint configuration */
314 	mousemode_t	mode;		/* operation mode */
315 	mousemode_t	dflt_mode;	/* default operation mode */
316 	mousestatus_t	status;		/* accumulated mouse movement */
317 	ringbuf_t	queue;		/* mouse status queue */
318 	packetbuf_t	pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
319 	int		pqueue_start;	/* start of data in queue */
320 	int		pqueue_end;	/* end of data in queue */
321 	int		button;		/* the latest button state */
322 	int		xold;		/* previous absolute X position */
323 	int		yold;		/* previous absolute Y position */
324 	int		xaverage;	/* average X position */
325 	int		yaverage;	/* average Y position */
326 	int		squelch; /* level to filter movement at low speed */
327 	int		zmax;	/* maximum pressure value for touchpads */
328 	int		syncerrors; /* # of bytes discarded to synchronize */
329 	int		pkterrors;  /* # of packets failed during quaranteen. */
330 	struct timeval	inputtimeout;
331 	struct timeval	lastsoftintr;	/* time of last soft interrupt */
332 	struct timeval	lastinputerr;	/* time last sync error happened */
333 	struct timeval	taptimeout;	/* tap timeout for touchpads */
334 	int		watchdog;	/* watchdog timer flag */
335 	struct callout	callout;	/* watchdog timer call out */
336 	struct callout	softcallout; /* buffer timer call out */
337 	struct cdev	*dev;
338 	struct cdev	*bdev;
339 	int		lasterr;
340 	int		cmdcount;
341 	struct sigio	*async;		/* Processes waiting for SIGIO */
342 	int		extended_buttons;
343 };
344 static devclass_t psm_devclass;
345 
346 /* driver state flags (state) */
347 #define	PSM_VALID		0x80
348 #define	PSM_OPEN		1	/* Device is open */
349 #define	PSM_ASLP		2	/* Waiting for mouse data */
350 #define	PSM_SOFTARMED		4	/* Software interrupt armed */
351 #define	PSM_NEED_SYNCBITS	8	/* Set syncbits using next data pkt */
352 
353 /* driver configuration flags (config) */
354 #define	PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
355 #define	PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
356 #define	PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
357 #define	PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
358 #define	PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
359 #define	PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
360 #define	PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
361 #define	PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
362 #define	PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
363 
364 #define	PSM_CONFIG_FLAGS	\
365     (PSM_CONFIG_RESOLUTION |	\
366     PSM_CONFIG_ACCEL |		\
367     PSM_CONFIG_NOCHECKSYNC |	\
368     PSM_CONFIG_NOIDPROBE |	\
369     PSM_CONFIG_NORESET |	\
370     PSM_CONFIG_FORCETAP |	\
371     PSM_CONFIG_IGNPORTERROR |	\
372     PSM_CONFIG_HOOKRESUME |	\
373     PSM_CONFIG_INITAFTERSUSPEND)
374 
375 /* other flags (flags) */
376 #define	PSM_FLAGS_FINGERDOWN	0x0001	/* VersaPad finger down */
377 
378 #define kbdcp(p)			((atkbdc_softc_t *)(p))
379 #define ALWAYS_RESTORE_CONTROLLER(kbdc)	!(kbdcp(kbdc)->quirks \
380     & KBDC_QUIRK_KEEP_ACTIVATED)
381 
382 /* Tunables */
383 static int tap_enabled = -1;
384 TUNABLE_INT("hw.psm.tap_enabled", &tap_enabled);
385 
386 static int synaptics_support = 0;
387 TUNABLE_INT("hw.psm.synaptics_support", &synaptics_support);
388 
389 static int trackpoint_support = 0;
390 TUNABLE_INT("hw.psm.trackpoint_support", &trackpoint_support);
391 
392 static int verbose = PSM_DEBUG;
393 TUNABLE_INT("debug.psm.loglevel", &verbose);
394 
395 /* for backward compatibility */
396 #define	OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
397 #define	OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
398 #define	OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
399 
400 typedef struct old_mousehw {
401 	int	buttons;
402 	int	iftype;
403 	int	type;
404 	int	hwid;
405 } old_mousehw_t;
406 
407 typedef struct old_mousemode {
408 	int	protocol;
409 	int	rate;
410 	int	resolution;
411 	int	accelfactor;
412 } old_mousemode_t;
413 
414 /* packet formatting function */
415 typedef int	packetfunc_t(struct psm_softc *, u_char *, int *, int,
416     mousestatus_t *);
417 
418 /* function prototypes */
419 static void	psmidentify(driver_t *, device_t);
420 static int	psmprobe(device_t);
421 static int	psmattach(device_t);
422 static int	psmdetach(device_t);
423 static int	psmresume(device_t);
424 
425 static d_open_t		psmopen;
426 static d_close_t	psmclose;
427 static d_read_t		psmread;
428 static d_write_t	psmwrite;
429 static d_ioctl_t	psmioctl;
430 static d_poll_t		psmpoll;
431 
432 static int	enable_aux_dev(KBDC);
433 static int	disable_aux_dev(KBDC);
434 static int	get_mouse_status(KBDC, int *, int, int);
435 static int	get_aux_id(KBDC);
436 static int	set_mouse_sampling_rate(KBDC, int);
437 static int	set_mouse_scaling(KBDC, int);
438 static int	set_mouse_resolution(KBDC, int);
439 static int	set_mouse_mode(KBDC);
440 static int	get_mouse_buttons(KBDC);
441 static int	is_a_mouse(int);
442 static void	recover_from_error(KBDC);
443 static int	restore_controller(KBDC, int);
444 static int	doinitialize(struct psm_softc *, mousemode_t *);
445 static int	doopen(struct psm_softc *, int);
446 static int	reinitialize(struct psm_softc *, int);
447 static char	*model_name(int);
448 static void	psmsoftintr(void *);
449 static void	psmintr(void *);
450 static void	psmtimeout(void *);
451 static int	timeelapsed(const struct timeval *, int, int,
452 		    const struct timeval *);
453 static void	dropqueue(struct psm_softc *);
454 static void	flushpackets(struct psm_softc *);
455 static void	proc_mmanplus(struct psm_softc *, packetbuf_t *,
456 		    mousestatus_t *, int *, int *, int *);
457 static int	proc_synaptics(struct psm_softc *, packetbuf_t *,
458 		    mousestatus_t *, int *, int *, int *);
459 static void	proc_versapad(struct psm_softc *, packetbuf_t *,
460 		    mousestatus_t *, int *, int *, int *);
461 static int	tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
462 		    u_char *);
463 
464 /* vendor specific features */
465 typedef int	probefunc_t(KBDC, struct psm_softc *);
466 
467 static int	mouse_id_proc1(KBDC, int, int, int *);
468 static int	mouse_ext_command(KBDC, int);
469 
470 static probefunc_t	enable_groller;
471 static probefunc_t	enable_gmouse;
472 static probefunc_t	enable_aglide;
473 static probefunc_t	enable_kmouse;
474 static probefunc_t	enable_msexplorer;
475 static probefunc_t	enable_msintelli;
476 static probefunc_t	enable_4dmouse;
477 static probefunc_t	enable_4dplus;
478 static probefunc_t	enable_mmanplus;
479 static probefunc_t	enable_synaptics;
480 static probefunc_t	enable_trackpoint;
481 static probefunc_t	enable_versapad;
482 
483 static void set_trackpoint_parameters(struct psm_softc *sc);
484 static void synaptics_passthrough_on(struct psm_softc *sc);
485 static void synaptics_passthrough_off(struct psm_softc *sc);
486 
487 static struct {
488 	int		model;
489 	u_char		syncmask;
490 	int		packetsize;
491 	probefunc_t	*probefunc;
492 } vendortype[] = {
493 	/*
494 	 * WARNING: the order of probe is very important.  Don't mess it
495 	 * unless you know what you are doing.
496 	 */
497 	{ MOUSE_MODEL_NET,		/* Genius NetMouse */
498 	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
499 	{ MOUSE_MODEL_NETSCROLL,	/* Genius NetScroll */
500 	  0xc8, 6, enable_groller },
501 	{ MOUSE_MODEL_MOUSEMANPLUS,	/* Logitech MouseMan+ */
502 	  0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
503 	{ MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
504 	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
505 	{ MOUSE_MODEL_4D,		/* A4 Tech 4D Mouse */
506 	  0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
507 	{ MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
508 	  0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
509 	{ MOUSE_MODEL_SYNAPTICS,	/* Synaptics Touchpad */
510 	  0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
511 	{ MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
512 	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
513 	{ MOUSE_MODEL_GLIDEPOINT,	/* ALPS GlidePoint */
514 	  0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
515 	{ MOUSE_MODEL_THINK,		/* Kensington ThinkingMouse */
516 	  0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
517 	{ MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
518 	  0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
519 	{ MOUSE_MODEL_TRACKPOINT,	/* IBM/Lenovo TrackPoint */
520 	  0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
521 	{ MOUSE_MODEL_GENERIC,
522 	  0xc0, MOUSE_PS2_PACKETSIZE, NULL },
523 };
524 #define	GENERIC_MOUSE_ENTRY	\
525     ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
526 
527 /* device driver declarateion */
528 static device_method_t psm_methods[] = {
529 	/* Device interface */
530 	DEVMETHOD(device_identify,	psmidentify),
531 	DEVMETHOD(device_probe,		psmprobe),
532 	DEVMETHOD(device_attach,	psmattach),
533 	DEVMETHOD(device_detach,	psmdetach),
534 	DEVMETHOD(device_resume,	psmresume),
535 
536 	{ 0, 0 }
537 };
538 
539 static driver_t psm_driver = {
540 	PSM_DRIVER_NAME,
541 	psm_methods,
542 	sizeof(struct psm_softc),
543 };
544 
545 static struct cdevsw psm_cdevsw = {
546 	.d_version =	D_VERSION,
547 	.d_flags =	D_NEEDGIANT,
548 	.d_open =	psmopen,
549 	.d_close =	psmclose,
550 	.d_read =	psmread,
551 	.d_write =	psmwrite,
552 	.d_ioctl =	psmioctl,
553 	.d_poll =	psmpoll,
554 	.d_name =	PSM_DRIVER_NAME,
555 };
556 
557 /* device I/O routines */
558 static int
559 enable_aux_dev(KBDC kbdc)
560 {
561 	int res;
562 
563 	res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
564 	VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
565 
566 	return (res == PSM_ACK);
567 }
568 
569 static int
570 disable_aux_dev(KBDC kbdc)
571 {
572 	int res;
573 
574 	res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
575 	VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
576 
577 	return (res == PSM_ACK);
578 }
579 
580 static int
581 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
582 {
583 	int cmd;
584 	int res;
585 	int i;
586 
587 	switch (flag) {
588 	case 0:
589 	default:
590 		cmd = PSMC_SEND_DEV_STATUS;
591 		break;
592 	case 1:
593 		cmd = PSMC_SEND_DEV_DATA;
594 		break;
595 	}
596 	empty_aux_buffer(kbdc, 5);
597 	res = send_aux_command(kbdc, cmd);
598 	VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
599 	    (flag == 1) ? "DATA" : "STATUS", res));
600 	if (res != PSM_ACK)
601 		return (0);
602 
603 	for (i = 0; i < len; ++i) {
604 		status[i] = read_aux_data(kbdc);
605 		if (status[i] < 0)
606 			break;
607 	}
608 
609 	VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
610 	    (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
611 
612 	return (i);
613 }
614 
615 static int
616 get_aux_id(KBDC kbdc)
617 {
618 	int res;
619 	int id;
620 
621 	empty_aux_buffer(kbdc, 5);
622 	res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
623 	VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
624 	if (res != PSM_ACK)
625 		return (-1);
626 
627 	/* 10ms delay */
628 	DELAY(10000);
629 
630 	id = read_aux_data(kbdc);
631 	VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
632 
633 	return (id);
634 }
635 
636 static int
637 set_mouse_sampling_rate(KBDC kbdc, int rate)
638 {
639 	int res;
640 
641 	res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
642 	VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
643 
644 	return ((res == PSM_ACK) ? rate : -1);
645 }
646 
647 static int
648 set_mouse_scaling(KBDC kbdc, int scale)
649 {
650 	int res;
651 
652 	switch (scale) {
653 	case 1:
654 	default:
655 		scale = PSMC_SET_SCALING11;
656 		break;
657 	case 2:
658 		scale = PSMC_SET_SCALING21;
659 		break;
660 	}
661 	res = send_aux_command(kbdc, scale);
662 	VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
663 	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
664 
665 	return (res == PSM_ACK);
666 }
667 
668 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
669 static int
670 set_mouse_resolution(KBDC kbdc, int val)
671 {
672 	int res;
673 
674 	res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
675 	VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
676 
677 	return ((res == PSM_ACK) ? val : -1);
678 }
679 
680 /*
681  * NOTE: once `set_mouse_mode()' is called, the mouse device must be
682  * re-enabled by calling `enable_aux_dev()'
683  */
684 static int
685 set_mouse_mode(KBDC kbdc)
686 {
687 	int res;
688 
689 	res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
690 	VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
691 
692 	return (res == PSM_ACK);
693 }
694 
695 static int
696 get_mouse_buttons(KBDC kbdc)
697 {
698 	int c = 2;		/* assume two buttons by default */
699 	int status[3];
700 
701 	/*
702 	 * NOTE: a special sequence to obtain Logitech Mouse specific
703 	 * information: set resolution to 25 ppi, set scaling to 1:1, set
704 	 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
705 	 * mouse status bytes is the number of available buttons.
706 	 * Some manufactures also support this sequence.
707 	 */
708 	if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
709 		return (c);
710 	if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
711 	    set_mouse_scaling(kbdc, 1) &&
712 	    get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
713 		return (status[1]);
714 	return (c);
715 }
716 
717 /* misc subroutines */
718 /*
719  * Someday, I will get the complete list of valid pointing devices and
720  * their IDs... XXX
721  */
722 static int
723 is_a_mouse(int id)
724 {
725 #if 0
726 	static int valid_ids[] = {
727 		PSM_MOUSE_ID,		/* mouse */
728 		PSM_BALLPOINT_ID,	/* ballpoint device */
729 		PSM_INTELLI_ID,		/* Intellimouse */
730 		PSM_EXPLORER_ID,	/* Intellimouse Explorer */
731 		-1			/* end of table */
732 	};
733 	int i;
734 
735 	for (i = 0; valid_ids[i] >= 0; ++i)
736 	if (valid_ids[i] == id)
737 		return (TRUE);
738 	return (FALSE);
739 #else
740 	return (TRUE);
741 #endif
742 }
743 
744 static char *
745 model_name(int model)
746 {
747 	static struct {
748 		int	model_code;
749 		char	*model_name;
750 	} models[] = {
751 		{ MOUSE_MODEL_NETSCROLL,	"NetScroll" },
752 		{ MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
753 		{ MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
754 		{ MOUSE_MODEL_THINK,		"ThinkingMouse" },
755 		{ MOUSE_MODEL_INTELLI,		"IntelliMouse" },
756 		{ MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
757 		{ MOUSE_MODEL_VERSAPAD,		"VersaPad" },
758 		{ MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
759 		{ MOUSE_MODEL_4D,		"4D Mouse" },
760 		{ MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
761 		{ MOUSE_MODEL_SYNAPTICS,	"Synaptics Touchpad" },
762 		{ MOUSE_MODEL_TRACKPOINT,	"IBM/Lenovo TrackPoint" },
763 		{ MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
764 		{ MOUSE_MODEL_UNKNOWN,		"Unknown" },
765 	};
766 	int i;
767 
768 	for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
769 		if (models[i].model_code == model)
770 			break;
771 	return (models[i].model_name);
772 }
773 
774 static void
775 recover_from_error(KBDC kbdc)
776 {
777 	/* discard anything left in the output buffer */
778 	empty_both_buffers(kbdc, 10);
779 
780 #if 0
781 	/*
782 	 * NOTE: KBDC_RESET_KBD may not restore the communication between the
783 	 * keyboard and the controller.
784 	 */
785 	reset_kbd(kbdc);
786 #else
787 	/*
788 	 * NOTE: somehow diagnostic and keyboard port test commands bring the
789 	 * keyboard back.
790 	 */
791 	if (!test_controller(kbdc))
792 		log(LOG_ERR, "psm: keyboard controller failed.\n");
793 	/* if there isn't a keyboard in the system, the following error is OK */
794 	if (test_kbd_port(kbdc) != 0)
795 		VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
796 #endif
797 }
798 
799 static int
800 restore_controller(KBDC kbdc, int command_byte)
801 {
802 	empty_both_buffers(kbdc, 10);
803 
804 	if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
805 		log(LOG_ERR, "psm: failed to restore the keyboard controller "
806 		    "command byte.\n");
807 		empty_both_buffers(kbdc, 10);
808 		return (FALSE);
809 	} else {
810 		empty_both_buffers(kbdc, 10);
811 		return (TRUE);
812 	}
813 }
814 
815 /*
816  * Re-initialize the aux port and device. The aux port must be enabled
817  * and its interrupt must be disabled before calling this routine.
818  * The aux device will be disabled before returning.
819  * The keyboard controller must be locked via `kbdc_lock()' before
820  * calling this routine.
821  */
822 static int
823 doinitialize(struct psm_softc *sc, mousemode_t *mode)
824 {
825 	KBDC kbdc = sc->kbdc;
826 	int stat[3];
827 	int i;
828 
829 	switch((i = test_aux_port(kbdc))) {
830 	case 1:	/* ignore these errors */
831 	case 2:
832 	case 3:
833 	case PSM_ACK:
834 		if (verbose)
835 			log(LOG_DEBUG,
836 			    "psm%d: strange result for test aux port (%d).\n",
837 			    sc->unit, i);
838 		/* FALLTHROUGH */
839 	case 0:		/* no error */
840 		break;
841 	case -1:	/* time out */
842 	default:	/* error */
843 		recover_from_error(kbdc);
844 		if (sc->config & PSM_CONFIG_IGNPORTERROR)
845 			break;
846 		log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
847 		    sc->unit, i);
848 		return (FALSE);
849 	}
850 
851 	if (sc->config & PSM_CONFIG_NORESET) {
852 		/*
853 		 * Don't try to reset the pointing device.  It may possibly
854 		 * be left in the unknown state, though...
855 		 */
856 	} else {
857 		/*
858 		 * NOTE: some controllers appears to hang the `keyboard' when
859 		 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
860 		 */
861 		if (!reset_aux_dev(kbdc)) {
862 			recover_from_error(kbdc);
863 			log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
864 			    sc->unit);
865 			return (FALSE);
866 		}
867 	}
868 
869 	/*
870 	 * both the aux port and the aux device is functioning, see
871 	 * if the device can be enabled.
872 	 */
873 	if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
874 		log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
875 		    sc->unit);
876 		return (FALSE);
877 	}
878 	empty_both_buffers(kbdc, 10);	/* remove stray data if any */
879 
880 	/* Re-enable the mouse. */
881 	for (i = 0; vendortype[i].probefunc != NULL; ++i)
882 		if (vendortype[i].model == sc->hw.model)
883 			(*vendortype[i].probefunc)(sc->kbdc, NULL);
884 
885 	/* set mouse parameters */
886 	if (mode != (mousemode_t *)NULL) {
887 		if (mode->rate > 0)
888 			mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
889 		if (mode->resolution >= 0)
890 			mode->resolution =
891 			    set_mouse_resolution(kbdc, mode->resolution);
892 		set_mouse_scaling(kbdc, 1);
893 		set_mouse_mode(kbdc);
894 
895 		/*
896 		 * Trackpoint settings are lost on resume.
897 		 * Restore them here.
898 		 */
899 		if (sc->tphw > 0)
900 			set_trackpoint_parameters(sc);
901 	}
902 
903 	/* Record sync on the next data packet we see. */
904 	sc->flags |= PSM_NEED_SYNCBITS;
905 
906 	/* just check the status of the mouse */
907 	if (get_mouse_status(kbdc, stat, 0, 3) < 3)
908 		log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
909 		    sc->unit);
910 
911 	return (TRUE);
912 }
913 
914 static int
915 doopen(struct psm_softc *sc, int command_byte)
916 {
917 	int stat[3];
918 
919 	/*
920 	 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
921 	 * no obvious reason. Thus we check the current mode and restore the
922 	 * Absolute Mode if it was cleared.
923 	 *
924 	 * The previous hack at the end of psmprobe() wasn't efficient when
925 	 * moused(8) was restarted.
926 	 *
927 	 * A Reset (FF) or Set Defaults (F6) command would clear the
928 	 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
929 	 * doesn't show any evidence of such a command.
930 	 */
931 	if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
932 		mouse_ext_command(sc->kbdc, 1);
933 		get_mouse_status(sc->kbdc, stat, 0, 3);
934 		if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
935 		     stat[1] == 0x47) &&
936 		    stat[2] == 0x40) {
937 			/* Set the mode byte -- request wmode where
938 			 * available */
939 			if (sc->synhw.capExtended)
940 				mouse_ext_command(sc->kbdc, 0xc1);
941 			else
942 				mouse_ext_command(sc->kbdc, 0xc0);
943 			set_mouse_sampling_rate(sc->kbdc, 20);
944 			VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
945 			    "hopefully restored\n",
946 			    sc->unit));
947 		}
948 	}
949 
950 	/*
951 	 * A user may want to disable tap and drag gestures on a Synaptics
952 	 * TouchPad when it operates in Relative Mode.
953 	 */
954 	if (sc->hw.model == MOUSE_MODEL_GENERIC) {
955 		if (tap_enabled > 0) {
956 			/*
957 			 * Enable tap & drag gestures. We use a Mode Byte
958 			 * and clear the DisGest bit (see §2.5 of Synaptics
959 			 * TouchPad Interfacing Guide).
960 			 */
961 			VLOG(2, (LOG_DEBUG,
962 			    "psm%d: enable tap and drag gestures\n",
963 			    sc->unit));
964 			mouse_ext_command(sc->kbdc, 0x00);
965 			set_mouse_sampling_rate(sc->kbdc, 20);
966 		} else if (tap_enabled == 0) {
967 			/*
968 			 * Disable tap & drag gestures. We use a Mode Byte
969 			 * and set the DisGest bit (see §2.5 of Synaptics
970 			 * TouchPad Interfacing Guide).
971 			 */
972 			VLOG(2, (LOG_DEBUG,
973 			    "psm%d: disable tap and drag gestures\n",
974 			    sc->unit));
975 			mouse_ext_command(sc->kbdc, 0x04);
976 			set_mouse_sampling_rate(sc->kbdc, 20);
977 		}
978 	}
979 
980 	/* enable the mouse device */
981 	if (!enable_aux_dev(sc->kbdc)) {
982 		/* MOUSE ERROR: failed to enable the mouse because:
983 		 * 1) the mouse is faulty,
984 		 * 2) the mouse has been removed(!?)
985 		 * In the latter case, the keyboard may have hung, and need
986 		 * recovery procedure...
987 		 */
988 		recover_from_error(sc->kbdc);
989 #if 0
990 		/* FIXME: we could reset the mouse here and try to enable
991 		 * it again. But it will take long time and it's not a good
992 		 * idea to disable the keyboard that long...
993 		 */
994 		if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
995 			recover_from_error(sc->kbdc);
996 #else
997 		{
998 #endif
999 			restore_controller(sc->kbdc, command_byte);
1000 			/* mark this device is no longer available */
1001 			sc->state &= ~PSM_VALID;
1002 			log(LOG_ERR,
1003 			    "psm%d: failed to enable the device (doopen).\n",
1004 			sc->unit);
1005 			return (EIO);
1006 		}
1007 	}
1008 
1009 	if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1010 		log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
1011 		    sc->unit);
1012 
1013 	/* enable the aux port and interrupt */
1014 	if (!set_controller_command_byte(sc->kbdc,
1015 	    kbdc_get_device_mask(sc->kbdc),
1016 	    (command_byte & KBD_KBD_CONTROL_BITS) |
1017 	    KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
1018 		/* CONTROLLER ERROR */
1019 		disable_aux_dev(sc->kbdc);
1020 		restore_controller(sc->kbdc, command_byte);
1021 		log(LOG_ERR,
1022 		    "psm%d: failed to enable the aux interrupt (doopen).\n",
1023 		    sc->unit);
1024 		return (EIO);
1025 	}
1026 
1027 	/* start the watchdog timer */
1028 	sc->watchdog = FALSE;
1029 	callout_reset(&sc->callout, hz * 2, psmtimeout, sc);
1030 
1031 	return (0);
1032 }
1033 
1034 static int
1035 reinitialize(struct psm_softc *sc, int doinit)
1036 {
1037 	int err;
1038 	int c;
1039 	int s;
1040 
1041 	/* don't let anybody mess with the aux device */
1042 	if (!kbdc_lock(sc->kbdc, TRUE))
1043 		return (EIO);
1044 	s = spltty();
1045 
1046 	/* block our watchdog timer */
1047 	sc->watchdog = FALSE;
1048 	callout_stop(&sc->callout);
1049 
1050 	/* save the current controller command byte */
1051 	empty_both_buffers(sc->kbdc, 10);
1052 	c = get_controller_command_byte(sc->kbdc);
1053 	VLOG(2, (LOG_DEBUG,
1054 	    "psm%d: current command byte: %04x (reinitialize).\n",
1055 	    sc->unit, c));
1056 
1057 	/* enable the aux port but disable the aux interrupt and the keyboard */
1058 	if ((c == -1) || !set_controller_command_byte(sc->kbdc,
1059 	    kbdc_get_device_mask(sc->kbdc),
1060 	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1061 	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1062 		/* CONTROLLER ERROR */
1063 		splx(s);
1064 		kbdc_lock(sc->kbdc, FALSE);
1065 		log(LOG_ERR,
1066 		    "psm%d: unable to set the command byte (reinitialize).\n",
1067 		    sc->unit);
1068 		return (EIO);
1069 	}
1070 
1071 	/* flush any data */
1072 	if (sc->state & PSM_VALID) {
1073 		/* this may fail; but never mind... */
1074 		disable_aux_dev(sc->kbdc);
1075 		empty_aux_buffer(sc->kbdc, 10);
1076 	}
1077 	flushpackets(sc);
1078 	sc->syncerrors = 0;
1079 	sc->pkterrors = 0;
1080 	memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1081 
1082 	/* try to detect the aux device; are you still there? */
1083 	err = 0;
1084 	if (doinit) {
1085 		if (doinitialize(sc, &sc->mode)) {
1086 			/* yes */
1087 			sc->state |= PSM_VALID;
1088 		} else {
1089 			/* the device has gone! */
1090 			restore_controller(sc->kbdc, c);
1091 			sc->state &= ~PSM_VALID;
1092 			log(LOG_ERR,
1093 			    "psm%d: the aux device has gone! (reinitialize).\n",
1094 			    sc->unit);
1095 			err = ENXIO;
1096 		}
1097 	}
1098 	splx(s);
1099 
1100 	/* restore the driver state */
1101 	if ((sc->state & PSM_OPEN) && (err == 0)) {
1102 		/* enable the aux device and the port again */
1103 		err = doopen(sc, c);
1104 		if (err != 0)
1105 			log(LOG_ERR, "psm%d: failed to enable the device "
1106 			    "(reinitialize).\n", sc->unit);
1107 	} else {
1108 		/* restore the keyboard port and disable the aux port */
1109 		if (!set_controller_command_byte(sc->kbdc,
1110 		    kbdc_get_device_mask(sc->kbdc),
1111 		    (c & KBD_KBD_CONTROL_BITS) |
1112 		    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1113 			/* CONTROLLER ERROR */
1114 			log(LOG_ERR, "psm%d: failed to disable the aux port "
1115 			    "(reinitialize).\n", sc->unit);
1116 			err = EIO;
1117 		}
1118 	}
1119 
1120 	kbdc_lock(sc->kbdc, FALSE);
1121 	return (err);
1122 }
1123 
1124 /* psm driver entry points */
1125 
1126 static void
1127 psmidentify(driver_t *driver, device_t parent)
1128 {
1129 	device_t psmc;
1130 	device_t psm;
1131 	u_long irq;
1132 	int unit;
1133 
1134 	unit = device_get_unit(parent);
1135 
1136 	/* always add at least one child */
1137 	psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1138 	if (psm == NULL)
1139 		return;
1140 
1141 	irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1142 	if (irq > 0)
1143 		return;
1144 
1145 	/*
1146 	 * If the PS/2 mouse device has already been reported by ACPI or
1147 	 * PnP BIOS, obtain the IRQ resource from it.
1148 	 * (See psmcpnp_attach() below.)
1149 	 */
1150 	psmc = device_find_child(device_get_parent(parent),
1151 	    PSMCPNP_DRIVER_NAME, unit);
1152 	if (psmc == NULL)
1153 		return;
1154 	irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1155 	if (irq <= 0)
1156 		return;
1157 	bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1158 	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
1159 }
1160 
1161 #define	endprobe(v)	do {			\
1162 	if (bootverbose)			\
1163 		--verbose;			\
1164 	kbdc_set_device_mask(sc->kbdc, mask);	\
1165 	kbdc_lock(sc->kbdc, FALSE);		\
1166 	return (v);				\
1167 } while (0)
1168 
1169 static int
1170 psmprobe(device_t dev)
1171 {
1172 	int unit = device_get_unit(dev);
1173 	struct psm_softc *sc = device_get_softc(dev);
1174 	int stat[3];
1175 	int command_byte;
1176 	int mask;
1177 	int rid;
1178 	int i;
1179 
1180 #if 0
1181 	kbdc_debug(TRUE);
1182 #endif
1183 
1184 	/* see if IRQ is available */
1185 	rid = KBDC_RID_AUX;
1186 	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1187 	if (sc->intr == NULL) {
1188 		if (bootverbose)
1189 			device_printf(dev, "unable to allocate IRQ\n");
1190 		return (ENXIO);
1191 	}
1192 	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1193 
1194 	sc->unit = unit;
1195 	sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1196 	sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1197 	/* XXX: for backward compatibility */
1198 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1199 	sc->config |=
1200 #ifdef PSM_RESETAFTERSUSPEND
1201 	PSM_CONFIG_INITAFTERSUSPEND;
1202 #else
1203 	PSM_CONFIG_HOOKRESUME;
1204 #endif
1205 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1206 	sc->flags = 0;
1207 	if (bootverbose)
1208 		++verbose;
1209 
1210 	device_set_desc(dev, "PS/2 Mouse");
1211 
1212 	if (!kbdc_lock(sc->kbdc, TRUE)) {
1213 		printf("psm%d: unable to lock the controller.\n", unit);
1214 		if (bootverbose)
1215 			--verbose;
1216 		return (ENXIO);
1217 	}
1218 
1219 	/*
1220 	 * NOTE: two bits in the command byte controls the operation of the
1221 	 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1222 	 * port interrupt (IRQ 12) enable bit (bit 2).
1223 	 */
1224 
1225 	/* discard anything left after the keyboard initialization */
1226 	empty_both_buffers(sc->kbdc, 10);
1227 
1228 	/* save the current command byte; it will be used later */
1229 	mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1230 	command_byte = get_controller_command_byte(sc->kbdc);
1231 	if (verbose)
1232 		printf("psm%d: current command byte:%04x\n", unit,
1233 		    command_byte);
1234 	if (command_byte == -1) {
1235 		/* CONTROLLER ERROR */
1236 		printf("psm%d: unable to get the current command byte value.\n",
1237 			unit);
1238 		endprobe(ENXIO);
1239 	}
1240 
1241 	/*
1242 	 * disable the keyboard port while probing the aux port, which must be
1243 	 * enabled during this routine
1244 	 */
1245 	if (!set_controller_command_byte(sc->kbdc,
1246 	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1247 	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1248 	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1249 		/*
1250 		 * this is CONTROLLER ERROR; I don't know how to recover
1251 		 * from this error...
1252 		 */
1253 		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1254 			restore_controller(sc->kbdc, command_byte);
1255 		printf("psm%d: unable to set the command byte.\n", unit);
1256 		endprobe(ENXIO);
1257 	}
1258 	write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1259 
1260 	/*
1261 	 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1262 	 * port exists and is functioning. However, some controllers appears
1263 	 * to respond with zero even when the aux port doesn't exist. (It may
1264 	 * be that this is only the case when the controller DOES have the aux
1265 	 * port but the port is not wired on the motherboard.) The keyboard
1266 	 * controllers without the port, such as the original AT, are
1267 	 * supposed to return with an error code or simply time out. In any
1268 	 * case, we have to continue probing the port even when the controller
1269 	 * passes this test.
1270 	 *
1271 	 * XXX: some controllers erroneously return the error code 1, 2 or 3
1272 	 * when it has a perfectly functional aux port. We have to ignore
1273 	 * this error code. Even if the controller HAS error with the aux
1274 	 * port, it will be detected later...
1275 	 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1276 	 */
1277 	switch ((i = test_aux_port(sc->kbdc))) {
1278 	case 1:		/* ignore these errors */
1279 	case 2:
1280 	case 3:
1281 	case PSM_ACK:
1282 		if (verbose)
1283 			printf("psm%d: strange result for test aux port "
1284 			    "(%d).\n", unit, i);
1285 		/* FALLTHROUGH */
1286 	case 0:		/* no error */
1287 		break;
1288 	case -1:	/* time out */
1289 	default:	/* error */
1290 		recover_from_error(sc->kbdc);
1291 		if (sc->config & PSM_CONFIG_IGNPORTERROR)
1292 			break;
1293 		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1294 			restore_controller(sc->kbdc, command_byte);
1295 		if (verbose)
1296 			printf("psm%d: the aux port is not functioning (%d).\n",
1297 			    unit, i);
1298 		endprobe(ENXIO);
1299 	}
1300 
1301 	if (sc->config & PSM_CONFIG_NORESET) {
1302 		/*
1303 		 * Don't try to reset the pointing device.  It may possibly be
1304 		 * left in an unknown state, though...
1305 		 */
1306 	} else {
1307 		/*
1308 		 * NOTE: some controllers appears to hang the `keyboard' when
1309 		 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1310 		 *
1311 		 * Attempt to reset the controller twice -- this helps
1312 		 * pierce through some KVM switches. The second reset
1313 		 * is non-fatal.
1314 		 */
1315 		if (!reset_aux_dev(sc->kbdc)) {
1316 			recover_from_error(sc->kbdc);
1317 			if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1318 				restore_controller(sc->kbdc, command_byte);
1319 			if (verbose)
1320 				printf("psm%d: failed to reset the aux "
1321 				    "device.\n", unit);
1322 			endprobe(ENXIO);
1323 		} else if (!reset_aux_dev(sc->kbdc)) {
1324 			recover_from_error(sc->kbdc);
1325 			if (verbose >= 2)
1326 				printf("psm%d: failed to reset the aux device "
1327 				    "(2).\n", unit);
1328 		}
1329 	}
1330 
1331 	/*
1332 	 * both the aux port and the aux device are functioning, see if the
1333 	 * device can be enabled. NOTE: when enabled, the device will start
1334 	 * sending data; we shall immediately disable the device once we know
1335 	 * the device can be enabled.
1336 	 */
1337 	if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1338 		/* MOUSE ERROR */
1339 		recover_from_error(sc->kbdc);
1340 		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1341 			restore_controller(sc->kbdc, command_byte);
1342 		if (verbose)
1343 			printf("psm%d: failed to enable the aux device.\n",
1344 			    unit);
1345 		endprobe(ENXIO);
1346 	}
1347 
1348 	/* save the default values after reset */
1349 	if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1350 		sc->dflt_mode.rate = sc->mode.rate = stat[2];
1351 		sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1352 	} else {
1353 		sc->dflt_mode.rate = sc->mode.rate = -1;
1354 		sc->dflt_mode.resolution = sc->mode.resolution = -1;
1355 	}
1356 
1357 	/* hardware information */
1358 	sc->hw.iftype = MOUSE_IF_PS2;
1359 
1360 	/* verify the device is a mouse */
1361 	sc->hw.hwid = get_aux_id(sc->kbdc);
1362 	if (!is_a_mouse(sc->hw.hwid)) {
1363 		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1364 			restore_controller(sc->kbdc, command_byte);
1365 		if (verbose)
1366 			printf("psm%d: unknown device type (%d).\n", unit,
1367 			    sc->hw.hwid);
1368 		endprobe(ENXIO);
1369 	}
1370 	switch (sc->hw.hwid) {
1371 	case PSM_BALLPOINT_ID:
1372 		sc->hw.type = MOUSE_TRACKBALL;
1373 		break;
1374 	case PSM_MOUSE_ID:
1375 	case PSM_INTELLI_ID:
1376 	case PSM_EXPLORER_ID:
1377 	case PSM_4DMOUSE_ID:
1378 	case PSM_4DPLUS_ID:
1379 		sc->hw.type = MOUSE_MOUSE;
1380 		break;
1381 	default:
1382 		sc->hw.type = MOUSE_UNKNOWN;
1383 		break;
1384 	}
1385 
1386 	if (sc->config & PSM_CONFIG_NOIDPROBE) {
1387 		sc->hw.buttons = 2;
1388 		i = GENERIC_MOUSE_ENTRY;
1389 	} else {
1390 		/* # of buttons */
1391 		sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1392 
1393 		/* other parameters */
1394 		for (i = 0; vendortype[i].probefunc != NULL; ++i)
1395 			if ((*vendortype[i].probefunc)(sc->kbdc, sc)) {
1396 				if (verbose >= 2)
1397 					printf("psm%d: found %s\n", unit,
1398 					    model_name(vendortype[i].model));
1399 				break;
1400 			}
1401 	}
1402 
1403 	sc->hw.model = vendortype[i].model;
1404 
1405 	sc->dflt_mode.level = PSM_LEVEL_BASE;
1406 	sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1407 	sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1408 	if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1409 		sc->dflt_mode.syncmask[0] = 0;
1410 	else
1411 		sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1412 	if (sc->config & PSM_CONFIG_FORCETAP)
1413 		sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1414 	sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
1415 	sc->mode = sc->dflt_mode;
1416 	sc->mode.packetsize = vendortype[i].packetsize;
1417 
1418 	/* set mouse parameters */
1419 #if 0
1420 	/*
1421 	 * A version of Logitech FirstMouse+ won't report wheel movement,
1422 	 * if SET_DEFAULTS is sent...  Don't use this command.
1423 	 * This fix was found by Takashi Nishida.
1424 	 */
1425 	i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1426 	if (verbose >= 2)
1427 		printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1428 #endif
1429 	if (sc->config & PSM_CONFIG_RESOLUTION)
1430 		sc->mode.resolution =
1431 		    set_mouse_resolution(sc->kbdc,
1432 		    (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1433 	else if (sc->mode.resolution >= 0)
1434 		sc->mode.resolution =
1435 		    set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1436 	if (sc->mode.rate > 0)
1437 		sc->mode.rate =
1438 		    set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1439 	set_mouse_scaling(sc->kbdc, 1);
1440 
1441 	/* Record sync on the next data packet we see. */
1442 	sc->flags |= PSM_NEED_SYNCBITS;
1443 
1444 	/* just check the status of the mouse */
1445 	/*
1446 	 * NOTE: XXX there are some arcane controller/mouse combinations out
1447 	 * there, which hung the controller unless there is data transmission
1448 	 * after ACK from the mouse.
1449 	 */
1450 	if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1451 		printf("psm%d: failed to get status.\n", unit);
1452 	else {
1453 		/*
1454 		 * When in its native mode, some mice operate with different
1455 		 * default parameters than in the PS/2 compatible mode.
1456 		 */
1457 		sc->dflt_mode.rate = sc->mode.rate = stat[2];
1458 		sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1459 	}
1460 
1461 	/* disable the aux port for now... */
1462 	if (!set_controller_command_byte(sc->kbdc,
1463 	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1464 	    (command_byte & KBD_KBD_CONTROL_BITS) |
1465 	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1466 		/*
1467 		 * this is CONTROLLER ERROR; I don't know the proper way to
1468 		 * recover from this error...
1469 		 */
1470 		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1471 			restore_controller(sc->kbdc, command_byte);
1472 		printf("psm%d: unable to set the command byte.\n", unit);
1473 		endprobe(ENXIO);
1474 	}
1475 
1476 	/* done */
1477 	kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1478 	kbdc_lock(sc->kbdc, FALSE);
1479 	return (0);
1480 }
1481 
1482 static int
1483 psmattach(device_t dev)
1484 {
1485 	int unit = device_get_unit(dev);
1486 	struct psm_softc *sc = device_get_softc(dev);
1487 	int error;
1488 	int rid;
1489 
1490 	/* Setup initial state */
1491 	sc->state = PSM_VALID;
1492 	callout_init(&sc->callout, 0);
1493 	callout_init(&sc->softcallout, 0);
1494 
1495 	/* Setup our interrupt handler */
1496 	rid = KBDC_RID_AUX;
1497 	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1498 	if (sc->intr == NULL)
1499 		return (ENXIO);
1500 	error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1501 	    &sc->ih);
1502 	if (error) {
1503 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1504 		return (error);
1505 	}
1506 
1507 	/* Done */
1508 	sc->dev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "psm%d", unit);
1509 	sc->dev->si_drv1 = sc;
1510 	sc->bdev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "bpsm%d", unit);
1511 	sc->bdev->si_drv1 = sc;
1512 
1513 	/* Some touchpad devices need full reinitialization after suspend. */
1514 	switch (sc->hw.model) {
1515 	case MOUSE_MODEL_SYNAPTICS:
1516 	case MOUSE_MODEL_GLIDEPOINT:
1517 	case MOUSE_MODEL_VERSAPAD:
1518 		sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1519 		break;
1520 	default:
1521 		if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
1522 			sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1523 		break;
1524 	}
1525 
1526 	if (!verbose)
1527 		printf("psm%d: model %s, device ID %d\n",
1528 		    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1529 	else {
1530 		printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1531 		    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1532 		    sc->hw.hwid >> 8, sc->hw.buttons);
1533 		printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1534 		    unit, sc->config, sc->flags, sc->mode.packetsize);
1535 		printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1536 		    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1537 	}
1538 
1539 	if (bootverbose)
1540 		--verbose;
1541 
1542 	return (0);
1543 }
1544 
1545 static int
1546 psmdetach(device_t dev)
1547 {
1548 	struct psm_softc *sc;
1549 	int rid;
1550 
1551 	sc = device_get_softc(dev);
1552 	if (sc->state & PSM_OPEN)
1553 		return (EBUSY);
1554 
1555 	rid = KBDC_RID_AUX;
1556 	bus_teardown_intr(dev, sc->intr, sc->ih);
1557 	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1558 
1559 	destroy_dev(sc->dev);
1560 	destroy_dev(sc->bdev);
1561 
1562 	callout_drain(&sc->callout);
1563 	callout_drain(&sc->softcallout);
1564 
1565 	return (0);
1566 }
1567 
1568 static int
1569 psmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
1570 {
1571 	struct psm_softc *sc;
1572 	int command_byte;
1573 	int err;
1574 	int s;
1575 
1576 	/* Get device data */
1577 	sc = dev->si_drv1;
1578 	if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
1579 		/* the device is no longer valid/functioning */
1580 		return (ENXIO);
1581 	}
1582 
1583 	/* Disallow multiple opens */
1584 	if (sc->state & PSM_OPEN)
1585 		return (EBUSY);
1586 
1587 	device_busy(devclass_get_device(psm_devclass, sc->unit));
1588 
1589 	/* Initialize state */
1590 	sc->mode.level = sc->dflt_mode.level;
1591 	sc->mode.protocol = sc->dflt_mode.protocol;
1592 	sc->watchdog = FALSE;
1593 	sc->async = NULL;
1594 
1595 	/* flush the event queue */
1596 	sc->queue.count = 0;
1597 	sc->queue.head = 0;
1598 	sc->queue.tail = 0;
1599 	sc->status.flags = 0;
1600 	sc->status.button = 0;
1601 	sc->status.obutton = 0;
1602 	sc->status.dx = 0;
1603 	sc->status.dy = 0;
1604 	sc->status.dz = 0;
1605 	sc->button = 0;
1606 	sc->pqueue_start = 0;
1607 	sc->pqueue_end = 0;
1608 
1609 	/* empty input buffer */
1610 	flushpackets(sc);
1611 	sc->syncerrors = 0;
1612 	sc->pkterrors = 0;
1613 
1614 	/* don't let timeout routines in the keyboard driver to poll the kbdc */
1615 	if (!kbdc_lock(sc->kbdc, TRUE))
1616 		return (EIO);
1617 
1618 	/* save the current controller command byte */
1619 	s = spltty();
1620 	command_byte = get_controller_command_byte(sc->kbdc);
1621 
1622 	/* enable the aux port and temporalily disable the keyboard */
1623 	if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
1624 	    kbdc_get_device_mask(sc->kbdc),
1625 	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1626 	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1627 		/* CONTROLLER ERROR; do you know how to get out of this? */
1628 		kbdc_lock(sc->kbdc, FALSE);
1629 		splx(s);
1630 		log(LOG_ERR,
1631 		    "psm%d: unable to set the command byte (psmopen).\n",
1632 		    sc->unit);
1633 		return (EIO);
1634 	}
1635 	/*
1636 	 * Now that the keyboard controller is told not to generate
1637 	 * the keyboard and mouse interrupts, call `splx()' to allow
1638 	 * the other tty interrupts. The clock interrupt may also occur,
1639 	 * but timeout routines will be blocked by the poll flag set
1640 	 * via `kbdc_lock()'
1641 	 */
1642 	splx(s);
1643 
1644 	/* enable the mouse device */
1645 	err = doopen(sc, command_byte);
1646 
1647 	/* done */
1648 	if (err == 0)
1649 		sc->state |= PSM_OPEN;
1650 	kbdc_lock(sc->kbdc, FALSE);
1651 	return (err);
1652 }
1653 
1654 static int
1655 psmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1656 {
1657 	struct psm_softc *sc = dev->si_drv1;
1658 	int stat[3];
1659 	int command_byte;
1660 	int s;
1661 
1662 	/* don't let timeout routines in the keyboard driver to poll the kbdc */
1663 	if (!kbdc_lock(sc->kbdc, TRUE))
1664 		return (EIO);
1665 
1666 	/* save the current controller command byte */
1667 	s = spltty();
1668 	command_byte = get_controller_command_byte(sc->kbdc);
1669 	if (command_byte == -1) {
1670 		kbdc_lock(sc->kbdc, FALSE);
1671 		splx(s);
1672 		return (EIO);
1673 	}
1674 
1675 	/* disable the aux interrupt and temporalily disable the keyboard */
1676 	if (!set_controller_command_byte(sc->kbdc,
1677 	    kbdc_get_device_mask(sc->kbdc),
1678 	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1679 	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1680 		log(LOG_ERR,
1681 		    "psm%d: failed to disable the aux int (psmclose).\n",
1682 		    sc->unit);
1683 		/* CONTROLLER ERROR;
1684 		 * NOTE: we shall force our way through. Because the only
1685 		 * ill effect we shall see is that we may not be able
1686 		 * to read ACK from the mouse, and it doesn't matter much
1687 		 * so long as the mouse will accept the DISABLE command.
1688 		 */
1689 	}
1690 	splx(s);
1691 
1692 	/* stop the watchdog timer */
1693 	callout_stop(&sc->callout);
1694 
1695 	/* remove anything left in the output buffer */
1696 	empty_aux_buffer(sc->kbdc, 10);
1697 
1698 	/* disable the aux device, port and interrupt */
1699 	if (sc->state & PSM_VALID) {
1700 		if (!disable_aux_dev(sc->kbdc)) {
1701 			/* MOUSE ERROR;
1702 			 * NOTE: we don't return (error) and continue,
1703 			 * pretending we have successfully disabled the device.
1704 			 * It's OK because the interrupt routine will discard
1705 			 * any data from the mouse hereafter.
1706 			 */
1707 			log(LOG_ERR,
1708 			    "psm%d: failed to disable the device (psmclose).\n",
1709 			    sc->unit);
1710 		}
1711 
1712 		if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1713 			log(LOG_DEBUG,
1714 			    "psm%d: failed to get status (psmclose).\n",
1715 			    sc->unit);
1716 	}
1717 
1718 	if (!set_controller_command_byte(sc->kbdc,
1719 	    kbdc_get_device_mask(sc->kbdc),
1720 	    (command_byte & KBD_KBD_CONTROL_BITS) |
1721 	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1722 		/*
1723 		 * CONTROLLER ERROR;
1724 		 * we shall ignore this error; see the above comment.
1725 		 */
1726 		log(LOG_ERR,
1727 		    "psm%d: failed to disable the aux port (psmclose).\n",
1728 		    sc->unit);
1729 	}
1730 
1731 	/* remove anything left in the output buffer */
1732 	empty_aux_buffer(sc->kbdc, 10);
1733 
1734 	/* clean up and sigio requests */
1735 	if (sc->async != NULL) {
1736 		funsetown(&sc->async);
1737 		sc->async = NULL;
1738 	}
1739 
1740 	/* close is almost always successful */
1741 	sc->state &= ~PSM_OPEN;
1742 	kbdc_lock(sc->kbdc, FALSE);
1743 	device_unbusy(devclass_get_device(psm_devclass, sc->unit));
1744 	return (0);
1745 }
1746 
1747 static int
1748 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
1749     u_char *buf)
1750 {
1751 	static u_char butmapps2[8] = {
1752 		0,
1753 		MOUSE_PS2_BUTTON1DOWN,
1754 		MOUSE_PS2_BUTTON2DOWN,
1755 		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1756 		MOUSE_PS2_BUTTON3DOWN,
1757 		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1758 		MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1759 		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
1760 		    MOUSE_PS2_BUTTON3DOWN,
1761 	};
1762 	static u_char butmapmsc[8] = {
1763 		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
1764 		    MOUSE_MSC_BUTTON3UP,
1765 		MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1766 		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1767 		MOUSE_MSC_BUTTON3UP,
1768 		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1769 		MOUSE_MSC_BUTTON2UP,
1770 		MOUSE_MSC_BUTTON1UP,
1771 		0,
1772 	};
1773 	int mapped;
1774 	int i;
1775 
1776 	if (sc->mode.level == PSM_LEVEL_BASE) {
1777 		mapped = status->button & ~MOUSE_BUTTON4DOWN;
1778 		if (status->button & MOUSE_BUTTON4DOWN)
1779 			mapped |= MOUSE_BUTTON1DOWN;
1780 		status->button = mapped;
1781 		buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1782 		i = imax(imin(status->dx, 255), -256);
1783 		if (i < 0)
1784 			buf[0] |= MOUSE_PS2_XNEG;
1785 		buf[1] = i;
1786 		i = imax(imin(status->dy, 255), -256);
1787 		if (i < 0)
1788 			buf[0] |= MOUSE_PS2_YNEG;
1789 		buf[2] = i;
1790 		return (MOUSE_PS2_PACKETSIZE);
1791 	} else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1792 		buf[0] = MOUSE_MSC_SYNC |
1793 		    butmapmsc[status->button & MOUSE_STDBUTTONS];
1794 		i = imax(imin(status->dx, 255), -256);
1795 		buf[1] = i >> 1;
1796 		buf[3] = i - buf[1];
1797 		i = imax(imin(status->dy, 255), -256);
1798 		buf[2] = i >> 1;
1799 		buf[4] = i - buf[2];
1800 		i = imax(imin(status->dz, 127), -128);
1801 		buf[5] = (i >> 1) & 0x7f;
1802 		buf[6] = (i - (i >> 1)) & 0x7f;
1803 		buf[7] = (~status->button >> 3) & 0x7f;
1804 		return (MOUSE_SYS_PACKETSIZE);
1805 	}
1806 	return (pb->inputbytes);
1807 }
1808 
1809 static int
1810 psmread(struct cdev *dev, struct uio *uio, int flag)
1811 {
1812 	struct psm_softc *sc = dev->si_drv1;
1813 	u_char buf[PSM_SMALLBUFSIZE];
1814 	int error = 0;
1815 	int s;
1816 	int l;
1817 
1818 	if ((sc->state & PSM_VALID) == 0)
1819 		return (EIO);
1820 
1821 	/* block until mouse activity occured */
1822 	s = spltty();
1823 	while (sc->queue.count <= 0) {
1824 		if (dev != sc->bdev) {
1825 			splx(s);
1826 			return (EWOULDBLOCK);
1827 		}
1828 		sc->state |= PSM_ASLP;
1829 		error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
1830 		sc->state &= ~PSM_ASLP;
1831 		if (error) {
1832 			splx(s);
1833 			return (error);
1834 		} else if ((sc->state & PSM_VALID) == 0) {
1835 			/* the device disappeared! */
1836 			splx(s);
1837 			return (EIO);
1838 		}
1839 	}
1840 	splx(s);
1841 
1842 	/* copy data to the user land */
1843 	while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1844 		s = spltty();
1845 		l = imin(sc->queue.count, uio->uio_resid);
1846 		if (l > sizeof(buf))
1847 			l = sizeof(buf);
1848 		if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1849 			bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1850 			    sizeof(sc->queue.buf) - sc->queue.head);
1851 			bcopy(&sc->queue.buf[0],
1852 			    &buf[sizeof(sc->queue.buf) - sc->queue.head],
1853 			    l - (sizeof(sc->queue.buf) - sc->queue.head));
1854 		} else
1855 			bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1856 		sc->queue.count -= l;
1857 		sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1858 		splx(s);
1859 		error = uiomove(buf, l, uio);
1860 		if (error)
1861 			break;
1862 	}
1863 
1864 	return (error);
1865 }
1866 
1867 static int
1868 block_mouse_data(struct psm_softc *sc, int *c)
1869 {
1870 	int s;
1871 
1872 	if (!kbdc_lock(sc->kbdc, TRUE))
1873 		return (EIO);
1874 
1875 	s = spltty();
1876 	*c = get_controller_command_byte(sc->kbdc);
1877 	if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
1878 	    kbdc_get_device_mask(sc->kbdc),
1879 	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1880 	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1881 		/* this is CONTROLLER ERROR */
1882 		splx(s);
1883 		kbdc_lock(sc->kbdc, FALSE);
1884 		return (EIO);
1885 	}
1886 
1887 	/*
1888 	 * The device may be in the middle of status data transmission.
1889 	 * The transmission will be interrupted, thus, incomplete status
1890 	 * data must be discarded. Although the aux interrupt is disabled
1891 	 * at the keyboard controller level, at most one aux interrupt
1892 	 * may have already been pending and a data byte is in the
1893 	 * output buffer; throw it away. Note that the second argument
1894 	 * to `empty_aux_buffer()' is zero, so that the call will just
1895 	 * flush the internal queue.
1896 	 * `psmintr()' will be invoked after `splx()' if an interrupt is
1897 	 * pending; it will see no data and returns immediately.
1898 	 */
1899 	empty_aux_buffer(sc->kbdc, 0);		/* flush the queue */
1900 	read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
1901 	flushpackets(sc);
1902 	splx(s);
1903 
1904 	return (0);
1905 }
1906 
1907 static void
1908 dropqueue(struct psm_softc *sc)
1909 {
1910 
1911 	sc->queue.count = 0;
1912 	sc->queue.head = 0;
1913 	sc->queue.tail = 0;
1914 	if ((sc->state & PSM_SOFTARMED) != 0) {
1915 		sc->state &= ~PSM_SOFTARMED;
1916 		callout_stop(&sc->softcallout);
1917 	}
1918 	sc->pqueue_start = sc->pqueue_end;
1919 }
1920 
1921 static void
1922 flushpackets(struct psm_softc *sc)
1923 {
1924 
1925 	dropqueue(sc);
1926 	bzero(&sc->pqueue, sizeof(sc->pqueue));
1927 }
1928 
1929 static int
1930 unblock_mouse_data(struct psm_softc *sc, int c)
1931 {
1932 	int error = 0;
1933 
1934 	/*
1935 	 * We may have seen a part of status data during `set_mouse_XXX()'.
1936 	 * they have been queued; flush it.
1937 	 */
1938 	empty_aux_buffer(sc->kbdc, 0);
1939 
1940 	/* restore ports and interrupt */
1941 	if (!set_controller_command_byte(sc->kbdc,
1942 	    kbdc_get_device_mask(sc->kbdc),
1943 	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1944 		/*
1945 		 * CONTROLLER ERROR; this is serious, we may have
1946 		 * been left with the inaccessible keyboard and
1947 		 * the disabled mouse interrupt.
1948 		 */
1949 		error = EIO;
1950 	}
1951 
1952 	kbdc_lock(sc->kbdc, FALSE);
1953 	return (error);
1954 }
1955 
1956 static int
1957 psmwrite(struct cdev *dev, struct uio *uio, int flag)
1958 {
1959 	struct psm_softc *sc = dev->si_drv1;
1960 	u_char buf[PSM_SMALLBUFSIZE];
1961 	int error = 0, i, l;
1962 
1963 	if ((sc->state & PSM_VALID) == 0)
1964 		return (EIO);
1965 
1966 	if (sc->mode.level < PSM_LEVEL_NATIVE)
1967 		return (ENODEV);
1968 
1969 	/* copy data from the user land */
1970 	while (uio->uio_resid > 0) {
1971 		l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
1972 		error = uiomove(buf, l, uio);
1973 		if (error)
1974 			break;
1975 		for (i = 0; i < l; i++) {
1976 			VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
1977 			if (!write_aux_command(sc->kbdc, buf[i])) {
1978 				VLOG(2, (LOG_DEBUG,
1979 				    "psm: cmd 0x%x failed.\n", buf[i]));
1980 				return (reinitialize(sc, FALSE));
1981 			}
1982 		}
1983 	}
1984 
1985 	return (error);
1986 }
1987 
1988 static int
1989 psmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
1990     struct thread *td)
1991 {
1992 	struct psm_softc *sc = dev->si_drv1;
1993 	mousemode_t mode;
1994 	mousestatus_t status;
1995 #if (defined(MOUSE_GETVARS))
1996 	mousevar_t *var;
1997 #endif
1998 	mousedata_t *data;
1999 	int stat[3];
2000 	int command_byte;
2001 	int error = 0;
2002 	int s;
2003 
2004 	/* Perform IOCTL command */
2005 	switch (cmd) {
2006 
2007 	case OLD_MOUSE_GETHWINFO:
2008 		s = spltty();
2009 		((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
2010 		((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
2011 		((old_mousehw_t *)addr)->type = sc->hw.type;
2012 		((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
2013 		splx(s);
2014 		break;
2015 
2016 	case MOUSE_GETHWINFO:
2017 		s = spltty();
2018 		*(mousehw_t *)addr = sc->hw;
2019 		if (sc->mode.level == PSM_LEVEL_BASE)
2020 			((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
2021 		splx(s);
2022 		break;
2023 
2024 	case MOUSE_SYN_GETHWINFO:
2025 		s = spltty();
2026 		if (sc->synhw.infoMajor >= 4)
2027 			*(synapticshw_t *)addr = sc->synhw;
2028 		else
2029 			error = EINVAL;
2030 		splx(s);
2031 		break;
2032 
2033 	case OLD_MOUSE_GETMODE:
2034 		s = spltty();
2035 		switch (sc->mode.level) {
2036 		case PSM_LEVEL_BASE:
2037 			((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2038 			break;
2039 		case PSM_LEVEL_STANDARD:
2040 			((old_mousemode_t *)addr)->protocol =
2041 			    MOUSE_PROTO_SYSMOUSE;
2042 			break;
2043 		case PSM_LEVEL_NATIVE:
2044 			((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2045 			break;
2046 		}
2047 		((old_mousemode_t *)addr)->rate = sc->mode.rate;
2048 		((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2049 		((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2050 		splx(s);
2051 		break;
2052 
2053 	case MOUSE_GETMODE:
2054 		s = spltty();
2055 		*(mousemode_t *)addr = sc->mode;
2056 		if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2057 			((mousemode_t *)addr)->syncmask[0] = 0;
2058 			((mousemode_t *)addr)->syncmask[1] = 0;
2059 		}
2060 		((mousemode_t *)addr)->resolution =
2061 			MOUSE_RES_LOW - sc->mode.resolution;
2062 		switch (sc->mode.level) {
2063 		case PSM_LEVEL_BASE:
2064 			((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2065 			((mousemode_t *)addr)->packetsize =
2066 			    MOUSE_PS2_PACKETSIZE;
2067 			break;
2068 		case PSM_LEVEL_STANDARD:
2069 			((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2070 			((mousemode_t *)addr)->packetsize =
2071 			    MOUSE_SYS_PACKETSIZE;
2072 			((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2073 			((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2074 			break;
2075 		case PSM_LEVEL_NATIVE:
2076 			/* FIXME: this isn't quite correct... XXX */
2077 			((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2078 			break;
2079 		}
2080 		splx(s);
2081 		break;
2082 
2083 	case OLD_MOUSE_SETMODE:
2084 	case MOUSE_SETMODE:
2085 		if (cmd == OLD_MOUSE_SETMODE) {
2086 			mode.rate = ((old_mousemode_t *)addr)->rate;
2087 			/*
2088 			 * resolution  old I/F   new I/F
2089 			 * default        0         0
2090 			 * low            1        -2
2091 			 * medium low     2        -3
2092 			 * medium high    3        -4
2093 			 * high           4        -5
2094 			 */
2095 			if (((old_mousemode_t *)addr)->resolution > 0)
2096 				mode.resolution =
2097 				    -((old_mousemode_t *)addr)->resolution - 1;
2098 			else
2099 				mode.resolution = 0;
2100 			mode.accelfactor =
2101 			    ((old_mousemode_t *)addr)->accelfactor;
2102 			mode.level = -1;
2103 		} else
2104 			mode = *(mousemode_t *)addr;
2105 
2106 		/* adjust and validate parameters. */
2107 		if (mode.rate > UCHAR_MAX)
2108 			return (EINVAL);
2109 		if (mode.rate == 0)
2110 			mode.rate = sc->dflt_mode.rate;
2111 		else if (mode.rate == -1)
2112 			/* don't change the current setting */
2113 			;
2114 		else if (mode.rate < 0)
2115 			return (EINVAL);
2116 		if (mode.resolution >= UCHAR_MAX)
2117 			return (EINVAL);
2118 		if (mode.resolution >= 200)
2119 			mode.resolution = MOUSE_RES_HIGH;
2120 		else if (mode.resolution >= 100)
2121 			mode.resolution = MOUSE_RES_MEDIUMHIGH;
2122 		else if (mode.resolution >= 50)
2123 			mode.resolution = MOUSE_RES_MEDIUMLOW;
2124 		else if (mode.resolution > 0)
2125 			mode.resolution = MOUSE_RES_LOW;
2126 		if (mode.resolution == MOUSE_RES_DEFAULT)
2127 			mode.resolution = sc->dflt_mode.resolution;
2128 		else if (mode.resolution == -1)
2129 			/* don't change the current setting */
2130 			;
2131 		else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2132 			mode.resolution = MOUSE_RES_LOW - mode.resolution;
2133 		if (mode.level == -1)
2134 			/* don't change the current setting */
2135 			mode.level = sc->mode.level;
2136 		else if ((mode.level < PSM_LEVEL_MIN) ||
2137 		    (mode.level > PSM_LEVEL_MAX))
2138 			return (EINVAL);
2139 		if (mode.accelfactor == -1)
2140 			/* don't change the current setting */
2141 			mode.accelfactor = sc->mode.accelfactor;
2142 		else if (mode.accelfactor < 0)
2143 			return (EINVAL);
2144 
2145 		/* don't allow anybody to poll the keyboard controller */
2146 		error = block_mouse_data(sc, &command_byte);
2147 		if (error)
2148 			return (error);
2149 
2150 		/* set mouse parameters */
2151 		if (mode.rate > 0)
2152 			mode.rate = set_mouse_sampling_rate(sc->kbdc,
2153 			    mode.rate);
2154 		if (mode.resolution >= 0)
2155 			mode.resolution =
2156 			    set_mouse_resolution(sc->kbdc, mode.resolution);
2157 		set_mouse_scaling(sc->kbdc, 1);
2158 		get_mouse_status(sc->kbdc, stat, 0, 3);
2159 
2160 		s = spltty();
2161 		sc->mode.rate = mode.rate;
2162 		sc->mode.resolution = mode.resolution;
2163 		sc->mode.accelfactor = mode.accelfactor;
2164 		sc->mode.level = mode.level;
2165 		splx(s);
2166 
2167 		unblock_mouse_data(sc, command_byte);
2168 		break;
2169 
2170 	case MOUSE_GETLEVEL:
2171 		*(int *)addr = sc->mode.level;
2172 		break;
2173 
2174 	case MOUSE_SETLEVEL:
2175 		if ((*(int *)addr < PSM_LEVEL_MIN) ||
2176 		    (*(int *)addr > PSM_LEVEL_MAX))
2177 			return (EINVAL);
2178 		sc->mode.level = *(int *)addr;
2179 		break;
2180 
2181 	case MOUSE_GETSTATUS:
2182 		s = spltty();
2183 		status = sc->status;
2184 		sc->status.flags = 0;
2185 		sc->status.obutton = sc->status.button;
2186 		sc->status.button = 0;
2187 		sc->status.dx = 0;
2188 		sc->status.dy = 0;
2189 		sc->status.dz = 0;
2190 		splx(s);
2191 		*(mousestatus_t *)addr = status;
2192 		break;
2193 
2194 #if (defined(MOUSE_GETVARS))
2195 	case MOUSE_GETVARS:
2196 		var = (mousevar_t *)addr;
2197 		bzero(var, sizeof(*var));
2198 		s = spltty();
2199 		var->var[0] = MOUSE_VARS_PS2_SIG;
2200 		var->var[1] = sc->config;
2201 		var->var[2] = sc->flags;
2202 		splx(s);
2203 		break;
2204 
2205 	case MOUSE_SETVARS:
2206 		return (ENODEV);
2207 #endif /* MOUSE_GETVARS */
2208 
2209 	case MOUSE_READSTATE:
2210 	case MOUSE_READDATA:
2211 		data = (mousedata_t *)addr;
2212 		if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2213 			return (EINVAL);
2214 
2215 		error = block_mouse_data(sc, &command_byte);
2216 		if (error)
2217 			return (error);
2218 		if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2219 		    (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2220 			error = EIO;
2221 		unblock_mouse_data(sc, command_byte);
2222 		break;
2223 
2224 #if (defined(MOUSE_SETRESOLUTION))
2225 	case MOUSE_SETRESOLUTION:
2226 		mode.resolution = *(int *)addr;
2227 		if (mode.resolution >= UCHAR_MAX)
2228 			return (EINVAL);
2229 		else if (mode.resolution >= 200)
2230 			mode.resolution = MOUSE_RES_HIGH;
2231 		else if (mode.resolution >= 100)
2232 			mode.resolution = MOUSE_RES_MEDIUMHIGH;
2233 		else if (mode.resolution >= 50)
2234 			mode.resolution = MOUSE_RES_MEDIUMLOW;
2235 		else if (mode.resolution > 0)
2236 			mode.resolution = MOUSE_RES_LOW;
2237 		if (mode.resolution == MOUSE_RES_DEFAULT)
2238 			mode.resolution = sc->dflt_mode.resolution;
2239 		else if (mode.resolution == -1)
2240 			mode.resolution = sc->mode.resolution;
2241 		else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2242 			mode.resolution = MOUSE_RES_LOW - mode.resolution;
2243 
2244 		error = block_mouse_data(sc, &command_byte);
2245 		if (error)
2246 			return (error);
2247 		sc->mode.resolution =
2248 		    set_mouse_resolution(sc->kbdc, mode.resolution);
2249 		if (sc->mode.resolution != mode.resolution)
2250 			error = EIO;
2251 		unblock_mouse_data(sc, command_byte);
2252 		break;
2253 #endif /* MOUSE_SETRESOLUTION */
2254 
2255 #if (defined(MOUSE_SETRATE))
2256 	case MOUSE_SETRATE:
2257 		mode.rate = *(int *)addr;
2258 		if (mode.rate > UCHAR_MAX)
2259 			return (EINVAL);
2260 		if (mode.rate == 0)
2261 			mode.rate = sc->dflt_mode.rate;
2262 		else if (mode.rate < 0)
2263 			mode.rate = sc->mode.rate;
2264 
2265 		error = block_mouse_data(sc, &command_byte);
2266 		if (error)
2267 			return (error);
2268 		sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2269 		if (sc->mode.rate != mode.rate)
2270 			error = EIO;
2271 		unblock_mouse_data(sc, command_byte);
2272 		break;
2273 #endif /* MOUSE_SETRATE */
2274 
2275 #if (defined(MOUSE_SETSCALING))
2276 	case MOUSE_SETSCALING:
2277 		if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2278 			return (EINVAL);
2279 
2280 		error = block_mouse_data(sc, &command_byte);
2281 		if (error)
2282 			return (error);
2283 		if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2284 			error = EIO;
2285 		unblock_mouse_data(sc, command_byte);
2286 		break;
2287 #endif /* MOUSE_SETSCALING */
2288 
2289 #if (defined(MOUSE_GETHWID))
2290 	case MOUSE_GETHWID:
2291 		error = block_mouse_data(sc, &command_byte);
2292 		if (error)
2293 			return (error);
2294 		sc->hw.hwid &= ~0x00ff;
2295 		sc->hw.hwid |= get_aux_id(sc->kbdc);
2296 		*(int *)addr = sc->hw.hwid & 0x00ff;
2297 		unblock_mouse_data(sc, command_byte);
2298 		break;
2299 #endif /* MOUSE_GETHWID */
2300 
2301 	case FIONBIO:
2302 	case FIOASYNC:
2303 		break;
2304 	case FIOSETOWN:
2305 		error = fsetown(*(int *)addr, &sc->async);
2306 		break;
2307 	case FIOGETOWN:
2308 		*(int *) addr = fgetown(&sc->async);
2309 		break;
2310 	default:
2311 		return (ENOTTY);
2312 	}
2313 
2314 	return (error);
2315 }
2316 
2317 static void
2318 psmtimeout(void *arg)
2319 {
2320 	struct psm_softc *sc;
2321 	int s;
2322 
2323 	sc = (struct psm_softc *)arg;
2324 	s = spltty();
2325 	if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2326 		VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2327 		psmintr(sc);
2328 		kbdc_lock(sc->kbdc, FALSE);
2329 	}
2330 	sc->watchdog = TRUE;
2331 	splx(s);
2332 	callout_reset(&sc->callout, hz, psmtimeout, sc);
2333 }
2334 
2335 /* Add all sysctls under the debug.psm and hw.psm nodes */
2336 static SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2337 static SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2338 
2339 SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RW, &verbose, 0,
2340     "Verbosity level");
2341 
2342 static int psmhz = 20;
2343 SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2344     "Frequency of the softcallout (in hz)");
2345 static int psmerrsecs = 2;
2346 SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2347     "Number of seconds during which packets will dropped after a sync error");
2348 static int psmerrusecs = 0;
2349 SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2350     "Microseconds to add to psmerrsecs");
2351 static int psmsecs = 0;
2352 SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2353     "Max number of seconds between soft interrupts");
2354 static int psmusecs = 500000;
2355 SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2356     "Microseconds to add to psmsecs");
2357 static int pkterrthresh = 2;
2358 SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2359     "Number of error packets allowed before reinitializing the mouse");
2360 
2361 SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RW, &tap_enabled, 0,
2362     "Enable tap and drag gestures");
2363 static int tap_threshold = PSM_TAP_THRESHOLD;
2364 SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2365     "Button tap threshold");
2366 static int tap_timeout = PSM_TAP_TIMEOUT;
2367 SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2368     "Tap timeout for touchpads");
2369 
2370 static void
2371 psmintr(void *arg)
2372 {
2373 	struct psm_softc *sc = arg;
2374 	struct timeval now;
2375 	int c;
2376 	packetbuf_t *pb;
2377 
2378 
2379 	/* read until there is nothing to read */
2380 	while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2381 		pb = &sc->pqueue[sc->pqueue_end];
2382 
2383 		/* discard the byte if the device is not open */
2384 		if ((sc->state & PSM_OPEN) == 0)
2385 			continue;
2386 
2387 		getmicrouptime(&now);
2388 		if ((pb->inputbytes > 0) &&
2389 		    timevalcmp(&now, &sc->inputtimeout, >)) {
2390 			VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2391 			    "resetting byte count\n"));
2392 			pb->inputbytes = 0;
2393 			sc->syncerrors = 0;
2394 			sc->pkterrors = 0;
2395 		}
2396 		sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2397 		sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2398 		timevaladd(&sc->inputtimeout, &now);
2399 
2400 		pb->ipacket[pb->inputbytes++] = c;
2401 
2402 		if (sc->mode.level == PSM_LEVEL_NATIVE) {
2403 			VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2404 			sc->syncerrors = 0;
2405 			sc->pkterrors = 0;
2406 			goto next;
2407 		} else {
2408 			if (pb->inputbytes < sc->mode.packetsize)
2409 				continue;
2410 
2411 			VLOG(4, (LOG_DEBUG,
2412 			    "psmintr: %02x %02x %02x %02x %02x %02x\n",
2413 			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2414 			    pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2415 		}
2416 
2417 		c = pb->ipacket[0];
2418 
2419 		if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2420 			sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2421 			sc->flags &= ~PSM_NEED_SYNCBITS;
2422 			VLOG(2, (LOG_DEBUG,
2423 			    "psmintr: Sync bytes now %04x,%04x\n",
2424 			    sc->mode.syncmask[0], sc->mode.syncmask[0]));
2425 		} else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2426 			VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
2427 			    "(%04x != %04x) %d cmds since last error.\n",
2428 			    c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2429 			    sc->cmdcount - sc->lasterr));
2430 			sc->lasterr = sc->cmdcount;
2431 			/*
2432 			 * The sync byte test is a weak measure of packet
2433 			 * validity.  Conservatively discard any input yet
2434 			 * to be seen by userland when we detect a sync
2435 			 * error since there is a good chance some of
2436 			 * the queued packets have undetected errors.
2437 			 */
2438 			dropqueue(sc);
2439 			if (sc->syncerrors == 0)
2440 				sc->pkterrors++;
2441 			++sc->syncerrors;
2442 			sc->lastinputerr = now;
2443 			if (sc->syncerrors >= sc->mode.packetsize * 2 ||
2444 			    sc->pkterrors >= pkterrthresh) {
2445 				/*
2446 				 * If we've failed to find a single sync byte
2447 				 * in 2 packets worth of data, or we've seen
2448 				 * persistent packet errors during the
2449 				 * validation period, reinitialize the mouse
2450 				 * in hopes of returning it to the expected
2451 				 * mode.
2452 				 */
2453 				VLOG(3, (LOG_DEBUG,
2454 				    "psmintr: reset the mouse.\n"));
2455 				reinitialize(sc, TRUE);
2456 			} else if (sc->syncerrors == sc->mode.packetsize) {
2457 				/*
2458 				 * Try a soft reset after searching for a sync
2459 				 * byte through a packet length of bytes.
2460 				 */
2461 				VLOG(3, (LOG_DEBUG,
2462 				    "psmintr: re-enable the mouse.\n"));
2463 				pb->inputbytes = 0;
2464 				disable_aux_dev(sc->kbdc);
2465 				enable_aux_dev(sc->kbdc);
2466 			} else {
2467 				VLOG(3, (LOG_DEBUG,
2468 				    "psmintr: discard a byte (%d)\n",
2469 				    sc->syncerrors));
2470 				pb->inputbytes--;
2471 				bcopy(&pb->ipacket[1], &pb->ipacket[0],
2472 				    pb->inputbytes);
2473 			}
2474 			continue;
2475 		}
2476 
2477 		/*
2478 		 * We have what appears to be a valid packet.
2479 		 * Reset the error counters.
2480 		 */
2481 		sc->syncerrors = 0;
2482 
2483 		/*
2484 		 * Drop even good packets if they occur within a timeout
2485 		 * period of a sync error.  This allows the detection of
2486 		 * a change in the mouse's packet mode without exposing
2487 		 * erratic mouse behavior to the user.  Some KVMs forget
2488 		 * enhanced mouse modes during switch events.
2489 		 */
2490 		if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
2491 		    &now)) {
2492 			pb->inputbytes = 0;
2493 			continue;
2494 		}
2495 
2496 		/*
2497 		 * Now that we're out of the validation period, reset
2498 		 * the packet error count.
2499 		 */
2500 		sc->pkterrors = 0;
2501 
2502 		sc->cmdcount++;
2503 next:
2504 		if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2505 			sc->pqueue_end = 0;
2506 		/*
2507 		 * If we've filled the queue then call the softintr ourselves,
2508 		 * otherwise schedule the interrupt for later.
2509 		 */
2510 		if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2511 		    (sc->pqueue_end == sc->pqueue_start)) {
2512 			if ((sc->state & PSM_SOFTARMED) != 0) {
2513 				sc->state &= ~PSM_SOFTARMED;
2514 				callout_stop(&sc->softcallout);
2515 			}
2516 			psmsoftintr(arg);
2517 		} else if ((sc->state & PSM_SOFTARMED) == 0) {
2518 			sc->state |= PSM_SOFTARMED;
2519 			callout_reset(&sc->softcallout,
2520 			    psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg);
2521 		}
2522 	}
2523 }
2524 
2525 static void
2526 proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2527     int *x, int *y, int *z)
2528 {
2529 
2530 	/*
2531 	 * PS2++ protocol packet
2532 	 *
2533 	 *          b7 b6 b5 b4 b3 b2 b1 b0
2534 	 * byte 1:  *  1  p3 p2 1  *  *  *
2535 	 * byte 2:  c1 c2 p1 p0 d1 d0 1  0
2536 	 *
2537 	 * p3-p0: packet type
2538 	 * c1, c2: c1 & c2 == 1, if p2 == 0
2539 	 *         c1 & c2 == 0, if p2 == 1
2540 	 *
2541 	 * packet type: 0 (device type)
2542 	 * See comments in enable_mmanplus() below.
2543 	 *
2544 	 * packet type: 1 (wheel data)
2545 	 *
2546 	 *          b7 b6 b5 b4 b3 b2 b1 b0
2547 	 * byte 3:  h  *  B5 B4 s  d2 d1 d0
2548 	 *
2549 	 * h: 1, if horizontal roller data
2550 	 *    0, if vertical roller data
2551 	 * B4, B5: button 4 and 5
2552 	 * s: sign bit
2553 	 * d2-d0: roller data
2554 	 *
2555 	 * packet type: 2 (reserved)
2556 	 */
2557 	if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
2558 	    (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2559 		/*
2560 		 * the extended data packet encodes button
2561 		 * and wheel events
2562 		 */
2563 		switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
2564 		case 1:
2565 			/* wheel data packet */
2566 			*x = *y = 0;
2567 			if (pb->ipacket[2] & 0x80) {
2568 				/* XXX horizontal roller count - ignore it */
2569 				;
2570 			} else {
2571 				/* vertical roller count */
2572 				*z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
2573 				    (pb->ipacket[2] & 0x0f) - 16 :
2574 				    (pb->ipacket[2] & 0x0f);
2575 			}
2576 			ms->button |= (pb->ipacket[2] &
2577 			    MOUSE_PS2PLUS_BUTTON4DOWN) ?
2578 			    MOUSE_BUTTON4DOWN : 0;
2579 			ms->button |= (pb->ipacket[2] &
2580 			    MOUSE_PS2PLUS_BUTTON5DOWN) ?
2581 			    MOUSE_BUTTON5DOWN : 0;
2582 			break;
2583 		case 2:
2584 			/*
2585 			 * this packet type is reserved by
2586 			 * Logitech...
2587 			 */
2588 			/*
2589 			 * IBM ScrollPoint Mouse uses this
2590 			 * packet type to encode both vertical
2591 			 * and horizontal scroll movement.
2592 			 */
2593 			*x = *y = 0;
2594 			/* horizontal count */
2595 			if (pb->ipacket[2] & 0x0f)
2596 				*z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
2597 				    -2 : 2;
2598 			/* vertical count */
2599 			if (pb->ipacket[2] & 0xf0)
2600 				*z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
2601 				    -1 : 1;
2602 			break;
2603 		case 0:
2604 			/* device type packet - shouldn't happen */
2605 			/* FALLTHROUGH */
2606 		default:
2607 			*x = *y = 0;
2608 			ms->button = ms->obutton;
2609 			VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
2610 			    "type %d: 0x%02x 0x%02x 0x%02x\n",
2611 			    MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2612 			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
2613 			break;
2614 		}
2615 	} else {
2616 		/* preserve button states */
2617 		ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
2618 	}
2619 }
2620 
2621 static int
2622 proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2623     int *x, int *y, int *z)
2624 {
2625 	static int touchpad_buttons;
2626 	static int guest_buttons;
2627 	int w, x0, y0;
2628 
2629 	/* TouchPad PS/2 absolute mode message format with capFourButtons:
2630 	 *
2631 	 *  Bits:        7   6   5   4   3   2   1   0 (LSB)
2632 	 *  ------------------------------------------------
2633 	 *  ipacket[0]:  1   0  W3  W2   0  W1   R   L
2634 	 *  ipacket[1]: Yb  Ya  Y9  Y8  Xb  Xa  X9  X8
2635 	 *  ipacket[2]: Z7  Z6  Z5  Z4  Z3  Z2  Z1  Z0
2636 	 *  ipacket[3]:  1   1  Yc  Xc   0  W0 D^R U^L
2637 	 *  ipacket[4]: X7  X6  X5  X4  X3  X2  X1  X0
2638 	 *  ipacket[5]: Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
2639 	 *
2640 	 * Legend:
2641 	 *  L: left physical mouse button
2642 	 *  R: right physical mouse button
2643 	 *  D: down button
2644 	 *  U: up button
2645 	 *  W: "wrist" value
2646 	 *  X: x position
2647 	 *  Y: y position
2648 	 *  Z: pressure
2649 	 *
2650 	 * Without capFourButtons but with nExtendeButtons and/or capMiddle
2651 	 *
2652 	 *  Bits:        7   6   5   4      3      2      1      0 (LSB)
2653 	 *  ------------------------------------------------------
2654 	 *  ipacket[3]:  1   1  Yc  Xc      0     W0    E^R    M^L
2655 	 *  ipacket[4]: X7  X6  X5  X4  X3|b7  X2|b5  X1|b3  X0|b1
2656 	 *  ipacket[5]: Y7  Y6  Y5  Y4  Y3|b8  Y2|b6  Y1|b4  Y0|b2
2657 	 *
2658 	 * Legend:
2659 	 *  M: Middle physical mouse button
2660 	 *  E: Extended mouse buttons reported instead of low bits of X and Y
2661 	 *  b1-b8: Extended mouse buttons
2662 	 *    Only ((nExtendedButtons + 1) >> 1) bits are used in packet
2663 	 *    4 and 5, for reading X and Y value they should be zeroed.
2664 	 *
2665 	 * Absolute reportable limits:    0 - 6143.
2666 	 * Typical bezel limits:       1472 - 5472.
2667 	 * Typical edge marings:       1632 - 5312.
2668 	 *
2669 	 * w = 3 Passthrough Packet
2670 	 *
2671 	 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2672 	 */
2673 
2674 	if (!synaptics_support)
2675 		return (0);
2676 
2677 	/* Sanity check for out of sync packets. */
2678 	if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2679 	    (pb->ipacket[3] & 0xc8) != 0xc0)
2680 		return (-1);
2681 
2682 	*x = *y = 0;
2683 
2684 	/*
2685 	 * Pressure value.
2686 	 * Interpretation:
2687 	 *   z = 0      No finger contact
2688 	 *   z = 10     Finger hovering near the pad
2689 	 *   z = 30     Very light finger contact
2690 	 *   z = 80     Normal finger contact
2691 	 *   z = 110    Very heavy finger contact
2692 	 *   z = 200    Finger lying flat on pad surface
2693 	 *   z = 255    Maximum reportable Z
2694 	 */
2695 	*z = pb->ipacket[2];
2696 
2697 	/*
2698 	 * Finger width value
2699 	 * Interpretation:
2700 	 *   w = 0      Two finger on the pad (capMultiFinger needed)
2701 	 *   w = 1      Three or more fingers (capMultiFinger needed)
2702 	 *   w = 2      Pen (instead of finger) (capPen needed)
2703 	 *   w = 3      Reserved (passthrough?)
2704 	 *   w = 4-7    Finger of normal width (capPalmDetect needed)
2705 	 *   w = 8-14   Very wide finger or palm (capPalmDetect needed)
2706 	 *   w = 15     Maximum reportable width (capPalmDetect needed)
2707 	 */
2708 	/* XXX Is checking capExtended enough? */
2709 	if (sc->synhw.capExtended)
2710 		w = ((pb->ipacket[0] & 0x30) >> 2) |
2711 		    ((pb->ipacket[0] & 0x04) >> 1) |
2712 		    ((pb->ipacket[3] & 0x04) >> 2);
2713 	else {
2714 		/* Assume a finger of regular width. */
2715 		w = 4;
2716 	}
2717 
2718 	/*
2719 	 * Handle packets from the guest device. See:
2720 	 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
2721 	 */
2722 	if (w == 3 && sc->synhw.capPassthrough) {
2723 		*x = ((pb->ipacket[1] & 0x10) ?
2724 		    pb->ipacket[4] - 256 : pb->ipacket[4]);
2725 		*y = ((pb->ipacket[1] & 0x20) ?
2726 		    pb->ipacket[5] - 256 : pb->ipacket[5]);
2727 		*z = 0;
2728 
2729 		guest_buttons = 0;
2730 		if (pb->ipacket[1] & 0x01)
2731 			guest_buttons |= MOUSE_BUTTON1DOWN;
2732 		if (pb->ipacket[1] & 0x04)
2733 			guest_buttons |= MOUSE_BUTTON2DOWN;
2734 		if (pb->ipacket[1] & 0x02)
2735 			guest_buttons |= MOUSE_BUTTON3DOWN;
2736 
2737 		ms->button = touchpad_buttons | guest_buttons;
2738 		goto SYNAPTICS_END;
2739 	}
2740 
2741 	if (sc->syninfo.touchpad_off) {
2742 		*x = *y = *z = 0;
2743 		ms->button = ms->obutton;
2744 		goto SYNAPTICS_END;
2745 	}
2746 
2747 	/* Button presses */
2748 	touchpad_buttons = 0;
2749 	if (pb->ipacket[0] & 0x01)
2750 		touchpad_buttons |= MOUSE_BUTTON1DOWN;
2751 	if (pb->ipacket[0] & 0x02)
2752 		touchpad_buttons |= MOUSE_BUTTON3DOWN;
2753 
2754 	if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2755 		if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
2756 			touchpad_buttons |= MOUSE_BUTTON4DOWN;
2757 		if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
2758 			touchpad_buttons |= MOUSE_BUTTON5DOWN;
2759 	} else if (sc->synhw.capExtended && sc->synhw.capMiddle &&
2760 	    !sc->synhw.capClickPad) {
2761 		/* Middle Button */
2762 		if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
2763 			touchpad_buttons |= MOUSE_BUTTON2DOWN;
2764 	} else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
2765 		/* Extended Buttons */
2766 		if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
2767 			if (sc->syninfo.directional_scrolls) {
2768 				if (pb->ipacket[4] & 0x01)
2769 					touchpad_buttons |= MOUSE_BUTTON4DOWN;
2770 				if (pb->ipacket[5] & 0x01)
2771 					touchpad_buttons |= MOUSE_BUTTON5DOWN;
2772 				if (pb->ipacket[4] & 0x02)
2773 					touchpad_buttons |= MOUSE_BUTTON6DOWN;
2774 				if (pb->ipacket[5] & 0x02)
2775 					touchpad_buttons |= MOUSE_BUTTON7DOWN;
2776 			} else {
2777 				if (pb->ipacket[4] & 0x01)
2778 					touchpad_buttons |= MOUSE_BUTTON1DOWN;
2779 				if (pb->ipacket[5] & 0x01)
2780 					touchpad_buttons |= MOUSE_BUTTON3DOWN;
2781 				if (pb->ipacket[4] & 0x02)
2782 					touchpad_buttons |= MOUSE_BUTTON2DOWN;
2783 				sc->extended_buttons = touchpad_buttons;
2784 			}
2785 
2786 			/*
2787 			 * Zero out bits used by extended buttons to avoid
2788 			 * misinterpretation of the data absolute position.
2789 			 *
2790 			 * The bits represented by
2791 			 *
2792 			 *     (nExtendedButtons + 1) >> 1
2793 			 *
2794 			 * will be masked out in both bytes.
2795 			 * The mask for n bits is computed with the formula
2796 			 *
2797 			 *     (1 << n) - 1
2798 			 */
2799 			int maskedbits = 0;
2800 			int mask = 0;
2801 			maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
2802 			mask = (1 << maskedbits) - 1;
2803 			pb->ipacket[4] &= ~(mask);
2804 			pb->ipacket[5] &= ~(mask);
2805 		} else	if (!sc->syninfo.directional_scrolls &&
2806 		    !sc->synaction.in_vscroll) {
2807 			/*
2808 			 * Keep reporting MOUSE DOWN until we get a new packet
2809 			 * indicating otherwise.
2810 			 */
2811 			touchpad_buttons |= sc->extended_buttons;
2812 		}
2813 	}
2814 	/* Handle ClickPad. */
2815 	if (sc->synhw.capClickPad &&
2816 	    ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01))
2817 		touchpad_buttons |= MOUSE_BUTTON1DOWN;
2818 
2819 	ms->button = touchpad_buttons | guest_buttons;
2820 
2821 	/*
2822 	 * Check pressure to detect a real wanted action on the
2823 	 * touchpad.
2824 	 */
2825 	if (*z >= sc->syninfo.min_pressure) {
2826 		synapticsaction_t *synaction;
2827 		int cursor, peer, window;
2828 		int dx, dy, dxp, dyp;
2829 		int max_width, max_pressure;
2830 		int margin_top, margin_right, margin_bottom, margin_left;
2831 		int na_top, na_right, na_bottom, na_left;
2832 		int window_min, window_max;
2833 		int multiplicator;
2834 		int weight_current, weight_previous, weight_len_squared;
2835 		int div_min, div_max, div_len;
2836 		int vscroll_hor_area, vscroll_ver_area;
2837 		int two_finger_scroll;
2838 		int len, weight_prev_x, weight_prev_y;
2839 		int div_max_x, div_max_y, div_x, div_y;
2840 
2841 		/* Read sysctl. */
2842 		/* XXX Verify values? */
2843 		max_width = sc->syninfo.max_width;
2844 		max_pressure = sc->syninfo.max_pressure;
2845 		margin_top = sc->syninfo.margin_top;
2846 		margin_right = sc->syninfo.margin_right;
2847 		margin_bottom = sc->syninfo.margin_bottom;
2848 		margin_left = sc->syninfo.margin_left;
2849 		na_top = sc->syninfo.na_top;
2850 		na_right = sc->syninfo.na_right;
2851 		na_bottom = sc->syninfo.na_bottom;
2852 		na_left = sc->syninfo.na_left;
2853 		window_min = sc->syninfo.window_min;
2854 		window_max = sc->syninfo.window_max;
2855 		multiplicator = sc->syninfo.multiplicator;
2856 		weight_current = sc->syninfo.weight_current;
2857 		weight_previous = sc->syninfo.weight_previous;
2858 		weight_len_squared = sc->syninfo.weight_len_squared;
2859 		div_min = sc->syninfo.div_min;
2860 		div_max = sc->syninfo.div_max;
2861 		div_len = sc->syninfo.div_len;
2862 		vscroll_hor_area = sc->syninfo.vscroll_hor_area;
2863 		vscroll_ver_area = sc->syninfo.vscroll_ver_area;
2864 		two_finger_scroll = sc->syninfo.two_finger_scroll;
2865 
2866 		/* Palm detection. */
2867 		if (!(
2868 		    (sc->synhw.capMultiFinger && (w == 0 || w == 1)) ||
2869 		    (sc->synhw.capPalmDetect && w >= 4 && w <= max_width) ||
2870 		    (!sc->synhw.capPalmDetect && *z <= max_pressure) ||
2871 		    (sc->synhw.capPen && w == 2))) {
2872 			/*
2873 			 * We consider the packet irrelevant for the current
2874 			 * action when:
2875 			 *  - the width isn't comprised in:
2876 			 *    [4; max_width]
2877 			 *  - the pressure isn't comprised in:
2878 			 *    [min_pressure; max_pressure]
2879 			 *  - pen aren't supported but w is 2
2880 			 *
2881 			 *  Note that this doesn't terminate the current action.
2882 			 */
2883 			VLOG(2, (LOG_DEBUG,
2884 			    "synaptics: palm detected! (%d)\n", w));
2885 			goto SYNAPTICS_END;
2886 		}
2887 
2888 		/* Read current absolute position. */
2889 		x0 = ((pb->ipacket[3] & 0x10) << 8) |
2890 		    ((pb->ipacket[1] & 0x0f) << 8) |
2891 		    pb->ipacket[4];
2892 		y0 = ((pb->ipacket[3] & 0x20) << 7) |
2893 		    ((pb->ipacket[1] & 0xf0) << 4) |
2894 		    pb->ipacket[5];
2895 
2896 		synaction = &(sc->synaction);
2897 
2898 		/*
2899 		 * If the action is just beginning, init the structure and
2900 		 * compute tap timeout.
2901 		 */
2902 		if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
2903 			VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
2904 
2905 			/* Store the first point of this action. */
2906 			synaction->start_x = x0;
2907 			synaction->start_y = y0;
2908 			dx = dy = 0;
2909 
2910 			/* Initialize queue. */
2911 			synaction->queue_cursor = SYNAPTICS_PACKETQUEUE;
2912 			synaction->queue_len = 0;
2913 			synaction->window_min = window_min;
2914 
2915 			/* Reset average. */
2916 			synaction->avg_dx = 0;
2917 			synaction->avg_dy = 0;
2918 
2919 			/* Reset squelch. */
2920 			synaction->squelch_x = 0;
2921 			synaction->squelch_y = 0;
2922 
2923 			/* Reset pressure peak. */
2924 			sc->zmax = 0;
2925 
2926 			/* Reset fingers count. */
2927 			synaction->fingers_nb = 0;
2928 
2929 			/* Reset virtual scrolling state. */
2930 			synaction->in_vscroll = 0;
2931 
2932 			/* Compute tap timeout. */
2933 			sc->taptimeout.tv_sec  = tap_timeout / 1000000;
2934 			sc->taptimeout.tv_usec = tap_timeout % 1000000;
2935 			timevaladd(&sc->taptimeout, &sc->lastsoftintr);
2936 
2937 			sc->flags |= PSM_FLAGS_FINGERDOWN;
2938 		} else {
2939 			/* Calculate the current delta. */
2940 			cursor = synaction->queue_cursor;
2941 			dx = x0 - synaction->queue[cursor].x;
2942 			dy = y0 - synaction->queue[cursor].y;
2943 		}
2944 
2945 		/* If in tap-hold, add the recorded button. */
2946 		if (synaction->in_taphold)
2947 			ms->button |= synaction->tap_button;
2948 
2949 		/*
2950 		 * From now on, we can use the SYNAPTICS_END label to skip
2951 		 * the current packet.
2952 		 */
2953 
2954 		/*
2955 		 * Limit the coordinates to the specified margins because
2956 		 * this area isn't very reliable.
2957 		 */
2958 		if (x0 <= margin_left)
2959 			x0 = margin_left;
2960 		else if (x0 >= 6143 - margin_right)
2961 			x0 = 6143 - margin_right;
2962 		if (y0 <= margin_bottom)
2963 			y0 = margin_bottom;
2964 		else if (y0 >= 6143 - margin_top)
2965 			y0 = 6143 - margin_top;
2966 
2967 		VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
2968 		    x0, y0, *z, w));
2969 
2970 		/* Queue this new packet. */
2971 		cursor = SYNAPTICS_QUEUE_CURSOR(synaction->queue_cursor - 1);
2972 		synaction->queue[cursor].x = x0;
2973 		synaction->queue[cursor].y = y0;
2974 		synaction->queue_cursor = cursor;
2975 		if (synaction->queue_len < SYNAPTICS_PACKETQUEUE)
2976 			synaction->queue_len++;
2977 		VLOG(5, (LOG_DEBUG,
2978 		    "synaptics: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
2979 		    cursor, x0, y0, dx, dy));
2980 
2981 		/*
2982 		 * For tap, we keep the maximum number of fingers and the
2983 		 * pressure peak. Also with multiple fingers, we increase
2984 		 * the minimum window.
2985 		 */
2986 		switch (w) {
2987 		case 1: /* Three or more fingers. */
2988 			synaction->fingers_nb = imax(3, synaction->fingers_nb);
2989 			synaction->window_min = window_max;
2990 			break;
2991 		case 0: /* Two fingers. */
2992 			synaction->fingers_nb = imax(2, synaction->fingers_nb);
2993 			synaction->window_min = window_max;
2994 			break;
2995 		default: /* One finger or undetectable. */
2996 			synaction->fingers_nb = imax(1, synaction->fingers_nb);
2997 		}
2998 		sc->zmax = imax(*z, sc->zmax);
2999 
3000 		/* Do we have enough packets to consider this a movement? */
3001 		if (synaction->queue_len < synaction->window_min)
3002 			goto SYNAPTICS_END;
3003 
3004 		/* Is a scrolling action occuring? */
3005 		if (!synaction->in_taphold && !synaction->in_vscroll) {
3006 			/*
3007 			 * A scrolling action must not conflict with a tap
3008 			 * action. Here are the conditions to consider a
3009 			 * scrolling action:
3010 			 *  - the action in a configurable area
3011 			 *  - one of the following:
3012 			 *     . the distance between the last packet and the
3013 			 *       first should be above a configurable minimum
3014 			 *     . tap timed out
3015 			 */
3016 			dxp = abs(synaction->queue[synaction->queue_cursor].x -
3017 			    synaction->start_x);
3018 			dyp = abs(synaction->queue[synaction->queue_cursor].y -
3019 			    synaction->start_y);
3020 
3021 			if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, >) ||
3022 			    dxp >= sc->syninfo.vscroll_min_delta ||
3023 			    dyp >= sc->syninfo.vscroll_min_delta) {
3024 				/*
3025 				 * Handle two finger scrolling.
3026 				 * Note that we don't rely on fingers_nb
3027 				 * as that keeps the maximum number of fingers.
3028 				 */
3029 				if (two_finger_scroll) {
3030 					if (w == 0) {
3031 						synaction->in_vscroll +=
3032 						    dyp ? 2 : 0;
3033 						synaction->in_vscroll +=
3034 						    dxp ? 1 : 0;
3035 					}
3036 				} else {
3037 					/* Check for horizontal scrolling. */
3038 					if ((vscroll_hor_area > 0 &&
3039 					    synaction->start_y <=
3040 					        vscroll_hor_area) ||
3041 					    (vscroll_hor_area < 0 &&
3042 					     synaction->start_y >=
3043 					     6143 + vscroll_hor_area))
3044 						synaction->in_vscroll += 2;
3045 
3046 					/* Check for vertical scrolling. */
3047 					if ((vscroll_ver_area > 0 &&
3048 					    synaction->start_x <=
3049 						vscroll_ver_area) ||
3050 					    (vscroll_ver_area < 0 &&
3051 					     synaction->start_x >=
3052 					     6143 + vscroll_ver_area))
3053 						synaction->in_vscroll += 1;
3054 				}
3055 
3056 				/* Avoid conflicts if area overlaps. */
3057 				if (synaction->in_vscroll >= 3)
3058 					synaction->in_vscroll =
3059 					    (dxp > dyp) ? 2 : 1;
3060 			}
3061 		}
3062 		/*
3063 		 * Reset two finger scrolling when the number of fingers
3064 		 * is different from two.
3065 		 */
3066 		if (two_finger_scroll && w != 0)
3067 			synaction->in_vscroll = 0;
3068 
3069 		VLOG(5, (LOG_DEBUG,
3070 			"synaptics: virtual scrolling: %s "
3071 			"(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n",
3072 			synaction->in_vscroll ? "YES" : "NO",
3073 			synaction->in_vscroll, dxp, dyp,
3074 			synaction->fingers_nb));
3075 
3076 		weight_prev_x = weight_prev_y = weight_previous;
3077 		div_max_x = div_max_y = div_max;
3078 
3079 		if (synaction->in_vscroll) {
3080 			/* Dividers are different with virtual scrolling. */
3081 			div_min = sc->syninfo.vscroll_div_min;
3082 			div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
3083 		} else {
3084 			/*
3085 			 * There's a lot of noise in coordinates when
3086 			 * the finger is on the touchpad's borders. When
3087 			 * using this area, we apply a special weight and
3088 			 * div.
3089 			 */
3090 			if (x0 <= na_left || x0 >= 6143 - na_right) {
3091 				weight_prev_x = sc->syninfo.weight_previous_na;
3092 				div_max_x = sc->syninfo.div_max_na;
3093 			}
3094 
3095 			if (y0 <= na_bottom || y0 >= 6143 - na_top) {
3096 				weight_prev_y = sc->syninfo.weight_previous_na;
3097 				div_max_y = sc->syninfo.div_max_na;
3098 			}
3099 		}
3100 
3101 		/*
3102 		 * Calculate weights for the average operands and
3103 		 * the divisor. Both depend on the distance between
3104 		 * the current packet and a previous one (based on the
3105 		 * window width).
3106 		 */
3107 		window = imin(synaction->queue_len, window_max);
3108 		peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
3109 		dxp = abs(x0 - synaction->queue[peer].x) + 1;
3110 		dyp = abs(y0 - synaction->queue[peer].y) + 1;
3111 		len = (dxp * dxp) + (dyp * dyp);
3112 		weight_prev_x = imin(weight_prev_x,
3113 		    weight_len_squared * weight_prev_x / len);
3114 		weight_prev_y = imin(weight_prev_y,
3115 		    weight_len_squared * weight_prev_y / len);
3116 
3117 		len = (dxp + dyp) / 2;
3118 		div_x = div_len * div_max_x / len;
3119 		div_x = imin(div_max_x, div_x);
3120 		div_x = imax(div_min, div_x);
3121 		div_y = div_len * div_max_y / len;
3122 		div_y = imin(div_max_y, div_y);
3123 		div_y = imax(div_min, div_y);
3124 
3125 		VLOG(3, (LOG_DEBUG,
3126 		    "synaptics: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
3127 		    peer, len, weight_prev_x, weight_prev_y, div_x, div_y));
3128 
3129 		/* Compute averages. */
3130 		synaction->avg_dx =
3131 		    (weight_current * dx * multiplicator +
3132 		     weight_prev_x * synaction->avg_dx) /
3133 		    (weight_current + weight_prev_x);
3134 
3135 		synaction->avg_dy =
3136 		    (weight_current * dy * multiplicator +
3137 		     weight_prev_y * synaction->avg_dy) /
3138 		    (weight_current + weight_prev_y);
3139 
3140 		VLOG(5, (LOG_DEBUG,
3141 		    "synaptics: avg_dx~=%d, avg_dy~=%d\n",
3142 		    synaction->avg_dx / multiplicator,
3143 		    synaction->avg_dy / multiplicator));
3144 
3145 		/* Use these averages to calculate x & y. */
3146 		synaction->squelch_x += synaction->avg_dx;
3147 		*x = synaction->squelch_x / (div_x * multiplicator);
3148 		synaction->squelch_x = synaction->squelch_x %
3149 		    (div_x * multiplicator);
3150 
3151 		synaction->squelch_y += synaction->avg_dy;
3152 		*y = synaction->squelch_y / (div_y * multiplicator);
3153 		synaction->squelch_y = synaction->squelch_y %
3154 		    (div_y * multiplicator);
3155 
3156 		if (synaction->in_vscroll) {
3157 			switch(synaction->in_vscroll) {
3158 			case 1: /* Vertical scrolling. */
3159 				if (*y != 0)
3160 					ms->button |= (*y > 0) ?
3161 					    MOUSE_BUTTON4DOWN :
3162 					    MOUSE_BUTTON5DOWN;
3163 				break;
3164 			case 2: /* Horizontal scrolling. */
3165 				if (*x != 0)
3166 					ms->button |= (*x > 0) ?
3167 					    MOUSE_BUTTON7DOWN :
3168 					    MOUSE_BUTTON6DOWN;
3169 				break;
3170 			}
3171 
3172 			/* The pointer is not moved. */
3173 			*x = *y = 0;
3174 		} else {
3175 			VLOG(3, (LOG_DEBUG, "synaptics: [%d, %d] -> [%d, %d]\n",
3176 			    dx, dy, *x, *y));
3177 		}
3178 	} else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3179 		/*
3180 		 * An action is currently taking place but the pressure
3181 		 * dropped under the minimum, putting an end to it.
3182 		 */
3183 		synapticsaction_t *synaction;
3184 		int taphold_timeout, dx, dy, tap_max_delta;
3185 
3186 		synaction = &(sc->synaction);
3187 		dx = abs(synaction->queue[synaction->queue_cursor].x -
3188 		    synaction->start_x);
3189 		dy = abs(synaction->queue[synaction->queue_cursor].y -
3190 		    synaction->start_y);
3191 
3192 		/* Max delta is disabled for multi-fingers tap. */
3193 		if (synaction->fingers_nb > 1)
3194 			tap_max_delta = imax(dx, dy);
3195 		else
3196 			tap_max_delta = sc->syninfo.tap_max_delta;
3197 
3198 		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3199 
3200 		/* Check for tap. */
3201 		VLOG(3, (LOG_DEBUG,
3202 		    "synaptics: zmax=%d, dx=%d, dy=%d, "
3203 		    "delta=%d, fingers=%d, queue=%d\n",
3204 		    sc->zmax, dx, dy, tap_max_delta, synaction->fingers_nb,
3205 		    synaction->queue_len));
3206 		if (!synaction->in_vscroll && sc->zmax >= tap_threshold &&
3207 		    timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=) &&
3208 		    dx <= tap_max_delta && dy <= tap_max_delta &&
3209 		    synaction->queue_len >= sc->syninfo.tap_min_queue) {
3210 			/*
3211 			 * We have a tap if:
3212 			 *   - the maximum pressure went over tap_threshold
3213 			 *   - the action ended before tap_timeout
3214 			 *
3215 			 * To handle tap-hold, we must delay any button push to
3216 			 * the next action.
3217 			 */
3218 			if (synaction->in_taphold) {
3219 				/*
3220 				 * This is the second and last tap of a
3221 				 * double tap action, not a tap-hold.
3222 				 */
3223 				synaction->in_taphold = 0;
3224 
3225 				/*
3226 				 * For double-tap to work:
3227 				 *   - no button press is emitted (to
3228 				 *     simulate a button release)
3229 				 *   - PSM_FLAGS_FINGERDOWN is set to
3230 				 *     force the next packet to emit a
3231 				 *     button press)
3232 				 */
3233 				VLOG(2, (LOG_DEBUG,
3234 				    "synaptics: button RELEASE: %d\n",
3235 				    synaction->tap_button));
3236 				sc->flags |= PSM_FLAGS_FINGERDOWN;
3237 			} else {
3238 				/*
3239 				 * This is the first tap: we set the
3240 				 * tap-hold state and notify the button
3241 				 * down event.
3242 				 */
3243 				synaction->in_taphold = 1;
3244 				taphold_timeout = sc->syninfo.taphold_timeout;
3245 				sc->taptimeout.tv_sec  = taphold_timeout /
3246 				    1000000;
3247 				sc->taptimeout.tv_usec = taphold_timeout %
3248 				    1000000;
3249 				timevaladd(&sc->taptimeout, &sc->lastsoftintr);
3250 
3251 				switch (synaction->fingers_nb) {
3252 				case 3:
3253 					synaction->tap_button =
3254 					    MOUSE_BUTTON2DOWN;
3255 					break;
3256 				case 2:
3257 					synaction->tap_button =
3258 					    MOUSE_BUTTON3DOWN;
3259 					break;
3260 				default:
3261 					synaction->tap_button =
3262 					    MOUSE_BUTTON1DOWN;
3263 				}
3264 				VLOG(2, (LOG_DEBUG,
3265 				    "synaptics: button PRESS: %d\n",
3266 				    synaction->tap_button));
3267 				ms->button |= synaction->tap_button;
3268 			}
3269 		} else {
3270 			/*
3271 			 * Not enough pressure or timeout: reset
3272 			 * tap-hold state.
3273 			 */
3274 			if (synaction->in_taphold) {
3275 				VLOG(2, (LOG_DEBUG,
3276 				    "synaptics: button RELEASE: %d\n",
3277 				    synaction->tap_button));
3278 				synaction->in_taphold = 0;
3279 			} else {
3280 				VLOG(2, (LOG_DEBUG,
3281 				    "synaptics: not a tap-hold\n"));
3282 			}
3283 		}
3284 	} else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) &&
3285 	    sc->synaction.in_taphold) {
3286 		/*
3287 		 * For a tap-hold to work, the button must remain down at
3288 		 * least until timeout (where the in_taphold flags will be
3289 		 * cleared) or during the next action.
3290 		 */
3291 		if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) {
3292 			ms->button |= sc->synaction.tap_button;
3293 		} else {
3294 			VLOG(2, (LOG_DEBUG,
3295 			    "synaptics: button RELEASE: %d\n",
3296 			    sc->synaction.tap_button));
3297 			sc->synaction.in_taphold = 0;
3298 		}
3299 	}
3300 
3301 SYNAPTICS_END:
3302 	/*
3303 	 * Use the extra buttons as a scrollwheel
3304 	 *
3305 	 * XXX X.Org uses the Z axis for vertical wheel only,
3306 	 * whereas moused(8) understands special values to differ
3307 	 * vertical and horizontal wheels.
3308 	 *
3309 	 * xf86-input-mouse needs therefore a small patch to
3310 	 * understand these special values. Without it, the
3311 	 * horizontal wheel acts as a vertical wheel in X.Org.
3312 	 *
3313 	 * That's why the horizontal wheel is disabled by
3314 	 * default for now.
3315 	 */
3316 
3317 	if (ms->button & MOUSE_BUTTON4DOWN) {
3318 		*z = -1;
3319 		ms->button &= ~MOUSE_BUTTON4DOWN;
3320 	} else if (ms->button & MOUSE_BUTTON5DOWN) {
3321 		*z = 1;
3322 		ms->button &= ~MOUSE_BUTTON5DOWN;
3323 	} else if (ms->button & MOUSE_BUTTON6DOWN) {
3324 		*z = -2;
3325 		ms->button &= ~MOUSE_BUTTON6DOWN;
3326 	} else if (ms->button & MOUSE_BUTTON7DOWN) {
3327 		*z = 2;
3328 		ms->button &= ~MOUSE_BUTTON7DOWN;
3329 	} else
3330 		*z = 0;
3331 
3332 	return (0);
3333 }
3334 
3335 static void
3336 proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3337     int *x, int *y, int *z)
3338 {
3339 	static int butmap_versapad[8] = {
3340 		0,
3341 		MOUSE_BUTTON3DOWN,
3342 		0,
3343 		MOUSE_BUTTON3DOWN,
3344 		MOUSE_BUTTON1DOWN,
3345 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3346 		MOUSE_BUTTON1DOWN,
3347 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
3348 	};
3349 	int c, x0, y0;
3350 
3351 	/* VersaPad PS/2 absolute mode message format
3352 	 *
3353 	 * [packet1]     7   6   5   4   3   2   1   0(LSB)
3354 	 *  ipacket[0]:  1   1   0   A   1   L   T   R
3355 	 *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
3356 	 *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
3357 	 *  ipacket[3]:  1   1   1   A   1   L   T   R
3358 	 *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
3359 	 *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
3360 	 *
3361 	 * [note]
3362 	 *  R: right physical mouse button (1=on)
3363 	 *  T: touch pad virtual button (1=tapping)
3364 	 *  L: left physical mouse button (1=on)
3365 	 *  A: position data is valid (1=valid)
3366 	 *  H: horizontal data (12bit signed integer. H11 is sign bit.)
3367 	 *  V: vertical data (12bit signed integer. V11 is sign bit.)
3368 	 *  P: pressure data
3369 	 *
3370 	 * Tapping is mapped to MOUSE_BUTTON4.
3371 	 */
3372 	c = pb->ipacket[0];
3373 	*x = *y = 0;
3374 	ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
3375 	ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
3376 	if (c & MOUSE_PS2VERSA_IN_USE) {
3377 		x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
3378 		y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
3379 		if (x0 & 0x800)
3380 			x0 -= 0x1000;
3381 		if (y0 & 0x800)
3382 			y0 -= 0x1000;
3383 		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3384 			*x = sc->xold - x0;
3385 			*y = y0 - sc->yold;
3386 			if (*x < 0)	/* XXX */
3387 				++*x;
3388 			else if (*x)
3389 				--*x;
3390 			if (*y < 0)
3391 				++*y;
3392 			else if (*y)
3393 				--*y;
3394 		} else
3395 			sc->flags |= PSM_FLAGS_FINGERDOWN;
3396 		sc->xold = x0;
3397 		sc->yold = y0;
3398 	} else
3399 		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3400 }
3401 
3402 static void
3403 psmsoftintr(void *arg)
3404 {
3405 	/*
3406 	 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
3407 	 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
3408 	 */
3409 	static int butmap[8] = {
3410 		0,
3411 		MOUSE_BUTTON1DOWN,
3412 		MOUSE_BUTTON3DOWN,
3413 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3414 		MOUSE_BUTTON2DOWN,
3415 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
3416 		MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
3417 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
3418 	};
3419 	struct psm_softc *sc = arg;
3420 	mousestatus_t ms;
3421 	packetbuf_t *pb;
3422 	int x, y, z, c, l, s;
3423 
3424 	getmicrouptime(&sc->lastsoftintr);
3425 
3426 	s = spltty();
3427 
3428 	do {
3429 		pb = &sc->pqueue[sc->pqueue_start];
3430 
3431 		if (sc->mode.level == PSM_LEVEL_NATIVE)
3432 			goto next_native;
3433 
3434 		c = pb->ipacket[0];
3435 		/*
3436 		 * A kludge for Kensington device!
3437 		 * The MSB of the horizontal count appears to be stored in
3438 		 * a strange place.
3439 		 */
3440 		if (sc->hw.model == MOUSE_MODEL_THINK)
3441 			pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
3442 
3443 		/* ignore the overflow bits... */
3444 		x = (c & MOUSE_PS2_XNEG) ?
3445 		    pb->ipacket[1] - 256 : pb->ipacket[1];
3446 		y = (c & MOUSE_PS2_YNEG) ?
3447 		    pb->ipacket[2] - 256 : pb->ipacket[2];
3448 		z = 0;
3449 		ms.obutton = sc->button;	  /* previous button state */
3450 		ms.button = butmap[c & MOUSE_PS2_BUTTONS];
3451 		/* `tapping' action */
3452 		if (sc->config & PSM_CONFIG_FORCETAP)
3453 			ms.button |= ((c & MOUSE_PS2_TAP)) ?
3454 			    0 : MOUSE_BUTTON4DOWN;
3455 
3456 		switch (sc->hw.model) {
3457 
3458 		case MOUSE_MODEL_EXPLORER:
3459 			/*
3460 			 *          b7 b6 b5 b4 b3 b2 b1 b0
3461 			 * byte 1:  oy ox sy sx 1  M  R  L
3462 			 * byte 2:  x  x  x  x  x  x  x  x
3463 			 * byte 3:  y  y  y  y  y  y  y  y
3464 			 * byte 4:  *  *  S2 S1 s  d2 d1 d0
3465 			 *
3466 			 * L, M, R, S1, S2: left, middle, right and side buttons
3467 			 * s: wheel data sign bit
3468 			 * d2-d0: wheel data
3469 			 */
3470 			z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
3471 			    (pb->ipacket[3] & 0x0f) - 16 :
3472 			    (pb->ipacket[3] & 0x0f);
3473 			ms.button |=
3474 			    (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
3475 			    MOUSE_BUTTON4DOWN : 0;
3476 			ms.button |=
3477 			    (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
3478 			    MOUSE_BUTTON5DOWN : 0;
3479 			break;
3480 
3481 		case MOUSE_MODEL_INTELLI:
3482 		case MOUSE_MODEL_NET:
3483 			/* wheel data is in the fourth byte */
3484 			z = (char)pb->ipacket[3];
3485 			/*
3486 			 * XXX some mice may send 7 when there is no Z movement?			 */
3487 			if ((z >= 7) || (z <= -7))
3488 				z = 0;
3489 			/* some compatible mice have additional buttons */
3490 			ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
3491 			    MOUSE_BUTTON4DOWN : 0;
3492 			ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
3493 			    MOUSE_BUTTON5DOWN : 0;
3494 			break;
3495 
3496 		case MOUSE_MODEL_MOUSEMANPLUS:
3497 			proc_mmanplus(sc, pb, &ms, &x, &y, &z);
3498 			break;
3499 
3500 		case MOUSE_MODEL_GLIDEPOINT:
3501 			/* `tapping' action */
3502 			ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
3503 			    MOUSE_BUTTON4DOWN;
3504 			break;
3505 
3506 		case MOUSE_MODEL_NETSCROLL:
3507 			/*
3508 			 * three addtional bytes encode buttons and
3509 			 * wheel events
3510 			 */
3511 			ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
3512 			    MOUSE_BUTTON4DOWN : 0;
3513 			ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
3514 			    MOUSE_BUTTON5DOWN : 0;
3515 			z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
3516 			    pb->ipacket[4] - 256 : pb->ipacket[4];
3517 			break;
3518 
3519 		case MOUSE_MODEL_THINK:
3520 			/* the fourth button state in the first byte */
3521 			ms.button |= (c & MOUSE_PS2_TAP) ?
3522 			    MOUSE_BUTTON4DOWN : 0;
3523 			break;
3524 
3525 		case MOUSE_MODEL_VERSAPAD:
3526 			proc_versapad(sc, pb, &ms, &x, &y, &z);
3527 			c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
3528 			    ((y < 0) ? MOUSE_PS2_YNEG : 0);
3529 			break;
3530 
3531 		case MOUSE_MODEL_4D:
3532 			/*
3533 			 *          b7 b6 b5 b4 b3 b2 b1 b0
3534 			 * byte 1:  s2 d2 s1 d1 1  M  R  L
3535 			 * byte 2:  sx x  x  x  x  x  x  x
3536 			 * byte 3:  sy y  y  y  y  y  y  y
3537 			 *
3538 			 * s1: wheel 1 direction
3539 			 * d1: wheel 1 data
3540 			 * s2: wheel 2 direction
3541 			 * d2: wheel 2 data
3542 			 */
3543 			x = (pb->ipacket[1] & 0x80) ?
3544 			    pb->ipacket[1] - 256 : pb->ipacket[1];
3545 			y = (pb->ipacket[2] & 0x80) ?
3546 			    pb->ipacket[2] - 256 : pb->ipacket[2];
3547 			switch (c & MOUSE_4D_WHEELBITS) {
3548 			case 0x10:
3549 				z = 1;
3550 				break;
3551 			case 0x30:
3552 				z = -1;
3553 				break;
3554 			case 0x40:	/* XXX 2nd wheel turning right */
3555 				z = 2;
3556 				break;
3557 			case 0xc0:	/* XXX 2nd wheel turning left */
3558 				z = -2;
3559 				break;
3560 			}
3561 			break;
3562 
3563 		case MOUSE_MODEL_4DPLUS:
3564 			if ((x < 16 - 256) && (y < 16 - 256)) {
3565 				/*
3566 				 *          b7 b6 b5 b4 b3 b2 b1 b0
3567 				 * byte 1:  0  0  1  1  1  M  R  L
3568 				 * byte 2:  0  0  0  0  1  0  0  0
3569 				 * byte 3:  0  0  0  0  S  s  d1 d0
3570 				 *
3571 				 * L, M, R, S: left, middle, right,
3572 				 *             and side buttons
3573 				 * s: wheel data sign bit
3574 				 * d1-d0: wheel data
3575 				 */
3576 				x = y = 0;
3577 				if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
3578 					ms.button |= MOUSE_BUTTON4DOWN;
3579 				z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
3580 				    ((pb->ipacket[2] & 0x07) - 8) :
3581 				    (pb->ipacket[2] & 0x07) ;
3582 			} else {
3583 				/* preserve previous button states */
3584 				ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
3585 			}
3586 			break;
3587 
3588 		case MOUSE_MODEL_SYNAPTICS:
3589 			if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0)
3590 				goto next;
3591 			break;
3592 
3593 		case MOUSE_MODEL_TRACKPOINT:
3594 		case MOUSE_MODEL_GENERIC:
3595 		default:
3596 			break;
3597 		}
3598 
3599 	/* scale values */
3600 	if (sc->mode.accelfactor >= 1) {
3601 		if (x != 0) {
3602 			x = x * x / sc->mode.accelfactor;
3603 			if (x == 0)
3604 				x = 1;
3605 			if (c & MOUSE_PS2_XNEG)
3606 				x = -x;
3607 		}
3608 		if (y != 0) {
3609 			y = y * y / sc->mode.accelfactor;
3610 			if (y == 0)
3611 				y = 1;
3612 			if (c & MOUSE_PS2_YNEG)
3613 				y = -y;
3614 		}
3615 	}
3616 
3617 	ms.dx = x;
3618 	ms.dy = y;
3619 	ms.dz = z;
3620 	ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
3621 	    (ms.obutton ^ ms.button);
3622 
3623 	pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
3624 
3625 	sc->status.flags |= ms.flags;
3626 	sc->status.dx += ms.dx;
3627 	sc->status.dy += ms.dy;
3628 	sc->status.dz += ms.dz;
3629 	sc->status.button = ms.button;
3630 	sc->button = ms.button;
3631 
3632 next_native:
3633 	sc->watchdog = FALSE;
3634 
3635 	/* queue data */
3636 	if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
3637 		l = imin(pb->inputbytes,
3638 		    sizeof(sc->queue.buf) - sc->queue.tail);
3639 		bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
3640 		if (pb->inputbytes > l)
3641 			bcopy(&pb->ipacket[l], &sc->queue.buf[0],
3642 			    pb->inputbytes - l);
3643 		sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
3644 		    sizeof(sc->queue.buf);
3645 		sc->queue.count += pb->inputbytes;
3646 	}
3647 	pb->inputbytes = 0;
3648 
3649 next:
3650 	if (++sc->pqueue_start >= PSM_PACKETQUEUE)
3651 		sc->pqueue_start = 0;
3652 	} while (sc->pqueue_start != sc->pqueue_end);
3653 
3654 	if (sc->state & PSM_ASLP) {
3655 		sc->state &= ~PSM_ASLP;
3656 		wakeup(sc);
3657 	}
3658 	selwakeuppri(&sc->rsel, PZERO);
3659 	if (sc->async != NULL) {
3660 		pgsigio(&sc->async, SIGIO, 0);
3661 	}
3662 	sc->state &= ~PSM_SOFTARMED;
3663 	splx(s);
3664 }
3665 
3666 static int
3667 psmpoll(struct cdev *dev, int events, struct thread *td)
3668 {
3669 	struct psm_softc *sc = dev->si_drv1;
3670 	int s;
3671 	int revents = 0;
3672 
3673 	/* Return true if a mouse event available */
3674 	s = spltty();
3675 	if (events & (POLLIN | POLLRDNORM)) {
3676 		if (sc->queue.count > 0)
3677 			revents |= events & (POLLIN | POLLRDNORM);
3678 		else
3679 			selrecord(td, &sc->rsel);
3680 	}
3681 	splx(s);
3682 
3683 	return (revents);
3684 }
3685 
3686 /* vendor/model specific routines */
3687 
3688 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
3689 {
3690 	if (set_mouse_resolution(kbdc, res) != res)
3691 		return (FALSE);
3692 	if (set_mouse_scaling(kbdc, scale) &&
3693 	    set_mouse_scaling(kbdc, scale) &&
3694 	    set_mouse_scaling(kbdc, scale) &&
3695 	    (get_mouse_status(kbdc, status, 0, 3) >= 3))
3696 		return (TRUE);
3697 	return (FALSE);
3698 }
3699 
3700 static int
3701 mouse_ext_command(KBDC kbdc, int command)
3702 {
3703 	int c;
3704 
3705 	c = (command >> 6) & 0x03;
3706 	if (set_mouse_resolution(kbdc, c) != c)
3707 		return (FALSE);
3708 	c = (command >> 4) & 0x03;
3709 	if (set_mouse_resolution(kbdc, c) != c)
3710 		return (FALSE);
3711 	c = (command >> 2) & 0x03;
3712 	if (set_mouse_resolution(kbdc, c) != c)
3713 		return (FALSE);
3714 	c = (command >> 0) & 0x03;
3715 	if (set_mouse_resolution(kbdc, c) != c)
3716 		return (FALSE);
3717 	return (TRUE);
3718 }
3719 
3720 #ifdef notyet
3721 /* Logitech MouseMan Cordless II */
3722 static int
3723 enable_lcordless(KDBC kbdc, struct psm_softc *sc)
3724 {
3725 	int status[3];
3726 	int ch;
3727 
3728 	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status))
3729 		return (FALSE);
3730 	if (status[1] == PSMD_RES_HIGH)
3731 		return (FALSE);
3732 	ch = (status[0] & 0x07) - 1;	/* channel # */
3733 	if ((ch <= 0) || (ch > 4))
3734 		return (FALSE);
3735 	/*
3736 	 * status[1]: always one?
3737 	 * status[2]: battery status? (0-100)
3738 	 */
3739 	return (TRUE);
3740 }
3741 #endif /* notyet */
3742 
3743 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
3744 static int
3745 enable_groller(KBDC kbdc, struct psm_softc *sc)
3746 {
3747 	int status[3];
3748 
3749 	/*
3750 	 * The special sequence to enable the fourth button and the
3751 	 * roller. Immediately after this sequence check status bytes.
3752 	 * if the mouse is NetScroll, the second and the third bytes are
3753 	 * '3' and 'D'.
3754 	 */
3755 
3756 	/*
3757 	 * If the mouse is an ordinary PS/2 mouse, the status bytes should
3758 	 * look like the following.
3759 	 *
3760 	 * byte 1 bit 7 always 0
3761 	 *        bit 6 stream mode (0)
3762 	 *        bit 5 disabled (0)
3763 	 *        bit 4 1:1 scaling (0)
3764 	 *        bit 3 always 0
3765 	 *        bit 0-2 button status
3766 	 * byte 2 resolution (PSMD_RES_HIGH)
3767 	 * byte 3 report rate (?)
3768 	 */
3769 
3770 	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
3771 		return (FALSE);
3772 	if ((status[1] != '3') || (status[2] != 'D'))
3773 		return (FALSE);
3774 	/* FIXME: SmartScroll Mouse has 5 buttons! XXX */
3775 	if (sc != NULL)
3776 		sc->hw.buttons = 4;
3777 	return (TRUE);
3778 }
3779 
3780 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
3781 static int
3782 enable_gmouse(KBDC kbdc, struct psm_softc *sc)
3783 {
3784 	int status[3];
3785 
3786 	/*
3787 	 * The special sequence to enable the middle, "rubber" button.
3788 	 * Immediately after this sequence check status bytes.
3789 	 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
3790 	 * the second and the third bytes are '3' and 'U'.
3791 	 * NOTE: NetMouse reports that it has three buttons although it has
3792 	 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
3793 	 * say they have three buttons too and they do have a button on the
3794 	 * side...
3795 	 */
3796 	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
3797 		return (FALSE);
3798 	if ((status[1] != '3') || (status[2] != 'U'))
3799 		return (FALSE);
3800 	return (TRUE);
3801 }
3802 
3803 /* ALPS GlidePoint */
3804 static int
3805 enable_aglide(KBDC kbdc, struct psm_softc *sc)
3806 {
3807 	int status[3];
3808 
3809 	/*
3810 	 * The special sequence to obtain ALPS GlidePoint specific
3811 	 * information. Immediately after this sequence, status bytes will
3812 	 * contain something interesting.
3813 	 * NOTE: ALPS produces several models of GlidePoint. Some of those
3814 	 * do not respond to this sequence, thus, cannot be detected this way.
3815 	 */
3816 	if (set_mouse_sampling_rate(kbdc, 100) != 100)
3817 		return (FALSE);
3818 	if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
3819 		return (FALSE);
3820 	if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
3821 		return (FALSE);
3822 	return (TRUE);
3823 }
3824 
3825 /* Kensington ThinkingMouse/Trackball */
3826 static int
3827 enable_kmouse(KBDC kbdc, struct psm_softc *sc)
3828 {
3829 	static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
3830 	int status[3];
3831 	int id1;
3832 	int id2;
3833 	int i;
3834 
3835 	id1 = get_aux_id(kbdc);
3836 	if (set_mouse_sampling_rate(kbdc, 10) != 10)
3837 		return (FALSE);
3838 	/*
3839 	 * The device is now in the native mode? It returns a different
3840 	 * ID value...
3841 	 */
3842 	id2 = get_aux_id(kbdc);
3843 	if ((id1 == id2) || (id2 != 2))
3844 		return (FALSE);
3845 
3846 	if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
3847 		return (FALSE);
3848 #if PSM_DEBUG >= 2
3849 	/* at this point, resolution is LOW, sampling rate is 10/sec */
3850 	if (get_mouse_status(kbdc, status, 0, 3) < 3)
3851 		return (FALSE);
3852 #endif
3853 
3854 	/*
3855 	 * The special sequence to enable the third and fourth buttons.
3856 	 * Otherwise they behave like the first and second buttons.
3857 	 */
3858 	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3859 		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3860 			return (FALSE);
3861 
3862 	/*
3863 	 * At this point, the device is using default resolution and
3864 	 * sampling rate for the native mode.
3865 	 */
3866 	if (get_mouse_status(kbdc, status, 0, 3) < 3)
3867 		return (FALSE);
3868 	if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
3869 		return (FALSE);
3870 
3871 	/* the device appears be enabled by this sequence, diable it for now */
3872 	disable_aux_dev(kbdc);
3873 	empty_aux_buffer(kbdc, 5);
3874 
3875 	return (TRUE);
3876 }
3877 
3878 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
3879 static int
3880 enable_mmanplus(KBDC kbdc, struct psm_softc *sc)
3881 {
3882 	int data[3];
3883 
3884 	/* the special sequence to enable the fourth button and the roller. */
3885 	/*
3886 	 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
3887 	 * must be called exactly three times since the last RESET command
3888 	 * before this sequence. XXX
3889 	 */
3890 	if (!set_mouse_scaling(kbdc, 1))
3891 		return (FALSE);
3892 	if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
3893 		return (FALSE);
3894 	if (get_mouse_status(kbdc, data, 1, 3) < 3)
3895 		return (FALSE);
3896 
3897 	/*
3898 	 * PS2++ protocol, packet type 0
3899 	 *
3900 	 *          b7 b6 b5 b4 b3 b2 b1 b0
3901 	 * byte 1:  *  1  p3 p2 1  *  *  *
3902 	 * byte 2:  1  1  p1 p0 m1 m0 1  0
3903 	 * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
3904 	 *
3905 	 * p3-p0: packet type: 0
3906 	 * m7-m0: model ID: MouseMan+:0x50,
3907 	 *		    FirstMouse+:0x51,
3908 	 *		    ScrollPoint:0x58...
3909 	 */
3910 	/* check constant bits */
3911 	if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
3912 		return (FALSE);
3913 	if ((data[1] & 0xc3) != 0xc2)
3914 		return (FALSE);
3915 	/* check d3-d0 in byte 2 */
3916 	if (!MOUSE_PS2PLUS_CHECKBITS(data))
3917 		return (FALSE);
3918 	/* check p3-p0 */
3919 	if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
3920 		return (FALSE);
3921 
3922 	if (sc != NULL) {
3923 		sc->hw.hwid &= 0x00ff;
3924 		sc->hw.hwid |= data[2] << 8;	/* save model ID */
3925 	}
3926 
3927 	/*
3928 	 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
3929 	 * the wheel and the fourth button events are encoded in the
3930 	 * special data packet. The mouse may be put in the IntelliMouse mode
3931 	 * if it is initialized by the IntelliMouse's method.
3932 	 */
3933 	return (TRUE);
3934 }
3935 
3936 /* MS IntelliMouse Explorer */
3937 static int
3938 enable_msexplorer(KBDC kbdc, struct psm_softc *sc)
3939 {
3940 	static u_char rate0[] = { 200, 100, 80, };
3941 	static u_char rate1[] = { 200, 200, 80, };
3942 	int id;
3943 	int i;
3944 
3945 	/*
3946 	 * This is needed for at least A4Tech X-7xx mice - they do not go
3947 	 * straight to Explorer mode, but need to be set to Intelli mode
3948 	 * first.
3949 	 */
3950 	enable_msintelli(kbdc, sc);
3951 
3952 	/* the special sequence to enable the extra buttons and the roller. */
3953 	for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i)
3954 		if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
3955 			return (FALSE);
3956 	/* the device will give the genuine ID only after the above sequence */
3957 	id = get_aux_id(kbdc);
3958 	if (id != PSM_EXPLORER_ID)
3959 		return (FALSE);
3960 
3961 	if (sc != NULL) {
3962 		sc->hw.buttons = 5;	/* IntelliMouse Explorer XXX */
3963 		sc->hw.hwid = id;
3964 	}
3965 
3966 	/*
3967 	 * XXX: this is a kludge to fool some KVM switch products
3968 	 * which think they are clever enough to know the 4-byte IntelliMouse
3969 	 * protocol, and assume any other protocols use 3-byte packets.
3970 	 * They don't convey 4-byte data packets from the IntelliMouse Explorer
3971 	 * correctly to the host computer because of this!
3972 	 * The following sequence is actually IntelliMouse's "wake up"
3973 	 * sequence; it will make the KVM think the mouse is IntelliMouse
3974 	 * when it is in fact IntelliMouse Explorer.
3975 	 */
3976 	for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i)
3977 		if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
3978 			break;
3979 	get_aux_id(kbdc);
3980 
3981 	return (TRUE);
3982 }
3983 
3984 /* MS IntelliMouse */
3985 static int
3986 enable_msintelli(KBDC kbdc, struct psm_softc *sc)
3987 {
3988 	/*
3989 	 * Logitech MouseMan+ and FirstMouse+ will also respond to this
3990 	 * probe routine and act like IntelliMouse.
3991 	 */
3992 
3993 	static u_char rate[] = { 200, 100, 80, };
3994 	int id;
3995 	int i;
3996 
3997 	/* the special sequence to enable the third button and the roller. */
3998 	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3999 		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4000 			return (FALSE);
4001 	/* the device will give the genuine ID only after the above sequence */
4002 	id = get_aux_id(kbdc);
4003 	if (id != PSM_INTELLI_ID)
4004 		return (FALSE);
4005 
4006 	if (sc != NULL) {
4007 		sc->hw.buttons = 3;
4008 		sc->hw.hwid = id;
4009 	}
4010 
4011 	return (TRUE);
4012 }
4013 
4014 /* A4 Tech 4D Mouse */
4015 static int
4016 enable_4dmouse(KBDC kbdc, struct psm_softc *sc)
4017 {
4018 	/*
4019 	 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
4020 	 */
4021 
4022 	static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
4023 	int id;
4024 	int i;
4025 
4026 	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
4027 		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4028 			return (FALSE);
4029 	id = get_aux_id(kbdc);
4030 	/*
4031 	 * WinEasy 4D, 4 Way Scroll 4D: 6
4032 	 * Cable-Free 4D: 8 (4DPLUS)
4033 	 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
4034 	 */
4035 	if (id != PSM_4DMOUSE_ID)
4036 		return (FALSE);
4037 
4038 	if (sc != NULL) {
4039 		sc->hw.buttons = 3;	/* XXX some 4D mice have 4? */
4040 		sc->hw.hwid = id;
4041 	}
4042 
4043 	return (TRUE);
4044 }
4045 
4046 /* A4 Tech 4D+ Mouse */
4047 static int
4048 enable_4dplus(KBDC kbdc, struct psm_softc *sc)
4049 {
4050 	/*
4051 	 * Newer wheel mice from A4 Tech seem to use this protocol.
4052 	 * Older models are recognized as either 4D Mouse or IntelliMouse.
4053 	 */
4054 	int id;
4055 
4056 	/*
4057 	 * enable_4dmouse() already issued the following ID sequence...
4058 	static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
4059 	int i;
4060 
4061 	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
4062 		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4063 			return (FALSE);
4064 	*/
4065 
4066 	id = get_aux_id(kbdc);
4067 	switch (id) {
4068 	case PSM_4DPLUS_ID:
4069 		break;
4070 	case PSM_4DPLUS_RFSW35_ID:
4071 		break;
4072 	default:
4073 		return (FALSE);
4074 	}
4075 
4076 	if (sc != NULL) {
4077 		sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
4078 		sc->hw.hwid = id;
4079 	}
4080 
4081 	return (TRUE);
4082 }
4083 
4084 /* Synaptics Touchpad */
4085 static int
4086 synaptics_sysctl(SYSCTL_HANDLER_ARGS)
4087 {
4088 	int error, arg;
4089 
4090 	/* Read the current value. */
4091 	arg = *(int *)oidp->oid_arg1;
4092 	error = sysctl_handle_int(oidp, &arg, 0, req);
4093 
4094 	/* Sanity check. */
4095 	if (error || !req->newptr)
4096 		return (error);
4097 
4098 	/*
4099 	 * Check that the new value is in the concerned node's range
4100 	 * of values.
4101 	 */
4102 	switch (oidp->oid_arg2) {
4103 	case SYNAPTICS_SYSCTL_MIN_PRESSURE:
4104 	case SYNAPTICS_SYSCTL_MAX_PRESSURE:
4105 		if (arg < 0 || arg > 255)
4106 			return (EINVAL);
4107 		break;
4108 	case SYNAPTICS_SYSCTL_MAX_WIDTH:
4109 		if (arg < 4 || arg > 15)
4110 			return (EINVAL);
4111 		break;
4112 	case SYNAPTICS_SYSCTL_MARGIN_TOP:
4113 	case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
4114 	case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
4115 	case SYNAPTICS_SYSCTL_MARGIN_LEFT:
4116 	case SYNAPTICS_SYSCTL_NA_TOP:
4117 	case SYNAPTICS_SYSCTL_NA_RIGHT:
4118 	case SYNAPTICS_SYSCTL_NA_BOTTOM:
4119 	case SYNAPTICS_SYSCTL_NA_LEFT:
4120 		if (arg < 0 || arg > 6143)
4121 			return (EINVAL);
4122 		break;
4123 	case SYNAPTICS_SYSCTL_WINDOW_MIN:
4124 	case SYNAPTICS_SYSCTL_WINDOW_MAX:
4125 	case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
4126 		if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
4127 			return (EINVAL);
4128 		break;
4129 	case SYNAPTICS_SYSCTL_MULTIPLICATOR:
4130 	case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
4131 	case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
4132 	case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
4133 	case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
4134 	case SYNAPTICS_SYSCTL_DIV_MIN:
4135 	case SYNAPTICS_SYSCTL_DIV_MAX:
4136 	case SYNAPTICS_SYSCTL_DIV_MAX_NA:
4137 	case SYNAPTICS_SYSCTL_DIV_LEN:
4138 	case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
4139 	case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
4140 		if (arg < 1)
4141 			return (EINVAL);
4142 		break;
4143 	case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
4144 	case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
4145 	case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
4146 		if (arg < 0)
4147 			return (EINVAL);
4148 		break;
4149 	case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
4150 	case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
4151 		if (arg < -6143 || arg > 6143)
4152 			return (EINVAL);
4153 		break;
4154         case SYNAPTICS_SYSCTL_TOUCHPAD_OFF:
4155 		if (arg < 0 || arg > 1)
4156 			return (EINVAL);
4157 		break;
4158 	default:
4159 		return (EINVAL);
4160 	}
4161 
4162 	/* Update. */
4163 	*(int *)oidp->oid_arg1 = arg;
4164 
4165 	return (error);
4166 }
4167 
4168 static void
4169 synaptics_sysctl_create_tree(struct psm_softc *sc)
4170 {
4171 
4172 	if (sc->syninfo.sysctl_tree != NULL)
4173 		return;
4174 
4175 	/* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
4176 	sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
4177 	sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
4178 	    SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "synaptics", CTLFLAG_RD,
4179 	    0, "Synaptics TouchPad");
4180 
4181 	/* hw.psm.synaptics.directional_scrolls. */
4182 	sc->syninfo.directional_scrolls = 0;
4183 	SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
4184 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4185 	    "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
4186 	    &sc->syninfo.directional_scrolls, 0,
4187 	    "Enable hardware scrolling pad (if non-zero) or register it as "
4188 	    "extended buttons (if 0)");
4189 
4190 	/*
4191 	 * Turn off two finger scroll if we have a
4192 	 * physical area reserved for scrolling or when
4193 	 * there's no multi finger support.
4194 	 */
4195 	if (sc->synhw.verticalScroll || sc->synhw.capMultiFinger == 0)
4196 		sc->syninfo.two_finger_scroll = 0;
4197 	else
4198 		sc->syninfo.two_finger_scroll = 1;
4199 	/* hw.psm.synaptics.two_finger_scroll. */
4200 	SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
4201 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4202 	    "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY,
4203 	    &sc->syninfo.two_finger_scroll, 0,
4204 	    "Enable two finger scrolling");
4205 
4206 	/* hw.psm.synaptics.min_pressure. */
4207 	sc->syninfo.min_pressure = 16;
4208 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4209 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4210 	    "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4211 	    &sc->syninfo.min_pressure, SYNAPTICS_SYSCTL_MIN_PRESSURE,
4212 	    synaptics_sysctl, "I",
4213 	    "Minimum pressure required to start an action");
4214 
4215 	/* hw.psm.synaptics.max_pressure. */
4216 	sc->syninfo.max_pressure = 220;
4217 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4218 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4219 	    "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4220 	    &sc->syninfo.max_pressure, SYNAPTICS_SYSCTL_MAX_PRESSURE,
4221 	    synaptics_sysctl, "I",
4222 	    "Maximum pressure to detect palm");
4223 
4224 	/* hw.psm.synaptics.max_width. */
4225 	sc->syninfo.max_width = 10;
4226 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4227 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4228 	    "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4229 	    &sc->syninfo.max_width, SYNAPTICS_SYSCTL_MAX_WIDTH,
4230 	    synaptics_sysctl, "I",
4231 	    "Maximum finger width to detect palm");
4232 
4233 	/* hw.psm.synaptics.top_margin. */
4234 	sc->syninfo.margin_top = 200;
4235 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4236 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4237 	    "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4238 	    &sc->syninfo.margin_top, SYNAPTICS_SYSCTL_MARGIN_TOP,
4239 	    synaptics_sysctl, "I",
4240 	    "Top margin");
4241 
4242 	/* hw.psm.synaptics.right_margin. */
4243 	sc->syninfo.margin_right = 200;
4244 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4245 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4246 	    "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4247 	    &sc->syninfo.margin_right, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
4248 	    synaptics_sysctl, "I",
4249 	    "Right margin");
4250 
4251 	/* hw.psm.synaptics.bottom_margin. */
4252 	sc->syninfo.margin_bottom = 200;
4253 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4254 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4255 	    "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4256 	    &sc->syninfo.margin_bottom, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
4257 	    synaptics_sysctl, "I",
4258 	    "Bottom margin");
4259 
4260 	/* hw.psm.synaptics.left_margin. */
4261 	sc->syninfo.margin_left = 200;
4262 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4263 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4264 	    "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4265 	    &sc->syninfo.margin_left, SYNAPTICS_SYSCTL_MARGIN_LEFT,
4266 	    synaptics_sysctl, "I",
4267 	    "Left margin");
4268 
4269 	/* hw.psm.synaptics.na_top. */
4270 	sc->syninfo.na_top = 1783;
4271 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4272 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4273 	    "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4274 	    &sc->syninfo.na_top, SYNAPTICS_SYSCTL_NA_TOP,
4275 	    synaptics_sysctl, "I",
4276 	    "Top noisy area, where weight_previous_na is used instead "
4277 	    "of weight_previous");
4278 
4279 	/* hw.psm.synaptics.na_right. */
4280 	sc->syninfo.na_right = 563;
4281 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4282 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4283 	    "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4284 	    &sc->syninfo.na_right, SYNAPTICS_SYSCTL_NA_RIGHT,
4285 	    synaptics_sysctl, "I",
4286 	    "Right noisy area, where weight_previous_na is used instead "
4287 	    "of weight_previous");
4288 
4289 	/* hw.psm.synaptics.na_bottom. */
4290 	sc->syninfo.na_bottom = 1408;
4291 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4292 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4293 	    "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4294 	    &sc->syninfo.na_bottom, SYNAPTICS_SYSCTL_NA_BOTTOM,
4295 	    synaptics_sysctl, "I",
4296 	    "Bottom noisy area, where weight_previous_na is used instead "
4297 	    "of weight_previous");
4298 
4299 	/* hw.psm.synaptics.na_left. */
4300 	sc->syninfo.na_left = 1600;
4301 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4302 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4303 	    "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4304 	    &sc->syninfo.na_left, SYNAPTICS_SYSCTL_NA_LEFT,
4305 	    synaptics_sysctl, "I",
4306 	    "Left noisy area, where weight_previous_na is used instead "
4307 	    "of weight_previous");
4308 
4309 	/* hw.psm.synaptics.window_min. */
4310 	sc->syninfo.window_min = 4;
4311 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4312 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4313 	    "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4314 	    &sc->syninfo.window_min, SYNAPTICS_SYSCTL_WINDOW_MIN,
4315 	    synaptics_sysctl, "I",
4316 	    "Minimum window size to start an action");
4317 
4318 	/* hw.psm.synaptics.window_max. */
4319 	sc->syninfo.window_max = 10;
4320 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4321 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4322 	    "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4323 	    &sc->syninfo.window_max, SYNAPTICS_SYSCTL_WINDOW_MAX,
4324 	    synaptics_sysctl, "I",
4325 	    "Maximum window size");
4326 
4327 	/* hw.psm.synaptics.multiplicator. */
4328 	sc->syninfo.multiplicator = 10000;
4329 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4330 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4331 	    "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4332 	    &sc->syninfo.multiplicator, SYNAPTICS_SYSCTL_MULTIPLICATOR,
4333 	    synaptics_sysctl, "I",
4334 	    "Multiplicator to increase precision in averages and divisions");
4335 
4336 	/* hw.psm.synaptics.weight_current. */
4337 	sc->syninfo.weight_current = 3;
4338 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4339 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4340 	    "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4341 	    &sc->syninfo.weight_current, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
4342 	    synaptics_sysctl, "I",
4343 	    "Weight of the current movement in the new average");
4344 
4345 	/* hw.psm.synaptics.weight_previous. */
4346 	sc->syninfo.weight_previous = 6;
4347 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4348 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4349 	    "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4350 	    &sc->syninfo.weight_previous, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
4351 	    synaptics_sysctl, "I",
4352 	    "Weight of the previous average");
4353 
4354 	/* hw.psm.synaptics.weight_previous_na. */
4355 	sc->syninfo.weight_previous_na = 20;
4356 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4357 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4358 	    "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4359 	    &sc->syninfo.weight_previous_na,
4360 	    SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
4361 	    synaptics_sysctl, "I",
4362 	    "Weight of the previous average (inside the noisy area)");
4363 
4364 	/* hw.psm.synaptics.weight_len_squared. */
4365 	sc->syninfo.weight_len_squared = 2000;
4366 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4367 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4368 	    "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4369 	    &sc->syninfo.weight_len_squared,
4370 	    SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
4371 	    synaptics_sysctl, "I",
4372 	    "Length (squared) of segments where weight_previous "
4373 	    "starts to decrease");
4374 
4375 	/* hw.psm.synaptics.div_min. */
4376 	sc->syninfo.div_min = 9;
4377 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4378 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4379 	    "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4380 	    &sc->syninfo.div_min, SYNAPTICS_SYSCTL_DIV_MIN,
4381 	    synaptics_sysctl, "I",
4382 	    "Divisor for fast movements");
4383 
4384 	/* hw.psm.synaptics.div_max. */
4385 	sc->syninfo.div_max = 17;
4386 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4387 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4388 	    "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4389 	    &sc->syninfo.div_max, SYNAPTICS_SYSCTL_DIV_MAX,
4390 	    synaptics_sysctl, "I",
4391 	    "Divisor for slow movements");
4392 
4393 	/* hw.psm.synaptics.div_max_na. */
4394 	sc->syninfo.div_max_na = 30;
4395 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4396 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4397 	    "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4398 	    &sc->syninfo.div_max_na, SYNAPTICS_SYSCTL_DIV_MAX_NA,
4399 	    synaptics_sysctl, "I",
4400 	    "Divisor with slow movements (inside the noisy area)");
4401 
4402 	/* hw.psm.synaptics.div_len. */
4403 	sc->syninfo.div_len = 100;
4404 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4405 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4406 	    "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4407 	    &sc->syninfo.div_len, SYNAPTICS_SYSCTL_DIV_LEN,
4408 	    synaptics_sysctl, "I",
4409 	    "Length of segments where div_max starts to decrease");
4410 
4411 	/* hw.psm.synaptics.tap_max_delta. */
4412 	sc->syninfo.tap_max_delta = 80;
4413 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4414 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4415 	    "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4416 	    &sc->syninfo.tap_max_delta, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
4417 	    synaptics_sysctl, "I",
4418 	    "Length of segments above which a tap is ignored");
4419 
4420 	/* hw.psm.synaptics.tap_min_queue. */
4421 	sc->syninfo.tap_min_queue = 2;
4422 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4423 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4424 	    "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4425 	    &sc->syninfo.tap_min_queue, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
4426 	    synaptics_sysctl, "I",
4427 	    "Number of packets required to consider a tap");
4428 
4429 	/* hw.psm.synaptics.taphold_timeout. */
4430 	sc->synaction.in_taphold = 0;
4431 	sc->syninfo.taphold_timeout = tap_timeout;
4432 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4433 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4434 	    "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4435 	    &sc->syninfo.taphold_timeout, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
4436 	    synaptics_sysctl, "I",
4437 	    "Maximum elapsed time between two taps to consider a tap-hold "
4438 	    "action");
4439 
4440 	/* hw.psm.synaptics.vscroll_hor_area. */
4441 	sc->syninfo.vscroll_hor_area = 0; /* 1300 */
4442 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4443 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4444 	    "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4445 	    &sc->syninfo.vscroll_hor_area, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
4446 	    synaptics_sysctl, "I",
4447 	    "Area reserved for horizontal virtual scrolling");
4448 
4449 	/* hw.psm.synaptics.vscroll_ver_area. */
4450 	sc->syninfo.vscroll_ver_area = -600;
4451 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4452 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4453 	    "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4454 	    &sc->syninfo.vscroll_ver_area, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
4455 	    synaptics_sysctl, "I",
4456 	    "Area reserved for vertical virtual scrolling");
4457 
4458 	/* hw.psm.synaptics.vscroll_min_delta. */
4459 	sc->syninfo.vscroll_min_delta = 50;
4460 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4461 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4462 	    "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4463 	    &sc->syninfo.vscroll_min_delta,
4464 	    SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
4465 	    synaptics_sysctl, "I",
4466 	    "Minimum movement to consider virtual scrolling");
4467 
4468 	/* hw.psm.synaptics.vscroll_div_min. */
4469 	sc->syninfo.vscroll_div_min = 100;
4470 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4471 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4472 	    "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4473 	    &sc->syninfo.vscroll_div_min, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
4474 	    synaptics_sysctl, "I",
4475 	    "Divisor for fast scrolling");
4476 
4477 	/* hw.psm.synaptics.vscroll_div_min. */
4478 	sc->syninfo.vscroll_div_max = 150;
4479 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4480 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4481 	    "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4482 	    &sc->syninfo.vscroll_div_max, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
4483 	    synaptics_sysctl, "I",
4484 	    "Divisor for slow scrolling");
4485 
4486 	/* hw.psm.synaptics.touchpad_off. */
4487 	sc->syninfo.touchpad_off = 0;
4488 	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4489 	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4490 	    "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4491 	    &sc->syninfo.touchpad_off, SYNAPTICS_SYSCTL_TOUCHPAD_OFF,
4492 	    synaptics_sysctl, "I",
4493 	    "Turn off touchpad");
4494 }
4495 
4496 static int
4497 enable_synaptics(KBDC kbdc, struct psm_softc *sc)
4498 {
4499 	synapticshw_t synhw;
4500 	int status[3];
4501 	int buttons;
4502 
4503 	VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
4504 
4505 	/*
4506 	 * Just to be on the safe side: this avoids troubles with
4507 	 * following mouse_ext_command() when the previous command
4508 	 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
4509 	 * Synaptics Touchpad behaviour.
4510 	 */
4511 	set_mouse_scaling(kbdc, 1);
4512 
4513 	/* Identify the Touchpad version. */
4514 	if (mouse_ext_command(kbdc, 0) == 0)
4515 		return (FALSE);
4516 	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4517 		return (FALSE);
4518 	if (status[1] != 0x47)
4519 		return (FALSE);
4520 
4521 	bzero(&synhw, sizeof(synhw));
4522 	synhw.infoMinor = status[0];
4523 	synhw.infoMajor = status[2] & 0x0f;
4524 
4525 	if (verbose >= 2)
4526 		printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
4527 		    synhw.infoMinor);
4528 
4529 	if (synhw.infoMajor < 4) {
4530 		printf("  Unsupported (pre-v4) Touchpad detected\n");
4531 		return (FALSE);
4532 	}
4533 
4534 	/* Get the Touchpad model information. */
4535 	if (mouse_ext_command(kbdc, 3) == 0)
4536 		return (FALSE);
4537 	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4538 		return (FALSE);
4539 	if ((status[1] & 0x01) != 0) {
4540 		printf("  Failed to read model information\n");
4541 		return (FALSE);
4542 	}
4543 
4544 	synhw.infoRot180   = (status[0] & 0x80) != 0;
4545 	synhw.infoPortrait = (status[0] & 0x40) != 0;
4546 	synhw.infoSensor   =  status[0] & 0x3f;
4547 	synhw.infoHardware = (status[1] & 0xfe) >> 1;
4548 	synhw.infoNewAbs   = (status[2] & 0x80) != 0;
4549 	synhw.capPen       = (status[2] & 0x40) != 0;
4550 	synhw.infoSimplC   = (status[2] & 0x20) != 0;
4551 	synhw.infoGeometry =  status[2] & 0x0f;
4552 
4553 	if (verbose >= 2) {
4554 		printf("  Model information:\n");
4555 		printf("   infoRot180: %d\n", synhw.infoRot180);
4556 		printf("   infoPortrait: %d\n", synhw.infoPortrait);
4557 		printf("   infoSensor: %d\n", synhw.infoSensor);
4558 		printf("   infoHardware: %d\n", synhw.infoHardware);
4559 		printf("   infoNewAbs: %d\n", synhw.infoNewAbs);
4560 		printf("   capPen: %d\n", synhw.capPen);
4561 		printf("   infoSimplC: %d\n", synhw.infoSimplC);
4562 		printf("   infoGeometry: %d\n", synhw.infoGeometry);
4563 	}
4564 
4565 	/* Read the extended capability bits. */
4566 	if (mouse_ext_command(kbdc, 2) == 0)
4567 		return (FALSE);
4568 	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4569 		return (FALSE);
4570 	if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
4571 		printf("  Failed to read extended capability bits\n");
4572 		return (FALSE);
4573 	}
4574 
4575 	/* Set the different capabilities when they exist. */
4576 	buttons = 0;
4577 	synhw.capExtended = (status[0] & 0x80) != 0;
4578 	if (synhw.capExtended) {
4579 		synhw.nExtendedQueries = (status[0] & 0x70) != 0;
4580 		synhw.capMiddle        = (status[0] & 0x04) != 0;
4581 		synhw.capPassthrough   = (status[2] & 0x80) != 0;
4582 		synhw.capLowPower      = (status[2] & 0x40) != 0;
4583 		synhw.capMultiFingerReport =
4584 					 (status[2] & 0x20) != 0;
4585 		synhw.capSleep         = (status[2] & 0x10) != 0;
4586 		synhw.capFourButtons   = (status[2] & 0x08) != 0;
4587 		synhw.capBallistics    = (status[2] & 0x04) != 0;
4588 		synhw.capMultiFinger   = (status[2] & 0x02) != 0;
4589 		synhw.capPalmDetect    = (status[2] & 0x01) != 0;
4590 
4591 		if (verbose >= 2) {
4592 			printf("  Extended capabilities:\n");
4593 			printf("   capExtended: %d\n", synhw.capExtended);
4594 			printf("   capMiddle: %d\n", synhw.capMiddle);
4595 			printf("   nExtendedQueries: %d\n",
4596 			    synhw.nExtendedQueries);
4597 			printf("   capPassthrough: %d\n", synhw.capPassthrough);
4598 			printf("   capLowPower: %d\n", synhw.capLowPower);
4599 			printf("   capMultiFingerReport: %d\n",
4600 			    synhw.capMultiFingerReport);
4601 			printf("   capSleep: %d\n", synhw.capSleep);
4602 			printf("   capFourButtons: %d\n", synhw.capFourButtons);
4603 			printf("   capBallistics: %d\n", synhw.capBallistics);
4604 			printf("   capMultiFinger: %d\n", synhw.capMultiFinger);
4605 			printf("   capPalmDetect: %d\n", synhw.capPalmDetect);
4606 		}
4607 
4608 		/*
4609 		 * If nExtendedQueries is 1 or greater, then the TouchPad
4610 		 * supports this number of extended queries. We can load
4611 		 * more information about buttons using query 0x09.
4612 		 */
4613 		if (synhw.capExtended && synhw.nExtendedQueries) {
4614 			if (mouse_ext_command(kbdc, 0x09) == 0)
4615 				return (FALSE);
4616 			if (get_mouse_status(kbdc, status, 0, 3) != 3)
4617 				return (FALSE);
4618 			synhw.verticalScroll   = (status[0] & 0x01) != 0;
4619 			synhw.horizontalScroll = (status[0] & 0x02) != 0;
4620 			synhw.verticalWheel    = (status[0] & 0x08) != 0;
4621 			synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
4622 			if (verbose >= 2) {
4623 				printf("  Extended model ID:\n");
4624 				printf("   verticalScroll: %d\n",
4625 				    synhw.verticalScroll);
4626 				printf("   horizontalScroll: %d\n",
4627 				    synhw.horizontalScroll);
4628 				printf("   verticalWheel: %d\n",
4629 				    synhw.verticalWheel);
4630 				printf("   nExtendedButtons: %d\n",
4631 				    synhw.nExtendedButtons);
4632 			}
4633 			/*
4634 			 * Add the number of extended buttons to the total
4635 			 * button support count, including the middle button
4636 			 * if capMiddle support bit is set.
4637 			 */
4638 			buttons = synhw.nExtendedButtons + synhw.capMiddle;
4639 		} else
4640 			/*
4641 			 * If the capFourButtons support bit is set,
4642 			 * add a fourth button to the total button count.
4643 			 */
4644 			buttons = synhw.capFourButtons ? 1 : 0;
4645 	}
4646 	if (verbose >= 2) {
4647 		if (synhw.capExtended)
4648 			printf("  Additional Buttons: %d\n", buttons);
4649 		else
4650 			printf("  No extended capabilities\n");
4651 	}
4652 
4653 	/* Read the continued capabilities bits. */
4654 	if (mouse_ext_command(kbdc, 0xc) != 0 &&
4655 	    get_mouse_status(kbdc, status, 0, 3) == 3) {
4656 		synhw.capClickPad         = (status[1] & 0x01) << 1;
4657 		synhw.capClickPad        |= (status[0] & 0x10) != 0;
4658 		synhw.capDeluxeLEDs       = (status[1] & 0x02) != 0;
4659 		synhw.noAbsoluteFilter    = (status[1] & 0x04) != 0;
4660 		synhw.capReportsV         = (status[1] & 0x08) != 0;
4661 		synhw.capUniformClickPad  = (status[1] & 0x10) != 0;
4662 		synhw.capReportsMin       = (status[1] & 0x20) != 0;
4663 		synhw.capInterTouch       = (status[1] & 0x40) != 0;
4664 		synhw.capReportsMax       = (status[2] & 0x02) != 0;
4665 		synhw.capClearPad         = (status[2] & 0x04) != 0;
4666 		synhw.capAdvancedGestures = (status[2] & 0x08) != 0;
4667 		synhw.capCoveredPad       = (status[2] & 0x80) != 0;
4668 
4669 		if (verbose >= 2) {
4670 			printf("  Continued capabilities:\n");
4671 			printf("   capClickPad: %d\n", synhw.capClickPad);
4672 			printf("   capDeluxeLEDs: %d\n", synhw.capDeluxeLEDs);
4673 			printf("   noAbsoluteFilter: %d\n",
4674 			    synhw.noAbsoluteFilter);
4675 			printf("   capReportsV: %d\n", synhw.capReportsV);
4676 			printf("   capUniformClickPad: %d\n",
4677 			    synhw.capUniformClickPad);
4678 			printf("   capReportsMin: %d\n", synhw.capReportsMin);
4679 			printf("   capInterTouch: %d\n", synhw.capInterTouch);
4680 			printf("   capReportsMax: %d\n", synhw.capReportsMax);
4681 			printf("   capClearPad: %d\n", synhw.capClearPad);
4682 			printf("   capAdvancedGestures: %d\n",
4683 			    synhw.capAdvancedGestures);
4684 			printf("   capCoveredPad: %d\n", synhw.capCoveredPad);
4685 		}
4686 		buttons += synhw.capClickPad;
4687 	}
4688 
4689 	/*
4690 	 * Add the default number of 3 buttons to the total
4691 	 * count of supported buttons reported above.
4692 	 */
4693 	buttons += 3;
4694 
4695 	/*
4696 	 * Read the mode byte.
4697 	 *
4698 	 * XXX: Note the Synaptics documentation also defines the first
4699 	 * byte of the response to this query to be a constant 0x3b, this
4700 	 * does not appear to be true for Touchpads with guest devices.
4701 	 */
4702 	if (mouse_ext_command(kbdc, 1) == 0)
4703 		return (FALSE);
4704 	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4705 		return (FALSE);
4706 	if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
4707 		printf("  Failed to read mode byte\n");
4708 		return (FALSE);
4709 	}
4710 
4711 	if (sc != NULL)
4712 		sc->synhw = synhw;
4713 	if (!synaptics_support)
4714 		return (FALSE);
4715 
4716 	/* Set the mode byte; request wmode where available. */
4717 	mouse_ext_command(kbdc, synhw.capExtended ? 0xc1 : 0xc0);
4718 
4719 	/* "Commit" the Set Mode Byte command sent above. */
4720 	set_mouse_sampling_rate(kbdc, 20);
4721 
4722 	VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
4723 
4724 	if (sc != NULL) {
4725 		if (trackpoint_support && synhw.capPassthrough) {
4726 			synaptics_passthrough_on(sc);
4727 			enable_trackpoint(kbdc, sc);
4728 			synaptics_passthrough_off(sc);
4729 		}
4730 		/* Create sysctl tree. */
4731 		synaptics_sysctl_create_tree(sc);
4732 		sc->hw.buttons = buttons;
4733 	}
4734 
4735 	return (TRUE);
4736 }
4737 
4738 static void
4739 synaptics_passthrough_on(struct psm_softc *sc)
4740 {
4741 	int mode_byte;
4742 
4743 	mode_byte = 0xc1 | (1 << 5);
4744 	VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode. %d\n",
4745 		mode_byte));
4746 	mouse_ext_command(sc->kbdc, mode_byte);
4747 
4748 	/* "Commit" the Set Mode Byte command sent above. */
4749 	set_mouse_sampling_rate(sc->kbdc, 20);
4750 }
4751 
4752 static void
4753 synaptics_passthrough_off(struct psm_softc *sc)
4754 {
4755 	int mode_byte;
4756 
4757 	mode_byte = 0xc1;
4758 	VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n"));
4759 	set_mouse_scaling(sc->kbdc, 2);
4760 	set_mouse_scaling(sc->kbdc, 1);
4761 	mouse_ext_command(sc->kbdc, mode_byte);
4762 
4763 	/* "Commit" the Set Mode Byte command sent above. */
4764 	set_mouse_sampling_rate(sc->kbdc, 20);
4765 }
4766 
4767 /* IBM/Lenovo TrackPoint */
4768 static int
4769 trackpoint_command(struct psm_softc *sc, int cmd, int loc, int val)
4770 {
4771 	const int seq[] = { 0xe2, cmd, loc, val };
4772 	int i;
4773 
4774 	if (sc->synhw.capPassthrough)
4775 		synaptics_passthrough_on(sc);
4776 
4777 	for (i = 0; i < nitems(seq); i++) {
4778 		if (sc->synhw.capPassthrough &&
4779 		    (seq[i] == 0xff || seq[i] == 0xe7))
4780 			if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) {
4781 				synaptics_passthrough_off(sc);
4782 				return (EIO);
4783 			}
4784 		if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) {
4785 			if (sc->synhw.capPassthrough)
4786 				synaptics_passthrough_off(sc);
4787 			return (EIO);
4788 		}
4789 	}
4790 
4791 	if (sc->synhw.capPassthrough)
4792 		synaptics_passthrough_off(sc);
4793 
4794 	return (0);
4795 }
4796 
4797 #define	PSM_TPINFO(x)	offsetof(struct psm_softc, tpinfo.x)
4798 #define	TPMASK		0
4799 #define	TPLOC		1
4800 #define	TPINFO		2
4801 
4802 static int
4803 trackpoint_sysctl(SYSCTL_HANDLER_ARGS)
4804 {
4805 	static const int data[][3] = {
4806 		{ 0x00, 0x4a, PSM_TPINFO(sensitivity) },
4807 		{ 0x00, 0x4d, PSM_TPINFO(inertia) },
4808 		{ 0x00, 0x60, PSM_TPINFO(uplateau) },
4809 		{ 0x00, 0x57, PSM_TPINFO(reach) },
4810 		{ 0x00, 0x58, PSM_TPINFO(draghys) },
4811 		{ 0x00, 0x59, PSM_TPINFO(mindrag) },
4812 		{ 0x00, 0x5a, PSM_TPINFO(upthresh) },
4813 		{ 0x00, 0x5c, PSM_TPINFO(threshold) },
4814 		{ 0x00, 0x5d, PSM_TPINFO(jenks) },
4815 		{ 0x00, 0x5e, PSM_TPINFO(ztime) },
4816 		{ 0x01, 0x2c, PSM_TPINFO(pts) },
4817 		{ 0x08, 0x2d, PSM_TPINFO(skipback) }
4818 	};
4819 	struct psm_softc *sc;
4820 	int error, newval, *oldvalp;
4821 	const int *tp;
4822 
4823 	if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
4824 		return (EINVAL);
4825 	sc = arg1;
4826 	tp = data[arg2];
4827 	oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
4828 	newval = *oldvalp;
4829 	error = sysctl_handle_int(oidp, &newval, 0, req);
4830 	if (error != 0)
4831 		return (error);
4832 	if (newval == *oldvalp)
4833 		return (0);
4834 	if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
4835 		return (EINVAL);
4836 	error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47,
4837 	    tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
4838 	if (error != 0)
4839 		return (error);
4840 	*oldvalp = newval;
4841 
4842 	return (0);
4843 }
4844 
4845 static void
4846 trackpoint_sysctl_create_tree(struct psm_softc *sc)
4847 {
4848 
4849 	if (sc->tpinfo.sysctl_tree != NULL)
4850 		return;
4851 
4852 	/* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
4853 	sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
4854 	sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
4855 	    SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD,
4856 	    0, "IBM/Lenovo TrackPoint");
4857 
4858 	/* hw.psm.trackpoint.sensitivity */
4859 	sc->tpinfo.sensitivity = 0x80;
4860 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4861 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4862 	    "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4863 	    sc, TRACKPOINT_SYSCTL_SENSITIVITY,
4864 	    trackpoint_sysctl, "I",
4865 	    "Sensitivity");
4866 
4867 	/* hw.psm.trackpoint.negative_inertia */
4868 	sc->tpinfo.inertia = 0x06;
4869 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4870 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4871 	    "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4872 	    sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
4873 	    trackpoint_sysctl, "I",
4874 	    "Negative inertia factor");
4875 
4876 	/* hw.psm.trackpoint.upper_plateau */
4877 	sc->tpinfo.uplateau = 0x61;
4878 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4879 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4880 	    "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4881 	    sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
4882 	    trackpoint_sysctl, "I",
4883 	    "Transfer function upper plateau speed");
4884 
4885 	/* hw.psm.trackpoint.backup_range */
4886 	sc->tpinfo.reach = 0x0a;
4887 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4888 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4889 	    "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4890 	    sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
4891 	    trackpoint_sysctl, "I",
4892 	    "Backup range");
4893 
4894 	/* hw.psm.trackpoint.drag_hysteresis */
4895 	sc->tpinfo.draghys = 0xff;
4896 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4897 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4898 	    "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4899 	    sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
4900 	    trackpoint_sysctl, "I",
4901 	    "Drag hysteresis");
4902 
4903 	/* hw.psm.trackpoint.minimum_drag */
4904 	sc->tpinfo.mindrag = 0x14;
4905 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4906 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4907 	    "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4908 	    sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
4909 	    trackpoint_sysctl, "I",
4910 	    "Minimum drag");
4911 
4912 	/* hw.psm.trackpoint.up_threshold */
4913 	sc->tpinfo.upthresh = 0xff;
4914 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4915 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4916 	    "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4917 	    sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
4918 	    trackpoint_sysctl, "I",
4919 	    "Up threshold for release");
4920 
4921 	/* hw.psm.trackpoint.threshold */
4922 	sc->tpinfo.threshold = 0x08;
4923 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4924 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4925 	    "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4926 	    sc, TRACKPOINT_SYSCTL_THRESHOLD,
4927 	    trackpoint_sysctl, "I",
4928 	    "Threshold");
4929 
4930 	/* hw.psm.trackpoint.jenks_curvature */
4931 	sc->tpinfo.jenks = 0x87;
4932 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4933 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4934 	    "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4935 	    sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
4936 	    trackpoint_sysctl, "I",
4937 	    "Jenks curvature");
4938 
4939 	/* hw.psm.trackpoint.z_time */
4940 	sc->tpinfo.ztime = 0x26;
4941 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4942 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4943 	    "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4944 	    sc, TRACKPOINT_SYSCTL_Z_TIME,
4945 	    trackpoint_sysctl, "I",
4946 	    "Z time constant");
4947 
4948 	/* hw.psm.trackpoint.press_to_select */
4949 	sc->tpinfo.pts = 0x00;
4950 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4951 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4952 	    "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4953 	    sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
4954 	    trackpoint_sysctl, "I",
4955 	    "Press to Select");
4956 
4957 	/* hw.psm.trackpoint.skip_backups */
4958 	sc->tpinfo.skipback = 0x00;
4959 	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4960 	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4961 	    "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4962 	    sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
4963 	    trackpoint_sysctl, "I",
4964 	    "Skip backups from drags");
4965 }
4966 
4967 static void
4968 set_trackpoint_parameters(struct psm_softc *sc)
4969 {
4970 	trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity);
4971 	trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau);
4972 	trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia);
4973 	trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach);
4974 	trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys);
4975 	trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag);
4976 	trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh);
4977 	trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold);
4978 	trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks);
4979 	trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime);
4980 	if (sc->tpinfo.pts == 0x01)
4981 		trackpoint_command(sc, 0x47, 0x2c, 0x01);
4982 	if (sc->tpinfo.skipback == 0x01)
4983 		trackpoint_command(sc, 0x47, 0x2d, 0x08);
4984 }
4985 
4986 static int
4987 enable_trackpoint(KBDC kbdc, struct psm_softc *sc)
4988 {
4989 	int id;
4990 
4991 	if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
4992 	    read_aux_data(kbdc) != 0x01)
4993 		return (FALSE);
4994 	id = read_aux_data(kbdc);
4995 	if (id < 0x01)
4996 		return (FALSE);
4997 	if (sc != NULL)
4998 		sc->tphw = id;
4999 	if (!trackpoint_support)
5000 		return (FALSE);
5001 
5002 	if (sc != NULL) {
5003 		/* Create sysctl tree. */
5004 		trackpoint_sysctl_create_tree(sc);
5005 
5006 		/*
5007 		 * Don't overwrite hwid and buttons when we are
5008 		 * a guest device.
5009 		 */
5010 		if (!sc->synhw.capPassthrough) {
5011 			sc->hw.hwid = id;
5012 			sc->hw.buttons = 3;
5013 		}
5014 	}
5015 
5016 	return (TRUE);
5017 }
5018 
5019 /* Interlink electronics VersaPad */
5020 static int
5021 enable_versapad(KBDC kbdc, struct psm_softc *sc)
5022 {
5023 	int data[3];
5024 
5025 	set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
5026 	set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
5027 	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
5028 	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
5029 	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
5030 	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
5031 	if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
5032 		return (FALSE);
5033 	if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
5034 		return (FALSE);
5035 	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
5036 
5037 	return (TRUE);				/* PS/2 absolute mode */
5038 }
5039 
5040 /*
5041  * Return true if 'now' is earlier than (start + (secs.usecs)).
5042  * Now may be NULL and the function will fetch the current time from
5043  * getmicrouptime(), or a cached 'now' can be passed in.
5044  * All values should be numbers derived from getmicrouptime().
5045  */
5046 static int
5047 timeelapsed(start, secs, usecs, now)
5048 	const struct timeval *start, *now;
5049 	int secs, usecs;
5050 {
5051 	struct timeval snow, tv;
5052 
5053 	/* if there is no 'now' passed in, the get it as a convience. */
5054 	if (now == NULL) {
5055 		getmicrouptime(&snow);
5056 		now = &snow;
5057 	}
5058 
5059 	tv.tv_sec = secs;
5060 	tv.tv_usec = usecs;
5061 	timevaladd(&tv, start);
5062 	return (timevalcmp(&tv, now, <));
5063 }
5064 
5065 static int
5066 psmresume(device_t dev)
5067 {
5068 	struct psm_softc *sc = device_get_softc(dev);
5069 	int unit = device_get_unit(dev);
5070 	int err;
5071 
5072 	VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
5073 
5074 	if ((sc->config &
5075 	    (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
5076 		return (0);
5077 
5078 	err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
5079 
5080 	if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
5081 		/*
5082 		 * Release the blocked process; it must be notified that
5083 		 * the device cannot be accessed anymore.
5084 		 */
5085 		sc->state &= ~PSM_ASLP;
5086 		wakeup(sc);
5087 	}
5088 
5089 	VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
5090 
5091 	return (err);
5092 }
5093 
5094 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
5095 
5096 #ifdef DEV_ISA
5097 
5098 /*
5099  * This sucks up assignments from PNPBIOS and ACPI.
5100  */
5101 
5102 /*
5103  * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
5104  * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
5105  * can be probed and attached only after the AT keyboard controller is
5106  * attached, we shall quietly reserve the IRQ resource for later use.
5107  * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
5108  * copy the IRQ resource to the PS/2 mouse device instance hanging
5109  * under the keyboard controller, then probe and attach it.
5110  */
5111 
5112 static	devclass_t			psmcpnp_devclass;
5113 
5114 static	device_probe_t			psmcpnp_probe;
5115 static	device_attach_t			psmcpnp_attach;
5116 
5117 static device_method_t psmcpnp_methods[] = {
5118 	DEVMETHOD(device_probe,		psmcpnp_probe),
5119 	DEVMETHOD(device_attach,	psmcpnp_attach),
5120 
5121 	{ 0, 0 }
5122 };
5123 
5124 static driver_t psmcpnp_driver = {
5125 	PSMCPNP_DRIVER_NAME,
5126 	psmcpnp_methods,
5127 	1,			/* no softc */
5128 };
5129 
5130 static struct isa_pnp_id psmcpnp_ids[] = {
5131 	{ 0x030fd041, "PS/2 mouse port" },		/* PNP0F03 */
5132 	{ 0x0e0fd041, "PS/2 mouse port" },		/* PNP0F0E */
5133 	{ 0x120fd041, "PS/2 mouse port" },		/* PNP0F12 */
5134 	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
5135 	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
5136 	{ 0x02002e4f, "Dell PS/2 mouse port" },		/* Lat. X200, Dell */
5137 	{ 0x0002a906, "ALPS Glide Point" },		/* ALPS Glide Point */
5138 	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
5139 	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
5140 	{ 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
5141 	{ 0x0290d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9002, Vaio */
5142 	{ 0x0390d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9003, Vaio */
5143 	{ 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
5144 	{ 0 }
5145 };
5146 
5147 static int
5148 create_a_copy(device_t atkbdc, device_t me)
5149 {
5150 	device_t psm;
5151 	u_long irq;
5152 
5153 	/* find the PS/2 mouse device instance under the keyboard controller */
5154 	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
5155 	    device_get_unit(atkbdc));
5156 	if (psm == NULL)
5157 		return (ENXIO);
5158 	if (device_get_state(psm) != DS_NOTPRESENT)
5159 		return (0);
5160 
5161 	/* move our resource to the found device */
5162 	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
5163 	bus_delete_resource(me, SYS_RES_IRQ, 0);
5164 	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
5165 
5166 	/* ...then probe and attach it */
5167 	return (device_probe_and_attach(psm));
5168 }
5169 
5170 static int
5171 psmcpnp_probe(device_t dev)
5172 {
5173 	struct resource *res;
5174 	u_long irq;
5175 	int rid;
5176 
5177 	if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
5178 		return (ENXIO);
5179 
5180 	/*
5181 	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
5182 	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
5183 	 * declares the PS/2 mouse device node without an IRQ resource!
5184 	 * If this happens, we shall refer to device hints.
5185 	 * If we still don't find it there, use a hardcoded value... XXX
5186 	 */
5187 	rid = 0;
5188 	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
5189 	if (irq <= 0) {
5190 		if (resource_long_value(PSM_DRIVER_NAME,
5191 		    device_get_unit(dev),"irq", &irq) != 0)
5192 			irq = 12;	/* XXX */
5193 		device_printf(dev, "irq resource info is missing; "
5194 		    "assuming irq %ld\n", irq);
5195 		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
5196 	}
5197 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0);
5198 	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
5199 
5200 	/* keep quiet */
5201 	if (!bootverbose)
5202 		device_quiet(dev);
5203 
5204 	return ((res == NULL) ? ENXIO : 0);
5205 }
5206 
5207 static int
5208 psmcpnp_attach(device_t dev)
5209 {
5210 	device_t atkbdc;
5211 
5212 	/* find the keyboard controller, which may be on acpi* or isa* bus */
5213 	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
5214 	    device_get_unit(dev));
5215 	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
5216 		create_a_copy(atkbdc, dev);
5217 
5218 	return (0);
5219 }
5220 
5221 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
5222 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
5223 
5224 #endif /* DEV_ISA */
5225