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: stable/10/sys/dev/atkbdc/psm.c 329533 2018-02-18 22:12:20Z wulf $");
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,
194281709Srpaulo	SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
195281709Srpaulo	SYNAPTICS_SYSCTL_TOUCHPAD_OFF
196183888Sdumbbell};
197183888Sdumbbell
198139982Sphiliptypedef struct synapticsinfo {
199183888Sdumbbell	struct sysctl_ctx_list	 sysctl_ctx;
200178019Sjkim	struct sysctl_oid	*sysctl_tree;
201183888Sdumbbell	int			 directional_scrolls;
202281708Srpaulo	int			 two_finger_scroll;
203183888Sdumbbell	int			 min_pressure;
204183888Sdumbbell	int			 max_pressure;
205183888Sdumbbell	int			 max_width;
206183888Sdumbbell	int			 margin_top;
207183888Sdumbbell	int			 margin_right;
208183888Sdumbbell	int			 margin_bottom;
209183888Sdumbbell	int			 margin_left;
210183888Sdumbbell	int			 na_top;
211183888Sdumbbell	int			 na_right;
212183888Sdumbbell	int			 na_bottom;
213183888Sdumbbell	int			 na_left;
214183888Sdumbbell	int			 window_min;
215183888Sdumbbell	int			 window_max;
216183888Sdumbbell	int			 multiplicator;
217183888Sdumbbell	int			 weight_current;
218183888Sdumbbell	int			 weight_previous;
219183888Sdumbbell	int			 weight_previous_na;
220183888Sdumbbell	int			 weight_len_squared;
221183888Sdumbbell	int			 div_min;
222183888Sdumbbell	int			 div_max;
223183888Sdumbbell	int			 div_max_na;
224183888Sdumbbell	int			 div_len;
225183888Sdumbbell	int			 tap_max_delta;
226183888Sdumbbell	int			 tap_min_queue;
227183888Sdumbbell	int			 taphold_timeout;
228183888Sdumbbell	int			 vscroll_ver_area;
229183888Sdumbbell	int			 vscroll_hor_area;
230183888Sdumbbell	int			 vscroll_min_delta;
231183888Sdumbbell	int			 vscroll_div_min;
232183888Sdumbbell	int			 vscroll_div_max;
233281709Srpaulo	int			 touchpad_off;
234139982Sphilip} synapticsinfo_t;
235139982Sphilip
236183888Sdumbbelltypedef struct synapticspacket {
237183888Sdumbbell	int			x;
238183888Sdumbbell	int			y;
239183888Sdumbbell} synapticspacket_t;
240183888Sdumbbell
241183888Sdumbbell#define	SYNAPTICS_PACKETQUEUE 10
242183888Sdumbbell#define SYNAPTICS_QUEUE_CURSOR(x)					\
243183888Sdumbbell	(x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
244183888Sdumbbell
245244405Sdumbbell#define	SYNAPTICS_VERSION_GE(synhw, major, minor)			\
246244405Sdumbbell    ((synhw).infoMajor > (major) ||					\
247244405Sdumbbell     ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
248244405Sdumbbell
249183888Sdumbbelltypedef struct synapticsaction {
250183888Sdumbbell	synapticspacket_t	queue[SYNAPTICS_PACKETQUEUE];
251183888Sdumbbell	int			queue_len;
252183888Sdumbbell	int			queue_cursor;
253183888Sdumbbell	int			window_min;
254183888Sdumbbell	int			start_x;
255183888Sdumbbell	int			start_y;
256183888Sdumbbell	int			avg_dx;
257183888Sdumbbell	int			avg_dy;
258183888Sdumbbell	int			squelch_x;
259183888Sdumbbell	int			squelch_y;
260183888Sdumbbell	int			fingers_nb;
261183888Sdumbbell	int			tap_button;
262183888Sdumbbell	int			in_taphold;
263183888Sdumbbell	int			in_vscroll;
264183888Sdumbbell} synapticsaction_t;
265183888Sdumbbell
266248478Sjkimenum {
267248478Sjkim	TRACKPOINT_SYSCTL_SENSITIVITY,
268248478Sjkim	TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
269248478Sjkim	TRACKPOINT_SYSCTL_UPPER_PLATEAU,
270248478Sjkim	TRACKPOINT_SYSCTL_BACKUP_RANGE,
271248478Sjkim	TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
272248478Sjkim	TRACKPOINT_SYSCTL_MINIMUM_DRAG,
273248478Sjkim	TRACKPOINT_SYSCTL_UP_THRESHOLD,
274248478Sjkim	TRACKPOINT_SYSCTL_THRESHOLD,
275248478Sjkim	TRACKPOINT_SYSCTL_JENKS_CURVATURE,
276248478Sjkim	TRACKPOINT_SYSCTL_Z_TIME,
277248478Sjkim	TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
278248478Sjkim	TRACKPOINT_SYSCTL_SKIP_BACKUPS
279248478Sjkim};
280248478Sjkim
281248478Sjkimtypedef struct trackpointinfo {
282248478Sjkim	struct sysctl_ctx_list sysctl_ctx;
283248478Sjkim	struct sysctl_oid *sysctl_tree;
284248478Sjkim	int	sensitivity;
285248478Sjkim	int	inertia;
286248478Sjkim	int	uplateau;
287248478Sjkim	int	reach;
288248478Sjkim	int	draghys;
289248478Sjkim	int	mindrag;
290248478Sjkim	int	upthresh;
291248478Sjkim	int	threshold;
292248478Sjkim	int	jenks;
293248478Sjkim	int	ztime;
294248478Sjkim	int	pts;
295248478Sjkim	int	skipback;
296248478Sjkim} trackpointinfo_t;
297248478Sjkim
29841016Sdfr/* driver control block */
29941016Sdfrstruct psm_softc {		/* Driver status information */
300178019Sjkim	int		unit;
301178019Sjkim	struct selinfo	rsel;		/* Process selecting for Input */
302178019Sjkim	u_char		state;		/* Mouse driver state */
303178019Sjkim	int		config;		/* driver configuration flags */
304178019Sjkim	int		flags;		/* other flags */
305178019Sjkim	KBDC		kbdc;		/* handle to access kbd controller */
306178019Sjkim	struct resource	*intr;		/* IRQ resource */
307178019Sjkim	void		*ih;		/* interrupt handle */
308178019Sjkim	mousehw_t	hw;		/* hardware information */
309178019Sjkim	synapticshw_t	synhw;		/* Synaptics hardware information */
310178019Sjkim	synapticsinfo_t	syninfo;	/* Synaptics configuration */
311183888Sdumbbell	synapticsaction_t synaction;	/* Synaptics action context */
312248478Sjkim	int		tphw;		/* TrackPoint hardware information */
313248478Sjkim	trackpointinfo_t tpinfo;	/* TrackPoint configuration */
314178019Sjkim	mousemode_t	mode;		/* operation mode */
315178019Sjkim	mousemode_t	dflt_mode;	/* default operation mode */
316178019Sjkim	mousestatus_t	status;		/* accumulated mouse movement */
317178019Sjkim	ringbuf_t	queue;		/* mouse status queue */
318178019Sjkim	packetbuf_t	pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
319178019Sjkim	int		pqueue_start;	/* start of data in queue */
320178019Sjkim	int		pqueue_end;	/* end of data in queue */
321178019Sjkim	int		button;		/* the latest button state */
322178019Sjkim	int		xold;		/* previous absolute X position */
323178019Sjkim	int		yold;		/* previous absolute Y position */
324178019Sjkim	int		xaverage;	/* average X position */
325178019Sjkim	int		yaverage;	/* average Y position */
326178019Sjkim	int		squelch; /* level to filter movement at low speed */
327178019Sjkim	int		zmax;	/* maximum pressure value for touchpads */
328178019Sjkim	int		syncerrors; /* # of bytes discarded to synchronize */
329178019Sjkim	int		pkterrors;  /* # of packets failed during quaranteen. */
330178019Sjkim	struct timeval	inputtimeout;
331178019Sjkim	struct timeval	lastsoftintr;	/* time of last soft interrupt */
332178019Sjkim	struct timeval	lastinputerr;	/* time last sync error happened */
333178019Sjkim	struct timeval	taptimeout;	/* tap timeout for touchpads */
334178019Sjkim	int		watchdog;	/* watchdog timer flag */
335240211Sjhb	struct callout	callout;	/* watchdog timer call out */
336240211Sjhb	struct callout	softcallout; /* buffer timer call out */
337178019Sjkim	struct cdev	*dev;
338178019Sjkim	struct cdev	*bdev;
339178019Sjkim	int		lasterr;
340178019Sjkim	int		cmdcount;
341189870Srnoland	struct sigio	*async;		/* Processes waiting for SIGIO */
342281708Srpaulo	int		extended_buttons;
34341016Sdfr};
344114293Smarkmstatic devclass_t psm_devclass;
34541016Sdfr
34641016Sdfr/* driver state flags (state) */
347178019Sjkim#define	PSM_VALID		0x80
348178019Sjkim#define	PSM_OPEN		1	/* Device is open */
349178019Sjkim#define	PSM_ASLP		2	/* Waiting for mouse data */
350178019Sjkim#define	PSM_SOFTARMED		4	/* Software interrupt armed */
351178019Sjkim#define	PSM_NEED_SYNCBITS	8	/* Set syncbits using next data pkt */
35241016Sdfr
35341016Sdfr/* driver configuration flags (config) */
354178019Sjkim#define	PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
355178019Sjkim#define	PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
356178019Sjkim#define	PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
357178019Sjkim#define	PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
358178019Sjkim#define	PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
359178019Sjkim#define	PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
360178019Sjkim#define	PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
361178019Sjkim#define	PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
362178019Sjkim#define	PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
36341016Sdfr
364178019Sjkim#define	PSM_CONFIG_FLAGS	\
365178019Sjkim    (PSM_CONFIG_RESOLUTION |	\
366178019Sjkim    PSM_CONFIG_ACCEL |		\
367178019Sjkim    PSM_CONFIG_NOCHECKSYNC |	\
368178019Sjkim    PSM_CONFIG_NOIDPROBE |	\
369178019Sjkim    PSM_CONFIG_NORESET |	\
370178019Sjkim    PSM_CONFIG_FORCETAP |	\
371178019Sjkim    PSM_CONFIG_IGNPORTERROR |	\
372178019Sjkim    PSM_CONFIG_HOOKRESUME |	\
373178019Sjkim    PSM_CONFIG_INITAFTERSUSPEND)
37441016Sdfr
37541016Sdfr/* other flags (flags) */
376178019Sjkim#define	PSM_FLAGS_FINGERDOWN	0x0001	/* VersaPad finger down */
37741016Sdfr
378135945Sphilip/* Tunables */
379200674Sdumbbellstatic int tap_enabled = -1;
380200674SdumbbellTUNABLE_INT("hw.psm.tap_enabled", &tap_enabled);
381200674Sdumbbell
382255210Seadlerstatic int synaptics_support = 0;
383135945SphilipTUNABLE_INT("hw.psm.synaptics_support", &synaptics_support);
384135945Sphilip
385255210Seadlerstatic int trackpoint_support = 0;
386248478SjkimTUNABLE_INT("hw.psm.trackpoint_support", &trackpoint_support);
387248478Sjkim
388139628Sphilipstatic int verbose = PSM_DEBUG;
389139628SphilipTUNABLE_INT("debug.psm.loglevel", &verbose);
390139628Sphilip
39141016Sdfr/* for backward compatibility */
392178019Sjkim#define	OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
393178019Sjkim#define	OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
394178019Sjkim#define	OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
39541016Sdfr
39641016Sdfrtypedef struct old_mousehw {
397178019Sjkim	int	buttons;
398178019Sjkim	int	iftype;
399178019Sjkim	int	type;
400178019Sjkim	int	hwid;
40141016Sdfr} old_mousehw_t;
40241016Sdfr
40341016Sdfrtypedef struct old_mousemode {
404178019Sjkim	int	protocol;
405178019Sjkim	int	rate;
406178019Sjkim	int	resolution;
407178019Sjkim	int	accelfactor;
40841016Sdfr} old_mousemode_t;
40941016Sdfr
41041016Sdfr/* packet formatting function */
411178019Sjkimtypedef int	packetfunc_t(struct psm_softc *, u_char *, int *, int,
412178019Sjkim    mousestatus_t *);
41341016Sdfr
41441016Sdfr/* function prototypes */
415178019Sjkimstatic void	psmidentify(driver_t *, device_t);
416178019Sjkimstatic int	psmprobe(device_t);
417178019Sjkimstatic int	psmattach(device_t);
418178019Sjkimstatic int	psmdetach(device_t);
419178019Sjkimstatic int	psmresume(device_t);
42041016Sdfr
421178019Sjkimstatic d_open_t		psmopen;
422178019Sjkimstatic d_close_t	psmclose;
423178019Sjkimstatic d_read_t		psmread;
424178019Sjkimstatic d_write_t	psmwrite;
425178019Sjkimstatic d_ioctl_t	psmioctl;
426178019Sjkimstatic d_poll_t		psmpoll;
42741016Sdfr
428178019Sjkimstatic int	enable_aux_dev(KBDC);
429178019Sjkimstatic int	disable_aux_dev(KBDC);
430178019Sjkimstatic int	get_mouse_status(KBDC, int *, int, int);
431178019Sjkimstatic int	get_aux_id(KBDC);
432178019Sjkimstatic int	set_mouse_sampling_rate(KBDC, int);
433178019Sjkimstatic int	set_mouse_scaling(KBDC, int);
434178019Sjkimstatic int	set_mouse_resolution(KBDC, int);
435178019Sjkimstatic int	set_mouse_mode(KBDC);
436178019Sjkimstatic int	get_mouse_buttons(KBDC);
437178019Sjkimstatic int	is_a_mouse(int);
438178019Sjkimstatic void	recover_from_error(KBDC);
439178019Sjkimstatic int	restore_controller(KBDC, int);
440178019Sjkimstatic int	doinitialize(struct psm_softc *, mousemode_t *);
441178019Sjkimstatic int	doopen(struct psm_softc *, int);
442178019Sjkimstatic int	reinitialize(struct psm_softc *, int);
443178019Sjkimstatic char	*model_name(int);
444178019Sjkimstatic void	psmsoftintr(void *);
445178019Sjkimstatic void	psmintr(void *);
446178019Sjkimstatic void	psmtimeout(void *);
447178019Sjkimstatic int	timeelapsed(const struct timeval *, int, int,
448178019Sjkim		    const struct timeval *);
449178019Sjkimstatic void	dropqueue(struct psm_softc *);
450178019Sjkimstatic void	flushpackets(struct psm_softc *);
451178019Sjkimstatic void	proc_mmanplus(struct psm_softc *, packetbuf_t *,
452178019Sjkim		    mousestatus_t *, int *, int *, int *);
453178019Sjkimstatic int	proc_synaptics(struct psm_softc *, packetbuf_t *,
454178019Sjkim		    mousestatus_t *, int *, int *, int *);
455178019Sjkimstatic void	proc_versapad(struct psm_softc *, packetbuf_t *,
456178019Sjkim		    mousestatus_t *, int *, int *, int *);
457178019Sjkimstatic int	tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
458178019Sjkim		    u_char *);
45941016Sdfr
46041016Sdfr/* vendor specific features */
461233580Sjkimtypedef int	probefunc_t(KBDC, struct psm_softc *);
46241016Sdfr
463178019Sjkimstatic int	mouse_id_proc1(KBDC, int, int, int *);
464178019Sjkimstatic int	mouse_ext_command(KBDC, int);
46541016Sdfr
466178019Sjkimstatic probefunc_t	enable_groller;
467178019Sjkimstatic probefunc_t	enable_gmouse;
468178019Sjkimstatic probefunc_t	enable_aglide;
469178019Sjkimstatic probefunc_t	enable_kmouse;
470178019Sjkimstatic probefunc_t	enable_msexplorer;
471178019Sjkimstatic probefunc_t	enable_msintelli;
472178019Sjkimstatic probefunc_t	enable_4dmouse;
473178019Sjkimstatic probefunc_t	enable_4dplus;
474178019Sjkimstatic probefunc_t	enable_mmanplus;
475178019Sjkimstatic probefunc_t	enable_synaptics;
476248478Sjkimstatic probefunc_t	enable_trackpoint;
477178019Sjkimstatic probefunc_t	enable_versapad;
478178019Sjkim
479281709Srpaulostatic void set_trackpoint_parameters(struct psm_softc *sc);
480281709Srpaulostatic void synaptics_passthrough_on(struct psm_softc *sc);
481281709Srpaulostatic void synaptics_passthrough_off(struct psm_softc *sc);
482281709Srpaulo
48341016Sdfrstatic struct {
484178019Sjkim	int		model;
485178019Sjkim	u_char		syncmask;
486178019Sjkim	int		packetsize;
487178019Sjkim	probefunc_t	*probefunc;
48841016Sdfr} vendortype[] = {
489178019Sjkim	/*
490178019Sjkim	 * WARNING: the order of probe is very important.  Don't mess it
491178019Sjkim	 * unless you know what you are doing.
492178019Sjkim	 */
493178019Sjkim	{ MOUSE_MODEL_NET,		/* Genius NetMouse */
494178019Sjkim	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
495178019Sjkim	{ MOUSE_MODEL_NETSCROLL,	/* Genius NetScroll */
496178019Sjkim	  0xc8, 6, enable_groller },
497178019Sjkim	{ MOUSE_MODEL_MOUSEMANPLUS,	/* Logitech MouseMan+ */
498178019Sjkim	  0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
499178019Sjkim	{ MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
500178019Sjkim	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
501178019Sjkim	{ MOUSE_MODEL_4D,		/* A4 Tech 4D Mouse */
502178019Sjkim	  0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
503178019Sjkim	{ MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
504178019Sjkim	  0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
505179474Sphilip	{ MOUSE_MODEL_SYNAPTICS,	/* Synaptics Touchpad */
506179474Sphilip	  0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
507178019Sjkim	{ MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
508178019Sjkim	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
509178019Sjkim	{ MOUSE_MODEL_GLIDEPOINT,	/* ALPS GlidePoint */
510178019Sjkim	  0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
511178019Sjkim	{ MOUSE_MODEL_THINK,		/* Kensington ThinkingMouse */
512178019Sjkim	  0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
513178019Sjkim	{ MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
514178019Sjkim	  0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
515248478Sjkim	{ MOUSE_MODEL_TRACKPOINT,	/* IBM/Lenovo TrackPoint */
516248478Sjkim	  0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
517178019Sjkim	{ MOUSE_MODEL_GENERIC,
518178019Sjkim	  0xc0, MOUSE_PS2_PACKETSIZE, NULL },
51941016Sdfr};
520178019Sjkim#define	GENERIC_MOUSE_ENTRY	\
521178019Sjkim    ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
52241016Sdfr
52341016Sdfr/* device driver declarateion */
52441016Sdfrstatic device_method_t psm_methods[] = {
52541016Sdfr	/* Device interface */
52683147Syokota	DEVMETHOD(device_identify,	psmidentify),
52741016Sdfr	DEVMETHOD(device_probe,		psmprobe),
52841016Sdfr	DEVMETHOD(device_attach,	psmattach),
52958230Syokota	DEVMETHOD(device_detach,	psmdetach),
53054629Syokota	DEVMETHOD(device_resume,	psmresume),
53141016Sdfr
53241016Sdfr	{ 0, 0 }
53341016Sdfr};
53441016Sdfr
53541016Sdfrstatic driver_t psm_driver = {
536178019Sjkim	PSM_DRIVER_NAME,
537178019Sjkim	psm_methods,
538178019Sjkim	sizeof(struct psm_softc),
53941016Sdfr};
54041016Sdfr
54147625Sphkstatic struct cdevsw psm_cdevsw = {
542126080Sphk	.d_version =	D_VERSION,
543126080Sphk	.d_flags =	D_NEEDGIANT,
544111815Sphk	.d_open =	psmopen,
545111815Sphk	.d_close =	psmclose,
546111815Sphk	.d_read =	psmread,
547178017Sjkim	.d_write =	psmwrite,
548111815Sphk	.d_ioctl =	psmioctl,
549111815Sphk	.d_poll =	psmpoll,
550111815Sphk	.d_name =	PSM_DRIVER_NAME,
55141016Sdfr};
55241016Sdfr
55341016Sdfr/* device I/O routines */
55441016Sdfrstatic int
55541016Sdfrenable_aux_dev(KBDC kbdc)
55641016Sdfr{
557178019Sjkim	int res;
55841016Sdfr
559178019Sjkim	res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
560178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
56141016Sdfr
562178019Sjkim	return (res == PSM_ACK);
56341016Sdfr}
56441016Sdfr
56541016Sdfrstatic int
56641016Sdfrdisable_aux_dev(KBDC kbdc)
56741016Sdfr{
568178019Sjkim	int res;
56941016Sdfr
570178019Sjkim	res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
571178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
57241016Sdfr
573178019Sjkim	return (res == PSM_ACK);
57441016Sdfr}
57541016Sdfr
57641016Sdfrstatic int
57741016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
57841016Sdfr{
579178019Sjkim	int cmd;
580178019Sjkim	int res;
581178019Sjkim	int i;
58241016Sdfr
583178019Sjkim	switch (flag) {
584178019Sjkim	case 0:
585178019Sjkim	default:
586178019Sjkim		cmd = PSMC_SEND_DEV_STATUS;
587178019Sjkim		break;
588178019Sjkim	case 1:
589178019Sjkim		cmd = PSMC_SEND_DEV_DATA;
590178019Sjkim		break;
591178019Sjkim	}
592178019Sjkim	empty_aux_buffer(kbdc, 5);
593178019Sjkim	res = send_aux_command(kbdc, cmd);
594178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
595178019Sjkim	    (flag == 1) ? "DATA" : "STATUS", res));
596178019Sjkim	if (res != PSM_ACK)
597178019Sjkim		return (0);
59841016Sdfr
599178019Sjkim	for (i = 0; i < len; ++i) {
600178019Sjkim		status[i] = read_aux_data(kbdc);
601178019Sjkim		if (status[i] < 0)
602178019Sjkim			break;
603178019Sjkim	}
60441016Sdfr
605178019Sjkim	VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
606178019Sjkim	    (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
60741016Sdfr
608178019Sjkim	return (i);
60941016Sdfr}
61041016Sdfr
61141016Sdfrstatic int
61241016Sdfrget_aux_id(KBDC kbdc)
61341016Sdfr{
614178019Sjkim	int res;
615178019Sjkim	int id;
61641016Sdfr
617178019Sjkim	empty_aux_buffer(kbdc, 5);
618178019Sjkim	res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
619178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
620178019Sjkim	if (res != PSM_ACK)
621178019Sjkim		return (-1);
62241016Sdfr
623178019Sjkim	/* 10ms delay */
624178019Sjkim	DELAY(10000);
62541016Sdfr
626178019Sjkim	id = read_aux_data(kbdc);
627178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
62841016Sdfr
629178019Sjkim	return (id);
63041016Sdfr}
63141016Sdfr
63241016Sdfrstatic int
63341016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
63441016Sdfr{
635178019Sjkim	int res;
63641016Sdfr
637178019Sjkim	res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
638178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
63941016Sdfr
640178019Sjkim	return ((res == PSM_ACK) ? rate : -1);
64141016Sdfr}
64241016Sdfr
64341016Sdfrstatic int
64441016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
64541016Sdfr{
646178019Sjkim	int res;
64741016Sdfr
648178019Sjkim	switch (scale) {
649178019Sjkim	case 1:
650178019Sjkim	default:
651178019Sjkim		scale = PSMC_SET_SCALING11;
652178019Sjkim		break;
653178019Sjkim	case 2:
654178019Sjkim		scale = PSMC_SET_SCALING21;
655178019Sjkim		break;
656178019Sjkim	}
657178019Sjkim	res = send_aux_command(kbdc, scale);
658178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
659178019Sjkim	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
66041016Sdfr
661178019Sjkim	return (res == PSM_ACK);
66241016Sdfr}
66341016Sdfr
66441016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
66541016Sdfrstatic int
66641016Sdfrset_mouse_resolution(KBDC kbdc, int val)
66741016Sdfr{
668178019Sjkim	int res;
66941016Sdfr
670178019Sjkim	res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
671178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
67241016Sdfr
673178019Sjkim	return ((res == PSM_ACK) ? val : -1);
67441016Sdfr}
67541016Sdfr
67641016Sdfr/*
67741016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
67841016Sdfr * re-enabled by calling `enable_aux_dev()'
67941016Sdfr */
68041016Sdfrstatic int
68141016Sdfrset_mouse_mode(KBDC kbdc)
68241016Sdfr{
683178019Sjkim	int res;
68441016Sdfr
685178019Sjkim	res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
686178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
68741016Sdfr
688178019Sjkim	return (res == PSM_ACK);
68941016Sdfr}
69041016Sdfr
69141016Sdfrstatic int
69241016Sdfrget_mouse_buttons(KBDC kbdc)
69341016Sdfr{
694178019Sjkim	int c = 2;		/* assume two buttons by default */
695178019Sjkim	int status[3];
69641016Sdfr
697178019Sjkim	/*
698178019Sjkim	 * NOTE: a special sequence to obtain Logitech Mouse specific
699178019Sjkim	 * information: set resolution to 25 ppi, set scaling to 1:1, set
700178019Sjkim	 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
701178019Sjkim	 * mouse status bytes is the number of available buttons.
702178019Sjkim	 * Some manufactures also support this sequence.
703178019Sjkim	 */
704178019Sjkim	if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
705178019Sjkim		return (c);
706178019Sjkim	if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
707178019Sjkim	    set_mouse_scaling(kbdc, 1) &&
708178019Sjkim	    get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
709178019Sjkim		return (status[1]);
710178019Sjkim	return (c);
71141016Sdfr}
71241016Sdfr
71341016Sdfr/* misc subroutines */
71441016Sdfr/*
71541016Sdfr * Someday, I will get the complete list of valid pointing devices and
71641016Sdfr * their IDs... XXX
71741016Sdfr */
71841016Sdfrstatic int
71941016Sdfris_a_mouse(int id)
72041016Sdfr{
72141016Sdfr#if 0
722178019Sjkim	static int valid_ids[] = {
723178019Sjkim		PSM_MOUSE_ID,		/* mouse */
724178019Sjkim		PSM_BALLPOINT_ID,	/* ballpoint device */
725178019Sjkim		PSM_INTELLI_ID,		/* Intellimouse */
726178019Sjkim		PSM_EXPLORER_ID,	/* Intellimouse Explorer */
727178019Sjkim		-1			/* end of table */
728178019Sjkim	};
729178019Sjkim	int i;
73041016Sdfr
731178019Sjkim	for (i = 0; valid_ids[i] >= 0; ++i)
732178019Sjkim	if (valid_ids[i] == id)
733178019Sjkim		return (TRUE);
734178019Sjkim	return (FALSE);
73541016Sdfr#else
736178019Sjkim	return (TRUE);
73741016Sdfr#endif
73841016Sdfr}
73941016Sdfr
74041016Sdfrstatic char *
74141016Sdfrmodel_name(int model)
74241016Sdfr{
743178019Sjkim	static struct {
744178019Sjkim		int	model_code;
745178019Sjkim		char	*model_name;
746178019Sjkim	} models[] = {
747178019Sjkim		{ MOUSE_MODEL_NETSCROLL,	"NetScroll" },
748178019Sjkim		{ MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
749178019Sjkim		{ MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
750178019Sjkim		{ MOUSE_MODEL_THINK,		"ThinkingMouse" },
751178019Sjkim		{ MOUSE_MODEL_INTELLI,		"IntelliMouse" },
752178019Sjkim		{ MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
753178019Sjkim		{ MOUSE_MODEL_VERSAPAD,		"VersaPad" },
754178019Sjkim		{ MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
755178019Sjkim		{ MOUSE_MODEL_4D,		"4D Mouse" },
756178019Sjkim		{ MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
757178019Sjkim		{ MOUSE_MODEL_SYNAPTICS,	"Synaptics Touchpad" },
758248479Sjkim		{ MOUSE_MODEL_TRACKPOINT,	"IBM/Lenovo TrackPoint" },
759178019Sjkim		{ MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
760178019Sjkim		{ MOUSE_MODEL_UNKNOWN,		"Unknown" },
761178019Sjkim	};
762178019Sjkim	int i;
76341016Sdfr
764178019Sjkim	for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
765178019Sjkim		if (models[i].model_code == model)
766178019Sjkim			break;
767178019Sjkim	return (models[i].model_name);
76841016Sdfr}
76941016Sdfr
77041016Sdfrstatic void
77141016Sdfrrecover_from_error(KBDC kbdc)
77241016Sdfr{
773178019Sjkim	/* discard anything left in the output buffer */
774178019Sjkim	empty_both_buffers(kbdc, 10);
77541016Sdfr
77641016Sdfr#if 0
777178019Sjkim	/*
778178019Sjkim	 * NOTE: KBDC_RESET_KBD may not restore the communication between the
779178019Sjkim	 * keyboard and the controller.
780178019Sjkim	 */
781178019Sjkim	reset_kbd(kbdc);
78241016Sdfr#else
783178019Sjkim	/*
784178019Sjkim	 * NOTE: somehow diagnostic and keyboard port test commands bring the
785178019Sjkim	 * keyboard back.
786178019Sjkim	 */
787178019Sjkim	if (!test_controller(kbdc))
788178019Sjkim		log(LOG_ERR, "psm: keyboard controller failed.\n");
789178019Sjkim	/* if there isn't a keyboard in the system, the following error is OK */
790178019Sjkim	if (test_kbd_port(kbdc) != 0)
791178019Sjkim		VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
79241016Sdfr#endif
79341016Sdfr}
79441016Sdfr
79541016Sdfrstatic int
79641016Sdfrrestore_controller(KBDC kbdc, int command_byte)
79741016Sdfr{
798178019Sjkim	empty_both_buffers(kbdc, 10);
79941016Sdfr
800178019Sjkim	if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
801178019Sjkim		log(LOG_ERR, "psm: failed to restore the keyboard controller "
802178019Sjkim		    "command byte.\n");
803178019Sjkim		empty_both_buffers(kbdc, 10);
804178019Sjkim		return (FALSE);
805178019Sjkim	} else {
806178019Sjkim		empty_both_buffers(kbdc, 10);
807178019Sjkim		return (TRUE);
808178019Sjkim	}
80941016Sdfr}
81041016Sdfr
811178019Sjkim/*
81241016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
813178019Sjkim * and its interrupt must be disabled before calling this routine.
81441016Sdfr * The aux device will be disabled before returning.
81541016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
81641016Sdfr * calling this routine.
81741016Sdfr */
81841016Sdfrstatic int
81984880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode)
82041016Sdfr{
821178019Sjkim	KBDC kbdc = sc->kbdc;
822178019Sjkim	int stat[3];
823178019Sjkim	int i;
82441016Sdfr
825178019Sjkim	switch((i = test_aux_port(kbdc))) {
826178019Sjkim	case 1:	/* ignore these errors */
827178019Sjkim	case 2:
828178019Sjkim	case 3:
829178019Sjkim	case PSM_ACK:
830178019Sjkim		if (verbose)
831178019Sjkim			log(LOG_DEBUG,
832178019Sjkim			    "psm%d: strange result for test aux port (%d).\n",
833178019Sjkim			    sc->unit, i);
834178019Sjkim		/* FALLTHROUGH */
835178019Sjkim	case 0:		/* no error */
836178019Sjkim		break;
837178019Sjkim	case -1:	/* time out */
838178019Sjkim	default:	/* error */
839178019Sjkim		recover_from_error(kbdc);
840178019Sjkim		if (sc->config & PSM_CONFIG_IGNPORTERROR)
841178019Sjkim			break;
842178019Sjkim		log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
843178019Sjkim		    sc->unit, i);
844178019Sjkim		return (FALSE);
845178019Sjkim	}
84641016Sdfr
847178019Sjkim	if (sc->config & PSM_CONFIG_NORESET) {
848178019Sjkim		/*
849178019Sjkim		 * Don't try to reset the pointing device.  It may possibly
850178019Sjkim		 * be left in the unknown state, though...
851178019Sjkim		 */
852178019Sjkim	} else {
853178019Sjkim		/*
854178019Sjkim		 * NOTE: some controllers appears to hang the `keyboard' when
855178019Sjkim		 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
856178019Sjkim		 */
857178019Sjkim		if (!reset_aux_dev(kbdc)) {
858178019Sjkim			recover_from_error(kbdc);
859178019Sjkim			log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
860178019Sjkim			    sc->unit);
861178019Sjkim			return (FALSE);
862178019Sjkim		}
863178019Sjkim	}
864178019Sjkim
865178019Sjkim	/*
866178019Sjkim	 * both the aux port and the aux device is functioning, see
867178019Sjkim	 * if the device can be enabled.
86845789Speter	 */
869178019Sjkim	if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
870178019Sjkim		log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
871178019Sjkim		    sc->unit);
872178019Sjkim		return (FALSE);
87345789Speter	}
874178019Sjkim	empty_both_buffers(kbdc, 10);	/* remove stray data if any */
87541016Sdfr
876233580Sjkim	/* Re-enable the mouse. */
877233580Sjkim	for (i = 0; vendortype[i].probefunc != NULL; ++i)
878233580Sjkim		if (vendortype[i].model == sc->hw.model)
879233580Sjkim			(*vendortype[i].probefunc)(sc->kbdc, NULL);
88041016Sdfr
881178019Sjkim	/* set mouse parameters */
882178019Sjkim	if (mode != (mousemode_t *)NULL) {
883178019Sjkim		if (mode->rate > 0)
884178019Sjkim			mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
885178019Sjkim		if (mode->resolution >= 0)
886178019Sjkim			mode->resolution =
887178019Sjkim			    set_mouse_resolution(kbdc, mode->resolution);
888178019Sjkim		set_mouse_scaling(kbdc, 1);
889178019Sjkim		set_mouse_mode(kbdc);
890281709Srpaulo
891281709Srpaulo		/*
892281709Srpaulo		 * Trackpoint settings are lost on resume.
893281709Srpaulo		 * Restore them here.
894281709Srpaulo		 */
895281709Srpaulo		if (sc->tphw > 0)
896281709Srpaulo			set_trackpoint_parameters(sc);
897178019Sjkim	}
89841016Sdfr
899178019Sjkim	/* Record sync on the next data packet we see. */
900178019Sjkim	sc->flags |= PSM_NEED_SYNCBITS;
90141016Sdfr
902178019Sjkim	/* just check the status of the mouse */
903178019Sjkim	if (get_mouse_status(kbdc, stat, 0, 3) < 3)
904178019Sjkim		log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
905178019Sjkim		    sc->unit);
90641016Sdfr
907178019Sjkim	return (TRUE);
90841016Sdfr}
90941016Sdfr
91041016Sdfrstatic int
91184880Syokotadoopen(struct psm_softc *sc, int command_byte)
91241016Sdfr{
913178019Sjkim	int stat[3];
91441016Sdfr
915183888Sdumbbell	/*
916183888Sdumbbell	 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
917183888Sdumbbell	 * no obvious reason. Thus we check the current mode and restore the
918183888Sdumbbell	 * Absolute Mode if it was cleared.
919183888Sdumbbell	 *
920183888Sdumbbell	 * The previous hack at the end of psmprobe() wasn't efficient when
921183888Sdumbbell	 * moused(8) was restarted.
922183888Sdumbbell	 *
923183888Sdumbbell	 * A Reset (FF) or Set Defaults (F6) command would clear the
924183888Sdumbbell	 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
925183888Sdumbbell	 * doesn't show any evidence of such a command.
926183888Sdumbbell	 */
927183888Sdumbbell	if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
928183888Sdumbbell		mouse_ext_command(sc->kbdc, 1);
929183888Sdumbbell		get_mouse_status(sc->kbdc, stat, 0, 3);
930244405Sdumbbell		if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
931244405Sdumbbell		     stat[1] == 0x47) &&
932244405Sdumbbell		    stat[2] == 0x40) {
933183888Sdumbbell			/* Set the mode byte -- request wmode where
934183888Sdumbbell			 * available */
935183888Sdumbbell			if (sc->synhw.capExtended)
936183888Sdumbbell				mouse_ext_command(sc->kbdc, 0xc1);
937183888Sdumbbell			else
938183888Sdumbbell				mouse_ext_command(sc->kbdc, 0xc0);
939183888Sdumbbell			set_mouse_sampling_rate(sc->kbdc, 20);
940183888Sdumbbell			VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
941183888Sdumbbell			    "hopefully restored\n",
942183888Sdumbbell			    sc->unit));
943183888Sdumbbell		}
944183888Sdumbbell	}
945183888Sdumbbell
946200674Sdumbbell	/*
947200674Sdumbbell	 * A user may want to disable tap and drag gestures on a Synaptics
948200674Sdumbbell	 * TouchPad when it operates in Relative Mode.
949200674Sdumbbell	 */
950200674Sdumbbell	if (sc->hw.model == MOUSE_MODEL_GENERIC) {
951200674Sdumbbell		if (tap_enabled > 0) {
952200674Sdumbbell			/*
953200674Sdumbbell			 * Enable tap & drag gestures. We use a Mode Byte
954200674Sdumbbell			 * and clear the DisGest bit (see ��2.5 of Synaptics
955200674Sdumbbell			 * TouchPad Interfacing Guide).
956200674Sdumbbell			 */
957200674Sdumbbell			VLOG(2, (LOG_DEBUG,
958200674Sdumbbell			    "psm%d: enable tap and drag gestures\n",
959200674Sdumbbell			    sc->unit));
960200674Sdumbbell			mouse_ext_command(sc->kbdc, 0x00);
961200674Sdumbbell			set_mouse_sampling_rate(sc->kbdc, 20);
962200674Sdumbbell		} else if (tap_enabled == 0) {
963200674Sdumbbell			/*
964200674Sdumbbell			 * Disable tap & drag gestures. We use a Mode Byte
965200674Sdumbbell			 * and set the DisGest bit (see ��2.5 of Synaptics
966200674Sdumbbell			 * TouchPad Interfacing Guide).
967200674Sdumbbell			 */
968200674Sdumbbell			VLOG(2, (LOG_DEBUG,
969200674Sdumbbell			    "psm%d: disable tap and drag gestures\n",
970200674Sdumbbell			    sc->unit));
971200674Sdumbbell			mouse_ext_command(sc->kbdc, 0x04);
972200674Sdumbbell			set_mouse_sampling_rate(sc->kbdc, 20);
973200674Sdumbbell		}
974200674Sdumbbell	}
975200674Sdumbbell
976178019Sjkim	/* enable the mouse device */
977178019Sjkim	if (!enable_aux_dev(sc->kbdc)) {
978178019Sjkim		/* MOUSE ERROR: failed to enable the mouse because:
979178019Sjkim		 * 1) the mouse is faulty,
980178019Sjkim		 * 2) the mouse has been removed(!?)
981178019Sjkim		 * In the latter case, the keyboard may have hung, and need
982178019Sjkim		 * recovery procedure...
983178019Sjkim		 */
984178019Sjkim		recover_from_error(sc->kbdc);
98541016Sdfr#if 0
986178019Sjkim		/* FIXME: we could reset the mouse here and try to enable
987178019Sjkim		 * it again. But it will take long time and it's not a good
988178019Sjkim		 * idea to disable the keyboard that long...
989178019Sjkim		 */
990178019Sjkim		if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
991178019Sjkim			recover_from_error(sc->kbdc);
99241016Sdfr#else
993178019Sjkim		{
99441016Sdfr#endif
995178019Sjkim			restore_controller(sc->kbdc, command_byte);
996178019Sjkim			/* mark this device is no longer available */
997178019Sjkim			sc->state &= ~PSM_VALID;
998178019Sjkim			log(LOG_ERR,
999178019Sjkim			    "psm%d: failed to enable the device (doopen).\n",
1000178019Sjkim			sc->unit);
1001178019Sjkim			return (EIO);
1002178019Sjkim		}
100341016Sdfr	}
100441016Sdfr
1005178019Sjkim	if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1006178019Sjkim		log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
1007178019Sjkim		    sc->unit);
100841016Sdfr
1009178019Sjkim	/* enable the aux port and interrupt */
1010178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
101141016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1012178019Sjkim	    (command_byte & KBD_KBD_CONTROL_BITS) |
1013178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
1014178019Sjkim		/* CONTROLLER ERROR */
1015178019Sjkim		disable_aux_dev(sc->kbdc);
1016178019Sjkim		restore_controller(sc->kbdc, command_byte);
1017178019Sjkim		log(LOG_ERR,
1018178019Sjkim		    "psm%d: failed to enable the aux interrupt (doopen).\n",
1019178019Sjkim		    sc->unit);
1020178019Sjkim		return (EIO);
1021178019Sjkim	}
102241016Sdfr
1023178019Sjkim	/* start the watchdog timer */
1024178019Sjkim	sc->watchdog = FALSE;
1025240211Sjhb	callout_reset(&sc->callout, hz * 2, psmtimeout, sc);
102658230Syokota
1027178019Sjkim	return (0);
102841016Sdfr}
102941016Sdfr
103084880Syokotastatic int
103184880Syokotareinitialize(struct psm_softc *sc, int doinit)
103284880Syokota{
1033178019Sjkim	int err;
1034178019Sjkim	int c;
1035178019Sjkim	int s;
103684880Syokota
1037178019Sjkim	/* don't let anybody mess with the aux device */
1038178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
1039178019Sjkim		return (EIO);
1040178019Sjkim	s = spltty();
104184880Syokota
1042178019Sjkim	/* block our watchdog timer */
1043178019Sjkim	sc->watchdog = FALSE;
1044240211Sjhb	callout_stop(&sc->callout);
104584880Syokota
1046178019Sjkim	/* save the current controller command byte */
1047178019Sjkim	empty_both_buffers(sc->kbdc, 10);
1048178019Sjkim	c = get_controller_command_byte(sc->kbdc);
1049178019Sjkim	VLOG(2, (LOG_DEBUG,
1050178019Sjkim	    "psm%d: current command byte: %04x (reinitialize).\n",
1051178019Sjkim	    sc->unit, c));
105284880Syokota
1053178019Sjkim	/* enable the aux port but disable the aux interrupt and the keyboard */
1054178019Sjkim	if ((c == -1) || !set_controller_command_byte(sc->kbdc,
105584880Syokota	    kbdc_get_device_mask(sc->kbdc),
1056178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1057178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1058178019Sjkim		/* CONTROLLER ERROR */
1059178019Sjkim		splx(s);
1060178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
1061178019Sjkim		log(LOG_ERR,
1062178019Sjkim		    "psm%d: unable to set the command byte (reinitialize).\n",
1063178019Sjkim		    sc->unit);
1064178019Sjkim		return (EIO);
1065178019Sjkim	}
106684880Syokota
1067178019Sjkim	/* flush any data */
1068178019Sjkim	if (sc->state & PSM_VALID) {
1069178019Sjkim		/* this may fail; but never mind... */
1070178019Sjkim		disable_aux_dev(sc->kbdc);
1071178019Sjkim		empty_aux_buffer(sc->kbdc, 10);
1072178019Sjkim	}
1073178019Sjkim	flushpackets(sc);
1074178019Sjkim	sc->syncerrors = 0;
1075178019Sjkim	sc->pkterrors = 0;
1076178019Sjkim	memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
107784880Syokota
1078178019Sjkim	/* try to detect the aux device; are you still there? */
1079178019Sjkim	err = 0;
1080178019Sjkim	if (doinit) {
1081178019Sjkim		if (doinitialize(sc, &sc->mode)) {
1082178019Sjkim			/* yes */
1083178019Sjkim			sc->state |= PSM_VALID;
1084178019Sjkim		} else {
1085178019Sjkim			/* the device has gone! */
1086178019Sjkim			restore_controller(sc->kbdc, c);
1087178019Sjkim			sc->state &= ~PSM_VALID;
1088178019Sjkim			log(LOG_ERR,
1089178019Sjkim			    "psm%d: the aux device has gone! (reinitialize).\n",
1090178019Sjkim			    sc->unit);
1091178019Sjkim			err = ENXIO;
1092178019Sjkim		}
109384880Syokota	}
1094178019Sjkim	splx(s);
109584880Syokota
1096178019Sjkim	/* restore the driver state */
1097178019Sjkim	if ((sc->state & PSM_OPEN) && (err == 0)) {
1098178019Sjkim		/* enable the aux device and the port again */
1099178019Sjkim		err = doopen(sc, c);
1100178019Sjkim		if (err != 0)
1101178019Sjkim			log(LOG_ERR, "psm%d: failed to enable the device "
1102178019Sjkim			    "(reinitialize).\n", sc->unit);
1103178019Sjkim	} else {
1104178019Sjkim		/* restore the keyboard port and disable the aux port */
1105178019Sjkim		if (!set_controller_command_byte(sc->kbdc,
1106178019Sjkim		    kbdc_get_device_mask(sc->kbdc),
1107178019Sjkim		    (c & KBD_KBD_CONTROL_BITS) |
1108178019Sjkim		    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1109178019Sjkim			/* CONTROLLER ERROR */
1110178019Sjkim			log(LOG_ERR, "psm%d: failed to disable the aux port "
1111178019Sjkim			    "(reinitialize).\n", sc->unit);
1112178019Sjkim			err = EIO;
1113178019Sjkim		}
111484880Syokota	}
111584880Syokota
1116178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1117178019Sjkim	return (err);
111884880Syokota}
111984880Syokota
112041016Sdfr/* psm driver entry points */
112141016Sdfr
112283147Syokotastatic void
112383147Syokotapsmidentify(driver_t *driver, device_t parent)
112483147Syokota{
1125178019Sjkim	device_t psmc;
1126178019Sjkim	device_t psm;
1127178019Sjkim	u_long irq;
1128178019Sjkim	int unit;
112983147Syokota
1130178019Sjkim	unit = device_get_unit(parent);
113183931Syokota
1132178019Sjkim	/* always add at least one child */
1133178019Sjkim	psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1134178019Sjkim	if (psm == NULL)
1135178019Sjkim		return;
113683931Syokota
1137178019Sjkim	irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1138178019Sjkim	if (irq > 0)
1139178019Sjkim		return;
114083931Syokota
1141178019Sjkim	/*
1142178019Sjkim	 * If the PS/2 mouse device has already been reported by ACPI or
1143178019Sjkim	 * PnP BIOS, obtain the IRQ resource from it.
1144178019Sjkim	 * (See psmcpnp_attach() below.)
1145178019Sjkim	 */
1146178019Sjkim	psmc = device_find_child(device_get_parent(parent),
1147178019Sjkim	    PSMCPNP_DRIVER_NAME, unit);
1148178019Sjkim	if (psmc == NULL)
1149178019Sjkim		return;
1150178019Sjkim	irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1151178019Sjkim	if (irq <= 0)
1152178019Sjkim		return;
1153216491Sjhb	bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1154178019Sjkim	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
115583147Syokota}
115683147Syokota
1157178019Sjkim#define	endprobe(v)	do {			\
1158178019Sjkim	if (bootverbose)			\
1159178019Sjkim		--verbose;			\
1160178019Sjkim	kbdc_set_device_mask(sc->kbdc, mask);	\
1161178019Sjkim	kbdc_lock(sc->kbdc, FALSE);		\
1162178019Sjkim	return (v);				\
1163178019Sjkim} while (0)
116441016Sdfr
116541016Sdfrstatic int
116641016Sdfrpsmprobe(device_t dev)
116741016Sdfr{
1168178019Sjkim	int unit = device_get_unit(dev);
1169178019Sjkim	struct psm_softc *sc = device_get_softc(dev);
1170178019Sjkim	int stat[3];
1171178019Sjkim	int command_byte;
1172178019Sjkim	int mask;
1173178019Sjkim	int rid;
1174178019Sjkim	int i;
117541016Sdfr
117641016Sdfr#if 0
1177178019Sjkim	kbdc_debug(TRUE);
117841016Sdfr#endif
117958230Syokota
1180178019Sjkim	/* see if IRQ is available */
1181178019Sjkim	rid = KBDC_RID_AUX;
1182216491Sjhb	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1183178019Sjkim	if (sc->intr == NULL) {
1184178019Sjkim		if (bootverbose)
1185178019Sjkim			device_printf(dev, "unable to allocate IRQ\n");
1186178019Sjkim		return (ENXIO);
1187178019Sjkim	}
1188178019Sjkim	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
118958230Syokota
1190178019Sjkim	sc->unit = unit;
1191178019Sjkim	sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1192178019Sjkim	sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1193178019Sjkim	/* XXX: for backward compatibility */
119458230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1195178019Sjkim	sc->config |=
119658230Syokota#ifdef PSM_RESETAFTERSUSPEND
1197233580Sjkim	PSM_CONFIG_INITAFTERSUSPEND;
119858230Syokota#else
119958230Syokota	PSM_CONFIG_HOOKRESUME;
120058230Syokota#endif
120158230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1202178019Sjkim	sc->flags = 0;
1203178019Sjkim	if (bootverbose)
1204178019Sjkim		++verbose;
120541016Sdfr
1206178019Sjkim	device_set_desc(dev, "PS/2 Mouse");
120743105Sdfr
1208178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE)) {
1209178019Sjkim		printf("psm%d: unable to lock the controller.\n", unit);
1210178019Sjkim		if (bootverbose)
1211178019Sjkim			--verbose;
1212178019Sjkim		return (ENXIO);
1213178019Sjkim	}
121441016Sdfr
1215178019Sjkim	/*
1216178019Sjkim	 * NOTE: two bits in the command byte controls the operation of the
1217178019Sjkim	 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1218178019Sjkim	 * port interrupt (IRQ 12) enable bit (bit 2).
1219178019Sjkim	 */
122041016Sdfr
1221178019Sjkim	/* discard anything left after the keyboard initialization */
1222178019Sjkim	empty_both_buffers(sc->kbdc, 10);
122341016Sdfr
1224178019Sjkim	/* save the current command byte; it will be used later */
1225178019Sjkim	mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1226178019Sjkim	command_byte = get_controller_command_byte(sc->kbdc);
1227178019Sjkim	if (verbose)
1228178019Sjkim		printf("psm%d: current command byte:%04x\n", unit,
1229178019Sjkim		    command_byte);
1230178019Sjkim	if (command_byte == -1) {
1231178019Sjkim		/* CONTROLLER ERROR */
1232178019Sjkim		printf("psm%d: unable to get the current command byte value.\n",
1233178019Sjkim			unit);
1234178019Sjkim		endprobe(ENXIO);
1235178019Sjkim	}
123641016Sdfr
1237178019Sjkim	/*
1238178019Sjkim	 * disable the keyboard port while probing the aux port, which must be
1239178019Sjkim	 * enabled during this routine
1240178019Sjkim	 */
1241178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
124241016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1243178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1244178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1245178019Sjkim		/*
1246178019Sjkim		 * this is CONTROLLER ERROR; I don't know how to recover
1247178019Sjkim		 * from this error...
1248178019Sjkim		 */
1249178019Sjkim		restore_controller(sc->kbdc, command_byte);
1250178019Sjkim		printf("psm%d: unable to set the command byte.\n", unit);
1251178019Sjkim		endprobe(ENXIO);
1252178019Sjkim	}
1253178019Sjkim	write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
125441016Sdfr
125545789Speter	/*
1256178019Sjkim	 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1257178019Sjkim	 * port exists and is functioning. However, some controllers appears
1258178019Sjkim	 * to respond with zero even when the aux port doesn't exist. (It may
1259178019Sjkim	 * be that this is only the case when the controller DOES have the aux
1260178019Sjkim	 * port but the port is not wired on the motherboard.) The keyboard
1261178019Sjkim	 * controllers without the port, such as the original AT, are
1262215473Sjhb	 * supposed to return with an error code or simply time out. In any
1263178019Sjkim	 * case, we have to continue probing the port even when the controller
1264178019Sjkim	 * passes this test.
1265117302Smikeh	 *
1266178019Sjkim	 * XXX: some controllers erroneously return the error code 1, 2 or 3
1267215473Sjhb	 * when it has a perfectly functional aux port. We have to ignore
1268178019Sjkim	 * this error code. Even if the controller HAS error with the aux
1269178019Sjkim	 * port, it will be detected later...
1270178019Sjkim	 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
127145789Speter	 */
1272178019Sjkim	switch ((i = test_aux_port(sc->kbdc))) {
1273178019Sjkim	case 1:		/* ignore these errors */
1274178019Sjkim	case 2:
1275178019Sjkim	case 3:
1276178019Sjkim	case PSM_ACK:
1277178019Sjkim		if (verbose)
1278178019Sjkim			printf("psm%d: strange result for test aux port "
1279178019Sjkim			    "(%d).\n", unit, i);
1280178019Sjkim		/* FALLTHROUGH */
1281178019Sjkim	case 0:		/* no error */
1282178019Sjkim		break;
1283178019Sjkim	case -1:	/* time out */
1284178019Sjkim	default:	/* error */
1285178019Sjkim		recover_from_error(sc->kbdc);
1286178019Sjkim		if (sc->config & PSM_CONFIG_IGNPORTERROR)
1287178019Sjkim			break;
1288178019Sjkim		restore_controller(sc->kbdc, command_byte);
1289178019Sjkim		if (verbose)
1290178019Sjkim			printf("psm%d: the aux port is not functioning (%d).\n",
1291178019Sjkim			    unit, i);
1292178019Sjkim		endprobe(ENXIO);
129345789Speter	}
129445789Speter
1295178019Sjkim	if (sc->config & PSM_CONFIG_NORESET) {
1296178019Sjkim		/*
1297178019Sjkim		 * Don't try to reset the pointing device.  It may possibly be
1298215473Sjhb		 * left in an unknown state, though...
1299178019Sjkim		 */
1300178019Sjkim	} else {
1301178019Sjkim		/*
1302178019Sjkim		 * NOTE: some controllers appears to hang the `keyboard' when
1303178019Sjkim		 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1304178019Sjkim		 *
1305178019Sjkim		 * Attempt to reset the controller twice -- this helps
1306178019Sjkim		 * pierce through some KVM switches. The second reset
1307178019Sjkim		 * is non-fatal.
1308178019Sjkim		 */
1309178019Sjkim		if (!reset_aux_dev(sc->kbdc)) {
1310178019Sjkim			recover_from_error(sc->kbdc);
1311178019Sjkim			restore_controller(sc->kbdc, command_byte);
1312178019Sjkim			if (verbose)
1313178019Sjkim				printf("psm%d: failed to reset the aux "
1314178019Sjkim				    "device.\n", unit);
1315178019Sjkim			endprobe(ENXIO);
1316178019Sjkim		} else if (!reset_aux_dev(sc->kbdc)) {
1317178019Sjkim			recover_from_error(sc->kbdc);
1318178019Sjkim			if (verbose >= 2)
1319178019Sjkim				printf("psm%d: failed to reset the aux device "
1320178019Sjkim				    "(2).\n", unit);
1321178019Sjkim		}
1322178019Sjkim	}
132341016Sdfr
1324178019Sjkim	/*
1325215473Sjhb	 * both the aux port and the aux device are functioning, see if the
1326178019Sjkim	 * device can be enabled. NOTE: when enabled, the device will start
1327178019Sjkim	 * sending data; we shall immediately disable the device once we know
1328178019Sjkim	 * the device can be enabled.
1329178019Sjkim	 */
1330178019Sjkim	if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1331178019Sjkim		/* MOUSE ERROR */
1332178019Sjkim		recover_from_error(sc->kbdc);
1333178019Sjkim		restore_controller(sc->kbdc, command_byte);
1334178019Sjkim		if (verbose)
1335178019Sjkim			printf("psm%d: failed to enable the aux device.\n",
1336178019Sjkim			    unit);
1337178019Sjkim		endprobe(ENXIO);
1338178019Sjkim	}
133941016Sdfr
1340178019Sjkim	/* save the default values after reset */
1341178019Sjkim	if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1342178019Sjkim		sc->dflt_mode.rate = sc->mode.rate = stat[2];
1343178019Sjkim		sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1344178019Sjkim	} else {
1345178019Sjkim		sc->dflt_mode.rate = sc->mode.rate = -1;
1346178019Sjkim		sc->dflt_mode.resolution = sc->mode.resolution = -1;
1347178019Sjkim	}
134841016Sdfr
1349178019Sjkim	/* hardware information */
1350178019Sjkim	sc->hw.iftype = MOUSE_IF_PS2;
135141016Sdfr
1352178019Sjkim	/* verify the device is a mouse */
1353178019Sjkim	sc->hw.hwid = get_aux_id(sc->kbdc);
1354178019Sjkim	if (!is_a_mouse(sc->hw.hwid)) {
1355178019Sjkim		restore_controller(sc->kbdc, command_byte);
1356178019Sjkim		if (verbose)
1357178019Sjkim			printf("psm%d: unknown device type (%d).\n", unit,
1358178019Sjkim			    sc->hw.hwid);
1359178019Sjkim		endprobe(ENXIO);
1360178019Sjkim	}
1361178019Sjkim	switch (sc->hw.hwid) {
1362178019Sjkim	case PSM_BALLPOINT_ID:
1363178019Sjkim		sc->hw.type = MOUSE_TRACKBALL;
136445789Speter		break;
1365178019Sjkim	case PSM_MOUSE_ID:
1366178019Sjkim	case PSM_INTELLI_ID:
1367178019Sjkim	case PSM_EXPLORER_ID:
1368178019Sjkim	case PSM_4DMOUSE_ID:
1369178019Sjkim	case PSM_4DPLUS_ID:
1370178019Sjkim		sc->hw.type = MOUSE_MOUSE;
1371178019Sjkim		break;
1372178019Sjkim	default:
1373178019Sjkim		sc->hw.type = MOUSE_UNKNOWN;
1374178019Sjkim		break;
137541016Sdfr	}
137641016Sdfr
1377178019Sjkim	if (sc->config & PSM_CONFIG_NOIDPROBE) {
1378178019Sjkim		sc->hw.buttons = 2;
1379178019Sjkim		i = GENERIC_MOUSE_ENTRY;
1380178019Sjkim	} else {
1381178019Sjkim		/* # of buttons */
1382178019Sjkim		sc->hw.buttons = get_mouse_buttons(sc->kbdc);
138341016Sdfr
1384178019Sjkim		/* other parameters */
1385178019Sjkim		for (i = 0; vendortype[i].probefunc != NULL; ++i)
1386233580Sjkim			if ((*vendortype[i].probefunc)(sc->kbdc, sc)) {
1387178019Sjkim				if (verbose >= 2)
1388178019Sjkim					printf("psm%d: found %s\n", unit,
1389178019Sjkim					    model_name(vendortype[i].model));
1390178019Sjkim				break;
1391178019Sjkim			}
1392178019Sjkim	}
139341016Sdfr
1394178019Sjkim	sc->hw.model = vendortype[i].model;
1395178019Sjkim
1396178019Sjkim	sc->dflt_mode.level = PSM_LEVEL_BASE;
1397178019Sjkim	sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1398178019Sjkim	sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1399178019Sjkim	if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1400178019Sjkim		sc->dflt_mode.syncmask[0] = 0;
1401178019Sjkim	else
1402178019Sjkim		sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1403178019Sjkim	if (sc->config & PSM_CONFIG_FORCETAP)
1404178019Sjkim		sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1405178019Sjkim	sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
1406178019Sjkim	sc->mode = sc->dflt_mode;
1407178019Sjkim	sc->mode.packetsize = vendortype[i].packetsize;
1408178019Sjkim
1409178019Sjkim	/* set mouse parameters */
141048773Syokota#if 0
1411178019Sjkim	/*
1412178019Sjkim	 * A version of Logitech FirstMouse+ won't report wheel movement,
1413178019Sjkim	 * if SET_DEFAULTS is sent...  Don't use this command.
1414178019Sjkim	 * This fix was found by Takashi Nishida.
1415178019Sjkim	 */
1416178019Sjkim	i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1417178019Sjkim	if (verbose >= 2)
1418178019Sjkim		printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
141948773Syokota#endif
1420178019Sjkim	if (sc->config & PSM_CONFIG_RESOLUTION)
1421178019Sjkim		sc->mode.resolution =
1422178019Sjkim		    set_mouse_resolution(sc->kbdc,
1423178019Sjkim		    (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1424178019Sjkim	else if (sc->mode.resolution >= 0)
1425178019Sjkim		sc->mode.resolution =
1426178019Sjkim		    set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1427178019Sjkim	if (sc->mode.rate > 0)
1428178019Sjkim		sc->mode.rate =
1429178019Sjkim		    set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1430178019Sjkim	set_mouse_scaling(sc->kbdc, 1);
143141016Sdfr
1432178019Sjkim	/* Record sync on the next data packet we see. */
1433178019Sjkim	sc->flags |= PSM_NEED_SYNCBITS;
143441016Sdfr
1435178019Sjkim	/* just check the status of the mouse */
1436178019Sjkim	/*
1437178019Sjkim	 * NOTE: XXX there are some arcane controller/mouse combinations out
1438178019Sjkim	 * there, which hung the controller unless there is data transmission
1439178019Sjkim	 * after ACK from the mouse.
144041016Sdfr	 */
1441178019Sjkim	if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1442178019Sjkim		printf("psm%d: failed to get status.\n", unit);
1443178019Sjkim	else {
1444178019Sjkim		/*
1445178019Sjkim		 * When in its native mode, some mice operate with different
1446178019Sjkim		 * default parameters than in the PS/2 compatible mode.
1447178019Sjkim		 */
1448178019Sjkim		sc->dflt_mode.rate = sc->mode.rate = stat[2];
1449178019Sjkim		sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1450178019Sjkim	}
145141016Sdfr
1452178019Sjkim	/* disable the aux port for now... */
1453178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
145441016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1455178019Sjkim	    (command_byte & KBD_KBD_CONTROL_BITS) |
1456178019Sjkim	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1457178019Sjkim		/*
1458178019Sjkim		 * this is CONTROLLER ERROR; I don't know the proper way to
1459178019Sjkim		 * recover from this error...
1460178019Sjkim		 */
1461178019Sjkim		restore_controller(sc->kbdc, command_byte);
1462178019Sjkim		printf("psm%d: unable to set the command byte.\n", unit);
1463178019Sjkim		endprobe(ENXIO);
1464178019Sjkim	}
1465178019Sjkim
1466178019Sjkim	/* done */
1467178019Sjkim	kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1468178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1469178019Sjkim	return (0);
147041016Sdfr}
147141016Sdfr
147241016Sdfrstatic int
147341016Sdfrpsmattach(device_t dev)
147441016Sdfr{
1475178019Sjkim	int unit = device_get_unit(dev);
1476178019Sjkim	struct psm_softc *sc = device_get_softc(dev);
1477178019Sjkim	int error;
1478178019Sjkim	int rid;
147941016Sdfr
1480178019Sjkim	/* Setup initial state */
1481178019Sjkim	sc->state = PSM_VALID;
1482240211Sjhb	callout_init(&sc->callout, 0);
1483240211Sjhb	callout_init(&sc->softcallout, 0);
148441016Sdfr
1485178019Sjkim	/* Setup our interrupt handler */
1486178019Sjkim	rid = KBDC_RID_AUX;
1487216491Sjhb	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1488178019Sjkim	if (sc->intr == NULL)
1489178019Sjkim		return (ENXIO);
1490178019Sjkim	error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1491178019Sjkim	    &sc->ih);
1492178019Sjkim	if (error) {
1493178019Sjkim		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1494178019Sjkim		return (error);
1495178019Sjkim	}
149658230Syokota
1497178019Sjkim	/* Done */
1498212355Sed	sc->dev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "psm%d", unit);
1499212355Sed	sc->dev->si_drv1 = sc;
1500212355Sed	sc->bdev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "bpsm%d", unit);
1501212355Sed	sc->bdev->si_drv1 = sc;
150241016Sdfr
1503233580Sjkim	/* Some touchpad devices need full reinitialization after suspend. */
1504233580Sjkim	switch (sc->hw.model) {
1505233580Sjkim	case MOUSE_MODEL_SYNAPTICS:
1506233580Sjkim	case MOUSE_MODEL_GLIDEPOINT:
1507233580Sjkim	case MOUSE_MODEL_VERSAPAD:
1508233580Sjkim		sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1509233580Sjkim		break;
1510233580Sjkim	default:
1511248478Sjkim		if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
1512233580Sjkim			sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1513233580Sjkim		break;
1514233580Sjkim	}
1515233580Sjkim
1516178019Sjkim	if (!verbose)
1517178019Sjkim		printf("psm%d: model %s, device ID %d\n",
1518178019Sjkim		    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1519178019Sjkim	else {
1520178019Sjkim		printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1521178019Sjkim		    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1522178019Sjkim		    sc->hw.hwid >> 8, sc->hw.buttons);
1523178019Sjkim		printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1524178019Sjkim		    unit, sc->config, sc->flags, sc->mode.packetsize);
1525178019Sjkim		printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1526178019Sjkim		    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1527178019Sjkim	}
152841016Sdfr
1529178019Sjkim	if (bootverbose)
1530178019Sjkim		--verbose;
153141016Sdfr
1532178019Sjkim	return (0);
153341016Sdfr}
153441016Sdfr
153541016Sdfrstatic int
153658230Syokotapsmdetach(device_t dev)
153758230Syokota{
1538178019Sjkim	struct psm_softc *sc;
1539178019Sjkim	int rid;
154058230Syokota
1541178019Sjkim	sc = device_get_softc(dev);
1542178019Sjkim	if (sc->state & PSM_OPEN)
1543178019Sjkim		return (EBUSY);
154458230Syokota
1545178019Sjkim	rid = KBDC_RID_AUX;
1546178019Sjkim	bus_teardown_intr(dev, sc->intr, sc->ih);
1547178019Sjkim	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
154858230Syokota
1549178019Sjkim	destroy_dev(sc->dev);
1550178019Sjkim	destroy_dev(sc->bdev);
155158230Syokota
1552240211Sjhb	callout_drain(&sc->callout);
1553240211Sjhb	callout_drain(&sc->softcallout);
1554240211Sjhb
1555178019Sjkim	return (0);
155658230Syokota}
155758230Syokota
155858230Syokotastatic int
1559130585Sphkpsmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
156041016Sdfr{
1561178019Sjkim	struct psm_softc *sc;
1562178019Sjkim	int command_byte;
1563178019Sjkim	int err;
1564178019Sjkim	int s;
156541016Sdfr
1566178019Sjkim	/* Get device data */
1567212355Sed	sc = dev->si_drv1;
1568178019Sjkim	if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
1569178019Sjkim		/* the device is no longer valid/functioning */
1570178019Sjkim		return (ENXIO);
1571178019Sjkim	}
157241016Sdfr
1573178019Sjkim	/* Disallow multiple opens */
1574178019Sjkim	if (sc->state & PSM_OPEN)
1575178019Sjkim		return (EBUSY);
157641016Sdfr
1577212355Sed	device_busy(devclass_get_device(psm_devclass, sc->unit));
157841016Sdfr
1579178019Sjkim	/* Initialize state */
1580178019Sjkim	sc->mode.level = sc->dflt_mode.level;
1581178019Sjkim	sc->mode.protocol = sc->dflt_mode.protocol;
1582178019Sjkim	sc->watchdog = FALSE;
1583189870Srnoland	sc->async = NULL;
158441016Sdfr
1585178019Sjkim	/* flush the event queue */
1586178019Sjkim	sc->queue.count = 0;
1587178019Sjkim	sc->queue.head = 0;
1588178019Sjkim	sc->queue.tail = 0;
1589178019Sjkim	sc->status.flags = 0;
1590178019Sjkim	sc->status.button = 0;
1591178019Sjkim	sc->status.obutton = 0;
1592178019Sjkim	sc->status.dx = 0;
1593178019Sjkim	sc->status.dy = 0;
1594178019Sjkim	sc->status.dz = 0;
1595178019Sjkim	sc->button = 0;
1596178019Sjkim	sc->pqueue_start = 0;
1597178019Sjkim	sc->pqueue_end = 0;
159841016Sdfr
1599178019Sjkim	/* empty input buffer */
1600178019Sjkim	flushpackets(sc);
1601178019Sjkim	sc->syncerrors = 0;
1602178019Sjkim	sc->pkterrors = 0;
160341016Sdfr
1604178019Sjkim	/* don't let timeout routines in the keyboard driver to poll the kbdc */
1605178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
1606178019Sjkim		return (EIO);
160741016Sdfr
1608178019Sjkim	/* save the current controller command byte */
1609178019Sjkim	s = spltty();
1610178019Sjkim	command_byte = get_controller_command_byte(sc->kbdc);
161141016Sdfr
1612178019Sjkim	/* enable the aux port and temporalily disable the keyboard */
1613178019Sjkim	if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
161441016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1615178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1616178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1617178019Sjkim		/* CONTROLLER ERROR; do you know how to get out of this? */
1618178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
1619178019Sjkim		splx(s);
1620178019Sjkim		log(LOG_ERR,
1621212355Sed		    "psm%d: unable to set the command byte (psmopen).\n",
1622212355Sed		    sc->unit);
1623178019Sjkim		return (EIO);
1624178019Sjkim	}
1625178019Sjkim	/*
1626178019Sjkim	 * Now that the keyboard controller is told not to generate
1627178019Sjkim	 * the keyboard and mouse interrupts, call `splx()' to allow
1628178019Sjkim	 * the other tty interrupts. The clock interrupt may also occur,
1629178019Sjkim	 * but timeout routines will be blocked by the poll flag set
1630178019Sjkim	 * via `kbdc_lock()'
1631178019Sjkim	 */
163241016Sdfr	splx(s);
163341016Sdfr
1634178019Sjkim	/* enable the mouse device */
1635178019Sjkim	err = doopen(sc, command_byte);
1636178019Sjkim
1637178019Sjkim	/* done */
1638178019Sjkim	if (err == 0)
1639178019Sjkim		sc->state |= PSM_OPEN;
1640178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1641178019Sjkim	return (err);
164241016Sdfr}
164341016Sdfr
164441016Sdfrstatic int
1645130585Sphkpsmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
164641016Sdfr{
1647212355Sed	struct psm_softc *sc = dev->si_drv1;
1648178019Sjkim	int stat[3];
1649178019Sjkim	int command_byte;
1650178019Sjkim	int s;
165141016Sdfr
1652178019Sjkim	/* don't let timeout routines in the keyboard driver to poll the kbdc */
1653178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
1654178019Sjkim		return (EIO);
165541016Sdfr
1656178019Sjkim	/* save the current controller command byte */
1657178019Sjkim	s = spltty();
1658178019Sjkim	command_byte = get_controller_command_byte(sc->kbdc);
1659178019Sjkim	if (command_byte == -1) {
1660178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
1661178019Sjkim		splx(s);
1662178019Sjkim		return (EIO);
1663178019Sjkim	}
166441016Sdfr
1665178019Sjkim	/* disable the aux interrupt and temporalily disable the keyboard */
1666178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
166741016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1668178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1669178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1670178019Sjkim		log(LOG_ERR,
1671212355Sed		    "psm%d: failed to disable the aux int (psmclose).\n",
1672212355Sed		    sc->unit);
1673178019Sjkim		/* CONTROLLER ERROR;
1674178019Sjkim		 * NOTE: we shall force our way through. Because the only
1675178019Sjkim		 * ill effect we shall see is that we may not be able
1676178019Sjkim		 * to read ACK from the mouse, and it doesn't matter much
1677178019Sjkim		 * so long as the mouse will accept the DISABLE command.
1678178019Sjkim		 */
1679178019Sjkim	}
1680178019Sjkim	splx(s);
168141016Sdfr
1682178019Sjkim	/* stop the watchdog timer */
1683240211Sjhb	callout_stop(&sc->callout);
168458230Syokota
1685178019Sjkim	/* remove anything left in the output buffer */
1686178019Sjkim	empty_aux_buffer(sc->kbdc, 10);
168741016Sdfr
1688178019Sjkim	/* disable the aux device, port and interrupt */
1689178019Sjkim	if (sc->state & PSM_VALID) {
1690178019Sjkim		if (!disable_aux_dev(sc->kbdc)) {
1691178019Sjkim			/* MOUSE ERROR;
1692178019Sjkim			 * NOTE: we don't return (error) and continue,
1693178019Sjkim			 * pretending we have successfully disabled the device.
1694178019Sjkim			 * It's OK because the interrupt routine will discard
1695178019Sjkim			 * any data from the mouse hereafter.
1696178019Sjkim			 */
1697178019Sjkim			log(LOG_ERR,
1698178019Sjkim			    "psm%d: failed to disable the device (psmclose).\n",
1699212355Sed			    sc->unit);
1700178019Sjkim		}
170141016Sdfr
1702178019Sjkim		if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1703178019Sjkim			log(LOG_DEBUG,
1704212355Sed			    "psm%d: failed to get status (psmclose).\n",
1705212355Sed			    sc->unit);
1706178019Sjkim	}
170741016Sdfr
1708178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
170941016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1710178019Sjkim	    (command_byte & KBD_KBD_CONTROL_BITS) |
1711178019Sjkim	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1712178019Sjkim		/*
1713178019Sjkim		 * CONTROLLER ERROR;
1714178019Sjkim		 * we shall ignore this error; see the above comment.
1715178019Sjkim		 */
1716178019Sjkim		log(LOG_ERR,
1717178019Sjkim		    "psm%d: failed to disable the aux port (psmclose).\n",
1718212355Sed		    sc->unit);
1719178019Sjkim	}
172041016Sdfr
1721178019Sjkim	/* remove anything left in the output buffer */
1722178019Sjkim	empty_aux_buffer(sc->kbdc, 10);
172341016Sdfr
1724189870Srnoland	/* clean up and sigio requests */
1725189870Srnoland	if (sc->async != NULL) {
1726189870Srnoland		funsetown(&sc->async);
1727189870Srnoland		sc->async = NULL;
1728189870Srnoland	}
1729189870Srnoland
1730178019Sjkim	/* close is almost always successful */
1731178019Sjkim	sc->state &= ~PSM_OPEN;
1732178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1733212355Sed	device_unbusy(devclass_get_device(psm_devclass, sc->unit));
1734178019Sjkim	return (0);
173541016Sdfr}
173641016Sdfr
173741016Sdfrstatic int
1738178019Sjkimtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
1739178019Sjkim    u_char *buf)
174041016Sdfr{
1741178019Sjkim	static u_char butmapps2[8] = {
1742178019Sjkim		0,
1743178019Sjkim		MOUSE_PS2_BUTTON1DOWN,
1744178019Sjkim		MOUSE_PS2_BUTTON2DOWN,
1745178019Sjkim		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1746178019Sjkim		MOUSE_PS2_BUTTON3DOWN,
1747178019Sjkim		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1748178019Sjkim		MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1749178019Sjkim		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
1750178019Sjkim		    MOUSE_PS2_BUTTON3DOWN,
1751178019Sjkim	};
1752178019Sjkim	static u_char butmapmsc[8] = {
1753178019Sjkim		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
1754178019Sjkim		    MOUSE_MSC_BUTTON3UP,
1755178019Sjkim		MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1756178019Sjkim		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1757178019Sjkim		MOUSE_MSC_BUTTON3UP,
1758178019Sjkim		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1759178019Sjkim		MOUSE_MSC_BUTTON2UP,
1760178019Sjkim		MOUSE_MSC_BUTTON1UP,
1761178019Sjkim		0,
1762178019Sjkim	};
1763178019Sjkim	int mapped;
1764178019Sjkim	int i;
176541016Sdfr
1766178019Sjkim	if (sc->mode.level == PSM_LEVEL_BASE) {
1767178019Sjkim		mapped = status->button & ~MOUSE_BUTTON4DOWN;
1768178019Sjkim		if (status->button & MOUSE_BUTTON4DOWN)
1769178019Sjkim			mapped |= MOUSE_BUTTON1DOWN;
1770178019Sjkim		status->button = mapped;
1771178019Sjkim		buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1772178019Sjkim		i = imax(imin(status->dx, 255), -256);
1773178019Sjkim		if (i < 0)
1774178019Sjkim			buf[0] |= MOUSE_PS2_XNEG;
1775178019Sjkim		buf[1] = i;
1776178019Sjkim		i = imax(imin(status->dy, 255), -256);
1777178019Sjkim		if (i < 0)
1778178019Sjkim			buf[0] |= MOUSE_PS2_YNEG;
1779178019Sjkim		buf[2] = i;
1780178019Sjkim		return (MOUSE_PS2_PACKETSIZE);
1781178019Sjkim	} else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1782178019Sjkim		buf[0] = MOUSE_MSC_SYNC |
1783178019Sjkim		    butmapmsc[status->button & MOUSE_STDBUTTONS];
1784178019Sjkim		i = imax(imin(status->dx, 255), -256);
1785178019Sjkim		buf[1] = i >> 1;
1786178019Sjkim		buf[3] = i - buf[1];
1787178019Sjkim		i = imax(imin(status->dy, 255), -256);
1788178019Sjkim		buf[2] = i >> 1;
1789178019Sjkim		buf[4] = i - buf[2];
1790178019Sjkim		i = imax(imin(status->dz, 127), -128);
1791178019Sjkim		buf[5] = (i >> 1) & 0x7f;
1792178019Sjkim		buf[6] = (i - (i >> 1)) & 0x7f;
1793178019Sjkim		buf[7] = (~status->button >> 3) & 0x7f;
1794178019Sjkim		return (MOUSE_SYS_PACKETSIZE);
1795178019Sjkim	}
1796178019Sjkim	return (pb->inputbytes);
179741016Sdfr}
179841016Sdfr
179941016Sdfrstatic int
1800130585Sphkpsmread(struct cdev *dev, struct uio *uio, int flag)
180141016Sdfr{
1802212355Sed	struct psm_softc *sc = dev->si_drv1;
1803178019Sjkim	u_char buf[PSM_SMALLBUFSIZE];
1804178019Sjkim	int error = 0;
1805178019Sjkim	int s;
1806178019Sjkim	int l;
180741016Sdfr
1808178019Sjkim	if ((sc->state & PSM_VALID) == 0)
1809178019Sjkim		return (EIO);
181041016Sdfr
1811178019Sjkim	/* block until mouse activity occured */
1812178019Sjkim	s = spltty();
1813178019Sjkim	while (sc->queue.count <= 0) {
1814212355Sed		if (dev != sc->bdev) {
1815178019Sjkim			splx(s);
1816178019Sjkim			return (EWOULDBLOCK);
1817178019Sjkim		}
1818178019Sjkim		sc->state |= PSM_ASLP;
1819178019Sjkim		error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
1820178019Sjkim		sc->state &= ~PSM_ASLP;
1821178019Sjkim		if (error) {
1822178019Sjkim			splx(s);
1823178019Sjkim			return (error);
1824178019Sjkim		} else if ((sc->state & PSM_VALID) == 0) {
1825178019Sjkim			/* the device disappeared! */
1826178019Sjkim			splx(s);
1827178019Sjkim			return (EIO);
1828178019Sjkim		}
182941016Sdfr	}
1830178019Sjkim	splx(s);
183141016Sdfr
1832178019Sjkim	/* copy data to the user land */
1833178019Sjkim	while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1834178019Sjkim		s = spltty();
1835178019Sjkim		l = imin(sc->queue.count, uio->uio_resid);
1836178019Sjkim		if (l > sizeof(buf))
1837178019Sjkim			l = sizeof(buf);
1838178019Sjkim		if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1839178019Sjkim			bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1840178019Sjkim			    sizeof(sc->queue.buf) - sc->queue.head);
1841178019Sjkim			bcopy(&sc->queue.buf[0],
1842178019Sjkim			    &buf[sizeof(sc->queue.buf) - sc->queue.head],
1843178019Sjkim			    l - (sizeof(sc->queue.buf) - sc->queue.head));
1844178019Sjkim		} else
1845178019Sjkim			bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1846178019Sjkim		sc->queue.count -= l;
1847178019Sjkim		sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1848178019Sjkim		splx(s);
1849178019Sjkim		error = uiomove(buf, l, uio);
1850178019Sjkim		if (error)
1851178019Sjkim			break;
185241016Sdfr	}
185341016Sdfr
1854178019Sjkim	return (error);
185541016Sdfr}
185641016Sdfr
185741016Sdfrstatic int
185841016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
185941016Sdfr{
1860178019Sjkim	int s;
186141016Sdfr
1862178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
1863178019Sjkim		return (EIO);
186441016Sdfr
1865178019Sjkim	s = spltty();
1866178019Sjkim	*c = get_controller_command_byte(sc->kbdc);
1867178019Sjkim	if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
186841016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1869178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1870178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1871178019Sjkim		/* this is CONTROLLER ERROR */
1872178019Sjkim		splx(s);
1873178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
1874178019Sjkim		return (EIO);
1875178019Sjkim	}
1876178019Sjkim
1877178019Sjkim	/*
1878178019Sjkim	 * The device may be in the middle of status data transmission.
1879178019Sjkim	 * The transmission will be interrupted, thus, incomplete status
1880178019Sjkim	 * data must be discarded. Although the aux interrupt is disabled
1881178019Sjkim	 * at the keyboard controller level, at most one aux interrupt
1882178019Sjkim	 * may have already been pending and a data byte is in the
1883178019Sjkim	 * output buffer; throw it away. Note that the second argument
1884178019Sjkim	 * to `empty_aux_buffer()' is zero, so that the call will just
1885178019Sjkim	 * flush the internal queue.
1886178019Sjkim	 * `psmintr()' will be invoked after `splx()' if an interrupt is
1887178019Sjkim	 * pending; it will see no data and returns immediately.
1888178019Sjkim	 */
1889178019Sjkim	empty_aux_buffer(sc->kbdc, 0);		/* flush the queue */
1890178019Sjkim	read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
1891178019Sjkim	flushpackets(sc);
189241016Sdfr	splx(s);
189341016Sdfr
1894178019Sjkim	return (0);
189541016Sdfr}
189641016Sdfr
1897123442Salfredstatic void
1898123442Salfreddropqueue(struct psm_softc *sc)
1899123442Salfred{
1900123442Salfred
1901178019Sjkim	sc->queue.count = 0;
1902178019Sjkim	sc->queue.head = 0;
1903178019Sjkim	sc->queue.tail = 0;
1904123442Salfred	if ((sc->state & PSM_SOFTARMED) != 0) {
1905123442Salfred		sc->state &= ~PSM_SOFTARMED;
1906240211Sjhb		callout_stop(&sc->softcallout);
1907123442Salfred	}
1908123442Salfred	sc->pqueue_start = sc->pqueue_end;
1909123442Salfred}
1910123442Salfred
1911123442Salfredstatic void
1912123442Salfredflushpackets(struct psm_softc *sc)
1913123442Salfred{
1914123442Salfred
1915123442Salfred	dropqueue(sc);
1916123442Salfred	bzero(&sc->pqueue, sizeof(sc->pqueue));
1917123442Salfred}
1918123442Salfred
191941016Sdfrstatic int
192041016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
192141016Sdfr{
1922178019Sjkim	int error = 0;
192341016Sdfr
1924178019Sjkim	/*
1925178019Sjkim	 * We may have seen a part of status data during `set_mouse_XXX()'.
1926178019Sjkim	 * they have been queued; flush it.
1927178019Sjkim	 */
1928178019Sjkim	empty_aux_buffer(sc->kbdc, 0);
192941016Sdfr
1930178019Sjkim	/* restore ports and interrupt */
1931178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
1932178019Sjkim	    kbdc_get_device_mask(sc->kbdc),
193341016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1934178019Sjkim		/*
1935178019Sjkim		 * CONTROLLER ERROR; this is serious, we may have
1936178019Sjkim		 * been left with the inaccessible keyboard and
1937178019Sjkim		 * the disabled mouse interrupt.
1938178019Sjkim		 */
1939178019Sjkim		error = EIO;
1940178019Sjkim	}
194141016Sdfr
1942178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1943178019Sjkim	return (error);
194441016Sdfr}
194541016Sdfr
194641016Sdfrstatic int
1947178017Sjkimpsmwrite(struct cdev *dev, struct uio *uio, int flag)
1948178017Sjkim{
1949212355Sed	struct psm_softc *sc = dev->si_drv1;
1950178019Sjkim	u_char buf[PSM_SMALLBUFSIZE];
1951178019Sjkim	int error = 0, i, l;
1952178017Sjkim
1953178019Sjkim	if ((sc->state & PSM_VALID) == 0)
1954178019Sjkim		return (EIO);
1955178017Sjkim
1956178019Sjkim	if (sc->mode.level < PSM_LEVEL_NATIVE)
1957178019Sjkim		return (ENODEV);
1958178017Sjkim
1959178019Sjkim	/* copy data from the user land */
1960178019Sjkim	while (uio->uio_resid > 0) {
1961178019Sjkim		l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
1962178019Sjkim		error = uiomove(buf, l, uio);
1963178019Sjkim		if (error)
1964178019Sjkim			break;
1965178019Sjkim		for (i = 0; i < l; i++) {
1966178019Sjkim			VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
1967178019Sjkim			if (!write_aux_command(sc->kbdc, buf[i])) {
1968178019Sjkim				VLOG(2, (LOG_DEBUG,
1969178019Sjkim				    "psm: cmd 0x%x failed.\n", buf[i]));
1970178019Sjkim				return (reinitialize(sc, FALSE));
1971178019Sjkim			}
1972178019Sjkim		}
1973178017Sjkim	}
1974178017Sjkim
1975178019Sjkim	return (error);
1976178017Sjkim}
1977178017Sjkim
1978178017Sjkimstatic int
1979178019Sjkimpsmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
1980178019Sjkim    struct thread *td)
198141016Sdfr{
1982212355Sed	struct psm_softc *sc = dev->si_drv1;
1983178019Sjkim	mousemode_t mode;
1984178019Sjkim	mousestatus_t status;
198541016Sdfr#if (defined(MOUSE_GETVARS))
1986178019Sjkim	mousevar_t *var;
198741016Sdfr#endif
1988178019Sjkim	mousedata_t *data;
1989178019Sjkim	int stat[3];
1990178019Sjkim	int command_byte;
1991178019Sjkim	int error = 0;
1992178019Sjkim	int s;
199341016Sdfr
1994178019Sjkim	/* Perform IOCTL command */
1995178019Sjkim	switch (cmd) {
199641016Sdfr
1997178019Sjkim	case OLD_MOUSE_GETHWINFO:
1998178019Sjkim		s = spltty();
1999178019Sjkim		((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
2000178019Sjkim		((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
2001178019Sjkim		((old_mousehw_t *)addr)->type = sc->hw.type;
2002178019Sjkim		((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
2003178019Sjkim		splx(s);
2004178019Sjkim		break;
200541016Sdfr
2006178019Sjkim	case MOUSE_GETHWINFO:
2007178019Sjkim		s = spltty();
2008178019Sjkim		*(mousehw_t *)addr = sc->hw;
2009178019Sjkim		if (sc->mode.level == PSM_LEVEL_BASE)
2010178019Sjkim			((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
2011178019Sjkim		splx(s);
2012178019Sjkim		break;
201341016Sdfr
2014178019Sjkim	case MOUSE_SYN_GETHWINFO:
2015178019Sjkim		s = spltty();
2016233580Sjkim		if (sc->synhw.infoMajor >= 4)
2017178019Sjkim			*(synapticshw_t *)addr = sc->synhw;
2018178019Sjkim		else
2019178019Sjkim			error = EINVAL;
2020178019Sjkim		splx(s);
2021178019Sjkim		break;
2022132865Snjl
2023178019Sjkim	case OLD_MOUSE_GETMODE:
2024178019Sjkim		s = spltty();
2025178019Sjkim		switch (sc->mode.level) {
2026178019Sjkim		case PSM_LEVEL_BASE:
2027178019Sjkim			((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2028178019Sjkim			break;
2029178019Sjkim		case PSM_LEVEL_STANDARD:
2030178019Sjkim			((old_mousemode_t *)addr)->protocol =
2031178019Sjkim			    MOUSE_PROTO_SYSMOUSE;
2032178019Sjkim			break;
2033178019Sjkim		case PSM_LEVEL_NATIVE:
2034178019Sjkim			((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2035178019Sjkim			break;
2036178019Sjkim		}
2037178019Sjkim		((old_mousemode_t *)addr)->rate = sc->mode.rate;
2038178019Sjkim		((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2039178019Sjkim		((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2040178019Sjkim		splx(s);
2041178019Sjkim		break;
204241016Sdfr
2043178019Sjkim	case MOUSE_GETMODE:
2044178019Sjkim		s = spltty();
2045178019Sjkim		*(mousemode_t *)addr = sc->mode;
2046178019Sjkim		if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2047178019Sjkim			((mousemode_t *)addr)->syncmask[0] = 0;
2048178019Sjkim			((mousemode_t *)addr)->syncmask[1] = 0;
2049178019Sjkim		}
2050178019Sjkim		((mousemode_t *)addr)->resolution =
2051178019Sjkim			MOUSE_RES_LOW - sc->mode.resolution;
2052178019Sjkim		switch (sc->mode.level) {
2053178019Sjkim		case PSM_LEVEL_BASE:
2054178019Sjkim			((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2055178019Sjkim			((mousemode_t *)addr)->packetsize =
2056178019Sjkim			    MOUSE_PS2_PACKETSIZE;
2057178019Sjkim			break;
2058178019Sjkim		case PSM_LEVEL_STANDARD:
2059178019Sjkim			((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2060178019Sjkim			((mousemode_t *)addr)->packetsize =
2061178019Sjkim			    MOUSE_SYS_PACKETSIZE;
2062178019Sjkim			((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2063178019Sjkim			((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2064178019Sjkim			break;
2065178019Sjkim		case PSM_LEVEL_NATIVE:
2066178019Sjkim			/* FIXME: this isn't quite correct... XXX */
2067178019Sjkim			((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2068178019Sjkim			break;
2069178019Sjkim		}
2070178019Sjkim		splx(s);
2071178019Sjkim		break;
207241016Sdfr
2073178019Sjkim	case OLD_MOUSE_SETMODE:
2074178019Sjkim	case MOUSE_SETMODE:
2075178019Sjkim		if (cmd == OLD_MOUSE_SETMODE) {
2076178019Sjkim			mode.rate = ((old_mousemode_t *)addr)->rate;
2077178019Sjkim			/*
2078178019Sjkim			 * resolution  old I/F   new I/F
2079178019Sjkim			 * default        0         0
2080178019Sjkim			 * low            1        -2
2081178019Sjkim			 * medium low     2        -3
2082178019Sjkim			 * medium high    3        -4
2083178019Sjkim			 * high           4        -5
2084178019Sjkim			 */
2085178019Sjkim			if (((old_mousemode_t *)addr)->resolution > 0)
2086178019Sjkim				mode.resolution =
2087178019Sjkim				    -((old_mousemode_t *)addr)->resolution - 1;
2088178019Sjkim			else
2089178019Sjkim				mode.resolution = 0;
2090178019Sjkim			mode.accelfactor =
2091178019Sjkim			    ((old_mousemode_t *)addr)->accelfactor;
2092178019Sjkim			mode.level = -1;
2093178019Sjkim		} else
2094178019Sjkim			mode = *(mousemode_t *)addr;
209541016Sdfr
2096178019Sjkim		/* adjust and validate parameters. */
2097178019Sjkim		if (mode.rate > UCHAR_MAX)
2098178019Sjkim			return (EINVAL);
2099178019Sjkim		if (mode.rate == 0)
2100178019Sjkim			mode.rate = sc->dflt_mode.rate;
2101178019Sjkim		else if (mode.rate == -1)
2102178019Sjkim			/* don't change the current setting */
2103178019Sjkim			;
2104178019Sjkim		else if (mode.rate < 0)
2105178019Sjkim			return (EINVAL);
2106178019Sjkim		if (mode.resolution >= UCHAR_MAX)
2107178019Sjkim			return (EINVAL);
2108178019Sjkim		if (mode.resolution >= 200)
2109178019Sjkim			mode.resolution = MOUSE_RES_HIGH;
2110178019Sjkim		else if (mode.resolution >= 100)
2111178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMHIGH;
2112178019Sjkim		else if (mode.resolution >= 50)
2113178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMLOW;
2114178019Sjkim		else if (mode.resolution > 0)
2115178019Sjkim			mode.resolution = MOUSE_RES_LOW;
2116178019Sjkim		if (mode.resolution == MOUSE_RES_DEFAULT)
2117178019Sjkim			mode.resolution = sc->dflt_mode.resolution;
2118178019Sjkim		else if (mode.resolution == -1)
2119178019Sjkim			/* don't change the current setting */
2120178019Sjkim			;
2121178019Sjkim		else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2122178019Sjkim			mode.resolution = MOUSE_RES_LOW - mode.resolution;
2123178019Sjkim		if (mode.level == -1)
2124178019Sjkim			/* don't change the current setting */
2125178019Sjkim			mode.level = sc->mode.level;
2126178019Sjkim		else if ((mode.level < PSM_LEVEL_MIN) ||
2127178019Sjkim		    (mode.level > PSM_LEVEL_MAX))
2128178019Sjkim			return (EINVAL);
2129178019Sjkim		if (mode.accelfactor == -1)
2130178019Sjkim			/* don't change the current setting */
2131178019Sjkim			mode.accelfactor = sc->mode.accelfactor;
2132178019Sjkim		else if (mode.accelfactor < 0)
2133178019Sjkim			return (EINVAL);
213441016Sdfr
2135178019Sjkim		/* don't allow anybody to poll the keyboard controller */
2136178019Sjkim		error = block_mouse_data(sc, &command_byte);
2137178019Sjkim		if (error)
2138178019Sjkim			return (error);
213941016Sdfr
2140178019Sjkim		/* set mouse parameters */
2141178019Sjkim		if (mode.rate > 0)
2142178019Sjkim			mode.rate = set_mouse_sampling_rate(sc->kbdc,
2143178019Sjkim			    mode.rate);
2144178019Sjkim		if (mode.resolution >= 0)
2145178019Sjkim			mode.resolution =
2146178019Sjkim			    set_mouse_resolution(sc->kbdc, mode.resolution);
2147178019Sjkim		set_mouse_scaling(sc->kbdc, 1);
2148178019Sjkim		get_mouse_status(sc->kbdc, stat, 0, 3);
214941016Sdfr
2150178019Sjkim		s = spltty();
2151178019Sjkim		sc->mode.rate = mode.rate;
2152178019Sjkim		sc->mode.resolution = mode.resolution;
2153178019Sjkim		sc->mode.accelfactor = mode.accelfactor;
2154178019Sjkim		sc->mode.level = mode.level;
2155178019Sjkim		splx(s);
215641016Sdfr
2157178019Sjkim		unblock_mouse_data(sc, command_byte);
2158178019Sjkim		break;
215941016Sdfr
2160178019Sjkim	case MOUSE_GETLEVEL:
2161178019Sjkim		*(int *)addr = sc->mode.level;
2162178019Sjkim		break;
216341016Sdfr
2164178019Sjkim	case MOUSE_SETLEVEL:
2165178019Sjkim		if ((*(int *)addr < PSM_LEVEL_MIN) ||
2166178019Sjkim		    (*(int *)addr > PSM_LEVEL_MAX))
2167178019Sjkim			return (EINVAL);
2168178019Sjkim		sc->mode.level = *(int *)addr;
2169178019Sjkim		break;
217041016Sdfr
2171178019Sjkim	case MOUSE_GETSTATUS:
2172178019Sjkim		s = spltty();
2173178019Sjkim		status = sc->status;
2174178019Sjkim		sc->status.flags = 0;
2175178019Sjkim		sc->status.obutton = sc->status.button;
2176178019Sjkim		sc->status.button = 0;
2177178019Sjkim		sc->status.dx = 0;
2178178019Sjkim		sc->status.dy = 0;
2179178019Sjkim		sc->status.dz = 0;
2180178019Sjkim		splx(s);
2181178019Sjkim		*(mousestatus_t *)addr = status;
2182178019Sjkim		break;
218341016Sdfr
218441016Sdfr#if (defined(MOUSE_GETVARS))
2185178019Sjkim	case MOUSE_GETVARS:
2186178019Sjkim		var = (mousevar_t *)addr;
2187178019Sjkim		bzero(var, sizeof(*var));
2188178019Sjkim		s = spltty();
2189178019Sjkim		var->var[0] = MOUSE_VARS_PS2_SIG;
2190178019Sjkim		var->var[1] = sc->config;
2191178019Sjkim		var->var[2] = sc->flags;
2192178019Sjkim		splx(s);
2193178019Sjkim		break;
219441016Sdfr
2195178019Sjkim	case MOUSE_SETVARS:
2196178019Sjkim		return (ENODEV);
219741016Sdfr#endif /* MOUSE_GETVARS */
219841016Sdfr
2199178019Sjkim	case MOUSE_READSTATE:
2200178019Sjkim	case MOUSE_READDATA:
2201178019Sjkim		data = (mousedata_t *)addr;
2202178019Sjkim		if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2203178019Sjkim			return (EINVAL);
220441016Sdfr
2205178019Sjkim		error = block_mouse_data(sc, &command_byte);
2206178019Sjkim		if (error)
2207178019Sjkim			return (error);
2208178019Sjkim		if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2209178019Sjkim		    (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2210178019Sjkim			error = EIO;
2211178019Sjkim		unblock_mouse_data(sc, command_byte);
2212178019Sjkim		break;
221341016Sdfr
221441016Sdfr#if (defined(MOUSE_SETRESOLUTION))
2215178019Sjkim	case MOUSE_SETRESOLUTION:
2216178019Sjkim		mode.resolution = *(int *)addr;
2217178019Sjkim		if (mode.resolution >= UCHAR_MAX)
2218178019Sjkim			return (EINVAL);
2219178019Sjkim		else if (mode.resolution >= 200)
2220178019Sjkim			mode.resolution = MOUSE_RES_HIGH;
2221178019Sjkim		else if (mode.resolution >= 100)
2222178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMHIGH;
2223178019Sjkim		else if (mode.resolution >= 50)
2224178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMLOW;
2225178019Sjkim		else if (mode.resolution > 0)
2226178019Sjkim			mode.resolution = MOUSE_RES_LOW;
2227178019Sjkim		if (mode.resolution == MOUSE_RES_DEFAULT)
2228178019Sjkim			mode.resolution = sc->dflt_mode.resolution;
2229178019Sjkim		else if (mode.resolution == -1)
2230178019Sjkim			mode.resolution = sc->mode.resolution;
2231178019Sjkim		else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2232178019Sjkim			mode.resolution = MOUSE_RES_LOW - mode.resolution;
223341016Sdfr
2234178019Sjkim		error = block_mouse_data(sc, &command_byte);
2235178019Sjkim		if (error)
2236178019Sjkim			return (error);
2237178019Sjkim		sc->mode.resolution =
2238178019Sjkim		    set_mouse_resolution(sc->kbdc, mode.resolution);
2239178019Sjkim		if (sc->mode.resolution != mode.resolution)
2240178019Sjkim			error = EIO;
2241178019Sjkim		unblock_mouse_data(sc, command_byte);
2242178019Sjkim		break;
224341016Sdfr#endif /* MOUSE_SETRESOLUTION */
224441016Sdfr
224541016Sdfr#if (defined(MOUSE_SETRATE))
2246178019Sjkim	case MOUSE_SETRATE:
2247178019Sjkim		mode.rate = *(int *)addr;
2248178019Sjkim		if (mode.rate > UCHAR_MAX)
2249178019Sjkim			return (EINVAL);
2250178019Sjkim		if (mode.rate == 0)
2251178019Sjkim			mode.rate = sc->dflt_mode.rate;
2252178019Sjkim		else if (mode.rate < 0)
2253178019Sjkim			mode.rate = sc->mode.rate;
225441016Sdfr
2255178019Sjkim		error = block_mouse_data(sc, &command_byte);
2256178019Sjkim		if (error)
2257178019Sjkim			return (error);
2258178019Sjkim		sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2259178019Sjkim		if (sc->mode.rate != mode.rate)
2260178019Sjkim			error = EIO;
2261178019Sjkim		unblock_mouse_data(sc, command_byte);
2262178019Sjkim		break;
226341016Sdfr#endif /* MOUSE_SETRATE */
226441016Sdfr
226541016Sdfr#if (defined(MOUSE_SETSCALING))
2266178019Sjkim	case MOUSE_SETSCALING:
2267178019Sjkim		if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2268178019Sjkim			return (EINVAL);
226941016Sdfr
2270178019Sjkim		error = block_mouse_data(sc, &command_byte);
2271178019Sjkim		if (error)
2272178019Sjkim			return (error);
2273178019Sjkim		if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2274178019Sjkim			error = EIO;
2275178019Sjkim		unblock_mouse_data(sc, command_byte);
2276178019Sjkim		break;
227741016Sdfr#endif /* MOUSE_SETSCALING */
227841016Sdfr
227941016Sdfr#if (defined(MOUSE_GETHWID))
2280178019Sjkim	case MOUSE_GETHWID:
2281178019Sjkim		error = block_mouse_data(sc, &command_byte);
2282178019Sjkim		if (error)
2283178019Sjkim			return (error);
2284178019Sjkim		sc->hw.hwid &= ~0x00ff;
2285178019Sjkim		sc->hw.hwid |= get_aux_id(sc->kbdc);
2286178019Sjkim		*(int *)addr = sc->hw.hwid & 0x00ff;
2287178019Sjkim		unblock_mouse_data(sc, command_byte);
2288178019Sjkim		break;
228941016Sdfr#endif /* MOUSE_GETHWID */
229041016Sdfr
2291189870Srnoland	case FIONBIO:
2292189870Srnoland	case FIOASYNC:
2293189870Srnoland		break;
2294189870Srnoland	case FIOSETOWN:
2295189870Srnoland		error = fsetown(*(int *)addr, &sc->async);
2296189870Srnoland		break;
2297189870Srnoland	case FIOGETOWN:
2298189870Srnoland		*(int *) addr = fgetown(&sc->async);
2299189870Srnoland		break;
2300178019Sjkim	default:
2301178019Sjkim		return (ENOTTY);
2302178019Sjkim	}
230341016Sdfr
2304178019Sjkim	return (error);
230541016Sdfr}
230641016Sdfr
230741016Sdfrstatic void
230858230Syokotapsmtimeout(void *arg)
230958230Syokota{
2310178019Sjkim	struct psm_softc *sc;
2311178019Sjkim	int s;
231258230Syokota
2313178019Sjkim	sc = (struct psm_softc *)arg;
2314178019Sjkim	s = spltty();
2315178019Sjkim	if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2316178019Sjkim		VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2317178019Sjkim		psmintr(sc);
2318178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
2319178019Sjkim	}
2320178019Sjkim	sc->watchdog = TRUE;
2321178019Sjkim	splx(s);
2322240211Sjhb	callout_reset(&sc->callout, hz, psmtimeout, sc);
232358230Syokota}
232458230Syokota
2325139628Sphilip/* Add all sysctls under the debug.psm and hw.psm nodes */
2326227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2327227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2328123442Salfred
2329180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RW, &verbose, 0,
2330180818Strhodes    "Verbosity level");
2331123442Salfred
2332139628Sphilipstatic int psmhz = 20;
2333180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2334180818Strhodes    "Frequency of the softcallout (in hz)");
2335123442Salfredstatic int psmerrsecs = 2;
2336180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2337180818Strhodes    "Number of seconds during which packets will dropped after a sync error");
2338123442Salfredstatic int psmerrusecs = 0;
2339180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2340180818Strhodes    "Microseconds to add to psmerrsecs");
2341123442Salfredstatic int psmsecs = 0;
2342180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2343180818Strhodes    "Max number of seconds between soft interrupts");
2344123442Salfredstatic int psmusecs = 500000;
2345180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2346180818Strhodes    "Microseconds to add to psmsecs");
2347139628Sphilipstatic int pkterrthresh = 2;
2348180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2349180818Strhodes    "Number of error packets allowed before reinitializing the mouse");
2350123442Salfred
2351200674SdumbbellSYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RW, &tap_enabled, 0,
2352200674Sdumbbell    "Enable tap and drag gestures");
2353139628Sphilipstatic int tap_threshold = PSM_TAP_THRESHOLD;
2354180818StrhodesSYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2355180818Strhodes    "Button tap threshold");
2356139628Sphilipstatic int tap_timeout = PSM_TAP_TIMEOUT;
2357180818StrhodesSYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2358180818Strhodes    "Tap timeout for touchpads");
2359134405Sgibbs
236058230Syokotastatic void
236141016Sdfrpsmintr(void *arg)
236241016Sdfr{
2363178019Sjkim	struct psm_softc *sc = arg;
2364178019Sjkim	struct timeval now;
2365178019Sjkim	int c;
2366178019Sjkim	packetbuf_t *pb;
236741016Sdfr
2368123442Salfred
2369178019Sjkim	/* read until there is nothing to read */
2370178019Sjkim	while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2371178019Sjkim		pb = &sc->pqueue[sc->pqueue_end];
237284880Syokota
2373178019Sjkim		/* discard the byte if the device is not open */
2374178019Sjkim		if ((sc->state & PSM_OPEN) == 0)
2375178019Sjkim			continue;
237641016Sdfr
2377178019Sjkim		getmicrouptime(&now);
2378178019Sjkim		if ((pb->inputbytes > 0) &&
2379178019Sjkim		    timevalcmp(&now, &sc->inputtimeout, >)) {
2380178019Sjkim			VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2381178019Sjkim			    "resetting byte count\n"));
2382178019Sjkim			pb->inputbytes = 0;
2383178019Sjkim			sc->syncerrors = 0;
2384178019Sjkim			sc->pkterrors = 0;
2385178019Sjkim		}
2386178019Sjkim		sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2387178019Sjkim		sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2388178019Sjkim		timevaladd(&sc->inputtimeout, &now);
238941016Sdfr
2390178019Sjkim		pb->ipacket[pb->inputbytes++] = c;
2391178017Sjkim
2392178019Sjkim		if (sc->mode.level == PSM_LEVEL_NATIVE) {
2393178019Sjkim			VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2394178019Sjkim			sc->syncerrors = 0;
2395178019Sjkim			sc->pkterrors = 0;
2396178019Sjkim			goto next;
2397178019Sjkim		} else {
2398178019Sjkim			if (pb->inputbytes < sc->mode.packetsize)
2399178019Sjkim				continue;
240041016Sdfr
2401178019Sjkim			VLOG(4, (LOG_DEBUG,
2402178019Sjkim			    "psmintr: %02x %02x %02x %02x %02x %02x\n",
2403178019Sjkim			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2404178019Sjkim			    pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2405178019Sjkim		}
2406134405Sgibbs
2407178019Sjkim		c = pb->ipacket[0];
2408178019Sjkim
2409178019Sjkim		if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2410178019Sjkim			sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2411178019Sjkim			sc->flags &= ~PSM_NEED_SYNCBITS;
2412178019Sjkim			VLOG(2, (LOG_DEBUG,
2413178019Sjkim			    "psmintr: Sync bytes now %04x,%04x\n",
2414328485Sdumbbell			    sc->mode.syncmask[0], sc->mode.syncmask[1]));
2415178019Sjkim		} else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2416178019Sjkim			VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
2417178019Sjkim			    "(%04x != %04x) %d cmds since last error.\n",
2418178019Sjkim			    c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2419178019Sjkim			    sc->cmdcount - sc->lasterr));
2420178019Sjkim			sc->lasterr = sc->cmdcount;
2421178019Sjkim			/*
2422178019Sjkim			 * The sync byte test is a weak measure of packet
2423178019Sjkim			 * validity.  Conservatively discard any input yet
2424178019Sjkim			 * to be seen by userland when we detect a sync
2425178019Sjkim			 * error since there is a good chance some of
2426178019Sjkim			 * the queued packets have undetected errors.
2427178019Sjkim			 */
2428178019Sjkim			dropqueue(sc);
2429178019Sjkim			if (sc->syncerrors == 0)
2430178019Sjkim				sc->pkterrors++;
2431178019Sjkim			++sc->syncerrors;
2432178019Sjkim			sc->lastinputerr = now;
2433178019Sjkim			if (sc->syncerrors >= sc->mode.packetsize * 2 ||
2434178019Sjkim			    sc->pkterrors >= pkterrthresh) {
2435178019Sjkim				/*
2436178019Sjkim				 * If we've failed to find a single sync byte
2437178019Sjkim				 * in 2 packets worth of data, or we've seen
2438178019Sjkim				 * persistent packet errors during the
2439178019Sjkim				 * validation period, reinitialize the mouse
2440178019Sjkim				 * in hopes of returning it to the expected
2441178019Sjkim				 * mode.
2442178019Sjkim				 */
2443178019Sjkim				VLOG(3, (LOG_DEBUG,
2444178019Sjkim				    "psmintr: reset the mouse.\n"));
2445178019Sjkim				reinitialize(sc, TRUE);
2446178019Sjkim			} else if (sc->syncerrors == sc->mode.packetsize) {
2447178019Sjkim				/*
2448178019Sjkim				 * Try a soft reset after searching for a sync
2449178019Sjkim				 * byte through a packet length of bytes.
2450178019Sjkim				 */
2451178019Sjkim				VLOG(3, (LOG_DEBUG,
2452178019Sjkim				    "psmintr: re-enable the mouse.\n"));
2453178019Sjkim				pb->inputbytes = 0;
2454178019Sjkim				disable_aux_dev(sc->kbdc);
2455178019Sjkim				enable_aux_dev(sc->kbdc);
2456178019Sjkim			} else {
2457178019Sjkim				VLOG(3, (LOG_DEBUG,
2458178019Sjkim				    "psmintr: discard a byte (%d)\n",
2459178019Sjkim				    sc->syncerrors));
2460178019Sjkim				pb->inputbytes--;
2461178019Sjkim				bcopy(&pb->ipacket[1], &pb->ipacket[0],
2462178019Sjkim				    pb->inputbytes);
2463178019Sjkim			}
2464178019Sjkim			continue;
2465178019Sjkim		}
2466178019Sjkim
2467134405Sgibbs		/*
2468178019Sjkim		 * We have what appears to be a valid packet.
2469178019Sjkim		 * Reset the error counters.
2470134405Sgibbs		 */
2471178019Sjkim		sc->syncerrors = 0;
2472134405Sgibbs
2473134405Sgibbs		/*
2474178019Sjkim		 * Drop even good packets if they occur within a timeout
2475178019Sjkim		 * period of a sync error.  This allows the detection of
2476178019Sjkim		 * a change in the mouse's packet mode without exposing
2477178019Sjkim		 * erratic mouse behavior to the user.  Some KVMs forget
2478178019Sjkim		 * enhanced mouse modes during switch events.
2479134405Sgibbs		 */
2480178019Sjkim		if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
2481178019Sjkim		    &now)) {
2482178019Sjkim			pb->inputbytes = 0;
2483178019Sjkim			continue;
2484178019Sjkim		}
2485134405Sgibbs
2486178019Sjkim		/*
2487178019Sjkim		 * Now that we're out of the validation period, reset
2488178019Sjkim		 * the packet error count.
2489178019Sjkim		 */
2490178019Sjkim		sc->pkterrors = 0;
2491134405Sgibbs
2492178019Sjkim		sc->cmdcount++;
2493178019Sjkimnext:
2494178019Sjkim		if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2495178019Sjkim			sc->pqueue_end = 0;
2496178019Sjkim		/*
2497178019Sjkim		 * If we've filled the queue then call the softintr ourselves,
2498178019Sjkim		 * otherwise schedule the interrupt for later.
2499178019Sjkim		 */
2500178019Sjkim		if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2501178019Sjkim		    (sc->pqueue_end == sc->pqueue_start)) {
2502178019Sjkim			if ((sc->state & PSM_SOFTARMED) != 0) {
2503178019Sjkim				sc->state &= ~PSM_SOFTARMED;
2504240211Sjhb				callout_stop(&sc->softcallout);
2505178019Sjkim			}
2506178019Sjkim			psmsoftintr(arg);
2507178019Sjkim		} else if ((sc->state & PSM_SOFTARMED) == 0) {
2508178019Sjkim			sc->state |= PSM_SOFTARMED;
2509240211Sjhb			callout_reset(&sc->softcallout,
2510240211Sjhb			    psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg);
2511123442Salfred		}
2512123442Salfred	}
2513123442Salfred}
2514123442Salfred
2515123442Salfredstatic void
2516178019Sjkimproc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2517178019Sjkim    int *x, int *y, int *z)
2518123442Salfred{
2519123442Salfred
2520178019Sjkim	/*
2521240743Skevlo	 * PS2++ protocol packet
2522178019Sjkim	 *
2523178019Sjkim	 *          b7 b6 b5 b4 b3 b2 b1 b0
2524178019Sjkim	 * byte 1:  *  1  p3 p2 1  *  *  *
2525178019Sjkim	 * byte 2:  c1 c2 p1 p0 d1 d0 1  0
2526178019Sjkim	 *
2527178019Sjkim	 * p3-p0: packet type
2528178019Sjkim	 * c1, c2: c1 & c2 == 1, if p2 == 0
2529178019Sjkim	 *         c1 & c2 == 0, if p2 == 1
2530178019Sjkim	 *
2531178019Sjkim	 * packet type: 0 (device type)
2532178019Sjkim	 * See comments in enable_mmanplus() below.
2533178019Sjkim	 *
2534178019Sjkim	 * packet type: 1 (wheel data)
2535178019Sjkim	 *
2536178019Sjkim	 *          b7 b6 b5 b4 b3 b2 b1 b0
2537178019Sjkim	 * byte 3:  h  *  B5 B4 s  d2 d1 d0
2538178019Sjkim	 *
2539178019Sjkim	 * h: 1, if horizontal roller data
2540178019Sjkim	 *    0, if vertical roller data
2541178019Sjkim	 * B4, B5: button 4 and 5
2542178019Sjkim	 * s: sign bit
2543178019Sjkim	 * d2-d0: roller data
2544178019Sjkim	 *
2545178019Sjkim	 * packet type: 2 (reserved)
254641016Sdfr	 */
2547178019Sjkim	if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
2548178019Sjkim	    (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2549178019Sjkim		/*
2550178019Sjkim		 * the extended data packet encodes button
2551178019Sjkim		 * and wheel events
2552178019Sjkim		 */
2553123442Salfred		switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
255448778Syokota		case 1:
2555178019Sjkim			/* wheel data packet */
2556178019Sjkim			*x = *y = 0;
2557178019Sjkim			if (pb->ipacket[2] & 0x80) {
2558178019Sjkim				/* XXX horizontal roller count - ignore it */
2559178019Sjkim				;
2560178019Sjkim			} else {
2561178019Sjkim				/* vertical roller count */
2562178019Sjkim				*z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
2563178019Sjkim				    (pb->ipacket[2] & 0x0f) - 16 :
2564178019Sjkim				    (pb->ipacket[2] & 0x0f);
2565178019Sjkim			}
2566178019Sjkim			ms->button |= (pb->ipacket[2] &
2567178019Sjkim			    MOUSE_PS2PLUS_BUTTON4DOWN) ?
2568178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
2569178019Sjkim			ms->button |= (pb->ipacket[2] &
2570178019Sjkim			    MOUSE_PS2PLUS_BUTTON5DOWN) ?
2571178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
2572178019Sjkim			break;
257348778Syokota		case 2:
2574178019Sjkim			/*
2575178019Sjkim			 * this packet type is reserved by
2576178019Sjkim			 * Logitech...
2577178019Sjkim			 */
2578178019Sjkim			/*
2579178019Sjkim			 * IBM ScrollPoint Mouse uses this
2580178019Sjkim			 * packet type to encode both vertical
2581178019Sjkim			 * and horizontal scroll movement.
2582178019Sjkim			 */
2583178019Sjkim			*x = *y = 0;
2584178019Sjkim			/* horizontal count */
2585178019Sjkim			if (pb->ipacket[2] & 0x0f)
2586178019Sjkim				*z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
2587178019Sjkim				    -2 : 2;
2588178019Sjkim			/* vertical count */
2589178019Sjkim			if (pb->ipacket[2] & 0xf0)
2590178019Sjkim				*z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
2591178019Sjkim				    -1 : 1;
2592178019Sjkim			break;
259348778Syokota		case 0:
2594178019Sjkim			/* device type packet - shouldn't happen */
2595178019Sjkim			/* FALLTHROUGH */
259648778Syokota		default:
2597178019Sjkim			*x = *y = 0;
2598178019Sjkim			ms->button = ms->obutton;
2599178019Sjkim			VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
2600178019Sjkim			    "type %d: 0x%02x 0x%02x 0x%02x\n",
2601178019Sjkim			    MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2602178019Sjkim			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
2603178019Sjkim			break;
260448778Syokota		}
2605178019Sjkim	} else {
260641016Sdfr		/* preserve button states */
2607178019Sjkim		ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
2608178019Sjkim	}
2609178019Sjkim}
261041016Sdfr
2611178019Sjkimstatic int
2612178019Sjkimproc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2613178019Sjkim    int *x, int *y, int *z)
2614178019Sjkim{
2615178019Sjkim	static int touchpad_buttons;
2616178019Sjkim	static int guest_buttons;
2617183888Sdumbbell	int w, x0, y0;
261841016Sdfr
2619255153Sdumbbell	/* TouchPad PS/2 absolute mode message format with capFourButtons:
2620178019Sjkim	 *
2621178019Sjkim	 *  Bits:        7   6   5   4   3   2   1   0 (LSB)
2622178019Sjkim	 *  ------------------------------------------------
2623178019Sjkim	 *  ipacket[0]:  1   0  W3  W2   0  W1   R   L
2624178019Sjkim	 *  ipacket[1]: Yb  Ya  Y9  Y8  Xb  Xa  X9  X8
2625178019Sjkim	 *  ipacket[2]: Z7  Z6  Z5  Z4  Z3  Z2  Z1  Z0
2626255153Sdumbbell	 *  ipacket[3]:  1   1  Yc  Xc   0  W0 D^R U^L
2627178019Sjkim	 *  ipacket[4]: X7  X6  X5  X4  X3  X2  X1  X0
2628178019Sjkim	 *  ipacket[5]: Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
2629178019Sjkim	 *
2630178019Sjkim	 * Legend:
2631178019Sjkim	 *  L: left physical mouse button
2632178019Sjkim	 *  R: right physical mouse button
2633178019Sjkim	 *  D: down button
2634178019Sjkim	 *  U: up button
2635178019Sjkim	 *  W: "wrist" value
2636178019Sjkim	 *  X: x position
2637183888Sdumbbell	 *  Y: y position
2638178019Sjkim	 *  Z: pressure
2639178019Sjkim	 *
2640255153Sdumbbell	 * Without capFourButtons but with nExtendeButtons and/or capMiddle
2641255153Sdumbbell	 *
2642255153Sdumbbell	 *  Bits:        7   6   5   4      3      2      1      0 (LSB)
2643255153Sdumbbell	 *  ------------------------------------------------------
2644255153Sdumbbell	 *  ipacket[3]:  1   1  Yc  Xc      0     W0    E^R    M^L
2645255153Sdumbbell	 *  ipacket[4]: X7  X6  X5  X4  X3|b7  X2|b5  X1|b3  X0|b1
2646255153Sdumbbell	 *  ipacket[5]: Y7  Y6  Y5  Y4  Y3|b8  Y2|b6  Y1|b4  Y0|b2
2647255153Sdumbbell	 *
2648255153Sdumbbell	 * Legend:
2649255153Sdumbbell	 *  M: Middle physical mouse button
2650255153Sdumbbell	 *  E: Extended mouse buttons reported instead of low bits of X and Y
2651255153Sdumbbell	 *  b1-b8: Extended mouse buttons
2652255153Sdumbbell	 *    Only ((nExtendedButtons + 1) >> 1) bits are used in packet
2653255153Sdumbbell	 *    4 and 5, for reading X and Y value they should be zeroed.
2654255153Sdumbbell	 *
2655178019Sjkim	 * Absolute reportable limits:    0 - 6143.
2656178019Sjkim	 * Typical bezel limits:       1472 - 5472.
2657178019Sjkim	 * Typical edge marings:       1632 - 5312.
2658178019Sjkim	 *
2659178019Sjkim	 * w = 3 Passthrough Packet
2660178019Sjkim	 *
2661178019Sjkim	 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2662178019Sjkim	 */
266341016Sdfr
2664178019Sjkim	if (!synaptics_support)
2665178019Sjkim		return (0);
266641016Sdfr
2667178019Sjkim	/* Sanity check for out of sync packets. */
2668178019Sjkim	if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2669178019Sjkim	    (pb->ipacket[3] & 0xc8) != 0xc0)
2670178019Sjkim		return (-1);
267149965Syokota
2672183888Sdumbbell	*x = *y = 0;
267358230Syokota
2674183888Sdumbbell	/*
2675183888Sdumbbell	 * Pressure value.
2676183888Sdumbbell	 * Interpretation:
2677183888Sdumbbell	 *   z = 0      No finger contact
2678183888Sdumbbell	 *   z = 10     Finger hovering near the pad
2679183888Sdumbbell	 *   z = 30     Very light finger contact
2680183888Sdumbbell	 *   z = 80     Normal finger contact
2681183888Sdumbbell	 *   z = 110    Very heavy finger contact
2682183888Sdumbbell	 *   z = 200    Finger lying flat on pad surface
2683183888Sdumbbell	 *   z = 255    Maximum reportable Z
2684183888Sdumbbell	 */
2685178019Sjkim	*z = pb->ipacket[2];
268658230Syokota
2687183888Sdumbbell	/*
2688183888Sdumbbell	 * Finger width value
2689183888Sdumbbell	 * Interpretation:
2690183888Sdumbbell	 *   w = 0      Two finger on the pad (capMultiFinger needed)
2691183888Sdumbbell	 *   w = 1      Three or more fingers (capMultiFinger needed)
2692183888Sdumbbell	 *   w = 2      Pen (instead of finger) (capPen needed)
2693183888Sdumbbell	 *   w = 3      Reserved (passthrough?)
2694183888Sdumbbell	 *   w = 4-7    Finger of normal width (capPalmDetect needed)
2695183888Sdumbbell	 *   w = 8-14   Very wide finger or palm (capPalmDetect needed)
2696183888Sdumbbell	 *   w = 15     Maximum reportable width (capPalmDetect needed)
2697183888Sdumbbell	 */
2698183888Sdumbbell	/* XXX Is checking capExtended enough? */
2699178019Sjkim	if (sc->synhw.capExtended)
2700133301Sphilip		w = ((pb->ipacket[0] & 0x30) >> 2) |
2701133301Sphilip		    ((pb->ipacket[0] & 0x04) >> 1) |
2702133301Sphilip		    ((pb->ipacket[3] & 0x04) >> 2);
2703178019Sjkim	else {
2704183888Sdumbbell		/* Assume a finger of regular width. */
2705133301Sphilip		w = 4;
2706178019Sjkim	}
2707133301Sphilip
2708255153Sdumbbell	/*
2709255153Sdumbbell	 * Handle packets from the guest device. See:
2710255153Sdumbbell	 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
2711255153Sdumbbell	 */
2712178019Sjkim	if (w == 3 && sc->synhw.capPassthrough) {
2713178019Sjkim		*x = ((pb->ipacket[1] & 0x10) ?
2714133868Sphilip		    pb->ipacket[4] - 256 : pb->ipacket[4]);
2715178019Sjkim		*y = ((pb->ipacket[1] & 0x20) ?
2716133868Sphilip		    pb->ipacket[5] - 256 : pb->ipacket[5]);
2717178019Sjkim		*z = 0;
2718133868Sphilip
2719133868Sphilip		guest_buttons = 0;
2720133868Sphilip		if (pb->ipacket[1] & 0x01)
2721178019Sjkim			guest_buttons |= MOUSE_BUTTON1DOWN;
2722133868Sphilip		if (pb->ipacket[1] & 0x04)
2723178019Sjkim			guest_buttons |= MOUSE_BUTTON2DOWN;
2724133868Sphilip		if (pb->ipacket[1] & 0x02)
2725178019Sjkim			guest_buttons |= MOUSE_BUTTON3DOWN;
2726133868Sphilip
2727178019Sjkim		ms->button = touchpad_buttons | guest_buttons;
2728183888Sdumbbell		goto SYNAPTICS_END;
2729178019Sjkim	}
2730133868Sphilip
2731281709Srpaulo	if (sc->syninfo.touchpad_off) {
2732281709Srpaulo		*x = *y = *z = 0;
2733281709Srpaulo		ms->button = ms->obutton;
2734281709Srpaulo		goto SYNAPTICS_END;
2735281709Srpaulo	}
2736281709Srpaulo
2737178019Sjkim	/* Button presses */
2738178019Sjkim	touchpad_buttons = 0;
2739178019Sjkim	if (pb->ipacket[0] & 0x01)
2740178019Sjkim		touchpad_buttons |= MOUSE_BUTTON1DOWN;
2741178019Sjkim	if (pb->ipacket[0] & 0x02)
2742178019Sjkim		touchpad_buttons |= MOUSE_BUTTON3DOWN;
2743132865Snjl
2744178019Sjkim	if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2745255153Sdumbbell		if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
2746178019Sjkim			touchpad_buttons |= MOUSE_BUTTON4DOWN;
2747255153Sdumbbell		if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
2748178019Sjkim			touchpad_buttons |= MOUSE_BUTTON5DOWN;
2749281964Srpaulo	} else if (sc->synhw.capExtended && sc->synhw.capMiddle &&
2750281964Srpaulo	    !sc->synhw.capClickPad) {
2751255153Sdumbbell		/* Middle Button */
2752255153Sdumbbell		if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
2753255153Sdumbbell			touchpad_buttons |= MOUSE_BUTTON2DOWN;
2754255153Sdumbbell	} else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
2755255153Sdumbbell		/* Extended Buttons */
2756255153Sdumbbell		if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
2757255153Sdumbbell			if (sc->syninfo.directional_scrolls) {
2758255153Sdumbbell				if (pb->ipacket[4] & 0x01)
2759255153Sdumbbell					touchpad_buttons |= MOUSE_BUTTON4DOWN;
2760255153Sdumbbell				if (pb->ipacket[5] & 0x01)
2761255153Sdumbbell					touchpad_buttons |= MOUSE_BUTTON5DOWN;
2762255153Sdumbbell				if (pb->ipacket[4] & 0x02)
2763255153Sdumbbell					touchpad_buttons |= MOUSE_BUTTON6DOWN;
2764255153Sdumbbell				if (pb->ipacket[5] & 0x02)
2765255153Sdumbbell					touchpad_buttons |= MOUSE_BUTTON7DOWN;
2766255153Sdumbbell			} else {
2767281708Srpaulo				if (pb->ipacket[4] & 0x01)
2768281708Srpaulo					touchpad_buttons |= MOUSE_BUTTON1DOWN;
2769281708Srpaulo				if (pb->ipacket[5] & 0x01)
2770281708Srpaulo					touchpad_buttons |= MOUSE_BUTTON3DOWN;
2771281708Srpaulo				if (pb->ipacket[4] & 0x02)
2772281708Srpaulo					touchpad_buttons |= MOUSE_BUTTON2DOWN;
2773281708Srpaulo				sc->extended_buttons = touchpad_buttons;
2774255153Sdumbbell			}
2775132865Snjl
2776255153Sdumbbell			/*
2777255153Sdumbbell			 * Zero out bits used by extended buttons to avoid
2778255153Sdumbbell			 * misinterpretation of the data absolute position.
2779255153Sdumbbell			 *
2780255153Sdumbbell			 * The bits represented by
2781255153Sdumbbell			 *
2782255153Sdumbbell			 *     (nExtendedButtons + 1) >> 1
2783255153Sdumbbell			 *
2784255153Sdumbbell			 * will be masked out in both bytes.
2785255153Sdumbbell			 * The mask for n bits is computed with the formula
2786255153Sdumbbell			 *
2787255153Sdumbbell			 *     (1 << n) - 1
2788255153Sdumbbell			 */
2789255153Sdumbbell			int maskedbits = 0;
2790255153Sdumbbell			int mask = 0;
2791255153Sdumbbell			maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
2792255153Sdumbbell			mask = (1 << maskedbits) - 1;
2793255153Sdumbbell			pb->ipacket[4] &= ~(mask);
2794255153Sdumbbell			pb->ipacket[5] &= ~(mask);
2795281708Srpaulo		} else	if (!sc->syninfo.directional_scrolls &&
2796281708Srpaulo		    !sc->synaction.in_vscroll) {
2797281708Srpaulo			/*
2798281708Srpaulo			 * Keep reporting MOUSE DOWN until we get a new packet
2799281708Srpaulo			 * indicating otherwise.
2800281708Srpaulo			 */
2801281708Srpaulo			touchpad_buttons |= sc->extended_buttons;
2802139982Sphilip		}
2803178019Sjkim	}
2804281708Srpaulo	/* Handle ClickPad. */
2805281708Srpaulo	if (sc->synhw.capClickPad &&
2806281708Srpaulo	    ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01))
2807281708Srpaulo		touchpad_buttons |= MOUSE_BUTTON1DOWN;
2808139982Sphilip
2809178019Sjkim	ms->button = touchpad_buttons | guest_buttons;
2810139982Sphilip
2811281708Srpaulo	/*
2812281708Srpaulo	 * Check pressure to detect a real wanted action on the
2813281708Srpaulo	 * touchpad.
2814281708Srpaulo	 */
2815183888Sdumbbell	if (*z >= sc->syninfo.min_pressure) {
2816183888Sdumbbell		synapticsaction_t *synaction;
2817183888Sdumbbell		int cursor, peer, window;
2818183888Sdumbbell		int dx, dy, dxp, dyp;
2819183888Sdumbbell		int max_width, max_pressure;
2820183888Sdumbbell		int margin_top, margin_right, margin_bottom, margin_left;
2821183888Sdumbbell		int na_top, na_right, na_bottom, na_left;
2822183888Sdumbbell		int window_min, window_max;
2823183888Sdumbbell		int multiplicator;
2824183888Sdumbbell		int weight_current, weight_previous, weight_len_squared;
2825183888Sdumbbell		int div_min, div_max, div_len;
2826183888Sdumbbell		int vscroll_hor_area, vscroll_ver_area;
2827281708Srpaulo		int two_finger_scroll;
2828183888Sdumbbell		int len, weight_prev_x, weight_prev_y;
2829183888Sdumbbell		int div_max_x, div_max_y, div_x, div_y;
2830183888Sdumbbell
2831183888Sdumbbell		/* Read sysctl. */
2832183888Sdumbbell		/* XXX Verify values? */
2833183888Sdumbbell		max_width = sc->syninfo.max_width;
2834183888Sdumbbell		max_pressure = sc->syninfo.max_pressure;
2835183888Sdumbbell		margin_top = sc->syninfo.margin_top;
2836183888Sdumbbell		margin_right = sc->syninfo.margin_right;
2837183888Sdumbbell		margin_bottom = sc->syninfo.margin_bottom;
2838183888Sdumbbell		margin_left = sc->syninfo.margin_left;
2839183888Sdumbbell		na_top = sc->syninfo.na_top;
2840183888Sdumbbell		na_right = sc->syninfo.na_right;
2841183888Sdumbbell		na_bottom = sc->syninfo.na_bottom;
2842183888Sdumbbell		na_left = sc->syninfo.na_left;
2843183888Sdumbbell		window_min = sc->syninfo.window_min;
2844183888Sdumbbell		window_max = sc->syninfo.window_max;
2845183888Sdumbbell		multiplicator = sc->syninfo.multiplicator;
2846183888Sdumbbell		weight_current = sc->syninfo.weight_current;
2847183888Sdumbbell		weight_previous = sc->syninfo.weight_previous;
2848183888Sdumbbell		weight_len_squared = sc->syninfo.weight_len_squared;
2849183888Sdumbbell		div_min = sc->syninfo.div_min;
2850183888Sdumbbell		div_max = sc->syninfo.div_max;
2851183888Sdumbbell		div_len = sc->syninfo.div_len;
2852183888Sdumbbell		vscroll_hor_area = sc->syninfo.vscroll_hor_area;
2853183888Sdumbbell		vscroll_ver_area = sc->syninfo.vscroll_ver_area;
2854281708Srpaulo		two_finger_scroll = sc->syninfo.two_finger_scroll;
2855183888Sdumbbell
2856183888Sdumbbell		/* Palm detection. */
2857183888Sdumbbell		if (!(
2858183888Sdumbbell		    (sc->synhw.capMultiFinger && (w == 0 || w == 1)) ||
2859183888Sdumbbell		    (sc->synhw.capPalmDetect && w >= 4 && w <= max_width) ||
2860183888Sdumbbell		    (!sc->synhw.capPalmDetect && *z <= max_pressure) ||
2861183888Sdumbbell		    (sc->synhw.capPen && w == 2))) {
2862183888Sdumbbell			/*
2863183888Sdumbbell			 * We consider the packet irrelevant for the current
2864183888Sdumbbell			 * action when:
2865183888Sdumbbell			 *  - the width isn't comprised in:
2866183888Sdumbbell			 *    [4; max_width]
2867183888Sdumbbell			 *  - the pressure isn't comprised in:
2868183888Sdumbbell			 *    [min_pressure; max_pressure]
2869183888Sdumbbell			 *  - pen aren't supported but w is 2
2870183888Sdumbbell			 *
2871183888Sdumbbell			 *  Note that this doesn't terminate the current action.
2872183888Sdumbbell			 */
2873183888Sdumbbell			VLOG(2, (LOG_DEBUG,
2874183888Sdumbbell			    "synaptics: palm detected! (%d)\n", w));
2875183888Sdumbbell			goto SYNAPTICS_END;
2876183888Sdumbbell		}
2877183888Sdumbbell
2878183888Sdumbbell		/* Read current absolute position. */
2879132865Snjl		x0 = ((pb->ipacket[3] & 0x10) << 8) |
2880183888Sdumbbell		    ((pb->ipacket[1] & 0x0f) << 8) |
2881183888Sdumbbell		    pb->ipacket[4];
2882132865Snjl		y0 = ((pb->ipacket[3] & 0x20) << 7) |
2883183888Sdumbbell		    ((pb->ipacket[1] & 0xf0) << 4) |
2884183888Sdumbbell		    pb->ipacket[5];
2885132865Snjl
2886183888Sdumbbell		synaction = &(sc->synaction);
2887139982Sphilip
2888183888Sdumbbell		/*
2889183888Sdumbbell		 * If the action is just beginning, init the structure and
2890183888Sdumbbell		 * compute tap timeout.
2891183888Sdumbbell		 */
2892183888Sdumbbell		if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
2893183888Sdumbbell			VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
2894183888Sdumbbell
2895183888Sdumbbell			/* Store the first point of this action. */
2896183888Sdumbbell			synaction->start_x = x0;
2897183888Sdumbbell			synaction->start_y = y0;
2898183888Sdumbbell			dx = dy = 0;
2899183888Sdumbbell
2900183888Sdumbbell			/* Initialize queue. */
2901183888Sdumbbell			synaction->queue_cursor = SYNAPTICS_PACKETQUEUE;
2902183888Sdumbbell			synaction->queue_len = 0;
2903183888Sdumbbell			synaction->window_min = window_min;
2904183888Sdumbbell
2905183888Sdumbbell			/* Reset average. */
2906183888Sdumbbell			synaction->avg_dx = 0;
2907183888Sdumbbell			synaction->avg_dy = 0;
2908183888Sdumbbell
2909183888Sdumbbell			/* Reset squelch. */
2910183888Sdumbbell			synaction->squelch_x = 0;
2911183888Sdumbbell			synaction->squelch_y = 0;
2912183888Sdumbbell
2913183888Sdumbbell			/* Reset pressure peak. */
2914183888Sdumbbell			sc->zmax = 0;
2915183888Sdumbbell
2916183888Sdumbbell			/* Reset fingers count. */
2917183888Sdumbbell			synaction->fingers_nb = 0;
2918183888Sdumbbell
2919183888Sdumbbell			/* Reset virtual scrolling state. */
2920183888Sdumbbell			synaction->in_vscroll = 0;
2921183888Sdumbbell
2922183888Sdumbbell			/* Compute tap timeout. */
2923183888Sdumbbell			sc->taptimeout.tv_sec  = tap_timeout / 1000000;
2924183888Sdumbbell			sc->taptimeout.tv_usec = tap_timeout % 1000000;
2925183888Sdumbbell			timevaladd(&sc->taptimeout, &sc->lastsoftintr);
2926183888Sdumbbell
2927183888Sdumbbell			sc->flags |= PSM_FLAGS_FINGERDOWN;
2928183888Sdumbbell		} else {
2929183888Sdumbbell			/* Calculate the current delta. */
2930183888Sdumbbell			cursor = synaction->queue_cursor;
2931183888Sdumbbell			dx = x0 - synaction->queue[cursor].x;
2932183888Sdumbbell			dy = y0 - synaction->queue[cursor].y;
2933183888Sdumbbell		}
2934183888Sdumbbell
2935183888Sdumbbell		/* If in tap-hold, add the recorded button. */
2936183888Sdumbbell		if (synaction->in_taphold)
2937183888Sdumbbell			ms->button |= synaction->tap_button;
2938183888Sdumbbell
2939183888Sdumbbell		/*
2940183888Sdumbbell		 * From now on, we can use the SYNAPTICS_END label to skip
2941183888Sdumbbell		 * the current packet.
2942183888Sdumbbell		 */
2943183888Sdumbbell
2944183888Sdumbbell		/*
2945183888Sdumbbell		 * Limit the coordinates to the specified margins because
2946183888Sdumbbell		 * this area isn't very reliable.
2947183888Sdumbbell		 */
2948183888Sdumbbell		if (x0 <= margin_left)
2949183888Sdumbbell			x0 = margin_left;
2950183888Sdumbbell		else if (x0 >= 6143 - margin_right)
2951183888Sdumbbell			x0 = 6143 - margin_right;
2952183888Sdumbbell		if (y0 <= margin_bottom)
2953183888Sdumbbell			y0 = margin_bottom;
2954183888Sdumbbell		else if (y0 >= 6143 - margin_top)
2955183888Sdumbbell			y0 = 6143 - margin_top;
2956183888Sdumbbell
2957183888Sdumbbell		VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
2958183888Sdumbbell		    x0, y0, *z, w));
2959183888Sdumbbell
2960183888Sdumbbell		/* Queue this new packet. */
2961183888Sdumbbell		cursor = SYNAPTICS_QUEUE_CURSOR(synaction->queue_cursor - 1);
2962183888Sdumbbell		synaction->queue[cursor].x = x0;
2963183888Sdumbbell		synaction->queue[cursor].y = y0;
2964183888Sdumbbell		synaction->queue_cursor = cursor;
2965183888Sdumbbell		if (synaction->queue_len < SYNAPTICS_PACKETQUEUE)
2966183888Sdumbbell			synaction->queue_len++;
2967183888Sdumbbell		VLOG(5, (LOG_DEBUG,
2968183888Sdumbbell		    "synaptics: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
2969183888Sdumbbell		    cursor, x0, y0, dx, dy));
2970183888Sdumbbell
2971183888Sdumbbell		/*
2972183888Sdumbbell		 * For tap, we keep the maximum number of fingers and the
2973183888Sdumbbell		 * pressure peak. Also with multiple fingers, we increase
2974183888Sdumbbell		 * the minimum window.
2975183888Sdumbbell		 */
2976183888Sdumbbell		switch (w) {
2977183888Sdumbbell		case 1: /* Three or more fingers. */
2978183888Sdumbbell			synaction->fingers_nb = imax(3, synaction->fingers_nb);
2979183888Sdumbbell			synaction->window_min = window_max;
2980183888Sdumbbell			break;
2981183888Sdumbbell		case 0: /* Two fingers. */
2982183888Sdumbbell			synaction->fingers_nb = imax(2, synaction->fingers_nb);
2983183888Sdumbbell			synaction->window_min = window_max;
2984183888Sdumbbell			break;
2985183888Sdumbbell		default: /* One finger or undetectable. */
2986183888Sdumbbell			synaction->fingers_nb = imax(1, synaction->fingers_nb);
2987183888Sdumbbell		}
2988183888Sdumbbell		sc->zmax = imax(*z, sc->zmax);
2989183888Sdumbbell
2990183888Sdumbbell		/* Do we have enough packets to consider this a movement? */
2991183888Sdumbbell		if (synaction->queue_len < synaction->window_min)
2992183888Sdumbbell			goto SYNAPTICS_END;
2993183888Sdumbbell
2994183888Sdumbbell		/* Is a scrolling action occuring? */
2995183888Sdumbbell		if (!synaction->in_taphold && !synaction->in_vscroll) {
2996178019Sjkim			/*
2997183888Sdumbbell			 * A scrolling action must not conflict with a tap
2998183888Sdumbbell			 * action. Here are the conditions to consider a
2999183888Sdumbbell			 * scrolling action:
3000183888Sdumbbell			 *  - the action in a configurable area
3001183888Sdumbbell			 *  - one of the following:
3002183888Sdumbbell			 *     . the distance between the last packet and the
3003183888Sdumbbell			 *       first should be above a configurable minimum
3004183888Sdumbbell			 *     . tap timed out
3005178019Sjkim			 */
3006183888Sdumbbell			dxp = abs(synaction->queue[synaction->queue_cursor].x -
3007183888Sdumbbell			    synaction->start_x);
3008183888Sdumbbell			dyp = abs(synaction->queue[synaction->queue_cursor].y -
3009183888Sdumbbell			    synaction->start_y);
3010139982Sphilip
3011183888Sdumbbell			if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, >) ||
3012183888Sdumbbell			    dxp >= sc->syninfo.vscroll_min_delta ||
3013183888Sdumbbell			    dyp >= sc->syninfo.vscroll_min_delta) {
3014281708Srpaulo				/*
3015281708Srpaulo				 * Handle two finger scrolling.
3016281708Srpaulo				 * Note that we don't rely on fingers_nb
3017281708Srpaulo				 * as that keeps the maximum number of fingers.
3018281708Srpaulo				 */
3019281708Srpaulo				if (two_finger_scroll) {
3020281708Srpaulo					if (w == 0) {
3021281708Srpaulo						synaction->in_vscroll +=
3022281708Srpaulo						    dyp ? 2 : 0;
3023281708Srpaulo						synaction->in_vscroll +=
3024281708Srpaulo						    dxp ? 1 : 0;
3025281708Srpaulo					}
3026281708Srpaulo				} else {
3027281708Srpaulo					/* Check for horizontal scrolling. */
3028281708Srpaulo					if ((vscroll_hor_area > 0 &&
3029281708Srpaulo					    synaction->start_y <=
3030281708Srpaulo					        vscroll_hor_area) ||
3031281708Srpaulo					    (vscroll_hor_area < 0 &&
3032281708Srpaulo					     synaction->start_y >=
3033281708Srpaulo					     6143 + vscroll_hor_area))
3034281708Srpaulo						synaction->in_vscroll += 2;
3035139982Sphilip
3036281708Srpaulo					/* Check for vertical scrolling. */
3037281708Srpaulo					if ((vscroll_ver_area > 0 &&
3038281708Srpaulo					    synaction->start_x <=
3039281708Srpaulo						vscroll_ver_area) ||
3040281708Srpaulo					    (vscroll_ver_area < 0 &&
3041281708Srpaulo					     synaction->start_x >=
3042281708Srpaulo					     6143 + vscroll_ver_area))
3043281708Srpaulo						synaction->in_vscroll += 1;
3044281708Srpaulo				}
3045139982Sphilip
3046183888Sdumbbell				/* Avoid conflicts if area overlaps. */
3047281708Srpaulo				if (synaction->in_vscroll >= 3)
3048183888Sdumbbell					synaction->in_vscroll =
3049183888Sdumbbell					    (dxp > dyp) ? 2 : 1;
3050183888Sdumbbell			}
3051183888Sdumbbell		}
3052281708Srpaulo		/*
3053281708Srpaulo		 * Reset two finger scrolling when the number of fingers
3054281708Srpaulo		 * is different from two.
3055281708Srpaulo		 */
3056281708Srpaulo		if (two_finger_scroll && w != 0)
3057281708Srpaulo			synaction->in_vscroll = 0;
3058139982Sphilip
3059281708Srpaulo		VLOG(5, (LOG_DEBUG,
3060281708Srpaulo			"synaptics: virtual scrolling: %s "
3061281708Srpaulo			"(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n",
3062281708Srpaulo			synaction->in_vscroll ? "YES" : "NO",
3063281708Srpaulo			synaction->in_vscroll, dxp, dyp,
3064281708Srpaulo			synaction->fingers_nb));
3065281708Srpaulo
3066183888Sdumbbell		weight_prev_x = weight_prev_y = weight_previous;
3067183888Sdumbbell		div_max_x = div_max_y = div_max;
3068183888Sdumbbell
3069183888Sdumbbell		if (synaction->in_vscroll) {
3070183888Sdumbbell			/* Dividers are different with virtual scrolling. */
3071183888Sdumbbell			div_min = sc->syninfo.vscroll_div_min;
3072183888Sdumbbell			div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
3073183888Sdumbbell		} else {
3074178019Sjkim			/*
3075183888Sdumbbell			 * There's a lot of noise in coordinates when
3076183888Sdumbbell			 * the finger is on the touchpad's borders. When
3077183888Sdumbbell			 * using this area, we apply a special weight and
3078183888Sdumbbell			 * div.
3079178019Sjkim			 */
3080183888Sdumbbell			if (x0 <= na_left || x0 >= 6143 - na_right) {
3081183888Sdumbbell				weight_prev_x = sc->syninfo.weight_previous_na;
3082183888Sdumbbell				div_max_x = sc->syninfo.div_max_na;
3083183888Sdumbbell			}
3084139982Sphilip
3085183888Sdumbbell			if (y0 <= na_bottom || y0 >= 6143 - na_top) {
3086183888Sdumbbell				weight_prev_y = sc->syninfo.weight_previous_na;
3087183888Sdumbbell				div_max_y = sc->syninfo.div_max_na;
3088183888Sdumbbell			}
3089183888Sdumbbell		}
3090139982Sphilip
3091183888Sdumbbell		/*
3092183888Sdumbbell		 * Calculate weights for the average operands and
3093183888Sdumbbell		 * the divisor. Both depend on the distance between
3094183888Sdumbbell		 * the current packet and a previous one (based on the
3095183888Sdumbbell		 * window width).
3096183888Sdumbbell		 */
3097183888Sdumbbell		window = imin(synaction->queue_len, window_max);
3098183888Sdumbbell		peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
3099183888Sdumbbell		dxp = abs(x0 - synaction->queue[peer].x) + 1;
3100183888Sdumbbell		dyp = abs(y0 - synaction->queue[peer].y) + 1;
3101183888Sdumbbell		len = (dxp * dxp) + (dyp * dyp);
3102183888Sdumbbell		weight_prev_x = imin(weight_prev_x,
3103183888Sdumbbell		    weight_len_squared * weight_prev_x / len);
3104183888Sdumbbell		weight_prev_y = imin(weight_prev_y,
3105183888Sdumbbell		    weight_len_squared * weight_prev_y / len);
3106183888Sdumbbell
3107183888Sdumbbell		len = (dxp + dyp) / 2;
3108183888Sdumbbell		div_x = div_len * div_max_x / len;
3109183888Sdumbbell		div_x = imin(div_max_x, div_x);
3110183888Sdumbbell		div_x = imax(div_min, div_x);
3111183888Sdumbbell		div_y = div_len * div_max_y / len;
3112183888Sdumbbell		div_y = imin(div_max_y, div_y);
3113183888Sdumbbell		div_y = imax(div_min, div_y);
3114183888Sdumbbell
3115183888Sdumbbell		VLOG(3, (LOG_DEBUG,
3116183888Sdumbbell		    "synaptics: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
3117183888Sdumbbell		    peer, len, weight_prev_x, weight_prev_y, div_x, div_y));
3118183888Sdumbbell
3119183888Sdumbbell		/* Compute averages. */
3120183888Sdumbbell		synaction->avg_dx =
3121183888Sdumbbell		    (weight_current * dx * multiplicator +
3122183888Sdumbbell		     weight_prev_x * synaction->avg_dx) /
3123183888Sdumbbell		    (weight_current + weight_prev_x);
3124183888Sdumbbell
3125183888Sdumbbell		synaction->avg_dy =
3126183888Sdumbbell		    (weight_current * dy * multiplicator +
3127183888Sdumbbell		     weight_prev_y * synaction->avg_dy) /
3128183888Sdumbbell		    (weight_current + weight_prev_y);
3129183888Sdumbbell
3130183888Sdumbbell		VLOG(5, (LOG_DEBUG,
3131183888Sdumbbell		    "synaptics: avg_dx~=%d, avg_dy~=%d\n",
3132183888Sdumbbell		    synaction->avg_dx / multiplicator,
3133183888Sdumbbell		    synaction->avg_dy / multiplicator));
3134183888Sdumbbell
3135183888Sdumbbell		/* Use these averages to calculate x & y. */
3136183888Sdumbbell		synaction->squelch_x += synaction->avg_dx;
3137183888Sdumbbell		*x = synaction->squelch_x / (div_x * multiplicator);
3138183888Sdumbbell		synaction->squelch_x = synaction->squelch_x %
3139183888Sdumbbell		    (div_x * multiplicator);
3140183888Sdumbbell
3141183888Sdumbbell		synaction->squelch_y += synaction->avg_dy;
3142183888Sdumbbell		*y = synaction->squelch_y / (div_y * multiplicator);
3143183888Sdumbbell		synaction->squelch_y = synaction->squelch_y %
3144183888Sdumbbell		    (div_y * multiplicator);
3145183888Sdumbbell
3146183888Sdumbbell		if (synaction->in_vscroll) {
3147183888Sdumbbell			switch(synaction->in_vscroll) {
3148183888Sdumbbell			case 1: /* Vertical scrolling. */
3149183888Sdumbbell				if (*y != 0)
3150183888Sdumbbell					ms->button |= (*y > 0) ?
3151183888Sdumbbell					    MOUSE_BUTTON4DOWN :
3152183888Sdumbbell					    MOUSE_BUTTON5DOWN;
3153183888Sdumbbell				break;
3154183888Sdumbbell			case 2: /* Horizontal scrolling. */
3155183888Sdumbbell				if (*x != 0)
3156183888Sdumbbell					ms->button |= (*x > 0) ?
3157183888Sdumbbell					    MOUSE_BUTTON7DOWN :
3158183888Sdumbbell					    MOUSE_BUTTON6DOWN;
3159183888Sdumbbell				break;
3160183888Sdumbbell			}
3161183888Sdumbbell
3162183888Sdumbbell			/* The pointer is not moved. */
3163183888Sdumbbell			*x = *y = 0;
3164183888Sdumbbell		} else {
3165183888Sdumbbell			VLOG(3, (LOG_DEBUG, "synaptics: [%d, %d] -> [%d, %d]\n",
3166183888Sdumbbell			    dx, dy, *x, *y));
3167183888Sdumbbell		}
3168183888Sdumbbell	} else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3169183888Sdumbbell		/*
3170183888Sdumbbell		 * An action is currently taking place but the pressure
3171183888Sdumbbell		 * dropped under the minimum, putting an end to it.
3172183888Sdumbbell		 */
3173183888Sdumbbell		synapticsaction_t *synaction;
3174183888Sdumbbell		int taphold_timeout, dx, dy, tap_max_delta;
3175183888Sdumbbell
3176183888Sdumbbell		synaction = &(sc->synaction);
3177183888Sdumbbell		dx = abs(synaction->queue[synaction->queue_cursor].x -
3178183888Sdumbbell		    synaction->start_x);
3179183888Sdumbbell		dy = abs(synaction->queue[synaction->queue_cursor].y -
3180183888Sdumbbell		    synaction->start_y);
3181183888Sdumbbell
3182183888Sdumbbell		/* Max delta is disabled for multi-fingers tap. */
3183183888Sdumbbell		if (synaction->fingers_nb > 1)
3184183888Sdumbbell			tap_max_delta = imax(dx, dy);
3185183888Sdumbbell		else
3186183888Sdumbbell			tap_max_delta = sc->syninfo.tap_max_delta;
3187183888Sdumbbell
3188183888Sdumbbell		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3189183888Sdumbbell
3190183888Sdumbbell		/* Check for tap. */
3191183888Sdumbbell		VLOG(3, (LOG_DEBUG,
3192183888Sdumbbell		    "synaptics: zmax=%d, dx=%d, dy=%d, "
3193183888Sdumbbell		    "delta=%d, fingers=%d, queue=%d\n",
3194183888Sdumbbell		    sc->zmax, dx, dy, tap_max_delta, synaction->fingers_nb,
3195183888Sdumbbell		    synaction->queue_len));
3196183888Sdumbbell		if (!synaction->in_vscroll && sc->zmax >= tap_threshold &&
3197183888Sdumbbell		    timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=) &&
3198183888Sdumbbell		    dx <= tap_max_delta && dy <= tap_max_delta &&
3199183888Sdumbbell		    synaction->queue_len >= sc->syninfo.tap_min_queue) {
3200178019Sjkim			/*
3201183888Sdumbbell			 * We have a tap if:
3202183888Sdumbbell			 *   - the maximum pressure went over tap_threshold
3203183888Sdumbbell			 *   - the action ended before tap_timeout
3204183888Sdumbbell			 *
3205183888Sdumbbell			 * To handle tap-hold, we must delay any button push to
3206183888Sdumbbell			 * the next action.
3207178019Sjkim			 */
3208183888Sdumbbell			if (synaction->in_taphold) {
3209183888Sdumbbell				/*
3210183888Sdumbbell				 * This is the second and last tap of a
3211183888Sdumbbell				 * double tap action, not a tap-hold.
3212183888Sdumbbell				 */
3213183888Sdumbbell				synaction->in_taphold = 0;
3214139982Sphilip
3215183888Sdumbbell				/*
3216183888Sdumbbell				 * For double-tap to work:
3217183888Sdumbbell				 *   - no button press is emitted (to
3218183888Sdumbbell				 *     simulate a button release)
3219183888Sdumbbell				 *   - PSM_FLAGS_FINGERDOWN is set to
3220183888Sdumbbell				 *     force the next packet to emit a
3221183888Sdumbbell				 *     button press)
3222183888Sdumbbell				 */
3223183888Sdumbbell				VLOG(2, (LOG_DEBUG,
3224183888Sdumbbell				    "synaptics: button RELEASE: %d\n",
3225183888Sdumbbell				    synaction->tap_button));
3226183888Sdumbbell				sc->flags |= PSM_FLAGS_FINGERDOWN;
3227178019Sjkim			} else {
3228178019Sjkim				/*
3229183888Sdumbbell				 * This is the first tap: we set the
3230183888Sdumbbell				 * tap-hold state and notify the button
3231183888Sdumbbell				 * down event.
3232178019Sjkim				 */
3233183888Sdumbbell				synaction->in_taphold = 1;
3234183888Sdumbbell				taphold_timeout = sc->syninfo.taphold_timeout;
3235183888Sdumbbell				sc->taptimeout.tv_sec  = taphold_timeout /
3236183888Sdumbbell				    1000000;
3237183888Sdumbbell				sc->taptimeout.tv_usec = taphold_timeout %
3238183888Sdumbbell				    1000000;
3239183888Sdumbbell				timevaladd(&sc->taptimeout, &sc->lastsoftintr);
3240139982Sphilip
3241183888Sdumbbell				switch (synaction->fingers_nb) {
3242183888Sdumbbell				case 3:
3243183888Sdumbbell					synaction->tap_button =
3244183888Sdumbbell					    MOUSE_BUTTON2DOWN;
3245183888Sdumbbell					break;
3246183888Sdumbbell				case 2:
3247183888Sdumbbell					synaction->tap_button =
3248183888Sdumbbell					    MOUSE_BUTTON3DOWN;
3249183888Sdumbbell					break;
3250183888Sdumbbell				default:
3251183888Sdumbbell					synaction->tap_button =
3252183888Sdumbbell					    MOUSE_BUTTON1DOWN;
3253183888Sdumbbell				}
3254183888Sdumbbell				VLOG(2, (LOG_DEBUG,
3255183888Sdumbbell				    "synaptics: button PRESS: %d\n",
3256183888Sdumbbell				    synaction->tap_button));
3257183888Sdumbbell				ms->button |= synaction->tap_button;
3258178019Sjkim			}
3259183888Sdumbbell		} else {
3260183888Sdumbbell			/*
3261183888Sdumbbell			 * Not enough pressure or timeout: reset
3262183888Sdumbbell			 * tap-hold state.
3263183888Sdumbbell			 */
3264183888Sdumbbell			if (synaction->in_taphold) {
3265183888Sdumbbell				VLOG(2, (LOG_DEBUG,
3266183888Sdumbbell				    "synaptics: button RELEASE: %d\n",
3267183888Sdumbbell				    synaction->tap_button));
3268183888Sdumbbell				synaction->in_taphold = 0;
3269183888Sdumbbell			} else {
3270183888Sdumbbell				VLOG(2, (LOG_DEBUG,
3271183888Sdumbbell				    "synaptics: not a tap-hold\n"));
3272183888Sdumbbell			}
3273183888Sdumbbell		}
3274183888Sdumbbell	} else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) &&
3275183888Sdumbbell	    sc->synaction.in_taphold) {
3276178019Sjkim		/*
3277183888Sdumbbell		 * For a tap-hold to work, the button must remain down at
3278183888Sdumbbell		 * least until timeout (where the in_taphold flags will be
3279183888Sdumbbell		 * cleared) or during the next action.
3280139982Sphilip		 */
3281183888Sdumbbell		if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) {
3282183888Sdumbbell			ms->button |= sc->synaction.tap_button;
3283183888Sdumbbell		} else {
3284183888Sdumbbell			VLOG(2, (LOG_DEBUG,
3285183888Sdumbbell			    "synaptics: button RELEASE: %d\n",
3286183888Sdumbbell			    sc->synaction.tap_button));
3287183888Sdumbbell			sc->synaction.in_taphold = 0;
3288133297Sphilip		}
3289178019Sjkim	}
3290133296Sphilip
3291183888SdumbbellSYNAPTICS_END:
3292183888Sdumbbell	/*
3293183888Sdumbbell	 * Use the extra buttons as a scrollwheel
3294183888Sdumbbell	 *
3295183888Sdumbbell	 * XXX X.Org uses the Z axis for vertical wheel only,
3296183888Sdumbbell	 * whereas moused(8) understands special values to differ
3297183888Sdumbbell	 * vertical and horizontal wheels.
3298183888Sdumbbell	 *
3299183888Sdumbbell	 * xf86-input-mouse needs therefore a small patch to
3300183888Sdumbbell	 * understand these special values. Without it, the
3301183888Sdumbbell	 * horizontal wheel acts as a vertical wheel in X.Org.
3302183888Sdumbbell	 *
3303183888Sdumbbell	 * That's why the horizontal wheel is disabled by
3304183888Sdumbbell	 * default for now.
3305183888Sdumbbell	 */
3306281708Srpaulo
3307183888Sdumbbell	if (ms->button & MOUSE_BUTTON4DOWN) {
3308178019Sjkim		*z = -1;
3309183888Sdumbbell		ms->button &= ~MOUSE_BUTTON4DOWN;
3310183888Sdumbbell	} else if (ms->button & MOUSE_BUTTON5DOWN) {
3311178019Sjkim		*z = 1;
3312183888Sdumbbell		ms->button &= ~MOUSE_BUTTON5DOWN;
3313183888Sdumbbell	} else if (ms->button & MOUSE_BUTTON6DOWN) {
3314183888Sdumbbell		*z = -2;
3315183888Sdumbbell		ms->button &= ~MOUSE_BUTTON6DOWN;
3316183888Sdumbbell	} else if (ms->button & MOUSE_BUTTON7DOWN) {
3317183888Sdumbbell		*z = 2;
3318183888Sdumbbell		ms->button &= ~MOUSE_BUTTON7DOWN;
3319183888Sdumbbell	} else
3320178019Sjkim		*z = 0;
3321178019Sjkim
3322178019Sjkim	return (0);
3323178019Sjkim}
3324178019Sjkim
3325178019Sjkimstatic void
3326178019Sjkimproc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3327178019Sjkim    int *x, int *y, int *z)
3328178019Sjkim{
3329178019Sjkim	static int butmap_versapad[8] = {
3330178019Sjkim		0,
3331178019Sjkim		MOUSE_BUTTON3DOWN,
3332178019Sjkim		0,
3333178019Sjkim		MOUSE_BUTTON3DOWN,
3334178019Sjkim		MOUSE_BUTTON1DOWN,
3335178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3336178019Sjkim		MOUSE_BUTTON1DOWN,
3337178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
3338178019Sjkim	};
3339178019Sjkim	int c, x0, y0;
3340178019Sjkim
3341178019Sjkim	/* VersaPad PS/2 absolute mode message format
3342178019Sjkim	 *
3343178019Sjkim	 * [packet1]     7   6   5   4   3   2   1   0(LSB)
3344178019Sjkim	 *  ipacket[0]:  1   1   0   A   1   L   T   R
3345178019Sjkim	 *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
3346178019Sjkim	 *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
3347178019Sjkim	 *  ipacket[3]:  1   1   1   A   1   L   T   R
3348178019Sjkim	 *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
3349178019Sjkim	 *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
3350178019Sjkim	 *
3351178019Sjkim	 * [note]
3352178019Sjkim	 *  R: right physical mouse button (1=on)
3353178019Sjkim	 *  T: touch pad virtual button (1=tapping)
3354178019Sjkim	 *  L: left physical mouse button (1=on)
3355178019Sjkim	 *  A: position data is valid (1=valid)
3356178019Sjkim	 *  H: horizontal data (12bit signed integer. H11 is sign bit.)
3357178019Sjkim	 *  V: vertical data (12bit signed integer. V11 is sign bit.)
3358178019Sjkim	 *  P: pressure data
3359178019Sjkim	 *
3360178019Sjkim	 * Tapping is mapped to MOUSE_BUTTON4.
3361178019Sjkim	 */
3362178019Sjkim	c = pb->ipacket[0];
3363178019Sjkim	*x = *y = 0;
3364178019Sjkim	ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
3365178019Sjkim	ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
3366178019Sjkim	if (c & MOUSE_PS2VERSA_IN_USE) {
3367178019Sjkim		x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
3368178019Sjkim		y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
3369178019Sjkim		if (x0 & 0x800)
3370178019Sjkim			x0 -= 0x1000;
3371178019Sjkim		if (y0 & 0x800)
3372178019Sjkim			y0 -= 0x1000;
3373178019Sjkim		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3374178019Sjkim			*x = sc->xold - x0;
3375178019Sjkim			*y = y0 - sc->yold;
3376178019Sjkim			if (*x < 0)	/* XXX */
3377178019Sjkim				++*x;
3378178019Sjkim			else if (*x)
3379178019Sjkim				--*x;
3380178019Sjkim			if (*y < 0)
3381178019Sjkim				++*y;
3382178019Sjkim			else if (*y)
3383178019Sjkim				--*y;
3384178019Sjkim		} else
3385178019Sjkim			sc->flags |= PSM_FLAGS_FINGERDOWN;
3386178019Sjkim		sc->xold = x0;
3387178019Sjkim		sc->yold = y0;
3388178019Sjkim	} else
3389178019Sjkim		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3390178019Sjkim}
3391178019Sjkim
3392178019Sjkimstatic void
3393178019Sjkimpsmsoftintr(void *arg)
3394178019Sjkim{
3395178019Sjkim	/*
3396178019Sjkim	 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
3397178019Sjkim	 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
3398178019Sjkim	 */
3399178019Sjkim	static int butmap[8] = {
3400178019Sjkim		0,
3401178019Sjkim		MOUSE_BUTTON1DOWN,
3402178019Sjkim		MOUSE_BUTTON3DOWN,
3403178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3404178019Sjkim		MOUSE_BUTTON2DOWN,
3405178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
3406178019Sjkim		MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
3407178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
3408178019Sjkim	};
3409212355Sed	struct psm_softc *sc = arg;
3410178019Sjkim	mousestatus_t ms;
3411178019Sjkim	packetbuf_t *pb;
3412178019Sjkim	int x, y, z, c, l, s;
3413178019Sjkim
3414178019Sjkim	getmicrouptime(&sc->lastsoftintr);
3415178019Sjkim
3416178019Sjkim	s = spltty();
3417178019Sjkim
3418178019Sjkim	do {
3419178019Sjkim		pb = &sc->pqueue[sc->pqueue_start];
3420178019Sjkim
3421178019Sjkim		if (sc->mode.level == PSM_LEVEL_NATIVE)
3422178019Sjkim			goto next_native;
3423178019Sjkim
3424178019Sjkim		c = pb->ipacket[0];
3425178019Sjkim		/*
3426178019Sjkim		 * A kludge for Kensington device!
3427178019Sjkim		 * The MSB of the horizontal count appears to be stored in
3428178019Sjkim		 * a strange place.
3429178019Sjkim		 */
3430178019Sjkim		if (sc->hw.model == MOUSE_MODEL_THINK)
3431178019Sjkim			pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
3432178019Sjkim
3433178019Sjkim		/* ignore the overflow bits... */
3434178019Sjkim		x = (c & MOUSE_PS2_XNEG) ?
3435178019Sjkim		    pb->ipacket[1] - 256 : pb->ipacket[1];
3436178019Sjkim		y = (c & MOUSE_PS2_YNEG) ?
3437178019Sjkim		    pb->ipacket[2] - 256 : pb->ipacket[2];
3438133296Sphilip		z = 0;
3439178019Sjkim		ms.obutton = sc->button;	  /* previous button state */
3440178019Sjkim		ms.button = butmap[c & MOUSE_PS2_BUTTONS];
3441178019Sjkim		/* `tapping' action */
3442178019Sjkim		if (sc->config & PSM_CONFIG_FORCETAP)
3443178019Sjkim			ms.button |= ((c & MOUSE_PS2_TAP)) ?
3444178019Sjkim			    0 : MOUSE_BUTTON4DOWN;
3445133296Sphilip
3446178019Sjkim		switch (sc->hw.model) {
3447132865Snjl
3448178019Sjkim		case MOUSE_MODEL_EXPLORER:
3449178019Sjkim			/*
3450178019Sjkim			 *          b7 b6 b5 b4 b3 b2 b1 b0
3451178019Sjkim			 * byte 1:  oy ox sy sx 1  M  R  L
3452178019Sjkim			 * byte 2:  x  x  x  x  x  x  x  x
3453178019Sjkim			 * byte 3:  y  y  y  y  y  y  y  y
3454178019Sjkim			 * byte 4:  *  *  S2 S1 s  d2 d1 d0
3455178019Sjkim			 *
3456178019Sjkim			 * L, M, R, S1, S2: left, middle, right and side buttons
3457178019Sjkim			 * s: wheel data sign bit
3458178019Sjkim			 * d2-d0: wheel data
3459178019Sjkim			 */
3460178019Sjkim			z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
3461178019Sjkim			    (pb->ipacket[3] & 0x0f) - 16 :
3462178019Sjkim			    (pb->ipacket[3] & 0x0f);
3463178019Sjkim			ms.button |=
3464178019Sjkim			    (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
3465178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
3466178019Sjkim			ms.button |=
3467178019Sjkim			    (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
3468178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
3469178019Sjkim			break;
3470178019Sjkim
3471178019Sjkim		case MOUSE_MODEL_INTELLI:
3472178019Sjkim		case MOUSE_MODEL_NET:
3473178019Sjkim			/* wheel data is in the fourth byte */
3474178019Sjkim			z = (char)pb->ipacket[3];
3475178019Sjkim			/*
3476178019Sjkim			 * XXX some mice may send 7 when there is no Z movement?			 */
3477178019Sjkim			if ((z >= 7) || (z <= -7))
3478178019Sjkim				z = 0;
3479178019Sjkim			/* some compatible mice have additional buttons */
3480178019Sjkim			ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
3481178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
3482178019Sjkim			ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
3483178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
3484178019Sjkim			break;
3485178019Sjkim
3486178019Sjkim		case MOUSE_MODEL_MOUSEMANPLUS:
3487178019Sjkim			proc_mmanplus(sc, pb, &ms, &x, &y, &z);
3488178019Sjkim			break;
3489178019Sjkim
3490178019Sjkim		case MOUSE_MODEL_GLIDEPOINT:
3491178019Sjkim			/* `tapping' action */
3492178019Sjkim			ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
3493178019Sjkim			    MOUSE_BUTTON4DOWN;
3494178019Sjkim			break;
3495178019Sjkim
3496178019Sjkim		case MOUSE_MODEL_NETSCROLL:
3497178019Sjkim			/*
3498178019Sjkim			 * three addtional bytes encode buttons and
3499178019Sjkim			 * wheel events
3500178019Sjkim			 */
3501178019Sjkim			ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
3502178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
3503178019Sjkim			ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
3504178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
3505178019Sjkim			z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
3506178019Sjkim			    pb->ipacket[4] - 256 : pb->ipacket[4];
3507178019Sjkim			break;
3508178019Sjkim
3509178019Sjkim		case MOUSE_MODEL_THINK:
3510178019Sjkim			/* the fourth button state in the first byte */
3511178019Sjkim			ms.button |= (c & MOUSE_PS2_TAP) ?
3512178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
3513178019Sjkim			break;
3514178019Sjkim
3515178019Sjkim		case MOUSE_MODEL_VERSAPAD:
3516178019Sjkim			proc_versapad(sc, pb, &ms, &x, &y, &z);
3517178019Sjkim			c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
3518178019Sjkim			    ((y < 0) ? MOUSE_PS2_YNEG : 0);
3519178019Sjkim			break;
3520281708Srpaulo
3521178019Sjkim		case MOUSE_MODEL_4D:
3522178019Sjkim			/*
3523178019Sjkim			 *          b7 b6 b5 b4 b3 b2 b1 b0
3524178019Sjkim			 * byte 1:  s2 d2 s1 d1 1  M  R  L
3525178019Sjkim			 * byte 2:  sx x  x  x  x  x  x  x
3526178019Sjkim			 * byte 3:  sy y  y  y  y  y  y  y
3527178019Sjkim			 *
3528178019Sjkim			 * s1: wheel 1 direction
3529178019Sjkim			 * d1: wheel 1 data
3530178019Sjkim			 * s2: wheel 2 direction
3531178019Sjkim			 * d2: wheel 2 data
3532178019Sjkim			 */
3533178019Sjkim			x = (pb->ipacket[1] & 0x80) ?
3534178019Sjkim			    pb->ipacket[1] - 256 : pb->ipacket[1];
3535178019Sjkim			y = (pb->ipacket[2] & 0x80) ?
3536178019Sjkim			    pb->ipacket[2] - 256 : pb->ipacket[2];
3537178019Sjkim			switch (c & MOUSE_4D_WHEELBITS) {
3538178019Sjkim			case 0x10:
3539178019Sjkim				z = 1;
3540178019Sjkim				break;
3541178019Sjkim			case 0x30:
3542178019Sjkim				z = -1;
3543178019Sjkim				break;
3544178019Sjkim			case 0x40:	/* XXX 2nd wheel turning right */
3545178019Sjkim				z = 2;
3546178019Sjkim				break;
3547178019Sjkim			case 0xc0:	/* XXX 2nd wheel turning left */
3548178019Sjkim				z = -2;
3549178019Sjkim				break;
3550178019Sjkim			}
3551178019Sjkim			break;
3552178019Sjkim
3553178019Sjkim		case MOUSE_MODEL_4DPLUS:
3554178019Sjkim			if ((x < 16 - 256) && (y < 16 - 256)) {
3555178019Sjkim				/*
3556178019Sjkim				 *          b7 b6 b5 b4 b3 b2 b1 b0
3557178019Sjkim				 * byte 1:  0  0  1  1  1  M  R  L
3558178019Sjkim				 * byte 2:  0  0  0  0  1  0  0  0
3559178019Sjkim				 * byte 3:  0  0  0  0  S  s  d1 d0
3560178019Sjkim				 *
3561178019Sjkim				 * L, M, R, S: left, middle, right,
3562178019Sjkim				 *             and side buttons
3563178019Sjkim				 * s: wheel data sign bit
3564178019Sjkim				 * d1-d0: wheel data
3565178019Sjkim				 */
3566178019Sjkim				x = y = 0;
3567178019Sjkim				if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
3568178019Sjkim					ms.button |= MOUSE_BUTTON4DOWN;
3569178019Sjkim				z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
3570178019Sjkim				    ((pb->ipacket[2] & 0x07) - 8) :
3571178019Sjkim				    (pb->ipacket[2] & 0x07) ;
3572178019Sjkim			} else {
3573178019Sjkim				/* preserve previous button states */
3574178019Sjkim				ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
3575178019Sjkim			}
3576178019Sjkim			break;
3577178019Sjkim
3578178019Sjkim		case MOUSE_MODEL_SYNAPTICS:
3579329533Swulf			if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0) {
3580329533Swulf				VLOG(3, (LOG_DEBUG, "synaptics: "
3581329533Swulf				    "packet rejected\n"));
3582178019Sjkim				goto next;
3583329533Swulf			}
3584178019Sjkim			break;
3585178019Sjkim
3586248478Sjkim		case MOUSE_MODEL_TRACKPOINT:
3587178019Sjkim		case MOUSE_MODEL_GENERIC:
3588178019Sjkim		default:
3589178019Sjkim			break;
3590178019Sjkim		}
3591178019Sjkim
3592178019Sjkim	/* scale values */
3593178019Sjkim	if (sc->mode.accelfactor >= 1) {
3594178019Sjkim		if (x != 0) {
3595178019Sjkim			x = x * x / sc->mode.accelfactor;
3596178019Sjkim			if (x == 0)
3597178019Sjkim				x = 1;
3598178019Sjkim			if (c & MOUSE_PS2_XNEG)
3599178019Sjkim				x = -x;
3600178019Sjkim		}
3601178019Sjkim		if (y != 0) {
3602178019Sjkim			y = y * y / sc->mode.accelfactor;
3603178019Sjkim			if (y == 0)
3604178019Sjkim				y = 1;
3605178019Sjkim			if (c & MOUSE_PS2_YNEG)
3606178019Sjkim				y = -y;
3607178019Sjkim		}
360841016Sdfr	}
360941016Sdfr
3610178019Sjkim	ms.dx = x;
3611178019Sjkim	ms.dy = y;
3612178019Sjkim	ms.dz = z;
3613178019Sjkim	ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
3614178019Sjkim	    (ms.obutton ^ ms.button);
361541016Sdfr
3616178017Sjkim	pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
361741016Sdfr
3618178019Sjkim	sc->status.flags |= ms.flags;
3619178019Sjkim	sc->status.dx += ms.dx;
3620178019Sjkim	sc->status.dy += ms.dy;
3621178019Sjkim	sc->status.dz += ms.dz;
3622178019Sjkim	sc->status.button = ms.button;
3623178019Sjkim	sc->button = ms.button;
362441016Sdfr
3625178019Sjkimnext_native:
362658230Syokota	sc->watchdog = FALSE;
362758230Syokota
3628178019Sjkim	/* queue data */
3629178019Sjkim	if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
3630178019Sjkim		l = imin(pb->inputbytes,
3631178019Sjkim		    sizeof(sc->queue.buf) - sc->queue.tail);
3632178019Sjkim		bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
3633178019Sjkim		if (pb->inputbytes > l)
3634178019Sjkim			bcopy(&pb->ipacket[l], &sc->queue.buf[0],
3635178019Sjkim			    pb->inputbytes - l);
3636178019Sjkim		sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
3637178019Sjkim		    sizeof(sc->queue.buf);
3638178019Sjkim		sc->queue.count += pb->inputbytes;
363941016Sdfr	}
364041016Sdfr
3641178019Sjkimnext:
3642329533Swulf	pb->inputbytes = 0;
3643123442Salfred	if (++sc->pqueue_start >= PSM_PACKETQUEUE)
3644123442Salfred		sc->pqueue_start = 0;
3645178019Sjkim	} while (sc->pqueue_start != sc->pqueue_end);
3646178019Sjkim
3647178019Sjkim	if (sc->state & PSM_ASLP) {
3648178019Sjkim		sc->state &= ~PSM_ASLP;
3649178019Sjkim		wakeup(sc);
3650178019Sjkim	}
3651178019Sjkim	selwakeuppri(&sc->rsel, PZERO);
3652189870Srnoland	if (sc->async != NULL) {
3653189870Srnoland		pgsigio(&sc->async, SIGIO, 0);
3654189870Srnoland	}
3655178019Sjkim	sc->state &= ~PSM_SOFTARMED;
3656178019Sjkim	splx(s);
365741016Sdfr}
365841016Sdfr
365941016Sdfrstatic int
3660130585Sphkpsmpoll(struct cdev *dev, int events, struct thread *td)
366141016Sdfr{
3662212355Sed	struct psm_softc *sc = dev->si_drv1;
3663178019Sjkim	int s;
3664178019Sjkim	int revents = 0;
366541016Sdfr
3666178019Sjkim	/* Return true if a mouse event available */
3667178019Sjkim	s = spltty();
3668178019Sjkim	if (events & (POLLIN | POLLRDNORM)) {
3669178019Sjkim		if (sc->queue.count > 0)
3670178019Sjkim			revents |= events & (POLLIN | POLLRDNORM);
3671178019Sjkim		else
3672178019Sjkim			selrecord(td, &sc->rsel);
3673178019Sjkim	}
3674178019Sjkim	splx(s);
367541016Sdfr
3676178019Sjkim	return (revents);
367741016Sdfr}
367841016Sdfr
367941016Sdfr/* vendor/model specific routines */
368041016Sdfr
368141016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
368241016Sdfr{
3683178019Sjkim	if (set_mouse_resolution(kbdc, res) != res)
3684178019Sjkim		return (FALSE);
3685178019Sjkim	if (set_mouse_scaling(kbdc, scale) &&
3686178019Sjkim	    set_mouse_scaling(kbdc, scale) &&
3687178019Sjkim	    set_mouse_scaling(kbdc, scale) &&
3688178019Sjkim	    (get_mouse_status(kbdc, status, 0, 3) >= 3))
3689178019Sjkim		return (TRUE);
3690178019Sjkim	return (FALSE);
369141016Sdfr}
369241016Sdfr
3693178019Sjkimstatic int
369469438Syokotamouse_ext_command(KBDC kbdc, int command)
369569438Syokota{
3696178019Sjkim	int c;
369769438Syokota
3698178019Sjkim	c = (command >> 6) & 0x03;
3699178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
3700178019Sjkim		return (FALSE);
3701178019Sjkim	c = (command >> 4) & 0x03;
3702178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
3703178019Sjkim		return (FALSE);
3704178019Sjkim	c = (command >> 2) & 0x03;
3705178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
3706178019Sjkim		return (FALSE);
3707178019Sjkim	c = (command >> 0) & 0x03;
3708178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
3709178019Sjkim		return (FALSE);
3710178019Sjkim	return (TRUE);
371169438Syokota}
371269438Syokota
3713153072Sru#ifdef notyet
371441016Sdfr/* Logitech MouseMan Cordless II */
371541016Sdfrstatic int
3716233580Sjkimenable_lcordless(KDBC kbdc, struct psm_softc *sc)
371741016Sdfr{
3718178019Sjkim	int status[3];
3719178019Sjkim	int ch;
372041016Sdfr
3721233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status))
3722178019Sjkim		return (FALSE);
3723178019Sjkim	if (status[1] == PSMD_RES_HIGH)
3724178019Sjkim		return (FALSE);
3725178019Sjkim	ch = (status[0] & 0x07) - 1;	/* channel # */
3726178019Sjkim	if ((ch <= 0) || (ch > 4))
3727178019Sjkim		return (FALSE);
3728178019Sjkim	/*
3729178019Sjkim	 * status[1]: always one?
3730178019Sjkim	 * status[2]: battery status? (0-100)
3731178019Sjkim	 */
3732178019Sjkim	return (TRUE);
373341016Sdfr}
373441016Sdfr#endif /* notyet */
373541016Sdfr
373658230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
373741016Sdfrstatic int
3738233580Sjkimenable_groller(KBDC kbdc, struct psm_softc *sc)
373941016Sdfr{
3740178019Sjkim	int status[3];
374141016Sdfr
3742178019Sjkim	/*
3743178019Sjkim	 * The special sequence to enable the fourth button and the
3744178019Sjkim	 * roller. Immediately after this sequence check status bytes.
3745178019Sjkim	 * if the mouse is NetScroll, the second and the third bytes are
3746178019Sjkim	 * '3' and 'D'.
3747178019Sjkim	 */
374841016Sdfr
3749178019Sjkim	/*
3750178019Sjkim	 * If the mouse is an ordinary PS/2 mouse, the status bytes should
3751178019Sjkim	 * look like the following.
3752178019Sjkim	 *
3753178019Sjkim	 * byte 1 bit 7 always 0
3754178019Sjkim	 *        bit 6 stream mode (0)
3755178019Sjkim	 *        bit 5 disabled (0)
3756178019Sjkim	 *        bit 4 1:1 scaling (0)
3757178019Sjkim	 *        bit 3 always 0
3758178019Sjkim	 *        bit 0-2 button status
3759178019Sjkim	 * byte 2 resolution (PSMD_RES_HIGH)
3760178019Sjkim	 * byte 3 report rate (?)
3761178019Sjkim	 */
376241016Sdfr
3763233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
3764178019Sjkim		return (FALSE);
3765178019Sjkim	if ((status[1] != '3') || (status[2] != 'D'))
3766178019Sjkim		return (FALSE);
3767178019Sjkim	/* FIXME: SmartScroll Mouse has 5 buttons! XXX */
3768233580Sjkim	if (sc != NULL)
3769233580Sjkim		sc->hw.buttons = 4;
3770178019Sjkim	return (TRUE);
377141016Sdfr}
377241016Sdfr
377358230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
377441016Sdfrstatic int
3775233580Sjkimenable_gmouse(KBDC kbdc, struct psm_softc *sc)
377641016Sdfr{
3777178019Sjkim	int status[3];
377841016Sdfr
3779178019Sjkim	/*
3780178019Sjkim	 * The special sequence to enable the middle, "rubber" button.
3781178019Sjkim	 * Immediately after this sequence check status bytes.
3782178019Sjkim	 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
3783178019Sjkim	 * the second and the third bytes are '3' and 'U'.
3784178019Sjkim	 * NOTE: NetMouse reports that it has three buttons although it has
3785178019Sjkim	 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
3786178019Sjkim	 * say they have three buttons too and they do have a button on the
3787178019Sjkim	 * side...
3788178019Sjkim	 */
3789233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
3790178019Sjkim		return (FALSE);
3791178019Sjkim	if ((status[1] != '3') || (status[2] != 'U'))
3792178019Sjkim		return (FALSE);
3793178019Sjkim	return (TRUE);
379441016Sdfr}
379541016Sdfr
379641016Sdfr/* ALPS GlidePoint */
379741016Sdfrstatic int
3798233580Sjkimenable_aglide(KBDC kbdc, struct psm_softc *sc)
379941016Sdfr{
3800178019Sjkim	int status[3];
380141016Sdfr
3802178019Sjkim	/*
3803178019Sjkim	 * The special sequence to obtain ALPS GlidePoint specific
3804178019Sjkim	 * information. Immediately after this sequence, status bytes will
3805178019Sjkim	 * contain something interesting.
3806178019Sjkim	 * NOTE: ALPS produces several models of GlidePoint. Some of those
3807178019Sjkim	 * do not respond to this sequence, thus, cannot be detected this way.
3808178019Sjkim	 */
3809233580Sjkim	if (set_mouse_sampling_rate(kbdc, 100) != 100)
3810178019Sjkim		return (FALSE);
3811233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
3812178019Sjkim		return (FALSE);
3813178019Sjkim	if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
3814178019Sjkim		return (FALSE);
3815178019Sjkim	return (TRUE);
381641016Sdfr}
381741016Sdfr
381841016Sdfr/* Kensington ThinkingMouse/Trackball */
381941016Sdfrstatic int
3820233580Sjkimenable_kmouse(KBDC kbdc, struct psm_softc *sc)
382141016Sdfr{
3822178019Sjkim	static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
3823178019Sjkim	int status[3];
3824178019Sjkim	int id1;
3825178019Sjkim	int id2;
3826178019Sjkim	int i;
382741016Sdfr
3828178019Sjkim	id1 = get_aux_id(kbdc);
3829178019Sjkim	if (set_mouse_sampling_rate(kbdc, 10) != 10)
3830178019Sjkim		return (FALSE);
3831178019Sjkim	/*
3832178019Sjkim	 * The device is now in the native mode? It returns a different
3833178019Sjkim	 * ID value...
3834178019Sjkim	 */
3835178019Sjkim	id2 = get_aux_id(kbdc);
3836178019Sjkim	if ((id1 == id2) || (id2 != 2))
3837178019Sjkim		return (FALSE);
383841016Sdfr
3839178019Sjkim	if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
3840178019Sjkim		return (FALSE);
384141016Sdfr#if PSM_DEBUG >= 2
3842178019Sjkim	/* at this point, resolution is LOW, sampling rate is 10/sec */
3843178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) < 3)
3844178019Sjkim		return (FALSE);
384541016Sdfr#endif
384641016Sdfr
3847178019Sjkim	/*
3848178019Sjkim	 * The special sequence to enable the third and fourth buttons.
3849178019Sjkim	 * Otherwise they behave like the first and second buttons.
3850178019Sjkim	 */
3851178019Sjkim	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3852178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3853178019Sjkim			return (FALSE);
385441016Sdfr
3855178019Sjkim	/*
3856178019Sjkim	 * At this point, the device is using default resolution and
3857178019Sjkim	 * sampling rate for the native mode.
3858178019Sjkim	 */
3859178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) < 3)
3860178019Sjkim		return (FALSE);
3861178019Sjkim	if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
3862178019Sjkim		return (FALSE);
386341016Sdfr
3864178019Sjkim	/* the device appears be enabled by this sequence, diable it for now */
3865178019Sjkim	disable_aux_dev(kbdc);
3866178019Sjkim	empty_aux_buffer(kbdc, 5);
386741016Sdfr
3868178019Sjkim	return (TRUE);
386941016Sdfr}
387041016Sdfr
387158230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
387241016Sdfrstatic int
3873233580Sjkimenable_mmanplus(KBDC kbdc, struct psm_softc *sc)
387441016Sdfr{
3875178019Sjkim	int data[3];
387641016Sdfr
3877178019Sjkim	/* the special sequence to enable the fourth button and the roller. */
3878178019Sjkim	/*
3879178019Sjkim	 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
3880178019Sjkim	 * must be called exactly three times since the last RESET command
3881178019Sjkim	 * before this sequence. XXX
3882178019Sjkim	 */
3883178019Sjkim	if (!set_mouse_scaling(kbdc, 1))
3884178019Sjkim		return (FALSE);
3885178019Sjkim	if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
3886178019Sjkim		return (FALSE);
3887178019Sjkim	if (get_mouse_status(kbdc, data, 1, 3) < 3)
3888178019Sjkim		return (FALSE);
388941016Sdfr
3890178019Sjkim	/*
3891240743Skevlo	 * PS2++ protocol, packet type 0
3892178019Sjkim	 *
3893178019Sjkim	 *          b7 b6 b5 b4 b3 b2 b1 b0
3894178019Sjkim	 * byte 1:  *  1  p3 p2 1  *  *  *
3895178019Sjkim	 * byte 2:  1  1  p1 p0 m1 m0 1  0
3896178019Sjkim	 * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
3897178019Sjkim	 *
3898178019Sjkim	 * p3-p0: packet type: 0
3899178019Sjkim	 * m7-m0: model ID: MouseMan+:0x50,
3900178019Sjkim	 *		    FirstMouse+:0x51,
3901178019Sjkim	 *		    ScrollPoint:0x58...
3902178019Sjkim	 */
3903178019Sjkim	/* check constant bits */
3904178019Sjkim	if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
3905178019Sjkim		return (FALSE);
3906178019Sjkim	if ((data[1] & 0xc3) != 0xc2)
3907178019Sjkim		return (FALSE);
3908178019Sjkim	/* check d3-d0 in byte 2 */
3909178019Sjkim	if (!MOUSE_PS2PLUS_CHECKBITS(data))
3910178019Sjkim		return (FALSE);
3911178019Sjkim	/* check p3-p0 */
3912178019Sjkim	if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
3913178019Sjkim		return (FALSE);
391441016Sdfr
3915233580Sjkim	if (sc != NULL) {
3916233580Sjkim		sc->hw.hwid &= 0x00ff;
3917233580Sjkim		sc->hw.hwid |= data[2] << 8;	/* save model ID */
3918233580Sjkim	}
391948778Syokota
3920178019Sjkim	/*
3921178019Sjkim	 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
3922178019Sjkim	 * the wheel and the fourth button events are encoded in the
3923178019Sjkim	 * special data packet. The mouse may be put in the IntelliMouse mode
3924178019Sjkim	 * if it is initialized by the IntelliMouse's method.
3925178019Sjkim	 */
3926178019Sjkim	return (TRUE);
392741016Sdfr}
392841016Sdfr
392958230Syokota/* MS IntelliMouse Explorer */
393058230Syokotastatic int
3931233580Sjkimenable_msexplorer(KBDC kbdc, struct psm_softc *sc)
393258230Syokota{
3933178019Sjkim	static u_char rate0[] = { 200, 100, 80, };
3934178019Sjkim	static u_char rate1[] = { 200, 200, 80, };
3935178019Sjkim	int id;
3936178019Sjkim	int i;
393758230Syokota
3938178019Sjkim	/*
3939178019Sjkim	 * This is needed for at least A4Tech X-7xx mice - they do not go
3940178019Sjkim	 * straight to Explorer mode, but need to be set to Intelli mode
3941178019Sjkim	 * first.
3942178019Sjkim	 */
3943233580Sjkim	enable_msintelli(kbdc, sc);
3944176554Srink
3945178019Sjkim	/* the special sequence to enable the extra buttons and the roller. */
3946178019Sjkim	for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i)
3947178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
3948178019Sjkim			return (FALSE);
3949178019Sjkim	/* the device will give the genuine ID only after the above sequence */
3950178019Sjkim	id = get_aux_id(kbdc);
3951178019Sjkim	if (id != PSM_EXPLORER_ID)
3952178019Sjkim		return (FALSE);
395369438Syokota
3954233580Sjkim	if (sc != NULL) {
3955233580Sjkim		sc->hw.buttons = 5;	/* IntelliMouse Explorer XXX */
3956233580Sjkim		sc->hw.hwid = id;
3957233580Sjkim	}
395869438Syokota
3959178019Sjkim	/*
3960178019Sjkim	 * XXX: this is a kludge to fool some KVM switch products
3961178019Sjkim	 * which think they are clever enough to know the 4-byte IntelliMouse
3962178019Sjkim	 * protocol, and assume any other protocols use 3-byte packets.
3963178019Sjkim	 * They don't convey 4-byte data packets from the IntelliMouse Explorer
3964178019Sjkim	 * correctly to the host computer because of this!
3965178019Sjkim	 * The following sequence is actually IntelliMouse's "wake up"
3966178019Sjkim	 * sequence; it will make the KVM think the mouse is IntelliMouse
3967178019Sjkim	 * when it is in fact IntelliMouse Explorer.
3968178019Sjkim	 */
3969178019Sjkim	for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i)
3970178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
3971178019Sjkim			break;
3972233580Sjkim	get_aux_id(kbdc);
397358923Syokota
3974178019Sjkim	return (TRUE);
397558230Syokota}
397658230Syokota
397741016Sdfr/* MS IntelliMouse */
397841016Sdfrstatic int
3979233580Sjkimenable_msintelli(KBDC kbdc, struct psm_softc *sc)
398041016Sdfr{
3981178019Sjkim	/*
3982178019Sjkim	 * Logitech MouseMan+ and FirstMouse+ will also respond to this
3983178019Sjkim	 * probe routine and act like IntelliMouse.
3984178019Sjkim	 */
398541016Sdfr
3986178019Sjkim	static u_char rate[] = { 200, 100, 80, };
3987178019Sjkim	int id;
3988178019Sjkim	int i;
398941016Sdfr
3990178019Sjkim	/* the special sequence to enable the third button and the roller. */
3991178019Sjkim	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3992178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3993178019Sjkim			return (FALSE);
3994178019Sjkim	/* the device will give the genuine ID only after the above sequence */
3995178019Sjkim	id = get_aux_id(kbdc);
3996178019Sjkim	if (id != PSM_INTELLI_ID)
3997178019Sjkim		return (FALSE);
399841016Sdfr
3999233580Sjkim	if (sc != NULL) {
4000233580Sjkim		sc->hw.buttons = 3;
4001233580Sjkim		sc->hw.hwid = id;
4002233580Sjkim	}
400341016Sdfr
4004178019Sjkim	return (TRUE);
400541016Sdfr}
400641016Sdfr
400758230Syokota/* A4 Tech 4D Mouse */
400858230Syokotastatic int
4009233580Sjkimenable_4dmouse(KBDC kbdc, struct psm_softc *sc)
401058230Syokota{
4011178019Sjkim	/*
4012178019Sjkim	 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
4013178019Sjkim	 */
401458230Syokota
4015178019Sjkim	static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
4016178019Sjkim	int id;
4017178019Sjkim	int i;
401858230Syokota
4019178019Sjkim	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
4020178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4021178019Sjkim			return (FALSE);
4022178019Sjkim	id = get_aux_id(kbdc);
4023178019Sjkim	/*
4024178019Sjkim	 * WinEasy 4D, 4 Way Scroll 4D: 6
4025178019Sjkim	 * Cable-Free 4D: 8 (4DPLUS)
4026178019Sjkim	 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
4027178019Sjkim	 */
4028178019Sjkim	if (id != PSM_4DMOUSE_ID)
4029178019Sjkim		return (FALSE);
403058230Syokota
4031233580Sjkim	if (sc != NULL) {
4032233580Sjkim		sc->hw.buttons = 3;	/* XXX some 4D mice have 4? */
4033233580Sjkim		sc->hw.hwid = id;
4034233580Sjkim	}
403558230Syokota
4036178019Sjkim	return (TRUE);
403758230Syokota}
403858230Syokota
403958230Syokota/* A4 Tech 4D+ Mouse */
404058230Syokotastatic int
4041233580Sjkimenable_4dplus(KBDC kbdc, struct psm_softc *sc)
404258230Syokota{
4043178019Sjkim	/*
4044178019Sjkim	 * Newer wheel mice from A4 Tech seem to use this protocol.
4045178019Sjkim	 * Older models are recognized as either 4D Mouse or IntelliMouse.
4046178019Sjkim	 */
4047178019Sjkim	int id;
404858230Syokota
4049178019Sjkim	/*
4050178019Sjkim	 * enable_4dmouse() already issued the following ID sequence...
4051178019Sjkim	static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
4052178019Sjkim	int i;
405358230Syokota
4054178019Sjkim	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
4055178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4056178019Sjkim			return (FALSE);
4057178019Sjkim	*/
405858230Syokota
4059178019Sjkim	id = get_aux_id(kbdc);
4060178019Sjkim	switch (id) {
4061178019Sjkim	case PSM_4DPLUS_ID:
4062178019Sjkim		break;
4063178019Sjkim	case PSM_4DPLUS_RFSW35_ID:
4064178019Sjkim		break;
4065178019Sjkim	default:
4066178019Sjkim		return (FALSE);
4067178019Sjkim	}
406858230Syokota
4069233580Sjkim	if (sc != NULL) {
4070233580Sjkim		sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
4071233580Sjkim		sc->hw.hwid = id;
4072233580Sjkim	}
407358230Syokota
4074178019Sjkim	return (TRUE);
407558230Syokota}
407658230Syokota
4077132865Snjl/* Synaptics Touchpad */
4078132865Snjlstatic int
4079183888Sdumbbellsynaptics_sysctl(SYSCTL_HANDLER_ARGS)
4080132865Snjl{
4081183888Sdumbbell	int error, arg;
4082132865Snjl
4083183888Sdumbbell	/* Read the current value. */
4084183888Sdumbbell	arg = *(int *)oidp->oid_arg1;
4085183888Sdumbbell	error = sysctl_handle_int(oidp, &arg, 0, req);
4086135945Sphilip
4087183888Sdumbbell	/* Sanity check. */
4088183888Sdumbbell	if (error || !req->newptr)
4089183888Sdumbbell		return (error);
4090183888Sdumbbell
4091183888Sdumbbell	/*
4092183888Sdumbbell	 * Check that the new value is in the concerned node's range
4093183888Sdumbbell	 * of values.
4094183888Sdumbbell	 */
4095183888Sdumbbell	switch (oidp->oid_arg2) {
4096183888Sdumbbell	case SYNAPTICS_SYSCTL_MIN_PRESSURE:
4097183888Sdumbbell	case SYNAPTICS_SYSCTL_MAX_PRESSURE:
4098183888Sdumbbell		if (arg < 0 || arg > 255)
4099183888Sdumbbell			return (EINVAL);
4100183888Sdumbbell		break;
4101183888Sdumbbell	case SYNAPTICS_SYSCTL_MAX_WIDTH:
4102183888Sdumbbell		if (arg < 4 || arg > 15)
4103183888Sdumbbell			return (EINVAL);
4104183888Sdumbbell		break;
4105183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_TOP:
4106183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
4107183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
4108183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_LEFT:
4109183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_TOP:
4110183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_RIGHT:
4111183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_BOTTOM:
4112183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_LEFT:
4113183888Sdumbbell		if (arg < 0 || arg > 6143)
4114183888Sdumbbell			return (EINVAL);
4115183888Sdumbbell		break;
4116183888Sdumbbell	case SYNAPTICS_SYSCTL_WINDOW_MIN:
4117183888Sdumbbell	case SYNAPTICS_SYSCTL_WINDOW_MAX:
4118183888Sdumbbell	case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
4119183888Sdumbbell		if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
4120183888Sdumbbell			return (EINVAL);
4121183888Sdumbbell		break;
4122183888Sdumbbell	case SYNAPTICS_SYSCTL_MULTIPLICATOR:
4123183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
4124183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
4125183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
4126183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
4127183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_MIN:
4128183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_MAX:
4129183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_MAX_NA:
4130183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_LEN:
4131183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
4132183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
4133183888Sdumbbell		if (arg < 1)
4134183888Sdumbbell			return (EINVAL);
4135183888Sdumbbell		break;
4136183888Sdumbbell	case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
4137183888Sdumbbell	case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
4138183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
4139183888Sdumbbell		if (arg < 0)
4140183888Sdumbbell			return (EINVAL);
4141183888Sdumbbell		break;
4142183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
4143183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
4144183888Sdumbbell		if (arg < -6143 || arg > 6143)
4145183888Sdumbbell			return (EINVAL);
4146183888Sdumbbell		break;
4147281709Srpaulo        case SYNAPTICS_SYSCTL_TOUCHPAD_OFF:
4148281709Srpaulo		if (arg < 0 || arg > 1)
4149281709Srpaulo			return (EINVAL);
4150281709Srpaulo		break;
4151183888Sdumbbell	default:
4152183888Sdumbbell		return (EINVAL);
4153183888Sdumbbell	}
4154183888Sdumbbell
4155183888Sdumbbell	/* Update. */
4156183888Sdumbbell	*(int *)oidp->oid_arg1 = arg;
4157183888Sdumbbell
4158183888Sdumbbell	return (error);
4159183888Sdumbbell}
4160183888Sdumbbell
4161183888Sdumbbellstatic void
4162183888Sdumbbellsynaptics_sysctl_create_tree(struct psm_softc *sc)
4163183888Sdumbbell{
4164183888Sdumbbell
4165186218Sdumbbell	if (sc->syninfo.sysctl_tree != NULL)
4166186218Sdumbbell		return;
4167186218Sdumbbell
4168178019Sjkim	/* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
4169178019Sjkim	sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
4170178019Sjkim	sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
4171178019Sjkim	    SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "synaptics", CTLFLAG_RD,
4172178019Sjkim	    0, "Synaptics TouchPad");
4173139982Sphilip
4174183888Sdumbbell	/* hw.psm.synaptics.directional_scrolls. */
4175281708Srpaulo	sc->syninfo.directional_scrolls = 0;
4176178019Sjkim	SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
4177178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4178183888Sdumbbell	    "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
4179178019Sjkim	    &sc->syninfo.directional_scrolls, 0,
4180183888Sdumbbell	    "Enable hardware scrolling pad (if non-zero) or register it as "
4181281708Srpaulo	    "extended buttons (if 0)");
4182139982Sphilip
4183281708Srpaulo	/*
4184281708Srpaulo	 * Turn off two finger scroll if we have a
4185281708Srpaulo	 * physical area reserved for scrolling or when
4186281708Srpaulo	 * there's no multi finger support.
4187281708Srpaulo	 */
4188281708Srpaulo	if (sc->synhw.verticalScroll || sc->synhw.capMultiFinger == 0)
4189281708Srpaulo		sc->syninfo.two_finger_scroll = 0;
4190281708Srpaulo	else
4191281708Srpaulo		sc->syninfo.two_finger_scroll = 1;
4192281708Srpaulo	/* hw.psm.synaptics.two_finger_scroll. */
4193281708Srpaulo	SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
4194281708Srpaulo	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4195281708Srpaulo	    "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY,
4196281708Srpaulo	    &sc->syninfo.two_finger_scroll, 0,
4197281708Srpaulo	    "Enable two finger scrolling");
4198281708Srpaulo
4199183888Sdumbbell	/* hw.psm.synaptics.min_pressure. */
4200183888Sdumbbell	sc->syninfo.min_pressure = 16;
4201183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4202178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4203183888Sdumbbell	    "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4204183888Sdumbbell	    &sc->syninfo.min_pressure, SYNAPTICS_SYSCTL_MIN_PRESSURE,
4205183888Sdumbbell	    synaptics_sysctl, "I",
4206183888Sdumbbell	    "Minimum pressure required to start an action");
4207139982Sphilip
4208183888Sdumbbell	/* hw.psm.synaptics.max_pressure. */
4209183888Sdumbbell	sc->syninfo.max_pressure = 220;
4210183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4211178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4212183888Sdumbbell	    "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4213183888Sdumbbell	    &sc->syninfo.max_pressure, SYNAPTICS_SYSCTL_MAX_PRESSURE,
4214183888Sdumbbell	    synaptics_sysctl, "I",
4215183888Sdumbbell	    "Maximum pressure to detect palm");
4216139982Sphilip
4217183888Sdumbbell	/* hw.psm.synaptics.max_width. */
4218183888Sdumbbell	sc->syninfo.max_width = 10;
4219183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4220178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4221183888Sdumbbell	    "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4222183888Sdumbbell	    &sc->syninfo.max_width, SYNAPTICS_SYSCTL_MAX_WIDTH,
4223183888Sdumbbell	    synaptics_sysctl, "I",
4224183888Sdumbbell	    "Maximum finger width to detect palm");
4225132865Snjl
4226183888Sdumbbell	/* hw.psm.synaptics.top_margin. */
4227183888Sdumbbell	sc->syninfo.margin_top = 200;
4228183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4229183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4230183888Sdumbbell	    "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4231183888Sdumbbell	    &sc->syninfo.margin_top, SYNAPTICS_SYSCTL_MARGIN_TOP,
4232183888Sdumbbell	    synaptics_sysctl, "I",
4233183888Sdumbbell	    "Top margin");
4234183888Sdumbbell
4235183888Sdumbbell	/* hw.psm.synaptics.right_margin. */
4236183888Sdumbbell	sc->syninfo.margin_right = 200;
4237183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4238183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4239183888Sdumbbell	    "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4240183888Sdumbbell	    &sc->syninfo.margin_right, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
4241183888Sdumbbell	    synaptics_sysctl, "I",
4242183888Sdumbbell	    "Right margin");
4243183888Sdumbbell
4244183888Sdumbbell	/* hw.psm.synaptics.bottom_margin. */
4245183888Sdumbbell	sc->syninfo.margin_bottom = 200;
4246183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4247183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4248183888Sdumbbell	    "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4249183888Sdumbbell	    &sc->syninfo.margin_bottom, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
4250183888Sdumbbell	    synaptics_sysctl, "I",
4251183888Sdumbbell	    "Bottom margin");
4252183888Sdumbbell
4253183888Sdumbbell	/* hw.psm.synaptics.left_margin. */
4254183888Sdumbbell	sc->syninfo.margin_left = 200;
4255183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4256183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4257183888Sdumbbell	    "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4258183888Sdumbbell	    &sc->syninfo.margin_left, SYNAPTICS_SYSCTL_MARGIN_LEFT,
4259183888Sdumbbell	    synaptics_sysctl, "I",
4260183888Sdumbbell	    "Left margin");
4261183888Sdumbbell
4262183888Sdumbbell	/* hw.psm.synaptics.na_top. */
4263183888Sdumbbell	sc->syninfo.na_top = 1783;
4264183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4265183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4266183888Sdumbbell	    "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4267183888Sdumbbell	    &sc->syninfo.na_top, SYNAPTICS_SYSCTL_NA_TOP,
4268183888Sdumbbell	    synaptics_sysctl, "I",
4269183888Sdumbbell	    "Top noisy area, where weight_previous_na is used instead "
4270183888Sdumbbell	    "of weight_previous");
4271183888Sdumbbell
4272183888Sdumbbell	/* hw.psm.synaptics.na_right. */
4273183888Sdumbbell	sc->syninfo.na_right = 563;
4274183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4275183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4276183888Sdumbbell	    "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4277183888Sdumbbell	    &sc->syninfo.na_right, SYNAPTICS_SYSCTL_NA_RIGHT,
4278183888Sdumbbell	    synaptics_sysctl, "I",
4279183888Sdumbbell	    "Right noisy area, where weight_previous_na is used instead "
4280183888Sdumbbell	    "of weight_previous");
4281183888Sdumbbell
4282183888Sdumbbell	/* hw.psm.synaptics.na_bottom. */
4283183888Sdumbbell	sc->syninfo.na_bottom = 1408;
4284183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4285183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4286183888Sdumbbell	    "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4287183888Sdumbbell	    &sc->syninfo.na_bottom, SYNAPTICS_SYSCTL_NA_BOTTOM,
4288183888Sdumbbell	    synaptics_sysctl, "I",
4289183888Sdumbbell	    "Bottom noisy area, where weight_previous_na is used instead "
4290183888Sdumbbell	    "of weight_previous");
4291183888Sdumbbell
4292183888Sdumbbell	/* hw.psm.synaptics.na_left. */
4293183888Sdumbbell	sc->syninfo.na_left = 1600;
4294183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4295183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4296183888Sdumbbell	    "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4297183888Sdumbbell	    &sc->syninfo.na_left, SYNAPTICS_SYSCTL_NA_LEFT,
4298183888Sdumbbell	    synaptics_sysctl, "I",
4299183888Sdumbbell	    "Left noisy area, where weight_previous_na is used instead "
4300183888Sdumbbell	    "of weight_previous");
4301183888Sdumbbell
4302183888Sdumbbell	/* hw.psm.synaptics.window_min. */
4303183888Sdumbbell	sc->syninfo.window_min = 4;
4304183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4305183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4306183888Sdumbbell	    "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4307183888Sdumbbell	    &sc->syninfo.window_min, SYNAPTICS_SYSCTL_WINDOW_MIN,
4308183888Sdumbbell	    synaptics_sysctl, "I",
4309183888Sdumbbell	    "Minimum window size to start an action");
4310183888Sdumbbell
4311183888Sdumbbell	/* hw.psm.synaptics.window_max. */
4312183888Sdumbbell	sc->syninfo.window_max = 10;
4313183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4314183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4315183888Sdumbbell	    "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4316183888Sdumbbell	    &sc->syninfo.window_max, SYNAPTICS_SYSCTL_WINDOW_MAX,
4317183888Sdumbbell	    synaptics_sysctl, "I",
4318183888Sdumbbell	    "Maximum window size");
4319183888Sdumbbell
4320183888Sdumbbell	/* hw.psm.synaptics.multiplicator. */
4321183888Sdumbbell	sc->syninfo.multiplicator = 10000;
4322183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4323183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4324183888Sdumbbell	    "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4325183888Sdumbbell	    &sc->syninfo.multiplicator, SYNAPTICS_SYSCTL_MULTIPLICATOR,
4326183888Sdumbbell	    synaptics_sysctl, "I",
4327183888Sdumbbell	    "Multiplicator to increase precision in averages and divisions");
4328183888Sdumbbell
4329183888Sdumbbell	/* hw.psm.synaptics.weight_current. */
4330183888Sdumbbell	sc->syninfo.weight_current = 3;
4331183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4332183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4333183888Sdumbbell	    "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4334183888Sdumbbell	    &sc->syninfo.weight_current, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
4335183888Sdumbbell	    synaptics_sysctl, "I",
4336183888Sdumbbell	    "Weight of the current movement in the new average");
4337183888Sdumbbell
4338183888Sdumbbell	/* hw.psm.synaptics.weight_previous. */
4339183888Sdumbbell	sc->syninfo.weight_previous = 6;
4340183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4341183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4342183888Sdumbbell	    "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4343183888Sdumbbell	    &sc->syninfo.weight_previous, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
4344183888Sdumbbell	    synaptics_sysctl, "I",
4345183888Sdumbbell	    "Weight of the previous average");
4346183888Sdumbbell
4347183888Sdumbbell	/* hw.psm.synaptics.weight_previous_na. */
4348183888Sdumbbell	sc->syninfo.weight_previous_na = 20;
4349183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4350183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4351183888Sdumbbell	    "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4352183888Sdumbbell	    &sc->syninfo.weight_previous_na,
4353183888Sdumbbell	    SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
4354183888Sdumbbell	    synaptics_sysctl, "I",
4355183888Sdumbbell	    "Weight of the previous average (inside the noisy area)");
4356183888Sdumbbell
4357183888Sdumbbell	/* hw.psm.synaptics.weight_len_squared. */
4358183888Sdumbbell	sc->syninfo.weight_len_squared = 2000;
4359183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4360183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4361183888Sdumbbell	    "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4362183888Sdumbbell	    &sc->syninfo.weight_len_squared,
4363183888Sdumbbell	    SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
4364183888Sdumbbell	    synaptics_sysctl, "I",
4365183888Sdumbbell	    "Length (squared) of segments where weight_previous "
4366183888Sdumbbell	    "starts to decrease");
4367183888Sdumbbell
4368183888Sdumbbell	/* hw.psm.synaptics.div_min. */
4369183888Sdumbbell	sc->syninfo.div_min = 9;
4370183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4371183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4372183888Sdumbbell	    "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4373183888Sdumbbell	    &sc->syninfo.div_min, SYNAPTICS_SYSCTL_DIV_MIN,
4374183888Sdumbbell	    synaptics_sysctl, "I",
4375183888Sdumbbell	    "Divisor for fast movements");
4376183888Sdumbbell
4377183888Sdumbbell	/* hw.psm.synaptics.div_max. */
4378183888Sdumbbell	sc->syninfo.div_max = 17;
4379183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4380183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4381183888Sdumbbell	    "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4382183888Sdumbbell	    &sc->syninfo.div_max, SYNAPTICS_SYSCTL_DIV_MAX,
4383183888Sdumbbell	    synaptics_sysctl, "I",
4384183888Sdumbbell	    "Divisor for slow movements");
4385183888Sdumbbell
4386183888Sdumbbell	/* hw.psm.synaptics.div_max_na. */
4387183888Sdumbbell	sc->syninfo.div_max_na = 30;
4388183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4389183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4390183888Sdumbbell	    "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4391183888Sdumbbell	    &sc->syninfo.div_max_na, SYNAPTICS_SYSCTL_DIV_MAX_NA,
4392183888Sdumbbell	    synaptics_sysctl, "I",
4393183888Sdumbbell	    "Divisor with slow movements (inside the noisy area)");
4394183888Sdumbbell
4395183888Sdumbbell	/* hw.psm.synaptics.div_len. */
4396183888Sdumbbell	sc->syninfo.div_len = 100;
4397183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4398183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4399183888Sdumbbell	    "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4400183888Sdumbbell	    &sc->syninfo.div_len, SYNAPTICS_SYSCTL_DIV_LEN,
4401183888Sdumbbell	    synaptics_sysctl, "I",
4402183888Sdumbbell	    "Length of segments where div_max starts to decrease");
4403183888Sdumbbell
4404183888Sdumbbell	/* hw.psm.synaptics.tap_max_delta. */
4405183888Sdumbbell	sc->syninfo.tap_max_delta = 80;
4406183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4407183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4408183888Sdumbbell	    "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4409183888Sdumbbell	    &sc->syninfo.tap_max_delta, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
4410183888Sdumbbell	    synaptics_sysctl, "I",
4411183888Sdumbbell	    "Length of segments above which a tap is ignored");
4412183888Sdumbbell
4413183888Sdumbbell	/* hw.psm.synaptics.tap_min_queue. */
4414183888Sdumbbell	sc->syninfo.tap_min_queue = 2;
4415183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4416183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4417183888Sdumbbell	    "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4418183888Sdumbbell	    &sc->syninfo.tap_min_queue, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
4419183888Sdumbbell	    synaptics_sysctl, "I",
4420183888Sdumbbell	    "Number of packets required to consider a tap");
4421183888Sdumbbell
4422183888Sdumbbell	/* hw.psm.synaptics.taphold_timeout. */
4423183888Sdumbbell	sc->synaction.in_taphold = 0;
4424183888Sdumbbell	sc->syninfo.taphold_timeout = tap_timeout;
4425183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4426183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4427183888Sdumbbell	    "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4428183888Sdumbbell	    &sc->syninfo.taphold_timeout, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
4429183888Sdumbbell	    synaptics_sysctl, "I",
4430183888Sdumbbell	    "Maximum elapsed time between two taps to consider a tap-hold "
4431183888Sdumbbell	    "action");
4432183888Sdumbbell
4433183888Sdumbbell	/* hw.psm.synaptics.vscroll_hor_area. */
4434183888Sdumbbell	sc->syninfo.vscroll_hor_area = 0; /* 1300 */
4435183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4436183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4437183888Sdumbbell	    "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4438183888Sdumbbell	    &sc->syninfo.vscroll_hor_area, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
4439183888Sdumbbell	    synaptics_sysctl, "I",
4440183888Sdumbbell	    "Area reserved for horizontal virtual scrolling");
4441183888Sdumbbell
4442183888Sdumbbell	/* hw.psm.synaptics.vscroll_ver_area. */
4443183888Sdumbbell	sc->syninfo.vscroll_ver_area = -600;
4444183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4445183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4446183888Sdumbbell	    "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4447183888Sdumbbell	    &sc->syninfo.vscroll_ver_area, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
4448183888Sdumbbell	    synaptics_sysctl, "I",
4449183888Sdumbbell	    "Area reserved for vertical virtual scrolling");
4450183888Sdumbbell
4451183888Sdumbbell	/* hw.psm.synaptics.vscroll_min_delta. */
4452183888Sdumbbell	sc->syninfo.vscroll_min_delta = 50;
4453183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4454183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4455183888Sdumbbell	    "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4456183888Sdumbbell	    &sc->syninfo.vscroll_min_delta,
4457183888Sdumbbell	    SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
4458183888Sdumbbell	    synaptics_sysctl, "I",
4459183888Sdumbbell	    "Minimum movement to consider virtual scrolling");
4460183888Sdumbbell
4461183888Sdumbbell	/* hw.psm.synaptics.vscroll_div_min. */
4462183888Sdumbbell	sc->syninfo.vscroll_div_min = 100;
4463183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4464183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4465183888Sdumbbell	    "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4466183888Sdumbbell	    &sc->syninfo.vscroll_div_min, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
4467183888Sdumbbell	    synaptics_sysctl, "I",
4468183888Sdumbbell	    "Divisor for fast scrolling");
4469183888Sdumbbell
4470183888Sdumbbell	/* hw.psm.synaptics.vscroll_div_min. */
4471183888Sdumbbell	sc->syninfo.vscroll_div_max = 150;
4472183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4473183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4474183888Sdumbbell	    "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4475183888Sdumbbell	    &sc->syninfo.vscroll_div_max, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
4476183888Sdumbbell	    synaptics_sysctl, "I",
4477183888Sdumbbell	    "Divisor for slow scrolling");
4478281709Srpaulo
4479281709Srpaulo	/* hw.psm.synaptics.touchpad_off. */
4480281709Srpaulo	sc->syninfo.touchpad_off = 0;
4481281709Srpaulo	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4482281709Srpaulo	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4483281709Srpaulo	    "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4484281709Srpaulo	    &sc->syninfo.touchpad_off, SYNAPTICS_SYSCTL_TOUCHPAD_OFF,
4485281709Srpaulo	    synaptics_sysctl, "I",
4486281709Srpaulo	    "Turn off touchpad");
4487183888Sdumbbell}
4488183888Sdumbbell
4489183888Sdumbbellstatic int
4490233580Sjkimenable_synaptics(KBDC kbdc, struct psm_softc *sc)
4491183888Sdumbbell{
4492233580Sjkim	synapticshw_t synhw;
4493183888Sdumbbell	int status[3];
4494233580Sjkim	int buttons;
4495183888Sdumbbell
4496183888Sdumbbell	VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
4497132865Snjl
4498186175Sdumbbell	/*
4499186175Sdumbbell	 * Just to be on the safe side: this avoids troubles with
4500186175Sdumbbell	 * following mouse_ext_command() when the previous command
4501186175Sdumbbell	 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
4502186175Sdumbbell	 * Synaptics Touchpad behaviour.
4503186175Sdumbbell	 */
4504178019Sjkim	set_mouse_scaling(kbdc, 1);
4505133295Sphilip
4506186175Sdumbbell	/* Identify the Touchpad version. */
4507178019Sjkim	if (mouse_ext_command(kbdc, 0) == 0)
4508178019Sjkim		return (FALSE);
4509178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4510178019Sjkim		return (FALSE);
4511178019Sjkim	if (status[1] != 0x47)
4512178019Sjkim		return (FALSE);
4513133295Sphilip
4514233580Sjkim	bzero(&synhw, sizeof(synhw));
4515233580Sjkim	synhw.infoMinor = status[0];
4516233580Sjkim	synhw.infoMajor = status[2] & 0x0f;
4517132865Snjl
4518178019Sjkim	if (verbose >= 2)
4519233580Sjkim		printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
4520233580Sjkim		    synhw.infoMinor);
4521132865Snjl
4522233580Sjkim	if (synhw.infoMajor < 4) {
4523178019Sjkim		printf("  Unsupported (pre-v4) Touchpad detected\n");
4524178019Sjkim		return (FALSE);
4525178019Sjkim	}
4526133295Sphilip
4527186175Sdumbbell	/* Get the Touchpad model information. */
4528178019Sjkim	if (mouse_ext_command(kbdc, 3) == 0)
4529178019Sjkim		return (FALSE);
4530178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4531178019Sjkim		return (FALSE);
4532178019Sjkim	if ((status[1] & 0x01) != 0) {
4533178019Sjkim		printf("  Failed to read model information\n");
4534178019Sjkim		return (FALSE);
4535178019Sjkim	}
4536132865Snjl
4537233580Sjkim	synhw.infoRot180   = (status[0] & 0x80) != 0;
4538233580Sjkim	synhw.infoPortrait = (status[0] & 0x40) != 0;
4539233580Sjkim	synhw.infoSensor   =  status[0] & 0x3f;
4540233580Sjkim	synhw.infoHardware = (status[1] & 0xfe) >> 1;
4541233580Sjkim	synhw.infoNewAbs   = (status[2] & 0x80) != 0;
4542233580Sjkim	synhw.capPen       = (status[2] & 0x40) != 0;
4543233580Sjkim	synhw.infoSimplC   = (status[2] & 0x20) != 0;
4544233580Sjkim	synhw.infoGeometry =  status[2] & 0x0f;
4545132865Snjl
4546133295Sphilip	if (verbose >= 2) {
4547178019Sjkim		printf("  Model information:\n");
4548233580Sjkim		printf("   infoRot180: %d\n", synhw.infoRot180);
4549233580Sjkim		printf("   infoPortrait: %d\n", synhw.infoPortrait);
4550233580Sjkim		printf("   infoSensor: %d\n", synhw.infoSensor);
4551233580Sjkim		printf("   infoHardware: %d\n", synhw.infoHardware);
4552233580Sjkim		printf("   infoNewAbs: %d\n", synhw.infoNewAbs);
4553233580Sjkim		printf("   capPen: %d\n", synhw.capPen);
4554233580Sjkim		printf("   infoSimplC: %d\n", synhw.infoSimplC);
4555233580Sjkim		printf("   infoGeometry: %d\n", synhw.infoGeometry);
4556133295Sphilip	}
4557139982Sphilip
4558186175Sdumbbell	/* Read the extended capability bits. */
4559178019Sjkim	if (mouse_ext_command(kbdc, 2) == 0)
4560178019Sjkim		return (FALSE);
4561178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4562178019Sjkim		return (FALSE);
4563244405Sdumbbell	if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
4564178019Sjkim		printf("  Failed to read extended capability bits\n");
4565178019Sjkim		return (FALSE);
4566178019Sjkim	}
4567178019Sjkim
4568186175Sdumbbell	/* Set the different capabilities when they exist. */
4569233580Sjkim	buttons = 0;
4570233580Sjkim	synhw.capExtended = (status[0] & 0x80) != 0;
4571233580Sjkim	if (synhw.capExtended) {
4572255153Sdumbbell		synhw.nExtendedQueries = (status[0] & 0x70) != 0;
4573255153Sdumbbell		synhw.capMiddle        = (status[0] & 0x04) != 0;
4574255153Sdumbbell		synhw.capPassthrough   = (status[2] & 0x80) != 0;
4575255153Sdumbbell		synhw.capSleep         = (status[2] & 0x10) != 0;
4576255153Sdumbbell		synhw.capFourButtons   = (status[2] & 0x08) != 0;
4577255153Sdumbbell		synhw.capMultiFinger   = (status[2] & 0x02) != 0;
4578255153Sdumbbell		synhw.capPalmDetect    = (status[2] & 0x01) != 0;
4579178019Sjkim
4580178019Sjkim		if (verbose >= 2) {
4581178019Sjkim			printf("  Extended capabilities:\n");
4582233580Sjkim			printf("   capExtended: %d\n", synhw.capExtended);
4583255153Sdumbbell			printf("   capMiddle: %d\n", synhw.capMiddle);
4584255153Sdumbbell			printf("   nExtendedQueries: %d\n",
4585255153Sdumbbell			    synhw.nExtendedQueries);
4586233580Sjkim			printf("   capPassthrough: %d\n", synhw.capPassthrough);
4587233580Sjkim			printf("   capSleep: %d\n", synhw.capSleep);
4588233580Sjkim			printf("   capFourButtons: %d\n", synhw.capFourButtons);
4589233580Sjkim			printf("   capMultiFinger: %d\n", synhw.capMultiFinger);
4590233580Sjkim			printf("   capPalmDetect: %d\n", synhw.capPalmDetect);
4591178019Sjkim		}
4592178019Sjkim
4593178019Sjkim		/*
4594255153Sdumbbell		 * If nExtendedQueries is 1 or greater, then the TouchPad
4595255153Sdumbbell		 * supports this number of extended queries. We can load
4596186175Sdumbbell		 * more information about buttons using query 0x09.
4597178019Sjkim		 */
4598255153Sdumbbell		if (synhw.capExtended && synhw.nExtendedQueries) {
4599178019Sjkim			if (mouse_ext_command(kbdc, 0x09) == 0)
4600178019Sjkim				return (FALSE);
4601178019Sjkim			if (get_mouse_status(kbdc, status, 0, 3) != 3)
4602178019Sjkim				return (FALSE);
4603281708Srpaulo			synhw.verticalScroll   = (status[0] & 0x01) != 0;
4604281708Srpaulo			synhw.horizontalScroll = (status[0] & 0x02) != 0;
4605281708Srpaulo			synhw.verticalWheel    = (status[0] & 0x08) != 0;
4606255153Sdumbbell			synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
4607281708Srpaulo			if (verbose >= 2) {
4608281708Srpaulo				printf("  Extended model ID:\n");
4609281708Srpaulo				printf("   verticalScroll: %d\n",
4610281708Srpaulo				    synhw.verticalScroll);
4611281708Srpaulo				printf("   horizontalScroll: %d\n",
4612281708Srpaulo				    synhw.horizontalScroll);
4613281708Srpaulo				printf("   verticalWheel: %d\n",
4614281708Srpaulo				    synhw.verticalWheel);
4615281708Srpaulo				printf("   nExtendedButtons: %d\n",
4616281708Srpaulo				    synhw.nExtendedButtons);
4617281708Srpaulo			}
4618255153Sdumbbell			/*
4619255153Sdumbbell			 * Add the number of extended buttons to the total
4620255153Sdumbbell			 * button support count, including the middle button
4621255153Sdumbbell			 * if capMiddle support bit is set.
4622255153Sdumbbell			 */
4623255153Sdumbbell			buttons = synhw.nExtendedButtons + synhw.capMiddle;
4624233580Sjkim		} else
4625255153Sdumbbell			/*
4626255153Sdumbbell			 * If the capFourButtons support bit is set,
4627255153Sdumbbell			 * add a fourth button to the total button count.
4628255153Sdumbbell			 */
4629233580Sjkim			buttons = synhw.capFourButtons ? 1 : 0;
4630233580Sjkim	}
4631233580Sjkim	if (verbose >= 2) {
4632233580Sjkim		if (synhw.capExtended)
4633233580Sjkim			printf("  Additional Buttons: %d\n", buttons);
4634233580Sjkim		else
4635178019Sjkim			printf("  No extended capabilities\n");
4636178019Sjkim	}
4637178019Sjkim
4638281708Srpaulo	/* Read the continued capabilities bits. */
4639281708Srpaulo	if (mouse_ext_command(kbdc, 0xc) != 0 &&
4640281708Srpaulo	    get_mouse_status(kbdc, status, 0, 3) == 3) {
4641281708Srpaulo		synhw.capClickPad         = (status[1] & 0x01) << 1;
4642281708Srpaulo		synhw.capClickPad        |= (status[0] & 0x10) != 0;
4643281708Srpaulo		synhw.capDeluxeLEDs       = (status[1] & 0x02) != 0;
4644281708Srpaulo		synhw.noAbsoluteFilter    = (status[1] & 0x04) != 0;
4645281708Srpaulo		synhw.capReportsV         = (status[1] & 0x08) != 0;
4646281708Srpaulo		synhw.capUniformClickPad  = (status[1] & 0x10) != 0;
4647281708Srpaulo		synhw.capReportsMin       = (status[1] & 0x20) != 0;
4648281708Srpaulo		synhw.capInterTouch       = (status[1] & 0x40) != 0;
4649281708Srpaulo		synhw.capReportsMax       = (status[2] & 0x02) != 0;
4650281708Srpaulo		synhw.capClearPad         = (status[2] & 0x04) != 0;
4651281708Srpaulo		synhw.capAdvancedGestures = (status[2] & 0x08) != 0;
4652281708Srpaulo		synhw.capCoveredPad       = (status[2] & 0x80) != 0;
4653281708Srpaulo
4654281708Srpaulo		if (verbose >= 2) {
4655281708Srpaulo			printf("  Continued capabilities:\n");
4656281708Srpaulo			printf("   capClickPad: %d\n", synhw.capClickPad);
4657281708Srpaulo			printf("   capDeluxeLEDs: %d\n", synhw.capDeluxeLEDs);
4658281708Srpaulo			printf("   noAbsoluteFilter: %d\n",
4659281708Srpaulo			    synhw.noAbsoluteFilter);
4660281708Srpaulo			printf("   capReportsV: %d\n", synhw.capReportsV);
4661281708Srpaulo			printf("   capUniformClickPad: %d\n",
4662281708Srpaulo			    synhw.capUniformClickPad);
4663281708Srpaulo			printf("   capReportsMin: %d\n", synhw.capReportsMin);
4664281708Srpaulo			printf("   capInterTouch: %d\n", synhw.capInterTouch);
4665281708Srpaulo			printf("   capReportsMax: %d\n", synhw.capReportsMax);
4666281708Srpaulo			printf("   capClearPad: %d\n", synhw.capClearPad);
4667281708Srpaulo			printf("   capAdvancedGestures: %d\n",
4668281708Srpaulo			    synhw.capAdvancedGestures);
4669281708Srpaulo			printf("   capCoveredPad: %d\n", synhw.capCoveredPad);
4670281708Srpaulo		}
4671281708Srpaulo		buttons += synhw.capClickPad;
4672281708Srpaulo	}
4673281708Srpaulo
4674139982Sphilip	/*
4675255153Sdumbbell	 * Add the default number of 3 buttons to the total
4676255153Sdumbbell	 * count of supported buttons reported above.
4677255153Sdumbbell	 */
4678255153Sdumbbell	buttons += 3;
4679255153Sdumbbell
4680255153Sdumbbell	/*
4681186175Sdumbbell	 * Read the mode byte.
4682178019Sjkim	 *
4683178019Sjkim	 * XXX: Note the Synaptics documentation also defines the first
4684178019Sjkim	 * byte of the response to this query to be a constant 0x3b, this
4685178019Sjkim	 * does not appear to be true for Touchpads with guest devices.
4686139982Sphilip	 */
4687178019Sjkim	if (mouse_ext_command(kbdc, 1) == 0)
4688139982Sphilip		return (FALSE);
4689178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
4690139982Sphilip		return (FALSE);
4691244405Sdumbbell	if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
4692178019Sjkim		printf("  Failed to read mode byte\n");
4693178019Sjkim		return (FALSE);
4694139982Sphilip	}
4695139982Sphilip
4696233580Sjkim	if (sc != NULL)
4697233580Sjkim		sc->synhw = synhw;
4698233580Sjkim	if (!synaptics_support)
4699233580Sjkim		return (FALSE);
4700233580Sjkim
4701186175Sdumbbell	/* Set the mode byte; request wmode where available. */
4702233580Sjkim	mouse_ext_command(kbdc, synhw.capExtended ? 0xc1 : 0xc0);
4703133295Sphilip
4704186175Sdumbbell	/* "Commit" the Set Mode Byte command sent above. */
4705178019Sjkim	set_mouse_sampling_rate(kbdc, 20);
4706132865Snjl
4707233580Sjkim	VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
4708132865Snjl
4709233580Sjkim	if (sc != NULL) {
4710281709Srpaulo		if (trackpoint_support && synhw.capPassthrough) {
4711281709Srpaulo			synaptics_passthrough_on(sc);
4712281709Srpaulo			enable_trackpoint(kbdc, sc);
4713281709Srpaulo			synaptics_passthrough_off(sc);
4714281709Srpaulo		}
4715233580Sjkim		/* Create sysctl tree. */
4716233580Sjkim		synaptics_sysctl_create_tree(sc);
4717233580Sjkim		sc->hw.buttons = buttons;
4718233580Sjkim	}
4719183888Sdumbbell
4720178019Sjkim	return (TRUE);
4721132865Snjl}
4722133295Sphilip
4723281709Srpaulostatic void
4724281709Srpaulosynaptics_passthrough_on(struct psm_softc *sc)
4725281709Srpaulo{
4726281709Srpaulo	int mode_byte;
4727281709Srpaulo
4728281709Srpaulo	mode_byte = 0xc1 | (1 << 5);
4729281709Srpaulo	VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode. %d\n",
4730281709Srpaulo		mode_byte));
4731281709Srpaulo	mouse_ext_command(sc->kbdc, mode_byte);
4732281709Srpaulo
4733281709Srpaulo	/* "Commit" the Set Mode Byte command sent above. */
4734281709Srpaulo	set_mouse_sampling_rate(sc->kbdc, 20);
4735281709Srpaulo}
4736281709Srpaulo
4737281709Srpaulostatic void
4738281709Srpaulosynaptics_passthrough_off(struct psm_softc *sc)
4739281709Srpaulo{
4740281709Srpaulo	int mode_byte;
4741281709Srpaulo
4742281709Srpaulo	mode_byte = 0xc1;
4743281709Srpaulo	VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n"));
4744281709Srpaulo	set_mouse_scaling(sc->kbdc, 2);
4745281709Srpaulo	set_mouse_scaling(sc->kbdc, 1);
4746281709Srpaulo	mouse_ext_command(sc->kbdc, mode_byte);
4747281709Srpaulo
4748281709Srpaulo	/* "Commit" the Set Mode Byte command sent above. */
4749281709Srpaulo	set_mouse_sampling_rate(sc->kbdc, 20);
4750281709Srpaulo}
4751281709Srpaulo
4752248478Sjkim/* IBM/Lenovo TrackPoint */
4753248478Sjkimstatic int
4754281709Srpaulotrackpoint_command(struct psm_softc *sc, int cmd, int loc, int val)
4755248478Sjkim{
4756248478Sjkim	const int seq[] = { 0xe2, cmd, loc, val };
4757248478Sjkim	int i;
4758248478Sjkim
4759281709Srpaulo	if (sc->synhw.capPassthrough)
4760281709Srpaulo		synaptics_passthrough_on(sc);
4761281709Srpaulo
4762281709Srpaulo	for (i = 0; i < nitems(seq); i++) {
4763281709Srpaulo		if (sc->synhw.capPassthrough &&
4764281709Srpaulo		    (seq[i] == 0xff || seq[i] == 0xe7))
4765281709Srpaulo			if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) {
4766281709Srpaulo				synaptics_passthrough_off(sc);
4767281709Srpaulo				return (EIO);
4768281709Srpaulo			}
4769281709Srpaulo		if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) {
4770281709Srpaulo			if (sc->synhw.capPassthrough)
4771281709Srpaulo				synaptics_passthrough_off(sc);
4772248478Sjkim			return (EIO);
4773281709Srpaulo		}
4774281709Srpaulo	}
4775281709Srpaulo
4776281709Srpaulo	if (sc->synhw.capPassthrough)
4777281709Srpaulo		synaptics_passthrough_off(sc);
4778281709Srpaulo
4779248478Sjkim	return (0);
4780248478Sjkim}
4781248478Sjkim
4782248478Sjkim#define	PSM_TPINFO(x)	offsetof(struct psm_softc, tpinfo.x)
4783248478Sjkim#define	TPMASK		0
4784248478Sjkim#define	TPLOC		1
4785248478Sjkim#define	TPINFO		2
4786248478Sjkim
4787248478Sjkimstatic int
4788248478Sjkimtrackpoint_sysctl(SYSCTL_HANDLER_ARGS)
4789248478Sjkim{
4790248478Sjkim	static const int data[][3] = {
4791248478Sjkim		{ 0x00, 0x4a, PSM_TPINFO(sensitivity) },
4792248478Sjkim		{ 0x00, 0x4d, PSM_TPINFO(inertia) },
4793248478Sjkim		{ 0x00, 0x60, PSM_TPINFO(uplateau) },
4794248478Sjkim		{ 0x00, 0x57, PSM_TPINFO(reach) },
4795248478Sjkim		{ 0x00, 0x58, PSM_TPINFO(draghys) },
4796248478Sjkim		{ 0x00, 0x59, PSM_TPINFO(mindrag) },
4797248478Sjkim		{ 0x00, 0x5a, PSM_TPINFO(upthresh) },
4798248478Sjkim		{ 0x00, 0x5c, PSM_TPINFO(threshold) },
4799248478Sjkim		{ 0x00, 0x5d, PSM_TPINFO(jenks) },
4800248478Sjkim		{ 0x00, 0x5e, PSM_TPINFO(ztime) },
4801248478Sjkim		{ 0x01, 0x2c, PSM_TPINFO(pts) },
4802248478Sjkim		{ 0x08, 0x2d, PSM_TPINFO(skipback) }
4803248478Sjkim	};
4804248478Sjkim	struct psm_softc *sc;
4805248478Sjkim	int error, newval, *oldvalp;
4806248478Sjkim	const int *tp;
4807248478Sjkim
4808248478Sjkim	if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
4809248478Sjkim		return (EINVAL);
4810248478Sjkim	sc = arg1;
4811248478Sjkim	tp = data[arg2];
4812248478Sjkim	oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
4813248478Sjkim	newval = *oldvalp;
4814248478Sjkim	error = sysctl_handle_int(oidp, &newval, 0, req);
4815248478Sjkim	if (error != 0)
4816248478Sjkim		return (error);
4817248478Sjkim	if (newval == *oldvalp)
4818248478Sjkim		return (0);
4819248478Sjkim	if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
4820248478Sjkim		return (EINVAL);
4821281709Srpaulo	error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47,
4822248478Sjkim	    tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
4823248478Sjkim	if (error != 0)
4824248478Sjkim		return (error);
4825248478Sjkim	*oldvalp = newval;
4826248478Sjkim
4827248478Sjkim	return (0);
4828248478Sjkim}
4829248478Sjkim
4830248478Sjkimstatic void
4831248478Sjkimtrackpoint_sysctl_create_tree(struct psm_softc *sc)
4832248478Sjkim{
4833248478Sjkim
4834248478Sjkim	if (sc->tpinfo.sysctl_tree != NULL)
4835248478Sjkim		return;
4836248478Sjkim
4837248478Sjkim	/* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
4838248478Sjkim	sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
4839248478Sjkim	sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
4840248478Sjkim	    SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD,
4841248478Sjkim	    0, "IBM/Lenovo TrackPoint");
4842248478Sjkim
4843248478Sjkim	/* hw.psm.trackpoint.sensitivity */
4844281709Srpaulo	sc->tpinfo.sensitivity = 0x80;
4845248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4846248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4847248478Sjkim	    "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4848248478Sjkim	    sc, TRACKPOINT_SYSCTL_SENSITIVITY,
4849248478Sjkim	    trackpoint_sysctl, "I",
4850248478Sjkim	    "Sensitivity");
4851248478Sjkim
4852248478Sjkim	/* hw.psm.trackpoint.negative_inertia */
4853248478Sjkim	sc->tpinfo.inertia = 0x06;
4854248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4855248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4856248478Sjkim	    "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4857248478Sjkim	    sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
4858248478Sjkim	    trackpoint_sysctl, "I",
4859248478Sjkim	    "Negative inertia factor");
4860248478Sjkim
4861248478Sjkim	/* hw.psm.trackpoint.upper_plateau */
4862248478Sjkim	sc->tpinfo.uplateau = 0x61;
4863248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4864248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4865248478Sjkim	    "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4866248478Sjkim	    sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
4867248478Sjkim	    trackpoint_sysctl, "I",
4868248478Sjkim	    "Transfer function upper plateau speed");
4869248478Sjkim
4870248478Sjkim	/* hw.psm.trackpoint.backup_range */
4871248478Sjkim	sc->tpinfo.reach = 0x0a;
4872248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4873248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4874248478Sjkim	    "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4875248478Sjkim	    sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
4876248478Sjkim	    trackpoint_sysctl, "I",
4877248478Sjkim	    "Backup range");
4878248478Sjkim
4879248478Sjkim	/* hw.psm.trackpoint.drag_hysteresis */
4880248478Sjkim	sc->tpinfo.draghys = 0xff;
4881248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4882248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4883248478Sjkim	    "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4884248478Sjkim	    sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
4885248478Sjkim	    trackpoint_sysctl, "I",
4886248478Sjkim	    "Drag hysteresis");
4887248478Sjkim
4888248478Sjkim	/* hw.psm.trackpoint.minimum_drag */
4889248478Sjkim	sc->tpinfo.mindrag = 0x14;
4890248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4891248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4892248478Sjkim	    "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4893248478Sjkim	    sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
4894248478Sjkim	    trackpoint_sysctl, "I",
4895248478Sjkim	    "Minimum drag");
4896248478Sjkim
4897248478Sjkim	/* hw.psm.trackpoint.up_threshold */
4898248478Sjkim	sc->tpinfo.upthresh = 0xff;
4899248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4900248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4901248478Sjkim	    "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4902248478Sjkim	    sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
4903248478Sjkim	    trackpoint_sysctl, "I",
4904248478Sjkim	    "Up threshold for release");
4905248478Sjkim
4906248478Sjkim	/* hw.psm.trackpoint.threshold */
4907248478Sjkim	sc->tpinfo.threshold = 0x08;
4908248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4909248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4910248478Sjkim	    "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4911248478Sjkim	    sc, TRACKPOINT_SYSCTL_THRESHOLD,
4912248478Sjkim	    trackpoint_sysctl, "I",
4913248478Sjkim	    "Threshold");
4914248478Sjkim
4915248478Sjkim	/* hw.psm.trackpoint.jenks_curvature */
4916248478Sjkim	sc->tpinfo.jenks = 0x87;
4917248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4918248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4919248478Sjkim	    "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4920248478Sjkim	    sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
4921248478Sjkim	    trackpoint_sysctl, "I",
4922248478Sjkim	    "Jenks curvature");
4923248478Sjkim
4924248478Sjkim	/* hw.psm.trackpoint.z_time */
4925248478Sjkim	sc->tpinfo.ztime = 0x26;
4926248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4927248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4928248478Sjkim	    "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4929248478Sjkim	    sc, TRACKPOINT_SYSCTL_Z_TIME,
4930248478Sjkim	    trackpoint_sysctl, "I",
4931248478Sjkim	    "Z time constant");
4932248478Sjkim
4933248478Sjkim	/* hw.psm.trackpoint.press_to_select */
4934248478Sjkim	sc->tpinfo.pts = 0x00;
4935248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4936248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4937248478Sjkim	    "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4938248478Sjkim	    sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
4939248478Sjkim	    trackpoint_sysctl, "I",
4940248478Sjkim	    "Press to Select");
4941248478Sjkim
4942248478Sjkim	/* hw.psm.trackpoint.skip_backups */
4943248478Sjkim	sc->tpinfo.skipback = 0x00;
4944248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
4945248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
4946248478Sjkim	    "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4947248478Sjkim	    sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
4948248478Sjkim	    trackpoint_sysctl, "I",
4949248478Sjkim	    "Skip backups from drags");
4950248478Sjkim}
4951248478Sjkim
4952281709Srpaulostatic void
4953281709Srpauloset_trackpoint_parameters(struct psm_softc *sc)
4954281709Srpaulo{
4955281709Srpaulo	trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity);
4956281709Srpaulo	trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau);
4957281709Srpaulo	trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia);
4958281709Srpaulo	trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach);
4959281709Srpaulo	trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys);
4960281709Srpaulo	trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag);
4961281709Srpaulo	trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh);
4962281709Srpaulo	trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold);
4963281709Srpaulo	trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks);
4964281709Srpaulo	trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime);
4965281709Srpaulo	if (sc->tpinfo.pts == 0x01)
4966281709Srpaulo		trackpoint_command(sc, 0x47, 0x2c, 0x01);
4967281709Srpaulo	if (sc->tpinfo.skipback == 0x01)
4968281709Srpaulo		trackpoint_command(sc, 0x47, 0x2d, 0x08);
4969281709Srpaulo}
4970281709Srpaulo
4971248478Sjkimstatic int
4972248478Sjkimenable_trackpoint(KBDC kbdc, struct psm_softc *sc)
4973248478Sjkim{
4974248478Sjkim	int id;
4975248478Sjkim
4976248478Sjkim	if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
4977248478Sjkim	    read_aux_data(kbdc) != 0x01)
4978248478Sjkim		return (FALSE);
4979248478Sjkim	id = read_aux_data(kbdc);
4980248478Sjkim	if (id < 0x01)
4981248478Sjkim		return (FALSE);
4982248478Sjkim	if (sc != NULL)
4983248478Sjkim		sc->tphw = id;
4984248478Sjkim	if (!trackpoint_support)
4985248478Sjkim		return (FALSE);
4986248478Sjkim
4987248478Sjkim	if (sc != NULL) {
4988248478Sjkim		/* Create sysctl tree. */
4989248478Sjkim		trackpoint_sysctl_create_tree(sc);
4990248478Sjkim
4991281709Srpaulo		/*
4992281709Srpaulo		 * Don't overwrite hwid and buttons when we are
4993281709Srpaulo		 * a guest device.
4994281709Srpaulo		 */
4995281709Srpaulo		if (!sc->synhw.capPassthrough) {
4996281709Srpaulo			sc->hw.hwid = id;
4997281709Srpaulo			sc->hw.buttons = 3;
4998281709Srpaulo		}
4999248478Sjkim	}
5000248478Sjkim
5001248478Sjkim	return (TRUE);
5002248478Sjkim}
5003248478Sjkim
500449965Syokota/* Interlink electronics VersaPad */
500549965Syokotastatic int
5006233580Sjkimenable_versapad(KBDC kbdc, struct psm_softc *sc)
500749965Syokota{
5008178019Sjkim	int data[3];
500949965Syokota
5010178019Sjkim	set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
5011178019Sjkim	set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
5012178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
5013178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
5014178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
5015178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
5016178019Sjkim	if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
5017178019Sjkim		return (FALSE);
5018178019Sjkim	if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
5019178019Sjkim		return (FALSE);
5020178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
502149965Syokota
5022178019Sjkim	return (TRUE);				/* PS/2 absolute mode */
502349965Syokota}
502449965Syokota
5025147271Smarius/*
5026147271Smarius * Return true if 'now' is earlier than (start + (secs.usecs)).
5027147271Smarius * Now may be NULL and the function will fetch the current time from
5028147271Smarius * getmicrouptime(), or a cached 'now' can be passed in.
5029147271Smarius * All values should be numbers derived from getmicrouptime().
5030147271Smarius */
503141016Sdfrstatic int
5032147271Smariustimeelapsed(start, secs, usecs, now)
5033147271Smarius	const struct timeval *start, *now;
5034147271Smarius	int secs, usecs;
5035147271Smarius{
5036147271Smarius	struct timeval snow, tv;
5037147271Smarius
5038147271Smarius	/* if there is no 'now' passed in, the get it as a convience. */
5039147271Smarius	if (now == NULL) {
5040147271Smarius		getmicrouptime(&snow);
5041147271Smarius		now = &snow;
5042147271Smarius	}
5043178019Sjkim
5044147271Smarius	tv.tv_sec = secs;
5045147271Smarius	tv.tv_usec = usecs;
5046147271Smarius	timevaladd(&tv, start);
5047147271Smarius	return (timevalcmp(&tv, now, <));
5048147271Smarius}
5049147271Smarius
5050147271Smariusstatic int
505154629Syokotapsmresume(device_t dev)
505241016Sdfr{
5053178019Sjkim	struct psm_softc *sc = device_get_softc(dev);
5054178019Sjkim	int unit = device_get_unit(dev);
5055178019Sjkim	int err;
505641016Sdfr
5057178019Sjkim	VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
505841016Sdfr
5059233580Sjkim	if ((sc->config &
5060233580Sjkim	    (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
5061178019Sjkim		return (0);
506258230Syokota
5063178019Sjkim	err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
506441016Sdfr
5065178019Sjkim	if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
5066178019Sjkim		/*
5067178019Sjkim		 * Release the blocked process; it must be notified that
5068178019Sjkim		 * the device cannot be accessed anymore.
5069178019Sjkim		 */
5070178019Sjkim		sc->state &= ~PSM_ASLP;
5071178019Sjkim		wakeup(sc);
5072178019Sjkim	}
507341016Sdfr
5074178019Sjkim	VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
507541016Sdfr
5076178019Sjkim	return (err);
507741016Sdfr}
507841016Sdfr
507952997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
508083147Syokota
5081147271Smarius#ifdef DEV_ISA
5082147271Smarius
508383147Syokota/*
508483147Syokota * This sucks up assignments from PNPBIOS and ACPI.
508583147Syokota */
508683147Syokota
508783931Syokota/*
508883931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
508983931Syokota * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
509083931Syokota * can be probed and attached only after the AT keyboard controller is
509183931Syokota * attached, we shall quietly reserve the IRQ resource for later use.
509283931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
509383931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging
509483931Syokota * under the keyboard controller, then probe and attach it.
509583931Syokota */
509683147Syokota
509783147Syokotastatic	devclass_t			psmcpnp_devclass;
509883147Syokota
509983147Syokotastatic	device_probe_t			psmcpnp_probe;
510083147Syokotastatic	device_attach_t			psmcpnp_attach;
510183147Syokota
510283147Syokotastatic device_method_t psmcpnp_methods[] = {
510383147Syokota	DEVMETHOD(device_probe,		psmcpnp_probe),
510483147Syokota	DEVMETHOD(device_attach,	psmcpnp_attach),
5105178019Sjkim
510683147Syokota	{ 0, 0 }
510783147Syokota};
510883147Syokota
510983147Syokotastatic driver_t psmcpnp_driver = {
511083147Syokota	PSMCPNP_DRIVER_NAME,
511183147Syokota	psmcpnp_methods,
511283147Syokota	1,			/* no softc */
511383147Syokota};
511483147Syokota
511583147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = {
511688188Ssheldonh	{ 0x030fd041, "PS/2 mouse port" },		/* PNP0F03 */
5117156730Stakawata	{ 0x0e0fd041, "PS/2 mouse port" },		/* PNP0F0E */
5118156730Stakawata	{ 0x120fd041, "PS/2 mouse port" },		/* PNP0F12 */
511983147Syokota	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
512083147Syokota	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
5121117117Smikeh	{ 0x02002e4f, "Dell PS/2 mouse port" },		/* Lat. X200, Dell */
5122156730Stakawata	{ 0x0002a906, "ALPS Glide Point" },		/* ALPS Glide Point */
512383492Syokota	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
512484407Stakawata	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
5125109679Shsu	{ 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
5126109679Shsu	{ 0x0290d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9002, Vaio */
5127109710Smarcel	{ 0x0390d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9003, Vaio */
5128109679Shsu	{ 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
512983147Syokota	{ 0 }
513083147Syokota};
513183147Syokota
513283147Syokotastatic int
513383147Syokotacreate_a_copy(device_t atkbdc, device_t me)
513483147Syokota{
513583147Syokota	device_t psm;
513683147Syokota	u_long irq;
513783147Syokota
513883931Syokota	/* find the PS/2 mouse device instance under the keyboard controller */
513983931Syokota	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
5140178019Sjkim	    device_get_unit(atkbdc));
514183147Syokota	if (psm == NULL)
5142178019Sjkim		return (ENXIO);
514383931Syokota	if (device_get_state(psm) != DS_NOTPRESENT)
5144178019Sjkim		return (0);
514583147Syokota
514683931Syokota	/* move our resource to the found device */
514783931Syokota	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
5148216491Sjhb	bus_delete_resource(me, SYS_RES_IRQ, 0);
514983147Syokota	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
515083147Syokota
515183147Syokota	/* ...then probe and attach it */
5152178019Sjkim	return (device_probe_and_attach(psm));
515383147Syokota}
515483147Syokota
515583147Syokotastatic int
515683147Syokotapsmcpnp_probe(device_t dev)
515783147Syokota{
515883931Syokota	struct resource *res;
515983931Syokota	u_long irq;
516083931Syokota	int rid;
516183147Syokota
516283147Syokota	if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
5163178019Sjkim		return (ENXIO);
516483147Syokota
516583931Syokota	/*
516683931Syokota	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
516783931Syokota	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
516883931Syokota	 * declares the PS/2 mouse device node without an IRQ resource!
516983931Syokota	 * If this happens, we shall refer to device hints.
517083931Syokota	 * If we still don't find it there, use a hardcoded value... XXX
517183931Syokota	 */
517283931Syokota	rid = 0;
517383931Syokota	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
517483931Syokota	if (irq <= 0) {
517583931Syokota		if (resource_long_value(PSM_DRIVER_NAME,
5176178019Sjkim		    device_get_unit(dev),"irq", &irq) != 0)
517783931Syokota			irq = 12;	/* XXX */
517883931Syokota		device_printf(dev, "irq resource info is missing; "
5179178019Sjkim		    "assuming irq %ld\n", irq);
518083931Syokota		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
518183931Syokota	}
5182216491Sjhb	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0);
518383931Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
518483147Syokota
518583147Syokota	/* keep quiet */
518683147Syokota	if (!bootverbose)
518783147Syokota		device_quiet(dev);
518883492Syokota
518983931Syokota	return ((res == NULL) ? ENXIO : 0);
519083147Syokota}
519183147Syokota
519283147Syokotastatic int
519383147Syokotapsmcpnp_attach(device_t dev)
519483147Syokota{
519583492Syokota	device_t atkbdc;
519683147Syokota
519783931Syokota	/* find the keyboard controller, which may be on acpi* or isa* bus */
519883931Syokota	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
5199178019Sjkim	    device_get_unit(dev));
5200178019Sjkim	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
520183492Syokota		create_a_copy(atkbdc, dev);
520283492Syokota
5203178019Sjkim	return (0);
520483147Syokota}
520583147Syokota
520683147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
520783147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
5208147271Smarius
5209147271Smarius#endif /* DEV_ISA */
5210