psm.c revision 133298
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 133298 2004-08-08 01:10:23Z philip $");
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 */
20341016Sdfr
20441016Sdfr/* driver configuration flags (config) */
20541016Sdfr#define PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
20641016Sdfr#define PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
20741016Sdfr#define PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
20845789Speter#define PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
20945789Speter#define PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
21045789Speter#define PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
21145789Speter#define PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
21258230Syokota#define PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
21358230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
21469439Syokota#define PSM_CONFIG_SYNCHACK	0x8000 /* enable `out-of-sync' hack */
21541016Sdfr
21641016Sdfr#define PSM_CONFIG_FLAGS	(PSM_CONFIG_RESOLUTION 		\
21741016Sdfr				    | PSM_CONFIG_ACCEL		\
21845789Speter				    | PSM_CONFIG_NOCHECKSYNC	\
21969439Syokota				    | PSM_CONFIG_SYNCHACK	\
22045789Speter				    | PSM_CONFIG_NOIDPROBE	\
22145789Speter				    | PSM_CONFIG_NORESET	\
22245789Speter				    | PSM_CONFIG_FORCETAP	\
22358230Syokota				    | PSM_CONFIG_IGNPORTERROR	\
22458230Syokota				    | PSM_CONFIG_HOOKRESUME	\
22558230Syokota				    | PSM_CONFIG_INITAFTERSUSPEND)
22641016Sdfr
22741016Sdfr/* other flags (flags) */
22849965Syokota#define PSM_FLAGS_FINGERDOWN	0x0001 /* VersaPad finger down */
22941016Sdfr
23041016Sdfr/* for backward compatibility */
23141016Sdfr#define OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
23241016Sdfr#define OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
23341016Sdfr#define OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
23441016Sdfr
23541016Sdfrtypedef struct old_mousehw {
23641016Sdfr    int buttons;
23741016Sdfr    int iftype;
23841016Sdfr    int type;
23941016Sdfr    int hwid;
24041016Sdfr} old_mousehw_t;
24141016Sdfr
24241016Sdfrtypedef struct old_mousemode {
24341016Sdfr    int protocol;
24441016Sdfr    int rate;
24541016Sdfr    int resolution;
24641016Sdfr    int accelfactor;
24741016Sdfr} old_mousemode_t;
24841016Sdfr
24941016Sdfr/* packet formatting function */
25092756Salfredtypedef int packetfunc_t(struct psm_softc *, unsigned char *,
25192756Salfred			      int *, int, mousestatus_t *);
25241016Sdfr
25341016Sdfr/* function prototypes */
25492756Salfredstatic void psmidentify(driver_t *, device_t);
25592756Salfredstatic int psmprobe(device_t);
25692756Salfredstatic int psmattach(device_t);
25792756Salfredstatic int psmdetach(device_t);
25892756Salfredstatic int psmresume(device_t);
25941016Sdfr
26041016Sdfrstatic d_open_t psmopen;
26141016Sdfrstatic d_close_t psmclose;
26241016Sdfrstatic d_read_t psmread;
26341016Sdfrstatic d_ioctl_t psmioctl;
26441016Sdfrstatic d_poll_t psmpoll;
26541016Sdfr
26692756Salfredstatic int enable_aux_dev(KBDC);
26792756Salfredstatic int disable_aux_dev(KBDC);
26892756Salfredstatic int get_mouse_status(KBDC, int *, int, int);
26992756Salfredstatic int get_aux_id(KBDC);
27092756Salfredstatic int set_mouse_sampling_rate(KBDC, int);
27192756Salfredstatic int set_mouse_scaling(KBDC, int);
27292756Salfredstatic int set_mouse_resolution(KBDC, int);
27392756Salfredstatic int set_mouse_mode(KBDC);
27492756Salfredstatic int get_mouse_buttons(KBDC);
27592756Salfredstatic int is_a_mouse(int);
27692756Salfredstatic void recover_from_error(KBDC);
27792756Salfredstatic int restore_controller(KBDC, int);
27892756Salfredstatic int doinitialize(struct psm_softc *, mousemode_t *);
27992756Salfredstatic int doopen(struct psm_softc *, int);
28092756Salfredstatic int reinitialize(struct psm_softc *, int);
28192756Salfredstatic char *model_name(int);
282123442Salfredstatic void psmsoftintr(void *);
28392756Salfredstatic void psmintr(void *);
28492756Salfredstatic void psmtimeout(void *);
285123442Salfredstatic int timeelapsed(const struct timeval *,
286123442Salfred    int, int, const struct timeval *);
287123442Salfredstatic void dropqueue(struct psm_softc *);
288123442Salfredstatic void flushpackets(struct psm_softc *);
28941016Sdfr
29041016Sdfr/* vendor specific features */
29192756Salfredtypedef int probefunc_t(struct psm_softc *);
29241016Sdfr
29392756Salfredstatic int mouse_id_proc1(KBDC, int, int, int *);
29492756Salfredstatic int mouse_ext_command(KBDC, int);
29541016Sdfrstatic probefunc_t enable_groller;
29641016Sdfrstatic probefunc_t enable_gmouse;
29741016Sdfrstatic probefunc_t enable_aglide;
29841016Sdfrstatic probefunc_t enable_kmouse;
29958230Syokotastatic probefunc_t enable_msexplorer;
30041016Sdfrstatic probefunc_t enable_msintelli;
30158230Syokotastatic probefunc_t enable_4dmouse;
30258230Syokotastatic probefunc_t enable_4dplus;
30341016Sdfrstatic probefunc_t enable_mmanplus;
304132865Snjlstatic probefunc_t enable_synaptics;
30549965Syokotastatic probefunc_t enable_versapad;
306123442Salfredstatic int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, unsigned char *);
30741016Sdfr
30841016Sdfrstatic struct {
30941016Sdfr    int                 model;
31041016Sdfr    unsigned char	syncmask;
31141016Sdfr    int 		packetsize;
31241016Sdfr    probefunc_t 	*probefunc;
31341016Sdfr} vendortype[] = {
31458230Syokota    /*
31558230Syokota     * WARNING: the order of probe is very important.  Don't mess it
31658230Syokota     * unless you know what you are doing.
31758230Syokota     */
31841016Sdfr    { MOUSE_MODEL_NET,			/* Genius NetMouse */
31958230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
32041016Sdfr    { MOUSE_MODEL_NETSCROLL,		/* Genius NetScroll */
32141016Sdfr      0xc8, 6, enable_groller, },
32258230Syokota    { MOUSE_MODEL_MOUSEMANPLUS,		/* Logitech MouseMan+ */
32358230Syokota      0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
32458230Syokota    { MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
32558230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
32658230Syokota    { MOUSE_MODEL_4D,			/* A4 Tech 4D Mouse */
32758230Syokota      0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
32858230Syokota    { MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
32958230Syokota      0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
33058230Syokota    { MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
33158230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
33241016Sdfr    { MOUSE_MODEL_GLIDEPOINT,		/* ALPS GlidePoint */
33341016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
33441016Sdfr    { MOUSE_MODEL_THINK,		/* Kensignton ThinkingMouse */
33541016Sdfr      0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
33649965Syokota    { MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
33749965Syokota      0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
338132865Snjl    { MOUSE_MODEL_SYNAPTICS,		/* Synaptics Touchpad */
339132865Snjl      0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics, },
34041016Sdfr    { MOUSE_MODEL_GENERIC,
34141016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
34241016Sdfr};
34363951Syokota#define GENERIC_MOUSE_ENTRY	((sizeof(vendortype) / sizeof(*vendortype)) - 1)
34441016Sdfr
34541016Sdfr/* device driver declarateion */
34641016Sdfrstatic device_method_t psm_methods[] = {
34741016Sdfr	/* Device interface */
34883147Syokota	DEVMETHOD(device_identify,	psmidentify),
34941016Sdfr	DEVMETHOD(device_probe,		psmprobe),
35041016Sdfr	DEVMETHOD(device_attach,	psmattach),
35158230Syokota	DEVMETHOD(device_detach,	psmdetach),
35254629Syokota	DEVMETHOD(device_resume,	psmresume),
35341016Sdfr
35441016Sdfr	{ 0, 0 }
35541016Sdfr};
35641016Sdfr
35741016Sdfrstatic driver_t psm_driver = {
35883492Syokota    PSM_DRIVER_NAME,
35941016Sdfr    psm_methods,
36041016Sdfr    sizeof(struct psm_softc),
36141016Sdfr};
36241016Sdfr
36341016Sdfr
36447625Sphkstatic struct cdevsw psm_cdevsw = {
365126080Sphk	.d_version =	D_VERSION,
366126080Sphk	.d_flags =	D_NEEDGIANT,
367111815Sphk	.d_open =	psmopen,
368111815Sphk	.d_close =	psmclose,
369111815Sphk	.d_read =	psmread,
370111815Sphk	.d_ioctl =	psmioctl,
371111815Sphk	.d_poll =	psmpoll,
372111815Sphk	.d_name =	PSM_DRIVER_NAME,
37341016Sdfr};
37441016Sdfr
37541016Sdfr/* debug message level */
37641016Sdfrstatic int verbose = PSM_DEBUG;
37741016Sdfr
37841016Sdfr/* device I/O routines */
37941016Sdfrstatic int
38041016Sdfrenable_aux_dev(KBDC kbdc)
38141016Sdfr{
38241016Sdfr    int res;
38341016Sdfr
38441016Sdfr    res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
38541016Sdfr    if (verbose >= 2)
38641016Sdfr        log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
38741016Sdfr
38841016Sdfr    return (res == PSM_ACK);
38941016Sdfr}
39041016Sdfr
39141016Sdfrstatic int
39241016Sdfrdisable_aux_dev(KBDC kbdc)
39341016Sdfr{
39441016Sdfr    int res;
39541016Sdfr
39641016Sdfr    res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
39741016Sdfr    if (verbose >= 2)
39841016Sdfr        log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
39941016Sdfr
40041016Sdfr    return (res == PSM_ACK);
40141016Sdfr}
40241016Sdfr
40341016Sdfrstatic int
40441016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
40541016Sdfr{
40641016Sdfr    int cmd;
40741016Sdfr    int res;
40841016Sdfr    int i;
40941016Sdfr
41041016Sdfr    switch (flag) {
41141016Sdfr    case 0:
41241016Sdfr    default:
41341016Sdfr	cmd = PSMC_SEND_DEV_STATUS;
41441016Sdfr	break;
41541016Sdfr    case 1:
41641016Sdfr	cmd = PSMC_SEND_DEV_DATA;
41741016Sdfr	break;
41841016Sdfr    }
41941016Sdfr    empty_aux_buffer(kbdc, 5);
42041016Sdfr    res = send_aux_command(kbdc, cmd);
42141016Sdfr    if (verbose >= 2)
42241016Sdfr        log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
42341016Sdfr	    (flag == 1) ? "DATA" : "STATUS", res);
42441016Sdfr    if (res != PSM_ACK)
42541016Sdfr        return 0;
42641016Sdfr
42741016Sdfr    for (i = 0; i < len; ++i) {
42841016Sdfr        status[i] = read_aux_data(kbdc);
42941016Sdfr	if (status[i] < 0)
43041016Sdfr	    break;
43141016Sdfr    }
43241016Sdfr
43341016Sdfr    if (verbose) {
43441016Sdfr        log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
43541016Sdfr            (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
43641016Sdfr    }
43741016Sdfr
43841016Sdfr    return i;
43941016Sdfr}
44041016Sdfr
44141016Sdfrstatic int
44241016Sdfrget_aux_id(KBDC kbdc)
44341016Sdfr{
44441016Sdfr    int res;
44541016Sdfr    int id;
44641016Sdfr
44741016Sdfr    empty_aux_buffer(kbdc, 5);
44841016Sdfr    res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
44941016Sdfr    if (verbose >= 2)
45041016Sdfr        log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
45141016Sdfr    if (res != PSM_ACK)
45241016Sdfr	return (-1);
45341016Sdfr
45441016Sdfr    /* 10ms delay */
45541016Sdfr    DELAY(10000);
45641016Sdfr
45741016Sdfr    id = read_aux_data(kbdc);
45841016Sdfr    if (verbose >= 2)
45941016Sdfr        log(LOG_DEBUG, "psm: device ID: %04x\n", id);
46041016Sdfr
46141016Sdfr    return id;
46241016Sdfr}
46341016Sdfr
46441016Sdfrstatic int
46541016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
46641016Sdfr{
46741016Sdfr    int res;
46841016Sdfr
46941016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
47041016Sdfr    if (verbose >= 2)
47141016Sdfr        log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
47241016Sdfr
47341016Sdfr    return ((res == PSM_ACK) ? rate : -1);
47441016Sdfr}
47541016Sdfr
47641016Sdfrstatic int
47741016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
47841016Sdfr{
47941016Sdfr    int res;
48041016Sdfr
48141016Sdfr    switch (scale) {
48241016Sdfr    case 1:
48341016Sdfr    default:
48441016Sdfr	scale = PSMC_SET_SCALING11;
48541016Sdfr	break;
48641016Sdfr    case 2:
48741016Sdfr	scale = PSMC_SET_SCALING21;
48841016Sdfr	break;
48941016Sdfr    }
49041016Sdfr    res = send_aux_command(kbdc, scale);
49141016Sdfr    if (verbose >= 2)
49241016Sdfr        log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
49341016Sdfr	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
49441016Sdfr
49541016Sdfr    return (res == PSM_ACK);
49641016Sdfr}
49741016Sdfr
49841016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
49941016Sdfrstatic int
50041016Sdfrset_mouse_resolution(KBDC kbdc, int val)
50141016Sdfr{
50241016Sdfr    int res;
50341016Sdfr
50441016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
50541016Sdfr    if (verbose >= 2)
50641016Sdfr        log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
50741016Sdfr
50841016Sdfr    return ((res == PSM_ACK) ? val : -1);
50941016Sdfr}
51041016Sdfr
51141016Sdfr/*
51241016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
51341016Sdfr * re-enabled by calling `enable_aux_dev()'
51441016Sdfr */
51541016Sdfrstatic int
51641016Sdfrset_mouse_mode(KBDC kbdc)
51741016Sdfr{
51841016Sdfr    int res;
51941016Sdfr
52041016Sdfr    res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
52141016Sdfr    if (verbose >= 2)
52241016Sdfr        log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
52341016Sdfr
52441016Sdfr    return (res == PSM_ACK);
52541016Sdfr}
52641016Sdfr
52741016Sdfrstatic int
52841016Sdfrget_mouse_buttons(KBDC kbdc)
52941016Sdfr{
53041016Sdfr    int c = 2;		/* assume two buttons by default */
53141016Sdfr    int status[3];
53241016Sdfr
53341016Sdfr    /*
53441016Sdfr     * NOTE: a special sequence to obtain Logitech Mouse specific
53541016Sdfr     * information: set resolution to 25 ppi, set scaling to 1:1, set
53641016Sdfr     * scaling to 1:1, set scaling to 1:1. Then the second byte of the
53741016Sdfr     * mouse status bytes is the number of available buttons.
53841016Sdfr     * Some manufactures also support this sequence.
53941016Sdfr     */
54041016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
54141016Sdfr        return c;
54241016Sdfr    if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
54341016Sdfr        && set_mouse_scaling(kbdc, 1)
54441016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
54541016Sdfr        if (status[1] != 0)
54641016Sdfr            return status[1];
54741016Sdfr    }
54841016Sdfr    return c;
54941016Sdfr}
55041016Sdfr
55141016Sdfr/* misc subroutines */
55241016Sdfr/*
55341016Sdfr * Someday, I will get the complete list of valid pointing devices and
55441016Sdfr * their IDs... XXX
55541016Sdfr */
55641016Sdfrstatic int
55741016Sdfris_a_mouse(int id)
55841016Sdfr{
55941016Sdfr#if 0
56041016Sdfr    static int valid_ids[] = {
56141016Sdfr        PSM_MOUSE_ID,		/* mouse */
56241016Sdfr        PSM_BALLPOINT_ID,	/* ballpoint device */
56341016Sdfr        PSM_INTELLI_ID,		/* Intellimouse */
56458230Syokota        PSM_EXPLORER_ID,	/* Intellimouse Explorer */
56541016Sdfr        -1			/* end of table */
56641016Sdfr    };
56741016Sdfr    int i;
56841016Sdfr
56941016Sdfr    for (i = 0; valid_ids[i] >= 0; ++i)
57041016Sdfr        if (valid_ids[i] == id)
57141016Sdfr            return TRUE;
57241016Sdfr    return FALSE;
57341016Sdfr#else
57441016Sdfr    return TRUE;
57541016Sdfr#endif
57641016Sdfr}
57741016Sdfr
57841016Sdfrstatic char *
57941016Sdfrmodel_name(int model)
58041016Sdfr{
58141016Sdfr    static struct {
58241016Sdfr	int model_code;
58341016Sdfr	char *model_name;
58441016Sdfr    } models[] = {
58558230Syokota        { MOUSE_MODEL_NETSCROLL,	"NetScroll" },
58658230Syokota        { MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
58741016Sdfr        { MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
58841016Sdfr        { MOUSE_MODEL_THINK,		"ThinkingMouse" },
58941016Sdfr        { MOUSE_MODEL_INTELLI,		"IntelliMouse" },
59041016Sdfr        { MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
59149965Syokota        { MOUSE_MODEL_VERSAPAD,		"VersaPad" },
59258230Syokota        { MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
59358230Syokota        { MOUSE_MODEL_4D,		"4D Mouse" },
59458230Syokota        { MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
595132865Snjl        { MOUSE_MODEL_SYNAPTICS,	"Synaptics Touchpad" },
59641016Sdfr        { MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
59741016Sdfr        { MOUSE_MODEL_UNKNOWN,		NULL },
59841016Sdfr    };
59941016Sdfr    int i;
60041016Sdfr
60141016Sdfr    for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
60241016Sdfr	if (models[i].model_code == model)
60341016Sdfr	    return models[i].model_name;
60441016Sdfr    }
60541016Sdfr    return "Unknown";
60641016Sdfr}
60741016Sdfr
60841016Sdfrstatic void
60941016Sdfrrecover_from_error(KBDC kbdc)
61041016Sdfr{
61141016Sdfr    /* discard anything left in the output buffer */
61241016Sdfr    empty_both_buffers(kbdc, 10);
61341016Sdfr
61441016Sdfr#if 0
61541016Sdfr    /*
61641016Sdfr     * NOTE: KBDC_RESET_KBD may not restore the communication between the
61741016Sdfr     * keyboard and the controller.
61841016Sdfr     */
61941016Sdfr    reset_kbd(kbdc);
62041016Sdfr#else
62141016Sdfr    /*
62241016Sdfr     * NOTE: somehow diagnostic and keyboard port test commands bring the
62341016Sdfr     * keyboard back.
62441016Sdfr     */
62541016Sdfr    if (!test_controller(kbdc))
62641016Sdfr        log(LOG_ERR, "psm: keyboard controller failed.\n");
62741016Sdfr    /* if there isn't a keyboard in the system, the following error is OK */
62841016Sdfr    if (test_kbd_port(kbdc) != 0) {
62941016Sdfr	if (verbose)
63041016Sdfr	    log(LOG_ERR, "psm: keyboard port failed.\n");
63141016Sdfr    }
63241016Sdfr#endif
63341016Sdfr}
63441016Sdfr
63541016Sdfrstatic int
63641016Sdfrrestore_controller(KBDC kbdc, int command_byte)
63741016Sdfr{
63841016Sdfr    empty_both_buffers(kbdc, 10);
63941016Sdfr
64041016Sdfr    if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
64141016Sdfr	log(LOG_ERR, "psm: failed to restore the keyboard controller "
64241016Sdfr		     "command byte.\n");
64358230Syokota	empty_both_buffers(kbdc, 10);
64441016Sdfr	return FALSE;
64541016Sdfr    } else {
64658230Syokota	empty_both_buffers(kbdc, 10);
64741016Sdfr	return TRUE;
64841016Sdfr    }
64941016Sdfr}
65041016Sdfr
65141016Sdfr/*
65241016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
65341016Sdfr * and its interrupt must be disabled before calling this routine.
65441016Sdfr * The aux device will be disabled before returning.
65541016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
65641016Sdfr * calling this routine.
65741016Sdfr */
65841016Sdfrstatic int
65984880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode)
66041016Sdfr{
66141016Sdfr    KBDC kbdc = sc->kbdc;
66241016Sdfr    int stat[3];
66341016Sdfr    int i;
66441016Sdfr
66541016Sdfr    switch((i = test_aux_port(kbdc))) {
666132270Smux    case 1:	/* ignore these errors */
667132270Smux    case 2:
668132270Smux    case 3:
66941016Sdfr    case PSM_ACK:
67041016Sdfr	if (verbose)
67141016Sdfr	    log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
67284880Syokota	        sc->unit, i);
673102412Scharnier	/* FALLTHROUGH */
67441016Sdfr    case 0:	/* no error */
67541016Sdfr    	break;
67641016Sdfr    case -1: 	/* time out */
67741016Sdfr    default: 	/* error */
67841016Sdfr    	recover_from_error(kbdc);
67945789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
68045789Speter	    break;
68141016Sdfr    	log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
68284880Syokota    	    sc->unit, i);
68341016Sdfr    	return FALSE;
68441016Sdfr    }
68541016Sdfr
68645789Speter    if (sc->config & PSM_CONFIG_NORESET) {
68745789Speter	/*
68845789Speter	 * Don't try to reset the pointing device.  It may possibly be
68945789Speter	 * left in the unknown state, though...
69045789Speter	 */
69145789Speter    } else {
69245789Speter	/*
69345789Speter	 * NOTE: some controllers appears to hang the `keyboard' when
69445789Speter	 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
69545789Speter	 */
69645789Speter	if (!reset_aux_dev(kbdc)) {
69745789Speter            recover_from_error(kbdc);
69884880Syokota            log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
69945789Speter            return FALSE;
70045789Speter	}
70141016Sdfr    }
70241016Sdfr
70341016Sdfr    /*
70441016Sdfr     * both the aux port and the aux device is functioning, see
70541016Sdfr     * if the device can be enabled.
70641016Sdfr     */
70741016Sdfr    if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
70884880Syokota        log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
70941016Sdfr        return FALSE;
71041016Sdfr    }
71141016Sdfr    empty_both_buffers(kbdc, 10);	/* remove stray data if any */
71241016Sdfr
71345789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
71445789Speter	i = GENERIC_MOUSE_ENTRY;
71545789Speter    } else {
71645789Speter	/* FIXME: hardware ID, mouse buttons? */
71741016Sdfr
71845789Speter	/* other parameters */
71945789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
72045789Speter	    if ((*vendortype[i].probefunc)(sc)) {
72145789Speter		if (verbose >= 2)
72245789Speter		    log(LOG_ERR, "psm%d: found %s\n",
72384880Syokota			sc->unit, model_name(vendortype[i].model));
72445789Speter		break;
72545789Speter	    }
72641016Sdfr	}
72741016Sdfr    }
72841016Sdfr
72941016Sdfr    sc->hw.model = vendortype[i].model;
73041016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
73141016Sdfr
73241016Sdfr    /* set mouse parameters */
73341016Sdfr    if (mode != (mousemode_t *)NULL) {
73441016Sdfr	if (mode->rate > 0)
73541016Sdfr            mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
73641016Sdfr	if (mode->resolution >= 0)
73741016Sdfr            mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
73841016Sdfr        set_mouse_scaling(kbdc, 1);
73941016Sdfr        set_mouse_mode(kbdc);
74041016Sdfr    }
74141016Sdfr
74241016Sdfr    /* request a data packet and extract sync. bits */
74341016Sdfr    if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
74484880Syokota        log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
74584880Syokota	    sc->unit);
74641016Sdfr        sc->mode.syncmask[0] = 0;
74741016Sdfr    } else {
74841016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
74941016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
75041016Sdfr	empty_aux_buffer(kbdc, 5);
75141016Sdfr    }
75241016Sdfr
75341016Sdfr    /* just check the status of the mouse */
75441016Sdfr    if (get_mouse_status(kbdc, stat, 0, 3) < 3)
75584880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
75684880Syokota	    sc->unit);
75741016Sdfr
75841016Sdfr    return TRUE;
75941016Sdfr}
76041016Sdfr
76141016Sdfrstatic int
76284880Syokotadoopen(struct psm_softc *sc, int command_byte)
76341016Sdfr{
76441016Sdfr    int stat[3];
76541016Sdfr
76641016Sdfr    /* enable the mouse device */
76741016Sdfr    if (!enable_aux_dev(sc->kbdc)) {
76841016Sdfr	/* MOUSE ERROR: failed to enable the mouse because:
76941016Sdfr	 * 1) the mouse is faulty,
77041016Sdfr	 * 2) the mouse has been removed(!?)
77141016Sdfr	 * In the latter case, the keyboard may have hung, and need
77241016Sdfr	 * recovery procedure...
77341016Sdfr	 */
77441016Sdfr	recover_from_error(sc->kbdc);
77541016Sdfr#if 0
77641016Sdfr	/* FIXME: we could reset the mouse here and try to enable
77741016Sdfr	 * it again. But it will take long time and it's not a good
77841016Sdfr	 * idea to disable the keyboard that long...
77941016Sdfr	 */
78084880Syokota	if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
78141016Sdfr	    recover_from_error(sc->kbdc);
78241016Sdfr#else
78341016Sdfr        {
78441016Sdfr#endif
78541016Sdfr            restore_controller(sc->kbdc, command_byte);
78641016Sdfr	    /* mark this device is no longer available */
78741016Sdfr	    sc->state &= ~PSM_VALID;
78841016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
78984880Syokota		sc->unit);
79041016Sdfr	    return (EIO);
79141016Sdfr	}
79241016Sdfr    }
79341016Sdfr
79441016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
79584880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
79641016Sdfr
79741016Sdfr    /* enable the aux port and interrupt */
79841016Sdfr    if (!set_controller_command_byte(sc->kbdc,
79941016Sdfr	    kbdc_get_device_mask(sc->kbdc),
80041016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
80141016Sdfr		| KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
80241016Sdfr	/* CONTROLLER ERROR */
80341016Sdfr	disable_aux_dev(sc->kbdc);
80441016Sdfr        restore_controller(sc->kbdc, command_byte);
80541016Sdfr	log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
80684880Syokota	    sc->unit);
80741016Sdfr	return (EIO);
80841016Sdfr    }
80941016Sdfr
81058230Syokota    /* start the watchdog timer */
81158230Syokota    sc->watchdog = FALSE;
81284880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
81358230Syokota
81441016Sdfr    return (0);
81541016Sdfr}
81641016Sdfr
81784880Syokotastatic int
81884880Syokotareinitialize(struct psm_softc *sc, int doinit)
81984880Syokota{
82084880Syokota    int err;
82184880Syokota    int c;
82284880Syokota    int s;
82384880Syokota
82484880Syokota    /* don't let anybody mess with the aux device */
82584880Syokota    if (!kbdc_lock(sc->kbdc, TRUE))
82684880Syokota	return (EIO);
82784880Syokota    s = spltty();
82884880Syokota
82984880Syokota    /* block our watchdog timer */
83084880Syokota    sc->watchdog = FALSE;
83184880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
83284880Syokota    callout_handle_init(&sc->callout);
83384880Syokota
83484880Syokota    /* save the current controller command byte */
83584880Syokota    empty_both_buffers(sc->kbdc, 10);
83684880Syokota    c = get_controller_command_byte(sc->kbdc);
83784880Syokota    if (verbose >= 2)
83884880Syokota        log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
83984880Syokota	    sc->unit, c);
84084880Syokota
84184880Syokota    /* enable the aux port but disable the aux interrupt and the keyboard */
84284880Syokota    if ((c == -1) || !set_controller_command_byte(sc->kbdc,
84384880Syokota	    kbdc_get_device_mask(sc->kbdc),
84484880Syokota  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
84584880Syokota	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
84684880Syokota        /* CONTROLLER ERROR */
84784880Syokota	splx(s);
84884880Syokota        kbdc_lock(sc->kbdc, FALSE);
84984880Syokota	log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
85084880Syokota	    sc->unit);
85184880Syokota	return (EIO);
85284880Syokota    }
85384880Syokota
85484880Syokota    /* flush any data */
85584880Syokota    if (sc->state & PSM_VALID) {
85684880Syokota	disable_aux_dev(sc->kbdc);	/* this may fail; but never mind... */
85784880Syokota	empty_aux_buffer(sc->kbdc, 10);
85884880Syokota    }
859123442Salfred    flushpackets(sc);
86084880Syokota    sc->syncerrors = 0;
86184880Syokota
86284880Syokota    /* try to detect the aux device; are you still there? */
86384880Syokota    err = 0;
86484880Syokota    if (doinit) {
86584880Syokota	if (doinitialize(sc, &sc->mode)) {
86684880Syokota	    /* yes */
86784880Syokota	    sc->state |= PSM_VALID;
86884880Syokota	} else {
86984880Syokota	    /* the device has gone! */
87084880Syokota	    restore_controller(sc->kbdc, c);
87184880Syokota	    sc->state &= ~PSM_VALID;
87284880Syokota	    log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
87384880Syokota		sc->unit);
87484880Syokota	    err = ENXIO;
87584880Syokota	}
87684880Syokota    }
87784880Syokota    splx(s);
87884880Syokota
87984880Syokota    /* restore the driver state */
88084880Syokota    if ((sc->state & PSM_OPEN) && (err == 0)) {
88184880Syokota        /* enable the aux device and the port again */
88284880Syokota	err = doopen(sc, c);
88384880Syokota	if (err != 0)
88484880Syokota	    log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
88584880Syokota		sc->unit);
88684880Syokota    } else {
88784880Syokota        /* restore the keyboard port and disable the aux port */
88884880Syokota        if (!set_controller_command_byte(sc->kbdc,
88984880Syokota                kbdc_get_device_mask(sc->kbdc),
89084880Syokota                (c & KBD_KBD_CONTROL_BITS)
89184880Syokota                    | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
89284880Syokota            /* CONTROLLER ERROR */
89384880Syokota            log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
89484880Syokota                sc->unit);
89584880Syokota            err = EIO;
89684880Syokota	}
89784880Syokota    }
89884880Syokota
89984880Syokota    kbdc_lock(sc->kbdc, FALSE);
90084880Syokota    return (err);
90184880Syokota}
90284880Syokota
90341016Sdfr/* psm driver entry points */
90441016Sdfr
90583147Syokotastatic void
90683147Syokotapsmidentify(driver_t *driver, device_t parent)
90783147Syokota{
90883931Syokota    device_t psmc;
90983931Syokota    device_t psm;
91083931Syokota    u_long irq;
91183931Syokota    int unit;
91283147Syokota
91383931Syokota    unit = device_get_unit(parent);
91483931Syokota
91583147Syokota    /* always add at least one child */
91683931Syokota    psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
91783931Syokota    if (psm == NULL)
91883931Syokota	return;
91983931Syokota
92083931Syokota    irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
92183931Syokota    if (irq > 0)
92283931Syokota	return;
92383931Syokota
92483931Syokota    /*
92583931Syokota     * If the PS/2 mouse device has already been reported by ACPI or
92683931Syokota     * PnP BIOS, obtain the IRQ resource from it.
92783931Syokota     * (See psmcpnp_attach() below.)
92883931Syokota     */
92983931Syokota    psmc = device_find_child(device_get_parent(parent),
93083931Syokota			     PSMCPNP_DRIVER_NAME, unit);
93183931Syokota    if (psmc == NULL)
93283931Syokota	return;
93383931Syokota    irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
93483931Syokota    if (irq <= 0)
93583931Syokota	return;
93683931Syokota    bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
93783147Syokota}
93883147Syokota
939115569Sphk#define endprobe(v)	do {   if (bootverbose)				\
94041016Sdfr				--verbose;   				\
94141016Sdfr                            kbdc_set_device_mask(sc->kbdc, mask);	\
94241016Sdfr			    kbdc_lock(sc->kbdc, FALSE);			\
94341016Sdfr			    return (v);	     				\
944115569Sphk			} while (0)
94541016Sdfr
94641016Sdfrstatic int
94741016Sdfrpsmprobe(device_t dev)
94841016Sdfr{
94941016Sdfr    int unit = device_get_unit(dev);
95041016Sdfr    struct psm_softc *sc = device_get_softc(dev);
95141016Sdfr    int stat[3];
95241016Sdfr    int command_byte;
95341016Sdfr    int mask;
95458230Syokota    int rid;
95541016Sdfr    int i;
95641016Sdfr
95741016Sdfr#if 0
95841016Sdfr    kbdc_debug(TRUE);
95941016Sdfr#endif
96058230Syokota
96183147Syokota    /* see if IRQ is available */
96283147Syokota    rid = KBDC_RID_AUX;
963127135Snjl    sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
964127135Snjl				      RF_SHAREABLE | RF_ACTIVE);
96583147Syokota    if (sc->intr == NULL) {
96683147Syokota	if (bootverbose)
96783147Syokota            device_printf(dev, "unable to allocate IRQ\n");
96883147Syokota        return (ENXIO);
96983147Syokota    }
97083147Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
97158230Syokota
97284880Syokota    sc->unit = unit;
97358230Syokota    sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
97483147Syokota    sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
97558230Syokota    /* XXX: for backward compatibility */
97658230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
97758230Syokota    sc->config |=
97858230Syokota#ifdef PSM_RESETAFTERSUSPEND
97958230Syokota	PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
98058230Syokota#else
98158230Syokota	PSM_CONFIG_HOOKRESUME;
98258230Syokota#endif
98358230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
98441016Sdfr    sc->flags = 0;
98541016Sdfr    if (bootverbose)
98641016Sdfr        ++verbose;
98741016Sdfr
98843105Sdfr    device_set_desc(dev, "PS/2 Mouse");
98943105Sdfr
99041016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE)) {
99141016Sdfr        printf("psm%d: unable to lock the controller.\n", unit);
99241016Sdfr        if (bootverbose)
99341016Sdfr            --verbose;
99441016Sdfr	return (ENXIO);
99541016Sdfr    }
99641016Sdfr
99741016Sdfr    /*
99841016Sdfr     * NOTE: two bits in the command byte controls the operation of the
99941016Sdfr     * aux port (mouse port): the aux port disable bit (bit 5) and the aux
100041016Sdfr     * port interrupt (IRQ 12) enable bit (bit 2).
100141016Sdfr     */
100241016Sdfr
100341016Sdfr    /* discard anything left after the keyboard initialization */
100441016Sdfr    empty_both_buffers(sc->kbdc, 10);
100541016Sdfr
100641016Sdfr    /* save the current command byte; it will be used later */
100741016Sdfr    mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
100841016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
100941016Sdfr    if (verbose)
101041016Sdfr        printf("psm%d: current command byte:%04x\n", unit, command_byte);
101141016Sdfr    if (command_byte == -1) {
101241016Sdfr        /* CONTROLLER ERROR */
101341016Sdfr        printf("psm%d: unable to get the current command byte value.\n",
101441016Sdfr            unit);
101541016Sdfr        endprobe(ENXIO);
101641016Sdfr    }
101741016Sdfr
101841016Sdfr    /*
101941016Sdfr     * disable the keyboard port while probing the aux port, which must be
102041016Sdfr     * enabled during this routine
102141016Sdfr     */
102241016Sdfr    if (!set_controller_command_byte(sc->kbdc,
102341016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
102441016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
102541016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
102641016Sdfr        /*
102741016Sdfr	 * this is CONTROLLER ERROR; I don't know how to recover
102841016Sdfr         * from this error...
102941016Sdfr	 */
103041016Sdfr        restore_controller(sc->kbdc, command_byte);
103141016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
103241016Sdfr        endprobe(ENXIO);
103341016Sdfr    }
103445789Speter    write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
103541016Sdfr
103641016Sdfr    /*
103741016Sdfr     * NOTE: `test_aux_port()' is designed to return with zero if the aux
103841016Sdfr     * port exists and is functioning. However, some controllers appears
103941016Sdfr     * to respond with zero even when the aux port doesn't exist. (It may
104041016Sdfr     * be that this is only the case when the controller DOES have the aux
104141016Sdfr     * port but the port is not wired on the motherboard.) The keyboard
104241016Sdfr     * controllers without the port, such as the original AT, are
104341016Sdfr     * supporsed to return with an error code or simply time out. In any
104441016Sdfr     * case, we have to continue probing the port even when the controller
104541016Sdfr     * passes this test.
104641016Sdfr     *
1047132270Smux     * XXX: some controllers erroneously return the error code 1, 2 or 3
1048132270Smux     * when it has the perfectly functional aux port. We have to ignore
1049132270Smux     * this error code. Even if the controller HAS error with the aux
1050132270Smux     * port, it will be detected later...
105141016Sdfr     * XXX: another incompatible controller returns PSM_ACK (0xfa)...
105241016Sdfr     */
105341016Sdfr    switch ((i = test_aux_port(sc->kbdc))) {
1054132270Smux    case 1:	   /* ignore these errors */
1055132270Smux    case 2:
1056132270Smux    case 3:
105741016Sdfr    case PSM_ACK:
105841016Sdfr        if (verbose)
105941016Sdfr	    printf("psm%d: strange result for test aux port (%d).\n",
106041016Sdfr	        unit, i);
1061102412Scharnier	/* FALLTHROUGH */
106241016Sdfr    case 0:        /* no error */
106341016Sdfr        break;
106441016Sdfr    case -1:        /* time out */
106541016Sdfr    default:        /* error */
106641016Sdfr        recover_from_error(sc->kbdc);
106745789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
106845789Speter	    break;
106941016Sdfr        restore_controller(sc->kbdc, command_byte);
107041016Sdfr        if (verbose)
107141016Sdfr            printf("psm%d: the aux port is not functioning (%d).\n",
107241016Sdfr                unit, i);
107341016Sdfr        endprobe(ENXIO);
107441016Sdfr    }
107541016Sdfr
107645789Speter    if (sc->config & PSM_CONFIG_NORESET) {
107745789Speter	/*
107845789Speter	 * Don't try to reset the pointing device.  It may possibly be
107945789Speter	 * left in the unknown state, though...
108045789Speter	 */
108145789Speter    } else {
108245789Speter	/*
108345789Speter	 * NOTE: some controllers appears to hang the `keyboard' when the aux
108445789Speter	 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1085117302Smikeh	 *
1086117302Smikeh	 * Attempt to reset the controller twice -- this helps
1087117302Smikeh	 * pierce through some KVM switches. The second reset
1088117302Smikeh	 * is non-fatal.
108945789Speter	 */
109045789Speter	if (!reset_aux_dev(sc->kbdc)) {
109145789Speter            recover_from_error(sc->kbdc);
109245789Speter            restore_controller(sc->kbdc, command_byte);
109345789Speter            if (verbose)
109445789Speter        	printf("psm%d: failed to reset the aux device.\n", unit);
109545789Speter            endprobe(ENXIO);
1096117302Smikeh	} else if (!reset_aux_dev(sc->kbdc)) {
1097117302Smikeh	    recover_from_error(sc->kbdc);
1098117302Smikeh	    if (verbose >= 2)
1099117302Smikeh        	printf("psm%d: failed to reset the aux device (2).\n",
1100117302Smikeh        	    unit);
110145789Speter	}
110241016Sdfr    }
110345789Speter
110441016Sdfr    /*
110541016Sdfr     * both the aux port and the aux device is functioning, see if the
110641016Sdfr     * device can be enabled. NOTE: when enabled, the device will start
110741016Sdfr     * sending data; we shall immediately disable the device once we know
110841016Sdfr     * the device can be enabled.
110941016Sdfr     */
111041016Sdfr    if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
111145789Speter        /* MOUSE ERROR */
111245789Speter	recover_from_error(sc->kbdc);
111345789Speter	restore_controller(sc->kbdc, command_byte);
111445789Speter	if (verbose)
111545789Speter	    printf("psm%d: failed to enable the aux device.\n", unit);
111641016Sdfr        endprobe(ENXIO);
111741016Sdfr    }
111841016Sdfr
111941016Sdfr    /* save the default values after reset */
112041016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
112141016Sdfr	sc->dflt_mode.rate = sc->mode.rate = stat[2];
112241016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
112341016Sdfr    } else {
112441016Sdfr	sc->dflt_mode.rate = sc->mode.rate = -1;
112541016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = -1;
112641016Sdfr    }
112741016Sdfr
112841016Sdfr    /* hardware information */
112941016Sdfr    sc->hw.iftype = MOUSE_IF_PS2;
113041016Sdfr
113141016Sdfr    /* verify the device is a mouse */
113241016Sdfr    sc->hw.hwid = get_aux_id(sc->kbdc);
113341016Sdfr    if (!is_a_mouse(sc->hw.hwid)) {
113441016Sdfr        restore_controller(sc->kbdc, command_byte);
113541016Sdfr        if (verbose)
113641016Sdfr            printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
113741016Sdfr        endprobe(ENXIO);
113841016Sdfr    }
113941016Sdfr    switch (sc->hw.hwid) {
114041016Sdfr    case PSM_BALLPOINT_ID:
114141016Sdfr        sc->hw.type = MOUSE_TRACKBALL;
114241016Sdfr        break;
114341016Sdfr    case PSM_MOUSE_ID:
114441016Sdfr    case PSM_INTELLI_ID:
114558230Syokota    case PSM_EXPLORER_ID:
114658230Syokota    case PSM_4DMOUSE_ID:
114758230Syokota    case PSM_4DPLUS_ID:
114841016Sdfr        sc->hw.type = MOUSE_MOUSE;
114941016Sdfr        break;
115041016Sdfr    default:
115141016Sdfr        sc->hw.type = MOUSE_UNKNOWN;
115241016Sdfr        break;
115341016Sdfr    }
115441016Sdfr
115545789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
115645789Speter	sc->hw.buttons = 2;
115745789Speter	i = GENERIC_MOUSE_ENTRY;
115845789Speter    } else {
115945789Speter	/* # of buttons */
116045789Speter	sc->hw.buttons = get_mouse_buttons(sc->kbdc);
116141016Sdfr
116245789Speter	/* other parameters */
116345789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
116445789Speter	    if ((*vendortype[i].probefunc)(sc)) {
116545789Speter		if (verbose >= 2)
116645789Speter		    printf("psm%d: found %s\n",
116745789Speter			   unit, model_name(vendortype[i].model));
116845789Speter		break;
116945789Speter	    }
117041016Sdfr	}
117141016Sdfr    }
117241016Sdfr
117341016Sdfr    sc->hw.model = vendortype[i].model;
117441016Sdfr
117541016Sdfr    sc->dflt_mode.level = PSM_LEVEL_BASE;
117641016Sdfr    sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
117741016Sdfr    sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
117841016Sdfr    if (sc->config & PSM_CONFIG_NOCHECKSYNC)
117941016Sdfr        sc->dflt_mode.syncmask[0] = 0;
118041016Sdfr    else
118141016Sdfr        sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
118245789Speter    if (sc->config & PSM_CONFIG_FORCETAP)
118345789Speter        sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
118441016Sdfr    sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
118541016Sdfr    sc->mode = sc->dflt_mode;
118641016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
118741016Sdfr
118841016Sdfr    /* set mouse parameters */
118948773Syokota#if 0
119048773Syokota    /*
119148773Syokota     * A version of Logitech FirstMouse+ won't report wheel movement,
119248773Syokota     * if SET_DEFAULTS is sent...  Don't use this command.
119348773Syokota     * This fix was found by Takashi Nishida.
119448773Syokota     */
119541016Sdfr    i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
119641016Sdfr    if (verbose >= 2)
119741016Sdfr	printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
119848773Syokota#endif
119941016Sdfr    if (sc->config & PSM_CONFIG_RESOLUTION) {
120041016Sdfr        sc->mode.resolution
120141016Sdfr	    = set_mouse_resolution(sc->kbdc,
120248773Syokota				   (sc->config & PSM_CONFIG_RESOLUTION) - 1);
120348773Syokota    } else if (sc->mode.resolution >= 0) {
120448773Syokota	sc->mode.resolution
120548773Syokota	    = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
120641016Sdfr    }
120748773Syokota    if (sc->mode.rate > 0) {
120848773Syokota	sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
120948773Syokota    }
121048773Syokota    set_mouse_scaling(sc->kbdc, 1);
121141016Sdfr
121241016Sdfr    /* request a data packet and extract sync. bits */
121341016Sdfr    if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
121441016Sdfr        printf("psm%d: failed to get data.\n", unit);
121541016Sdfr        sc->mode.syncmask[0] = 0;
121641016Sdfr    } else {
121741016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
121841016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
121941016Sdfr	empty_aux_buffer(sc->kbdc, 5);
122041016Sdfr    }
122141016Sdfr
122241016Sdfr    /* just check the status of the mouse */
122341016Sdfr    /*
122441016Sdfr     * NOTE: XXX there are some arcane controller/mouse combinations out
122541016Sdfr     * there, which hung the controller unless there is data transmission
122641016Sdfr     * after ACK from the mouse.
122741016Sdfr     */
122841016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
122941016Sdfr        printf("psm%d: failed to get status.\n", unit);
123041016Sdfr    } else {
123141016Sdfr	/*
123241016Sdfr	 * When in its native mode, some mice operate with different
123341016Sdfr	 * default parameters than in the PS/2 compatible mode.
123441016Sdfr	 */
123541016Sdfr        sc->dflt_mode.rate = sc->mode.rate = stat[2];
123641016Sdfr        sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
123741016Sdfr     }
123841016Sdfr
123941016Sdfr    /* disable the aux port for now... */
124041016Sdfr    if (!set_controller_command_byte(sc->kbdc,
124141016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
124241016Sdfr            (command_byte & KBD_KBD_CONTROL_BITS)
124341016Sdfr                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
124441016Sdfr        /*
124541016Sdfr	 * this is CONTROLLER ERROR; I don't know the proper way to
124641016Sdfr         * recover from this error...
124741016Sdfr	 */
124841016Sdfr        restore_controller(sc->kbdc, command_byte);
124941016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
125041016Sdfr        endprobe(ENXIO);
125141016Sdfr    }
125241016Sdfr
125341016Sdfr    /* done */
125441016Sdfr    kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
125541016Sdfr    kbdc_lock(sc->kbdc, FALSE);
125641016Sdfr    return (0);
125741016Sdfr}
125841016Sdfr
125941016Sdfrstatic int
126041016Sdfrpsmattach(device_t dev)
126141016Sdfr{
126241016Sdfr    int unit = device_get_unit(dev);
126341016Sdfr    struct psm_softc *sc = device_get_softc(dev);
126458230Syokota    int error;
126558230Syokota    int rid;
126641016Sdfr
126741016Sdfr    /* Setup initial state */
126841016Sdfr    sc->state = PSM_VALID;
126958230Syokota    callout_handle_init(&sc->callout);
127041016Sdfr
127158230Syokota    /* Setup our interrupt handler */
127283147Syokota    rid = KBDC_RID_AUX;
1273127135Snjl    sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1274127135Snjl				      RF_SHAREABLE | RF_ACTIVE);
127558230Syokota    if (sc->intr == NULL)
127658230Syokota	return (ENXIO);
127783147Syokota    error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
127858230Syokota    if (error) {
127958230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
128058230Syokota	return (error);
128158230Syokota    }
128258230Syokota
128341016Sdfr    /* Done */
128458230Syokota    sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
128558230Syokota		       "psm%d", unit);
128658230Syokota    sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
128758230Syokota			"bpsm%d", unit);
128841016Sdfr
128941016Sdfr    if (!verbose) {
129041016Sdfr        printf("psm%d: model %s, device ID %d\n",
129148778Syokota	    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
129241016Sdfr    } else {
129348778Syokota        printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
129448778Syokota	    unit, model_name(sc->hw.model),
129548778Syokota	    sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
129641016Sdfr	printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
129741016Sdfr	    unit, sc->config, sc->flags, sc->mode.packetsize);
129841016Sdfr	printf("psm%d: syncmask:%02x, syncbits:%02x\n",
129941016Sdfr	    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
130041016Sdfr    }
130141016Sdfr
130241016Sdfr    if (bootverbose)
130341016Sdfr        --verbose;
130441016Sdfr
130541016Sdfr    return (0);
130641016Sdfr}
130741016Sdfr
130841016Sdfrstatic int
130958230Syokotapsmdetach(device_t dev)
131058230Syokota{
131158230Syokota    struct psm_softc *sc;
131258230Syokota    int rid;
131358230Syokota
131458230Syokota    sc = device_get_softc(dev);
131558230Syokota    if (sc->state & PSM_OPEN)
131658230Syokota	return EBUSY;
131758230Syokota
131883147Syokota    rid = KBDC_RID_AUX;
131983147Syokota    bus_teardown_intr(dev, sc->intr, sc->ih);
132058230Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
132158230Syokota
132258230Syokota    destroy_dev(sc->dev);
132358230Syokota    destroy_dev(sc->bdev);
132458230Syokota
132558230Syokota    return 0;
132658230Syokota}
132758230Syokota
132858230Syokotastatic int
1329130585Sphkpsmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
133041016Sdfr{
133141016Sdfr    int unit = PSM_UNIT(dev);
133241016Sdfr    struct psm_softc *sc;
133341016Sdfr    int command_byte;
133441016Sdfr    int err;
133541016Sdfr    int s;
133641016Sdfr
133741016Sdfr    /* Get device data */
133841016Sdfr    sc = PSM_SOFTC(unit);
133941016Sdfr    if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
134041016Sdfr	/* the device is no longer valid/functioning */
134141016Sdfr        return (ENXIO);
134241016Sdfr
134341016Sdfr    /* Disallow multiple opens */
134441016Sdfr    if (sc->state & PSM_OPEN)
134541016Sdfr        return (EBUSY);
134641016Sdfr
134741016Sdfr    device_busy(devclass_get_device(psm_devclass, unit));
134841016Sdfr
134941016Sdfr    /* Initialize state */
135041016Sdfr    sc->mode.level = sc->dflt_mode.level;
135141016Sdfr    sc->mode.protocol = sc->dflt_mode.protocol;
135258230Syokota    sc->watchdog = FALSE;
135341016Sdfr
135441016Sdfr    /* flush the event queue */
135541016Sdfr    sc->queue.count = 0;
135641016Sdfr    sc->queue.head = 0;
135741016Sdfr    sc->queue.tail = 0;
135841016Sdfr    sc->status.flags = 0;
135941016Sdfr    sc->status.button = 0;
136041016Sdfr    sc->status.obutton = 0;
136141016Sdfr    sc->status.dx = 0;
136241016Sdfr    sc->status.dy = 0;
136341016Sdfr    sc->status.dz = 0;
136441016Sdfr    sc->button = 0;
1365123442Salfred    sc->pqueue_start = 0;
1366123442Salfred    sc->pqueue_end = 0;
136741016Sdfr
136841016Sdfr    /* empty input buffer */
1369123442Salfred    flushpackets(sc);
137084880Syokota    sc->syncerrors = 0;
137141016Sdfr
137241016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
137341016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
137441016Sdfr	return (EIO);
137541016Sdfr
137641016Sdfr    /* save the current controller command byte */
137741016Sdfr    s = spltty();
137841016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
137941016Sdfr
138041016Sdfr    /* enable the aux port and temporalily disable the keyboard */
138141016Sdfr    if ((command_byte == -1)
138241016Sdfr        || !set_controller_command_byte(sc->kbdc,
138341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
138441016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
138541016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
138641016Sdfr        /* CONTROLLER ERROR; do you know how to get out of this? */
138741016Sdfr        kbdc_lock(sc->kbdc, FALSE);
138841016Sdfr	splx(s);
138941016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
139041016Sdfr	    unit);
139141016Sdfr	return (EIO);
139241016Sdfr    }
139341016Sdfr    /*
139441016Sdfr     * Now that the keyboard controller is told not to generate
139541016Sdfr     * the keyboard and mouse interrupts, call `splx()' to allow
139641016Sdfr     * the other tty interrupts. The clock interrupt may also occur,
139741016Sdfr     * but timeout routines will be blocked by the poll flag set
139841016Sdfr     * via `kbdc_lock()'
139941016Sdfr     */
140041016Sdfr    splx(s);
140141016Sdfr
140241016Sdfr    /* enable the mouse device */
140384880Syokota    err = doopen(sc, command_byte);
140441016Sdfr
140541016Sdfr    /* done */
140641016Sdfr    if (err == 0)
140741016Sdfr        sc->state |= PSM_OPEN;
140841016Sdfr    kbdc_lock(sc->kbdc, FALSE);
140941016Sdfr    return (err);
141041016Sdfr}
141141016Sdfr
141241016Sdfrstatic int
1413130585Sphkpsmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
141441016Sdfr{
141541016Sdfr    int unit = PSM_UNIT(dev);
141641016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
141741016Sdfr    int stat[3];
141841016Sdfr    int command_byte;
141941016Sdfr    int s;
142041016Sdfr
142141016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
142241016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
142341016Sdfr	return (EIO);
142441016Sdfr
142541016Sdfr    /* save the current controller command byte */
142641016Sdfr    s = spltty();
142741016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
142841016Sdfr    if (command_byte == -1) {
142941016Sdfr        kbdc_lock(sc->kbdc, FALSE);
143041016Sdfr	splx(s);
143141016Sdfr	return (EIO);
143241016Sdfr    }
143341016Sdfr
143441016Sdfr    /* disable the aux interrupt and temporalily disable the keyboard */
143541016Sdfr    if (!set_controller_command_byte(sc->kbdc,
143641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
143741016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
143841016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
143941016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
144058230Syokota	    unit);
144141016Sdfr	/* CONTROLLER ERROR;
144241016Sdfr	 * NOTE: we shall force our way through. Because the only
144341016Sdfr	 * ill effect we shall see is that we may not be able
144441016Sdfr	 * to read ACK from the mouse, and it doesn't matter much
144541016Sdfr	 * so long as the mouse will accept the DISABLE command.
144641016Sdfr	 */
144741016Sdfr    }
144841016Sdfr    splx(s);
144941016Sdfr
145058230Syokota    /* stop the watchdog timer */
145184880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
145258230Syokota    callout_handle_init(&sc->callout);
145358230Syokota
145441016Sdfr    /* remove anything left in the output buffer */
145541016Sdfr    empty_aux_buffer(sc->kbdc, 10);
145641016Sdfr
145741016Sdfr    /* disable the aux device, port and interrupt */
145841016Sdfr    if (sc->state & PSM_VALID) {
145941016Sdfr        if (!disable_aux_dev(sc->kbdc)) {
146041016Sdfr	    /* MOUSE ERROR;
146141016Sdfr	     * NOTE: we don't return error and continue, pretending
146241016Sdfr	     * we have successfully disabled the device. It's OK because
146341016Sdfr	     * the interrupt routine will discard any data from the mouse
146441016Sdfr	     * hereafter.
146541016Sdfr	     */
146641016Sdfr	    log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
146758230Syokota		unit);
146841016Sdfr        }
146941016Sdfr
147041016Sdfr        if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
147141016Sdfr            log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
147258230Syokota		unit);
147341016Sdfr    }
147441016Sdfr
147541016Sdfr    if (!set_controller_command_byte(sc->kbdc,
147641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
147741016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
147841016Sdfr	        | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
147941016Sdfr	/* CONTROLLER ERROR;
148041016Sdfr	 * we shall ignore this error; see the above comment.
148141016Sdfr	 */
148241016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
148358230Syokota	    unit);
148441016Sdfr    }
148541016Sdfr
148641016Sdfr    /* remove anything left in the output buffer */
148741016Sdfr    empty_aux_buffer(sc->kbdc, 10);
148841016Sdfr
148941016Sdfr    /* close is almost always successful */
149041016Sdfr    sc->state &= ~PSM_OPEN;
149141016Sdfr    kbdc_lock(sc->kbdc, FALSE);
149241016Sdfr    device_unbusy(devclass_get_device(psm_devclass, unit));
149341016Sdfr    return (0);
149441016Sdfr}
149541016Sdfr
149641016Sdfrstatic int
1497123442Salfredtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, unsigned char *buf)
149841016Sdfr{
149941016Sdfr    static unsigned char butmapps2[8] = {
150041016Sdfr        0,
150141016Sdfr        MOUSE_PS2_BUTTON1DOWN,
150241016Sdfr        MOUSE_PS2_BUTTON2DOWN,
150341016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
150441016Sdfr        MOUSE_PS2_BUTTON3DOWN,
150541016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
150641016Sdfr        MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
150741016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
150841016Sdfr    };
150941016Sdfr    static unsigned char butmapmsc[8] = {
151041016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
151141016Sdfr        MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
151241016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
151341016Sdfr        MOUSE_MSC_BUTTON3UP,
151441016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
151541016Sdfr        MOUSE_MSC_BUTTON2UP,
151641016Sdfr        MOUSE_MSC_BUTTON1UP,
151741016Sdfr        0,
151841016Sdfr    };
151941016Sdfr    int mapped;
152041016Sdfr    int i;
152141016Sdfr
152241016Sdfr    if (sc->mode.level == PSM_LEVEL_BASE) {
152341016Sdfr        mapped = status->button & ~MOUSE_BUTTON4DOWN;
152441016Sdfr        if (status->button & MOUSE_BUTTON4DOWN)
152541016Sdfr	    mapped |= MOUSE_BUTTON1DOWN;
152641016Sdfr        status->button = mapped;
152741016Sdfr        buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1528109269Smdodd        i = imax(imin(status->dx, 255), -256);
152941016Sdfr	if (i < 0)
153041016Sdfr	    buf[0] |= MOUSE_PS2_XNEG;
153141016Sdfr        buf[1] = i;
1532109269Smdodd        i = imax(imin(status->dy, 255), -256);
153341016Sdfr	if (i < 0)
153441016Sdfr	    buf[0] |= MOUSE_PS2_YNEG;
153541016Sdfr        buf[2] = i;
153641016Sdfr	return MOUSE_PS2_PACKETSIZE;
153741016Sdfr    } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
153841016Sdfr        buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1539109269Smdodd        i = imax(imin(status->dx, 255), -256);
154041016Sdfr        buf[1] = i >> 1;
154141016Sdfr        buf[3] = i - buf[1];
1542109269Smdodd        i = imax(imin(status->dy, 255), -256);
154341016Sdfr        buf[2] = i >> 1;
154441016Sdfr        buf[4] = i - buf[2];
1545109269Smdodd        i = imax(imin(status->dz, 127), -128);
154641016Sdfr        buf[5] = (i >> 1) & 0x7f;
154741016Sdfr        buf[6] = (i - (i >> 1)) & 0x7f;
154841016Sdfr        buf[7] = (~status->button >> 3) & 0x7f;
154941016Sdfr	return MOUSE_SYS_PACKETSIZE;
155041016Sdfr    }
1551123442Salfred    return pb->inputbytes;
155241016Sdfr}
155341016Sdfr
155441016Sdfrstatic int
1555130585Sphkpsmread(struct cdev *dev, struct uio *uio, int flag)
155641016Sdfr{
155741016Sdfr    register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
155841016Sdfr    unsigned char buf[PSM_SMALLBUFSIZE];
155941016Sdfr    int error = 0;
156041016Sdfr    int s;
156141016Sdfr    int l;
156241016Sdfr
156341016Sdfr    if ((sc->state & PSM_VALID) == 0)
156441016Sdfr	return EIO;
156541016Sdfr
156641016Sdfr    /* block until mouse activity occured */
156741016Sdfr    s = spltty();
156841016Sdfr    while (sc->queue.count <= 0) {
156941016Sdfr        if (PSM_NBLOCKIO(dev)) {
157041016Sdfr            splx(s);
157141016Sdfr            return EWOULDBLOCK;
157241016Sdfr        }
157341016Sdfr        sc->state |= PSM_ASLP;
1574111748Sdes        error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
157541016Sdfr        sc->state &= ~PSM_ASLP;
157641016Sdfr        if (error) {
157741016Sdfr            splx(s);
157841016Sdfr            return error;
157941016Sdfr        } else if ((sc->state & PSM_VALID) == 0) {
158041016Sdfr            /* the device disappeared! */
158141016Sdfr            splx(s);
158241016Sdfr            return EIO;
158341016Sdfr	}
158441016Sdfr    }
158541016Sdfr    splx(s);
158641016Sdfr
158741016Sdfr    /* copy data to the user land */
158841016Sdfr    while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
158941016Sdfr        s = spltty();
1590109269Smdodd	l = imin(sc->queue.count, uio->uio_resid);
159141016Sdfr	if (l > sizeof(buf))
159241016Sdfr	    l = sizeof(buf);
159341016Sdfr	if (l > sizeof(sc->queue.buf) - sc->queue.head) {
159441016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
159541016Sdfr		sizeof(sc->queue.buf) - sc->queue.head);
159641016Sdfr	    bcopy(&sc->queue.buf[0],
159741016Sdfr		&buf[sizeof(sc->queue.buf) - sc->queue.head],
159841016Sdfr		l - (sizeof(sc->queue.buf) - sc->queue.head));
159941016Sdfr	} else {
160041016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
160141016Sdfr	}
160241016Sdfr	sc->queue.count -= l;
160341016Sdfr	sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
160441016Sdfr        splx(s);
160541016Sdfr        error = uiomove(buf, l, uio);
160641016Sdfr        if (error)
160741016Sdfr	    break;
160841016Sdfr    }
160941016Sdfr
161041016Sdfr    return error;
161141016Sdfr}
161241016Sdfr
161341016Sdfrstatic int
161441016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
161541016Sdfr{
161641016Sdfr    int s;
161741016Sdfr
161841016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
161941016Sdfr	return EIO;
162041016Sdfr
162141016Sdfr    s = spltty();
162241016Sdfr    *c = get_controller_command_byte(sc->kbdc);
162341016Sdfr    if ((*c == -1)
162441016Sdfr	|| !set_controller_command_byte(sc->kbdc,
162541016Sdfr	    kbdc_get_device_mask(sc->kbdc),
162641016Sdfr            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
162741016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
162841016Sdfr        /* this is CONTROLLER ERROR */
162941016Sdfr	splx(s);
163041016Sdfr        kbdc_lock(sc->kbdc, FALSE);
163141016Sdfr	return EIO;
163241016Sdfr    }
163341016Sdfr
163441016Sdfr    /*
163541016Sdfr     * The device may be in the middle of status data transmission.
163641016Sdfr     * The transmission will be interrupted, thus, incomplete status
163741016Sdfr     * data must be discarded. Although the aux interrupt is disabled
163841016Sdfr     * at the keyboard controller level, at most one aux interrupt
163941016Sdfr     * may have already been pending and a data byte is in the
164041016Sdfr     * output buffer; throw it away. Note that the second argument
164141016Sdfr     * to `empty_aux_buffer()' is zero, so that the call will just
164241016Sdfr     * flush the internal queue.
164341016Sdfr     * `psmintr()' will be invoked after `splx()' if an interrupt is
164441016Sdfr     * pending; it will see no data and returns immediately.
164541016Sdfr     */
164641016Sdfr    empty_aux_buffer(sc->kbdc, 0);	/* flush the queue */
164741016Sdfr    read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
1648123442Salfred    flushpackets(sc);
164941016Sdfr    splx(s);
165041016Sdfr
165141016Sdfr    return 0;
165241016Sdfr}
165341016Sdfr
1654123442Salfredstatic void
1655123442Salfreddropqueue(struct psm_softc *sc)
1656123442Salfred{
1657123442Salfred
1658123442Salfred    	sc->queue.count = 0;
1659123442Salfred   	sc->queue.head = 0;
1660123442Salfred    	sc->queue.tail = 0;
1661123442Salfred	if ((sc->state & PSM_SOFTARMED) != 0) {
1662123442Salfred		sc->state &= ~PSM_SOFTARMED;
1663123442Salfred		untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout);
1664123442Salfred	}
1665123442Salfred	sc->pqueue_start = sc->pqueue_end;
1666123442Salfred}
1667123442Salfred
1668123442Salfredstatic void
1669123442Salfredflushpackets(struct psm_softc *sc)
1670123442Salfred{
1671123442Salfred
1672123442Salfred	dropqueue(sc);
1673123442Salfred	bzero(&sc->pqueue, sizeof(sc->pqueue));
1674123442Salfred}
1675123442Salfred
167641016Sdfrstatic int
167741016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
167841016Sdfr{
167941016Sdfr    int error = 0;
168041016Sdfr
168141016Sdfr    /*
168241016Sdfr     * We may have seen a part of status data during `set_mouse_XXX()'.
168341016Sdfr     * they have been queued; flush it.
168441016Sdfr     */
168541016Sdfr    empty_aux_buffer(sc->kbdc, 0);
168641016Sdfr
168741016Sdfr    /* restore ports and interrupt */
168841016Sdfr    if (!set_controller_command_byte(sc->kbdc,
168941016Sdfr            kbdc_get_device_mask(sc->kbdc),
169041016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
169141016Sdfr        /* CONTROLLER ERROR; this is serious, we may have
169241016Sdfr         * been left with the inaccessible keyboard and
169341016Sdfr         * the disabled mouse interrupt.
169441016Sdfr         */
169541016Sdfr        error = EIO;
169641016Sdfr    }
169741016Sdfr
169841016Sdfr    kbdc_lock(sc->kbdc, FALSE);
169941016Sdfr    return error;
170041016Sdfr}
170141016Sdfr
170241016Sdfrstatic int
1703130585Sphkpsmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
170441016Sdfr{
170541016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
170641016Sdfr    mousemode_t mode;
170741016Sdfr    mousestatus_t status;
170841016Sdfr#if (defined(MOUSE_GETVARS))
170941016Sdfr    mousevar_t *var;
171041016Sdfr#endif
171141016Sdfr    mousedata_t *data;
171241016Sdfr    int stat[3];
171341016Sdfr    int command_byte;
171441016Sdfr    int error = 0;
171541016Sdfr    int s;
171641016Sdfr
171741016Sdfr    /* Perform IOCTL command */
171841016Sdfr    switch (cmd) {
171941016Sdfr
172041016Sdfr    case OLD_MOUSE_GETHWINFO:
172141016Sdfr	s = spltty();
172241016Sdfr        ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
172341016Sdfr        ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
172441016Sdfr        ((old_mousehw_t *)addr)->type = sc->hw.type;
172548778Syokota        ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
172641016Sdfr	splx(s);
172741016Sdfr        break;
172841016Sdfr
172941016Sdfr    case MOUSE_GETHWINFO:
173041016Sdfr	s = spltty();
173141016Sdfr        *(mousehw_t *)addr = sc->hw;
173241016Sdfr	if (sc->mode.level == PSM_LEVEL_BASE)
173341016Sdfr	    ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
173441016Sdfr	splx(s);
173541016Sdfr        break;
173641016Sdfr
1737132865Snjl    case MOUSE_SYN_GETHWINFO:
1738132865Snjl	s = spltty();
1739132865Snjl	if (sc->hw.model == MOUSE_MODEL_SYNAPTICS)
1740132865Snjl	    *(synapticshw_t *)addr = sc->synhw;
1741132865Snjl	else
1742132865Snjl	    error = EINVAL;
1743132865Snjl	splx(s);
1744132865Snjl	break;
1745132865Snjl
174641016Sdfr    case OLD_MOUSE_GETMODE:
174741016Sdfr	s = spltty();
174841016Sdfr	switch (sc->mode.level) {
174941016Sdfr	case PSM_LEVEL_BASE:
175041016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
175141016Sdfr	    break;
175241016Sdfr	case PSM_LEVEL_STANDARD:
175341016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
175441016Sdfr	    break;
175541016Sdfr	case PSM_LEVEL_NATIVE:
175641016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
175741016Sdfr	    break;
175841016Sdfr	}
175941016Sdfr        ((old_mousemode_t *)addr)->rate = sc->mode.rate;
176041016Sdfr        ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
176141016Sdfr        ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
176241016Sdfr	splx(s);
176341016Sdfr        break;
176441016Sdfr
176541016Sdfr    case MOUSE_GETMODE:
176641016Sdfr	s = spltty();
176741016Sdfr        *(mousemode_t *)addr = sc->mode;
176841016Sdfr        ((mousemode_t *)addr)->resolution =
176941016Sdfr	    MOUSE_RES_LOW - sc->mode.resolution;
177041016Sdfr	switch (sc->mode.level) {
177141016Sdfr	case PSM_LEVEL_BASE:
177241016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
177341016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
177441016Sdfr	    break;
177541016Sdfr	case PSM_LEVEL_STANDARD:
177641016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
177741016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
177841016Sdfr	    ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
177941016Sdfr	    ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
178041016Sdfr	    break;
178141016Sdfr	case PSM_LEVEL_NATIVE:
178241016Sdfr	    /* FIXME: this isn't quite correct... XXX */
178341016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
178441016Sdfr	    break;
178541016Sdfr	}
178641016Sdfr	splx(s);
178741016Sdfr        break;
178841016Sdfr
178941016Sdfr    case OLD_MOUSE_SETMODE:
179041016Sdfr    case MOUSE_SETMODE:
179141016Sdfr	if (cmd == OLD_MOUSE_SETMODE) {
179241016Sdfr	    mode.rate = ((old_mousemode_t *)addr)->rate;
179341016Sdfr	    /*
179441016Sdfr	     * resolution  old I/F   new I/F
179541016Sdfr	     * default        0         0
179641016Sdfr	     * low            1        -2
179741016Sdfr	     * medium low     2        -3
179841016Sdfr	     * medium high    3        -4
179941016Sdfr	     * high           4        -5
180041016Sdfr	     */
180141016Sdfr	    if (((old_mousemode_t *)addr)->resolution > 0)
180241016Sdfr	        mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
180341016Sdfr	    mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
180441016Sdfr	    mode.level = -1;
180541016Sdfr	} else {
180641016Sdfr	    mode = *(mousemode_t *)addr;
180741016Sdfr	}
180841016Sdfr
180941016Sdfr	/* adjust and validate parameters. */
181041016Sdfr	if (mode.rate > UCHAR_MAX)
181141016Sdfr	    return EINVAL;
181241016Sdfr        if (mode.rate == 0)
181341016Sdfr            mode.rate = sc->dflt_mode.rate;
181441016Sdfr	else if (mode.rate == -1)
181541016Sdfr	    /* don't change the current setting */
181641016Sdfr	    ;
181741016Sdfr	else if (mode.rate < 0)
181841016Sdfr	    return EINVAL;
181941016Sdfr	if (mode.resolution >= UCHAR_MAX)
182041016Sdfr	    return EINVAL;
182141016Sdfr	if (mode.resolution >= 200)
182241016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
182341016Sdfr	else if (mode.resolution >= 100)
182441016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
182541016Sdfr	else if (mode.resolution >= 50)
182641016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
182741016Sdfr	else if (mode.resolution > 0)
182841016Sdfr	    mode.resolution = MOUSE_RES_LOW;
182941016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
183041016Sdfr            mode.resolution = sc->dflt_mode.resolution;
183141016Sdfr        else if (mode.resolution == -1)
183241016Sdfr	    /* don't change the current setting */
183341016Sdfr	    ;
183441016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
183541016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
183641016Sdfr	if (mode.level == -1)
183741016Sdfr	    /* don't change the current setting */
183841016Sdfr	    mode.level = sc->mode.level;
183941016Sdfr	else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
184041016Sdfr	    return EINVAL;
184141016Sdfr        if (mode.accelfactor == -1)
184241016Sdfr	    /* don't change the current setting */
184341016Sdfr	    mode.accelfactor = sc->mode.accelfactor;
184441016Sdfr        else if (mode.accelfactor < 0)
184541016Sdfr	    return EINVAL;
184641016Sdfr
184741016Sdfr	/* don't allow anybody to poll the keyboard controller */
184841016Sdfr	error = block_mouse_data(sc, &command_byte);
184941016Sdfr	if (error)
185041016Sdfr            return error;
185141016Sdfr
185241016Sdfr        /* set mouse parameters */
185341016Sdfr	if (mode.rate > 0)
185441016Sdfr	    mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
185541016Sdfr	if (mode.resolution >= 0)
185641016Sdfr	    mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
185741016Sdfr	set_mouse_scaling(sc->kbdc, 1);
185841016Sdfr	get_mouse_status(sc->kbdc, stat, 0, 3);
185941016Sdfr
186041016Sdfr        s = spltty();
186141016Sdfr    	sc->mode.rate = mode.rate;
186241016Sdfr    	sc->mode.resolution = mode.resolution;
186341016Sdfr    	sc->mode.accelfactor = mode.accelfactor;
186441016Sdfr    	sc->mode.level = mode.level;
186541016Sdfr        splx(s);
186641016Sdfr
186741016Sdfr	unblock_mouse_data(sc, command_byte);
186841016Sdfr        break;
186941016Sdfr
187041016Sdfr    case MOUSE_GETLEVEL:
187141016Sdfr	*(int *)addr = sc->mode.level;
187241016Sdfr        break;
187341016Sdfr
187441016Sdfr    case MOUSE_SETLEVEL:
187541016Sdfr	if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
187641016Sdfr	    return EINVAL;
187741016Sdfr	sc->mode.level = *(int *)addr;
187841016Sdfr        break;
187941016Sdfr
188041016Sdfr    case MOUSE_GETSTATUS:
188141016Sdfr        s = spltty();
188241016Sdfr	status = sc->status;
188341016Sdfr	sc->status.flags = 0;
188441016Sdfr	sc->status.obutton = sc->status.button;
188541016Sdfr	sc->status.button = 0;
188641016Sdfr	sc->status.dx = 0;
188741016Sdfr	sc->status.dy = 0;
188841016Sdfr	sc->status.dz = 0;
188941016Sdfr        splx(s);
189041016Sdfr        *(mousestatus_t *)addr = status;
189141016Sdfr        break;
189241016Sdfr
189341016Sdfr#if (defined(MOUSE_GETVARS))
189441016Sdfr    case MOUSE_GETVARS:
189541016Sdfr	var = (mousevar_t *)addr;
189641016Sdfr	bzero(var, sizeof(*var));
189741016Sdfr	s = spltty();
189841016Sdfr        var->var[0] = MOUSE_VARS_PS2_SIG;
189941016Sdfr        var->var[1] = sc->config;
190041016Sdfr        var->var[2] = sc->flags;
190141016Sdfr	splx(s);
190241016Sdfr        break;
190341016Sdfr
190441016Sdfr    case MOUSE_SETVARS:
190541016Sdfr	return ENODEV;
190641016Sdfr#endif /* MOUSE_GETVARS */
190741016Sdfr
190841016Sdfr    case MOUSE_READSTATE:
190941016Sdfr    case MOUSE_READDATA:
191041016Sdfr	data = (mousedata_t *)addr;
191141016Sdfr	if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
191241016Sdfr	    return EINVAL;
191341016Sdfr
191441016Sdfr	error = block_mouse_data(sc, &command_byte);
191541016Sdfr	if (error)
191641016Sdfr            return error;
191741016Sdfr        if ((data->len = get_mouse_status(sc->kbdc, data->buf,
191841016Sdfr		(cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
191941016Sdfr            error = EIO;
192041016Sdfr	unblock_mouse_data(sc, command_byte);
192141016Sdfr	break;
192241016Sdfr
192341016Sdfr#if (defined(MOUSE_SETRESOLUTION))
192441016Sdfr    case MOUSE_SETRESOLUTION:
192541016Sdfr	mode.resolution = *(int *)addr;
192641016Sdfr	if (mode.resolution >= UCHAR_MAX)
192741016Sdfr	    return EINVAL;
192841016Sdfr	else if (mode.resolution >= 200)
192941016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
193041016Sdfr	else if (mode.resolution >= 100)
193141016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
193241016Sdfr	else if (mode.resolution >= 50)
193341016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
193441016Sdfr	else if (mode.resolution > 0)
193541016Sdfr	    mode.resolution = MOUSE_RES_LOW;
193641016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
193741016Sdfr            mode.resolution = sc->dflt_mode.resolution;
193841016Sdfr        else if (mode.resolution == -1)
193941016Sdfr	    mode.resolution = sc->mode.resolution;
194041016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
194141016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
194241016Sdfr
194341016Sdfr	error = block_mouse_data(sc, &command_byte);
194441016Sdfr	if (error)
194541016Sdfr            return error;
194641016Sdfr        sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
194741016Sdfr	if (sc->mode.resolution != mode.resolution)
194841016Sdfr	    error = EIO;
194941016Sdfr	unblock_mouse_data(sc, command_byte);
195041016Sdfr        break;
195141016Sdfr#endif /* MOUSE_SETRESOLUTION */
195241016Sdfr
195341016Sdfr#if (defined(MOUSE_SETRATE))
195441016Sdfr    case MOUSE_SETRATE:
195541016Sdfr	mode.rate = *(int *)addr;
195641016Sdfr	if (mode.rate > UCHAR_MAX)
195741016Sdfr	    return EINVAL;
195841016Sdfr        if (mode.rate == 0)
195941016Sdfr            mode.rate = sc->dflt_mode.rate;
196041016Sdfr	else if (mode.rate < 0)
196141016Sdfr	    mode.rate = sc->mode.rate;
196241016Sdfr
196341016Sdfr	error = block_mouse_data(sc, &command_byte);
196441016Sdfr	if (error)
196541016Sdfr            return error;
196641016Sdfr        sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
196741016Sdfr	if (sc->mode.rate != mode.rate)
196841016Sdfr	    error = EIO;
196941016Sdfr	unblock_mouse_data(sc, command_byte);
197041016Sdfr        break;
197141016Sdfr#endif /* MOUSE_SETRATE */
197241016Sdfr
197341016Sdfr#if (defined(MOUSE_SETSCALING))
197441016Sdfr    case MOUSE_SETSCALING:
197541016Sdfr	if ((*(int *)addr <= 0) || (*(int *)addr > 2))
197641016Sdfr	    return EINVAL;
197741016Sdfr
197841016Sdfr	error = block_mouse_data(sc, &command_byte);
197941016Sdfr	if (error)
198041016Sdfr            return error;
198141016Sdfr        if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
198241016Sdfr	    error = EIO;
198341016Sdfr	unblock_mouse_data(sc, command_byte);
198441016Sdfr        break;
198541016Sdfr#endif /* MOUSE_SETSCALING */
198641016Sdfr
198741016Sdfr#if (defined(MOUSE_GETHWID))
198841016Sdfr    case MOUSE_GETHWID:
198941016Sdfr	error = block_mouse_data(sc, &command_byte);
199041016Sdfr	if (error)
199141016Sdfr            return error;
199248778Syokota        sc->hw.hwid &= ~0x00ff;
199348778Syokota        sc->hw.hwid |= get_aux_id(sc->kbdc);
199448778Syokota	*(int *)addr = sc->hw.hwid & 0x00ff;
199541016Sdfr	unblock_mouse_data(sc, command_byte);
199641016Sdfr        break;
199741016Sdfr#endif /* MOUSE_GETHWID */
199841016Sdfr
199941016Sdfr    default:
200041016Sdfr	return ENOTTY;
200141016Sdfr    }
200241016Sdfr
200341016Sdfr    return error;
200441016Sdfr}
200541016Sdfr
200641016Sdfrstatic void
200758230Syokotapsmtimeout(void *arg)
200858230Syokota{
200958230Syokota    struct psm_softc *sc;
201065045Syokota    int s;
201158230Syokota
201284880Syokota    sc = (struct psm_softc *)arg;
201365045Syokota    s = spltty();
201463746Syokota    if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
201558230Syokota	if (verbose >= 4)
201684880Syokota	    log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
201758230Syokota	psmintr(sc);
201863746Syokota	kbdc_lock(sc->kbdc, FALSE);
201958230Syokota    }
202058230Syokota    sc->watchdog = TRUE;
202165045Syokota    splx(s);
202284880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
202358230Syokota}
202458230Syokota
2025123442Salfredstatic int psmhz = 20;
2026123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmhz, CTLFLAG_RW, &psmhz, 0, "");
2027123442Salfred
2028123442Salfredstatic int psm_soft_timeout = 500000; /* 0.5 sec */
2029123442SalfredSYSCTL_INT(_debug, OID_AUTO, psm_soft_timeout, CTLFLAG_RW,
2030123442Salfred    &psm_soft_timeout, 0, "");
2031123442Salfred
2032123442Salfredstatic int psmerrsecs = 2;
2033123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrsecs, CTLFLAG_RW, &psmerrsecs, 0, "");
2034123442Salfredstatic int psmerrusecs = 0;
2035123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrusecs, CTLFLAG_RW, &psmerrusecs, 0, "");
2036123442Salfredstatic int psmsecs = 0;
2037123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmsecs, CTLFLAG_RW, &psmsecs, 0, "");
2038123442Salfredstatic int psmusecs = 500000;
2039123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmusecs, CTLFLAG_RW, &psmusecs, 0, "");
2040123442Salfred
204158230Syokotastatic void
204241016Sdfrpsmintr(void *arg)
204341016Sdfr{
2044123442Salfred    struct psm_softc *sc = arg;
2045123442Salfred    struct timeval now;
204641016Sdfr    int c;
2047123442Salfred    packetbuf_t *pb;
2048123442Salfred    int haderror = 0;
204941016Sdfr
2050123442Salfred
205141016Sdfr    /* read until there is nothing to read */
205241016Sdfr    while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
205341016Sdfr
2054123442Salfred        pb = &sc->pqueue[sc->pqueue_end];
205541016Sdfr        /* discard the byte if the device is not open */
205641016Sdfr        if ((sc->state & PSM_OPEN) == 0)
205741016Sdfr            continue;
205841016Sdfr
2059123442Salfred	getmicrouptime(&now);
2060123442Salfred	if ((pb->inputbytes > 0) && timevalcmp(&now, &sc->inputtimeout, >)) {
2061127841Smarkm#if DEBUG
206284880Syokota	    log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2063127841Smarkm#endif
2064123442Salfred	    pb->inputbytes = 0;
206584880Syokota	    sc->syncerrors = 0;
206684880Syokota	}
206784880Syokota	sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
206884880Syokota	sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2069123442Salfred	timevaladd(&sc->inputtimeout, &now);
207084880Syokota
2071123442Salfred        pb->ipacket[pb->inputbytes++] = c;
2072123442Salfred        if (pb->inputbytes < sc->mode.packetsize)
207341016Sdfr	    continue;
207441016Sdfr
2075127841Smarkm#if DEBUG
207641016Sdfr        log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2077123442Salfred	    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2078123442Salfred	    pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]);
207941016Sdfr#endif
208041016Sdfr
2081123442Salfred	c = pb->ipacket[0];
208241016Sdfr
208363746Syokota	if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2084127841Smarkm#if DEBUG
2085123442Salfred            log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x) %d"
2086123442Salfred		" cmds since last error.\n",
2087123442Salfred		c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2088123442Salfred		sc->cmdcount - sc->lasterr);
2089127841Smarkm#endif
2090123442Salfred	    haderror = 1;
2091123442Salfred	    sc->lasterr = sc->cmdcount;
2092123442Salfred	    dropqueue(sc);
209384880Syokota	    ++sc->syncerrors;
2094123442Salfred	    sc->lastinputerr = now;
209584880Syokota	    if (sc->syncerrors < sc->mode.packetsize) {
2096127841Smarkm#if DEBUG
209784880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2098127841Smarkm#endif
2099123442Salfred		--pb->inputbytes;
2100123442Salfred		bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
210184880Syokota	    } else if (sc->syncerrors == sc->mode.packetsize) {
2102127841Smarkm#if DEBUG
210369439Syokota		log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2104127841Smarkm#endif
2105123442Salfred		pb->inputbytes = 0;
210669439Syokota		disable_aux_dev(sc->kbdc);
210769439Syokota		enable_aux_dev(sc->kbdc);
210884880Syokota	    } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2109127841Smarkm#if DEBUG
211084880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2111127841Smarkm#endif
2112123442Salfred		--pb->inputbytes;
2113123442Salfred		bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
211484880Syokota	    } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2115127841Smarkm#if DEBUG
211684880Syokota		log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2117127841Smarkm#endif
211884880Syokota		reinitialize(sc, TRUE);
211969439Syokota	    }
212084880Syokota	    continue;
212163746Syokota	}
2122123442Salfred	/* if this packet is at all bogus then drop the packet. */
2123123442Salfred	if (haderror ||
2124123442Salfred	    !timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, &now)) {
2125123442Salfred		pb->inputbytes = 0;
2126123442Salfred		haderror = 0;
2127123442Salfred		continue;
2128123442Salfred	}
212963746Syokota
2130123442Salfred	sc->cmdcount++;
2131123442Salfred	if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2132123442Salfred		sc->pqueue_end = 0;
2133123442Salfred	/*
2134123442Salfred	 * If we've filled the queue then call the softintr ourselves,
2135123442Salfred	 * otherwise schedule the interrupt for later.
2136123442Salfred	 */
2137123442Salfred	if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2138123442Salfred	    (sc->pqueue_end == sc->pqueue_start)) {
2139123442Salfred    		if ((sc->state & PSM_SOFTARMED) != 0) {
2140123442Salfred			sc->state &= ~PSM_SOFTARMED;
2141123442Salfred			untimeout(psmsoftintr, arg, sc->softcallout);
2142123442Salfred		}
2143123442Salfred		psmsoftintr(arg);
2144123442Salfred	} else if ((sc->state & PSM_SOFTARMED) == 0) {
2145123442Salfred		sc->state |= PSM_SOFTARMED;
2146123442Salfred		sc->softcallout = timeout(psmsoftintr, arg,
2147123442Salfred		    psmhz < 1 ? 1 : (hz/psmhz));
2148123442Salfred	}
2149123442Salfred    }
2150123442Salfred}
2151123442Salfred
2152123442Salfredstatic void
2153123442Salfredpsmsoftintr(void *arg)
2154123442Salfred{
2155123442Salfred    /*
2156123442Salfred     * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
2157123442Salfred     * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
2158123442Salfred     */
2159123442Salfred    static int butmap[8] = {
2160123442Salfred        0,
2161123442Salfred	MOUSE_BUTTON1DOWN,
2162123442Salfred	MOUSE_BUTTON3DOWN,
2163123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2164123442Salfred	MOUSE_BUTTON2DOWN,
2165123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
2166123442Salfred	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
2167123442Salfred        MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
2168123442Salfred    };
2169123442Salfred    static int butmap_versapad[8] = {
2170123442Salfred	0,
2171123442Salfred	MOUSE_BUTTON3DOWN,
2172123442Salfred	0,
2173123442Salfred	MOUSE_BUTTON3DOWN,
2174123442Salfred	MOUSE_BUTTON1DOWN,
2175123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2176123442Salfred	MOUSE_BUTTON1DOWN,
2177123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
2178123442Salfred    };
2179123442Salfred    register struct psm_softc *sc = arg;
2180123442Salfred    mousestatus_t ms;
2181132865Snjl    int w, x, y, z;
2182123442Salfred    int c;
2183123442Salfred    int l;
2184123442Salfred    int x0, y0;
2185123442Salfred    int s;
2186123442Salfred    packetbuf_t *pb;
2187123442Salfred
2188123442Salfred    getmicrouptime(&sc->lastsoftintr);
2189123442Salfred
2190123442Salfred    s = spltty();
2191123442Salfred
2192123442Salfred    do {
2193123442Salfred
2194123442Salfred	pb = &sc->pqueue[sc->pqueue_start];
2195123442Salfred	c = pb->ipacket[0];
219649965Syokota	/*
219741016Sdfr	 * A kludge for Kensington device!
219841016Sdfr	 * The MSB of the horizontal count appears to be stored in
219958230Syokota	 * a strange place.
220041016Sdfr	 */
220158230Syokota	if (sc->hw.model == MOUSE_MODEL_THINK)
2202123442Salfred	    pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
220341016Sdfr
220441016Sdfr        /* ignore the overflow bits... */
2205123442Salfred        x = (c & MOUSE_PS2_XNEG) ?  pb->ipacket[1] - 256 : pb->ipacket[1];
2206123442Salfred        y = (c & MOUSE_PS2_YNEG) ?  pb->ipacket[2] - 256 : pb->ipacket[2];
220741016Sdfr	z = 0;
220841016Sdfr        ms.obutton = sc->button;		  /* previous button state */
220941016Sdfr        ms.button = butmap[c & MOUSE_PS2_BUTTONS];
221045789Speter	/* `tapping' action */
221145789Speter	if (sc->config & PSM_CONFIG_FORCETAP)
221245789Speter	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
221341016Sdfr
221441016Sdfr	switch (sc->hw.model) {
221541016Sdfr
221658230Syokota	case MOUSE_MODEL_EXPLORER:
221758230Syokota	    /*
221858230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
221958230Syokota	     * byte 1:  oy ox sy sx 1  M  R  L
222058230Syokota	     * byte 2:  x  x  x  x  x  x  x  x
222158230Syokota	     * byte 3:  y  y  y  y  y  y  y  y
222258230Syokota	     * byte 4:  *  *  S2 S1 s  d2 d1 d0
222358230Syokota	     *
222458230Syokota	     * L, M, R, S1, S2: left, middle, right and side buttons
222558230Syokota	     * s: wheel data sign bit
222658230Syokota	     * d2-d0: wheel data
222758230Syokota	     */
2228123442Salfred	    z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2229123442Salfred		? (pb->ipacket[3] & 0x0f) - 16 : (pb->ipacket[3] & 0x0f);
2230123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
223158230Syokota		? MOUSE_BUTTON4DOWN : 0;
2232123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
223358230Syokota		? MOUSE_BUTTON5DOWN : 0;
223458230Syokota	    break;
223558230Syokota
223641016Sdfr	case MOUSE_MODEL_INTELLI:
223741016Sdfr	case MOUSE_MODEL_NET:
223841016Sdfr	    /* wheel data is in the fourth byte */
2239123442Salfred	    z = (char)pb->ipacket[3];
224058230Syokota	    /* some mice may send 7 when there is no Z movement?! XXX */
224158230Syokota	    if ((z >= 7) || (z <= -7))
224258230Syokota		z = 0;
224358230Syokota	    /* some compatible mice have additional buttons */
224458230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
224558230Syokota		? MOUSE_BUTTON4DOWN : 0;
224658230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
224758230Syokota		? MOUSE_BUTTON5DOWN : 0;
224841016Sdfr	    break;
224941016Sdfr
225041016Sdfr	case MOUSE_MODEL_MOUSEMANPLUS:
225148778Syokota	    /*
225248778Syokota	     * PS2++ protocl packet
225348778Syokota	     *
225448778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
225548778Syokota	     * byte 1:  *  1  p3 p2 1  *  *  *
225648778Syokota	     * byte 2:  c1 c2 p1 p0 d1 d0 1  0
225748778Syokota	     *
225848778Syokota	     * p3-p0: packet type
225948778Syokota	     * c1, c2: c1 & c2 == 1, if p2 == 0
226048778Syokota	     *         c1 & c2 == 0, if p2 == 1
226148778Syokota	     *
226248778Syokota	     * packet type: 0 (device type)
226348778Syokota	     * See comments in enable_mmanplus() below.
226448778Syokota	     *
226548778Syokota	     * packet type: 1 (wheel data)
226648778Syokota	     *
226748778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
226848778Syokota	     * byte 3:  h  *  B5 B4 s  d2 d1 d0
226948778Syokota	     *
227048778Syokota	     * h: 1, if horizontal roller data
227148778Syokota	     *    0, if vertical roller data
227248778Syokota	     * B4, B5: button 4 and 5
227348778Syokota	     * s: sign bit
227448778Syokota	     * d2-d0: roller data
227548778Syokota	     *
227648778Syokota	     * packet type: 2 (reserved)
227748778Syokota	     */
227848778Syokota	    if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
227948778Syokota		    && (abs(x) > 191)
2280123442Salfred		    && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
228141016Sdfr		/* the extended data packet encodes button and wheel events */
2282123442Salfred		switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
228348778Syokota		case 1:
228448778Syokota		    /* wheel data packet */
228548778Syokota		    x = y = 0;
2286123442Salfred		    if (pb->ipacket[2] & 0x80) {
228748778Syokota			/* horizontal roller count - ignore it XXX*/
228848778Syokota		    } else {
228948778Syokota			/* vertical roller count */
2290123442Salfred			z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2291123442Salfred			    ? (pb->ipacket[2] & 0x0f) - 16
2292123442Salfred			    : (pb->ipacket[2] & 0x0f);
229348778Syokota		    }
2294123442Salfred		    ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
229548778Syokota			? MOUSE_BUTTON4DOWN : 0;
2296123442Salfred		    ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
229748778Syokota			? MOUSE_BUTTON5DOWN : 0;
229848778Syokota		    break;
229948778Syokota		case 2:
230058230Syokota		    /* this packet type is reserved by Logitech... */
230158230Syokota		    /*
230258230Syokota		     * IBM ScrollPoint Mouse uses this packet type to
230358230Syokota		     * encode both vertical and horizontal scroll movement.
230458230Syokota		     */
230558230Syokota		    x = y = 0;
230658230Syokota		    /* horizontal count */
2307123442Salfred		    if (pb->ipacket[2] & 0x0f)
2308123442Salfred			z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
230958230Syokota		    /* vertical count */
2310123442Salfred		    if (pb->ipacket[2] & 0xf0)
2311123442Salfred			z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
231258230Syokota#if 0
231358230Syokota		    /* vertical count */
2314123442Salfred		    z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG)
2315123442Salfred			? ((pb->ipacket[2] >> 4) & 0x0f) - 16
2316123442Salfred			: ((pb->ipacket[2] >> 4) & 0x0f);
231758230Syokota		    /* horizontal count */
2318123442Salfred		    w = (pb->ipacket[2] & MOUSE_SPOINT_WNEG)
2319123442Salfred			? (pb->ipacket[2] & 0x0f) - 16
2320123442Salfred			: (pb->ipacket[2] & 0x0f);
232158230Syokota#endif
232258230Syokota		    break;
232348778Syokota		case 0:
232448778Syokota		    /* device type packet - shouldn't happen */
2325102412Scharnier		    /* FALLTHROUGH */
232648778Syokota		default:
232748778Syokota		    x = y = 0;
232848778Syokota		    ms.button = ms.obutton;
232958230Syokota		    if (bootverbose)
233058230Syokota			log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
233158230Syokota				       "0x%02x 0x%02x 0x%02x\n",
2332123442Salfred			    MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2333123442Salfred			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]);
233448778Syokota		    break;
233548778Syokota		}
233641016Sdfr	    } else {
233741016Sdfr		/* preserve button states */
233841016Sdfr		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
233941016Sdfr	    }
234041016Sdfr	    break;
234141016Sdfr
234241016Sdfr	case MOUSE_MODEL_GLIDEPOINT:
234341016Sdfr	    /* `tapping' action */
234441016Sdfr	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
234541016Sdfr	    break;
234641016Sdfr
234741016Sdfr	case MOUSE_MODEL_NETSCROLL:
234858230Syokota	    /* three addtional bytes encode buttons and wheel events */
2349123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
235041016Sdfr		? MOUSE_BUTTON4DOWN : 0;
2351123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
235258230Syokota		? MOUSE_BUTTON5DOWN : 0;
2353123442Salfred	    z = (pb->ipacket[3] & MOUSE_PS2_XNEG)
2354123442Salfred		? pb->ipacket[4] - 256 : pb->ipacket[4];
235541016Sdfr	    break;
235641016Sdfr
235741016Sdfr	case MOUSE_MODEL_THINK:
235841016Sdfr	    /* the fourth button state in the first byte */
235941016Sdfr	    ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
236041016Sdfr	    break;
236141016Sdfr
236249965Syokota	case MOUSE_MODEL_VERSAPAD:
236349965Syokota	    /* VersaPad PS/2 absolute mode message format
236449965Syokota	     *
236549965Syokota	     * [packet1]     7   6   5   4   3   2   1   0(LSB)
236649965Syokota	     *  ipacket[0]:  1   1   0   A   1   L   T   R
236749965Syokota	     *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
236849965Syokota	     *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
236949965Syokota	     *  ipacket[3]:  1   1   1   A   1   L   T   R
237049965Syokota	     *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
237149965Syokota	     *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
237249965Syokota	     *
237349965Syokota	     * [note]
237449965Syokota	     *  R: right physical mouse button (1=on)
237549965Syokota	     *  T: touch pad virtual button (1=tapping)
237649965Syokota	     *  L: left physical mouse button (1=on)
237749965Syokota	     *  A: position data is valid (1=valid)
237849965Syokota	     *  H: horizontal data (12bit signed integer. H11 is sign bit.)
237949965Syokota	     *  V: vertical data (12bit signed integer. V11 is sign bit.)
238049965Syokota	     *  P: pressure data
238149965Syokota	     *
238249965Syokota	     * Tapping is mapped to MOUSE_BUTTON4.
238349965Syokota	     */
238449965Syokota	    ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
238549965Syokota	    ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
238649965Syokota	    x = y = 0;
238749965Syokota	    if (c & MOUSE_PS2VERSA_IN_USE) {
2388123442Salfred		x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
2389123442Salfred		y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
239049965Syokota		if (x0 & 0x800)
239149965Syokota		    x0 -= 0x1000;
239249965Syokota		if (y0 & 0x800)
239349965Syokota		    y0 -= 0x1000;
239449965Syokota		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
239549965Syokota		    x = sc->xold - x0;
239649965Syokota		    y = y0 - sc->yold;
239749965Syokota		    if (x < 0)	/* XXX */
239849965Syokota			x++;
239949965Syokota		    else if (x)
240049965Syokota			x--;
240149965Syokota		    if (y < 0)
240249965Syokota			y++;
240349965Syokota		    else if (y)
240449965Syokota			y--;
240549965Syokota		} else {
240649965Syokota		    sc->flags |= PSM_FLAGS_FINGERDOWN;
240749965Syokota		}
240849965Syokota		sc->xold = x0;
240949965Syokota		sc->yold = y0;
241049965Syokota	    } else {
241149965Syokota		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
241249965Syokota	    }
241349965Syokota	    c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
241449965Syokota		| ((y < 0) ? MOUSE_PS2_YNEG : 0);
241549965Syokota	    break;
241649965Syokota
241758230Syokota	case MOUSE_MODEL_4D:
241858230Syokota	    /*
241958230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
242058230Syokota	     * byte 1:  s2 d2 s1 d1 1  M  R  L
242158230Syokota	     * byte 2:  sx x  x  x  x  x  x  x
242258230Syokota	     * byte 3:  sy y  y  y  y  y  y  y
242358230Syokota	     *
242458230Syokota	     * s1: wheel 1 direction
242558230Syokota	     * d1: wheel 1 data
242658230Syokota	     * s2: wheel 2 direction
242758230Syokota	     * d2: wheel 2 data
242858230Syokota	     */
2429123442Salfred	    x = (pb->ipacket[1] & 0x80) ? pb->ipacket[1] - 256 : pb->ipacket[1];
2430123442Salfred	    y = (pb->ipacket[2] & 0x80) ? pb->ipacket[2] - 256 : pb->ipacket[2];
243158230Syokota	    switch (c & MOUSE_4D_WHEELBITS) {
243258230Syokota	    case 0x10:
243358230Syokota		z = 1;
243458230Syokota		break;
243558230Syokota	    case 0x30:
243658230Syokota		z = -1;
243758230Syokota		break;
243858230Syokota	    case 0x40:	/* 2nd wheel turning right XXX */
243958230Syokota		z = 2;
244058230Syokota		break;
244158230Syokota	    case 0xc0:	/* 2nd wheel turning left XXX */
244258230Syokota		z = -2;
244358230Syokota		break;
244458230Syokota	    }
244558230Syokota	    break;
244658230Syokota
244758230Syokota	case MOUSE_MODEL_4DPLUS:
244858230Syokota	    if ((x < 16 - 256) && (y < 16 - 256)) {
244958230Syokota		/*
245058230Syokota		 *          b7 b6 b5 b4 b3 b2 b1 b0
245158230Syokota		 * byte 1:  0  0  1  1  1  M  R  L
245258230Syokota		 * byte 2:  0  0  0  0  1  0  0  0
245358230Syokota		 * byte 3:  0  0  0  0  S  s  d1 d0
245458230Syokota		 *
245558230Syokota		 * L, M, R, S: left, middle, right and side buttons
245658230Syokota		 * s: wheel data sign bit
245758230Syokota		 * d1-d0: wheel data
245858230Syokota		 */
245958230Syokota		x = y = 0;
2460123442Salfred		if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
246158230Syokota		    ms.button |= MOUSE_BUTTON4DOWN;
2462123442Salfred		z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2463123442Salfred			? ((pb->ipacket[2] & 0x07) - 8)
2464123442Salfred			: (pb->ipacket[2] & 0x07) ;
246558230Syokota	    } else {
246658230Syokota		/* preserve previous button states */
246758230Syokota		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
246858230Syokota	    }
246958230Syokota	    break;
247058230Syokota
2471132865Snjl	case MOUSE_MODEL_SYNAPTICS:
2472132865Snjl	    /* TouchPad PS/2 absolute mode message format
2473132865Snjl	     *
2474132865Snjl	     *  Bits:        7   6   5   4   3   2   1   0 (LSB)
2475132865Snjl	     *  ------------------------------------------------
2476132865Snjl	     *  ipacket[0]:  1   0  W3  W2   0  W1   R   L
2477132865Snjl	     *  ipacket[1]: Yb  Ya  Y9  Y8  Xb  Xa  X9  X8
2478132865Snjl	     *  ipacket[2]: Z7  Z6  Z5  Z4  Z3  Z2  Z1  Z0
2479132865Snjl	     *  ipacket[3]:  1   1  Yc  Xc   0  W0   D   U
2480132865Snjl	     *  ipacket[4]: X7  X6  X5  X4  X3  X2  X1  X0
2481132865Snjl	     *  ipacket[5]: Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
2482132865Snjl	     *
2483132865Snjl	     * Legend:
2484132865Snjl	     *  L: left physical mouse button
2485132865Snjl	     *  R: right physical mouse button
2486132865Snjl	     *  D: down button
2487132865Snjl	     *  U: up button
2488132865Snjl	     *  W: "wrist" value
2489132865Snjl	     *  X: x position
2490132865Snjl	     *  Y: x position
2491132865Snjl	     *  Z: pressure
2492132865Snjl	     *
2493132865Snjl	     * Absolute reportable limits:    0 - 6143.
2494132865Snjl	     * Typical bezel limits:       1472 - 5472.
2495132865Snjl	     * Typical edge marings:       1632 - 5312.
2496132865Snjl	     *
2497132865Snjl	     * w = 3 Passthrough Packet
2498132865Snjl	     *
2499132865Snjl	     * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2500132865Snjl	     */
2501132865Snjl
2502132865Snjl	    /* Sanity check for out of sync packets. */
2503132865Snjl	    if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2504132865Snjl		(pb->ipacket[3] & 0xc8) != 0xc0)
2505132865Snjl		continue;
2506132865Snjl
2507132865Snjl	    x = y = x0 = y0 = 0;
2508132865Snjl
2509132865Snjl	    /* Pressure value. */
2510132865Snjl	    z = pb->ipacket[2];
2511132865Snjl	    w = ((pb->ipacket[0] & 0x30) >> 2) |
2512132865Snjl		((pb->ipacket[0] & 0x04) >> 1) |
2513132865Snjl		((pb->ipacket[3] & 0x04) >> 2);
2514132865Snjl
2515133296Sphilip	    /* Button presses */
2516132865Snjl	    ms.button = 0;
2517132865Snjl	    if (pb->ipacket[0] & 0x01)
2518132865Snjl		ms.button |= MOUSE_BUTTON1DOWN;
2519132865Snjl	    if (pb->ipacket[0] & 0x02)
2520132865Snjl		ms.button |= MOUSE_BUTTON3DOWN;
2521132865Snjl
2522133296Sphilip	    if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2523133296Sphilip		if ((pb->ipacket[3] & 0x01) && (pb->ipacket[0] & 0x01) == 0)
2524133296Sphilip		    ms.button |= MOUSE_BUTTON4DOWN;
2525133296Sphilip		if ((pb->ipacket[3] & 0x02) && (pb->ipacket[0] & 0x02) == 0)
2526133296Sphilip		    ms.button |= MOUSE_BUTTON5DOWN;
2527133296Sphilip	    }
2528132865Snjl
2529132865Snjl	    /* There is a finger on the pad. */
2530132865Snjl	    if ((w >= 4 && w <= 7) && (z >= 16 && z < 200)) {
2531132865Snjl		x0 = ((pb->ipacket[3] & 0x10) << 8) |
2532132865Snjl		    ((pb->ipacket[1] & 0x0f) << 8) |
2533132865Snjl		    pb->ipacket[4];
2534132865Snjl		y0 = ((pb->ipacket[3] & 0x20) << 7) |
2535132865Snjl		    ((pb->ipacket[1] & 0xf0) << 4) |
2536132865Snjl		    pb->ipacket[5];
2537132865Snjl
2538132865Snjl		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2539132865Snjl		    x0 = (x0 + sc->xold * 3) / 4;
2540132865Snjl		    y0 = (y0 + sc->yold * 3) / 4;
2541132865Snjl
2542133298Sphilip		    x = (x0 - sc->xold) * 10 / 85;
2543133298Sphilip		    y = (y0 - sc->yold) * 10 / 85;
2544132865Snjl		} else {
2545132865Snjl		    sc->flags |= PSM_FLAGS_FINGERDOWN;
2546132865Snjl		}
2547132865Snjl
2548132865Snjl		sc->xold = x0;
2549132865Snjl		sc->yold = y0;
2550133297Sphilip		sc->zmax = imax(z, sc->zmax);
2551132865Snjl	    } else {
2552132865Snjl		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2553133297Sphilip
2554133297Sphilip		if (sc->zmax > PSM_TAP_THRESHOLD &&
2555133297Sphilip		    timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) {
2556133297Sphilip			if (w == 0)
2557133297Sphilip			    ms.button |= MOUSE_BUTTON3DOWN;
2558133297Sphilip			else if (w == 1)
2559133297Sphilip			    ms.button |= MOUSE_BUTTON2DOWN;
2560133297Sphilip			else
2561133297Sphilip			    ms.button |= MOUSE_BUTTON1DOWN;
2562133297Sphilip		}
2563133297Sphilip
2564133297Sphilip		sc->zmax = 0;
2565133297Sphilip		sc->taptimeout.tv_sec = PSM_TAP_TIMEOUT / 1000000;
2566133297Sphilip		sc->taptimeout.tv_usec = PSM_TAP_TIMEOUT % 1000000;
2567133297Sphilip		timevaladd(&sc->taptimeout, &sc->lastsoftintr);
2568132865Snjl	    }
2569133296Sphilip
2570133296Sphilip	    /* Use the extra buttons as a scrollwheel */
2571133296Sphilip	    if (ms.button & MOUSE_BUTTON4DOWN)
2572133296Sphilip		z = -1;
2573133296Sphilip	    else if (ms.button & MOUSE_BUTTON5DOWN)
2574133296Sphilip		z = 1;
2575133296Sphilip	    else
2576133296Sphilip		z = 0;
2577133296Sphilip
2578132865Snjl	    break;
2579132865Snjl
258041016Sdfr	case MOUSE_MODEL_GENERIC:
258141016Sdfr	default:
258241016Sdfr	    break;
258341016Sdfr	}
258441016Sdfr
258541016Sdfr        /* scale values */
258641016Sdfr        if (sc->mode.accelfactor >= 1) {
258741016Sdfr            if (x != 0) {
258841016Sdfr                x = x * x / sc->mode.accelfactor;
258941016Sdfr                if (x == 0)
259041016Sdfr                    x = 1;
259141016Sdfr                if (c & MOUSE_PS2_XNEG)
259241016Sdfr                    x = -x;
259341016Sdfr            }
259441016Sdfr            if (y != 0) {
259541016Sdfr                y = y * y / sc->mode.accelfactor;
259641016Sdfr                if (y == 0)
259741016Sdfr                    y = 1;
259841016Sdfr                if (c & MOUSE_PS2_YNEG)
259941016Sdfr                    y = -y;
260041016Sdfr            }
260141016Sdfr        }
260241016Sdfr
260341016Sdfr        ms.dx = x;
260441016Sdfr        ms.dy = y;
260541016Sdfr        ms.dz = z;
260641016Sdfr        ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
260741016Sdfr	    | (ms.obutton ^ ms.button);
260841016Sdfr
260941016Sdfr	if (sc->mode.level < PSM_LEVEL_NATIVE)
2610123442Salfred	    pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
261141016Sdfr
261241016Sdfr        sc->status.flags |= ms.flags;
261341016Sdfr        sc->status.dx += ms.dx;
261441016Sdfr        sc->status.dy += ms.dy;
261541016Sdfr        sc->status.dz += ms.dz;
261641016Sdfr        sc->status.button = ms.button;
261741016Sdfr        sc->button = ms.button;
261841016Sdfr
261958230Syokota	sc->watchdog = FALSE;
262058230Syokota
262141016Sdfr        /* queue data */
2622123442Salfred        if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
2623123442Salfred	    l = imin(pb->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2624123442Salfred	    bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2625123442Salfred	    if (pb->inputbytes > l)
2626123442Salfred	        bcopy(&pb->ipacket[l], &sc->queue.buf[0], pb->inputbytes - l);
262741016Sdfr            sc->queue.tail =
2628123442Salfred		(sc->queue.tail + pb->inputbytes) % sizeof(sc->queue.buf);
2629123442Salfred            sc->queue.count += pb->inputbytes;
263041016Sdfr	}
2631123442Salfred        pb->inputbytes = 0;
263241016Sdfr
2633123442Salfred	if (++sc->pqueue_start >= PSM_PACKETQUEUE)
2634123442Salfred		sc->pqueue_start = 0;
2635123442Salfred    } while (sc->pqueue_start != sc->pqueue_end);
2636123442Salfred    if (sc->state & PSM_ASLP) {
2637123442Salfred        sc->state &= ~PSM_ASLP;
2638123442Salfred        wakeup( sc);
263941016Sdfr    }
2640123442Salfred    selwakeuppri(&sc->rsel, PZERO);
2641123442Salfred    sc->state &= ~PSM_SOFTARMED;
2642123442Salfred    splx(s);
264341016Sdfr}
264441016Sdfr
264541016Sdfrstatic int
2646130585Sphkpsmpoll(struct cdev *dev, int events, struct thread *td)
264741016Sdfr{
264841016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
264941016Sdfr    int s;
265041016Sdfr    int revents = 0;
265141016Sdfr
265241016Sdfr    /* Return true if a mouse event available */
265341016Sdfr    s = spltty();
265445789Speter    if (events & (POLLIN | POLLRDNORM)) {
265541016Sdfr	if (sc->queue.count > 0)
265641016Sdfr	    revents |= events & (POLLIN | POLLRDNORM);
265741016Sdfr	else
265883366Sjulian	    selrecord(td, &sc->rsel);
265945789Speter    }
266041016Sdfr    splx(s);
266141016Sdfr
266241016Sdfr    return (revents);
266341016Sdfr}
266441016Sdfr
266541016Sdfr/* vendor/model specific routines */
266641016Sdfr
266741016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
266841016Sdfr{
266941016Sdfr    if (set_mouse_resolution(kbdc, res) != res)
267041016Sdfr        return FALSE;
267141016Sdfr    if (set_mouse_scaling(kbdc, scale)
267241016Sdfr	&& set_mouse_scaling(kbdc, scale)
267341016Sdfr	&& set_mouse_scaling(kbdc, scale)
267441016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3))
267541016Sdfr	return TRUE;
267641016Sdfr    return FALSE;
267741016Sdfr}
267841016Sdfr
267969438Syokotastatic int
268069438Syokotamouse_ext_command(KBDC kbdc, int command)
268169438Syokota{
268269438Syokota    int c;
268369438Syokota
268469438Syokota    c = (command >> 6) & 0x03;
268569438Syokota    if (set_mouse_resolution(kbdc, c) != c)
268669438Syokota	return FALSE;
268769438Syokota    c = (command >> 4) & 0x03;
268869438Syokota    if (set_mouse_resolution(kbdc, c) != c)
268969438Syokota	return FALSE;
269069438Syokota    c = (command >> 2) & 0x03;
269169438Syokota    if (set_mouse_resolution(kbdc, c) != c)
269269438Syokota	return FALSE;
269369438Syokota    c = (command >> 0) & 0x03;
269469438Syokota    if (set_mouse_resolution(kbdc, c) != c)
269569438Syokota	return FALSE;
269669438Syokota    return TRUE;
269769438Syokota}
269869438Syokota
269941016Sdfr#if notyet
270041016Sdfr/* Logitech MouseMan Cordless II */
270141016Sdfrstatic int
270241016Sdfrenable_lcordless(struct psm_softc *sc)
270341016Sdfr{
270441016Sdfr    int status[3];
270541016Sdfr    int ch;
270641016Sdfr
270741016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
270841016Sdfr        return FALSE;
270941016Sdfr    if (status[1] == PSMD_RES_HIGH)
271041016Sdfr	return FALSE;
271141016Sdfr    ch = (status[0] & 0x07) - 1;	/* channel # */
271241016Sdfr    if ((ch <= 0) || (ch > 4))
271341016Sdfr	return FALSE;
271441016Sdfr    /*
271541016Sdfr     * status[1]: always one?
271641016Sdfr     * status[2]: battery status? (0-100)
271741016Sdfr     */
271841016Sdfr    return TRUE;
271941016Sdfr}
272041016Sdfr#endif /* notyet */
272141016Sdfr
272258230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
272341016Sdfrstatic int
272441016Sdfrenable_groller(struct psm_softc *sc)
272541016Sdfr{
272641016Sdfr    int status[3];
272741016Sdfr
272841016Sdfr    /*
272941016Sdfr     * The special sequence to enable the fourth button and the
273041016Sdfr     * roller. Immediately after this sequence check status bytes.
273141016Sdfr     * if the mouse is NetScroll, the second and the third bytes are
273241016Sdfr     * '3' and 'D'.
273341016Sdfr     */
273441016Sdfr
273541016Sdfr    /*
273641016Sdfr     * If the mouse is an ordinary PS/2 mouse, the status bytes should
273741016Sdfr     * look like the following.
273841016Sdfr     *
273941016Sdfr     * byte 1 bit 7 always 0
274041016Sdfr     *        bit 6 stream mode (0)
274141016Sdfr     *        bit 5 disabled (0)
274241016Sdfr     *        bit 4 1:1 scaling (0)
274341016Sdfr     *        bit 3 always 0
274441016Sdfr     *        bit 0-2 button status
274541016Sdfr     * byte 2 resolution (PSMD_RES_HIGH)
274641016Sdfr     * byte 3 report rate (?)
274741016Sdfr     */
274841016Sdfr
274941016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
275041016Sdfr        return FALSE;
275141016Sdfr    if ((status[1] != '3') || (status[2] != 'D'))
275241016Sdfr        return FALSE;
275358230Syokota    /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
275441016Sdfr    sc->hw.buttons = 4;
275541016Sdfr    return TRUE;
275641016Sdfr}
275741016Sdfr
275858230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
275941016Sdfrstatic int
276041016Sdfrenable_gmouse(struct psm_softc *sc)
276141016Sdfr{
276241016Sdfr    int status[3];
276341016Sdfr
276441016Sdfr    /*
276541016Sdfr     * The special sequence to enable the middle, "rubber" button.
276641016Sdfr     * Immediately after this sequence check status bytes.
276741016Sdfr     * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
276841016Sdfr     * the second and the third bytes are '3' and 'U'.
276941016Sdfr     * NOTE: NetMouse reports that it has three buttons although it has
277041016Sdfr     * two buttons and a rubber button. NetMouse Pro and MIE Mouse
277141016Sdfr     * say they have three buttons too and they do have a button on the
277241016Sdfr     * side...
277341016Sdfr     */
277441016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
277541016Sdfr        return FALSE;
277641016Sdfr    if ((status[1] != '3') || (status[2] != 'U'))
277741016Sdfr        return FALSE;
277841016Sdfr    return TRUE;
277941016Sdfr}
278041016Sdfr
278141016Sdfr/* ALPS GlidePoint */
278241016Sdfrstatic int
278341016Sdfrenable_aglide(struct psm_softc *sc)
278441016Sdfr{
278541016Sdfr    int status[3];
278641016Sdfr
278741016Sdfr    /*
278841016Sdfr     * The special sequence to obtain ALPS GlidePoint specific
278941016Sdfr     * information. Immediately after this sequence, status bytes will
279041016Sdfr     * contain something interesting.
279141016Sdfr     * NOTE: ALPS produces several models of GlidePoint. Some of those
279241016Sdfr     * do not respond to this sequence, thus, cannot be detected this way.
279341016Sdfr     */
279450149Syokota    if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
279550149Syokota	return FALSE;
279641016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
279741016Sdfr        return FALSE;
279850149Syokota    if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
279941016Sdfr        return FALSE;
280041016Sdfr    return TRUE;
280141016Sdfr}
280241016Sdfr
280341016Sdfr/* Kensington ThinkingMouse/Trackball */
280441016Sdfrstatic int
280541016Sdfrenable_kmouse(struct psm_softc *sc)
280641016Sdfr{
280741016Sdfr    static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
280841016Sdfr    KBDC kbdc = sc->kbdc;
280941016Sdfr    int status[3];
281041016Sdfr    int id1;
281141016Sdfr    int id2;
281241016Sdfr    int i;
281341016Sdfr
281441016Sdfr    id1 = get_aux_id(kbdc);
281541016Sdfr    if (set_mouse_sampling_rate(kbdc, 10) != 10)
281641016Sdfr	return FALSE;
281741016Sdfr    /*
281841016Sdfr     * The device is now in the native mode? It returns a different
281941016Sdfr     * ID value...
282041016Sdfr     */
282141016Sdfr    id2 = get_aux_id(kbdc);
282241016Sdfr    if ((id1 == id2) || (id2 != 2))
282341016Sdfr	return FALSE;
282441016Sdfr
282541016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
282641016Sdfr        return FALSE;
282741016Sdfr#if PSM_DEBUG >= 2
282841016Sdfr    /* at this point, resolution is LOW, sampling rate is 10/sec */
282941016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
283041016Sdfr        return FALSE;
283141016Sdfr#endif
283241016Sdfr
283341016Sdfr    /*
283441016Sdfr     * The special sequence to enable the third and fourth buttons.
283541016Sdfr     * Otherwise they behave like the first and second buttons.
283641016Sdfr     */
283741016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
283841016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
283941016Sdfr	    return FALSE;
284041016Sdfr    }
284141016Sdfr
284241016Sdfr    /*
284341016Sdfr     * At this point, the device is using default resolution and
284441016Sdfr     * sampling rate for the native mode.
284541016Sdfr     */
284641016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
284741016Sdfr        return FALSE;
284841016Sdfr    if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
284941016Sdfr        return FALSE;
285041016Sdfr
285141016Sdfr    /* the device appears be enabled by this sequence, diable it for now */
285241016Sdfr    disable_aux_dev(kbdc);
285341016Sdfr    empty_aux_buffer(kbdc, 5);
285441016Sdfr
285541016Sdfr    return TRUE;
285641016Sdfr}
285741016Sdfr
285858230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
285941016Sdfrstatic int
286041016Sdfrenable_mmanplus(struct psm_softc *sc)
286141016Sdfr{
286241016Sdfr    KBDC kbdc = sc->kbdc;
286341016Sdfr    int data[3];
286441016Sdfr
286541016Sdfr    /* the special sequence to enable the fourth button and the roller. */
286658230Syokota    /*
286758230Syokota     * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
286858230Syokota     * must be called exactly three times since the last RESET command
286958230Syokota     * before this sequence. XXX
287058230Syokota     */
287169438Syokota    if (!set_mouse_scaling(kbdc, 1))
287269438Syokota	return FALSE;
287369438Syokota    if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
287469438Syokota	return FALSE;
287541016Sdfr    if (get_mouse_status(kbdc, data, 1, 3) < 3)
287641016Sdfr        return FALSE;
287741016Sdfr
287848778Syokota    /*
287948778Syokota     * PS2++ protocl, packet type 0
288041016Sdfr     *
288148778Syokota     *          b7 b6 b5 b4 b3 b2 b1 b0
288248778Syokota     * byte 1:  *  1  p3 p2 1  *  *  *
288348778Syokota     * byte 2:  1  1  p1 p0 m1 m0 1  0
288448778Syokota     * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
288548778Syokota     *
288648778Syokota     * p3-p0: packet type: 0
288758230Syokota     * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
288841016Sdfr     */
288948778Syokota    /* check constant bits */
289048778Syokota    if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
289141016Sdfr        return FALSE;
289248778Syokota    if ((data[1] & 0xc3) != 0xc2)
289348778Syokota        return FALSE;
289448778Syokota    /* check d3-d0 in byte 2 */
289548778Syokota    if (!MOUSE_PS2PLUS_CHECKBITS(data))
289648778Syokota        return FALSE;
289748778Syokota    /* check p3-p0 */
289848778Syokota    if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
289948778Syokota        return FALSE;
290041016Sdfr
290148778Syokota    sc->hw.hwid &= 0x00ff;
290248778Syokota    sc->hw.hwid |= data[2] << 8;	/* save model ID */
290348778Syokota
290441016Sdfr    /*
290541016Sdfr     * MouseMan+ (or FirstMouse+) is now in its native mode, in which
290641016Sdfr     * the wheel and the fourth button events are encoded in the
290741016Sdfr     * special data packet. The mouse may be put in the IntelliMouse mode
290841016Sdfr     * if it is initialized by the IntelliMouse's method.
290941016Sdfr     */
291041016Sdfr    return TRUE;
291141016Sdfr}
291241016Sdfr
291358230Syokota/* MS IntelliMouse Explorer */
291458230Syokotastatic int
291558230Syokotaenable_msexplorer(struct psm_softc *sc)
291658230Syokota{
291758923Syokota    static unsigned char rate0[] = { 200, 100, 80, };
291858923Syokota    static unsigned char rate1[] = { 200, 200, 80, };
291958230Syokota    KBDC kbdc = sc->kbdc;
292058230Syokota    int id;
292158230Syokota    int i;
292258230Syokota
292369438Syokota    /* the special sequence to enable the extra buttons and the roller. */
292469438Syokota    for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
292569438Syokota        if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
292669438Syokota	    return FALSE;
292769438Syokota    }
292869438Syokota    /* the device will give the genuine ID only after the above sequence */
292969438Syokota    id = get_aux_id(kbdc);
293069438Syokota    if (id != PSM_EXPLORER_ID)
293169438Syokota	return FALSE;
293269438Syokota
293369438Syokota    sc->hw.hwid = id;
293469438Syokota    sc->hw.buttons = 5;		/* IntelliMouse Explorer XXX */
293569438Syokota
293658923Syokota    /*
293758923Syokota     * XXX: this is a kludge to fool some KVM switch products
293858923Syokota     * which think they are clever enough to know the 4-byte IntelliMouse
293958923Syokota     * protocol, and assume any other protocols use 3-byte packets.
294058923Syokota     * They don't convey 4-byte data packets from the IntelliMouse Explorer
294158923Syokota     * correctly to the host computer because of this!
294258923Syokota     * The following sequence is actually IntelliMouse's "wake up"
294358923Syokota     * sequence; it will make the KVM think the mouse is IntelliMouse
294458923Syokota     * when it is in fact IntelliMouse Explorer.
294558923Syokota     */
294658923Syokota    for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
294758923Syokota        if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
294869438Syokota	    break;
294958923Syokota    }
295058923Syokota    id = get_aux_id(kbdc);
295158923Syokota
295258230Syokota    return TRUE;
295358230Syokota}
295458230Syokota
295541016Sdfr/* MS IntelliMouse */
295641016Sdfrstatic int
295741016Sdfrenable_msintelli(struct psm_softc *sc)
295841016Sdfr{
295941016Sdfr    /*
296041016Sdfr     * Logitech MouseMan+ and FirstMouse+ will also respond to this
296141016Sdfr     * probe routine and act like IntelliMouse.
296241016Sdfr     */
296341016Sdfr
296441016Sdfr    static unsigned char rate[] = { 200, 100, 80, };
296541016Sdfr    KBDC kbdc = sc->kbdc;
296641016Sdfr    int id;
296741016Sdfr    int i;
296841016Sdfr
296941016Sdfr    /* the special sequence to enable the third button and the roller. */
297041016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
297141016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
297241016Sdfr	    return FALSE;
297341016Sdfr    }
297441016Sdfr    /* the device will give the genuine ID only after the above sequence */
297541016Sdfr    id = get_aux_id(kbdc);
297641016Sdfr    if (id != PSM_INTELLI_ID)
297741016Sdfr	return FALSE;
297841016Sdfr
297941016Sdfr    sc->hw.hwid = id;
298041016Sdfr    sc->hw.buttons = 3;
298141016Sdfr
298241016Sdfr    return TRUE;
298341016Sdfr}
298441016Sdfr
298558230Syokota/* A4 Tech 4D Mouse */
298658230Syokotastatic int
298758230Syokotaenable_4dmouse(struct psm_softc *sc)
298858230Syokota{
298958230Syokota    /*
299058230Syokota     * Newer wheel mice from A4 Tech may use the 4D+ protocol.
299158230Syokota     */
299258230Syokota
299358230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
299458230Syokota    KBDC kbdc = sc->kbdc;
299558230Syokota    int id;
299658230Syokota    int i;
299758230Syokota
299858230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
299958230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
300058230Syokota	    return FALSE;
300158230Syokota    }
300258230Syokota    id = get_aux_id(kbdc);
300358230Syokota    /*
300458923Syokota     * WinEasy 4D, 4 Way Scroll 4D: 6
300558230Syokota     * Cable-Free 4D: 8 (4DPLUS)
300658923Syokota     * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
300758230Syokota     */
300858230Syokota    if (id != PSM_4DMOUSE_ID)
300958230Syokota	return FALSE;
301058230Syokota
301158230Syokota    sc->hw.hwid = id;
301258230Syokota    sc->hw.buttons = 3;		/* XXX some 4D mice have 4? */
301358230Syokota
301458230Syokota    return TRUE;
301558230Syokota}
301658230Syokota
301758230Syokota/* A4 Tech 4D+ Mouse */
301858230Syokotastatic int
301958230Syokotaenable_4dplus(struct psm_softc *sc)
302058230Syokota{
302158230Syokota    /*
302258230Syokota     * Newer wheel mice from A4 Tech seem to use this protocol.
302358230Syokota     * Older models are recognized as either 4D Mouse or IntelliMouse.
302458230Syokota     */
302558230Syokota    KBDC kbdc = sc->kbdc;
302658230Syokota    int id;
302758230Syokota
302858230Syokota    /*
302958230Syokota     * enable_4dmouse() already issued the following ID sequence...
303058230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
303158230Syokota    int i;
303258230Syokota
303358230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
303458230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
303558230Syokota	    return FALSE;
303658230Syokota    }
303758230Syokota    */
303858230Syokota
303958230Syokota    id = get_aux_id(kbdc);
3040117478Smikeh    switch (id) {
3041117478Smikeh    case PSM_4DPLUS_ID:
3042117478Smikeh	    sc->hw.buttons = 4;
3043117478Smikeh	    break;
3044117478Smikeh    case PSM_4DPLUS_RFSW35_ID:
3045117478Smikeh	    sc->hw.buttons = 3;
3046117478Smikeh	    break;
3047117478Smikeh    default:
3048117478Smikeh	    return FALSE;
3049117478Smikeh    }
305058230Syokota
305158230Syokota    sc->hw.hwid = id;
305258230Syokota
305358230Syokota    return TRUE;
305458230Syokota}
305558230Syokota
3056132865Snjl/* Synaptics Touchpad */
3057132865Snjlstatic int
3058132865Snjlenable_synaptics(struct psm_softc *sc)
3059132865Snjl{
3060132865Snjl    int status[3];
3061132865Snjl    KBDC kbdc;
3062132865Snjl
3063132865Snjl    kbdc = sc->kbdc;
3064132865Snjl    disable_aux_dev(kbdc);
3065132865Snjl
3066132865Snjl    /* Just to be on the safe side */
3067132865Snjl    set_mouse_scaling(kbdc, 1);
3068132865Snjl
3069133295Sphilip    /* Identify the Touchpad version */
3070132865Snjl    if (mouse_ext_command(kbdc, 0) == 0)
3071132865Snjl	return (FALSE);
3072132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3073132865Snjl	return (FALSE);
3074132865Snjl    if (status[1] != 0x47)
3075132865Snjl	return (FALSE);
3076132865Snjl
3077132865Snjl    sc->synhw.infoMinor = status[0];
3078132865Snjl    sc->synhw.infoMajor = status[2] & 0x0f;
3079133295Sphilip
3080133295Sphilip    if (verbose >= 2)
3081133295Sphilip	printf("Synaptics Touchpad v%d.%d\n",
3082133295Sphilip	    sc->synhw.infoMajor, sc->synhw.infoMinor);
3083133295Sphilip
3084132865Snjl    if (sc->synhw.infoMajor < 4) {
3085133295Sphilip	printf("  Unsupported (pre-v4) Touchpad detected\n");
3086132865Snjl	return (FALSE);
3087132865Snjl    }
3088132865Snjl
3089133295Sphilip    /* Get the Touchpad model information */
3090132865Snjl    if (mouse_ext_command(kbdc, 3) == 0)
3091132865Snjl	return (FALSE);
3092132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3093132865Snjl	return (FALSE);
3094132865Snjl    if ((status[1] & 0x01) != 0) {
3095133295Sphilip	printf("  Failed to read model information\n");
3096132865Snjl	return (FALSE);
3097132865Snjl    }
3098132865Snjl
3099132865Snjl    sc->synhw.infoRot180   = (status[0] & 0x80) >> 7;
3100132865Snjl    sc->synhw.infoPortrait = (status[0] & 0x40) >> 6;
3101132865Snjl    sc->synhw.infoSensor   =  status[0] & 0x3f;
3102132865Snjl    sc->synhw.infoHardware = (status[1] & 0xfe) >> 1;
3103132865Snjl    sc->synhw.infoNewAbs   = (status[2] & 0x80) >> 7;
3104132865Snjl    sc->synhw.capPen       = (status[2] & 0x40) >> 6;
3105132865Snjl    sc->synhw.infoSimplC   = (status[2] & 0x20) >> 5;
3106132865Snjl    sc->synhw.infoGeometry =  status[2] & 0x0f;
3107133295Sphilip
3108132865Snjl    if (verbose >= 2) {
3109133295Sphilip	printf("  Model information:\n");
3110133295Sphilip	printf("   infoRot180: %d\n", sc->synhw.infoRot180);
3111133295Sphilip	printf("   infoPortrait: %d\n", sc->synhw.infoPortrait);
3112133295Sphilip	printf("   infoSensor: %d\n", sc->synhw.infoSensor);
3113133295Sphilip	printf("   infoHardware: %d\n", sc->synhw.infoHardware);
3114133295Sphilip	printf("   infoNewAbs: %d\n", sc->synhw.infoNewAbs);
3115133295Sphilip	printf("   capPen: %d\n", sc->synhw.capPen);
3116133295Sphilip	printf("   infoSimplC: %d\n", sc->synhw.infoSimplC);
3117133295Sphilip	printf("   infoGeometry: %d\n", sc->synhw.infoGeometry);
3118132865Snjl    }
3119132865Snjl
3120133295Sphilip    /* Read the extended capability bits */
3121132865Snjl    if (mouse_ext_command(kbdc, 2) == 0)
3122132865Snjl	return (FALSE);
3123132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3124132865Snjl	return (FALSE);
3125132865Snjl    if (status[1] != 0x47) {
3126133295Sphilip	printf("  Failed to read extended capability bits\n");
3127132865Snjl	return (FALSE);
3128132865Snjl    }
3129132865Snjl
3130133295Sphilip    /* Set the different capabilities when they exist */
3131133295Sphilip    if ((status[0] & 0x80) >> 7) {
3132133295Sphilip	sc->synhw.capExtended    = (status[0] & 0x80) >> 7;
3133133295Sphilip    	sc->synhw.capPassthrough = (status[2] & 0x80) >> 7;
3134133295Sphilip    	sc->synhw.capSleep       = (status[2] & 0x10) >> 4;
3135133295Sphilip    	sc->synhw.capFourButtons = (status[2] & 0x08) >> 3;
3136133295Sphilip    	sc->synhw.capMultiFinger = (status[2] & 0x02) >> 1;
3137133295Sphilip    	sc->synhw.capPalmDetect  = (status[2] & 0x01);
3138133295Sphilip
3139133295Sphilip	if (verbose >= 2) {
3140133295Sphilip	    printf("  Extended capabilities:\n");
3141133295Sphilip	    printf("   capExtended: %d\n", sc->synhw.capExtended);
3142133295Sphilip	    printf("   capPassthrough: %d\n", sc->synhw.capPassthrough);
3143133295Sphilip	    printf("   capSleep: %d\n", sc->synhw.capSleep);
3144133295Sphilip	    printf("   capFourButtons: %d\n", sc->synhw.capFourButtons);
3145133295Sphilip	    printf("   capMultiFinger: %d\n", sc->synhw.capMultiFinger);
3146133295Sphilip	    printf("   capPalmDetect: %d\n", sc->synhw.capPalmDetect);
3147133295Sphilip	}
3148133295Sphilip    } else {
3149133295Sphilip	sc->synhw.capExtended = 0;
3150133295Sphilip
3151133295Sphilip	if (verbose >= 2)
3152133295Sphilip	    printf("  No extended capabilities\n");
3153133295Sphilip    }
3154133295Sphilip
3155133295Sphilip    /*
3156133295Sphilip     * Read the mode byte
3157133295Sphilip     *
3158133295Sphilip     * XXX: Note the Synaptics documentation also defines the first
3159133295Sphilip     * byte of the response to this query to be a constant 0x3b, this
3160133295Sphilip     * does not appear to be true for Touchpads with guest devices.
3161133295Sphilip     */
3162132865Snjl    if (mouse_ext_command(kbdc, 1) == 0)
3163132865Snjl	return (FALSE);
3164132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3165132865Snjl	return (FALSE);
3166133295Sphilip    if (status[1] != 0x47) {
3167133295Sphilip	printf("  Failed to read mode byte\n");
3168132865Snjl	return (FALSE);
3169132865Snjl    }
3170132865Snjl
3171133295Sphilip    /* Set the mode byte -- request wmode where available */
3172133295Sphilip    if (sc->synhw.capExtended)
3173133295Sphilip	mouse_ext_command(kbdc, 0xc1);
3174133295Sphilip    else
3175133295Sphilip	mouse_ext_command(kbdc, 0xc0);
3176132865Snjl
3177133295Sphilip    /* Reset the sampling rate */
3178132865Snjl    set_mouse_sampling_rate(kbdc, 20);
3179132865Snjl
3180133296Sphilip    /*
3181133296Sphilip     * Report the correct number of buttons
3182133296Sphilip     *
3183133296Sphilip     * XXX: I'm not sure this is used anywhere.
3184133296Sphilip     */
3185133296Sphilip    if (sc->synhw.capExtended && sc->synhw.capFourButtons)
3186133296Sphilip	sc->hw.buttons = 4;
3187133296Sphilip    else
3188133296Sphilip	sc->hw.buttons = 3;
3189133296Sphilip
3190132865Snjl    return (TRUE);
3191132865Snjl}
3192133295Sphilip
319349965Syokota/* Interlink electronics VersaPad */
319449965Syokotastatic int
319549965Syokotaenable_versapad(struct psm_softc *sc)
319649965Syokota{
319749965Syokota    KBDC kbdc = sc->kbdc;
319849965Syokota    int data[3];
319949965Syokota
320049965Syokota    set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
320149965Syokota    set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
320249965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
320349965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
320449965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
320549965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
320649965Syokota    if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
320749965Syokota	return FALSE;
320849965Syokota    if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
320949965Syokota	return FALSE;
321049965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
321149965Syokota
321258230Syokota    sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
321358230Syokota
321449965Syokota    return TRUE;				/* PS/2 absolute mode */
321549965Syokota}
321649965Syokota
321741016Sdfrstatic int
321854629Syokotapsmresume(device_t dev)
321941016Sdfr{
322054629Syokota    struct psm_softc *sc = device_get_softc(dev);
322154629Syokota    int unit = device_get_unit(dev);
322284880Syokota    int err;
322341016Sdfr
322441016Sdfr    if (verbose >= 2)
322554629Syokota        log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
322641016Sdfr
322758230Syokota    if (!(sc->config & PSM_CONFIG_HOOKRESUME))
322858230Syokota	return (0);
322958230Syokota
323084880Syokota    err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
323141016Sdfr
323241016Sdfr    if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
323341016Sdfr	/*
323441016Sdfr	 * Release the blocked process; it must be notified that the device
323541016Sdfr	 * cannot be accessed anymore.
323641016Sdfr	 */
323741016Sdfr        sc->state &= ~PSM_ASLP;
3238111748Sdes        wakeup(sc);
323941016Sdfr    }
324041016Sdfr
324141016Sdfr    if (verbose >= 2)
324254629Syokota        log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
324341016Sdfr
324441016Sdfr    return (err);
324541016Sdfr}
324641016Sdfr
324752997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
324883147Syokota
324983147Syokota/*
325083147Syokota * This sucks up assignments from PNPBIOS and ACPI.
325183147Syokota */
325283147Syokota
325383931Syokota/*
325483931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
325583931Syokota * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
325683931Syokota * can be probed and attached only after the AT keyboard controller is
325783931Syokota * attached, we shall quietly reserve the IRQ resource for later use.
325883931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
325983931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging
326083931Syokota * under the keyboard controller, then probe and attach it.
326183931Syokota */
326283147Syokota
326383147Syokotastatic	devclass_t			psmcpnp_devclass;
326483147Syokota
326583147Syokotastatic	device_probe_t			psmcpnp_probe;
326683147Syokotastatic	device_attach_t			psmcpnp_attach;
326783147Syokota
326883147Syokotastatic device_method_t psmcpnp_methods[] = {
326983147Syokota	DEVMETHOD(device_probe,		psmcpnp_probe),
327083147Syokota	DEVMETHOD(device_attach,	psmcpnp_attach),
327183147Syokota
327283147Syokota	{ 0, 0 }
327383147Syokota};
327483147Syokota
327583147Syokotastatic driver_t psmcpnp_driver = {
327683147Syokota	PSMCPNP_DRIVER_NAME,
327783147Syokota	psmcpnp_methods,
327883147Syokota	1,			/* no softc */
327983147Syokota};
328083147Syokota
328183147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = {
328288188Ssheldonh	{ 0x030fd041, "PS/2 mouse port" },		/* PNP0F03 */
328383147Syokota	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
328483147Syokota	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
3285117117Smikeh	{ 0x02002e4f, "Dell PS/2 mouse port" },		/* Lat. X200, Dell */
328683492Syokota	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
328784407Stakawata	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
3288109679Shsu	{ 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
3289109679Shsu	{ 0x0290d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9002, Vaio */
3290109710Smarcel	{ 0x0390d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9003, Vaio */
3291109679Shsu	{ 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
329283147Syokota	{ 0 }
329383147Syokota};
329483147Syokota
329583147Syokotastatic int
329683147Syokotacreate_a_copy(device_t atkbdc, device_t me)
329783147Syokota{
329883147Syokota	device_t psm;
329983147Syokota	u_long irq;
330083147Syokota
330183931Syokota	/* find the PS/2 mouse device instance under the keyboard controller */
330283931Syokota	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
330383931Syokota				device_get_unit(atkbdc));
330483147Syokota	if (psm == NULL)
330583147Syokota		return ENXIO;
330683931Syokota	if (device_get_state(psm) != DS_NOTPRESENT)
330783931Syokota		return 0;
330883147Syokota
330983931Syokota	/* move our resource to the found device */
331083931Syokota	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
331183147Syokota	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
331283147Syokota
331383147Syokota	/* ...then probe and attach it */
331483147Syokota	return device_probe_and_attach(psm);
331583147Syokota}
331683147Syokota
331783147Syokotastatic int
331883147Syokotapsmcpnp_probe(device_t dev)
331983147Syokota{
332083931Syokota	struct resource *res;
332183931Syokota	u_long irq;
332283931Syokota	int rid;
332383147Syokota
332483147Syokota	if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
332583147Syokota		return ENXIO;
332683147Syokota
332783931Syokota	/*
332883931Syokota	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
332983931Syokota	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
333083931Syokota	 * declares the PS/2 mouse device node without an IRQ resource!
333183931Syokota	 * If this happens, we shall refer to device hints.
333283931Syokota	 * If we still don't find it there, use a hardcoded value... XXX
333383931Syokota	 */
333483931Syokota	rid = 0;
333583931Syokota	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
333683931Syokota	if (irq <= 0) {
333783931Syokota		if (resource_long_value(PSM_DRIVER_NAME,
333883931Syokota					device_get_unit(dev), "irq", &irq) != 0)
333983931Syokota			irq = 12;	/* XXX */
334083931Syokota		device_printf(dev, "irq resource info is missing; "
334183931Syokota			      "assuming irq %ld\n", irq);
334283931Syokota		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
334383931Syokota	}
3344127135Snjl	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3345127135Snjl				     RF_SHAREABLE);
334683931Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
334783147Syokota
334883147Syokota	/* keep quiet */
334983147Syokota	if (!bootverbose)
335083147Syokota		device_quiet(dev);
335183492Syokota
335283931Syokota	return ((res == NULL) ? ENXIO : 0);
335383147Syokota}
335483147Syokota
335583147Syokotastatic int
335683147Syokotapsmcpnp_attach(device_t dev)
335783147Syokota{
335883492Syokota	device_t atkbdc;
335983931Syokota	int rid;
336083147Syokota
336183931Syokota	/* find the keyboard controller, which may be on acpi* or isa* bus */
336283931Syokota	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
336383931Syokota				     device_get_unit(dev));
336483931Syokota	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
336583492Syokota		create_a_copy(atkbdc, dev);
336683931Syokota	} else {
336783931Syokota		/*
336883931Syokota		 * If we don't have the AT keyboard controller yet,
336983931Syokota		 * just reserve the IRQ for later use...
337083931Syokota		 * (See psmidentify() above.)
337183931Syokota		 */
337283931Syokota		rid = 0;
3373127135Snjl		bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
337483931Syokota	}
337583492Syokota
337683147Syokota	return 0;
337783147Syokota}
337883147Syokota
3379123442Salfred/*
3380123442Salfred * Return true if 'now' is earlier than (start + (secs.usecs)).
3381123442Salfred * Now may be NULL and the function will fetch the current time from
3382123442Salfred * getmicrouptime(), or a cached 'now' can be passed in.
3383123442Salfred * All values should be numbers derived from getmicrouptime().
3384123442Salfred */
3385123442Salfredstatic int
3386123442Salfredtimeelapsed(start, secs, usecs, now)
3387123442Salfred	const struct timeval *start, *now;
3388123442Salfred	int secs, usecs;
3389123442Salfred{
3390123442Salfred	struct timeval snow, tv;
3391123442Salfred
3392123442Salfred	/* if there is no 'now' passed in, the get it as a convience. */
3393123442Salfred	if (now == NULL) {
3394123442Salfred		getmicrouptime(&snow);
3395123442Salfred		now = &snow;
3396123442Salfred	}
3397123442Salfred
3398123442Salfred	tv.tv_sec = secs;
3399123442Salfred	tv.tv_usec = usecs;
3400123442Salfred	timevaladd(&tv, start);
3401123442Salfred	return (timevalcmp(&tv, now, <));
3402123442Salfred}
3403123442Salfred
340483147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
340583147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
3406