psm.c revision 133918
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'.
5341016Sdfr *  - January/February 1997. Tweaked probe logic for
5441016Sdfr *    HiNote UltraII/Latitude/Armada laptops.
5541016Sdfr *  - 30 July 1997. Added APM support.
5641016Sdfr *  - 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: head/sys/dev/atkbdc/psm.c 133918 2004-08-17 18:12:37Z gibbs $");
63116181Sobrien
6441016Sdfr#include "opt_psm.h"
6541016Sdfr
6641016Sdfr#include <sys/param.h>
6741016Sdfr#include <sys/systm.h>
6841016Sdfr#include <sys/kernel.h>
6941016Sdfr#include <sys/module.h>
7041016Sdfr#include <sys/bus.h>
7141016Sdfr#include <sys/conf.h>
7241016Sdfr#include <sys/poll.h>
7341016Sdfr#include <sys/syslog.h>
7445720Speter#include <machine/bus.h>
7541181Sdfr#include <sys/rman.h>
7670834Swollman#include <sys/selinfo.h>
77123442Salfred#include <sys/sysctl.h>
7884880Syokota#include <sys/time.h>
7941016Sdfr#include <sys/uio.h>
8041016Sdfr
81114216Skan#include <sys/limits.h>
8266860Sphk#include <sys/mouse.h>
8341181Sdfr#include <machine/resource.h>
8441016Sdfr
8541016Sdfr#include <isa/isavar.h>
8643105Sdfr#include <dev/kbd/atkbdcreg.h>
8741016Sdfr
8841016Sdfr/*
8941016Sdfr * Driver specific options: the following options may be set by
9041016Sdfr * `options' statements in the kernel configuration file.
9141016Sdfr */
9241016Sdfr
9341016Sdfr/* debugging */
9441016Sdfr#ifndef PSM_DEBUG
9541016Sdfr#define PSM_DEBUG	0	/* logging: 0: none, 1: brief, 2: verbose */
9641016Sdfr#endif
9741016Sdfr
9884880Syokota#ifndef PSM_SYNCERR_THRESHOLD1
9984880Syokota#define PSM_SYNCERR_THRESHOLD1	20
10084880Syokota#endif
10184880Syokota
10284880Syokota#ifndef PSM_INPUT_TIMEOUT
10384880Syokota#define PSM_INPUT_TIMEOUT	2000000	/* 2 sec */
10484880Syokota#endif
10584880Syokota
106133297Sphilip#ifndef PSM_TAP_TIMEOUT
107133297Sphilip#define PSM_TAP_TIMEOUT		125000
108133297Sphilip#endif
109133297Sphilip
110133297Sphilip#ifndef PSM_TAP_THRESHOLD
111133297Sphilip#define PSM_TAP_THRESHOLD	25
112133297Sphilip#endif
113133297Sphilip
11441016Sdfr/* end of driver specific options */
11541016Sdfr
11683492Syokota#define PSM_DRIVER_NAME		"psm"
11783931Syokota#define PSMCPNP_DRIVER_NAME	"psmcpnp"
11883492Syokota
11941016Sdfr/* input queue */
12041016Sdfr#define PSM_BUFSIZE		960
12141016Sdfr#define PSM_SMALLBUFSIZE	240
12241016Sdfr
12341016Sdfr/* operation levels */
12441016Sdfr#define PSM_LEVEL_BASE		0
12541016Sdfr#define PSM_LEVEL_STANDARD	1
12641016Sdfr#define PSM_LEVEL_NATIVE	2
12741016Sdfr#define PSM_LEVEL_MIN		PSM_LEVEL_BASE
12841016Sdfr#define PSM_LEVEL_MAX		PSM_LEVEL_NATIVE
12941016Sdfr
13048778Syokota/* Logitech PS2++ protocol */
13148778Syokota#define MOUSE_PS2PLUS_CHECKBITS(b)	\
13248778Syokota				((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
13348778Syokota#define MOUSE_PS2PLUS_PACKET_TYPE(b)	\
13448778Syokota				(((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
13548778Syokota
13641016Sdfr/* some macros */
13741016Sdfr#define PSM_UNIT(dev)		(minor(dev) >> 1)
13841016Sdfr#define PSM_NBLOCKIO(dev)	(minor(dev) & 1)
13941016Sdfr#define PSM_MKMINOR(unit,block)	(((unit) << 1) | ((block) ? 0:1))
14041016Sdfr
14141016Sdfr/* ring buffer */
14241016Sdfrtypedef struct ringbuf {
14341016Sdfr    int           count;	/* # of valid elements in the buffer */
14441016Sdfr    int           head;		/* head pointer */
14541016Sdfr    int           tail;		/* tail poiner */
14641016Sdfr    unsigned char buf[PSM_BUFSIZE];
14741016Sdfr} ringbuf_t;
14841016Sdfr
149123442Salfred/* data buffer */
150123442Salfredtypedef struct packetbuf {
151123442Salfred    unsigned char ipacket[16];	/* interim input buffer */
152123442Salfred    int           inputbytes;	/* # of bytes in the input buffer */
153123442Salfred} packetbuf_t;
154123442Salfred
155123442Salfred#ifndef PSM_PACKETQUEUE
156123442Salfred#define PSM_PACKETQUEUE	128
157123442Salfred#endif
158123442Salfred
15941016Sdfr/* driver control block */
16041016Sdfrstruct psm_softc {		/* Driver status information */
16184880Syokota    int		  unit;
16241016Sdfr    struct selinfo rsel;	/* Process selecting for Input */
16341016Sdfr    unsigned char state;	/* Mouse driver state */
16441016Sdfr    int           config;	/* driver configuration flags */
16541016Sdfr    int           flags;	/* other flags */
16641016Sdfr    KBDC          kbdc;		/* handle to access the keyboard controller */
16758230Syokota    struct resource *intr;	/* IRQ resource */
16858230Syokota    void	  *ih;		/* interrupt handle */
16941016Sdfr    mousehw_t     hw;		/* hardware information */
170132865Snjl    synapticshw_t synhw;	/* Synaptics-specific hardware information */
17141016Sdfr    mousemode_t   mode;		/* operation mode */
17241016Sdfr    mousemode_t   dflt_mode;	/* default operation mode */
17341016Sdfr    mousestatus_t status;	/* accumulated mouse movement */
17441016Sdfr    ringbuf_t     queue;	/* mouse status queue */
175123442Salfred    packetbuf_t   pqueue[PSM_PACKETQUEUE];	/* mouse data queue */
176123442Salfred    int           pqueue_start; /* start of data in queue */
177123442Salfred    int           pqueue_end;   /* end of data in queue */
17841016Sdfr    int           button;	/* the latest button state */
17949965Syokota    int		  xold;	/* previous absolute X position */
18049965Syokota    int		  yold;	/* previous absolute Y position */
181133297Sphilip    int		  zmax;	/* maximum pressure value for touchpads */
182123442Salfred    int		  syncerrors; /* XXX: KILL ME! */
18384880Syokota    struct timeval inputtimeout;
184123442Salfred    struct timeval lastsoftintr;	/* time of last soft interrupt */
185123442Salfred    struct timeval lastinputerr;	/* time last sync error happened */
186133297Sphilip    struct timeval taptimeout;		/* tap timeout for touchpads */
18758230Syokota    int		  watchdog;	/* watchdog timer flag */
18858230Syokota    struct callout_handle callout;	/* watchdog timer call out */
189123442Salfred    struct callout_handle softcallout;	/* buffer timer call out */
190130585Sphk    struct cdev *dev;
191130585Sphk    struct cdev *bdev;
192123442Salfred    int           lasterr;
193123442Salfred    int           cmdcount;
19441016Sdfr};
195114293Smarkmstatic devclass_t psm_devclass;
19641016Sdfr#define PSM_SOFTC(unit)	((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
19741016Sdfr
19841016Sdfr/* driver state flags (state) */
19941016Sdfr#define PSM_VALID		0x80
20041016Sdfr#define PSM_OPEN		1	/* Device is open */
20141016Sdfr#define PSM_ASLP		2	/* Waiting for mouse data */
202123442Salfred#define PSM_SOFTARMED		4	/* Software interrupt armed */
203133918Sgibbs#define PSM_NEED_SYNCBITS	8	/* Set syncbits using next data pkt */
20441016Sdfr
20541016Sdfr/* driver configuration flags (config) */
20641016Sdfr#define PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
20741016Sdfr#define PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
20841016Sdfr#define PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
20945789Speter#define PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
21045789Speter#define PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
21145789Speter#define PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
21245789Speter#define PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
21358230Syokota#define PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
21458230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
21569439Syokota#define PSM_CONFIG_SYNCHACK	0x8000 /* enable `out-of-sync' hack */
21641016Sdfr
21741016Sdfr#define PSM_CONFIG_FLAGS	(PSM_CONFIG_RESOLUTION 		\
21841016Sdfr				    | PSM_CONFIG_ACCEL		\
21945789Speter				    | PSM_CONFIG_NOCHECKSYNC	\
22069439Syokota				    | PSM_CONFIG_SYNCHACK	\
22145789Speter				    | PSM_CONFIG_NOIDPROBE	\
22245789Speter				    | PSM_CONFIG_NORESET	\
22345789Speter				    | PSM_CONFIG_FORCETAP	\
22458230Syokota				    | PSM_CONFIG_IGNPORTERROR	\
22558230Syokota				    | PSM_CONFIG_HOOKRESUME	\
22658230Syokota				    | PSM_CONFIG_INITAFTERSUSPEND)
22741016Sdfr
22841016Sdfr/* other flags (flags) */
22949965Syokota#define PSM_FLAGS_FINGERDOWN	0x0001 /* VersaPad finger down */
23041016Sdfr
23141016Sdfr/* for backward compatibility */
23241016Sdfr#define OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
23341016Sdfr#define OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
23441016Sdfr#define OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
23541016Sdfr
23641016Sdfrtypedef struct old_mousehw {
23741016Sdfr    int buttons;
23841016Sdfr    int iftype;
23941016Sdfr    int type;
24041016Sdfr    int hwid;
24141016Sdfr} old_mousehw_t;
24241016Sdfr
24341016Sdfrtypedef struct old_mousemode {
24441016Sdfr    int protocol;
24541016Sdfr    int rate;
24641016Sdfr    int resolution;
24741016Sdfr    int accelfactor;
24841016Sdfr} old_mousemode_t;
24941016Sdfr
25041016Sdfr/* packet formatting function */
25192756Salfredtypedef int packetfunc_t(struct psm_softc *, unsigned char *,
25292756Salfred			      int *, int, mousestatus_t *);
25341016Sdfr
25441016Sdfr/* function prototypes */
25592756Salfredstatic void psmidentify(driver_t *, device_t);
25692756Salfredstatic int psmprobe(device_t);
25792756Salfredstatic int psmattach(device_t);
25892756Salfredstatic int psmdetach(device_t);
25992756Salfredstatic int psmresume(device_t);
26041016Sdfr
26141016Sdfrstatic d_open_t psmopen;
26241016Sdfrstatic d_close_t psmclose;
26341016Sdfrstatic d_read_t psmread;
26441016Sdfrstatic d_ioctl_t psmioctl;
26541016Sdfrstatic d_poll_t psmpoll;
26641016Sdfr
26792756Salfredstatic int enable_aux_dev(KBDC);
26892756Salfredstatic int disable_aux_dev(KBDC);
26992756Salfredstatic int get_mouse_status(KBDC, int *, int, int);
27092756Salfredstatic int get_aux_id(KBDC);
27192756Salfredstatic int set_mouse_sampling_rate(KBDC, int);
27292756Salfredstatic int set_mouse_scaling(KBDC, int);
27392756Salfredstatic int set_mouse_resolution(KBDC, int);
27492756Salfredstatic int set_mouse_mode(KBDC);
27592756Salfredstatic int get_mouse_buttons(KBDC);
27692756Salfredstatic int is_a_mouse(int);
27792756Salfredstatic void recover_from_error(KBDC);
27892756Salfredstatic int restore_controller(KBDC, int);
27992756Salfredstatic int doinitialize(struct psm_softc *, mousemode_t *);
28092756Salfredstatic int doopen(struct psm_softc *, int);
28192756Salfredstatic int reinitialize(struct psm_softc *, int);
28292756Salfredstatic char *model_name(int);
283123442Salfredstatic void psmsoftintr(void *);
28492756Salfredstatic void psmintr(void *);
28592756Salfredstatic void psmtimeout(void *);
286123442Salfredstatic int timeelapsed(const struct timeval *,
287123442Salfred    int, int, const struct timeval *);
288123442Salfredstatic void dropqueue(struct psm_softc *);
289123442Salfredstatic void flushpackets(struct psm_softc *);
29041016Sdfr
29141016Sdfr/* vendor specific features */
29292756Salfredtypedef int probefunc_t(struct psm_softc *);
29341016Sdfr
29492756Salfredstatic int mouse_id_proc1(KBDC, int, int, int *);
29592756Salfredstatic int mouse_ext_command(KBDC, int);
29641016Sdfrstatic probefunc_t enable_groller;
29741016Sdfrstatic probefunc_t enable_gmouse;
29841016Sdfrstatic probefunc_t enable_aglide;
29941016Sdfrstatic probefunc_t enable_kmouse;
30058230Syokotastatic probefunc_t enable_msexplorer;
30141016Sdfrstatic probefunc_t enable_msintelli;
30258230Syokotastatic probefunc_t enable_4dmouse;
30358230Syokotastatic probefunc_t enable_4dplus;
30441016Sdfrstatic probefunc_t enable_mmanplus;
305132865Snjlstatic probefunc_t enable_synaptics;
30649965Syokotastatic probefunc_t enable_versapad;
307123442Salfredstatic int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, unsigned char *);
30841016Sdfr
30941016Sdfrstatic struct {
31041016Sdfr    int                 model;
31141016Sdfr    unsigned char	syncmask;
31241016Sdfr    int 		packetsize;
31341016Sdfr    probefunc_t 	*probefunc;
31441016Sdfr} vendortype[] = {
31558230Syokota    /*
31658230Syokota     * WARNING: the order of probe is very important.  Don't mess it
31758230Syokota     * unless you know what you are doing.
31858230Syokota     */
31941016Sdfr    { MOUSE_MODEL_NET,			/* Genius NetMouse */
32058230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
32141016Sdfr    { MOUSE_MODEL_NETSCROLL,		/* Genius NetScroll */
32241016Sdfr      0xc8, 6, enable_groller, },
32358230Syokota    { MOUSE_MODEL_MOUSEMANPLUS,		/* Logitech MouseMan+ */
32458230Syokota      0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
32558230Syokota    { MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
32658230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
32758230Syokota    { MOUSE_MODEL_4D,			/* A4 Tech 4D Mouse */
32858230Syokota      0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
32958230Syokota    { MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
33058230Syokota      0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
33158230Syokota    { MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
33258230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
33341016Sdfr    { MOUSE_MODEL_GLIDEPOINT,		/* ALPS GlidePoint */
33441016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
33541016Sdfr    { MOUSE_MODEL_THINK,		/* Kensignton ThinkingMouse */
33641016Sdfr      0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
33749965Syokota    { MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
33849965Syokota      0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
339132865Snjl    { MOUSE_MODEL_SYNAPTICS,		/* Synaptics Touchpad */
340132865Snjl      0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics, },
34141016Sdfr    { MOUSE_MODEL_GENERIC,
34241016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
34341016Sdfr};
34463951Syokota#define GENERIC_MOUSE_ENTRY	((sizeof(vendortype) / sizeof(*vendortype)) - 1)
34541016Sdfr
34641016Sdfr/* device driver declarateion */
34741016Sdfrstatic device_method_t psm_methods[] = {
34841016Sdfr	/* Device interface */
34983147Syokota	DEVMETHOD(device_identify,	psmidentify),
35041016Sdfr	DEVMETHOD(device_probe,		psmprobe),
35141016Sdfr	DEVMETHOD(device_attach,	psmattach),
35258230Syokota	DEVMETHOD(device_detach,	psmdetach),
35354629Syokota	DEVMETHOD(device_resume,	psmresume),
35441016Sdfr
35541016Sdfr	{ 0, 0 }
35641016Sdfr};
35741016Sdfr
35841016Sdfrstatic driver_t psm_driver = {
35983492Syokota    PSM_DRIVER_NAME,
36041016Sdfr    psm_methods,
36141016Sdfr    sizeof(struct psm_softc),
36241016Sdfr};
36341016Sdfr
36441016Sdfr
36547625Sphkstatic struct cdevsw psm_cdevsw = {
366126080Sphk	.d_version =	D_VERSION,
367126080Sphk	.d_flags =	D_NEEDGIANT,
368111815Sphk	.d_open =	psmopen,
369111815Sphk	.d_close =	psmclose,
370111815Sphk	.d_read =	psmread,
371111815Sphk	.d_ioctl =	psmioctl,
372111815Sphk	.d_poll =	psmpoll,
373111815Sphk	.d_name =	PSM_DRIVER_NAME,
37441016Sdfr};
37541016Sdfr
37641016Sdfr/* debug message level */
37741016Sdfrstatic int verbose = PSM_DEBUG;
37841016Sdfr
37941016Sdfr/* device I/O routines */
38041016Sdfrstatic int
38141016Sdfrenable_aux_dev(KBDC kbdc)
38241016Sdfr{
38341016Sdfr    int res;
38441016Sdfr
38541016Sdfr    res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
38641016Sdfr    if (verbose >= 2)
38741016Sdfr        log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
38841016Sdfr
38941016Sdfr    return (res == PSM_ACK);
39041016Sdfr}
39141016Sdfr
39241016Sdfrstatic int
39341016Sdfrdisable_aux_dev(KBDC kbdc)
39441016Sdfr{
39541016Sdfr    int res;
39641016Sdfr
39741016Sdfr    res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
39841016Sdfr    if (verbose >= 2)
39941016Sdfr        log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
40041016Sdfr
40141016Sdfr    return (res == PSM_ACK);
40241016Sdfr}
40341016Sdfr
40441016Sdfrstatic int
40541016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
40641016Sdfr{
40741016Sdfr    int cmd;
40841016Sdfr    int res;
40941016Sdfr    int i;
41041016Sdfr
41141016Sdfr    switch (flag) {
41241016Sdfr    case 0:
41341016Sdfr    default:
41441016Sdfr	cmd = PSMC_SEND_DEV_STATUS;
41541016Sdfr	break;
41641016Sdfr    case 1:
41741016Sdfr	cmd = PSMC_SEND_DEV_DATA;
41841016Sdfr	break;
41941016Sdfr    }
42041016Sdfr    empty_aux_buffer(kbdc, 5);
42141016Sdfr    res = send_aux_command(kbdc, cmd);
42241016Sdfr    if (verbose >= 2)
42341016Sdfr        log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
42441016Sdfr	    (flag == 1) ? "DATA" : "STATUS", res);
42541016Sdfr    if (res != PSM_ACK)
42641016Sdfr        return 0;
42741016Sdfr
42841016Sdfr    for (i = 0; i < len; ++i) {
42941016Sdfr        status[i] = read_aux_data(kbdc);
43041016Sdfr	if (status[i] < 0)
43141016Sdfr	    break;
43241016Sdfr    }
43341016Sdfr
43441016Sdfr    if (verbose) {
43541016Sdfr        log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
43641016Sdfr            (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
43741016Sdfr    }
43841016Sdfr
43941016Sdfr    return i;
44041016Sdfr}
44141016Sdfr
44241016Sdfrstatic int
44341016Sdfrget_aux_id(KBDC kbdc)
44441016Sdfr{
44541016Sdfr    int res;
44641016Sdfr    int id;
44741016Sdfr
44841016Sdfr    empty_aux_buffer(kbdc, 5);
44941016Sdfr    res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
45041016Sdfr    if (verbose >= 2)
45141016Sdfr        log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
45241016Sdfr    if (res != PSM_ACK)
45341016Sdfr	return (-1);
45441016Sdfr
45541016Sdfr    /* 10ms delay */
45641016Sdfr    DELAY(10000);
45741016Sdfr
45841016Sdfr    id = read_aux_data(kbdc);
45941016Sdfr    if (verbose >= 2)
46041016Sdfr        log(LOG_DEBUG, "psm: device ID: %04x\n", id);
46141016Sdfr
46241016Sdfr    return id;
46341016Sdfr}
46441016Sdfr
46541016Sdfrstatic int
46641016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
46741016Sdfr{
46841016Sdfr    int res;
46941016Sdfr
47041016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
47141016Sdfr    if (verbose >= 2)
47241016Sdfr        log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
47341016Sdfr
47441016Sdfr    return ((res == PSM_ACK) ? rate : -1);
47541016Sdfr}
47641016Sdfr
47741016Sdfrstatic int
47841016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
47941016Sdfr{
48041016Sdfr    int res;
48141016Sdfr
48241016Sdfr    switch (scale) {
48341016Sdfr    case 1:
48441016Sdfr    default:
48541016Sdfr	scale = PSMC_SET_SCALING11;
48641016Sdfr	break;
48741016Sdfr    case 2:
48841016Sdfr	scale = PSMC_SET_SCALING21;
48941016Sdfr	break;
49041016Sdfr    }
49141016Sdfr    res = send_aux_command(kbdc, scale);
49241016Sdfr    if (verbose >= 2)
49341016Sdfr        log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
49441016Sdfr	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
49541016Sdfr
49641016Sdfr    return (res == PSM_ACK);
49741016Sdfr}
49841016Sdfr
49941016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
50041016Sdfrstatic int
50141016Sdfrset_mouse_resolution(KBDC kbdc, int val)
50241016Sdfr{
50341016Sdfr    int res;
50441016Sdfr
50541016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
50641016Sdfr    if (verbose >= 2)
50741016Sdfr        log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
50841016Sdfr
50941016Sdfr    return ((res == PSM_ACK) ? val : -1);
51041016Sdfr}
51141016Sdfr
51241016Sdfr/*
51341016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
51441016Sdfr * re-enabled by calling `enable_aux_dev()'
51541016Sdfr */
51641016Sdfrstatic int
51741016Sdfrset_mouse_mode(KBDC kbdc)
51841016Sdfr{
51941016Sdfr    int res;
52041016Sdfr
52141016Sdfr    res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
52241016Sdfr    if (verbose >= 2)
52341016Sdfr        log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
52441016Sdfr
52541016Sdfr    return (res == PSM_ACK);
52641016Sdfr}
52741016Sdfr
52841016Sdfrstatic int
52941016Sdfrget_mouse_buttons(KBDC kbdc)
53041016Sdfr{
53141016Sdfr    int c = 2;		/* assume two buttons by default */
53241016Sdfr    int status[3];
53341016Sdfr
53441016Sdfr    /*
53541016Sdfr     * NOTE: a special sequence to obtain Logitech Mouse specific
53641016Sdfr     * information: set resolution to 25 ppi, set scaling to 1:1, set
53741016Sdfr     * scaling to 1:1, set scaling to 1:1. Then the second byte of the
53841016Sdfr     * mouse status bytes is the number of available buttons.
53941016Sdfr     * Some manufactures also support this sequence.
54041016Sdfr     */
54141016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
54241016Sdfr        return c;
54341016Sdfr    if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
54441016Sdfr        && set_mouse_scaling(kbdc, 1)
54541016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
54641016Sdfr        if (status[1] != 0)
54741016Sdfr            return status[1];
54841016Sdfr    }
54941016Sdfr    return c;
55041016Sdfr}
55141016Sdfr
55241016Sdfr/* misc subroutines */
55341016Sdfr/*
55441016Sdfr * Someday, I will get the complete list of valid pointing devices and
55541016Sdfr * their IDs... XXX
55641016Sdfr */
55741016Sdfrstatic int
55841016Sdfris_a_mouse(int id)
55941016Sdfr{
56041016Sdfr#if 0
56141016Sdfr    static int valid_ids[] = {
56241016Sdfr        PSM_MOUSE_ID,		/* mouse */
56341016Sdfr        PSM_BALLPOINT_ID,	/* ballpoint device */
56441016Sdfr        PSM_INTELLI_ID,		/* Intellimouse */
56558230Syokota        PSM_EXPLORER_ID,	/* Intellimouse Explorer */
56641016Sdfr        -1			/* end of table */
56741016Sdfr    };
56841016Sdfr    int i;
56941016Sdfr
57041016Sdfr    for (i = 0; valid_ids[i] >= 0; ++i)
57141016Sdfr        if (valid_ids[i] == id)
57241016Sdfr            return TRUE;
57341016Sdfr    return FALSE;
57441016Sdfr#else
57541016Sdfr    return TRUE;
57641016Sdfr#endif
57741016Sdfr}
57841016Sdfr
57941016Sdfrstatic char *
58041016Sdfrmodel_name(int model)
58141016Sdfr{
58241016Sdfr    static struct {
58341016Sdfr	int model_code;
58441016Sdfr	char *model_name;
58541016Sdfr    } models[] = {
58658230Syokota        { MOUSE_MODEL_NETSCROLL,	"NetScroll" },
58758230Syokota        { MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
58841016Sdfr        { MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
58941016Sdfr        { MOUSE_MODEL_THINK,		"ThinkingMouse" },
59041016Sdfr        { MOUSE_MODEL_INTELLI,		"IntelliMouse" },
59141016Sdfr        { MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
59249965Syokota        { MOUSE_MODEL_VERSAPAD,		"VersaPad" },
59358230Syokota        { MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
59458230Syokota        { MOUSE_MODEL_4D,		"4D Mouse" },
59558230Syokota        { MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
596132865Snjl        { MOUSE_MODEL_SYNAPTICS,	"Synaptics Touchpad" },
59741016Sdfr        { MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
59841016Sdfr        { MOUSE_MODEL_UNKNOWN,		NULL },
59941016Sdfr    };
60041016Sdfr    int i;
60141016Sdfr
60241016Sdfr    for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
60341016Sdfr	if (models[i].model_code == model)
60441016Sdfr	    return models[i].model_name;
60541016Sdfr    }
60641016Sdfr    return "Unknown";
60741016Sdfr}
60841016Sdfr
60941016Sdfrstatic void
61041016Sdfrrecover_from_error(KBDC kbdc)
61141016Sdfr{
61241016Sdfr    /* discard anything left in the output buffer */
61341016Sdfr    empty_both_buffers(kbdc, 10);
61441016Sdfr
61541016Sdfr#if 0
61641016Sdfr    /*
61741016Sdfr     * NOTE: KBDC_RESET_KBD may not restore the communication between the
61841016Sdfr     * keyboard and the controller.
61941016Sdfr     */
62041016Sdfr    reset_kbd(kbdc);
62141016Sdfr#else
62241016Sdfr    /*
62341016Sdfr     * NOTE: somehow diagnostic and keyboard port test commands bring the
62441016Sdfr     * keyboard back.
62541016Sdfr     */
62641016Sdfr    if (!test_controller(kbdc))
62741016Sdfr        log(LOG_ERR, "psm: keyboard controller failed.\n");
62841016Sdfr    /* if there isn't a keyboard in the system, the following error is OK */
62941016Sdfr    if (test_kbd_port(kbdc) != 0) {
63041016Sdfr	if (verbose)
63141016Sdfr	    log(LOG_ERR, "psm: keyboard port failed.\n");
63241016Sdfr    }
63341016Sdfr#endif
63441016Sdfr}
63541016Sdfr
63641016Sdfrstatic int
63741016Sdfrrestore_controller(KBDC kbdc, int command_byte)
63841016Sdfr{
63941016Sdfr    empty_both_buffers(kbdc, 10);
64041016Sdfr
64141016Sdfr    if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
64241016Sdfr	log(LOG_ERR, "psm: failed to restore the keyboard controller "
64341016Sdfr		     "command byte.\n");
64458230Syokota	empty_both_buffers(kbdc, 10);
64541016Sdfr	return FALSE;
64641016Sdfr    } else {
64758230Syokota	empty_both_buffers(kbdc, 10);
64841016Sdfr	return TRUE;
64941016Sdfr    }
65041016Sdfr}
65141016Sdfr
65241016Sdfr/*
65341016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
65441016Sdfr * and its interrupt must be disabled before calling this routine.
65541016Sdfr * The aux device will be disabled before returning.
65641016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
65741016Sdfr * calling this routine.
65841016Sdfr */
65941016Sdfrstatic int
66084880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode)
66141016Sdfr{
66241016Sdfr    KBDC kbdc = sc->kbdc;
66341016Sdfr    int stat[3];
66441016Sdfr    int i;
66541016Sdfr
66641016Sdfr    switch((i = test_aux_port(kbdc))) {
667132270Smux    case 1:	/* ignore these errors */
668132270Smux    case 2:
669132270Smux    case 3:
67041016Sdfr    case PSM_ACK:
67141016Sdfr	if (verbose)
67241016Sdfr	    log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
67384880Syokota	        sc->unit, i);
674102412Scharnier	/* FALLTHROUGH */
67541016Sdfr    case 0:	/* no error */
67641016Sdfr    	break;
67741016Sdfr    case -1: 	/* time out */
67841016Sdfr    default: 	/* error */
67941016Sdfr    	recover_from_error(kbdc);
68045789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
68145789Speter	    break;
68241016Sdfr    	log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
68384880Syokota    	    sc->unit, i);
68441016Sdfr    	return FALSE;
68541016Sdfr    }
68641016Sdfr
68745789Speter    if (sc->config & PSM_CONFIG_NORESET) {
68845789Speter	/*
68945789Speter	 * Don't try to reset the pointing device.  It may possibly be
69045789Speter	 * left in the unknown state, though...
69145789Speter	 */
69245789Speter    } else {
69345789Speter	/*
69445789Speter	 * NOTE: some controllers appears to hang the `keyboard' when
69545789Speter	 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
69645789Speter	 */
69745789Speter	if (!reset_aux_dev(kbdc)) {
69845789Speter            recover_from_error(kbdc);
69984880Syokota            log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
70045789Speter            return FALSE;
70145789Speter	}
70241016Sdfr    }
70341016Sdfr
70441016Sdfr    /*
70541016Sdfr     * both the aux port and the aux device is functioning, see
70641016Sdfr     * if the device can be enabled.
70741016Sdfr     */
70841016Sdfr    if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
70984880Syokota        log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
71041016Sdfr        return FALSE;
71141016Sdfr    }
71241016Sdfr    empty_both_buffers(kbdc, 10);	/* remove stray data if any */
71341016Sdfr
71445789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
71545789Speter	i = GENERIC_MOUSE_ENTRY;
71645789Speter    } else {
71745789Speter	/* FIXME: hardware ID, mouse buttons? */
71841016Sdfr
71945789Speter	/* other parameters */
72045789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
72145789Speter	    if ((*vendortype[i].probefunc)(sc)) {
72245789Speter		if (verbose >= 2)
72345789Speter		    log(LOG_ERR, "psm%d: found %s\n",
72484880Syokota			sc->unit, model_name(vendortype[i].model));
72545789Speter		break;
72645789Speter	    }
72741016Sdfr	}
72841016Sdfr    }
72941016Sdfr
73041016Sdfr    sc->hw.model = vendortype[i].model;
73141016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
73241016Sdfr
73341016Sdfr    /* set mouse parameters */
73441016Sdfr    if (mode != (mousemode_t *)NULL) {
73541016Sdfr	if (mode->rate > 0)
73641016Sdfr            mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
73741016Sdfr	if (mode->resolution >= 0)
73841016Sdfr            mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
73941016Sdfr        set_mouse_scaling(kbdc, 1);
74041016Sdfr        set_mouse_mode(kbdc);
74141016Sdfr    }
74241016Sdfr
743133918Sgibbs    /* Record sync on the next data packet we see. */
744133918Sgibbs    sc->flags |= PSM_NEED_SYNCBITS;
74541016Sdfr
74641016Sdfr    /* just check the status of the mouse */
74741016Sdfr    if (get_mouse_status(kbdc, stat, 0, 3) < 3)
74884880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
74984880Syokota	    sc->unit);
75041016Sdfr
75141016Sdfr    return TRUE;
75241016Sdfr}
75341016Sdfr
75441016Sdfrstatic int
75584880Syokotadoopen(struct psm_softc *sc, int command_byte)
75641016Sdfr{
75741016Sdfr    int stat[3];
75841016Sdfr
75941016Sdfr    /* enable the mouse device */
76041016Sdfr    if (!enable_aux_dev(sc->kbdc)) {
76141016Sdfr	/* MOUSE ERROR: failed to enable the mouse because:
76241016Sdfr	 * 1) the mouse is faulty,
76341016Sdfr	 * 2) the mouse has been removed(!?)
76441016Sdfr	 * In the latter case, the keyboard may have hung, and need
76541016Sdfr	 * recovery procedure...
76641016Sdfr	 */
76741016Sdfr	recover_from_error(sc->kbdc);
76841016Sdfr#if 0
76941016Sdfr	/* FIXME: we could reset the mouse here and try to enable
77041016Sdfr	 * it again. But it will take long time and it's not a good
77141016Sdfr	 * idea to disable the keyboard that long...
77241016Sdfr	 */
77384880Syokota	if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
77441016Sdfr	    recover_from_error(sc->kbdc);
77541016Sdfr#else
77641016Sdfr        {
77741016Sdfr#endif
77841016Sdfr            restore_controller(sc->kbdc, command_byte);
77941016Sdfr	    /* mark this device is no longer available */
78041016Sdfr	    sc->state &= ~PSM_VALID;
78141016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
78284880Syokota		sc->unit);
78341016Sdfr	    return (EIO);
78441016Sdfr	}
78541016Sdfr    }
78641016Sdfr
78741016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
78884880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
78941016Sdfr
79041016Sdfr    /* enable the aux port and interrupt */
79141016Sdfr    if (!set_controller_command_byte(sc->kbdc,
79241016Sdfr	    kbdc_get_device_mask(sc->kbdc),
79341016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
79441016Sdfr		| KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
79541016Sdfr	/* CONTROLLER ERROR */
79641016Sdfr	disable_aux_dev(sc->kbdc);
79741016Sdfr        restore_controller(sc->kbdc, command_byte);
79841016Sdfr	log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
79984880Syokota	    sc->unit);
80041016Sdfr	return (EIO);
80141016Sdfr    }
80241016Sdfr
80358230Syokota    /* start the watchdog timer */
80458230Syokota    sc->watchdog = FALSE;
80584880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
80658230Syokota
80741016Sdfr    return (0);
80841016Sdfr}
80941016Sdfr
81084880Syokotastatic int
81184880Syokotareinitialize(struct psm_softc *sc, int doinit)
81284880Syokota{
81384880Syokota    int err;
81484880Syokota    int c;
81584880Syokota    int s;
81684880Syokota
81784880Syokota    /* don't let anybody mess with the aux device */
81884880Syokota    if (!kbdc_lock(sc->kbdc, TRUE))
81984880Syokota	return (EIO);
82084880Syokota    s = spltty();
82184880Syokota
82284880Syokota    /* block our watchdog timer */
82384880Syokota    sc->watchdog = FALSE;
82484880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
82584880Syokota    callout_handle_init(&sc->callout);
82684880Syokota
82784880Syokota    /* save the current controller command byte */
82884880Syokota    empty_both_buffers(sc->kbdc, 10);
82984880Syokota    c = get_controller_command_byte(sc->kbdc);
83084880Syokota    if (verbose >= 2)
83184880Syokota        log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
83284880Syokota	    sc->unit, c);
83384880Syokota
83484880Syokota    /* enable the aux port but disable the aux interrupt and the keyboard */
83584880Syokota    if ((c == -1) || !set_controller_command_byte(sc->kbdc,
83684880Syokota	    kbdc_get_device_mask(sc->kbdc),
83784880Syokota  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
83884880Syokota	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
83984880Syokota        /* CONTROLLER ERROR */
84084880Syokota	splx(s);
84184880Syokota        kbdc_lock(sc->kbdc, FALSE);
84284880Syokota	log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
84384880Syokota	    sc->unit);
84484880Syokota	return (EIO);
84584880Syokota    }
84684880Syokota
84784880Syokota    /* flush any data */
84884880Syokota    if (sc->state & PSM_VALID) {
84984880Syokota	disable_aux_dev(sc->kbdc);	/* this may fail; but never mind... */
85084880Syokota	empty_aux_buffer(sc->kbdc, 10);
85184880Syokota    }
852123442Salfred    flushpackets(sc);
85384880Syokota    sc->syncerrors = 0;
85484880Syokota
85584880Syokota    /* try to detect the aux device; are you still there? */
85684880Syokota    err = 0;
85784880Syokota    if (doinit) {
85884880Syokota	if (doinitialize(sc, &sc->mode)) {
85984880Syokota	    /* yes */
86084880Syokota	    sc->state |= PSM_VALID;
86184880Syokota	} else {
86284880Syokota	    /* the device has gone! */
86384880Syokota	    restore_controller(sc->kbdc, c);
86484880Syokota	    sc->state &= ~PSM_VALID;
86584880Syokota	    log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
86684880Syokota		sc->unit);
86784880Syokota	    err = ENXIO;
86884880Syokota	}
86984880Syokota    }
87084880Syokota    splx(s);
87184880Syokota
87284880Syokota    /* restore the driver state */
87384880Syokota    if ((sc->state & PSM_OPEN) && (err == 0)) {
87484880Syokota        /* enable the aux device and the port again */
87584880Syokota	err = doopen(sc, c);
87684880Syokota	if (err != 0)
87784880Syokota	    log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
87884880Syokota		sc->unit);
87984880Syokota    } else {
88084880Syokota        /* restore the keyboard port and disable the aux port */
88184880Syokota        if (!set_controller_command_byte(sc->kbdc,
88284880Syokota                kbdc_get_device_mask(sc->kbdc),
88384880Syokota                (c & KBD_KBD_CONTROL_BITS)
88484880Syokota                    | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
88584880Syokota            /* CONTROLLER ERROR */
886133918Sgibbs            log(LOG_ERR,
887133918Sgibbs                "psm%d: failed to disable the aux port (reinitialize).\n",
88884880Syokota                sc->unit);
88984880Syokota            err = EIO;
89084880Syokota	}
89184880Syokota    }
89284880Syokota
89384880Syokota    kbdc_lock(sc->kbdc, FALSE);
89484880Syokota    return (err);
89584880Syokota}
89684880Syokota
89741016Sdfr/* psm driver entry points */
89841016Sdfr
89983147Syokotastatic void
90083147Syokotapsmidentify(driver_t *driver, device_t parent)
90183147Syokota{
90283931Syokota    device_t psmc;
90383931Syokota    device_t psm;
90483931Syokota    u_long irq;
90583931Syokota    int unit;
90683147Syokota
90783931Syokota    unit = device_get_unit(parent);
90883931Syokota
90983147Syokota    /* always add at least one child */
91083931Syokota    psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
91183931Syokota    if (psm == NULL)
91283931Syokota	return;
91383931Syokota
91483931Syokota    irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
91583931Syokota    if (irq > 0)
91683931Syokota	return;
91783931Syokota
91883931Syokota    /*
91983931Syokota     * If the PS/2 mouse device has already been reported by ACPI or
92083931Syokota     * PnP BIOS, obtain the IRQ resource from it.
92183931Syokota     * (See psmcpnp_attach() below.)
92283931Syokota     */
92383931Syokota    psmc = device_find_child(device_get_parent(parent),
92483931Syokota			     PSMCPNP_DRIVER_NAME, unit);
92583931Syokota    if (psmc == NULL)
92683931Syokota	return;
92783931Syokota    irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
92883931Syokota    if (irq <= 0)
92983931Syokota	return;
93083931Syokota    bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
93183147Syokota}
93283147Syokota
933115569Sphk#define endprobe(v)	do {   if (bootverbose)				\
93441016Sdfr				--verbose;   				\
93541016Sdfr                            kbdc_set_device_mask(sc->kbdc, mask);	\
93641016Sdfr			    kbdc_lock(sc->kbdc, FALSE);			\
93741016Sdfr			    return (v);	     				\
938115569Sphk			} while (0)
93941016Sdfr
94041016Sdfrstatic int
94141016Sdfrpsmprobe(device_t dev)
94241016Sdfr{
94341016Sdfr    int unit = device_get_unit(dev);
94441016Sdfr    struct psm_softc *sc = device_get_softc(dev);
94541016Sdfr    int stat[3];
94641016Sdfr    int command_byte;
94741016Sdfr    int mask;
94858230Syokota    int rid;
94941016Sdfr    int i;
95041016Sdfr
95141016Sdfr#if 0
95241016Sdfr    kbdc_debug(TRUE);
95341016Sdfr#endif
95458230Syokota
95583147Syokota    /* see if IRQ is available */
95683147Syokota    rid = KBDC_RID_AUX;
957127135Snjl    sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
958127135Snjl				      RF_SHAREABLE | RF_ACTIVE);
95983147Syokota    if (sc->intr == NULL) {
96083147Syokota	if (bootverbose)
96183147Syokota            device_printf(dev, "unable to allocate IRQ\n");
96283147Syokota        return (ENXIO);
96383147Syokota    }
96483147Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
96558230Syokota
96684880Syokota    sc->unit = unit;
96758230Syokota    sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
96883147Syokota    sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
96958230Syokota    /* XXX: for backward compatibility */
97058230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
97158230Syokota    sc->config |=
97258230Syokota#ifdef PSM_RESETAFTERSUSPEND
97358230Syokota	PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
97458230Syokota#else
97558230Syokota	PSM_CONFIG_HOOKRESUME;
97658230Syokota#endif
97758230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
97841016Sdfr    sc->flags = 0;
97941016Sdfr    if (bootverbose)
98041016Sdfr        ++verbose;
98141016Sdfr
98243105Sdfr    device_set_desc(dev, "PS/2 Mouse");
98343105Sdfr
98441016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE)) {
98541016Sdfr        printf("psm%d: unable to lock the controller.\n", unit);
98641016Sdfr        if (bootverbose)
98741016Sdfr            --verbose;
98841016Sdfr	return (ENXIO);
98941016Sdfr    }
99041016Sdfr
99141016Sdfr    /*
99241016Sdfr     * NOTE: two bits in the command byte controls the operation of the
99341016Sdfr     * aux port (mouse port): the aux port disable bit (bit 5) and the aux
99441016Sdfr     * port interrupt (IRQ 12) enable bit (bit 2).
99541016Sdfr     */
99641016Sdfr
99741016Sdfr    /* discard anything left after the keyboard initialization */
99841016Sdfr    empty_both_buffers(sc->kbdc, 10);
99941016Sdfr
100041016Sdfr    /* save the current command byte; it will be used later */
100141016Sdfr    mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
100241016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
100341016Sdfr    if (verbose)
100441016Sdfr        printf("psm%d: current command byte:%04x\n", unit, command_byte);
100541016Sdfr    if (command_byte == -1) {
100641016Sdfr        /* CONTROLLER ERROR */
100741016Sdfr        printf("psm%d: unable to get the current command byte value.\n",
100841016Sdfr            unit);
100941016Sdfr        endprobe(ENXIO);
101041016Sdfr    }
101141016Sdfr
101241016Sdfr    /*
101341016Sdfr     * disable the keyboard port while probing the aux port, which must be
101441016Sdfr     * enabled during this routine
101541016Sdfr     */
101641016Sdfr    if (!set_controller_command_byte(sc->kbdc,
101741016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
101841016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
101941016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
102041016Sdfr        /*
102141016Sdfr	 * this is CONTROLLER ERROR; I don't know how to recover
102241016Sdfr         * from this error...
102341016Sdfr	 */
102441016Sdfr        restore_controller(sc->kbdc, command_byte);
102541016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
102641016Sdfr        endprobe(ENXIO);
102741016Sdfr    }
102845789Speter    write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
102941016Sdfr
103041016Sdfr    /*
103141016Sdfr     * NOTE: `test_aux_port()' is designed to return with zero if the aux
103241016Sdfr     * port exists and is functioning. However, some controllers appears
103341016Sdfr     * to respond with zero even when the aux port doesn't exist. (It may
103441016Sdfr     * be that this is only the case when the controller DOES have the aux
103541016Sdfr     * port but the port is not wired on the motherboard.) The keyboard
103641016Sdfr     * controllers without the port, such as the original AT, are
103741016Sdfr     * supporsed to return with an error code or simply time out. In any
103841016Sdfr     * case, we have to continue probing the port even when the controller
103941016Sdfr     * passes this test.
104041016Sdfr     *
1041132270Smux     * XXX: some controllers erroneously return the error code 1, 2 or 3
1042132270Smux     * when it has the perfectly functional aux port. We have to ignore
1043132270Smux     * this error code. Even if the controller HAS error with the aux
1044132270Smux     * port, it will be detected later...
104541016Sdfr     * XXX: another incompatible controller returns PSM_ACK (0xfa)...
104641016Sdfr     */
104741016Sdfr    switch ((i = test_aux_port(sc->kbdc))) {
1048132270Smux    case 1:	   /* ignore these errors */
1049132270Smux    case 2:
1050132270Smux    case 3:
105141016Sdfr    case PSM_ACK:
105241016Sdfr        if (verbose)
105341016Sdfr	    printf("psm%d: strange result for test aux port (%d).\n",
105441016Sdfr	        unit, i);
1055102412Scharnier	/* FALLTHROUGH */
105641016Sdfr    case 0:        /* no error */
105741016Sdfr        break;
105841016Sdfr    case -1:        /* time out */
105941016Sdfr    default:        /* error */
106041016Sdfr        recover_from_error(sc->kbdc);
106145789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
106245789Speter	    break;
106341016Sdfr        restore_controller(sc->kbdc, command_byte);
106441016Sdfr        if (verbose)
106541016Sdfr            printf("psm%d: the aux port is not functioning (%d).\n",
106641016Sdfr                unit, i);
106741016Sdfr        endprobe(ENXIO);
106841016Sdfr    }
106941016Sdfr
107045789Speter    if (sc->config & PSM_CONFIG_NORESET) {
107145789Speter	/*
107245789Speter	 * Don't try to reset the pointing device.  It may possibly be
107345789Speter	 * left in the unknown state, though...
107445789Speter	 */
107545789Speter    } else {
107645789Speter	/*
107745789Speter	 * NOTE: some controllers appears to hang the `keyboard' when the aux
107845789Speter	 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1079117302Smikeh	 *
1080117302Smikeh	 * Attempt to reset the controller twice -- this helps
1081117302Smikeh	 * pierce through some KVM switches. The second reset
1082117302Smikeh	 * is non-fatal.
108345789Speter	 */
108445789Speter	if (!reset_aux_dev(sc->kbdc)) {
108545789Speter            recover_from_error(sc->kbdc);
108645789Speter            restore_controller(sc->kbdc, command_byte);
108745789Speter            if (verbose)
108845789Speter        	printf("psm%d: failed to reset the aux device.\n", unit);
108945789Speter            endprobe(ENXIO);
1090117302Smikeh	} else if (!reset_aux_dev(sc->kbdc)) {
1091117302Smikeh	    recover_from_error(sc->kbdc);
1092117302Smikeh	    if (verbose >= 2)
1093117302Smikeh        	printf("psm%d: failed to reset the aux device (2).\n",
1094117302Smikeh        	    unit);
109545789Speter	}
109641016Sdfr    }
109745789Speter
109841016Sdfr    /*
109941016Sdfr     * both the aux port and the aux device is functioning, see if the
110041016Sdfr     * device can be enabled. NOTE: when enabled, the device will start
110141016Sdfr     * sending data; we shall immediately disable the device once we know
110241016Sdfr     * the device can be enabled.
110341016Sdfr     */
110441016Sdfr    if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
110545789Speter        /* MOUSE ERROR */
110645789Speter	recover_from_error(sc->kbdc);
110745789Speter	restore_controller(sc->kbdc, command_byte);
110845789Speter	if (verbose)
110945789Speter	    printf("psm%d: failed to enable the aux device.\n", unit);
111041016Sdfr        endprobe(ENXIO);
111141016Sdfr    }
111241016Sdfr
111341016Sdfr    /* save the default values after reset */
111441016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
111541016Sdfr	sc->dflt_mode.rate = sc->mode.rate = stat[2];
111641016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
111741016Sdfr    } else {
111841016Sdfr	sc->dflt_mode.rate = sc->mode.rate = -1;
111941016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = -1;
112041016Sdfr    }
112141016Sdfr
112241016Sdfr    /* hardware information */
112341016Sdfr    sc->hw.iftype = MOUSE_IF_PS2;
112441016Sdfr
112541016Sdfr    /* verify the device is a mouse */
112641016Sdfr    sc->hw.hwid = get_aux_id(sc->kbdc);
112741016Sdfr    if (!is_a_mouse(sc->hw.hwid)) {
112841016Sdfr        restore_controller(sc->kbdc, command_byte);
112941016Sdfr        if (verbose)
113041016Sdfr            printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
113141016Sdfr        endprobe(ENXIO);
113241016Sdfr    }
113341016Sdfr    switch (sc->hw.hwid) {
113441016Sdfr    case PSM_BALLPOINT_ID:
113541016Sdfr        sc->hw.type = MOUSE_TRACKBALL;
113641016Sdfr        break;
113741016Sdfr    case PSM_MOUSE_ID:
113841016Sdfr    case PSM_INTELLI_ID:
113958230Syokota    case PSM_EXPLORER_ID:
114058230Syokota    case PSM_4DMOUSE_ID:
114158230Syokota    case PSM_4DPLUS_ID:
114241016Sdfr        sc->hw.type = MOUSE_MOUSE;
114341016Sdfr        break;
114441016Sdfr    default:
114541016Sdfr        sc->hw.type = MOUSE_UNKNOWN;
114641016Sdfr        break;
114741016Sdfr    }
114841016Sdfr
114945789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
115045789Speter	sc->hw.buttons = 2;
115145789Speter	i = GENERIC_MOUSE_ENTRY;
115245789Speter    } else {
115345789Speter	/* # of buttons */
115445789Speter	sc->hw.buttons = get_mouse_buttons(sc->kbdc);
115541016Sdfr
115645789Speter	/* other parameters */
115745789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
115845789Speter	    if ((*vendortype[i].probefunc)(sc)) {
115945789Speter		if (verbose >= 2)
116045789Speter		    printf("psm%d: found %s\n",
116145789Speter			   unit, model_name(vendortype[i].model));
116245789Speter		break;
116345789Speter	    }
116441016Sdfr	}
116541016Sdfr    }
116641016Sdfr
116741016Sdfr    sc->hw.model = vendortype[i].model;
116841016Sdfr
116941016Sdfr    sc->dflt_mode.level = PSM_LEVEL_BASE;
117041016Sdfr    sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
117141016Sdfr    sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
117241016Sdfr    if (sc->config & PSM_CONFIG_NOCHECKSYNC)
117341016Sdfr        sc->dflt_mode.syncmask[0] = 0;
117441016Sdfr    else
117541016Sdfr        sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
117645789Speter    if (sc->config & PSM_CONFIG_FORCETAP)
117745789Speter        sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
117841016Sdfr    sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
117941016Sdfr    sc->mode = sc->dflt_mode;
118041016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
118141016Sdfr
118241016Sdfr    /* set mouse parameters */
118348773Syokota#if 0
118448773Syokota    /*
118548773Syokota     * A version of Logitech FirstMouse+ won't report wheel movement,
118648773Syokota     * if SET_DEFAULTS is sent...  Don't use this command.
118748773Syokota     * This fix was found by Takashi Nishida.
118848773Syokota     */
118941016Sdfr    i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
119041016Sdfr    if (verbose >= 2)
119141016Sdfr	printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
119248773Syokota#endif
119341016Sdfr    if (sc->config & PSM_CONFIG_RESOLUTION) {
119441016Sdfr        sc->mode.resolution
119541016Sdfr	    = set_mouse_resolution(sc->kbdc,
119648773Syokota				   (sc->config & PSM_CONFIG_RESOLUTION) - 1);
119748773Syokota    } else if (sc->mode.resolution >= 0) {
119848773Syokota	sc->mode.resolution
119948773Syokota	    = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
120041016Sdfr    }
120148773Syokota    if (sc->mode.rate > 0) {
120248773Syokota	sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
120348773Syokota    }
120448773Syokota    set_mouse_scaling(sc->kbdc, 1);
120541016Sdfr
1206133918Sgibbs    /* Record sync on the next data packet we see. */
1207133918Sgibbs    sc->flags |= PSM_NEED_SYNCBITS;
120841016Sdfr
120941016Sdfr    /* just check the status of the mouse */
121041016Sdfr    /*
121141016Sdfr     * NOTE: XXX there are some arcane controller/mouse combinations out
121241016Sdfr     * there, which hung the controller unless there is data transmission
121341016Sdfr     * after ACK from the mouse.
121441016Sdfr     */
121541016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
121641016Sdfr        printf("psm%d: failed to get status.\n", unit);
121741016Sdfr    } else {
121841016Sdfr	/*
121941016Sdfr	 * When in its native mode, some mice operate with different
122041016Sdfr	 * default parameters than in the PS/2 compatible mode.
122141016Sdfr	 */
122241016Sdfr        sc->dflt_mode.rate = sc->mode.rate = stat[2];
122341016Sdfr        sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
122441016Sdfr     }
122541016Sdfr
122641016Sdfr    /* disable the aux port for now... */
122741016Sdfr    if (!set_controller_command_byte(sc->kbdc,
122841016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
122941016Sdfr            (command_byte & KBD_KBD_CONTROL_BITS)
123041016Sdfr                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
123141016Sdfr        /*
123241016Sdfr	 * this is CONTROLLER ERROR; I don't know the proper way to
123341016Sdfr         * recover from this error...
123441016Sdfr	 */
123541016Sdfr        restore_controller(sc->kbdc, command_byte);
123641016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
123741016Sdfr        endprobe(ENXIO);
123841016Sdfr    }
123941016Sdfr
124041016Sdfr    /* done */
124141016Sdfr    kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
124241016Sdfr    kbdc_lock(sc->kbdc, FALSE);
124341016Sdfr    return (0);
124441016Sdfr}
124541016Sdfr
124641016Sdfrstatic int
124741016Sdfrpsmattach(device_t dev)
124841016Sdfr{
124941016Sdfr    int unit = device_get_unit(dev);
125041016Sdfr    struct psm_softc *sc = device_get_softc(dev);
125158230Syokota    int error;
125258230Syokota    int rid;
125341016Sdfr
125441016Sdfr    /* Setup initial state */
125541016Sdfr    sc->state = PSM_VALID;
125658230Syokota    callout_handle_init(&sc->callout);
125741016Sdfr
125858230Syokota    /* Setup our interrupt handler */
125983147Syokota    rid = KBDC_RID_AUX;
1260127135Snjl    sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1261127135Snjl				      RF_SHAREABLE | RF_ACTIVE);
126258230Syokota    if (sc->intr == NULL)
126358230Syokota	return (ENXIO);
126483147Syokota    error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
126558230Syokota    if (error) {
126658230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
126758230Syokota	return (error);
126858230Syokota    }
126958230Syokota
127041016Sdfr    /* Done */
127158230Syokota    sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
127258230Syokota		       "psm%d", unit);
127358230Syokota    sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
127458230Syokota			"bpsm%d", unit);
127541016Sdfr
127641016Sdfr    if (!verbose) {
127741016Sdfr        printf("psm%d: model %s, device ID %d\n",
127848778Syokota	    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
127941016Sdfr    } else {
128048778Syokota        printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
128148778Syokota	    unit, model_name(sc->hw.model),
128248778Syokota	    sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
128341016Sdfr	printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
128441016Sdfr	    unit, sc->config, sc->flags, sc->mode.packetsize);
128541016Sdfr	printf("psm%d: syncmask:%02x, syncbits:%02x\n",
128641016Sdfr	    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
128741016Sdfr    }
128841016Sdfr
128941016Sdfr    if (bootverbose)
129041016Sdfr        --verbose;
129141016Sdfr
129241016Sdfr    return (0);
129341016Sdfr}
129441016Sdfr
129541016Sdfrstatic int
129658230Syokotapsmdetach(device_t dev)
129758230Syokota{
129858230Syokota    struct psm_softc *sc;
129958230Syokota    int rid;
130058230Syokota
130158230Syokota    sc = device_get_softc(dev);
130258230Syokota    if (sc->state & PSM_OPEN)
130358230Syokota	return EBUSY;
130458230Syokota
130583147Syokota    rid = KBDC_RID_AUX;
130683147Syokota    bus_teardown_intr(dev, sc->intr, sc->ih);
130758230Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
130858230Syokota
130958230Syokota    destroy_dev(sc->dev);
131058230Syokota    destroy_dev(sc->bdev);
131158230Syokota
131258230Syokota    return 0;
131358230Syokota}
131458230Syokota
131558230Syokotastatic int
1316130585Sphkpsmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
131741016Sdfr{
131841016Sdfr    int unit = PSM_UNIT(dev);
131941016Sdfr    struct psm_softc *sc;
132041016Sdfr    int command_byte;
132141016Sdfr    int err;
132241016Sdfr    int s;
132341016Sdfr
132441016Sdfr    /* Get device data */
132541016Sdfr    sc = PSM_SOFTC(unit);
132641016Sdfr    if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
132741016Sdfr	/* the device is no longer valid/functioning */
132841016Sdfr        return (ENXIO);
132941016Sdfr
133041016Sdfr    /* Disallow multiple opens */
133141016Sdfr    if (sc->state & PSM_OPEN)
133241016Sdfr        return (EBUSY);
133341016Sdfr
133441016Sdfr    device_busy(devclass_get_device(psm_devclass, unit));
133541016Sdfr
133641016Sdfr    /* Initialize state */
133741016Sdfr    sc->mode.level = sc->dflt_mode.level;
133841016Sdfr    sc->mode.protocol = sc->dflt_mode.protocol;
133958230Syokota    sc->watchdog = FALSE;
134041016Sdfr
134141016Sdfr    /* flush the event queue */
134241016Sdfr    sc->queue.count = 0;
134341016Sdfr    sc->queue.head = 0;
134441016Sdfr    sc->queue.tail = 0;
134541016Sdfr    sc->status.flags = 0;
134641016Sdfr    sc->status.button = 0;
134741016Sdfr    sc->status.obutton = 0;
134841016Sdfr    sc->status.dx = 0;
134941016Sdfr    sc->status.dy = 0;
135041016Sdfr    sc->status.dz = 0;
135141016Sdfr    sc->button = 0;
1352123442Salfred    sc->pqueue_start = 0;
1353123442Salfred    sc->pqueue_end = 0;
135441016Sdfr
135541016Sdfr    /* empty input buffer */
1356123442Salfred    flushpackets(sc);
135784880Syokota    sc->syncerrors = 0;
135841016Sdfr
135941016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
136041016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
136141016Sdfr	return (EIO);
136241016Sdfr
136341016Sdfr    /* save the current controller command byte */
136441016Sdfr    s = spltty();
136541016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
136641016Sdfr
136741016Sdfr    /* enable the aux port and temporalily disable the keyboard */
136841016Sdfr    if ((command_byte == -1)
136941016Sdfr        || !set_controller_command_byte(sc->kbdc,
137041016Sdfr	    kbdc_get_device_mask(sc->kbdc),
137141016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
137241016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
137341016Sdfr        /* CONTROLLER ERROR; do you know how to get out of this? */
137441016Sdfr        kbdc_lock(sc->kbdc, FALSE);
137541016Sdfr	splx(s);
137641016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
137741016Sdfr	    unit);
137841016Sdfr	return (EIO);
137941016Sdfr    }
138041016Sdfr    /*
138141016Sdfr     * Now that the keyboard controller is told not to generate
138241016Sdfr     * the keyboard and mouse interrupts, call `splx()' to allow
138341016Sdfr     * the other tty interrupts. The clock interrupt may also occur,
138441016Sdfr     * but timeout routines will be blocked by the poll flag set
138541016Sdfr     * via `kbdc_lock()'
138641016Sdfr     */
138741016Sdfr    splx(s);
138841016Sdfr
138941016Sdfr    /* enable the mouse device */
139084880Syokota    err = doopen(sc, command_byte);
139141016Sdfr
139241016Sdfr    /* done */
139341016Sdfr    if (err == 0)
139441016Sdfr        sc->state |= PSM_OPEN;
139541016Sdfr    kbdc_lock(sc->kbdc, FALSE);
139641016Sdfr    return (err);
139741016Sdfr}
139841016Sdfr
139941016Sdfrstatic int
1400130585Sphkpsmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
140141016Sdfr{
140241016Sdfr    int unit = PSM_UNIT(dev);
140341016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
140441016Sdfr    int stat[3];
140541016Sdfr    int command_byte;
140641016Sdfr    int s;
140741016Sdfr
140841016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
140941016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
141041016Sdfr	return (EIO);
141141016Sdfr
141241016Sdfr    /* save the current controller command byte */
141341016Sdfr    s = spltty();
141441016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
141541016Sdfr    if (command_byte == -1) {
141641016Sdfr        kbdc_lock(sc->kbdc, FALSE);
141741016Sdfr	splx(s);
141841016Sdfr	return (EIO);
141941016Sdfr    }
142041016Sdfr
142141016Sdfr    /* disable the aux interrupt and temporalily disable the keyboard */
142241016Sdfr    if (!set_controller_command_byte(sc->kbdc,
142341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
142441016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
142541016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
142641016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
142758230Syokota	    unit);
142841016Sdfr	/* CONTROLLER ERROR;
142941016Sdfr	 * NOTE: we shall force our way through. Because the only
143041016Sdfr	 * ill effect we shall see is that we may not be able
143141016Sdfr	 * to read ACK from the mouse, and it doesn't matter much
143241016Sdfr	 * so long as the mouse will accept the DISABLE command.
143341016Sdfr	 */
143441016Sdfr    }
143541016Sdfr    splx(s);
143641016Sdfr
143758230Syokota    /* stop the watchdog timer */
143884880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
143958230Syokota    callout_handle_init(&sc->callout);
144058230Syokota
144141016Sdfr    /* remove anything left in the output buffer */
144241016Sdfr    empty_aux_buffer(sc->kbdc, 10);
144341016Sdfr
144441016Sdfr    /* disable the aux device, port and interrupt */
144541016Sdfr    if (sc->state & PSM_VALID) {
144641016Sdfr        if (!disable_aux_dev(sc->kbdc)) {
144741016Sdfr	    /* MOUSE ERROR;
144841016Sdfr	     * NOTE: we don't return error and continue, pretending
144941016Sdfr	     * we have successfully disabled the device. It's OK because
145041016Sdfr	     * the interrupt routine will discard any data from the mouse
145141016Sdfr	     * hereafter.
145241016Sdfr	     */
145341016Sdfr	    log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
145458230Syokota		unit);
145541016Sdfr        }
145641016Sdfr
145741016Sdfr        if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
145841016Sdfr            log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
145958230Syokota		unit);
146041016Sdfr    }
146141016Sdfr
146241016Sdfr    if (!set_controller_command_byte(sc->kbdc,
146341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
146441016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
146541016Sdfr	        | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
146641016Sdfr	/* CONTROLLER ERROR;
146741016Sdfr	 * we shall ignore this error; see the above comment.
146841016Sdfr	 */
146941016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
147058230Syokota	    unit);
147141016Sdfr    }
147241016Sdfr
147341016Sdfr    /* remove anything left in the output buffer */
147441016Sdfr    empty_aux_buffer(sc->kbdc, 10);
147541016Sdfr
147641016Sdfr    /* close is almost always successful */
147741016Sdfr    sc->state &= ~PSM_OPEN;
147841016Sdfr    kbdc_lock(sc->kbdc, FALSE);
147941016Sdfr    device_unbusy(devclass_get_device(psm_devclass, unit));
148041016Sdfr    return (0);
148141016Sdfr}
148241016Sdfr
148341016Sdfrstatic int
1484123442Salfredtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, unsigned char *buf)
148541016Sdfr{
148641016Sdfr    static unsigned char butmapps2[8] = {
148741016Sdfr        0,
148841016Sdfr        MOUSE_PS2_BUTTON1DOWN,
148941016Sdfr        MOUSE_PS2_BUTTON2DOWN,
149041016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
149141016Sdfr        MOUSE_PS2_BUTTON3DOWN,
149241016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
149341016Sdfr        MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
149441016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
149541016Sdfr    };
149641016Sdfr    static unsigned char butmapmsc[8] = {
149741016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
149841016Sdfr        MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
149941016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
150041016Sdfr        MOUSE_MSC_BUTTON3UP,
150141016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
150241016Sdfr        MOUSE_MSC_BUTTON2UP,
150341016Sdfr        MOUSE_MSC_BUTTON1UP,
150441016Sdfr        0,
150541016Sdfr    };
150641016Sdfr    int mapped;
150741016Sdfr    int i;
150841016Sdfr
150941016Sdfr    if (sc->mode.level == PSM_LEVEL_BASE) {
151041016Sdfr        mapped = status->button & ~MOUSE_BUTTON4DOWN;
151141016Sdfr        if (status->button & MOUSE_BUTTON4DOWN)
151241016Sdfr	    mapped |= MOUSE_BUTTON1DOWN;
151341016Sdfr        status->button = mapped;
151441016Sdfr        buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1515109269Smdodd        i = imax(imin(status->dx, 255), -256);
151641016Sdfr	if (i < 0)
151741016Sdfr	    buf[0] |= MOUSE_PS2_XNEG;
151841016Sdfr        buf[1] = i;
1519109269Smdodd        i = imax(imin(status->dy, 255), -256);
152041016Sdfr	if (i < 0)
152141016Sdfr	    buf[0] |= MOUSE_PS2_YNEG;
152241016Sdfr        buf[2] = i;
152341016Sdfr	return MOUSE_PS2_PACKETSIZE;
152441016Sdfr    } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
152541016Sdfr        buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1526109269Smdodd        i = imax(imin(status->dx, 255), -256);
152741016Sdfr        buf[1] = i >> 1;
152841016Sdfr        buf[3] = i - buf[1];
1529109269Smdodd        i = imax(imin(status->dy, 255), -256);
153041016Sdfr        buf[2] = i >> 1;
153141016Sdfr        buf[4] = i - buf[2];
1532109269Smdodd        i = imax(imin(status->dz, 127), -128);
153341016Sdfr        buf[5] = (i >> 1) & 0x7f;
153441016Sdfr        buf[6] = (i - (i >> 1)) & 0x7f;
153541016Sdfr        buf[7] = (~status->button >> 3) & 0x7f;
153641016Sdfr	return MOUSE_SYS_PACKETSIZE;
153741016Sdfr    }
1538123442Salfred    return pb->inputbytes;
153941016Sdfr}
154041016Sdfr
154141016Sdfrstatic int
1542130585Sphkpsmread(struct cdev *dev, struct uio *uio, int flag)
154341016Sdfr{
154441016Sdfr    register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
154541016Sdfr    unsigned char buf[PSM_SMALLBUFSIZE];
154641016Sdfr    int error = 0;
154741016Sdfr    int s;
154841016Sdfr    int l;
154941016Sdfr
155041016Sdfr    if ((sc->state & PSM_VALID) == 0)
155141016Sdfr	return EIO;
155241016Sdfr
155341016Sdfr    /* block until mouse activity occured */
155441016Sdfr    s = spltty();
155541016Sdfr    while (sc->queue.count <= 0) {
155641016Sdfr        if (PSM_NBLOCKIO(dev)) {
155741016Sdfr            splx(s);
155841016Sdfr            return EWOULDBLOCK;
155941016Sdfr        }
156041016Sdfr        sc->state |= PSM_ASLP;
1561111748Sdes        error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
156241016Sdfr        sc->state &= ~PSM_ASLP;
156341016Sdfr        if (error) {
156441016Sdfr            splx(s);
156541016Sdfr            return error;
156641016Sdfr        } else if ((sc->state & PSM_VALID) == 0) {
156741016Sdfr            /* the device disappeared! */
156841016Sdfr            splx(s);
156941016Sdfr            return EIO;
157041016Sdfr	}
157141016Sdfr    }
157241016Sdfr    splx(s);
157341016Sdfr
157441016Sdfr    /* copy data to the user land */
157541016Sdfr    while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
157641016Sdfr        s = spltty();
1577109269Smdodd	l = imin(sc->queue.count, uio->uio_resid);
157841016Sdfr	if (l > sizeof(buf))
157941016Sdfr	    l = sizeof(buf);
158041016Sdfr	if (l > sizeof(sc->queue.buf) - sc->queue.head) {
158141016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
158241016Sdfr		sizeof(sc->queue.buf) - sc->queue.head);
158341016Sdfr	    bcopy(&sc->queue.buf[0],
158441016Sdfr		&buf[sizeof(sc->queue.buf) - sc->queue.head],
158541016Sdfr		l - (sizeof(sc->queue.buf) - sc->queue.head));
158641016Sdfr	} else {
158741016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
158841016Sdfr	}
158941016Sdfr	sc->queue.count -= l;
159041016Sdfr	sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
159141016Sdfr        splx(s);
159241016Sdfr        error = uiomove(buf, l, uio);
159341016Sdfr        if (error)
159441016Sdfr	    break;
159541016Sdfr    }
159641016Sdfr
159741016Sdfr    return error;
159841016Sdfr}
159941016Sdfr
160041016Sdfrstatic int
160141016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
160241016Sdfr{
160341016Sdfr    int s;
160441016Sdfr
160541016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
160641016Sdfr	return EIO;
160741016Sdfr
160841016Sdfr    s = spltty();
160941016Sdfr    *c = get_controller_command_byte(sc->kbdc);
161041016Sdfr    if ((*c == -1)
161141016Sdfr	|| !set_controller_command_byte(sc->kbdc,
161241016Sdfr	    kbdc_get_device_mask(sc->kbdc),
161341016Sdfr            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
161441016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
161541016Sdfr        /* this is CONTROLLER ERROR */
161641016Sdfr	splx(s);
161741016Sdfr        kbdc_lock(sc->kbdc, FALSE);
161841016Sdfr	return EIO;
161941016Sdfr    }
162041016Sdfr
162141016Sdfr    /*
162241016Sdfr     * The device may be in the middle of status data transmission.
162341016Sdfr     * The transmission will be interrupted, thus, incomplete status
162441016Sdfr     * data must be discarded. Although the aux interrupt is disabled
162541016Sdfr     * at the keyboard controller level, at most one aux interrupt
162641016Sdfr     * may have already been pending and a data byte is in the
162741016Sdfr     * output buffer; throw it away. Note that the second argument
162841016Sdfr     * to `empty_aux_buffer()' is zero, so that the call will just
162941016Sdfr     * flush the internal queue.
163041016Sdfr     * `psmintr()' will be invoked after `splx()' if an interrupt is
163141016Sdfr     * pending; it will see no data and returns immediately.
163241016Sdfr     */
163341016Sdfr    empty_aux_buffer(sc->kbdc, 0);	/* flush the queue */
163441016Sdfr    read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
1635123442Salfred    flushpackets(sc);
163641016Sdfr    splx(s);
163741016Sdfr
163841016Sdfr    return 0;
163941016Sdfr}
164041016Sdfr
1641123442Salfredstatic void
1642123442Salfreddropqueue(struct psm_softc *sc)
1643123442Salfred{
1644123442Salfred
1645123442Salfred    	sc->queue.count = 0;
1646123442Salfred   	sc->queue.head = 0;
1647123442Salfred    	sc->queue.tail = 0;
1648123442Salfred	if ((sc->state & PSM_SOFTARMED) != 0) {
1649123442Salfred		sc->state &= ~PSM_SOFTARMED;
1650123442Salfred		untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout);
1651123442Salfred	}
1652123442Salfred	sc->pqueue_start = sc->pqueue_end;
1653123442Salfred}
1654123442Salfred
1655123442Salfredstatic void
1656123442Salfredflushpackets(struct psm_softc *sc)
1657123442Salfred{
1658123442Salfred
1659123442Salfred	dropqueue(sc);
1660123442Salfred	bzero(&sc->pqueue, sizeof(sc->pqueue));
1661123442Salfred}
1662123442Salfred
166341016Sdfrstatic int
166441016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
166541016Sdfr{
166641016Sdfr    int error = 0;
166741016Sdfr
166841016Sdfr    /*
166941016Sdfr     * We may have seen a part of status data during `set_mouse_XXX()'.
167041016Sdfr     * they have been queued; flush it.
167141016Sdfr     */
167241016Sdfr    empty_aux_buffer(sc->kbdc, 0);
167341016Sdfr
167441016Sdfr    /* restore ports and interrupt */
167541016Sdfr    if (!set_controller_command_byte(sc->kbdc,
167641016Sdfr            kbdc_get_device_mask(sc->kbdc),
167741016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
167841016Sdfr        /* CONTROLLER ERROR; this is serious, we may have
167941016Sdfr         * been left with the inaccessible keyboard and
168041016Sdfr         * the disabled mouse interrupt.
168141016Sdfr         */
168241016Sdfr        error = EIO;
168341016Sdfr    }
168441016Sdfr
168541016Sdfr    kbdc_lock(sc->kbdc, FALSE);
168641016Sdfr    return error;
168741016Sdfr}
168841016Sdfr
168941016Sdfrstatic int
1690130585Sphkpsmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
169141016Sdfr{
169241016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
169341016Sdfr    mousemode_t mode;
169441016Sdfr    mousestatus_t status;
169541016Sdfr#if (defined(MOUSE_GETVARS))
169641016Sdfr    mousevar_t *var;
169741016Sdfr#endif
169841016Sdfr    mousedata_t *data;
169941016Sdfr    int stat[3];
170041016Sdfr    int command_byte;
170141016Sdfr    int error = 0;
170241016Sdfr    int s;
170341016Sdfr
170441016Sdfr    /* Perform IOCTL command */
170541016Sdfr    switch (cmd) {
170641016Sdfr
170741016Sdfr    case OLD_MOUSE_GETHWINFO:
170841016Sdfr	s = spltty();
170941016Sdfr        ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
171041016Sdfr        ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
171141016Sdfr        ((old_mousehw_t *)addr)->type = sc->hw.type;
171248778Syokota        ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
171341016Sdfr	splx(s);
171441016Sdfr        break;
171541016Sdfr
171641016Sdfr    case MOUSE_GETHWINFO:
171741016Sdfr	s = spltty();
171841016Sdfr        *(mousehw_t *)addr = sc->hw;
171941016Sdfr	if (sc->mode.level == PSM_LEVEL_BASE)
172041016Sdfr	    ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
172141016Sdfr	splx(s);
172241016Sdfr        break;
172341016Sdfr
1724132865Snjl    case MOUSE_SYN_GETHWINFO:
1725132865Snjl	s = spltty();
1726132865Snjl	if (sc->hw.model == MOUSE_MODEL_SYNAPTICS)
1727132865Snjl	    *(synapticshw_t *)addr = sc->synhw;
1728132865Snjl	else
1729132865Snjl	    error = EINVAL;
1730132865Snjl	splx(s);
1731132865Snjl	break;
1732132865Snjl
173341016Sdfr    case OLD_MOUSE_GETMODE:
173441016Sdfr	s = spltty();
173541016Sdfr	switch (sc->mode.level) {
173641016Sdfr	case PSM_LEVEL_BASE:
173741016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
173841016Sdfr	    break;
173941016Sdfr	case PSM_LEVEL_STANDARD:
174041016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
174141016Sdfr	    break;
174241016Sdfr	case PSM_LEVEL_NATIVE:
174341016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
174441016Sdfr	    break;
174541016Sdfr	}
174641016Sdfr        ((old_mousemode_t *)addr)->rate = sc->mode.rate;
174741016Sdfr        ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
174841016Sdfr        ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
174941016Sdfr	splx(s);
175041016Sdfr        break;
175141016Sdfr
175241016Sdfr    case MOUSE_GETMODE:
175341016Sdfr	s = spltty();
175441016Sdfr        *(mousemode_t *)addr = sc->mode;
175541016Sdfr        ((mousemode_t *)addr)->resolution =
175641016Sdfr	    MOUSE_RES_LOW - sc->mode.resolution;
175741016Sdfr	switch (sc->mode.level) {
175841016Sdfr	case PSM_LEVEL_BASE:
175941016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
176041016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
176141016Sdfr	    break;
176241016Sdfr	case PSM_LEVEL_STANDARD:
176341016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
176441016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
176541016Sdfr	    ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
176641016Sdfr	    ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
176741016Sdfr	    break;
176841016Sdfr	case PSM_LEVEL_NATIVE:
176941016Sdfr	    /* FIXME: this isn't quite correct... XXX */
177041016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
177141016Sdfr	    break;
177241016Sdfr	}
177341016Sdfr	splx(s);
177441016Sdfr        break;
177541016Sdfr
177641016Sdfr    case OLD_MOUSE_SETMODE:
177741016Sdfr    case MOUSE_SETMODE:
177841016Sdfr	if (cmd == OLD_MOUSE_SETMODE) {
177941016Sdfr	    mode.rate = ((old_mousemode_t *)addr)->rate;
178041016Sdfr	    /*
178141016Sdfr	     * resolution  old I/F   new I/F
178241016Sdfr	     * default        0         0
178341016Sdfr	     * low            1        -2
178441016Sdfr	     * medium low     2        -3
178541016Sdfr	     * medium high    3        -4
178641016Sdfr	     * high           4        -5
178741016Sdfr	     */
178841016Sdfr	    if (((old_mousemode_t *)addr)->resolution > 0)
178941016Sdfr	        mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
179041016Sdfr	    mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
179141016Sdfr	    mode.level = -1;
179241016Sdfr	} else {
179341016Sdfr	    mode = *(mousemode_t *)addr;
179441016Sdfr	}
179541016Sdfr
179641016Sdfr	/* adjust and validate parameters. */
179741016Sdfr	if (mode.rate > UCHAR_MAX)
179841016Sdfr	    return EINVAL;
179941016Sdfr        if (mode.rate == 0)
180041016Sdfr            mode.rate = sc->dflt_mode.rate;
180141016Sdfr	else if (mode.rate == -1)
180241016Sdfr	    /* don't change the current setting */
180341016Sdfr	    ;
180441016Sdfr	else if (mode.rate < 0)
180541016Sdfr	    return EINVAL;
180641016Sdfr	if (mode.resolution >= UCHAR_MAX)
180741016Sdfr	    return EINVAL;
180841016Sdfr	if (mode.resolution >= 200)
180941016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
181041016Sdfr	else if (mode.resolution >= 100)
181141016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
181241016Sdfr	else if (mode.resolution >= 50)
181341016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
181441016Sdfr	else if (mode.resolution > 0)
181541016Sdfr	    mode.resolution = MOUSE_RES_LOW;
181641016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
181741016Sdfr            mode.resolution = sc->dflt_mode.resolution;
181841016Sdfr        else if (mode.resolution == -1)
181941016Sdfr	    /* don't change the current setting */
182041016Sdfr	    ;
182141016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
182241016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
182341016Sdfr	if (mode.level == -1)
182441016Sdfr	    /* don't change the current setting */
182541016Sdfr	    mode.level = sc->mode.level;
182641016Sdfr	else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
182741016Sdfr	    return EINVAL;
182841016Sdfr        if (mode.accelfactor == -1)
182941016Sdfr	    /* don't change the current setting */
183041016Sdfr	    mode.accelfactor = sc->mode.accelfactor;
183141016Sdfr        else if (mode.accelfactor < 0)
183241016Sdfr	    return EINVAL;
183341016Sdfr
183441016Sdfr	/* don't allow anybody to poll the keyboard controller */
183541016Sdfr	error = block_mouse_data(sc, &command_byte);
183641016Sdfr	if (error)
183741016Sdfr            return error;
183841016Sdfr
183941016Sdfr        /* set mouse parameters */
184041016Sdfr	if (mode.rate > 0)
184141016Sdfr	    mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
184241016Sdfr	if (mode.resolution >= 0)
184341016Sdfr	    mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
184441016Sdfr	set_mouse_scaling(sc->kbdc, 1);
184541016Sdfr	get_mouse_status(sc->kbdc, stat, 0, 3);
184641016Sdfr
184741016Sdfr        s = spltty();
184841016Sdfr    	sc->mode.rate = mode.rate;
184941016Sdfr    	sc->mode.resolution = mode.resolution;
185041016Sdfr    	sc->mode.accelfactor = mode.accelfactor;
185141016Sdfr    	sc->mode.level = mode.level;
185241016Sdfr        splx(s);
185341016Sdfr
185441016Sdfr	unblock_mouse_data(sc, command_byte);
185541016Sdfr        break;
185641016Sdfr
185741016Sdfr    case MOUSE_GETLEVEL:
185841016Sdfr	*(int *)addr = sc->mode.level;
185941016Sdfr        break;
186041016Sdfr
186141016Sdfr    case MOUSE_SETLEVEL:
186241016Sdfr	if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
186341016Sdfr	    return EINVAL;
186441016Sdfr	sc->mode.level = *(int *)addr;
186541016Sdfr        break;
186641016Sdfr
186741016Sdfr    case MOUSE_GETSTATUS:
186841016Sdfr        s = spltty();
186941016Sdfr	status = sc->status;
187041016Sdfr	sc->status.flags = 0;
187141016Sdfr	sc->status.obutton = sc->status.button;
187241016Sdfr	sc->status.button = 0;
187341016Sdfr	sc->status.dx = 0;
187441016Sdfr	sc->status.dy = 0;
187541016Sdfr	sc->status.dz = 0;
187641016Sdfr        splx(s);
187741016Sdfr        *(mousestatus_t *)addr = status;
187841016Sdfr        break;
187941016Sdfr
188041016Sdfr#if (defined(MOUSE_GETVARS))
188141016Sdfr    case MOUSE_GETVARS:
188241016Sdfr	var = (mousevar_t *)addr;
188341016Sdfr	bzero(var, sizeof(*var));
188441016Sdfr	s = spltty();
188541016Sdfr        var->var[0] = MOUSE_VARS_PS2_SIG;
188641016Sdfr        var->var[1] = sc->config;
188741016Sdfr        var->var[2] = sc->flags;
188841016Sdfr	splx(s);
188941016Sdfr        break;
189041016Sdfr
189141016Sdfr    case MOUSE_SETVARS:
189241016Sdfr	return ENODEV;
189341016Sdfr#endif /* MOUSE_GETVARS */
189441016Sdfr
189541016Sdfr    case MOUSE_READSTATE:
189641016Sdfr    case MOUSE_READDATA:
189741016Sdfr	data = (mousedata_t *)addr;
189841016Sdfr	if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
189941016Sdfr	    return EINVAL;
190041016Sdfr
190141016Sdfr	error = block_mouse_data(sc, &command_byte);
190241016Sdfr	if (error)
190341016Sdfr            return error;
190441016Sdfr        if ((data->len = get_mouse_status(sc->kbdc, data->buf,
190541016Sdfr		(cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
190641016Sdfr            error = EIO;
190741016Sdfr	unblock_mouse_data(sc, command_byte);
190841016Sdfr	break;
190941016Sdfr
191041016Sdfr#if (defined(MOUSE_SETRESOLUTION))
191141016Sdfr    case MOUSE_SETRESOLUTION:
191241016Sdfr	mode.resolution = *(int *)addr;
191341016Sdfr	if (mode.resolution >= UCHAR_MAX)
191441016Sdfr	    return EINVAL;
191541016Sdfr	else if (mode.resolution >= 200)
191641016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
191741016Sdfr	else if (mode.resolution >= 100)
191841016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
191941016Sdfr	else if (mode.resolution >= 50)
192041016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
192141016Sdfr	else if (mode.resolution > 0)
192241016Sdfr	    mode.resolution = MOUSE_RES_LOW;
192341016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
192441016Sdfr            mode.resolution = sc->dflt_mode.resolution;
192541016Sdfr        else if (mode.resolution == -1)
192641016Sdfr	    mode.resolution = sc->mode.resolution;
192741016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
192841016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
192941016Sdfr
193041016Sdfr	error = block_mouse_data(sc, &command_byte);
193141016Sdfr	if (error)
193241016Sdfr            return error;
193341016Sdfr        sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
193441016Sdfr	if (sc->mode.resolution != mode.resolution)
193541016Sdfr	    error = EIO;
193641016Sdfr	unblock_mouse_data(sc, command_byte);
193741016Sdfr        break;
193841016Sdfr#endif /* MOUSE_SETRESOLUTION */
193941016Sdfr
194041016Sdfr#if (defined(MOUSE_SETRATE))
194141016Sdfr    case MOUSE_SETRATE:
194241016Sdfr	mode.rate = *(int *)addr;
194341016Sdfr	if (mode.rate > UCHAR_MAX)
194441016Sdfr	    return EINVAL;
194541016Sdfr        if (mode.rate == 0)
194641016Sdfr            mode.rate = sc->dflt_mode.rate;
194741016Sdfr	else if (mode.rate < 0)
194841016Sdfr	    mode.rate = sc->mode.rate;
194941016Sdfr
195041016Sdfr	error = block_mouse_data(sc, &command_byte);
195141016Sdfr	if (error)
195241016Sdfr            return error;
195341016Sdfr        sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
195441016Sdfr	if (sc->mode.rate != mode.rate)
195541016Sdfr	    error = EIO;
195641016Sdfr	unblock_mouse_data(sc, command_byte);
195741016Sdfr        break;
195841016Sdfr#endif /* MOUSE_SETRATE */
195941016Sdfr
196041016Sdfr#if (defined(MOUSE_SETSCALING))
196141016Sdfr    case MOUSE_SETSCALING:
196241016Sdfr	if ((*(int *)addr <= 0) || (*(int *)addr > 2))
196341016Sdfr	    return EINVAL;
196441016Sdfr
196541016Sdfr	error = block_mouse_data(sc, &command_byte);
196641016Sdfr	if (error)
196741016Sdfr            return error;
196841016Sdfr        if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
196941016Sdfr	    error = EIO;
197041016Sdfr	unblock_mouse_data(sc, command_byte);
197141016Sdfr        break;
197241016Sdfr#endif /* MOUSE_SETSCALING */
197341016Sdfr
197441016Sdfr#if (defined(MOUSE_GETHWID))
197541016Sdfr    case MOUSE_GETHWID:
197641016Sdfr	error = block_mouse_data(sc, &command_byte);
197741016Sdfr	if (error)
197841016Sdfr            return error;
197948778Syokota        sc->hw.hwid &= ~0x00ff;
198048778Syokota        sc->hw.hwid |= get_aux_id(sc->kbdc);
198148778Syokota	*(int *)addr = sc->hw.hwid & 0x00ff;
198241016Sdfr	unblock_mouse_data(sc, command_byte);
198341016Sdfr        break;
198441016Sdfr#endif /* MOUSE_GETHWID */
198541016Sdfr
198641016Sdfr    default:
198741016Sdfr	return ENOTTY;
198841016Sdfr    }
198941016Sdfr
199041016Sdfr    return error;
199141016Sdfr}
199241016Sdfr
199341016Sdfrstatic void
199458230Syokotapsmtimeout(void *arg)
199558230Syokota{
199658230Syokota    struct psm_softc *sc;
199765045Syokota    int s;
199858230Syokota
199984880Syokota    sc = (struct psm_softc *)arg;
200065045Syokota    s = spltty();
200163746Syokota    if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
200258230Syokota	if (verbose >= 4)
200384880Syokota	    log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
200458230Syokota	psmintr(sc);
200563746Syokota	kbdc_lock(sc->kbdc, FALSE);
200658230Syokota    }
200758230Syokota    sc->watchdog = TRUE;
200865045Syokota    splx(s);
200984880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
201058230Syokota}
201158230Syokota
2012123442Salfredstatic int psmhz = 20;
2013123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmhz, CTLFLAG_RW, &psmhz, 0, "");
2014123442Salfred
2015123442Salfredstatic int psm_soft_timeout = 500000; /* 0.5 sec */
2016123442SalfredSYSCTL_INT(_debug, OID_AUTO, psm_soft_timeout, CTLFLAG_RW,
2017123442Salfred    &psm_soft_timeout, 0, "");
2018123442Salfred
2019123442Salfredstatic int psmerrsecs = 2;
2020123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrsecs, CTLFLAG_RW, &psmerrsecs, 0, "");
2021123442Salfredstatic int psmerrusecs = 0;
2022123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrusecs, CTLFLAG_RW, &psmerrusecs, 0, "");
2023123442Salfredstatic int psmsecs = 0;
2024123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmsecs, CTLFLAG_RW, &psmsecs, 0, "");
2025123442Salfredstatic int psmusecs = 500000;
2026123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmusecs, CTLFLAG_RW, &psmusecs, 0, "");
2027123442Salfred
202858230Syokotastatic void
202941016Sdfrpsmintr(void *arg)
203041016Sdfr{
2031123442Salfred    struct psm_softc *sc = arg;
2032123442Salfred    struct timeval now;
203341016Sdfr    int c;
2034123442Salfred    packetbuf_t *pb;
2035123442Salfred    int haderror = 0;
203641016Sdfr
2037123442Salfred
203841016Sdfr    /* read until there is nothing to read */
203941016Sdfr    while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
204041016Sdfr
2041123442Salfred        pb = &sc->pqueue[sc->pqueue_end];
204241016Sdfr        /* discard the byte if the device is not open */
204341016Sdfr        if ((sc->state & PSM_OPEN) == 0)
204441016Sdfr            continue;
204541016Sdfr
2046123442Salfred	getmicrouptime(&now);
2047123442Salfred	if ((pb->inputbytes > 0) && timevalcmp(&now, &sc->inputtimeout, >)) {
2048127841Smarkm#if DEBUG
204984880Syokota	    log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2050127841Smarkm#endif
2051123442Salfred	    pb->inputbytes = 0;
205284880Syokota	    sc->syncerrors = 0;
205384880Syokota	}
205484880Syokota	sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
205584880Syokota	sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2056123442Salfred	timevaladd(&sc->inputtimeout, &now);
205784880Syokota
2058123442Salfred        pb->ipacket[pb->inputbytes++] = c;
2059123442Salfred        if (pb->inputbytes < sc->mode.packetsize)
206041016Sdfr	    continue;
206141016Sdfr
2062127841Smarkm#if DEBUG
206341016Sdfr        log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2064123442Salfred	    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2065123442Salfred	    pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]);
206641016Sdfr#endif
206741016Sdfr
2068123442Salfred	c = pb->ipacket[0];
206941016Sdfr
207063746Syokota	if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2071133918Sgibbs	    if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2072133918Sgibbs		sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2073133918Sgibbs		sc->flags &= ~PSM_NEED_SYNCBITS;
2074133918Sgibbs		goto valid_sync;
2075133918Sgibbs	    }
2076127841Smarkm#if DEBUG
2077123442Salfred            log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x) %d"
2078123442Salfred		" cmds since last error.\n",
2079123442Salfred		c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2080123442Salfred		sc->cmdcount - sc->lasterr);
2081127841Smarkm#endif
2082123442Salfred	    haderror = 1;
2083123442Salfred	    sc->lasterr = sc->cmdcount;
2084123442Salfred	    dropqueue(sc);
208584880Syokota	    ++sc->syncerrors;
2086123442Salfred	    sc->lastinputerr = now;
208784880Syokota	    if (sc->syncerrors < sc->mode.packetsize) {
2088127841Smarkm#if DEBUG
208984880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2090127841Smarkm#endif
2091123442Salfred		--pb->inputbytes;
2092123442Salfred		bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
209384880Syokota	    } else if (sc->syncerrors == sc->mode.packetsize) {
2094127841Smarkm#if DEBUG
209569439Syokota		log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2096127841Smarkm#endif
2097123442Salfred		pb->inputbytes = 0;
209869439Syokota		disable_aux_dev(sc->kbdc);
209969439Syokota		enable_aux_dev(sc->kbdc);
210084880Syokota	    } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2101127841Smarkm#if DEBUG
210284880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2103127841Smarkm#endif
2104123442Salfred		--pb->inputbytes;
2105123442Salfred		bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
210684880Syokota	    } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2107127841Smarkm#if DEBUG
210884880Syokota		log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2109127841Smarkm#endif
211084880Syokota		reinitialize(sc, TRUE);
211169439Syokota	    }
211284880Syokota	    continue;
211363746Syokota	}
2114133918Sgibbsvalid_sync:
2115123442Salfred	/* if this packet is at all bogus then drop the packet. */
2116123442Salfred	if (haderror ||
2117123442Salfred	    !timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, &now)) {
2118123442Salfred		pb->inputbytes = 0;
2119123442Salfred		haderror = 0;
2120123442Salfred		continue;
2121123442Salfred	}
212263746Syokota
2123123442Salfred	sc->cmdcount++;
2124123442Salfred	if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2125123442Salfred		sc->pqueue_end = 0;
2126123442Salfred	/*
2127123442Salfred	 * If we've filled the queue then call the softintr ourselves,
2128123442Salfred	 * otherwise schedule the interrupt for later.
2129123442Salfred	 */
2130123442Salfred	if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2131123442Salfred	    (sc->pqueue_end == sc->pqueue_start)) {
2132123442Salfred    		if ((sc->state & PSM_SOFTARMED) != 0) {
2133123442Salfred			sc->state &= ~PSM_SOFTARMED;
2134123442Salfred			untimeout(psmsoftintr, arg, sc->softcallout);
2135123442Salfred		}
2136123442Salfred		psmsoftintr(arg);
2137123442Salfred	} else if ((sc->state & PSM_SOFTARMED) == 0) {
2138123442Salfred		sc->state |= PSM_SOFTARMED;
2139123442Salfred		sc->softcallout = timeout(psmsoftintr, arg,
2140123442Salfred		    psmhz < 1 ? 1 : (hz/psmhz));
2141123442Salfred	}
2142123442Salfred    }
2143123442Salfred}
2144123442Salfred
2145123442Salfredstatic void
2146123442Salfredpsmsoftintr(void *arg)
2147123442Salfred{
2148123442Salfred    /*
2149123442Salfred     * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
2150123442Salfred     * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
2151123442Salfred     */
2152123442Salfred    static int butmap[8] = {
2153123442Salfred        0,
2154123442Salfred	MOUSE_BUTTON1DOWN,
2155123442Salfred	MOUSE_BUTTON3DOWN,
2156123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2157123442Salfred	MOUSE_BUTTON2DOWN,
2158123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
2159123442Salfred	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
2160123442Salfred        MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
2161123442Salfred    };
2162123442Salfred    static int butmap_versapad[8] = {
2163123442Salfred	0,
2164123442Salfred	MOUSE_BUTTON3DOWN,
2165123442Salfred	0,
2166123442Salfred	MOUSE_BUTTON3DOWN,
2167123442Salfred	MOUSE_BUTTON1DOWN,
2168123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2169123442Salfred	MOUSE_BUTTON1DOWN,
2170123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
2171123442Salfred    };
2172133882Sphilip    static int touchpad_buttons;
2173133882Sphilip    static int guest_buttons;
2174123442Salfred    register struct psm_softc *sc = arg;
2175123442Salfred    mousestatus_t ms;
2176132865Snjl    int w, x, y, z;
2177123442Salfred    int c;
2178123442Salfred    int l;
2179123442Salfred    int x0, y0;
2180123442Salfred    int s;
2181123442Salfred    packetbuf_t *pb;
2182123442Salfred
2183123442Salfred    getmicrouptime(&sc->lastsoftintr);
2184123442Salfred
2185123442Salfred    s = spltty();
2186123442Salfred
2187123442Salfred    do {
2188123442Salfred
2189123442Salfred	pb = &sc->pqueue[sc->pqueue_start];
2190123442Salfred	c = pb->ipacket[0];
219149965Syokota	/*
219241016Sdfr	 * A kludge for Kensington device!
219341016Sdfr	 * The MSB of the horizontal count appears to be stored in
219458230Syokota	 * a strange place.
219541016Sdfr	 */
219658230Syokota	if (sc->hw.model == MOUSE_MODEL_THINK)
2197123442Salfred	    pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
219841016Sdfr
219941016Sdfr        /* ignore the overflow bits... */
2200123442Salfred        x = (c & MOUSE_PS2_XNEG) ?  pb->ipacket[1] - 256 : pb->ipacket[1];
2201123442Salfred        y = (c & MOUSE_PS2_YNEG) ?  pb->ipacket[2] - 256 : pb->ipacket[2];
220241016Sdfr	z = 0;
220341016Sdfr        ms.obutton = sc->button;		  /* previous button state */
220441016Sdfr        ms.button = butmap[c & MOUSE_PS2_BUTTONS];
220545789Speter	/* `tapping' action */
220645789Speter	if (sc->config & PSM_CONFIG_FORCETAP)
220745789Speter	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
220841016Sdfr
220941016Sdfr	switch (sc->hw.model) {
221041016Sdfr
221158230Syokota	case MOUSE_MODEL_EXPLORER:
221258230Syokota	    /*
221358230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
221458230Syokota	     * byte 1:  oy ox sy sx 1  M  R  L
221558230Syokota	     * byte 2:  x  x  x  x  x  x  x  x
221658230Syokota	     * byte 3:  y  y  y  y  y  y  y  y
221758230Syokota	     * byte 4:  *  *  S2 S1 s  d2 d1 d0
221858230Syokota	     *
221958230Syokota	     * L, M, R, S1, S2: left, middle, right and side buttons
222058230Syokota	     * s: wheel data sign bit
222158230Syokota	     * d2-d0: wheel data
222258230Syokota	     */
2223123442Salfred	    z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2224123442Salfred		? (pb->ipacket[3] & 0x0f) - 16 : (pb->ipacket[3] & 0x0f);
2225123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
222658230Syokota		? MOUSE_BUTTON4DOWN : 0;
2227123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
222858230Syokota		? MOUSE_BUTTON5DOWN : 0;
222958230Syokota	    break;
223058230Syokota
223141016Sdfr	case MOUSE_MODEL_INTELLI:
223241016Sdfr	case MOUSE_MODEL_NET:
223341016Sdfr	    /* wheel data is in the fourth byte */
2234123442Salfred	    z = (char)pb->ipacket[3];
223558230Syokota	    /* some mice may send 7 when there is no Z movement?! XXX */
223658230Syokota	    if ((z >= 7) || (z <= -7))
223758230Syokota		z = 0;
223858230Syokota	    /* some compatible mice have additional buttons */
223958230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
224058230Syokota		? MOUSE_BUTTON4DOWN : 0;
224158230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
224258230Syokota		? MOUSE_BUTTON5DOWN : 0;
224341016Sdfr	    break;
224441016Sdfr
224541016Sdfr	case MOUSE_MODEL_MOUSEMANPLUS:
224648778Syokota	    /*
224748778Syokota	     * PS2++ protocl packet
224848778Syokota	     *
224948778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
225048778Syokota	     * byte 1:  *  1  p3 p2 1  *  *  *
225148778Syokota	     * byte 2:  c1 c2 p1 p0 d1 d0 1  0
225248778Syokota	     *
225348778Syokota	     * p3-p0: packet type
225448778Syokota	     * c1, c2: c1 & c2 == 1, if p2 == 0
225548778Syokota	     *         c1 & c2 == 0, if p2 == 1
225648778Syokota	     *
225748778Syokota	     * packet type: 0 (device type)
225848778Syokota	     * See comments in enable_mmanplus() below.
225948778Syokota	     *
226048778Syokota	     * packet type: 1 (wheel data)
226148778Syokota	     *
226248778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
226348778Syokota	     * byte 3:  h  *  B5 B4 s  d2 d1 d0
226448778Syokota	     *
226548778Syokota	     * h: 1, if horizontal roller data
226648778Syokota	     *    0, if vertical roller data
226748778Syokota	     * B4, B5: button 4 and 5
226848778Syokota	     * s: sign bit
226948778Syokota	     * d2-d0: roller data
227048778Syokota	     *
227148778Syokota	     * packet type: 2 (reserved)
227248778Syokota	     */
227348778Syokota	    if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
227448778Syokota		    && (abs(x) > 191)
2275123442Salfred		    && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
227641016Sdfr		/* the extended data packet encodes button and wheel events */
2277123442Salfred		switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
227848778Syokota		case 1:
227948778Syokota		    /* wheel data packet */
228048778Syokota		    x = y = 0;
2281123442Salfred		    if (pb->ipacket[2] & 0x80) {
228248778Syokota			/* horizontal roller count - ignore it XXX*/
228348778Syokota		    } else {
228448778Syokota			/* vertical roller count */
2285123442Salfred			z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2286123442Salfred			    ? (pb->ipacket[2] & 0x0f) - 16
2287123442Salfred			    : (pb->ipacket[2] & 0x0f);
228848778Syokota		    }
2289123442Salfred		    ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
229048778Syokota			? MOUSE_BUTTON4DOWN : 0;
2291123442Salfred		    ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
229248778Syokota			? MOUSE_BUTTON5DOWN : 0;
229348778Syokota		    break;
229448778Syokota		case 2:
229558230Syokota		    /* this packet type is reserved by Logitech... */
229658230Syokota		    /*
229758230Syokota		     * IBM ScrollPoint Mouse uses this packet type to
229858230Syokota		     * encode both vertical and horizontal scroll movement.
229958230Syokota		     */
230058230Syokota		    x = y = 0;
230158230Syokota		    /* horizontal count */
2302123442Salfred		    if (pb->ipacket[2] & 0x0f)
2303123442Salfred			z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
230458230Syokota		    /* vertical count */
2305123442Salfred		    if (pb->ipacket[2] & 0xf0)
2306123442Salfred			z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
230758230Syokota#if 0
230858230Syokota		    /* vertical count */
2309123442Salfred		    z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG)
2310123442Salfred			? ((pb->ipacket[2] >> 4) & 0x0f) - 16
2311123442Salfred			: ((pb->ipacket[2] >> 4) & 0x0f);
231258230Syokota		    /* horizontal count */
2313123442Salfred		    w = (pb->ipacket[2] & MOUSE_SPOINT_WNEG)
2314123442Salfred			? (pb->ipacket[2] & 0x0f) - 16
2315123442Salfred			: (pb->ipacket[2] & 0x0f);
231658230Syokota#endif
231758230Syokota		    break;
231848778Syokota		case 0:
231948778Syokota		    /* device type packet - shouldn't happen */
2320102412Scharnier		    /* FALLTHROUGH */
232148778Syokota		default:
232248778Syokota		    x = y = 0;
232348778Syokota		    ms.button = ms.obutton;
232458230Syokota		    if (bootverbose)
232558230Syokota			log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
232658230Syokota				       "0x%02x 0x%02x 0x%02x\n",
2327123442Salfred			    MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2328123442Salfred			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]);
232948778Syokota		    break;
233048778Syokota		}
233141016Sdfr	    } else {
233241016Sdfr		/* preserve button states */
233341016Sdfr		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
233441016Sdfr	    }
233541016Sdfr	    break;
233641016Sdfr
233741016Sdfr	case MOUSE_MODEL_GLIDEPOINT:
233841016Sdfr	    /* `tapping' action */
233941016Sdfr	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
234041016Sdfr	    break;
234141016Sdfr
234241016Sdfr	case MOUSE_MODEL_NETSCROLL:
234358230Syokota	    /* three addtional bytes encode buttons and wheel events */
2344123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
234541016Sdfr		? MOUSE_BUTTON4DOWN : 0;
2346123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
234758230Syokota		? MOUSE_BUTTON5DOWN : 0;
2348123442Salfred	    z = (pb->ipacket[3] & MOUSE_PS2_XNEG)
2349123442Salfred		? pb->ipacket[4] - 256 : pb->ipacket[4];
235041016Sdfr	    break;
235141016Sdfr
235241016Sdfr	case MOUSE_MODEL_THINK:
235341016Sdfr	    /* the fourth button state in the first byte */
235441016Sdfr	    ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
235541016Sdfr	    break;
235641016Sdfr
235749965Syokota	case MOUSE_MODEL_VERSAPAD:
235849965Syokota	    /* VersaPad PS/2 absolute mode message format
235949965Syokota	     *
236049965Syokota	     * [packet1]     7   6   5   4   3   2   1   0(LSB)
236149965Syokota	     *  ipacket[0]:  1   1   0   A   1   L   T   R
236249965Syokota	     *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
236349965Syokota	     *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
236449965Syokota	     *  ipacket[3]:  1   1   1   A   1   L   T   R
236549965Syokota	     *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
236649965Syokota	     *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
236749965Syokota	     *
236849965Syokota	     * [note]
236949965Syokota	     *  R: right physical mouse button (1=on)
237049965Syokota	     *  T: touch pad virtual button (1=tapping)
237149965Syokota	     *  L: left physical mouse button (1=on)
237249965Syokota	     *  A: position data is valid (1=valid)
237349965Syokota	     *  H: horizontal data (12bit signed integer. H11 is sign bit.)
237449965Syokota	     *  V: vertical data (12bit signed integer. V11 is sign bit.)
237549965Syokota	     *  P: pressure data
237649965Syokota	     *
237749965Syokota	     * Tapping is mapped to MOUSE_BUTTON4.
237849965Syokota	     */
237949965Syokota	    ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
238049965Syokota	    ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
238149965Syokota	    x = y = 0;
238249965Syokota	    if (c & MOUSE_PS2VERSA_IN_USE) {
2383123442Salfred		x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
2384123442Salfred		y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
238549965Syokota		if (x0 & 0x800)
238649965Syokota		    x0 -= 0x1000;
238749965Syokota		if (y0 & 0x800)
238849965Syokota		    y0 -= 0x1000;
238949965Syokota		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
239049965Syokota		    x = sc->xold - x0;
239149965Syokota		    y = y0 - sc->yold;
239249965Syokota		    if (x < 0)	/* XXX */
239349965Syokota			x++;
239449965Syokota		    else if (x)
239549965Syokota			x--;
239649965Syokota		    if (y < 0)
239749965Syokota			y++;
239849965Syokota		    else if (y)
239949965Syokota			y--;
240049965Syokota		} else {
240149965Syokota		    sc->flags |= PSM_FLAGS_FINGERDOWN;
240249965Syokota		}
240349965Syokota		sc->xold = x0;
240449965Syokota		sc->yold = y0;
240549965Syokota	    } else {
240649965Syokota		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
240749965Syokota	    }
240849965Syokota	    c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
240949965Syokota		| ((y < 0) ? MOUSE_PS2_YNEG : 0);
241049965Syokota	    break;
241149965Syokota
241258230Syokota	case MOUSE_MODEL_4D:
241358230Syokota	    /*
241458230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
241558230Syokota	     * byte 1:  s2 d2 s1 d1 1  M  R  L
241658230Syokota	     * byte 2:  sx x  x  x  x  x  x  x
241758230Syokota	     * byte 3:  sy y  y  y  y  y  y  y
241858230Syokota	     *
241958230Syokota	     * s1: wheel 1 direction
242058230Syokota	     * d1: wheel 1 data
242158230Syokota	     * s2: wheel 2 direction
242258230Syokota	     * d2: wheel 2 data
242358230Syokota	     */
2424123442Salfred	    x = (pb->ipacket[1] & 0x80) ? pb->ipacket[1] - 256 : pb->ipacket[1];
2425123442Salfred	    y = (pb->ipacket[2] & 0x80) ? pb->ipacket[2] - 256 : pb->ipacket[2];
242658230Syokota	    switch (c & MOUSE_4D_WHEELBITS) {
242758230Syokota	    case 0x10:
242858230Syokota		z = 1;
242958230Syokota		break;
243058230Syokota	    case 0x30:
243158230Syokota		z = -1;
243258230Syokota		break;
243358230Syokota	    case 0x40:	/* 2nd wheel turning right XXX */
243458230Syokota		z = 2;
243558230Syokota		break;
243658230Syokota	    case 0xc0:	/* 2nd wheel turning left XXX */
243758230Syokota		z = -2;
243858230Syokota		break;
243958230Syokota	    }
244058230Syokota	    break;
244158230Syokota
244258230Syokota	case MOUSE_MODEL_4DPLUS:
244358230Syokota	    if ((x < 16 - 256) && (y < 16 - 256)) {
244458230Syokota		/*
244558230Syokota		 *          b7 b6 b5 b4 b3 b2 b1 b0
244658230Syokota		 * byte 1:  0  0  1  1  1  M  R  L
244758230Syokota		 * byte 2:  0  0  0  0  1  0  0  0
244858230Syokota		 * byte 3:  0  0  0  0  S  s  d1 d0
244958230Syokota		 *
245058230Syokota		 * L, M, R, S: left, middle, right and side buttons
245158230Syokota		 * s: wheel data sign bit
245258230Syokota		 * d1-d0: wheel data
245358230Syokota		 */
245458230Syokota		x = y = 0;
2455123442Salfred		if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
245658230Syokota		    ms.button |= MOUSE_BUTTON4DOWN;
2457123442Salfred		z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2458123442Salfred			? ((pb->ipacket[2] & 0x07) - 8)
2459123442Salfred			: (pb->ipacket[2] & 0x07) ;
246058230Syokota	    } else {
246158230Syokota		/* preserve previous button states */
246258230Syokota		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
246358230Syokota	    }
246458230Syokota	    break;
246558230Syokota
2466132865Snjl	case MOUSE_MODEL_SYNAPTICS:
2467132865Snjl	    /* TouchPad PS/2 absolute mode message format
2468132865Snjl	     *
2469132865Snjl	     *  Bits:        7   6   5   4   3   2   1   0 (LSB)
2470132865Snjl	     *  ------------------------------------------------
2471132865Snjl	     *  ipacket[0]:  1   0  W3  W2   0  W1   R   L
2472132865Snjl	     *  ipacket[1]: Yb  Ya  Y9  Y8  Xb  Xa  X9  X8
2473132865Snjl	     *  ipacket[2]: Z7  Z6  Z5  Z4  Z3  Z2  Z1  Z0
2474132865Snjl	     *  ipacket[3]:  1   1  Yc  Xc   0  W0   D   U
2475132865Snjl	     *  ipacket[4]: X7  X6  X5  X4  X3  X2  X1  X0
2476132865Snjl	     *  ipacket[5]: Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
2477132865Snjl	     *
2478132865Snjl	     * Legend:
2479132865Snjl	     *  L: left physical mouse button
2480132865Snjl	     *  R: right physical mouse button
2481132865Snjl	     *  D: down button
2482132865Snjl	     *  U: up button
2483132865Snjl	     *  W: "wrist" value
2484132865Snjl	     *  X: x position
2485132865Snjl	     *  Y: x position
2486132865Snjl	     *  Z: pressure
2487132865Snjl	     *
2488132865Snjl	     * Absolute reportable limits:    0 - 6143.
2489132865Snjl	     * Typical bezel limits:       1472 - 5472.
2490132865Snjl	     * Typical edge marings:       1632 - 5312.
2491132865Snjl	     *
2492132865Snjl	     * w = 3 Passthrough Packet
2493132865Snjl	     *
2494132865Snjl	     * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2495132865Snjl	     */
2496132865Snjl
2497132865Snjl	    /* Sanity check for out of sync packets. */
2498132865Snjl	    if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2499132865Snjl		(pb->ipacket[3] & 0xc8) != 0xc0)
2500132865Snjl		continue;
2501132865Snjl
2502132865Snjl	    x = y = x0 = y0 = 0;
2503132865Snjl
2504132865Snjl	    /* Pressure value. */
2505132865Snjl	    z = pb->ipacket[2];
2506132865Snjl
2507133301Sphilip	    /* Finger width value */
2508133301Sphilip	    if (sc->synhw.capExtended) {
2509133301Sphilip		w = ((pb->ipacket[0] & 0x30) >> 2) |
2510133301Sphilip		    ((pb->ipacket[0] & 0x04) >> 1) |
2511133301Sphilip		    ((pb->ipacket[3] & 0x04) >> 2);
2512133301Sphilip	    } else {
2513133301Sphilip		/* Assume a finger of regular width */
2514133301Sphilip		w = 4;
2515133301Sphilip	    }
2516133301Sphilip
2517133868Sphilip	    /* Handle packets from the guest device */
2518133868Sphilip	    if (w == 3 && sc->synhw.capPassthrough) {
2519133868Sphilip		x = ((pb->ipacket[1] & 0x10) ?
2520133868Sphilip		    pb->ipacket[4] - 256 : pb->ipacket[4]);
2521133868Sphilip		y = ((pb->ipacket[1] & 0x20) ?
2522133868Sphilip		    pb->ipacket[5] - 256 : pb->ipacket[5]);
2523133868Sphilip		z = 0;
2524133868Sphilip
2525133868Sphilip		guest_buttons = 0;
2526133868Sphilip		if (pb->ipacket[1] & 0x01)
2527133868Sphilip		    guest_buttons |= MOUSE_BUTTON1DOWN;
2528133868Sphilip		if (pb->ipacket[1] & 0x04)
2529133868Sphilip		    guest_buttons |= MOUSE_BUTTON2DOWN;
2530133868Sphilip		if (pb->ipacket[1] & 0x02)
2531133868Sphilip		    guest_buttons |= MOUSE_BUTTON3DOWN;
2532133868Sphilip
2533133868Sphilip		ms.button = touchpad_buttons | guest_buttons;
2534133868Sphilip		break;
2535133868Sphilip	    }
2536133868Sphilip
2537133296Sphilip	    /* Button presses */
2538133868Sphilip	    touchpad_buttons = 0;
2539132865Snjl	    if (pb->ipacket[0] & 0x01)
2540133868Sphilip		  touchpad_buttons |= MOUSE_BUTTON1DOWN;
2541132865Snjl	    if (pb->ipacket[0] & 0x02)
2542133868Sphilip		  touchpad_buttons |= MOUSE_BUTTON3DOWN;
2543132865Snjl
2544133296Sphilip	    if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2545133296Sphilip		if ((pb->ipacket[3] & 0x01) && (pb->ipacket[0] & 0x01) == 0)
2546133868Sphilip		    touchpad_buttons |= MOUSE_BUTTON4DOWN;
2547133296Sphilip		if ((pb->ipacket[3] & 0x02) && (pb->ipacket[0] & 0x02) == 0)
2548133868Sphilip		    touchpad_buttons |= MOUSE_BUTTON5DOWN;
2549133296Sphilip	    }
2550132865Snjl
2551133868Sphilip	    ms.button = touchpad_buttons | guest_buttons;
2552133868Sphilip
2553132865Snjl	    /* There is a finger on the pad. */
2554132865Snjl	    if ((w >= 4 && w <= 7) && (z >= 16 && z < 200)) {
2555132865Snjl		x0 = ((pb->ipacket[3] & 0x10) << 8) |
2556132865Snjl		    ((pb->ipacket[1] & 0x0f) << 8) |
2557132865Snjl		    pb->ipacket[4];
2558132865Snjl		y0 = ((pb->ipacket[3] & 0x20) << 7) |
2559132865Snjl		    ((pb->ipacket[1] & 0xf0) << 4) |
2560132865Snjl		    pb->ipacket[5];
2561132865Snjl
2562132865Snjl		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2563132865Snjl		    x0 = (x0 + sc->xold * 3) / 4;
2564132865Snjl		    y0 = (y0 + sc->yold * 3) / 4;
2565132865Snjl
2566133298Sphilip		    x = (x0 - sc->xold) * 10 / 85;
2567133298Sphilip		    y = (y0 - sc->yold) * 10 / 85;
2568132865Snjl		} else {
2569132865Snjl		    sc->flags |= PSM_FLAGS_FINGERDOWN;
2570132865Snjl		}
2571132865Snjl
2572132865Snjl		sc->xold = x0;
2573132865Snjl		sc->yold = y0;
2574133297Sphilip		sc->zmax = imax(z, sc->zmax);
2575132865Snjl	    } else {
2576132865Snjl		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2577133297Sphilip
2578133297Sphilip		if (sc->zmax > PSM_TAP_THRESHOLD &&
2579133297Sphilip		    timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) {
2580133297Sphilip			if (w == 0)
2581133297Sphilip			    ms.button |= MOUSE_BUTTON3DOWN;
2582133297Sphilip			else if (w == 1)
2583133297Sphilip			    ms.button |= MOUSE_BUTTON2DOWN;
2584133297Sphilip			else
2585133297Sphilip			    ms.button |= MOUSE_BUTTON1DOWN;
2586133297Sphilip		}
2587133297Sphilip
2588133297Sphilip		sc->zmax = 0;
2589133297Sphilip		sc->taptimeout.tv_sec = PSM_TAP_TIMEOUT / 1000000;
2590133297Sphilip		sc->taptimeout.tv_usec = PSM_TAP_TIMEOUT % 1000000;
2591133297Sphilip		timevaladd(&sc->taptimeout, &sc->lastsoftintr);
2592132865Snjl	    }
2593133296Sphilip
2594133296Sphilip	    /* Use the extra buttons as a scrollwheel */
2595133296Sphilip	    if (ms.button & MOUSE_BUTTON4DOWN)
2596133296Sphilip		z = -1;
2597133296Sphilip	    else if (ms.button & MOUSE_BUTTON5DOWN)
2598133296Sphilip		z = 1;
2599133296Sphilip	    else
2600133296Sphilip		z = 0;
2601133296Sphilip
2602132865Snjl	    break;
2603132865Snjl
260441016Sdfr	case MOUSE_MODEL_GENERIC:
260541016Sdfr	default:
260641016Sdfr	    break;
260741016Sdfr	}
260841016Sdfr
260941016Sdfr        /* scale values */
261041016Sdfr        if (sc->mode.accelfactor >= 1) {
261141016Sdfr            if (x != 0) {
261241016Sdfr                x = x * x / sc->mode.accelfactor;
261341016Sdfr                if (x == 0)
261441016Sdfr                    x = 1;
261541016Sdfr                if (c & MOUSE_PS2_XNEG)
261641016Sdfr                    x = -x;
261741016Sdfr            }
261841016Sdfr            if (y != 0) {
261941016Sdfr                y = y * y / sc->mode.accelfactor;
262041016Sdfr                if (y == 0)
262141016Sdfr                    y = 1;
262241016Sdfr                if (c & MOUSE_PS2_YNEG)
262341016Sdfr                    y = -y;
262441016Sdfr            }
262541016Sdfr        }
262641016Sdfr
262741016Sdfr        ms.dx = x;
262841016Sdfr        ms.dy = y;
262941016Sdfr        ms.dz = z;
263041016Sdfr        ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
263141016Sdfr	    | (ms.obutton ^ ms.button);
263241016Sdfr
263341016Sdfr	if (sc->mode.level < PSM_LEVEL_NATIVE)
2634123442Salfred	    pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
263541016Sdfr
263641016Sdfr        sc->status.flags |= ms.flags;
263741016Sdfr        sc->status.dx += ms.dx;
263841016Sdfr        sc->status.dy += ms.dy;
263941016Sdfr        sc->status.dz += ms.dz;
264041016Sdfr        sc->status.button = ms.button;
264141016Sdfr        sc->button = ms.button;
264241016Sdfr
264358230Syokota	sc->watchdog = FALSE;
264458230Syokota
264541016Sdfr        /* queue data */
2646123442Salfred        if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
2647123442Salfred	    l = imin(pb->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2648123442Salfred	    bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2649123442Salfred	    if (pb->inputbytes > l)
2650123442Salfred	        bcopy(&pb->ipacket[l], &sc->queue.buf[0], pb->inputbytes - l);
265141016Sdfr            sc->queue.tail =
2652123442Salfred		(sc->queue.tail + pb->inputbytes) % sizeof(sc->queue.buf);
2653123442Salfred            sc->queue.count += pb->inputbytes;
265441016Sdfr	}
2655123442Salfred        pb->inputbytes = 0;
265641016Sdfr
2657123442Salfred	if (++sc->pqueue_start >= PSM_PACKETQUEUE)
2658123442Salfred		sc->pqueue_start = 0;
2659123442Salfred    } while (sc->pqueue_start != sc->pqueue_end);
2660123442Salfred    if (sc->state & PSM_ASLP) {
2661123442Salfred        sc->state &= ~PSM_ASLP;
2662123442Salfred        wakeup( sc);
266341016Sdfr    }
2664123442Salfred    selwakeuppri(&sc->rsel, PZERO);
2665123442Salfred    sc->state &= ~PSM_SOFTARMED;
2666123442Salfred    splx(s);
266741016Sdfr}
266841016Sdfr
266941016Sdfrstatic int
2670130585Sphkpsmpoll(struct cdev *dev, int events, struct thread *td)
267141016Sdfr{
267241016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
267341016Sdfr    int s;
267441016Sdfr    int revents = 0;
267541016Sdfr
267641016Sdfr    /* Return true if a mouse event available */
267741016Sdfr    s = spltty();
267845789Speter    if (events & (POLLIN | POLLRDNORM)) {
267941016Sdfr	if (sc->queue.count > 0)
268041016Sdfr	    revents |= events & (POLLIN | POLLRDNORM);
268141016Sdfr	else
268283366Sjulian	    selrecord(td, &sc->rsel);
268345789Speter    }
268441016Sdfr    splx(s);
268541016Sdfr
268641016Sdfr    return (revents);
268741016Sdfr}
268841016Sdfr
268941016Sdfr/* vendor/model specific routines */
269041016Sdfr
269141016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
269241016Sdfr{
269341016Sdfr    if (set_mouse_resolution(kbdc, res) != res)
269441016Sdfr        return FALSE;
269541016Sdfr    if (set_mouse_scaling(kbdc, scale)
269641016Sdfr	&& set_mouse_scaling(kbdc, scale)
269741016Sdfr	&& set_mouse_scaling(kbdc, scale)
269841016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3))
269941016Sdfr	return TRUE;
270041016Sdfr    return FALSE;
270141016Sdfr}
270241016Sdfr
270369438Syokotastatic int
270469438Syokotamouse_ext_command(KBDC kbdc, int command)
270569438Syokota{
270669438Syokota    int c;
270769438Syokota
270869438Syokota    c = (command >> 6) & 0x03;
270969438Syokota    if (set_mouse_resolution(kbdc, c) != c)
271069438Syokota	return FALSE;
271169438Syokota    c = (command >> 4) & 0x03;
271269438Syokota    if (set_mouse_resolution(kbdc, c) != c)
271369438Syokota	return FALSE;
271469438Syokota    c = (command >> 2) & 0x03;
271569438Syokota    if (set_mouse_resolution(kbdc, c) != c)
271669438Syokota	return FALSE;
271769438Syokota    c = (command >> 0) & 0x03;
271869438Syokota    if (set_mouse_resolution(kbdc, c) != c)
271969438Syokota	return FALSE;
272069438Syokota    return TRUE;
272169438Syokota}
272269438Syokota
272341016Sdfr#if notyet
272441016Sdfr/* Logitech MouseMan Cordless II */
272541016Sdfrstatic int
272641016Sdfrenable_lcordless(struct psm_softc *sc)
272741016Sdfr{
272841016Sdfr    int status[3];
272941016Sdfr    int ch;
273041016Sdfr
273141016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
273241016Sdfr        return FALSE;
273341016Sdfr    if (status[1] == PSMD_RES_HIGH)
273441016Sdfr	return FALSE;
273541016Sdfr    ch = (status[0] & 0x07) - 1;	/* channel # */
273641016Sdfr    if ((ch <= 0) || (ch > 4))
273741016Sdfr	return FALSE;
273841016Sdfr    /*
273941016Sdfr     * status[1]: always one?
274041016Sdfr     * status[2]: battery status? (0-100)
274141016Sdfr     */
274241016Sdfr    return TRUE;
274341016Sdfr}
274441016Sdfr#endif /* notyet */
274541016Sdfr
274658230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
274741016Sdfrstatic int
274841016Sdfrenable_groller(struct psm_softc *sc)
274941016Sdfr{
275041016Sdfr    int status[3];
275141016Sdfr
275241016Sdfr    /*
275341016Sdfr     * The special sequence to enable the fourth button and the
275441016Sdfr     * roller. Immediately after this sequence check status bytes.
275541016Sdfr     * if the mouse is NetScroll, the second and the third bytes are
275641016Sdfr     * '3' and 'D'.
275741016Sdfr     */
275841016Sdfr
275941016Sdfr    /*
276041016Sdfr     * If the mouse is an ordinary PS/2 mouse, the status bytes should
276141016Sdfr     * look like the following.
276241016Sdfr     *
276341016Sdfr     * byte 1 bit 7 always 0
276441016Sdfr     *        bit 6 stream mode (0)
276541016Sdfr     *        bit 5 disabled (0)
276641016Sdfr     *        bit 4 1:1 scaling (0)
276741016Sdfr     *        bit 3 always 0
276841016Sdfr     *        bit 0-2 button status
276941016Sdfr     * byte 2 resolution (PSMD_RES_HIGH)
277041016Sdfr     * byte 3 report rate (?)
277141016Sdfr     */
277241016Sdfr
277341016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
277441016Sdfr        return FALSE;
277541016Sdfr    if ((status[1] != '3') || (status[2] != 'D'))
277641016Sdfr        return FALSE;
277758230Syokota    /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
277841016Sdfr    sc->hw.buttons = 4;
277941016Sdfr    return TRUE;
278041016Sdfr}
278141016Sdfr
278258230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
278341016Sdfrstatic int
278441016Sdfrenable_gmouse(struct psm_softc *sc)
278541016Sdfr{
278641016Sdfr    int status[3];
278741016Sdfr
278841016Sdfr    /*
278941016Sdfr     * The special sequence to enable the middle, "rubber" button.
279041016Sdfr     * Immediately after this sequence check status bytes.
279141016Sdfr     * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
279241016Sdfr     * the second and the third bytes are '3' and 'U'.
279341016Sdfr     * NOTE: NetMouse reports that it has three buttons although it has
279441016Sdfr     * two buttons and a rubber button. NetMouse Pro and MIE Mouse
279541016Sdfr     * say they have three buttons too and they do have a button on the
279641016Sdfr     * side...
279741016Sdfr     */
279841016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
279941016Sdfr        return FALSE;
280041016Sdfr    if ((status[1] != '3') || (status[2] != 'U'))
280141016Sdfr        return FALSE;
280241016Sdfr    return TRUE;
280341016Sdfr}
280441016Sdfr
280541016Sdfr/* ALPS GlidePoint */
280641016Sdfrstatic int
280741016Sdfrenable_aglide(struct psm_softc *sc)
280841016Sdfr{
280941016Sdfr    int status[3];
281041016Sdfr
281141016Sdfr    /*
281241016Sdfr     * The special sequence to obtain ALPS GlidePoint specific
281341016Sdfr     * information. Immediately after this sequence, status bytes will
281441016Sdfr     * contain something interesting.
281541016Sdfr     * NOTE: ALPS produces several models of GlidePoint. Some of those
281641016Sdfr     * do not respond to this sequence, thus, cannot be detected this way.
281741016Sdfr     */
281850149Syokota    if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
281950149Syokota	return FALSE;
282041016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
282141016Sdfr        return FALSE;
282250149Syokota    if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
282341016Sdfr        return FALSE;
282441016Sdfr    return TRUE;
282541016Sdfr}
282641016Sdfr
282741016Sdfr/* Kensington ThinkingMouse/Trackball */
282841016Sdfrstatic int
282941016Sdfrenable_kmouse(struct psm_softc *sc)
283041016Sdfr{
283141016Sdfr    static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
283241016Sdfr    KBDC kbdc = sc->kbdc;
283341016Sdfr    int status[3];
283441016Sdfr    int id1;
283541016Sdfr    int id2;
283641016Sdfr    int i;
283741016Sdfr
283841016Sdfr    id1 = get_aux_id(kbdc);
283941016Sdfr    if (set_mouse_sampling_rate(kbdc, 10) != 10)
284041016Sdfr	return FALSE;
284141016Sdfr    /*
284241016Sdfr     * The device is now in the native mode? It returns a different
284341016Sdfr     * ID value...
284441016Sdfr     */
284541016Sdfr    id2 = get_aux_id(kbdc);
284641016Sdfr    if ((id1 == id2) || (id2 != 2))
284741016Sdfr	return FALSE;
284841016Sdfr
284941016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
285041016Sdfr        return FALSE;
285141016Sdfr#if PSM_DEBUG >= 2
285241016Sdfr    /* at this point, resolution is LOW, sampling rate is 10/sec */
285341016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
285441016Sdfr        return FALSE;
285541016Sdfr#endif
285641016Sdfr
285741016Sdfr    /*
285841016Sdfr     * The special sequence to enable the third and fourth buttons.
285941016Sdfr     * Otherwise they behave like the first and second buttons.
286041016Sdfr     */
286141016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
286241016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
286341016Sdfr	    return FALSE;
286441016Sdfr    }
286541016Sdfr
286641016Sdfr    /*
286741016Sdfr     * At this point, the device is using default resolution and
286841016Sdfr     * sampling rate for the native mode.
286941016Sdfr     */
287041016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
287141016Sdfr        return FALSE;
287241016Sdfr    if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
287341016Sdfr        return FALSE;
287441016Sdfr
287541016Sdfr    /* the device appears be enabled by this sequence, diable it for now */
287641016Sdfr    disable_aux_dev(kbdc);
287741016Sdfr    empty_aux_buffer(kbdc, 5);
287841016Sdfr
287941016Sdfr    return TRUE;
288041016Sdfr}
288141016Sdfr
288258230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
288341016Sdfrstatic int
288441016Sdfrenable_mmanplus(struct psm_softc *sc)
288541016Sdfr{
288641016Sdfr    KBDC kbdc = sc->kbdc;
288741016Sdfr    int data[3];
288841016Sdfr
288941016Sdfr    /* the special sequence to enable the fourth button and the roller. */
289058230Syokota    /*
289158230Syokota     * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
289258230Syokota     * must be called exactly three times since the last RESET command
289358230Syokota     * before this sequence. XXX
289458230Syokota     */
289569438Syokota    if (!set_mouse_scaling(kbdc, 1))
289669438Syokota	return FALSE;
289769438Syokota    if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
289869438Syokota	return FALSE;
289941016Sdfr    if (get_mouse_status(kbdc, data, 1, 3) < 3)
290041016Sdfr        return FALSE;
290141016Sdfr
290248778Syokota    /*
290348778Syokota     * PS2++ protocl, packet type 0
290441016Sdfr     *
290548778Syokota     *          b7 b6 b5 b4 b3 b2 b1 b0
290648778Syokota     * byte 1:  *  1  p3 p2 1  *  *  *
290748778Syokota     * byte 2:  1  1  p1 p0 m1 m0 1  0
290848778Syokota     * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
290948778Syokota     *
291048778Syokota     * p3-p0: packet type: 0
291158230Syokota     * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
291241016Sdfr     */
291348778Syokota    /* check constant bits */
291448778Syokota    if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
291541016Sdfr        return FALSE;
291648778Syokota    if ((data[1] & 0xc3) != 0xc2)
291748778Syokota        return FALSE;
291848778Syokota    /* check d3-d0 in byte 2 */
291948778Syokota    if (!MOUSE_PS2PLUS_CHECKBITS(data))
292048778Syokota        return FALSE;
292148778Syokota    /* check p3-p0 */
292248778Syokota    if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
292348778Syokota        return FALSE;
292441016Sdfr
292548778Syokota    sc->hw.hwid &= 0x00ff;
292648778Syokota    sc->hw.hwid |= data[2] << 8;	/* save model ID */
292748778Syokota
292841016Sdfr    /*
292941016Sdfr     * MouseMan+ (or FirstMouse+) is now in its native mode, in which
293041016Sdfr     * the wheel and the fourth button events are encoded in the
293141016Sdfr     * special data packet. The mouse may be put in the IntelliMouse mode
293241016Sdfr     * if it is initialized by the IntelliMouse's method.
293341016Sdfr     */
293441016Sdfr    return TRUE;
293541016Sdfr}
293641016Sdfr
293758230Syokota/* MS IntelliMouse Explorer */
293858230Syokotastatic int
293958230Syokotaenable_msexplorer(struct psm_softc *sc)
294058230Syokota{
294158923Syokota    static unsigned char rate0[] = { 200, 100, 80, };
294258923Syokota    static unsigned char rate1[] = { 200, 200, 80, };
294358230Syokota    KBDC kbdc = sc->kbdc;
294458230Syokota    int id;
294558230Syokota    int i;
294658230Syokota
294769438Syokota    /* the special sequence to enable the extra buttons and the roller. */
294869438Syokota    for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
294969438Syokota        if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
295069438Syokota	    return FALSE;
295169438Syokota    }
295269438Syokota    /* the device will give the genuine ID only after the above sequence */
295369438Syokota    id = get_aux_id(kbdc);
295469438Syokota    if (id != PSM_EXPLORER_ID)
295569438Syokota	return FALSE;
295669438Syokota
295769438Syokota    sc->hw.hwid = id;
295869438Syokota    sc->hw.buttons = 5;		/* IntelliMouse Explorer XXX */
295969438Syokota
296058923Syokota    /*
296158923Syokota     * XXX: this is a kludge to fool some KVM switch products
296258923Syokota     * which think they are clever enough to know the 4-byte IntelliMouse
296358923Syokota     * protocol, and assume any other protocols use 3-byte packets.
296458923Syokota     * They don't convey 4-byte data packets from the IntelliMouse Explorer
296558923Syokota     * correctly to the host computer because of this!
296658923Syokota     * The following sequence is actually IntelliMouse's "wake up"
296758923Syokota     * sequence; it will make the KVM think the mouse is IntelliMouse
296858923Syokota     * when it is in fact IntelliMouse Explorer.
296958923Syokota     */
297058923Syokota    for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
297158923Syokota        if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
297269438Syokota	    break;
297358923Syokota    }
297458923Syokota    id = get_aux_id(kbdc);
297558923Syokota
297658230Syokota    return TRUE;
297758230Syokota}
297858230Syokota
297941016Sdfr/* MS IntelliMouse */
298041016Sdfrstatic int
298141016Sdfrenable_msintelli(struct psm_softc *sc)
298241016Sdfr{
298341016Sdfr    /*
298441016Sdfr     * Logitech MouseMan+ and FirstMouse+ will also respond to this
298541016Sdfr     * probe routine and act like IntelliMouse.
298641016Sdfr     */
298741016Sdfr
298841016Sdfr    static unsigned char rate[] = { 200, 100, 80, };
298941016Sdfr    KBDC kbdc = sc->kbdc;
299041016Sdfr    int id;
299141016Sdfr    int i;
299241016Sdfr
299341016Sdfr    /* the special sequence to enable the third button and the roller. */
299441016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
299541016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
299641016Sdfr	    return FALSE;
299741016Sdfr    }
299841016Sdfr    /* the device will give the genuine ID only after the above sequence */
299941016Sdfr    id = get_aux_id(kbdc);
300041016Sdfr    if (id != PSM_INTELLI_ID)
300141016Sdfr	return FALSE;
300241016Sdfr
300341016Sdfr    sc->hw.hwid = id;
300441016Sdfr    sc->hw.buttons = 3;
300541016Sdfr
300641016Sdfr    return TRUE;
300741016Sdfr}
300841016Sdfr
300958230Syokota/* A4 Tech 4D Mouse */
301058230Syokotastatic int
301158230Syokotaenable_4dmouse(struct psm_softc *sc)
301258230Syokota{
301358230Syokota    /*
301458230Syokota     * Newer wheel mice from A4 Tech may use the 4D+ protocol.
301558230Syokota     */
301658230Syokota
301758230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
301858230Syokota    KBDC kbdc = sc->kbdc;
301958230Syokota    int id;
302058230Syokota    int i;
302158230Syokota
302258230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
302358230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
302458230Syokota	    return FALSE;
302558230Syokota    }
302658230Syokota    id = get_aux_id(kbdc);
302758230Syokota    /*
302858923Syokota     * WinEasy 4D, 4 Way Scroll 4D: 6
302958230Syokota     * Cable-Free 4D: 8 (4DPLUS)
303058923Syokota     * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
303158230Syokota     */
303258230Syokota    if (id != PSM_4DMOUSE_ID)
303358230Syokota	return FALSE;
303458230Syokota
303558230Syokota    sc->hw.hwid = id;
303658230Syokota    sc->hw.buttons = 3;		/* XXX some 4D mice have 4? */
303758230Syokota
303858230Syokota    return TRUE;
303958230Syokota}
304058230Syokota
304158230Syokota/* A4 Tech 4D+ Mouse */
304258230Syokotastatic int
304358230Syokotaenable_4dplus(struct psm_softc *sc)
304458230Syokota{
304558230Syokota    /*
304658230Syokota     * Newer wheel mice from A4 Tech seem to use this protocol.
304758230Syokota     * Older models are recognized as either 4D Mouse or IntelliMouse.
304858230Syokota     */
304958230Syokota    KBDC kbdc = sc->kbdc;
305058230Syokota    int id;
305158230Syokota
305258230Syokota    /*
305358230Syokota     * enable_4dmouse() already issued the following ID sequence...
305458230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
305558230Syokota    int i;
305658230Syokota
305758230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
305858230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
305958230Syokota	    return FALSE;
306058230Syokota    }
306158230Syokota    */
306258230Syokota
306358230Syokota    id = get_aux_id(kbdc);
3064117478Smikeh    switch (id) {
3065117478Smikeh    case PSM_4DPLUS_ID:
3066117478Smikeh	    sc->hw.buttons = 4;
3067117478Smikeh	    break;
3068117478Smikeh    case PSM_4DPLUS_RFSW35_ID:
3069117478Smikeh	    sc->hw.buttons = 3;
3070117478Smikeh	    break;
3071117478Smikeh    default:
3072117478Smikeh	    return FALSE;
3073117478Smikeh    }
307458230Syokota
307558230Syokota    sc->hw.hwid = id;
307658230Syokota
307758230Syokota    return TRUE;
307858230Syokota}
307958230Syokota
3080132865Snjl/* Synaptics Touchpad */
3081132865Snjlstatic int
3082132865Snjlenable_synaptics(struct psm_softc *sc)
3083132865Snjl{
3084132865Snjl    int status[3];
3085132865Snjl    KBDC kbdc;
3086132865Snjl
3087132865Snjl    kbdc = sc->kbdc;
3088132865Snjl    disable_aux_dev(kbdc);
3089132865Snjl
3090132865Snjl    /* Just to be on the safe side */
3091132865Snjl    set_mouse_scaling(kbdc, 1);
3092132865Snjl
3093133295Sphilip    /* Identify the Touchpad version */
3094132865Snjl    if (mouse_ext_command(kbdc, 0) == 0)
3095132865Snjl	return (FALSE);
3096132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3097132865Snjl	return (FALSE);
3098132865Snjl    if (status[1] != 0x47)
3099132865Snjl	return (FALSE);
3100132865Snjl
3101132865Snjl    sc->synhw.infoMinor = status[0];
3102132865Snjl    sc->synhw.infoMajor = status[2] & 0x0f;
3103133295Sphilip
3104133295Sphilip    if (verbose >= 2)
3105133295Sphilip	printf("Synaptics Touchpad v%d.%d\n",
3106133295Sphilip	    sc->synhw.infoMajor, sc->synhw.infoMinor);
3107133295Sphilip
3108132865Snjl    if (sc->synhw.infoMajor < 4) {
3109133295Sphilip	printf("  Unsupported (pre-v4) Touchpad detected\n");
3110132865Snjl	return (FALSE);
3111132865Snjl    }
3112132865Snjl
3113133295Sphilip    /* Get the Touchpad model information */
3114132865Snjl    if (mouse_ext_command(kbdc, 3) == 0)
3115132865Snjl	return (FALSE);
3116132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3117132865Snjl	return (FALSE);
3118132865Snjl    if ((status[1] & 0x01) != 0) {
3119133295Sphilip	printf("  Failed to read model information\n");
3120132865Snjl	return (FALSE);
3121132865Snjl    }
3122132865Snjl
3123132865Snjl    sc->synhw.infoRot180   = (status[0] & 0x80) >> 7;
3124132865Snjl    sc->synhw.infoPortrait = (status[0] & 0x40) >> 6;
3125132865Snjl    sc->synhw.infoSensor   =  status[0] & 0x3f;
3126132865Snjl    sc->synhw.infoHardware = (status[1] & 0xfe) >> 1;
3127132865Snjl    sc->synhw.infoNewAbs   = (status[2] & 0x80) >> 7;
3128132865Snjl    sc->synhw.capPen       = (status[2] & 0x40) >> 6;
3129132865Snjl    sc->synhw.infoSimplC   = (status[2] & 0x20) >> 5;
3130132865Snjl    sc->synhw.infoGeometry =  status[2] & 0x0f;
3131133295Sphilip
3132132865Snjl    if (verbose >= 2) {
3133133295Sphilip	printf("  Model information:\n");
3134133295Sphilip	printf("   infoRot180: %d\n", sc->synhw.infoRot180);
3135133295Sphilip	printf("   infoPortrait: %d\n", sc->synhw.infoPortrait);
3136133295Sphilip	printf("   infoSensor: %d\n", sc->synhw.infoSensor);
3137133295Sphilip	printf("   infoHardware: %d\n", sc->synhw.infoHardware);
3138133295Sphilip	printf("   infoNewAbs: %d\n", sc->synhw.infoNewAbs);
3139133295Sphilip	printf("   capPen: %d\n", sc->synhw.capPen);
3140133295Sphilip	printf("   infoSimplC: %d\n", sc->synhw.infoSimplC);
3141133295Sphilip	printf("   infoGeometry: %d\n", sc->synhw.infoGeometry);
3142132865Snjl    }
3143132865Snjl
3144133295Sphilip    /* Read the extended capability bits */
3145132865Snjl    if (mouse_ext_command(kbdc, 2) == 0)
3146132865Snjl	return (FALSE);
3147132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3148132865Snjl	return (FALSE);
3149132865Snjl    if (status[1] != 0x47) {
3150133295Sphilip	printf("  Failed to read extended capability bits\n");
3151132865Snjl	return (FALSE);
3152132865Snjl    }
3153132865Snjl
3154133295Sphilip    /* Set the different capabilities when they exist */
3155133295Sphilip    if ((status[0] & 0x80) >> 7) {
3156133295Sphilip	sc->synhw.capExtended    = (status[0] & 0x80) >> 7;
3157133295Sphilip    	sc->synhw.capPassthrough = (status[2] & 0x80) >> 7;
3158133295Sphilip    	sc->synhw.capSleep       = (status[2] & 0x10) >> 4;
3159133295Sphilip    	sc->synhw.capFourButtons = (status[2] & 0x08) >> 3;
3160133295Sphilip    	sc->synhw.capMultiFinger = (status[2] & 0x02) >> 1;
3161133295Sphilip    	sc->synhw.capPalmDetect  = (status[2] & 0x01);
3162133295Sphilip
3163133295Sphilip	if (verbose >= 2) {
3164133295Sphilip	    printf("  Extended capabilities:\n");
3165133295Sphilip	    printf("   capExtended: %d\n", sc->synhw.capExtended);
3166133295Sphilip	    printf("   capPassthrough: %d\n", sc->synhw.capPassthrough);
3167133295Sphilip	    printf("   capSleep: %d\n", sc->synhw.capSleep);
3168133295Sphilip	    printf("   capFourButtons: %d\n", sc->synhw.capFourButtons);
3169133295Sphilip	    printf("   capMultiFinger: %d\n", sc->synhw.capMultiFinger);
3170133295Sphilip	    printf("   capPalmDetect: %d\n", sc->synhw.capPalmDetect);
3171133295Sphilip	}
3172133295Sphilip    } else {
3173133295Sphilip	sc->synhw.capExtended = 0;
3174133295Sphilip
3175133295Sphilip	if (verbose >= 2)
3176133295Sphilip	    printf("  No extended capabilities\n");
3177133295Sphilip    }
3178133295Sphilip
3179133295Sphilip    /*
3180133295Sphilip     * Read the mode byte
3181133295Sphilip     *
3182133295Sphilip     * XXX: Note the Synaptics documentation also defines the first
3183133295Sphilip     * byte of the response to this query to be a constant 0x3b, this
3184133295Sphilip     * does not appear to be true for Touchpads with guest devices.
3185133295Sphilip     */
3186132865Snjl    if (mouse_ext_command(kbdc, 1) == 0)
3187132865Snjl	return (FALSE);
3188132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3189132865Snjl	return (FALSE);
3190133295Sphilip    if (status[1] != 0x47) {
3191133295Sphilip	printf("  Failed to read mode byte\n");
3192132865Snjl	return (FALSE);
3193132865Snjl    }
3194132865Snjl
3195133295Sphilip    /* Set the mode byte -- request wmode where available */
3196133295Sphilip    if (sc->synhw.capExtended)
3197133295Sphilip	mouse_ext_command(kbdc, 0xc1);
3198133295Sphilip    else
3199133295Sphilip	mouse_ext_command(kbdc, 0xc0);
3200132865Snjl
3201133295Sphilip    /* Reset the sampling rate */
3202132865Snjl    set_mouse_sampling_rate(kbdc, 20);
3203132865Snjl
3204133296Sphilip    /*
3205133296Sphilip     * Report the correct number of buttons
3206133296Sphilip     *
3207133296Sphilip     * XXX: I'm not sure this is used anywhere.
3208133296Sphilip     */
3209133296Sphilip    if (sc->synhw.capExtended && sc->synhw.capFourButtons)
3210133296Sphilip	sc->hw.buttons = 4;
3211133296Sphilip    else
3212133296Sphilip	sc->hw.buttons = 3;
3213133296Sphilip
3214132865Snjl    return (TRUE);
3215132865Snjl}
3216133295Sphilip
321749965Syokota/* Interlink electronics VersaPad */
321849965Syokotastatic int
321949965Syokotaenable_versapad(struct psm_softc *sc)
322049965Syokota{
322149965Syokota    KBDC kbdc = sc->kbdc;
322249965Syokota    int data[3];
322349965Syokota
322449965Syokota    set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
322549965Syokota    set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
322649965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
322749965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
322849965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
322949965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
323049965Syokota    if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
323149965Syokota	return FALSE;
323249965Syokota    if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
323349965Syokota	return FALSE;
323449965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
323549965Syokota
323658230Syokota    sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
323758230Syokota
323849965Syokota    return TRUE;				/* PS/2 absolute mode */
323949965Syokota}
324049965Syokota
324141016Sdfrstatic int
324254629Syokotapsmresume(device_t dev)
324341016Sdfr{
324454629Syokota    struct psm_softc *sc = device_get_softc(dev);
324554629Syokota    int unit = device_get_unit(dev);
324684880Syokota    int err;
324741016Sdfr
324841016Sdfr    if (verbose >= 2)
324954629Syokota        log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
325041016Sdfr
325158230Syokota    if (!(sc->config & PSM_CONFIG_HOOKRESUME))
325258230Syokota	return (0);
325358230Syokota
325484880Syokota    err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
325541016Sdfr
325641016Sdfr    if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
325741016Sdfr	/*
325841016Sdfr	 * Release the blocked process; it must be notified that the device
325941016Sdfr	 * cannot be accessed anymore.
326041016Sdfr	 */
326141016Sdfr        sc->state &= ~PSM_ASLP;
3262111748Sdes        wakeup(sc);
326341016Sdfr    }
326441016Sdfr
326541016Sdfr    if (verbose >= 2)
326654629Syokota        log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
326741016Sdfr
326841016Sdfr    return (err);
326941016Sdfr}
327041016Sdfr
327152997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
327283147Syokota
327383147Syokota/*
327483147Syokota * This sucks up assignments from PNPBIOS and ACPI.
327583147Syokota */
327683147Syokota
327783931Syokota/*
327883931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
327983931Syokota * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
328083931Syokota * can be probed and attached only after the AT keyboard controller is
328183931Syokota * attached, we shall quietly reserve the IRQ resource for later use.
328283931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
328383931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging
328483931Syokota * under the keyboard controller, then probe and attach it.
328583931Syokota */
328683147Syokota
328783147Syokotastatic	devclass_t			psmcpnp_devclass;
328883147Syokota
328983147Syokotastatic	device_probe_t			psmcpnp_probe;
329083147Syokotastatic	device_attach_t			psmcpnp_attach;
329183147Syokota
329283147Syokotastatic device_method_t psmcpnp_methods[] = {
329383147Syokota	DEVMETHOD(device_probe,		psmcpnp_probe),
329483147Syokota	DEVMETHOD(device_attach,	psmcpnp_attach),
329583147Syokota
329683147Syokota	{ 0, 0 }
329783147Syokota};
329883147Syokota
329983147Syokotastatic driver_t psmcpnp_driver = {
330083147Syokota	PSMCPNP_DRIVER_NAME,
330183147Syokota	psmcpnp_methods,
330283147Syokota	1,			/* no softc */
330383147Syokota};
330483147Syokota
330583147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = {
330688188Ssheldonh	{ 0x030fd041, "PS/2 mouse port" },		/* PNP0F03 */
330783147Syokota	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
330883147Syokota	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
3309117117Smikeh	{ 0x02002e4f, "Dell PS/2 mouse port" },		/* Lat. X200, Dell */
331083492Syokota	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
331184407Stakawata	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
3312109679Shsu	{ 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
3313109679Shsu	{ 0x0290d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9002, Vaio */
3314109710Smarcel	{ 0x0390d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9003, Vaio */
3315109679Shsu	{ 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
331683147Syokota	{ 0 }
331783147Syokota};
331883147Syokota
331983147Syokotastatic int
332083147Syokotacreate_a_copy(device_t atkbdc, device_t me)
332183147Syokota{
332283147Syokota	device_t psm;
332383147Syokota	u_long irq;
332483147Syokota
332583931Syokota	/* find the PS/2 mouse device instance under the keyboard controller */
332683931Syokota	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
332783931Syokota				device_get_unit(atkbdc));
332883147Syokota	if (psm == NULL)
332983147Syokota		return ENXIO;
333083931Syokota	if (device_get_state(psm) != DS_NOTPRESENT)
333183931Syokota		return 0;
333283147Syokota
333383931Syokota	/* move our resource to the found device */
333483931Syokota	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
333583147Syokota	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
333683147Syokota
333783147Syokota	/* ...then probe and attach it */
333883147Syokota	return device_probe_and_attach(psm);
333983147Syokota}
334083147Syokota
334183147Syokotastatic int
334283147Syokotapsmcpnp_probe(device_t dev)
334383147Syokota{
334483931Syokota	struct resource *res;
334583931Syokota	u_long irq;
334683931Syokota	int rid;
334783147Syokota
334883147Syokota	if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
334983147Syokota		return ENXIO;
335083147Syokota
335183931Syokota	/*
335283931Syokota	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
335383931Syokota	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
335483931Syokota	 * declares the PS/2 mouse device node without an IRQ resource!
335583931Syokota	 * If this happens, we shall refer to device hints.
335683931Syokota	 * If we still don't find it there, use a hardcoded value... XXX
335783931Syokota	 */
335883931Syokota	rid = 0;
335983931Syokota	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
336083931Syokota	if (irq <= 0) {
336183931Syokota		if (resource_long_value(PSM_DRIVER_NAME,
336283931Syokota					device_get_unit(dev), "irq", &irq) != 0)
336383931Syokota			irq = 12;	/* XXX */
336483931Syokota		device_printf(dev, "irq resource info is missing; "
336583931Syokota			      "assuming irq %ld\n", irq);
336683931Syokota		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
336783931Syokota	}
3368127135Snjl	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3369127135Snjl				     RF_SHAREABLE);
337083931Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
337183147Syokota
337283147Syokota	/* keep quiet */
337383147Syokota	if (!bootverbose)
337483147Syokota		device_quiet(dev);
337583492Syokota
337683931Syokota	return ((res == NULL) ? ENXIO : 0);
337783147Syokota}
337883147Syokota
337983147Syokotastatic int
338083147Syokotapsmcpnp_attach(device_t dev)
338183147Syokota{
338283492Syokota	device_t atkbdc;
338383931Syokota	int rid;
338483147Syokota
338583931Syokota	/* find the keyboard controller, which may be on acpi* or isa* bus */
338683931Syokota	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
338783931Syokota				     device_get_unit(dev));
338883931Syokota	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
338983492Syokota		create_a_copy(atkbdc, dev);
339083931Syokota	} else {
339183931Syokota		/*
339283931Syokota		 * If we don't have the AT keyboard controller yet,
339383931Syokota		 * just reserve the IRQ for later use...
339483931Syokota		 * (See psmidentify() above.)
339583931Syokota		 */
339683931Syokota		rid = 0;
3397127135Snjl		bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
339883931Syokota	}
339983492Syokota
340083147Syokota	return 0;
340183147Syokota}
340283147Syokota
3403123442Salfred/*
3404123442Salfred * Return true if 'now' is earlier than (start + (secs.usecs)).
3405123442Salfred * Now may be NULL and the function will fetch the current time from
3406123442Salfred * getmicrouptime(), or a cached 'now' can be passed in.
3407123442Salfred * All values should be numbers derived from getmicrouptime().
3408123442Salfred */
3409123442Salfredstatic int
3410123442Salfredtimeelapsed(start, secs, usecs, now)
3411123442Salfred	const struct timeval *start, *now;
3412123442Salfred	int secs, usecs;
3413123442Salfred{
3414123442Salfred	struct timeval snow, tv;
3415123442Salfred
3416123442Salfred	/* if there is no 'now' passed in, the get it as a convience. */
3417123442Salfred	if (now == NULL) {
3418123442Salfred		getmicrouptime(&snow);
3419123442Salfred		now = &snow;
3420123442Salfred	}
3421123442Salfred
3422123442Salfred	tv.tv_sec = secs;
3423123442Salfred	tv.tv_usec = usecs;
3424123442Salfred	timevaladd(&tv, start);
3425123442Salfred	return (timevalcmp(&tv, now, <));
3426123442Salfred}
3427123442Salfred
342883147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
342983147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
3430