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/11/sys/dev/atkbdc/psm.c 362209 2020-06-15 22:43:46Z wulf $");
63116181Sobrien
64147271Smarius#include "opt_isa.h"
6541016Sdfr#include "opt_psm.h"
66321060Swulf#include "opt_evdev.h"
6741016Sdfr
6841016Sdfr#include <sys/param.h>
6941016Sdfr#include <sys/systm.h>
7041016Sdfr#include <sys/kernel.h>
7141016Sdfr#include <sys/module.h>
7241016Sdfr#include <sys/bus.h>
7341016Sdfr#include <sys/conf.h>
74189870Srnoland#include <sys/filio.h>
7541016Sdfr#include <sys/poll.h>
76189870Srnoland#include <sys/sigio.h>
77189870Srnoland#include <sys/signalvar.h>
7841016Sdfr#include <sys/syslog.h>
7945720Speter#include <machine/bus.h>
8041181Sdfr#include <sys/rman.h>
8170834Swollman#include <sys/selinfo.h>
82123442Salfred#include <sys/sysctl.h>
8384880Syokota#include <sys/time.h>
8441016Sdfr#include <sys/uio.h>
8541016Sdfr
86114216Skan#include <sys/limits.h>
8766860Sphk#include <sys/mouse.h>
8841181Sdfr#include <machine/resource.h>
8941016Sdfr
90147271Smarius#ifdef DEV_ISA
9141016Sdfr#include <isa/isavar.h>
92147271Smarius#endif
9341016Sdfr
94321060Swulf#ifdef EVDEV_SUPPORT
95321060Swulf#include <dev/evdev/evdev.h>
96321060Swulf#include <dev/evdev/input.h>
97321060Swulf#endif
98321060Swulf
99147271Smarius#include <dev/atkbdc/atkbdcreg.h>
100147271Smarius#include <dev/atkbdc/psm.h>
101147271Smarius
10241016Sdfr/*
10341016Sdfr * Driver specific options: the following options may be set by
10441016Sdfr * `options' statements in the kernel configuration file.
10541016Sdfr */
10641016Sdfr
10741016Sdfr/* debugging */
10841016Sdfr#ifndef PSM_DEBUG
109178019Sjkim#define	PSM_DEBUG	0	/*
110134405Sgibbs				 * logging: 0: none, 1: brief, 2: verbose
111134405Sgibbs				 *          3: sync errors, 4: all packets
112134405Sgibbs				 */
11341016Sdfr#endif
114178019Sjkim#define	VLOG(level, args)	do {	\
115178019Sjkim	if (verbose >= level)		\
116178019Sjkim		log args;		\
117134405Sgibbs} while (0)
11841016Sdfr
11984880Syokota#ifndef PSM_INPUT_TIMEOUT
120178019Sjkim#define	PSM_INPUT_TIMEOUT	2000000	/* 2 sec */
12184880Syokota#endif
12284880Syokota
123133297Sphilip#ifndef PSM_TAP_TIMEOUT
124178019Sjkim#define	PSM_TAP_TIMEOUT		125000
125133297Sphilip#endif
126133297Sphilip
127133297Sphilip#ifndef PSM_TAP_THRESHOLD
128178019Sjkim#define	PSM_TAP_THRESHOLD	25
129133297Sphilip#endif
130133297Sphilip
13141016Sdfr/* end of driver specific options */
13241016Sdfr
133178019Sjkim#define	PSMCPNP_DRIVER_NAME	"psmcpnp"
13483492Syokota
135331170Seadlerstruct psmcpnp_softc {
136331170Seadler	enum {
137331170Seadler		PSMCPNP_GENERIC,
138331170Seadler		PSMCPNP_FORCEPAD,
139344165Swulf		PSMCPNP_TOPBUTTONPAD,
140331170Seadler	} type;		/* Based on PnP ID */
141331170Seadler};
142331170Seadler
14341016Sdfr/* input queue */
144178019Sjkim#define	PSM_BUFSIZE		960
145178019Sjkim#define	PSM_SMALLBUFSIZE	240
14641016Sdfr
14741016Sdfr/* operation levels */
148178019Sjkim#define	PSM_LEVEL_BASE		0
149178019Sjkim#define	PSM_LEVEL_STANDARD	1
150178019Sjkim#define	PSM_LEVEL_NATIVE	2
151178019Sjkim#define	PSM_LEVEL_MIN		PSM_LEVEL_BASE
152178019Sjkim#define	PSM_LEVEL_MAX		PSM_LEVEL_NATIVE
15341016Sdfr
154343158Swulf/* Active PS/2 multiplexing */
155343158Swulf#define	PSM_NOMUX		(-1)
156343158Swulf
15748778Syokota/* Logitech PS2++ protocol */
158178019Sjkim#define	MOUSE_PS2PLUS_CHECKBITS(b)	\
159178019Sjkim    ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
160178019Sjkim#define	MOUSE_PS2PLUS_PACKET_TYPE(b)	\
161178019Sjkim    (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
16248778Syokota
16341016Sdfr/* ring buffer */
16441016Sdfrtypedef struct ringbuf {
165178019Sjkim	int		count;	/* # of valid elements in the buffer */
166178019Sjkim	int		head;	/* head pointer */
167178019Sjkim	int		tail;	/* tail poiner */
168178019Sjkim	u_char buf[PSM_BUFSIZE];
16941016Sdfr} ringbuf_t;
17041016Sdfr
171123442Salfred/* data buffer */
172123442Salfredtypedef struct packetbuf {
173178019Sjkim	u_char	ipacket[16];	/* interim input buffer */
174178019Sjkim	int	inputbytes;	/* # of bytes in the input buffer */
175123442Salfred} packetbuf_t;
176123442Salfred
177123442Salfred#ifndef PSM_PACKETQUEUE
178178019Sjkim#define	PSM_PACKETQUEUE	128
179123442Salfred#endif
180123442Salfred
181349541Swulf/*
182349541Swulf * Synaptics command definitions.
183349541Swulf */
184349541Swulf#define	SYNAPTICS_READ_IDENTITY			0x00
185349541Swulf#define	SYNAPTICS_READ_MODES			0x01
186349541Swulf#define	SYNAPTICS_READ_CAPABILITIES		0x02
187349541Swulf#define	SYNAPTICS_READ_MODEL_ID			0x03
188349541Swulf#define	SYNAPTICS_READ_SERIAL_PREFIX		0x06
189349541Swulf#define	SYNAPTICS_READ_SERIAL_SUFFIX		0x07
190349541Swulf#define	SYNAPTICS_READ_RESOLUTIONS		0x08
191349541Swulf#define	SYNAPTICS_READ_EXTENDED			0x09
192349541Swulf#define	SYNAPTICS_READ_CAPABILITIES_CONT	0x0c
193349541Swulf#define	SYNAPTICS_READ_MAX_COORDS		0x0d
194349541Swulf#define	SYNAPTICS_READ_DELUXE_LED		0x0e
195349541Swulf#define	SYNAPTICS_READ_MIN_COORDS		0x0f
196349541Swulf
197139982Sphiliptypedef struct synapticsinfo {
198183888Sdumbbell	struct sysctl_ctx_list	 sysctl_ctx;
199178019Sjkim	struct sysctl_oid	*sysctl_tree;
200183888Sdumbbell	int			 directional_scrolls;
201281440Srpaulo	int			 two_finger_scroll;
202183888Sdumbbell	int			 min_pressure;
203183888Sdumbbell	int			 max_pressure;
204183888Sdumbbell	int			 max_width;
205183888Sdumbbell	int			 margin_top;
206183888Sdumbbell	int			 margin_right;
207183888Sdumbbell	int			 margin_bottom;
208183888Sdumbbell	int			 margin_left;
209183888Sdumbbell	int			 na_top;
210183888Sdumbbell	int			 na_right;
211183888Sdumbbell	int			 na_bottom;
212183888Sdumbbell	int			 na_left;
213183888Sdumbbell	int			 window_min;
214183888Sdumbbell	int			 window_max;
215183888Sdumbbell	int			 multiplicator;
216183888Sdumbbell	int			 weight_current;
217183888Sdumbbell	int			 weight_previous;
218183888Sdumbbell	int			 weight_previous_na;
219183888Sdumbbell	int			 weight_len_squared;
220183888Sdumbbell	int			 div_min;
221183888Sdumbbell	int			 div_max;
222183888Sdumbbell	int			 div_max_na;
223183888Sdumbbell	int			 div_len;
224183888Sdumbbell	int			 tap_max_delta;
225183888Sdumbbell	int			 tap_min_queue;
226183888Sdumbbell	int			 taphold_timeout;
227183888Sdumbbell	int			 vscroll_ver_area;
228183888Sdumbbell	int			 vscroll_hor_area;
229183888Sdumbbell	int			 vscroll_min_delta;
230183888Sdumbbell	int			 vscroll_div_min;
231183888Sdumbbell	int			 vscroll_div_max;
232281441Srpaulo	int			 touchpad_off;
233307576Sgonzo	int			 softbuttons_y;
234307576Sgonzo	int			 softbutton2_x;
235307576Sgonzo	int			 softbutton3_x;
236307576Sgonzo	int			 max_x;
237307576Sgonzo	int			 max_y;
238349541Swulf	int			 natural_scroll;
239139982Sphilip} synapticsinfo_t;
240139982Sphilip
241183888Sdumbbelltypedef struct synapticspacket {
242183888Sdumbbell	int			x;
243183888Sdumbbell	int			y;
244183888Sdumbbell} synapticspacket_t;
245183888Sdumbbell
246183888Sdumbbell#define	SYNAPTICS_PACKETQUEUE 10
247183888Sdumbbell#define SYNAPTICS_QUEUE_CURSOR(x)					\
248183888Sdumbbell	(x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
249183888Sdumbbell
250244405Sdumbbell#define	SYNAPTICS_VERSION_GE(synhw, major, minor)			\
251244405Sdumbbell    ((synhw).infoMajor > (major) ||					\
252244405Sdumbbell     ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
253244405Sdumbbell
254307576Sgonzotypedef struct smoother {
255183888Sdumbbell	synapticspacket_t	queue[SYNAPTICS_PACKETQUEUE];
256183888Sdumbbell	int			queue_len;
257183888Sdumbbell	int			queue_cursor;
258183888Sdumbbell	int			start_x;
259183888Sdumbbell	int			start_y;
260183888Sdumbbell	int			avg_dx;
261183888Sdumbbell	int			avg_dy;
262183888Sdumbbell	int			squelch_x;
263183888Sdumbbell	int			squelch_y;
264307576Sgonzo	int			is_fuzzy;
265307576Sgonzo	int			active;
266307576Sgonzo} smoother_t;
267307576Sgonzo
268307576Sgonzotypedef struct gesture {
269307576Sgonzo	int			window_min;
270183888Sdumbbell	int			fingers_nb;
271183888Sdumbbell	int			tap_button;
272183888Sdumbbell	int			in_taphold;
273183888Sdumbbell	int			in_vscroll;
274307576Sgonzo	int			zmax;		/* maximum pressure value */
275307576Sgonzo	struct timeval		taptimeout;	/* tap timeout for touchpads */
276307576Sgonzo} gesture_t;
277183888Sdumbbell
278248478Sjkimenum {
279248478Sjkim	TRACKPOINT_SYSCTL_SENSITIVITY,
280248478Sjkim	TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
281248478Sjkim	TRACKPOINT_SYSCTL_UPPER_PLATEAU,
282248478Sjkim	TRACKPOINT_SYSCTL_BACKUP_RANGE,
283248478Sjkim	TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
284248478Sjkim	TRACKPOINT_SYSCTL_MINIMUM_DRAG,
285248478Sjkim	TRACKPOINT_SYSCTL_UP_THRESHOLD,
286248478Sjkim	TRACKPOINT_SYSCTL_THRESHOLD,
287248478Sjkim	TRACKPOINT_SYSCTL_JENKS_CURVATURE,
288248478Sjkim	TRACKPOINT_SYSCTL_Z_TIME,
289248478Sjkim	TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
290248478Sjkim	TRACKPOINT_SYSCTL_SKIP_BACKUPS
291248478Sjkim};
292248478Sjkim
293248478Sjkimtypedef struct trackpointinfo {
294248478Sjkim	struct sysctl_ctx_list sysctl_ctx;
295248478Sjkim	struct sysctl_oid *sysctl_tree;
296248478Sjkim	int	sensitivity;
297248478Sjkim	int	inertia;
298248478Sjkim	int	uplateau;
299248478Sjkim	int	reach;
300248478Sjkim	int	draghys;
301248478Sjkim	int	mindrag;
302248478Sjkim	int	upthresh;
303248478Sjkim	int	threshold;
304248478Sjkim	int	jenks;
305248478Sjkim	int	ztime;
306248478Sjkim	int	pts;
307248478Sjkim	int	skipback;
308248478Sjkim} trackpointinfo_t;
309248478Sjkim
310307576Sgonzotypedef struct finger {
311307576Sgonzo	int			x;
312307576Sgonzo	int			y;
313307576Sgonzo	int			p;
314307576Sgonzo	int			w;
315307576Sgonzo	int			flags;
316307576Sgonzo} finger_t;
317307576Sgonzo#define	PSM_FINGERS		2	/* # of processed fingers */
318307576Sgonzo#define	PSM_FINGER_IS_PEN	(1<<0)
319307576Sgonzo#define	PSM_FINGER_FUZZY	(1<<1)
320307576Sgonzo#define	PSM_FINGER_DEFAULT_P	tap_threshold
321307576Sgonzo#define	PSM_FINGER_DEFAULT_W	1
322307576Sgonzo#define	PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0)
323307576Sgonzo#define	PSM_FINGER_RESET(f) do { \
324307576Sgonzo	(f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \
325307576Sgonzo} while (0)
326307576Sgonzo
327307576Sgonzotypedef struct elantechhw {
328307576Sgonzo	int			hwversion;
329307576Sgonzo	int			fwversion;
330307576Sgonzo	int			sizex;
331307576Sgonzo	int			sizey;
332307576Sgonzo	int			dpmmx;
333307576Sgonzo	int			dpmmy;
334307576Sgonzo	int			ntracesx;
335307576Sgonzo	int			ntracesy;
336318500Swulf	int			dptracex;
337318500Swulf	int			dptracey;
338307576Sgonzo	int			issemimt;
339307576Sgonzo	int			isclickpad;
340307576Sgonzo	int			hascrc;
341307576Sgonzo	int			hastrackpoint;
342307576Sgonzo	int			haspressure;
343307576Sgonzo} elantechhw_t;
344307576Sgonzo
345307576Sgonzo/* minimum versions supported by this driver */
346307576Sgonzo#define	ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600)
347307576Sgonzo
348307576Sgonzo#define	ELANTECH_MAGIC(magic)				\
349307576Sgonzo	((magic)[0] == 0x3c && (magic)[1] == 0x03 &&	\
350307576Sgonzo	((magic)[2] == 0xc8 || (magic)[2] == 0x00))
351307576Sgonzo
352307576Sgonzo#define	ELANTECH_FW_ID		0x00
353307576Sgonzo#define	ELANTECH_FW_VERSION	0x01
354307576Sgonzo#define	ELANTECH_CAPABILITIES	0x02
355307576Sgonzo#define	ELANTECH_SAMPLE		0x03
356307576Sgonzo#define	ELANTECH_RESOLUTION	0x04
357307576Sgonzo#define	ELANTECH_REG_READ	0x10
358307576Sgonzo#define	ELANTECH_REG_WRITE	0x11
359307576Sgonzo#define	ELANTECH_REG_RDWR	0x00
360307576Sgonzo#define	ELANTECH_CUSTOM_CMD	0xf8
361307576Sgonzo
362321060Swulf#ifdef EVDEV_SUPPORT
363321060Swulf#define	ELANTECH_MAX_FINGERS	5
364321060Swulf#else
365307576Sgonzo#define	ELANTECH_MAX_FINGERS	PSM_FINGERS
366321060Swulf#endif
367307576Sgonzo
368321060Swulf#define	ELANTECH_FINGER_MAX_P	255
369321060Swulf#define	ELANTECH_FINGER_MAX_W	15
370307576Sgonzo#define	ELANTECH_FINGER_SET_XYP(pb) (finger_t) {			\
371307576Sgonzo    .x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2],		\
372307576Sgonzo    .y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5],		\
373307576Sgonzo    .p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f),	\
374307576Sgonzo    .w = PSM_FINGER_DEFAULT_W,						\
375307576Sgonzo    .flags = 0								\
376307576Sgonzo}
377307576Sgonzo
378307576Sgonzoenum {
379307576Sgonzo	ELANTECH_PKT_NOP,
380307576Sgonzo	ELANTECH_PKT_TRACKPOINT,
381307576Sgonzo	ELANTECH_PKT_V2_COMMON,
382307576Sgonzo	ELANTECH_PKT_V2_2FINGER,
383307576Sgonzo	ELANTECH_PKT_V3,
384307576Sgonzo	ELANTECH_PKT_V4_STATUS,
385307576Sgonzo	ELANTECH_PKT_V4_HEAD,
386307576Sgonzo	ELANTECH_PKT_V4_MOTION
387307576Sgonzo};
388307576Sgonzo
389307576Sgonzo#define	ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06)
390307576Sgonzo#define	ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 :	\
391307576Sgonzo    (pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) &&		\
392307576Sgonzo    (pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff &&		\
393307576Sgonzo    (pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff &&		\
394307576Sgonzo    (pb)->ipacket[5] == 0xff)
395307576Sgonzo#define	ELANTECH_PKT_IS_V2(pb) 						\
396307576Sgonzo    (((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02)
397307576Sgonzo#define	ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? 		\
398307576Sgonzo    ((pb)->ipacket[3] & 0x09) == 0x08 : 				\
399307576Sgonzo    ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02)
400307576Sgonzo#define	ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? 		\
401307576Sgonzo    ((pb)->ipacket[3] & 0x09) == 0x09 : 				\
402307576Sgonzo    ((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c)
403307576Sgonzo#define	ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? 			\
404307576Sgonzo    ((pb)->ipacket[3] & 0x08) == 0x00 :					\
405307576Sgonzo    ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10)
406307576Sgonzo
407307576Sgonzotypedef struct elantechaction {
408307576Sgonzo	finger_t		fingers[ELANTECH_MAX_FINGERS];
409307576Sgonzo	int			mask;
410318500Swulf	int			mask_v4wait;
411307576Sgonzo} elantechaction_t;
412307576Sgonzo
41341016Sdfr/* driver control block */
41441016Sdfrstruct psm_softc {		/* Driver status information */
415178019Sjkim	int		unit;
416178019Sjkim	struct selinfo	rsel;		/* Process selecting for Input */
417178019Sjkim	u_char		state;		/* Mouse driver state */
418178019Sjkim	int		config;		/* driver configuration flags */
419178019Sjkim	int		flags;		/* other flags */
420178019Sjkim	KBDC		kbdc;		/* handle to access kbd controller */
421178019Sjkim	struct resource	*intr;		/* IRQ resource */
422178019Sjkim	void		*ih;		/* interrupt handle */
423178019Sjkim	mousehw_t	hw;		/* hardware information */
424178019Sjkim	synapticshw_t	synhw;		/* Synaptics hardware information */
425178019Sjkim	synapticsinfo_t	syninfo;	/* Synaptics configuration */
426307576Sgonzo	smoother_t	smoother[PSM_FINGERS];	/* Motion smoothing */
427307576Sgonzo	gesture_t	gesture;	/* Gesture context */
428307576Sgonzo	elantechhw_t	elanhw;		/* Elantech hardware information */
429307576Sgonzo	elantechaction_t elanaction;	/* Elantech action context */
430248478Sjkim	int		tphw;		/* TrackPoint hardware information */
431248478Sjkim	trackpointinfo_t tpinfo;	/* TrackPoint configuration */
432178019Sjkim	mousemode_t	mode;		/* operation mode */
433178019Sjkim	mousemode_t	dflt_mode;	/* default operation mode */
434178019Sjkim	mousestatus_t	status;		/* accumulated mouse movement */
435178019Sjkim	ringbuf_t	queue;		/* mouse status queue */
436178019Sjkim	packetbuf_t	pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
437178019Sjkim	int		pqueue_start;	/* start of data in queue */
438178019Sjkim	int		pqueue_end;	/* end of data in queue */
439178019Sjkim	int		button;		/* the latest button state */
440178019Sjkim	int		xold;		/* previous absolute X position */
441178019Sjkim	int		yold;		/* previous absolute Y position */
442178019Sjkim	int		xaverage;	/* average X position */
443178019Sjkim	int		yaverage;	/* average Y position */
444178019Sjkim	int		squelch; /* level to filter movement at low speed */
445178019Sjkim	int		syncerrors; /* # of bytes discarded to synchronize */
446178019Sjkim	int		pkterrors;  /* # of packets failed during quaranteen. */
447331170Seadler	int		fpcount;	/* forcePad valid packet counter */
448178019Sjkim	struct timeval	inputtimeout;
449178019Sjkim	struct timeval	lastsoftintr;	/* time of last soft interrupt */
450178019Sjkim	struct timeval	lastinputerr;	/* time last sync error happened */
451307576Sgonzo	struct timeval	idletimeout;
452307576Sgonzo	packetbuf_t	idlepacket;	/* packet to send after idle timeout */
453178019Sjkim	int		watchdog;	/* watchdog timer flag */
454240211Sjhb	struct callout	callout;	/* watchdog timer call out */
455240211Sjhb	struct callout	softcallout; /* buffer timer call out */
456178019Sjkim	struct cdev	*dev;
457178019Sjkim	struct cdev	*bdev;
458178019Sjkim	int		lasterr;
459178019Sjkim	int		cmdcount;
460189870Srnoland	struct sigio	*async;		/* Processes waiting for SIGIO */
461281440Srpaulo	int		extended_buttons;
462343158Swulf	int		muxport;	/* MUX port with attached Synaptics */
463343158Swulf	u_char		muxsave[3];	/* 3->6 byte proto conversion buffer */
464343158Swulf	int		muxtpbuttons;	/* Touchpad button state */
465343158Swulf	int		muxmsbuttons;	/* Mouse (trackpoint) button state */
466343158Swulf	struct timeval	muxmidtimeout;	/* middle button supression timeout */
467321060Swulf#ifdef EVDEV_SUPPORT
468321060Swulf	struct evdev_dev *evdev_a;	/* Absolute reporting device */
469321060Swulf	struct evdev_dev *evdev_r;	/* Relative reporting device */
470321060Swulf#endif
47141016Sdfr};
472114293Smarkmstatic devclass_t psm_devclass;
47341016Sdfr
47441016Sdfr/* driver state flags (state) */
475178019Sjkim#define	PSM_VALID		0x80
476178019Sjkim#define	PSM_OPEN		1	/* Device is open */
477178019Sjkim#define	PSM_ASLP		2	/* Waiting for mouse data */
478178019Sjkim#define	PSM_SOFTARMED		4	/* Software interrupt armed */
479178019Sjkim#define	PSM_NEED_SYNCBITS	8	/* Set syncbits using next data pkt */
480321060Swulf#define	PSM_EV_OPEN_R		0x10	/* Relative evdev device is open */
481321060Swulf#define	PSM_EV_OPEN_A		0x20	/* Absolute evdev device is open */
48241016Sdfr
48341016Sdfr/* driver configuration flags (config) */
484178019Sjkim#define	PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
485178019Sjkim#define	PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
486178019Sjkim#define	PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
487178019Sjkim#define	PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
488178019Sjkim#define	PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
489178019Sjkim#define	PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
490178019Sjkim#define	PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
491178019Sjkim#define	PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
492178019Sjkim#define	PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
49341016Sdfr
494178019Sjkim#define	PSM_CONFIG_FLAGS	\
495178019Sjkim    (PSM_CONFIG_RESOLUTION |	\
496178019Sjkim    PSM_CONFIG_ACCEL |		\
497178019Sjkim    PSM_CONFIG_NOCHECKSYNC |	\
498178019Sjkim    PSM_CONFIG_NOIDPROBE |	\
499178019Sjkim    PSM_CONFIG_NORESET |	\
500178019Sjkim    PSM_CONFIG_FORCETAP |	\
501178019Sjkim    PSM_CONFIG_IGNPORTERROR |	\
502178019Sjkim    PSM_CONFIG_HOOKRESUME |	\
503178019Sjkim    PSM_CONFIG_INITAFTERSUSPEND)
50441016Sdfr
50541016Sdfr/* other flags (flags) */
506178019Sjkim#define	PSM_FLAGS_FINGERDOWN	0x0001	/* VersaPad finger down */
50741016Sdfr
508278787Sgrembo#define kbdcp(p)			((atkbdc_softc_t *)(p))
509278787Sgrembo#define ALWAYS_RESTORE_CONTROLLER(kbdc)	!(kbdcp(kbdc)->quirks \
510278787Sgrembo    & KBDC_QUIRK_KEEP_ACTIVATED)
511278787Sgrembo
512135945Sphilip/* Tunables */
513200674Sdumbbellstatic int tap_enabled = -1;
514307576Sgonzostatic int verbose = PSM_DEBUG;
515255210Seadlerstatic int synaptics_support = 0;
516255210Seadlerstatic int trackpoint_support = 0;
517307576Sgonzostatic int elantech_support = 0;
518362209Swulfstatic int mux_disabled = 0;
519248478Sjkim
52041016Sdfr/* for backward compatibility */
521178019Sjkim#define	OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
522178019Sjkim#define	OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
523178019Sjkim#define	OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
52441016Sdfr
52541016Sdfrtypedef struct old_mousehw {
526178019Sjkim	int	buttons;
527178019Sjkim	int	iftype;
528178019Sjkim	int	type;
529178019Sjkim	int	hwid;
53041016Sdfr} old_mousehw_t;
53141016Sdfr
53241016Sdfrtypedef struct old_mousemode {
533178019Sjkim	int	protocol;
534178019Sjkim	int	rate;
535178019Sjkim	int	resolution;
536178019Sjkim	int	accelfactor;
53741016Sdfr} old_mousemode_t;
53841016Sdfr
539307576Sgonzo#define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field)
540307576Sgonzoenum {
541307576Sgonzo	SYNAPTICS_SYSCTL_MIN_PRESSURE =		SYN_OFFSET(min_pressure),
542307576Sgonzo	SYNAPTICS_SYSCTL_MAX_PRESSURE =		SYN_OFFSET(max_pressure),
543307576Sgonzo	SYNAPTICS_SYSCTL_MAX_WIDTH =		SYN_OFFSET(max_width),
544307576Sgonzo	SYNAPTICS_SYSCTL_MARGIN_TOP =		SYN_OFFSET(margin_top),
545307576Sgonzo	SYNAPTICS_SYSCTL_MARGIN_RIGHT =		SYN_OFFSET(margin_right),
546307576Sgonzo	SYNAPTICS_SYSCTL_MARGIN_BOTTOM =	SYN_OFFSET(margin_bottom),
547307576Sgonzo	SYNAPTICS_SYSCTL_MARGIN_LEFT =		SYN_OFFSET(margin_left),
548307576Sgonzo	SYNAPTICS_SYSCTL_NA_TOP =		SYN_OFFSET(na_top),
549307576Sgonzo	SYNAPTICS_SYSCTL_NA_RIGHT =		SYN_OFFSET(na_right),
550307576Sgonzo	SYNAPTICS_SYSCTL_NA_BOTTOM =		SYN_OFFSET(na_bottom),
551307576Sgonzo	SYNAPTICS_SYSCTL_NA_LEFT = 		SYN_OFFSET(na_left),
552307576Sgonzo	SYNAPTICS_SYSCTL_WINDOW_MIN =		SYN_OFFSET(window_min),
553307576Sgonzo	SYNAPTICS_SYSCTL_WINDOW_MAX =		SYN_OFFSET(window_max),
554307576Sgonzo	SYNAPTICS_SYSCTL_MULTIPLICATOR =	SYN_OFFSET(multiplicator),
555307576Sgonzo	SYNAPTICS_SYSCTL_WEIGHT_CURRENT =	SYN_OFFSET(weight_current),
556307576Sgonzo	SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS =	SYN_OFFSET(weight_previous),
557307576Sgonzo	SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA =	SYN_OFFSET(weight_previous_na),
558307576Sgonzo	SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED =	SYN_OFFSET(weight_len_squared),
559307576Sgonzo	SYNAPTICS_SYSCTL_DIV_MIN =		SYN_OFFSET(div_min),
560307576Sgonzo	SYNAPTICS_SYSCTL_DIV_MAX =		SYN_OFFSET(div_max),
561307576Sgonzo	SYNAPTICS_SYSCTL_DIV_MAX_NA =		SYN_OFFSET(div_max_na),
562307576Sgonzo	SYNAPTICS_SYSCTL_DIV_LEN =		SYN_OFFSET(div_len),
563307576Sgonzo	SYNAPTICS_SYSCTL_TAP_MAX_DELTA =	SYN_OFFSET(tap_max_delta),
564307576Sgonzo	SYNAPTICS_SYSCTL_TAP_MIN_QUEUE =	SYN_OFFSET(tap_min_queue),
565307576Sgonzo	SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT =	SYN_OFFSET(taphold_timeout),
566307576Sgonzo	SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA =	SYN_OFFSET(vscroll_hor_area),
567307576Sgonzo	SYNAPTICS_SYSCTL_VSCROLL_VER_AREA =	SYN_OFFSET(vscroll_ver_area),
568307576Sgonzo	SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA =	SYN_OFFSET(vscroll_min_delta),
569307576Sgonzo	SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN =	SYN_OFFSET(vscroll_div_min),
570307576Sgonzo	SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX =	SYN_OFFSET(vscroll_div_max),
571307576Sgonzo	SYNAPTICS_SYSCTL_TOUCHPAD_OFF =		SYN_OFFSET(touchpad_off),
572307576Sgonzo	SYNAPTICS_SYSCTL_SOFTBUTTONS_Y =	SYN_OFFSET(softbuttons_y),
573307576Sgonzo	SYNAPTICS_SYSCTL_SOFTBUTTON2_X =	SYN_OFFSET(softbutton2_x),
574307576Sgonzo	SYNAPTICS_SYSCTL_SOFTBUTTON3_X =	SYN_OFFSET(softbutton3_x),
575349541Swulf	SYNAPTICS_SYSCTL_NATURAL_SCROLL =	SYN_OFFSET(natural_scroll),
576349541Swulf#define	SYNAPTICS_SYSCTL_LAST	SYNAPTICS_SYSCTL_NATURAL_SCROLL
577307576Sgonzo};
578307576Sgonzo
57941016Sdfr/* packet formatting function */
580178019Sjkimtypedef int	packetfunc_t(struct psm_softc *, u_char *, int *, int,
581178019Sjkim    mousestatus_t *);
58241016Sdfr
58341016Sdfr/* function prototypes */
584178019Sjkimstatic void	psmidentify(driver_t *, device_t);
585178019Sjkimstatic int	psmprobe(device_t);
586178019Sjkimstatic int	psmattach(device_t);
587178019Sjkimstatic int	psmdetach(device_t);
588178019Sjkimstatic int	psmresume(device_t);
58941016Sdfr
590321060Swulfstatic d_open_t		psm_cdev_open;
591321060Swulfstatic d_close_t	psm_cdev_close;
592178019Sjkimstatic d_read_t		psmread;
593178019Sjkimstatic d_write_t	psmwrite;
594178019Sjkimstatic d_ioctl_t	psmioctl;
595178019Sjkimstatic d_poll_t		psmpoll;
59641016Sdfr
597321060Swulfstatic int	psmopen(struct psm_softc *);
598321060Swulfstatic int	psmclose(struct psm_softc *);
599321060Swulf
600321060Swulf#ifdef EVDEV_SUPPORT
601321060Swulfstatic evdev_open_t	psm_ev_open_r;
602321060Swulfstatic evdev_close_t	psm_ev_close_r;
603321060Swulfstatic evdev_open_t	psm_ev_open_a;
604321060Swulfstatic evdev_close_t	psm_ev_close_a;
605321060Swulf#endif
606321060Swulf
607178019Sjkimstatic int	enable_aux_dev(KBDC);
608178019Sjkimstatic int	disable_aux_dev(KBDC);
609178019Sjkimstatic int	get_mouse_status(KBDC, int *, int, int);
610178019Sjkimstatic int	get_aux_id(KBDC);
611178019Sjkimstatic int	set_mouse_sampling_rate(KBDC, int);
612178019Sjkimstatic int	set_mouse_scaling(KBDC, int);
613178019Sjkimstatic int	set_mouse_resolution(KBDC, int);
614178019Sjkimstatic int	set_mouse_mode(KBDC);
615178019Sjkimstatic int	get_mouse_buttons(KBDC);
616178019Sjkimstatic int	is_a_mouse(int);
617178019Sjkimstatic void	recover_from_error(KBDC);
618178019Sjkimstatic int	restore_controller(KBDC, int);
619178019Sjkimstatic int	doinitialize(struct psm_softc *, mousemode_t *);
620178019Sjkimstatic int	doopen(struct psm_softc *, int);
621178019Sjkimstatic int	reinitialize(struct psm_softc *, int);
622178019Sjkimstatic char	*model_name(int);
623178019Sjkimstatic void	psmsoftintr(void *);
624307576Sgonzostatic void	psmsoftintridle(void *);
625178019Sjkimstatic void	psmintr(void *);
626178019Sjkimstatic void	psmtimeout(void *);
627178019Sjkimstatic int	timeelapsed(const struct timeval *, int, int,
628178019Sjkim		    const struct timeval *);
629178019Sjkimstatic void	dropqueue(struct psm_softc *);
630178019Sjkimstatic void	flushpackets(struct psm_softc *);
631178019Sjkimstatic void	proc_mmanplus(struct psm_softc *, packetbuf_t *,
632178019Sjkim		    mousestatus_t *, int *, int *, int *);
633178019Sjkimstatic int	proc_synaptics(struct psm_softc *, packetbuf_t *,
634178019Sjkim		    mousestatus_t *, int *, int *, int *);
635343158Swulfstatic int	proc_synaptics_mux(struct psm_softc *, packetbuf_t *);
636178019Sjkimstatic void	proc_versapad(struct psm_softc *, packetbuf_t *,
637178019Sjkim		    mousestatus_t *, int *, int *, int *);
638307576Sgonzostatic int	proc_elantech(struct psm_softc *, packetbuf_t *,
639307576Sgonzo		    mousestatus_t *, int *, int *, int *);
640307576Sgonzostatic int	psmpalmdetect(struct psm_softc *, finger_t *, int);
641307576Sgonzostatic void	psmgestures(struct psm_softc *, finger_t *, int,
642307576Sgonzo		    mousestatus_t *);
643307576Sgonzostatic void	psmsmoother(struct psm_softc *, finger_t *, int,
644307576Sgonzo		    mousestatus_t *, int *, int *);
645178019Sjkimstatic int	tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
646178019Sjkim		    u_char *);
64741016Sdfr
64841016Sdfr/* vendor specific features */
649284320Sglebiusenum probearg { PROBE, REINIT };
650284320Sglebiustypedef int	probefunc_t(struct psm_softc *, enum probearg);
65141016Sdfr
652178019Sjkimstatic int	mouse_id_proc1(KBDC, int, int, int *);
653178019Sjkimstatic int	mouse_ext_command(KBDC, int);
65441016Sdfr
655178019Sjkimstatic probefunc_t	enable_groller;
656178019Sjkimstatic probefunc_t	enable_gmouse;
657178019Sjkimstatic probefunc_t	enable_aglide;
658178019Sjkimstatic probefunc_t	enable_kmouse;
659178019Sjkimstatic probefunc_t	enable_msexplorer;
660178019Sjkimstatic probefunc_t	enable_msintelli;
661178019Sjkimstatic probefunc_t	enable_4dmouse;
662178019Sjkimstatic probefunc_t	enable_4dplus;
663178019Sjkimstatic probefunc_t	enable_mmanplus;
664178019Sjkimstatic probefunc_t	enable_synaptics;
665343158Swulfstatic probefunc_t	enable_synaptics_mux;
666248478Sjkimstatic probefunc_t	enable_trackpoint;
667178019Sjkimstatic probefunc_t	enable_versapad;
668307576Sgonzostatic probefunc_t	enable_elantech;
669178019Sjkim
670281441Srpaulostatic void set_trackpoint_parameters(struct psm_softc *sc);
671281441Srpaulostatic void synaptics_passthrough_on(struct psm_softc *sc);
672281441Srpaulostatic void synaptics_passthrough_off(struct psm_softc *sc);
673282734Srpaulostatic int synaptics_preferred_mode(struct psm_softc *sc);
674282734Srpaulostatic void synaptics_set_mode(struct psm_softc *sc, int mode_byte);
675281441Srpaulo
67641016Sdfrstatic struct {
677178019Sjkim	int		model;
678178019Sjkim	u_char		syncmask;
679178019Sjkim	int		packetsize;
680178019Sjkim	probefunc_t	*probefunc;
68141016Sdfr} vendortype[] = {
682178019Sjkim	/*
683178019Sjkim	 * WARNING: the order of probe is very important.  Don't mess it
684178019Sjkim	 * unless you know what you are doing.
685178019Sjkim	 */
686343158Swulf	{ MOUSE_MODEL_SYNAPTICS,	/* Synaptics Touchpad on Active Mux */
687343158Swulf	  0x00, MOUSE_PS2_PACKETSIZE, enable_synaptics_mux },
688178019Sjkim	{ MOUSE_MODEL_NET,		/* Genius NetMouse */
689178019Sjkim	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
690178019Sjkim	{ MOUSE_MODEL_NETSCROLL,	/* Genius NetScroll */
691178019Sjkim	  0xc8, 6, enable_groller },
692178019Sjkim	{ MOUSE_MODEL_MOUSEMANPLUS,	/* Logitech MouseMan+ */
693178019Sjkim	  0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
694178019Sjkim	{ MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
695178019Sjkim	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
696178019Sjkim	{ MOUSE_MODEL_4D,		/* A4 Tech 4D Mouse */
697178019Sjkim	  0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
698178019Sjkim	{ MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
699178019Sjkim	  0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
700179474Sphilip	{ MOUSE_MODEL_SYNAPTICS,	/* Synaptics Touchpad */
701179474Sphilip	  0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
702307576Sgonzo	{ MOUSE_MODEL_ELANTECH,		/* Elantech Touchpad */
703307576Sgonzo	  0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech },
704178019Sjkim	{ MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
705178019Sjkim	  0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
706178019Sjkim	{ MOUSE_MODEL_GLIDEPOINT,	/* ALPS GlidePoint */
707178019Sjkim	  0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
708178019Sjkim	{ MOUSE_MODEL_THINK,		/* Kensington ThinkingMouse */
709178019Sjkim	  0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
710178019Sjkim	{ MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
711178019Sjkim	  0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
712248478Sjkim	{ MOUSE_MODEL_TRACKPOINT,	/* IBM/Lenovo TrackPoint */
713248478Sjkim	  0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
714178019Sjkim	{ MOUSE_MODEL_GENERIC,
715178019Sjkim	  0xc0, MOUSE_PS2_PACKETSIZE, NULL },
71641016Sdfr};
717298307Spfg#define	GENERIC_MOUSE_ENTRY (nitems(vendortype) - 1)
71841016Sdfr
71941016Sdfr/* device driver declarateion */
72041016Sdfrstatic device_method_t psm_methods[] = {
72141016Sdfr	/* Device interface */
72283147Syokota	DEVMETHOD(device_identify,	psmidentify),
72341016Sdfr	DEVMETHOD(device_probe,		psmprobe),
72441016Sdfr	DEVMETHOD(device_attach,	psmattach),
72558230Syokota	DEVMETHOD(device_detach,	psmdetach),
72654629Syokota	DEVMETHOD(device_resume,	psmresume),
72741016Sdfr
72841016Sdfr	{ 0, 0 }
72941016Sdfr};
73041016Sdfr
73141016Sdfrstatic driver_t psm_driver = {
732178019Sjkim	PSM_DRIVER_NAME,
733178019Sjkim	psm_methods,
734178019Sjkim	sizeof(struct psm_softc),
73541016Sdfr};
73641016Sdfr
73747625Sphkstatic struct cdevsw psm_cdevsw = {
738126080Sphk	.d_version =	D_VERSION,
739126080Sphk	.d_flags =	D_NEEDGIANT,
740321060Swulf	.d_open =	psm_cdev_open,
741321060Swulf	.d_close =	psm_cdev_close,
742111815Sphk	.d_read =	psmread,
743178017Sjkim	.d_write =	psmwrite,
744111815Sphk	.d_ioctl =	psmioctl,
745111815Sphk	.d_poll =	psmpoll,
746111815Sphk	.d_name =	PSM_DRIVER_NAME,
74741016Sdfr};
74841016Sdfr
749321060Swulf#ifdef EVDEV_SUPPORT
750321060Swulfstatic const struct evdev_methods psm_ev_methods_r = {
751321060Swulf	.ev_open = psm_ev_open_r,
752321060Swulf	.ev_close = psm_ev_close_r,
753321060Swulf};
754321060Swulfstatic const struct evdev_methods psm_ev_methods_a = {
755321060Swulf	.ev_open = psm_ev_open_a,
756321060Swulf	.ev_close = psm_ev_close_a,
757321060Swulf};
758321060Swulf#endif
759321060Swulf
76041016Sdfr/* device I/O routines */
76141016Sdfrstatic int
76241016Sdfrenable_aux_dev(KBDC kbdc)
76341016Sdfr{
764178019Sjkim	int res;
76541016Sdfr
766178019Sjkim	res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
767178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
76841016Sdfr
769178019Sjkim	return (res == PSM_ACK);
77041016Sdfr}
77141016Sdfr
77241016Sdfrstatic int
77341016Sdfrdisable_aux_dev(KBDC kbdc)
77441016Sdfr{
775178019Sjkim	int res;
77641016Sdfr
777178019Sjkim	res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
778178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
77941016Sdfr
780178019Sjkim	return (res == PSM_ACK);
78141016Sdfr}
78241016Sdfr
78341016Sdfrstatic int
78441016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
78541016Sdfr{
786178019Sjkim	int cmd;
787178019Sjkim	int res;
788178019Sjkim	int i;
78941016Sdfr
790178019Sjkim	switch (flag) {
791178019Sjkim	case 0:
792178019Sjkim	default:
793178019Sjkim		cmd = PSMC_SEND_DEV_STATUS;
794178019Sjkim		break;
795178019Sjkim	case 1:
796178019Sjkim		cmd = PSMC_SEND_DEV_DATA;
797178019Sjkim		break;
798178019Sjkim	}
799178019Sjkim	empty_aux_buffer(kbdc, 5);
800178019Sjkim	res = send_aux_command(kbdc, cmd);
801178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
802178019Sjkim	    (flag == 1) ? "DATA" : "STATUS", res));
803178019Sjkim	if (res != PSM_ACK)
804178019Sjkim		return (0);
80541016Sdfr
806178019Sjkim	for (i = 0; i < len; ++i) {
807178019Sjkim		status[i] = read_aux_data(kbdc);
808178019Sjkim		if (status[i] < 0)
809178019Sjkim			break;
810178019Sjkim	}
81141016Sdfr
812178019Sjkim	VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
813178019Sjkim	    (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
81441016Sdfr
815178019Sjkim	return (i);
81641016Sdfr}
81741016Sdfr
81841016Sdfrstatic int
81941016Sdfrget_aux_id(KBDC kbdc)
82041016Sdfr{
821178019Sjkim	int res;
822178019Sjkim	int id;
82341016Sdfr
824178019Sjkim	empty_aux_buffer(kbdc, 5);
825178019Sjkim	res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
826178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
827178019Sjkim	if (res != PSM_ACK)
828178019Sjkim		return (-1);
82941016Sdfr
830178019Sjkim	/* 10ms delay */
831178019Sjkim	DELAY(10000);
83241016Sdfr
833178019Sjkim	id = read_aux_data(kbdc);
834178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
83541016Sdfr
836178019Sjkim	return (id);
83741016Sdfr}
83841016Sdfr
83941016Sdfrstatic int
84041016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
84141016Sdfr{
842178019Sjkim	int res;
84341016Sdfr
844178019Sjkim	res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
845178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
84641016Sdfr
847178019Sjkim	return ((res == PSM_ACK) ? rate : -1);
84841016Sdfr}
84941016Sdfr
85041016Sdfrstatic int
85141016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
85241016Sdfr{
853178019Sjkim	int res;
85441016Sdfr
855178019Sjkim	switch (scale) {
856178019Sjkim	case 1:
857178019Sjkim	default:
858178019Sjkim		scale = PSMC_SET_SCALING11;
859178019Sjkim		break;
860178019Sjkim	case 2:
861178019Sjkim		scale = PSMC_SET_SCALING21;
862178019Sjkim		break;
863178019Sjkim	}
864178019Sjkim	res = send_aux_command(kbdc, scale);
865178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
866178019Sjkim	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
86741016Sdfr
868178019Sjkim	return (res == PSM_ACK);
86941016Sdfr}
87041016Sdfr
87141016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
87241016Sdfrstatic int
87341016Sdfrset_mouse_resolution(KBDC kbdc, int val)
87441016Sdfr{
875178019Sjkim	int res;
87641016Sdfr
877178019Sjkim	res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
878178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
87941016Sdfr
880178019Sjkim	return ((res == PSM_ACK) ? val : -1);
88141016Sdfr}
88241016Sdfr
88341016Sdfr/*
88441016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
88541016Sdfr * re-enabled by calling `enable_aux_dev()'
88641016Sdfr */
88741016Sdfrstatic int
88841016Sdfrset_mouse_mode(KBDC kbdc)
88941016Sdfr{
890178019Sjkim	int res;
89141016Sdfr
892178019Sjkim	res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
893178019Sjkim	VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
89441016Sdfr
895178019Sjkim	return (res == PSM_ACK);
89641016Sdfr}
89741016Sdfr
89841016Sdfrstatic int
89941016Sdfrget_mouse_buttons(KBDC kbdc)
90041016Sdfr{
901178019Sjkim	int c = 2;		/* assume two buttons by default */
902178019Sjkim	int status[3];
90341016Sdfr
904178019Sjkim	/*
905178019Sjkim	 * NOTE: a special sequence to obtain Logitech Mouse specific
906178019Sjkim	 * information: set resolution to 25 ppi, set scaling to 1:1, set
907178019Sjkim	 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
908178019Sjkim	 * mouse status bytes is the number of available buttons.
909178019Sjkim	 * Some manufactures also support this sequence.
910178019Sjkim	 */
911178019Sjkim	if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
912178019Sjkim		return (c);
913178019Sjkim	if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
914178019Sjkim	    set_mouse_scaling(kbdc, 1) &&
915178019Sjkim	    get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
916178019Sjkim		return (status[1]);
917178019Sjkim	return (c);
91841016Sdfr}
91941016Sdfr
92041016Sdfr/* misc subroutines */
92141016Sdfr/*
92241016Sdfr * Someday, I will get the complete list of valid pointing devices and
92341016Sdfr * their IDs... XXX
92441016Sdfr */
92541016Sdfrstatic int
92641016Sdfris_a_mouse(int id)
92741016Sdfr{
92841016Sdfr#if 0
929178019Sjkim	static int valid_ids[] = {
930178019Sjkim		PSM_MOUSE_ID,		/* mouse */
931178019Sjkim		PSM_BALLPOINT_ID,	/* ballpoint device */
932178019Sjkim		PSM_INTELLI_ID,		/* Intellimouse */
933178019Sjkim		PSM_EXPLORER_ID,	/* Intellimouse Explorer */
934178019Sjkim		-1			/* end of table */
935178019Sjkim	};
936178019Sjkim	int i;
93741016Sdfr
938178019Sjkim	for (i = 0; valid_ids[i] >= 0; ++i)
939178019Sjkim	if (valid_ids[i] == id)
940178019Sjkim		return (TRUE);
941178019Sjkim	return (FALSE);
94241016Sdfr#else
943178019Sjkim	return (TRUE);
94441016Sdfr#endif
94541016Sdfr}
94641016Sdfr
94741016Sdfrstatic char *
94841016Sdfrmodel_name(int model)
94941016Sdfr{
950178019Sjkim	static struct {
951178019Sjkim		int	model_code;
952178019Sjkim		char	*model_name;
953178019Sjkim	} models[] = {
954178019Sjkim		{ MOUSE_MODEL_NETSCROLL,	"NetScroll" },
955178019Sjkim		{ MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
956178019Sjkim		{ MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
957178019Sjkim		{ MOUSE_MODEL_THINK,		"ThinkingMouse" },
958178019Sjkim		{ MOUSE_MODEL_INTELLI,		"IntelliMouse" },
959178019Sjkim		{ MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
960178019Sjkim		{ MOUSE_MODEL_VERSAPAD,		"VersaPad" },
961178019Sjkim		{ MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
962178019Sjkim		{ MOUSE_MODEL_4D,		"4D Mouse" },
963178019Sjkim		{ MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
964178019Sjkim		{ MOUSE_MODEL_SYNAPTICS,	"Synaptics Touchpad" },
965248479Sjkim		{ MOUSE_MODEL_TRACKPOINT,	"IBM/Lenovo TrackPoint" },
966307576Sgonzo		{ MOUSE_MODEL_ELANTECH,		"Elantech Touchpad" },
967178019Sjkim		{ MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
968178019Sjkim		{ MOUSE_MODEL_UNKNOWN,		"Unknown" },
969178019Sjkim	};
970178019Sjkim	int i;
97141016Sdfr
972178019Sjkim	for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
973178019Sjkim		if (models[i].model_code == model)
974178019Sjkim			break;
975178019Sjkim	return (models[i].model_name);
97641016Sdfr}
97741016Sdfr
97841016Sdfrstatic void
97941016Sdfrrecover_from_error(KBDC kbdc)
98041016Sdfr{
981178019Sjkim	/* discard anything left in the output buffer */
982178019Sjkim	empty_both_buffers(kbdc, 10);
98341016Sdfr
98441016Sdfr#if 0
985178019Sjkim	/*
986178019Sjkim	 * NOTE: KBDC_RESET_KBD may not restore the communication between the
987178019Sjkim	 * keyboard and the controller.
988178019Sjkim	 */
989178019Sjkim	reset_kbd(kbdc);
99041016Sdfr#else
991178019Sjkim	/*
992178019Sjkim	 * NOTE: somehow diagnostic and keyboard port test commands bring the
993178019Sjkim	 * keyboard back.
994178019Sjkim	 */
995178019Sjkim	if (!test_controller(kbdc))
996178019Sjkim		log(LOG_ERR, "psm: keyboard controller failed.\n");
997178019Sjkim	/* if there isn't a keyboard in the system, the following error is OK */
998178019Sjkim	if (test_kbd_port(kbdc) != 0)
999178019Sjkim		VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
100041016Sdfr#endif
100141016Sdfr}
100241016Sdfr
100341016Sdfrstatic int
100441016Sdfrrestore_controller(KBDC kbdc, int command_byte)
100541016Sdfr{
1006178019Sjkim	empty_both_buffers(kbdc, 10);
100741016Sdfr
1008178019Sjkim	if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
1009178019Sjkim		log(LOG_ERR, "psm: failed to restore the keyboard controller "
1010178019Sjkim		    "command byte.\n");
1011178019Sjkim		empty_both_buffers(kbdc, 10);
1012178019Sjkim		return (FALSE);
1013178019Sjkim	} else {
1014178019Sjkim		empty_both_buffers(kbdc, 10);
1015178019Sjkim		return (TRUE);
1016178019Sjkim	}
101741016Sdfr}
101841016Sdfr
1019178019Sjkim/*
102041016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
1021178019Sjkim * and its interrupt must be disabled before calling this routine.
102241016Sdfr * The aux device will be disabled before returning.
102341016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
102441016Sdfr * calling this routine.
102541016Sdfr */
102641016Sdfrstatic int
102784880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode)
102841016Sdfr{
1029178019Sjkim	KBDC kbdc = sc->kbdc;
1030178019Sjkim	int stat[3];
1031178019Sjkim	int i;
103241016Sdfr
1033178019Sjkim	switch((i = test_aux_port(kbdc))) {
1034178019Sjkim	case 1:	/* ignore these errors */
1035178019Sjkim	case 2:
1036178019Sjkim	case 3:
1037178019Sjkim	case PSM_ACK:
1038178019Sjkim		if (verbose)
1039178019Sjkim			log(LOG_DEBUG,
1040178019Sjkim			    "psm%d: strange result for test aux port (%d).\n",
1041178019Sjkim			    sc->unit, i);
1042178019Sjkim		/* FALLTHROUGH */
1043178019Sjkim	case 0:		/* no error */
1044178019Sjkim		break;
1045178019Sjkim	case -1:	/* time out */
1046178019Sjkim	default:	/* error */
1047178019Sjkim		recover_from_error(kbdc);
1048178019Sjkim		if (sc->config & PSM_CONFIG_IGNPORTERROR)
1049178019Sjkim			break;
1050178019Sjkim		log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
1051178019Sjkim		    sc->unit, i);
1052178019Sjkim		return (FALSE);
1053178019Sjkim	}
105441016Sdfr
1055178019Sjkim	if (sc->config & PSM_CONFIG_NORESET) {
1056178019Sjkim		/*
1057178019Sjkim		 * Don't try to reset the pointing device.  It may possibly
1058178019Sjkim		 * be left in the unknown state, though...
1059178019Sjkim		 */
1060178019Sjkim	} else {
1061178019Sjkim		/*
1062178019Sjkim		 * NOTE: some controllers appears to hang the `keyboard' when
1063178019Sjkim		 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1064178019Sjkim		 */
1065178019Sjkim		if (!reset_aux_dev(kbdc)) {
1066178019Sjkim			recover_from_error(kbdc);
1067178019Sjkim			log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
1068178019Sjkim			    sc->unit);
1069178019Sjkim			return (FALSE);
1070178019Sjkim		}
1071178019Sjkim	}
1072178019Sjkim
1073178019Sjkim	/*
1074178019Sjkim	 * both the aux port and the aux device is functioning, see
1075178019Sjkim	 * if the device can be enabled.
107645789Speter	 */
1077178019Sjkim	if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
1078178019Sjkim		log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
1079178019Sjkim		    sc->unit);
1080178019Sjkim		return (FALSE);
108145789Speter	}
1082178019Sjkim	empty_both_buffers(kbdc, 10);	/* remove stray data if any */
108341016Sdfr
1084233580Sjkim	/* Re-enable the mouse. */
1085233580Sjkim	for (i = 0; vendortype[i].probefunc != NULL; ++i)
1086233580Sjkim		if (vendortype[i].model == sc->hw.model)
1087284320Sglebius			(*vendortype[i].probefunc)(sc, REINIT);
108841016Sdfr
1089178019Sjkim	/* set mouse parameters */
1090178019Sjkim	if (mode != (mousemode_t *)NULL) {
1091178019Sjkim		if (mode->rate > 0)
1092178019Sjkim			mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
1093178019Sjkim		if (mode->resolution >= 0)
1094178019Sjkim			mode->resolution =
1095178019Sjkim			    set_mouse_resolution(kbdc, mode->resolution);
1096178019Sjkim		set_mouse_scaling(kbdc, 1);
1097178019Sjkim		set_mouse_mode(kbdc);
1098178019Sjkim	}
109941016Sdfr
1100178019Sjkim	/* Record sync on the next data packet we see. */
1101178019Sjkim	sc->flags |= PSM_NEED_SYNCBITS;
110241016Sdfr
1103178019Sjkim	/* just check the status of the mouse */
1104178019Sjkim	if (get_mouse_status(kbdc, stat, 0, 3) < 3)
1105178019Sjkim		log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
1106178019Sjkim		    sc->unit);
110741016Sdfr
1108178019Sjkim	return (TRUE);
110941016Sdfr}
111041016Sdfr
111141016Sdfrstatic int
111284880Syokotadoopen(struct psm_softc *sc, int command_byte)
111341016Sdfr{
1114178019Sjkim	int stat[3];
1115343158Swulf	int mux_enabled = FALSE;
111641016Sdfr
1117183888Sdumbbell	/*
1118183888Sdumbbell	 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
1119183888Sdumbbell	 * no obvious reason. Thus we check the current mode and restore the
1120183888Sdumbbell	 * Absolute Mode if it was cleared.
1121183888Sdumbbell	 *
1122183888Sdumbbell	 * The previous hack at the end of psmprobe() wasn't efficient when
1123183888Sdumbbell	 * moused(8) was restarted.
1124183888Sdumbbell	 *
1125183888Sdumbbell	 * A Reset (FF) or Set Defaults (F6) command would clear the
1126183888Sdumbbell	 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
1127183888Sdumbbell	 * doesn't show any evidence of such a command.
1128183888Sdumbbell	 */
1129183888Sdumbbell	if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
1130343158Swulf		if (sc->muxport != PSM_NOMUX) {
1131343158Swulf			mux_enabled = enable_aux_mux(sc->kbdc) >= 0;
1132343158Swulf			if (mux_enabled)
1133343158Swulf				set_active_aux_mux_port(sc->kbdc, sc->muxport);
1134343158Swulf			else
1135343158Swulf				log(LOG_ERR, "psm%d: failed to enable "
1136343158Swulf				    "active multiplexing mode.\n",
1137343158Swulf				    sc->unit);
1138343158Swulf		}
1139349541Swulf		mouse_ext_command(sc->kbdc, SYNAPTICS_READ_MODES);
1140183888Sdumbbell		get_mouse_status(sc->kbdc, stat, 0, 3);
1141244405Sdumbbell		if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
1142343158Swulf		     stat[1] == 0x47) &&
1143282734Srpaulo		     stat[2] == 0x40) {
1144282734Srpaulo			synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1145183888Sdumbbell			VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
1146183888Sdumbbell			    "hopefully restored\n",
1147183888Sdumbbell			    sc->unit));
1148183888Sdumbbell		}
1149343158Swulf		if (mux_enabled)
1150343158Swulf			disable_aux_mux(sc->kbdc);
1151183888Sdumbbell	}
1152183888Sdumbbell
1153200674Sdumbbell	/*
1154200674Sdumbbell	 * A user may want to disable tap and drag gestures on a Synaptics
1155200674Sdumbbell	 * TouchPad when it operates in Relative Mode.
1156200674Sdumbbell	 */
1157200674Sdumbbell	if (sc->hw.model == MOUSE_MODEL_GENERIC) {
1158200674Sdumbbell		if (tap_enabled > 0) {
1159200674Sdumbbell			VLOG(2, (LOG_DEBUG,
1160200674Sdumbbell			    "psm%d: enable tap and drag gestures\n",
1161200674Sdumbbell			    sc->unit));
1162308652Sgonzo			synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1163200674Sdumbbell		} else if (tap_enabled == 0) {
1164200674Sdumbbell			VLOG(2, (LOG_DEBUG,
1165200674Sdumbbell			    "psm%d: disable tap and drag gestures\n",
1166200674Sdumbbell			    sc->unit));
1167308652Sgonzo			synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1168200674Sdumbbell		}
1169200674Sdumbbell	}
1170200674Sdumbbell
1171178019Sjkim	/* enable the mouse device */
1172178019Sjkim	if (!enable_aux_dev(sc->kbdc)) {
1173178019Sjkim		/* MOUSE ERROR: failed to enable the mouse because:
1174178019Sjkim		 * 1) the mouse is faulty,
1175178019Sjkim		 * 2) the mouse has been removed(!?)
1176178019Sjkim		 * In the latter case, the keyboard may have hung, and need
1177178019Sjkim		 * recovery procedure...
1178178019Sjkim		 */
1179178019Sjkim		recover_from_error(sc->kbdc);
118041016Sdfr#if 0
1181178019Sjkim		/* FIXME: we could reset the mouse here and try to enable
1182178019Sjkim		 * it again. But it will take long time and it's not a good
1183178019Sjkim		 * idea to disable the keyboard that long...
1184178019Sjkim		 */
1185178019Sjkim		if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
1186178019Sjkim			recover_from_error(sc->kbdc);
118741016Sdfr#else
1188178019Sjkim		{
118941016Sdfr#endif
1190178019Sjkim			restore_controller(sc->kbdc, command_byte);
1191178019Sjkim			/* mark this device is no longer available */
1192178019Sjkim			sc->state &= ~PSM_VALID;
1193178019Sjkim			log(LOG_ERR,
1194178019Sjkim			    "psm%d: failed to enable the device (doopen).\n",
1195178019Sjkim			sc->unit);
1196178019Sjkim			return (EIO);
1197178019Sjkim		}
119841016Sdfr	}
119941016Sdfr
1200178019Sjkim	if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1201178019Sjkim		log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
1202178019Sjkim		    sc->unit);
120341016Sdfr
1204178019Sjkim	/* enable the aux port and interrupt */
1205178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
120641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
1207178019Sjkim	    (command_byte & KBD_KBD_CONTROL_BITS) |
1208178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
1209178019Sjkim		/* CONTROLLER ERROR */
1210178019Sjkim		disable_aux_dev(sc->kbdc);
1211178019Sjkim		restore_controller(sc->kbdc, command_byte);
1212178019Sjkim		log(LOG_ERR,
1213178019Sjkim		    "psm%d: failed to enable the aux interrupt (doopen).\n",
1214178019Sjkim		    sc->unit);
1215178019Sjkim		return (EIO);
1216178019Sjkim	}
121741016Sdfr
1218178019Sjkim	/* start the watchdog timer */
1219178019Sjkim	sc->watchdog = FALSE;
1220240211Sjhb	callout_reset(&sc->callout, hz * 2, psmtimeout, sc);
122158230Syokota
1222178019Sjkim	return (0);
122341016Sdfr}
122441016Sdfr
122584880Syokotastatic int
122684880Syokotareinitialize(struct psm_softc *sc, int doinit)
122784880Syokota{
1228178019Sjkim	int err;
1229178019Sjkim	int c;
1230178019Sjkim	int s;
123184880Syokota
1232178019Sjkim	/* don't let anybody mess with the aux device */
1233178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
1234178019Sjkim		return (EIO);
1235178019Sjkim	s = spltty();
123684880Syokota
1237178019Sjkim	/* block our watchdog timer */
1238178019Sjkim	sc->watchdog = FALSE;
1239240211Sjhb	callout_stop(&sc->callout);
124084880Syokota
1241178019Sjkim	/* save the current controller command byte */
1242178019Sjkim	empty_both_buffers(sc->kbdc, 10);
1243178019Sjkim	c = get_controller_command_byte(sc->kbdc);
1244178019Sjkim	VLOG(2, (LOG_DEBUG,
1245178019Sjkim	    "psm%d: current command byte: %04x (reinitialize).\n",
1246178019Sjkim	    sc->unit, c));
124784880Syokota
1248178019Sjkim	/* enable the aux port but disable the aux interrupt and the keyboard */
1249178019Sjkim	if ((c == -1) || !set_controller_command_byte(sc->kbdc,
125084880Syokota	    kbdc_get_device_mask(sc->kbdc),
1251178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1252178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1253178019Sjkim		/* CONTROLLER ERROR */
1254178019Sjkim		splx(s);
1255178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
1256178019Sjkim		log(LOG_ERR,
1257178019Sjkim		    "psm%d: unable to set the command byte (reinitialize).\n",
1258178019Sjkim		    sc->unit);
1259178019Sjkim		return (EIO);
1260178019Sjkim	}
126184880Syokota
1262178019Sjkim	/* flush any data */
1263178019Sjkim	if (sc->state & PSM_VALID) {
1264178019Sjkim		/* this may fail; but never mind... */
1265178019Sjkim		disable_aux_dev(sc->kbdc);
1266178019Sjkim		empty_aux_buffer(sc->kbdc, 10);
1267178019Sjkim	}
1268178019Sjkim	flushpackets(sc);
1269178019Sjkim	sc->syncerrors = 0;
1270178019Sjkim	sc->pkterrors = 0;
1271178019Sjkim	memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
127284880Syokota
1273178019Sjkim	/* try to detect the aux device; are you still there? */
1274178019Sjkim	err = 0;
1275178019Sjkim	if (doinit) {
1276178019Sjkim		if (doinitialize(sc, &sc->mode)) {
1277178019Sjkim			/* yes */
1278178019Sjkim			sc->state |= PSM_VALID;
1279178019Sjkim		} else {
1280178019Sjkim			/* the device has gone! */
1281178019Sjkim			restore_controller(sc->kbdc, c);
1282178019Sjkim			sc->state &= ~PSM_VALID;
1283178019Sjkim			log(LOG_ERR,
1284178019Sjkim			    "psm%d: the aux device has gone! (reinitialize).\n",
1285178019Sjkim			    sc->unit);
1286178019Sjkim			err = ENXIO;
1287178019Sjkim		}
128884880Syokota	}
1289178019Sjkim	splx(s);
129084880Syokota
1291178019Sjkim	/* restore the driver state */
1292321060Swulf	if ((sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)) &&
1293321060Swulf	    (err == 0)) {
1294178019Sjkim		/* enable the aux device and the port again */
1295178019Sjkim		err = doopen(sc, c);
1296178019Sjkim		if (err != 0)
1297178019Sjkim			log(LOG_ERR, "psm%d: failed to enable the device "
1298178019Sjkim			    "(reinitialize).\n", sc->unit);
1299178019Sjkim	} else {
1300178019Sjkim		/* restore the keyboard port and disable the aux port */
1301178019Sjkim		if (!set_controller_command_byte(sc->kbdc,
1302178019Sjkim		    kbdc_get_device_mask(sc->kbdc),
1303178019Sjkim		    (c & KBD_KBD_CONTROL_BITS) |
1304178019Sjkim		    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1305178019Sjkim			/* CONTROLLER ERROR */
1306178019Sjkim			log(LOG_ERR, "psm%d: failed to disable the aux port "
1307178019Sjkim			    "(reinitialize).\n", sc->unit);
1308178019Sjkim			err = EIO;
1309178019Sjkim		}
131084880Syokota	}
131184880Syokota
1312178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1313178019Sjkim	return (err);
131484880Syokota}
131584880Syokota
131641016Sdfr/* psm driver entry points */
131741016Sdfr
131883147Syokotastatic void
131983147Syokotapsmidentify(driver_t *driver, device_t parent)
132083147Syokota{
1321178019Sjkim	device_t psmc;
1322178019Sjkim	device_t psm;
1323178019Sjkim	u_long irq;
1324178019Sjkim	int unit;
132583147Syokota
1326178019Sjkim	unit = device_get_unit(parent);
132783931Syokota
1328178019Sjkim	/* always add at least one child */
1329178019Sjkim	psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1330178019Sjkim	if (psm == NULL)
1331178019Sjkim		return;
133283931Syokota
1333178019Sjkim	irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1334178019Sjkim	if (irq > 0)
1335178019Sjkim		return;
133683931Syokota
1337178019Sjkim	/*
1338178019Sjkim	 * If the PS/2 mouse device has already been reported by ACPI or
1339178019Sjkim	 * PnP BIOS, obtain the IRQ resource from it.
1340178019Sjkim	 * (See psmcpnp_attach() below.)
1341178019Sjkim	 */
1342178019Sjkim	psmc = device_find_child(device_get_parent(parent),
1343178019Sjkim	    PSMCPNP_DRIVER_NAME, unit);
1344178019Sjkim	if (psmc == NULL)
1345178019Sjkim		return;
1346178019Sjkim	irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1347178019Sjkim	if (irq <= 0)
1348178019Sjkim		return;
1349216491Sjhb	bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1350178019Sjkim	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
135183147Syokota}
135283147Syokota
1353178019Sjkim#define	endprobe(v)	do {			\
1354178019Sjkim	if (bootverbose)			\
1355178019Sjkim		--verbose;			\
1356178019Sjkim	kbdc_set_device_mask(sc->kbdc, mask);	\
1357178019Sjkim	kbdc_lock(sc->kbdc, FALSE);		\
1358178019Sjkim	return (v);				\
1359178019Sjkim} while (0)
136041016Sdfr
136141016Sdfrstatic int
136241016Sdfrpsmprobe(device_t dev)
136341016Sdfr{
1364178019Sjkim	int unit = device_get_unit(dev);
1365178019Sjkim	struct psm_softc *sc = device_get_softc(dev);
1366178019Sjkim	int stat[3];
1367178019Sjkim	int command_byte;
1368178019Sjkim	int mask;
1369178019Sjkim	int rid;
1370178019Sjkim	int i;
137141016Sdfr
137241016Sdfr#if 0
1373178019Sjkim	kbdc_debug(TRUE);
137441016Sdfr#endif
137558230Syokota
1376178019Sjkim	/* see if IRQ is available */
1377178019Sjkim	rid = KBDC_RID_AUX;
1378216491Sjhb	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1379178019Sjkim	if (sc->intr == NULL) {
1380178019Sjkim		if (bootverbose)
1381178019Sjkim			device_printf(dev, "unable to allocate IRQ\n");
1382178019Sjkim		return (ENXIO);
1383178019Sjkim	}
1384178019Sjkim	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
138558230Syokota
1386178019Sjkim	sc->unit = unit;
1387178019Sjkim	sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1388178019Sjkim	sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1389178019Sjkim	/* XXX: for backward compatibility */
139058230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1391178019Sjkim	sc->config |=
139258230Syokota#ifdef PSM_RESETAFTERSUSPEND
1393233580Sjkim	PSM_CONFIG_INITAFTERSUSPEND;
139458230Syokota#else
139558230Syokota	PSM_CONFIG_HOOKRESUME;
139658230Syokota#endif
139758230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1398178019Sjkim	sc->flags = 0;
1399343158Swulf	sc->muxport = PSM_NOMUX;
1400178019Sjkim	if (bootverbose)
1401178019Sjkim		++verbose;
140241016Sdfr
1403178019Sjkim	device_set_desc(dev, "PS/2 Mouse");
140443105Sdfr
1405178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE)) {
1406178019Sjkim		printf("psm%d: unable to lock the controller.\n", unit);
1407178019Sjkim		if (bootverbose)
1408178019Sjkim			--verbose;
1409178019Sjkim		return (ENXIO);
1410178019Sjkim	}
141141016Sdfr
1412178019Sjkim	/*
1413178019Sjkim	 * NOTE: two bits in the command byte controls the operation of the
1414178019Sjkim	 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1415178019Sjkim	 * port interrupt (IRQ 12) enable bit (bit 2).
1416178019Sjkim	 */
141741016Sdfr
1418178019Sjkim	/* discard anything left after the keyboard initialization */
1419178019Sjkim	empty_both_buffers(sc->kbdc, 10);
142041016Sdfr
1421178019Sjkim	/* save the current command byte; it will be used later */
1422178019Sjkim	mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1423178019Sjkim	command_byte = get_controller_command_byte(sc->kbdc);
1424178019Sjkim	if (verbose)
1425178019Sjkim		printf("psm%d: current command byte:%04x\n", unit,
1426178019Sjkim		    command_byte);
1427178019Sjkim	if (command_byte == -1) {
1428178019Sjkim		/* CONTROLLER ERROR */
1429178019Sjkim		printf("psm%d: unable to get the current command byte value.\n",
1430178019Sjkim			unit);
1431178019Sjkim		endprobe(ENXIO);
1432178019Sjkim	}
143341016Sdfr
1434178019Sjkim	/*
1435178019Sjkim	 * disable the keyboard port while probing the aux port, which must be
1436178019Sjkim	 * enabled during this routine
1437178019Sjkim	 */
1438178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
143941016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1440178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1441178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1442178019Sjkim		/*
1443178019Sjkim		 * this is CONTROLLER ERROR; I don't know how to recover
1444178019Sjkim		 * from this error...
1445178019Sjkim		 */
1446278787Sgrembo		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1447278787Sgrembo			restore_controller(sc->kbdc, command_byte);
1448178019Sjkim		printf("psm%d: unable to set the command byte.\n", unit);
1449178019Sjkim		endprobe(ENXIO);
1450178019Sjkim	}
1451178019Sjkim	write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
145241016Sdfr
145345789Speter	/*
1454178019Sjkim	 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1455178019Sjkim	 * port exists and is functioning. However, some controllers appears
1456178019Sjkim	 * to respond with zero even when the aux port doesn't exist. (It may
1457178019Sjkim	 * be that this is only the case when the controller DOES have the aux
1458178019Sjkim	 * port but the port is not wired on the motherboard.) The keyboard
1459178019Sjkim	 * controllers without the port, such as the original AT, are
1460215473Sjhb	 * supposed to return with an error code or simply time out. In any
1461178019Sjkim	 * case, we have to continue probing the port even when the controller
1462178019Sjkim	 * passes this test.
1463117302Smikeh	 *
1464178019Sjkim	 * XXX: some controllers erroneously return the error code 1, 2 or 3
1465215473Sjhb	 * when it has a perfectly functional aux port. We have to ignore
1466178019Sjkim	 * this error code. Even if the controller HAS error with the aux
1467178019Sjkim	 * port, it will be detected later...
1468178019Sjkim	 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
146945789Speter	 */
1470178019Sjkim	switch ((i = test_aux_port(sc->kbdc))) {
1471178019Sjkim	case 1:		/* ignore these errors */
1472178019Sjkim	case 2:
1473178019Sjkim	case 3:
1474178019Sjkim	case PSM_ACK:
1475178019Sjkim		if (verbose)
1476178019Sjkim			printf("psm%d: strange result for test aux port "
1477178019Sjkim			    "(%d).\n", unit, i);
1478178019Sjkim		/* FALLTHROUGH */
1479178019Sjkim	case 0:		/* no error */
1480178019Sjkim		break;
1481178019Sjkim	case -1:	/* time out */
1482178019Sjkim	default:	/* error */
1483178019Sjkim		recover_from_error(sc->kbdc);
1484178019Sjkim		if (sc->config & PSM_CONFIG_IGNPORTERROR)
1485178019Sjkim			break;
1486278787Sgrembo		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1487278787Sgrembo			restore_controller(sc->kbdc, command_byte);
1488178019Sjkim		if (verbose)
1489178019Sjkim			printf("psm%d: the aux port is not functioning (%d).\n",
1490178019Sjkim			    unit, i);
1491178019Sjkim		endprobe(ENXIO);
149245789Speter	}
149345789Speter
1494178019Sjkim	if (sc->config & PSM_CONFIG_NORESET) {
1495178019Sjkim		/*
1496178019Sjkim		 * Don't try to reset the pointing device.  It may possibly be
1497215473Sjhb		 * left in an unknown state, though...
1498178019Sjkim		 */
1499178019Sjkim	} else {
1500178019Sjkim		/*
1501178019Sjkim		 * NOTE: some controllers appears to hang the `keyboard' when
1502178019Sjkim		 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1503178019Sjkim		 *
1504178019Sjkim		 * Attempt to reset the controller twice -- this helps
1505178019Sjkim		 * pierce through some KVM switches. The second reset
1506178019Sjkim		 * is non-fatal.
1507178019Sjkim		 */
1508178019Sjkim		if (!reset_aux_dev(sc->kbdc)) {
1509178019Sjkim			recover_from_error(sc->kbdc);
1510278787Sgrembo			if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1511278787Sgrembo				restore_controller(sc->kbdc, command_byte);
1512178019Sjkim			if (verbose)
1513178019Sjkim				printf("psm%d: failed to reset the aux "
1514178019Sjkim				    "device.\n", unit);
1515178019Sjkim			endprobe(ENXIO);
1516178019Sjkim		} else if (!reset_aux_dev(sc->kbdc)) {
1517178019Sjkim			recover_from_error(sc->kbdc);
1518178019Sjkim			if (verbose >= 2)
1519178019Sjkim				printf("psm%d: failed to reset the aux device "
1520178019Sjkim				    "(2).\n", unit);
1521178019Sjkim		}
1522178019Sjkim	}
152341016Sdfr
1524178019Sjkim	/*
1525215473Sjhb	 * both the aux port and the aux device are functioning, see if the
1526178019Sjkim	 * device can be enabled. NOTE: when enabled, the device will start
1527178019Sjkim	 * sending data; we shall immediately disable the device once we know
1528178019Sjkim	 * the device can be enabled.
1529178019Sjkim	 */
1530178019Sjkim	if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1531178019Sjkim		/* MOUSE ERROR */
1532178019Sjkim		recover_from_error(sc->kbdc);
1533278787Sgrembo		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1534278787Sgrembo			restore_controller(sc->kbdc, command_byte);
1535178019Sjkim		if (verbose)
1536178019Sjkim			printf("psm%d: failed to enable the aux device.\n",
1537178019Sjkim			    unit);
1538178019Sjkim		endprobe(ENXIO);
1539178019Sjkim	}
154041016Sdfr
1541178019Sjkim	/* save the default values after reset */
1542178019Sjkim	if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1543178019Sjkim		sc->dflt_mode.rate = sc->mode.rate = stat[2];
1544178019Sjkim		sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1545178019Sjkim	} else {
1546178019Sjkim		sc->dflt_mode.rate = sc->mode.rate = -1;
1547178019Sjkim		sc->dflt_mode.resolution = sc->mode.resolution = -1;
1548178019Sjkim	}
154941016Sdfr
1550178019Sjkim	/* hardware information */
1551178019Sjkim	sc->hw.iftype = MOUSE_IF_PS2;
155241016Sdfr
1553178019Sjkim	/* verify the device is a mouse */
1554178019Sjkim	sc->hw.hwid = get_aux_id(sc->kbdc);
1555178019Sjkim	if (!is_a_mouse(sc->hw.hwid)) {
1556278787Sgrembo		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1557278787Sgrembo			restore_controller(sc->kbdc, command_byte);
1558178019Sjkim		if (verbose)
1559178019Sjkim			printf("psm%d: unknown device type (%d).\n", unit,
1560178019Sjkim			    sc->hw.hwid);
1561178019Sjkim		endprobe(ENXIO);
1562178019Sjkim	}
1563178019Sjkim	switch (sc->hw.hwid) {
1564178019Sjkim	case PSM_BALLPOINT_ID:
1565178019Sjkim		sc->hw.type = MOUSE_TRACKBALL;
156645789Speter		break;
1567178019Sjkim	case PSM_MOUSE_ID:
1568178019Sjkim	case PSM_INTELLI_ID:
1569178019Sjkim	case PSM_EXPLORER_ID:
1570178019Sjkim	case PSM_4DMOUSE_ID:
1571178019Sjkim	case PSM_4DPLUS_ID:
1572178019Sjkim		sc->hw.type = MOUSE_MOUSE;
1573178019Sjkim		break;
1574178019Sjkim	default:
1575178019Sjkim		sc->hw.type = MOUSE_UNKNOWN;
1576178019Sjkim		break;
157741016Sdfr	}
157841016Sdfr
1579178019Sjkim	if (sc->config & PSM_CONFIG_NOIDPROBE) {
1580178019Sjkim		sc->hw.buttons = 2;
1581178019Sjkim		i = GENERIC_MOUSE_ENTRY;
1582178019Sjkim	} else {
1583178019Sjkim		/* # of buttons */
1584178019Sjkim		sc->hw.buttons = get_mouse_buttons(sc->kbdc);
158541016Sdfr
1586178019Sjkim		/* other parameters */
1587178019Sjkim		for (i = 0; vendortype[i].probefunc != NULL; ++i)
1588284320Sglebius			if ((*vendortype[i].probefunc)(sc, PROBE)) {
1589178019Sjkim				if (verbose >= 2)
1590178019Sjkim					printf("psm%d: found %s\n", unit,
1591178019Sjkim					    model_name(vendortype[i].model));
1592178019Sjkim				break;
1593178019Sjkim			}
1594178019Sjkim	}
159541016Sdfr
1596178019Sjkim	sc->hw.model = vendortype[i].model;
1597178019Sjkim
1598178019Sjkim	sc->dflt_mode.level = PSM_LEVEL_BASE;
1599178019Sjkim	sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1600178019Sjkim	sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1601178019Sjkim	if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1602178019Sjkim		sc->dflt_mode.syncmask[0] = 0;
1603178019Sjkim	else
1604178019Sjkim		sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1605178019Sjkim	if (sc->config & PSM_CONFIG_FORCETAP)
1606178019Sjkim		sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1607178019Sjkim	sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
1608178019Sjkim	sc->mode = sc->dflt_mode;
1609178019Sjkim	sc->mode.packetsize = vendortype[i].packetsize;
1610178019Sjkim
1611178019Sjkim	/* set mouse parameters */
161248773Syokota#if 0
1613178019Sjkim	/*
1614178019Sjkim	 * A version of Logitech FirstMouse+ won't report wheel movement,
1615178019Sjkim	 * if SET_DEFAULTS is sent...  Don't use this command.
1616178019Sjkim	 * This fix was found by Takashi Nishida.
1617178019Sjkim	 */
1618178019Sjkim	i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1619178019Sjkim	if (verbose >= 2)
1620178019Sjkim		printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
162148773Syokota#endif
1622178019Sjkim	if (sc->config & PSM_CONFIG_RESOLUTION)
1623178019Sjkim		sc->mode.resolution =
1624178019Sjkim		    set_mouse_resolution(sc->kbdc,
1625178019Sjkim		    (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1626178019Sjkim	else if (sc->mode.resolution >= 0)
1627178019Sjkim		sc->mode.resolution =
1628178019Sjkim		    set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1629178019Sjkim	if (sc->mode.rate > 0)
1630178019Sjkim		sc->mode.rate =
1631178019Sjkim		    set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1632178019Sjkim	set_mouse_scaling(sc->kbdc, 1);
163341016Sdfr
1634178019Sjkim	/* Record sync on the next data packet we see. */
1635178019Sjkim	sc->flags |= PSM_NEED_SYNCBITS;
163641016Sdfr
1637178019Sjkim	/* just check the status of the mouse */
1638178019Sjkim	/*
1639178019Sjkim	 * NOTE: XXX there are some arcane controller/mouse combinations out
1640178019Sjkim	 * there, which hung the controller unless there is data transmission
1641178019Sjkim	 * after ACK from the mouse.
164241016Sdfr	 */
1643178019Sjkim	if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1644178019Sjkim		printf("psm%d: failed to get status.\n", unit);
1645178019Sjkim	else {
1646178019Sjkim		/*
1647178019Sjkim		 * When in its native mode, some mice operate with different
1648178019Sjkim		 * default parameters than in the PS/2 compatible mode.
1649178019Sjkim		 */
1650178019Sjkim		sc->dflt_mode.rate = sc->mode.rate = stat[2];
1651178019Sjkim		sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1652178019Sjkim	}
165341016Sdfr
1654178019Sjkim	/* disable the aux port for now... */
1655178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
165641016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1657178019Sjkim	    (command_byte & KBD_KBD_CONTROL_BITS) |
1658178019Sjkim	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1659178019Sjkim		/*
1660178019Sjkim		 * this is CONTROLLER ERROR; I don't know the proper way to
1661178019Sjkim		 * recover from this error...
1662178019Sjkim		 */
1663278787Sgrembo		if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1664278787Sgrembo			restore_controller(sc->kbdc, command_byte);
1665178019Sjkim		printf("psm%d: unable to set the command byte.\n", unit);
1666178019Sjkim		endprobe(ENXIO);
1667178019Sjkim	}
1668178019Sjkim
1669178019Sjkim	/* done */
1670178019Sjkim	kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1671178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
1672178019Sjkim	return (0);
167341016Sdfr}
167441016Sdfr
1675321060Swulf#ifdef EVDEV_SUPPORT
1676321060Swulf/* Values are taken from Linux drivers for userland software compatibility */
1677321060Swulf#define	PS2_MOUSE_VENDOR		0x0002
1678321060Swulf#define	PS2_MOUSE_GENERIC_PRODUCT	0x0001
1679321060Swulf#define	PS2_MOUSE_SYNAPTICS_NAME	"SynPS/2 Synaptics TouchPad"
1680321060Swulf#define	PS2_MOUSE_SYNAPTICS_PRODUCT	0x0007
1681321060Swulf#define	PS2_MOUSE_TRACKPOINT_NAME	"TPPS/2 IBM TrackPoint"
1682321060Swulf#define	PS2_MOUSE_TRACKPOINT_PRODUCT	0x000A
1683321060Swulf#define	PS2_MOUSE_ELANTECH_NAME		"ETPS/2 Elantech Touchpad"
1684321060Swulf#define	PS2_MOUSE_ELANTECH_ST_NAME	"ETPS/2 Elantech TrackPoint"
1685321060Swulf#define	PS2_MOUSE_ELANTECH_PRODUCT	0x000E
1686321060Swulf
1687321060Swulf#define	ABSINFO_END	{ ABS_CNT, 0, 0, 0 }
1688321060Swulf
1689321060Swulfstatic void
1690321060Swulfpsm_support_abs_bulk(struct evdev_dev *evdev, const uint16_t info[][4])
1691321060Swulf{
1692321060Swulf	size_t i;
1693321060Swulf
1694321060Swulf	for (i = 0; info[i][0] != ABS_CNT; i++)
1695321060Swulf		evdev_support_abs(evdev, info[i][0], 0, info[i][1], info[i][2],
1696321060Swulf		    0, 0, info[i][3]);
1697321060Swulf}
1698321060Swulf
1699321060Swulfstatic void
1700321060Swulfpsm_push_mt_finger(struct psm_softc *sc, int id, const finger_t *f)
1701321060Swulf{
1702321060Swulf	int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1703321060Swulf
1704321060Swulf	evdev_push_abs(sc->evdev_a, ABS_MT_SLOT, id);
1705321060Swulf	evdev_push_abs(sc->evdev_a, ABS_MT_TRACKING_ID, id);
1706321060Swulf	evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_X, f->x);
1707321060Swulf	evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_Y, y);
1708321060Swulf	evdev_push_abs(sc->evdev_a, ABS_MT_PRESSURE, f->p);
1709321060Swulf}
1710321060Swulf
1711321060Swulfstatic void
1712321060Swulfpsm_push_st_finger(struct psm_softc *sc, const finger_t *f)
1713321060Swulf{
1714321060Swulf	int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1715321060Swulf
1716321060Swulf	evdev_push_abs(sc->evdev_a, ABS_X, f->x);
1717321060Swulf	evdev_push_abs(sc->evdev_a, ABS_Y, y);
1718321060Swulf	evdev_push_abs(sc->evdev_a, ABS_PRESSURE, f->p);
1719321060Swulf	if (sc->synhw.capPalmDetect)
1720321060Swulf		evdev_push_abs(sc->evdev_a, ABS_TOOL_WIDTH, f->w);
1721321060Swulf}
1722321060Swulf
1723321060Swulfstatic void
1724321060Swulfpsm_release_mt_slot(struct evdev_dev *evdev, int32_t slot)
1725321060Swulf{
1726321060Swulf
1727321060Swulf	evdev_push_abs(evdev, ABS_MT_SLOT, slot);
1728321060Swulf	evdev_push_abs(evdev, ABS_MT_TRACKING_ID, -1);
1729321060Swulf}
1730321060Swulf
173141016Sdfrstatic int
1732321060Swulfpsm_register(device_t dev, int model_code)
1733321060Swulf{
1734321060Swulf	struct psm_softc *sc = device_get_softc(dev);
1735321060Swulf	struct evdev_dev *evdev_r;
1736321060Swulf	int error, i, nbuttons, nwheels, product;
1737321060Swulf	bool is_pointing_stick;
1738321060Swulf	const char *name;
1739321060Swulf
1740321060Swulf	name = model_name(model_code);
1741321060Swulf	nbuttons = sc->hw.buttons;
1742321060Swulf	product = PS2_MOUSE_GENERIC_PRODUCT;
1743321060Swulf	nwheels = 0;
1744321060Swulf	is_pointing_stick = false;
1745321060Swulf
1746321060Swulf	switch (model_code) {
1747321060Swulf	case MOUSE_MODEL_TRACKPOINT:
1748321060Swulf		name = PS2_MOUSE_TRACKPOINT_NAME;
1749321060Swulf		product = PS2_MOUSE_TRACKPOINT_PRODUCT;
1750321060Swulf		nbuttons = 3;
1751321060Swulf		is_pointing_stick = true;
1752321060Swulf		break;
1753321060Swulf
1754321060Swulf	case MOUSE_MODEL_ELANTECH:
1755321060Swulf		name = PS2_MOUSE_ELANTECH_ST_NAME;
1756321060Swulf		product = PS2_MOUSE_ELANTECH_PRODUCT;
1757321060Swulf		nbuttons = 3;
1758321060Swulf		is_pointing_stick = true;
1759321060Swulf		break;
1760321060Swulf
1761321060Swulf	case MOUSE_MODEL_MOUSEMANPLUS:
1762321060Swulf	case MOUSE_MODEL_4D:
1763321060Swulf		nwheels = 2;
1764321060Swulf		break;
1765321060Swulf
1766321060Swulf	case MOUSE_MODEL_EXPLORER:
1767321060Swulf	case MOUSE_MODEL_INTELLI:
1768321060Swulf	case MOUSE_MODEL_NET:
1769321060Swulf	case MOUSE_MODEL_NETSCROLL:
1770321060Swulf	case MOUSE_MODEL_4DPLUS:
1771321060Swulf		nwheels = 1;
1772321060Swulf		break;
1773321060Swulf	}
1774321060Swulf
1775321060Swulf	evdev_r = evdev_alloc();
1776321060Swulf	evdev_set_name(evdev_r, name);
1777321060Swulf	evdev_set_phys(evdev_r, device_get_nameunit(dev));
1778321060Swulf	evdev_set_id(evdev_r, BUS_I8042, PS2_MOUSE_VENDOR, product, 0);
1779321060Swulf	evdev_set_methods(evdev_r, sc, &psm_ev_methods_r);
1780321060Swulf
1781321060Swulf	evdev_support_prop(evdev_r, INPUT_PROP_POINTER);
1782321060Swulf	if (is_pointing_stick)
1783321060Swulf		evdev_support_prop(evdev_r, INPUT_PROP_POINTING_STICK);
1784321060Swulf	evdev_support_event(evdev_r, EV_SYN);
1785321060Swulf	evdev_support_event(evdev_r, EV_KEY);
1786321060Swulf	evdev_support_event(evdev_r, EV_REL);
1787321060Swulf	evdev_support_rel(evdev_r, REL_X);
1788321060Swulf	evdev_support_rel(evdev_r, REL_Y);
1789321060Swulf	switch (nwheels) {
1790321060Swulf	case 2:
1791321060Swulf		evdev_support_rel(evdev_r, REL_HWHEEL);
1792321060Swulf		/* FALLTHROUGH */
1793321060Swulf	case 1:
1794321060Swulf		evdev_support_rel(evdev_r, REL_WHEEL);
1795321060Swulf	}
1796321060Swulf	for (i = 0; i < nbuttons; i++)
1797321060Swulf		evdev_support_key(evdev_r, BTN_MOUSE + i);
1798321060Swulf
1799321060Swulf	error = evdev_register_mtx(evdev_r, &Giant);
1800321060Swulf	if (error)
1801321060Swulf		evdev_free(evdev_r);
1802321060Swulf	else
1803321060Swulf		sc->evdev_r = evdev_r;
1804321060Swulf	return (error);
1805321060Swulf}
1806321060Swulf
1807321060Swulfstatic int
1808321060Swulfpsm_register_synaptics(device_t dev)
1809321060Swulf{
1810321060Swulf	struct psm_softc *sc = device_get_softc(dev);
1811321060Swulf	const uint16_t synaptics_absinfo_st[][4] = {
1812321060Swulf		{ ABS_X,		sc->synhw.minimumXCoord,
1813321060Swulf		    sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1814321060Swulf		{ ABS_Y,		sc->synhw.minimumYCoord,
1815321060Swulf		    sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1816321060Swulf		{ ABS_PRESSURE,		0, ELANTECH_FINGER_MAX_P, 0 },
1817321060Swulf		ABSINFO_END,
1818321060Swulf	};
1819321060Swulf	const uint16_t synaptics_absinfo_mt[][4] = {
1820321060Swulf		{ ABS_MT_SLOT,		0, PSM_FINGERS-1, 0},
1821321060Swulf		{ ABS_MT_TRACKING_ID,	-1, PSM_FINGERS-1, 0},
1822321060Swulf		{ ABS_MT_POSITION_X,	sc->synhw.minimumXCoord,
1823321060Swulf		    sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1824321060Swulf		{ ABS_MT_POSITION_Y,	sc->synhw.minimumYCoord,
1825321060Swulf		    sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1826321060Swulf		{ ABS_MT_PRESSURE,	0, ELANTECH_FINGER_MAX_P, 0 },
1827321060Swulf		ABSINFO_END,
1828321060Swulf	};
1829321060Swulf	struct evdev_dev *evdev_a;
1830321060Swulf	int error, i, guest_model;
1831321060Swulf
1832321060Swulf	evdev_a = evdev_alloc();
1833321060Swulf	evdev_set_name(evdev_a, PS2_MOUSE_SYNAPTICS_NAME);
1834321060Swulf	evdev_set_phys(evdev_a, device_get_nameunit(dev));
1835321060Swulf	evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1836321060Swulf	    PS2_MOUSE_SYNAPTICS_PRODUCT, 0);
1837321060Swulf	evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1838321060Swulf
1839321060Swulf	evdev_support_event(evdev_a, EV_SYN);
1840321060Swulf	evdev_support_event(evdev_a, EV_KEY);
1841321060Swulf	evdev_support_event(evdev_a, EV_ABS);
1842321060Swulf	evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1843321060Swulf	if (sc->synhw.capAdvancedGestures)
1844321060Swulf		evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1845321060Swulf	if (sc->synhw.capClickPad)
1846321060Swulf		evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1847344165Swulf	if (sc->synhw.capClickPad && sc->synhw.topButtonPad)
1848344165Swulf		evdev_support_prop(evdev_a, INPUT_PROP_TOPBUTTONPAD);
1849321060Swulf	evdev_support_key(evdev_a, BTN_TOUCH);
1850349541Swulf	evdev_support_nfingers(evdev_a, sc->synhw.capReportsV ? 5 : 3);
1851321060Swulf	psm_support_abs_bulk(evdev_a, synaptics_absinfo_st);
1852321060Swulf	if (sc->synhw.capAdvancedGestures || sc->synhw.capReportsV)
1853321060Swulf		psm_support_abs_bulk(evdev_a, synaptics_absinfo_mt);
1854321060Swulf	if (sc->synhw.capPalmDetect)
1855321060Swulf		evdev_support_abs(evdev_a, ABS_TOOL_WIDTH, 0, 0, 15, 0, 0, 0);
1856321060Swulf	evdev_support_key(evdev_a, BTN_LEFT);
1857321060Swulf	if (!sc->synhw.capClickPad) {
1858321060Swulf		evdev_support_key(evdev_a, BTN_RIGHT);
1859321060Swulf		if (sc->synhw.capExtended && sc->synhw.capMiddle)
1860321060Swulf			evdev_support_key(evdev_a, BTN_MIDDLE);
1861321060Swulf	}
1862321060Swulf	if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
1863321060Swulf		evdev_support_key(evdev_a, BTN_BACK);
1864321060Swulf		evdev_support_key(evdev_a, BTN_FORWARD);
1865321060Swulf	}
1866321060Swulf	if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0))
1867321060Swulf		for (i = 0; i < sc->synhw.nExtendedButtons; i++)
1868321060Swulf			evdev_support_key(evdev_a, BTN_0 + i);
1869321060Swulf
1870321060Swulf	error = evdev_register_mtx(evdev_a, &Giant);
1871343158Swulf	if (!error && (sc->synhw.capPassthrough || sc->muxport != PSM_NOMUX)) {
1872321060Swulf		guest_model = sc->tpinfo.sysctl_tree != NULL ?
1873321060Swulf		    MOUSE_MODEL_TRACKPOINT : MOUSE_MODEL_GENERIC;
1874321060Swulf		error = psm_register(dev, guest_model);
1875321060Swulf	}
1876321060Swulf	if (error)
1877321060Swulf		evdev_free(evdev_a);
1878321060Swulf	else
1879321060Swulf		sc->evdev_a = evdev_a;
1880321060Swulf	return (error);
1881321060Swulf}
1882321060Swulf
1883321060Swulfstatic int
1884321060Swulfpsm_register_elantech(device_t dev)
1885321060Swulf{
1886321060Swulf	struct psm_softc *sc = device_get_softc(dev);
1887321060Swulf	const uint16_t elantech_absinfo[][4] = {
1888321060Swulf		{ ABS_X,		0, sc->elanhw.sizex,
1889321060Swulf					   sc->elanhw.dpmmx },
1890321060Swulf		{ ABS_Y,		0, sc->elanhw.sizey,
1891321060Swulf					   sc->elanhw.dpmmy },
1892321060Swulf		{ ABS_PRESSURE,		0, ELANTECH_FINGER_MAX_P, 0 },
1893321060Swulf		{ ABS_TOOL_WIDTH,	0, ELANTECH_FINGER_MAX_W, 0 },
1894321060Swulf		{ ABS_MT_SLOT,		0, ELANTECH_MAX_FINGERS - 1, 0 },
1895321060Swulf		{ ABS_MT_TRACKING_ID,	-1, ELANTECH_MAX_FINGERS - 1, 0 },
1896321060Swulf		{ ABS_MT_POSITION_X,	0, sc->elanhw.sizex,
1897321060Swulf					   sc->elanhw.dpmmx },
1898321060Swulf		{ ABS_MT_POSITION_Y,	0, sc->elanhw.sizey,
1899321060Swulf					   sc->elanhw.dpmmy },
1900321060Swulf		{ ABS_MT_PRESSURE,	0, ELANTECH_FINGER_MAX_P, 0 },
1901321060Swulf		{ ABS_MT_TOUCH_MAJOR,	0, ELANTECH_FINGER_MAX_W *
1902321060Swulf					   sc->elanhw.dptracex, 0 },
1903321060Swulf		ABSINFO_END,
1904321060Swulf	};
1905321060Swulf	struct evdev_dev *evdev_a;
1906321060Swulf	int error;
1907321060Swulf
1908321060Swulf	evdev_a = evdev_alloc();
1909321060Swulf	evdev_set_name(evdev_a, PS2_MOUSE_ELANTECH_NAME);
1910321060Swulf	evdev_set_phys(evdev_a, device_get_nameunit(dev));
1911321060Swulf	evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1912321060Swulf	    PS2_MOUSE_ELANTECH_PRODUCT, 0);
1913321060Swulf	evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1914321060Swulf
1915321060Swulf	evdev_support_event(evdev_a, EV_SYN);
1916321060Swulf	evdev_support_event(evdev_a, EV_KEY);
1917321060Swulf	evdev_support_event(evdev_a, EV_ABS);
1918321060Swulf	evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1919321060Swulf	if (sc->elanhw.issemimt)
1920321060Swulf		evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1921321060Swulf	if (sc->elanhw.isclickpad)
1922321060Swulf		evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1923321060Swulf	evdev_support_key(evdev_a, BTN_TOUCH);
1924321060Swulf	evdev_support_nfingers(evdev_a, ELANTECH_MAX_FINGERS);
1925321060Swulf	evdev_support_key(evdev_a, BTN_LEFT);
1926321060Swulf	if (!sc->elanhw.isclickpad)
1927321060Swulf		evdev_support_key(evdev_a, BTN_RIGHT);
1928321060Swulf	psm_support_abs_bulk(evdev_a, elantech_absinfo);
1929321060Swulf
1930321060Swulf	error = evdev_register_mtx(evdev_a, &Giant);
1931321060Swulf	if (!error && sc->elanhw.hastrackpoint)
1932321060Swulf		error = psm_register(dev, MOUSE_MODEL_ELANTECH);
1933321060Swulf	if (error)
1934321060Swulf		evdev_free(evdev_a);
1935321060Swulf	else
1936321060Swulf		sc->evdev_a = evdev_a;
1937321060Swulf	return (error);
1938321060Swulf}
1939321060Swulf#endif
1940321060Swulf
1941321060Swulfstatic int
194241016Sdfrpsmattach(device_t dev)
194341016Sdfr{
1944357666Skevans	struct make_dev_args mda;
1945178019Sjkim	int unit = device_get_unit(dev);
1946178019Sjkim	struct psm_softc *sc = device_get_softc(dev);
1947178019Sjkim	int error;
1948178019Sjkim	int rid;
194941016Sdfr
1950178019Sjkim	/* Setup initial state */
1951178019Sjkim	sc->state = PSM_VALID;
1952240211Sjhb	callout_init(&sc->callout, 0);
1953240211Sjhb	callout_init(&sc->softcallout, 0);
195441016Sdfr
1955178019Sjkim	/* Setup our interrupt handler */
1956178019Sjkim	rid = KBDC_RID_AUX;
1957216491Sjhb	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1958178019Sjkim	if (sc->intr == NULL)
1959178019Sjkim		return (ENXIO);
1960178019Sjkim	error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1961178019Sjkim	    &sc->ih);
1962357666Skevans	if (error)
1963357666Skevans		goto out;
196458230Syokota
1965178019Sjkim	/* Done */
1966357666Skevans	make_dev_args_init(&mda);
1967357666Skevans	mda.mda_devsw = &psm_cdevsw;
1968357666Skevans	mda.mda_mode = 0666;
1969357666Skevans	mda.mda_si_drv1 = sc;
197041016Sdfr
1971357666Skevans	if ((error = make_dev_s(&mda, &sc->dev, "psm%d", unit)) != 0)
1972357666Skevans		goto out;
1973357666Skevans	if ((error = make_dev_s(&mda, &sc->bdev, "bpsm%d", unit)) != 0)
1974357666Skevans		goto out;
1975357666Skevans
1976321060Swulf#ifdef EVDEV_SUPPORT
1977321060Swulf	switch (sc->hw.model) {
1978321060Swulf	case MOUSE_MODEL_SYNAPTICS:
1979321060Swulf		error = psm_register_synaptics(dev);
1980321060Swulf		break;
1981321060Swulf
1982321060Swulf	case MOUSE_MODEL_ELANTECH:
1983321060Swulf		error = psm_register_elantech(dev);
1984321060Swulf		break;
1985321060Swulf
1986321060Swulf	default:
1987321060Swulf		error = psm_register(dev, sc->hw.model);
1988321060Swulf	}
1989321060Swulf
1990321060Swulf	if (error)
1991357666Skevans		goto out;
1992321060Swulf#endif
1993321060Swulf
1994233580Sjkim	/* Some touchpad devices need full reinitialization after suspend. */
1995233580Sjkim	switch (sc->hw.model) {
1996233580Sjkim	case MOUSE_MODEL_SYNAPTICS:
1997233580Sjkim	case MOUSE_MODEL_GLIDEPOINT:
1998233580Sjkim	case MOUSE_MODEL_VERSAPAD:
1999307576Sgonzo	case MOUSE_MODEL_ELANTECH:
2000233580Sjkim		sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
2001233580Sjkim		break;
2002233580Sjkim	default:
2003248478Sjkim		if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
2004233580Sjkim			sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
2005233580Sjkim		break;
2006233580Sjkim	}
2007233580Sjkim
2008307576Sgonzo	/* Elantech trackpad`s sync bit differs from touchpad`s one */
2009307576Sgonzo	if (sc->hw.model == MOUSE_MODEL_ELANTECH &&
2010328481Sdumbbell	    (sc->elanhw.hascrc || sc->elanhw.hastrackpoint)) {
2011307576Sgonzo		sc->config |= PSM_CONFIG_NOCHECKSYNC;
2012328481Sdumbbell		sc->flags &= ~PSM_NEED_SYNCBITS;
2013328481Sdumbbell	}
2014307576Sgonzo
2015178019Sjkim	if (!verbose)
2016178019Sjkim		printf("psm%d: model %s, device ID %d\n",
2017178019Sjkim		    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
2018178019Sjkim	else {
2019178019Sjkim		printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
2020178019Sjkim		    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
2021178019Sjkim		    sc->hw.hwid >> 8, sc->hw.buttons);
2022178019Sjkim		printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
2023178019Sjkim		    unit, sc->config, sc->flags, sc->mode.packetsize);
2024328481Sdumbbell		printf("psm%d: syncmask:%02x, syncbits:%02x%s\n",
2025328481Sdumbbell		    unit, sc->mode.syncmask[0], sc->mode.syncmask[1],
2026328481Sdumbbell		    sc->config & PSM_CONFIG_NOCHECKSYNC ? " (sync not checked)" : "");
2027178019Sjkim	}
202841016Sdfr
2029178019Sjkim	if (bootverbose)
2030178019Sjkim		--verbose;
203141016Sdfr
2032357666Skevansout:
2033357666Skevans	if (error != 0) {
2034357666Skevans		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
2035357666Skevans		if (sc->dev != NULL)
2036357666Skevans			destroy_dev(sc->dev);
2037357666Skevans		if (sc->bdev != NULL)
2038357666Skevans			destroy_dev(sc->bdev);
2039357666Skevans	}
2040357666Skevans	return (error);
204141016Sdfr}
204241016Sdfr
204341016Sdfrstatic int
204458230Syokotapsmdetach(device_t dev)
204558230Syokota{
2046178019Sjkim	struct psm_softc *sc;
2047178019Sjkim	int rid;
204858230Syokota
2049178019Sjkim	sc = device_get_softc(dev);
2050178019Sjkim	if (sc->state & PSM_OPEN)
2051178019Sjkim		return (EBUSY);
205258230Syokota
2053321060Swulf#ifdef EVDEV_SUPPORT
2054321060Swulf	evdev_free(sc->evdev_r);
2055321060Swulf	evdev_free(sc->evdev_a);
2056321060Swulf#endif
2057321060Swulf
2058178019Sjkim	rid = KBDC_RID_AUX;
2059178019Sjkim	bus_teardown_intr(dev, sc->intr, sc->ih);
2060178019Sjkim	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
206158230Syokota
2062178019Sjkim	destroy_dev(sc->dev);
2063178019Sjkim	destroy_dev(sc->bdev);
206458230Syokota
2065240211Sjhb	callout_drain(&sc->callout);
2066240211Sjhb	callout_drain(&sc->softcallout);
2067240211Sjhb
2068178019Sjkim	return (0);
206958230Syokota}
207058230Syokota
2071321060Swulf#ifdef EVDEV_SUPPORT
207258230Syokotastatic int
2073321060Swulfpsm_ev_open_r(struct evdev_dev *evdev, void *ev_softc)
207441016Sdfr{
2075321060Swulf	struct psm_softc *sc = (struct psm_softc *)ev_softc;
2076321060Swulf	int err = 0;
2077321060Swulf
2078321060Swulf	/* Get device data */
2079321060Swulf	if ((sc->state & PSM_VALID) == 0) {
2080321060Swulf		/* the device is no longer valid/functioning */
2081321060Swulf		return (ENXIO);
2082321060Swulf	}
2083321060Swulf
2084321060Swulf	if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_A)))
2085321060Swulf		err = psmopen(sc);
2086321060Swulf
2087321060Swulf	if (err == 0)
2088321060Swulf		sc->state |= PSM_EV_OPEN_R;
2089321060Swulf
2090321060Swulf	return (err);
2091321060Swulf}
2092321060Swulf
2093321060Swulfstatic void
2094321060Swulfpsm_ev_close_r(struct evdev_dev *evdev, void *ev_softc)
2095321060Swulf{
2096321060Swulf	struct psm_softc *sc = (struct psm_softc *)ev_softc;
2097321060Swulf
2098321060Swulf	sc->state &= ~PSM_EV_OPEN_R;
2099321060Swulf
2100321060Swulf	if (sc->state & (PSM_OPEN | PSM_EV_OPEN_A))
2101321060Swulf		return;
2102321060Swulf
2103321060Swulf	if (sc->state & PSM_VALID)
2104321060Swulf		psmclose(sc);
2105321060Swulf}
2106321060Swulf
2107321060Swulfstatic int
2108321060Swulfpsm_ev_open_a(struct evdev_dev *evdev, void *ev_softc)
2109321060Swulf{
2110321060Swulf	struct psm_softc *sc = (struct psm_softc *)ev_softc;
2111321060Swulf	int err = 0;
2112321060Swulf
2113321060Swulf	/* Get device data */
2114321060Swulf	if ((sc->state & PSM_VALID) == 0) {
2115321060Swulf		/* the device is no longer valid/functioning */
2116321060Swulf		return (ENXIO);
2117321060Swulf	}
2118321060Swulf
2119321060Swulf	if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R)))
2120321060Swulf		err = psmopen(sc);
2121321060Swulf
2122321060Swulf	if (err == 0)
2123321060Swulf		sc->state |= PSM_EV_OPEN_A;
2124321060Swulf
2125321060Swulf	return (err);
2126321060Swulf}
2127321060Swulf
2128321060Swulfstatic void
2129321060Swulfpsm_ev_close_a(struct evdev_dev *evdev, void *ev_softc)
2130321060Swulf{
2131321060Swulf	struct psm_softc *sc = (struct psm_softc *)ev_softc;
2132321060Swulf
2133321060Swulf	sc->state &= ~PSM_EV_OPEN_A;
2134321060Swulf
2135321060Swulf	if (sc->state & (PSM_OPEN | PSM_EV_OPEN_R))
2136321060Swulf		return;
2137321060Swulf
2138321060Swulf	if (sc->state & PSM_VALID)
2139321060Swulf		psmclose(sc);
2140321060Swulf}
2141321060Swulf#endif
2142321060Swulf
2143321060Swulfstatic int
2144321060Swulfpsm_cdev_open(struct cdev *dev, int flag, int fmt, struct thread *td)
2145321060Swulf{
2146178019Sjkim	struct psm_softc *sc;
2147321060Swulf	int err = 0;
214841016Sdfr
2149178019Sjkim	/* Get device data */
2150212355Sed	sc = dev->si_drv1;
2151178019Sjkim	if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2152178019Sjkim		/* the device is no longer valid/functioning */
2153178019Sjkim		return (ENXIO);
2154178019Sjkim	}
215541016Sdfr
2156178019Sjkim	/* Disallow multiple opens */
2157178019Sjkim	if (sc->state & PSM_OPEN)
2158178019Sjkim		return (EBUSY);
215941016Sdfr
2160212355Sed	device_busy(devclass_get_device(psm_devclass, sc->unit));
216141016Sdfr
2162321060Swulf#ifdef EVDEV_SUPPORT
2163321060Swulf	/* Already opened by evdev */
2164321060Swulf	if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2165321060Swulf#endif
2166321060Swulf		err = psmopen(sc);
2167321060Swulf
2168321060Swulf	if (err == 0)
2169321060Swulf		sc->state |= PSM_OPEN;
2170321060Swulf	else
2171321060Swulf		device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2172321060Swulf
2173321060Swulf	return (err);
2174321060Swulf}
2175321060Swulf
2176321060Swulfstatic int
2177321060Swulfpsm_cdev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
2178321060Swulf{
2179321060Swulf	struct psm_softc *sc;
2180321060Swulf	int err = 0;
2181321060Swulf
2182321060Swulf	/* Get device data */
2183321060Swulf	sc = dev->si_drv1;
2184321060Swulf	if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2185321060Swulf		/* the device is no longer valid/functioning */
2186321060Swulf		return (ENXIO);
2187321060Swulf	}
2188321060Swulf
2189321060Swulf#ifdef EVDEV_SUPPORT
2190321060Swulf	/* Still opened by evdev */
2191321060Swulf	if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2192321060Swulf#endif
2193321060Swulf		err = psmclose(sc);
2194321060Swulf
2195321060Swulf	if (err == 0) {
2196321060Swulf		sc->state &= ~PSM_OPEN;
2197321060Swulf		/* clean up and sigio requests */
2198321060Swulf		if (sc->async != NULL) {
2199321060Swulf			funsetown(&sc->async);
2200321060Swulf			sc->async = NULL;
2201321060Swulf		}
2202321060Swulf		device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2203321060Swulf	}
2204321060Swulf
2205321060Swulf	return (err);
2206321060Swulf}
2207321060Swulf
2208321060Swulfstatic int
2209321060Swulfpsmopen(struct psm_softc *sc)
2210321060Swulf{
2211321060Swulf	int command_byte;
2212321060Swulf	int err;
2213321060Swulf	int s;
2214321060Swulf
2215178019Sjkim	/* Initialize state */
2216178019Sjkim	sc->mode.level = sc->dflt_mode.level;
2217178019Sjkim	sc->mode.protocol = sc->dflt_mode.protocol;
2218178019Sjkim	sc->watchdog = FALSE;
2219189870Srnoland	sc->async = NULL;
222041016Sdfr
2221178019Sjkim	/* flush the event queue */
2222178019Sjkim	sc->queue.count = 0;
2223178019Sjkim	sc->queue.head = 0;
2224178019Sjkim	sc->queue.tail = 0;
2225178019Sjkim	sc->status.flags = 0;
2226178019Sjkim	sc->status.button = 0;
2227178019Sjkim	sc->status.obutton = 0;
2228178019Sjkim	sc->status.dx = 0;
2229178019Sjkim	sc->status.dy = 0;
2230178019Sjkim	sc->status.dz = 0;
2231178019Sjkim	sc->button = 0;
2232178019Sjkim	sc->pqueue_start = 0;
2233178019Sjkim	sc->pqueue_end = 0;
223441016Sdfr
2235178019Sjkim	/* empty input buffer */
2236178019Sjkim	flushpackets(sc);
2237178019Sjkim	sc->syncerrors = 0;
2238178019Sjkim	sc->pkterrors = 0;
223941016Sdfr
2240178019Sjkim	/* don't let timeout routines in the keyboard driver to poll the kbdc */
2241178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
2242178019Sjkim		return (EIO);
224341016Sdfr
2244178019Sjkim	/* save the current controller command byte */
2245178019Sjkim	s = spltty();
2246178019Sjkim	command_byte = get_controller_command_byte(sc->kbdc);
224741016Sdfr
2248178019Sjkim	/* enable the aux port and temporalily disable the keyboard */
2249178019Sjkim	if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
225041016Sdfr	    kbdc_get_device_mask(sc->kbdc),
2251178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2252178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2253178019Sjkim		/* CONTROLLER ERROR; do you know how to get out of this? */
2254178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
2255178019Sjkim		splx(s);
2256178019Sjkim		log(LOG_ERR,
2257212355Sed		    "psm%d: unable to set the command byte (psmopen).\n",
2258212355Sed		    sc->unit);
2259178019Sjkim		return (EIO);
2260178019Sjkim	}
2261178019Sjkim	/*
2262178019Sjkim	 * Now that the keyboard controller is told not to generate
2263178019Sjkim	 * the keyboard and mouse interrupts, call `splx()' to allow
2264178019Sjkim	 * the other tty interrupts. The clock interrupt may also occur,
2265178019Sjkim	 * but timeout routines will be blocked by the poll flag set
2266178019Sjkim	 * via `kbdc_lock()'
2267178019Sjkim	 */
226841016Sdfr	splx(s);
226941016Sdfr
2270178019Sjkim	/* enable the mouse device */
2271178019Sjkim	err = doopen(sc, command_byte);
2272178019Sjkim
2273178019Sjkim	/* done */
2274178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
2275178019Sjkim	return (err);
227641016Sdfr}
227741016Sdfr
227841016Sdfrstatic int
2279321060Swulfpsmclose(struct psm_softc *sc)
228041016Sdfr{
2281178019Sjkim	int stat[3];
2282178019Sjkim	int command_byte;
2283178019Sjkim	int s;
228441016Sdfr
2285178019Sjkim	/* don't let timeout routines in the keyboard driver to poll the kbdc */
2286178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
2287178019Sjkim		return (EIO);
228841016Sdfr
2289178019Sjkim	/* save the current controller command byte */
2290178019Sjkim	s = spltty();
2291178019Sjkim	command_byte = get_controller_command_byte(sc->kbdc);
2292178019Sjkim	if (command_byte == -1) {
2293178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
2294178019Sjkim		splx(s);
2295178019Sjkim		return (EIO);
2296178019Sjkim	}
229741016Sdfr
2298178019Sjkim	/* disable the aux interrupt and temporalily disable the keyboard */
2299178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
230041016Sdfr	    kbdc_get_device_mask(sc->kbdc),
2301178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2302178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2303178019Sjkim		log(LOG_ERR,
2304212355Sed		    "psm%d: failed to disable the aux int (psmclose).\n",
2305212355Sed		    sc->unit);
2306178019Sjkim		/* CONTROLLER ERROR;
2307178019Sjkim		 * NOTE: we shall force our way through. Because the only
2308178019Sjkim		 * ill effect we shall see is that we may not be able
2309178019Sjkim		 * to read ACK from the mouse, and it doesn't matter much
2310178019Sjkim		 * so long as the mouse will accept the DISABLE command.
2311178019Sjkim		 */
2312178019Sjkim	}
2313178019Sjkim	splx(s);
231441016Sdfr
2315178019Sjkim	/* stop the watchdog timer */
2316240211Sjhb	callout_stop(&sc->callout);
231758230Syokota
2318178019Sjkim	/* remove anything left in the output buffer */
2319178019Sjkim	empty_aux_buffer(sc->kbdc, 10);
232041016Sdfr
2321178019Sjkim	/* disable the aux device, port and interrupt */
2322178019Sjkim	if (sc->state & PSM_VALID) {
2323178019Sjkim		if (!disable_aux_dev(sc->kbdc)) {
2324178019Sjkim			/* MOUSE ERROR;
2325178019Sjkim			 * NOTE: we don't return (error) and continue,
2326178019Sjkim			 * pretending we have successfully disabled the device.
2327178019Sjkim			 * It's OK because the interrupt routine will discard
2328178019Sjkim			 * any data from the mouse hereafter.
2329178019Sjkim			 */
2330178019Sjkim			log(LOG_ERR,
2331178019Sjkim			    "psm%d: failed to disable the device (psmclose).\n",
2332212355Sed			    sc->unit);
2333178019Sjkim		}
233441016Sdfr
2335178019Sjkim		if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
2336178019Sjkim			log(LOG_DEBUG,
2337212355Sed			    "psm%d: failed to get status (psmclose).\n",
2338212355Sed			    sc->unit);
2339178019Sjkim	}
234041016Sdfr
2341178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
234241016Sdfr	    kbdc_get_device_mask(sc->kbdc),
2343178019Sjkim	    (command_byte & KBD_KBD_CONTROL_BITS) |
2344178019Sjkim	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2345178019Sjkim		/*
2346178019Sjkim		 * CONTROLLER ERROR;
2347178019Sjkim		 * we shall ignore this error; see the above comment.
2348178019Sjkim		 */
2349178019Sjkim		log(LOG_ERR,
2350178019Sjkim		    "psm%d: failed to disable the aux port (psmclose).\n",
2351212355Sed		    sc->unit);
2352178019Sjkim	}
235341016Sdfr
2354178019Sjkim	/* remove anything left in the output buffer */
2355178019Sjkim	empty_aux_buffer(sc->kbdc, 10);
235641016Sdfr
2357178019Sjkim	/* close is almost always successful */
2358178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
2359178019Sjkim	return (0);
236041016Sdfr}
236141016Sdfr
236241016Sdfrstatic int
2363178019Sjkimtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
2364178019Sjkim    u_char *buf)
236541016Sdfr{
2366178019Sjkim	static u_char butmapps2[8] = {
2367178019Sjkim		0,
2368178019Sjkim		MOUSE_PS2_BUTTON1DOWN,
2369178019Sjkim		MOUSE_PS2_BUTTON2DOWN,
2370178019Sjkim		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
2371178019Sjkim		MOUSE_PS2_BUTTON3DOWN,
2372178019Sjkim		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
2373178019Sjkim		MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
2374178019Sjkim		MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
2375178019Sjkim		    MOUSE_PS2_BUTTON3DOWN,
2376178019Sjkim	};
2377178019Sjkim	static u_char butmapmsc[8] = {
2378178019Sjkim		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
2379178019Sjkim		    MOUSE_MSC_BUTTON3UP,
2380178019Sjkim		MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
2381178019Sjkim		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
2382178019Sjkim		MOUSE_MSC_BUTTON3UP,
2383178019Sjkim		MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
2384178019Sjkim		MOUSE_MSC_BUTTON2UP,
2385178019Sjkim		MOUSE_MSC_BUTTON1UP,
2386178019Sjkim		0,
2387178019Sjkim	};
2388178019Sjkim	int mapped;
2389178019Sjkim	int i;
239041016Sdfr
2391178019Sjkim	if (sc->mode.level == PSM_LEVEL_BASE) {
2392178019Sjkim		mapped = status->button & ~MOUSE_BUTTON4DOWN;
2393178019Sjkim		if (status->button & MOUSE_BUTTON4DOWN)
2394178019Sjkim			mapped |= MOUSE_BUTTON1DOWN;
2395178019Sjkim		status->button = mapped;
2396178019Sjkim		buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
2397178019Sjkim		i = imax(imin(status->dx, 255), -256);
2398178019Sjkim		if (i < 0)
2399178019Sjkim			buf[0] |= MOUSE_PS2_XNEG;
2400178019Sjkim		buf[1] = i;
2401178019Sjkim		i = imax(imin(status->dy, 255), -256);
2402178019Sjkim		if (i < 0)
2403178019Sjkim			buf[0] |= MOUSE_PS2_YNEG;
2404178019Sjkim		buf[2] = i;
2405178019Sjkim		return (MOUSE_PS2_PACKETSIZE);
2406178019Sjkim	} else if (sc->mode.level == PSM_LEVEL_STANDARD) {
2407178019Sjkim		buf[0] = MOUSE_MSC_SYNC |
2408178019Sjkim		    butmapmsc[status->button & MOUSE_STDBUTTONS];
2409178019Sjkim		i = imax(imin(status->dx, 255), -256);
2410178019Sjkim		buf[1] = i >> 1;
2411178019Sjkim		buf[3] = i - buf[1];
2412178019Sjkim		i = imax(imin(status->dy, 255), -256);
2413178019Sjkim		buf[2] = i >> 1;
2414178019Sjkim		buf[4] = i - buf[2];
2415178019Sjkim		i = imax(imin(status->dz, 127), -128);
2416178019Sjkim		buf[5] = (i >> 1) & 0x7f;
2417178019Sjkim		buf[6] = (i - (i >> 1)) & 0x7f;
2418178019Sjkim		buf[7] = (~status->button >> 3) & 0x7f;
2419178019Sjkim		return (MOUSE_SYS_PACKETSIZE);
2420178019Sjkim	}
2421178019Sjkim	return (pb->inputbytes);
242241016Sdfr}
242341016Sdfr
242441016Sdfrstatic int
2425130585Sphkpsmread(struct cdev *dev, struct uio *uio, int flag)
242641016Sdfr{
2427212355Sed	struct psm_softc *sc = dev->si_drv1;
2428178019Sjkim	u_char buf[PSM_SMALLBUFSIZE];
2429178019Sjkim	int error = 0;
2430178019Sjkim	int s;
2431178019Sjkim	int l;
243241016Sdfr
2433178019Sjkim	if ((sc->state & PSM_VALID) == 0)
2434178019Sjkim		return (EIO);
243541016Sdfr
2436298955Spfg	/* block until mouse activity occurred */
2437178019Sjkim	s = spltty();
2438178019Sjkim	while (sc->queue.count <= 0) {
2439212355Sed		if (dev != sc->bdev) {
2440178019Sjkim			splx(s);
2441178019Sjkim			return (EWOULDBLOCK);
2442178019Sjkim		}
2443178019Sjkim		sc->state |= PSM_ASLP;
2444178019Sjkim		error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
2445178019Sjkim		sc->state &= ~PSM_ASLP;
2446178019Sjkim		if (error) {
2447178019Sjkim			splx(s);
2448178019Sjkim			return (error);
2449178019Sjkim		} else if ((sc->state & PSM_VALID) == 0) {
2450178019Sjkim			/* the device disappeared! */
2451178019Sjkim			splx(s);
2452178019Sjkim			return (EIO);
2453178019Sjkim		}
245441016Sdfr	}
2455178019Sjkim	splx(s);
245641016Sdfr
2457178019Sjkim	/* copy data to the user land */
2458178019Sjkim	while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
2459178019Sjkim		s = spltty();
2460178019Sjkim		l = imin(sc->queue.count, uio->uio_resid);
2461178019Sjkim		if (l > sizeof(buf))
2462178019Sjkim			l = sizeof(buf);
2463178019Sjkim		if (l > sizeof(sc->queue.buf) - sc->queue.head) {
2464178019Sjkim			bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
2465178019Sjkim			    sizeof(sc->queue.buf) - sc->queue.head);
2466178019Sjkim			bcopy(&sc->queue.buf[0],
2467178019Sjkim			    &buf[sizeof(sc->queue.buf) - sc->queue.head],
2468178019Sjkim			    l - (sizeof(sc->queue.buf) - sc->queue.head));
2469178019Sjkim		} else
2470178019Sjkim			bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
2471178019Sjkim		sc->queue.count -= l;
2472178019Sjkim		sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
2473178019Sjkim		splx(s);
2474178019Sjkim		error = uiomove(buf, l, uio);
2475178019Sjkim		if (error)
2476178019Sjkim			break;
247741016Sdfr	}
247841016Sdfr
2479178019Sjkim	return (error);
248041016Sdfr}
248141016Sdfr
248241016Sdfrstatic int
248341016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
248441016Sdfr{
2485178019Sjkim	int s;
248641016Sdfr
2487178019Sjkim	if (!kbdc_lock(sc->kbdc, TRUE))
2488178019Sjkim		return (EIO);
248941016Sdfr
2490178019Sjkim	s = spltty();
2491178019Sjkim	*c = get_controller_command_byte(sc->kbdc);
2492178019Sjkim	if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
249341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
2494178019Sjkim	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2495178019Sjkim	    KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2496178019Sjkim		/* this is CONTROLLER ERROR */
2497178019Sjkim		splx(s);
2498178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
2499178019Sjkim		return (EIO);
2500178019Sjkim	}
2501178019Sjkim
2502178019Sjkim	/*
2503178019Sjkim	 * The device may be in the middle of status data transmission.
2504178019Sjkim	 * The transmission will be interrupted, thus, incomplete status
2505178019Sjkim	 * data must be discarded. Although the aux interrupt is disabled
2506178019Sjkim	 * at the keyboard controller level, at most one aux interrupt
2507178019Sjkim	 * may have already been pending and a data byte is in the
2508178019Sjkim	 * output buffer; throw it away. Note that the second argument
2509178019Sjkim	 * to `empty_aux_buffer()' is zero, so that the call will just
2510178019Sjkim	 * flush the internal queue.
2511178019Sjkim	 * `psmintr()' will be invoked after `splx()' if an interrupt is
2512178019Sjkim	 * pending; it will see no data and returns immediately.
2513178019Sjkim	 */
2514178019Sjkim	empty_aux_buffer(sc->kbdc, 0);		/* flush the queue */
2515178019Sjkim	read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
2516178019Sjkim	flushpackets(sc);
251741016Sdfr	splx(s);
251841016Sdfr
2519178019Sjkim	return (0);
252041016Sdfr}
252141016Sdfr
2522123442Salfredstatic void
2523123442Salfreddropqueue(struct psm_softc *sc)
2524123442Salfred{
2525123442Salfred
2526178019Sjkim	sc->queue.count = 0;
2527178019Sjkim	sc->queue.head = 0;
2528178019Sjkim	sc->queue.tail = 0;
2529123442Salfred	if ((sc->state & PSM_SOFTARMED) != 0) {
2530123442Salfred		sc->state &= ~PSM_SOFTARMED;
2531240211Sjhb		callout_stop(&sc->softcallout);
2532123442Salfred	}
2533123442Salfred	sc->pqueue_start = sc->pqueue_end;
2534123442Salfred}
2535123442Salfred
2536123442Salfredstatic void
2537123442Salfredflushpackets(struct psm_softc *sc)
2538123442Salfred{
2539123442Salfred
2540123442Salfred	dropqueue(sc);
2541123442Salfred	bzero(&sc->pqueue, sizeof(sc->pqueue));
2542123442Salfred}
2543123442Salfred
254441016Sdfrstatic int
254541016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
254641016Sdfr{
2547178019Sjkim	int error = 0;
254841016Sdfr
2549178019Sjkim	/*
2550178019Sjkim	 * We may have seen a part of status data during `set_mouse_XXX()'.
2551178019Sjkim	 * they have been queued; flush it.
2552178019Sjkim	 */
2553178019Sjkim	empty_aux_buffer(sc->kbdc, 0);
255441016Sdfr
2555178019Sjkim	/* restore ports and interrupt */
2556178019Sjkim	if (!set_controller_command_byte(sc->kbdc,
2557178019Sjkim	    kbdc_get_device_mask(sc->kbdc),
255841016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
2559178019Sjkim		/*
2560178019Sjkim		 * CONTROLLER ERROR; this is serious, we may have
2561178019Sjkim		 * been left with the inaccessible keyboard and
2562178019Sjkim		 * the disabled mouse interrupt.
2563178019Sjkim		 */
2564178019Sjkim		error = EIO;
2565178019Sjkim	}
256641016Sdfr
2567178019Sjkim	kbdc_lock(sc->kbdc, FALSE);
2568178019Sjkim	return (error);
256941016Sdfr}
257041016Sdfr
257141016Sdfrstatic int
2572178017Sjkimpsmwrite(struct cdev *dev, struct uio *uio, int flag)
2573178017Sjkim{
2574212355Sed	struct psm_softc *sc = dev->si_drv1;
2575178019Sjkim	u_char buf[PSM_SMALLBUFSIZE];
2576178019Sjkim	int error = 0, i, l;
2577178017Sjkim
2578178019Sjkim	if ((sc->state & PSM_VALID) == 0)
2579178019Sjkim		return (EIO);
2580178017Sjkim
2581178019Sjkim	if (sc->mode.level < PSM_LEVEL_NATIVE)
2582178019Sjkim		return (ENODEV);
2583178017Sjkim
2584178019Sjkim	/* copy data from the user land */
2585178019Sjkim	while (uio->uio_resid > 0) {
2586178019Sjkim		l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
2587178019Sjkim		error = uiomove(buf, l, uio);
2588178019Sjkim		if (error)
2589178019Sjkim			break;
2590178019Sjkim		for (i = 0; i < l; i++) {
2591178019Sjkim			VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
2592178019Sjkim			if (!write_aux_command(sc->kbdc, buf[i])) {
2593178019Sjkim				VLOG(2, (LOG_DEBUG,
2594178019Sjkim				    "psm: cmd 0x%x failed.\n", buf[i]));
2595178019Sjkim				return (reinitialize(sc, FALSE));
2596178019Sjkim			}
2597178019Sjkim		}
2598178017Sjkim	}
2599178017Sjkim
2600178019Sjkim	return (error);
2601178017Sjkim}
2602178017Sjkim
2603178017Sjkimstatic int
2604178019Sjkimpsmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2605178019Sjkim    struct thread *td)
260641016Sdfr{
2607212355Sed	struct psm_softc *sc = dev->si_drv1;
2608178019Sjkim	mousemode_t mode;
2609178019Sjkim	mousestatus_t status;
261041016Sdfr#if (defined(MOUSE_GETVARS))
2611178019Sjkim	mousevar_t *var;
261241016Sdfr#endif
2613178019Sjkim	mousedata_t *data;
2614178019Sjkim	int stat[3];
2615178019Sjkim	int command_byte;
2616178019Sjkim	int error = 0;
2617178019Sjkim	int s;
261841016Sdfr
2619178019Sjkim	/* Perform IOCTL command */
2620178019Sjkim	switch (cmd) {
262141016Sdfr
2622178019Sjkim	case OLD_MOUSE_GETHWINFO:
2623178019Sjkim		s = spltty();
2624178019Sjkim		((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
2625178019Sjkim		((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
2626178019Sjkim		((old_mousehw_t *)addr)->type = sc->hw.type;
2627178019Sjkim		((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
2628178019Sjkim		splx(s);
2629178019Sjkim		break;
263041016Sdfr
2631178019Sjkim	case MOUSE_GETHWINFO:
2632178019Sjkim		s = spltty();
2633178019Sjkim		*(mousehw_t *)addr = sc->hw;
2634178019Sjkim		if (sc->mode.level == PSM_LEVEL_BASE)
2635178019Sjkim			((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
2636178019Sjkim		splx(s);
2637178019Sjkim		break;
263841016Sdfr
2639178019Sjkim	case MOUSE_SYN_GETHWINFO:
2640178019Sjkim		s = spltty();
2641233580Sjkim		if (sc->synhw.infoMajor >= 4)
2642178019Sjkim			*(synapticshw_t *)addr = sc->synhw;
2643178019Sjkim		else
2644178019Sjkim			error = EINVAL;
2645178019Sjkim		splx(s);
2646178019Sjkim		break;
2647132865Snjl
2648178019Sjkim	case OLD_MOUSE_GETMODE:
2649178019Sjkim		s = spltty();
2650178019Sjkim		switch (sc->mode.level) {
2651178019Sjkim		case PSM_LEVEL_BASE:
2652178019Sjkim			((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2653178019Sjkim			break;
2654178019Sjkim		case PSM_LEVEL_STANDARD:
2655178019Sjkim			((old_mousemode_t *)addr)->protocol =
2656178019Sjkim			    MOUSE_PROTO_SYSMOUSE;
2657178019Sjkim			break;
2658178019Sjkim		case PSM_LEVEL_NATIVE:
2659178019Sjkim			((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2660178019Sjkim			break;
2661178019Sjkim		}
2662178019Sjkim		((old_mousemode_t *)addr)->rate = sc->mode.rate;
2663178019Sjkim		((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2664178019Sjkim		((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2665178019Sjkim		splx(s);
2666178019Sjkim		break;
266741016Sdfr
2668178019Sjkim	case MOUSE_GETMODE:
2669178019Sjkim		s = spltty();
2670178019Sjkim		*(mousemode_t *)addr = sc->mode;
2671178019Sjkim		if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2672178019Sjkim			((mousemode_t *)addr)->syncmask[0] = 0;
2673178019Sjkim			((mousemode_t *)addr)->syncmask[1] = 0;
2674178019Sjkim		}
2675178019Sjkim		((mousemode_t *)addr)->resolution =
2676178019Sjkim			MOUSE_RES_LOW - sc->mode.resolution;
2677178019Sjkim		switch (sc->mode.level) {
2678178019Sjkim		case PSM_LEVEL_BASE:
2679178019Sjkim			((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2680178019Sjkim			((mousemode_t *)addr)->packetsize =
2681178019Sjkim			    MOUSE_PS2_PACKETSIZE;
2682178019Sjkim			break;
2683178019Sjkim		case PSM_LEVEL_STANDARD:
2684178019Sjkim			((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2685178019Sjkim			((mousemode_t *)addr)->packetsize =
2686178019Sjkim			    MOUSE_SYS_PACKETSIZE;
2687178019Sjkim			((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2688178019Sjkim			((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2689178019Sjkim			break;
2690178019Sjkim		case PSM_LEVEL_NATIVE:
2691178019Sjkim			/* FIXME: this isn't quite correct... XXX */
2692178019Sjkim			((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2693178019Sjkim			break;
2694178019Sjkim		}
2695178019Sjkim		splx(s);
2696178019Sjkim		break;
269741016Sdfr
2698178019Sjkim	case OLD_MOUSE_SETMODE:
2699178019Sjkim	case MOUSE_SETMODE:
2700178019Sjkim		if (cmd == OLD_MOUSE_SETMODE) {
2701178019Sjkim			mode.rate = ((old_mousemode_t *)addr)->rate;
2702178019Sjkim			/*
2703178019Sjkim			 * resolution  old I/F   new I/F
2704178019Sjkim			 * default        0         0
2705178019Sjkim			 * low            1        -2
2706178019Sjkim			 * medium low     2        -3
2707178019Sjkim			 * medium high    3        -4
2708178019Sjkim			 * high           4        -5
2709178019Sjkim			 */
2710178019Sjkim			if (((old_mousemode_t *)addr)->resolution > 0)
2711178019Sjkim				mode.resolution =
2712178019Sjkim				    -((old_mousemode_t *)addr)->resolution - 1;
2713178019Sjkim			else
2714178019Sjkim				mode.resolution = 0;
2715178019Sjkim			mode.accelfactor =
2716178019Sjkim			    ((old_mousemode_t *)addr)->accelfactor;
2717178019Sjkim			mode.level = -1;
2718178019Sjkim		} else
2719178019Sjkim			mode = *(mousemode_t *)addr;
272041016Sdfr
2721178019Sjkim		/* adjust and validate parameters. */
2722178019Sjkim		if (mode.rate > UCHAR_MAX)
2723178019Sjkim			return (EINVAL);
2724178019Sjkim		if (mode.rate == 0)
2725178019Sjkim			mode.rate = sc->dflt_mode.rate;
2726178019Sjkim		else if (mode.rate == -1)
2727178019Sjkim			/* don't change the current setting */
2728178019Sjkim			;
2729178019Sjkim		else if (mode.rate < 0)
2730178019Sjkim			return (EINVAL);
2731178019Sjkim		if (mode.resolution >= UCHAR_MAX)
2732178019Sjkim			return (EINVAL);
2733178019Sjkim		if (mode.resolution >= 200)
2734178019Sjkim			mode.resolution = MOUSE_RES_HIGH;
2735178019Sjkim		else if (mode.resolution >= 100)
2736178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMHIGH;
2737178019Sjkim		else if (mode.resolution >= 50)
2738178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMLOW;
2739178019Sjkim		else if (mode.resolution > 0)
2740178019Sjkim			mode.resolution = MOUSE_RES_LOW;
2741178019Sjkim		if (mode.resolution == MOUSE_RES_DEFAULT)
2742178019Sjkim			mode.resolution = sc->dflt_mode.resolution;
2743178019Sjkim		else if (mode.resolution == -1)
2744178019Sjkim			/* don't change the current setting */
2745178019Sjkim			;
2746178019Sjkim		else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2747178019Sjkim			mode.resolution = MOUSE_RES_LOW - mode.resolution;
2748178019Sjkim		if (mode.level == -1)
2749178019Sjkim			/* don't change the current setting */
2750178019Sjkim			mode.level = sc->mode.level;
2751178019Sjkim		else if ((mode.level < PSM_LEVEL_MIN) ||
2752178019Sjkim		    (mode.level > PSM_LEVEL_MAX))
2753178019Sjkim			return (EINVAL);
2754178019Sjkim		if (mode.accelfactor == -1)
2755178019Sjkim			/* don't change the current setting */
2756178019Sjkim			mode.accelfactor = sc->mode.accelfactor;
2757178019Sjkim		else if (mode.accelfactor < 0)
2758178019Sjkim			return (EINVAL);
275941016Sdfr
2760178019Sjkim		/* don't allow anybody to poll the keyboard controller */
2761178019Sjkim		error = block_mouse_data(sc, &command_byte);
2762178019Sjkim		if (error)
2763178019Sjkim			return (error);
276441016Sdfr
2765178019Sjkim		/* set mouse parameters */
2766178019Sjkim		if (mode.rate > 0)
2767178019Sjkim			mode.rate = set_mouse_sampling_rate(sc->kbdc,
2768178019Sjkim			    mode.rate);
2769178019Sjkim		if (mode.resolution >= 0)
2770178019Sjkim			mode.resolution =
2771178019Sjkim			    set_mouse_resolution(sc->kbdc, mode.resolution);
2772178019Sjkim		set_mouse_scaling(sc->kbdc, 1);
2773178019Sjkim		get_mouse_status(sc->kbdc, stat, 0, 3);
277441016Sdfr
2775178019Sjkim		s = spltty();
2776178019Sjkim		sc->mode.rate = mode.rate;
2777178019Sjkim		sc->mode.resolution = mode.resolution;
2778178019Sjkim		sc->mode.accelfactor = mode.accelfactor;
2779178019Sjkim		sc->mode.level = mode.level;
2780178019Sjkim		splx(s);
278141016Sdfr
2782178019Sjkim		unblock_mouse_data(sc, command_byte);
2783178019Sjkim		break;
278441016Sdfr
2785178019Sjkim	case MOUSE_GETLEVEL:
2786178019Sjkim		*(int *)addr = sc->mode.level;
2787178019Sjkim		break;
278841016Sdfr
2789178019Sjkim	case MOUSE_SETLEVEL:
2790178019Sjkim		if ((*(int *)addr < PSM_LEVEL_MIN) ||
2791178019Sjkim		    (*(int *)addr > PSM_LEVEL_MAX))
2792178019Sjkim			return (EINVAL);
2793178019Sjkim		sc->mode.level = *(int *)addr;
2794178019Sjkim		break;
279541016Sdfr
2796178019Sjkim	case MOUSE_GETSTATUS:
2797178019Sjkim		s = spltty();
2798178019Sjkim		status = sc->status;
2799178019Sjkim		sc->status.flags = 0;
2800178019Sjkim		sc->status.obutton = sc->status.button;
2801178019Sjkim		sc->status.button = 0;
2802178019Sjkim		sc->status.dx = 0;
2803178019Sjkim		sc->status.dy = 0;
2804178019Sjkim		sc->status.dz = 0;
2805178019Sjkim		splx(s);
2806178019Sjkim		*(mousestatus_t *)addr = status;
2807178019Sjkim		break;
280841016Sdfr
280941016Sdfr#if (defined(MOUSE_GETVARS))
2810178019Sjkim	case MOUSE_GETVARS:
2811178019Sjkim		var = (mousevar_t *)addr;
2812178019Sjkim		bzero(var, sizeof(*var));
2813178019Sjkim		s = spltty();
2814178019Sjkim		var->var[0] = MOUSE_VARS_PS2_SIG;
2815178019Sjkim		var->var[1] = sc->config;
2816178019Sjkim		var->var[2] = sc->flags;
2817178019Sjkim		splx(s);
2818178019Sjkim		break;
281941016Sdfr
2820178019Sjkim	case MOUSE_SETVARS:
2821178019Sjkim		return (ENODEV);
282241016Sdfr#endif /* MOUSE_GETVARS */
282341016Sdfr
2824178019Sjkim	case MOUSE_READSTATE:
2825178019Sjkim	case MOUSE_READDATA:
2826178019Sjkim		data = (mousedata_t *)addr;
2827178019Sjkim		if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2828178019Sjkim			return (EINVAL);
282941016Sdfr
2830178019Sjkim		error = block_mouse_data(sc, &command_byte);
2831178019Sjkim		if (error)
2832178019Sjkim			return (error);
2833178019Sjkim		if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2834178019Sjkim		    (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2835178019Sjkim			error = EIO;
2836178019Sjkim		unblock_mouse_data(sc, command_byte);
2837178019Sjkim		break;
283841016Sdfr
283941016Sdfr#if (defined(MOUSE_SETRESOLUTION))
2840178019Sjkim	case MOUSE_SETRESOLUTION:
2841178019Sjkim		mode.resolution = *(int *)addr;
2842178019Sjkim		if (mode.resolution >= UCHAR_MAX)
2843178019Sjkim			return (EINVAL);
2844178019Sjkim		else if (mode.resolution >= 200)
2845178019Sjkim			mode.resolution = MOUSE_RES_HIGH;
2846178019Sjkim		else if (mode.resolution >= 100)
2847178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMHIGH;
2848178019Sjkim		else if (mode.resolution >= 50)
2849178019Sjkim			mode.resolution = MOUSE_RES_MEDIUMLOW;
2850178019Sjkim		else if (mode.resolution > 0)
2851178019Sjkim			mode.resolution = MOUSE_RES_LOW;
2852178019Sjkim		if (mode.resolution == MOUSE_RES_DEFAULT)
2853178019Sjkim			mode.resolution = sc->dflt_mode.resolution;
2854178019Sjkim		else if (mode.resolution == -1)
2855178019Sjkim			mode.resolution = sc->mode.resolution;
2856178019Sjkim		else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2857178019Sjkim			mode.resolution = MOUSE_RES_LOW - mode.resolution;
285841016Sdfr
2859178019Sjkim		error = block_mouse_data(sc, &command_byte);
2860178019Sjkim		if (error)
2861178019Sjkim			return (error);
2862178019Sjkim		sc->mode.resolution =
2863178019Sjkim		    set_mouse_resolution(sc->kbdc, mode.resolution);
2864178019Sjkim		if (sc->mode.resolution != mode.resolution)
2865178019Sjkim			error = EIO;
2866178019Sjkim		unblock_mouse_data(sc, command_byte);
2867178019Sjkim		break;
286841016Sdfr#endif /* MOUSE_SETRESOLUTION */
286941016Sdfr
287041016Sdfr#if (defined(MOUSE_SETRATE))
2871178019Sjkim	case MOUSE_SETRATE:
2872178019Sjkim		mode.rate = *(int *)addr;
2873178019Sjkim		if (mode.rate > UCHAR_MAX)
2874178019Sjkim			return (EINVAL);
2875178019Sjkim		if (mode.rate == 0)
2876178019Sjkim			mode.rate = sc->dflt_mode.rate;
2877178019Sjkim		else if (mode.rate < 0)
2878178019Sjkim			mode.rate = sc->mode.rate;
287941016Sdfr
2880178019Sjkim		error = block_mouse_data(sc, &command_byte);
2881178019Sjkim		if (error)
2882178019Sjkim			return (error);
2883178019Sjkim		sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2884178019Sjkim		if (sc->mode.rate != mode.rate)
2885178019Sjkim			error = EIO;
2886178019Sjkim		unblock_mouse_data(sc, command_byte);
2887178019Sjkim		break;
288841016Sdfr#endif /* MOUSE_SETRATE */
288941016Sdfr
289041016Sdfr#if (defined(MOUSE_SETSCALING))
2891178019Sjkim	case MOUSE_SETSCALING:
2892178019Sjkim		if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2893178019Sjkim			return (EINVAL);
289441016Sdfr
2895178019Sjkim		error = block_mouse_data(sc, &command_byte);
2896178019Sjkim		if (error)
2897178019Sjkim			return (error);
2898178019Sjkim		if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2899178019Sjkim			error = EIO;
2900178019Sjkim		unblock_mouse_data(sc, command_byte);
2901178019Sjkim		break;
290241016Sdfr#endif /* MOUSE_SETSCALING */
290341016Sdfr
290441016Sdfr#if (defined(MOUSE_GETHWID))
2905178019Sjkim	case MOUSE_GETHWID:
2906178019Sjkim		error = block_mouse_data(sc, &command_byte);
2907178019Sjkim		if (error)
2908178019Sjkim			return (error);
2909178019Sjkim		sc->hw.hwid &= ~0x00ff;
2910178019Sjkim		sc->hw.hwid |= get_aux_id(sc->kbdc);
2911178019Sjkim		*(int *)addr = sc->hw.hwid & 0x00ff;
2912178019Sjkim		unblock_mouse_data(sc, command_byte);
2913178019Sjkim		break;
291441016Sdfr#endif /* MOUSE_GETHWID */
291541016Sdfr
2916189870Srnoland	case FIONBIO:
2917189870Srnoland	case FIOASYNC:
2918189870Srnoland		break;
2919189870Srnoland	case FIOSETOWN:
2920189870Srnoland		error = fsetown(*(int *)addr, &sc->async);
2921189870Srnoland		break;
2922189870Srnoland	case FIOGETOWN:
2923189870Srnoland		*(int *) addr = fgetown(&sc->async);
2924189870Srnoland		break;
2925178019Sjkim	default:
2926178019Sjkim		return (ENOTTY);
2927178019Sjkim	}
292841016Sdfr
2929178019Sjkim	return (error);
293041016Sdfr}
293141016Sdfr
293241016Sdfrstatic void
293358230Syokotapsmtimeout(void *arg)
293458230Syokota{
2935178019Sjkim	struct psm_softc *sc;
2936178019Sjkim	int s;
293758230Syokota
2938178019Sjkim	sc = (struct psm_softc *)arg;
2939178019Sjkim	s = spltty();
2940178019Sjkim	if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2941331171Seadler		VLOG(6, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2942178019Sjkim		psmintr(sc);
2943178019Sjkim		kbdc_lock(sc->kbdc, FALSE);
2944178019Sjkim	}
2945178019Sjkim	sc->watchdog = TRUE;
2946178019Sjkim	splx(s);
2947240211Sjhb	callout_reset(&sc->callout, hz, psmtimeout, sc);
294858230Syokota}
294958230Syokota
2950139628Sphilip/* Add all sysctls under the debug.psm and hw.psm nodes */
2951227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2952227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2953123442Salfred
2954307576SgonzoSYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RWTUN, &verbose, 0,
2955180818Strhodes    "Verbosity level");
2956123442Salfred
2957139628Sphilipstatic int psmhz = 20;
2958180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2959180818Strhodes    "Frequency of the softcallout (in hz)");
2960123442Salfredstatic int psmerrsecs = 2;
2961180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2962180818Strhodes    "Number of seconds during which packets will dropped after a sync error");
2963123442Salfredstatic int psmerrusecs = 0;
2964180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2965180818Strhodes    "Microseconds to add to psmerrsecs");
2966123442Salfredstatic int psmsecs = 0;
2967180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2968180818Strhodes    "Max number of seconds between soft interrupts");
2969123442Salfredstatic int psmusecs = 500000;
2970180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2971180818Strhodes    "Microseconds to add to psmsecs");
2972139628Sphilipstatic int pkterrthresh = 2;
2973180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2974180818Strhodes    "Number of error packets allowed before reinitializing the mouse");
2975123442Salfred
2976307576SgonzoSYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RWTUN, &tap_enabled, 0,
2977200674Sdumbbell    "Enable tap and drag gestures");
2978139628Sphilipstatic int tap_threshold = PSM_TAP_THRESHOLD;
2979180818StrhodesSYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2980180818Strhodes    "Button tap threshold");
2981139628Sphilipstatic int tap_timeout = PSM_TAP_TIMEOUT;
2982180818StrhodesSYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2983180818Strhodes    "Tap timeout for touchpads");
2984134405Sgibbs
2985307576Sgonzo/* Tunables */
2986307576SgonzoSYSCTL_INT(_hw_psm, OID_AUTO, synaptics_support, CTLFLAG_RDTUN,
2987307576Sgonzo    &synaptics_support, 0, "Enable support for Synaptics touchpads");
2988307576Sgonzo
2989307576SgonzoSYSCTL_INT(_hw_psm, OID_AUTO, trackpoint_support, CTLFLAG_RDTUN,
2990307576Sgonzo    &trackpoint_support, 0, "Enable support for IBM/Lenovo TrackPoint");
2991307576Sgonzo
2992307576SgonzoSYSCTL_INT(_hw_psm, OID_AUTO, elantech_support, CTLFLAG_RDTUN,
2993307576Sgonzo    &elantech_support, 0, "Enable support for Elantech touchpads");
2994307576Sgonzo
2995362209SwulfSYSCTL_INT(_hw_psm, OID_AUTO, mux_disabled, CTLFLAG_RDTUN,
2996362209Swulf    &mux_disabled, 0, "Disable active multiplexing");
2997362209Swulf
299858230Syokotastatic void
299941016Sdfrpsmintr(void *arg)
300041016Sdfr{
3001178019Sjkim	struct psm_softc *sc = arg;
3002178019Sjkim	struct timeval now;
3003178019Sjkim	int c;
3004178019Sjkim	packetbuf_t *pb;
300541016Sdfr
3006343158Swulf	if (aux_mux_is_enabled(sc->kbdc))
3007343158Swulf		VLOG(2, (LOG_DEBUG, "psmintr: active multiplexing mode is not "
3008343158Swulf		    "supported!\n"));
3009123442Salfred
3010178019Sjkim	/* read until there is nothing to read */
3011178019Sjkim	while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
3012178019Sjkim		pb = &sc->pqueue[sc->pqueue_end];
301384880Syokota
3014178019Sjkim		/* discard the byte if the device is not open */
3015321060Swulf		if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
3016178019Sjkim			continue;
301741016Sdfr
3018178019Sjkim		getmicrouptime(&now);
3019178019Sjkim		if ((pb->inputbytes > 0) &&
3020178019Sjkim		    timevalcmp(&now, &sc->inputtimeout, >)) {
3021178019Sjkim			VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
3022178019Sjkim			    "resetting byte count\n"));
3023178019Sjkim			pb->inputbytes = 0;
3024178019Sjkim			sc->syncerrors = 0;
3025178019Sjkim			sc->pkterrors = 0;
3026178019Sjkim		}
3027178019Sjkim		sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
3028178019Sjkim		sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
3029178019Sjkim		timevaladd(&sc->inputtimeout, &now);
303041016Sdfr
3031178019Sjkim		pb->ipacket[pb->inputbytes++] = c;
3032178017Sjkim
3033178019Sjkim		if (sc->mode.level == PSM_LEVEL_NATIVE) {
3034178019Sjkim			VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
3035178019Sjkim			sc->syncerrors = 0;
3036178019Sjkim			sc->pkterrors = 0;
3037178019Sjkim			goto next;
3038178019Sjkim		} else {
3039178019Sjkim			if (pb->inputbytes < sc->mode.packetsize)
3040178019Sjkim				continue;
304141016Sdfr
3042178019Sjkim			VLOG(4, (LOG_DEBUG,
3043178019Sjkim			    "psmintr: %02x %02x %02x %02x %02x %02x\n",
3044178019Sjkim			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
3045178019Sjkim			    pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
3046178019Sjkim		}
3047134405Sgibbs
3048178019Sjkim		c = pb->ipacket[0];
3049178019Sjkim
3050178019Sjkim		if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
3051178019Sjkim			sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
3052178019Sjkim			sc->flags &= ~PSM_NEED_SYNCBITS;
3053178019Sjkim			VLOG(2, (LOG_DEBUG,
3054178019Sjkim			    "psmintr: Sync bytes now %04x,%04x\n",
3055328484Sdumbbell			    sc->mode.syncmask[0], sc->mode.syncmask[1]));
3056328481Sdumbbell		} else if ((sc->config & PSM_CONFIG_NOCHECKSYNC) == 0 &&
3057328481Sdumbbell		    (c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
3058178019Sjkim			VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
3059178019Sjkim			    "(%04x != %04x) %d cmds since last error.\n",
3060178019Sjkim			    c & sc->mode.syncmask[0], sc->mode.syncmask[1],
3061178019Sjkim			    sc->cmdcount - sc->lasterr));
3062178019Sjkim			sc->lasterr = sc->cmdcount;
3063178019Sjkim			/*
3064178019Sjkim			 * The sync byte test is a weak measure of packet
3065178019Sjkim			 * validity.  Conservatively discard any input yet
3066178019Sjkim			 * to be seen by userland when we detect a sync
3067178019Sjkim			 * error since there is a good chance some of
3068178019Sjkim			 * the queued packets have undetected errors.
3069178019Sjkim			 */
3070178019Sjkim			dropqueue(sc);
3071178019Sjkim			if (sc->syncerrors == 0)
3072178019Sjkim				sc->pkterrors++;
3073178019Sjkim			++sc->syncerrors;
3074178019Sjkim			sc->lastinputerr = now;
3075178019Sjkim			if (sc->syncerrors >= sc->mode.packetsize * 2 ||
3076178019Sjkim			    sc->pkterrors >= pkterrthresh) {
3077178019Sjkim				/*
3078178019Sjkim				 * If we've failed to find a single sync byte
3079178019Sjkim				 * in 2 packets worth of data, or we've seen
3080178019Sjkim				 * persistent packet errors during the
3081178019Sjkim				 * validation period, reinitialize the mouse
3082178019Sjkim				 * in hopes of returning it to the expected
3083178019Sjkim				 * mode.
3084178019Sjkim				 */
3085178019Sjkim				VLOG(3, (LOG_DEBUG,
3086178019Sjkim				    "psmintr: reset the mouse.\n"));
3087178019Sjkim				reinitialize(sc, TRUE);
3088178019Sjkim			} else if (sc->syncerrors == sc->mode.packetsize) {
3089178019Sjkim				/*
3090178019Sjkim				 * Try a soft reset after searching for a sync
3091178019Sjkim				 * byte through a packet length of bytes.
3092178019Sjkim				 */
3093178019Sjkim				VLOG(3, (LOG_DEBUG,
3094178019Sjkim				    "psmintr: re-enable the mouse.\n"));
3095178019Sjkim				pb->inputbytes = 0;
3096178019Sjkim				disable_aux_dev(sc->kbdc);
3097178019Sjkim				enable_aux_dev(sc->kbdc);
3098178019Sjkim			} else {
3099178019Sjkim				VLOG(3, (LOG_DEBUG,
3100178019Sjkim				    "psmintr: discard a byte (%d)\n",
3101178019Sjkim				    sc->syncerrors));
3102178019Sjkim				pb->inputbytes--;
3103178019Sjkim				bcopy(&pb->ipacket[1], &pb->ipacket[0],
3104178019Sjkim				    pb->inputbytes);
3105178019Sjkim			}
3106178019Sjkim			continue;
3107178019Sjkim		}
3108178019Sjkim
3109134405Sgibbs		/*
3110178019Sjkim		 * We have what appears to be a valid packet.
3111178019Sjkim		 * Reset the error counters.
3112134405Sgibbs		 */
3113178019Sjkim		sc->syncerrors = 0;
3114134405Sgibbs
3115134405Sgibbs		/*
3116178019Sjkim		 * Drop even good packets if they occur within a timeout
3117178019Sjkim		 * period of a sync error.  This allows the detection of
3118178019Sjkim		 * a change in the mouse's packet mode without exposing
3119178019Sjkim		 * erratic mouse behavior to the user.  Some KVMs forget
3120178019Sjkim		 * enhanced mouse modes during switch events.
3121134405Sgibbs		 */
3122178019Sjkim		if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
3123178019Sjkim		    &now)) {
3124178019Sjkim			pb->inputbytes = 0;
3125178019Sjkim			continue;
3126178019Sjkim		}
3127134405Sgibbs
3128178019Sjkim		/*
3129178019Sjkim		 * Now that we're out of the validation period, reset
3130178019Sjkim		 * the packet error count.
3131178019Sjkim		 */
3132178019Sjkim		sc->pkterrors = 0;
3133134405Sgibbs
3134178019Sjkim		sc->cmdcount++;
3135178019Sjkimnext:
3136178019Sjkim		if (++sc->pqueue_end >= PSM_PACKETQUEUE)
3137178019Sjkim			sc->pqueue_end = 0;
3138178019Sjkim		/*
3139178019Sjkim		 * If we've filled the queue then call the softintr ourselves,
3140178019Sjkim		 * otherwise schedule the interrupt for later.
3141178019Sjkim		 */
3142178019Sjkim		if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
3143178019Sjkim		    (sc->pqueue_end == sc->pqueue_start)) {
3144178019Sjkim			if ((sc->state & PSM_SOFTARMED) != 0) {
3145178019Sjkim				sc->state &= ~PSM_SOFTARMED;
3146240211Sjhb				callout_stop(&sc->softcallout);
3147178019Sjkim			}
3148178019Sjkim			psmsoftintr(arg);
3149178019Sjkim		} else if ((sc->state & PSM_SOFTARMED) == 0) {
3150178019Sjkim			sc->state |= PSM_SOFTARMED;
3151240211Sjhb			callout_reset(&sc->softcallout,
3152240211Sjhb			    psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg);
3153123442Salfred		}
3154123442Salfred	}
3155123442Salfred}
3156123442Salfred
3157123442Salfredstatic void
3158178019Sjkimproc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3159178019Sjkim    int *x, int *y, int *z)
3160123442Salfred{
3161123442Salfred
3162178019Sjkim	/*
3163240743Skevlo	 * PS2++ protocol packet
3164178019Sjkim	 *
3165178019Sjkim	 *          b7 b6 b5 b4 b3 b2 b1 b0
3166178019Sjkim	 * byte 1:  *  1  p3 p2 1  *  *  *
3167178019Sjkim	 * byte 2:  c1 c2 p1 p0 d1 d0 1  0
3168178019Sjkim	 *
3169178019Sjkim	 * p3-p0: packet type
3170178019Sjkim	 * c1, c2: c1 & c2 == 1, if p2 == 0
3171178019Sjkim	 *         c1 & c2 == 0, if p2 == 1
3172178019Sjkim	 *
3173178019Sjkim	 * packet type: 0 (device type)
3174178019Sjkim	 * See comments in enable_mmanplus() below.
3175178019Sjkim	 *
3176178019Sjkim	 * packet type: 1 (wheel data)
3177178019Sjkim	 *
3178178019Sjkim	 *          b7 b6 b5 b4 b3 b2 b1 b0
3179178019Sjkim	 * byte 3:  h  *  B5 B4 s  d2 d1 d0
3180178019Sjkim	 *
3181178019Sjkim	 * h: 1, if horizontal roller data
3182178019Sjkim	 *    0, if vertical roller data
3183178019Sjkim	 * B4, B5: button 4 and 5
3184178019Sjkim	 * s: sign bit
3185178019Sjkim	 * d2-d0: roller data
3186178019Sjkim	 *
3187178019Sjkim	 * packet type: 2 (reserved)
318841016Sdfr	 */
3189178019Sjkim	if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
3190178019Sjkim	    (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
3191178019Sjkim		/*
3192178019Sjkim		 * the extended data packet encodes button
3193178019Sjkim		 * and wheel events
3194178019Sjkim		 */
3195123442Salfred		switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
319648778Syokota		case 1:
3197178019Sjkim			/* wheel data packet */
3198178019Sjkim			*x = *y = 0;
3199178019Sjkim			if (pb->ipacket[2] & 0x80) {
3200178019Sjkim				/* XXX horizontal roller count - ignore it */
3201178019Sjkim				;
3202178019Sjkim			} else {
3203178019Sjkim				/* vertical roller count */
3204178019Sjkim				*z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
3205178019Sjkim				    (pb->ipacket[2] & 0x0f) - 16 :
3206178019Sjkim				    (pb->ipacket[2] & 0x0f);
3207178019Sjkim			}
3208178019Sjkim			ms->button |= (pb->ipacket[2] &
3209178019Sjkim			    MOUSE_PS2PLUS_BUTTON4DOWN) ?
3210178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
3211178019Sjkim			ms->button |= (pb->ipacket[2] &
3212178019Sjkim			    MOUSE_PS2PLUS_BUTTON5DOWN) ?
3213178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
3214178019Sjkim			break;
321548778Syokota		case 2:
3216178019Sjkim			/*
3217178019Sjkim			 * this packet type is reserved by
3218178019Sjkim			 * Logitech...
3219178019Sjkim			 */
3220178019Sjkim			/*
3221178019Sjkim			 * IBM ScrollPoint Mouse uses this
3222178019Sjkim			 * packet type to encode both vertical
3223178019Sjkim			 * and horizontal scroll movement.
3224178019Sjkim			 */
3225178019Sjkim			*x = *y = 0;
3226178019Sjkim			/* horizontal count */
3227178019Sjkim			if (pb->ipacket[2] & 0x0f)
3228178019Sjkim				*z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
3229178019Sjkim				    -2 : 2;
3230178019Sjkim			/* vertical count */
3231178019Sjkim			if (pb->ipacket[2] & 0xf0)
3232178019Sjkim				*z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
3233178019Sjkim				    -1 : 1;
3234178019Sjkim			break;
323548778Syokota		case 0:
3236178019Sjkim			/* device type packet - shouldn't happen */
3237178019Sjkim			/* FALLTHROUGH */
323848778Syokota		default:
3239178019Sjkim			*x = *y = 0;
3240178019Sjkim			ms->button = ms->obutton;
3241178019Sjkim			VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
3242178019Sjkim			    "type %d: 0x%02x 0x%02x 0x%02x\n",
3243178019Sjkim			    MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
3244178019Sjkim			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
3245178019Sjkim			break;
324648778Syokota		}
3247178019Sjkim	} else {
324841016Sdfr		/* preserve button states */
3249178019Sjkim		ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
3250178019Sjkim	}
3251178019Sjkim}
325241016Sdfr
3253178019Sjkimstatic int
3254178019Sjkimproc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3255178019Sjkim    int *x, int *y, int *z)
3256178019Sjkim{
3257178019Sjkim	static int touchpad_buttons;
3258178019Sjkim	static int guest_buttons;
3259349541Swulf	static int ew_finger_count;
3260307576Sgonzo	static finger_t f[PSM_FINGERS];
3261349541Swulf	int w, id, nfingers, palm, ewcode, extended_buttons, clickpad_pressed;
326241016Sdfr
3263307576Sgonzo	extended_buttons = 0;
3264307576Sgonzo
3265255153Sdumbbell	/* TouchPad PS/2 absolute mode message format with capFourButtons:
3266178019Sjkim	 *
3267178019Sjkim	 *  Bits:        7   6   5   4   3   2   1   0 (LSB)
3268178019Sjkim	 *  ------------------------------------------------
3269178019Sjkim	 *  ipacket[0]:  1   0  W3  W2   0  W1   R   L
3270178019Sjkim	 *  ipacket[1]: Yb  Ya  Y9  Y8  Xb  Xa  X9  X8
3271178019Sjkim	 *  ipacket[2]: Z7  Z6  Z5  Z4  Z3  Z2  Z1  Z0
3272255153Sdumbbell	 *  ipacket[3]:  1   1  Yc  Xc   0  W0 D^R U^L
3273178019Sjkim	 *  ipacket[4]: X7  X6  X5  X4  X3  X2  X1  X0
3274178019Sjkim	 *  ipacket[5]: Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
3275178019Sjkim	 *
3276178019Sjkim	 * Legend:
3277178019Sjkim	 *  L: left physical mouse button
3278178019Sjkim	 *  R: right physical mouse button
3279178019Sjkim	 *  D: down button
3280178019Sjkim	 *  U: up button
3281178019Sjkim	 *  W: "wrist" value
3282178019Sjkim	 *  X: x position
3283183888Sdumbbell	 *  Y: y position
3284178019Sjkim	 *  Z: pressure
3285178019Sjkim	 *
3286255153Sdumbbell	 * Without capFourButtons but with nExtendeButtons and/or capMiddle
3287255153Sdumbbell	 *
3288255153Sdumbbell	 *  Bits:        7   6   5   4      3      2      1      0 (LSB)
3289255153Sdumbbell	 *  ------------------------------------------------------
3290255153Sdumbbell	 *  ipacket[3]:  1   1  Yc  Xc      0     W0    E^R    M^L
3291255153Sdumbbell	 *  ipacket[4]: X7  X6  X5  X4  X3|b7  X2|b5  X1|b3  X0|b1
3292255153Sdumbbell	 *  ipacket[5]: Y7  Y6  Y5  Y4  Y3|b8  Y2|b6  Y1|b4  Y0|b2
3293255153Sdumbbell	 *
3294255153Sdumbbell	 * Legend:
3295255153Sdumbbell	 *  M: Middle physical mouse button
3296255153Sdumbbell	 *  E: Extended mouse buttons reported instead of low bits of X and Y
3297255153Sdumbbell	 *  b1-b8: Extended mouse buttons
3298255153Sdumbbell	 *    Only ((nExtendedButtons + 1) >> 1) bits are used in packet
3299255153Sdumbbell	 *    4 and 5, for reading X and Y value they should be zeroed.
3300255153Sdumbbell	 *
3301178019Sjkim	 * Absolute reportable limits:    0 - 6143.
3302178019Sjkim	 * Typical bezel limits:       1472 - 5472.
3303178019Sjkim	 * Typical edge marings:       1632 - 5312.
3304178019Sjkim	 *
3305178019Sjkim	 * w = 3 Passthrough Packet
3306178019Sjkim	 *
3307178019Sjkim	 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
3308178019Sjkim	 */
330941016Sdfr
3310178019Sjkim	if (!synaptics_support)
3311178019Sjkim		return (0);
331241016Sdfr
3313178019Sjkim	/* Sanity check for out of sync packets. */
3314178019Sjkim	if ((pb->ipacket[0] & 0xc8) != 0x80 ||
3315178019Sjkim	    (pb->ipacket[3] & 0xc8) != 0xc0)
3316178019Sjkim		return (-1);
331749965Syokota
3318183888Sdumbbell	*x = *y = 0;
3319307576Sgonzo	ms->button = ms->obutton;
332058230Syokota
3321183888Sdumbbell	/*
3322183888Sdumbbell	 * Pressure value.
3323183888Sdumbbell	 * Interpretation:
3324183888Sdumbbell	 *   z = 0      No finger contact
3325183888Sdumbbell	 *   z = 10     Finger hovering near the pad
3326183888Sdumbbell	 *   z = 30     Very light finger contact
3327183888Sdumbbell	 *   z = 80     Normal finger contact
3328183888Sdumbbell	 *   z = 110    Very heavy finger contact
3329183888Sdumbbell	 *   z = 200    Finger lying flat on pad surface
3330183888Sdumbbell	 *   z = 255    Maximum reportable Z
3331183888Sdumbbell	 */
3332178019Sjkim	*z = pb->ipacket[2];
333358230Syokota
3334183888Sdumbbell	/*
3335183888Sdumbbell	 * Finger width value
3336183888Sdumbbell	 * Interpretation:
3337183888Sdumbbell	 *   w = 0      Two finger on the pad (capMultiFinger needed)
3338183888Sdumbbell	 *   w = 1      Three or more fingers (capMultiFinger needed)
3339183888Sdumbbell	 *   w = 2      Pen (instead of finger) (capPen needed)
3340183888Sdumbbell	 *   w = 3      Reserved (passthrough?)
3341183888Sdumbbell	 *   w = 4-7    Finger of normal width (capPalmDetect needed)
3342183888Sdumbbell	 *   w = 8-14   Very wide finger or palm (capPalmDetect needed)
3343183888Sdumbbell	 *   w = 15     Maximum reportable width (capPalmDetect needed)
3344183888Sdumbbell	 */
3345183888Sdumbbell	/* XXX Is checking capExtended enough? */
3346178019Sjkim	if (sc->synhw.capExtended)
3347133301Sphilip		w = ((pb->ipacket[0] & 0x30) >> 2) |
3348133301Sphilip		    ((pb->ipacket[0] & 0x04) >> 1) |
3349133301Sphilip		    ((pb->ipacket[3] & 0x04) >> 2);
3350178019Sjkim	else {
3351183888Sdumbbell		/* Assume a finger of regular width. */
3352133301Sphilip		w = 4;
3353178019Sjkim	}
3354133301Sphilip
3355307576Sgonzo	switch (w) {
3356307576Sgonzo	case 3:
3357307576Sgonzo		/*
3358307576Sgonzo		 * Handle packets from the guest device. See:
3359307576Sgonzo		 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
3360307576Sgonzo		 */
3361343158Swulf		if (sc->synhw.capPassthrough || sc->muxport != PSM_NOMUX) {
3362307576Sgonzo			*x = ((pb->ipacket[1] & 0x10) ?
3363307576Sgonzo			    pb->ipacket[4] - 256 : pb->ipacket[4]);
3364307576Sgonzo			*y = ((pb->ipacket[1] & 0x20) ?
3365307576Sgonzo			    pb->ipacket[5] - 256 : pb->ipacket[5]);
3366307576Sgonzo			*z = 0;
3367133868Sphilip
3368307576Sgonzo			guest_buttons = 0;
3369307576Sgonzo			if (pb->ipacket[1] & 0x01)
3370307576Sgonzo				guest_buttons |= MOUSE_BUTTON1DOWN;
3371307576Sgonzo			if (pb->ipacket[1] & 0x04)
3372307576Sgonzo				guest_buttons |= MOUSE_BUTTON2DOWN;
3373360782Sdim			if (pb->ipacket[1] & 0x02)
3374307576Sgonzo				guest_buttons |= MOUSE_BUTTON3DOWN;
3375321060Swulf#ifdef EVDEV_SUPPORT
3376321060Swulf			if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3377321060Swulf				evdev_push_rel(sc->evdev_r, REL_X, *x);
3378321060Swulf				evdev_push_rel(sc->evdev_r, REL_Y, -*y);
3379321060Swulf				evdev_push_mouse_btn(sc->evdev_r,
3380360617Swulf				    guest_buttons | sc->extended_buttons);
3381321060Swulf				evdev_sync(sc->evdev_r);
3382321060Swulf			}
3383321060Swulf#endif
3384307576Sgonzo			ms->button = touchpad_buttons | guest_buttons |
3385307576Sgonzo			    sc->extended_buttons;
3386307576Sgonzo		}
3387183888Sdumbbell		goto SYNAPTICS_END;
3388133868Sphilip
3389307576Sgonzo	case 2:
3390307576Sgonzo		/* Handle Extended W mode packets */
3391307576Sgonzo		ewcode = (pb->ipacket[5] & 0xf0) >> 4;
3392307576Sgonzo#if PSM_FINGERS > 1
3393307576Sgonzo		switch (ewcode) {
3394307576Sgonzo		case 1:
3395307576Sgonzo			/* Secondary finger */
3396307576Sgonzo			if (sc->synhw.capAdvancedGestures)
3397307576Sgonzo				f[1] = (finger_t) {
3398307576Sgonzo					.x = (((pb->ipacket[4] & 0x0f) << 8) |
3399307576Sgonzo					    pb->ipacket[1]) << 1,
3400307576Sgonzo					.y = (((pb->ipacket[4] & 0xf0) << 4) |
3401307576Sgonzo					    pb->ipacket[2]) << 1,
3402307576Sgonzo					.p = ((pb->ipacket[3] & 0x30) |
3403307576Sgonzo					    (pb->ipacket[5] & 0x0f)) << 1,
3404307576Sgonzo					.w = PSM_FINGER_DEFAULT_W,
3405307576Sgonzo					.flags = PSM_FINGER_FUZZY,
3406307576Sgonzo				};
3407307576Sgonzo			else if (sc->synhw.capReportsV)
3408307576Sgonzo				f[1] = (finger_t) {
3409307576Sgonzo					.x = (((pb->ipacket[4] & 0x0f) << 8) |
3410307576Sgonzo					    (pb->ipacket[1] & 0xfe)) << 1,
3411307576Sgonzo					.y = (((pb->ipacket[4] & 0xf0) << 4) |
3412307576Sgonzo					    (pb->ipacket[2] & 0xfe)) << 1,
3413307576Sgonzo					.p = ((pb->ipacket[3] & 0x30) |
3414307576Sgonzo					    (pb->ipacket[5] & 0x0e)) << 1,
3415307576Sgonzo					.w = (((pb->ipacket[5] & 0x01) << 2) |
3416307576Sgonzo					    ((pb->ipacket[2] & 0x01) << 1) |
3417307576Sgonzo					    (pb->ipacket[1] & 0x01)) + 8,
3418307576Sgonzo					.flags = PSM_FINGER_FUZZY,
3419307576Sgonzo				};
3420349541Swulf			break;
3421349541Swulf		case 2:
3422349541Swulf			ew_finger_count = pb->ipacket[1] & 0x0f;
3423307576Sgonzo		default:
3424307576Sgonzo			break;
3425307576Sgonzo		}
3426307576Sgonzo#endif
3427281441Srpaulo		goto SYNAPTICS_END;
3428307576Sgonzo
3429307576Sgonzo	case 1:
3430349541Swulf		if (sc->synhw.capReportsV && ew_finger_count > 3) {
3431349541Swulf			nfingers = ew_finger_count;
3432349541Swulf			break;
3433349541Swulf		}
3434349541Swulf		/* FALLTHROUGH */
3435307576Sgonzo	case 0:
3436307576Sgonzo		nfingers = w + 2;
3437307576Sgonzo		break;
3438307576Sgonzo
3439307576Sgonzo	default:
3440307576Sgonzo		nfingers = 1;
3441281441Srpaulo	}
3442281441Srpaulo
3443307576Sgonzo	if (sc->syninfo.touchpad_off)
3444307576Sgonzo		goto SYNAPTICS_END;
3445307576Sgonzo
3446178019Sjkim	/* Button presses */
3447178019Sjkim	touchpad_buttons = 0;
3448178019Sjkim	if (pb->ipacket[0] & 0x01)
3449178019Sjkim		touchpad_buttons |= MOUSE_BUTTON1DOWN;
3450178019Sjkim	if (pb->ipacket[0] & 0x02)
3451178019Sjkim		touchpad_buttons |= MOUSE_BUTTON3DOWN;
3452132865Snjl
3453178019Sjkim	if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3454255153Sdumbbell		if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
3455178019Sjkim			touchpad_buttons |= MOUSE_BUTTON4DOWN;
3456255153Sdumbbell		if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
3457178019Sjkim			touchpad_buttons |= MOUSE_BUTTON5DOWN;
3458281704Srpaulo	} else if (sc->synhw.capExtended && sc->synhw.capMiddle &&
3459281704Srpaulo	    !sc->synhw.capClickPad) {
3460255153Sdumbbell		/* Middle Button */
3461255153Sdumbbell		if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
3462255153Sdumbbell			touchpad_buttons |= MOUSE_BUTTON2DOWN;
3463255153Sdumbbell	} else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
3464255153Sdumbbell		/* Extended Buttons */
3465255153Sdumbbell		if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
3466255153Sdumbbell			if (sc->syninfo.directional_scrolls) {
3467255153Sdumbbell				if (pb->ipacket[4] & 0x01)
3468307576Sgonzo					extended_buttons |= MOUSE_BUTTON4DOWN;
3469255153Sdumbbell				if (pb->ipacket[5] & 0x01)
3470307576Sgonzo					extended_buttons |= MOUSE_BUTTON5DOWN;
3471255153Sdumbbell				if (pb->ipacket[4] & 0x02)
3472307576Sgonzo					extended_buttons |= MOUSE_BUTTON6DOWN;
3473255153Sdumbbell				if (pb->ipacket[5] & 0x02)
3474307576Sgonzo					extended_buttons |= MOUSE_BUTTON7DOWN;
3475255153Sdumbbell			} else {
3476281440Srpaulo				if (pb->ipacket[4] & 0x01)
3477307576Sgonzo					extended_buttons |= MOUSE_BUTTON1DOWN;
3478281440Srpaulo				if (pb->ipacket[5] & 0x01)
3479307576Sgonzo					extended_buttons |= MOUSE_BUTTON3DOWN;
3480281440Srpaulo				if (pb->ipacket[4] & 0x02)
3481307576Sgonzo					extended_buttons |= MOUSE_BUTTON2DOWN;
3482307576Sgonzo				sc->extended_buttons = extended_buttons;
3483255153Sdumbbell			}
3484132865Snjl
3485255153Sdumbbell			/*
3486255153Sdumbbell			 * Zero out bits used by extended buttons to avoid
3487255153Sdumbbell			 * misinterpretation of the data absolute position.
3488255153Sdumbbell			 *
3489255153Sdumbbell			 * The bits represented by
3490255153Sdumbbell			 *
3491255153Sdumbbell			 *     (nExtendedButtons + 1) >> 1
3492255153Sdumbbell			 *
3493255153Sdumbbell			 * will be masked out in both bytes.
3494255153Sdumbbell			 * The mask for n bits is computed with the formula
3495255153Sdumbbell			 *
3496255153Sdumbbell			 *     (1 << n) - 1
3497255153Sdumbbell			 */
3498255153Sdumbbell			int maskedbits = 0;
3499255153Sdumbbell			int mask = 0;
3500255153Sdumbbell			maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
3501255153Sdumbbell			mask = (1 << maskedbits) - 1;
3502321060Swulf#ifdef EVDEV_SUPPORT
3503321060Swulf			int i;
3504321060Swulf			if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3505321060Swulf				if (sc->synhw.capPassthrough) {
3506321060Swulf					evdev_push_mouse_btn(sc->evdev_r,
3507321060Swulf						extended_buttons);
3508321060Swulf					evdev_sync(sc->evdev_r);
3509321060Swulf				}
3510321060Swulf				for (i = 0; i < maskedbits; i++) {
3511321060Swulf					evdev_push_key(sc->evdev_a,
3512321060Swulf					    BTN_0 + i * 2,
3513321060Swulf					    pb->ipacket[4] & (1 << i));
3514321060Swulf					evdev_push_key(sc->evdev_a,
3515321060Swulf					    BTN_0 + i * 2 + 1,
3516321060Swulf					    pb->ipacket[5] & (1 << i));
3517321060Swulf				}
3518321060Swulf			}
3519321060Swulf#endif
3520255153Sdumbbell			pb->ipacket[4] &= ~(mask);
3521255153Sdumbbell			pb->ipacket[5] &= ~(mask);
3522281440Srpaulo		} else	if (!sc->syninfo.directional_scrolls &&
3523307576Sgonzo		    !sc->gesture.in_vscroll) {
3524281440Srpaulo			/*
3525281440Srpaulo			 * Keep reporting MOUSE DOWN until we get a new packet
3526281440Srpaulo			 * indicating otherwise.
3527281440Srpaulo			 */
3528307576Sgonzo			extended_buttons |= sc->extended_buttons;
3529139982Sphilip		}
3530178019Sjkim	}
3531139982Sphilip
3532307576Sgonzo	if (sc->synhw.capReportsV && nfingers > 1)
3533307576Sgonzo		f[0] = (finger_t) {
3534307576Sgonzo			.x = ((pb->ipacket[3] & 0x10) << 8) |
3535307576Sgonzo			    ((pb->ipacket[1] & 0x0f) << 8) |
3536307576Sgonzo			    (pb->ipacket[4] & 0xfd),
3537307576Sgonzo			.y = ((pb->ipacket[3] & 0x20) << 7) |
3538307576Sgonzo			    ((pb->ipacket[1] & 0xf0) << 4) |
3539307576Sgonzo			    (pb->ipacket[5] & 0xfd),
3540307576Sgonzo			.p = *z & 0xfe,
3541307576Sgonzo			.w = (((pb->ipacket[2] & 0x01) << 2) |
3542307576Sgonzo			    (pb->ipacket[5] & 0x02) |
3543307576Sgonzo			    ((pb->ipacket[4] & 0x02) >> 1)) + 8,
3544307576Sgonzo			.flags = PSM_FINGER_FUZZY,
3545307576Sgonzo		};
3546307576Sgonzo	else
3547307576Sgonzo		f[0] = (finger_t) {
3548307576Sgonzo			.x = ((pb->ipacket[3] & 0x10) << 8) |
3549307576Sgonzo			    ((pb->ipacket[1] & 0x0f) << 8) |
3550307576Sgonzo			    pb->ipacket[4],
3551307576Sgonzo			.y = ((pb->ipacket[3] & 0x20) << 7) |
3552307576Sgonzo			    ((pb->ipacket[1] & 0xf0) << 4) |
3553307576Sgonzo			    pb->ipacket[5],
3554307576Sgonzo			.p = *z,
3555307576Sgonzo			.w = w,
3556307576Sgonzo			.flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0,
3557307576Sgonzo		};
3558139982Sphilip
3559307576Sgonzo	/* Ignore hovering and unmeasurable touches */
3560307576Sgonzo	if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2)
3561307576Sgonzo		nfingers = 0;
3562307576Sgonzo
3563331170Seadler	/* Handle ClickPad */
3564331170Seadler	if (sc->synhw.capClickPad) {
3565331170Seadler		clickpad_pressed = (pb->ipacket[0] ^ pb->ipacket[3]) & 0x01;
3566331170Seadler		if (sc->synhw.forcePad) {
3567331170Seadler			/*
3568331170Seadler			 * Forcepads erroneously report button click if there
3569331170Seadler			 * are 2 or more fingers on the touchpad breaking
3570331170Seadler			 * multifinger gestures. To workaround this start
3571331170Seadler			 * reporting a click only after 4 consecutive single
3572331170Seadler			 * touch packets has been received.
3573331170Seadler			 * Skip these packets in case more contacts appear.
3574331170Seadler			 */
3575331170Seadler			switch (nfingers) {
3576331170Seadler			case 0:
3577331170Seadler				sc->fpcount = 0;
3578331170Seadler				break;
3579331170Seadler			case 1:
3580331170Seadler				if (clickpad_pressed && sc->fpcount < INT_MAX)
3581331170Seadler					++sc->fpcount;
3582331170Seadler				/* FALLTHROUGH */
3583331170Seadler			default:
3584331170Seadler				if (!clickpad_pressed)
3585331170Seadler					sc->fpcount = 0;
3586331170Seadler				if (sc->fpcount >= sc->syninfo.window_min)
3587331170Seadler					touchpad_buttons |= MOUSE_BUTTON1DOWN;
3588331170Seadler			}
3589331170Seadler		} else if (clickpad_pressed)
3590331170Seadler			touchpad_buttons |= MOUSE_BUTTON1DOWN;
3591331170Seadler	}
3592331170Seadler
3593307576Sgonzo	for (id = 0; id < PSM_FINGERS; id++)
3594307576Sgonzo		if (id >= nfingers)
3595307576Sgonzo			PSM_FINGER_RESET(f[id]);
3596307576Sgonzo
3597321060Swulf#ifdef EVDEV_SUPPORT
3598321060Swulf	if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3599321060Swulf		for (id = 0; id < PSM_FINGERS; id++) {
3600321060Swulf			if (PSM_FINGER_IS_SET(f[id]))
3601321060Swulf				psm_push_mt_finger(sc, id, &f[id]);
3602321060Swulf			else
3603321060Swulf				psm_release_mt_slot(sc->evdev_a, id);
3604321060Swulf		}
3605321060Swulf		evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
3606321060Swulf		evdev_push_nfingers(sc->evdev_a, nfingers);
3607321060Swulf		if (nfingers > 0)
3608321060Swulf			psm_push_st_finger(sc, &f[0]);
3609321060Swulf		else
3610321060Swulf			evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
3611321060Swulf		evdev_push_mouse_btn(sc->evdev_a, touchpad_buttons);
3612321060Swulf		if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3613321060Swulf			evdev_push_key(sc->evdev_a, BTN_FORWARD,
3614321060Swulf			    touchpad_buttons & MOUSE_BUTTON4DOWN);
3615321060Swulf			evdev_push_key(sc->evdev_a, BTN_BACK,
3616321060Swulf			    touchpad_buttons & MOUSE_BUTTON5DOWN);
3617321060Swulf		}
3618321060Swulf		evdev_sync(sc->evdev_a);
3619321060Swulf	}
3620321060Swulf#endif
3621321060Swulf
3622307576Sgonzo	ms->button = touchpad_buttons;
3623307576Sgonzo
3624349541Swulf	palm = psmpalmdetect(sc, &f[0], nfingers);
3625349541Swulf
3626349541Swulf	/* Palm detection doesn't terminate the current action. */
3627349541Swulf	if (!palm)
3628349541Swulf		psmgestures(sc, &f[0], nfingers, ms);
3629349541Swulf
3630318500Swulf	for (id = 0; id < PSM_FINGERS; id++)
3631318500Swulf		psmsmoother(sc, &f[id], id, ms, x, y);
3632318500Swulf
3633349541Swulf	if (palm) {
3634318500Swulf		*x = *y = *z = 0;
3635318500Swulf		ms->button = ms->obutton;
3636318500Swulf		return (0);
3637307576Sgonzo	}
3638307576Sgonzo
3639307576Sgonzo	ms->button |= extended_buttons | guest_buttons;
3640307576Sgonzo
3641307576SgonzoSYNAPTICS_END:
3642281440Srpaulo	/*
3643307576Sgonzo	 * Use the extra buttons as a scrollwheel
3644307576Sgonzo	 *
3645307576Sgonzo	 * XXX X.Org uses the Z axis for vertical wheel only,
3646307576Sgonzo	 * whereas moused(8) understands special values to differ
3647307576Sgonzo	 * vertical and horizontal wheels.
3648307576Sgonzo	 *
3649307576Sgonzo	 * xf86-input-mouse needs therefore a small patch to
3650307576Sgonzo	 * understand these special values. Without it, the
3651307576Sgonzo	 * horizontal wheel acts as a vertical wheel in X.Org.
3652307576Sgonzo	 *
3653307576Sgonzo	 * That's why the horizontal wheel is disabled by
3654307576Sgonzo	 * default for now.
3655307576Sgonzo	 */
3656307576Sgonzo	if (ms->button & MOUSE_BUTTON4DOWN)
3657307576Sgonzo		*z = -1;
3658307576Sgonzo	else if (ms->button & MOUSE_BUTTON5DOWN)
3659307576Sgonzo		*z = 1;
3660307576Sgonzo	else if (ms->button & MOUSE_BUTTON6DOWN)
3661307576Sgonzo		*z = -2;
3662307576Sgonzo	else if (ms->button & MOUSE_BUTTON7DOWN)
3663307576Sgonzo		*z = 2;
3664307576Sgonzo	else
3665307576Sgonzo		*z = 0;
3666307576Sgonzo	ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
3667307576Sgonzo	    MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
3668307576Sgonzo
3669307576Sgonzo	return (0);
3670307576Sgonzo}
3671307576Sgonzo
3672307576Sgonzostatic int
3673343158Swulfproc_synaptics_mux(struct psm_softc *sc, packetbuf_t *pb)
3674343158Swulf{
3675343158Swulf	int butt;
3676343158Swulf
3677343158Swulf	/*
3678343158Swulf	 * Convert 3-byte interleaved mixture of Synaptics and generic mouse
3679343158Swulf	 * packets into plain 6-byte Synaptics packet protocol.
3680343158Swulf	 * While in hidden multiplexing mode KBC does some editing of the
3681343158Swulf	 * packet stream. It remembers the button bits from the last packet
3682343158Swulf	 * received from each device, and replaces the button bits of every
3683343158Swulf	 * packet with the logical OR of all devices��� most recent button bits.
3684343158Swulf	 * This button crosstalk should be filtered out as Synaptics and
3685343158Swulf	 * generic mouse encode middle button presses in a different way.
3686343158Swulf	 */
3687343158Swulf	switch (pb->ipacket[0] & 0xc0) {
3688343158Swulf	case 0x80:	/* First 3 bytes of Synaptics packet */
3689343158Swulf		bcopy(pb->ipacket, sc->muxsave, 3);
3690343158Swulf		/* Compute middle mouse button supression timeout. */
3691343158Swulf		sc->muxmidtimeout.tv_sec  = 0;
3692343158Swulf		sc->muxmidtimeout.tv_usec = 50000;	/* ~2-3 ints */
3693343158Swulf		timevaladd(&sc->muxmidtimeout, &sc->lastsoftintr);
3694343158Swulf		return (1);
3695343158Swulf
3696343158Swulf	case 0xc0:	/* Second 3 bytes of Synaptics packet */
3697343158Swulf		/* Join two 3-bytes absolute packets */
3698343158Swulf		bcopy(pb->ipacket, pb->ipacket + 3, 3);
3699343158Swulf		bcopy(sc->muxsave, pb->ipacket, 3);
3700343158Swulf		/* Prefer trackpoint buttons over touchpad's */
3701343158Swulf		pb->ipacket[0] &= ~(0x08 | sc->muxmsbuttons);
3702343158Swulf		pb->ipacket[3] &= ~(0x08 | sc->muxmsbuttons);
3703343158Swulf		butt = (pb->ipacket[3] & 0x03) << 2 | (pb->ipacket[0] & 0x03);
3704343158Swulf		/* Add hysteresis to remove spurious middle button events */
3705343158Swulf		if (butt != sc->muxtpbuttons && sc->fpcount < 1) {
3706343158Swulf			pb->ipacket[0] &= 0xfc;
3707343158Swulf			pb->ipacket[0] |= sc->muxtpbuttons & 0x03;
3708343158Swulf			pb->ipacket[3] &= 0xfc;
3709343158Swulf			pb->ipacket[3] |= sc->muxtpbuttons >> 2 & 0x03;
3710343158Swulf			++sc->fpcount;
3711343158Swulf		} else {
3712343158Swulf			sc->fpcount = 0;
3713343158Swulf			sc->muxtpbuttons = butt;
3714343158Swulf		}
3715343158Swulf		/* Filter out impossible w induced by middle trackpoint btn */
3716343158Swulf		if (sc->synhw.capExtended && !sc->synhw.capPassthrough &&
3717343158Swulf		    (pb->ipacket[0] & 0x34) == 0x04 &&
3718343158Swulf		    (pb->ipacket[3] & 0x04) == 0x04) {
3719343158Swulf			pb->ipacket[0] &= 0xfb;
3720343158Swulf			pb->ipacket[3] &= 0xfb;
3721343158Swulf		}
3722343158Swulf		sc->muxsave[0] &= 0x30;
3723343158Swulf		break;
3724343158Swulf
3725343158Swulf	default:	/* Generic mouse (Trackpoint) packet */
3726343158Swulf		/* Filter out middle button events induced by some w values */
3727343158Swulf		if (sc->muxmsbuttons & 0x03 || pb->ipacket[0] & 0x03 ||
3728343158Swulf		    (timevalcmp(&sc->lastsoftintr, &sc->muxmidtimeout, <=) &&
3729343158Swulf		     (sc->muxsave[0] & 0x30 || sc->muxsave[2] > 8)))
3730343158Swulf			pb->ipacket[0] &= 0xfb;
3731343158Swulf		sc->muxmsbuttons = pb->ipacket[0] & 0x07;
3732343158Swulf		/* Convert to Synaptics pass-through protocol */
3733343158Swulf		pb->ipacket[4] = pb->ipacket[1];
3734343158Swulf		pb->ipacket[5] = pb->ipacket[2];
3735343158Swulf		pb->ipacket[1] = pb->ipacket[0];
3736343158Swulf		pb->ipacket[2] = 0;
3737343158Swulf		pb->ipacket[0] = 0x84 | (sc->muxtpbuttons & 0x03);
3738343158Swulf		pb->ipacket[3] = 0xc4 | (sc->muxtpbuttons >> 2 & 0x03);
3739343158Swulf	}
3740343158Swulf
3741343158Swulf	VLOG(4, (LOG_DEBUG, "synaptics: %02x %02x %02x %02x %02x %02x\n",
3742343158Swulf	    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
3743343158Swulf	    pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
3744343158Swulf
3745343158Swulf	pb->inputbytes = MOUSE_SYNAPTICS_PACKETSIZE;
3746343158Swulf	return (0);
3747343158Swulf}
3748343158Swulf
3749343158Swulfstatic int
3750307576Sgonzopsmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers)
3751307576Sgonzo{
3752307576Sgonzo	if (!(
3753318500Swulf	    ((sc->synhw.capMultiFinger || sc->synhw.capAdvancedGestures) &&
3754318500Swulf	      !sc->synhw.capReportsV && nfingers > 1) ||
3755318500Swulf	    (sc->synhw.capReportsV && nfingers > 2) ||
3756307576Sgonzo	    (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) ||
3757307576Sgonzo	    (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) ||
3758307576Sgonzo	    (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) {
3759307576Sgonzo		/*
3760307576Sgonzo		 * We consider the packet irrelevant for the current
3761307576Sgonzo		 * action when:
3762307576Sgonzo		 *  - the width isn't comprised in:
3763307576Sgonzo		 *    [1; max_width]
3764307576Sgonzo		 *  - the pressure isn't comprised in:
3765307576Sgonzo		 *    [min_pressure; max_pressure]
3766307576Sgonzo		 *  - pen aren't supported but PSM_FINGER_IS_PEN is set
3767307576Sgonzo		 */
3768318500Swulf		VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f->w));
3769307576Sgonzo		return (1);
3770307576Sgonzo	}
3771307576Sgonzo	return (0);
3772307576Sgonzo}
3773307576Sgonzo
3774307576Sgonzostatic void
3775307576Sgonzopsmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers,
3776307576Sgonzo    mousestatus_t *ms)
3777307576Sgonzo{
3778307576Sgonzo	smoother_t *smoother;
3779307576Sgonzo	gesture_t *gest;
3780307576Sgonzo	finger_t *f;
3781307576Sgonzo	int y_ok, center_button, center_x, right_button, right_x, i;
3782307576Sgonzo
3783307576Sgonzo	f = &fingers[0];
3784307576Sgonzo	smoother = &sc->smoother[0];
3785307576Sgonzo	gest = &sc->gesture;
3786307576Sgonzo
3787307576Sgonzo	/* Find first active finger. */
3788307576Sgonzo	if (nfingers > 0) {
3789307576Sgonzo		for (i = 0; i < PSM_FINGERS; i++) {
3790307576Sgonzo			if (PSM_FINGER_IS_SET(fingers[i])) {
3791307576Sgonzo				f = &fingers[i];
3792307576Sgonzo				smoother = &sc->smoother[i];
3793307576Sgonzo				break;
3794307576Sgonzo			}
3795307576Sgonzo		}
3796307576Sgonzo	}
3797307576Sgonzo
3798307576Sgonzo	/*
3799281440Srpaulo	 * Check pressure to detect a real wanted action on the
3800281440Srpaulo	 * touchpad.
3801281440Srpaulo	 */
3802307576Sgonzo	if (f->p >= sc->syninfo.min_pressure) {
3803307576Sgonzo		int x0, y0;
3804307576Sgonzo		int dxp, dyp;
3805307576Sgonzo		int start_x, start_y;
3806307576Sgonzo		int queue_len;
3807183888Sdumbbell		int margin_top, margin_right, margin_bottom, margin_left;
3808183888Sdumbbell		int window_min, window_max;
3809183888Sdumbbell		int vscroll_hor_area, vscroll_ver_area;
3810281440Srpaulo		int two_finger_scroll;
3811307576Sgonzo		int max_x, max_y;
3812183888Sdumbbell
3813183888Sdumbbell		/* Read sysctl. */
3814183888Sdumbbell		/* XXX Verify values? */
3815183888Sdumbbell		margin_top = sc->syninfo.margin_top;
3816183888Sdumbbell		margin_right = sc->syninfo.margin_right;
3817183888Sdumbbell		margin_bottom = sc->syninfo.margin_bottom;
3818183888Sdumbbell		margin_left = sc->syninfo.margin_left;
3819183888Sdumbbell		window_min = sc->syninfo.window_min;
3820183888Sdumbbell		window_max = sc->syninfo.window_max;
3821183888Sdumbbell		vscroll_hor_area = sc->syninfo.vscroll_hor_area;
3822183888Sdumbbell		vscroll_ver_area = sc->syninfo.vscroll_ver_area;
3823281440Srpaulo		two_finger_scroll = sc->syninfo.two_finger_scroll;
3824307576Sgonzo		max_x = sc->syninfo.max_x;
3825307576Sgonzo		max_y = sc->syninfo.max_y;
3826183888Sdumbbell
3827307576Sgonzo		/* Read current absolute position. */
3828307576Sgonzo		x0 = f->x;
3829307576Sgonzo		y0 = f->y;
3830292197Sandrew
3831307576Sgonzo		/*
3832307576Sgonzo		 * Limit the coordinates to the specified margins because
3833307576Sgonzo		 * this area isn't very reliable.
3834307576Sgonzo		 */
3835307576Sgonzo		if (x0 <= margin_left)
3836307576Sgonzo			x0 = margin_left;
3837307576Sgonzo		else if (x0 >= max_x - margin_right)
3838307576Sgonzo			x0 = max_x - margin_right;
3839307576Sgonzo		if (y0 <= margin_bottom)
3840307576Sgonzo			y0 = margin_bottom;
3841307576Sgonzo		else if (y0 >= max_y - margin_top)
3842307576Sgonzo			y0 = max_y - margin_top;
3843183888Sdumbbell
3844307576Sgonzo		VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
3845307576Sgonzo		    x0, y0, f->p, f->w));
3846132865Snjl
3847183888Sdumbbell		/*
3848183888Sdumbbell		 * If the action is just beginning, init the structure and
3849183888Sdumbbell		 * compute tap timeout.
3850183888Sdumbbell		 */
3851183888Sdumbbell		if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
3852183888Sdumbbell			VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
3853183888Sdumbbell
3854183888Sdumbbell			/* Initialize queue. */
3855307576Sgonzo			gest->window_min = window_min;
3856183888Sdumbbell
3857183888Sdumbbell			/* Reset pressure peak. */
3858307576Sgonzo			gest->zmax = 0;
3859183888Sdumbbell
3860183888Sdumbbell			/* Reset fingers count. */
3861307576Sgonzo			gest->fingers_nb = 0;
3862183888Sdumbbell
3863183888Sdumbbell			/* Reset virtual scrolling state. */
3864307576Sgonzo			gest->in_vscroll = 0;
3865183888Sdumbbell
3866183888Sdumbbell			/* Compute tap timeout. */
3867349541Swulf			if (tap_enabled != 0) {
3868349541Swulf				gest->taptimeout = (struct timeval) {
3869349541Swulf					.tv_sec  = tap_timeout / 1000000,
3870349541Swulf					.tv_usec = tap_timeout % 1000000,
3871349541Swulf				};
3872349541Swulf				timevaladd(
3873349541Swulf				    &gest->taptimeout, &sc->lastsoftintr);
3874349541Swulf			} else
3875349541Swulf				timevalclear(&gest->taptimeout);
3876183888Sdumbbell
3877183888Sdumbbell			sc->flags |= PSM_FLAGS_FINGERDOWN;
3878307576Sgonzo
3879307576Sgonzo			/* Smoother has not been reset yet */
3880307576Sgonzo			queue_len = 1;
3881307576Sgonzo			start_x = x0;
3882307576Sgonzo			start_y = y0;
3883183888Sdumbbell		} else {
3884307576Sgonzo			queue_len = smoother->queue_len + 1;
3885307576Sgonzo			start_x = smoother->start_x;
3886307576Sgonzo			start_y = smoother->start_y;
3887183888Sdumbbell		}
3888183888Sdumbbell
3889307576Sgonzo		/* Process ClickPad softbuttons */
3890307576Sgonzo		if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) {
3891307576Sgonzo			y_ok = sc->syninfo.softbuttons_y >= 0 ?
3892307576Sgonzo			    start_y < sc->syninfo.softbuttons_y :
3893314361Sgonzo			    start_y > max_y + sc->syninfo.softbuttons_y;
3894183888Sdumbbell
3895307576Sgonzo			center_button = MOUSE_BUTTON2DOWN;
3896307576Sgonzo			center_x = sc->syninfo.softbutton2_x;
3897307576Sgonzo			right_button = MOUSE_BUTTON3DOWN;
3898307576Sgonzo			right_x = sc->syninfo.softbutton3_x;
3899183888Sdumbbell
3900307576Sgonzo			if (center_x > 0 && right_x > 0 && center_x > right_x) {
3901307576Sgonzo				center_button = MOUSE_BUTTON3DOWN;
3902307576Sgonzo				center_x = sc->syninfo.softbutton3_x;
3903307576Sgonzo				right_button = MOUSE_BUTTON2DOWN;
3904307576Sgonzo				right_x = sc->syninfo.softbutton2_x;
3905307576Sgonzo			}
3906183888Sdumbbell
3907307576Sgonzo			if (right_x > 0 && start_x > right_x && y_ok)
3908307576Sgonzo				ms->button = (ms->button &
3909307576Sgonzo				    ~MOUSE_BUTTON1DOWN) | right_button;
3910307576Sgonzo			else if (center_x > 0 && start_x > center_x && y_ok)
3911307576Sgonzo				ms->button = (ms->button &
3912307576Sgonzo				    ~MOUSE_BUTTON1DOWN) | center_button;
3913307576Sgonzo		}
3914183888Sdumbbell
3915307576Sgonzo		/* If in tap-hold, add the recorded button. */
3916307576Sgonzo		if (gest->in_taphold)
3917307576Sgonzo			ms->button |= gest->tap_button;
3918183888Sdumbbell
3919183888Sdumbbell		/*
3920183888Sdumbbell		 * For tap, we keep the maximum number of fingers and the
3921183888Sdumbbell		 * pressure peak. Also with multiple fingers, we increase
3922183888Sdumbbell		 * the minimum window.
3923183888Sdumbbell		 */
3924307576Sgonzo		if (nfingers > 1)
3925307576Sgonzo			gest->window_min = window_max;
3926307576Sgonzo		gest->fingers_nb = imax(nfingers, gest->fingers_nb);
3927307576Sgonzo		gest->zmax = imax(f->p, gest->zmax);
3928183888Sdumbbell
3929307576Sgonzo		/* Do we have enough packets to consider this a gesture? */
3930307576Sgonzo		if (queue_len < gest->window_min)
3931307576Sgonzo			return;
3932183888Sdumbbell
3933298955Spfg		/* Is a scrolling action occurring? */
3934307576Sgonzo		if (!gest->in_taphold && !ms->button &&
3935307576Sgonzo		    (!gest->in_vscroll || two_finger_scroll)) {
3936178019Sjkim			/*
3937183888Sdumbbell			 * A scrolling action must not conflict with a tap
3938183888Sdumbbell			 * action. Here are the conditions to consider a
3939183888Sdumbbell			 * scrolling action:
3940183888Sdumbbell			 *  - the action in a configurable area
3941183888Sdumbbell			 *  - one of the following:
3942183888Sdumbbell			 *     . the distance between the last packet and the
3943183888Sdumbbell			 *       first should be above a configurable minimum
3944183888Sdumbbell			 *     . tap timed out
3945178019Sjkim			 */
3946307576Sgonzo			dxp = abs(x0 - start_x);
3947307576Sgonzo			dyp = abs(y0 - start_y);
3948139982Sphilip
3949307576Sgonzo			if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) ||
3950183888Sdumbbell			    dxp >= sc->syninfo.vscroll_min_delta ||
3951183888Sdumbbell			    dyp >= sc->syninfo.vscroll_min_delta) {
3952281440Srpaulo				/*
3953281440Srpaulo				 * Handle two finger scrolling.
3954281440Srpaulo				 * Note that we don't rely on fingers_nb
3955281440Srpaulo				 * as that keeps the maximum number of fingers.
3956281440Srpaulo				 */
3957281440Srpaulo				if (two_finger_scroll) {
3958307576Sgonzo					if (nfingers == 2) {
3959307576Sgonzo						gest->in_vscroll +=
3960281440Srpaulo						    dyp ? 2 : 0;
3961307576Sgonzo						gest->in_vscroll +=
3962281440Srpaulo						    dxp ? 1 : 0;
3963281440Srpaulo					}
3964281440Srpaulo				} else {
3965281440Srpaulo					/* Check for horizontal scrolling. */
3966281440Srpaulo					if ((vscroll_hor_area > 0 &&
3967307576Sgonzo					    start_y <= vscroll_hor_area) ||
3968281440Srpaulo					    (vscroll_hor_area < 0 &&
3969307576Sgonzo					     start_y >=
3970307576Sgonzo					     max_y + vscroll_hor_area))
3971307576Sgonzo						gest->in_vscroll += 2;
3972139982Sphilip
3973281440Srpaulo					/* Check for vertical scrolling. */
3974281440Srpaulo					if ((vscroll_ver_area > 0 &&
3975307576Sgonzo					    start_x <= vscroll_ver_area) ||
3976281440Srpaulo					    (vscroll_ver_area < 0 &&
3977307576Sgonzo					     start_x >=
3978307576Sgonzo					     max_x + vscroll_ver_area))
3979307576Sgonzo						gest->in_vscroll += 1;
3980281440Srpaulo				}
3981139982Sphilip
3982183888Sdumbbell				/* Avoid conflicts if area overlaps. */
3983307576Sgonzo				if (gest->in_vscroll >= 3)
3984307576Sgonzo					gest->in_vscroll =
3985183888Sdumbbell					    (dxp > dyp) ? 2 : 1;
3986183888Sdumbbell			}
3987183888Sdumbbell		}
3988281440Srpaulo		/*
3989281440Srpaulo		 * Reset two finger scrolling when the number of fingers
3990307576Sgonzo		 * is different from two or any button is pressed.
3991281440Srpaulo		 */
3992307576Sgonzo		if (two_finger_scroll && gest->in_vscroll != 0 &&
3993307576Sgonzo		    (nfingers != 2 || ms->button))
3994307576Sgonzo			gest->in_vscroll = 0;
3995139982Sphilip
3996281440Srpaulo		VLOG(5, (LOG_DEBUG,
3997281440Srpaulo			"synaptics: virtual scrolling: %s "
3998281440Srpaulo			"(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n",
3999307576Sgonzo			gest->in_vscroll ? "YES" : "NO",
4000307576Sgonzo			gest->in_vscroll, dxp, dyp,
4001307576Sgonzo			gest->fingers_nb));
4002281440Srpaulo
4003183888Sdumbbell	} else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
4004183888Sdumbbell		/*
4005183888Sdumbbell		 * An action is currently taking place but the pressure
4006183888Sdumbbell		 * dropped under the minimum, putting an end to it.
4007183888Sdumbbell		 */
4008183888Sdumbbell		int taphold_timeout, dx, dy, tap_max_delta;
4009183888Sdumbbell
4010307576Sgonzo		dx = abs(smoother->queue[smoother->queue_cursor].x -
4011307576Sgonzo		    smoother->start_x);
4012307576Sgonzo		dy = abs(smoother->queue[smoother->queue_cursor].y -
4013307576Sgonzo		    smoother->start_y);
4014183888Sdumbbell
4015183888Sdumbbell		/* Max delta is disabled for multi-fingers tap. */
4016307576Sgonzo		if (gest->fingers_nb > 1)
4017183888Sdumbbell			tap_max_delta = imax(dx, dy);
4018183888Sdumbbell		else
4019183888Sdumbbell			tap_max_delta = sc->syninfo.tap_max_delta;
4020183888Sdumbbell
4021183888Sdumbbell		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
4022183888Sdumbbell
4023183888Sdumbbell		/* Check for tap. */
4024183888Sdumbbell		VLOG(3, (LOG_DEBUG,
4025183888Sdumbbell		    "synaptics: zmax=%d, dx=%d, dy=%d, "
4026183888Sdumbbell		    "delta=%d, fingers=%d, queue=%d\n",
4027307576Sgonzo		    gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb,
4028307576Sgonzo		    smoother->queue_len));
4029307576Sgonzo		if (!gest->in_vscroll && gest->zmax >= tap_threshold &&
4030307576Sgonzo		    timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) &&
4031183888Sdumbbell		    dx <= tap_max_delta && dy <= tap_max_delta &&
4032307576Sgonzo		    smoother->queue_len >= sc->syninfo.tap_min_queue) {
4033178019Sjkim			/*
4034183888Sdumbbell			 * We have a tap if:
4035183888Sdumbbell			 *   - the maximum pressure went over tap_threshold
4036183888Sdumbbell			 *   - the action ended before tap_timeout
4037183888Sdumbbell			 *
4038183888Sdumbbell			 * To handle tap-hold, we must delay any button push to
4039183888Sdumbbell			 * the next action.
4040178019Sjkim			 */
4041307576Sgonzo			if (gest->in_taphold) {
4042183888Sdumbbell				/*
4043183888Sdumbbell				 * This is the second and last tap of a
4044183888Sdumbbell				 * double tap action, not a tap-hold.
4045183888Sdumbbell				 */
4046307576Sgonzo				gest->in_taphold = 0;
4047139982Sphilip
4048183888Sdumbbell				/*
4049183888Sdumbbell				 * For double-tap to work:
4050183888Sdumbbell				 *   - no button press is emitted (to
4051183888Sdumbbell				 *     simulate a button release)
4052183888Sdumbbell				 *   - PSM_FLAGS_FINGERDOWN is set to
4053183888Sdumbbell				 *     force the next packet to emit a
4054183888Sdumbbell				 *     button press)
4055183888Sdumbbell				 */
4056183888Sdumbbell				VLOG(2, (LOG_DEBUG,
4057183888Sdumbbell				    "synaptics: button RELEASE: %d\n",
4058307576Sgonzo				    gest->tap_button));
4059183888Sdumbbell				sc->flags |= PSM_FLAGS_FINGERDOWN;
4060307576Sgonzo
4061307576Sgonzo				/* Schedule button press on next interrupt */
4062307576Sgonzo				sc->idletimeout.tv_sec  = psmhz > 1 ?
4063307576Sgonzo				    0 : 1;
4064307576Sgonzo				sc->idletimeout.tv_usec = psmhz > 1 ?
4065307576Sgonzo				    1000000 / psmhz : 0;
4066178019Sjkim			} else {
4067178019Sjkim				/*
4068183888Sdumbbell				 * This is the first tap: we set the
4069183888Sdumbbell				 * tap-hold state and notify the button
4070183888Sdumbbell				 * down event.
4071178019Sjkim				 */
4072307576Sgonzo				gest->in_taphold = 1;
4073183888Sdumbbell				taphold_timeout = sc->syninfo.taphold_timeout;
4074307576Sgonzo				gest->taptimeout.tv_sec  = taphold_timeout /
4075183888Sdumbbell				    1000000;
4076307576Sgonzo				gest->taptimeout.tv_usec = taphold_timeout %
4077183888Sdumbbell				    1000000;
4078307576Sgonzo				sc->idletimeout = gest->taptimeout;
4079307576Sgonzo				timevaladd(&gest->taptimeout,
4080307576Sgonzo				    &sc->lastsoftintr);
4081139982Sphilip
4082307576Sgonzo				switch (gest->fingers_nb) {
4083183888Sdumbbell				case 3:
4084307576Sgonzo					gest->tap_button =
4085183888Sdumbbell					    MOUSE_BUTTON2DOWN;
4086183888Sdumbbell					break;
4087183888Sdumbbell				case 2:
4088307576Sgonzo					gest->tap_button =
4089183888Sdumbbell					    MOUSE_BUTTON3DOWN;
4090183888Sdumbbell					break;
4091183888Sdumbbell				default:
4092307576Sgonzo					gest->tap_button =
4093183888Sdumbbell					    MOUSE_BUTTON1DOWN;
4094183888Sdumbbell				}
4095183888Sdumbbell				VLOG(2, (LOG_DEBUG,
4096183888Sdumbbell				    "synaptics: button PRESS: %d\n",
4097307576Sgonzo				    gest->tap_button));
4098307576Sgonzo				ms->button |= gest->tap_button;
4099178019Sjkim			}
4100183888Sdumbbell		} else {
4101183888Sdumbbell			/*
4102183888Sdumbbell			 * Not enough pressure or timeout: reset
4103183888Sdumbbell			 * tap-hold state.
4104183888Sdumbbell			 */
4105307576Sgonzo			if (gest->in_taphold) {
4106183888Sdumbbell				VLOG(2, (LOG_DEBUG,
4107183888Sdumbbell				    "synaptics: button RELEASE: %d\n",
4108307576Sgonzo				    gest->tap_button));
4109307576Sgonzo				gest->in_taphold = 0;
4110183888Sdumbbell			} else {
4111183888Sdumbbell				VLOG(2, (LOG_DEBUG,
4112183888Sdumbbell				    "synaptics: not a tap-hold\n"));
4113183888Sdumbbell			}
4114183888Sdumbbell		}
4115307576Sgonzo	} else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) {
4116178019Sjkim		/*
4117183888Sdumbbell		 * For a tap-hold to work, the button must remain down at
4118183888Sdumbbell		 * least until timeout (where the in_taphold flags will be
4119183888Sdumbbell		 * cleared) or during the next action.
4120139982Sphilip		 */
4121307576Sgonzo		if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) {
4122307576Sgonzo			ms->button |= gest->tap_button;
4123183888Sdumbbell		} else {
4124307576Sgonzo			VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n",
4125307576Sgonzo			    gest->tap_button));
4126307576Sgonzo			gest->in_taphold = 0;
4127133297Sphilip		}
4128178019Sjkim	}
4129133296Sphilip
4130307576Sgonzo	return;
4131307576Sgonzo}
4132307576Sgonzo
4133307576Sgonzostatic void
4134307576Sgonzopsmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id,
4135307576Sgonzo    mousestatus_t *ms, int *x, int *y)
4136307576Sgonzo{
4137307576Sgonzo	smoother_t *smoother = &sc->smoother[smoother_id];
4138307576Sgonzo	gesture_t *gest = &(sc->gesture);
4139307576Sgonzo
4140183888Sdumbbell	/*
4141307576Sgonzo	 * Check pressure to detect a real wanted action on the
4142307576Sgonzo	 * touchpad.
4143183888Sdumbbell	 */
4144307576Sgonzo	if (f->p >= sc->syninfo.min_pressure) {
4145307576Sgonzo		int x0, y0;
4146307576Sgonzo		int cursor, peer, window;
4147307576Sgonzo		int dx, dy, dxp, dyp;
4148307576Sgonzo		int max_width, max_pressure;
4149307576Sgonzo		int margin_top, margin_right, margin_bottom, margin_left;
4150307576Sgonzo		int na_top, na_right, na_bottom, na_left;
4151307576Sgonzo		int window_min, window_max;
4152307576Sgonzo		int multiplicator;
4153307576Sgonzo		int weight_current, weight_previous, weight_len_squared;
4154307576Sgonzo		int div_min, div_max, div_len;
4155307576Sgonzo		int vscroll_hor_area, vscroll_ver_area;
4156307576Sgonzo		int two_finger_scroll;
4157307576Sgonzo		int max_x, max_y;
4158307576Sgonzo		int len, weight_prev_x, weight_prev_y;
4159307576Sgonzo		int div_max_x, div_max_y, div_x, div_y;
4160307576Sgonzo		int is_fuzzy;
4161349541Swulf		int natural_scroll;
4162281440Srpaulo
4163307576Sgonzo		/* Read sysctl. */
4164307576Sgonzo		/* XXX Verify values? */
4165307576Sgonzo		max_width = sc->syninfo.max_width;
4166307576Sgonzo		max_pressure = sc->syninfo.max_pressure;
4167307576Sgonzo		margin_top = sc->syninfo.margin_top;
4168307576Sgonzo		margin_right = sc->syninfo.margin_right;
4169307576Sgonzo		margin_bottom = sc->syninfo.margin_bottom;
4170307576Sgonzo		margin_left = sc->syninfo.margin_left;
4171307576Sgonzo		na_top = sc->syninfo.na_top;
4172307576Sgonzo		na_right = sc->syninfo.na_right;
4173307576Sgonzo		na_bottom = sc->syninfo.na_bottom;
4174307576Sgonzo		na_left = sc->syninfo.na_left;
4175307576Sgonzo		window_min = sc->syninfo.window_min;
4176307576Sgonzo		window_max = sc->syninfo.window_max;
4177307576Sgonzo		multiplicator = sc->syninfo.multiplicator;
4178307576Sgonzo		weight_current = sc->syninfo.weight_current;
4179307576Sgonzo		weight_previous = sc->syninfo.weight_previous;
4180307576Sgonzo		weight_len_squared = sc->syninfo.weight_len_squared;
4181307576Sgonzo		div_min = sc->syninfo.div_min;
4182307576Sgonzo		div_max = sc->syninfo.div_max;
4183307576Sgonzo		div_len = sc->syninfo.div_len;
4184307576Sgonzo		vscroll_hor_area = sc->syninfo.vscroll_hor_area;
4185307576Sgonzo		vscroll_ver_area = sc->syninfo.vscroll_ver_area;
4186307576Sgonzo		two_finger_scroll = sc->syninfo.two_finger_scroll;
4187307576Sgonzo		max_x = sc->syninfo.max_x;
4188307576Sgonzo		max_y = sc->syninfo.max_y;
4189349541Swulf		natural_scroll = sc->syninfo.natural_scroll;
4190307576Sgonzo
4191307576Sgonzo		is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0;
4192307576Sgonzo
4193307576Sgonzo		/* Read current absolute position. */
4194307576Sgonzo		x0 = f->x;
4195307576Sgonzo		y0 = f->y;
4196307576Sgonzo
4197307576Sgonzo		/*
4198307576Sgonzo		 * Limit the coordinates to the specified margins because
4199307576Sgonzo		 * this area isn't very reliable.
4200307576Sgonzo		 */
4201307576Sgonzo		if (x0 <= margin_left)
4202307576Sgonzo			x0 = margin_left;
4203307576Sgonzo		else if (x0 >= max_x - margin_right)
4204307576Sgonzo			x0 = max_x - margin_right;
4205307576Sgonzo		if (y0 <= margin_bottom)
4206307576Sgonzo			y0 = margin_bottom;
4207307576Sgonzo		else if (y0 >= max_y - margin_top)
4208307576Sgonzo			y0 = max_y - margin_top;
4209307576Sgonzo
4210307576Sgonzo		/* If the action is just beginning, init the structure. */
4211307576Sgonzo		if (smoother->active == 0) {
4212307576Sgonzo			VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id));
4213307576Sgonzo
4214307576Sgonzo			/* Store the first point of this action. */
4215307576Sgonzo			smoother->start_x = x0;
4216307576Sgonzo			smoother->start_y = y0;
4217307576Sgonzo			dx = dy = 0;
4218307576Sgonzo
4219307576Sgonzo			/* Initialize queue. */
4220307576Sgonzo			smoother->queue_cursor = SYNAPTICS_PACKETQUEUE;
4221307576Sgonzo			smoother->queue_len = 0;
4222307576Sgonzo
4223307576Sgonzo			/* Reset average. */
4224307576Sgonzo			smoother->avg_dx = 0;
4225307576Sgonzo			smoother->avg_dy = 0;
4226307576Sgonzo
4227307576Sgonzo			/* Reset squelch. */
4228307576Sgonzo			smoother->squelch_x = 0;
4229307576Sgonzo			smoother->squelch_y = 0;
4230307576Sgonzo
4231307576Sgonzo			/* Activate queue */
4232307576Sgonzo			smoother->active = 1;
4233307576Sgonzo		} else {
4234307576Sgonzo			/* Calculate the current delta. */
4235307576Sgonzo			cursor = smoother->queue_cursor;
4236307576Sgonzo			dx = x0 - smoother->queue[cursor].x;
4237307576Sgonzo			dy = y0 - smoother->queue[cursor].y;
4238307576Sgonzo		}
4239307576Sgonzo
4240307576Sgonzo		VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n",
4241307576Sgonzo		    smoother_id, x0, y0, f->p, f->w));
4242307576Sgonzo
4243307576Sgonzo		/* Queue this new packet. */
4244307576Sgonzo		cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1);
4245307576Sgonzo		smoother->queue[cursor].x = x0;
4246307576Sgonzo		smoother->queue[cursor].y = y0;
4247307576Sgonzo		smoother->queue_cursor = cursor;
4248307576Sgonzo		if (smoother->queue_len < SYNAPTICS_PACKETQUEUE)
4249307576Sgonzo			smoother->queue_len++;
4250307576Sgonzo		VLOG(5, (LOG_DEBUG,
4251307576Sgonzo		    "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
4252307576Sgonzo		    smoother_id, cursor, x0, y0, dx, dy));
4253307576Sgonzo
4254307576Sgonzo		/* Do we have enough packets to consider this a movement? */
4255307576Sgonzo		if (smoother->queue_len < gest->window_min)
4256307576Sgonzo			return;
4257307576Sgonzo
4258307576Sgonzo		weight_prev_x = weight_prev_y = weight_previous;
4259307576Sgonzo		div_max_x = div_max_y = div_max;
4260307576Sgonzo
4261307576Sgonzo		if (gest->in_vscroll) {
4262307576Sgonzo			/* Dividers are different with virtual scrolling. */
4263307576Sgonzo			div_min = sc->syninfo.vscroll_div_min;
4264307576Sgonzo			div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
4265307576Sgonzo		} else {
4266307576Sgonzo			/*
4267307576Sgonzo			 * There's a lot of noise in coordinates when
4268307576Sgonzo			 * the finger is on the touchpad's borders. When
4269307576Sgonzo			 * using this area, we apply a special weight and
4270307576Sgonzo			 * div.
4271307576Sgonzo			 */
4272307576Sgonzo			if (x0 <= na_left || x0 >= max_x - na_right) {
4273307576Sgonzo				weight_prev_x = sc->syninfo.weight_previous_na;
4274307576Sgonzo				div_max_x = sc->syninfo.div_max_na;
4275307576Sgonzo			}
4276307576Sgonzo
4277307576Sgonzo			if (y0 <= na_bottom || y0 >= max_y - na_top) {
4278307576Sgonzo				weight_prev_y = sc->syninfo.weight_previous_na;
4279307576Sgonzo				div_max_y = sc->syninfo.div_max_na;
4280307576Sgonzo			}
4281307576Sgonzo		}
4282307576Sgonzo
4283307576Sgonzo		/*
4284307576Sgonzo		 * Calculate weights for the average operands and
4285307576Sgonzo		 * the divisor. Both depend on the distance between
4286307576Sgonzo		 * the current packet and a previous one (based on the
4287307576Sgonzo		 * window width).
4288307576Sgonzo		 */
4289307576Sgonzo		window = imin(smoother->queue_len, window_max);
4290307576Sgonzo		peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
4291307576Sgonzo		dxp = abs(x0 - smoother->queue[peer].x) + 1;
4292307576Sgonzo		dyp = abs(y0 - smoother->queue[peer].y) + 1;
4293307576Sgonzo		len = (dxp * dxp) + (dyp * dyp);
4294307576Sgonzo		weight_prev_x = imin(weight_prev_x,
4295307576Sgonzo		    weight_len_squared * weight_prev_x / len);
4296307576Sgonzo		weight_prev_y = imin(weight_prev_y,
4297307576Sgonzo		    weight_len_squared * weight_prev_y / len);
4298307576Sgonzo
4299307576Sgonzo		len = (dxp + dyp) / 2;
4300307576Sgonzo		div_x = div_len * div_max_x / len;
4301307576Sgonzo		div_x = imin(div_max_x, div_x);
4302307576Sgonzo		div_x = imax(div_min, div_x);
4303307576Sgonzo		div_y = div_len * div_max_y / len;
4304307576Sgonzo		div_y = imin(div_max_y, div_y);
4305307576Sgonzo		div_y = imax(div_min, div_y);
4306307576Sgonzo
4307307576Sgonzo		VLOG(3, (LOG_DEBUG,
4308307576Sgonzo		    "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
4309307576Sgonzo		    smoother_id, peer, len, weight_prev_x, weight_prev_y,
4310307576Sgonzo		    div_x, div_y));
4311307576Sgonzo
4312307576Sgonzo		/* Compute averages. */
4313307576Sgonzo		smoother->avg_dx =
4314307576Sgonzo		    (weight_current * dx * multiplicator +
4315307576Sgonzo		     weight_prev_x * smoother->avg_dx) /
4316307576Sgonzo		    (weight_current + weight_prev_x);
4317307576Sgonzo
4318307576Sgonzo		smoother->avg_dy =
4319307576Sgonzo		    (weight_current * dy * multiplicator +
4320307576Sgonzo		     weight_prev_y * smoother->avg_dy) /
4321307576Sgonzo		    (weight_current + weight_prev_y);
4322307576Sgonzo
4323307576Sgonzo		VLOG(5, (LOG_DEBUG,
4324307576Sgonzo		    "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id,
4325307576Sgonzo		    smoother->avg_dx / multiplicator,
4326307576Sgonzo		    smoother->avg_dy / multiplicator));
4327307576Sgonzo
4328307576Sgonzo		/* Use these averages to calculate x & y. */
4329307576Sgonzo		smoother->squelch_x += smoother->avg_dx;
4330307576Sgonzo		dxp = smoother->squelch_x / (div_x * multiplicator);
4331307576Sgonzo		smoother->squelch_x = smoother->squelch_x %
4332307576Sgonzo		    (div_x * multiplicator);
4333307576Sgonzo
4334307576Sgonzo		smoother->squelch_y += smoother->avg_dy;
4335307576Sgonzo		dyp = smoother->squelch_y / (div_y * multiplicator);
4336307576Sgonzo		smoother->squelch_y = smoother->squelch_y %
4337307576Sgonzo		    (div_y * multiplicator);
4338307576Sgonzo
4339307576Sgonzo		switch(gest->in_vscroll) {
4340307576Sgonzo		case 0: /* Pointer movement. */
4341307576Sgonzo			/* On real<->fuzzy finger switch the x/y pos jumps */
4342307576Sgonzo			if (is_fuzzy == smoother->is_fuzzy) {
4343307576Sgonzo				*x += dxp;
4344307576Sgonzo				*y += dyp;
4345307576Sgonzo			}
4346307576Sgonzo
4347307576Sgonzo			VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n",
4348307576Sgonzo			    smoother_id, dx, dy, dxp, dyp));
4349307576Sgonzo			break;
4350307576Sgonzo		case 1: /* Vertical scrolling. */
4351349541Swulf			if (dyp != 0) {
4352349541Swulf				if (two_finger_scroll && natural_scroll)
4353349541Swulf					ms->button |= (dyp > 0) ?
4354349541Swulf					    MOUSE_BUTTON5DOWN : MOUSE_BUTTON4DOWN;
4355349541Swulf				else
4356349541Swulf					ms->button |= (dyp > 0) ?
4357349541Swulf					    MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN;
4358349541Swulf			}
4359307576Sgonzo			break;
4360307576Sgonzo		case 2: /* Horizontal scrolling. */
4361349541Swulf			if (dxp != 0) {
4362349541Swulf				if (two_finger_scroll && natural_scroll)
4363349541Swulf					ms->button |= (dxp > 0) ?
4364349541Swulf					    MOUSE_BUTTON6DOWN : MOUSE_BUTTON7DOWN;
4365349541Swulf				else
4366349541Swulf					ms->button |= (dxp > 0) ?
4367349541Swulf					    MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN;
4368349541Swulf			}
4369307576Sgonzo			break;
4370307576Sgonzo		}
4371307576Sgonzo
4372307576Sgonzo		smoother->is_fuzzy = is_fuzzy;
4373307576Sgonzo
4374307576Sgonzo	} else {
4375307576Sgonzo		/*
4376307576Sgonzo		 * Deactivate queue. Note: We can not just reset queue here
4377307576Sgonzo		 * as these values are still used by gesture processor.
4378307576Sgonzo		 * So postpone reset till next touch.
4379307576Sgonzo		 */
4380307576Sgonzo		smoother->active = 0;
4381307576Sgonzo	}
4382307576Sgonzo}
4383307576Sgonzo
4384307576Sgonzostatic int
4385307576Sgonzoproc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4386307576Sgonzo    int *x, int *y, int *z)
4387307576Sgonzo{
4388307576Sgonzo	static int touchpad_button, trackpoint_button;
4389307576Sgonzo	finger_t fn, f[ELANTECH_MAX_FINGERS];
4390349541Swulf	int pkt, id, scale, i, nfingers, mask, palm;
4391307576Sgonzo
4392307576Sgonzo	if (!elantech_support)
4393307576Sgonzo		return (0);
4394307576Sgonzo
4395307576Sgonzo	/* Determine packet format and do a sanity check for out of sync packets. */
4396307576Sgonzo	if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion))
4397307576Sgonzo		pkt = ELANTECH_PKT_NOP;
4398328482Sdumbbell	else if (sc->elanhw.hastrackpoint && ELANTECH_PKT_IS_TRACKPOINT(pb))
4399307576Sgonzo		pkt = ELANTECH_PKT_TRACKPOINT;
4400307576Sgonzo	else
4401307576Sgonzo	switch (sc->elanhw.hwversion) {
4402307576Sgonzo	case 2:
4403307576Sgonzo		if (!ELANTECH_PKT_IS_V2(pb))
4404307576Sgonzo			return (-1);
4405307576Sgonzo
4406307576Sgonzo		pkt = (pb->ipacket[0] & 0xc0) == 0x80 ?
4407307576Sgonzo		    ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON;
4408307576Sgonzo		break;
4409307576Sgonzo	case 3:
4410307576Sgonzo		if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) &&
4411307576Sgonzo		    !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc))
4412307576Sgonzo			return (-1);
4413307576Sgonzo
4414307576Sgonzo		pkt = ELANTECH_PKT_V3;
4415307576Sgonzo		break;
4416307576Sgonzo	case 4:
4417307576Sgonzo		if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc))
4418307576Sgonzo			return (-1);
4419307576Sgonzo
4420307576Sgonzo		switch (pb->ipacket[3] & 0x03) {
4421307576Sgonzo		case 0x00:
4422307576Sgonzo			pkt = ELANTECH_PKT_V4_STATUS;
4423307576Sgonzo			break;
4424307576Sgonzo		case 0x01:
4425307576Sgonzo			pkt = ELANTECH_PKT_V4_HEAD;
4426307576Sgonzo			break;
4427307576Sgonzo		case 0x02:
4428307576Sgonzo			pkt = ELANTECH_PKT_V4_MOTION;
4429307576Sgonzo			break;
4430307576Sgonzo		default:
4431307576Sgonzo			return (-1);
4432307576Sgonzo		}
4433307576Sgonzo		break;
4434307576Sgonzo	default:
4435307576Sgonzo		return (-1);
4436307576Sgonzo	}
4437307576Sgonzo
4438307576Sgonzo	VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt));
4439307576Sgonzo
4440307576Sgonzo	for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4441307576Sgonzo		PSM_FINGER_RESET(f[id]);
4442307576Sgonzo
4443307576Sgonzo	*x = *y = *z = 0;
4444307576Sgonzo	ms->button = ms->obutton;
4445307576Sgonzo
4446362209Swulf	if (sc->syninfo.touchpad_off && pkt != ELANTECH_PKT_TRACKPOINT)
4447307576Sgonzo		return (0);
4448307576Sgonzo
4449307576Sgonzo	/* Common legend
4450307576Sgonzo	 * L: Left mouse button pressed
4451307576Sgonzo	 * R: Right mouse button pressed
4452307576Sgonzo	 * N: number of fingers on touchpad
4453307576Sgonzo	 * X: absolute x value (horizontal)
4454307576Sgonzo	 * Y: absolute y value (vertical)
4455307576Sgonzo	 * W; width of the finger touch
4456307576Sgonzo	 * P: pressure
4457307576Sgonzo	 */
4458307576Sgonzo	switch (pkt) {
4459307576Sgonzo	case ELANTECH_PKT_V2_COMMON:	/* HW V2. One/Three finger touch */
4460307576Sgonzo		/*               7   6   5   4   3   2   1   0 (LSB)
4461307576Sgonzo		 * -------------------------------------------
4462307576Sgonzo		 * ipacket[0]:  N1  N0  W3  W2   .   .   R   L
4463307576Sgonzo		 * ipacket[1]:  P7  P6  P5  P4 X11 X10  X9  X8
4464307576Sgonzo		 * ipacket[2]:  X7  X6  X5  X4  X3  X2  X1  X0
4465307576Sgonzo		 * ipacket[3]:  N4  VF  W1  W0   .   .   .  B2
4466307576Sgonzo		 * ipacket[4]:  P3  P1  P2  P0 Y11 Y10  Y9  Y8
4467307576Sgonzo		 * ipacket[5]:  Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
4468307576Sgonzo		 * -------------------------------------------
4469307576Sgonzo		 * N4: set if more than 3 fingers (only in 3 fingers mode)
4470307576Sgonzo		 * VF: a kind of flag? (only on EF123, 0 when finger
4471307576Sgonzo		 *     is over one of the buttons, 1 otherwise)
4472307576Sgonzo		 * B2: (on EF113 only, 0 otherwise), one button pressed
4473307576Sgonzo		 * P & W is not reported on EF113 touchpads
4474307576Sgonzo		 */
4475307576Sgonzo		nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4476307576Sgonzo		if (nfingers == 3 && (pb->ipacket[3] & 0x80))
4477307576Sgonzo			nfingers = 4;
4478307576Sgonzo
4479318500Swulf		if (nfingers == 0) {
4480318500Swulf			mask = (1 << nfingers) - 1;	/* = 0x00 */
4481318500Swulf			break;
4482318500Swulf		}
4483318500Swulf
4484318500Swulf		/* Map 3-rd and 4-th fingers to first finger */
4485318500Swulf		mask = (1 << 1) - 1;	/* = 0x01 */
4486318500Swulf		f[0] = ELANTECH_FINGER_SET_XYP(pb);
4487307576Sgonzo		if (sc->elanhw.haspressure) {
4488318500Swulf			f[0].w = ((pb->ipacket[0] & 0x30) >> 2) |
4489307576Sgonzo			    ((pb->ipacket[3] & 0x30) >> 4);
4490307576Sgonzo		} else {
4491318500Swulf			f[0].p = PSM_FINGER_DEFAULT_P;
4492318500Swulf			f[0].w = PSM_FINGER_DEFAULT_W;
4493307576Sgonzo		}
4494307576Sgonzo
4495307576Sgonzo		/*
4496307576Sgonzo		 * HW v2 dont report exact finger positions when 3 or more
4497318500Swulf		 * fingers are on touchpad.
4498307576Sgonzo		 */
4499307576Sgonzo		if (nfingers > 2)
4500318500Swulf			f[0].flags = PSM_FINGER_FUZZY;
4501307576Sgonzo
4502307576Sgonzo		break;
4503307576Sgonzo
4504307576Sgonzo	case ELANTECH_PKT_V2_2FINGER:	/*HW V2. Two finger touch */
4505307576Sgonzo		/*               7   6   5   4   3   2   1   0 (LSB)
4506307576Sgonzo		 * -------------------------------------------
4507307576Sgonzo		 * ipacket[0]:  N1  N0 AY8 AX8   .   .   R   L
4508307576Sgonzo		 * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0
4509307576Sgonzo		 * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0
4510307576Sgonzo		 * ipacket[3]:   .   . BY8 BX8   .   .   .   .
4511307576Sgonzo		 * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0
4512307576Sgonzo		 * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0
4513307576Sgonzo		 * -------------------------------------------
4514307576Sgonzo		 * AX: lower-left finger absolute x value
4515307576Sgonzo		 * AY: lower-left finger absolute y value
4516307576Sgonzo		 * BX: upper-right finger absolute x value
4517307576Sgonzo		 * BY: upper-right finger absolute y value
4518307576Sgonzo		 */
4519307576Sgonzo		nfingers = 2;
4520307576Sgonzo		mask = (1 << nfingers) - 1;
4521307576Sgonzo
4522307576Sgonzo		for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++)
4523307576Sgonzo			f[id] = (finger_t) {
4524307576Sgonzo				.x = (((pb->ipacket[id * 3] & 0x10) << 4) |
4525307576Sgonzo				    pb->ipacket[id * 3 + 1]) << 2,
4526307576Sgonzo				.y = (((pb->ipacket[id * 3] & 0x20) << 3) |
4527307576Sgonzo				    pb->ipacket[id * 3 + 2]) << 2,
4528307576Sgonzo				.p = PSM_FINGER_DEFAULT_P,
4529307576Sgonzo				.w = PSM_FINGER_DEFAULT_W,
4530307576Sgonzo				/* HW ver.2 sends bounding box */
4531307576Sgonzo				.flags = PSM_FINGER_FUZZY
4532307576Sgonzo			};
4533307576Sgonzo		break;
4534307576Sgonzo
4535307576Sgonzo	case ELANTECH_PKT_V3:	/* HW Version 3 */
4536307576Sgonzo		/*               7   6   5   4   3   2   1   0 (LSB)
4537307576Sgonzo		 * -------------------------------------------
4538307576Sgonzo		 * ipacket[0]:  N1  N0  W3  W2   0   1   R   L
4539307576Sgonzo		 * ipacket[1]:  P7  P6  P5  P4 X11 X10  X9  X8
4540307576Sgonzo		 * ipacket[2]:  X7  X6  X5  X4  X3  X2  X1  X0
4541307576Sgonzo		 * ipacket[3]:   0   0  W1  W0   0   0   1   0
4542307576Sgonzo		 * ipacket[4]:  P3  P1  P2  P0 Y11 Y10  Y9  Y8
4543307576Sgonzo		 * ipacket[5]:  Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
4544307576Sgonzo		 * -------------------------------------------
4545307576Sgonzo		 */
4546307576Sgonzo		nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4547318500Swulf		/* Map 3-rd finger to first finger */
4548318500Swulf		id = nfingers > 2 ? 0 : nfingers - 1;
4549318500Swulf		mask = (1 << (id + 1)) - 1;
4550307576Sgonzo
4551318500Swulf		if (nfingers == 0)
4552318500Swulf			break;
4553318500Swulf
4554307576Sgonzo		fn = ELANTECH_FINGER_SET_XYP(pb);
4555307576Sgonzo		fn.w = ((pb->ipacket[0] & 0x30) >> 2) |
4556307576Sgonzo		    ((pb->ipacket[3] & 0x30) >> 4);
4557307576Sgonzo
4558307576Sgonzo		/*
4559307576Sgonzo		 * HW v3 dont report exact finger positions when 3 or more
4560318500Swulf		 * fingers are on touchpad.
4561307576Sgonzo		 */
4562307576Sgonzo		if (nfingers > 1)
4563307576Sgonzo			fn.flags = PSM_FINGER_FUZZY;
4564307576Sgonzo
4565307576Sgonzo		if (nfingers == 2) {
4566307576Sgonzo			if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) {
4567307576Sgonzo				sc->elanaction.fingers[0] = fn;
4568307576Sgonzo				return (0);
4569307576Sgonzo			} else
4570307576Sgonzo				f[0] = sc->elanaction.fingers[0];
4571307576Sgonzo		}
4572318500Swulf		f[id] = fn;
4573307576Sgonzo		break;
4574307576Sgonzo
4575307576Sgonzo	case ELANTECH_PKT_V4_STATUS:	/* HW Version 4. Status packet */
4576307576Sgonzo		/*               7   6   5   4   3   2   1   0 (LSB)
4577307576Sgonzo		 * -------------------------------------------
4578307576Sgonzo		 * ipacket[0]:   .   .   .   .   0   1   R   L
4579307576Sgonzo		 * ipacket[1]:   .   .   .  F4  F3  F2  F1  F0
4580307576Sgonzo		 * ipacket[2]:   .   .   .   .   .   .   .   .
4581307576Sgonzo		 * ipacket[3]:   .   .   .   1   0   0   0   0
4582307576Sgonzo		 * ipacket[4]:  PL   .   .   .   .   .   .   .
4583307576Sgonzo		 * ipacket[5]:   .   .   .   .   .   .   .   .
4584307576Sgonzo		 * -------------------------------------------
4585307576Sgonzo		 * Fn: finger n is on touchpad
4586307576Sgonzo		 * PL: palm
4587307576Sgonzo		 * HV ver4 sends a status packet to indicate that the numbers
4588307576Sgonzo		 * or identities of the fingers has been changed
4589307576Sgonzo		 */
4590307576Sgonzo
4591307576Sgonzo		mask = pb->ipacket[1] & 0x1f;
4592307576Sgonzo		nfingers = bitcount(mask);
4593307576Sgonzo
4594318500Swulf		if (sc->elanaction.mask_v4wait != 0)
4595318500Swulf			VLOG(3, (LOG_DEBUG, "elantech: HW v4 status packet"
4596318500Swulf			    " when not all previous head packets received\n"));
4597318500Swulf
4598318500Swulf		/* Bitmap of fingers to receive before gesture processing */
4599318500Swulf		sc->elanaction.mask_v4wait = mask & ~sc->elanaction.mask;
4600318500Swulf
4601307576Sgonzo		/* Skip "new finger is on touchpad" packets */
4602318500Swulf		if (sc->elanaction.mask_v4wait) {
4603307576Sgonzo			sc->elanaction.mask = mask;
4604307576Sgonzo			return (0);
4605307576Sgonzo		}
4606307576Sgonzo
4607307576Sgonzo		break;
4608307576Sgonzo
4609307576Sgonzo	case ELANTECH_PKT_V4_HEAD:	/* HW Version 4. Head packet */
4610307576Sgonzo		/*               7   6   5   4   3   2   1   0 (LSB)
4611307576Sgonzo		 * -------------------------------------------
4612307576Sgonzo		 * ipacket[0]:  W3  W2  W1  W0   0   1   R   L
4613307576Sgonzo		 * ipacket[1]:  P7  P6  P5  P4 X11 X10  X9  X8
4614307576Sgonzo		 * ipacket[2]:  X7  X6  X5  X4  X3  X2  X1  X0
4615307576Sgonzo		 * ipacket[3]: ID2 ID1 ID0   1   0   0   0   1
4616307576Sgonzo		 * ipacket[4]:  P3  P1  P2  P0 Y11 Y10  Y9  Y8
4617307576Sgonzo		 * ipacket[5]:  Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
4618307576Sgonzo		 * -------------------------------------------
4619307576Sgonzo		 * ID: finger id
4620307576Sgonzo		 * HW ver 4 sends head packets in two cases:
4621307576Sgonzo		 * 1. One finger touch and movement.
4622307576Sgonzo		 * 2. Next after status packet to tell new finger positions.
4623307576Sgonzo		 */
4624307576Sgonzo		mask = sc->elanaction.mask;
4625307576Sgonzo		nfingers = bitcount(mask);
4626307576Sgonzo		id = ((pb->ipacket[3] & 0xe0) >> 5) - 1;
4627318500Swulf		fn = ELANTECH_FINGER_SET_XYP(pb);
4628318500Swulf		fn.w =(pb->ipacket[0] & 0xf0) >> 4;
4629307576Sgonzo
4630318500Swulf		if (id < 0)
4631318500Swulf			return (0);
4632318500Swulf
4633318500Swulf		/* Packet is finger position update. Report it */
4634318500Swulf		if (sc->elanaction.mask_v4wait == 0) {
4635318500Swulf			if (id < ELANTECH_MAX_FINGERS)
4636318500Swulf				f[id] = fn;
4637318500Swulf			break;
4638307576Sgonzo		}
4639318500Swulf
4640318500Swulf		/* Remove finger from waiting bitmap and store into context */
4641318500Swulf		sc->elanaction.mask_v4wait &= ~(1 << id);
4642318500Swulf		if (id < ELANTECH_MAX_FINGERS)
4643318500Swulf			sc->elanaction.fingers[id] = fn;
4644318500Swulf
4645318500Swulf		/* Wait for other fingers if needed */
4646318500Swulf		if (sc->elanaction.mask_v4wait != 0)
4647318500Swulf			return (0);
4648318500Swulf
4649318500Swulf		/* All new fingers are received. Report them from context */
4650318500Swulf		for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4651318500Swulf			if (sc->elanaction.mask & (1 << id))
4652318500Swulf				f[id] =  sc->elanaction.fingers[id];
4653318500Swulf
4654307576Sgonzo		break;
4655307576Sgonzo
4656307576Sgonzo	case ELANTECH_PKT_V4_MOTION:	/* HW Version 4. Motion packet */
4657307576Sgonzo		/*               7   6   5   4   3   2   1   0 (LSB)
4658307576Sgonzo		 * -------------------------------------------
4659307576Sgonzo		 * ipacket[0]: ID2 ID1 ID0  OF   0   1   R   L
4660307576Sgonzo		 * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4661307576Sgonzo		 * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4662307576Sgonzo		 * ipacket[3]: ID2 ID1 ID0   1   0   0   1   0
4663307576Sgonzo		 * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4664307576Sgonzo		 * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4665307576Sgonzo		 * -------------------------------------------
4666307576Sgonzo		 * OF: delta overflows (> 127 or < -128), in this case
4667307576Sgonzo		 *     firmware sends us (delta x / 5) and (delta y / 5)
4668307576Sgonzo		 * ID: finger id
4669307576Sgonzo		 * DX: delta x (two's complement)
4670307576Sgonzo		 * XY: delta y (two's complement)
4671307576Sgonzo		 * byte 0 ~ 2 for one finger
4672307576Sgonzo		 * byte 3 ~ 5 for another finger
4673307576Sgonzo		 */
4674307576Sgonzo		mask = sc->elanaction.mask;
4675307576Sgonzo		nfingers = bitcount(mask);
4676307576Sgonzo
4677307576Sgonzo		scale = (pb->ipacket[0] & 0x10) ? 5 : 1;
4678307576Sgonzo		for (i = 0; i <= 3; i += 3) {
4679307576Sgonzo			id = ((pb->ipacket[i] & 0xe0) >> 5) - 1;
4680307576Sgonzo			if (id < 0 || id >= ELANTECH_MAX_FINGERS)
4681307576Sgonzo				continue;
4682307576Sgonzo
4683307576Sgonzo			if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) {
4684307576Sgonzo				f[id] = sc->elanaction.fingers[id];
4685307576Sgonzo				f[id].x += imax(-f[id].x,
4686307576Sgonzo				    (signed char)pb->ipacket[i+1] * scale);
4687307576Sgonzo				f[id].y += imax(-f[id].y,
4688307576Sgonzo				    (signed char)pb->ipacket[i+2] * scale);
4689307576Sgonzo			} else {
4690307576Sgonzo				VLOG(3, (LOG_DEBUG, "elantech: "
4691307576Sgonzo				    "HW v4 motion packet skipped\n"));
4692307576Sgonzo			}
4693307576Sgonzo		}
4694307576Sgonzo
4695307576Sgonzo		break;
4696307576Sgonzo
4697307576Sgonzo	case ELANTECH_PKT_TRACKPOINT:
4698307576Sgonzo		/*               7   6   5   4   3   2   1   0 (LSB)
4699307576Sgonzo		 * -------------------------------------------
4700349541Swulf		 * ipacket[0]:   0   0  SY  SX   0   M   R   L
4701307576Sgonzo		 * ipacket[1]: ~SX   0   0   0   0   0   0   0
4702307576Sgonzo		 * ipacket[2]: ~SY   0   0   0   0   0   0   0
4703307576Sgonzo		 * ipacket[3]:   0   0 ~SY ~SX   0   1   1   0
4704307576Sgonzo		 * ipacket[4]:  X7  X6  X5  X4  X3  X2  X1  X0
4705307576Sgonzo		 * ipacket[5]:  Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
4706307576Sgonzo		 * -------------------------------------------
4707307576Sgonzo		 * X and Y are written in two's complement spread
4708307576Sgonzo		 * over 9 bits with SX/SY the relative top bit and
4709307576Sgonzo		 * X7..X0 and Y7..Y0 the lower bits.
4710307576Sgonzo		 */
4711349541Swulf		if (!(pb->ipacket[0] & 0xC8) && !(pb->ipacket[1] & 0x7F) &&
4712349541Swulf		    !(pb->ipacket[2] & 0x7F) && !(pb->ipacket[3] & 0xC9) &&
4713349541Swulf		    !(pb->ipacket[0] & 0x10) != !(pb->ipacket[1] & 0x80) &&
4714349541Swulf		    !(pb->ipacket[0] & 0x10) != !(pb->ipacket[3] & 0x10) &&
4715349541Swulf		    !(pb->ipacket[0] & 0x20) != !(pb->ipacket[2] & 0x80) &&
4716349541Swulf		    !(pb->ipacket[0] & 0x20) != !(pb->ipacket[3] & 0x20)) {
4717307576Sgonzo
4718349541Swulf			*x = (pb->ipacket[0] & MOUSE_PS2_XNEG) ?
4719349541Swulf			    pb->ipacket[4] - 256 : pb->ipacket[4];
4720349541Swulf			*y = (pb->ipacket[0] & MOUSE_PS2_YNEG) ?
4721349541Swulf			    pb->ipacket[5] - 256 : pb->ipacket[5];
4722349541Swulf
4723349541Swulf			trackpoint_button =
4724349541Swulf			    ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4725349541Swulf			    ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) |
4726349541Swulf			    ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0);
4727321060Swulf#ifdef EVDEV_SUPPORT
4728349541Swulf			evdev_push_rel(sc->evdev_r, REL_X, *x);
4729349541Swulf			evdev_push_rel(sc->evdev_r, REL_Y, -*y);
4730349541Swulf			evdev_push_mouse_btn(sc->evdev_r, trackpoint_button);
4731349541Swulf			evdev_sync(sc->evdev_r);
4732321060Swulf#endif
4733349541Swulf			ms->button = touchpad_button | trackpoint_button;
4734349541Swulf		} else
4735349541Swulf			VLOG(3, (LOG_DEBUG, "elantech: "
4736349541Swulf			    "unexpected trackpoint packet skipped\n"));
4737307576Sgonzo		return (0);
4738307576Sgonzo
4739307576Sgonzo	case ELANTECH_PKT_NOP:
4740307576Sgonzo		return (0);
4741307576Sgonzo
4742307576Sgonzo	default:
4743307576Sgonzo		return (-1);
4744307576Sgonzo	}
4745307576Sgonzo
4746307576Sgonzo	for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4747307576Sgonzo		if (PSM_FINGER_IS_SET(f[id]))
4748307576Sgonzo			VLOG(2, (LOG_DEBUG, "elantech: "
4749307576Sgonzo			    "finger %d: down [%d, %d], %d, %d, %d\n", id + 1,
4750307576Sgonzo			    f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags));
4751307576Sgonzo
4752307576Sgonzo	/* Touchpad button presses */
4753307576Sgonzo	if (sc->elanhw.isclickpad) {
4754307576Sgonzo		touchpad_button =
4755307576Sgonzo		    ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0);
4756307576Sgonzo	} else {
4757307576Sgonzo		touchpad_button =
4758307576Sgonzo		    ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4759307576Sgonzo		    ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0);
4760307576Sgonzo	}
4761307576Sgonzo
4762321060Swulf#ifdef EVDEV_SUPPORT
4763321060Swulf	if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
4764321060Swulf		for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4765321060Swulf			if (PSM_FINGER_IS_SET(f[id])) {
4766321060Swulf				psm_push_mt_finger(sc, id, &f[id]);
4767321060Swulf				/* Convert touch width to surface units */
4768321060Swulf				evdev_push_abs(sc->evdev_a, ABS_MT_TOUCH_MAJOR,
4769321060Swulf				    f[id].w * sc->elanhw.dptracex);
4770321060Swulf			}
4771321060Swulf			if (sc->elanaction.mask & (1 << id) &&
4772321060Swulf			    !(mask & (1 << id)))
4773321060Swulf				psm_release_mt_slot(sc->evdev_a, id);
4774321060Swulf		}
4775321060Swulf		evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
4776321060Swulf		evdev_push_nfingers(sc->evdev_a, nfingers);
4777321060Swulf		if (nfingers > 0) {
4778321060Swulf			if (PSM_FINGER_IS_SET(f[0]))
4779321060Swulf				psm_push_st_finger(sc, &f[0]);
4780321060Swulf		} else
4781321060Swulf			evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
4782321060Swulf		evdev_push_mouse_btn(sc->evdev_a, touchpad_button);
4783321060Swulf		evdev_sync(sc->evdev_a);
4784321060Swulf	}
4785321060Swulf#endif
4786321060Swulf
4787307576Sgonzo	ms->button = touchpad_button | trackpoint_button;
4788307576Sgonzo
4789349541Swulf	/* Palm detection doesn't terminate the current action. */
4790349541Swulf	palm = psmpalmdetect(sc, &f[0], nfingers);
4791349541Swulf
4792318500Swulf	/* Send finger 1 position to gesture processor */
4793349541Swulf	if ((PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) ||
4794349541Swulf	    nfingers == 0) && !palm)
4795318500Swulf		psmgestures(sc, &f[0], imin(nfingers, 3), ms);
4796349541Swulf
4797318500Swulf	/* Send fingers positions to movement smoothers */
4798318500Swulf	for (id = 0; id < PSM_FINGERS; id++)
4799318500Swulf		if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id)))
4800318500Swulf			psmsmoother(sc, &f[id], id, ms, x, y);
4801307576Sgonzo
4802307576Sgonzo	/* Store current finger positions in action context */
4803307576Sgonzo	for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4804307576Sgonzo		if (PSM_FINGER_IS_SET(f[id]))
4805307576Sgonzo			sc->elanaction.fingers[id] = f[id];
4806307576Sgonzo		if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id)))
4807307576Sgonzo			PSM_FINGER_RESET(sc->elanaction.fingers[id]);
4808307576Sgonzo	}
4809307576Sgonzo	sc->elanaction.mask = mask;
4810307576Sgonzo
4811349541Swulf	if (palm) {
4812318500Swulf		*x = *y = *z = 0;
4813318500Swulf		ms->button = ms->obutton;
4814318500Swulf		return (0);
4815318500Swulf	}
4816318500Swulf
4817307576Sgonzo	/* Use the extra buttons as a scrollwheel */
4818307576Sgonzo	if (ms->button & MOUSE_BUTTON4DOWN)
4819178019Sjkim		*z = -1;
4820307576Sgonzo	else if (ms->button & MOUSE_BUTTON5DOWN)
4821178019Sjkim		*z = 1;
4822307576Sgonzo	else if (ms->button & MOUSE_BUTTON6DOWN)
4823183888Sdumbbell		*z = -2;
4824307576Sgonzo	else if (ms->button & MOUSE_BUTTON7DOWN)
4825183888Sdumbbell		*z = 2;
4826307576Sgonzo	else
4827178019Sjkim		*z = 0;
4828307576Sgonzo	ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
4829307576Sgonzo	    MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
4830178019Sjkim
4831178019Sjkim	return (0);
4832178019Sjkim}
4833178019Sjkim
4834178019Sjkimstatic void
4835178019Sjkimproc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4836178019Sjkim    int *x, int *y, int *z)
4837178019Sjkim{
4838178019Sjkim	static int butmap_versapad[8] = {
4839178019Sjkim		0,
4840178019Sjkim		MOUSE_BUTTON3DOWN,
4841178019Sjkim		0,
4842178019Sjkim		MOUSE_BUTTON3DOWN,
4843178019Sjkim		MOUSE_BUTTON1DOWN,
4844178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4845178019Sjkim		MOUSE_BUTTON1DOWN,
4846178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
4847178019Sjkim	};
4848178019Sjkim	int c, x0, y0;
4849178019Sjkim
4850178019Sjkim	/* VersaPad PS/2 absolute mode message format
4851178019Sjkim	 *
4852178019Sjkim	 * [packet1]     7   6   5   4   3   2   1   0(LSB)
4853178019Sjkim	 *  ipacket[0]:  1   1   0   A   1   L   T   R
4854178019Sjkim	 *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
4855178019Sjkim	 *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
4856178019Sjkim	 *  ipacket[3]:  1   1   1   A   1   L   T   R
4857178019Sjkim	 *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
4858178019Sjkim	 *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
4859178019Sjkim	 *
4860178019Sjkim	 * [note]
4861178019Sjkim	 *  R: right physical mouse button (1=on)
4862178019Sjkim	 *  T: touch pad virtual button (1=tapping)
4863178019Sjkim	 *  L: left physical mouse button (1=on)
4864178019Sjkim	 *  A: position data is valid (1=valid)
4865178019Sjkim	 *  H: horizontal data (12bit signed integer. H11 is sign bit.)
4866178019Sjkim	 *  V: vertical data (12bit signed integer. V11 is sign bit.)
4867178019Sjkim	 *  P: pressure data
4868178019Sjkim	 *
4869178019Sjkim	 * Tapping is mapped to MOUSE_BUTTON4.
4870178019Sjkim	 */
4871178019Sjkim	c = pb->ipacket[0];
4872178019Sjkim	*x = *y = 0;
4873178019Sjkim	ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
4874178019Sjkim	ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
4875178019Sjkim	if (c & MOUSE_PS2VERSA_IN_USE) {
4876178019Sjkim		x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
4877178019Sjkim		y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
4878178019Sjkim		if (x0 & 0x800)
4879178019Sjkim			x0 -= 0x1000;
4880178019Sjkim		if (y0 & 0x800)
4881178019Sjkim			y0 -= 0x1000;
4882178019Sjkim		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
4883178019Sjkim			*x = sc->xold - x0;
4884178019Sjkim			*y = y0 - sc->yold;
4885178019Sjkim			if (*x < 0)	/* XXX */
4886178019Sjkim				++*x;
4887178019Sjkim			else if (*x)
4888178019Sjkim				--*x;
4889178019Sjkim			if (*y < 0)
4890178019Sjkim				++*y;
4891178019Sjkim			else if (*y)
4892178019Sjkim				--*y;
4893178019Sjkim		} else
4894178019Sjkim			sc->flags |= PSM_FLAGS_FINGERDOWN;
4895178019Sjkim		sc->xold = x0;
4896178019Sjkim		sc->yold = y0;
4897178019Sjkim	} else
4898178019Sjkim		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
4899178019Sjkim}
4900178019Sjkim
4901178019Sjkimstatic void
4902307576Sgonzopsmsoftintridle(void *arg)
4903307576Sgonzo{
4904307576Sgonzo	struct psm_softc *sc = arg;
4905307576Sgonzo	packetbuf_t *pb;
4906307576Sgonzo
4907307576Sgonzo	/* Invoke soft handler only when pqueue is empty. Otherwise it will be
4908307576Sgonzo	 * invoked from psmintr soon with pqueue filled with real data */
4909307576Sgonzo	if (sc->pqueue_start == sc->pqueue_end &&
4910307576Sgonzo	    sc->idlepacket.inputbytes > 0) {
4911307576Sgonzo		/* Grow circular queue backwards to avoid race with psmintr */
4912307576Sgonzo		if (--sc->pqueue_start < 0)
4913307576Sgonzo			sc->pqueue_start = PSM_PACKETQUEUE - 1;
4914307576Sgonzo
4915307576Sgonzo		pb = &sc->pqueue[sc->pqueue_start];
4916307576Sgonzo		memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t));
4917307576Sgonzo		VLOG(4, (LOG_DEBUG,
4918307576Sgonzo		    "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n",
4919307576Sgonzo		    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
4920307576Sgonzo		    pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
4921307576Sgonzo
4922307576Sgonzo		psmsoftintr(arg);
4923307576Sgonzo	}
4924307576Sgonzo}
4925307576Sgonzo
4926307576Sgonzostatic void
4927178019Sjkimpsmsoftintr(void *arg)
4928178019Sjkim{
4929178019Sjkim	/*
4930178019Sjkim	 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
4931178019Sjkim	 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
4932178019Sjkim	 */
4933178019Sjkim	static int butmap[8] = {
4934178019Sjkim		0,
4935178019Sjkim		MOUSE_BUTTON1DOWN,
4936178019Sjkim		MOUSE_BUTTON3DOWN,
4937178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4938178019Sjkim		MOUSE_BUTTON2DOWN,
4939178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
4940178019Sjkim		MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
4941178019Sjkim		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
4942178019Sjkim	};
4943212355Sed	struct psm_softc *sc = arg;
4944178019Sjkim	mousestatus_t ms;
4945178019Sjkim	packetbuf_t *pb;
4946178019Sjkim	int x, y, z, c, l, s;
4947178019Sjkim
4948178019Sjkim	getmicrouptime(&sc->lastsoftintr);
4949178019Sjkim
4950178019Sjkim	s = spltty();
4951178019Sjkim
4952178019Sjkim	do {
4953178019Sjkim		pb = &sc->pqueue[sc->pqueue_start];
4954178019Sjkim
4955178019Sjkim		if (sc->mode.level == PSM_LEVEL_NATIVE)
4956178019Sjkim			goto next_native;
4957178019Sjkim
4958178019Sjkim		c = pb->ipacket[0];
4959178019Sjkim		/*
4960178019Sjkim		 * A kludge for Kensington device!
4961178019Sjkim		 * The MSB of the horizontal count appears to be stored in
4962178019Sjkim		 * a strange place.
4963178019Sjkim		 */
4964178019Sjkim		if (sc->hw.model == MOUSE_MODEL_THINK)
4965178019Sjkim			pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
4966178019Sjkim
4967178019Sjkim		/* ignore the overflow bits... */
4968178019Sjkim		x = (c & MOUSE_PS2_XNEG) ?
4969178019Sjkim		    pb->ipacket[1] - 256 : pb->ipacket[1];
4970178019Sjkim		y = (c & MOUSE_PS2_YNEG) ?
4971178019Sjkim		    pb->ipacket[2] - 256 : pb->ipacket[2];
4972133296Sphilip		z = 0;
4973178019Sjkim		ms.obutton = sc->button;	  /* previous button state */
4974178019Sjkim		ms.button = butmap[c & MOUSE_PS2_BUTTONS];
4975178019Sjkim		/* `tapping' action */
4976178019Sjkim		if (sc->config & PSM_CONFIG_FORCETAP)
4977178019Sjkim			ms.button |= ((c & MOUSE_PS2_TAP)) ?
4978178019Sjkim			    0 : MOUSE_BUTTON4DOWN;
4979307576Sgonzo		timevalclear(&sc->idletimeout);
4980307576Sgonzo		sc->idlepacket.inputbytes = 0;
4981133296Sphilip
4982178019Sjkim		switch (sc->hw.model) {
4983132865Snjl
4984178019Sjkim		case MOUSE_MODEL_EXPLORER:
4985178019Sjkim			/*
4986178019Sjkim			 *          b7 b6 b5 b4 b3 b2 b1 b0
4987178019Sjkim			 * byte 1:  oy ox sy sx 1  M  R  L
4988178019Sjkim			 * byte 2:  x  x  x  x  x  x  x  x
4989178019Sjkim			 * byte 3:  y  y  y  y  y  y  y  y
4990178019Sjkim			 * byte 4:  *  *  S2 S1 s  d2 d1 d0
4991178019Sjkim			 *
4992178019Sjkim			 * L, M, R, S1, S2: left, middle, right and side buttons
4993178019Sjkim			 * s: wheel data sign bit
4994178019Sjkim			 * d2-d0: wheel data
4995178019Sjkim			 */
4996178019Sjkim			z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
4997178019Sjkim			    (pb->ipacket[3] & 0x0f) - 16 :
4998178019Sjkim			    (pb->ipacket[3] & 0x0f);
4999178019Sjkim			ms.button |=
5000178019Sjkim			    (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
5001178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
5002178019Sjkim			ms.button |=
5003178019Sjkim			    (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
5004178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
5005178019Sjkim			break;
5006178019Sjkim
5007178019Sjkim		case MOUSE_MODEL_INTELLI:
5008178019Sjkim		case MOUSE_MODEL_NET:
5009178019Sjkim			/* wheel data is in the fourth byte */
5010178019Sjkim			z = (char)pb->ipacket[3];
5011178019Sjkim			/*
5012178019Sjkim			 * XXX some mice may send 7 when there is no Z movement?			 */
5013178019Sjkim			if ((z >= 7) || (z <= -7))
5014178019Sjkim				z = 0;
5015178019Sjkim			/* some compatible mice have additional buttons */
5016178019Sjkim			ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
5017178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
5018178019Sjkim			ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
5019178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
5020178019Sjkim			break;
5021178019Sjkim
5022178019Sjkim		case MOUSE_MODEL_MOUSEMANPLUS:
5023178019Sjkim			proc_mmanplus(sc, pb, &ms, &x, &y, &z);
5024178019Sjkim			break;
5025178019Sjkim
5026178019Sjkim		case MOUSE_MODEL_GLIDEPOINT:
5027178019Sjkim			/* `tapping' action */
5028178019Sjkim			ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
5029178019Sjkim			    MOUSE_BUTTON4DOWN;
5030178019Sjkim			break;
5031178019Sjkim
5032178019Sjkim		case MOUSE_MODEL_NETSCROLL:
5033178019Sjkim			/*
5034298955Spfg			 * three additional bytes encode buttons and
5035178019Sjkim			 * wheel events
5036178019Sjkim			 */
5037178019Sjkim			ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
5038178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
5039178019Sjkim			ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
5040178019Sjkim			    MOUSE_BUTTON5DOWN : 0;
5041178019Sjkim			z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
5042178019Sjkim			    pb->ipacket[4] - 256 : pb->ipacket[4];
5043178019Sjkim			break;
5044178019Sjkim
5045178019Sjkim		case MOUSE_MODEL_THINK:
5046178019Sjkim			/* the fourth button state in the first byte */
5047178019Sjkim			ms.button |= (c & MOUSE_PS2_TAP) ?
5048178019Sjkim			    MOUSE_BUTTON4DOWN : 0;
5049178019Sjkim			break;
5050178019Sjkim
5051178019Sjkim		case MOUSE_MODEL_VERSAPAD:
5052178019Sjkim			proc_versapad(sc, pb, &ms, &x, &y, &z);
5053178019Sjkim			c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
5054178019Sjkim			    ((y < 0) ? MOUSE_PS2_YNEG : 0);
5055178019Sjkim			break;
5056281440Srpaulo
5057178019Sjkim		case MOUSE_MODEL_4D:
5058178019Sjkim			/*
5059178019Sjkim			 *          b7 b6 b5 b4 b3 b2 b1 b0
5060178019Sjkim			 * byte 1:  s2 d2 s1 d1 1  M  R  L
5061178019Sjkim			 * byte 2:  sx x  x  x  x  x  x  x
5062178019Sjkim			 * byte 3:  sy y  y  y  y  y  y  y
5063178019Sjkim			 *
5064178019Sjkim			 * s1: wheel 1 direction
5065178019Sjkim			 * d1: wheel 1 data
5066178019Sjkim			 * s2: wheel 2 direction
5067178019Sjkim			 * d2: wheel 2 data
5068178019Sjkim			 */
5069178019Sjkim			x = (pb->ipacket[1] & 0x80) ?
5070178019Sjkim			    pb->ipacket[1] - 256 : pb->ipacket[1];
5071178019Sjkim			y = (pb->ipacket[2] & 0x80) ?
5072178019Sjkim			    pb->ipacket[2] - 256 : pb->ipacket[2];
5073178019Sjkim			switch (c & MOUSE_4D_WHEELBITS) {
5074178019Sjkim			case 0x10:
5075178019Sjkim				z = 1;
5076178019Sjkim				break;
5077178019Sjkim			case 0x30:
5078178019Sjkim				z = -1;
5079178019Sjkim				break;
5080178019Sjkim			case 0x40:	/* XXX 2nd wheel turning right */
5081178019Sjkim				z = 2;
5082178019Sjkim				break;
5083178019Sjkim			case 0xc0:	/* XXX 2nd wheel turning left */
5084178019Sjkim				z = -2;
5085178019Sjkim				break;
5086178019Sjkim			}
5087178019Sjkim			break;
5088178019Sjkim
5089178019Sjkim		case MOUSE_MODEL_4DPLUS:
5090178019Sjkim			if ((x < 16 - 256) && (y < 16 - 256)) {
5091178019Sjkim				/*
5092178019Sjkim				 *          b7 b6 b5 b4 b3 b2 b1 b0
5093178019Sjkim				 * byte 1:  0  0  1  1  1  M  R  L
5094178019Sjkim				 * byte 2:  0  0  0  0  1  0  0  0
5095178019Sjkim				 * byte 3:  0  0  0  0  S  s  d1 d0
5096178019Sjkim				 *
5097178019Sjkim				 * L, M, R, S: left, middle, right,
5098178019Sjkim				 *             and side buttons
5099178019Sjkim				 * s: wheel data sign bit
5100178019Sjkim				 * d1-d0: wheel data
5101178019Sjkim				 */
5102178019Sjkim				x = y = 0;
5103178019Sjkim				if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
5104178019Sjkim					ms.button |= MOUSE_BUTTON4DOWN;
5105178019Sjkim				z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
5106178019Sjkim				    ((pb->ipacket[2] & 0x07) - 8) :
5107178019Sjkim				    (pb->ipacket[2] & 0x07) ;
5108178019Sjkim			} else {
5109178019Sjkim				/* preserve previous button states */
5110178019Sjkim				ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
5111178019Sjkim			}
5112178019Sjkim			break;
5113178019Sjkim
5114178019Sjkim		case MOUSE_MODEL_SYNAPTICS:
5115343158Swulf			if (pb->inputbytes == MOUSE_PS2_PACKETSIZE)
5116343158Swulf				if (proc_synaptics_mux(sc, pb))
5117343158Swulf					goto next;
5118343158Swulf
5119329532Swulf			if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0) {
5120329532Swulf				VLOG(3, (LOG_DEBUG, "synaptics: "
5121329532Swulf				    "packet rejected\n"));
5122178019Sjkim				goto next;
5123329532Swulf			}
5124178019Sjkim			break;
5125178019Sjkim
5126307576Sgonzo		case MOUSE_MODEL_ELANTECH:
5127329532Swulf			if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0) {
5128329532Swulf				VLOG(3, (LOG_DEBUG, "elantech: "
5129329532Swulf				    "packet rejected\n"));
5130307576Sgonzo				goto next;
5131329532Swulf			}
5132307576Sgonzo			break;
5133307576Sgonzo
5134248478Sjkim		case MOUSE_MODEL_TRACKPOINT:
5135178019Sjkim		case MOUSE_MODEL_GENERIC:
5136178019Sjkim		default:
5137178019Sjkim			break;
5138178019Sjkim		}
5139178019Sjkim
5140321060Swulf#ifdef EVDEV_SUPPORT
5141321060Swulf	if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE &&
5142321060Swulf	    sc->hw.model != MOUSE_MODEL_ELANTECH &&
5143321060Swulf	    sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
5144334763Shselasky		evdev_push_rel(sc->evdev_r, REL_X, x);
5145334763Shselasky		evdev_push_rel(sc->evdev_r, REL_Y, -y);
5146321060Swulf
5147321060Swulf		switch (sc->hw.model) {
5148321060Swulf		case MOUSE_MODEL_EXPLORER:
5149321060Swulf		case MOUSE_MODEL_INTELLI:
5150321060Swulf		case MOUSE_MODEL_NET:
5151321060Swulf		case MOUSE_MODEL_NETSCROLL:
5152321060Swulf		case MOUSE_MODEL_4DPLUS:
5153321060Swulf			evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
5154321060Swulf			break;
5155321060Swulf		case MOUSE_MODEL_MOUSEMANPLUS:
5156321060Swulf		case MOUSE_MODEL_4D:
5157321060Swulf			switch (z) {
5158321060Swulf			case 1:
5159321060Swulf			case -1:
5160321060Swulf				evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
5161321060Swulf				break;
5162321060Swulf			case 2:
5163321060Swulf			case -2:
5164321060Swulf				evdev_push_rel(sc->evdev_r, REL_HWHEEL, z / 2);
5165321060Swulf				break;
5166321060Swulf			}
5167321060Swulf			break;
5168321060Swulf		}
5169321060Swulf
5170321060Swulf		evdev_push_mouse_btn(sc->evdev_r, ms.button);
5171321060Swulf		evdev_sync(sc->evdev_r);
5172321060Swulf	}
5173321060Swulf#endif
5174321060Swulf
5175178019Sjkim	/* scale values */
5176178019Sjkim	if (sc->mode.accelfactor >= 1) {
5177178019Sjkim		if (x != 0) {
5178178019Sjkim			x = x * x / sc->mode.accelfactor;
5179178019Sjkim			if (x == 0)
5180178019Sjkim				x = 1;
5181178019Sjkim			if (c & MOUSE_PS2_XNEG)
5182178019Sjkim				x = -x;
5183178019Sjkim		}
5184178019Sjkim		if (y != 0) {
5185178019Sjkim			y = y * y / sc->mode.accelfactor;
5186178019Sjkim			if (y == 0)
5187178019Sjkim				y = 1;
5188178019Sjkim			if (c & MOUSE_PS2_YNEG)
5189178019Sjkim				y = -y;
5190178019Sjkim		}
519141016Sdfr	}
519241016Sdfr
5193307576Sgonzo	/* Store last packet for reinjection if it has not been set already */
5194307576Sgonzo	if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0)
5195307576Sgonzo		sc->idlepacket = *pb;
5196307576Sgonzo
5197178019Sjkim	ms.dx = x;
5198178019Sjkim	ms.dy = y;
5199178019Sjkim	ms.dz = z;
5200178019Sjkim	ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
5201178019Sjkim	    (ms.obutton ^ ms.button);
520241016Sdfr
5203178017Sjkim	pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
520441016Sdfr
5205178019Sjkim	sc->status.flags |= ms.flags;
5206178019Sjkim	sc->status.dx += ms.dx;
5207178019Sjkim	sc->status.dy += ms.dy;
5208178019Sjkim	sc->status.dz += ms.dz;
5209178019Sjkim	sc->status.button = ms.button;
5210178019Sjkim	sc->button = ms.button;
521141016Sdfr
5212178019Sjkimnext_native:
521358230Syokota	sc->watchdog = FALSE;
521458230Syokota
5215178019Sjkim	/* queue data */
5216178019Sjkim	if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
5217178019Sjkim		l = imin(pb->inputbytes,
5218178019Sjkim		    sizeof(sc->queue.buf) - sc->queue.tail);
5219178019Sjkim		bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
5220178019Sjkim		if (pb->inputbytes > l)
5221178019Sjkim			bcopy(&pb->ipacket[l], &sc->queue.buf[0],
5222178019Sjkim			    pb->inputbytes - l);
5223178019Sjkim		sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
5224178019Sjkim		    sizeof(sc->queue.buf);
5225178019Sjkim		sc->queue.count += pb->inputbytes;
522641016Sdfr	}
522741016Sdfr
5228178019Sjkimnext:
5229329532Swulf	pb->inputbytes = 0;
5230123442Salfred	if (++sc->pqueue_start >= PSM_PACKETQUEUE)
5231123442Salfred		sc->pqueue_start = 0;
5232178019Sjkim	} while (sc->pqueue_start != sc->pqueue_end);
5233178019Sjkim
5234178019Sjkim	if (sc->state & PSM_ASLP) {
5235178019Sjkim		sc->state &= ~PSM_ASLP;
5236178019Sjkim		wakeup(sc);
5237178019Sjkim	}
5238178019Sjkim	selwakeuppri(&sc->rsel, PZERO);
5239189870Srnoland	if (sc->async != NULL) {
5240189870Srnoland		pgsigio(&sc->async, SIGIO, 0);
5241189870Srnoland	}
5242178019Sjkim	sc->state &= ~PSM_SOFTARMED;
5243307576Sgonzo
5244307576Sgonzo	/* schedule injection of predefined packet after idletimeout
5245307576Sgonzo	 * if no data packets have been received from psmintr */
5246307576Sgonzo	if (timevalisset(&sc->idletimeout)) {
5247307576Sgonzo		sc->state |= PSM_SOFTARMED;
5248307576Sgonzo		callout_reset(&sc->softcallout, tvtohz(&sc->idletimeout),
5249307576Sgonzo		    psmsoftintridle, sc);
5250307576Sgonzo		VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n",
5251307576Sgonzo		    tvtohz(&sc->idletimeout)));
5252307576Sgonzo	}
5253178019Sjkim	splx(s);
525441016Sdfr}
525541016Sdfr
525641016Sdfrstatic int
5257130585Sphkpsmpoll(struct cdev *dev, int events, struct thread *td)
525841016Sdfr{
5259212355Sed	struct psm_softc *sc = dev->si_drv1;
5260178019Sjkim	int s;
5261178019Sjkim	int revents = 0;
526241016Sdfr
5263178019Sjkim	/* Return true if a mouse event available */
5264178019Sjkim	s = spltty();
5265178019Sjkim	if (events & (POLLIN | POLLRDNORM)) {
5266178019Sjkim		if (sc->queue.count > 0)
5267178019Sjkim			revents |= events & (POLLIN | POLLRDNORM);
5268178019Sjkim		else
5269178019Sjkim			selrecord(td, &sc->rsel);
5270178019Sjkim	}
5271178019Sjkim	splx(s);
527241016Sdfr
5273178019Sjkim	return (revents);
527441016Sdfr}
527541016Sdfr
527641016Sdfr/* vendor/model specific routines */
527741016Sdfr
527841016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
527941016Sdfr{
5280178019Sjkim	if (set_mouse_resolution(kbdc, res) != res)
5281178019Sjkim		return (FALSE);
5282178019Sjkim	if (set_mouse_scaling(kbdc, scale) &&
5283178019Sjkim	    set_mouse_scaling(kbdc, scale) &&
5284178019Sjkim	    set_mouse_scaling(kbdc, scale) &&
5285178019Sjkim	    (get_mouse_status(kbdc, status, 0, 3) >= 3))
5286178019Sjkim		return (TRUE);
5287178019Sjkim	return (FALSE);
528841016Sdfr}
528941016Sdfr
5290178019Sjkimstatic int
529169438Syokotamouse_ext_command(KBDC kbdc, int command)
529269438Syokota{
5293178019Sjkim	int c;
529469438Syokota
5295178019Sjkim	c = (command >> 6) & 0x03;
5296178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
5297178019Sjkim		return (FALSE);
5298178019Sjkim	c = (command >> 4) & 0x03;
5299178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
5300178019Sjkim		return (FALSE);
5301178019Sjkim	c = (command >> 2) & 0x03;
5302178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
5303178019Sjkim		return (FALSE);
5304178019Sjkim	c = (command >> 0) & 0x03;
5305178019Sjkim	if (set_mouse_resolution(kbdc, c) != c)
5306178019Sjkim		return (FALSE);
5307178019Sjkim	return (TRUE);
530869438Syokota}
530969438Syokota
5310153072Sru#ifdef notyet
531141016Sdfr/* Logitech MouseMan Cordless II */
531241016Sdfrstatic int
5313284320Sglebiusenable_lcordless(struct psm_softc *sc, enum probearg arg)
531441016Sdfr{
5315284320Sglebius	KBDC kbdc = sc->kbdc;
5316178019Sjkim	int status[3];
5317178019Sjkim	int ch;
531841016Sdfr
5319233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status))
5320178019Sjkim		return (FALSE);
5321178019Sjkim	if (status[1] == PSMD_RES_HIGH)
5322178019Sjkim		return (FALSE);
5323178019Sjkim	ch = (status[0] & 0x07) - 1;	/* channel # */
5324178019Sjkim	if ((ch <= 0) || (ch > 4))
5325178019Sjkim		return (FALSE);
5326178019Sjkim	/*
5327178019Sjkim	 * status[1]: always one?
5328178019Sjkim	 * status[2]: battery status? (0-100)
5329178019Sjkim	 */
5330178019Sjkim	return (TRUE);
533141016Sdfr}
533241016Sdfr#endif /* notyet */
533341016Sdfr
533458230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
533541016Sdfrstatic int
5336284320Sglebiusenable_groller(struct psm_softc *sc, enum probearg arg)
533741016Sdfr{
5338284320Sglebius	KBDC kbdc = sc->kbdc;
5339178019Sjkim	int status[3];
534041016Sdfr
5341178019Sjkim	/*
5342178019Sjkim	 * The special sequence to enable the fourth button and the
5343178019Sjkim	 * roller. Immediately after this sequence check status bytes.
5344178019Sjkim	 * if the mouse is NetScroll, the second and the third bytes are
5345178019Sjkim	 * '3' and 'D'.
5346178019Sjkim	 */
534741016Sdfr
5348178019Sjkim	/*
5349178019Sjkim	 * If the mouse is an ordinary PS/2 mouse, the status bytes should
5350178019Sjkim	 * look like the following.
5351178019Sjkim	 *
5352178019Sjkim	 * byte 1 bit 7 always 0
5353178019Sjkim	 *        bit 6 stream mode (0)
5354178019Sjkim	 *        bit 5 disabled (0)
5355178019Sjkim	 *        bit 4 1:1 scaling (0)
5356178019Sjkim	 *        bit 3 always 0
5357178019Sjkim	 *        bit 0-2 button status
5358178019Sjkim	 * byte 2 resolution (PSMD_RES_HIGH)
5359178019Sjkim	 * byte 3 report rate (?)
5360178019Sjkim	 */
536141016Sdfr
5362233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5363178019Sjkim		return (FALSE);
5364178019Sjkim	if ((status[1] != '3') || (status[2] != 'D'))
5365178019Sjkim		return (FALSE);
5366178019Sjkim	/* FIXME: SmartScroll Mouse has 5 buttons! XXX */
5367284320Sglebius	if (arg == PROBE)
5368233580Sjkim		sc->hw.buttons = 4;
5369178019Sjkim	return (TRUE);
537041016Sdfr}
537141016Sdfr
537258230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
537341016Sdfrstatic int
5374284320Sglebiusenable_gmouse(struct psm_softc *sc, enum probearg arg)
537541016Sdfr{
5376284320Sglebius	KBDC kbdc = sc->kbdc;
5377178019Sjkim	int status[3];
537841016Sdfr
5379178019Sjkim	/*
5380178019Sjkim	 * The special sequence to enable the middle, "rubber" button.
5381178019Sjkim	 * Immediately after this sequence check status bytes.
5382178019Sjkim	 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
5383178019Sjkim	 * the second and the third bytes are '3' and 'U'.
5384178019Sjkim	 * NOTE: NetMouse reports that it has three buttons although it has
5385178019Sjkim	 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
5386178019Sjkim	 * say they have three buttons too and they do have a button on the
5387178019Sjkim	 * side...
5388178019Sjkim	 */
5389233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5390178019Sjkim		return (FALSE);
5391178019Sjkim	if ((status[1] != '3') || (status[2] != 'U'))
5392178019Sjkim		return (FALSE);
5393178019Sjkim	return (TRUE);
539441016Sdfr}
539541016Sdfr
539641016Sdfr/* ALPS GlidePoint */
539741016Sdfrstatic int
5398284320Sglebiusenable_aglide(struct psm_softc *sc, enum probearg arg)
539941016Sdfr{
5400284320Sglebius	KBDC kbdc = sc->kbdc;
5401178019Sjkim	int status[3];
540241016Sdfr
5403178019Sjkim	/*
5404178019Sjkim	 * The special sequence to obtain ALPS GlidePoint specific
5405178019Sjkim	 * information. Immediately after this sequence, status bytes will
5406178019Sjkim	 * contain something interesting.
5407178019Sjkim	 * NOTE: ALPS produces several models of GlidePoint. Some of those
5408178019Sjkim	 * do not respond to this sequence, thus, cannot be detected this way.
5409178019Sjkim	 */
5410233580Sjkim	if (set_mouse_sampling_rate(kbdc, 100) != 100)
5411178019Sjkim		return (FALSE);
5412233580Sjkim	if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
5413178019Sjkim		return (FALSE);
5414178019Sjkim	if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
5415178019Sjkim		return (FALSE);
5416178019Sjkim	return (TRUE);
541741016Sdfr}
541841016Sdfr
541941016Sdfr/* Kensington ThinkingMouse/Trackball */
542041016Sdfrstatic int
5421284320Sglebiusenable_kmouse(struct psm_softc *sc, enum probearg arg)
542241016Sdfr{
5423178019Sjkim	static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
5424284320Sglebius	KBDC kbdc = sc->kbdc;
5425178019Sjkim	int status[3];
5426178019Sjkim	int id1;
5427178019Sjkim	int id2;
5428178019Sjkim	int i;
542941016Sdfr
5430178019Sjkim	id1 = get_aux_id(kbdc);
5431178019Sjkim	if (set_mouse_sampling_rate(kbdc, 10) != 10)
5432178019Sjkim		return (FALSE);
5433178019Sjkim	/*
5434178019Sjkim	 * The device is now in the native mode? It returns a different
5435178019Sjkim	 * ID value...
5436178019Sjkim	 */
5437178019Sjkim	id2 = get_aux_id(kbdc);
5438178019Sjkim	if ((id1 == id2) || (id2 != 2))
5439178019Sjkim		return (FALSE);
544041016Sdfr
5441178019Sjkim	if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
5442178019Sjkim		return (FALSE);
544341016Sdfr#if PSM_DEBUG >= 2
5444178019Sjkim	/* at this point, resolution is LOW, sampling rate is 10/sec */
5445178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) < 3)
5446178019Sjkim		return (FALSE);
544741016Sdfr#endif
544841016Sdfr
5449178019Sjkim	/*
5450178019Sjkim	 * The special sequence to enable the third and fourth buttons.
5451178019Sjkim	 * Otherwise they behave like the first and second buttons.
5452178019Sjkim	 */
5453298307Spfg	for (i = 0; i < nitems(rate); ++i)
5454178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5455178019Sjkim			return (FALSE);
545641016Sdfr
5457178019Sjkim	/*
5458178019Sjkim	 * At this point, the device is using default resolution and
5459178019Sjkim	 * sampling rate for the native mode.
5460178019Sjkim	 */
5461178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) < 3)
5462178019Sjkim		return (FALSE);
5463178019Sjkim	if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
5464178019Sjkim		return (FALSE);
546541016Sdfr
5466178019Sjkim	/* the device appears be enabled by this sequence, diable it for now */
5467178019Sjkim	disable_aux_dev(kbdc);
5468178019Sjkim	empty_aux_buffer(kbdc, 5);
546941016Sdfr
5470178019Sjkim	return (TRUE);
547141016Sdfr}
547241016Sdfr
547358230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
547441016Sdfrstatic int
5475284320Sglebiusenable_mmanplus(struct psm_softc *sc, enum probearg arg)
547641016Sdfr{
5477284320Sglebius	KBDC kbdc = sc->kbdc;
5478178019Sjkim	int data[3];
547941016Sdfr
5480178019Sjkim	/* the special sequence to enable the fourth button and the roller. */
5481178019Sjkim	/*
5482178019Sjkim	 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
5483178019Sjkim	 * must be called exactly three times since the last RESET command
5484178019Sjkim	 * before this sequence. XXX
5485178019Sjkim	 */
5486178019Sjkim	if (!set_mouse_scaling(kbdc, 1))
5487178019Sjkim		return (FALSE);
5488178019Sjkim	if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
5489178019Sjkim		return (FALSE);
5490178019Sjkim	if (get_mouse_status(kbdc, data, 1, 3) < 3)
5491178019Sjkim		return (FALSE);
549241016Sdfr
5493178019Sjkim	/*
5494240743Skevlo	 * PS2++ protocol, packet type 0
5495178019Sjkim	 *
5496178019Sjkim	 *          b7 b6 b5 b4 b3 b2 b1 b0
5497178019Sjkim	 * byte 1:  *  1  p3 p2 1  *  *  *
5498178019Sjkim	 * byte 2:  1  1  p1 p0 m1 m0 1  0
5499178019Sjkim	 * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
5500178019Sjkim	 *
5501178019Sjkim	 * p3-p0: packet type: 0
5502178019Sjkim	 * m7-m0: model ID: MouseMan+:0x50,
5503178019Sjkim	 *		    FirstMouse+:0x51,
5504178019Sjkim	 *		    ScrollPoint:0x58...
5505178019Sjkim	 */
5506178019Sjkim	/* check constant bits */
5507178019Sjkim	if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
5508178019Sjkim		return (FALSE);
5509178019Sjkim	if ((data[1] & 0xc3) != 0xc2)
5510178019Sjkim		return (FALSE);
5511178019Sjkim	/* check d3-d0 in byte 2 */
5512178019Sjkim	if (!MOUSE_PS2PLUS_CHECKBITS(data))
5513178019Sjkim		return (FALSE);
5514178019Sjkim	/* check p3-p0 */
5515178019Sjkim	if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
5516178019Sjkim		return (FALSE);
551741016Sdfr
5518284320Sglebius	if (arg == PROBE) {
5519233580Sjkim		sc->hw.hwid &= 0x00ff;
5520233580Sjkim		sc->hw.hwid |= data[2] << 8;	/* save model ID */
5521233580Sjkim	}
552248778Syokota
5523178019Sjkim	/*
5524178019Sjkim	 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
5525178019Sjkim	 * the wheel and the fourth button events are encoded in the
5526178019Sjkim	 * special data packet. The mouse may be put in the IntelliMouse mode
5527178019Sjkim	 * if it is initialized by the IntelliMouse's method.
5528178019Sjkim	 */
5529178019Sjkim	return (TRUE);
553041016Sdfr}
553141016Sdfr
553258230Syokota/* MS IntelliMouse Explorer */
553358230Syokotastatic int
5534284320Sglebiusenable_msexplorer(struct psm_softc *sc, enum probearg arg)
553558230Syokota{
5536284320Sglebius	KBDC kbdc = sc->kbdc;
5537178019Sjkim	static u_char rate0[] = { 200, 100, 80, };
5538178019Sjkim	static u_char rate1[] = { 200, 200, 80, };
5539178019Sjkim	int id;
5540178019Sjkim	int i;
554158230Syokota
5542178019Sjkim	/*
5543178019Sjkim	 * This is needed for at least A4Tech X-7xx mice - they do not go
5544178019Sjkim	 * straight to Explorer mode, but need to be set to Intelli mode
5545178019Sjkim	 * first.
5546178019Sjkim	 */
5547284320Sglebius	enable_msintelli(sc, arg);
5548176554Srink
5549178019Sjkim	/* the special sequence to enable the extra buttons and the roller. */
5550298307Spfg	for (i = 0; i < nitems(rate1); ++i)
5551178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
5552178019Sjkim			return (FALSE);
5553178019Sjkim	/* the device will give the genuine ID only after the above sequence */
5554178019Sjkim	id = get_aux_id(kbdc);
5555178019Sjkim	if (id != PSM_EXPLORER_ID)
5556178019Sjkim		return (FALSE);
555769438Syokota
5558284320Sglebius	if (arg == PROBE) {
5559233580Sjkim		sc->hw.buttons = 5;	/* IntelliMouse Explorer XXX */
5560233580Sjkim		sc->hw.hwid = id;
5561233580Sjkim	}
556269438Syokota
5563178019Sjkim	/*
5564178019Sjkim	 * XXX: this is a kludge to fool some KVM switch products
5565178019Sjkim	 * which think they are clever enough to know the 4-byte IntelliMouse
5566178019Sjkim	 * protocol, and assume any other protocols use 3-byte packets.
5567178019Sjkim	 * They don't convey 4-byte data packets from the IntelliMouse Explorer
5568178019Sjkim	 * correctly to the host computer because of this!
5569178019Sjkim	 * The following sequence is actually IntelliMouse's "wake up"
5570178019Sjkim	 * sequence; it will make the KVM think the mouse is IntelliMouse
5571178019Sjkim	 * when it is in fact IntelliMouse Explorer.
5572178019Sjkim	 */
5573298307Spfg	for (i = 0; i < nitems(rate0); ++i)
5574178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
5575178019Sjkim			break;
5576233580Sjkim	get_aux_id(kbdc);
557758923Syokota
5578178019Sjkim	return (TRUE);
557958230Syokota}
558058230Syokota
5581284320Sglebius/*
5582284320Sglebius * MS IntelliMouse
5583284320Sglebius * Logitech MouseMan+ and FirstMouse+ will also respond to this
5584284320Sglebius * probe routine and act like IntelliMouse.
5585284320Sglebius */
558641016Sdfrstatic int
5587284320Sglebiusenable_msintelli(struct psm_softc *sc, enum probearg arg)
558841016Sdfr{
5589284320Sglebius	KBDC kbdc = sc->kbdc;
5590178019Sjkim	static u_char rate[] = { 200, 100, 80, };
5591178019Sjkim	int id;
5592178019Sjkim	int i;
559341016Sdfr
5594178019Sjkim	/* the special sequence to enable the third button and the roller. */
5595298307Spfg	for (i = 0; i < nitems(rate); ++i)
5596178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5597178019Sjkim			return (FALSE);
5598178019Sjkim	/* the device will give the genuine ID only after the above sequence */
5599178019Sjkim	id = get_aux_id(kbdc);
5600178019Sjkim	if (id != PSM_INTELLI_ID)
5601178019Sjkim		return (FALSE);
560241016Sdfr
5603284320Sglebius	if (arg == PROBE) {
5604233580Sjkim		sc->hw.buttons = 3;
5605233580Sjkim		sc->hw.hwid = id;
5606233580Sjkim	}
560741016Sdfr
5608178019Sjkim	return (TRUE);
560941016Sdfr}
561041016Sdfr
5611284320Sglebius/*
5612284320Sglebius * A4 Tech 4D Mouse
5613284320Sglebius * Newer wheel mice from A4 Tech may use the 4D+ protocol.
5614284320Sglebius */
561558230Syokotastatic int
5616284320Sglebiusenable_4dmouse(struct psm_softc *sc, enum probearg arg)
561758230Syokota{
5618178019Sjkim	static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5619284320Sglebius	KBDC kbdc = sc->kbdc;
5620178019Sjkim	int id;
5621178019Sjkim	int i;
562258230Syokota
5623298307Spfg	for (i = 0; i < nitems(rate); ++i)
5624178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5625178019Sjkim			return (FALSE);
5626178019Sjkim	id = get_aux_id(kbdc);
5627178019Sjkim	/*
5628178019Sjkim	 * WinEasy 4D, 4 Way Scroll 4D: 6
5629178019Sjkim	 * Cable-Free 4D: 8 (4DPLUS)
5630178019Sjkim	 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
5631178019Sjkim	 */
5632178019Sjkim	if (id != PSM_4DMOUSE_ID)
5633178019Sjkim		return (FALSE);
563458230Syokota
5635284320Sglebius	if (arg == PROBE) {
5636233580Sjkim		sc->hw.buttons = 3;	/* XXX some 4D mice have 4? */
5637233580Sjkim		sc->hw.hwid = id;
5638233580Sjkim	}
563958230Syokota
5640178019Sjkim	return (TRUE);
564158230Syokota}
564258230Syokota
5643284320Sglebius/*
5644284320Sglebius * A4 Tech 4D+ Mouse
5645284320Sglebius * Newer wheel mice from A4 Tech seem to use this protocol.
5646284320Sglebius * Older models are recognized as either 4D Mouse or IntelliMouse.
5647284320Sglebius */
564858230Syokotastatic int
5649284320Sglebiusenable_4dplus(struct psm_softc *sc, enum probearg arg)
565058230Syokota{
5651284320Sglebius	KBDC kbdc = sc->kbdc;
5652178019Sjkim	int id;
565358230Syokota
5654178019Sjkim	/*
5655178019Sjkim	 * enable_4dmouse() already issued the following ID sequence...
5656178019Sjkim	static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5657178019Sjkim	int i;
565858230Syokota
5659178019Sjkim	for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
5660178019Sjkim		if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5661178019Sjkim			return (FALSE);
5662178019Sjkim	*/
566358230Syokota
5664178019Sjkim	id = get_aux_id(kbdc);
5665178019Sjkim	switch (id) {
5666178019Sjkim	case PSM_4DPLUS_ID:
5667178019Sjkim		break;
5668178019Sjkim	case PSM_4DPLUS_RFSW35_ID:
5669178019Sjkim		break;
5670178019Sjkim	default:
5671178019Sjkim		return (FALSE);
5672178019Sjkim	}
567358230Syokota
5674284320Sglebius	if (arg == PROBE) {
5675233580Sjkim		sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
5676233580Sjkim		sc->hw.hwid = id;
5677233580Sjkim	}
567858230Syokota
5679178019Sjkim	return (TRUE);
568058230Syokota}
568158230Syokota
5682132865Snjl/* Synaptics Touchpad */
5683132865Snjlstatic int
5684183888Sdumbbellsynaptics_sysctl(SYSCTL_HANDLER_ARGS)
5685132865Snjl{
5686307576Sgonzo	struct psm_softc *sc;
5687183888Sdumbbell	int error, arg;
5688132865Snjl
5689307576Sgonzo	if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 ||
5690349541Swulf	    oidp->oid_arg2 > SYNAPTICS_SYSCTL_LAST)
5691307576Sgonzo		return (EINVAL);
5692307576Sgonzo
5693307576Sgonzo	sc = oidp->oid_arg1;
5694307576Sgonzo
5695183888Sdumbbell	/* Read the current value. */
5696307576Sgonzo	arg = *(int *)((char *)sc + oidp->oid_arg2);
5697183888Sdumbbell	error = sysctl_handle_int(oidp, &arg, 0, req);
5698135945Sphilip
5699183888Sdumbbell	/* Sanity check. */
5700183888Sdumbbell	if (error || !req->newptr)
5701183888Sdumbbell		return (error);
5702183888Sdumbbell
5703183888Sdumbbell	/*
5704183888Sdumbbell	 * Check that the new value is in the concerned node's range
5705183888Sdumbbell	 * of values.
5706183888Sdumbbell	 */
5707183888Sdumbbell	switch (oidp->oid_arg2) {
5708183888Sdumbbell	case SYNAPTICS_SYSCTL_MIN_PRESSURE:
5709183888Sdumbbell	case SYNAPTICS_SYSCTL_MAX_PRESSURE:
5710183888Sdumbbell		if (arg < 0 || arg > 255)
5711183888Sdumbbell			return (EINVAL);
5712183888Sdumbbell		break;
5713183888Sdumbbell	case SYNAPTICS_SYSCTL_MAX_WIDTH:
5714183888Sdumbbell		if (arg < 4 || arg > 15)
5715183888Sdumbbell			return (EINVAL);
5716183888Sdumbbell		break;
5717183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_TOP:
5718307576Sgonzo	case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
5719307576Sgonzo	case SYNAPTICS_SYSCTL_NA_TOP:
5720307576Sgonzo	case SYNAPTICS_SYSCTL_NA_BOTTOM:
5721307576Sgonzo		if (arg < 0 || arg > sc->synhw.maximumYCoord)
5722307576Sgonzo			return (EINVAL);
5723307576Sgonzo		break;
5724307576Sgonzo	case SYNAPTICS_SYSCTL_SOFTBUTTON2_X:
5725307576Sgonzo	case SYNAPTICS_SYSCTL_SOFTBUTTON3_X:
5726307576Sgonzo		/* Softbuttons is clickpad only feature */
5727307576Sgonzo		if (!sc->synhw.capClickPad && arg != 0)
5728307576Sgonzo			return (EINVAL);
5729307576Sgonzo		/* FALLTHROUGH */
5730183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
5731183888Sdumbbell	case SYNAPTICS_SYSCTL_MARGIN_LEFT:
5732183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_RIGHT:
5733183888Sdumbbell	case SYNAPTICS_SYSCTL_NA_LEFT:
5734307576Sgonzo		if (arg < 0 || arg > sc->synhw.maximumXCoord)
5735183888Sdumbbell			return (EINVAL);
5736183888Sdumbbell		break;
5737183888Sdumbbell	case SYNAPTICS_SYSCTL_WINDOW_MIN:
5738183888Sdumbbell	case SYNAPTICS_SYSCTL_WINDOW_MAX:
5739183888Sdumbbell	case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
5740183888Sdumbbell		if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
5741183888Sdumbbell			return (EINVAL);
5742183888Sdumbbell		break;
5743183888Sdumbbell	case SYNAPTICS_SYSCTL_MULTIPLICATOR:
5744183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
5745183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
5746183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
5747183888Sdumbbell	case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
5748183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_MIN:
5749183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_MAX:
5750183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_MAX_NA:
5751183888Sdumbbell	case SYNAPTICS_SYSCTL_DIV_LEN:
5752183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
5753183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
5754183888Sdumbbell		if (arg < 1)
5755183888Sdumbbell			return (EINVAL);
5756183888Sdumbbell		break;
5757183888Sdumbbell	case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
5758183888Sdumbbell	case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
5759183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
5760183888Sdumbbell		if (arg < 0)
5761183888Sdumbbell			return (EINVAL);
5762183888Sdumbbell		break;
5763183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
5764307576Sgonzo		if (arg < -sc->synhw.maximumXCoord ||
5765307576Sgonzo		    arg > sc->synhw.maximumXCoord)
5766307576Sgonzo			return (EINVAL);
5767307576Sgonzo		break;
5768307576Sgonzo	case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y:
5769307576Sgonzo		/* Softbuttons is clickpad only feature */
5770307576Sgonzo		if (!sc->synhw.capClickPad && arg != 0)
5771307576Sgonzo			return (EINVAL);
5772307576Sgonzo		/* FALLTHROUGH */
5773183888Sdumbbell	case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
5774307576Sgonzo		if (arg < -sc->synhw.maximumYCoord ||
5775307576Sgonzo		    arg > sc->synhw.maximumYCoord)
5776183888Sdumbbell			return (EINVAL);
5777183888Sdumbbell		break;
5778281441Srpaulo        case SYNAPTICS_SYSCTL_TOUCHPAD_OFF:
5779349541Swulf	case SYNAPTICS_SYSCTL_NATURAL_SCROLL:
5780281441Srpaulo		if (arg < 0 || arg > 1)
5781281441Srpaulo			return (EINVAL);
5782281441Srpaulo		break;
5783183888Sdumbbell	default:
5784183888Sdumbbell		return (EINVAL);
5785183888Sdumbbell	}
5786183888Sdumbbell
5787183888Sdumbbell	/* Update. */
5788307576Sgonzo	*(int *)((char *)sc + oidp->oid_arg2) = arg;
5789183888Sdumbbell
5790183888Sdumbbell	return (error);
5791183888Sdumbbell}
5792183888Sdumbbell
5793183888Sdumbbellstatic void
5794307576Sgonzosynaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc)
5795183888Sdumbbell{
5796307576Sgonzo	/*
5797307576Sgonzo	 * Set predefined sizes for softbuttons.
5798307576Sgonzo	 * Values are taken to match HP Pavilion dv6 clickpad drawings
5799307576Sgonzo	 * with thin middle softbutton placed on separator
5800307576Sgonzo	 */
5801183888Sdumbbell
5802307576Sgonzo	/* hw.psm.synaptics.softbuttons_y */
5803344165Swulf	sc->syninfo.softbuttons_y = sc->synhw.topButtonPad ? -1700 : 1700;
5804307576Sgonzo	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5805307576Sgonzo	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5806307576Sgonzo	    "softbuttons_y", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5807307576Sgonzo	    sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y,
5808307576Sgonzo	    synaptics_sysctl, "I",
5809307576Sgonzo	    "Vertical size of softbuttons area");
5810307576Sgonzo
5811307576Sgonzo	/* hw.psm.synaptics.softbutton2_x */
5812307576Sgonzo	sc->syninfo.softbutton2_x = 3100;
5813307576Sgonzo	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5814307576Sgonzo	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5815307576Sgonzo	    "softbutton2_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5816307576Sgonzo	    sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X,
5817307576Sgonzo	    synaptics_sysctl, "I",
5818307576Sgonzo	    "Horisontal position of 2-nd softbutton left edge (0-disable)");
5819307576Sgonzo
5820307576Sgonzo	/* hw.psm.synaptics.softbutton3_x */
5821307576Sgonzo	sc->syninfo.softbutton3_x = 3900;
5822307576Sgonzo	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5823307576Sgonzo	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5824307576Sgonzo	    "softbutton3_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5825307576Sgonzo	    sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X,
5826307576Sgonzo	    synaptics_sysctl, "I",
5827307576Sgonzo	    "Horisontal position of 3-rd softbutton left edge (0-disable)");
5828307576Sgonzo}
5829307576Sgonzo
5830307576Sgonzostatic void
5831307576Sgonzosynaptics_sysctl_create_tree(struct psm_softc *sc, const char *name,
5832307576Sgonzo    const char *descr)
5833307576Sgonzo{
5834307576Sgonzo
5835186218Sdumbbell	if (sc->syninfo.sysctl_tree != NULL)
5836186218Sdumbbell		return;
5837186218Sdumbbell
5838178019Sjkim	/* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
5839178019Sjkim	sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
5840178019Sjkim	sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
5841307576Sgonzo	    SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name, CTLFLAG_RD,
5842307576Sgonzo	    0, descr);
5843139982Sphilip
5844183888Sdumbbell	/* hw.psm.synaptics.directional_scrolls. */
5845281440Srpaulo	sc->syninfo.directional_scrolls = 0;
5846178019Sjkim	SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5847178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5848183888Sdumbbell	    "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
5849178019Sjkim	    &sc->syninfo.directional_scrolls, 0,
5850183888Sdumbbell	    "Enable hardware scrolling pad (if non-zero) or register it as "
5851281440Srpaulo	    "extended buttons (if 0)");
5852139982Sphilip
5853307576Sgonzo	/* hw.psm.synaptics.max_x. */
5854307576Sgonzo	sc->syninfo.max_x = 6143;
5855307576Sgonzo	SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5856307576Sgonzo	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5857307576Sgonzo	    "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY,
5858307576Sgonzo	    &sc->syninfo.max_x, 0,
5859307576Sgonzo	    "Horizontal reporting range");
5860307576Sgonzo
5861307576Sgonzo	/* hw.psm.synaptics.max_y. */
5862307576Sgonzo	sc->syninfo.max_y = 6143;
5863307576Sgonzo	SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5864307576Sgonzo	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5865307576Sgonzo	    "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY,
5866307576Sgonzo	    &sc->syninfo.max_y, 0,
5867307576Sgonzo	    "Vertical reporting range");
5868307576Sgonzo
5869281440Srpaulo	/*
5870281440Srpaulo	 * Turn off two finger scroll if we have a
5871281440Srpaulo	 * physical area reserved for scrolling or when
5872281440Srpaulo	 * there's no multi finger support.
5873281440Srpaulo	 */
5874282734Srpaulo	if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 &&
5875282734Srpaulo					 sc->synhw.capAdvancedGestures == 0))
5876281440Srpaulo		sc->syninfo.two_finger_scroll = 0;
5877281440Srpaulo	else
5878281440Srpaulo		sc->syninfo.two_finger_scroll = 1;
5879281440Srpaulo	/* hw.psm.synaptics.two_finger_scroll. */
5880281440Srpaulo	SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5881281440Srpaulo	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5882281440Srpaulo	    "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY,
5883281440Srpaulo	    &sc->syninfo.two_finger_scroll, 0,
5884281440Srpaulo	    "Enable two finger scrolling");
5885281440Srpaulo
5886183888Sdumbbell	/* hw.psm.synaptics.min_pressure. */
5887318500Swulf	sc->syninfo.min_pressure = 32;
5888183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5889178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5890183888Sdumbbell	    "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5891307576Sgonzo	    sc, SYNAPTICS_SYSCTL_MIN_PRESSURE,
5892183888Sdumbbell	    synaptics_sysctl, "I",
5893183888Sdumbbell	    "Minimum pressure required to start an action");
5894139982Sphilip
5895183888Sdumbbell	/* hw.psm.synaptics.max_pressure. */
5896183888Sdumbbell	sc->syninfo.max_pressure = 220;
5897183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5898178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5899183888Sdumbbell	    "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5900307576Sgonzo	    sc, SYNAPTICS_SYSCTL_MAX_PRESSURE,
5901183888Sdumbbell	    synaptics_sysctl, "I",
5902183888Sdumbbell	    "Maximum pressure to detect palm");
5903139982Sphilip
5904183888Sdumbbell	/* hw.psm.synaptics.max_width. */
5905183888Sdumbbell	sc->syninfo.max_width = 10;
5906183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5907178019Sjkim	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5908183888Sdumbbell	    "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5909307576Sgonzo	    sc, SYNAPTICS_SYSCTL_MAX_WIDTH,
5910183888Sdumbbell	    synaptics_sysctl, "I",
5911183888Sdumbbell	    "Maximum finger width to detect palm");
5912132865Snjl
5913183888Sdumbbell	/* hw.psm.synaptics.top_margin. */
5914183888Sdumbbell	sc->syninfo.margin_top = 200;
5915183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5916183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5917183888Sdumbbell	    "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5918307576Sgonzo	    sc, SYNAPTICS_SYSCTL_MARGIN_TOP,
5919183888Sdumbbell	    synaptics_sysctl, "I",
5920183888Sdumbbell	    "Top margin");
5921183888Sdumbbell
5922183888Sdumbbell	/* hw.psm.synaptics.right_margin. */
5923183888Sdumbbell	sc->syninfo.margin_right = 200;
5924183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5925183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5926183888Sdumbbell	    "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5927307576Sgonzo	    sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
5928183888Sdumbbell	    synaptics_sysctl, "I",
5929183888Sdumbbell	    "Right margin");
5930183888Sdumbbell
5931183888Sdumbbell	/* hw.psm.synaptics.bottom_margin. */
5932183888Sdumbbell	sc->syninfo.margin_bottom = 200;
5933183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5934183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5935183888Sdumbbell	    "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5936307576Sgonzo	    sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
5937183888Sdumbbell	    synaptics_sysctl, "I",
5938183888Sdumbbell	    "Bottom margin");
5939183888Sdumbbell
5940183888Sdumbbell	/* hw.psm.synaptics.left_margin. */
5941183888Sdumbbell	sc->syninfo.margin_left = 200;
5942183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5943183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5944183888Sdumbbell	    "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5945307576Sgonzo	    sc, SYNAPTICS_SYSCTL_MARGIN_LEFT,
5946183888Sdumbbell	    synaptics_sysctl, "I",
5947183888Sdumbbell	    "Left margin");
5948183888Sdumbbell
5949183888Sdumbbell	/* hw.psm.synaptics.na_top. */
5950183888Sdumbbell	sc->syninfo.na_top = 1783;
5951183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5952183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5953183888Sdumbbell	    "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5954307576Sgonzo	    sc, SYNAPTICS_SYSCTL_NA_TOP,
5955183888Sdumbbell	    synaptics_sysctl, "I",
5956183888Sdumbbell	    "Top noisy area, where weight_previous_na is used instead "
5957183888Sdumbbell	    "of weight_previous");
5958183888Sdumbbell
5959183888Sdumbbell	/* hw.psm.synaptics.na_right. */
5960183888Sdumbbell	sc->syninfo.na_right = 563;
5961183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5962183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5963183888Sdumbbell	    "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5964307576Sgonzo	    sc, SYNAPTICS_SYSCTL_NA_RIGHT,
5965183888Sdumbbell	    synaptics_sysctl, "I",
5966183888Sdumbbell	    "Right noisy area, where weight_previous_na is used instead "
5967183888Sdumbbell	    "of weight_previous");
5968183888Sdumbbell
5969183888Sdumbbell	/* hw.psm.synaptics.na_bottom. */
5970183888Sdumbbell	sc->syninfo.na_bottom = 1408;
5971183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5972183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5973183888Sdumbbell	    "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5974307576Sgonzo	    sc, SYNAPTICS_SYSCTL_NA_BOTTOM,
5975183888Sdumbbell	    synaptics_sysctl, "I",
5976183888Sdumbbell	    "Bottom noisy area, where weight_previous_na is used instead "
5977183888Sdumbbell	    "of weight_previous");
5978183888Sdumbbell
5979183888Sdumbbell	/* hw.psm.synaptics.na_left. */
5980183888Sdumbbell	sc->syninfo.na_left = 1600;
5981183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5982183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5983183888Sdumbbell	    "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5984307576Sgonzo	    sc, SYNAPTICS_SYSCTL_NA_LEFT,
5985183888Sdumbbell	    synaptics_sysctl, "I",
5986183888Sdumbbell	    "Left noisy area, where weight_previous_na is used instead "
5987183888Sdumbbell	    "of weight_previous");
5988183888Sdumbbell
5989183888Sdumbbell	/* hw.psm.synaptics.window_min. */
5990183888Sdumbbell	sc->syninfo.window_min = 4;
5991183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5992183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5993183888Sdumbbell	    "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5994307576Sgonzo	    sc, SYNAPTICS_SYSCTL_WINDOW_MIN,
5995183888Sdumbbell	    synaptics_sysctl, "I",
5996183888Sdumbbell	    "Minimum window size to start an action");
5997183888Sdumbbell
5998183888Sdumbbell	/* hw.psm.synaptics.window_max. */
5999183888Sdumbbell	sc->syninfo.window_max = 10;
6000183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6001183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6002183888Sdumbbell	    "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6003307576Sgonzo	    sc, SYNAPTICS_SYSCTL_WINDOW_MAX,
6004183888Sdumbbell	    synaptics_sysctl, "I",
6005183888Sdumbbell	    "Maximum window size");
6006183888Sdumbbell
6007183888Sdumbbell	/* hw.psm.synaptics.multiplicator. */
6008183888Sdumbbell	sc->syninfo.multiplicator = 10000;
6009183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6010183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6011183888Sdumbbell	    "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6012307576Sgonzo	    sc, SYNAPTICS_SYSCTL_MULTIPLICATOR,
6013183888Sdumbbell	    synaptics_sysctl, "I",
6014183888Sdumbbell	    "Multiplicator to increase precision in averages and divisions");
6015183888Sdumbbell
6016183888Sdumbbell	/* hw.psm.synaptics.weight_current. */
6017183888Sdumbbell	sc->syninfo.weight_current = 3;
6018183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6019183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6020183888Sdumbbell	    "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6021307576Sgonzo	    sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
6022183888Sdumbbell	    synaptics_sysctl, "I",
6023183888Sdumbbell	    "Weight of the current movement in the new average");
6024183888Sdumbbell
6025183888Sdumbbell	/* hw.psm.synaptics.weight_previous. */
6026183888Sdumbbell	sc->syninfo.weight_previous = 6;
6027183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6028183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6029183888Sdumbbell	    "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6030307576Sgonzo	    sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
6031183888Sdumbbell	    synaptics_sysctl, "I",
6032183888Sdumbbell	    "Weight of the previous average");
6033183888Sdumbbell
6034183888Sdumbbell	/* hw.psm.synaptics.weight_previous_na. */
6035183888Sdumbbell	sc->syninfo.weight_previous_na = 20;
6036183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6037183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6038183888Sdumbbell	    "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6039307576Sgonzo	    sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
6040183888Sdumbbell	    synaptics_sysctl, "I",
6041183888Sdumbbell	    "Weight of the previous average (inside the noisy area)");
6042183888Sdumbbell
6043183888Sdumbbell	/* hw.psm.synaptics.weight_len_squared. */
6044183888Sdumbbell	sc->syninfo.weight_len_squared = 2000;
6045183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6046183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6047183888Sdumbbell	    "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6048307576Sgonzo	    sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
6049183888Sdumbbell	    synaptics_sysctl, "I",
6050183888Sdumbbell	    "Length (squared) of segments where weight_previous "
6051183888Sdumbbell	    "starts to decrease");
6052183888Sdumbbell
6053183888Sdumbbell	/* hw.psm.synaptics.div_min. */
6054183888Sdumbbell	sc->syninfo.div_min = 9;
6055183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6056183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6057183888Sdumbbell	    "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6058307576Sgonzo	    sc, SYNAPTICS_SYSCTL_DIV_MIN,
6059183888Sdumbbell	    synaptics_sysctl, "I",
6060183888Sdumbbell	    "Divisor for fast movements");
6061183888Sdumbbell
6062183888Sdumbbell	/* hw.psm.synaptics.div_max. */
6063183888Sdumbbell	sc->syninfo.div_max = 17;
6064183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6065183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6066183888Sdumbbell	    "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6067307576Sgonzo	    sc, SYNAPTICS_SYSCTL_DIV_MAX,
6068183888Sdumbbell	    synaptics_sysctl, "I",
6069183888Sdumbbell	    "Divisor for slow movements");
6070183888Sdumbbell
6071183888Sdumbbell	/* hw.psm.synaptics.div_max_na. */
6072183888Sdumbbell	sc->syninfo.div_max_na = 30;
6073183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6074183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6075183888Sdumbbell	    "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6076307576Sgonzo	    sc, SYNAPTICS_SYSCTL_DIV_MAX_NA,
6077183888Sdumbbell	    synaptics_sysctl, "I",
6078183888Sdumbbell	    "Divisor with slow movements (inside the noisy area)");
6079183888Sdumbbell
6080183888Sdumbbell	/* hw.psm.synaptics.div_len. */
6081183888Sdumbbell	sc->syninfo.div_len = 100;
6082183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6083183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6084183888Sdumbbell	    "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6085307576Sgonzo	    sc, SYNAPTICS_SYSCTL_DIV_LEN,
6086183888Sdumbbell	    synaptics_sysctl, "I",
6087183888Sdumbbell	    "Length of segments where div_max starts to decrease");
6088183888Sdumbbell
6089183888Sdumbbell	/* hw.psm.synaptics.tap_max_delta. */
6090183888Sdumbbell	sc->syninfo.tap_max_delta = 80;
6091183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6092183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6093183888Sdumbbell	    "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6094307576Sgonzo	    sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
6095183888Sdumbbell	    synaptics_sysctl, "I",
6096183888Sdumbbell	    "Length of segments above which a tap is ignored");
6097183888Sdumbbell
6098183888Sdumbbell	/* hw.psm.synaptics.tap_min_queue. */
6099183888Sdumbbell	sc->syninfo.tap_min_queue = 2;
6100183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6101183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6102183888Sdumbbell	    "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6103307576Sgonzo	    sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
6104183888Sdumbbell	    synaptics_sysctl, "I",
6105183888Sdumbbell	    "Number of packets required to consider a tap");
6106183888Sdumbbell
6107183888Sdumbbell	/* hw.psm.synaptics.taphold_timeout. */
6108307576Sgonzo	sc->gesture.in_taphold = 0;
6109183888Sdumbbell	sc->syninfo.taphold_timeout = tap_timeout;
6110183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6111183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6112183888Sdumbbell	    "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6113307576Sgonzo	    sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
6114183888Sdumbbell	    synaptics_sysctl, "I",
6115183888Sdumbbell	    "Maximum elapsed time between two taps to consider a tap-hold "
6116183888Sdumbbell	    "action");
6117183888Sdumbbell
6118183888Sdumbbell	/* hw.psm.synaptics.vscroll_hor_area. */
6119183888Sdumbbell	sc->syninfo.vscroll_hor_area = 0; /* 1300 */
6120183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6121183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6122183888Sdumbbell	    "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6123307576Sgonzo	    sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
6124183888Sdumbbell	    synaptics_sysctl, "I",
6125183888Sdumbbell	    "Area reserved for horizontal virtual scrolling");
6126183888Sdumbbell
6127183888Sdumbbell	/* hw.psm.synaptics.vscroll_ver_area. */
6128307576Sgonzo	sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right;
6129183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6130183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6131183888Sdumbbell	    "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6132307576Sgonzo	    sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
6133183888Sdumbbell	    synaptics_sysctl, "I",
6134183888Sdumbbell	    "Area reserved for vertical virtual scrolling");
6135183888Sdumbbell
6136183888Sdumbbell	/* hw.psm.synaptics.vscroll_min_delta. */
6137183888Sdumbbell	sc->syninfo.vscroll_min_delta = 50;
6138183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6139183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6140183888Sdumbbell	    "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6141307576Sgonzo	    sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
6142183888Sdumbbell	    synaptics_sysctl, "I",
6143183888Sdumbbell	    "Minimum movement to consider virtual scrolling");
6144183888Sdumbbell
6145183888Sdumbbell	/* hw.psm.synaptics.vscroll_div_min. */
6146183888Sdumbbell	sc->syninfo.vscroll_div_min = 100;
6147183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6148183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6149183888Sdumbbell	    "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6150307576Sgonzo	    sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
6151183888Sdumbbell	    synaptics_sysctl, "I",
6152183888Sdumbbell	    "Divisor for fast scrolling");
6153183888Sdumbbell
6154183888Sdumbbell	/* hw.psm.synaptics.vscroll_div_min. */
6155183888Sdumbbell	sc->syninfo.vscroll_div_max = 150;
6156183888Sdumbbell	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6157183888Sdumbbell	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6158183888Sdumbbell	    "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6159307576Sgonzo	    sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
6160183888Sdumbbell	    synaptics_sysctl, "I",
6161183888Sdumbbell	    "Divisor for slow scrolling");
6162281441Srpaulo
6163281441Srpaulo	/* hw.psm.synaptics.touchpad_off. */
6164281441Srpaulo	sc->syninfo.touchpad_off = 0;
6165281441Srpaulo	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6166281441Srpaulo	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6167281441Srpaulo	    "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6168307576Sgonzo	    sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF,
6169281441Srpaulo	    synaptics_sysctl, "I",
6170281441Srpaulo	    "Turn off touchpad");
6171307576Sgonzo
6172349541Swulf	/* hw.psm.synaptics.natural_scroll. */
6173349541Swulf	sc->syninfo.natural_scroll = 0;
6174349541Swulf	SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6175349541Swulf	    SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6176349541Swulf	    "natural_scroll", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6177349541Swulf	    sc, SYNAPTICS_SYSCTL_NATURAL_SCROLL,
6178349541Swulf	    synaptics_sysctl, "I",
6179349541Swulf	    "Enable natural scrolling");
6180349541Swulf
6181307576Sgonzo	sc->syninfo.softbuttons_y = 0;
6182307576Sgonzo	sc->syninfo.softbutton2_x = 0;
6183307576Sgonzo	sc->syninfo.softbutton3_x = 0;
6184307576Sgonzo
6185307576Sgonzo	/* skip softbuttons sysctl on not clickpads */
6186307576Sgonzo	if (sc->synhw.capClickPad)
6187307576Sgonzo		synaptics_sysctl_create_softbuttons_tree(sc);
6188183888Sdumbbell}
6189183888Sdumbbell
6190183888Sdumbbellstatic int
6191282734Srpaulosynaptics_preferred_mode(struct psm_softc *sc) {
6192282734Srpaulo	int mode_byte;
6193282734Srpaulo
6194308652Sgonzo	/* Check if we are in relative mode */
6195308652Sgonzo	if (sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
6196308652Sgonzo		if (tap_enabled == 0)
6197308652Sgonzo			/*
6198308652Sgonzo			 * Disable tap & drag gestures. We use a Mode Byte
6199308652Sgonzo			 * and set the DisGest bit (see ��2.5 of Synaptics
6200308652Sgonzo			 * TouchPad Interfacing Guide).
6201308652Sgonzo			 */
6202308652Sgonzo			return (0x04);
6203308652Sgonzo		else
6204308652Sgonzo			/*
6205308652Sgonzo			 * Enable tap & drag gestures. We use a Mode Byte
6206308652Sgonzo			 * and clear the DisGest bit (see ��2.5 of Synaptics
6207308652Sgonzo			 * TouchPad Interfacing Guide).
6208308652Sgonzo			 */
6209308652Sgonzo			return (0x00);
6210308652Sgonzo	}
6211308652Sgonzo
6212307576Sgonzo	mode_byte = 0xc4;
6213282734Srpaulo
6214282734Srpaulo	/* request wmode where available */
6215282734Srpaulo	if (sc->synhw.capExtended)
6216282734Srpaulo		mode_byte |= 1;
6217282734Srpaulo
6218282734Srpaulo	return mode_byte;
6219282734Srpaulo}
6220282734Srpaulo
6221282734Srpaulostatic void
6222282734Srpaulosynaptics_set_mode(struct psm_softc *sc, int mode_byte) {
6223282734Srpaulo	mouse_ext_command(sc->kbdc, mode_byte);
6224282734Srpaulo
6225282734Srpaulo	/* "Commit" the Set Mode Byte command sent above. */
6226282734Srpaulo	set_mouse_sampling_rate(sc->kbdc, 20);
6227282734Srpaulo
6228282734Srpaulo	/*
6229282734Srpaulo	 * Enable advanced gestures mode if supported and we are not entering
6230308652Sgonzo	 * passthrough or relative mode.
6231282734Srpaulo	 */
6232307576Sgonzo	if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) &&
6233308652Sgonzo	    sc->hw.model == MOUSE_MODEL_SYNAPTICS && !(mode_byte & (1 << 5))) {
6234349541Swulf		mouse_ext_command(sc->kbdc, SYNAPTICS_READ_MODEL_ID);
6235282734Srpaulo		set_mouse_sampling_rate(sc->kbdc, 0xc8);
6236282734Srpaulo	}
6237282734Srpaulo}
6238282734Srpaulo
6239343158Swulf/*
6240343158Swulf * AUX MUX detection code should be placed at very beginning of probe sequence
6241343158Swulf * at least before 4-byte protocol mouse probes e.g. MS IntelliMouse probe as
6242343158Swulf * latter can trigger switching the MUX to incompatible state.
6243343158Swulf */
6244282734Srpaulostatic int
6245343158Swulfenable_synaptics_mux(struct psm_softc *sc, enum probearg arg)
6246343158Swulf{
6247343158Swulf	KBDC kbdc = sc->kbdc;
6248343158Swulf	int port, version;
6249343158Swulf	int probe = FALSE;
6250343158Swulf	int active_ports_count = 0;
6251343158Swulf	int active_ports_mask = 0;
6252343158Swulf
6253362209Swulf	if (mux_disabled != 0)
6254362209Swulf		return (FALSE);
6255362209Swulf
6256343158Swulf	version = enable_aux_mux(kbdc);
6257343158Swulf	if (version == -1)
6258343158Swulf		return (FALSE);
6259343158Swulf
6260343158Swulf	for (port = 0; port < KBDC_AUX_MUX_NUM_PORTS; port++) {
6261343158Swulf		VLOG(3, (LOG_DEBUG, "aux_mux: ping port %d\n", port));
6262343158Swulf		set_active_aux_mux_port(kbdc, port);
6263343158Swulf		if (enable_aux_dev(kbdc) && disable_aux_dev(kbdc)) {
6264343158Swulf			active_ports_count++;
6265343158Swulf			active_ports_mask |= 1 << port;
6266343158Swulf		}
6267343158Swulf	}
6268343158Swulf
6269343158Swulf	if (verbose >= 2)
6270343158Swulf		printf("Active Multiplexing PS/2 controller v%d.%d with %d "
6271343158Swulf		    "active port(s)\n", version >> 4 & 0x0f, version & 0x0f,
6272343158Swulf		    active_ports_count);
6273343158Swulf
6274343158Swulf	/* psm has a special support for GenMouse + SynTouchpad combination */
6275343158Swulf	if (active_ports_count >= 2) {
6276343158Swulf		for (port = 0; port < KBDC_AUX_MUX_NUM_PORTS; port++) {
6277343158Swulf			if ((active_ports_mask & 1 << port) == 0)
6278343158Swulf				continue;
6279343158Swulf			VLOG(3, (LOG_DEBUG, "aux_mux: probe port %d\n", port));
6280343158Swulf			set_active_aux_mux_port(kbdc, port);
6281343158Swulf			probe = enable_synaptics(sc, arg);
6282343158Swulf			if (probe) {
6283343158Swulf				if (arg == PROBE)
6284343158Swulf					sc->muxport = port;
6285343158Swulf				break;
6286343158Swulf			}
6287343158Swulf		}
6288343158Swulf	}
6289343158Swulf
6290343158Swulf	/* IRQ handler does not support active multiplexing mode */
6291343158Swulf	disable_aux_mux(kbdc);
6292343158Swulf
6293362209Swulf	/* Is MUX still alive after switching back to legacy mode? */
6294362209Swulf	if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
6295362209Swulf		/*
6296362209Swulf		 * On some laptops e.g. Lenovo X121e dead AUX MUX can be
6297362209Swulf		 * brought back to life with resetting of keyboard.
6298362209Swulf		 */
6299362209Swulf		reset_kbd(kbdc);
6300362209Swulf		if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
6301362209Swulf			printf("psm%d: AUX MUX hang detected!\n", sc->unit);
6302362209Swulf			printf("Consider adding hw.psm.mux_disabled=1 to "
6303362209Swulf			    "loader tunables\n");
6304362209Swulf		}
6305362209Swulf	}
6306362209Swulf	empty_both_buffers(kbdc, 10);	/* remove stray data if any */
6307362209Swulf
6308343158Swulf	return (probe);
6309343158Swulf}
6310343158Swulf
6311343158Swulfstatic int
6312284320Sglebiusenable_synaptics(struct psm_softc *sc, enum probearg arg)
6313183888Sdumbbell{
6314331170Seadler	device_t psmcpnp;
6315331170Seadler	struct psmcpnp_softc *psmcpnp_sc;
6316284320Sglebius	KBDC kbdc = sc->kbdc;
6317233580Sjkim	synapticshw_t synhw;
6318183888Sdumbbell	int status[3];
6319343158Swulf	int buttons;
6320183888Sdumbbell
6321183888Sdumbbell	VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
6322132865Snjl
6323186175Sdumbbell	/*
6324186175Sdumbbell	 * Just to be on the safe side: this avoids troubles with
6325186175Sdumbbell	 * following mouse_ext_command() when the previous command
6326186175Sdumbbell	 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
6327186175Sdumbbell	 * Synaptics Touchpad behaviour.
6328186175Sdumbbell	 */
6329178019Sjkim	set_mouse_scaling(kbdc, 1);
6330133295Sphilip
6331186175Sdumbbell	/* Identify the Touchpad version. */
6332349541Swulf	if (mouse_ext_command(kbdc, SYNAPTICS_READ_IDENTITY) == 0)
6333178019Sjkim		return (FALSE);
6334178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
6335178019Sjkim		return (FALSE);
6336343158Swulf	if (status[1] != 0x47)
6337178019Sjkim		return (FALSE);
6338133295Sphilip
6339233580Sjkim	bzero(&synhw, sizeof(synhw));
6340233580Sjkim	synhw.infoMinor = status[0];
6341233580Sjkim	synhw.infoMajor = status[2] & 0x0f;
6342132865Snjl
6343178019Sjkim	if (verbose >= 2)
6344233580Sjkim		printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
6345233580Sjkim		    synhw.infoMinor);
6346132865Snjl
6347343158Swulf	if (synhw.infoMajor < 4) {
6348178019Sjkim		printf("  Unsupported (pre-v4) Touchpad detected\n");
6349178019Sjkim		return (FALSE);
6350178019Sjkim	}
6351133295Sphilip
6352186175Sdumbbell	/* Get the Touchpad model information. */
6353349541Swulf	if (mouse_ext_command(kbdc, SYNAPTICS_READ_MODEL_ID) == 0)
6354178019Sjkim		return (FALSE);
6355178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
6356178019Sjkim		return (FALSE);
6357178019Sjkim	if ((status[1] & 0x01) != 0) {
6358178019Sjkim		printf("  Failed to read model information\n");
6359178019Sjkim		return (FALSE);
6360178019Sjkim	}
6361132865Snjl
6362233580Sjkim	synhw.infoRot180   = (status[0] & 0x80) != 0;
6363233580Sjkim	synhw.infoPortrait = (status[0] & 0x40) != 0;
6364233580Sjkim	synhw.infoSensor   =  status[0] & 0x3f;
6365233580Sjkim	synhw.infoHardware = (status[1] & 0xfe) >> 1;
6366233580Sjkim	synhw.infoNewAbs   = (status[2] & 0x80) != 0;
6367233580Sjkim	synhw.capPen       = (status[2] & 0x40) != 0;
6368233580Sjkim	synhw.infoSimplC   = (status[2] & 0x20) != 0;
6369233580Sjkim	synhw.infoGeometry =  status[2] & 0x0f;
6370132865Snjl
6371133295Sphilip	if (verbose >= 2) {
6372178019Sjkim		printf("  Model information:\n");
6373233580Sjkim		printf("   infoRot180: %d\n", synhw.infoRot180);
6374233580Sjkim		printf("   infoPortrait: %d\n", synhw.infoPortrait);
6375233580Sjkim		printf("   infoSensor: %d\n", synhw.infoSensor);
6376233580Sjkim		printf("   infoHardware: %d\n", synhw.infoHardware);
6377233580Sjkim		printf("   infoNewAbs: %d\n", synhw.infoNewAbs);
6378233580Sjkim		printf("   capPen: %d\n", synhw.capPen);
6379233580Sjkim		printf("   infoSimplC: %d\n", synhw.infoSimplC);
6380233580Sjkim		printf("   infoGeometry: %d\n", synhw.infoGeometry);
6381133295Sphilip	}
6382139982Sphilip
6383186175Sdumbbell	/* Read the extended capability bits. */
6384349541Swulf	if (mouse_ext_command(kbdc, SYNAPTICS_READ_CAPABILITIES) == 0)
6385178019Sjkim		return (FALSE);
6386178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
6387178019Sjkim		return (FALSE);
6388343158Swulf	if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
6389178019Sjkim		printf("  Failed to read extended capability bits\n");
6390178019Sjkim		return (FALSE);
6391178019Sjkim	}
6392178019Sjkim
6393331170Seadler	psmcpnp = devclass_get_device(devclass_find(PSMCPNP_DRIVER_NAME),
6394331170Seadler	    sc->unit);
6395331170Seadler	psmcpnp_sc = (psmcpnp != NULL) ? device_get_softc(psmcpnp) : NULL;
6396331170Seadler
6397186175Sdumbbell	/* Set the different capabilities when they exist. */
6398233580Sjkim	buttons = 0;
6399343158Swulf	synhw.capExtended = (status[0] & 0x80) != 0;
6400343158Swulf	if (synhw.capExtended) {
6401282734Srpaulo		synhw.nExtendedQueries = (status[0] & 0x70) >> 4;
6402255153Sdumbbell		synhw.capMiddle        = (status[0] & 0x04) != 0;
6403255153Sdumbbell		synhw.capPassthrough   = (status[2] & 0x80) != 0;
6404281127Srpaulo		synhw.capLowPower      = (status[2] & 0x40) != 0;
6405281127Srpaulo		synhw.capMultiFingerReport =
6406281127Srpaulo					 (status[2] & 0x20) != 0;
6407255153Sdumbbell		synhw.capSleep         = (status[2] & 0x10) != 0;
6408255153Sdumbbell		synhw.capFourButtons   = (status[2] & 0x08) != 0;
6409281127Srpaulo		synhw.capBallistics    = (status[2] & 0x04) != 0;
6410255153Sdumbbell		synhw.capMultiFinger   = (status[2] & 0x02) != 0;
6411255153Sdumbbell		synhw.capPalmDetect    = (status[2] & 0x01) != 0;
6412178019Sjkim
6413282734Srpaulo		if (!set_mouse_scaling(kbdc, 1))
6414282734Srpaulo			return (FALSE);
6415349541Swulf		if (mouse_ext_command(kbdc, SYNAPTICS_READ_RESOLUTIONS) == 0)
6416282734Srpaulo			return (FALSE);
6417282734Srpaulo		if (get_mouse_status(kbdc, status, 0, 3) != 3)
6418282734Srpaulo			return (FALSE);
6419282734Srpaulo
6420325345Swulf		if (status[0] != 0 && (status[1] & 0x80) && status[2] != 0) {
6421325345Swulf			synhw.infoXupmm = status[0];
6422325345Swulf			synhw.infoYupmm = status[2];
6423325345Swulf		}
6424282734Srpaulo
6425178019Sjkim		if (verbose >= 2) {
6426178019Sjkim			printf("  Extended capabilities:\n");
6427233580Sjkim			printf("   capExtended: %d\n", synhw.capExtended);
6428255153Sdumbbell			printf("   capMiddle: %d\n", synhw.capMiddle);
6429255153Sdumbbell			printf("   nExtendedQueries: %d\n",
6430255153Sdumbbell			    synhw.nExtendedQueries);
6431233580Sjkim			printf("   capPassthrough: %d\n", synhw.capPassthrough);
6432281127Srpaulo			printf("   capLowPower: %d\n", synhw.capLowPower);
6433281127Srpaulo			printf("   capMultiFingerReport: %d\n",
6434281127Srpaulo			    synhw.capMultiFingerReport);
6435233580Sjkim			printf("   capSleep: %d\n", synhw.capSleep);
6436233580Sjkim			printf("   capFourButtons: %d\n", synhw.capFourButtons);
6437281127Srpaulo			printf("   capBallistics: %d\n", synhw.capBallistics);
6438233580Sjkim			printf("   capMultiFinger: %d\n", synhw.capMultiFinger);
6439233580Sjkim			printf("   capPalmDetect: %d\n", synhw.capPalmDetect);
6440282734Srpaulo			printf("   infoXupmm: %d\n", synhw.infoXupmm);
6441282734Srpaulo			printf("   infoYupmm: %d\n", synhw.infoYupmm);
6442178019Sjkim		}
6443178019Sjkim
6444178019Sjkim		/*
6445255153Sdumbbell		 * If nExtendedQueries is 1 or greater, then the TouchPad
6446255153Sdumbbell		 * supports this number of extended queries. We can load
6447186175Sdumbbell		 * more information about buttons using query 0x09.
6448178019Sjkim		 */
6449282734Srpaulo		if (synhw.nExtendedQueries >= 1) {
6450282734Srpaulo			if (!set_mouse_scaling(kbdc, 1))
6451282734Srpaulo				return (FALSE);
6452349541Swulf			if (mouse_ext_command(kbdc,
6453349541Swulf			    SYNAPTICS_READ_EXTENDED) == 0)
6454178019Sjkim				return (FALSE);
6455178019Sjkim			if (get_mouse_status(kbdc, status, 0, 3) != 3)
6456178019Sjkim				return (FALSE);
6457281440Srpaulo			synhw.verticalScroll   = (status[0] & 0x01) != 0;
6458281440Srpaulo			synhw.horizontalScroll = (status[0] & 0x02) != 0;
6459281440Srpaulo			synhw.verticalWheel    = (status[0] & 0x08) != 0;
6460255153Sdumbbell			synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
6461282734Srpaulo			synhw.capEWmode        = (status[0] & 0x04) != 0;
6462281440Srpaulo			if (verbose >= 2) {
6463281440Srpaulo				printf("  Extended model ID:\n");
6464281440Srpaulo				printf("   verticalScroll: %d\n",
6465281440Srpaulo				    synhw.verticalScroll);
6466281440Srpaulo				printf("   horizontalScroll: %d\n",
6467281440Srpaulo				    synhw.horizontalScroll);
6468281440Srpaulo				printf("   verticalWheel: %d\n",
6469281440Srpaulo				    synhw.verticalWheel);
6470281440Srpaulo				printf("   nExtendedButtons: %d\n",
6471281440Srpaulo				    synhw.nExtendedButtons);
6472282734Srpaulo				printf("   capEWmode: %d\n",
6473282734Srpaulo				    synhw.capEWmode);
6474281440Srpaulo			}
6475255153Sdumbbell			/*
6476255153Sdumbbell			 * Add the number of extended buttons to the total
6477255153Sdumbbell			 * button support count, including the middle button
6478255153Sdumbbell			 * if capMiddle support bit is set.
6479255153Sdumbbell			 */
6480255153Sdumbbell			buttons = synhw.nExtendedButtons + synhw.capMiddle;
6481233580Sjkim		} else
6482255153Sdumbbell			/*
6483255153Sdumbbell			 * If the capFourButtons support bit is set,
6484255153Sdumbbell			 * add a fourth button to the total button count.
6485255153Sdumbbell			 */
6486233580Sjkim			buttons = synhw.capFourButtons ? 1 : 0;
6487282734Srpaulo
6488282734Srpaulo		/* Read the continued capabilities bits. */
6489282734Srpaulo		if (synhw.nExtendedQueries >= 4) {
6490282734Srpaulo			if (!set_mouse_scaling(kbdc, 1))
6491282734Srpaulo				return (FALSE);
6492349541Swulf			if (mouse_ext_command(kbdc,
6493349541Swulf			    SYNAPTICS_READ_CAPABILITIES_CONT) == 0)
6494282734Srpaulo				return (FALSE);
6495282734Srpaulo			if (get_mouse_status(kbdc, status, 0, 3) != 3)
6496282734Srpaulo				return (FALSE);
6497282734Srpaulo
6498282734Srpaulo			synhw.capClickPad         = (status[1] & 0x01) << 1;
6499282734Srpaulo			synhw.capClickPad        |= (status[0] & 0x10) != 0;
6500282734Srpaulo			synhw.capDeluxeLEDs       = (status[1] & 0x02) != 0;
6501282734Srpaulo			synhw.noAbsoluteFilter    = (status[1] & 0x04) != 0;
6502282734Srpaulo			synhw.capReportsV         = (status[1] & 0x08) != 0;
6503282734Srpaulo			synhw.capUniformClickPad  = (status[1] & 0x10) != 0;
6504282734Srpaulo			synhw.capReportsMin       = (status[1] & 0x20) != 0;
6505282734Srpaulo			synhw.capInterTouch       = (status[1] & 0x40) != 0;
6506282734Srpaulo			synhw.capReportsMax       = (status[0] & 0x02) != 0;
6507282734Srpaulo			synhw.capClearPad         = (status[0] & 0x04) != 0;
6508282734Srpaulo			synhw.capAdvancedGestures = (status[0] & 0x08) != 0;
6509282734Srpaulo			synhw.capCoveredPad       = (status[0] & 0x80) != 0;
6510282734Srpaulo
6511282734Srpaulo			if (synhw.capReportsMax) {
6512282734Srpaulo				if (!set_mouse_scaling(kbdc, 1))
6513282734Srpaulo					return (FALSE);
6514349541Swulf				if (mouse_ext_command(kbdc,
6515349541Swulf				    SYNAPTICS_READ_MAX_COORDS) == 0)
6516282734Srpaulo					return (FALSE);
6517282734Srpaulo				if (get_mouse_status(kbdc, status, 0, 3) != 3)
6518282734Srpaulo					return (FALSE);
6519282734Srpaulo
6520282734Srpaulo				synhw.maximumXCoord = (status[0] << 5) |
6521282734Srpaulo						     ((status[1] & 0x0f) << 1);
6522282734Srpaulo				synhw.maximumYCoord = (status[2] << 5) |
6523282734Srpaulo						     ((status[1] & 0xf0) >> 3);
6524307576Sgonzo			} else {
6525343158Swulf				/*
6526343158Swulf				 * Typical bezel limits. Taken from 'Synaptics
6527343158Swulf				 * PS/2 * TouchPad Interfacing Guide' p.3.2.3.
6528343158Swulf				 */
6529343158Swulf				synhw.maximumXCoord = 5472;
6530343158Swulf				synhw.maximumYCoord = 4448;
6531282734Srpaulo			}
6532307576Sgonzo
6533282734Srpaulo			if (synhw.capReportsMin) {
6534282734Srpaulo				if (!set_mouse_scaling(kbdc, 1))
6535282734Srpaulo					return (FALSE);
6536349541Swulf				if (mouse_ext_command(kbdc,
6537349541Swulf				    SYNAPTICS_READ_MIN_COORDS) == 0)
6538282734Srpaulo					return (FALSE);
6539282734Srpaulo				if (get_mouse_status(kbdc, status, 0, 3) != 3)
6540282734Srpaulo					return (FALSE);
6541282734Srpaulo
6542282734Srpaulo				synhw.minimumXCoord = (status[0] << 5) |
6543282734Srpaulo						     ((status[1] & 0x0f) << 1);
6544282734Srpaulo				synhw.minimumYCoord = (status[2] << 5) |
6545282734Srpaulo						     ((status[1] & 0xf0) >> 3);
6546307576Sgonzo			} else {
6547343158Swulf				/*
6548343158Swulf				 * Typical bezel limits. Taken from 'Synaptics
6549343158Swulf				 * PS/2 * TouchPad Interfacing Guide' p.3.2.3.
6550343158Swulf				 */
6551343158Swulf				synhw.minimumXCoord = 1472;
6552343158Swulf				synhw.minimumYCoord = 1408;
6553282734Srpaulo			}
6554282734Srpaulo
6555331170Seadler			/*
6556331170Seadler			 * ClickPad properties are not exported through PS/2
6557331170Seadler			 * protocol. Detection is based on controller's PnP ID.
6558331170Seadler			 */
6559331170Seadler			if (synhw.capClickPad && psmcpnp_sc != NULL) {
6560331170Seadler				switch (psmcpnp_sc->type) {
6561331170Seadler				case PSMCPNP_FORCEPAD:
6562331170Seadler					synhw.forcePad = 1;
6563331170Seadler					break;
6564344165Swulf				case PSMCPNP_TOPBUTTONPAD:
6565344165Swulf					synhw.topButtonPad = 1;
6566344165Swulf					break;
6567331170Seadler				default:
6568331170Seadler					break;
6569331170Seadler				}
6570331170Seadler			}
6571331170Seadler
6572282734Srpaulo			if (verbose >= 2) {
6573282734Srpaulo				printf("  Continued capabilities:\n");
6574282734Srpaulo				printf("   capClickPad: %d\n",
6575282734Srpaulo				       synhw.capClickPad);
6576282734Srpaulo				printf("   capDeluxeLEDs: %d\n",
6577282734Srpaulo				       synhw.capDeluxeLEDs);
6578282734Srpaulo				printf("   noAbsoluteFilter: %d\n",
6579282734Srpaulo				       synhw.noAbsoluteFilter);
6580282734Srpaulo				printf("   capReportsV: %d\n",
6581282734Srpaulo				       synhw.capReportsV);
6582282734Srpaulo				printf("   capUniformClickPad: %d\n",
6583282734Srpaulo				       synhw.capUniformClickPad);
6584282734Srpaulo				printf("   capReportsMin: %d\n",
6585282734Srpaulo				       synhw.capReportsMin);
6586282734Srpaulo				printf("   capInterTouch: %d\n",
6587282734Srpaulo				       synhw.capInterTouch);
6588282734Srpaulo				printf("   capReportsMax: %d\n",
6589282734Srpaulo				       synhw.capReportsMax);
6590282734Srpaulo				printf("   capClearPad: %d\n",
6591282734Srpaulo				       synhw.capClearPad);
6592282734Srpaulo				printf("   capAdvancedGestures: %d\n",
6593282734Srpaulo				       synhw.capAdvancedGestures);
6594282734Srpaulo				printf("   capCoveredPad: %d\n",
6595282734Srpaulo				       synhw.capCoveredPad);
6596282734Srpaulo				if (synhw.capReportsMax) {
6597282734Srpaulo					printf("   maximumXCoord: %d\n",
6598282734Srpaulo					       synhw.maximumXCoord);
6599282734Srpaulo					printf("   maximumYCoord: %d\n",
6600282734Srpaulo					       synhw.maximumYCoord);
6601282734Srpaulo				}
6602282734Srpaulo				if (synhw.capReportsMin) {
6603282734Srpaulo					printf("   minimumXCoord: %d\n",
6604282734Srpaulo					       synhw.minimumXCoord);
6605282734Srpaulo					printf("   minimumYCoord: %d\n",
6606282734Srpaulo					       synhw.minimumYCoord);
6607282734Srpaulo				}
6608331170Seadler				if (synhw.capClickPad) {
6609344165Swulf					printf("  Clickpad capabilities:\n");
6610331170Seadler					printf("   forcePad: %d\n",
6611331170Seadler					       synhw.forcePad);
6612344165Swulf					printf("   topButtonPad: %d\n",
6613344165Swulf					       synhw.topButtonPad);
6614331170Seadler				}
6615282734Srpaulo			}
6616282734Srpaulo			buttons += synhw.capClickPad;
6617282734Srpaulo		}
6618233580Sjkim	}
6619282734Srpaulo
6620233580Sjkim	if (verbose >= 2) {
6621233580Sjkim		if (synhw.capExtended)
6622233580Sjkim			printf("  Additional Buttons: %d\n", buttons);
6623233580Sjkim		else
6624178019Sjkim			printf("  No extended capabilities\n");
6625178019Sjkim	}
6626178019Sjkim
6627139982Sphilip	/*
6628255153Sdumbbell	 * Add the default number of 3 buttons to the total
6629255153Sdumbbell	 * count of supported buttons reported above.
6630255153Sdumbbell	 */
6631255153Sdumbbell	buttons += 3;
6632255153Sdumbbell
6633255153Sdumbbell	/*
6634186175Sdumbbell	 * Read the mode byte.
6635178019Sjkim	 *
6636178019Sjkim	 * XXX: Note the Synaptics documentation also defines the first
6637178019Sjkim	 * byte of the response to this query to be a constant 0x3b, this
6638178019Sjkim	 * does not appear to be true for Touchpads with guest devices.
6639139982Sphilip	 */
6640349541Swulf	if (mouse_ext_command(kbdc, SYNAPTICS_READ_MODES) == 0)
6641139982Sphilip		return (FALSE);
6642178019Sjkim	if (get_mouse_status(kbdc, status, 0, 3) != 3)
6643139982Sphilip		return (FALSE);
6644343158Swulf	if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
6645178019Sjkim		printf("  Failed to read mode byte\n");
6646178019Sjkim		return (FALSE);
6647139982Sphilip	}
6648139982Sphilip
6649284320Sglebius	if (arg == PROBE)
6650233580Sjkim		sc->synhw = synhw;
6651233580Sjkim	if (!synaptics_support)
6652233580Sjkim		return (FALSE);
6653233580Sjkim
6654308652Sgonzo	/* Set mouse type just now for synaptics_set_mode() */
6655308652Sgonzo	sc->hw.model = MOUSE_MODEL_SYNAPTICS;
6656308652Sgonzo
6657284320Sglebius	synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6658284320Sglebius
6659284320Sglebius	if (trackpoint_support && synhw.capPassthrough) {
6660284320Sglebius		enable_trackpoint(sc, arg);
6661284320Sglebius	}
6662284320Sglebius
6663233580Sjkim	VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
6664132865Snjl
6665284320Sglebius	if (arg == PROBE) {
6666233580Sjkim		/* Create sysctl tree. */
6667307576Sgonzo		synaptics_sysctl_create_tree(sc, "synaptics",
6668307576Sgonzo		    "Synaptics TouchPad");
6669233580Sjkim		sc->hw.buttons = buttons;
6670233580Sjkim	}
6671183888Sdumbbell
6672178019Sjkim	return (TRUE);
6673132865Snjl}
6674133295Sphilip
6675281441Srpaulostatic void
6676281441Srpaulosynaptics_passthrough_on(struct psm_softc *sc)
6677281441Srpaulo{
6678282734Srpaulo	VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n"));
6679282734Srpaulo	synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5));
6680281441Srpaulo}
6681281441Srpaulo
6682281441Srpaulostatic void
6683281441Srpaulosynaptics_passthrough_off(struct psm_softc *sc)
6684281441Srpaulo{
6685281441Srpaulo	VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n"));
6686281441Srpaulo	set_mouse_scaling(sc->kbdc, 2);
6687281441Srpaulo	set_mouse_scaling(sc->kbdc, 1);
6688282734Srpaulo	synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6689281441Srpaulo}
6690281441Srpaulo
6691248478Sjkim/* IBM/Lenovo TrackPoint */
6692248478Sjkimstatic int
6693281441Srpaulotrackpoint_command(struct psm_softc *sc, int cmd, int loc, int val)
6694248478Sjkim{
6695248478Sjkim	const int seq[] = { 0xe2, cmd, loc, val };
6696248478Sjkim	int i;
6697248478Sjkim
6698281441Srpaulo	if (sc->synhw.capPassthrough)
6699281441Srpaulo		synaptics_passthrough_on(sc);
6700281441Srpaulo
6701281441Srpaulo	for (i = 0; i < nitems(seq); i++) {
6702281441Srpaulo		if (sc->synhw.capPassthrough &&
6703281441Srpaulo		    (seq[i] == 0xff || seq[i] == 0xe7))
6704281441Srpaulo			if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) {
6705281441Srpaulo				synaptics_passthrough_off(sc);
6706281441Srpaulo				return (EIO);
6707281441Srpaulo			}
6708281441Srpaulo		if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) {
6709281441Srpaulo			if (sc->synhw.capPassthrough)
6710281441Srpaulo				synaptics_passthrough_off(sc);
6711248478Sjkim			return (EIO);
6712281441Srpaulo		}
6713281441Srpaulo	}
6714281441Srpaulo
6715281441Srpaulo	if (sc->synhw.capPassthrough)
6716281441Srpaulo		synaptics_passthrough_off(sc);
6717281441Srpaulo
6718248478Sjkim	return (0);
6719248478Sjkim}
6720248478Sjkim
6721248478Sjkim#define	PSM_TPINFO(x)	offsetof(struct psm_softc, tpinfo.x)
6722248478Sjkim#define	TPMASK		0
6723248478Sjkim#define	TPLOC		1
6724248478Sjkim#define	TPINFO		2
6725248478Sjkim
6726248478Sjkimstatic int
6727248478Sjkimtrackpoint_sysctl(SYSCTL_HANDLER_ARGS)
6728248478Sjkim{
6729248478Sjkim	static const int data[][3] = {
6730248478Sjkim		{ 0x00, 0x4a, PSM_TPINFO(sensitivity) },
6731248478Sjkim		{ 0x00, 0x4d, PSM_TPINFO(inertia) },
6732248478Sjkim		{ 0x00, 0x60, PSM_TPINFO(uplateau) },
6733248478Sjkim		{ 0x00, 0x57, PSM_TPINFO(reach) },
6734248478Sjkim		{ 0x00, 0x58, PSM_TPINFO(draghys) },
6735248478Sjkim		{ 0x00, 0x59, PSM_TPINFO(mindrag) },
6736248478Sjkim		{ 0x00, 0x5a, PSM_TPINFO(upthresh) },
6737248478Sjkim		{ 0x00, 0x5c, PSM_TPINFO(threshold) },
6738248478Sjkim		{ 0x00, 0x5d, PSM_TPINFO(jenks) },
6739248478Sjkim		{ 0x00, 0x5e, PSM_TPINFO(ztime) },
6740248478Sjkim		{ 0x01, 0x2c, PSM_TPINFO(pts) },
6741248478Sjkim		{ 0x08, 0x2d, PSM_TPINFO(skipback) }
6742248478Sjkim	};
6743248478Sjkim	struct psm_softc *sc;
6744248478Sjkim	int error, newval, *oldvalp;
6745248478Sjkim	const int *tp;
6746248478Sjkim
6747248478Sjkim	if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
6748248478Sjkim		return (EINVAL);
6749248478Sjkim	sc = arg1;
6750248478Sjkim	tp = data[arg2];
6751248478Sjkim	oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
6752248478Sjkim	newval = *oldvalp;
6753248478Sjkim	error = sysctl_handle_int(oidp, &newval, 0, req);
6754248478Sjkim	if (error != 0)
6755248478Sjkim		return (error);
6756248478Sjkim	if (newval == *oldvalp)
6757248478Sjkim		return (0);
6758248478Sjkim	if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
6759248478Sjkim		return (EINVAL);
6760281441Srpaulo	error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47,
6761248478Sjkim	    tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
6762248478Sjkim	if (error != 0)
6763248478Sjkim		return (error);
6764248478Sjkim	*oldvalp = newval;
6765248478Sjkim
6766248478Sjkim	return (0);
6767248478Sjkim}
6768248478Sjkim
6769248478Sjkimstatic void
6770248478Sjkimtrackpoint_sysctl_create_tree(struct psm_softc *sc)
6771248478Sjkim{
6772248478Sjkim
6773248478Sjkim	if (sc->tpinfo.sysctl_tree != NULL)
6774248478Sjkim		return;
6775248478Sjkim
6776248478Sjkim	/* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
6777248478Sjkim	sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
6778248478Sjkim	sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
6779248478Sjkim	    SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD,
6780248478Sjkim	    0, "IBM/Lenovo TrackPoint");
6781248478Sjkim
6782248478Sjkim	/* hw.psm.trackpoint.sensitivity */
6783281441Srpaulo	sc->tpinfo.sensitivity = 0x80;
6784248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6785248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6786248478Sjkim	    "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6787248478Sjkim	    sc, TRACKPOINT_SYSCTL_SENSITIVITY,
6788248478Sjkim	    trackpoint_sysctl, "I",
6789248478Sjkim	    "Sensitivity");
6790248478Sjkim
6791248478Sjkim	/* hw.psm.trackpoint.negative_inertia */
6792248478Sjkim	sc->tpinfo.inertia = 0x06;
6793248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6794248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6795248478Sjkim	    "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6796248478Sjkim	    sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
6797248478Sjkim	    trackpoint_sysctl, "I",
6798248478Sjkim	    "Negative inertia factor");
6799248478Sjkim
6800248478Sjkim	/* hw.psm.trackpoint.upper_plateau */
6801248478Sjkim	sc->tpinfo.uplateau = 0x61;
6802248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6803248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6804248478Sjkim	    "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6805248478Sjkim	    sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
6806248478Sjkim	    trackpoint_sysctl, "I",
6807248478Sjkim	    "Transfer function upper plateau speed");
6808248478Sjkim
6809248478Sjkim	/* hw.psm.trackpoint.backup_range */
6810248478Sjkim	sc->tpinfo.reach = 0x0a;
6811248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6812248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6813248478Sjkim	    "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6814248478Sjkim	    sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
6815248478Sjkim	    trackpoint_sysctl, "I",
6816248478Sjkim	    "Backup range");
6817248478Sjkim
6818248478Sjkim	/* hw.psm.trackpoint.drag_hysteresis */
6819248478Sjkim	sc->tpinfo.draghys = 0xff;
6820248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6821248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6822248478Sjkim	    "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6823248478Sjkim	    sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
6824248478Sjkim	    trackpoint_sysctl, "I",
6825248478Sjkim	    "Drag hysteresis");
6826248478Sjkim
6827248478Sjkim	/* hw.psm.trackpoint.minimum_drag */
6828248478Sjkim	sc->tpinfo.mindrag = 0x14;
6829248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6830248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6831248478Sjkim	    "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6832248478Sjkim	    sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
6833248478Sjkim	    trackpoint_sysctl, "I",
6834248478Sjkim	    "Minimum drag");
6835248478Sjkim
6836248478Sjkim	/* hw.psm.trackpoint.up_threshold */
6837248478Sjkim	sc->tpinfo.upthresh = 0xff;
6838248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6839248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6840248478Sjkim	    "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6841248478Sjkim	    sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
6842248478Sjkim	    trackpoint_sysctl, "I",
6843248478Sjkim	    "Up threshold for release");
6844248478Sjkim
6845248478Sjkim	/* hw.psm.trackpoint.threshold */
6846248478Sjkim	sc->tpinfo.threshold = 0x08;
6847248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6848248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6849248478Sjkim	    "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6850248478Sjkim	    sc, TRACKPOINT_SYSCTL_THRESHOLD,
6851248478Sjkim	    trackpoint_sysctl, "I",
6852248478Sjkim	    "Threshold");
6853248478Sjkim
6854248478Sjkim	/* hw.psm.trackpoint.jenks_curvature */
6855248478Sjkim	sc->tpinfo.jenks = 0x87;
6856248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6857248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6858248478Sjkim	    "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6859248478Sjkim	    sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
6860248478Sjkim	    trackpoint_sysctl, "I",
6861248478Sjkim	    "Jenks curvature");
6862248478Sjkim
6863248478Sjkim	/* hw.psm.trackpoint.z_time */
6864248478Sjkim	sc->tpinfo.ztime = 0x26;
6865248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6866248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6867248478Sjkim	    "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6868248478Sjkim	    sc, TRACKPOINT_SYSCTL_Z_TIME,
6869248478Sjkim	    trackpoint_sysctl, "I",
6870248478Sjkim	    "Z time constant");
6871248478Sjkim
6872248478Sjkim	/* hw.psm.trackpoint.press_to_select */
6873248478Sjkim	sc->tpinfo.pts = 0x00;
6874248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6875248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6876248478Sjkim	    "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6877248478Sjkim	    sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
6878248478Sjkim	    trackpoint_sysctl, "I",
6879248478Sjkim	    "Press to Select");
6880248478Sjkim
6881248478Sjkim	/* hw.psm.trackpoint.skip_backups */
6882248478Sjkim	sc->tpinfo.skipback = 0x00;
6883248478Sjkim	SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6884248478Sjkim	    SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6885248478Sjkim	    "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6886248478Sjkim	    sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
6887248478Sjkim	    trackpoint_sysctl, "I",
6888248478Sjkim	    "Skip backups from drags");
6889248478Sjkim}
6890248478Sjkim
6891281441Srpaulostatic void
6892281441Srpauloset_trackpoint_parameters(struct psm_softc *sc)
6893281441Srpaulo{
6894281441Srpaulo	trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity);
6895281441Srpaulo	trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau);
6896281441Srpaulo	trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia);
6897281441Srpaulo	trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach);
6898281441Srpaulo	trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys);
6899281441Srpaulo	trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag);
6900281441Srpaulo	trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh);
6901281441Srpaulo	trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold);
6902281441Srpaulo	trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks);
6903281441Srpaulo	trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime);
6904281441Srpaulo	if (sc->tpinfo.pts == 0x01)
6905281441Srpaulo		trackpoint_command(sc, 0x47, 0x2c, 0x01);
6906281441Srpaulo	if (sc->tpinfo.skipback == 0x01)
6907281441Srpaulo		trackpoint_command(sc, 0x47, 0x2d, 0x08);
6908281441Srpaulo}
6909281441Srpaulo
6910248478Sjkimstatic int
6911284320Sglebiusenable_trackpoint(struct psm_softc *sc, enum probearg arg)
6912248478Sjkim{
6913284320Sglebius	KBDC kbdc = sc->kbdc;
6914248478Sjkim	int id;
6915248478Sjkim
6916284585Srpaulo	/*
6917284585Srpaulo	 * If called from enable_synaptics(), make sure that passthrough
6918284585Srpaulo	 * mode is enabled so we can reach the trackpoint.
6919284585Srpaulo	 * However, passthrough mode must be disabled before setting the
6920284585Srpaulo	 * trackpoint parameters, as rackpoint_command() enables and disables
6921284585Srpaulo	 * passthrough mode on its own.
6922284585Srpaulo	 */
6923284585Srpaulo	if (sc->synhw.capPassthrough)
6924284585Srpaulo		synaptics_passthrough_on(sc);
6925284585Srpaulo
6926248478Sjkim	if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
6927248478Sjkim	    read_aux_data(kbdc) != 0x01)
6928284585Srpaulo		goto no_trackpoint;
6929248478Sjkim	id = read_aux_data(kbdc);
6930248478Sjkim	if (id < 0x01)
6931284585Srpaulo		goto no_trackpoint;
6932284450Sglebius	if (arg == PROBE)
6933248478Sjkim		sc->tphw = id;
6934248478Sjkim	if (!trackpoint_support)
6935284585Srpaulo		goto no_trackpoint;
6936248478Sjkim
6937284585Srpaulo	if (sc->synhw.capPassthrough)
6938284585Srpaulo		synaptics_passthrough_off(sc);
6939284585Srpaulo
6940284320Sglebius	if (arg == PROBE) {
6941248478Sjkim		trackpoint_sysctl_create_tree(sc);
6942281441Srpaulo		/*
6943281441Srpaulo		 * Don't overwrite hwid and buttons when we are
6944281441Srpaulo		 * a guest device.
6945281441Srpaulo		 */
6946281441Srpaulo		if (!sc->synhw.capPassthrough) {
6947281441Srpaulo			sc->hw.hwid = id;
6948281441Srpaulo			sc->hw.buttons = 3;
6949281441Srpaulo		}
6950248478Sjkim	}
6951248478Sjkim
6952284320Sglebius	set_trackpoint_parameters(sc);
6953284320Sglebius
6954248478Sjkim	return (TRUE);
6955284585Srpaulo
6956284585Srpaulono_trackpoint:
6957284585Srpaulo	if (sc->synhw.capPassthrough)
6958284585Srpaulo		synaptics_passthrough_off(sc);
6959284585Srpaulo
6960284585Srpaulo	return (FALSE);
6961248478Sjkim}
6962248478Sjkim
696349965Syokota/* Interlink electronics VersaPad */
696449965Syokotastatic int
6965284320Sglebiusenable_versapad(struct psm_softc *sc, enum probearg arg)
696649965Syokota{
6967284320Sglebius	KBDC kbdc = sc->kbdc;
6968178019Sjkim	int data[3];
696949965Syokota
6970178019Sjkim	set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
6971178019Sjkim	set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
6972178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
6973178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
6974178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
6975178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
6976178019Sjkim	if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
6977178019Sjkim		return (FALSE);
6978178019Sjkim	if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
6979178019Sjkim		return (FALSE);
6980178019Sjkim	set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
698149965Syokota
6982178019Sjkim	return (TRUE);				/* PS/2 absolute mode */
698349965Syokota}
698449965Syokota
6985307576Sgonzo/* Elantech Touchpad */
6986307576Sgonzostatic int
6987307576Sgonzoelantech_read_1(KBDC kbdc, int hwversion, int reg, int *val)
6988307576Sgonzo{
6989307576Sgonzo	int res, readcmd, retidx;
6990307576Sgonzo	int resp[3];
6991307576Sgonzo
6992307576Sgonzo	readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR;
6993307576Sgonzo	retidx = hwversion == 4 ? 1 : 0;
6994307576Sgonzo
6995307576Sgonzo	res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6996307576Sgonzo	res |= send_aux_command(kbdc, readcmd) != PSM_ACK;
6997307576Sgonzo	res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6998307576Sgonzo	res |= send_aux_command(kbdc, reg) != PSM_ACK;
6999307576Sgonzo	res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
7000307576Sgonzo
7001307576Sgonzo	if (res == 0)
7002307576Sgonzo		*val = resp[retidx];
7003307576Sgonzo
7004307576Sgonzo	return (res);
7005307576Sgonzo}
7006307576Sgonzo
7007307576Sgonzostatic int
7008307576Sgonzoelantech_write_1(KBDC kbdc, int hwversion, int reg, int val)
7009307576Sgonzo{
7010307576Sgonzo	int res, writecmd;
7011307576Sgonzo
7012307576Sgonzo	writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR;
7013307576Sgonzo
7014307576Sgonzo	res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7015307576Sgonzo	res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
7016307576Sgonzo	res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7017307576Sgonzo	res |= send_aux_command(kbdc, reg) != PSM_ACK;
7018307576Sgonzo	if (hwversion == 4) {
7019307576Sgonzo		res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7020307576Sgonzo		res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
7021307576Sgonzo	}
7022307576Sgonzo	res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7023307576Sgonzo	res |= send_aux_command(kbdc, val) != PSM_ACK;
7024307576Sgonzo	res |= set_mouse_scaling(kbdc, 1) == 0;
7025307576Sgonzo
7026307576Sgonzo	return (res);
7027307576Sgonzo}
7028307576Sgonzo
7029307576Sgonzostatic int
7030307576Sgonzoelantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp)
7031307576Sgonzo{
7032307576Sgonzo	int res;
7033307576Sgonzo
7034307576Sgonzo	if (hwversion == 2) {
7035307576Sgonzo		res = set_mouse_scaling(kbdc, 1) == 0;
7036307576Sgonzo		res |= mouse_ext_command(kbdc, cmd) == 0;
7037307576Sgonzo	} else {
7038307576Sgonzo		res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7039307576Sgonzo		res |= send_aux_command(kbdc, cmd) != PSM_ACK;
7040307576Sgonzo	}
7041307576Sgonzo	res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
7042307576Sgonzo
7043307576Sgonzo	return (res);
7044307576Sgonzo}
7045307576Sgonzo
7046307576Sgonzostatic int
7047307576Sgonzoelantech_init(KBDC kbdc, elantechhw_t *elanhw)
7048307576Sgonzo{
7049307576Sgonzo	int i, val, res, hwversion, reg10;
7050307576Sgonzo
7051307576Sgonzo	/* set absolute mode */
7052307576Sgonzo	hwversion = elanhw->hwversion;
7053307576Sgonzo	reg10 = -1;
7054307576Sgonzo	switch (hwversion) {
7055307576Sgonzo	case 2:
7056307576Sgonzo		reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4;
7057307576Sgonzo		res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
7058307576Sgonzo		if (res)
7059307576Sgonzo			break;
7060307576Sgonzo		res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A);
7061307576Sgonzo		break;
7062307576Sgonzo	case 3:
7063307576Sgonzo		reg10 = 0x0b;
7064307576Sgonzo		res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
7065307576Sgonzo		break;
7066307576Sgonzo	case 4:
7067307576Sgonzo		res = elantech_write_1(kbdc, hwversion, 0x07, 0x01);
7068307576Sgonzo		break;
7069307576Sgonzo	default:
7070307576Sgonzo		res = 1;
7071307576Sgonzo	}
7072307576Sgonzo
7073307576Sgonzo	/* Read back reg 0x10 to ensure hardware is ready. */
7074307576Sgonzo	if (res == 0 && reg10 >= 0) {
7075307576Sgonzo		for (i = 0; i < 5; i++) {
7076307576Sgonzo			if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0)
7077307576Sgonzo				break;
7078307576Sgonzo			DELAY(2000);
7079307576Sgonzo		}
7080307576Sgonzo		if (i == 5)
7081307576Sgonzo			res = 1;
7082307576Sgonzo	}
7083307576Sgonzo
7084307576Sgonzo	if (res)
7085307576Sgonzo		printf("couldn't set absolute mode\n");
7086307576Sgonzo
7087307576Sgonzo	return (res);
7088307576Sgonzo}
7089307576Sgonzo
7090307576Sgonzostatic void
7091307576Sgonzoelantech_init_synaptics(struct psm_softc *sc)
7092307576Sgonzo{
7093307576Sgonzo
7094307576Sgonzo	/* Set capabilites required by movement smother */
7095307576Sgonzo	sc->synhw.infoMajor = sc->elanhw.hwversion;
7096307576Sgonzo	sc->synhw.infoMinor = sc->elanhw.fwversion;
7097307576Sgonzo	sc->synhw.infoXupmm = sc->elanhw.dpmmx;
7098307576Sgonzo	sc->synhw.infoYupmm = sc->elanhw.dpmmy;
7099307576Sgonzo	sc->synhw.verticalScroll = 0;
7100307576Sgonzo	sc->synhw.nExtendedQueries = 4;
7101307576Sgonzo	sc->synhw.capExtended = 1;
7102307576Sgonzo	sc->synhw.capPassthrough = sc->elanhw.hastrackpoint;
7103307576Sgonzo	sc->synhw.capClickPad = sc->elanhw.isclickpad;
7104307576Sgonzo	sc->synhw.capMultiFinger = 1;
7105318500Swulf	if (sc->elanhw.issemimt)
7106318500Swulf		sc->synhw.capAdvancedGestures = 1;
7107318500Swulf	else
7108318500Swulf		sc->synhw.capReportsV = 1;
7109307576Sgonzo	sc->synhw.capPalmDetect = 1;
7110307576Sgonzo	sc->synhw.capPen = 0;
7111307576Sgonzo	sc->synhw.capReportsMax = 1;
7112307576Sgonzo	sc->synhw.maximumXCoord = sc->elanhw.sizex;
7113307576Sgonzo	sc->synhw.maximumYCoord = sc->elanhw.sizey;
7114307576Sgonzo	sc->synhw.capReportsMin = 1;
7115307576Sgonzo	sc->synhw.minimumXCoord = 0;
7116307576Sgonzo	sc->synhw.minimumYCoord = 0;
7117307576Sgonzo
7118307576Sgonzo	if (sc->syninfo.sysctl_tree == NULL) {
7119307576Sgonzo		synaptics_sysctl_create_tree(sc, "elantech",
7120307576Sgonzo		    "Elantech Touchpad");
7121307576Sgonzo
7122307576Sgonzo		/*
7123307576Sgonzo		 * Adjust synaptic smoother tunables
7124307576Sgonzo		 * 1. Disable finger detection pressure threshold. Unlike
7125307576Sgonzo		 *    synaptics we assume the finger is acting when packet with
7126307576Sgonzo		 *    its X&Y arrives not when pressure exceedes some threshold
7127307576Sgonzo		 * 2. Disable unrelated features like margins and noisy areas
7128307576Sgonzo		 * 3. Disable virtual scroll areas as 2nd finger is preferable
7129307576Sgonzo		 * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized
7130307576Sgonzo		 *    softbuttons
7131307576Sgonzo		 * 5. Scale down divisors and movement lengths by a factor of 3
7132307576Sgonzo		 *    where 3 is Synaptics to Elantech (~2200/800) dpi ratio
7133307576Sgonzo		 */
7134307576Sgonzo
7135307576Sgonzo		/* Set reporting range to be equal touchpad size */
7136307576Sgonzo		sc->syninfo.max_x = sc->elanhw.sizex;
7137307576Sgonzo		sc->syninfo.max_y = sc->elanhw.sizey;
7138307576Sgonzo
7139307576Sgonzo		/* Disable finger detection pressure threshold */
7140307576Sgonzo		sc->syninfo.min_pressure = 1;
7141307576Sgonzo
7142318500Swulf		/* Adjust palm width to nearly match synaptics w=10 */
7143318500Swulf		sc->syninfo.max_width = 7;
7144318500Swulf
7145318500Swulf		/* Elans often report double & triple taps as single event */
7146318500Swulf		sc->syninfo.tap_min_queue = 1;
7147318500Swulf
7148307576Sgonzo		/* Use full area of touchpad */
7149307576Sgonzo		sc->syninfo.margin_top = 0;
7150307576Sgonzo		sc->syninfo.margin_right = 0;
7151307576Sgonzo		sc->syninfo.margin_bottom = 0;
7152307576Sgonzo		sc->syninfo.margin_left = 0;
7153307576Sgonzo
7154307576Sgonzo		/* Disable noisy area */
7155307576Sgonzo		sc->syninfo.na_top = 0;
7156307576Sgonzo		sc->syninfo.na_right = 0;
7157307576Sgonzo		sc->syninfo.na_bottom = 0;
7158307576Sgonzo		sc->syninfo.na_left = 0;
7159307576Sgonzo
7160307576Sgonzo		/* Tune divisors and movement lengths */
7161307576Sgonzo		sc->syninfo.weight_len_squared = 200;
7162307576Sgonzo		sc->syninfo.div_min = 3;
7163307576Sgonzo		sc->syninfo.div_max = 6;
7164307576Sgonzo		sc->syninfo.div_max_na = 10;
7165307576Sgonzo		sc->syninfo.div_len = 30;
7166307576Sgonzo		sc->syninfo.tap_max_delta = 25;
7167307576Sgonzo
7168307576Sgonzo		/* Disable virtual scrolling areas and tune its divisors */
7169307576Sgonzo		sc->syninfo.vscroll_hor_area = 0;
7170307576Sgonzo		sc->syninfo.vscroll_ver_area = 0;
7171307576Sgonzo		sc->syninfo.vscroll_min_delta = 15;
7172307576Sgonzo		sc->syninfo.vscroll_div_min = 30;
7173307576Sgonzo		sc->syninfo.vscroll_div_max = 50;
7174307576Sgonzo
7175307576Sgonzo		/* Set bottom quarter as 42% - 16% - 42% sized softbuttons */
7176307576Sgonzo		if (sc->elanhw.isclickpad) {
7177307576Sgonzo			sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4;
7178307576Sgonzo			sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25;
7179307576Sgonzo			sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25;
7180307576Sgonzo		}
7181307576Sgonzo	}
7182307576Sgonzo
7183307576Sgonzo	return;
7184307576Sgonzo}
7185307576Sgonzo
7186307576Sgonzostatic int
7187307576Sgonzoenable_elantech(struct psm_softc *sc, enum probearg arg)
7188307576Sgonzo{
7189307576Sgonzo	static const int ic2hw[] =
7190307576Sgonzo	/*IC: 0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
7191349541Swulf	    { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
7192318500Swulf	static const int fw_sizes[][3] = {
7193318500Swulf		/* FW.vers  MaxX  MaxY */
7194318500Swulf		{ 0x020030, 1152,  768 },
7195318500Swulf		{ 0x020800, 1152,  768 },
7196318500Swulf		{ 0x020b00, 1152,  768 },
7197318500Swulf		{ 0x040215,  900,  500 },
7198318500Swulf		{ 0x040216,  819,  405 },
7199318500Swulf		{ 0x040219,  900,  500 },
7200318500Swulf	};
7201307576Sgonzo	elantechhw_t elanhw;
7202318500Swulf	int icversion, hwversion, xtr, i, id, resp[3], dpix, dpiy;
7203307576Sgonzo	KBDC kbdc = sc->kbdc;
7204307576Sgonzo
7205307576Sgonzo	VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n"));
7206307576Sgonzo
7207307576Sgonzo	set_mouse_scaling(kbdc, 1);
7208307576Sgonzo	set_mouse_scaling(kbdc, 1);
7209307576Sgonzo	set_mouse_scaling(kbdc, 1);
7210307576Sgonzo	if (get_mouse_status(kbdc, resp, 0, 3) != 3)
7211307576Sgonzo		return (FALSE);
7212307576Sgonzo
7213307576Sgonzo	if (!ELANTECH_MAGIC(resp))
7214307576Sgonzo		return (FALSE);
7215307576Sgonzo
7216307576Sgonzo	/* Identify the Touchpad version. */
7217307576Sgonzo	if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp))
7218307576Sgonzo		return (FALSE);
7219307576Sgonzo
7220307576Sgonzo	bzero(&elanhw, sizeof(elanhw));
7221307576Sgonzo
7222307576Sgonzo	elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2];
7223307576Sgonzo	icversion = resp[0] & 0x0f;
7224307576Sgonzo	hwversion = ic2hw[icversion];
7225307576Sgonzo
7226307576Sgonzo	if (verbose >= 2)
7227307576Sgonzo		printf("Elantech touchpad hardware v.%d firmware v.0x%06x\n",
7228307576Sgonzo		    hwversion, elanhw.fwversion);
7229307576Sgonzo
7230307576Sgonzo	if (ELANTECH_HW_IS_V1(elanhw.fwversion)) {
7231307576Sgonzo		printf ("  Unsupported touchpad hardware (v1)\n");
7232307576Sgonzo		return (FALSE);
7233307576Sgonzo	}
7234307576Sgonzo	if (hwversion == 0) {
7235307576Sgonzo		printf ("  Unknown touchpad hardware (firmware v.0x%06x)\n",
7236307576Sgonzo		    elanhw.fwversion);
7237307576Sgonzo		return (FALSE);
7238307576Sgonzo	}
7239307576Sgonzo
7240307576Sgonzo	/* Get the Touchpad model information. */
7241307576Sgonzo	elanhw.hwversion = hwversion;
7242307576Sgonzo	elanhw.issemimt = hwversion == 2;
7243307576Sgonzo	elanhw.isclickpad = (resp[1] & 0x10) != 0;
7244307576Sgonzo	elanhw.hascrc = (resp[1] & 0x40) != 0;
7245307576Sgonzo	elanhw.haspressure = elanhw.fwversion >= 0x020800;
7246307576Sgonzo
7247307576Sgonzo	/* Read the capability bits. */
7248307576Sgonzo	if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) {
7249307576Sgonzo		printf("  Failed to read capability bits\n");
7250307576Sgonzo		return (FALSE);
7251307576Sgonzo	}
7252307576Sgonzo
7253318500Swulf	elanhw.ntracesx = imax(resp[1], 3);
7254318500Swulf	elanhw.ntracesy = imax(resp[2], 3);
7255307576Sgonzo	elanhw.hastrackpoint = (resp[0] & 0x80) != 0;
7256307576Sgonzo
7257307576Sgonzo	/* Get the touchpad resolution */
7258307576Sgonzo	switch (hwversion) {
7259307576Sgonzo	case 4:
7260307576Sgonzo		if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp)
7261307576Sgonzo		    == 0) {
7262307576Sgonzo			dpix = (resp[1] & 0x0f) * 10 + 790;
7263307576Sgonzo			dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790;
7264307576Sgonzo			elanhw.dpmmx = (dpix * 10 + 5) / 254;
7265307576Sgonzo			elanhw.dpmmy = (dpiy * 10 + 5) / 254;
7266307576Sgonzo			break;
7267307576Sgonzo		}
7268307576Sgonzo		/* FALLTHROUGH */
7269307576Sgonzo	case 2:
7270307576Sgonzo	case 3:
7271307576Sgonzo		elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */
7272307576Sgonzo		break;
7273307576Sgonzo	}
7274307576Sgonzo
7275307576Sgonzo	if (!elantech_support)
7276307576Sgonzo		return (FALSE);
7277307576Sgonzo
7278307576Sgonzo	if (elantech_init(kbdc, &elanhw)) {
7279307576Sgonzo		printf("couldn't initialize elantech touchpad\n");
7280307576Sgonzo		return (FALSE);
7281307576Sgonzo	}
7282307576Sgonzo
7283307576Sgonzo	/*
7284307576Sgonzo	 * Get the touchpad reporting range.
7285307576Sgonzo	 * On HW v.3 touchpads it should be done after switching hardware
7286307576Sgonzo	 * to real resolution mode (by setting bit 3 of reg10)
7287307576Sgonzo	 */
7288318500Swulf	elanhw.dptracex = elanhw.dptracey = 64;
7289318500Swulf	for (i = 0; i < nitems(fw_sizes); i++) {
7290318500Swulf		if (elanhw.fwversion == fw_sizes[i][0]) {
7291318500Swulf			elanhw.sizex = fw_sizes[i][1];
7292318500Swulf			elanhw.sizey = fw_sizes[i][2];
7293318500Swulf			goto found;
7294318500Swulf		}
7295318500Swulf	}
7296307576Sgonzo	if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) {
7297307576Sgonzo		printf("  Failed to read touchpad size\n");
7298307576Sgonzo		elanhw.sizex = 10000; /* Arbitrary high values to     */
7299307576Sgonzo		elanhw.sizey = 10000; /* prevent clipping in smoother */
7300307576Sgonzo	} else if (hwversion == 2) {
7301307576Sgonzo		if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) &&
7302307576Sgonzo		    !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) {
7303318500Swulf			elanhw.dptracex = resp[1] / 2;
7304318500Swulf			elanhw.dptracey = resp[2] / 2;
7305307576Sgonzo		}
7306318500Swulf		xtr = ((elanhw.fwversion >> 8) == 0x0208) ? 1 : 2;
7307318500Swulf		elanhw.sizex = (elanhw.ntracesx - xtr) * elanhw.dptracex;
7308318500Swulf		elanhw.sizey = (elanhw.ntracesy - xtr) * elanhw.dptracey;
7309307576Sgonzo	} else {
7310307576Sgonzo		elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1];
7311307576Sgonzo		elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2];
7312318500Swulf		xtr = (elanhw.sizex % (elanhw.ntracesx - 2) == 0) ? 2 : 1;
7313318500Swulf		elanhw.dptracex = elanhw.sizex / (elanhw.ntracesx - xtr);
7314318500Swulf		elanhw.dptracey = elanhw.sizey / (elanhw.ntracesy - xtr);
7315307576Sgonzo	}
7316318500Swulffound:
7317307576Sgonzo	if (verbose >= 2) {
7318307576Sgonzo		printf("  Model information:\n");
7319307576Sgonzo		printf("   MaxX:       %d\n", elanhw.sizex);
7320307576Sgonzo		printf("   MaxY:       %d\n", elanhw.sizey);
7321307576Sgonzo		printf("   DpmmX:      %d\n", elanhw.dpmmx);
7322307576Sgonzo		printf("   DpmmY:      %d\n", elanhw.dpmmy);
7323307576Sgonzo		printf("   TracesX:    %d\n", elanhw.ntracesx);
7324307576Sgonzo		printf("   TracesY:    %d\n", elanhw.ntracesy);
7325318500Swulf		printf("   DptraceX:   %d\n", elanhw.dptracex);
7326318500Swulf		printf("   DptraceY:   %d\n", elanhw.dptracey);
7327307576Sgonzo		printf("   SemiMT:     %d\n", elanhw.issemimt);
7328307576Sgonzo		printf("   Clickpad:   %d\n", elanhw.isclickpad);
7329307576Sgonzo		printf("   Trackpoint: %d\n", elanhw.hastrackpoint);
7330307576Sgonzo		printf("   CRC:        %d\n", elanhw.hascrc);
7331307576Sgonzo		printf("   Pressure:   %d\n", elanhw.haspressure);
7332307576Sgonzo	}
7333307576Sgonzo
7334307576Sgonzo	VLOG(3, (LOG_DEBUG, "elantech: END init\n"));
7335307576Sgonzo
7336307576Sgonzo	if (arg == PROBE) {
7337307576Sgonzo		sc->elanhw = elanhw;
7338307576Sgonzo		sc->hw.buttons = 3;
7339307576Sgonzo
7340307576Sgonzo		/* Initialize synaptics movement smoother */
7341307576Sgonzo		elantech_init_synaptics(sc);
7342307576Sgonzo
7343307576Sgonzo		for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
7344307576Sgonzo			PSM_FINGER_RESET(sc->elanaction.fingers[id]);
7345307576Sgonzo	}
7346307576Sgonzo
7347307576Sgonzo	return (TRUE);
7348307576Sgonzo}
7349307576Sgonzo
7350147271Smarius/*
7351147271Smarius * Return true if 'now' is earlier than (start + (secs.usecs)).
7352147271Smarius * Now may be NULL and the function will fetch the current time from
7353147271Smarius * getmicrouptime(), or a cached 'now' can be passed in.
7354147271Smarius * All values should be numbers derived from getmicrouptime().
7355147271Smarius */
735641016Sdfrstatic int
7357147271Smariustimeelapsed(start, secs, usecs, now)
7358147271Smarius	const struct timeval *start, *now;
7359147271Smarius	int secs, usecs;
7360147271Smarius{
7361147271Smarius	struct timeval snow, tv;
7362147271Smarius
7363147271Smarius	/* if there is no 'now' passed in, the get it as a convience. */
7364147271Smarius	if (now == NULL) {
7365147271Smarius		getmicrouptime(&snow);
7366147271Smarius		now = &snow;
7367147271Smarius	}
7368178019Sjkim
7369147271Smarius	tv.tv_sec = secs;
7370147271Smarius	tv.tv_usec = usecs;
7371147271Smarius	timevaladd(&tv, start);
7372147271Smarius	return (timevalcmp(&tv, now, <));
7373147271Smarius}
7374147271Smarius
7375147271Smariusstatic int
737654629Syokotapsmresume(device_t dev)
737741016Sdfr{
7378178019Sjkim	struct psm_softc *sc = device_get_softc(dev);
7379178019Sjkim	int unit = device_get_unit(dev);
7380178019Sjkim	int err;
738141016Sdfr
7382178019Sjkim	VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
738341016Sdfr
7384233580Sjkim	if ((sc->config &
7385233580Sjkim	    (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
7386178019Sjkim		return (0);
738758230Syokota
7388178019Sjkim	err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
738941016Sdfr
7390178019Sjkim	if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
7391178019Sjkim		/*
7392178019Sjkim		 * Release the blocked process; it must be notified that
7393178019Sjkim		 * the device cannot be accessed anymore.
7394178019Sjkim		 */
7395178019Sjkim		sc->state &= ~PSM_ASLP;
7396178019Sjkim		wakeup(sc);
7397178019Sjkim	}
739841016Sdfr
7399178019Sjkim	VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
740041016Sdfr
7401178019Sjkim	return (err);
740241016Sdfr}
740341016Sdfr
740452997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
7405321060Swulf#ifdef EVDEV_SUPPORT
7406321060SwulfMODULE_DEPEND(psm, evdev, 1, 1, 1);
7407321060Swulf#endif
740883147Syokota
7409147271Smarius#ifdef DEV_ISA
7410147271Smarius
741183147Syokota/*
741283147Syokota * This sucks up assignments from PNPBIOS and ACPI.
741383147Syokota */
741483147Syokota
741583931Syokota/*
741683931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
741783931Syokota * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
741883931Syokota * can be probed and attached only after the AT keyboard controller is
741983931Syokota * attached, we shall quietly reserve the IRQ resource for later use.
742083931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
742183931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging
742283931Syokota * under the keyboard controller, then probe and attach it.
742383931Syokota */
742483147Syokota
742583147Syokotastatic	devclass_t			psmcpnp_devclass;
742683147Syokota
742783147Syokotastatic	device_probe_t			psmcpnp_probe;
742883147Syokotastatic	device_attach_t			psmcpnp_attach;
742983147Syokota
743083147Syokotastatic device_method_t psmcpnp_methods[] = {
743183147Syokota	DEVMETHOD(device_probe,		psmcpnp_probe),
743283147Syokota	DEVMETHOD(device_attach,	psmcpnp_attach),
7433178019Sjkim
743483147Syokota	{ 0, 0 }
743583147Syokota};
743683147Syokota
743783147Syokotastatic driver_t psmcpnp_driver = {
743883147Syokota	PSMCPNP_DRIVER_NAME,
743983147Syokota	psmcpnp_methods,
7440331170Seadler	sizeof(struct psmcpnp_softc),
744183147Syokota};
744283147Syokota
744383147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = {
744488188Ssheldonh	{ 0x030fd041, "PS/2 mouse port" },		/* PNP0F03 */
7445156730Stakawata	{ 0x0e0fd041, "PS/2 mouse port" },		/* PNP0F0E */
7446156730Stakawata	{ 0x120fd041, "PS/2 mouse port" },		/* PNP0F12 */
744783147Syokota	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
744883147Syokota	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
7449117117Smikeh	{ 0x02002e4f, "Dell PS/2 mouse port" },		/* Lat. X200, Dell */
7450156730Stakawata	{ 0x0002a906, "ALPS Glide Point" },		/* ALPS Glide Point */
745183492Syokota	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
745284407Stakawata	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
7453109679Shsu	{ 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
7454109679Shsu	{ 0x0290d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9002, Vaio */
7455109710Smarcel	{ 0x0390d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9003, Vaio */
7456109679Shsu	{ 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
745783147Syokota	{ 0 }
745883147Syokota};
745983147Syokota
7460331170Seadler/* _HID list for quirk detection. Any device below has _CID from psmcpnp_ids */
7461344165Swulfstatic struct isa_pnp_id topbtpad_ids[] = {
7462344165Swulf	{ 0x1700ae30, "Lenovo PS/2 clickpad port" },	/* LEN0017, ThinkPad */
7463344165Swulf	{ 0x1800ae30, "Lenovo PS/2 clickpad port" },	/* LEN0018, ThinkPad */
7464344165Swulf	{ 0x1900ae30, "Lenovo PS/2 clickpad port" },	/* LEN0019, ThinkPad */
7465344165Swulf	{ 0x2300ae30, "Lenovo PS/2 clickpad port" },	/* LEN0023, ThinkPad */
7466344165Swulf	{ 0x2a00ae30, "Lenovo PS/2 clickpad port" },	/* LEN002a, ThinkPad */
7467344165Swulf	{ 0x2b00ae30, "Lenovo PS/2 clickpad port" },	/* LEN002b, ThinkPad */
7468344165Swulf	{ 0x2c00ae30, "Lenovo PS/2 clickpad port" },	/* LEN002c, ThinkPad */
7469344165Swulf	{ 0x2d00ae30, "Lenovo PS/2 clickpad port" },	/* LEN002d, ThinkPad */
7470344165Swulf	{ 0x2e00ae30, "Lenovo PS/2 clickpad port" },	/* LEN002e, ThinkPad */
7471344165Swulf	{ 0x3300ae30, "Lenovo PS/2 clickpad port" },	/* LEN0033, ThinkPad */
7472344165Swulf	{ 0x3400ae30, "Lenovo PS/2 clickpad port" },	/* LEN0034, ThinkPad */
7473344165Swulf	{ 0x3500ae30, "Lenovo PS/2 clickpad port" },	/* LEN0035, ThinkPad */
7474344165Swulf	{ 0x3600ae30, "Lenovo PS/2 clickpad port" },	/* LEN0036, ThinkPad */
7475344165Swulf	{ 0x3700ae30, "Lenovo PS/2 clickpad port" },	/* LEN0037, ThinkPad */
7476344165Swulf	{ 0x3800ae30, "Lenovo PS/2 clickpad port" },	/* LEN0038, ThinkPad */
7477344165Swulf	{ 0x3900ae30, "Lenovo PS/2 clickpad port" },	/* LEN0039, ThinkPad */
7478344165Swulf	{ 0x4100ae30, "Lenovo PS/2 clickpad port" },	/* LEN0041, ThinkPad */
7479344165Swulf	{ 0x4200ae30, "Lenovo PS/2 clickpad port" },	/* LEN0042, ThinkPad */
7480344165Swulf	{ 0x4500ae30, "Lenovo PS/2 clickpad port" },	/* LEN0045, ThinkPad */
7481344165Swulf	{ 0x4700ae30, "Lenovo PS/2 clickpad port" },	/* LEN0047, ThinkPad */
7482344165Swulf	{ 0x4900ae30, "Lenovo PS/2 clickpad port" },	/* LEN0049, ThinkPad */
7483344165Swulf	{ 0x0020ae30, "Lenovo PS/2 clickpad port" },	/* LEN2000, ThinkPad */
7484344165Swulf	{ 0x0120ae30, "Lenovo PS/2 clickpad port" },	/* LEN2001, ThinkPad */
7485344165Swulf	{ 0x0220ae30, "Lenovo PS/2 clickpad port" },	/* LEN2002, ThinkPad */
7486344165Swulf	{ 0x0320ae30, "Lenovo PS/2 clickpad port" },	/* LEN2003, ThinkPad */
7487344165Swulf	{ 0x0420ae30, "Lenovo PS/2 clickpad port" },	/* LEN2004, ThinkPad */
7488344165Swulf	{ 0x0520ae30, "Lenovo PS/2 clickpad port" },	/* LEN2005, ThinkPad */
7489344165Swulf	{ 0x0620ae30, "Lenovo PS/2 clickpad port" },	/* LEN2006, ThinkPad */
7490344165Swulf	{ 0x0720ae30, "Lenovo PS/2 clickpad port" },	/* LEN2007, ThinkPad */
7491344165Swulf	{ 0x0820ae30, "Lenovo PS/2 clickpad port" },	/* LEN2008, ThinkPad */
7492344165Swulf	{ 0x0920ae30, "Lenovo PS/2 clickpad port" },	/* LEN2009, ThinkPad */
7493344165Swulf	{ 0x0a20ae30, "Lenovo PS/2 clickpad port" },	/* LEN200a, ThinkPad */
7494344165Swulf	{ 0x0b20ae30, "Lenovo PS/2 clickpad port" },	/* LEN200b, ThinkPad */
7495344165Swulf	{ 0 }
7496344165Swulf};
7497344165Swulf
7498344165Swulf/* _HID list for quirk detection. Any device below has _CID from psmcpnp_ids */
7499331170Seadlerstatic struct isa_pnp_id forcepad_ids[] = {
7500331170Seadler	{ 0x0d302e4f, "HP PS/2 forcepad port" },	/* SYN300D, EB 1040 */
7501331170Seadler	{ 0x14302e4f, "HP PS/2 forcepad port" },	/* SYN3014, EB 1040 */
7502331170Seadler	{ 0 }
7503331170Seadler};
7504331170Seadler
750583147Syokotastatic int
750683147Syokotacreate_a_copy(device_t atkbdc, device_t me)
750783147Syokota{
750883147Syokota	device_t psm;
750983147Syokota	u_long irq;
751083147Syokota
751183931Syokota	/* find the PS/2 mouse device instance under the keyboard controller */
751283931Syokota	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
7513178019Sjkim	    device_get_unit(atkbdc));
751483147Syokota	if (psm == NULL)
7515178019Sjkim		return (ENXIO);
751683931Syokota	if (device_get_state(psm) != DS_NOTPRESENT)
7517178019Sjkim		return (0);
751883147Syokota
751983931Syokota	/* move our resource to the found device */
752083931Syokota	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
7521216491Sjhb	bus_delete_resource(me, SYS_RES_IRQ, 0);
752283147Syokota	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
752383147Syokota
752483147Syokota	/* ...then probe and attach it */
7525178019Sjkim	return (device_probe_and_attach(psm));
752683147Syokota}
752783147Syokota
752883147Syokotastatic int
752983147Syokotapsmcpnp_probe(device_t dev)
753083147Syokota{
7531331170Seadler	struct psmcpnp_softc *sc = device_get_softc(dev);
753283931Syokota	struct resource *res;
753383931Syokota	u_long irq;
753483931Syokota	int rid;
753583147Syokota
7536331170Seadler	if (ISA_PNP_PROBE(device_get_parent(dev), dev, forcepad_ids) == 0)
7537331170Seadler		sc->type = PSMCPNP_FORCEPAD;
7538344165Swulf	else if (ISA_PNP_PROBE(device_get_parent(dev), dev, topbtpad_ids) == 0)
7539344165Swulf		sc->type = PSMCPNP_TOPBUTTONPAD;
7540331170Seadler	else if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids) == 0)
7541331170Seadler		sc->type = PSMCPNP_GENERIC;
7542331170Seadler	else
7543178019Sjkim		return (ENXIO);
754483147Syokota
754583931Syokota	/*
754683931Syokota	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
754783931Syokota	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
754883931Syokota	 * declares the PS/2 mouse device node without an IRQ resource!
754983931Syokota	 * If this happens, we shall refer to device hints.
755083931Syokota	 * If we still don't find it there, use a hardcoded value... XXX
755183931Syokota	 */
755283931Syokota	rid = 0;
755383931Syokota	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
755483931Syokota	if (irq <= 0) {
755583931Syokota		if (resource_long_value(PSM_DRIVER_NAME,
7556178019Sjkim		    device_get_unit(dev),"irq", &irq) != 0)
755783931Syokota			irq = 12;	/* XXX */
755883931Syokota		device_printf(dev, "irq resource info is missing; "
7559178019Sjkim		    "assuming irq %ld\n", irq);
756083931Syokota		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
756183931Syokota	}
7562216491Sjhb	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0);
756383931Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
756483147Syokota
756583147Syokota	/* keep quiet */
756683147Syokota	if (!bootverbose)
756783147Syokota		device_quiet(dev);
756883492Syokota
756983931Syokota	return ((res == NULL) ? ENXIO : 0);
757083147Syokota}
757183147Syokota
757283147Syokotastatic int
757383147Syokotapsmcpnp_attach(device_t dev)
757483147Syokota{
757583492Syokota	device_t atkbdc;
757683147Syokota
757783931Syokota	/* find the keyboard controller, which may be on acpi* or isa* bus */
757883931Syokota	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
7579178019Sjkim	    device_get_unit(dev));
7580178019Sjkim	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
758183492Syokota		create_a_copy(atkbdc, dev);
758283492Syokota
7583178019Sjkim	return (0);
758483147Syokota}
758583147Syokota
758683147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
758783147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
7588147271Smarius
7589147271Smarius#endif /* DEV_ISA */
7590