141016Sdfr/*-
241016Sdfr * Copyright (c) 1992, 1993 Erik Forsberg.
341016Sdfr * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
441016Sdfr * All rights reserved.
541016Sdfr *
641016Sdfr * Redistribution and use in source and binary forms, with or without
741016Sdfr * modification, are permitted provided that the following conditions
841016Sdfr * are met:
941016Sdfr * 1. Redistributions of source code must retain the above copyright
1041016Sdfr *    notice, this list of conditions and the following disclaimer.
1141016Sdfr *
1241016Sdfr * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
1341016Sdfr * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1441016Sdfr * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
1541016Sdfr * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1641016Sdfr * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1741016Sdfr * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1841016Sdfr * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1941016Sdfr * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2041016Sdfr * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2141016Sdfr * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2241016Sdfr */
2341016Sdfr/*
2441016Sdfr *  Ported to 386bsd Oct 17, 1992
2541016Sdfr *  Sandi Donno, Computer Science, University of Cape Town, South Africa
2641016Sdfr *  Please send bug reports to sandi@cs.uct.ac.za
2741016Sdfr *
2841016Sdfr *  Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
2941016Sdfr *  although I was only partially successful in getting the alpha release
3041016Sdfr *  of his "driver for the Logitech and ATI Inport Bus mice for use with
3141016Sdfr *  386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
3241016Sdfr *  found his code to be an invaluable reference when porting this driver
3341016Sdfr *  to 386bsd.
3441016Sdfr *
3541016Sdfr *  Further modifications for latest 386BSD+patchkit and port to NetBSD,
3641016Sdfr *  Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
3741016Sdfr *
3841016Sdfr *  Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
3941016Sdfr *  Andrew Herbert - 12 June 1993
4041016Sdfr *
4141016Sdfr *  Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
4241016Sdfr *  - 13 June 1993
4341016Sdfr *
4441016Sdfr *  Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
4541016Sdfr *  - 24 October 1993
4641016Sdfr *
4741016Sdfr *  Hardware access routines and probe logic rewritten by
4841016Sdfr *  Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
4941016Sdfr *  - 3, 14, 22 October 1996.
5041016Sdfr *  - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
5141016Sdfr *  - 14, 30 November 1996. Uses `kbdio.c'.
5241016Sdfr *  - 13 December 1996. Uses queuing version of `kbdio.c'.
53178019Sjkim *  - January/February 1997. Tweaked probe logic for
5441016Sdfr *    HiNote UltraII/Latitude/Armada laptops.
5541016Sdfr *  - 30 July 1997. Added APM support.
56178019Sjkim *  - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
5741016Sdfr *    Improved sync check logic.
5841016Sdfr *    Vendor specific support routines.
5941016Sdfr */
6041016Sdfr
61116181Sobrien#include <sys/cdefs.h>
62116181Sobrien__FBSDID("$FreeBSD$");
63116181Sobrien
64147271Smarius#include "opt_isa.h"
6541016Sdfr#include "opt_psm.h"
6641016Sdfr
6741016Sdfr#include <sys/param.h>
6841016Sdfr#include <sys/systm.h>
6941016Sdfr#include <sys/kernel.h>
7041016Sdfr#include <sys/module.h>
7141016Sdfr#include <sys/bus.h>
7241016Sdfr#include <sys/conf.h>
73189870Srnoland#include <sys/filio.h>
7441016Sdfr#include <sys/poll.h>
75189870Srnoland#include <sys/sigio.h>
76189870Srnoland#include <sys/signalvar.h>
7741016Sdfr#include <sys/syslog.h>
7845720Speter#include <machine/bus.h>
7941181Sdfr#include <sys/rman.h>
8070834Swollman#include <sys/selinfo.h>
81123442Salfred#include <sys/sysctl.h>
8284880Syokota#include <sys/time.h>
8341016Sdfr#include <sys/uio.h>
8441016Sdfr
85114216Skan#include <sys/limits.h>
8666860Sphk#include <sys/mouse.h>
8741181Sdfr#include <machine/resource.h>
8841016Sdfr
89147271Smarius#ifdef DEV_ISA
9041016Sdfr#include <isa/isavar.h>
91147271Smarius#endif
9241016Sdfr
93147271Smarius#include <dev/atkbdc/atkbdcreg.h>
94147271Smarius#include <dev/atkbdc/psm.h>
95147271Smarius
9641016Sdfr/*
9741016Sdfr * Driver specific options: the following options may be set by
9841016Sdfr * `options' statements in the kernel configuration file.
9941016Sdfr */
10041016Sdfr
10141016Sdfr/* debugging */
10241016Sdfr#ifndef PSM_DEBUG
103178019Sjkim#define	PSM_DEBUG	0	/*
104134405Sgibbs				 * logging: 0: none, 1: brief, 2: verbose
105134405Sgibbs				 *          3: sync errors, 4: all packets
106134405Sgibbs				 */
10741016Sdfr#endif
108178019Sjkim#define	VLOG(level, args)	do {	\
109178019Sjkim	if (verbose >= level)		\
110178019Sjkim		log args;		\
111134405Sgibbs} while (0)
11241016Sdfr
11384880Syokota#ifndef PSM_INPUT_TIMEOUT
114178019Sjkim#define	PSM_INPUT_TIMEOUT	2000000	/* 2 sec */
11584880Syokota#endif
11684880Syokota
117133297Sphilip#ifndef PSM_TAP_TIMEOUT
118178019Sjkim#define	PSM_TAP_TIMEOUT		125000
119133297Sphilip#endif
120133297Sphilip
121133297Sphilip#ifndef PSM_TAP_THRESHOLD
122178019Sjkim#define	PSM_TAP_THRESHOLD	25
123133297Sphilip#endif
124133297Sphilip
12541016Sdfr/* end of driver specific options */
12641016Sdfr
127178019Sjkim#define	PSMCPNP_DRIVER_NAME	"psmcpnp"
12883492Syokota
12941016Sdfr/* input queue */
130178019Sjkim#define	PSM_BUFSIZE		960
131178019Sjkim#define	PSM_SMALLBUFSIZE	240
13241016Sdfr
13341016Sdfr/* operation levels */
134178019Sjkim#define	PSM_LEVEL_BASE		0
135178019Sjkim#define	PSM_LEVEL_STANDARD	1
136178019Sjkim#define	PSM_LEVEL_NATIVE	2
137178019Sjkim#define	PSM_LEVEL_MIN		PSM_LEVEL_BASE
138178019Sjkim#define	PSM_LEVEL_MAX		PSM_LEVEL_NATIVE
13941016Sdfr
14048778Syokota/* Logitech PS2++ protocol */
141178019Sjkim#define	MOUSE_PS2PLUS_CHECKBITS(b)	\
142178019Sjkim    ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
143178019Sjkim#define	MOUSE_PS2PLUS_PACKET_TYPE(b)	\
144178019Sjkim    (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
14548778Syokota
14641016Sdfr/* ring buffer */
14741016Sdfrtypedef struct ringbuf {
148178019Sjkim	int		count;	/* # of valid elements in the buffer */
149178019Sjkim	int		head;	/* head pointer */
150178019Sjkim	int		tail;	/* tail poiner */
151178019Sjkim	u_char buf[PSM_BUFSIZE];
15241016Sdfr} ringbuf_t;
15341016Sdfr
154123442Salfred/* data buffer */
155123442Salfredtypedef struct packetbuf {
156178019Sjkim	u_char	ipacket[16];	/* interim input buffer */
157178019Sjkim	int	inputbytes;	/* # of bytes in the input buffer */
158123442Salfred} packetbuf_t;
159123442Salfred
160123442Salfred#ifndef PSM_PACKETQUEUE
161178019Sjkim#define	PSM_PACKETQUEUE	128
162123442Salfred#endif
163123442Salfred
164183888Sdumbbellenum {
165183888Sdumbbell	SYNAPTICS_SYSCTL_MIN_PRESSURE,
166183888Sdumbbell	SYNAPTICS_SYSCTL_MAX_PRESSURE,
167183888Sdumbbell	SYNAPTICS_SYSCTL_MAX_WIDTH,
168183888Sdumbbell	SYNAPTICS_SYSCTL_MARGIN_TOP,
169183888Sdumbbell	SYNAPTICS_SYSCTL_MARGIN_RIGHT,
170183888Sdumbbell	SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
171183888Sdumbbell	SYNAPTICS_SYSCTL_MARGIN_LEFT,
172183888Sdumbbell	SYNAPTICS_SYSCTL_NA_TOP,
173183888Sdumbbell	SYNAPTICS_SYSCTL_NA_RIGHT,
174183888Sdumbbell	SYNAPTICS_SYSCTL_NA_BOTTOM,
175183888Sdumbbell	SYNAPTICS_SYSCTL_NA_LEFT,
176183888Sdumbbell	SYNAPTICS_SYSCTL_WINDOW_MIN,
177183888Sdumbbell	SYNAPTICS_SYSCTL_WINDOW_MAX,
178183888Sdumbbell	SYNAPTICS_SYSCTL_MULTIPLICATOR,
179183888Sdumbbell	SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
180183888Sdumbbell	SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
181183888Sdumbbell	SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
182183888Sdumbbell	SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
183183888Sdumbbell	SYNAPTICS_SYSCTL_DIV_MIN,
184183888Sdumbbell	SYNAPTICS_SYSCTL_DIV_MAX,
185183888Sdumbbell	SYNAPTICS_SYSCTL_DIV_MAX_NA,
186183888Sdumbbell	SYNAPTICS_SYSCTL_DIV_LEN,
187183888Sdumbbell	SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
188183888Sdumbbell	SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
189183888Sdumbbell	SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
190183888Sdumbbell	SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
191183888Sdumbbell	SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
192183888Sdumbbell	SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
193183888Sdumbbell	SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
194183888Sdumbbell	SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX
195183888Sdumbbell};
196183888Sdumbbell
197139982Sphiliptypedef struct synapticsinfo {
198183888Sdumbbell	struct sysctl_ctx_list	 sysctl_ctx;
199178019Sjkim	struct sysctl_oid	*sysctl_tree;
200183888Sdumbbell	int			 directional_scrolls;
201183888Sdumbbell	int			 min_pressure;
202183888Sdumbbell	int			 max_pressure;
203183888Sdumbbell	int			 max_width;
204183888Sdumbbell	int			 margin_top;
205183888Sdumbbell	int			 margin_right;
206183888Sdumbbell	int			 margin_bottom;
207183888Sdumbbell	int			 margin_left;
208183888Sdumbbell	int			 na_top;
209183888Sdumbbell	int			 na_right;
210183888Sdumbbell	int			 na_bottom;
211183888Sdumbbell	int			 na_left;
212183888Sdumbbell	int			 window_min;
213183888Sdumbbell	int			 window_max;
214183888Sdumbbell	int			 multiplicator;
215183888Sdumbbell	int			 weight_current;
216183888Sdumbbell	int			 weight_previous;
217183888Sdumbbell	int			 weight_previous_na;
218183888Sdumbbell	int			 weight_len_squared;
219183888Sdumbbell	int			 div_min;
220183888Sdumbbell	int			 div_max;
221183888Sdumbbell	int			 div_max_na;
222183888Sdumbbell	int			 div_len;
223183888Sdumbbell	int			 tap_max_delta;
224183888Sdumbbell	int			 tap_min_queue;
225183888Sdumbbell	int			 taphold_timeout;
226183888Sdumbbell	int			 vscroll_ver_area;
227183888Sdumbbell	int			 vscroll_hor_area;
228183888Sdumbbell	int			 vscroll_min_delta;
229183888Sdumbbell	int			 vscroll_div_min;
230183888Sdumbbell	int			 vscroll_div_max;
231139982Sphilip} synapticsinfo_t;
232139982Sphilip
233183888Sdumbbelltypedef struct synapticspacket {
234183888Sdumbbell	int			x;
235183888Sdumbbell	int			y;
236183888Sdumbbell} synapticspacket_t;
237183888Sdumbbell
238183888Sdumbbell#define	SYNAPTICS_PACKETQUEUE 10
239183888Sdumbbell#define SYNAPTICS_QUEUE_CURSOR(x)					\
240183888Sdumbbell	(x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
241183888Sdumbbell
242244405Sdumbbell#define	SYNAPTICS_VERSION_GE(synhw, major, minor)			\
243244405Sdumbbell    ((synhw).infoMajor > (major) ||					\
244244405Sdumbbell     ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
245244405Sdumbbell
246183888Sdumbbelltypedef struct synapticsaction {
247183888Sdumbbell	synapticspacket_t	queue[SYNAPTICS_PACKETQUEUE];
248183888Sdumbbell	int			queue_len;
249183888Sdumbbell	int			queue_cursor;
250183888Sdumbbell	int			window_min;
251183888Sdumbbell	int			start_x;
252183888Sdumbbell	int			start_y;
253183888Sdumbbell	int			avg_dx;
254183888Sdumbbell	int			avg_dy;
255183888Sdumbbell	int			squelch_x;
256183888Sdumbbell	int			squelch_y;
257183888Sdumbbell	int			fingers_nb;
258183888Sdumbbell	int			tap_button;
259183888Sdumbbell	int			in_taphold;
260183888Sdumbbell	int			in_vscroll;
261183888Sdumbbell} synapticsaction_t;
262183888Sdumbbell
263248478Sjkimenum {
264248478Sjkim	TRACKPOINT_SYSCTL_SENSITIVITY,
265248478Sjkim	TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
266248478Sjkim	TRACKPOINT_SYSCTL_UPPER_PLATEAU,
267248478Sjkim	TRACKPOINT_SYSCTL_BACKUP_RANGE,
268248478Sjkim	TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
269248478Sjkim	TRACKPOINT_SYSCTL_MINIMUM_DRAG,
270248478Sjkim	TRACKPOINT_SYSCTL_UP_THRESHOLD,
271248478Sjkim	TRACKPOINT_SYSCTL_THRESHOLD,
272248478Sjkim	TRACKPOINT_SYSCTL_JENKS_CURVATURE,
273248478Sjkim	TRACKPOINT_SYSCTL_Z_TIME,
274248478Sjkim	TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
275248478Sjkim	TRACKPOINT_SYSCTL_SKIP_BACKUPS
276248478Sjkim};
277248478Sjkim
278248478Sjkimtypedef struct trackpointinfo {
279248478Sjkim	struct sysctl_ctx_list sysctl_ctx;
280248478Sjkim	struct sysctl_oid *sysctl_tree;
281248478Sjkim	int	sensitivity;
282248478Sjkim	int	inertia;
283248478Sjkim	int	uplateau;
284248478Sjkim	int	reach;
285248478Sjkim	int	draghys;
286248478Sjkim	int	mindrag;
287248478Sjkim	int	upthresh;
288248478Sjkim	int	threshold;
289248478Sjkim	int	jenks;
290248478Sjkim	int	ztime;
291248478Sjkim	int	pts;
292248478Sjkim	int	skipback;
293248478Sjkim} trackpointinfo_t;
294248478Sjkim
29541016Sdfr/* driver control block */
29641016Sdfrstruct psm_softc {		/* Driver status information */
297178019Sjkim	int		unit;
298178019Sjkim	struct selinfo	rsel;		/* Process selecting for Input */
299178019Sjkim	u_char		state;		/* Mouse driver state */
300178019Sjkim	int		config;		/* driver configuration flags */
301178019Sjkim	int		flags;		/* other flags */
302178019Sjkim	KBDC		kbdc;		/* handle to access kbd controller */
303178019Sjkim	struct resource	*intr;		/* IRQ resource */
304178019Sjkim	void		*ih;		/* interrupt handle */
305178019Sjkim	mousehw_t	hw;		/* hardware information */
306178019Sjkim	synapticshw_t	synhw;		/* Synaptics hardware information */
307178019Sjkim	synapticsinfo_t	syninfo;	/* Synaptics configuration */
308183888Sdumbbell	synapticsaction_t synaction;	/* Synaptics action context */
309248478Sjkim	int		tphw;		/* TrackPoint hardware information */
310248478Sjkim	trackpointinfo_t tpinfo;	/* TrackPoint configuration */
311178019Sjkim	mousemode_t	mode;		/* operation mode */
312178019Sjkim	mousemode_t	dflt_mode;	/* default operation mode */
313178019Sjkim	mousestatus_t	status;		/* accumulated mouse movement */
314178019Sjkim	ringbuf_t	queue;		/* mouse status queue */
315178019Sjkim	packetbuf_t	pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
316178019Sjkim	int		pqueue_start;	/* start of data in queue */
317178019Sjkim	int		pqueue_end;	/* end of data in queue */
318178019Sjkim	int		button;		/* the latest button state */
319178019Sjkim	int		xold;		/* previous absolute X position */
320178019Sjkim	int		yold;		/* previous absolute Y position */
321178019Sjkim	int		xaverage;	/* average X position */
322178019Sjkim	int		yaverage;	/* average Y position */
323178019Sjkim	int		squelch; /* level to filter movement at low speed */
324178019Sjkim	int		zmax;	/* maximum pressure value for touchpads */
325178019Sjkim	int		syncerrors; /* # of bytes discarded to synchronize */
326178019Sjkim	int		pkterrors;  /* # of packets failed during quaranteen. */
327178019Sjkim	struct timeval	inputtimeout;
328178019Sjkim	struct timeval	lastsoftintr;	/* time of last soft interrupt */
329178019Sjkim	struct timeval	lastinputerr;	/* time last sync error happened */
330178019Sjkim	struct timeval	taptimeout;	/* tap timeout for touchpads */
331178019Sjkim	int		watchdog;	/* watchdog timer flag */
332240211Sjhb	struct callout	callout;	/* watchdog timer call out */
333240211Sjhb	struct callout	softcallout; /* buffer timer call out */
334178019Sjkim	struct cdev	*dev;
335178019Sjkim	struct cdev	*bdev;
336178019Sjkim	int		lasterr;
337178019Sjkim	int		cmdcount;
338189870Srnoland	struct sigio	*async;		/* Processes waiting for SIGIO */
33941016Sdfr};
340114293Smarkmstatic devclass_t psm_devclass;
34141016Sdfr
34241016Sdfr/* driver state flags (state) */
343178019Sjkim#define	PSM_VALID		0x80
344178019Sjkim#define	PSM_OPEN		1	/* Device is open */
345178019Sjkim#define	PSM_ASLP		2	/* Waiting for mouse data */
346178019Sjkim#define	PSM_SOFTARMED		4	/* Software interrupt armed */
347178019Sjkim#define	PSM_NEED_SYNCBITS	8	/* Set syncbits using next data pkt */
34841016Sdfr
34941016Sdfr/* driver configuration flags (config) */
350178019Sjkim#define	PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
351178019Sjkim#define	PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
352178019Sjkim#define	PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
353178019Sjkim#define	PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
354178019Sjkim#define	PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
355178019Sjkim#define	PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
356178019Sjkim#define	PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
357178019Sjkim#define	PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
358178019Sjkim#define	PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
35941016Sdfr
360178019Sjkim#define	PSM_CONFIG_FLAGS	\
361178019Sjkim    (PSM_CONFIG_RESOLUTION |	\
362178019Sjkim    PSM_CONFIG_ACCEL |		\
363178019Sjkim    PSM_CONFIG_NOCHECKSYNC |	\
364178019Sjkim    PSM_CONFIG_NOIDPROBE |	\
365178019Sjkim    PSM_CONFIG_NORESET |	\
366178019Sjkim    PSM_CONFIG_FORCETAP |	\
367178019Sjkim    PSM_CONFIG_IGNPORTERROR |	\
368178019Sjkim    PSM_CONFIG_HOOKRESUME |	\
369178019Sjkim    PSM_CONFIG_INITAFTERSUSPEND)
37041016Sdfr
37141016Sdfr/* other flags (flags) */
372178019Sjkim#define	PSM_FLAGS_FINGERDOWN	0x0001	/* VersaPad finger down */
37341016Sdfr
374135945Sphilip/* Tunables */
375200674Sdumbbellstatic int tap_enabled = -1;
376200674SdumbbellTUNABLE_INT("hw.psm.tap_enabled", &tap_enabled);
377200674Sdumbbell
378255210Seadlerstatic int synaptics_support = 0;
379135945SphilipTUNABLE_INT("hw.psm.synaptics_support", &synaptics_support);
380135945Sphilip
381255210Seadlerstatic int trackpoint_support = 0;
382248478SjkimTUNABLE_INT("hw.psm.trackpoint_support", &trackpoint_support);
383248478Sjkim
384139628Sphilipstatic int verbose = PSM_DEBUG;
385139628SphilipTUNABLE_INT("debug.psm.loglevel", &verbose);
386139628Sphilip
38741016Sdfr/* for backward compatibility */
388178019Sjkim#define	OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
389178019Sjkim#define	OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
390178019Sjkim#define	OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
39141016Sdfr
39241016Sdfrtypedef struct old_mousehw {
393178019Sjkim	int	buttons;
394178019Sjkim	int	iftype;
395178019Sjkim	int	type;
396178019Sjkim	int	hwid;
39741016Sdfr} old_mousehw_t;
39841016Sdfr
39941016Sdfrtypedef struct old_mousemode {
400178019Sjkim	int	protocol;
401178019Sjkim	int	rate;
402178019Sjkim	int	resolution;
403178019Sjkim	int	accelfactor;
40441016Sdfr} old_mousemode_t;
40541016Sdfr
40641016Sdfr/* packet formatting function */
407178019Sjkimtypedef int	packetfunc_t(struct psm_softc *, u_char *, int *, int,
408178019Sjkim    mousestatus_t *);
40941016Sdfr
41041016Sdfr/* function prototypes */
411178019Sjkimstatic void	psmidentify(driver_t *, device_t);
412178019Sjkimstatic int	psmprobe(device_t);
413178019Sjkimstatic int	psmattach(device_t);
414178019Sjkimstatic int	psmdetach(device_t);
415178019Sjkimstatic int	psmresume(device_t);
41641016Sdfr
417178019Sjkimstatic d_open_t		psmopen;
418178019Sjkimstatic d_close_t	psmclose;
419178019Sjkimstatic d_read_t		psmread;
420178019Sjkimstatic d_write_t	psmwrite;
421178019Sjkimstatic d_ioctl_t	psmioctl;
422178019Sjkimstatic d_poll_t		psmpoll;
42341016Sdfr
424178019Sjkimstatic int	enable_aux_dev(KBDC);
425178019Sjkimstatic int	disable_aux_dev(KBDC);
426178019Sjkimstatic int	get_mouse_status(KBDC, int *, int, int);
427178019Sjkimstatic int	get_aux_id(KBDC);
428178019Sjkimstatic int	set_mouse_sampling_rate(KBDC, int);
429178019Sjkimstatic int	set_mouse_scaling(KBDC, int);
430178019Sjkimstatic int	set_mouse_resolution(KBDC, int);
431178019Sjkimstatic int	set_mouse_mode(KBDC);
432178019Sjkimstatic int	get_mouse_buttons(KBDC);
433178019Sjkimstatic int	is_a_mouse(int);
434178019Sjkimstatic void	recover_from_error(KBDC);
435178019Sjkimstatic int	restore_controller(KBDC, int);
436178019Sjkimstatic int	doinitialize(struct psm_softc *, mousemode_t *);
437178019Sjkimstatic int	doopen(struct psm_softc *, int);
438178019Sjkimstatic int	reinitialize(struct psm_softc *, int);
439178019Sjkimstatic char	*model_name(int);
440178019Sjkimstatic void	psmsoftintr(void *);
441178019Sjkimstatic void	psmintr(void *);
442178019Sjkimstatic void	psmtimeout(void *);
443178019Sjkimstatic int	timeelapsed(const struct timeval *, int, int,
444178019Sjkim		    const struct timeval *);
445178019Sjkimstatic void	dropqueue(struct psm_softc *);
446178019Sjkimstatic void	flushpackets(struct psm_softc *);
447178019Sjkimstatic void	proc_mmanplus(struct psm_softc *, packetbuf_t *,
448178019Sjkim		    mousestatus_t *, int *, int *, int *);
449178019Sjkimstatic int	proc_synaptics(struct psm_softc *, packetbuf_t *,
450178019Sjkim		    mousestatus_t *, int *, int *, int *);
451178019Sjkimstatic void	proc_versapad(struct psm_softc *, packetbuf_t *,
452178019Sjkim		    mousestatus_t *, int *, int *, int *);
453178019Sjkimstatic int	tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
454178019Sjkim		    u_char *);
45541016Sdfr
45641016Sdfr/* vendor specific features */
457233580Sjkimtypedef int	probefunc_t(KBDC, struct psm_softc *);
45841016Sdfr
459178019Sjkimstatic int	mouse_id_proc1(KBDC, int, int, int *);
460178019Sjkimstatic int	mouse_ext_command(KBDC, int);
46141016Sdfr
462178019Sjkimstatic probefunc_t	enable_groller;
463178019Sjkimstatic probefunc_t	enable_gmouse;
464178019Sjkimstatic probefunc_t	enable_aglide;
465178019Sjkimstatic probefunc_t	enable_kmouse;
466178019Sjkimstatic probefunc_t	enable_msexplorer;
467178019Sjkimstatic probefunc_t	enable_msintelli;
468178019Sjkimstatic probefunc_t	enable_4dmouse;
469178019Sjkimstatic probefunc_t	enable_4dplus;
470178019Sjkimstatic probefunc_t	enable_mmanplus;
471178019Sjkimstatic probefunc_t	enable_synaptics;
472248478Sjkimstatic probefunc_t	enable_trackpoint;
473178019Sjkimstatic probefunc_t	enable_versapad;
474178019Sjkim
47541016Sdfrstatic struct {
476178019Sjkim	int		model;
477178019Sjkim	u_char		syncmask;
478178019Sjkim	int		packetsize;
479178019Sjkim	probefunc_t	*probefunc;
48041016Sdfr} vendortype[] = {
481178019Sjkim	/*
482178019Sjkim	 * WARNING: the order of probe is very important.  Don't mess it
483178019Sjkim	 * unless you know what you are doing.
484178019Sjkim	 */
485178019Sjkim	{ MOUSE_MODEL_NET,		/* Genius NetMouse */
486178019Sjkim	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
487178019Sjkim	{ MOUSE_MODEL_NETSCROLL,	/* Genius NetScroll */
488178019Sjkim	  0xc8, 6, enable_groller },
489178019Sjkim	{ MOUSE_MODEL_MOUSEMANPLUS,	/* Logitech MouseMan+ */
490178019Sjkim	  0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
491178019Sjkim	{ MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
492178019Sjkim	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
493178019Sjkim	{ MOUSE_MODEL_4D,		/* A4 Tech 4D Mouse */
494178019Sjkim	  0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
495178019Sjkim	{ MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
496178019Sjkim	  0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
497179474Sphilip	{ MOUSE_MODEL_SYNAPTICS,	/* Synaptics Touchpad */
498179474Sphilip	  0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
499178019Sjkim	{ MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
500178019Sjkim	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
501178019Sjkim	{ MOUSE_MODEL_GLIDEPOINT,	/* ALPS GlidePoint */
502178019Sjkim	  0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
503178019Sjkim	{ MOUSE_MODEL_THINK,		/* Kensington ThinkingMouse */
504178019Sjkim	  0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
505178019Sjkim	{ MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
506178019Sjkim	  0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
507248478Sjkim	{ MOUSE_MODEL_TRACKPOINT,	/* IBM/Lenovo TrackPoint */
508248478Sjkim	  0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
509178019Sjkim	{ MOUSE_MODEL_GENERIC,
510178019Sjkim	  0xc0, MOUSE_PS2_PACKETSIZE, NULL },
51141016Sdfr};
512178019Sjkim#define	GENERIC_MOUSE_ENTRY	\
513178019Sjkim    ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
51441016Sdfr
51541016Sdfr/* device driver declarateion */
51641016Sdfrstatic device_method_t psm_methods[] = {
51741016Sdfr	/* Device interface */
51883147Syokota	DEVMETHOD(device_identify,	psmidentify),
51941016Sdfr	DEVMETHOD(device_probe,		psmprobe),
52041016Sdfr	DEVMETHOD(device_attach,	psmattach),
52158230Syokota	DEVMETHOD(device_detach,	psmdetach),
52254629Syokota	DEVMETHOD(device_resume,	psmresume),
52341016Sdfr
52441016Sdfr	{ 0, 0 }
52541016Sdfr};
52641016Sdfr
52741016Sdfrstatic driver_t psm_driver = {
528178019Sjkim	PSM_DRIVER_NAME,
529178019Sjkim	psm_methods,
530178019Sjkim	sizeof(struct psm_softc),
53141016Sdfr};
53241016Sdfr
53347625Sphkstatic struct cdevsw psm_cdevsw = {
534126080Sphk	.d_version =	D_VERSION,
535126080Sphk	.d_flags =	D_NEEDGIANT,
536111815Sphk	.d_open =	psmopen,
537111815Sphk	.d_close =	psmclose,
538111815Sphk	.d_read =	psmread,
539178017Sjkim	.d_write =	psmwrite,
540111815Sphk	.d_ioctl =	psmioctl,
541111815Sphk	.d_poll =	psmpoll,
542111815Sphk	.d_name =	PSM_DRIVER_NAME,
54341016Sdfr};
54441016Sdfr
54541016Sdfr/* device I/O routines */
54641016Sdfrstatic int
54741016Sdfrenable_aux_dev(KBDC kbdc)
54841016Sdfr{
549178019Sjkim	int res;
55041016Sdfr
551178019Sjkim	res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
552178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
55341016Sdfr
554178019Sjkim	return (res == PSM_ACK);
55541016Sdfr}
55641016Sdfr
55741016Sdfrstatic int
55841016Sdfrdisable_aux_dev(KBDC kbdc)
55941016Sdfr{
560178019Sjkim	int res;
56141016Sdfr
562178019Sjkim	res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
563178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
56441016Sdfr
565178019Sjkim	return (res == PSM_ACK);
56641016Sdfr}
56741016Sdfr
56841016Sdfrstatic int
56941016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
57041016Sdfr{
571178019Sjkim	int cmd;
572178019Sjkim	int res;
573178019Sjkim	int i;
57441016Sdfr
575178019Sjkim	switch (flag) {
576178019Sjkim	case 0:
577178019Sjkim	default:
578178019Sjkim		cmd = PSMC_SEND_DEV_STATUS;
579178019Sjkim		break;
580178019Sjkim	case 1:
581178019Sjkim		cmd = PSMC_SEND_DEV_DATA;
582178019Sjkim		break;
583178019Sjkim	}
584178019Sjkim	empty_aux_buffer(kbdc, 5);
585178019Sjkim	res = send_aux_command(kbdc, cmd);
586178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
587178019Sjkim	    (flag == 1) ? "DATA" : "STATUS", res));
588178019Sjkim	if (res != PSM_ACK)
589178019Sjkim		return (0);
59041016Sdfr
591178019Sjkim	for (i = 0; i < len; ++i) {
592178019Sjkim		status[i] = read_aux_data(kbdc);
593178019Sjkim		if (status[i] < 0)
594178019Sjkim			break;
595178019Sjkim	}
59641016Sdfr
597178019Sjkim	VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
598178019Sjkim	    (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
59941016Sdfr
600178019Sjkim	return (i);
60141016Sdfr}
60241016Sdfr
60341016Sdfrstatic int
60441016Sdfrget_aux_id(KBDC kbdc)
60541016Sdfr{
606178019Sjkim	int res;
607178019Sjkim	int id;
60841016Sdfr
609178019Sjkim	empty_aux_buffer(kbdc, 5);
610178019Sjkim	res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
611178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
612178019Sjkim	if (res != PSM_ACK)
613178019Sjkim		return (-1);
61441016Sdfr
615178019Sjkim	/* 10ms delay */
616178019Sjkim	DELAY(10000);
61741016Sdfr
618178019Sjkim	id = read_aux_data(kbdc);
619178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
62041016Sdfr
621178019Sjkim	return (id);
62241016Sdfr}
62341016Sdfr
62441016Sdfrstatic int
62541016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
62641016Sdfr{
627178019Sjkim	int res;
62841016Sdfr
629178019Sjkim	res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
630178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
63141016Sdfr
632178019Sjkim	return ((res == PSM_ACK) ? rate : -1);
63341016Sdfr}
63441016Sdfr
63541016Sdfrstatic int
63641016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
63741016Sdfr{
638178019Sjkim	int res;
63941016Sdfr
640178019Sjkim	switch (scale) {
641178019Sjkim	case 1:
642178019Sjkim	default:
643178019Sjkim		scale = PSMC_SET_SCALING11;
644178019Sjkim		break;
645178019Sjkim	case 2:
646178019Sjkim		scale = PSMC_SET_SCALING21;
647178019Sjkim		break;
648178019Sjkim	}
649178019Sjkim	res = send_aux_command(kbdc, scale);
650178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
651178019Sjkim	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
65241016Sdfr
653178019Sjkim	return (res == PSM_ACK);
65441016Sdfr}
65541016Sdfr
65641016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
65741016Sdfrstatic int
65841016Sdfrset_mouse_resolution(KBDC kbdc, int val)
65941016Sdfr{
660178019Sjkim	int res;
66141016Sdfr
662178019Sjkim	res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
663178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
66441016Sdfr
665178019Sjkim	return ((res == PSM_ACK) ? val : -1);
66641016Sdfr}
66741016Sdfr
66841016Sdfr/*
66941016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
67041016Sdfr * re-enabled by calling `enable_aux_dev()'
67141016Sdfr */
67241016Sdfrstatic int
67341016Sdfrset_mouse_mode(KBDC kbdc)
67441016Sdfr{
675178019Sjkim	int res;
67641016Sdfr
677178019Sjkim	res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
678178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
67941016Sdfr
680178019Sjkim	return (res == PSM_ACK);
68141016Sdfr}
68241016Sdfr
68341016Sdfrstatic int
68441016Sdfrget_mouse_buttons(KBDC kbdc)
68541016Sdfr{
686178019Sjkim	int c = 2;		/* assume two buttons by default */
687178019Sjkim	int status[3];
68841016Sdfr
689178019Sjkim	/*
690178019Sjkim	 * NOTE: a special sequence to obtain Logitech Mouse specific
691178019Sjkim	 * information: set resolution to 25 ppi, set scaling to 1:1, set
692178019Sjkim	 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
693178019Sjkim	 * mouse status bytes is the number of available buttons.
694178019Sjkim	 * Some manufactures also support this sequence.
695178019Sjkim	 */
696178019Sjkim	if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
697178019Sjkim		return (c);
698178019Sjkim	if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
699178019Sjkim	    set_mouse_scaling(kbdc, 1) &&
700178019Sjkim	    get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
701178019Sjkim		return (status[1]);
702178019Sjkim	return (c);
70341016Sdfr}
70441016Sdfr
70541016Sdfr/* misc subroutines */
70641016Sdfr/*
70741016Sdfr * Someday, I will get the complete list of valid pointing devices and
70841016Sdfr * their IDs... XXX
70941016Sdfr */
71041016Sdfrstatic int
71141016Sdfris_a_mouse(int id)
71241016Sdfr{
71341016Sdfr#if 0
714178019Sjkim	static int valid_ids[] = {
715178019Sjkim		PSM_MOUSE_ID,		/* mouse */
716178019Sjkim		PSM_BALLPOINT_ID,	/* ballpoint device */
717178019Sjkim		PSM_INTELLI_ID,		/* Intellimouse */
718178019Sjkim		PSM_EXPLORER_ID,	/* Intellimouse Explorer */
719178019Sjkim		-1			/* end of table */
720178019Sjkim	};
721178019Sjkim	int i;
72241016Sdfr
723178019Sjkim	for (i = 0; valid_ids[i] >= 0; ++i)
724178019Sjkim	if (valid_ids[i] == id)
725178019Sjkim		return (TRUE);
726178019Sjkim	return (FALSE);
72741016Sdfr#else
728178019Sjkim	return (TRUE);
72941016Sdfr#endif
73041016Sdfr}
73141016Sdfr
73241016Sdfrstatic char *
73341016Sdfrmodel_name(int model)
73441016Sdfr{
735178019Sjkim	static struct {
736178019Sjkim		int	model_code;
737178019Sjkim		char	*model_name;
738178019Sjkim	} models[] = {
739178019Sjkim		{ MOUSE_MODEL_NETSCROLL,	"NetScroll" },
740178019Sjkim		{ MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
741178019Sjkim		{ MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
742178019Sjkim		{ MOUSE_MODEL_THINK,		"ThinkingMouse" },
743178019Sjkim		{ MOUSE_MODEL_INTELLI,		"IntelliMouse" },
744178019Sjkim		{ MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
745178019Sjkim		{ MOUSE_MODEL_VERSAPAD,		"VersaPad" },
746178019Sjkim		{ MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
747178019Sjkim		{ MOUSE_MODEL_4D,		"4D Mouse" },
748178019Sjkim		{ MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
749178019Sjkim		{ MOUSE_MODEL_SYNAPTICS,	"Synaptics Touchpad" },
750248479Sjkim		{ MOUSE_MODEL_TRACKPOINT,	"IBM/Lenovo TrackPoint" },
751178019Sjkim		{ MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
752178019Sjkim		{ MOUSE_MODEL_UNKNOWN,		"Unknown" },
753178019Sjkim	};
754178019Sjkim	int i;
75541016Sdfr
756178019Sjkim	for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
757178019Sjkim		if (models[i].model_code == model)
758178019Sjkim			break;
759178019Sjkim	return (models[i].model_name);
76041016Sdfr}
76141016Sdfr
76241016Sdfrstatic void
76341016Sdfrrecover_from_error(KBDC kbdc)
76441016Sdfr{
765178019Sjkim	/* discard anything left in the output buffer */
766178019Sjkim	empty_both_buffers(kbdc, 10);
76741016Sdfr
76841016Sdfr#if 0
769178019Sjkim	/*
770178019Sjkim	 * NOTE: KBDC_RESET_KBD may not restore the communication between the
771178019Sjkim	 * keyboard and the controller.
772178019Sjkim	 */
773178019Sjkim	reset_kbd(kbdc);
77441016Sdfr#else
775178019Sjkim	/*
776178019Sjkim	 * NOTE: somehow diagnostic and keyboard port test commands bring the
777178019Sjkim	 * keyboard back.
778178019Sjkim	 */
779178019Sjkim	if (!test_controller(kbdc))
780178019Sjkim		log(LOG_ERR, "psm: keyboard controller failed.\n");
781178019Sjkim	/* if there isn't a keyboard in the system, the following error is OK */
782178019Sjkim	if (test_kbd_port(kbdc) != 0)
783178019Sjkim		VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
78441016Sdfr#endif
78541016Sdfr}
78641016Sdfr
78741016Sdfrstatic int
78841016Sdfrrestore_controller(KBDC kbdc, int command_byte)
78941016Sdfr{
790178019Sjkim	empty_both_buffers(kbdc, 10);
79141016Sdfr
792178019Sjkim	if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
793178019Sjkim		log(LOG_ERR, "psm: failed to restore the keyboard controller "
794178019Sjkim		    "command byte.\n");
795178019Sjkim		empty_both_buffers(kbdc, 10);
796178019Sjkim		return (FALSE);
797178019Sjkim	} else {
798178019Sjkim		empty_both_buffers(kbdc, 10);
799178019Sjkim		return (TRUE);
800178019Sjkim	}
80141016Sdfr}
80241016Sdfr
803178019Sjkim/*
80441016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
805178019Sjkim * and its interrupt must be disabled before calling this routine.
80641016Sdfr * The aux device will be disabled before returning.
80741016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
80841016Sdfr * calling this routine.
80941016Sdfr */
81041016Sdfrstatic int
81184880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode)
81241016Sdfr{
813178019Sjkim	KBDC kbdc = sc->kbdc;
814178019Sjkim	int stat[3];
815178019Sjkim	int i;
81641016Sdfr
817178019Sjkim	switch((i = test_aux_port(kbdc))) {
818178019Sjkim	case 1:	/* ignore these errors */
819178019Sjkim	case 2:
820178019Sjkim	case 3:
821178019Sjkim	case PSM_ACK:
822178019Sjkim		if (verbose)
823178019Sjkim			log(LOG_DEBUG,
824178019Sjkim			    "psm%d: strange result for test aux port (%d).\n",
825178019Sjkim			    sc->unit, i);
826178019Sjkim		/* FALLTHROUGH */
827178019Sjkim	case 0:		/* no error */
828178019Sjkim		break;
829178019Sjkim	case -1:	/* time out */
830178019Sjkim	default:	/* error */
831178019Sjkim		recover_from_error(kbdc);
832178019Sjkim		if (sc->config & PSM_CONFIG_IGNPORTERROR)
833178019Sjkim			break;
834178019Sjkim		log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
835178019Sjkim		    sc->unit, i);
836178019Sjkim		return (FALSE);
837178019Sjkim	}
83841016Sdfr
839178019Sjkim	if (sc->config & PSM_CONFIG_NORESET) {
840178019Sjkim		/*
841178019Sjkim		 * Don't try to reset the pointing device.  It may possibly
842178019Sjkim		 * be left in the unknown state, though...
843178019Sjkim		 */
844178019Sjkim	} else {
845178019Sjkim		/*
846178019Sjkim		 * NOTE: some controllers appears to hang the `keyboard' when
847178019Sjkim		 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
848178019Sjkim		 */
849178019Sjkim		if (!reset_aux_dev(kbdc)) {
850178019Sjkim			recover_from_error(kbdc);
851178019Sjkim			log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
852178019Sjkim			    sc->unit);
853178019Sjkim			return (FALSE);
854178019Sjkim		}
855178019Sjkim	}
856178019Sjkim
857178019Sjkim	/*
858178019Sjkim	 * both the aux port and the aux device is functioning, see
859178019Sjkim	 * if the device can be enabled.
86045789Speter	 */
861178019Sjkim	if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
862178019Sjkim		log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
863178019Sjkim		    sc->unit);
864178019Sjkim		return (FALSE);
86545789Speter	}
866178019Sjkim	empty_both_buffers(kbdc, 10);	/* remove stray data if any */
86741016Sdfr
868233580Sjkim	/* Re-enable the mouse. */
869233580Sjkim	for (i = 0; vendortype[i].probefunc != NULL; ++i)
870233580Sjkim		if (vendortype[i].model == sc->hw.model)
871233580Sjkim			(*vendortype[i].probefunc)(sc->kbdc, NULL);
87241016Sdfr
873178019Sjkim	/* set mouse parameters */
874178019Sjkim	if (mode != (mousemode_t *)NULL) {
875178019Sjkim		if (mode->rate > 0)
876178019Sjkim			mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
877178019Sjkim		if (mode->resolution >= 0)
878178019Sjkim			mode->resolution =
879178019Sjkim			    set_mouse_resolution(kbdc, mode->resolution);
880178019Sjkim		set_mouse_scaling(kbdc, 1);
881178019Sjkim		set_mouse_mode(kbdc);
882178019Sjkim	}
88341016Sdfr
884178019Sjkim	/* Record sync on the next data packet we see. */
885178019Sjkim	sc->flags |= PSM_NEED_SYNCBITS;
88641016Sdfr
887178019Sjkim	/* just check the status of the mouse */
888178019Sjkim	if (get_mouse_status(kbdc, stat, 0, 3) < 3)
889178019Sjkim		log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
890178019Sjkim		    sc->unit);
89141016Sdfr
892178019Sjkim	return (TRUE);
89341016Sdfr}
89441016Sdfr
89541016Sdfrstatic int
89684880Syokotadoopen(struct psm_softc *sc, int command_byte)
89741016Sdfr{
898178019Sjkim	int stat[3];
89941016Sdfr
900183888Sdumbbell	/*
901183888Sdumbbell	 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
902183888Sdumbbell	 * no obvious reason. Thus we check the current mode and restore the
903183888Sdumbbell	 * Absolute Mode if it was cleared.
904183888Sdumbbell	 *
905183888Sdumbbell	 * The previous hack at the end of psmprobe() wasn't efficient when
906183888Sdumbbell	 * moused(8) was restarted.
907183888Sdumbbell	 *
908183888Sdumbbell	 * A Reset (FF) or Set Defaults (F6) command would clear the
909183888Sdumbbell	 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
910183888Sdumbbell	 * doesn't show any evidence of such a command.
911183888Sdumbbell	 */
912183888Sdumbbell	if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
913183888Sdumbbell		mouse_ext_command(sc->kbdc, 1);
914183888Sdumbbell		get_mouse_status(sc->kbdc, stat, 0, 3);
915244405Sdumbbell		if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
916244405Sdumbbell		     stat[1] == 0x47) &&
917244405Sdumbbell		    stat[2] == 0x40) {
918183888Sdumbbell			/* Set the mode byte -- request wmode where
919183888Sdumbbell			 * available */
920183888Sdumbbell			if (sc->synhw.capExtended)
921183888Sdumbbell				mouse_ext_command(sc->kbdc, 0xc1);
922183888Sdumbbell			else
923183888Sdumbbell				mouse_ext_command(sc->kbdc, 0xc0);
924183888Sdumbbell			set_mouse_sampling_rate(sc->kbdc, 20);
925183888Sdumbbell			VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
926183888Sdumbbell			    "hopefully restored\n",
927183888Sdumbbell			    sc->unit));
928183888Sdumbbell		}
929183888Sdumbbell	}
930183888Sdumbbell
931200674Sdumbbell	/*
932200674Sdumbbell	 * A user may want to disable tap and drag gestures on a Synaptics
933200674Sdumbbell	 * TouchPad when it operates in Relative Mode.
934200674Sdumbbell	 */
935200674Sdumbbell	if (sc->hw.model == MOUSE_MODEL_GENERIC) {
936200674Sdumbbell		if (tap_enabled > 0) {
937200674Sdumbbell			/*
938200674Sdumbbell			 * Enable tap & drag gestures. We use a Mode Byte
939200674Sdumbbell			 * and clear the DisGest bit (see ��2.5 of Synaptics
940200674Sdumbbell			 * TouchPad Interfacing Guide).
941200674Sdumbbell			 */
942200674Sdumbbell			VLOG(2, (LOG_DEBUG,
943200674Sdumbbell			    "psm%d: enable tap and drag gestures\n",
944200674Sdumbbell			    sc->unit));
945200674Sdumbbell			mouse_ext_command(sc->kbdc, 0x00);
946200674Sdumbbell			set_mouse_sampling_rate(sc->kbdc, 20);
947200674Sdumbbell		} else if (tap_enabled == 0) {
948200674Sdumbbell			/*
949200674Sdumbbell			 * Disable tap & drag gestures. We use a Mode Byte
950200674Sdumbbell			 * and set the DisGest bit (see ��2.5 of Synaptics
951200674Sdumbbell			 * TouchPad Interfacing Guide).
952200674Sdumbbell			 */
953200674Sdumbbell			VLOG(2, (LOG_DEBUG,
954200674Sdumbbell			    "psm%d: disable tap and drag gestures\n",
955200674Sdumbbell			    sc->unit));
956200674Sdumbbell			mouse_ext_command(sc->kbdc, 0x04);
957200674Sdumbbell			set_mouse_sampling_rate(sc->kbdc, 20);
958200674Sdumbbell		}
959200674Sdumbbell	}
960200674Sdumbbell
961178019Sjkim	/* enable the mouse device */
962178019Sjkim	if (!enable_aux_dev(sc->kbdc)) {
963178019Sjkim		/* MOUSE ERROR: failed to enable the mouse because:
964178019Sjkim		 * 1) the mouse is faulty,
965178019Sjkim		 * 2) the mouse has been removed(!?)
966178019Sjkim		 * In the latter case, the keyboard may have hung, and need
967178019Sjkim		 * recovery procedure...
968178019Sjkim		 */
969178019Sjkim		recover_from_error(sc->kbdc);
97041016Sdfr#if 0
971178019Sjkim		/* FIXME: we could reset the mouse here and try to enable
972178019Sjkim		 * it again. But it will take long time and it's not a good
973178019Sjkim		 * idea to disable the keyboard that long...
974178019Sjkim		 */
975178019Sjkim		if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
976178019Sjkim			recover_from_error(sc->kbdc);
97741016Sdfr#else
978178019Sjkim		{
97941016Sdfr#endif
980178019Sjkim			restore_controller(sc->kbdc, command_byte);
981178019Sjkim			/* mark this device is no longer available */
982178019Sjkim			sc->state &= ~PSM_VALID;
983178019Sjkim			log(LOG_ERR,
984178019Sjkim			    "psm%d: failed to enable the device (doopen).\n",
985178019Sjkim			sc->unit);
986178019Sjkim			return (EIO);
987178019Sjkim		}
98841016Sdfr	}
98941016Sdfr
990178019Sjkim	if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
991178019Sjkim		log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
992178019Sjkim		    sc->unit);
99341016Sdfr
994178019Sjkim	/* enable the aux port and interrupt */
995178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
99641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
997178019Sjkim	    (command_byte & KBD_KBD_CONTROL_BITS) |
998178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
999178019Sjkim		/* CONTROLLER ERROR */
1000178019Sjkim		disable_aux_dev(sc->kbdc);
1001178019Sjkim		restore_controller(sc->kbdc, command_byte);
1002178019Sjkim		log(LOG_ERR,
1003178019Sjkim		    "psm%d: failed to enable the aux interrupt (doopen).\n",
1004178019Sjkim		    sc->unit);
1005178019Sjkim		return (EIO);
1006178019Sjkim	}
100741016Sdfr
1008178019Sjkim	/* start the watchdog timer */
1009178019Sjkim	sc->watchdog = FALSE;
1010240211Sjhb	callout_reset(&sc->callout, hz * 2, psmtimeout, sc);
101158230Syokota
1012178019Sjkim	return (0);
101341016Sdfr}
101441016Sdfr
101584880Syokotastatic int
101684880Syokotareinitialize(struct psm_softc *sc, int doinit)
101784880Syokota{
1018178019Sjkim	int err;
1019178019Sjkim	int c;
1020178019Sjkim	int s;
102184880Syokota
1022178019Sjkim	/* don't let anybody mess with the aux device */
1023178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
1024178019Sjkim		return (EIO);
1025178019Sjkim	s = spltty();
102684880Syokota
1027178019Sjkim	/* block our watchdog timer */
1028178019Sjkim	sc->watchdog = FALSE;
1029240211Sjhb	callout_stop(&sc->callout);
103084880Syokota
1031178019Sjkim	/* save the current controller command byte */
1032178019Sjkim	empty_both_buffers(sc->kbdc, 10);
1033178019Sjkim	c = get_controller_command_byte(sc->kbdc);
1034178019Sjkim	VLOG(2, (LOG_DEBUG,
1035178019Sjkim	    "psm%d: current command byte: %04x (reinitialize).\n",
1036178019Sjkim	    sc->unit, c));
103784880Syokota
1038178019Sjkim	/* enable the aux port but disable the aux interrupt and the keyboard */
1039178019Sjkim	if ((c == -1) || !set_controller_command_byte(sc->kbdc,
104084880Syokota	    kbdc_get_device_mask(sc->kbdc),
1041178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1042178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1043178019Sjkim		/* CONTROLLER ERROR */
1044178019Sjkim		splx(s);
1045178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
1046178019Sjkim		log(LOG_ERR,
1047178019Sjkim		    "psm%d: unable to set the command byte (reinitialize).\n",
1048178019Sjkim		    sc->unit);
1049178019Sjkim		return (EIO);
1050178019Sjkim	}
105184880Syokota
1052178019Sjkim	/* flush any data */
1053178019Sjkim	if (sc->state & PSM_VALID) {
1054178019Sjkim		/* this may fail; but never mind... */
1055178019Sjkim		disable_aux_dev(sc->kbdc);
1056178019Sjkim		empty_aux_buffer(sc->kbdc, 10);
1057178019Sjkim	}
1058178019Sjkim	flushpackets(sc);
1059178019Sjkim	sc->syncerrors = 0;
1060178019Sjkim	sc->pkterrors = 0;
1061178019Sjkim	memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
106284880Syokota
1063178019Sjkim	/* try to detect the aux device; are you still there? */
1064178019Sjkim	err = 0;
1065178019Sjkim	if (doinit) {
1066178019Sjkim		if (doinitialize(sc, &sc->mode)) {
1067178019Sjkim			/* yes */
1068178019Sjkim			sc->state |= PSM_VALID;
1069178019Sjkim		} else {
1070178019Sjkim			/* the device has gone! */
1071178019Sjkim			restore_controller(sc->kbdc, c);
1072178019Sjkim			sc->state &= ~PSM_VALID;
1073178019Sjkim			log(LOG_ERR,
1074178019Sjkim			    "psm%d: the aux device has gone! (reinitialize).\n",
1075178019Sjkim			    sc->unit);
1076178019Sjkim			err = ENXIO;
1077178019Sjkim		}
107884880Syokota	}
1079178019Sjkim	splx(s);
108084880Syokota
1081178019Sjkim	/* restore the driver state */
1082178019Sjkim	if ((sc->state & PSM_OPEN) && (err == 0)) {
1083178019Sjkim		/* enable the aux device and the port again */
1084178019Sjkim		err = doopen(sc, c);
1085178019Sjkim		if (err != 0)
1086178019Sjkim			log(LOG_ERR, "psm%d: failed to enable the device "
1087178019Sjkim			    "(reinitialize).\n", sc->unit);
1088178019Sjkim	} else {
1089178019Sjkim		/* restore the keyboard port and disable the aux port */
1090178019Sjkim		if (!set_controller_command_byte(sc->kbdc,
1091178019Sjkim		    kbdc_get_device_mask(sc->kbdc),
1092178019Sjkim		    (c & KBD_KBD_CONTROL_BITS) |
1093178019Sjkim		    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1094178019Sjkim			/* CONTROLLER ERROR */
1095178019Sjkim			log(LOG_ERR, "psm%d: failed to disable the aux port "
1096178019Sjkim			    "(reinitialize).\n", sc->unit);
1097178019Sjkim			err = EIO;
1098178019Sjkim		}
109984880Syokota	}
110084880Syokota
1101178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1102178019Sjkim	return (err);
110384880Syokota}
110484880Syokota
110541016Sdfr/* psm driver entry points */
110641016Sdfr
110783147Syokotastatic void
110883147Syokotapsmidentify(driver_t *driver, device_t parent)
110983147Syokota{
1110178019Sjkim	device_t psmc;
1111178019Sjkim	device_t psm;
1112178019Sjkim	u_long irq;
1113178019Sjkim	int unit;
111483147Syokota
1115178019Sjkim	unit = device_get_unit(parent);
111683931Syokota
1117178019Sjkim	/* always add at least one child */
1118178019Sjkim	psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1119178019Sjkim	if (psm == NULL)
1120178019Sjkim		return;
112183931Syokota
1122178019Sjkim	irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1123178019Sjkim	if (irq > 0)
1124178019Sjkim		return;
112583931Syokota
1126178019Sjkim	/*
1127178019Sjkim	 * If the PS/2 mouse device has already been reported by ACPI or
1128178019Sjkim	 * PnP BIOS, obtain the IRQ resource from it.
1129178019Sjkim	 * (See psmcpnp_attach() below.)
1130178019Sjkim	 */
1131178019Sjkim	psmc = device_find_child(device_get_parent(parent),
1132178019Sjkim	    PSMCPNP_DRIVER_NAME, unit);
1133178019Sjkim	if (psmc == NULL)
1134178019Sjkim		return;
1135178019Sjkim	irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1136178019Sjkim	if (irq <= 0)
1137178019Sjkim		return;
1138216491Sjhb	bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1139178019Sjkim	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
114083147Syokota}
114183147Syokota
1142178019Sjkim#define	endprobe(v)	do {			\
1143178019Sjkim	if (bootverbose)			\
1144178019Sjkim		--verbose;			\
1145178019Sjkim	kbdc_set_device_mask(sc->kbdc, mask);	\
1146178019Sjkim	kbdc_lock(sc->kbdc, FALSE);		\
1147178019Sjkim	return (v);				\
1148178019Sjkim} while (0)
114941016Sdfr
115041016Sdfrstatic int
115141016Sdfrpsmprobe(device_t dev)
115241016Sdfr{
1153178019Sjkim	int unit = device_get_unit(dev);
1154178019Sjkim	struct psm_softc *sc = device_get_softc(dev);
1155178019Sjkim	int stat[3];
1156178019Sjkim	int command_byte;
1157178019Sjkim	int mask;
1158178019Sjkim	int rid;
1159178019Sjkim	int i;
116041016Sdfr
116141016Sdfr#if 0
1162178019Sjkim	kbdc_debug(TRUE);
116341016Sdfr#endif
116458230Syokota
1165178019Sjkim	/* see if IRQ is available */
1166178019Sjkim	rid = KBDC_RID_AUX;
1167216491Sjhb	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1168178019Sjkim	if (sc->intr == NULL) {
1169178019Sjkim		if (bootverbose)
1170178019Sjkim			device_printf(dev, "unable to allocate IRQ\n");
1171178019Sjkim		return (ENXIO);
1172178019Sjkim	}
1173178019Sjkim	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
117458230Syokota
1175178019Sjkim	sc->unit = unit;
1176178019Sjkim	sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1177178019Sjkim	sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1178178019Sjkim	/* XXX: for backward compatibility */
117958230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1180178019Sjkim	sc->config |=
118158230Syokota#ifdef PSM_RESETAFTERSUSPEND
1182233580Sjkim	PSM_CONFIG_INITAFTERSUSPEND;
118358230Syokota#else
118458230Syokota	PSM_CONFIG_HOOKRESUME;
118558230Syokota#endif
118658230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1187178019Sjkim	sc->flags = 0;
1188178019Sjkim	if (bootverbose)
1189178019Sjkim		++verbose;
119041016Sdfr
1191178019Sjkim	device_set_desc(dev, "PS/2 Mouse");
119243105Sdfr
1193178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE)) {
1194178019Sjkim		printf("psm%d: unable to lock the controller.\n", unit);
1195178019Sjkim		if (bootverbose)
1196178019Sjkim			--verbose;
1197178019Sjkim		return (ENXIO);
1198178019Sjkim	}
119941016Sdfr
1200178019Sjkim	/*
1201178019Sjkim	 * NOTE: two bits in the command byte controls the operation of the
1202178019Sjkim	 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1203178019Sjkim	 * port interrupt (IRQ 12) enable bit (bit 2).
1204178019Sjkim	 */
120541016Sdfr
1206178019Sjkim	/* discard anything left after the keyboard initialization */
1207178019Sjkim	empty_both_buffers(sc->kbdc, 10);
120841016Sdfr
1209178019Sjkim	/* save the current command byte; it will be used later */
1210178019Sjkim	mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1211178019Sjkim	command_byte = get_controller_command_byte(sc->kbdc);
1212178019Sjkim	if (verbose)
1213178019Sjkim		printf("psm%d: current command byte:%04x\n", unit,
1214178019Sjkim		    command_byte);
1215178019Sjkim	if (command_byte == -1) {
1216178019Sjkim		/* CONTROLLER ERROR */
1217178019Sjkim		printf("psm%d: unable to get the current command byte value.\n",
1218178019Sjkim			unit);
1219178019Sjkim		endprobe(ENXIO);
1220178019Sjkim	}
122141016Sdfr
1222178019Sjkim	/*
1223178019Sjkim	 * disable the keyboard port while probing the aux port, which must be
1224178019Sjkim	 * enabled during this routine
1225178019Sjkim	 */
1226178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
122741016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1228178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1229178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1230178019Sjkim		/*
1231178019Sjkim		 * this is CONTROLLER ERROR; I don't know how to recover
1232178019Sjkim		 * from this error...
1233178019Sjkim		 */
1234178019Sjkim		restore_controller(sc->kbdc, command_byte);
1235178019Sjkim		printf("psm%d: unable to set the command byte.\n", unit);
1236178019Sjkim		endprobe(ENXIO);
1237178019Sjkim	}
1238178019Sjkim	write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
123941016Sdfr
124045789Speter	/*
1241178019Sjkim	 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1242178019Sjkim	 * port exists and is functioning. However, some controllers appears
1243178019Sjkim	 * to respond with zero even when the aux port doesn't exist. (It may
1244178019Sjkim	 * be that this is only the case when the controller DOES have the aux
1245178019Sjkim	 * port but the port is not wired on the motherboard.) The keyboard
1246178019Sjkim	 * controllers without the port, such as the original AT, are
1247215473Sjhb	 * supposed to return with an error code or simply time out. In any
1248178019Sjkim	 * case, we have to continue probing the port even when the controller
1249178019Sjkim	 * passes this test.
1250117302Smikeh	 *
1251178019Sjkim	 * XXX: some controllers erroneously return the error code 1, 2 or 3
1252215473Sjhb	 * when it has a perfectly functional aux port. We have to ignore
1253178019Sjkim	 * this error code. Even if the controller HAS error with the aux
1254178019Sjkim	 * port, it will be detected later...
1255178019Sjkim	 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
125645789Speter	 */
1257178019Sjkim	switch ((i = test_aux_port(sc->kbdc))) {
1258178019Sjkim	case 1:		/* ignore these errors */
1259178019Sjkim	case 2:
1260178019Sjkim	case 3:
1261178019Sjkim	case PSM_ACK:
1262178019Sjkim		if (verbose)
1263178019Sjkim			printf("psm%d: strange result for test aux port "
1264178019Sjkim			    "(%d).\n", unit, i);
1265178019Sjkim		/* FALLTHROUGH */
1266178019Sjkim	case 0:		/* no error */
1267178019Sjkim		break;
1268178019Sjkim	case -1:	/* time out */
1269178019Sjkim	default:	/* error */
1270178019Sjkim		recover_from_error(sc->kbdc);
1271178019Sjkim		if (sc->config & PSM_CONFIG_IGNPORTERROR)
1272178019Sjkim			break;
1273178019Sjkim		restore_controller(sc->kbdc, command_byte);
1274178019Sjkim		if (verbose)
1275178019Sjkim			printf("psm%d: the aux port is not functioning (%d).\n",
1276178019Sjkim			    unit, i);
1277178019Sjkim		endprobe(ENXIO);
127845789Speter	}
127945789Speter
1280178019Sjkim	if (sc->config & PSM_CONFIG_NORESET) {
1281178019Sjkim		/*
1282178019Sjkim		 * Don't try to reset the pointing device.  It may possibly be
1283215473Sjhb		 * left in an unknown state, though...
1284178019Sjkim		 */
1285178019Sjkim	} else {
1286178019Sjkim		/*
1287178019Sjkim		 * NOTE: some controllers appears to hang the `keyboard' when
1288178019Sjkim		 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1289178019Sjkim		 *
1290178019Sjkim		 * Attempt to reset the controller twice -- this helps
1291178019Sjkim		 * pierce through some KVM switches. The second reset
1292178019Sjkim		 * is non-fatal.
1293178019Sjkim		 */
1294178019Sjkim		if (!reset_aux_dev(sc->kbdc)) {
1295178019Sjkim			recover_from_error(sc->kbdc);
1296178019Sjkim			restore_controller(sc->kbdc, command_byte);
1297178019Sjkim			if (verbose)
1298178019Sjkim				printf("psm%d: failed to reset the aux "
1299178019Sjkim				    "device.\n", unit);
1300178019Sjkim			endprobe(ENXIO);
1301178019Sjkim		} else if (!reset_aux_dev(sc->kbdc)) {
1302178019Sjkim			recover_from_error(sc->kbdc);
1303178019Sjkim			if (verbose >= 2)
1304178019Sjkim				printf("psm%d: failed to reset the aux device "
1305178019Sjkim				    "(2).\n", unit);
1306178019Sjkim		}
1307178019Sjkim	}
130841016Sdfr
1309178019Sjkim	/*
1310215473Sjhb	 * both the aux port and the aux device are functioning, see if the
1311178019Sjkim	 * device can be enabled. NOTE: when enabled, the device will start
1312178019Sjkim	 * sending data; we shall immediately disable the device once we know
1313178019Sjkim	 * the device can be enabled.
1314178019Sjkim	 */
1315178019Sjkim	if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1316178019Sjkim		/* MOUSE ERROR */
1317178019Sjkim		recover_from_error(sc->kbdc);
1318178019Sjkim		restore_controller(sc->kbdc, command_byte);
1319178019Sjkim		if (verbose)
1320178019Sjkim			printf("psm%d: failed to enable the aux device.\n",
1321178019Sjkim			    unit);
1322178019Sjkim		endprobe(ENXIO);
1323178019Sjkim	}
132441016Sdfr
1325178019Sjkim	/* save the default values after reset */
1326178019Sjkim	if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1327178019Sjkim		sc->dflt_mode.rate = sc->mode.rate = stat[2];
1328178019Sjkim		sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1329178019Sjkim	} else {
1330178019Sjkim		sc->dflt_mode.rate = sc->mode.rate = -1;
1331178019Sjkim		sc->dflt_mode.resolution = sc->mode.resolution = -1;
1332178019Sjkim	}
133341016Sdfr
1334178019Sjkim	/* hardware information */
1335178019Sjkim	sc->hw.iftype = MOUSE_IF_PS2;
133641016Sdfr
1337178019Sjkim	/* verify the device is a mouse */
1338178019Sjkim	sc->hw.hwid = get_aux_id(sc->kbdc);
1339178019Sjkim	if (!is_a_mouse(sc->hw.hwid)) {
1340178019Sjkim		restore_controller(sc->kbdc, command_byte);
1341178019Sjkim		if (verbose)
1342178019Sjkim			printf("psm%d: unknown device type (%d).\n", unit,
1343178019Sjkim			    sc->hw.hwid);
1344178019Sjkim		endprobe(ENXIO);
1345178019Sjkim	}
1346178019Sjkim	switch (sc->hw.hwid) {
1347178019Sjkim	case PSM_BALLPOINT_ID:
1348178019Sjkim		sc->hw.type = MOUSE_TRACKBALL;
134945789Speter		break;
1350178019Sjkim	case PSM_MOUSE_ID:
1351178019Sjkim	case PSM_INTELLI_ID:
1352178019Sjkim	case PSM_EXPLORER_ID:
1353178019Sjkim	case PSM_4DMOUSE_ID:
1354178019Sjkim	case PSM_4DPLUS_ID:
1355178019Sjkim		sc->hw.type = MOUSE_MOUSE;
1356178019Sjkim		break;
1357178019Sjkim	default:
1358178019Sjkim		sc->hw.type = MOUSE_UNKNOWN;
1359178019Sjkim		break;
136041016Sdfr	}
136141016Sdfr
1362178019Sjkim	if (sc->config & PSM_CONFIG_NOIDPROBE) {
1363178019Sjkim		sc->hw.buttons = 2;
1364178019Sjkim		i = GENERIC_MOUSE_ENTRY;
1365178019Sjkim	} else {
1366178019Sjkim		/* # of buttons */
1367178019Sjkim		sc->hw.buttons = get_mouse_buttons(sc->kbdc);
136841016Sdfr
1369178019Sjkim		/* other parameters */
1370178019Sjkim		for (i = 0; vendortype[i].probefunc != NULL; ++i)
1371233580Sjkim			if ((*vendortype[i].probefunc)(sc->kbdc, sc)) {
1372178019Sjkim				if (verbose >= 2)
1373178019Sjkim					printf("psm%d: found %s\n", unit,
1374178019Sjkim					    model_name(vendortype[i].model));
1375178019Sjkim				break;
1376178019Sjkim			}
1377178019Sjkim	}
137841016Sdfr
1379178019Sjkim	sc->hw.model = vendortype[i].model;
1380178019Sjkim
1381178019Sjkim	sc->dflt_mode.level = PSM_LEVEL_BASE;
1382178019Sjkim	sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1383178019Sjkim	sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1384178019Sjkim	if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1385178019Sjkim		sc->dflt_mode.syncmask[0] = 0;
1386178019Sjkim	else
1387178019Sjkim		sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1388178019Sjkim	if (sc->config & PSM_CONFIG_FORCETAP)
1389178019Sjkim		sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1390178019Sjkim	sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
1391178019Sjkim	sc->mode = sc->dflt_mode;
1392178019Sjkim	sc->mode.packetsize = vendortype[i].packetsize;
1393178019Sjkim
1394178019Sjkim	/* set mouse parameters */
139548773Syokota#if 0
1396178019Sjkim	/*
1397178019Sjkim	 * A version of Logitech FirstMouse+ won't report wheel movement,
1398178019Sjkim	 * if SET_DEFAULTS is sent...  Don't use this command.
1399178019Sjkim	 * This fix was found by Takashi Nishida.
1400178019Sjkim	 */
1401178019Sjkim	i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1402178019Sjkim	if (verbose >= 2)
1403178019Sjkim		printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
140448773Syokota#endif
1405178019Sjkim	if (sc->config & PSM_CONFIG_RESOLUTION)
1406178019Sjkim		sc->mode.resolution =
1407178019Sjkim		    set_mouse_resolution(sc->kbdc,
1408178019Sjkim		    (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1409178019Sjkim	else if (sc->mode.resolution >= 0)
1410178019Sjkim		sc->mode.resolution =
1411178019Sjkim		    set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1412178019Sjkim	if (sc->mode.rate > 0)
1413178019Sjkim		sc->mode.rate =
1414178019Sjkim		    set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1415178019Sjkim	set_mouse_scaling(sc->kbdc, 1);
141641016Sdfr
1417178019Sjkim	/* Record sync on the next data packet we see. */
1418178019Sjkim	sc->flags |= PSM_NEED_SYNCBITS;
141941016Sdfr
1420178019Sjkim	/* just check the status of the mouse */
1421178019Sjkim	/*
1422178019Sjkim	 * NOTE: XXX there are some arcane controller/mouse combinations out
1423178019Sjkim	 * there, which hung the controller unless there is data transmission
1424178019Sjkim	 * after ACK from the mouse.
142541016Sdfr	 */
1426178019Sjkim	if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1427178019Sjkim		printf("psm%d: failed to get status.\n", unit);
1428178019Sjkim	else {
1429178019Sjkim		/*
1430178019Sjkim		 * When in its native mode, some mice operate with different
1431178019Sjkim		 * default parameters than in the PS/2 compatible mode.
1432178019Sjkim		 */
1433178019Sjkim		sc->dflt_mode.rate = sc->mode.rate = stat[2];
1434178019Sjkim		sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1435178019Sjkim	}
143641016Sdfr
1437178019Sjkim	/* disable the aux port for now... */
1438178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
143941016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1440178019Sjkim	    (command_byte & KBD_KBD_CONTROL_BITS) |
1441178019Sjkim	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1442178019Sjkim		/*
1443178019Sjkim		 * this is CONTROLLER ERROR; I don't know the proper way to
1444178019Sjkim		 * recover from this error...
1445178019Sjkim		 */
1446178019Sjkim		restore_controller(sc->kbdc, command_byte);
1447178019Sjkim		printf("psm%d: unable to set the command byte.\n", unit);
1448178019Sjkim		endprobe(ENXIO);
1449178019Sjkim	}
1450178019Sjkim
1451178019Sjkim	/* done */
1452178019Sjkim	kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1453178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1454178019Sjkim	return (0);
145541016Sdfr}
145641016Sdfr
145741016Sdfrstatic int
145841016Sdfrpsmattach(device_t dev)
145941016Sdfr{
1460178019Sjkim	int unit = device_get_unit(dev);
1461178019Sjkim	struct psm_softc *sc = device_get_softc(dev);
1462178019Sjkim	int error;
1463178019Sjkim	int rid;
146441016Sdfr
1465178019Sjkim	/* Setup initial state */
1466178019Sjkim	sc->state = PSM_VALID;
1467240211Sjhb	callout_init(&sc->callout, 0);
1468240211Sjhb	callout_init(&sc->softcallout, 0);
146941016Sdfr
1470178019Sjkim	/* Setup our interrupt handler */
1471178019Sjkim	rid = KBDC_RID_AUX;
1472216491Sjhb	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1473178019Sjkim	if (sc->intr == NULL)
1474178019Sjkim		return (ENXIO);
1475178019Sjkim	error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1476178019Sjkim	    &sc->ih);
1477178019Sjkim	if (error) {
1478178019Sjkim		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1479178019Sjkim		return (error);
1480178019Sjkim	}
148158230Syokota
1482178019Sjkim	/* Done */
1483212355Sed	sc->dev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "psm%d", unit);
1484212355Sed	sc->dev->si_drv1 = sc;
1485212355Sed	sc->bdev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "bpsm%d", unit);
1486212355Sed	sc->bdev->si_drv1 = sc;
148741016Sdfr
1488233580Sjkim	/* Some touchpad devices need full reinitialization after suspend. */
1489233580Sjkim	switch (sc->hw.model) {
1490233580Sjkim	case MOUSE_MODEL_SYNAPTICS:
1491233580Sjkim	case MOUSE_MODEL_GLIDEPOINT:
1492233580Sjkim	case MOUSE_MODEL_VERSAPAD:
1493233580Sjkim		sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1494233580Sjkim		break;
1495233580Sjkim	default:
1496248478Sjkim		if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
1497233580Sjkim			sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1498233580Sjkim		break;
1499233580Sjkim	}
1500233580Sjkim
1501178019Sjkim	if (!verbose)
1502178019Sjkim		printf("psm%d: model %s, device ID %d\n",
1503178019Sjkim		    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1504178019Sjkim	else {
1505178019Sjkim		printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1506178019Sjkim		    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1507178019Sjkim		    sc->hw.hwid >> 8, sc->hw.buttons);
1508178019Sjkim		printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1509178019Sjkim		    unit, sc->config, sc->flags, sc->mode.packetsize);
1510178019Sjkim		printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1511178019Sjkim		    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1512178019Sjkim	}
151341016Sdfr
1514178019Sjkim	if (bootverbose)
1515178019Sjkim		--verbose;
151641016Sdfr
1517178019Sjkim	return (0);
151841016Sdfr}
151941016Sdfr
152041016Sdfrstatic int
152158230Syokotapsmdetach(device_t dev)
152258230Syokota{
1523178019Sjkim	struct psm_softc *sc;
1524178019Sjkim	int rid;
152558230Syokota
1526178019Sjkim	sc = device_get_softc(dev);
1527178019Sjkim	if (sc->state & PSM_OPEN)
1528178019Sjkim		return (EBUSY);
152958230Syokota
1530178019Sjkim	rid = KBDC_RID_AUX;
1531178019Sjkim	bus_teardown_intr(dev, sc->intr, sc->ih);
1532178019Sjkim	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
153358230Syokota
1534178019Sjkim	destroy_dev(sc->dev);
1535178019Sjkim	destroy_dev(sc->bdev);
153658230Syokota
1537240211Sjhb	callout_drain(&sc->callout);
1538240211Sjhb	callout_drain(&sc->softcallout);
1539240211Sjhb
1540178019Sjkim	return (0);
154158230Syokota}
154258230Syokota
154358230Syokotastatic int
1544130585Sphkpsmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
154541016Sdfr{
1546178019Sjkim	struct psm_softc *sc;
1547178019Sjkim	int command_byte;
1548178019Sjkim	int err;
1549178019Sjkim	int s;
155041016Sdfr
1551178019Sjkim	/* Get device data */
1552212355Sed	sc = dev->si_drv1;
1553178019Sjkim	if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
1554178019Sjkim		/* the device is no longer valid/functioning */
1555178019Sjkim		return (ENXIO);
1556178019Sjkim	}
155741016Sdfr
1558178019Sjkim	/* Disallow multiple opens */
1559178019Sjkim	if (sc->state & PSM_OPEN)
1560178019Sjkim		return (EBUSY);
156141016Sdfr
1562212355Sed	device_busy(devclass_get_device(psm_devclass, sc->unit));
156341016Sdfr
1564178019Sjkim	/* Initialize state */
1565178019Sjkim	sc->mode.level = sc->dflt_mode.level;
1566178019Sjkim	sc->mode.protocol = sc->dflt_mode.protocol;
1567178019Sjkim	sc->watchdog = FALSE;
1568189870Srnoland	sc->async = NULL;
156941016Sdfr
1570178019Sjkim	/* flush the event queue */
1571178019Sjkim	sc->queue.count = 0;
1572178019Sjkim	sc->queue.head = 0;
1573178019Sjkim	sc->queue.tail = 0;
1574178019Sjkim	sc->status.flags = 0;
1575178019Sjkim	sc->status.button = 0;
1576178019Sjkim	sc->status.obutton = 0;
1577178019Sjkim	sc->status.dx = 0;
1578178019Sjkim	sc->status.dy = 0;
1579178019Sjkim	sc->status.dz = 0;
1580178019Sjkim	sc->button = 0;
1581178019Sjkim	sc->pqueue_start = 0;
1582178019Sjkim	sc->pqueue_end = 0;
158341016Sdfr
1584178019Sjkim	/* empty input buffer */
1585178019Sjkim	flushpackets(sc);
1586178019Sjkim	sc->syncerrors = 0;
1587178019Sjkim	sc->pkterrors = 0;
158841016Sdfr
1589178019Sjkim	/* don't let timeout routines in the keyboard driver to poll the kbdc */
1590178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
1591178019Sjkim		return (EIO);
159241016Sdfr
1593178019Sjkim	/* save the current controller command byte */
1594178019Sjkim	s = spltty();
1595178019Sjkim	command_byte = get_controller_command_byte(sc->kbdc);
159641016Sdfr
1597178019Sjkim	/* enable the aux port and temporalily disable the keyboard */
1598178019Sjkim	if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
159941016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1600178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1601178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1602178019Sjkim		/* CONTROLLER ERROR; do you know how to get out of this? */
1603178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
1604178019Sjkim		splx(s);
1605178019Sjkim		log(LOG_ERR,
1606212355Sed		    "psm%d: unable to set the command byte (psmopen).\n",
1607212355Sed		    sc->unit);
1608178019Sjkim		return (EIO);
1609178019Sjkim	}
1610178019Sjkim	/*
1611178019Sjkim	 * Now that the keyboard controller is told not to generate
1612178019Sjkim	 * the keyboard and mouse interrupts, call `splx()' to allow
1613178019Sjkim	 * the other tty interrupts. The clock interrupt may also occur,
1614178019Sjkim	 * but timeout routines will be blocked by the poll flag set
1615178019Sjkim	 * via `kbdc_lock()'
1616178019Sjkim	 */
161741016Sdfr	splx(s);
161841016Sdfr
1619178019Sjkim	/* enable the mouse device */
1620178019Sjkim	err = doopen(sc, command_byte);
1621178019Sjkim
1622178019Sjkim	/* done */
1623178019Sjkim	if (err == 0)
1624178019Sjkim		sc->state |= PSM_OPEN;
1625178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1626178019Sjkim	return (err);
162741016Sdfr}
162841016Sdfr
162941016Sdfrstatic int
1630130585Sphkpsmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
163141016Sdfr{
1632212355Sed	struct psm_softc *sc = dev->si_drv1;
1633178019Sjkim	int stat[3];
1634178019Sjkim	int command_byte;
1635178019Sjkim	int s;
163641016Sdfr
1637178019Sjkim	/* don't let timeout routines in the keyboard driver to poll the kbdc */
1638178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
1639178019Sjkim		return (EIO);
164041016Sdfr
1641178019Sjkim	/* save the current controller command byte */
1642178019Sjkim	s = spltty();
1643178019Sjkim	command_byte = get_controller_command_byte(sc->kbdc);
1644178019Sjkim	if (command_byte == -1) {
1645178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
1646178019Sjkim		splx(s);
1647178019Sjkim		return (EIO);
1648178019Sjkim	}
164941016Sdfr
1650178019Sjkim	/* disable the aux interrupt and temporalily disable the keyboard */
1651178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
165241016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1653178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1654178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1655178019Sjkim		log(LOG_ERR,
1656212355Sed		    "psm%d: failed to disable the aux int (psmclose).\n",
1657212355Sed		    sc->unit);
1658178019Sjkim		/* CONTROLLER ERROR;
1659178019Sjkim		 * NOTE: we shall force our way through. Because the only
1660178019Sjkim		 * ill effect we shall see is that we may not be able
1661178019Sjkim		 * to read ACK from the mouse, and it doesn't matter much
1662178019Sjkim		 * so long as the mouse will accept the DISABLE command.
1663178019Sjkim		 */
1664178019Sjkim	}
1665178019Sjkim	splx(s);
166641016Sdfr
1667178019Sjkim	/* stop the watchdog timer */
1668240211Sjhb	callout_stop(&sc->callout);
166958230Syokota
1670178019Sjkim	/* remove anything left in the output buffer */
1671178019Sjkim	empty_aux_buffer(sc->kbdc, 10);
167241016Sdfr
1673178019Sjkim	/* disable the aux device, port and interrupt */
1674178019Sjkim	if (sc->state & PSM_VALID) {
1675178019Sjkim		if (!disable_aux_dev(sc->kbdc)) {
1676178019Sjkim			/* MOUSE ERROR;
1677178019Sjkim			 * NOTE: we don't return (error) and continue,
1678178019Sjkim			 * pretending we have successfully disabled the device.
1679178019Sjkim			 * It's OK because the interrupt routine will discard
1680178019Sjkim			 * any data from the mouse hereafter.
1681178019Sjkim			 */
1682178019Sjkim			log(LOG_ERR,
1683178019Sjkim			    "psm%d: failed to disable the device (psmclose).\n",
1684212355Sed			    sc->unit);
1685178019Sjkim		}
168641016Sdfr
1687178019Sjkim		if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1688178019Sjkim			log(LOG_DEBUG,
1689212355Sed			    "psm%d: failed to get status (psmclose).\n",
1690212355Sed			    sc->unit);
1691178019Sjkim	}
169241016Sdfr
1693178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
169441016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1695178019Sjkim	    (command_byte & KBD_KBD_CONTROL_BITS) |
1696178019Sjkim	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1697178019Sjkim		/*
1698178019Sjkim		 * CONTROLLER ERROR;
1699178019Sjkim		 * we shall ignore this error; see the above comment.
1700178019Sjkim		 */
1701178019Sjkim		log(LOG_ERR,
1702178019Sjkim		    "psm%d: failed to disable the aux port (psmclose).\n",
1703212355Sed		    sc->unit);
1704178019Sjkim	}
170541016Sdfr
1706178019Sjkim	/* remove anything left in the output buffer */
1707178019Sjkim	empty_aux_buffer(sc->kbdc, 10);
170841016Sdfr
1709189870Srnoland	/* clean up and sigio requests */
1710189870Srnoland	if (sc->async != NULL) {
1711189870Srnoland		funsetown(&sc->async);
1712189870Srnoland		sc->async = NULL;
1713189870Srnoland	}
1714189870Srnoland
1715178019Sjkim	/* close is almost always successful */
1716178019Sjkim	sc->state &= ~PSM_OPEN;
1717178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1718212355Sed	device_unbusy(devclass_get_device(psm_devclass, sc->unit));
1719178019Sjkim	return (0);
172041016Sdfr}
172141016Sdfr
172241016Sdfrstatic int
1723178019Sjkimtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
1724178019Sjkim    u_char *buf)
172541016Sdfr{
1726178019Sjkim	static u_char butmapps2[8] = {
1727178019Sjkim		0,
1728178019Sjkim		MOUSE_PS2_BUTTON1DOWN,
1729178019Sjkim		MOUSE_PS2_BUTTON2DOWN,
1730178019Sjkim		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1731178019Sjkim		MOUSE_PS2_BUTTON3DOWN,
1732178019Sjkim		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1733178019Sjkim		MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1734178019Sjkim		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
1735178019Sjkim		    MOUSE_PS2_BUTTON3DOWN,
1736178019Sjkim	};
1737178019Sjkim	static u_char butmapmsc[8] = {
1738178019Sjkim		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
1739178019Sjkim		    MOUSE_MSC_BUTTON3UP,
1740178019Sjkim		MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1741178019Sjkim		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1742178019Sjkim		MOUSE_MSC_BUTTON3UP,
1743178019Sjkim		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1744178019Sjkim		MOUSE_MSC_BUTTON2UP,
1745178019Sjkim		MOUSE_MSC_BUTTON1UP,
1746178019Sjkim		0,
1747178019Sjkim	};
1748178019Sjkim	int mapped;
1749178019Sjkim	int i;
175041016Sdfr
1751178019Sjkim	if (sc->mode.level == PSM_LEVEL_BASE) {
1752178019Sjkim		mapped = status->button & ~MOUSE_BUTTON4DOWN;
1753178019Sjkim		if (status->button & MOUSE_BUTTON4DOWN)
1754178019Sjkim			mapped |= MOUSE_BUTTON1DOWN;
1755178019Sjkim		status->button = mapped;
1756178019Sjkim		buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1757178019Sjkim		i = imax(imin(status->dx, 255), -256);
1758178019Sjkim		if (i < 0)
1759178019Sjkim			buf[0] |= MOUSE_PS2_XNEG;
1760178019Sjkim		buf[1] = i;
1761178019Sjkim		i = imax(imin(status->dy, 255), -256);
1762178019Sjkim		if (i < 0)
1763178019Sjkim			buf[0] |= MOUSE_PS2_YNEG;
1764178019Sjkim		buf[2] = i;
1765178019Sjkim		return (MOUSE_PS2_PACKETSIZE);
1766178019Sjkim	} else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1767178019Sjkim		buf[0] = MOUSE_MSC_SYNC |
1768178019Sjkim		    butmapmsc[status->button & MOUSE_STDBUTTONS];
1769178019Sjkim		i = imax(imin(status->dx, 255), -256);
1770178019Sjkim		buf[1] = i >> 1;
1771178019Sjkim		buf[3] = i - buf[1];
1772178019Sjkim		i = imax(imin(status->dy, 255), -256);
1773178019Sjkim		buf[2] = i >> 1;
1774178019Sjkim		buf[4] = i - buf[2];
1775178019Sjkim		i = imax(imin(status->dz, 127), -128);
1776178019Sjkim		buf[5] = (i >> 1) & 0x7f;
1777178019Sjkim		buf[6] = (i - (i >> 1)) & 0x7f;
1778178019Sjkim		buf[7] = (~status->button >> 3) & 0x7f;
1779178019Sjkim		return (MOUSE_SYS_PACKETSIZE);
1780178019Sjkim	}
1781178019Sjkim	return (pb->inputbytes);
178241016Sdfr}
178341016Sdfr
178441016Sdfrstatic int
1785130585Sphkpsmread(struct cdev *dev, struct uio *uio, int flag)
178641016Sdfr{
1787212355Sed	struct psm_softc *sc = dev->si_drv1;
1788178019Sjkim	u_char buf[PSM_SMALLBUFSIZE];
1789178019Sjkim	int error = 0;
1790178019Sjkim	int s;
1791178019Sjkim	int l;
179241016Sdfr
1793178019Sjkim	if ((sc->state & PSM_VALID) == 0)
1794178019Sjkim		return (EIO);
179541016Sdfr
1796178019Sjkim	/* block until mouse activity occured */
1797178019Sjkim	s = spltty();
1798178019Sjkim	while (sc->queue.count <= 0) {
1799212355Sed		if (dev != sc->bdev) {
1800178019Sjkim			splx(s);
1801178019Sjkim			return (EWOULDBLOCK);
1802178019Sjkim		}
1803178019Sjkim		sc->state |= PSM_ASLP;
1804178019Sjkim		error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
1805178019Sjkim		sc->state &= ~PSM_ASLP;
1806178019Sjkim		if (error) {
1807178019Sjkim			splx(s);
1808178019Sjkim			return (error);
1809178019Sjkim		} else if ((sc->state & PSM_VALID) == 0) {
1810178019Sjkim			/* the device disappeared! */
1811178019Sjkim			splx(s);
1812178019Sjkim			return (EIO);
1813178019Sjkim		}
181441016Sdfr	}
1815178019Sjkim	splx(s);
181641016Sdfr
1817178019Sjkim	/* copy data to the user land */
1818178019Sjkim	while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1819178019Sjkim		s = spltty();
1820178019Sjkim		l = imin(sc->queue.count, uio->uio_resid);
1821178019Sjkim		if (l > sizeof(buf))
1822178019Sjkim			l = sizeof(buf);
1823178019Sjkim		if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1824178019Sjkim			bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1825178019Sjkim			    sizeof(sc->queue.buf) - sc->queue.head);
1826178019Sjkim			bcopy(&sc->queue.buf[0],
1827178019Sjkim			    &buf[sizeof(sc->queue.buf) - sc->queue.head],
1828178019Sjkim			    l - (sizeof(sc->queue.buf) - sc->queue.head));
1829178019Sjkim		} else
1830178019Sjkim			bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1831178019Sjkim		sc->queue.count -= l;
1832178019Sjkim		sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1833178019Sjkim		splx(s);
1834178019Sjkim		error = uiomove(buf, l, uio);
1835178019Sjkim		if (error)
1836178019Sjkim			break;
183741016Sdfr	}
183841016Sdfr
1839178019Sjkim	return (error);
184041016Sdfr}
184141016Sdfr
184241016Sdfrstatic int
184341016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
184441016Sdfr{
1845178019Sjkim	int s;
184641016Sdfr
1847178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
1848178019Sjkim		return (EIO);
184941016Sdfr
1850178019Sjkim	s = spltty();
1851178019Sjkim	*c = get_controller_command_byte(sc->kbdc);
1852178019Sjkim	if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
185341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1854178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1855178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1856178019Sjkim		/* this is CONTROLLER ERROR */
1857178019Sjkim		splx(s);
1858178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
1859178019Sjkim		return (EIO);
1860178019Sjkim	}
1861178019Sjkim
1862178019Sjkim	/*
1863178019Sjkim	 * The device may be in the middle of status data transmission.
1864178019Sjkim	 * The transmission will be interrupted, thus, incomplete status
1865178019Sjkim	 * data must be discarded. Although the aux interrupt is disabled
1866178019Sjkim	 * at the keyboard controller level, at most one aux interrupt
1867178019Sjkim	 * may have already been pending and a data byte is in the
1868178019Sjkim	 * output buffer; throw it away. Note that the second argument
1869178019Sjkim	 * to `empty_aux_buffer()' is zero, so that the call will just
1870178019Sjkim	 * flush the internal queue.
1871178019Sjkim	 * `psmintr()' will be invoked after `splx()' if an interrupt is
1872178019Sjkim	 * pending; it will see no data and returns immediately.
1873178019Sjkim	 */
1874178019Sjkim	empty_aux_buffer(sc->kbdc, 0);		/* flush the queue */
1875178019Sjkim	read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
1876178019Sjkim	flushpackets(sc);
187741016Sdfr	splx(s);
187841016Sdfr
1879178019Sjkim	return (0);
188041016Sdfr}
188141016Sdfr
1882123442Salfredstatic void
1883123442Salfreddropqueue(struct psm_softc *sc)
1884123442Salfred{
1885123442Salfred
1886178019Sjkim	sc->queue.count = 0;
1887178019Sjkim	sc->queue.head = 0;
1888178019Sjkim	sc->queue.tail = 0;
1889123442Salfred	if ((sc->state & PSM_SOFTARMED) != 0) {
1890123442Salfred		sc->state &= ~PSM_SOFTARMED;
1891240211Sjhb		callout_stop(&sc->softcallout);
1892123442Salfred	}
1893123442Salfred	sc->pqueue_start = sc->pqueue_end;
1894123442Salfred}
1895123442Salfred
1896123442Salfredstatic void
1897123442Salfredflushpackets(struct psm_softc *sc)
1898123442Salfred{
1899123442Salfred
1900123442Salfred	dropqueue(sc);
1901123442Salfred	bzero(&sc->pqueue, sizeof(sc->pqueue));
1902123442Salfred}
1903123442Salfred
190441016Sdfrstatic int
190541016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
190641016Sdfr{
1907178019Sjkim	int error = 0;
190841016Sdfr
1909178019Sjkim	/*
1910178019Sjkim	 * We may have seen a part of status data during `set_mouse_XXX()'.
1911178019Sjkim	 * they have been queued; flush it.
1912178019Sjkim	 */
1913178019Sjkim	empty_aux_buffer(sc->kbdc, 0);
191441016Sdfr
1915178019Sjkim	/* restore ports and interrupt */
1916178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
1917178019Sjkim	    kbdc_get_device_mask(sc->kbdc),
191841016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1919178019Sjkim		/*
1920178019Sjkim		 * CONTROLLER ERROR; this is serious, we may have
1921178019Sjkim		 * been left with the inaccessible keyboard and
1922178019Sjkim		 * the disabled mouse interrupt.
1923178019Sjkim		 */
1924178019Sjkim		error = EIO;
1925178019Sjkim	}
192641016Sdfr
1927178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1928178019Sjkim	return (error);
192941016Sdfr}
193041016Sdfr
193141016Sdfrstatic int
1932178017Sjkimpsmwrite(struct cdev *dev, struct uio *uio, int flag)
1933178017Sjkim{
1934212355Sed	struct psm_softc *sc = dev->si_drv1;
1935178019Sjkim	u_char buf[PSM_SMALLBUFSIZE];
1936178019Sjkim	int error = 0, i, l;
1937178017Sjkim
1938178019Sjkim	if ((sc->state & PSM_VALID) == 0)
1939178019Sjkim		return (EIO);
1940178017Sjkim
1941178019Sjkim	if (sc->mode.level < PSM_LEVEL_NATIVE)
1942178019Sjkim		return (ENODEV);
1943178017Sjkim
1944178019Sjkim	/* copy data from the user land */
1945178019Sjkim	while (uio->uio_resid > 0) {
1946178019Sjkim		l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
1947178019Sjkim		error = uiomove(buf, l, uio);
1948178019Sjkim		if (error)
1949178019Sjkim			break;
1950178019Sjkim		for (i = 0; i < l; i++) {
1951178019Sjkim			VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
1952178019Sjkim			if (!write_aux_command(sc->kbdc, buf[i])) {
1953178019Sjkim				VLOG(2, (LOG_DEBUG,
1954178019Sjkim				    "psm: cmd 0x%x failed.\n", buf[i]));
1955178019Sjkim				return (reinitialize(sc, FALSE));
1956178019Sjkim			}
1957178019Sjkim		}
1958178017Sjkim	}
1959178017Sjkim
1960178019Sjkim	return (error);
1961178017Sjkim}
1962178017Sjkim
1963178017Sjkimstatic int
1964178019Sjkimpsmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
1965178019Sjkim    struct thread *td)
196641016Sdfr{
1967212355Sed	struct psm_softc *sc = dev->si_drv1;
1968178019Sjkim	mousemode_t mode;
1969178019Sjkim	mousestatus_t status;
197041016Sdfr#if (defined(MOUSE_GETVARS))
1971178019Sjkim	mousevar_t *var;
197241016Sdfr#endif
1973178019Sjkim	mousedata_t *data;
1974178019Sjkim	int stat[3];
1975178019Sjkim	int command_byte;
1976178019Sjkim	int error = 0;
1977178019Sjkim	int s;
197841016Sdfr
1979178019Sjkim	/* Perform IOCTL command */
1980178019Sjkim	switch (cmd) {
198141016Sdfr
1982178019Sjkim	case OLD_MOUSE_GETHWINFO:
1983178019Sjkim		s = spltty();
1984178019Sjkim		((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1985178019Sjkim		((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1986178019Sjkim		((old_mousehw_t *)addr)->type = sc->hw.type;
1987178019Sjkim		((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1988178019Sjkim		splx(s);
1989178019Sjkim		break;
199041016Sdfr
1991178019Sjkim	case MOUSE_GETHWINFO:
1992178019Sjkim		s = spltty();
1993178019Sjkim		*(mousehw_t *)addr = sc->hw;
1994178019Sjkim		if (sc->mode.level == PSM_LEVEL_BASE)
1995178019Sjkim			((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1996178019Sjkim		splx(s);
1997178019Sjkim		break;
199841016Sdfr
1999178019Sjkim	case MOUSE_SYN_GETHWINFO:
2000178019Sjkim		s = spltty();
2001233580Sjkim		if (sc->synhw.infoMajor >= 4)
2002178019Sjkim			*(synapticshw_t *)addr = sc->synhw;
2003178019Sjkim		else
2004178019Sjkim			error = EINVAL;
2005178019Sjkim		splx(s);
2006178019Sjkim		break;
2007132865Snjl
2008178019Sjkim	case OLD_MOUSE_GETMODE:
2009178019Sjkim		s = spltty();
2010178019Sjkim		switch (sc->mode.level) {
2011178019Sjkim		case PSM_LEVEL_BASE:
2012178019Sjkim			((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2013178019Sjkim			break;
2014178019Sjkim		case PSM_LEVEL_STANDARD:
2015178019Sjkim			((old_mousemode_t *)addr)->protocol =
2016178019Sjkim			    MOUSE_PROTO_SYSMOUSE;
2017178019Sjkim			break;
2018178019Sjkim		case PSM_LEVEL_NATIVE:
2019178019Sjkim			((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2020178019Sjkim			break;
2021178019Sjkim		}
2022178019Sjkim		((old_mousemode_t *)addr)->rate = sc->mode.rate;
2023178019Sjkim		((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2024178019Sjkim		((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2025178019Sjkim		splx(s);
2026178019Sjkim		break;
202741016Sdfr
2028178019Sjkim	case MOUSE_GETMODE:
2029178019Sjkim		s = spltty();
2030178019Sjkim		*(mousemode_t *)addr = sc->mode;
2031178019Sjkim		if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2032178019Sjkim			((mousemode_t *)addr)->syncmask[0] = 0;
2033178019Sjkim			((mousemode_t *)addr)->syncmask[1] = 0;
2034178019Sjkim		}
2035178019Sjkim		((mousemode_t *)addr)->resolution =
2036178019Sjkim			MOUSE_RES_LOW - sc->mode.resolution;
2037178019Sjkim		switch (sc->mode.level) {
2038178019Sjkim		case PSM_LEVEL_BASE:
2039178019Sjkim			((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2040178019Sjkim			((mousemode_t *)addr)->packetsize =
2041178019Sjkim			    MOUSE_PS2_PACKETSIZE;
2042178019Sjkim			break;
2043178019Sjkim		case PSM_LEVEL_STANDARD:
2044178019Sjkim			((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2045178019Sjkim			((mousemode_t *)addr)->packetsize =
2046178019Sjkim			    MOUSE_SYS_PACKETSIZE;
2047178019Sjkim			((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2048178019Sjkim			((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2049178019Sjkim			break;
2050178019Sjkim		case PSM_LEVEL_NATIVE:
2051178019Sjkim			/* FIXME: this isn't quite correct... XXX */
2052178019Sjkim			((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2053178019Sjkim			break;
2054178019Sjkim		}
2055178019Sjkim		splx(s);
2056178019Sjkim		break;
205741016Sdfr
2058178019Sjkim	case OLD_MOUSE_SETMODE:
2059178019Sjkim	case MOUSE_SETMODE:
2060178019Sjkim		if (cmd == OLD_MOUSE_SETMODE) {
2061178019Sjkim			mode.rate = ((old_mousemode_t *)addr)->rate;
2062178019Sjkim			/*
2063178019Sjkim			 * resolution  old I/F   new I/F
2064178019Sjkim			 * default        0         0
2065178019Sjkim			 * low            1        -2
2066178019Sjkim			 * medium low     2        -3
2067178019Sjkim			 * medium high    3        -4
2068178019Sjkim			 * high           4        -5
2069178019Sjkim			 */
2070178019Sjkim			if (((old_mousemode_t *)addr)->resolution > 0)
2071178019Sjkim				mode.resolution =
2072178019Sjkim				    -((old_mousemode_t *)addr)->resolution - 1;
2073178019Sjkim			else
2074178019Sjkim				mode.resolution = 0;
2075178019Sjkim			mode.accelfactor =
2076178019Sjkim			    ((old_mousemode_t *)addr)->accelfactor;
2077178019Sjkim			mode.level = -1;
2078178019Sjkim		} else
2079178019Sjkim			mode = *(mousemode_t *)addr;
208041016Sdfr
2081178019Sjkim		/* adjust and validate parameters. */
2082178019Sjkim		if (mode.rate > UCHAR_MAX)
2083178019Sjkim			return (EINVAL);
2084178019Sjkim		if (mode.rate == 0)
2085178019Sjkim			mode.rate = sc->dflt_mode.rate;
2086178019Sjkim		else if (mode.rate == -1)
2087178019Sjkim			/* don't change the current setting */
2088178019Sjkim			;
2089178019Sjkim		else if (mode.rate < 0)
2090178019Sjkim			return (EINVAL);
2091178019Sjkim		if (mode.resolution >= UCHAR_MAX)
2092178019Sjkim			return (EINVAL);
2093178019Sjkim		if (mode.resolution >= 200)
2094178019Sjkim			mode.resolution = MOUSE_RES_HIGH;
2095178019Sjkim		else if (mode.resolution >= 100)
2096178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMHIGH;
2097178019Sjkim		else if (mode.resolution >= 50)
2098178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMLOW;
2099178019Sjkim		else if (mode.resolution > 0)
2100178019Sjkim			mode.resolution = MOUSE_RES_LOW;
2101178019Sjkim		if (mode.resolution == MOUSE_RES_DEFAULT)
2102178019Sjkim			mode.resolution = sc->dflt_mode.resolution;
2103178019Sjkim		else if (mode.resolution == -1)
2104178019Sjkim			/* don't change the current setting */
2105178019Sjkim			;
2106178019Sjkim		else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2107178019Sjkim			mode.resolution = MOUSE_RES_LOW - mode.resolution;
2108178019Sjkim		if (mode.level == -1)
2109178019Sjkim			/* don't change the current setting */
2110178019Sjkim			mode.level = sc->mode.level;
2111178019Sjkim		else if ((mode.level < PSM_LEVEL_MIN) ||
2112178019Sjkim		    (mode.level > PSM_LEVEL_MAX))
2113178019Sjkim			return (EINVAL);
2114178019Sjkim		if (mode.accelfactor == -1)
2115178019Sjkim			/* don't change the current setting */
2116178019Sjkim			mode.accelfactor = sc->mode.accelfactor;
2117178019Sjkim		else if (mode.accelfactor < 0)
2118178019Sjkim			return (EINVAL);
211941016Sdfr
2120178019Sjkim		/* don't allow anybody to poll the keyboard controller */
2121178019Sjkim		error = block_mouse_data(sc, &command_byte);
2122178019Sjkim		if (error)
2123178019Sjkim			return (error);
212441016Sdfr
2125178019Sjkim		/* set mouse parameters */
2126178019Sjkim		if (mode.rate > 0)
2127178019Sjkim			mode.rate = set_mouse_sampling_rate(sc->kbdc,
2128178019Sjkim			    mode.rate);
2129178019Sjkim		if (mode.resolution >= 0)
2130178019Sjkim			mode.resolution =
2131178019Sjkim			    set_mouse_resolution(sc->kbdc, mode.resolution);
2132178019Sjkim		set_mouse_scaling(sc->kbdc, 1);
2133178019Sjkim		get_mouse_status(sc->kbdc, stat, 0, 3);
213441016Sdfr
2135178019Sjkim		s = spltty();
2136178019Sjkim		sc->mode.rate = mode.rate;
2137178019Sjkim		sc->mode.resolution = mode.resolution;
2138178019Sjkim		sc->mode.accelfactor = mode.accelfactor;
2139178019Sjkim		sc->mode.level = mode.level;
2140178019Sjkim		splx(s);
214141016Sdfr
2142178019Sjkim		unblock_mouse_data(sc, command_byte);
2143178019Sjkim		break;
214441016Sdfr
2145178019Sjkim	case MOUSE_GETLEVEL:
2146178019Sjkim		*(int *)addr = sc->mode.level;
2147178019Sjkim		break;
214841016Sdfr
2149178019Sjkim	case MOUSE_SETLEVEL:
2150178019Sjkim		if ((*(int *)addr < PSM_LEVEL_MIN) ||
2151178019Sjkim		    (*(int *)addr > PSM_LEVEL_MAX))
2152178019Sjkim			return (EINVAL);
2153178019Sjkim		sc->mode.level = *(int *)addr;
2154178019Sjkim		break;
215541016Sdfr
2156178019Sjkim	case MOUSE_GETSTATUS:
2157178019Sjkim		s = spltty();
2158178019Sjkim		status = sc->status;
2159178019Sjkim		sc->status.flags = 0;
2160178019Sjkim		sc->status.obutton = sc->status.button;
2161178019Sjkim		sc->status.button = 0;
2162178019Sjkim		sc->status.dx = 0;
2163178019Sjkim		sc->status.dy = 0;
2164178019Sjkim		sc->status.dz = 0;
2165178019Sjkim		splx(s);
2166178019Sjkim		*(mousestatus_t *)addr = status;
2167178019Sjkim		break;
216841016Sdfr
216941016Sdfr#if (defined(MOUSE_GETVARS))
2170178019Sjkim	case MOUSE_GETVARS:
2171178019Sjkim		var = (mousevar_t *)addr;
2172178019Sjkim		bzero(var, sizeof(*var));
2173178019Sjkim		s = spltty();
2174178019Sjkim		var->var[0] = MOUSE_VARS_PS2_SIG;
2175178019Sjkim		var->var[1] = sc->config;
2176178019Sjkim		var->var[2] = sc->flags;
2177178019Sjkim		splx(s);
2178178019Sjkim		break;
217941016Sdfr
2180178019Sjkim	case MOUSE_SETVARS:
2181178019Sjkim		return (ENODEV);
218241016Sdfr#endif /* MOUSE_GETVARS */
218341016Sdfr
2184178019Sjkim	case MOUSE_READSTATE:
2185178019Sjkim	case MOUSE_READDATA:
2186178019Sjkim		data = (mousedata_t *)addr;
2187178019Sjkim		if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2188178019Sjkim			return (EINVAL);
218941016Sdfr
2190178019Sjkim		error = block_mouse_data(sc, &command_byte);
2191178019Sjkim		if (error)
2192178019Sjkim			return (error);
2193178019Sjkim		if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2194178019Sjkim		    (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2195178019Sjkim			error = EIO;
2196178019Sjkim		unblock_mouse_data(sc, command_byte);
2197178019Sjkim		break;
219841016Sdfr
219941016Sdfr#if (defined(MOUSE_SETRESOLUTION))
2200178019Sjkim	case MOUSE_SETRESOLUTION:
2201178019Sjkim		mode.resolution = *(int *)addr;
2202178019Sjkim		if (mode.resolution >= UCHAR_MAX)
2203178019Sjkim			return (EINVAL);
2204178019Sjkim		else if (mode.resolution >= 200)
2205178019Sjkim			mode.resolution = MOUSE_RES_HIGH;
2206178019Sjkim		else if (mode.resolution >= 100)
2207178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMHIGH;
2208178019Sjkim		else if (mode.resolution >= 50)
2209178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMLOW;
2210178019Sjkim		else if (mode.resolution > 0)
2211178019Sjkim			mode.resolution = MOUSE_RES_LOW;
2212178019Sjkim		if (mode.resolution == MOUSE_RES_DEFAULT)
2213178019Sjkim			mode.resolution = sc->dflt_mode.resolution;
2214178019Sjkim		else if (mode.resolution == -1)
2215178019Sjkim			mode.resolution = sc->mode.resolution;
2216178019Sjkim		else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2217178019Sjkim			mode.resolution = MOUSE_RES_LOW - mode.resolution;
221841016Sdfr
2219178019Sjkim		error = block_mouse_data(sc, &command_byte);
2220178019Sjkim		if (error)
2221178019Sjkim			return (error);
2222178019Sjkim		sc->mode.resolution =
2223178019Sjkim		    set_mouse_resolution(sc->kbdc, mode.resolution);
2224178019Sjkim		if (sc->mode.resolution != mode.resolution)
2225178019Sjkim			error = EIO;
2226178019Sjkim		unblock_mouse_data(sc, command_byte);
2227178019Sjkim		break;
222841016Sdfr#endif /* MOUSE_SETRESOLUTION */
222941016Sdfr
223041016Sdfr#if (defined(MOUSE_SETRATE))
2231178019Sjkim	case MOUSE_SETRATE:
2232178019Sjkim		mode.rate = *(int *)addr;
2233178019Sjkim		if (mode.rate > UCHAR_MAX)
2234178019Sjkim			return (EINVAL);
2235178019Sjkim		if (mode.rate == 0)
2236178019Sjkim			mode.rate = sc->dflt_mode.rate;
2237178019Sjkim		else if (mode.rate < 0)
2238178019Sjkim			mode.rate = sc->mode.rate;
223941016Sdfr
2240178019Sjkim		error = block_mouse_data(sc, &command_byte);
2241178019Sjkim		if (error)
2242178019Sjkim			return (error);
2243178019Sjkim		sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2244178019Sjkim		if (sc->mode.rate != mode.rate)
2245178019Sjkim			error = EIO;
2246178019Sjkim		unblock_mouse_data(sc, command_byte);
2247178019Sjkim		break;
224841016Sdfr#endif /* MOUSE_SETRATE */
224941016Sdfr
225041016Sdfr#if (defined(MOUSE_SETSCALING))
2251178019Sjkim	case MOUSE_SETSCALING:
2252178019Sjkim		if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2253178019Sjkim			return (EINVAL);
225441016Sdfr
2255178019Sjkim		error = block_mouse_data(sc, &command_byte);
2256178019Sjkim		if (error)
2257178019Sjkim			return (error);
2258178019Sjkim		if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2259178019Sjkim			error = EIO;
2260178019Sjkim		unblock_mouse_data(sc, command_byte);
2261178019Sjkim		break;
226241016Sdfr#endif /* MOUSE_SETSCALING */
226341016Sdfr
226441016Sdfr#if (defined(MOUSE_GETHWID))
2265178019Sjkim	case MOUSE_GETHWID:
2266178019Sjkim		error = block_mouse_data(sc, &command_byte);
2267178019Sjkim		if (error)
2268178019Sjkim			return (error);
2269178019Sjkim		sc->hw.hwid &= ~0x00ff;
2270178019Sjkim		sc->hw.hwid |= get_aux_id(sc->kbdc);
2271178019Sjkim		*(int *)addr = sc->hw.hwid & 0x00ff;
2272178019Sjkim		unblock_mouse_data(sc, command_byte);
2273178019Sjkim		break;
227441016Sdfr#endif /* MOUSE_GETHWID */
227541016Sdfr
2276189870Srnoland	case FIONBIO:
2277189870Srnoland	case FIOASYNC:
2278189870Srnoland		break;
2279189870Srnoland	case FIOSETOWN:
2280189870Srnoland		error = fsetown(*(int *)addr, &sc->async);
2281189870Srnoland		break;
2282189870Srnoland	case FIOGETOWN:
2283189870Srnoland		*(int *) addr = fgetown(&sc->async);
2284189870Srnoland		break;
2285178019Sjkim	default:
2286178019Sjkim		return (ENOTTY);
2287178019Sjkim	}
228841016Sdfr
2289178019Sjkim	return (error);
229041016Sdfr}
229141016Sdfr
229241016Sdfrstatic void
229358230Syokotapsmtimeout(void *arg)
229458230Syokota{
2295178019Sjkim	struct psm_softc *sc;
2296178019Sjkim	int s;
229758230Syokota
2298178019Sjkim	sc = (struct psm_softc *)arg;
2299178019Sjkim	s = spltty();
2300178019Sjkim	if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2301178019Sjkim		VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2302178019Sjkim		psmintr(sc);
2303178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
2304178019Sjkim	}
2305178019Sjkim	sc->watchdog = TRUE;
2306178019Sjkim	splx(s);
2307240211Sjhb	callout_reset(&sc->callout, hz, psmtimeout, sc);
230858230Syokota}
230958230Syokota
2310139628Sphilip/* Add all sysctls under the debug.psm and hw.psm nodes */
2311227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2312227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2313123442Salfred
2314180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RW, &verbose, 0,
2315180818Strhodes    "Verbosity level");
2316123442Salfred
2317139628Sphilipstatic int psmhz = 20;
2318180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2319180818Strhodes    "Frequency of the softcallout (in hz)");
2320123442Salfredstatic int psmerrsecs = 2;
2321180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2322180818Strhodes    "Number of seconds during which packets will dropped after a sync error");
2323123442Salfredstatic int psmerrusecs = 0;
2324180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2325180818Strhodes    "Microseconds to add to psmerrsecs");
2326123442Salfredstatic int psmsecs = 0;
2327180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2328180818Strhodes    "Max number of seconds between soft interrupts");
2329123442Salfredstatic int psmusecs = 500000;
2330180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2331180818Strhodes    "Microseconds to add to psmsecs");
2332139628Sphilipstatic int pkterrthresh = 2;
2333180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2334180818Strhodes    "Number of error packets allowed before reinitializing the mouse");
2335123442Salfred
2336200674SdumbbellSYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RW, &tap_enabled, 0,
2337200674Sdumbbell    "Enable tap and drag gestures");
2338139628Sphilipstatic int tap_threshold = PSM_TAP_THRESHOLD;
2339180818StrhodesSYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2340180818Strhodes    "Button tap threshold");
2341139628Sphilipstatic int tap_timeout = PSM_TAP_TIMEOUT;
2342180818StrhodesSYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2343180818Strhodes    "Tap timeout for touchpads");
2344134405Sgibbs
234558230Syokotastatic void
234641016Sdfrpsmintr(void *arg)
234741016Sdfr{
2348178019Sjkim	struct psm_softc *sc = arg;
2349178019Sjkim	struct timeval now;
2350178019Sjkim	int c;
2351178019Sjkim	packetbuf_t *pb;
235241016Sdfr
2353123442Salfred
2354178019Sjkim	/* read until there is nothing to read */
2355178019Sjkim	while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2356178019Sjkim		pb = &sc->pqueue[sc->pqueue_end];
235784880Syokota
2358178019Sjkim		/* discard the byte if the device is not open */
2359178019Sjkim		if ((sc->state & PSM_OPEN) == 0)
2360178019Sjkim			continue;
236141016Sdfr
2362178019Sjkim		getmicrouptime(&now);
2363178019Sjkim		if ((pb->inputbytes > 0) &&
2364178019Sjkim		    timevalcmp(&now, &sc->inputtimeout, >)) {
2365178019Sjkim			VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2366178019Sjkim			    "resetting byte count\n"));
2367178019Sjkim			pb->inputbytes = 0;
2368178019Sjkim			sc->syncerrors = 0;
2369178019Sjkim			sc->pkterrors = 0;
2370178019Sjkim		}
2371178019Sjkim		sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2372178019Sjkim		sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2373178019Sjkim		timevaladd(&sc->inputtimeout, &now);
237441016Sdfr
2375178019Sjkim		pb->ipacket[pb->inputbytes++] = c;
2376178017Sjkim
2377178019Sjkim		if (sc->mode.level == PSM_LEVEL_NATIVE) {
2378178019Sjkim			VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2379178019Sjkim			sc->syncerrors = 0;
2380178019Sjkim			sc->pkterrors = 0;
2381178019Sjkim			goto next;
2382178019Sjkim		} else {
2383178019Sjkim			if (pb->inputbytes < sc->mode.packetsize)
2384178019Sjkim				continue;
238541016Sdfr
2386178019Sjkim			VLOG(4, (LOG_DEBUG,
2387178019Sjkim			    "psmintr: %02x %02x %02x %02x %02x %02x\n",
2388178019Sjkim			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2389178019Sjkim			    pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2390178019Sjkim		}
2391134405Sgibbs
2392178019Sjkim		c = pb->ipacket[0];
2393178019Sjkim
2394178019Sjkim		if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2395178019Sjkim			sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2396178019Sjkim			sc->flags &= ~PSM_NEED_SYNCBITS;
2397178019Sjkim			VLOG(2, (LOG_DEBUG,
2398178019Sjkim			    "psmintr: Sync bytes now %04x,%04x\n",
2399178019Sjkim			    sc->mode.syncmask[0], sc->mode.syncmask[0]));
2400178019Sjkim		} else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2401178019Sjkim			VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
2402178019Sjkim			    "(%04x != %04x) %d cmds since last error.\n",
2403178019Sjkim			    c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2404178019Sjkim			    sc->cmdcount - sc->lasterr));
2405178019Sjkim			sc->lasterr = sc->cmdcount;
2406178019Sjkim			/*
2407178019Sjkim			 * The sync byte test is a weak measure of packet
2408178019Sjkim			 * validity.  Conservatively discard any input yet
2409178019Sjkim			 * to be seen by userland when we detect a sync
2410178019Sjkim			 * error since there is a good chance some of
2411178019Sjkim			 * the queued packets have undetected errors.
2412178019Sjkim			 */
2413178019Sjkim			dropqueue(sc);
2414178019Sjkim			if (sc->syncerrors == 0)
2415178019Sjkim				sc->pkterrors++;
2416178019Sjkim			++sc->syncerrors;
2417178019Sjkim			sc->lastinputerr = now;
2418178019Sjkim			if (sc->syncerrors >= sc->mode.packetsize * 2 ||
2419178019Sjkim			    sc->pkterrors >= pkterrthresh) {
2420178019Sjkim				/*
2421178019Sjkim				 * If we've failed to find a single sync byte
2422178019Sjkim				 * in 2 packets worth of data, or we've seen
2423178019Sjkim				 * persistent packet errors during the
2424178019Sjkim				 * validation period, reinitialize the mouse
2425178019Sjkim				 * in hopes of returning it to the expected
2426178019Sjkim				 * mode.
2427178019Sjkim				 */
2428178019Sjkim				VLOG(3, (LOG_DEBUG,
2429178019Sjkim				    "psmintr: reset the mouse.\n"));
2430178019Sjkim				reinitialize(sc, TRUE);
2431178019Sjkim			} else if (sc->syncerrors == sc->mode.packetsize) {
2432178019Sjkim				/*
2433178019Sjkim				 * Try a soft reset after searching for a sync
2434178019Sjkim				 * byte through a packet length of bytes.
2435178019Sjkim				 */
2436178019Sjkim				VLOG(3, (LOG_DEBUG,
2437178019Sjkim				    "psmintr: re-enable the mouse.\n"));
2438178019Sjkim				pb->inputbytes = 0;
2439178019Sjkim				disable_aux_dev(sc->kbdc);
2440178019Sjkim				enable_aux_dev(sc->kbdc);
2441178019Sjkim			} else {
2442178019Sjkim				VLOG(3, (LOG_DEBUG,
2443178019Sjkim				    "psmintr: discard a byte (%d)\n",
2444178019Sjkim				    sc->syncerrors));
2445178019Sjkim				pb->inputbytes--;
2446178019Sjkim				bcopy(&pb->ipacket[1], &pb->ipacket[0],
2447178019Sjkim				    pb->inputbytes);
2448178019Sjkim			}
2449178019Sjkim			continue;
2450178019Sjkim		}
2451178019Sjkim
2452134405Sgibbs		/*
2453178019Sjkim		 * We have what appears to be a valid packet.
2454178019Sjkim		 * Reset the error counters.
2455134405Sgibbs		 */
2456178019Sjkim		sc->syncerrors = 0;
2457134405Sgibbs
2458134405Sgibbs		/*
2459178019Sjkim		 * Drop even good packets if they occur within a timeout
2460178019Sjkim		 * period of a sync error.  This allows the detection of
2461178019Sjkim		 * a change in the mouse's packet mode without exposing
2462178019Sjkim		 * erratic mouse behavior to the user.  Some KVMs forget
2463178019Sjkim		 * enhanced mouse modes during switch events.
2464134405Sgibbs		 */
2465178019Sjkim		if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
2466178019Sjkim		    &now)) {
2467178019Sjkim			pb->inputbytes = 0;
2468178019Sjkim			continue;
2469178019Sjkim		}
2470134405Sgibbs
2471178019Sjkim		/*
2472178019Sjkim		 * Now that we're out of the validation period, reset
2473178019Sjkim		 * the packet error count.
2474178019Sjkim		 */
2475178019Sjkim		sc->pkterrors = 0;
2476134405Sgibbs
2477178019Sjkim		sc->cmdcount++;
2478178019Sjkimnext:
2479178019Sjkim		if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2480178019Sjkim			sc->pqueue_end = 0;
2481178019Sjkim		/*
2482178019Sjkim		 * If we've filled the queue then call the softintr ourselves,
2483178019Sjkim		 * otherwise schedule the interrupt for later.
2484178019Sjkim		 */
2485178019Sjkim		if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2486178019Sjkim		    (sc->pqueue_end == sc->pqueue_start)) {
2487178019Sjkim			if ((sc->state & PSM_SOFTARMED) != 0) {
2488178019Sjkim				sc->state &= ~PSM_SOFTARMED;
2489240211Sjhb				callout_stop(&sc->softcallout);
2490178019Sjkim			}
2491178019Sjkim			psmsoftintr(arg);
2492178019Sjkim		} else if ((sc->state & PSM_SOFTARMED) == 0) {
2493178019Sjkim			sc->state |= PSM_SOFTARMED;
2494240211Sjhb			callout_reset(&sc->softcallout,
2495240211Sjhb			    psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg);
2496123442Salfred		}
2497123442Salfred	}
2498123442Salfred}
2499123442Salfred
2500123442Salfredstatic void
2501178019Sjkimproc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2502178019Sjkim    int *x, int *y, int *z)
2503123442Salfred{
2504123442Salfred
2505178019Sjkim	/*
2506240743Skevlo	 * PS2++ protocol packet
2507178019Sjkim	 *
2508178019Sjkim	 *          b7 b6 b5 b4 b3 b2 b1 b0
2509178019Sjkim	 * byte 1:  *  1  p3 p2 1  *  *  *
2510178019Sjkim	 * byte 2:  c1 c2 p1 p0 d1 d0 1  0
2511178019Sjkim	 *
2512178019Sjkim	 * p3-p0: packet type
2513178019Sjkim	 * c1, c2: c1 & c2 == 1, if p2 == 0
2514178019Sjkim	 *         c1 & c2 == 0, if p2 == 1
2515178019Sjkim	 *
2516178019Sjkim	 * packet type: 0 (device type)
2517178019Sjkim	 * See comments in enable_mmanplus() below.
2518178019Sjkim	 *
2519178019Sjkim	 * packet type: 1 (wheel data)
2520178019Sjkim	 *
2521178019Sjkim	 *          b7 b6 b5 b4 b3 b2 b1 b0
2522178019Sjkim	 * byte 3:  h  *  B5 B4 s  d2 d1 d0
2523178019Sjkim	 *
2524178019Sjkim	 * h: 1, if horizontal roller data
2525178019Sjkim	 *    0, if vertical roller data
2526178019Sjkim	 * B4, B5: button 4 and 5
2527178019Sjkim	 * s: sign bit
2528178019Sjkim	 * d2-d0: roller data
2529178019Sjkim	 *
2530178019Sjkim	 * packet type: 2 (reserved)
253141016Sdfr	 */
2532178019Sjkim	if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
2533178019Sjkim	    (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2534178019Sjkim		/*
2535178019Sjkim		 * the extended data packet encodes button
2536178019Sjkim		 * and wheel events
2537178019Sjkim		 */
2538123442Salfred		switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
253948778Syokota		case 1:
2540178019Sjkim			/* wheel data packet */
2541178019Sjkim			*x = *y = 0;
2542178019Sjkim			if (pb->ipacket[2] & 0x80) {
2543178019Sjkim				/* XXX horizontal roller count - ignore it */
2544178019Sjkim				;
2545178019Sjkim			} else {
2546178019Sjkim				/* vertical roller count */
2547178019Sjkim				*z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
2548178019Sjkim				    (pb->ipacket[2] & 0x0f) - 16 :
2549178019Sjkim				    (pb->ipacket[2] & 0x0f);
2550178019Sjkim			}
2551178019Sjkim			ms->button |= (pb->ipacket[2] &
2552178019Sjkim			    MOUSE_PS2PLUS_BUTTON4DOWN) ?
2553178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
2554178019Sjkim			ms->button |= (pb->ipacket[2] &
2555178019Sjkim			    MOUSE_PS2PLUS_BUTTON5DOWN) ?
2556178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
2557178019Sjkim			break;
255848778Syokota		case 2:
2559178019Sjkim			/*
2560178019Sjkim			 * this packet type is reserved by
2561178019Sjkim			 * Logitech...
2562178019Sjkim			 */
2563178019Sjkim			/*
2564178019Sjkim			 * IBM ScrollPoint Mouse uses this
2565178019Sjkim			 * packet type to encode both vertical
2566178019Sjkim			 * and horizontal scroll movement.
2567178019Sjkim			 */
2568178019Sjkim			*x = *y = 0;
2569178019Sjkim			/* horizontal count */
2570178019Sjkim			if (pb->ipacket[2] & 0x0f)
2571178019Sjkim				*z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
2572178019Sjkim				    -2 : 2;
2573178019Sjkim			/* vertical count */
2574178019Sjkim			if (pb->ipacket[2] & 0xf0)
2575178019Sjkim				*z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
2576178019Sjkim				    -1 : 1;
2577178019Sjkim			break;
257848778Syokota		case 0:
2579178019Sjkim			/* device type packet - shouldn't happen */
2580178019Sjkim			/* FALLTHROUGH */
258148778Syokota		default:
2582178019Sjkim			*x = *y = 0;
2583178019Sjkim			ms->button = ms->obutton;
2584178019Sjkim			VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
2585178019Sjkim			    "type %d: 0x%02x 0x%02x 0x%02x\n",
2586178019Sjkim			    MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2587178019Sjkim			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
2588178019Sjkim			break;
258948778Syokota		}
2590178019Sjkim	} else {
259141016Sdfr		/* preserve button states */
2592178019Sjkim		ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
2593178019Sjkim	}
2594178019Sjkim}
259541016Sdfr
2596178019Sjkimstatic int
2597178019Sjkimproc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2598178019Sjkim    int *x, int *y, int *z)
2599178019Sjkim{
2600178019Sjkim	static int touchpad_buttons;
2601178019Sjkim	static int guest_buttons;
2602183888Sdumbbell	int w, x0, y0;
260341016Sdfr
2604255153Sdumbbell	/* TouchPad PS/2 absolute mode message format with capFourButtons:
2605178019Sjkim	 *
2606178019Sjkim	 *  Bits:        7   6   5   4   3   2   1   0 (LSB)
2607178019Sjkim	 *  ------------------------------------------------
2608178019Sjkim	 *  ipacket[0]:  1   0  W3  W2   0  W1   R   L
2609178019Sjkim	 *  ipacket[1]: Yb  Ya  Y9  Y8  Xb  Xa  X9  X8
2610178019Sjkim	 *  ipacket[2]: Z7  Z6  Z5  Z4  Z3  Z2  Z1  Z0
2611255153Sdumbbell	 *  ipacket[3]:  1   1  Yc  Xc   0  W0 D^R U^L
2612178019Sjkim	 *  ipacket[4]: X7  X6  X5  X4  X3  X2  X1  X0
2613178019Sjkim	 *  ipacket[5]: Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
2614178019Sjkim	 *
2615178019Sjkim	 * Legend:
2616178019Sjkim	 *  L: left physical mouse button
2617178019Sjkim	 *  R: right physical mouse button
2618178019Sjkim	 *  D: down button
2619178019Sjkim	 *  U: up button
2620178019Sjkim	 *  W: "wrist" value
2621178019Sjkim	 *  X: x position
2622183888Sdumbbell	 *  Y: y position
2623178019Sjkim	 *  Z: pressure
2624178019Sjkim	 *
2625255153Sdumbbell	 * Without capFourButtons but with nExtendeButtons and/or capMiddle
2626255153Sdumbbell	 *
2627255153Sdumbbell	 *  Bits:        7   6   5   4      3      2      1      0 (LSB)
2628255153Sdumbbell	 *  ------------------------------------------------------
2629255153Sdumbbell	 *  ipacket[3]:  1   1  Yc  Xc      0     W0    E^R    M^L
2630255153Sdumbbell	 *  ipacket[4]: X7  X6  X5  X4  X3|b7  X2|b5  X1|b3  X0|b1
2631255153Sdumbbell	 *  ipacket[5]: Y7  Y6  Y5  Y4  Y3|b8  Y2|b6  Y1|b4  Y0|b2
2632255153Sdumbbell	 *
2633255153Sdumbbell	 * Legend:
2634255153Sdumbbell	 *  M: Middle physical mouse button
2635255153Sdumbbell	 *  E: Extended mouse buttons reported instead of low bits of X and Y
2636255153Sdumbbell	 *  b1-b8: Extended mouse buttons
2637255153Sdumbbell	 *    Only ((nExtendedButtons + 1) >> 1) bits are used in packet
2638255153Sdumbbell	 *    4 and 5, for reading X and Y value they should be zeroed.
2639255153Sdumbbell	 *
2640178019Sjkim	 * Absolute reportable limits:    0 - 6143.
2641178019Sjkim	 * Typical bezel limits:       1472 - 5472.
2642178019Sjkim	 * Typical edge marings:       1632 - 5312.
2643178019Sjkim	 *
2644178019Sjkim	 * w = 3 Passthrough Packet
2645178019Sjkim	 *
2646178019Sjkim	 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2647178019Sjkim	 */
264841016Sdfr
2649178019Sjkim	if (!synaptics_support)
2650178019Sjkim		return (0);
265141016Sdfr
2652178019Sjkim	/* Sanity check for out of sync packets. */
2653178019Sjkim	if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2654178019Sjkim	    (pb->ipacket[3] & 0xc8) != 0xc0)
2655178019Sjkim		return (-1);
265649965Syokota
2657183888Sdumbbell	*x = *y = 0;
265858230Syokota
2659183888Sdumbbell	/*
2660183888Sdumbbell	 * Pressure value.
2661183888Sdumbbell	 * Interpretation:
2662183888Sdumbbell	 *   z = 0      No finger contact
2663183888Sdumbbell	 *   z = 10     Finger hovering near the pad
2664183888Sdumbbell	 *   z = 30     Very light finger contact
2665183888Sdumbbell	 *   z = 80     Normal finger contact
2666183888Sdumbbell	 *   z = 110    Very heavy finger contact
2667183888Sdumbbell	 *   z = 200    Finger lying flat on pad surface
2668183888Sdumbbell	 *   z = 255    Maximum reportable Z
2669183888Sdumbbell	 */
2670178019Sjkim	*z = pb->ipacket[2];
267158230Syokota
2672183888Sdumbbell	/*
2673183888Sdumbbell	 * Finger width value
2674183888Sdumbbell	 * Interpretation:
2675183888Sdumbbell	 *   w = 0      Two finger on the pad (capMultiFinger needed)
2676183888Sdumbbell	 *   w = 1      Three or more fingers (capMultiFinger needed)
2677183888Sdumbbell	 *   w = 2      Pen (instead of finger) (capPen needed)
2678183888Sdumbbell	 *   w = 3      Reserved (passthrough?)
2679183888Sdumbbell	 *   w = 4-7    Finger of normal width (capPalmDetect needed)
2680183888Sdumbbell	 *   w = 8-14   Very wide finger or palm (capPalmDetect needed)
2681183888Sdumbbell	 *   w = 15     Maximum reportable width (capPalmDetect needed)
2682183888Sdumbbell	 */
2683183888Sdumbbell	/* XXX Is checking capExtended enough? */
2684178019Sjkim	if (sc->synhw.capExtended)
2685133301Sphilip		w = ((pb->ipacket[0] & 0x30) >> 2) |
2686133301Sphilip		    ((pb->ipacket[0] & 0x04) >> 1) |
2687133301Sphilip		    ((pb->ipacket[3] & 0x04) >> 2);
2688178019Sjkim	else {
2689183888Sdumbbell		/* Assume a finger of regular width. */
2690133301Sphilip		w = 4;
2691178019Sjkim	}
2692133301Sphilip
2693255153Sdumbbell	/*
2694255153Sdumbbell	 * Handle packets from the guest device. See:
2695255153Sdumbbell	 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
2696255153Sdumbbell	 */
2697178019Sjkim	if (w == 3 && sc->synhw.capPassthrough) {
2698178019Sjkim		*x = ((pb->ipacket[1] & 0x10) ?
2699133868Sphilip		    pb->ipacket[4] - 256 : pb->ipacket[4]);
2700178019Sjkim		*y = ((pb->ipacket[1] & 0x20) ?
2701133868Sphilip		    pb->ipacket[5] - 256 : pb->ipacket[5]);
2702178019Sjkim		*z = 0;
2703133868Sphilip
2704133868Sphilip		guest_buttons = 0;
2705133868Sphilip		if (pb->ipacket[1] & 0x01)
2706178019Sjkim			guest_buttons |= MOUSE_BUTTON1DOWN;
2707133868Sphilip		if (pb->ipacket[1] & 0x04)
2708178019Sjkim			guest_buttons |= MOUSE_BUTTON2DOWN;
2709133868Sphilip		if (pb->ipacket[1] & 0x02)
2710178019Sjkim			guest_buttons |= MOUSE_BUTTON3DOWN;
2711133868Sphilip
2712178019Sjkim		ms->button = touchpad_buttons | guest_buttons;
2713183888Sdumbbell		goto SYNAPTICS_END;
2714178019Sjkim	}
2715133868Sphilip
2716178019Sjkim	/* Button presses */
2717178019Sjkim	touchpad_buttons = 0;
2718178019Sjkim	if (pb->ipacket[0] & 0x01)
2719178019Sjkim		touchpad_buttons |= MOUSE_BUTTON1DOWN;
2720178019Sjkim	if (pb->ipacket[0] & 0x02)
2721178019Sjkim		touchpad_buttons |= MOUSE_BUTTON3DOWN;
2722132865Snjl
2723178019Sjkim	if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2724255153Sdumbbell		if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
2725178019Sjkim			touchpad_buttons |= MOUSE_BUTTON4DOWN;
2726255153Sdumbbell		if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
2727178019Sjkim			touchpad_buttons |= MOUSE_BUTTON5DOWN;
2728255153Sdumbbell	} else if (sc->synhw.capExtended && sc->synhw.capMiddle) {
2729255153Sdumbbell		/* Middle Button */
2730255153Sdumbbell		if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
2731255153Sdumbbell			touchpad_buttons |= MOUSE_BUTTON2DOWN;
2732255153Sdumbbell	} else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
2733255153Sdumbbell		/* Extended Buttons */
2734255153Sdumbbell		if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
2735255153Sdumbbell			if (sc->syninfo.directional_scrolls) {
2736255153Sdumbbell				if (pb->ipacket[4] & 0x01)
2737255153Sdumbbell					touchpad_buttons |= MOUSE_BUTTON4DOWN;
2738255153Sdumbbell				if (pb->ipacket[5] & 0x01)
2739255153Sdumbbell					touchpad_buttons |= MOUSE_BUTTON5DOWN;
2740255153Sdumbbell				if (pb->ipacket[4] & 0x02)
2741255153Sdumbbell					touchpad_buttons |= MOUSE_BUTTON6DOWN;
2742255153Sdumbbell				if (pb->ipacket[5] & 0x02)
2743255153Sdumbbell					touchpad_buttons |= MOUSE_BUTTON7DOWN;
2744255153Sdumbbell			} else {
2745255153Sdumbbell				touchpad_buttons |= MOUSE_BUTTON2DOWN;
2746255153Sdumbbell			}
2747132865Snjl
2748255153Sdumbbell			/*
2749255153Sdumbbell			 * Zero out bits used by extended buttons to avoid
2750255153Sdumbbell			 * misinterpretation of the data absolute position.
2751255153Sdumbbell			 *
2752255153Sdumbbell			 * The bits represented by
2753255153Sdumbbell			 *
2754255153Sdumbbell			 *     (nExtendedButtons + 1) >> 1
2755255153Sdumbbell			 *
2756255153Sdumbbell			 * will be masked out in both bytes.
2757255153Sdumbbell			 * The mask for n bits is computed with the formula
2758255153Sdumbbell			 *
2759255153Sdumbbell			 *     (1 << n) - 1
2760255153Sdumbbell			 */
2761255153Sdumbbell			int maskedbits = 0;
2762255153Sdumbbell			int mask = 0;
2763255153Sdumbbell			maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
2764255153Sdumbbell			mask = (1 << maskedbits) - 1;
2765255153Sdumbbell			pb->ipacket[4] &= ~(mask);
2766255153Sdumbbell			pb->ipacket[5] &= ~(mask);
2767139982Sphilip		}
2768178019Sjkim	}
2769139982Sphilip
2770178019Sjkim	ms->button = touchpad_buttons | guest_buttons;
2771139982Sphilip
2772183888Sdumbbell	/* Check pressure to detect a real wanted action on the
2773183888Sdumbbell	 * touchpad. */
2774183888Sdumbbell	if (*z >= sc->syninfo.min_pressure) {
2775183888Sdumbbell		synapticsaction_t *synaction;
2776183888Sdumbbell		int cursor, peer, window;
2777183888Sdumbbell		int dx, dy, dxp, dyp;
2778183888Sdumbbell		int max_width, max_pressure;
2779183888Sdumbbell		int margin_top, margin_right, margin_bottom, margin_left;
2780183888Sdumbbell		int na_top, na_right, na_bottom, na_left;
2781183888Sdumbbell		int window_min, window_max;
2782183888Sdumbbell		int multiplicator;
2783183888Sdumbbell		int weight_current, weight_previous, weight_len_squared;
2784183888Sdumbbell		int div_min, div_max, div_len;
2785183888Sdumbbell		int vscroll_hor_area, vscroll_ver_area;
2786183888Sdumbbell
2787183888Sdumbbell		int len, weight_prev_x, weight_prev_y;
2788183888Sdumbbell		int div_max_x, div_max_y, div_x, div_y;
2789183888Sdumbbell
2790183888Sdumbbell		/* Read sysctl. */
2791183888Sdumbbell		/* XXX Verify values? */
2792183888Sdumbbell		max_width = sc->syninfo.max_width;
2793183888Sdumbbell		max_pressure = sc->syninfo.max_pressure;
2794183888Sdumbbell		margin_top = sc->syninfo.margin_top;
2795183888Sdumbbell		margin_right = sc->syninfo.margin_right;
2796183888Sdumbbell		margin_bottom = sc->syninfo.margin_bottom;
2797183888Sdumbbell		margin_left = sc->syninfo.margin_left;
2798183888Sdumbbell		na_top = sc->syninfo.na_top;
2799183888Sdumbbell		na_right = sc->syninfo.na_right;
2800183888Sdumbbell		na_bottom = sc->syninfo.na_bottom;
2801183888Sdumbbell		na_left = sc->syninfo.na_left;
2802183888Sdumbbell		window_min = sc->syninfo.window_min;
2803183888Sdumbbell		window_max = sc->syninfo.window_max;
2804183888Sdumbbell		multiplicator = sc->syninfo.multiplicator;
2805183888Sdumbbell		weight_current = sc->syninfo.weight_current;
2806183888Sdumbbell		weight_previous = sc->syninfo.weight_previous;
2807183888Sdumbbell		weight_len_squared = sc->syninfo.weight_len_squared;
2808183888Sdumbbell		div_min = sc->syninfo.div_min;
2809183888Sdumbbell		div_max = sc->syninfo.div_max;
2810183888Sdumbbell		div_len = sc->syninfo.div_len;
2811183888Sdumbbell		vscroll_hor_area = sc->syninfo.vscroll_hor_area;
2812183888Sdumbbell		vscroll_ver_area = sc->syninfo.vscroll_ver_area;
2813183888Sdumbbell
2814183888Sdumbbell		/* Palm detection. */
2815183888Sdumbbell		if (!(
2816183888Sdumbbell		    (sc->synhw.capMultiFinger && (w == 0 || w == 1)) ||
2817183888Sdumbbell		    (sc->synhw.capPalmDetect && w >= 4 && w <= max_width) ||
2818183888Sdumbbell		    (!sc->synhw.capPalmDetect && *z <= max_pressure) ||
2819183888Sdumbbell		    (sc->synhw.capPen && w == 2))) {
2820183888Sdumbbell			/*
2821183888Sdumbbell			 * We consider the packet irrelevant for the current
2822183888Sdumbbell			 * action when:
2823183888Sdumbbell			 *  - the width isn't comprised in:
2824183888Sdumbbell			 *    [4; max_width]
2825183888Sdumbbell			 *  - the pressure isn't comprised in:
2826183888Sdumbbell			 *    [min_pressure; max_pressure]
2827183888Sdumbbell			 *  - pen aren't supported but w is 2
2828183888Sdumbbell			 *
2829183888Sdumbbell			 *  Note that this doesn't terminate the current action.
2830183888Sdumbbell			 */
2831183888Sdumbbell			VLOG(2, (LOG_DEBUG,
2832183888Sdumbbell			    "synaptics: palm detected! (%d)\n", w));
2833183888Sdumbbell			goto SYNAPTICS_END;
2834183888Sdumbbell		}
2835183888Sdumbbell
2836183888Sdumbbell		/* Read current absolute position. */
2837132865Snjl		x0 = ((pb->ipacket[3] & 0x10) << 8) |
2838183888Sdumbbell		    ((pb->ipacket[1] & 0x0f) << 8) |
2839183888Sdumbbell		    pb->ipacket[4];
2840132865Snjl		y0 = ((pb->ipacket[3] & 0x20) << 7) |
2841183888Sdumbbell		    ((pb->ipacket[1] & 0xf0) << 4) |
2842183888Sdumbbell		    pb->ipacket[5];
2843132865Snjl
2844183888Sdumbbell		synaction = &(sc->synaction);
2845139982Sphilip
2846183888Sdumbbell		/*
2847183888Sdumbbell		 * If the action is just beginning, init the structure and
2848183888Sdumbbell		 * compute tap timeout.
2849183888Sdumbbell		 */
2850183888Sdumbbell		if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
2851183888Sdumbbell			VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
2852183888Sdumbbell
2853183888Sdumbbell			/* Store the first point of this action. */
2854183888Sdumbbell			synaction->start_x = x0;
2855183888Sdumbbell			synaction->start_y = y0;
2856183888Sdumbbell			dx = dy = 0;
2857183888Sdumbbell
2858183888Sdumbbell			/* Initialize queue. */
2859183888Sdumbbell			synaction->queue_cursor = SYNAPTICS_PACKETQUEUE;
2860183888Sdumbbell			synaction->queue_len = 0;
2861183888Sdumbbell			synaction->window_min = window_min;
2862183888Sdumbbell
2863183888Sdumbbell			/* Reset average. */
2864183888Sdumbbell			synaction->avg_dx = 0;
2865183888Sdumbbell			synaction->avg_dy = 0;
2866183888Sdumbbell
2867183888Sdumbbell			/* Reset squelch. */
2868183888Sdumbbell			synaction->squelch_x = 0;
2869183888Sdumbbell			synaction->squelch_y = 0;
2870183888Sdumbbell
2871183888Sdumbbell			/* Reset pressure peak. */
2872183888Sdumbbell			sc->zmax = 0;
2873183888Sdumbbell
2874183888Sdumbbell			/* Reset fingers count. */
2875183888Sdumbbell			synaction->fingers_nb = 0;
2876183888Sdumbbell
2877183888Sdumbbell			/* Reset virtual scrolling state. */
2878183888Sdumbbell			synaction->in_vscroll = 0;
2879183888Sdumbbell
2880183888Sdumbbell			/* Compute tap timeout. */
2881183888Sdumbbell			sc->taptimeout.tv_sec  = tap_timeout / 1000000;
2882183888Sdumbbell			sc->taptimeout.tv_usec = tap_timeout % 1000000;
2883183888Sdumbbell			timevaladd(&sc->taptimeout, &sc->lastsoftintr);
2884183888Sdumbbell
2885183888Sdumbbell			sc->flags |= PSM_FLAGS_FINGERDOWN;
2886183888Sdumbbell		} else {
2887183888Sdumbbell			/* Calculate the current delta. */
2888183888Sdumbbell			cursor = synaction->queue_cursor;
2889183888Sdumbbell			dx = x0 - synaction->queue[cursor].x;
2890183888Sdumbbell			dy = y0 - synaction->queue[cursor].y;
2891183888Sdumbbell		}
2892183888Sdumbbell
2893183888Sdumbbell		/* If in tap-hold, add the recorded button. */
2894183888Sdumbbell		if (synaction->in_taphold)
2895183888Sdumbbell			ms->button |= synaction->tap_button;
2896183888Sdumbbell
2897183888Sdumbbell		/*
2898183888Sdumbbell		 * From now on, we can use the SYNAPTICS_END label to skip
2899183888Sdumbbell		 * the current packet.
2900183888Sdumbbell		 */
2901183888Sdumbbell
2902183888Sdumbbell		/*
2903183888Sdumbbell		 * Limit the coordinates to the specified margins because
2904183888Sdumbbell		 * this area isn't very reliable.
2905183888Sdumbbell		 */
2906183888Sdumbbell		if (x0 <= margin_left)
2907183888Sdumbbell			x0 = margin_left;
2908183888Sdumbbell		else if (x0 >= 6143 - margin_right)
2909183888Sdumbbell			x0 = 6143 - margin_right;
2910183888Sdumbbell		if (y0 <= margin_bottom)
2911183888Sdumbbell			y0 = margin_bottom;
2912183888Sdumbbell		else if (y0 >= 6143 - margin_top)
2913183888Sdumbbell			y0 = 6143 - margin_top;
2914183888Sdumbbell
2915183888Sdumbbell		VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
2916183888Sdumbbell		    x0, y0, *z, w));
2917183888Sdumbbell
2918183888Sdumbbell		/* Queue this new packet. */
2919183888Sdumbbell		cursor = SYNAPTICS_QUEUE_CURSOR(synaction->queue_cursor - 1);
2920183888Sdumbbell		synaction->queue[cursor].x = x0;
2921183888Sdumbbell		synaction->queue[cursor].y = y0;
2922183888Sdumbbell		synaction->queue_cursor = cursor;
2923183888Sdumbbell		if (synaction->queue_len < SYNAPTICS_PACKETQUEUE)
2924183888Sdumbbell			synaction->queue_len++;
2925183888Sdumbbell		VLOG(5, (LOG_DEBUG,
2926183888Sdumbbell		    "synaptics: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
2927183888Sdumbbell		    cursor, x0, y0, dx, dy));
2928183888Sdumbbell
2929183888Sdumbbell		/*
2930183888Sdumbbell		 * For tap, we keep the maximum number of fingers and the
2931183888Sdumbbell		 * pressure peak. Also with multiple fingers, we increase
2932183888Sdumbbell		 * the minimum window.
2933183888Sdumbbell		 */
2934183888Sdumbbell		switch (w) {
2935183888Sdumbbell		case 1: /* Three or more fingers. */
2936183888Sdumbbell			synaction->fingers_nb = imax(3, synaction->fingers_nb);
2937183888Sdumbbell			synaction->window_min = window_max;
2938183888Sdumbbell			break;
2939183888Sdumbbell		case 0: /* Two fingers. */
2940183888Sdumbbell			synaction->fingers_nb = imax(2, synaction->fingers_nb);
2941183888Sdumbbell			synaction->window_min = window_max;
2942183888Sdumbbell			break;
2943183888Sdumbbell		default: /* One finger or undetectable. */
2944183888Sdumbbell			synaction->fingers_nb = imax(1, synaction->fingers_nb);
2945183888Sdumbbell		}
2946183888Sdumbbell		sc->zmax = imax(*z, sc->zmax);
2947183888Sdumbbell
2948183888Sdumbbell		/* Do we have enough packets to consider this a movement? */
2949183888Sdumbbell		if (synaction->queue_len < synaction->window_min)
2950183888Sdumbbell			goto SYNAPTICS_END;
2951183888Sdumbbell
2952183888Sdumbbell		/* Is a scrolling action occuring? */
2953183888Sdumbbell		if (!synaction->in_taphold && !synaction->in_vscroll) {
2954178019Sjkim			/*
2955183888Sdumbbell			 * A scrolling action must not conflict with a tap
2956183888Sdumbbell			 * action. Here are the conditions to consider a
2957183888Sdumbbell			 * scrolling action:
2958183888Sdumbbell			 *  - the action in a configurable area
2959183888Sdumbbell			 *  - one of the following:
2960183888Sdumbbell			 *     . the distance between the last packet and the
2961183888Sdumbbell			 *       first should be above a configurable minimum
2962183888Sdumbbell			 *     . tap timed out
2963178019Sjkim			 */
2964183888Sdumbbell			dxp = abs(synaction->queue[synaction->queue_cursor].x -
2965183888Sdumbbell			    synaction->start_x);
2966183888Sdumbbell			dyp = abs(synaction->queue[synaction->queue_cursor].y -
2967183888Sdumbbell			    synaction->start_y);
2968139982Sphilip
2969183888Sdumbbell			if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, >) ||
2970183888Sdumbbell			    dxp >= sc->syninfo.vscroll_min_delta ||
2971183888Sdumbbell			    dyp >= sc->syninfo.vscroll_min_delta) {
2972183888Sdumbbell				/* Check for horizontal scrolling. */
2973183888Sdumbbell				if ((vscroll_hor_area > 0 &&
2974183888Sdumbbell				    synaction->start_y <= vscroll_hor_area) ||
2975183888Sdumbbell				    (vscroll_hor_area < 0 &&
2976183888Sdumbbell				     synaction->start_y >=
2977183888Sdumbbell				     6143 + vscroll_hor_area))
2978183888Sdumbbell					synaction->in_vscroll += 2;
2979139982Sphilip
2980183888Sdumbbell				/* Check for vertical scrolling. */
2981183888Sdumbbell				if ((vscroll_ver_area > 0 &&
2982183888Sdumbbell				    synaction->start_x <= vscroll_ver_area) ||
2983183888Sdumbbell				    (vscroll_ver_area < 0 &&
2984183888Sdumbbell				     synaction->start_x >=
2985183888Sdumbbell				     6143 + vscroll_ver_area))
2986183888Sdumbbell					synaction->in_vscroll += 1;
2987139982Sphilip
2988183888Sdumbbell				/* Avoid conflicts if area overlaps. */
2989183888Sdumbbell				if (synaction->in_vscroll == 3)
2990183888Sdumbbell					synaction->in_vscroll =
2991183888Sdumbbell					    (dxp > dyp) ? 2 : 1;
2992183888Sdumbbell			}
2993183888Sdumbbell			VLOG(5, (LOG_DEBUG,
2994183888Sdumbbell			    "synaptics: virtual scrolling: %s "
2995183888Sdumbbell			    "(direction=%d, dxp=%d, dyp=%d)\n",
2996183888Sdumbbell			    synaction->in_vscroll ? "YES" : "NO",
2997183888Sdumbbell			    synaction->in_vscroll, dxp, dyp));
2998183888Sdumbbell		}
2999139982Sphilip
3000183888Sdumbbell		weight_prev_x = weight_prev_y = weight_previous;
3001183888Sdumbbell		div_max_x = div_max_y = div_max;
3002183888Sdumbbell
3003183888Sdumbbell		if (synaction->in_vscroll) {
3004183888Sdumbbell			/* Dividers are different with virtual scrolling. */
3005183888Sdumbbell			div_min = sc->syninfo.vscroll_div_min;
3006183888Sdumbbell			div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
3007183888Sdumbbell		} else {
3008178019Sjkim			/*
3009183888Sdumbbell			 * There's a lot of noise in coordinates when
3010183888Sdumbbell			 * the finger is on the touchpad's borders. When
3011183888Sdumbbell			 * using this area, we apply a special weight and
3012183888Sdumbbell			 * div.
3013178019Sjkim			 */
3014183888Sdumbbell			if (x0 <= na_left || x0 >= 6143 - na_right) {
3015183888Sdumbbell				weight_prev_x = sc->syninfo.weight_previous_na;
3016183888Sdumbbell				div_max_x = sc->syninfo.div_max_na;
3017183888Sdumbbell			}
3018139982Sphilip
3019183888Sdumbbell			if (y0 <= na_bottom || y0 >= 6143 - na_top) {
3020183888Sdumbbell				weight_prev_y = sc->syninfo.weight_previous_na;
3021183888Sdumbbell				div_max_y = sc->syninfo.div_max_na;
3022183888Sdumbbell			}
3023183888Sdumbbell		}
3024139982Sphilip
3025183888Sdumbbell		/*
3026183888Sdumbbell		 * Calculate weights for the average operands and
3027183888Sdumbbell		 * the divisor. Both depend on the distance between
3028183888Sdumbbell		 * the current packet and a previous one (based on the
3029183888Sdumbbell		 * window width).
3030183888Sdumbbell		 */
3031183888Sdumbbell		window = imin(synaction->queue_len, window_max);
3032183888Sdumbbell		peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
3033183888Sdumbbell		dxp = abs(x0 - synaction->queue[peer].x) + 1;
3034183888Sdumbbell		dyp = abs(y0 - synaction->queue[peer].y) + 1;
3035183888Sdumbbell		len = (dxp * dxp) + (dyp * dyp);
3036183888Sdumbbell		weight_prev_x = imin(weight_prev_x,
3037183888Sdumbbell		    weight_len_squared * weight_prev_x / len);
3038183888Sdumbbell		weight_prev_y = imin(weight_prev_y,
3039183888Sdumbbell		    weight_len_squared * weight_prev_y / len);
3040183888Sdumbbell
3041183888Sdumbbell		len = (dxp + dyp) / 2;
3042183888Sdumbbell		div_x = div_len * div_max_x / len;
3043183888Sdumbbell		div_x = imin(div_max_x, div_x);
3044183888Sdumbbell		div_x = imax(div_min, div_x);
3045183888Sdumbbell		div_y = div_len * div_max_y / len;
3046183888Sdumbbell		div_y = imin(div_max_y, div_y);
3047183888Sdumbbell		div_y = imax(div_min, div_y);
3048183888Sdumbbell
3049183888Sdumbbell		VLOG(3, (LOG_DEBUG,
3050183888Sdumbbell		    "synaptics: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
3051183888Sdumbbell		    peer, len, weight_prev_x, weight_prev_y, div_x, div_y));
3052183888Sdumbbell
3053183888Sdumbbell		/* Compute averages. */
3054183888Sdumbbell		synaction->avg_dx =
3055183888Sdumbbell		    (weight_current * dx * multiplicator +
3056183888Sdumbbell		     weight_prev_x * synaction->avg_dx) /
3057183888Sdumbbell		    (weight_current + weight_prev_x);
3058183888Sdumbbell
3059183888Sdumbbell		synaction->avg_dy =
3060183888Sdumbbell		    (weight_current * dy * multiplicator +
3061183888Sdumbbell		     weight_prev_y * synaction->avg_dy) /
3062183888Sdumbbell		    (weight_current + weight_prev_y);
3063183888Sdumbbell
3064183888Sdumbbell		VLOG(5, (LOG_DEBUG,
3065183888Sdumbbell		    "synaptics: avg_dx~=%d, avg_dy~=%d\n",
3066183888Sdumbbell		    synaction->avg_dx / multiplicator,
3067183888Sdumbbell		    synaction->avg_dy / multiplicator));
3068183888Sdumbbell
3069183888Sdumbbell		/* Use these averages to calculate x & y. */
3070183888Sdumbbell		synaction->squelch_x += synaction->avg_dx;
3071183888Sdumbbell		*x = synaction->squelch_x / (div_x * multiplicator);
3072183888Sdumbbell		synaction->squelch_x = synaction->squelch_x %
3073183888Sdumbbell		    (div_x * multiplicator);
3074183888Sdumbbell
3075183888Sdumbbell		synaction->squelch_y += synaction->avg_dy;
3076183888Sdumbbell		*y = synaction->squelch_y / (div_y * multiplicator);
3077183888Sdumbbell		synaction->squelch_y = synaction->squelch_y %
3078183888Sdumbbell		    (div_y * multiplicator);
3079183888Sdumbbell
3080183888Sdumbbell		if (synaction->in_vscroll) {
3081183888Sdumbbell			switch(synaction->in_vscroll) {
3082183888Sdumbbell			case 1: /* Vertical scrolling. */
3083183888Sdumbbell				if (*y != 0)
3084183888Sdumbbell					ms->button |= (*y > 0) ?
3085183888Sdumbbell					    MOUSE_BUTTON4DOWN :
3086183888Sdumbbell					    MOUSE_BUTTON5DOWN;
3087183888Sdumbbell				break;
3088183888Sdumbbell			case 2: /* Horizontal scrolling. */
3089183888Sdumbbell				if (*x != 0)
3090183888Sdumbbell					ms->button |= (*x > 0) ?
3091183888Sdumbbell					    MOUSE_BUTTON7DOWN :
3092183888Sdumbbell					    MOUSE_BUTTON6DOWN;
3093183888Sdumbbell				break;
3094183888Sdumbbell			}
3095183888Sdumbbell
3096183888Sdumbbell			/* The pointer is not moved. */
3097183888Sdumbbell			*x = *y = 0;
3098183888Sdumbbell		} else {
3099183888Sdumbbell			VLOG(3, (LOG_DEBUG, "synaptics: [%d, %d] -> [%d, %d]\n",
3100183888Sdumbbell			    dx, dy, *x, *y));
3101183888Sdumbbell		}
3102183888Sdumbbell	} else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3103183888Sdumbbell		/*
3104183888Sdumbbell		 * An action is currently taking place but the pressure
3105183888Sdumbbell		 * dropped under the minimum, putting an end to it.
3106183888Sdumbbell		 */
3107183888Sdumbbell		synapticsaction_t *synaction;
3108183888Sdumbbell		int taphold_timeout, dx, dy, tap_max_delta;
3109183888Sdumbbell
3110183888Sdumbbell		synaction = &(sc->synaction);
3111183888Sdumbbell		dx = abs(synaction->queue[synaction->queue_cursor].x -
3112183888Sdumbbell		    synaction->start_x);
3113183888Sdumbbell		dy = abs(synaction->queue[synaction->queue_cursor].y -
3114183888Sdumbbell		    synaction->start_y);
3115183888Sdumbbell
3116183888Sdumbbell		/* Max delta is disabled for multi-fingers tap. */
3117183888Sdumbbell		if (synaction->fingers_nb > 1)
3118183888Sdumbbell			tap_max_delta = imax(dx, dy);
3119183888Sdumbbell		else
3120183888Sdumbbell			tap_max_delta = sc->syninfo.tap_max_delta;
3121183888Sdumbbell
3122183888Sdumbbell		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3123183888Sdumbbell
3124183888Sdumbbell		/* Check for tap. */
3125183888Sdumbbell		VLOG(3, (LOG_DEBUG,
3126183888Sdumbbell		    "synaptics: zmax=%d, dx=%d, dy=%d, "
3127183888Sdumbbell		    "delta=%d, fingers=%d, queue=%d\n",
3128183888Sdumbbell		    sc->zmax, dx, dy, tap_max_delta, synaction->fingers_nb,
3129183888Sdumbbell		    synaction->queue_len));
3130183888Sdumbbell		if (!synaction->in_vscroll && sc->zmax >= tap_threshold &&
3131183888Sdumbbell		    timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=) &&
3132183888Sdumbbell		    dx <= tap_max_delta && dy <= tap_max_delta &&
3133183888Sdumbbell		    synaction->queue_len >= sc->syninfo.tap_min_queue) {
3134178019Sjkim			/*
3135183888Sdumbbell			 * We have a tap if:
3136183888Sdumbbell			 *   - the maximum pressure went over tap_threshold
3137183888Sdumbbell			 *   - the action ended before tap_timeout
3138183888Sdumbbell			 *
3139183888Sdumbbell			 * To handle tap-hold, we must delay any button push to
3140183888Sdumbbell			 * the next action.
3141178019Sjkim			 */
3142183888Sdumbbell			if (synaction->in_taphold) {
3143183888Sdumbbell				/*
3144183888Sdumbbell				 * This is the second and last tap of a
3145183888Sdumbbell				 * double tap action, not a tap-hold.
3146183888Sdumbbell				 */
3147183888Sdumbbell				synaction->in_taphold = 0;
3148139982Sphilip
3149183888Sdumbbell				/*
3150183888Sdumbbell				 * For double-tap to work:
3151183888Sdumbbell				 *   - no button press is emitted (to
3152183888Sdumbbell				 *     simulate a button release)
3153183888Sdumbbell				 *   - PSM_FLAGS_FINGERDOWN is set to
3154183888Sdumbbell				 *     force the next packet to emit a
3155183888Sdumbbell				 *     button press)
3156183888Sdumbbell				 */
3157183888Sdumbbell				VLOG(2, (LOG_DEBUG,
3158183888Sdumbbell				    "synaptics: button RELEASE: %d\n",
3159183888Sdumbbell				    synaction->tap_button));
3160183888Sdumbbell				sc->flags |= PSM_FLAGS_FINGERDOWN;
3161178019Sjkim			} else {
3162178019Sjkim				/*
3163183888Sdumbbell				 * This is the first tap: we set the
3164183888Sdumbbell				 * tap-hold state and notify the button
3165183888Sdumbbell				 * down event.
3166178019Sjkim				 */
3167183888Sdumbbell				synaction->in_taphold = 1;
3168183888Sdumbbell				taphold_timeout = sc->syninfo.taphold_timeout;
3169183888Sdumbbell				sc->taptimeout.tv_sec  = taphold_timeout /
3170183888Sdumbbell				    1000000;
3171183888Sdumbbell				sc->taptimeout.tv_usec = taphold_timeout %
3172183888Sdumbbell				    1000000;
3173183888Sdumbbell				timevaladd(&sc->taptimeout, &sc->lastsoftintr);
3174139982Sphilip
3175183888Sdumbbell				switch (synaction->fingers_nb) {
3176183888Sdumbbell				case 3:
3177183888Sdumbbell					synaction->tap_button =
3178183888Sdumbbell					    MOUSE_BUTTON2DOWN;
3179183888Sdumbbell					break;
3180183888Sdumbbell				case 2:
3181183888Sdumbbell					synaction->tap_button =
3182183888Sdumbbell					    MOUSE_BUTTON3DOWN;
3183183888Sdumbbell					break;
3184183888Sdumbbell				default:
3185183888Sdumbbell					synaction->tap_button =
3186183888Sdumbbell					    MOUSE_BUTTON1DOWN;
3187183888Sdumbbell				}
3188183888Sdumbbell				VLOG(2, (LOG_DEBUG,
3189183888Sdumbbell				    "synaptics: button PRESS: %d\n",
3190183888Sdumbbell				    synaction->tap_button));
3191183888Sdumbbell				ms->button |= synaction->tap_button;
3192178019Sjkim			}
3193183888Sdumbbell		} else {
3194183888Sdumbbell			/*
3195183888Sdumbbell			 * Not enough pressure or timeout: reset
3196183888Sdumbbell			 * tap-hold state.
3197183888Sdumbbell			 */
3198183888Sdumbbell			if (synaction->in_taphold) {
3199183888Sdumbbell				VLOG(2, (LOG_DEBUG,
3200183888Sdumbbell				    "synaptics: button RELEASE: %d\n",
3201183888Sdumbbell				    synaction->tap_button));
3202183888Sdumbbell				synaction->in_taphold = 0;
3203183888Sdumbbell			} else {
3204183888Sdumbbell				VLOG(2, (LOG_DEBUG,
3205183888Sdumbbell				    "synaptics: not a tap-hold\n"));
3206183888Sdumbbell			}
3207183888Sdumbbell		}
3208183888Sdumbbell	} else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) &&
3209183888Sdumbbell	    sc->synaction.in_taphold) {
3210178019Sjkim		/*
3211183888Sdumbbell		 * For a tap-hold to work, the button must remain down at
3212183888Sdumbbell		 * least until timeout (where the in_taphold flags will be
3213183888Sdumbbell		 * cleared) or during the next action.
3214139982Sphilip		 */
3215183888Sdumbbell		if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) {
3216183888Sdumbbell			ms->button |= sc->synaction.tap_button;
3217183888Sdumbbell		} else {
3218183888Sdumbbell			VLOG(2, (LOG_DEBUG,
3219183888Sdumbbell			    "synaptics: button RELEASE: %d\n",
3220183888Sdumbbell			    sc->synaction.tap_button));
3221183888Sdumbbell			sc->synaction.in_taphold = 0;
3222133297Sphilip		}
3223178019Sjkim	}
3224133296Sphilip
3225183888SdumbbellSYNAPTICS_END:
3226183888Sdumbbell	/*
3227183888Sdumbbell	 * Use the extra buttons as a scrollwheel
3228183888Sdumbbell	 *
3229183888Sdumbbell	 * XXX X.Org uses the Z axis for vertical wheel only,
3230183888Sdumbbell	 * whereas moused(8) understands special values to differ
3231183888Sdumbbell	 * vertical and horizontal wheels.
3232183888Sdumbbell	 *
3233183888Sdumbbell	 * xf86-input-mouse needs therefore a small patch to
3234183888Sdumbbell	 * understand these special values. Without it, the
3235183888Sdumbbell	 * horizontal wheel acts as a vertical wheel in X.Org.
3236183888Sdumbbell	 *
3237183888Sdumbbell	 * That's why the horizontal wheel is disabled by
3238183888Sdumbbell	 * default for now.
3239183888Sdumbbell	 */
3240183888Sdumbbell	if (ms->button & MOUSE_BUTTON4DOWN) {
3241178019Sjkim		*z = -1;
3242183888Sdumbbell		ms->button &= ~MOUSE_BUTTON4DOWN;
3243183888Sdumbbell	} else if (ms->button & MOUSE_BUTTON5DOWN) {
3244178019Sjkim		*z = 1;
3245183888Sdumbbell		ms->button &= ~MOUSE_BUTTON5DOWN;
3246183888Sdumbbell	} else if (ms->button & MOUSE_BUTTON6DOWN) {
3247183888Sdumbbell		*z = -2;
3248183888Sdumbbell		ms->button &= ~MOUSE_BUTTON6DOWN;
3249183888Sdumbbell	} else if (ms->button & MOUSE_BUTTON7DOWN) {
3250183888Sdumbbell		*z = 2;
3251183888Sdumbbell		ms->button &= ~MOUSE_BUTTON7DOWN;
3252183888Sdumbbell	} else
3253178019Sjkim		*z = 0;
3254178019Sjkim
3255178019Sjkim	return (0);
3256178019Sjkim}
3257178019Sjkim
3258178019Sjkimstatic void
3259178019Sjkimproc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3260178019Sjkim    int *x, int *y, int *z)
3261178019Sjkim{
3262178019Sjkim	static int butmap_versapad[8] = {
3263178019Sjkim		0,
3264178019Sjkim		MOUSE_BUTTON3DOWN,
3265178019Sjkim		0,
3266178019Sjkim		MOUSE_BUTTON3DOWN,
3267178019Sjkim		MOUSE_BUTTON1DOWN,
3268178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3269178019Sjkim		MOUSE_BUTTON1DOWN,
3270178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
3271178019Sjkim	};
3272178019Sjkim	int c, x0, y0;
3273178019Sjkim
3274178019Sjkim	/* VersaPad PS/2 absolute mode message format
3275178019Sjkim	 *
3276178019Sjkim	 * [packet1]     7   6   5   4   3   2   1   0(LSB)
3277178019Sjkim	 *  ipacket[0]:  1   1   0   A   1   L   T   R
3278178019Sjkim	 *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
3279178019Sjkim	 *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
3280178019Sjkim	 *  ipacket[3]:  1   1   1   A   1   L   T   R
3281178019Sjkim	 *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
3282178019Sjkim	 *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
3283178019Sjkim	 *
3284178019Sjkim	 * [note]
3285178019Sjkim	 *  R: right physical mouse button (1=on)
3286178019Sjkim	 *  T: touch pad virtual button (1=tapping)
3287178019Sjkim	 *  L: left physical mouse button (1=on)
3288178019Sjkim	 *  A: position data is valid (1=valid)
3289178019Sjkim	 *  H: horizontal data (12bit signed integer. H11 is sign bit.)
3290178019Sjkim	 *  V: vertical data (12bit signed integer. V11 is sign bit.)
3291178019Sjkim	 *  P: pressure data
3292178019Sjkim	 *
3293178019Sjkim	 * Tapping is mapped to MOUSE_BUTTON4.
3294178019Sjkim	 */
3295178019Sjkim	c = pb->ipacket[0];
3296178019Sjkim	*x = *y = 0;
3297178019Sjkim	ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
3298178019Sjkim	ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
3299178019Sjkim	if (c & MOUSE_PS2VERSA_IN_USE) {
3300178019Sjkim		x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
3301178019Sjkim		y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
3302178019Sjkim		if (x0 & 0x800)
3303178019Sjkim			x0 -= 0x1000;
3304178019Sjkim		if (y0 & 0x800)
3305178019Sjkim			y0 -= 0x1000;
3306178019Sjkim		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3307178019Sjkim			*x = sc->xold - x0;
3308178019Sjkim			*y = y0 - sc->yold;
3309178019Sjkim			if (*x < 0)	/* XXX */
3310178019Sjkim				++*x;
3311178019Sjkim			else if (*x)
3312178019Sjkim				--*x;
3313178019Sjkim			if (*y < 0)
3314178019Sjkim				++*y;
3315178019Sjkim			else if (*y)
3316178019Sjkim				--*y;
3317178019Sjkim		} else
3318178019Sjkim			sc->flags |= PSM_FLAGS_FINGERDOWN;
3319178019Sjkim		sc->xold = x0;
3320178019Sjkim		sc->yold = y0;
3321178019Sjkim	} else
3322178019Sjkim		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3323178019Sjkim}
3324178019Sjkim
3325178019Sjkimstatic void
3326178019Sjkimpsmsoftintr(void *arg)
3327178019Sjkim{
3328178019Sjkim	/*
3329178019Sjkim	 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
3330178019Sjkim	 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
3331178019Sjkim	 */
3332178019Sjkim	static int butmap[8] = {
3333178019Sjkim		0,
3334178019Sjkim		MOUSE_BUTTON1DOWN,
3335178019Sjkim		MOUSE_BUTTON3DOWN,
3336178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3337178019Sjkim		MOUSE_BUTTON2DOWN,
3338178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
3339178019Sjkim		MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
3340178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
3341178019Sjkim	};
3342212355Sed	struct psm_softc *sc = arg;
3343178019Sjkim	mousestatus_t ms;
3344178019Sjkim	packetbuf_t *pb;
3345178019Sjkim	int x, y, z, c, l, s;
3346178019Sjkim
3347178019Sjkim	getmicrouptime(&sc->lastsoftintr);
3348178019Sjkim
3349178019Sjkim	s = spltty();
3350178019Sjkim
3351178019Sjkim	do {
3352178019Sjkim		pb = &sc->pqueue[sc->pqueue_start];
3353178019Sjkim
3354178019Sjkim		if (sc->mode.level == PSM_LEVEL_NATIVE)
3355178019Sjkim			goto next_native;
3356178019Sjkim
3357178019Sjkim		c = pb->ipacket[0];
3358178019Sjkim		/*
3359178019Sjkim		 * A kludge for Kensington device!
3360178019Sjkim		 * The MSB of the horizontal count appears to be stored in
3361178019Sjkim		 * a strange place.
3362178019Sjkim		 */
3363178019Sjkim		if (sc->hw.model == MOUSE_MODEL_THINK)
3364178019Sjkim			pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
3365178019Sjkim
3366178019Sjkim		/* ignore the overflow bits... */
3367178019Sjkim		x = (c & MOUSE_PS2_XNEG) ?
3368178019Sjkim		    pb->ipacket[1] - 256 : pb->ipacket[1];
3369178019Sjkim		y = (c & MOUSE_PS2_YNEG) ?
3370178019Sjkim		    pb->ipacket[2] - 256 : pb->ipacket[2];
3371133296Sphilip		z = 0;
3372178019Sjkim		ms.obutton = sc->button;	  /* previous button state */
3373178019Sjkim		ms.button = butmap[c & MOUSE_PS2_BUTTONS];
3374178019Sjkim		/* `tapping' action */
3375178019Sjkim		if (sc->config & PSM_CONFIG_FORCETAP)
3376178019Sjkim			ms.button |= ((c & MOUSE_PS2_TAP)) ?
3377178019Sjkim			    0 : MOUSE_BUTTON4DOWN;
3378133296Sphilip
3379178019Sjkim		switch (sc->hw.model) {
3380132865Snjl
3381178019Sjkim		case MOUSE_MODEL_EXPLORER:
3382178019Sjkim			/*
3383178019Sjkim			 *          b7 b6 b5 b4 b3 b2 b1 b0
3384178019Sjkim			 * byte 1:  oy ox sy sx 1  M  R  L
3385178019Sjkim			 * byte 2:  x  x  x  x  x  x  x  x
3386178019Sjkim			 * byte 3:  y  y  y  y  y  y  y  y
3387178019Sjkim			 * byte 4:  *  *  S2 S1 s  d2 d1 d0
3388178019Sjkim			 *
3389178019Sjkim			 * L, M, R, S1, S2: left, middle, right and side buttons
3390178019Sjkim			 * s: wheel data sign bit
3391178019Sjkim			 * d2-d0: wheel data
3392178019Sjkim			 */
3393178019Sjkim			z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
3394178019Sjkim			    (pb->ipacket[3] & 0x0f) - 16 :
3395178019Sjkim			    (pb->ipacket[3] & 0x0f);
3396178019Sjkim			ms.button |=
3397178019Sjkim			    (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
3398178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
3399178019Sjkim			ms.button |=
3400178019Sjkim			    (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
3401178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
3402178019Sjkim			break;
3403178019Sjkim
3404178019Sjkim		case MOUSE_MODEL_INTELLI:
3405178019Sjkim		case MOUSE_MODEL_NET:
3406178019Sjkim			/* wheel data is in the fourth byte */
3407178019Sjkim			z = (char)pb->ipacket[3];
3408178019Sjkim			/*
3409178019Sjkim			 * XXX some mice may send 7 when there is no Z movement?			 */
3410178019Sjkim			if ((z >= 7) || (z <= -7))
3411178019Sjkim				z = 0;
3412178019Sjkim			/* some compatible mice have additional buttons */
3413178019Sjkim			ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
3414178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
3415178019Sjkim			ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
3416178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
3417178019Sjkim			break;
3418178019Sjkim
3419178019Sjkim		case MOUSE_MODEL_MOUSEMANPLUS:
3420178019Sjkim			proc_mmanplus(sc, pb, &ms, &x, &y, &z);
3421178019Sjkim			break;
3422178019Sjkim
3423178019Sjkim		case MOUSE_MODEL_GLIDEPOINT:
3424178019Sjkim			/* `tapping' action */
3425178019Sjkim			ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
3426178019Sjkim			    MOUSE_BUTTON4DOWN;
3427178019Sjkim			break;
3428178019Sjkim
3429178019Sjkim		case MOUSE_MODEL_NETSCROLL:
3430178019Sjkim			/*
3431178019Sjkim			 * three addtional bytes encode buttons and
3432178019Sjkim			 * wheel events
3433178019Sjkim			 */
3434178019Sjkim			ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
3435178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
3436178019Sjkim			ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
3437178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
3438178019Sjkim			z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
3439178019Sjkim			    pb->ipacket[4] - 256 : pb->ipacket[4];
3440178019Sjkim			break;
3441178019Sjkim
3442178019Sjkim		case MOUSE_MODEL_THINK:
3443178019Sjkim			/* the fourth button state in the first byte */
3444178019Sjkim			ms.button |= (c & MOUSE_PS2_TAP) ?
3445178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
3446178019Sjkim			break;
3447178019Sjkim
3448178019Sjkim		case MOUSE_MODEL_VERSAPAD:
3449178019Sjkim			proc_versapad(sc, pb, &ms, &x, &y, &z);
3450178019Sjkim			c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
3451178019Sjkim			    ((y < 0) ? MOUSE_PS2_YNEG : 0);
3452178019Sjkim			break;
3453178019Sjkim
3454178019Sjkim		case MOUSE_MODEL_4D:
3455178019Sjkim			/*
3456178019Sjkim			 *          b7 b6 b5 b4 b3 b2 b1 b0
3457178019Sjkim			 * byte 1:  s2 d2 s1 d1 1  M  R  L
3458178019Sjkim			 * byte 2:  sx x  x  x  x  x  x  x
3459178019Sjkim			 * byte 3:  sy y  y  y  y  y  y  y
3460178019Sjkim			 *
3461178019Sjkim			 * s1: wheel 1 direction
3462178019Sjkim			 * d1: wheel 1 data
3463178019Sjkim			 * s2: wheel 2 direction
3464178019Sjkim			 * d2: wheel 2 data
3465178019Sjkim			 */
3466178019Sjkim			x = (pb->ipacket[1] & 0x80) ?
3467178019Sjkim			    pb->ipacket[1] - 256 : pb->ipacket[1];
3468178019Sjkim			y = (pb->ipacket[2] & 0x80) ?
3469178019Sjkim			    pb->ipacket[2] - 256 : pb->ipacket[2];
3470178019Sjkim			switch (c & MOUSE_4D_WHEELBITS) {
3471178019Sjkim			case 0x10:
3472178019Sjkim				z = 1;
3473178019Sjkim				break;
3474178019Sjkim			case 0x30:
3475178019Sjkim				z = -1;
3476178019Sjkim				break;
3477178019Sjkim			case 0x40:	/* XXX 2nd wheel turning right */
3478178019Sjkim				z = 2;
3479178019Sjkim				break;
3480178019Sjkim			case 0xc0:	/* XXX 2nd wheel turning left */
3481178019Sjkim				z = -2;
3482178019Sjkim				break;
3483178019Sjkim			}
3484178019Sjkim			break;
3485178019Sjkim
3486178019Sjkim		case MOUSE_MODEL_4DPLUS:
3487178019Sjkim			if ((x < 16 - 256) && (y < 16 - 256)) {
3488178019Sjkim				/*
3489178019Sjkim				 *          b7 b6 b5 b4 b3 b2 b1 b0
3490178019Sjkim				 * byte 1:  0  0  1  1  1  M  R  L
3491178019Sjkim				 * byte 2:  0  0  0  0  1  0  0  0
3492178019Sjkim				 * byte 3:  0  0  0  0  S  s  d1 d0
3493178019Sjkim				 *
3494178019Sjkim				 * L, M, R, S: left, middle, right,
3495178019Sjkim				 *             and side buttons
3496178019Sjkim				 * s: wheel data sign bit
3497178019Sjkim				 * d1-d0: wheel data
3498178019Sjkim				 */
3499178019Sjkim				x = y = 0;
3500178019Sjkim				if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
3501178019Sjkim					ms.button |= MOUSE_BUTTON4DOWN;
3502178019Sjkim				z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
3503178019Sjkim				    ((pb->ipacket[2] & 0x07) - 8) :
3504178019Sjkim				    (pb->ipacket[2] & 0x07) ;
3505178019Sjkim			} else {
3506178019Sjkim				/* preserve previous button states */
3507178019Sjkim				ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
3508178019Sjkim			}
3509178019Sjkim			break;
3510178019Sjkim
3511178019Sjkim		case MOUSE_MODEL_SYNAPTICS:
3512178019Sjkim			if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0)
3513178019Sjkim				goto next;
3514178019Sjkim			break;
3515178019Sjkim
3516248478Sjkim		case MOUSE_MODEL_TRACKPOINT:
3517178019Sjkim		case MOUSE_MODEL_GENERIC:
3518178019Sjkim		default:
3519178019Sjkim			break;
3520178019Sjkim		}
3521178019Sjkim
3522178019Sjkim	/* scale values */
3523178019Sjkim	if (sc->mode.accelfactor >= 1) {
3524178019Sjkim		if (x != 0) {
3525178019Sjkim			x = x * x / sc->mode.accelfactor;
3526178019Sjkim			if (x == 0)
3527178019Sjkim				x = 1;
3528178019Sjkim			if (c & MOUSE_PS2_XNEG)
3529178019Sjkim				x = -x;
3530178019Sjkim		}
3531178019Sjkim		if (y != 0) {
3532178019Sjkim			y = y * y / sc->mode.accelfactor;
3533178019Sjkim			if (y == 0)
3534178019Sjkim				y = 1;
3535178019Sjkim			if (c & MOUSE_PS2_YNEG)
3536178019Sjkim				y = -y;
3537178019Sjkim		}
353841016Sdfr	}
353941016Sdfr
3540178019Sjkim	ms.dx = x;
3541178019Sjkim	ms.dy = y;
3542178019Sjkim	ms.dz = z;
3543178019Sjkim	ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
3544178019Sjkim	    (ms.obutton ^ ms.button);
354541016Sdfr
3546178017Sjkim	pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
354741016Sdfr
3548178019Sjkim	sc->status.flags |= ms.flags;
3549178019Sjkim	sc->status.dx += ms.dx;
3550178019Sjkim	sc->status.dy += ms.dy;
3551178019Sjkim	sc->status.dz += ms.dz;
3552178019Sjkim	sc->status.button = ms.button;
3553178019Sjkim	sc->button = ms.button;
355441016Sdfr
3555178019Sjkimnext_native:
355658230Syokota	sc->watchdog = FALSE;
355758230Syokota
3558178019Sjkim	/* queue data */
3559178019Sjkim	if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
3560178019Sjkim		l = imin(pb->inputbytes,
3561178019Sjkim		    sizeof(sc->queue.buf) - sc->queue.tail);
3562178019Sjkim		bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
3563178019Sjkim		if (pb->inputbytes > l)
3564178019Sjkim			bcopy(&pb->ipacket[l], &sc->queue.buf[0],
3565178019Sjkim			    pb->inputbytes - l);
3566178019Sjkim		sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
3567178019Sjkim		    sizeof(sc->queue.buf);
3568178019Sjkim		sc->queue.count += pb->inputbytes;
356941016Sdfr	}
3570178019Sjkim	pb->inputbytes = 0;
357141016Sdfr
3572178019Sjkimnext:
3573123442Salfred	if (++sc->pqueue_start >= PSM_PACKETQUEUE)
3574123442Salfred		sc->pqueue_start = 0;
3575178019Sjkim	} while (sc->pqueue_start != sc->pqueue_end);
3576178019Sjkim
3577178019Sjkim	if (sc->state & PSM_ASLP) {
3578178019Sjkim		sc->state &= ~PSM_ASLP;
3579178019Sjkim		wakeup(sc);
3580178019Sjkim	}
3581178019Sjkim	selwakeuppri(&sc->rsel, PZERO);
3582189870Srnoland	if (sc->async != NULL) {
3583189870Srnoland		pgsigio(&sc->async, SIGIO, 0);
3584189870Srnoland	}
3585178019Sjkim	sc->state &= ~PSM_SOFTARMED;
3586178019Sjkim	splx(s);
358741016Sdfr}
358841016Sdfr
358941016Sdfrstatic int
3590130585Sphkpsmpoll(struct cdev *dev, int events, struct thread *td)
359141016Sdfr{
3592212355Sed	struct psm_softc *sc = dev->si_drv1;
3593178019Sjkim	int s;
3594178019Sjkim	int revents = 0;
359541016Sdfr
3596178019Sjkim	/* Return true if a mouse event available */
3597178019Sjkim	s = spltty();
3598178019Sjkim	if (events & (POLLIN | POLLRDNORM)) {
3599178019Sjkim		if (sc->queue.count > 0)
3600178019Sjkim			revents |= events & (POLLIN | POLLRDNORM);
3601178019Sjkim		else
3602178019Sjkim			selrecord(td, &sc->rsel);
3603178019Sjkim	}
3604178019Sjkim	splx(s);
360541016Sdfr
3606178019Sjkim	return (revents);
360741016Sdfr}
360841016Sdfr
360941016Sdfr/* vendor/model specific routines */
361041016Sdfr
361141016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
361241016Sdfr{
3613178019Sjkim	if (set_mouse_resolution(kbdc, res) != res)
3614178019Sjkim		return (FALSE);
3615178019Sjkim	if (set_mouse_scaling(kbdc, scale) &&
3616178019Sjkim	    set_mouse_scaling(kbdc, scale) &&
3617178019Sjkim	    set_mouse_scaling(kbdc, scale) &&
3618178019Sjkim	    (get_mouse_status(kbdc, status, 0, 3) >= 3))
3619178019Sjkim		return (TRUE);
3620178019Sjkim	return (FALSE);
362141016Sdfr}
362241016Sdfr
3623178019Sjkimstatic int
362469438Syokotamouse_ext_command(KBDC kbdc, int command)
362569438Syokota{
3626178019Sjkim	int c;
362769438Syokota
3628178019Sjkim	c = (command >> 6) & 0x03;
3629178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
3630178019Sjkim		return (FALSE);
3631178019Sjkim	c = (command >> 4) & 0x03;
3632178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
3633178019Sjkim		return (FALSE);
3634178019Sjkim	c = (command >> 2) & 0x03;
3635178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
3636178019Sjkim		return (FALSE);
3637178019Sjkim	c = (command >> 0) & 0x03;
3638178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
3639178019Sjkim		return (FALSE);
3640178019Sjkim	return (TRUE);
364169438Syokota}
364269438Syokota
3643153072Sru#ifdef notyet
364441016Sdfr/* Logitech MouseMan Cordless II */
364541016Sdfrstatic int
3646233580Sjkimenable_lcordless(KDBC kbdc, struct psm_softc *sc)
364741016Sdfr{
3648178019Sjkim	int status[3];
3649178019Sjkim	int ch;
365041016Sdfr
3651233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status))
3652178019Sjkim		return (FALSE);
3653178019Sjkim	if (status[1] == PSMD_RES_HIGH)
3654178019Sjkim		return (FALSE);
3655178019Sjkim	ch = (status[0] & 0x07) - 1;	/* channel # */
3656178019Sjkim	if ((ch <= 0) || (ch > 4))
3657178019Sjkim		return (FALSE);
3658178019Sjkim	/*
3659178019Sjkim	 * status[1]: always one?
3660178019Sjkim	 * status[2]: battery status? (0-100)
3661178019Sjkim	 */
3662178019Sjkim	return (TRUE);
366341016Sdfr}
366441016Sdfr#endif /* notyet */
366541016Sdfr
366658230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
366741016Sdfrstatic int
3668233580Sjkimenable_groller(KBDC kbdc, struct psm_softc *sc)
366941016Sdfr{
3670178019Sjkim	int status[3];
367141016Sdfr
3672178019Sjkim	/*
3673178019Sjkim	 * The special sequence to enable the fourth button and the
3674178019Sjkim	 * roller. Immediately after this sequence check status bytes.
3675178019Sjkim	 * if the mouse is NetScroll, the second and the third bytes are
3676178019Sjkim	 * '3' and 'D'.
3677178019Sjkim	 */
367841016Sdfr
3679178019Sjkim	/*
3680178019Sjkim	 * If the mouse is an ordinary PS/2 mouse, the status bytes should
3681178019Sjkim	 * look like the following.
3682178019Sjkim	 *
3683178019Sjkim	 * byte 1 bit 7 always 0
3684178019Sjkim	 *        bit 6 stream mode (0)
3685178019Sjkim	 *        bit 5 disabled (0)
3686178019Sjkim	 *        bit 4 1:1 scaling (0)
3687178019Sjkim	 *        bit 3 always 0
3688178019Sjkim	 *        bit 0-2 button status
3689178019Sjkim	 * byte 2 resolution (PSMD_RES_HIGH)
3690178019Sjkim	 * byte 3 report rate (?)
3691178019Sjkim	 */
369241016Sdfr
3693233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
3694178019Sjkim		return (FALSE);
3695178019Sjkim	if ((status[1] != '3') || (status[2] != 'D'))
3696178019Sjkim		return (FALSE);
3697178019Sjkim	/* FIXME: SmartScroll Mouse has 5 buttons! XXX */
3698233580Sjkim	if (sc != NULL)
3699233580Sjkim		sc->hw.buttons = 4;
3700178019Sjkim	return (TRUE);
370141016Sdfr}
370241016Sdfr
370358230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
370441016Sdfrstatic int
3705233580Sjkimenable_gmouse(KBDC kbdc, struct psm_softc *sc)
370641016Sdfr{
3707178019Sjkim	int status[3];
370841016Sdfr
3709178019Sjkim	/*
3710178019Sjkim	 * The special sequence to enable the middle, "rubber" button.
3711178019Sjkim	 * Immediately after this sequence check status bytes.
3712178019Sjkim	 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
3713178019Sjkim	 * the second and the third bytes are '3' and 'U'.
3714178019Sjkim	 * NOTE: NetMouse reports that it has three buttons although it has
3715178019Sjkim	 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
3716178019Sjkim	 * say they have three buttons too and they do have a button on the
3717178019Sjkim	 * side...
3718178019Sjkim	 */
3719233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
3720178019Sjkim		return (FALSE);
3721178019Sjkim	if ((status[1] != '3') || (status[2] != 'U'))
3722178019Sjkim		return (FALSE);
3723178019Sjkim	return (TRUE);
372441016Sdfr}
372541016Sdfr
372641016Sdfr/* ALPS GlidePoint */
372741016Sdfrstatic int
3728233580Sjkimenable_aglide(KBDC kbdc, struct psm_softc *sc)
372941016Sdfr{
3730178019Sjkim	int status[3];
373141016Sdfr
3732178019Sjkim	/*
3733178019Sjkim	 * The special sequence to obtain ALPS GlidePoint specific
3734178019Sjkim	 * information. Immediately after this sequence, status bytes will
3735178019Sjkim	 * contain something interesting.
3736178019Sjkim	 * NOTE: ALPS produces several models of GlidePoint. Some of those
3737178019Sjkim	 * do not respond to this sequence, thus, cannot be detected this way.
3738178019Sjkim	 */
3739233580Sjkim	if (set_mouse_sampling_rate(kbdc, 100) != 100)
3740178019Sjkim		return (FALSE);
3741233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
3742178019Sjkim		return (FALSE);
3743178019Sjkim	if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
3744178019Sjkim		return (FALSE);
3745178019Sjkim	return (TRUE);
374641016Sdfr}
374741016Sdfr
374841016Sdfr/* Kensington ThinkingMouse/Trackball */
374941016Sdfrstatic int
3750233580Sjkimenable_kmouse(KBDC kbdc, struct psm_softc *sc)
375141016Sdfr{
3752178019Sjkim	static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
3753178019Sjkim	int status[3];
3754178019Sjkim	int id1;
3755178019Sjkim	int id2;
3756178019Sjkim	int i;
375741016Sdfr
3758178019Sjkim	id1 = get_aux_id(kbdc);
3759178019Sjkim	if (set_mouse_sampling_rate(kbdc, 10) != 10)
3760178019Sjkim		return (FALSE);
3761178019Sjkim	/*
3762178019Sjkim	 * The device is now in the native mode? It returns a different
3763178019Sjkim	 * ID value...
3764178019Sjkim	 */
3765178019Sjkim	id2 = get_aux_id(kbdc);
3766178019Sjkim	if ((id1 == id2) || (id2 != 2))
3767178019Sjkim		return (FALSE);
376841016Sdfr
3769178019Sjkim	if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
3770178019Sjkim		return (FALSE);
377141016Sdfr#if PSM_DEBUG >= 2
3772178019Sjkim	/* at this point, resolution is LOW, sampling rate is 10/sec */
3773178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) < 3)
3774178019Sjkim		return (FALSE);
377541016Sdfr#endif
377641016Sdfr
3777178019Sjkim	/*
3778178019Sjkim	 * The special sequence to enable the third and fourth buttons.
3779178019Sjkim	 * Otherwise they behave like the first and second buttons.
3780178019Sjkim	 */
3781178019Sjkim	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3782178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3783178019Sjkim			return (FALSE);
378441016Sdfr
3785178019Sjkim	/*
3786178019Sjkim	 * At this point, the device is using default resolution and
3787178019Sjkim	 * sampling rate for the native mode.
3788178019Sjkim	 */
3789178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) < 3)
3790178019Sjkim		return (FALSE);
3791178019Sjkim	if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
3792178019Sjkim		return (FALSE);
379341016Sdfr
3794178019Sjkim	/* the device appears be enabled by this sequence, diable it for now */
3795178019Sjkim	disable_aux_dev(kbdc);
3796178019Sjkim	empty_aux_buffer(kbdc, 5);
379741016Sdfr
3798178019Sjkim	return (TRUE);
379941016Sdfr}
380041016Sdfr
380158230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
380241016Sdfrstatic int
3803233580Sjkimenable_mmanplus(KBDC kbdc, struct psm_softc *sc)
380441016Sdfr{
3805178019Sjkim	int data[3];
380641016Sdfr
3807178019Sjkim	/* the special sequence to enable the fourth button and the roller. */
3808178019Sjkim	/*
3809178019Sjkim	 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
3810178019Sjkim	 * must be called exactly three times since the last RESET command
3811178019Sjkim	 * before this sequence. XXX
3812178019Sjkim	 */
3813178019Sjkim	if (!set_mouse_scaling(kbdc, 1))
3814178019Sjkim		return (FALSE);
3815178019Sjkim	if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
3816178019Sjkim		return (FALSE);
3817178019Sjkim	if (get_mouse_status(kbdc, data, 1, 3) < 3)
3818178019Sjkim		return (FALSE);
381941016Sdfr
3820178019Sjkim	/*
3821240743Skevlo	 * PS2++ protocol, packet type 0
3822178019Sjkim	 *
3823178019Sjkim	 *          b7 b6 b5 b4 b3 b2 b1 b0
3824178019Sjkim	 * byte 1:  *  1  p3 p2 1  *  *  *
3825178019Sjkim	 * byte 2:  1  1  p1 p0 m1 m0 1  0
3826178019Sjkim	 * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
3827178019Sjkim	 *
3828178019Sjkim	 * p3-p0: packet type: 0
3829178019Sjkim	 * m7-m0: model ID: MouseMan+:0x50,
3830178019Sjkim	 *		    FirstMouse+:0x51,
3831178019Sjkim	 *		    ScrollPoint:0x58...
3832178019Sjkim	 */
3833178019Sjkim	/* check constant bits */
3834178019Sjkim	if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
3835178019Sjkim		return (FALSE);
3836178019Sjkim	if ((data[1] & 0xc3) != 0xc2)
3837178019Sjkim		return (FALSE);
3838178019Sjkim	/* check d3-d0 in byte 2 */
3839178019Sjkim	if (!MOUSE_PS2PLUS_CHECKBITS(data))
3840178019Sjkim		return (FALSE);
3841178019Sjkim	/* check p3-p0 */
3842178019Sjkim	if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
3843178019Sjkim		return (FALSE);
384441016Sdfr
3845233580Sjkim	if (sc != NULL) {
3846233580Sjkim		sc->hw.hwid &= 0x00ff;
3847233580Sjkim		sc->hw.hwid |= data[2] << 8;	/* save model ID */
3848233580Sjkim	}
384948778Syokota
3850178019Sjkim	/*
3851178019Sjkim	 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
3852178019Sjkim	 * the wheel and the fourth button events are encoded in the
3853178019Sjkim	 * special data packet. The mouse may be put in the IntelliMouse mode
3854178019Sjkim	 * if it is initialized by the IntelliMouse's method.
3855178019Sjkim	 */
3856178019Sjkim	return (TRUE);
385741016Sdfr}
385841016Sdfr
385958230Syokota/* MS IntelliMouse Explorer */
386058230Syokotastatic int
3861233580Sjkimenable_msexplorer(KBDC kbdc, struct psm_softc *sc)
386258230Syokota{
3863178019Sjkim	static u_char rate0[] = { 200, 100, 80, };
3864178019Sjkim	static u_char rate1[] = { 200, 200, 80, };
3865178019Sjkim	int id;
3866178019Sjkim	int i;
386758230Syokota
3868178019Sjkim	/*
3869178019Sjkim	 * This is needed for at least A4Tech X-7xx mice - they do not go
3870178019Sjkim	 * straight to Explorer mode, but need to be set to Intelli mode
3871178019Sjkim	 * first.
3872178019Sjkim	 */
3873233580Sjkim	enable_msintelli(kbdc, sc);
3874176554Srink
3875178019Sjkim	/* the special sequence to enable the extra buttons and the roller. */
3876178019Sjkim	for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i)
3877178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
3878178019Sjkim			return (FALSE);
3879178019Sjkim	/* the device will give the genuine ID only after the above sequence */
3880178019Sjkim	id = get_aux_id(kbdc);
3881178019Sjkim	if (id != PSM_EXPLORER_ID)
3882178019Sjkim		return (FALSE);
388369438Syokota
3884233580Sjkim	if (sc != NULL) {
3885233580Sjkim		sc->hw.buttons = 5;	/* IntelliMouse Explorer XXX */
3886233580Sjkim		sc->hw.hwid = id;
3887233580Sjkim	}
388869438Syokota
3889178019Sjkim	/*
3890178019Sjkim	 * XXX: this is a kludge to fool some KVM switch products
3891178019Sjkim	 * which think they are clever enough to know the 4-byte IntelliMouse
3892178019Sjkim	 * protocol, and assume any other protocols use 3-byte packets.
3893178019Sjkim	 * They don't convey 4-byte data packets from the IntelliMouse Explorer
3894178019Sjkim	 * correctly to the host computer because of this!
3895178019Sjkim	 * The following sequence is actually IntelliMouse's "wake up"
3896178019Sjkim	 * sequence; it will make the KVM think the mouse is IntelliMouse
3897178019Sjkim	 * when it is in fact IntelliMouse Explorer.
3898178019Sjkim	 */
3899178019Sjkim	for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i)
3900178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
3901178019Sjkim			break;
3902233580Sjkim	get_aux_id(kbdc);
390358923Syokota
3904178019Sjkim	return (TRUE);
390558230Syokota}
390658230Syokota
390741016Sdfr/* MS IntelliMouse */
390841016Sdfrstatic int
3909233580Sjkimenable_msintelli(KBDC kbdc, struct psm_softc *sc)
391041016Sdfr{
3911178019Sjkim	/*
3912178019Sjkim	 * Logitech MouseMan+ and FirstMouse+ will also respond to this
3913178019Sjkim	 * probe routine and act like IntelliMouse.
3914178019Sjkim	 */
391541016Sdfr
3916178019Sjkim	static u_char rate[] = { 200, 100, 80, };
3917178019Sjkim	int id;
3918178019Sjkim	int i;
391941016Sdfr
3920178019Sjkim	/* the special sequence to enable the third button and the roller. */
3921178019Sjkim	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3922178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3923178019Sjkim			return (FALSE);
3924178019Sjkim	/* the device will give the genuine ID only after the above sequence */
3925178019Sjkim	id = get_aux_id(kbdc);
3926178019Sjkim	if (id != PSM_INTELLI_ID)
3927178019Sjkim		return (FALSE);
392841016Sdfr
3929233580Sjkim	if (sc != NULL) {
3930233580Sjkim		sc->hw.buttons = 3;
3931233580Sjkim		sc->hw.hwid = id;
3932233580Sjkim	}
393341016Sdfr
3934178019Sjkim	return (TRUE);
393541016Sdfr}
393641016Sdfr
393758230Syokota/* A4 Tech 4D Mouse */
393858230Syokotastatic int
3939233580Sjkimenable_4dmouse(KBDC kbdc, struct psm_softc *sc)
394058230Syokota{
3941178019Sjkim	/*
3942178019Sjkim	 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
3943178019Sjkim	 */
394458230Syokota
3945178019Sjkim	static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
3946178019Sjkim	int id;
3947178019Sjkim	int i;
394858230Syokota
3949178019Sjkim	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3950178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3951178019Sjkim			return (FALSE);
3952178019Sjkim	id = get_aux_id(kbdc);
3953178019Sjkim	/*
3954178019Sjkim	 * WinEasy 4D, 4 Way Scroll 4D: 6
3955178019Sjkim	 * Cable-Free 4D: 8 (4DPLUS)
3956178019Sjkim	 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
3957178019Sjkim	 */
3958178019Sjkim	if (id != PSM_4DMOUSE_ID)
3959178019Sjkim		return (FALSE);
396058230Syokota
3961233580Sjkim	if (sc != NULL) {
3962233580Sjkim		sc->hw.buttons = 3;	/* XXX some 4D mice have 4? */
3963233580Sjkim		sc->hw.hwid = id;
3964233580Sjkim	}
396558230Syokota
3966178019Sjkim	return (TRUE);
396758230Syokota}
396858230Syokota
396958230Syokota/* A4 Tech 4D+ Mouse */
397058230Syokotastatic int
3971233580Sjkimenable_4dplus(KBDC kbdc, struct psm_softc *sc)
397258230Syokota{
3973178019Sjkim	/*
3974178019Sjkim	 * Newer wheel mice from A4 Tech seem to use this protocol.
3975178019Sjkim	 * Older models are recognized as either 4D Mouse or IntelliMouse.
3976178019Sjkim	 */
3977178019Sjkim	int id;
397858230Syokota
3979178019Sjkim	/*
3980178019Sjkim	 * enable_4dmouse() already issued the following ID sequence...
3981178019Sjkim	static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
3982178019Sjkim	int i;
398358230Syokota
3984178019Sjkim	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3985178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3986178019Sjkim			return (FALSE);
3987178019Sjkim	*/
398858230Syokota
3989178019Sjkim	id = get_aux_id(kbdc);
3990178019Sjkim	switch (id) {
3991178019Sjkim	case PSM_4DPLUS_ID:
3992178019Sjkim		break;
3993178019Sjkim	case PSM_4DPLUS_RFSW35_ID:
3994178019Sjkim		break;
3995178019Sjkim	default:
3996178019Sjkim		return (FALSE);
3997178019Sjkim	}
399858230Syokota
3999233580Sjkim	if (sc != NULL) {
4000233580Sjkim		sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
4001233580Sjkim		sc->hw.hwid = id;
4002233580Sjkim	}
400358230Syokota
4004178019Sjkim	return (TRUE);
400558230Syokota}
400658230Syokota
4007132865Snjl/* Synaptics Touchpad */
4008132865Snjlstatic int
4009183888Sdumbbellsynaptics_sysctl(SYSCTL_HANDLER_ARGS)
4010132865Snjl{
4011183888Sdumbbell	int error, arg;
4012132865Snjl
4013183888Sdumbbell	/* Read the current value. */
4014183888Sdumbbell	arg = *(int *)oidp->oid_arg1;
4015183888Sdumbbell	error = sysctl_handle_int(oidp, &arg, 0, req);
4016135945Sphilip
4017183888Sdumbbell	/* Sanity check. */
4018183888Sdumbbell	if (error || !req->newptr)
4019183888Sdumbbell		return (error);
4020183888Sdumbbell
4021183888Sdumbbell	/*
4022183888Sdumbbell	 * Check that the new value is in the concerned node's range
4023183888Sdumbbell	 * of values.
4024183888Sdumbbell	 */
4025183888Sdumbbell	switch (oidp->oid_arg2) {
4026183888Sdumbbell	case SYNAPTICS_SYSCTL_MIN_PRESSURE:
4027183888Sdumbbell	case SYNAPTICS_SYSCTL_MAX_PRESSURE:
4028183888Sdumbbell		if (arg < 0 || arg > 255)
4029183888Sdumbbell			return (EINVAL);
4030183888Sdumbbell		break;
4031183888Sdumbbell	case SYNAPTICS_SYSCTL_MAX_WIDTH:
4032183888Sdumbbell		if (arg < 4 || arg > 15)
4033183888Sdumbbell			return (EINVAL);
4034183888Sdumbbell		break;
4035183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_TOP:
4036183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
4037183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
4038183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_LEFT:
4039183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_TOP:
4040183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_RIGHT:
4041183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_BOTTOM:
4042183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_LEFT:
4043183888Sdumbbell		if (arg < 0 || arg > 6143)
4044183888Sdumbbell			return (EINVAL);
4045183888Sdumbbell		break;
4046183888Sdumbbell	case SYNAPTICS_SYSCTL_WINDOW_MIN:
4047183888Sdumbbell	case SYNAPTICS_SYSCTL_WINDOW_MAX:
4048183888Sdumbbell	case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
4049183888Sdumbbell		if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
4050183888Sdumbbell			return (EINVAL);
4051183888Sdumbbell		break;
4052183888Sdumbbell	case SYNAPTICS_SYSCTL_MULTIPLICATOR:
4053183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
4054183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
4055183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
4056183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
4057183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_MIN:
4058183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_MAX:
4059183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_MAX_NA:
4060183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_LEN:
4061183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
4062183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
4063183888Sdumbbell		if (arg < 1)
4064183888Sdumbbell			return (EINVAL);
4065183888Sdumbbell		break;
4066183888Sdumbbell	case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
4067183888Sdumbbell	case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
4068183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
4069183888Sdumbbell		if (arg < 0)
4070183888Sdumbbell			return (EINVAL);
4071183888Sdumbbell		break;
4072183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
4073183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
4074183888Sdumbbell		if (arg < -6143 || arg > 6143)
4075183888Sdumbbell			return (EINVAL);
4076183888Sdumbbell		break;
4077183888Sdumbbell	default:
4078183888Sdumbbell		return (EINVAL);
4079183888Sdumbbell	}
4080183888Sdumbbell
4081183888Sdumbbell	/* Update. */
4082183888Sdumbbell	*(int *)oidp->oid_arg1 = arg;
4083183888Sdumbbell
4084183888Sdumbbell	return (error);
4085183888Sdumbbell}
4086183888Sdumbbell
4087183888Sdumbbellstatic void
4088183888Sdumbbellsynaptics_sysctl_create_tree(struct psm_softc *sc)
4089183888Sdumbbell{
4090183888Sdumbbell
4091186218Sdumbbell	if (sc->syninfo.sysctl_tree != NULL)
4092186218Sdumbbell		return;
4093186218Sdumbbell
4094178019Sjkim	/* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
4095178019Sjkim	sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
4096178019Sjkim	sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
4097178019Sjkim	    SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "synaptics", CTLFLAG_RD,
4098178019Sjkim	    0, "Synaptics TouchPad");
4099139982Sphilip
4100183888Sdumbbell	/* hw.psm.synaptics.directional_scrolls. */
4101178019Sjkim	sc->syninfo.directional_scrolls = 1;
4102178019Sjkim	SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
4103178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4104183888Sdumbbell	    "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
4105178019Sjkim	    &sc->syninfo.directional_scrolls, 0,
4106183888Sdumbbell	    "Enable hardware scrolling pad (if non-zero) or register it as "
4107183888Sdumbbell	    "a middle-click (if 0)");
4108139982Sphilip
4109183888Sdumbbell	/* hw.psm.synaptics.min_pressure. */
4110183888Sdumbbell	sc->syninfo.min_pressure = 16;
4111183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4112178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4113183888Sdumbbell	    "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4114183888Sdumbbell	    &sc->syninfo.min_pressure, SYNAPTICS_SYSCTL_MIN_PRESSURE,
4115183888Sdumbbell	    synaptics_sysctl, "I",
4116183888Sdumbbell	    "Minimum pressure required to start an action");
4117139982Sphilip
4118183888Sdumbbell	/* hw.psm.synaptics.max_pressure. */
4119183888Sdumbbell	sc->syninfo.max_pressure = 220;
4120183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4121178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4122183888Sdumbbell	    "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4123183888Sdumbbell	    &sc->syninfo.max_pressure, SYNAPTICS_SYSCTL_MAX_PRESSURE,
4124183888Sdumbbell	    synaptics_sysctl, "I",
4125183888Sdumbbell	    "Maximum pressure to detect palm");
4126139982Sphilip
4127183888Sdumbbell	/* hw.psm.synaptics.max_width. */
4128183888Sdumbbell	sc->syninfo.max_width = 10;
4129183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4130178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4131183888Sdumbbell	    "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4132183888Sdumbbell	    &sc->syninfo.max_width, SYNAPTICS_SYSCTL_MAX_WIDTH,
4133183888Sdumbbell	    synaptics_sysctl, "I",
4134183888Sdumbbell	    "Maximum finger width to detect palm");
4135132865Snjl
4136183888Sdumbbell	/* hw.psm.synaptics.top_margin. */
4137183888Sdumbbell	sc->syninfo.margin_top = 200;
4138183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4139183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4140183888Sdumbbell	    "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4141183888Sdumbbell	    &sc->syninfo.margin_top, SYNAPTICS_SYSCTL_MARGIN_TOP,
4142183888Sdumbbell	    synaptics_sysctl, "I",
4143183888Sdumbbell	    "Top margin");
4144183888Sdumbbell
4145183888Sdumbbell	/* hw.psm.synaptics.right_margin. */
4146183888Sdumbbell	sc->syninfo.margin_right = 200;
4147183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4148183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4149183888Sdumbbell	    "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4150183888Sdumbbell	    &sc->syninfo.margin_right, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
4151183888Sdumbbell	    synaptics_sysctl, "I",
4152183888Sdumbbell	    "Right margin");
4153183888Sdumbbell
4154183888Sdumbbell	/* hw.psm.synaptics.bottom_margin. */
4155183888Sdumbbell	sc->syninfo.margin_bottom = 200;
4156183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4157183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4158183888Sdumbbell	    "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4159183888Sdumbbell	    &sc->syninfo.margin_bottom, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
4160183888Sdumbbell	    synaptics_sysctl, "I",
4161183888Sdumbbell	    "Bottom margin");
4162183888Sdumbbell
4163183888Sdumbbell	/* hw.psm.synaptics.left_margin. */
4164183888Sdumbbell	sc->syninfo.margin_left = 200;
4165183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4166183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4167183888Sdumbbell	    "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4168183888Sdumbbell	    &sc->syninfo.margin_left, SYNAPTICS_SYSCTL_MARGIN_LEFT,
4169183888Sdumbbell	    synaptics_sysctl, "I",
4170183888Sdumbbell	    "Left margin");
4171183888Sdumbbell
4172183888Sdumbbell	/* hw.psm.synaptics.na_top. */
4173183888Sdumbbell	sc->syninfo.na_top = 1783;
4174183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4175183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4176183888Sdumbbell	    "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4177183888Sdumbbell	    &sc->syninfo.na_top, SYNAPTICS_SYSCTL_NA_TOP,
4178183888Sdumbbell	    synaptics_sysctl, "I",
4179183888Sdumbbell	    "Top noisy area, where weight_previous_na is used instead "
4180183888Sdumbbell	    "of weight_previous");
4181183888Sdumbbell
4182183888Sdumbbell	/* hw.psm.synaptics.na_right. */
4183183888Sdumbbell	sc->syninfo.na_right = 563;
4184183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4185183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4186183888Sdumbbell	    "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4187183888Sdumbbell	    &sc->syninfo.na_right, SYNAPTICS_SYSCTL_NA_RIGHT,
4188183888Sdumbbell	    synaptics_sysctl, "I",
4189183888Sdumbbell	    "Right noisy area, where weight_previous_na is used instead "
4190183888Sdumbbell	    "of weight_previous");
4191183888Sdumbbell
4192183888Sdumbbell	/* hw.psm.synaptics.na_bottom. */
4193183888Sdumbbell	sc->syninfo.na_bottom = 1408;
4194183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4195183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4196183888Sdumbbell	    "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4197183888Sdumbbell	    &sc->syninfo.na_bottom, SYNAPTICS_SYSCTL_NA_BOTTOM,
4198183888Sdumbbell	    synaptics_sysctl, "I",
4199183888Sdumbbell	    "Bottom noisy area, where weight_previous_na is used instead "
4200183888Sdumbbell	    "of weight_previous");
4201183888Sdumbbell
4202183888Sdumbbell	/* hw.psm.synaptics.na_left. */
4203183888Sdumbbell	sc->syninfo.na_left = 1600;
4204183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4205183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4206183888Sdumbbell	    "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4207183888Sdumbbell	    &sc->syninfo.na_left, SYNAPTICS_SYSCTL_NA_LEFT,
4208183888Sdumbbell	    synaptics_sysctl, "I",
4209183888Sdumbbell	    "Left noisy area, where weight_previous_na is used instead "
4210183888Sdumbbell	    "of weight_previous");
4211183888Sdumbbell
4212183888Sdumbbell	/* hw.psm.synaptics.window_min. */
4213183888Sdumbbell	sc->syninfo.window_min = 4;
4214183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4215183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4216183888Sdumbbell	    "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4217183888Sdumbbell	    &sc->syninfo.window_min, SYNAPTICS_SYSCTL_WINDOW_MIN,
4218183888Sdumbbell	    synaptics_sysctl, "I",
4219183888Sdumbbell	    "Minimum window size to start an action");
4220183888Sdumbbell
4221183888Sdumbbell	/* hw.psm.synaptics.window_max. */
4222183888Sdumbbell	sc->syninfo.window_max = 10;
4223183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4224183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4225183888Sdumbbell	    "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4226183888Sdumbbell	    &sc->syninfo.window_max, SYNAPTICS_SYSCTL_WINDOW_MAX,
4227183888Sdumbbell	    synaptics_sysctl, "I",
4228183888Sdumbbell	    "Maximum window size");
4229183888Sdumbbell
4230183888Sdumbbell	/* hw.psm.synaptics.multiplicator. */
4231183888Sdumbbell	sc->syninfo.multiplicator = 10000;
4232183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4233183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4234183888Sdumbbell	    "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4235183888Sdumbbell	    &sc->syninfo.multiplicator, SYNAPTICS_SYSCTL_MULTIPLICATOR,
4236183888Sdumbbell	    synaptics_sysctl, "I",
4237183888Sdumbbell	    "Multiplicator to increase precision in averages and divisions");
4238183888Sdumbbell
4239183888Sdumbbell	/* hw.psm.synaptics.weight_current. */
4240183888Sdumbbell	sc->syninfo.weight_current = 3;
4241183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4242183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4243183888Sdumbbell	    "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4244183888Sdumbbell	    &sc->syninfo.weight_current, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
4245183888Sdumbbell	    synaptics_sysctl, "I",
4246183888Sdumbbell	    "Weight of the current movement in the new average");
4247183888Sdumbbell
4248183888Sdumbbell	/* hw.psm.synaptics.weight_previous. */
4249183888Sdumbbell	sc->syninfo.weight_previous = 6;
4250183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4251183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4252183888Sdumbbell	    "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4253183888Sdumbbell	    &sc->syninfo.weight_previous, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
4254183888Sdumbbell	    synaptics_sysctl, "I",
4255183888Sdumbbell	    "Weight of the previous average");
4256183888Sdumbbell
4257183888Sdumbbell	/* hw.psm.synaptics.weight_previous_na. */
4258183888Sdumbbell	sc->syninfo.weight_previous_na = 20;
4259183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4260183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4261183888Sdumbbell	    "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4262183888Sdumbbell	    &sc->syninfo.weight_previous_na,
4263183888Sdumbbell	    SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
4264183888Sdumbbell	    synaptics_sysctl, "I",
4265183888Sdumbbell	    "Weight of the previous average (inside the noisy area)");
4266183888Sdumbbell
4267183888Sdumbbell	/* hw.psm.synaptics.weight_len_squared. */
4268183888Sdumbbell	sc->syninfo.weight_len_squared = 2000;
4269183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4270183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4271183888Sdumbbell	    "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4272183888Sdumbbell	    &sc->syninfo.weight_len_squared,
4273183888Sdumbbell	    SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
4274183888Sdumbbell	    synaptics_sysctl, "I",
4275183888Sdumbbell	    "Length (squared) of segments where weight_previous "
4276183888Sdumbbell	    "starts to decrease");
4277183888Sdumbbell
4278183888Sdumbbell	/* hw.psm.synaptics.div_min. */
4279183888Sdumbbell	sc->syninfo.div_min = 9;
4280183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4281183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4282183888Sdumbbell	    "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4283183888Sdumbbell	    &sc->syninfo.div_min, SYNAPTICS_SYSCTL_DIV_MIN,
4284183888Sdumbbell	    synaptics_sysctl, "I",
4285183888Sdumbbell	    "Divisor for fast movements");
4286183888Sdumbbell
4287183888Sdumbbell	/* hw.psm.synaptics.div_max. */
4288183888Sdumbbell	sc->syninfo.div_max = 17;
4289183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4290183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4291183888Sdumbbell	    "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4292183888Sdumbbell	    &sc->syninfo.div_max, SYNAPTICS_SYSCTL_DIV_MAX,
4293183888Sdumbbell	    synaptics_sysctl, "I",
4294183888Sdumbbell	    "Divisor for slow movements");
4295183888Sdumbbell
4296183888Sdumbbell	/* hw.psm.synaptics.div_max_na. */
4297183888Sdumbbell	sc->syninfo.div_max_na = 30;
4298183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4299183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4300183888Sdumbbell	    "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4301183888Sdumbbell	    &sc->syninfo.div_max_na, SYNAPTICS_SYSCTL_DIV_MAX_NA,
4302183888Sdumbbell	    synaptics_sysctl, "I",
4303183888Sdumbbell	    "Divisor with slow movements (inside the noisy area)");
4304183888Sdumbbell
4305183888Sdumbbell	/* hw.psm.synaptics.div_len. */
4306183888Sdumbbell	sc->syninfo.div_len = 100;
4307183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4308183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4309183888Sdumbbell	    "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4310183888Sdumbbell	    &sc->syninfo.div_len, SYNAPTICS_SYSCTL_DIV_LEN,
4311183888Sdumbbell	    synaptics_sysctl, "I",
4312183888Sdumbbell	    "Length of segments where div_max starts to decrease");
4313183888Sdumbbell
4314183888Sdumbbell	/* hw.psm.synaptics.tap_max_delta. */
4315183888Sdumbbell	sc->syninfo.tap_max_delta = 80;
4316183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4317183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4318183888Sdumbbell	    "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4319183888Sdumbbell	    &sc->syninfo.tap_max_delta, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
4320183888Sdumbbell	    synaptics_sysctl, "I",
4321183888Sdumbbell	    "Length of segments above which a tap is ignored");
4322183888Sdumbbell
4323183888Sdumbbell	/* hw.psm.synaptics.tap_min_queue. */
4324183888Sdumbbell	sc->syninfo.tap_min_queue = 2;
4325183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4326183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4327183888Sdumbbell	    "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4328183888Sdumbbell	    &sc->syninfo.tap_min_queue, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
4329183888Sdumbbell	    synaptics_sysctl, "I",
4330183888Sdumbbell	    "Number of packets required to consider a tap");
4331183888Sdumbbell
4332183888Sdumbbell	/* hw.psm.synaptics.taphold_timeout. */
4333183888Sdumbbell	sc->synaction.in_taphold = 0;
4334183888Sdumbbell	sc->syninfo.taphold_timeout = tap_timeout;
4335183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4336183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4337183888Sdumbbell	    "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4338183888Sdumbbell	    &sc->syninfo.taphold_timeout, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
4339183888Sdumbbell	    synaptics_sysctl, "I",
4340183888Sdumbbell	    "Maximum elapsed time between two taps to consider a tap-hold "
4341183888Sdumbbell	    "action");
4342183888Sdumbbell
4343183888Sdumbbell	/* hw.psm.synaptics.vscroll_hor_area. */
4344183888Sdumbbell	sc->syninfo.vscroll_hor_area = 0; /* 1300 */
4345183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4346183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4347183888Sdumbbell	    "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4348183888Sdumbbell	    &sc->syninfo.vscroll_hor_area, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
4349183888Sdumbbell	    synaptics_sysctl, "I",
4350183888Sdumbbell	    "Area reserved for horizontal virtual scrolling");
4351183888Sdumbbell
4352183888Sdumbbell	/* hw.psm.synaptics.vscroll_ver_area. */
4353183888Sdumbbell	sc->syninfo.vscroll_ver_area = -600;
4354183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4355183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4356183888Sdumbbell	    "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4357183888Sdumbbell	    &sc->syninfo.vscroll_ver_area, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
4358183888Sdumbbell	    synaptics_sysctl, "I",
4359183888Sdumbbell	    "Area reserved for vertical virtual scrolling");
4360183888Sdumbbell
4361183888Sdumbbell	/* hw.psm.synaptics.vscroll_min_delta. */
4362183888Sdumbbell	sc->syninfo.vscroll_min_delta = 50;
4363183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4364183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4365183888Sdumbbell	    "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4366183888Sdumbbell	    &sc->syninfo.vscroll_min_delta,
4367183888Sdumbbell	    SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
4368183888Sdumbbell	    synaptics_sysctl, "I",
4369183888Sdumbbell	    "Minimum movement to consider virtual scrolling");
4370183888Sdumbbell
4371183888Sdumbbell	/* hw.psm.synaptics.vscroll_div_min. */
4372183888Sdumbbell	sc->syninfo.vscroll_div_min = 100;
4373183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4374183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4375183888Sdumbbell	    "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4376183888Sdumbbell	    &sc->syninfo.vscroll_div_min, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
4377183888Sdumbbell	    synaptics_sysctl, "I",
4378183888Sdumbbell	    "Divisor for fast scrolling");
4379183888Sdumbbell
4380183888Sdumbbell	/* hw.psm.synaptics.vscroll_div_min. */
4381183888Sdumbbell	sc->syninfo.vscroll_div_max = 150;
4382183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4383183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4384183888Sdumbbell	    "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4385183888Sdumbbell	    &sc->syninfo.vscroll_div_max, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
4386183888Sdumbbell	    synaptics_sysctl, "I",
4387183888Sdumbbell	    "Divisor for slow scrolling");
4388183888Sdumbbell}
4389183888Sdumbbell
4390183888Sdumbbellstatic int
4391233580Sjkimenable_synaptics(KBDC kbdc, struct psm_softc *sc)
4392183888Sdumbbell{
4393233580Sjkim	synapticshw_t synhw;
4394183888Sdumbbell	int status[3];
4395233580Sjkim	int buttons;
4396183888Sdumbbell
4397183888Sdumbbell	VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
4398132865Snjl
4399186175Sdumbbell	/*
4400186175Sdumbbell	 * Just to be on the safe side: this avoids troubles with
4401186175Sdumbbell	 * following mouse_ext_command() when the previous command
4402186175Sdumbbell	 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
4403186175Sdumbbell	 * Synaptics Touchpad behaviour.
4404186175Sdumbbell	 */
4405178019Sjkim	set_mouse_scaling(kbdc, 1);
4406133295Sphilip
4407186175Sdumbbell	/* Identify the Touchpad version. */
4408178019Sjkim	if (mouse_ext_command(kbdc, 0) == 0)
4409178019Sjkim		return (FALSE);
4410178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4411178019Sjkim		return (FALSE);
4412178019Sjkim	if (status[1] != 0x47)
4413178019Sjkim		return (FALSE);
4414133295Sphilip
4415233580Sjkim	bzero(&synhw, sizeof(synhw));
4416233580Sjkim	synhw.infoMinor = status[0];
4417233580Sjkim	synhw.infoMajor = status[2] & 0x0f;
4418132865Snjl
4419178019Sjkim	if (verbose >= 2)
4420233580Sjkim		printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
4421233580Sjkim		    synhw.infoMinor);
4422132865Snjl
4423233580Sjkim	if (synhw.infoMajor < 4) {
4424178019Sjkim		printf("  Unsupported (pre-v4) Touchpad detected\n");
4425178019Sjkim		return (FALSE);
4426178019Sjkim	}
4427133295Sphilip
4428186175Sdumbbell	/* Get the Touchpad model information. */
4429178019Sjkim	if (mouse_ext_command(kbdc, 3) == 0)
4430178019Sjkim		return (FALSE);
4431178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4432178019Sjkim		return (FALSE);
4433178019Sjkim	if ((status[1] & 0x01) != 0) {
4434178019Sjkim		printf("  Failed to read model information\n");
4435178019Sjkim		return (FALSE);
4436178019Sjkim	}
4437132865Snjl
4438233580Sjkim	synhw.infoRot180   = (status[0] & 0x80) != 0;
4439233580Sjkim	synhw.infoPortrait = (status[0] & 0x40) != 0;
4440233580Sjkim	synhw.infoSensor   =  status[0] & 0x3f;
4441233580Sjkim	synhw.infoHardware = (status[1] & 0xfe) >> 1;
4442233580Sjkim	synhw.infoNewAbs   = (status[2] & 0x80) != 0;
4443233580Sjkim	synhw.capPen       = (status[2] & 0x40) != 0;
4444233580Sjkim	synhw.infoSimplC   = (status[2] & 0x20) != 0;
4445233580Sjkim	synhw.infoGeometry =  status[2] & 0x0f;
4446132865Snjl
4447133295Sphilip	if (verbose >= 2) {
4448178019Sjkim		printf("  Model information:\n");
4449233580Sjkim		printf("   infoRot180: %d\n", synhw.infoRot180);
4450233580Sjkim		printf("   infoPortrait: %d\n", synhw.infoPortrait);
4451233580Sjkim		printf("   infoSensor: %d\n", synhw.infoSensor);
4452233580Sjkim		printf("   infoHardware: %d\n", synhw.infoHardware);
4453233580Sjkim		printf("   infoNewAbs: %d\n", synhw.infoNewAbs);
4454233580Sjkim		printf("   capPen: %d\n", synhw.capPen);
4455233580Sjkim		printf("   infoSimplC: %d\n", synhw.infoSimplC);
4456233580Sjkim		printf("   infoGeometry: %d\n", synhw.infoGeometry);
4457133295Sphilip	}
4458139982Sphilip
4459186175Sdumbbell	/* Read the extended capability bits. */
4460178019Sjkim	if (mouse_ext_command(kbdc, 2) == 0)
4461178019Sjkim		return (FALSE);
4462178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4463178019Sjkim		return (FALSE);
4464244405Sdumbbell	if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
4465178019Sjkim		printf("  Failed to read extended capability bits\n");
4466178019Sjkim		return (FALSE);
4467178019Sjkim	}
4468178019Sjkim
4469186175Sdumbbell	/* Set the different capabilities when they exist. */
4470233580Sjkim	buttons = 0;
4471233580Sjkim	synhw.capExtended = (status[0] & 0x80) != 0;
4472233580Sjkim	if (synhw.capExtended) {
4473255153Sdumbbell		synhw.nExtendedQueries = (status[0] & 0x70) != 0;
4474255153Sdumbbell		synhw.capMiddle        = (status[0] & 0x04) != 0;
4475255153Sdumbbell		synhw.capPassthrough   = (status[2] & 0x80) != 0;
4476255153Sdumbbell		synhw.capSleep         = (status[2] & 0x10) != 0;
4477255153Sdumbbell		synhw.capFourButtons   = (status[2] & 0x08) != 0;
4478255153Sdumbbell		synhw.capMultiFinger   = (status[2] & 0x02) != 0;
4479255153Sdumbbell		synhw.capPalmDetect    = (status[2] & 0x01) != 0;
4480178019Sjkim
4481178019Sjkim		if (verbose >= 2) {
4482178019Sjkim			printf("  Extended capabilities:\n");
4483233580Sjkim			printf("   capExtended: %d\n", synhw.capExtended);
4484255153Sdumbbell			printf("   capMiddle: %d\n", synhw.capMiddle);
4485255153Sdumbbell			printf("   nExtendedQueries: %d\n",
4486255153Sdumbbell			    synhw.nExtendedQueries);
4487233580Sjkim			printf("   capPassthrough: %d\n", synhw.capPassthrough);
4488233580Sjkim			printf("   capSleep: %d\n", synhw.capSleep);
4489233580Sjkim			printf("   capFourButtons: %d\n", synhw.capFourButtons);
4490233580Sjkim			printf("   capMultiFinger: %d\n", synhw.capMultiFinger);
4491233580Sjkim			printf("   capPalmDetect: %d\n", synhw.capPalmDetect);
4492178019Sjkim		}
4493178019Sjkim
4494178019Sjkim		/*
4495255153Sdumbbell		 * If nExtendedQueries is 1 or greater, then the TouchPad
4496255153Sdumbbell		 * supports this number of extended queries. We can load
4497186175Sdumbbell		 * more information about buttons using query 0x09.
4498178019Sjkim		 */
4499255153Sdumbbell		if (synhw.capExtended && synhw.nExtendedQueries) {
4500178019Sjkim			if (mouse_ext_command(kbdc, 0x09) == 0)
4501178019Sjkim				return (FALSE);
4502178019Sjkim			if (get_mouse_status(kbdc, status, 0, 3) != 3)
4503178019Sjkim				return (FALSE);
4504255153Sdumbbell			synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
4505255153Sdumbbell			/*
4506255153Sdumbbell			 * Add the number of extended buttons to the total
4507255153Sdumbbell			 * button support count, including the middle button
4508255153Sdumbbell			 * if capMiddle support bit is set.
4509255153Sdumbbell			 */
4510255153Sdumbbell			buttons = synhw.nExtendedButtons + synhw.capMiddle;
4511233580Sjkim		} else
4512255153Sdumbbell			/*
4513255153Sdumbbell			 * If the capFourButtons support bit is set,
4514255153Sdumbbell			 * add a fourth button to the total button count.
4515255153Sdumbbell			 */
4516233580Sjkim			buttons = synhw.capFourButtons ? 1 : 0;
4517233580Sjkim	}
4518233580Sjkim	if (verbose >= 2) {
4519233580Sjkim		if (synhw.capExtended)
4520233580Sjkim			printf("  Additional Buttons: %d\n", buttons);
4521233580Sjkim		else
4522178019Sjkim			printf("  No extended capabilities\n");
4523178019Sjkim	}
4524178019Sjkim
4525139982Sphilip	/*
4526255153Sdumbbell	 * Add the default number of 3 buttons to the total
4527255153Sdumbbell	 * count of supported buttons reported above.
4528255153Sdumbbell	 */
4529255153Sdumbbell	buttons += 3;
4530255153Sdumbbell
4531255153Sdumbbell	/*
4532186175Sdumbbell	 * Read the mode byte.
4533178019Sjkim	 *
4534178019Sjkim	 * XXX: Note the Synaptics documentation also defines the first
4535178019Sjkim	 * byte of the response to this query to be a constant 0x3b, this
4536178019Sjkim	 * does not appear to be true for Touchpads with guest devices.
4537139982Sphilip	 */
4538178019Sjkim	if (mouse_ext_command(kbdc, 1) == 0)
4539139982Sphilip		return (FALSE);
4540178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4541139982Sphilip		return (FALSE);
4542244405Sdumbbell	if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
4543178019Sjkim		printf("  Failed to read mode byte\n");
4544178019Sjkim		return (FALSE);
4545139982Sphilip	}
4546139982Sphilip
4547233580Sjkim	if (sc != NULL)
4548233580Sjkim		sc->synhw = synhw;
4549233580Sjkim	if (!synaptics_support)
4550233580Sjkim		return (FALSE);
4551233580Sjkim
4552186175Sdumbbell	/* Set the mode byte; request wmode where available. */
4553233580Sjkim	mouse_ext_command(kbdc, synhw.capExtended ? 0xc1 : 0xc0);
4554133295Sphilip
4555186175Sdumbbell	/* "Commit" the Set Mode Byte command sent above. */
4556178019Sjkim	set_mouse_sampling_rate(kbdc, 20);
4557132865Snjl
4558233580Sjkim	VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
4559132865Snjl
4560233580Sjkim	if (sc != NULL) {
4561233580Sjkim		/* Create sysctl tree. */
4562233580Sjkim		synaptics_sysctl_create_tree(sc);
4563183888Sdumbbell
4564233580Sjkim		sc->hw.buttons = buttons;
4565233580Sjkim	}
4566183888Sdumbbell
4567178019Sjkim	return (TRUE);
4568132865Snjl}
4569133295Sphilip
4570248478Sjkim/* IBM/Lenovo TrackPoint */
4571248478Sjkimstatic int
4572248478Sjkimtrackpoint_command(KBDC kbdc, int cmd, int loc, int val)
4573248478Sjkim{
4574248478Sjkim	const int seq[] = { 0xe2, cmd, loc, val };
4575248478Sjkim	int i;
4576248478Sjkim
4577248478Sjkim	for (i = 0; i < nitems(seq); i++)
4578248478Sjkim		if (send_aux_command(kbdc, seq[i]) != PSM_ACK)
4579248478Sjkim			return (EIO);
4580248478Sjkim	return (0);
4581248478Sjkim}
4582248478Sjkim
4583248478Sjkim#define	PSM_TPINFO(x)	offsetof(struct psm_softc, tpinfo.x)
4584248478Sjkim#define	TPMASK		0
4585248478Sjkim#define	TPLOC		1
4586248478Sjkim#define	TPINFO		2
4587248478Sjkim
4588248478Sjkimstatic int
4589248478Sjkimtrackpoint_sysctl(SYSCTL_HANDLER_ARGS)
4590248478Sjkim{
4591248478Sjkim	static const int data[][3] = {
4592248478Sjkim		{ 0x00, 0x4a, PSM_TPINFO(sensitivity) },
4593248478Sjkim		{ 0x00, 0x4d, PSM_TPINFO(inertia) },
4594248478Sjkim		{ 0x00, 0x60, PSM_TPINFO(uplateau) },
4595248478Sjkim		{ 0x00, 0x57, PSM_TPINFO(reach) },
4596248478Sjkim		{ 0x00, 0x58, PSM_TPINFO(draghys) },
4597248478Sjkim		{ 0x00, 0x59, PSM_TPINFO(mindrag) },
4598248478Sjkim		{ 0x00, 0x5a, PSM_TPINFO(upthresh) },
4599248478Sjkim		{ 0x00, 0x5c, PSM_TPINFO(threshold) },
4600248478Sjkim		{ 0x00, 0x5d, PSM_TPINFO(jenks) },
4601248478Sjkim		{ 0x00, 0x5e, PSM_TPINFO(ztime) },
4602248478Sjkim		{ 0x01, 0x2c, PSM_TPINFO(pts) },
4603248478Sjkim		{ 0x08, 0x2d, PSM_TPINFO(skipback) }
4604248478Sjkim	};
4605248478Sjkim	struct psm_softc *sc;
4606248478Sjkim	int error, newval, *oldvalp;
4607248478Sjkim	const int *tp;
4608248478Sjkim
4609248478Sjkim	if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
4610248478Sjkim		return (EINVAL);
4611248478Sjkim	sc = arg1;
4612248478Sjkim	tp = data[arg2];
4613248478Sjkim	oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
4614248478Sjkim	newval = *oldvalp;
4615248478Sjkim	error = sysctl_handle_int(oidp, &newval, 0, req);
4616248478Sjkim	if (error != 0)
4617248478Sjkim		return (error);
4618248478Sjkim	if (newval == *oldvalp)
4619248478Sjkim		return (0);
4620248478Sjkim	if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
4621248478Sjkim		return (EINVAL);
4622248478Sjkim	error = trackpoint_command(sc->kbdc, tp[TPMASK] == 0 ? 0x81 : 0x47,
4623248478Sjkim	    tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
4624248478Sjkim	if (error != 0)
4625248478Sjkim		return (error);
4626248478Sjkim	*oldvalp = newval;
4627248478Sjkim
4628248478Sjkim	return (0);
4629248478Sjkim}
4630248478Sjkim
4631248478Sjkimstatic void
4632248478Sjkimtrackpoint_sysctl_create_tree(struct psm_softc *sc)
4633248478Sjkim{
4634248478Sjkim
4635248478Sjkim	if (sc->tpinfo.sysctl_tree != NULL)
4636248478Sjkim		return;
4637248478Sjkim
4638248478Sjkim	/* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
4639248478Sjkim	sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
4640248478Sjkim	sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
4641248478Sjkim	    SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD,
4642248478Sjkim	    0, "IBM/Lenovo TrackPoint");
4643248478Sjkim
4644248478Sjkim	/* hw.psm.trackpoint.sensitivity */
4645248478Sjkim	sc->tpinfo.sensitivity = 0x64;
4646248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4647248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4648248478Sjkim	    "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4649248478Sjkim	    sc, TRACKPOINT_SYSCTL_SENSITIVITY,
4650248478Sjkim	    trackpoint_sysctl, "I",
4651248478Sjkim	    "Sensitivity");
4652248478Sjkim
4653248478Sjkim	/* hw.psm.trackpoint.negative_inertia */
4654248478Sjkim	sc->tpinfo.inertia = 0x06;
4655248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4656248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4657248478Sjkim	    "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4658248478Sjkim	    sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
4659248478Sjkim	    trackpoint_sysctl, "I",
4660248478Sjkim	    "Negative inertia factor");
4661248478Sjkim
4662248478Sjkim	/* hw.psm.trackpoint.upper_plateau */
4663248478Sjkim	sc->tpinfo.uplateau = 0x61;
4664248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4665248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4666248478Sjkim	    "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4667248478Sjkim	    sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
4668248478Sjkim	    trackpoint_sysctl, "I",
4669248478Sjkim	    "Transfer function upper plateau speed");
4670248478Sjkim
4671248478Sjkim	/* hw.psm.trackpoint.backup_range */
4672248478Sjkim	sc->tpinfo.reach = 0x0a;
4673248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4674248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4675248478Sjkim	    "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4676248478Sjkim	    sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
4677248478Sjkim	    trackpoint_sysctl, "I",
4678248478Sjkim	    "Backup range");
4679248478Sjkim
4680248478Sjkim	/* hw.psm.trackpoint.drag_hysteresis */
4681248478Sjkim	sc->tpinfo.draghys = 0xff;
4682248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4683248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4684248478Sjkim	    "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4685248478Sjkim	    sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
4686248478Sjkim	    trackpoint_sysctl, "I",
4687248478Sjkim	    "Drag hysteresis");
4688248478Sjkim
4689248478Sjkim	/* hw.psm.trackpoint.minimum_drag */
4690248478Sjkim	sc->tpinfo.mindrag = 0x14;
4691248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4692248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4693248478Sjkim	    "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4694248478Sjkim	    sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
4695248478Sjkim	    trackpoint_sysctl, "I",
4696248478Sjkim	    "Minimum drag");
4697248478Sjkim
4698248478Sjkim	/* hw.psm.trackpoint.up_threshold */
4699248478Sjkim	sc->tpinfo.upthresh = 0xff;
4700248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4701248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4702248478Sjkim	    "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4703248478Sjkim	    sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
4704248478Sjkim	    trackpoint_sysctl, "I",
4705248478Sjkim	    "Up threshold for release");
4706248478Sjkim
4707248478Sjkim	/* hw.psm.trackpoint.threshold */
4708248478Sjkim	sc->tpinfo.threshold = 0x08;
4709248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4710248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4711248478Sjkim	    "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4712248478Sjkim	    sc, TRACKPOINT_SYSCTL_THRESHOLD,
4713248478Sjkim	    trackpoint_sysctl, "I",
4714248478Sjkim	    "Threshold");
4715248478Sjkim
4716248478Sjkim	/* hw.psm.trackpoint.jenks_curvature */
4717248478Sjkim	sc->tpinfo.jenks = 0x87;
4718248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4719248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4720248478Sjkim	    "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4721248478Sjkim	    sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
4722248478Sjkim	    trackpoint_sysctl, "I",
4723248478Sjkim	    "Jenks curvature");
4724248478Sjkim
4725248478Sjkim	/* hw.psm.trackpoint.z_time */
4726248478Sjkim	sc->tpinfo.ztime = 0x26;
4727248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4728248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4729248478Sjkim	    "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4730248478Sjkim	    sc, TRACKPOINT_SYSCTL_Z_TIME,
4731248478Sjkim	    trackpoint_sysctl, "I",
4732248478Sjkim	    "Z time constant");
4733248478Sjkim
4734248478Sjkim	/* hw.psm.trackpoint.press_to_select */
4735248478Sjkim	sc->tpinfo.pts = 0x00;
4736248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4737248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4738248478Sjkim	    "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4739248478Sjkim	    sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
4740248478Sjkim	    trackpoint_sysctl, "I",
4741248478Sjkim	    "Press to Select");
4742248478Sjkim
4743248478Sjkim	/* hw.psm.trackpoint.skip_backups */
4744248478Sjkim	sc->tpinfo.skipback = 0x00;
4745248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4746248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4747248478Sjkim	    "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4748248478Sjkim	    sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
4749248478Sjkim	    trackpoint_sysctl, "I",
4750248478Sjkim	    "Skip backups from drags");
4751248478Sjkim}
4752248478Sjkim
4753248478Sjkimstatic int
4754248478Sjkimenable_trackpoint(KBDC kbdc, struct psm_softc *sc)
4755248478Sjkim{
4756248478Sjkim	int id;
4757248478Sjkim
4758248478Sjkim	if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
4759248478Sjkim	    read_aux_data(kbdc) != 0x01)
4760248478Sjkim		return (FALSE);
4761248478Sjkim	id = read_aux_data(kbdc);
4762248478Sjkim	if (id < 0x01)
4763248478Sjkim		return (FALSE);
4764248478Sjkim	if (sc != NULL)
4765248478Sjkim		sc->tphw = id;
4766248478Sjkim	if (!trackpoint_support)
4767248478Sjkim		return (FALSE);
4768248478Sjkim
4769248478Sjkim	if (sc != NULL) {
4770248478Sjkim		/* Create sysctl tree. */
4771248478Sjkim		trackpoint_sysctl_create_tree(sc);
4772248478Sjkim
4773248478Sjkim		trackpoint_command(kbdc, 0x81, 0x4a, sc->tpinfo.sensitivity);
4774248478Sjkim		trackpoint_command(kbdc, 0x81, 0x4d, sc->tpinfo.inertia);
4775248478Sjkim		trackpoint_command(kbdc, 0x81, 0x60, sc->tpinfo.uplateau);
4776248478Sjkim		trackpoint_command(kbdc, 0x81, 0x57, sc->tpinfo.reach);
4777248478Sjkim		trackpoint_command(kbdc, 0x81, 0x58, sc->tpinfo.draghys);
4778248478Sjkim		trackpoint_command(kbdc, 0x81, 0x59, sc->tpinfo.mindrag);
4779248478Sjkim		trackpoint_command(kbdc, 0x81, 0x5a, sc->tpinfo.upthresh);
4780248478Sjkim		trackpoint_command(kbdc, 0x81, 0x5c, sc->tpinfo.threshold);
4781248478Sjkim		trackpoint_command(kbdc, 0x81, 0x5d, sc->tpinfo.jenks);
4782248478Sjkim		trackpoint_command(kbdc, 0x81, 0x5e, sc->tpinfo.ztime);
4783248478Sjkim		if (sc->tpinfo.pts == 0x01)
4784248478Sjkim			trackpoint_command(kbdc, 0x47, 0x2c, 0x01);
4785248478Sjkim		if (sc->tpinfo.skipback == 0x01)
4786248478Sjkim			trackpoint_command(kbdc, 0x47, 0x2d, 0x08);
4787248478Sjkim
4788248478Sjkim		sc->hw.hwid = id;
4789248478Sjkim		sc->hw.buttons = 3;
4790248478Sjkim	}
4791248478Sjkim
4792248478Sjkim	return (TRUE);
4793248478Sjkim}
4794248478Sjkim
479549965Syokota/* Interlink electronics VersaPad */
479649965Syokotastatic int
4797233580Sjkimenable_versapad(KBDC kbdc, struct psm_softc *sc)
479849965Syokota{
4799178019Sjkim	int data[3];
480049965Syokota
4801178019Sjkim	set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
4802178019Sjkim	set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
4803178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
4804178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
4805178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
4806178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
4807178019Sjkim	if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
4808178019Sjkim		return (FALSE);
4809178019Sjkim	if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
4810178019Sjkim		return (FALSE);
4811178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
481249965Syokota
4813178019Sjkim	return (TRUE);				/* PS/2 absolute mode */
481449965Syokota}
481549965Syokota
4816147271Smarius/*
4817147271Smarius * Return true if 'now' is earlier than (start + (secs.usecs)).
4818147271Smarius * Now may be NULL and the function will fetch the current time from
4819147271Smarius * getmicrouptime(), or a cached 'now' can be passed in.
4820147271Smarius * All values should be numbers derived from getmicrouptime().
4821147271Smarius */
482241016Sdfrstatic int
4823147271Smariustimeelapsed(start, secs, usecs, now)
4824147271Smarius	const struct timeval *start, *now;
4825147271Smarius	int secs, usecs;
4826147271Smarius{
4827147271Smarius	struct timeval snow, tv;
4828147271Smarius
4829147271Smarius	/* if there is no 'now' passed in, the get it as a convience. */
4830147271Smarius	if (now == NULL) {
4831147271Smarius		getmicrouptime(&snow);
4832147271Smarius		now = &snow;
4833147271Smarius	}
4834178019Sjkim
4835147271Smarius	tv.tv_sec = secs;
4836147271Smarius	tv.tv_usec = usecs;
4837147271Smarius	timevaladd(&tv, start);
4838147271Smarius	return (timevalcmp(&tv, now, <));
4839147271Smarius}
4840147271Smarius
4841147271Smariusstatic int
484254629Syokotapsmresume(device_t dev)
484341016Sdfr{
4844178019Sjkim	struct psm_softc *sc = device_get_softc(dev);
4845178019Sjkim	int unit = device_get_unit(dev);
4846178019Sjkim	int err;
484741016Sdfr
4848178019Sjkim	VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
484941016Sdfr
4850233580Sjkim	if ((sc->config &
4851233580Sjkim	    (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
4852178019Sjkim		return (0);
485358230Syokota
4854178019Sjkim	err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
485541016Sdfr
4856178019Sjkim	if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
4857178019Sjkim		/*
4858178019Sjkim		 * Release the blocked process; it must be notified that
4859178019Sjkim		 * the device cannot be accessed anymore.
4860178019Sjkim		 */
4861178019Sjkim		sc->state &= ~PSM_ASLP;
4862178019Sjkim		wakeup(sc);
4863178019Sjkim	}
486441016Sdfr
4865178019Sjkim	VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
486641016Sdfr
4867178019Sjkim	return (err);
486841016Sdfr}
486941016Sdfr
487052997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
487183147Syokota
4872147271Smarius#ifdef DEV_ISA
4873147271Smarius
487483147Syokota/*
487583147Syokota * This sucks up assignments from PNPBIOS and ACPI.
487683147Syokota */
487783147Syokota
487883931Syokota/*
487983931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
488083931Syokota * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
488183931Syokota * can be probed and attached only after the AT keyboard controller is
488283931Syokota * attached, we shall quietly reserve the IRQ resource for later use.
488383931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
488483931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging
488583931Syokota * under the keyboard controller, then probe and attach it.
488683931Syokota */
488783147Syokota
488883147Syokotastatic	devclass_t			psmcpnp_devclass;
488983147Syokota
489083147Syokotastatic	device_probe_t			psmcpnp_probe;
489183147Syokotastatic	device_attach_t			psmcpnp_attach;
489283147Syokota
489383147Syokotastatic device_method_t psmcpnp_methods[] = {
489483147Syokota	DEVMETHOD(device_probe,		psmcpnp_probe),
489583147Syokota	DEVMETHOD(device_attach,	psmcpnp_attach),
4896178019Sjkim
489783147Syokota	{ 0, 0 }
489883147Syokota};
489983147Syokota
490083147Syokotastatic driver_t psmcpnp_driver = {
490183147Syokota	PSMCPNP_DRIVER_NAME,
490283147Syokota	psmcpnp_methods,
490383147Syokota	1,			/* no softc */
490483147Syokota};
490583147Syokota
490683147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = {
490788188Ssheldonh	{ 0x030fd041, "PS/2 mouse port" },		/* PNP0F03 */
4908156730Stakawata	{ 0x0e0fd041, "PS/2 mouse port" },		/* PNP0F0E */
4909156730Stakawata	{ 0x120fd041, "PS/2 mouse port" },		/* PNP0F12 */
491083147Syokota	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
491183147Syokota	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
4912117117Smikeh	{ 0x02002e4f, "Dell PS/2 mouse port" },		/* Lat. X200, Dell */
4913156730Stakawata	{ 0x0002a906, "ALPS Glide Point" },		/* ALPS Glide Point */
491483492Syokota	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
491584407Stakawata	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
4916109679Shsu	{ 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
4917109679Shsu	{ 0x0290d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9002, Vaio */
4918109710Smarcel	{ 0x0390d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9003, Vaio */
4919109679Shsu	{ 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
492083147Syokota	{ 0 }
492183147Syokota};
492283147Syokota
492383147Syokotastatic int
492483147Syokotacreate_a_copy(device_t atkbdc, device_t me)
492583147Syokota{
492683147Syokota	device_t psm;
492783147Syokota	u_long irq;
492883147Syokota
492983931Syokota	/* find the PS/2 mouse device instance under the keyboard controller */
493083931Syokota	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
4931178019Sjkim	    device_get_unit(atkbdc));
493283147Syokota	if (psm == NULL)
4933178019Sjkim		return (ENXIO);
493483931Syokota	if (device_get_state(psm) != DS_NOTPRESENT)
4935178019Sjkim		return (0);
493683147Syokota
493783931Syokota	/* move our resource to the found device */
493883931Syokota	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
4939216491Sjhb	bus_delete_resource(me, SYS_RES_IRQ, 0);
494083147Syokota	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
494183147Syokota
494283147Syokota	/* ...then probe and attach it */
4943178019Sjkim	return (device_probe_and_attach(psm));
494483147Syokota}
494583147Syokota
494683147Syokotastatic int
494783147Syokotapsmcpnp_probe(device_t dev)
494883147Syokota{
494983931Syokota	struct resource *res;
495083931Syokota	u_long irq;
495183931Syokota	int rid;
495283147Syokota
495383147Syokota	if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
4954178019Sjkim		return (ENXIO);
495583147Syokota
495683931Syokota	/*
495783931Syokota	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
495883931Syokota	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
495983931Syokota	 * declares the PS/2 mouse device node without an IRQ resource!
496083931Syokota	 * If this happens, we shall refer to device hints.
496183931Syokota	 * If we still don't find it there, use a hardcoded value... XXX
496283931Syokota	 */
496383931Syokota	rid = 0;
496483931Syokota	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
496583931Syokota	if (irq <= 0) {
496683931Syokota		if (resource_long_value(PSM_DRIVER_NAME,
4967178019Sjkim		    device_get_unit(dev),"irq", &irq) != 0)
496883931Syokota			irq = 12;	/* XXX */
496983931Syokota		device_printf(dev, "irq resource info is missing; "
4970178019Sjkim		    "assuming irq %ld\n", irq);
497183931Syokota		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
497283931Syokota	}
4973216491Sjhb	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0);
497483931Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
497583147Syokota
497683147Syokota	/* keep quiet */
497783147Syokota	if (!bootverbose)
497883147Syokota		device_quiet(dev);
497983492Syokota
498083931Syokota	return ((res == NULL) ? ENXIO : 0);
498183147Syokota}
498283147Syokota
498383147Syokotastatic int
498483147Syokotapsmcpnp_attach(device_t dev)
498583147Syokota{
498683492Syokota	device_t atkbdc;
498783147Syokota
498883931Syokota	/* find the keyboard controller, which may be on acpi* or isa* bus */
498983931Syokota	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
4990178019Sjkim	    device_get_unit(dev));
4991178019Sjkim	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
499283492Syokota		create_a_copy(atkbdc, dev);
499383492Syokota
4994178019Sjkim	return (0);
499583147Syokota}
499683147Syokota
499783147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
499883147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
4999147271Smarius
5000147271Smarius#endif /* DEV_ISA */
5001