psm.c revision 133295
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 133295 2004-08-08 00:52:11Z 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
10641016Sdfr/* end of driver specific options */
10741016Sdfr
10883492Syokota#define PSM_DRIVER_NAME		"psm"
10983931Syokota#define PSMCPNP_DRIVER_NAME	"psmcpnp"
11083492Syokota
11141016Sdfr/* input queue */
11241016Sdfr#define PSM_BUFSIZE		960
11341016Sdfr#define PSM_SMALLBUFSIZE	240
11441016Sdfr
11541016Sdfr/* operation levels */
11641016Sdfr#define PSM_LEVEL_BASE		0
11741016Sdfr#define PSM_LEVEL_STANDARD	1
11841016Sdfr#define PSM_LEVEL_NATIVE	2
11941016Sdfr#define PSM_LEVEL_MIN		PSM_LEVEL_BASE
12041016Sdfr#define PSM_LEVEL_MAX		PSM_LEVEL_NATIVE
12141016Sdfr
12248778Syokota/* Logitech PS2++ protocol */
12348778Syokota#define MOUSE_PS2PLUS_CHECKBITS(b)	\
12448778Syokota				((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
12548778Syokota#define MOUSE_PS2PLUS_PACKET_TYPE(b)	\
12648778Syokota				(((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
12748778Syokota
12841016Sdfr/* some macros */
12941016Sdfr#define PSM_UNIT(dev)		(minor(dev) >> 1)
13041016Sdfr#define PSM_NBLOCKIO(dev)	(minor(dev) & 1)
13141016Sdfr#define PSM_MKMINOR(unit,block)	(((unit) << 1) | ((block) ? 0:1))
13241016Sdfr
13341016Sdfr/* ring buffer */
13441016Sdfrtypedef struct ringbuf {
13541016Sdfr    int           count;	/* # of valid elements in the buffer */
13641016Sdfr    int           head;		/* head pointer */
13741016Sdfr    int           tail;		/* tail poiner */
13841016Sdfr    unsigned char buf[PSM_BUFSIZE];
13941016Sdfr} ringbuf_t;
14041016Sdfr
141123442Salfred/* data buffer */
142123442Salfredtypedef struct packetbuf {
143123442Salfred    unsigned char ipacket[16];	/* interim input buffer */
144123442Salfred    int           inputbytes;	/* # of bytes in the input buffer */
145123442Salfred} packetbuf_t;
146123442Salfred
147123442Salfred#ifndef PSM_PACKETQUEUE
148123442Salfred#define PSM_PACKETQUEUE	128
149123442Salfred#endif
150123442Salfred
15141016Sdfr/* driver control block */
15241016Sdfrstruct psm_softc {		/* Driver status information */
15384880Syokota    int		  unit;
15441016Sdfr    struct selinfo rsel;	/* Process selecting for Input */
15541016Sdfr    unsigned char state;	/* Mouse driver state */
15641016Sdfr    int           config;	/* driver configuration flags */
15741016Sdfr    int           flags;	/* other flags */
15841016Sdfr    KBDC          kbdc;		/* handle to access the keyboard controller */
15958230Syokota    struct resource *intr;	/* IRQ resource */
16058230Syokota    void	  *ih;		/* interrupt handle */
16141016Sdfr    mousehw_t     hw;		/* hardware information */
162132865Snjl    synapticshw_t synhw;	/* Synaptics-specific hardware information */
16341016Sdfr    mousemode_t   mode;		/* operation mode */
16441016Sdfr    mousemode_t   dflt_mode;	/* default operation mode */
16541016Sdfr    mousestatus_t status;	/* accumulated mouse movement */
16641016Sdfr    ringbuf_t     queue;	/* mouse status queue */
167123442Salfred    packetbuf_t   pqueue[PSM_PACKETQUEUE];	/* mouse data queue */
168123442Salfred    int           pqueue_start; /* start of data in queue */
169123442Salfred    int           pqueue_end;   /* end of data in queue */
17041016Sdfr    int           button;	/* the latest button state */
17149965Syokota    int		  xold;	/* previous absolute X position */
17249965Syokota    int		  yold;	/* previous absolute Y position */
173123442Salfred    int		  syncerrors; /* XXX: KILL ME! */
17484880Syokota    struct timeval inputtimeout;
175123442Salfred    struct timeval lastsoftintr;	/* time of last soft interrupt */
176123442Salfred    struct timeval lastinputerr;	/* time last sync error happened */
17758230Syokota    int		  watchdog;	/* watchdog timer flag */
17858230Syokota    struct callout_handle callout;	/* watchdog timer call out */
179123442Salfred    struct callout_handle softcallout;	/* buffer timer call out */
180130585Sphk    struct cdev *dev;
181130585Sphk    struct cdev *bdev;
182123442Salfred    int           lasterr;
183123442Salfred    int           cmdcount;
18441016Sdfr};
185114293Smarkmstatic devclass_t psm_devclass;
18641016Sdfr#define PSM_SOFTC(unit)	((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
18741016Sdfr
18841016Sdfr/* driver state flags (state) */
18941016Sdfr#define PSM_VALID		0x80
19041016Sdfr#define PSM_OPEN		1	/* Device is open */
19141016Sdfr#define PSM_ASLP		2	/* Waiting for mouse data */
192123442Salfred#define PSM_SOFTARMED		4	/* Software interrupt armed */
19341016Sdfr
19441016Sdfr/* driver configuration flags (config) */
19541016Sdfr#define PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
19641016Sdfr#define PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
19741016Sdfr#define PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
19845789Speter#define PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
19945789Speter#define PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
20045789Speter#define PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
20145789Speter#define PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
20258230Syokota#define PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
20358230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
20469439Syokota#define PSM_CONFIG_SYNCHACK	0x8000 /* enable `out-of-sync' hack */
20541016Sdfr
20641016Sdfr#define PSM_CONFIG_FLAGS	(PSM_CONFIG_RESOLUTION 		\
20741016Sdfr				    | PSM_CONFIG_ACCEL		\
20845789Speter				    | PSM_CONFIG_NOCHECKSYNC	\
20969439Syokota				    | PSM_CONFIG_SYNCHACK	\
21045789Speter				    | PSM_CONFIG_NOIDPROBE	\
21145789Speter				    | PSM_CONFIG_NORESET	\
21245789Speter				    | PSM_CONFIG_FORCETAP	\
21358230Syokota				    | PSM_CONFIG_IGNPORTERROR	\
21458230Syokota				    | PSM_CONFIG_HOOKRESUME	\
21558230Syokota				    | PSM_CONFIG_INITAFTERSUSPEND)
21641016Sdfr
21741016Sdfr/* other flags (flags) */
21849965Syokota#define PSM_FLAGS_FINGERDOWN	0x0001 /* VersaPad finger down */
21941016Sdfr
22041016Sdfr/* for backward compatibility */
22141016Sdfr#define OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
22241016Sdfr#define OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
22341016Sdfr#define OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
22441016Sdfr
22541016Sdfrtypedef struct old_mousehw {
22641016Sdfr    int buttons;
22741016Sdfr    int iftype;
22841016Sdfr    int type;
22941016Sdfr    int hwid;
23041016Sdfr} old_mousehw_t;
23141016Sdfr
23241016Sdfrtypedef struct old_mousemode {
23341016Sdfr    int protocol;
23441016Sdfr    int rate;
23541016Sdfr    int resolution;
23641016Sdfr    int accelfactor;
23741016Sdfr} old_mousemode_t;
23841016Sdfr
23941016Sdfr/* packet formatting function */
24092756Salfredtypedef int packetfunc_t(struct psm_softc *, unsigned char *,
24192756Salfred			      int *, int, mousestatus_t *);
24241016Sdfr
24341016Sdfr/* function prototypes */
24492756Salfredstatic void psmidentify(driver_t *, device_t);
24592756Salfredstatic int psmprobe(device_t);
24692756Salfredstatic int psmattach(device_t);
24792756Salfredstatic int psmdetach(device_t);
24892756Salfredstatic int psmresume(device_t);
24941016Sdfr
25041016Sdfrstatic d_open_t psmopen;
25141016Sdfrstatic d_close_t psmclose;
25241016Sdfrstatic d_read_t psmread;
25341016Sdfrstatic d_ioctl_t psmioctl;
25441016Sdfrstatic d_poll_t psmpoll;
25541016Sdfr
25692756Salfredstatic int enable_aux_dev(KBDC);
25792756Salfredstatic int disable_aux_dev(KBDC);
25892756Salfredstatic int get_mouse_status(KBDC, int *, int, int);
25992756Salfredstatic int get_aux_id(KBDC);
26092756Salfredstatic int set_mouse_sampling_rate(KBDC, int);
26192756Salfredstatic int set_mouse_scaling(KBDC, int);
26292756Salfredstatic int set_mouse_resolution(KBDC, int);
26392756Salfredstatic int set_mouse_mode(KBDC);
26492756Salfredstatic int get_mouse_buttons(KBDC);
26592756Salfredstatic int is_a_mouse(int);
26692756Salfredstatic void recover_from_error(KBDC);
26792756Salfredstatic int restore_controller(KBDC, int);
26892756Salfredstatic int doinitialize(struct psm_softc *, mousemode_t *);
26992756Salfredstatic int doopen(struct psm_softc *, int);
27092756Salfredstatic int reinitialize(struct psm_softc *, int);
27192756Salfredstatic char *model_name(int);
272123442Salfredstatic void psmsoftintr(void *);
27392756Salfredstatic void psmintr(void *);
27492756Salfredstatic void psmtimeout(void *);
275123442Salfredstatic int timeelapsed(const struct timeval *,
276123442Salfred    int, int, const struct timeval *);
277123442Salfredstatic void dropqueue(struct psm_softc *);
278123442Salfredstatic void flushpackets(struct psm_softc *);
27941016Sdfr
28041016Sdfr/* vendor specific features */
28192756Salfredtypedef int probefunc_t(struct psm_softc *);
28241016Sdfr
28392756Salfredstatic int mouse_id_proc1(KBDC, int, int, int *);
28492756Salfredstatic int mouse_ext_command(KBDC, int);
28541016Sdfrstatic probefunc_t enable_groller;
28641016Sdfrstatic probefunc_t enable_gmouse;
28741016Sdfrstatic probefunc_t enable_aglide;
28841016Sdfrstatic probefunc_t enable_kmouse;
28958230Syokotastatic probefunc_t enable_msexplorer;
29041016Sdfrstatic probefunc_t enable_msintelli;
29158230Syokotastatic probefunc_t enable_4dmouse;
29258230Syokotastatic probefunc_t enable_4dplus;
29341016Sdfrstatic probefunc_t enable_mmanplus;
294132865Snjlstatic probefunc_t enable_synaptics;
29549965Syokotastatic probefunc_t enable_versapad;
296123442Salfredstatic int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, unsigned char *);
29741016Sdfr
29841016Sdfrstatic struct {
29941016Sdfr    int                 model;
30041016Sdfr    unsigned char	syncmask;
30141016Sdfr    int 		packetsize;
30241016Sdfr    probefunc_t 	*probefunc;
30341016Sdfr} vendortype[] = {
30458230Syokota    /*
30558230Syokota     * WARNING: the order of probe is very important.  Don't mess it
30658230Syokota     * unless you know what you are doing.
30758230Syokota     */
30841016Sdfr    { MOUSE_MODEL_NET,			/* Genius NetMouse */
30958230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
31041016Sdfr    { MOUSE_MODEL_NETSCROLL,		/* Genius NetScroll */
31141016Sdfr      0xc8, 6, enable_groller, },
31258230Syokota    { MOUSE_MODEL_MOUSEMANPLUS,		/* Logitech MouseMan+ */
31358230Syokota      0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
31458230Syokota    { MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
31558230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
31658230Syokota    { MOUSE_MODEL_4D,			/* A4 Tech 4D Mouse */
31758230Syokota      0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
31858230Syokota    { MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
31958230Syokota      0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
32058230Syokota    { MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
32158230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
32241016Sdfr    { MOUSE_MODEL_GLIDEPOINT,		/* ALPS GlidePoint */
32341016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
32441016Sdfr    { MOUSE_MODEL_THINK,		/* Kensignton ThinkingMouse */
32541016Sdfr      0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
32649965Syokota    { MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
32749965Syokota      0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
328132865Snjl    { MOUSE_MODEL_SYNAPTICS,		/* Synaptics Touchpad */
329132865Snjl      0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics, },
33041016Sdfr    { MOUSE_MODEL_GENERIC,
33141016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
33241016Sdfr};
33363951Syokota#define GENERIC_MOUSE_ENTRY	((sizeof(vendortype) / sizeof(*vendortype)) - 1)
33441016Sdfr
33541016Sdfr/* device driver declarateion */
33641016Sdfrstatic device_method_t psm_methods[] = {
33741016Sdfr	/* Device interface */
33883147Syokota	DEVMETHOD(device_identify,	psmidentify),
33941016Sdfr	DEVMETHOD(device_probe,		psmprobe),
34041016Sdfr	DEVMETHOD(device_attach,	psmattach),
34158230Syokota	DEVMETHOD(device_detach,	psmdetach),
34254629Syokota	DEVMETHOD(device_resume,	psmresume),
34341016Sdfr
34441016Sdfr	{ 0, 0 }
34541016Sdfr};
34641016Sdfr
34741016Sdfrstatic driver_t psm_driver = {
34883492Syokota    PSM_DRIVER_NAME,
34941016Sdfr    psm_methods,
35041016Sdfr    sizeof(struct psm_softc),
35141016Sdfr};
35241016Sdfr
35341016Sdfr
35447625Sphkstatic struct cdevsw psm_cdevsw = {
355126080Sphk	.d_version =	D_VERSION,
356126080Sphk	.d_flags =	D_NEEDGIANT,
357111815Sphk	.d_open =	psmopen,
358111815Sphk	.d_close =	psmclose,
359111815Sphk	.d_read =	psmread,
360111815Sphk	.d_ioctl =	psmioctl,
361111815Sphk	.d_poll =	psmpoll,
362111815Sphk	.d_name =	PSM_DRIVER_NAME,
36341016Sdfr};
36441016Sdfr
36541016Sdfr/* debug message level */
36641016Sdfrstatic int verbose = PSM_DEBUG;
36741016Sdfr
36841016Sdfr/* device I/O routines */
36941016Sdfrstatic int
37041016Sdfrenable_aux_dev(KBDC kbdc)
37141016Sdfr{
37241016Sdfr    int res;
37341016Sdfr
37441016Sdfr    res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
37541016Sdfr    if (verbose >= 2)
37641016Sdfr        log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
37741016Sdfr
37841016Sdfr    return (res == PSM_ACK);
37941016Sdfr}
38041016Sdfr
38141016Sdfrstatic int
38241016Sdfrdisable_aux_dev(KBDC kbdc)
38341016Sdfr{
38441016Sdfr    int res;
38541016Sdfr
38641016Sdfr    res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
38741016Sdfr    if (verbose >= 2)
38841016Sdfr        log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
38941016Sdfr
39041016Sdfr    return (res == PSM_ACK);
39141016Sdfr}
39241016Sdfr
39341016Sdfrstatic int
39441016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
39541016Sdfr{
39641016Sdfr    int cmd;
39741016Sdfr    int res;
39841016Sdfr    int i;
39941016Sdfr
40041016Sdfr    switch (flag) {
40141016Sdfr    case 0:
40241016Sdfr    default:
40341016Sdfr	cmd = PSMC_SEND_DEV_STATUS;
40441016Sdfr	break;
40541016Sdfr    case 1:
40641016Sdfr	cmd = PSMC_SEND_DEV_DATA;
40741016Sdfr	break;
40841016Sdfr    }
40941016Sdfr    empty_aux_buffer(kbdc, 5);
41041016Sdfr    res = send_aux_command(kbdc, cmd);
41141016Sdfr    if (verbose >= 2)
41241016Sdfr        log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
41341016Sdfr	    (flag == 1) ? "DATA" : "STATUS", res);
41441016Sdfr    if (res != PSM_ACK)
41541016Sdfr        return 0;
41641016Sdfr
41741016Sdfr    for (i = 0; i < len; ++i) {
41841016Sdfr        status[i] = read_aux_data(kbdc);
41941016Sdfr	if (status[i] < 0)
42041016Sdfr	    break;
42141016Sdfr    }
42241016Sdfr
42341016Sdfr    if (verbose) {
42441016Sdfr        log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
42541016Sdfr            (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
42641016Sdfr    }
42741016Sdfr
42841016Sdfr    return i;
42941016Sdfr}
43041016Sdfr
43141016Sdfrstatic int
43241016Sdfrget_aux_id(KBDC kbdc)
43341016Sdfr{
43441016Sdfr    int res;
43541016Sdfr    int id;
43641016Sdfr
43741016Sdfr    empty_aux_buffer(kbdc, 5);
43841016Sdfr    res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
43941016Sdfr    if (verbose >= 2)
44041016Sdfr        log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
44141016Sdfr    if (res != PSM_ACK)
44241016Sdfr	return (-1);
44341016Sdfr
44441016Sdfr    /* 10ms delay */
44541016Sdfr    DELAY(10000);
44641016Sdfr
44741016Sdfr    id = read_aux_data(kbdc);
44841016Sdfr    if (verbose >= 2)
44941016Sdfr        log(LOG_DEBUG, "psm: device ID: %04x\n", id);
45041016Sdfr
45141016Sdfr    return id;
45241016Sdfr}
45341016Sdfr
45441016Sdfrstatic int
45541016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
45641016Sdfr{
45741016Sdfr    int res;
45841016Sdfr
45941016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
46041016Sdfr    if (verbose >= 2)
46141016Sdfr        log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
46241016Sdfr
46341016Sdfr    return ((res == PSM_ACK) ? rate : -1);
46441016Sdfr}
46541016Sdfr
46641016Sdfrstatic int
46741016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
46841016Sdfr{
46941016Sdfr    int res;
47041016Sdfr
47141016Sdfr    switch (scale) {
47241016Sdfr    case 1:
47341016Sdfr    default:
47441016Sdfr	scale = PSMC_SET_SCALING11;
47541016Sdfr	break;
47641016Sdfr    case 2:
47741016Sdfr	scale = PSMC_SET_SCALING21;
47841016Sdfr	break;
47941016Sdfr    }
48041016Sdfr    res = send_aux_command(kbdc, scale);
48141016Sdfr    if (verbose >= 2)
48241016Sdfr        log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
48341016Sdfr	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
48441016Sdfr
48541016Sdfr    return (res == PSM_ACK);
48641016Sdfr}
48741016Sdfr
48841016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
48941016Sdfrstatic int
49041016Sdfrset_mouse_resolution(KBDC kbdc, int val)
49141016Sdfr{
49241016Sdfr    int res;
49341016Sdfr
49441016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
49541016Sdfr    if (verbose >= 2)
49641016Sdfr        log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
49741016Sdfr
49841016Sdfr    return ((res == PSM_ACK) ? val : -1);
49941016Sdfr}
50041016Sdfr
50141016Sdfr/*
50241016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
50341016Sdfr * re-enabled by calling `enable_aux_dev()'
50441016Sdfr */
50541016Sdfrstatic int
50641016Sdfrset_mouse_mode(KBDC kbdc)
50741016Sdfr{
50841016Sdfr    int res;
50941016Sdfr
51041016Sdfr    res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
51141016Sdfr    if (verbose >= 2)
51241016Sdfr        log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
51341016Sdfr
51441016Sdfr    return (res == PSM_ACK);
51541016Sdfr}
51641016Sdfr
51741016Sdfrstatic int
51841016Sdfrget_mouse_buttons(KBDC kbdc)
51941016Sdfr{
52041016Sdfr    int c = 2;		/* assume two buttons by default */
52141016Sdfr    int status[3];
52241016Sdfr
52341016Sdfr    /*
52441016Sdfr     * NOTE: a special sequence to obtain Logitech Mouse specific
52541016Sdfr     * information: set resolution to 25 ppi, set scaling to 1:1, set
52641016Sdfr     * scaling to 1:1, set scaling to 1:1. Then the second byte of the
52741016Sdfr     * mouse status bytes is the number of available buttons.
52841016Sdfr     * Some manufactures also support this sequence.
52941016Sdfr     */
53041016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
53141016Sdfr        return c;
53241016Sdfr    if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
53341016Sdfr        && set_mouse_scaling(kbdc, 1)
53441016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
53541016Sdfr        if (status[1] != 0)
53641016Sdfr            return status[1];
53741016Sdfr    }
53841016Sdfr    return c;
53941016Sdfr}
54041016Sdfr
54141016Sdfr/* misc subroutines */
54241016Sdfr/*
54341016Sdfr * Someday, I will get the complete list of valid pointing devices and
54441016Sdfr * their IDs... XXX
54541016Sdfr */
54641016Sdfrstatic int
54741016Sdfris_a_mouse(int id)
54841016Sdfr{
54941016Sdfr#if 0
55041016Sdfr    static int valid_ids[] = {
55141016Sdfr        PSM_MOUSE_ID,		/* mouse */
55241016Sdfr        PSM_BALLPOINT_ID,	/* ballpoint device */
55341016Sdfr        PSM_INTELLI_ID,		/* Intellimouse */
55458230Syokota        PSM_EXPLORER_ID,	/* Intellimouse Explorer */
55541016Sdfr        -1			/* end of table */
55641016Sdfr    };
55741016Sdfr    int i;
55841016Sdfr
55941016Sdfr    for (i = 0; valid_ids[i] >= 0; ++i)
56041016Sdfr        if (valid_ids[i] == id)
56141016Sdfr            return TRUE;
56241016Sdfr    return FALSE;
56341016Sdfr#else
56441016Sdfr    return TRUE;
56541016Sdfr#endif
56641016Sdfr}
56741016Sdfr
56841016Sdfrstatic char *
56941016Sdfrmodel_name(int model)
57041016Sdfr{
57141016Sdfr    static struct {
57241016Sdfr	int model_code;
57341016Sdfr	char *model_name;
57441016Sdfr    } models[] = {
57558230Syokota        { MOUSE_MODEL_NETSCROLL,	"NetScroll" },
57658230Syokota        { MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
57741016Sdfr        { MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
57841016Sdfr        { MOUSE_MODEL_THINK,		"ThinkingMouse" },
57941016Sdfr        { MOUSE_MODEL_INTELLI,		"IntelliMouse" },
58041016Sdfr        { MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
58149965Syokota        { MOUSE_MODEL_VERSAPAD,		"VersaPad" },
58258230Syokota        { MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
58358230Syokota        { MOUSE_MODEL_4D,		"4D Mouse" },
58458230Syokota        { MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
585132865Snjl        { MOUSE_MODEL_SYNAPTICS,	"Synaptics Touchpad" },
58641016Sdfr        { MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
58741016Sdfr        { MOUSE_MODEL_UNKNOWN,		NULL },
58841016Sdfr    };
58941016Sdfr    int i;
59041016Sdfr
59141016Sdfr    for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
59241016Sdfr	if (models[i].model_code == model)
59341016Sdfr	    return models[i].model_name;
59441016Sdfr    }
59541016Sdfr    return "Unknown";
59641016Sdfr}
59741016Sdfr
59841016Sdfrstatic void
59941016Sdfrrecover_from_error(KBDC kbdc)
60041016Sdfr{
60141016Sdfr    /* discard anything left in the output buffer */
60241016Sdfr    empty_both_buffers(kbdc, 10);
60341016Sdfr
60441016Sdfr#if 0
60541016Sdfr    /*
60641016Sdfr     * NOTE: KBDC_RESET_KBD may not restore the communication between the
60741016Sdfr     * keyboard and the controller.
60841016Sdfr     */
60941016Sdfr    reset_kbd(kbdc);
61041016Sdfr#else
61141016Sdfr    /*
61241016Sdfr     * NOTE: somehow diagnostic and keyboard port test commands bring the
61341016Sdfr     * keyboard back.
61441016Sdfr     */
61541016Sdfr    if (!test_controller(kbdc))
61641016Sdfr        log(LOG_ERR, "psm: keyboard controller failed.\n");
61741016Sdfr    /* if there isn't a keyboard in the system, the following error is OK */
61841016Sdfr    if (test_kbd_port(kbdc) != 0) {
61941016Sdfr	if (verbose)
62041016Sdfr	    log(LOG_ERR, "psm: keyboard port failed.\n");
62141016Sdfr    }
62241016Sdfr#endif
62341016Sdfr}
62441016Sdfr
62541016Sdfrstatic int
62641016Sdfrrestore_controller(KBDC kbdc, int command_byte)
62741016Sdfr{
62841016Sdfr    empty_both_buffers(kbdc, 10);
62941016Sdfr
63041016Sdfr    if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
63141016Sdfr	log(LOG_ERR, "psm: failed to restore the keyboard controller "
63241016Sdfr		     "command byte.\n");
63358230Syokota	empty_both_buffers(kbdc, 10);
63441016Sdfr	return FALSE;
63541016Sdfr    } else {
63658230Syokota	empty_both_buffers(kbdc, 10);
63741016Sdfr	return TRUE;
63841016Sdfr    }
63941016Sdfr}
64041016Sdfr
64141016Sdfr/*
64241016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
64341016Sdfr * and its interrupt must be disabled before calling this routine.
64441016Sdfr * The aux device will be disabled before returning.
64541016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
64641016Sdfr * calling this routine.
64741016Sdfr */
64841016Sdfrstatic int
64984880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode)
65041016Sdfr{
65141016Sdfr    KBDC kbdc = sc->kbdc;
65241016Sdfr    int stat[3];
65341016Sdfr    int i;
65441016Sdfr
65541016Sdfr    switch((i = test_aux_port(kbdc))) {
656132270Smux    case 1:	/* ignore these errors */
657132270Smux    case 2:
658132270Smux    case 3:
65941016Sdfr    case PSM_ACK:
66041016Sdfr	if (verbose)
66141016Sdfr	    log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
66284880Syokota	        sc->unit, i);
663102412Scharnier	/* FALLTHROUGH */
66441016Sdfr    case 0:	/* no error */
66541016Sdfr    	break;
66641016Sdfr    case -1: 	/* time out */
66741016Sdfr    default: 	/* error */
66841016Sdfr    	recover_from_error(kbdc);
66945789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
67045789Speter	    break;
67141016Sdfr    	log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
67284880Syokota    	    sc->unit, i);
67341016Sdfr    	return FALSE;
67441016Sdfr    }
67541016Sdfr
67645789Speter    if (sc->config & PSM_CONFIG_NORESET) {
67745789Speter	/*
67845789Speter	 * Don't try to reset the pointing device.  It may possibly be
67945789Speter	 * left in the unknown state, though...
68045789Speter	 */
68145789Speter    } else {
68245789Speter	/*
68345789Speter	 * NOTE: some controllers appears to hang the `keyboard' when
68445789Speter	 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
68545789Speter	 */
68645789Speter	if (!reset_aux_dev(kbdc)) {
68745789Speter            recover_from_error(kbdc);
68884880Syokota            log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
68945789Speter            return FALSE;
69045789Speter	}
69141016Sdfr    }
69241016Sdfr
69341016Sdfr    /*
69441016Sdfr     * both the aux port and the aux device is functioning, see
69541016Sdfr     * if the device can be enabled.
69641016Sdfr     */
69741016Sdfr    if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
69884880Syokota        log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
69941016Sdfr        return FALSE;
70041016Sdfr    }
70141016Sdfr    empty_both_buffers(kbdc, 10);	/* remove stray data if any */
70241016Sdfr
70345789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
70445789Speter	i = GENERIC_MOUSE_ENTRY;
70545789Speter    } else {
70645789Speter	/* FIXME: hardware ID, mouse buttons? */
70741016Sdfr
70845789Speter	/* other parameters */
70945789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
71045789Speter	    if ((*vendortype[i].probefunc)(sc)) {
71145789Speter		if (verbose >= 2)
71245789Speter		    log(LOG_ERR, "psm%d: found %s\n",
71384880Syokota			sc->unit, model_name(vendortype[i].model));
71445789Speter		break;
71545789Speter	    }
71641016Sdfr	}
71741016Sdfr    }
71841016Sdfr
71941016Sdfr    sc->hw.model = vendortype[i].model;
72041016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
72141016Sdfr
72241016Sdfr    /* set mouse parameters */
72341016Sdfr    if (mode != (mousemode_t *)NULL) {
72441016Sdfr	if (mode->rate > 0)
72541016Sdfr            mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
72641016Sdfr	if (mode->resolution >= 0)
72741016Sdfr            mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
72841016Sdfr        set_mouse_scaling(kbdc, 1);
72941016Sdfr        set_mouse_mode(kbdc);
73041016Sdfr    }
73141016Sdfr
73241016Sdfr    /* request a data packet and extract sync. bits */
73341016Sdfr    if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
73484880Syokota        log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
73584880Syokota	    sc->unit);
73641016Sdfr        sc->mode.syncmask[0] = 0;
73741016Sdfr    } else {
73841016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
73941016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
74041016Sdfr	empty_aux_buffer(kbdc, 5);
74141016Sdfr    }
74241016Sdfr
74341016Sdfr    /* just check the status of the mouse */
74441016Sdfr    if (get_mouse_status(kbdc, stat, 0, 3) < 3)
74584880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
74684880Syokota	    sc->unit);
74741016Sdfr
74841016Sdfr    return TRUE;
74941016Sdfr}
75041016Sdfr
75141016Sdfrstatic int
75284880Syokotadoopen(struct psm_softc *sc, int command_byte)
75341016Sdfr{
75441016Sdfr    int stat[3];
75541016Sdfr
75641016Sdfr    /* enable the mouse device */
75741016Sdfr    if (!enable_aux_dev(sc->kbdc)) {
75841016Sdfr	/* MOUSE ERROR: failed to enable the mouse because:
75941016Sdfr	 * 1) the mouse is faulty,
76041016Sdfr	 * 2) the mouse has been removed(!?)
76141016Sdfr	 * In the latter case, the keyboard may have hung, and need
76241016Sdfr	 * recovery procedure...
76341016Sdfr	 */
76441016Sdfr	recover_from_error(sc->kbdc);
76541016Sdfr#if 0
76641016Sdfr	/* FIXME: we could reset the mouse here and try to enable
76741016Sdfr	 * it again. But it will take long time and it's not a good
76841016Sdfr	 * idea to disable the keyboard that long...
76941016Sdfr	 */
77084880Syokota	if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
77141016Sdfr	    recover_from_error(sc->kbdc);
77241016Sdfr#else
77341016Sdfr        {
77441016Sdfr#endif
77541016Sdfr            restore_controller(sc->kbdc, command_byte);
77641016Sdfr	    /* mark this device is no longer available */
77741016Sdfr	    sc->state &= ~PSM_VALID;
77841016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
77984880Syokota		sc->unit);
78041016Sdfr	    return (EIO);
78141016Sdfr	}
78241016Sdfr    }
78341016Sdfr
78441016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
78584880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
78641016Sdfr
78741016Sdfr    /* enable the aux port and interrupt */
78841016Sdfr    if (!set_controller_command_byte(sc->kbdc,
78941016Sdfr	    kbdc_get_device_mask(sc->kbdc),
79041016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
79141016Sdfr		| KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
79241016Sdfr	/* CONTROLLER ERROR */
79341016Sdfr	disable_aux_dev(sc->kbdc);
79441016Sdfr        restore_controller(sc->kbdc, command_byte);
79541016Sdfr	log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
79684880Syokota	    sc->unit);
79741016Sdfr	return (EIO);
79841016Sdfr    }
79941016Sdfr
80058230Syokota    /* start the watchdog timer */
80158230Syokota    sc->watchdog = FALSE;
80284880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
80358230Syokota
80441016Sdfr    return (0);
80541016Sdfr}
80641016Sdfr
80784880Syokotastatic int
80884880Syokotareinitialize(struct psm_softc *sc, int doinit)
80984880Syokota{
81084880Syokota    int err;
81184880Syokota    int c;
81284880Syokota    int s;
81384880Syokota
81484880Syokota    /* don't let anybody mess with the aux device */
81584880Syokota    if (!kbdc_lock(sc->kbdc, TRUE))
81684880Syokota	return (EIO);
81784880Syokota    s = spltty();
81884880Syokota
81984880Syokota    /* block our watchdog timer */
82084880Syokota    sc->watchdog = FALSE;
82184880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
82284880Syokota    callout_handle_init(&sc->callout);
82384880Syokota
82484880Syokota    /* save the current controller command byte */
82584880Syokota    empty_both_buffers(sc->kbdc, 10);
82684880Syokota    c = get_controller_command_byte(sc->kbdc);
82784880Syokota    if (verbose >= 2)
82884880Syokota        log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
82984880Syokota	    sc->unit, c);
83084880Syokota
83184880Syokota    /* enable the aux port but disable the aux interrupt and the keyboard */
83284880Syokota    if ((c == -1) || !set_controller_command_byte(sc->kbdc,
83384880Syokota	    kbdc_get_device_mask(sc->kbdc),
83484880Syokota  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
83584880Syokota	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
83684880Syokota        /* CONTROLLER ERROR */
83784880Syokota	splx(s);
83884880Syokota        kbdc_lock(sc->kbdc, FALSE);
83984880Syokota	log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
84084880Syokota	    sc->unit);
84184880Syokota	return (EIO);
84284880Syokota    }
84384880Syokota
84484880Syokota    /* flush any data */
84584880Syokota    if (sc->state & PSM_VALID) {
84684880Syokota	disable_aux_dev(sc->kbdc);	/* this may fail; but never mind... */
84784880Syokota	empty_aux_buffer(sc->kbdc, 10);
84884880Syokota    }
849123442Salfred    flushpackets(sc);
85084880Syokota    sc->syncerrors = 0;
85184880Syokota
85284880Syokota    /* try to detect the aux device; are you still there? */
85384880Syokota    err = 0;
85484880Syokota    if (doinit) {
85584880Syokota	if (doinitialize(sc, &sc->mode)) {
85684880Syokota	    /* yes */
85784880Syokota	    sc->state |= PSM_VALID;
85884880Syokota	} else {
85984880Syokota	    /* the device has gone! */
86084880Syokota	    restore_controller(sc->kbdc, c);
86184880Syokota	    sc->state &= ~PSM_VALID;
86284880Syokota	    log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
86384880Syokota		sc->unit);
86484880Syokota	    err = ENXIO;
86584880Syokota	}
86684880Syokota    }
86784880Syokota    splx(s);
86884880Syokota
86984880Syokota    /* restore the driver state */
87084880Syokota    if ((sc->state & PSM_OPEN) && (err == 0)) {
87184880Syokota        /* enable the aux device and the port again */
87284880Syokota	err = doopen(sc, c);
87384880Syokota	if (err != 0)
87484880Syokota	    log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
87584880Syokota		sc->unit);
87684880Syokota    } else {
87784880Syokota        /* restore the keyboard port and disable the aux port */
87884880Syokota        if (!set_controller_command_byte(sc->kbdc,
87984880Syokota                kbdc_get_device_mask(sc->kbdc),
88084880Syokota                (c & KBD_KBD_CONTROL_BITS)
88184880Syokota                    | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
88284880Syokota            /* CONTROLLER ERROR */
88384880Syokota            log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
88484880Syokota                sc->unit);
88584880Syokota            err = EIO;
88684880Syokota	}
88784880Syokota    }
88884880Syokota
88984880Syokota    kbdc_lock(sc->kbdc, FALSE);
89084880Syokota    return (err);
89184880Syokota}
89284880Syokota
89341016Sdfr/* psm driver entry points */
89441016Sdfr
89583147Syokotastatic void
89683147Syokotapsmidentify(driver_t *driver, device_t parent)
89783147Syokota{
89883931Syokota    device_t psmc;
89983931Syokota    device_t psm;
90083931Syokota    u_long irq;
90183931Syokota    int unit;
90283147Syokota
90383931Syokota    unit = device_get_unit(parent);
90483931Syokota
90583147Syokota    /* always add at least one child */
90683931Syokota    psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
90783931Syokota    if (psm == NULL)
90883931Syokota	return;
90983931Syokota
91083931Syokota    irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
91183931Syokota    if (irq > 0)
91283931Syokota	return;
91383931Syokota
91483931Syokota    /*
91583931Syokota     * If the PS/2 mouse device has already been reported by ACPI or
91683931Syokota     * PnP BIOS, obtain the IRQ resource from it.
91783931Syokota     * (See psmcpnp_attach() below.)
91883931Syokota     */
91983931Syokota    psmc = device_find_child(device_get_parent(parent),
92083931Syokota			     PSMCPNP_DRIVER_NAME, unit);
92183931Syokota    if (psmc == NULL)
92283931Syokota	return;
92383931Syokota    irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
92483931Syokota    if (irq <= 0)
92583931Syokota	return;
92683931Syokota    bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
92783147Syokota}
92883147Syokota
929115569Sphk#define endprobe(v)	do {   if (bootverbose)				\
93041016Sdfr				--verbose;   				\
93141016Sdfr                            kbdc_set_device_mask(sc->kbdc, mask);	\
93241016Sdfr			    kbdc_lock(sc->kbdc, FALSE);			\
93341016Sdfr			    return (v);	     				\
934115569Sphk			} while (0)
93541016Sdfr
93641016Sdfrstatic int
93741016Sdfrpsmprobe(device_t dev)
93841016Sdfr{
93941016Sdfr    int unit = device_get_unit(dev);
94041016Sdfr    struct psm_softc *sc = device_get_softc(dev);
94141016Sdfr    int stat[3];
94241016Sdfr    int command_byte;
94341016Sdfr    int mask;
94458230Syokota    int rid;
94541016Sdfr    int i;
94641016Sdfr
94741016Sdfr#if 0
94841016Sdfr    kbdc_debug(TRUE);
94941016Sdfr#endif
95058230Syokota
95183147Syokota    /* see if IRQ is available */
95283147Syokota    rid = KBDC_RID_AUX;
953127135Snjl    sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
954127135Snjl				      RF_SHAREABLE | RF_ACTIVE);
95583147Syokota    if (sc->intr == NULL) {
95683147Syokota	if (bootverbose)
95783147Syokota            device_printf(dev, "unable to allocate IRQ\n");
95883147Syokota        return (ENXIO);
95983147Syokota    }
96083147Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
96158230Syokota
96284880Syokota    sc->unit = unit;
96358230Syokota    sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
96483147Syokota    sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
96558230Syokota    /* XXX: for backward compatibility */
96658230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
96758230Syokota    sc->config |=
96858230Syokota#ifdef PSM_RESETAFTERSUSPEND
96958230Syokota	PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
97058230Syokota#else
97158230Syokota	PSM_CONFIG_HOOKRESUME;
97258230Syokota#endif
97358230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
97441016Sdfr    sc->flags = 0;
97541016Sdfr    if (bootverbose)
97641016Sdfr        ++verbose;
97741016Sdfr
97843105Sdfr    device_set_desc(dev, "PS/2 Mouse");
97943105Sdfr
98041016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE)) {
98141016Sdfr        printf("psm%d: unable to lock the controller.\n", unit);
98241016Sdfr        if (bootverbose)
98341016Sdfr            --verbose;
98441016Sdfr	return (ENXIO);
98541016Sdfr    }
98641016Sdfr
98741016Sdfr    /*
98841016Sdfr     * NOTE: two bits in the command byte controls the operation of the
98941016Sdfr     * aux port (mouse port): the aux port disable bit (bit 5) and the aux
99041016Sdfr     * port interrupt (IRQ 12) enable bit (bit 2).
99141016Sdfr     */
99241016Sdfr
99341016Sdfr    /* discard anything left after the keyboard initialization */
99441016Sdfr    empty_both_buffers(sc->kbdc, 10);
99541016Sdfr
99641016Sdfr    /* save the current command byte; it will be used later */
99741016Sdfr    mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
99841016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
99941016Sdfr    if (verbose)
100041016Sdfr        printf("psm%d: current command byte:%04x\n", unit, command_byte);
100141016Sdfr    if (command_byte == -1) {
100241016Sdfr        /* CONTROLLER ERROR */
100341016Sdfr        printf("psm%d: unable to get the current command byte value.\n",
100441016Sdfr            unit);
100541016Sdfr        endprobe(ENXIO);
100641016Sdfr    }
100741016Sdfr
100841016Sdfr    /*
100941016Sdfr     * disable the keyboard port while probing the aux port, which must be
101041016Sdfr     * enabled during this routine
101141016Sdfr     */
101241016Sdfr    if (!set_controller_command_byte(sc->kbdc,
101341016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
101441016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
101541016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
101641016Sdfr        /*
101741016Sdfr	 * this is CONTROLLER ERROR; I don't know how to recover
101841016Sdfr         * from this error...
101941016Sdfr	 */
102041016Sdfr        restore_controller(sc->kbdc, command_byte);
102141016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
102241016Sdfr        endprobe(ENXIO);
102341016Sdfr    }
102445789Speter    write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
102541016Sdfr
102641016Sdfr    /*
102741016Sdfr     * NOTE: `test_aux_port()' is designed to return with zero if the aux
102841016Sdfr     * port exists and is functioning. However, some controllers appears
102941016Sdfr     * to respond with zero even when the aux port doesn't exist. (It may
103041016Sdfr     * be that this is only the case when the controller DOES have the aux
103141016Sdfr     * port but the port is not wired on the motherboard.) The keyboard
103241016Sdfr     * controllers without the port, such as the original AT, are
103341016Sdfr     * supporsed to return with an error code or simply time out. In any
103441016Sdfr     * case, we have to continue probing the port even when the controller
103541016Sdfr     * passes this test.
103641016Sdfr     *
1037132270Smux     * XXX: some controllers erroneously return the error code 1, 2 or 3
1038132270Smux     * when it has the perfectly functional aux port. We have to ignore
1039132270Smux     * this error code. Even if the controller HAS error with the aux
1040132270Smux     * port, it will be detected later...
104141016Sdfr     * XXX: another incompatible controller returns PSM_ACK (0xfa)...
104241016Sdfr     */
104341016Sdfr    switch ((i = test_aux_port(sc->kbdc))) {
1044132270Smux    case 1:	   /* ignore these errors */
1045132270Smux    case 2:
1046132270Smux    case 3:
104741016Sdfr    case PSM_ACK:
104841016Sdfr        if (verbose)
104941016Sdfr	    printf("psm%d: strange result for test aux port (%d).\n",
105041016Sdfr	        unit, i);
1051102412Scharnier	/* FALLTHROUGH */
105241016Sdfr    case 0:        /* no error */
105341016Sdfr        break;
105441016Sdfr    case -1:        /* time out */
105541016Sdfr    default:        /* error */
105641016Sdfr        recover_from_error(sc->kbdc);
105745789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
105845789Speter	    break;
105941016Sdfr        restore_controller(sc->kbdc, command_byte);
106041016Sdfr        if (verbose)
106141016Sdfr            printf("psm%d: the aux port is not functioning (%d).\n",
106241016Sdfr                unit, i);
106341016Sdfr        endprobe(ENXIO);
106441016Sdfr    }
106541016Sdfr
106645789Speter    if (sc->config & PSM_CONFIG_NORESET) {
106745789Speter	/*
106845789Speter	 * Don't try to reset the pointing device.  It may possibly be
106945789Speter	 * left in the unknown state, though...
107045789Speter	 */
107145789Speter    } else {
107245789Speter	/*
107345789Speter	 * NOTE: some controllers appears to hang the `keyboard' when the aux
107445789Speter	 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1075117302Smikeh	 *
1076117302Smikeh	 * Attempt to reset the controller twice -- this helps
1077117302Smikeh	 * pierce through some KVM switches. The second reset
1078117302Smikeh	 * is non-fatal.
107945789Speter	 */
108045789Speter	if (!reset_aux_dev(sc->kbdc)) {
108145789Speter            recover_from_error(sc->kbdc);
108245789Speter            restore_controller(sc->kbdc, command_byte);
108345789Speter            if (verbose)
108445789Speter        	printf("psm%d: failed to reset the aux device.\n", unit);
108545789Speter            endprobe(ENXIO);
1086117302Smikeh	} else if (!reset_aux_dev(sc->kbdc)) {
1087117302Smikeh	    recover_from_error(sc->kbdc);
1088117302Smikeh	    if (verbose >= 2)
1089117302Smikeh        	printf("psm%d: failed to reset the aux device (2).\n",
1090117302Smikeh        	    unit);
109145789Speter	}
109241016Sdfr    }
109345789Speter
109441016Sdfr    /*
109541016Sdfr     * both the aux port and the aux device is functioning, see if the
109641016Sdfr     * device can be enabled. NOTE: when enabled, the device will start
109741016Sdfr     * sending data; we shall immediately disable the device once we know
109841016Sdfr     * the device can be enabled.
109941016Sdfr     */
110041016Sdfr    if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
110145789Speter        /* MOUSE ERROR */
110245789Speter	recover_from_error(sc->kbdc);
110345789Speter	restore_controller(sc->kbdc, command_byte);
110445789Speter	if (verbose)
110545789Speter	    printf("psm%d: failed to enable the aux device.\n", unit);
110641016Sdfr        endprobe(ENXIO);
110741016Sdfr    }
110841016Sdfr
110941016Sdfr    /* save the default values after reset */
111041016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
111141016Sdfr	sc->dflt_mode.rate = sc->mode.rate = stat[2];
111241016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
111341016Sdfr    } else {
111441016Sdfr	sc->dflt_mode.rate = sc->mode.rate = -1;
111541016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = -1;
111641016Sdfr    }
111741016Sdfr
111841016Sdfr    /* hardware information */
111941016Sdfr    sc->hw.iftype = MOUSE_IF_PS2;
112041016Sdfr
112141016Sdfr    /* verify the device is a mouse */
112241016Sdfr    sc->hw.hwid = get_aux_id(sc->kbdc);
112341016Sdfr    if (!is_a_mouse(sc->hw.hwid)) {
112441016Sdfr        restore_controller(sc->kbdc, command_byte);
112541016Sdfr        if (verbose)
112641016Sdfr            printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
112741016Sdfr        endprobe(ENXIO);
112841016Sdfr    }
112941016Sdfr    switch (sc->hw.hwid) {
113041016Sdfr    case PSM_BALLPOINT_ID:
113141016Sdfr        sc->hw.type = MOUSE_TRACKBALL;
113241016Sdfr        break;
113341016Sdfr    case PSM_MOUSE_ID:
113441016Sdfr    case PSM_INTELLI_ID:
113558230Syokota    case PSM_EXPLORER_ID:
113658230Syokota    case PSM_4DMOUSE_ID:
113758230Syokota    case PSM_4DPLUS_ID:
113841016Sdfr        sc->hw.type = MOUSE_MOUSE;
113941016Sdfr        break;
114041016Sdfr    default:
114141016Sdfr        sc->hw.type = MOUSE_UNKNOWN;
114241016Sdfr        break;
114341016Sdfr    }
114441016Sdfr
114545789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
114645789Speter	sc->hw.buttons = 2;
114745789Speter	i = GENERIC_MOUSE_ENTRY;
114845789Speter    } else {
114945789Speter	/* # of buttons */
115045789Speter	sc->hw.buttons = get_mouse_buttons(sc->kbdc);
115141016Sdfr
115245789Speter	/* other parameters */
115345789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
115445789Speter	    if ((*vendortype[i].probefunc)(sc)) {
115545789Speter		if (verbose >= 2)
115645789Speter		    printf("psm%d: found %s\n",
115745789Speter			   unit, model_name(vendortype[i].model));
115845789Speter		break;
115945789Speter	    }
116041016Sdfr	}
116141016Sdfr    }
116241016Sdfr
116341016Sdfr    sc->hw.model = vendortype[i].model;
116441016Sdfr
116541016Sdfr    sc->dflt_mode.level = PSM_LEVEL_BASE;
116641016Sdfr    sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
116741016Sdfr    sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
116841016Sdfr    if (sc->config & PSM_CONFIG_NOCHECKSYNC)
116941016Sdfr        sc->dflt_mode.syncmask[0] = 0;
117041016Sdfr    else
117141016Sdfr        sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
117245789Speter    if (sc->config & PSM_CONFIG_FORCETAP)
117345789Speter        sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
117441016Sdfr    sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
117541016Sdfr    sc->mode = sc->dflt_mode;
117641016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
117741016Sdfr
117841016Sdfr    /* set mouse parameters */
117948773Syokota#if 0
118048773Syokota    /*
118148773Syokota     * A version of Logitech FirstMouse+ won't report wheel movement,
118248773Syokota     * if SET_DEFAULTS is sent...  Don't use this command.
118348773Syokota     * This fix was found by Takashi Nishida.
118448773Syokota     */
118541016Sdfr    i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
118641016Sdfr    if (verbose >= 2)
118741016Sdfr	printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
118848773Syokota#endif
118941016Sdfr    if (sc->config & PSM_CONFIG_RESOLUTION) {
119041016Sdfr        sc->mode.resolution
119141016Sdfr	    = set_mouse_resolution(sc->kbdc,
119248773Syokota				   (sc->config & PSM_CONFIG_RESOLUTION) - 1);
119348773Syokota    } else if (sc->mode.resolution >= 0) {
119448773Syokota	sc->mode.resolution
119548773Syokota	    = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
119641016Sdfr    }
119748773Syokota    if (sc->mode.rate > 0) {
119848773Syokota	sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
119948773Syokota    }
120048773Syokota    set_mouse_scaling(sc->kbdc, 1);
120141016Sdfr
120241016Sdfr    /* request a data packet and extract sync. bits */
120341016Sdfr    if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
120441016Sdfr        printf("psm%d: failed to get data.\n", unit);
120541016Sdfr        sc->mode.syncmask[0] = 0;
120641016Sdfr    } else {
120741016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
120841016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
120941016Sdfr	empty_aux_buffer(sc->kbdc, 5);
121041016Sdfr    }
121141016Sdfr
121241016Sdfr    /* just check the status of the mouse */
121341016Sdfr    /*
121441016Sdfr     * NOTE: XXX there are some arcane controller/mouse combinations out
121541016Sdfr     * there, which hung the controller unless there is data transmission
121641016Sdfr     * after ACK from the mouse.
121741016Sdfr     */
121841016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
121941016Sdfr        printf("psm%d: failed to get status.\n", unit);
122041016Sdfr    } else {
122141016Sdfr	/*
122241016Sdfr	 * When in its native mode, some mice operate with different
122341016Sdfr	 * default parameters than in the PS/2 compatible mode.
122441016Sdfr	 */
122541016Sdfr        sc->dflt_mode.rate = sc->mode.rate = stat[2];
122641016Sdfr        sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
122741016Sdfr     }
122841016Sdfr
122941016Sdfr    /* disable the aux port for now... */
123041016Sdfr    if (!set_controller_command_byte(sc->kbdc,
123141016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
123241016Sdfr            (command_byte & KBD_KBD_CONTROL_BITS)
123341016Sdfr                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
123441016Sdfr        /*
123541016Sdfr	 * this is CONTROLLER ERROR; I don't know the proper way to
123641016Sdfr         * recover from this error...
123741016Sdfr	 */
123841016Sdfr        restore_controller(sc->kbdc, command_byte);
123941016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
124041016Sdfr        endprobe(ENXIO);
124141016Sdfr    }
124241016Sdfr
124341016Sdfr    /* done */
124441016Sdfr    kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
124541016Sdfr    kbdc_lock(sc->kbdc, FALSE);
124641016Sdfr    return (0);
124741016Sdfr}
124841016Sdfr
124941016Sdfrstatic int
125041016Sdfrpsmattach(device_t dev)
125141016Sdfr{
125241016Sdfr    int unit = device_get_unit(dev);
125341016Sdfr    struct psm_softc *sc = device_get_softc(dev);
125458230Syokota    int error;
125558230Syokota    int rid;
125641016Sdfr
125741016Sdfr    /* Setup initial state */
125841016Sdfr    sc->state = PSM_VALID;
125958230Syokota    callout_handle_init(&sc->callout);
126041016Sdfr
126158230Syokota    /* Setup our interrupt handler */
126283147Syokota    rid = KBDC_RID_AUX;
1263127135Snjl    sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1264127135Snjl				      RF_SHAREABLE | RF_ACTIVE);
126558230Syokota    if (sc->intr == NULL)
126658230Syokota	return (ENXIO);
126783147Syokota    error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
126858230Syokota    if (error) {
126958230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
127058230Syokota	return (error);
127158230Syokota    }
127258230Syokota
127341016Sdfr    /* Done */
127458230Syokota    sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
127558230Syokota		       "psm%d", unit);
127658230Syokota    sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
127758230Syokota			"bpsm%d", unit);
127841016Sdfr
127941016Sdfr    if (!verbose) {
128041016Sdfr        printf("psm%d: model %s, device ID %d\n",
128148778Syokota	    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
128241016Sdfr    } else {
128348778Syokota        printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
128448778Syokota	    unit, model_name(sc->hw.model),
128548778Syokota	    sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
128641016Sdfr	printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
128741016Sdfr	    unit, sc->config, sc->flags, sc->mode.packetsize);
128841016Sdfr	printf("psm%d: syncmask:%02x, syncbits:%02x\n",
128941016Sdfr	    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
129041016Sdfr    }
129141016Sdfr
129241016Sdfr    if (bootverbose)
129341016Sdfr        --verbose;
129441016Sdfr
129541016Sdfr    return (0);
129641016Sdfr}
129741016Sdfr
129841016Sdfrstatic int
129958230Syokotapsmdetach(device_t dev)
130058230Syokota{
130158230Syokota    struct psm_softc *sc;
130258230Syokota    int rid;
130358230Syokota
130458230Syokota    sc = device_get_softc(dev);
130558230Syokota    if (sc->state & PSM_OPEN)
130658230Syokota	return EBUSY;
130758230Syokota
130883147Syokota    rid = KBDC_RID_AUX;
130983147Syokota    bus_teardown_intr(dev, sc->intr, sc->ih);
131058230Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
131158230Syokota
131258230Syokota    destroy_dev(sc->dev);
131358230Syokota    destroy_dev(sc->bdev);
131458230Syokota
131558230Syokota    return 0;
131658230Syokota}
131758230Syokota
131858230Syokotastatic int
1319130585Sphkpsmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
132041016Sdfr{
132141016Sdfr    int unit = PSM_UNIT(dev);
132241016Sdfr    struct psm_softc *sc;
132341016Sdfr    int command_byte;
132441016Sdfr    int err;
132541016Sdfr    int s;
132641016Sdfr
132741016Sdfr    /* Get device data */
132841016Sdfr    sc = PSM_SOFTC(unit);
132941016Sdfr    if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
133041016Sdfr	/* the device is no longer valid/functioning */
133141016Sdfr        return (ENXIO);
133241016Sdfr
133341016Sdfr    /* Disallow multiple opens */
133441016Sdfr    if (sc->state & PSM_OPEN)
133541016Sdfr        return (EBUSY);
133641016Sdfr
133741016Sdfr    device_busy(devclass_get_device(psm_devclass, unit));
133841016Sdfr
133941016Sdfr    /* Initialize state */
134041016Sdfr    sc->mode.level = sc->dflt_mode.level;
134141016Sdfr    sc->mode.protocol = sc->dflt_mode.protocol;
134258230Syokota    sc->watchdog = FALSE;
134341016Sdfr
134441016Sdfr    /* flush the event queue */
134541016Sdfr    sc->queue.count = 0;
134641016Sdfr    sc->queue.head = 0;
134741016Sdfr    sc->queue.tail = 0;
134841016Sdfr    sc->status.flags = 0;
134941016Sdfr    sc->status.button = 0;
135041016Sdfr    sc->status.obutton = 0;
135141016Sdfr    sc->status.dx = 0;
135241016Sdfr    sc->status.dy = 0;
135341016Sdfr    sc->status.dz = 0;
135441016Sdfr    sc->button = 0;
1355123442Salfred    sc->pqueue_start = 0;
1356123442Salfred    sc->pqueue_end = 0;
135741016Sdfr
135841016Sdfr    /* empty input buffer */
1359123442Salfred    flushpackets(sc);
136084880Syokota    sc->syncerrors = 0;
136141016Sdfr
136241016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
136341016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
136441016Sdfr	return (EIO);
136541016Sdfr
136641016Sdfr    /* save the current controller command byte */
136741016Sdfr    s = spltty();
136841016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
136941016Sdfr
137041016Sdfr    /* enable the aux port and temporalily disable the keyboard */
137141016Sdfr    if ((command_byte == -1)
137241016Sdfr        || !set_controller_command_byte(sc->kbdc,
137341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
137441016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
137541016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
137641016Sdfr        /* CONTROLLER ERROR; do you know how to get out of this? */
137741016Sdfr        kbdc_lock(sc->kbdc, FALSE);
137841016Sdfr	splx(s);
137941016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
138041016Sdfr	    unit);
138141016Sdfr	return (EIO);
138241016Sdfr    }
138341016Sdfr    /*
138441016Sdfr     * Now that the keyboard controller is told not to generate
138541016Sdfr     * the keyboard and mouse interrupts, call `splx()' to allow
138641016Sdfr     * the other tty interrupts. The clock interrupt may also occur,
138741016Sdfr     * but timeout routines will be blocked by the poll flag set
138841016Sdfr     * via `kbdc_lock()'
138941016Sdfr     */
139041016Sdfr    splx(s);
139141016Sdfr
139241016Sdfr    /* enable the mouse device */
139384880Syokota    err = doopen(sc, command_byte);
139441016Sdfr
139541016Sdfr    /* done */
139641016Sdfr    if (err == 0)
139741016Sdfr        sc->state |= PSM_OPEN;
139841016Sdfr    kbdc_lock(sc->kbdc, FALSE);
139941016Sdfr    return (err);
140041016Sdfr}
140141016Sdfr
140241016Sdfrstatic int
1403130585Sphkpsmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
140441016Sdfr{
140541016Sdfr    int unit = PSM_UNIT(dev);
140641016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
140741016Sdfr    int stat[3];
140841016Sdfr    int command_byte;
140941016Sdfr    int s;
141041016Sdfr
141141016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
141241016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
141341016Sdfr	return (EIO);
141441016Sdfr
141541016Sdfr    /* save the current controller command byte */
141641016Sdfr    s = spltty();
141741016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
141841016Sdfr    if (command_byte == -1) {
141941016Sdfr        kbdc_lock(sc->kbdc, FALSE);
142041016Sdfr	splx(s);
142141016Sdfr	return (EIO);
142241016Sdfr    }
142341016Sdfr
142441016Sdfr    /* disable the aux interrupt and temporalily disable the keyboard */
142541016Sdfr    if (!set_controller_command_byte(sc->kbdc,
142641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
142741016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
142841016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
142941016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
143058230Syokota	    unit);
143141016Sdfr	/* CONTROLLER ERROR;
143241016Sdfr	 * NOTE: we shall force our way through. Because the only
143341016Sdfr	 * ill effect we shall see is that we may not be able
143441016Sdfr	 * to read ACK from the mouse, and it doesn't matter much
143541016Sdfr	 * so long as the mouse will accept the DISABLE command.
143641016Sdfr	 */
143741016Sdfr    }
143841016Sdfr    splx(s);
143941016Sdfr
144058230Syokota    /* stop the watchdog timer */
144184880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
144258230Syokota    callout_handle_init(&sc->callout);
144358230Syokota
144441016Sdfr    /* remove anything left in the output buffer */
144541016Sdfr    empty_aux_buffer(sc->kbdc, 10);
144641016Sdfr
144741016Sdfr    /* disable the aux device, port and interrupt */
144841016Sdfr    if (sc->state & PSM_VALID) {
144941016Sdfr        if (!disable_aux_dev(sc->kbdc)) {
145041016Sdfr	    /* MOUSE ERROR;
145141016Sdfr	     * NOTE: we don't return error and continue, pretending
145241016Sdfr	     * we have successfully disabled the device. It's OK because
145341016Sdfr	     * the interrupt routine will discard any data from the mouse
145441016Sdfr	     * hereafter.
145541016Sdfr	     */
145641016Sdfr	    log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
145758230Syokota		unit);
145841016Sdfr        }
145941016Sdfr
146041016Sdfr        if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
146141016Sdfr            log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
146258230Syokota		unit);
146341016Sdfr    }
146441016Sdfr
146541016Sdfr    if (!set_controller_command_byte(sc->kbdc,
146641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
146741016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
146841016Sdfr	        | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
146941016Sdfr	/* CONTROLLER ERROR;
147041016Sdfr	 * we shall ignore this error; see the above comment.
147141016Sdfr	 */
147241016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
147358230Syokota	    unit);
147441016Sdfr    }
147541016Sdfr
147641016Sdfr    /* remove anything left in the output buffer */
147741016Sdfr    empty_aux_buffer(sc->kbdc, 10);
147841016Sdfr
147941016Sdfr    /* close is almost always successful */
148041016Sdfr    sc->state &= ~PSM_OPEN;
148141016Sdfr    kbdc_lock(sc->kbdc, FALSE);
148241016Sdfr    device_unbusy(devclass_get_device(psm_devclass, unit));
148341016Sdfr    return (0);
148441016Sdfr}
148541016Sdfr
148641016Sdfrstatic int
1487123442Salfredtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, unsigned char *buf)
148841016Sdfr{
148941016Sdfr    static unsigned char butmapps2[8] = {
149041016Sdfr        0,
149141016Sdfr        MOUSE_PS2_BUTTON1DOWN,
149241016Sdfr        MOUSE_PS2_BUTTON2DOWN,
149341016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
149441016Sdfr        MOUSE_PS2_BUTTON3DOWN,
149541016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
149641016Sdfr        MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
149741016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
149841016Sdfr    };
149941016Sdfr    static unsigned char butmapmsc[8] = {
150041016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
150141016Sdfr        MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
150241016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
150341016Sdfr        MOUSE_MSC_BUTTON3UP,
150441016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
150541016Sdfr        MOUSE_MSC_BUTTON2UP,
150641016Sdfr        MOUSE_MSC_BUTTON1UP,
150741016Sdfr        0,
150841016Sdfr    };
150941016Sdfr    int mapped;
151041016Sdfr    int i;
151141016Sdfr
151241016Sdfr    if (sc->mode.level == PSM_LEVEL_BASE) {
151341016Sdfr        mapped = status->button & ~MOUSE_BUTTON4DOWN;
151441016Sdfr        if (status->button & MOUSE_BUTTON4DOWN)
151541016Sdfr	    mapped |= MOUSE_BUTTON1DOWN;
151641016Sdfr        status->button = mapped;
151741016Sdfr        buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1518109269Smdodd        i = imax(imin(status->dx, 255), -256);
151941016Sdfr	if (i < 0)
152041016Sdfr	    buf[0] |= MOUSE_PS2_XNEG;
152141016Sdfr        buf[1] = i;
1522109269Smdodd        i = imax(imin(status->dy, 255), -256);
152341016Sdfr	if (i < 0)
152441016Sdfr	    buf[0] |= MOUSE_PS2_YNEG;
152541016Sdfr        buf[2] = i;
152641016Sdfr	return MOUSE_PS2_PACKETSIZE;
152741016Sdfr    } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
152841016Sdfr        buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1529109269Smdodd        i = imax(imin(status->dx, 255), -256);
153041016Sdfr        buf[1] = i >> 1;
153141016Sdfr        buf[3] = i - buf[1];
1532109269Smdodd        i = imax(imin(status->dy, 255), -256);
153341016Sdfr        buf[2] = i >> 1;
153441016Sdfr        buf[4] = i - buf[2];
1535109269Smdodd        i = imax(imin(status->dz, 127), -128);
153641016Sdfr        buf[5] = (i >> 1) & 0x7f;
153741016Sdfr        buf[6] = (i - (i >> 1)) & 0x7f;
153841016Sdfr        buf[7] = (~status->button >> 3) & 0x7f;
153941016Sdfr	return MOUSE_SYS_PACKETSIZE;
154041016Sdfr    }
1541123442Salfred    return pb->inputbytes;
154241016Sdfr}
154341016Sdfr
154441016Sdfrstatic int
1545130585Sphkpsmread(struct cdev *dev, struct uio *uio, int flag)
154641016Sdfr{
154741016Sdfr    register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
154841016Sdfr    unsigned char buf[PSM_SMALLBUFSIZE];
154941016Sdfr    int error = 0;
155041016Sdfr    int s;
155141016Sdfr    int l;
155241016Sdfr
155341016Sdfr    if ((sc->state & PSM_VALID) == 0)
155441016Sdfr	return EIO;
155541016Sdfr
155641016Sdfr    /* block until mouse activity occured */
155741016Sdfr    s = spltty();
155841016Sdfr    while (sc->queue.count <= 0) {
155941016Sdfr        if (PSM_NBLOCKIO(dev)) {
156041016Sdfr            splx(s);
156141016Sdfr            return EWOULDBLOCK;
156241016Sdfr        }
156341016Sdfr        sc->state |= PSM_ASLP;
1564111748Sdes        error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
156541016Sdfr        sc->state &= ~PSM_ASLP;
156641016Sdfr        if (error) {
156741016Sdfr            splx(s);
156841016Sdfr            return error;
156941016Sdfr        } else if ((sc->state & PSM_VALID) == 0) {
157041016Sdfr            /* the device disappeared! */
157141016Sdfr            splx(s);
157241016Sdfr            return EIO;
157341016Sdfr	}
157441016Sdfr    }
157541016Sdfr    splx(s);
157641016Sdfr
157741016Sdfr    /* copy data to the user land */
157841016Sdfr    while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
157941016Sdfr        s = spltty();
1580109269Smdodd	l = imin(sc->queue.count, uio->uio_resid);
158141016Sdfr	if (l > sizeof(buf))
158241016Sdfr	    l = sizeof(buf);
158341016Sdfr	if (l > sizeof(sc->queue.buf) - sc->queue.head) {
158441016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
158541016Sdfr		sizeof(sc->queue.buf) - sc->queue.head);
158641016Sdfr	    bcopy(&sc->queue.buf[0],
158741016Sdfr		&buf[sizeof(sc->queue.buf) - sc->queue.head],
158841016Sdfr		l - (sizeof(sc->queue.buf) - sc->queue.head));
158941016Sdfr	} else {
159041016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
159141016Sdfr	}
159241016Sdfr	sc->queue.count -= l;
159341016Sdfr	sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
159441016Sdfr        splx(s);
159541016Sdfr        error = uiomove(buf, l, uio);
159641016Sdfr        if (error)
159741016Sdfr	    break;
159841016Sdfr    }
159941016Sdfr
160041016Sdfr    return error;
160141016Sdfr}
160241016Sdfr
160341016Sdfrstatic int
160441016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
160541016Sdfr{
160641016Sdfr    int s;
160741016Sdfr
160841016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
160941016Sdfr	return EIO;
161041016Sdfr
161141016Sdfr    s = spltty();
161241016Sdfr    *c = get_controller_command_byte(sc->kbdc);
161341016Sdfr    if ((*c == -1)
161441016Sdfr	|| !set_controller_command_byte(sc->kbdc,
161541016Sdfr	    kbdc_get_device_mask(sc->kbdc),
161641016Sdfr            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
161741016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
161841016Sdfr        /* this is CONTROLLER ERROR */
161941016Sdfr	splx(s);
162041016Sdfr        kbdc_lock(sc->kbdc, FALSE);
162141016Sdfr	return EIO;
162241016Sdfr    }
162341016Sdfr
162441016Sdfr    /*
162541016Sdfr     * The device may be in the middle of status data transmission.
162641016Sdfr     * The transmission will be interrupted, thus, incomplete status
162741016Sdfr     * data must be discarded. Although the aux interrupt is disabled
162841016Sdfr     * at the keyboard controller level, at most one aux interrupt
162941016Sdfr     * may have already been pending and a data byte is in the
163041016Sdfr     * output buffer; throw it away. Note that the second argument
163141016Sdfr     * to `empty_aux_buffer()' is zero, so that the call will just
163241016Sdfr     * flush the internal queue.
163341016Sdfr     * `psmintr()' will be invoked after `splx()' if an interrupt is
163441016Sdfr     * pending; it will see no data and returns immediately.
163541016Sdfr     */
163641016Sdfr    empty_aux_buffer(sc->kbdc, 0);	/* flush the queue */
163741016Sdfr    read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
1638123442Salfred    flushpackets(sc);
163941016Sdfr    splx(s);
164041016Sdfr
164141016Sdfr    return 0;
164241016Sdfr}
164341016Sdfr
1644123442Salfredstatic void
1645123442Salfreddropqueue(struct psm_softc *sc)
1646123442Salfred{
1647123442Salfred
1648123442Salfred    	sc->queue.count = 0;
1649123442Salfred   	sc->queue.head = 0;
1650123442Salfred    	sc->queue.tail = 0;
1651123442Salfred	if ((sc->state & PSM_SOFTARMED) != 0) {
1652123442Salfred		sc->state &= ~PSM_SOFTARMED;
1653123442Salfred		untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout);
1654123442Salfred	}
1655123442Salfred	sc->pqueue_start = sc->pqueue_end;
1656123442Salfred}
1657123442Salfred
1658123442Salfredstatic void
1659123442Salfredflushpackets(struct psm_softc *sc)
1660123442Salfred{
1661123442Salfred
1662123442Salfred	dropqueue(sc);
1663123442Salfred	bzero(&sc->pqueue, sizeof(sc->pqueue));
1664123442Salfred}
1665123442Salfred
166641016Sdfrstatic int
166741016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
166841016Sdfr{
166941016Sdfr    int error = 0;
167041016Sdfr
167141016Sdfr    /*
167241016Sdfr     * We may have seen a part of status data during `set_mouse_XXX()'.
167341016Sdfr     * they have been queued; flush it.
167441016Sdfr     */
167541016Sdfr    empty_aux_buffer(sc->kbdc, 0);
167641016Sdfr
167741016Sdfr    /* restore ports and interrupt */
167841016Sdfr    if (!set_controller_command_byte(sc->kbdc,
167941016Sdfr            kbdc_get_device_mask(sc->kbdc),
168041016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
168141016Sdfr        /* CONTROLLER ERROR; this is serious, we may have
168241016Sdfr         * been left with the inaccessible keyboard and
168341016Sdfr         * the disabled mouse interrupt.
168441016Sdfr         */
168541016Sdfr        error = EIO;
168641016Sdfr    }
168741016Sdfr
168841016Sdfr    kbdc_lock(sc->kbdc, FALSE);
168941016Sdfr    return error;
169041016Sdfr}
169141016Sdfr
169241016Sdfrstatic int
1693130585Sphkpsmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
169441016Sdfr{
169541016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
169641016Sdfr    mousemode_t mode;
169741016Sdfr    mousestatus_t status;
169841016Sdfr#if (defined(MOUSE_GETVARS))
169941016Sdfr    mousevar_t *var;
170041016Sdfr#endif
170141016Sdfr    mousedata_t *data;
170241016Sdfr    int stat[3];
170341016Sdfr    int command_byte;
170441016Sdfr    int error = 0;
170541016Sdfr    int s;
170641016Sdfr
170741016Sdfr    /* Perform IOCTL command */
170841016Sdfr    switch (cmd) {
170941016Sdfr
171041016Sdfr    case OLD_MOUSE_GETHWINFO:
171141016Sdfr	s = spltty();
171241016Sdfr        ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
171341016Sdfr        ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
171441016Sdfr        ((old_mousehw_t *)addr)->type = sc->hw.type;
171548778Syokota        ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
171641016Sdfr	splx(s);
171741016Sdfr        break;
171841016Sdfr
171941016Sdfr    case MOUSE_GETHWINFO:
172041016Sdfr	s = spltty();
172141016Sdfr        *(mousehw_t *)addr = sc->hw;
172241016Sdfr	if (sc->mode.level == PSM_LEVEL_BASE)
172341016Sdfr	    ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
172441016Sdfr	splx(s);
172541016Sdfr        break;
172641016Sdfr
1727132865Snjl    case MOUSE_SYN_GETHWINFO:
1728132865Snjl	s = spltty();
1729132865Snjl	if (sc->hw.model == MOUSE_MODEL_SYNAPTICS)
1730132865Snjl	    *(synapticshw_t *)addr = sc->synhw;
1731132865Snjl	else
1732132865Snjl	    error = EINVAL;
1733132865Snjl	splx(s);
1734132865Snjl	break;
1735132865Snjl
173641016Sdfr    case OLD_MOUSE_GETMODE:
173741016Sdfr	s = spltty();
173841016Sdfr	switch (sc->mode.level) {
173941016Sdfr	case PSM_LEVEL_BASE:
174041016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
174141016Sdfr	    break;
174241016Sdfr	case PSM_LEVEL_STANDARD:
174341016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
174441016Sdfr	    break;
174541016Sdfr	case PSM_LEVEL_NATIVE:
174641016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
174741016Sdfr	    break;
174841016Sdfr	}
174941016Sdfr        ((old_mousemode_t *)addr)->rate = sc->mode.rate;
175041016Sdfr        ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
175141016Sdfr        ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
175241016Sdfr	splx(s);
175341016Sdfr        break;
175441016Sdfr
175541016Sdfr    case MOUSE_GETMODE:
175641016Sdfr	s = spltty();
175741016Sdfr        *(mousemode_t *)addr = sc->mode;
175841016Sdfr        ((mousemode_t *)addr)->resolution =
175941016Sdfr	    MOUSE_RES_LOW - sc->mode.resolution;
176041016Sdfr	switch (sc->mode.level) {
176141016Sdfr	case PSM_LEVEL_BASE:
176241016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
176341016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
176441016Sdfr	    break;
176541016Sdfr	case PSM_LEVEL_STANDARD:
176641016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
176741016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
176841016Sdfr	    ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
176941016Sdfr	    ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
177041016Sdfr	    break;
177141016Sdfr	case PSM_LEVEL_NATIVE:
177241016Sdfr	    /* FIXME: this isn't quite correct... XXX */
177341016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
177441016Sdfr	    break;
177541016Sdfr	}
177641016Sdfr	splx(s);
177741016Sdfr        break;
177841016Sdfr
177941016Sdfr    case OLD_MOUSE_SETMODE:
178041016Sdfr    case MOUSE_SETMODE:
178141016Sdfr	if (cmd == OLD_MOUSE_SETMODE) {
178241016Sdfr	    mode.rate = ((old_mousemode_t *)addr)->rate;
178341016Sdfr	    /*
178441016Sdfr	     * resolution  old I/F   new I/F
178541016Sdfr	     * default        0         0
178641016Sdfr	     * low            1        -2
178741016Sdfr	     * medium low     2        -3
178841016Sdfr	     * medium high    3        -4
178941016Sdfr	     * high           4        -5
179041016Sdfr	     */
179141016Sdfr	    if (((old_mousemode_t *)addr)->resolution > 0)
179241016Sdfr	        mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
179341016Sdfr	    mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
179441016Sdfr	    mode.level = -1;
179541016Sdfr	} else {
179641016Sdfr	    mode = *(mousemode_t *)addr;
179741016Sdfr	}
179841016Sdfr
179941016Sdfr	/* adjust and validate parameters. */
180041016Sdfr	if (mode.rate > UCHAR_MAX)
180141016Sdfr	    return EINVAL;
180241016Sdfr        if (mode.rate == 0)
180341016Sdfr            mode.rate = sc->dflt_mode.rate;
180441016Sdfr	else if (mode.rate == -1)
180541016Sdfr	    /* don't change the current setting */
180641016Sdfr	    ;
180741016Sdfr	else if (mode.rate < 0)
180841016Sdfr	    return EINVAL;
180941016Sdfr	if (mode.resolution >= UCHAR_MAX)
181041016Sdfr	    return EINVAL;
181141016Sdfr	if (mode.resolution >= 200)
181241016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
181341016Sdfr	else if (mode.resolution >= 100)
181441016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
181541016Sdfr	else if (mode.resolution >= 50)
181641016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
181741016Sdfr	else if (mode.resolution > 0)
181841016Sdfr	    mode.resolution = MOUSE_RES_LOW;
181941016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
182041016Sdfr            mode.resolution = sc->dflt_mode.resolution;
182141016Sdfr        else if (mode.resolution == -1)
182241016Sdfr	    /* don't change the current setting */
182341016Sdfr	    ;
182441016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
182541016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
182641016Sdfr	if (mode.level == -1)
182741016Sdfr	    /* don't change the current setting */
182841016Sdfr	    mode.level = sc->mode.level;
182941016Sdfr	else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
183041016Sdfr	    return EINVAL;
183141016Sdfr        if (mode.accelfactor == -1)
183241016Sdfr	    /* don't change the current setting */
183341016Sdfr	    mode.accelfactor = sc->mode.accelfactor;
183441016Sdfr        else if (mode.accelfactor < 0)
183541016Sdfr	    return EINVAL;
183641016Sdfr
183741016Sdfr	/* don't allow anybody to poll the keyboard controller */
183841016Sdfr	error = block_mouse_data(sc, &command_byte);
183941016Sdfr	if (error)
184041016Sdfr            return error;
184141016Sdfr
184241016Sdfr        /* set mouse parameters */
184341016Sdfr	if (mode.rate > 0)
184441016Sdfr	    mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
184541016Sdfr	if (mode.resolution >= 0)
184641016Sdfr	    mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
184741016Sdfr	set_mouse_scaling(sc->kbdc, 1);
184841016Sdfr	get_mouse_status(sc->kbdc, stat, 0, 3);
184941016Sdfr
185041016Sdfr        s = spltty();
185141016Sdfr    	sc->mode.rate = mode.rate;
185241016Sdfr    	sc->mode.resolution = mode.resolution;
185341016Sdfr    	sc->mode.accelfactor = mode.accelfactor;
185441016Sdfr    	sc->mode.level = mode.level;
185541016Sdfr        splx(s);
185641016Sdfr
185741016Sdfr	unblock_mouse_data(sc, command_byte);
185841016Sdfr        break;
185941016Sdfr
186041016Sdfr    case MOUSE_GETLEVEL:
186141016Sdfr	*(int *)addr = sc->mode.level;
186241016Sdfr        break;
186341016Sdfr
186441016Sdfr    case MOUSE_SETLEVEL:
186541016Sdfr	if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
186641016Sdfr	    return EINVAL;
186741016Sdfr	sc->mode.level = *(int *)addr;
186841016Sdfr        break;
186941016Sdfr
187041016Sdfr    case MOUSE_GETSTATUS:
187141016Sdfr        s = spltty();
187241016Sdfr	status = sc->status;
187341016Sdfr	sc->status.flags = 0;
187441016Sdfr	sc->status.obutton = sc->status.button;
187541016Sdfr	sc->status.button = 0;
187641016Sdfr	sc->status.dx = 0;
187741016Sdfr	sc->status.dy = 0;
187841016Sdfr	sc->status.dz = 0;
187941016Sdfr        splx(s);
188041016Sdfr        *(mousestatus_t *)addr = status;
188141016Sdfr        break;
188241016Sdfr
188341016Sdfr#if (defined(MOUSE_GETVARS))
188441016Sdfr    case MOUSE_GETVARS:
188541016Sdfr	var = (mousevar_t *)addr;
188641016Sdfr	bzero(var, sizeof(*var));
188741016Sdfr	s = spltty();
188841016Sdfr        var->var[0] = MOUSE_VARS_PS2_SIG;
188941016Sdfr        var->var[1] = sc->config;
189041016Sdfr        var->var[2] = sc->flags;
189141016Sdfr	splx(s);
189241016Sdfr        break;
189341016Sdfr
189441016Sdfr    case MOUSE_SETVARS:
189541016Sdfr	return ENODEV;
189641016Sdfr#endif /* MOUSE_GETVARS */
189741016Sdfr
189841016Sdfr    case MOUSE_READSTATE:
189941016Sdfr    case MOUSE_READDATA:
190041016Sdfr	data = (mousedata_t *)addr;
190141016Sdfr	if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
190241016Sdfr	    return EINVAL;
190341016Sdfr
190441016Sdfr	error = block_mouse_data(sc, &command_byte);
190541016Sdfr	if (error)
190641016Sdfr            return error;
190741016Sdfr        if ((data->len = get_mouse_status(sc->kbdc, data->buf,
190841016Sdfr		(cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
190941016Sdfr            error = EIO;
191041016Sdfr	unblock_mouse_data(sc, command_byte);
191141016Sdfr	break;
191241016Sdfr
191341016Sdfr#if (defined(MOUSE_SETRESOLUTION))
191441016Sdfr    case MOUSE_SETRESOLUTION:
191541016Sdfr	mode.resolution = *(int *)addr;
191641016Sdfr	if (mode.resolution >= UCHAR_MAX)
191741016Sdfr	    return EINVAL;
191841016Sdfr	else if (mode.resolution >= 200)
191941016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
192041016Sdfr	else if (mode.resolution >= 100)
192141016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
192241016Sdfr	else if (mode.resolution >= 50)
192341016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
192441016Sdfr	else if (mode.resolution > 0)
192541016Sdfr	    mode.resolution = MOUSE_RES_LOW;
192641016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
192741016Sdfr            mode.resolution = sc->dflt_mode.resolution;
192841016Sdfr        else if (mode.resolution == -1)
192941016Sdfr	    mode.resolution = sc->mode.resolution;
193041016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
193141016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
193241016Sdfr
193341016Sdfr	error = block_mouse_data(sc, &command_byte);
193441016Sdfr	if (error)
193541016Sdfr            return error;
193641016Sdfr        sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
193741016Sdfr	if (sc->mode.resolution != mode.resolution)
193841016Sdfr	    error = EIO;
193941016Sdfr	unblock_mouse_data(sc, command_byte);
194041016Sdfr        break;
194141016Sdfr#endif /* MOUSE_SETRESOLUTION */
194241016Sdfr
194341016Sdfr#if (defined(MOUSE_SETRATE))
194441016Sdfr    case MOUSE_SETRATE:
194541016Sdfr	mode.rate = *(int *)addr;
194641016Sdfr	if (mode.rate > UCHAR_MAX)
194741016Sdfr	    return EINVAL;
194841016Sdfr        if (mode.rate == 0)
194941016Sdfr            mode.rate = sc->dflt_mode.rate;
195041016Sdfr	else if (mode.rate < 0)
195141016Sdfr	    mode.rate = sc->mode.rate;
195241016Sdfr
195341016Sdfr	error = block_mouse_data(sc, &command_byte);
195441016Sdfr	if (error)
195541016Sdfr            return error;
195641016Sdfr        sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
195741016Sdfr	if (sc->mode.rate != mode.rate)
195841016Sdfr	    error = EIO;
195941016Sdfr	unblock_mouse_data(sc, command_byte);
196041016Sdfr        break;
196141016Sdfr#endif /* MOUSE_SETRATE */
196241016Sdfr
196341016Sdfr#if (defined(MOUSE_SETSCALING))
196441016Sdfr    case MOUSE_SETSCALING:
196541016Sdfr	if ((*(int *)addr <= 0) || (*(int *)addr > 2))
196641016Sdfr	    return EINVAL;
196741016Sdfr
196841016Sdfr	error = block_mouse_data(sc, &command_byte);
196941016Sdfr	if (error)
197041016Sdfr            return error;
197141016Sdfr        if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
197241016Sdfr	    error = EIO;
197341016Sdfr	unblock_mouse_data(sc, command_byte);
197441016Sdfr        break;
197541016Sdfr#endif /* MOUSE_SETSCALING */
197641016Sdfr
197741016Sdfr#if (defined(MOUSE_GETHWID))
197841016Sdfr    case MOUSE_GETHWID:
197941016Sdfr	error = block_mouse_data(sc, &command_byte);
198041016Sdfr	if (error)
198141016Sdfr            return error;
198248778Syokota        sc->hw.hwid &= ~0x00ff;
198348778Syokota        sc->hw.hwid |= get_aux_id(sc->kbdc);
198448778Syokota	*(int *)addr = sc->hw.hwid & 0x00ff;
198541016Sdfr	unblock_mouse_data(sc, command_byte);
198641016Sdfr        break;
198741016Sdfr#endif /* MOUSE_GETHWID */
198841016Sdfr
198941016Sdfr    default:
199041016Sdfr	return ENOTTY;
199141016Sdfr    }
199241016Sdfr
199341016Sdfr    return error;
199441016Sdfr}
199541016Sdfr
199641016Sdfrstatic void
199758230Syokotapsmtimeout(void *arg)
199858230Syokota{
199958230Syokota    struct psm_softc *sc;
200065045Syokota    int s;
200158230Syokota
200284880Syokota    sc = (struct psm_softc *)arg;
200365045Syokota    s = spltty();
200463746Syokota    if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
200558230Syokota	if (verbose >= 4)
200684880Syokota	    log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
200758230Syokota	psmintr(sc);
200863746Syokota	kbdc_lock(sc->kbdc, FALSE);
200958230Syokota    }
201058230Syokota    sc->watchdog = TRUE;
201165045Syokota    splx(s);
201284880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
201358230Syokota}
201458230Syokota
2015123442Salfredstatic int psmhz = 20;
2016123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmhz, CTLFLAG_RW, &psmhz, 0, "");
2017123442Salfred
2018123442Salfredstatic int psm_soft_timeout = 500000; /* 0.5 sec */
2019123442SalfredSYSCTL_INT(_debug, OID_AUTO, psm_soft_timeout, CTLFLAG_RW,
2020123442Salfred    &psm_soft_timeout, 0, "");
2021123442Salfred
2022123442Salfredstatic int psmerrsecs = 2;
2023123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrsecs, CTLFLAG_RW, &psmerrsecs, 0, "");
2024123442Salfredstatic int psmerrusecs = 0;
2025123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrusecs, CTLFLAG_RW, &psmerrusecs, 0, "");
2026123442Salfredstatic int psmsecs = 0;
2027123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmsecs, CTLFLAG_RW, &psmsecs, 0, "");
2028123442Salfredstatic int psmusecs = 500000;
2029123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmusecs, CTLFLAG_RW, &psmusecs, 0, "");
2030123442Salfred
203158230Syokotastatic void
203241016Sdfrpsmintr(void *arg)
203341016Sdfr{
2034123442Salfred    struct psm_softc *sc = arg;
2035123442Salfred    struct timeval now;
203641016Sdfr    int c;
2037123442Salfred    packetbuf_t *pb;
2038123442Salfred    int haderror = 0;
203941016Sdfr
2040123442Salfred
204141016Sdfr    /* read until there is nothing to read */
204241016Sdfr    while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
204341016Sdfr
2044123442Salfred        pb = &sc->pqueue[sc->pqueue_end];
204541016Sdfr        /* discard the byte if the device is not open */
204641016Sdfr        if ((sc->state & PSM_OPEN) == 0)
204741016Sdfr            continue;
204841016Sdfr
2049123442Salfred	getmicrouptime(&now);
2050123442Salfred	if ((pb->inputbytes > 0) && timevalcmp(&now, &sc->inputtimeout, >)) {
2051127841Smarkm#if DEBUG
205284880Syokota	    log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2053127841Smarkm#endif
2054123442Salfred	    pb->inputbytes = 0;
205584880Syokota	    sc->syncerrors = 0;
205684880Syokota	}
205784880Syokota	sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
205884880Syokota	sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2059123442Salfred	timevaladd(&sc->inputtimeout, &now);
206084880Syokota
2061123442Salfred        pb->ipacket[pb->inputbytes++] = c;
2062123442Salfred        if (pb->inputbytes < sc->mode.packetsize)
206341016Sdfr	    continue;
206441016Sdfr
2065127841Smarkm#if DEBUG
206641016Sdfr        log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2067123442Salfred	    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2068123442Salfred	    pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]);
206941016Sdfr#endif
207041016Sdfr
2071123442Salfred	c = pb->ipacket[0];
207241016Sdfr
207363746Syokota	if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2074127841Smarkm#if DEBUG
2075123442Salfred            log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x) %d"
2076123442Salfred		" cmds since last error.\n",
2077123442Salfred		c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2078123442Salfred		sc->cmdcount - sc->lasterr);
2079127841Smarkm#endif
2080123442Salfred	    haderror = 1;
2081123442Salfred	    sc->lasterr = sc->cmdcount;
2082123442Salfred	    dropqueue(sc);
208384880Syokota	    ++sc->syncerrors;
2084123442Salfred	    sc->lastinputerr = now;
208584880Syokota	    if (sc->syncerrors < sc->mode.packetsize) {
2086127841Smarkm#if DEBUG
208784880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2088127841Smarkm#endif
2089123442Salfred		--pb->inputbytes;
2090123442Salfred		bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
209184880Syokota	    } else if (sc->syncerrors == sc->mode.packetsize) {
2092127841Smarkm#if DEBUG
209369439Syokota		log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2094127841Smarkm#endif
2095123442Salfred		pb->inputbytes = 0;
209669439Syokota		disable_aux_dev(sc->kbdc);
209769439Syokota		enable_aux_dev(sc->kbdc);
209884880Syokota	    } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2099127841Smarkm#if DEBUG
210084880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2101127841Smarkm#endif
2102123442Salfred		--pb->inputbytes;
2103123442Salfred		bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
210484880Syokota	    } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2105127841Smarkm#if DEBUG
210684880Syokota		log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2107127841Smarkm#endif
210884880Syokota		reinitialize(sc, TRUE);
210969439Syokota	    }
211084880Syokota	    continue;
211163746Syokota	}
2112123442Salfred	/* if this packet is at all bogus then drop the packet. */
2113123442Salfred	if (haderror ||
2114123442Salfred	    !timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, &now)) {
2115123442Salfred		pb->inputbytes = 0;
2116123442Salfred		haderror = 0;
2117123442Salfred		continue;
2118123442Salfred	}
211963746Syokota
2120123442Salfred	sc->cmdcount++;
2121123442Salfred	if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2122123442Salfred		sc->pqueue_end = 0;
2123123442Salfred	/*
2124123442Salfred	 * If we've filled the queue then call the softintr ourselves,
2125123442Salfred	 * otherwise schedule the interrupt for later.
2126123442Salfred	 */
2127123442Salfred	if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2128123442Salfred	    (sc->pqueue_end == sc->pqueue_start)) {
2129123442Salfred    		if ((sc->state & PSM_SOFTARMED) != 0) {
2130123442Salfred			sc->state &= ~PSM_SOFTARMED;
2131123442Salfred			untimeout(psmsoftintr, arg, sc->softcallout);
2132123442Salfred		}
2133123442Salfred		psmsoftintr(arg);
2134123442Salfred	} else if ((sc->state & PSM_SOFTARMED) == 0) {
2135123442Salfred		sc->state |= PSM_SOFTARMED;
2136123442Salfred		sc->softcallout = timeout(psmsoftintr, arg,
2137123442Salfred		    psmhz < 1 ? 1 : (hz/psmhz));
2138123442Salfred	}
2139123442Salfred    }
2140123442Salfred}
2141123442Salfred
2142123442Salfredstatic void
2143123442Salfredpsmsoftintr(void *arg)
2144123442Salfred{
2145123442Salfred    /*
2146123442Salfred     * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
2147123442Salfred     * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
2148123442Salfred     */
2149123442Salfred    static int butmap[8] = {
2150123442Salfred        0,
2151123442Salfred	MOUSE_BUTTON1DOWN,
2152123442Salfred	MOUSE_BUTTON3DOWN,
2153123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2154123442Salfred	MOUSE_BUTTON2DOWN,
2155123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
2156123442Salfred	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
2157123442Salfred        MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
2158123442Salfred    };
2159123442Salfred    static int butmap_versapad[8] = {
2160123442Salfred	0,
2161123442Salfred	MOUSE_BUTTON3DOWN,
2162123442Salfred	0,
2163123442Salfred	MOUSE_BUTTON3DOWN,
2164123442Salfred	MOUSE_BUTTON1DOWN,
2165123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2166123442Salfred	MOUSE_BUTTON1DOWN,
2167123442Salfred	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
2168123442Salfred    };
2169123442Salfred    register struct psm_softc *sc = arg;
2170123442Salfred    mousestatus_t ms;
2171132865Snjl    int w, x, y, z;
2172123442Salfred    int c;
2173123442Salfred    int l;
2174123442Salfred    int x0, y0;
2175123442Salfred    int s;
2176123442Salfred    packetbuf_t *pb;
2177123442Salfred
2178123442Salfred    getmicrouptime(&sc->lastsoftintr);
2179123442Salfred
2180123442Salfred    s = spltty();
2181123442Salfred
2182123442Salfred    do {
2183123442Salfred
2184123442Salfred	pb = &sc->pqueue[sc->pqueue_start];
2185123442Salfred	c = pb->ipacket[0];
218649965Syokota	/*
218741016Sdfr	 * A kludge for Kensington device!
218841016Sdfr	 * The MSB of the horizontal count appears to be stored in
218958230Syokota	 * a strange place.
219041016Sdfr	 */
219158230Syokota	if (sc->hw.model == MOUSE_MODEL_THINK)
2192123442Salfred	    pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
219341016Sdfr
219441016Sdfr        /* ignore the overflow bits... */
2195123442Salfred        x = (c & MOUSE_PS2_XNEG) ?  pb->ipacket[1] - 256 : pb->ipacket[1];
2196123442Salfred        y = (c & MOUSE_PS2_YNEG) ?  pb->ipacket[2] - 256 : pb->ipacket[2];
219741016Sdfr	z = 0;
219841016Sdfr        ms.obutton = sc->button;		  /* previous button state */
219941016Sdfr        ms.button = butmap[c & MOUSE_PS2_BUTTONS];
220045789Speter	/* `tapping' action */
220145789Speter	if (sc->config & PSM_CONFIG_FORCETAP)
220245789Speter	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
220341016Sdfr
220441016Sdfr	switch (sc->hw.model) {
220541016Sdfr
220658230Syokota	case MOUSE_MODEL_EXPLORER:
220758230Syokota	    /*
220858230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
220958230Syokota	     * byte 1:  oy ox sy sx 1  M  R  L
221058230Syokota	     * byte 2:  x  x  x  x  x  x  x  x
221158230Syokota	     * byte 3:  y  y  y  y  y  y  y  y
221258230Syokota	     * byte 4:  *  *  S2 S1 s  d2 d1 d0
221358230Syokota	     *
221458230Syokota	     * L, M, R, S1, S2: left, middle, right and side buttons
221558230Syokota	     * s: wheel data sign bit
221658230Syokota	     * d2-d0: wheel data
221758230Syokota	     */
2218123442Salfred	    z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2219123442Salfred		? (pb->ipacket[3] & 0x0f) - 16 : (pb->ipacket[3] & 0x0f);
2220123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
222158230Syokota		? MOUSE_BUTTON4DOWN : 0;
2222123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
222358230Syokota		? MOUSE_BUTTON5DOWN : 0;
222458230Syokota	    break;
222558230Syokota
222641016Sdfr	case MOUSE_MODEL_INTELLI:
222741016Sdfr	case MOUSE_MODEL_NET:
222841016Sdfr	    /* wheel data is in the fourth byte */
2229123442Salfred	    z = (char)pb->ipacket[3];
223058230Syokota	    /* some mice may send 7 when there is no Z movement?! XXX */
223158230Syokota	    if ((z >= 7) || (z <= -7))
223258230Syokota		z = 0;
223358230Syokota	    /* some compatible mice have additional buttons */
223458230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
223558230Syokota		? MOUSE_BUTTON4DOWN : 0;
223658230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
223758230Syokota		? MOUSE_BUTTON5DOWN : 0;
223841016Sdfr	    break;
223941016Sdfr
224041016Sdfr	case MOUSE_MODEL_MOUSEMANPLUS:
224148778Syokota	    /*
224248778Syokota	     * PS2++ protocl packet
224348778Syokota	     *
224448778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
224548778Syokota	     * byte 1:  *  1  p3 p2 1  *  *  *
224648778Syokota	     * byte 2:  c1 c2 p1 p0 d1 d0 1  0
224748778Syokota	     *
224848778Syokota	     * p3-p0: packet type
224948778Syokota	     * c1, c2: c1 & c2 == 1, if p2 == 0
225048778Syokota	     *         c1 & c2 == 0, if p2 == 1
225148778Syokota	     *
225248778Syokota	     * packet type: 0 (device type)
225348778Syokota	     * See comments in enable_mmanplus() below.
225448778Syokota	     *
225548778Syokota	     * packet type: 1 (wheel data)
225648778Syokota	     *
225748778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
225848778Syokota	     * byte 3:  h  *  B5 B4 s  d2 d1 d0
225948778Syokota	     *
226048778Syokota	     * h: 1, if horizontal roller data
226148778Syokota	     *    0, if vertical roller data
226248778Syokota	     * B4, B5: button 4 and 5
226348778Syokota	     * s: sign bit
226448778Syokota	     * d2-d0: roller data
226548778Syokota	     *
226648778Syokota	     * packet type: 2 (reserved)
226748778Syokota	     */
226848778Syokota	    if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
226948778Syokota		    && (abs(x) > 191)
2270123442Salfred		    && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
227141016Sdfr		/* the extended data packet encodes button and wheel events */
2272123442Salfred		switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
227348778Syokota		case 1:
227448778Syokota		    /* wheel data packet */
227548778Syokota		    x = y = 0;
2276123442Salfred		    if (pb->ipacket[2] & 0x80) {
227748778Syokota			/* horizontal roller count - ignore it XXX*/
227848778Syokota		    } else {
227948778Syokota			/* vertical roller count */
2280123442Salfred			z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2281123442Salfred			    ? (pb->ipacket[2] & 0x0f) - 16
2282123442Salfred			    : (pb->ipacket[2] & 0x0f);
228348778Syokota		    }
2284123442Salfred		    ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
228548778Syokota			? MOUSE_BUTTON4DOWN : 0;
2286123442Salfred		    ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
228748778Syokota			? MOUSE_BUTTON5DOWN : 0;
228848778Syokota		    break;
228948778Syokota		case 2:
229058230Syokota		    /* this packet type is reserved by Logitech... */
229158230Syokota		    /*
229258230Syokota		     * IBM ScrollPoint Mouse uses this packet type to
229358230Syokota		     * encode both vertical and horizontal scroll movement.
229458230Syokota		     */
229558230Syokota		    x = y = 0;
229658230Syokota		    /* horizontal count */
2297123442Salfred		    if (pb->ipacket[2] & 0x0f)
2298123442Salfred			z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
229958230Syokota		    /* vertical count */
2300123442Salfred		    if (pb->ipacket[2] & 0xf0)
2301123442Salfred			z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
230258230Syokota#if 0
230358230Syokota		    /* vertical count */
2304123442Salfred		    z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG)
2305123442Salfred			? ((pb->ipacket[2] >> 4) & 0x0f) - 16
2306123442Salfred			: ((pb->ipacket[2] >> 4) & 0x0f);
230758230Syokota		    /* horizontal count */
2308123442Salfred		    w = (pb->ipacket[2] & MOUSE_SPOINT_WNEG)
2309123442Salfred			? (pb->ipacket[2] & 0x0f) - 16
2310123442Salfred			: (pb->ipacket[2] & 0x0f);
231158230Syokota#endif
231258230Syokota		    break;
231348778Syokota		case 0:
231448778Syokota		    /* device type packet - shouldn't happen */
2315102412Scharnier		    /* FALLTHROUGH */
231648778Syokota		default:
231748778Syokota		    x = y = 0;
231848778Syokota		    ms.button = ms.obutton;
231958230Syokota		    if (bootverbose)
232058230Syokota			log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
232158230Syokota				       "0x%02x 0x%02x 0x%02x\n",
2322123442Salfred			    MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2323123442Salfred			    pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]);
232448778Syokota		    break;
232548778Syokota		}
232641016Sdfr	    } else {
232741016Sdfr		/* preserve button states */
232841016Sdfr		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
232941016Sdfr	    }
233041016Sdfr	    break;
233141016Sdfr
233241016Sdfr	case MOUSE_MODEL_GLIDEPOINT:
233341016Sdfr	    /* `tapping' action */
233441016Sdfr	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
233541016Sdfr	    break;
233641016Sdfr
233741016Sdfr	case MOUSE_MODEL_NETSCROLL:
233858230Syokota	    /* three addtional bytes encode buttons and wheel events */
2339123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
234041016Sdfr		? MOUSE_BUTTON4DOWN : 0;
2341123442Salfred	    ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
234258230Syokota		? MOUSE_BUTTON5DOWN : 0;
2343123442Salfred	    z = (pb->ipacket[3] & MOUSE_PS2_XNEG)
2344123442Salfred		? pb->ipacket[4] - 256 : pb->ipacket[4];
234541016Sdfr	    break;
234641016Sdfr
234741016Sdfr	case MOUSE_MODEL_THINK:
234841016Sdfr	    /* the fourth button state in the first byte */
234941016Sdfr	    ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
235041016Sdfr	    break;
235141016Sdfr
235249965Syokota	case MOUSE_MODEL_VERSAPAD:
235349965Syokota	    /* VersaPad PS/2 absolute mode message format
235449965Syokota	     *
235549965Syokota	     * [packet1]     7   6   5   4   3   2   1   0(LSB)
235649965Syokota	     *  ipacket[0]:  1   1   0   A   1   L   T   R
235749965Syokota	     *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
235849965Syokota	     *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
235949965Syokota	     *  ipacket[3]:  1   1   1   A   1   L   T   R
236049965Syokota	     *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
236149965Syokota	     *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
236249965Syokota	     *
236349965Syokota	     * [note]
236449965Syokota	     *  R: right physical mouse button (1=on)
236549965Syokota	     *  T: touch pad virtual button (1=tapping)
236649965Syokota	     *  L: left physical mouse button (1=on)
236749965Syokota	     *  A: position data is valid (1=valid)
236849965Syokota	     *  H: horizontal data (12bit signed integer. H11 is sign bit.)
236949965Syokota	     *  V: vertical data (12bit signed integer. V11 is sign bit.)
237049965Syokota	     *  P: pressure data
237149965Syokota	     *
237249965Syokota	     * Tapping is mapped to MOUSE_BUTTON4.
237349965Syokota	     */
237449965Syokota	    ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
237549965Syokota	    ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
237649965Syokota	    x = y = 0;
237749965Syokota	    if (c & MOUSE_PS2VERSA_IN_USE) {
2378123442Salfred		x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
2379123442Salfred		y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
238049965Syokota		if (x0 & 0x800)
238149965Syokota		    x0 -= 0x1000;
238249965Syokota		if (y0 & 0x800)
238349965Syokota		    y0 -= 0x1000;
238449965Syokota		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
238549965Syokota		    x = sc->xold - x0;
238649965Syokota		    y = y0 - sc->yold;
238749965Syokota		    if (x < 0)	/* XXX */
238849965Syokota			x++;
238949965Syokota		    else if (x)
239049965Syokota			x--;
239149965Syokota		    if (y < 0)
239249965Syokota			y++;
239349965Syokota		    else if (y)
239449965Syokota			y--;
239549965Syokota		} else {
239649965Syokota		    sc->flags |= PSM_FLAGS_FINGERDOWN;
239749965Syokota		}
239849965Syokota		sc->xold = x0;
239949965Syokota		sc->yold = y0;
240049965Syokota	    } else {
240149965Syokota		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
240249965Syokota	    }
240349965Syokota	    c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
240449965Syokota		| ((y < 0) ? MOUSE_PS2_YNEG : 0);
240549965Syokota	    break;
240649965Syokota
240758230Syokota	case MOUSE_MODEL_4D:
240858230Syokota	    /*
240958230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
241058230Syokota	     * byte 1:  s2 d2 s1 d1 1  M  R  L
241158230Syokota	     * byte 2:  sx x  x  x  x  x  x  x
241258230Syokota	     * byte 3:  sy y  y  y  y  y  y  y
241358230Syokota	     *
241458230Syokota	     * s1: wheel 1 direction
241558230Syokota	     * d1: wheel 1 data
241658230Syokota	     * s2: wheel 2 direction
241758230Syokota	     * d2: wheel 2 data
241858230Syokota	     */
2419123442Salfred	    x = (pb->ipacket[1] & 0x80) ? pb->ipacket[1] - 256 : pb->ipacket[1];
2420123442Salfred	    y = (pb->ipacket[2] & 0x80) ? pb->ipacket[2] - 256 : pb->ipacket[2];
242158230Syokota	    switch (c & MOUSE_4D_WHEELBITS) {
242258230Syokota	    case 0x10:
242358230Syokota		z = 1;
242458230Syokota		break;
242558230Syokota	    case 0x30:
242658230Syokota		z = -1;
242758230Syokota		break;
242858230Syokota	    case 0x40:	/* 2nd wheel turning right XXX */
242958230Syokota		z = 2;
243058230Syokota		break;
243158230Syokota	    case 0xc0:	/* 2nd wheel turning left XXX */
243258230Syokota		z = -2;
243358230Syokota		break;
243458230Syokota	    }
243558230Syokota	    break;
243658230Syokota
243758230Syokota	case MOUSE_MODEL_4DPLUS:
243858230Syokota	    if ((x < 16 - 256) && (y < 16 - 256)) {
243958230Syokota		/*
244058230Syokota		 *          b7 b6 b5 b4 b3 b2 b1 b0
244158230Syokota		 * byte 1:  0  0  1  1  1  M  R  L
244258230Syokota		 * byte 2:  0  0  0  0  1  0  0  0
244358230Syokota		 * byte 3:  0  0  0  0  S  s  d1 d0
244458230Syokota		 *
244558230Syokota		 * L, M, R, S: left, middle, right and side buttons
244658230Syokota		 * s: wheel data sign bit
244758230Syokota		 * d1-d0: wheel data
244858230Syokota		 */
244958230Syokota		x = y = 0;
2450123442Salfred		if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
245158230Syokota		    ms.button |= MOUSE_BUTTON4DOWN;
2452123442Salfred		z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2453123442Salfred			? ((pb->ipacket[2] & 0x07) - 8)
2454123442Salfred			: (pb->ipacket[2] & 0x07) ;
245558230Syokota	    } else {
245658230Syokota		/* preserve previous button states */
245758230Syokota		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
245858230Syokota	    }
245958230Syokota	    break;
246058230Syokota
2461132865Snjl	case MOUSE_MODEL_SYNAPTICS:
2462132865Snjl	    /* TouchPad PS/2 absolute mode message format
2463132865Snjl	     *
2464132865Snjl	     *  Bits:        7   6   5   4   3   2   1   0 (LSB)
2465132865Snjl	     *  ------------------------------------------------
2466132865Snjl	     *  ipacket[0]:  1   0  W3  W2   0  W1   R   L
2467132865Snjl	     *  ipacket[1]: Yb  Ya  Y9  Y8  Xb  Xa  X9  X8
2468132865Snjl	     *  ipacket[2]: Z7  Z6  Z5  Z4  Z3  Z2  Z1  Z0
2469132865Snjl	     *  ipacket[3]:  1   1  Yc  Xc   0  W0   D   U
2470132865Snjl	     *  ipacket[4]: X7  X6  X5  X4  X3  X2  X1  X0
2471132865Snjl	     *  ipacket[5]: Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
2472132865Snjl	     *
2473132865Snjl	     * Legend:
2474132865Snjl	     *  L: left physical mouse button
2475132865Snjl	     *  R: right physical mouse button
2476132865Snjl	     *  D: down button
2477132865Snjl	     *  U: up button
2478132865Snjl	     *  W: "wrist" value
2479132865Snjl	     *  X: x position
2480132865Snjl	     *  Y: x position
2481132865Snjl	     *  Z: pressure
2482132865Snjl	     *
2483132865Snjl	     * Absolute reportable limits:    0 - 6143.
2484132865Snjl	     * Typical bezel limits:       1472 - 5472.
2485132865Snjl	     * Typical edge marings:       1632 - 5312.
2486132865Snjl	     *
2487132865Snjl	     * w = 3 Passthrough Packet
2488132865Snjl	     *
2489132865Snjl	     * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2490132865Snjl	     */
2491132865Snjl
2492132865Snjl	    /* Sanity check for out of sync packets. */
2493132865Snjl	    if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2494132865Snjl		(pb->ipacket[3] & 0xc8) != 0xc0)
2495132865Snjl		continue;
2496132865Snjl
2497132865Snjl	    x = y = x0 = y0 = 0;
2498132865Snjl
2499132865Snjl	    /* Pressure value. */
2500132865Snjl	    z = pb->ipacket[2];
2501132865Snjl	    w = ((pb->ipacket[0] & 0x30) >> 2) |
2502132865Snjl		((pb->ipacket[0] & 0x04) >> 1) |
2503132865Snjl		((pb->ipacket[3] & 0x04) >> 2);
2504132865Snjl
2505132865Snjl	    ms.button = 0;
2506132865Snjl	    if (pb->ipacket[0] & 0x01)
2507132865Snjl		ms.button |= MOUSE_BUTTON1DOWN;
2508132865Snjl	    if (pb->ipacket[0] & 0x02)
2509132865Snjl		ms.button |= MOUSE_BUTTON3DOWN;
2510132865Snjl
2511132865Snjl	    if ((pb->ipacket[3] & 0x01) && (pb->ipacket[0] & 0x01) == 0)
2512132865Snjl		ms.button |= MOUSE_BUTTON2DOWN;
2513132865Snjl	    if ((pb->ipacket[3] & 0x02) && (pb->ipacket[0] & 0x02) == 0)
2514132865Snjl		ms.button |= MOUSE_BUTTON4DOWN;
2515132865Snjl
2516132865Snjl	    /* There is a finger on the pad. */
2517132865Snjl	    if ((w >= 4 && w <= 7) && (z >= 16 && z < 200)) {
2518132865Snjl		x0 = ((pb->ipacket[3] & 0x10) << 8) |
2519132865Snjl		    ((pb->ipacket[1] & 0x0f) << 8) |
2520132865Snjl		    pb->ipacket[4];
2521132865Snjl		y0 = ((pb->ipacket[3] & 0x20) << 7) |
2522132865Snjl		    ((pb->ipacket[1] & 0xf0) << 4) |
2523132865Snjl		    pb->ipacket[5];
2524132865Snjl
2525132865Snjl		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2526132865Snjl		    x0 = (x0 + sc->xold * 3) / 4;
2527132865Snjl		    y0 = (y0 + sc->yold * 3) / 4;
2528132865Snjl
2529132865Snjl		    x = (x0 - sc->xold) / 4;
2530132865Snjl		    y = (y0 - sc->yold) / 4;
2531132865Snjl		} else {
2532132865Snjl		    sc->flags |= PSM_FLAGS_FINGERDOWN;
2533132865Snjl		}
2534132865Snjl
2535132865Snjl		sc->xold = x0;
2536132865Snjl		sc->yold = y0;
2537132865Snjl	    } else {
2538132865Snjl		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2539132865Snjl	    }
2540132865Snjl	    z = 0;
2541132865Snjl	    break;
2542132865Snjl
254341016Sdfr	case MOUSE_MODEL_GENERIC:
254441016Sdfr	default:
254541016Sdfr	    break;
254641016Sdfr	}
254741016Sdfr
254841016Sdfr        /* scale values */
254941016Sdfr        if (sc->mode.accelfactor >= 1) {
255041016Sdfr            if (x != 0) {
255141016Sdfr                x = x * x / sc->mode.accelfactor;
255241016Sdfr                if (x == 0)
255341016Sdfr                    x = 1;
255441016Sdfr                if (c & MOUSE_PS2_XNEG)
255541016Sdfr                    x = -x;
255641016Sdfr            }
255741016Sdfr            if (y != 0) {
255841016Sdfr                y = y * y / sc->mode.accelfactor;
255941016Sdfr                if (y == 0)
256041016Sdfr                    y = 1;
256141016Sdfr                if (c & MOUSE_PS2_YNEG)
256241016Sdfr                    y = -y;
256341016Sdfr            }
256441016Sdfr        }
256541016Sdfr
256641016Sdfr        ms.dx = x;
256741016Sdfr        ms.dy = y;
256841016Sdfr        ms.dz = z;
256941016Sdfr        ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
257041016Sdfr	    | (ms.obutton ^ ms.button);
257141016Sdfr
257241016Sdfr	if (sc->mode.level < PSM_LEVEL_NATIVE)
2573123442Salfred	    pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
257441016Sdfr
257541016Sdfr        sc->status.flags |= ms.flags;
257641016Sdfr        sc->status.dx += ms.dx;
257741016Sdfr        sc->status.dy += ms.dy;
257841016Sdfr        sc->status.dz += ms.dz;
257941016Sdfr        sc->status.button = ms.button;
258041016Sdfr        sc->button = ms.button;
258141016Sdfr
258258230Syokota	sc->watchdog = FALSE;
258358230Syokota
258441016Sdfr        /* queue data */
2585123442Salfred        if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
2586123442Salfred	    l = imin(pb->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2587123442Salfred	    bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2588123442Salfred	    if (pb->inputbytes > l)
2589123442Salfred	        bcopy(&pb->ipacket[l], &sc->queue.buf[0], pb->inputbytes - l);
259041016Sdfr            sc->queue.tail =
2591123442Salfred		(sc->queue.tail + pb->inputbytes) % sizeof(sc->queue.buf);
2592123442Salfred            sc->queue.count += pb->inputbytes;
259341016Sdfr	}
2594123442Salfred        pb->inputbytes = 0;
259541016Sdfr
2596123442Salfred	if (++sc->pqueue_start >= PSM_PACKETQUEUE)
2597123442Salfred		sc->pqueue_start = 0;
2598123442Salfred    } while (sc->pqueue_start != sc->pqueue_end);
2599123442Salfred    if (sc->state & PSM_ASLP) {
2600123442Salfred        sc->state &= ~PSM_ASLP;
2601123442Salfred        wakeup( sc);
260241016Sdfr    }
2603123442Salfred    selwakeuppri(&sc->rsel, PZERO);
2604123442Salfred    sc->state &= ~PSM_SOFTARMED;
2605123442Salfred    splx(s);
260641016Sdfr}
260741016Sdfr
260841016Sdfrstatic int
2609130585Sphkpsmpoll(struct cdev *dev, int events, struct thread *td)
261041016Sdfr{
261141016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
261241016Sdfr    int s;
261341016Sdfr    int revents = 0;
261441016Sdfr
261541016Sdfr    /* Return true if a mouse event available */
261641016Sdfr    s = spltty();
261745789Speter    if (events & (POLLIN | POLLRDNORM)) {
261841016Sdfr	if (sc->queue.count > 0)
261941016Sdfr	    revents |= events & (POLLIN | POLLRDNORM);
262041016Sdfr	else
262183366Sjulian	    selrecord(td, &sc->rsel);
262245789Speter    }
262341016Sdfr    splx(s);
262441016Sdfr
262541016Sdfr    return (revents);
262641016Sdfr}
262741016Sdfr
262841016Sdfr/* vendor/model specific routines */
262941016Sdfr
263041016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
263141016Sdfr{
263241016Sdfr    if (set_mouse_resolution(kbdc, res) != res)
263341016Sdfr        return FALSE;
263441016Sdfr    if (set_mouse_scaling(kbdc, scale)
263541016Sdfr	&& set_mouse_scaling(kbdc, scale)
263641016Sdfr	&& set_mouse_scaling(kbdc, scale)
263741016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3))
263841016Sdfr	return TRUE;
263941016Sdfr    return FALSE;
264041016Sdfr}
264141016Sdfr
264269438Syokotastatic int
264369438Syokotamouse_ext_command(KBDC kbdc, int command)
264469438Syokota{
264569438Syokota    int c;
264669438Syokota
264769438Syokota    c = (command >> 6) & 0x03;
264869438Syokota    if (set_mouse_resolution(kbdc, c) != c)
264969438Syokota	return FALSE;
265069438Syokota    c = (command >> 4) & 0x03;
265169438Syokota    if (set_mouse_resolution(kbdc, c) != c)
265269438Syokota	return FALSE;
265369438Syokota    c = (command >> 2) & 0x03;
265469438Syokota    if (set_mouse_resolution(kbdc, c) != c)
265569438Syokota	return FALSE;
265669438Syokota    c = (command >> 0) & 0x03;
265769438Syokota    if (set_mouse_resolution(kbdc, c) != c)
265869438Syokota	return FALSE;
265969438Syokota    return TRUE;
266069438Syokota}
266169438Syokota
266241016Sdfr#if notyet
266341016Sdfr/* Logitech MouseMan Cordless II */
266441016Sdfrstatic int
266541016Sdfrenable_lcordless(struct psm_softc *sc)
266641016Sdfr{
266741016Sdfr    int status[3];
266841016Sdfr    int ch;
266941016Sdfr
267041016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
267141016Sdfr        return FALSE;
267241016Sdfr    if (status[1] == PSMD_RES_HIGH)
267341016Sdfr	return FALSE;
267441016Sdfr    ch = (status[0] & 0x07) - 1;	/* channel # */
267541016Sdfr    if ((ch <= 0) || (ch > 4))
267641016Sdfr	return FALSE;
267741016Sdfr    /*
267841016Sdfr     * status[1]: always one?
267941016Sdfr     * status[2]: battery status? (0-100)
268041016Sdfr     */
268141016Sdfr    return TRUE;
268241016Sdfr}
268341016Sdfr#endif /* notyet */
268441016Sdfr
268558230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
268641016Sdfrstatic int
268741016Sdfrenable_groller(struct psm_softc *sc)
268841016Sdfr{
268941016Sdfr    int status[3];
269041016Sdfr
269141016Sdfr    /*
269241016Sdfr     * The special sequence to enable the fourth button and the
269341016Sdfr     * roller. Immediately after this sequence check status bytes.
269441016Sdfr     * if the mouse is NetScroll, the second and the third bytes are
269541016Sdfr     * '3' and 'D'.
269641016Sdfr     */
269741016Sdfr
269841016Sdfr    /*
269941016Sdfr     * If the mouse is an ordinary PS/2 mouse, the status bytes should
270041016Sdfr     * look like the following.
270141016Sdfr     *
270241016Sdfr     * byte 1 bit 7 always 0
270341016Sdfr     *        bit 6 stream mode (0)
270441016Sdfr     *        bit 5 disabled (0)
270541016Sdfr     *        bit 4 1:1 scaling (0)
270641016Sdfr     *        bit 3 always 0
270741016Sdfr     *        bit 0-2 button status
270841016Sdfr     * byte 2 resolution (PSMD_RES_HIGH)
270941016Sdfr     * byte 3 report rate (?)
271041016Sdfr     */
271141016Sdfr
271241016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
271341016Sdfr        return FALSE;
271441016Sdfr    if ((status[1] != '3') || (status[2] != 'D'))
271541016Sdfr        return FALSE;
271658230Syokota    /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
271741016Sdfr    sc->hw.buttons = 4;
271841016Sdfr    return TRUE;
271941016Sdfr}
272041016Sdfr
272158230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
272241016Sdfrstatic int
272341016Sdfrenable_gmouse(struct psm_softc *sc)
272441016Sdfr{
272541016Sdfr    int status[3];
272641016Sdfr
272741016Sdfr    /*
272841016Sdfr     * The special sequence to enable the middle, "rubber" button.
272941016Sdfr     * Immediately after this sequence check status bytes.
273041016Sdfr     * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
273141016Sdfr     * the second and the third bytes are '3' and 'U'.
273241016Sdfr     * NOTE: NetMouse reports that it has three buttons although it has
273341016Sdfr     * two buttons and a rubber button. NetMouse Pro and MIE Mouse
273441016Sdfr     * say they have three buttons too and they do have a button on the
273541016Sdfr     * side...
273641016Sdfr     */
273741016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
273841016Sdfr        return FALSE;
273941016Sdfr    if ((status[1] != '3') || (status[2] != 'U'))
274041016Sdfr        return FALSE;
274141016Sdfr    return TRUE;
274241016Sdfr}
274341016Sdfr
274441016Sdfr/* ALPS GlidePoint */
274541016Sdfrstatic int
274641016Sdfrenable_aglide(struct psm_softc *sc)
274741016Sdfr{
274841016Sdfr    int status[3];
274941016Sdfr
275041016Sdfr    /*
275141016Sdfr     * The special sequence to obtain ALPS GlidePoint specific
275241016Sdfr     * information. Immediately after this sequence, status bytes will
275341016Sdfr     * contain something interesting.
275441016Sdfr     * NOTE: ALPS produces several models of GlidePoint. Some of those
275541016Sdfr     * do not respond to this sequence, thus, cannot be detected this way.
275641016Sdfr     */
275750149Syokota    if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
275850149Syokota	return FALSE;
275941016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
276041016Sdfr        return FALSE;
276150149Syokota    if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
276241016Sdfr        return FALSE;
276341016Sdfr    return TRUE;
276441016Sdfr}
276541016Sdfr
276641016Sdfr/* Kensington ThinkingMouse/Trackball */
276741016Sdfrstatic int
276841016Sdfrenable_kmouse(struct psm_softc *sc)
276941016Sdfr{
277041016Sdfr    static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
277141016Sdfr    KBDC kbdc = sc->kbdc;
277241016Sdfr    int status[3];
277341016Sdfr    int id1;
277441016Sdfr    int id2;
277541016Sdfr    int i;
277641016Sdfr
277741016Sdfr    id1 = get_aux_id(kbdc);
277841016Sdfr    if (set_mouse_sampling_rate(kbdc, 10) != 10)
277941016Sdfr	return FALSE;
278041016Sdfr    /*
278141016Sdfr     * The device is now in the native mode? It returns a different
278241016Sdfr     * ID value...
278341016Sdfr     */
278441016Sdfr    id2 = get_aux_id(kbdc);
278541016Sdfr    if ((id1 == id2) || (id2 != 2))
278641016Sdfr	return FALSE;
278741016Sdfr
278841016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
278941016Sdfr        return FALSE;
279041016Sdfr#if PSM_DEBUG >= 2
279141016Sdfr    /* at this point, resolution is LOW, sampling rate is 10/sec */
279241016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
279341016Sdfr        return FALSE;
279441016Sdfr#endif
279541016Sdfr
279641016Sdfr    /*
279741016Sdfr     * The special sequence to enable the third and fourth buttons.
279841016Sdfr     * Otherwise they behave like the first and second buttons.
279941016Sdfr     */
280041016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
280141016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
280241016Sdfr	    return FALSE;
280341016Sdfr    }
280441016Sdfr
280541016Sdfr    /*
280641016Sdfr     * At this point, the device is using default resolution and
280741016Sdfr     * sampling rate for the native mode.
280841016Sdfr     */
280941016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
281041016Sdfr        return FALSE;
281141016Sdfr    if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
281241016Sdfr        return FALSE;
281341016Sdfr
281441016Sdfr    /* the device appears be enabled by this sequence, diable it for now */
281541016Sdfr    disable_aux_dev(kbdc);
281641016Sdfr    empty_aux_buffer(kbdc, 5);
281741016Sdfr
281841016Sdfr    return TRUE;
281941016Sdfr}
282041016Sdfr
282158230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
282241016Sdfrstatic int
282341016Sdfrenable_mmanplus(struct psm_softc *sc)
282441016Sdfr{
282541016Sdfr    KBDC kbdc = sc->kbdc;
282641016Sdfr    int data[3];
282741016Sdfr
282841016Sdfr    /* the special sequence to enable the fourth button and the roller. */
282958230Syokota    /*
283058230Syokota     * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
283158230Syokota     * must be called exactly three times since the last RESET command
283258230Syokota     * before this sequence. XXX
283358230Syokota     */
283469438Syokota    if (!set_mouse_scaling(kbdc, 1))
283569438Syokota	return FALSE;
283669438Syokota    if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
283769438Syokota	return FALSE;
283841016Sdfr    if (get_mouse_status(kbdc, data, 1, 3) < 3)
283941016Sdfr        return FALSE;
284041016Sdfr
284148778Syokota    /*
284248778Syokota     * PS2++ protocl, packet type 0
284341016Sdfr     *
284448778Syokota     *          b7 b6 b5 b4 b3 b2 b1 b0
284548778Syokota     * byte 1:  *  1  p3 p2 1  *  *  *
284648778Syokota     * byte 2:  1  1  p1 p0 m1 m0 1  0
284748778Syokota     * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
284848778Syokota     *
284948778Syokota     * p3-p0: packet type: 0
285058230Syokota     * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
285141016Sdfr     */
285248778Syokota    /* check constant bits */
285348778Syokota    if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
285441016Sdfr        return FALSE;
285548778Syokota    if ((data[1] & 0xc3) != 0xc2)
285648778Syokota        return FALSE;
285748778Syokota    /* check d3-d0 in byte 2 */
285848778Syokota    if (!MOUSE_PS2PLUS_CHECKBITS(data))
285948778Syokota        return FALSE;
286048778Syokota    /* check p3-p0 */
286148778Syokota    if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
286248778Syokota        return FALSE;
286341016Sdfr
286448778Syokota    sc->hw.hwid &= 0x00ff;
286548778Syokota    sc->hw.hwid |= data[2] << 8;	/* save model ID */
286648778Syokota
286741016Sdfr    /*
286841016Sdfr     * MouseMan+ (or FirstMouse+) is now in its native mode, in which
286941016Sdfr     * the wheel and the fourth button events are encoded in the
287041016Sdfr     * special data packet. The mouse may be put in the IntelliMouse mode
287141016Sdfr     * if it is initialized by the IntelliMouse's method.
287241016Sdfr     */
287341016Sdfr    return TRUE;
287441016Sdfr}
287541016Sdfr
287658230Syokota/* MS IntelliMouse Explorer */
287758230Syokotastatic int
287858230Syokotaenable_msexplorer(struct psm_softc *sc)
287958230Syokota{
288058923Syokota    static unsigned char rate0[] = { 200, 100, 80, };
288158923Syokota    static unsigned char rate1[] = { 200, 200, 80, };
288258230Syokota    KBDC kbdc = sc->kbdc;
288358230Syokota    int id;
288458230Syokota    int i;
288558230Syokota
288669438Syokota    /* the special sequence to enable the extra buttons and the roller. */
288769438Syokota    for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
288869438Syokota        if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
288969438Syokota	    return FALSE;
289069438Syokota    }
289169438Syokota    /* the device will give the genuine ID only after the above sequence */
289269438Syokota    id = get_aux_id(kbdc);
289369438Syokota    if (id != PSM_EXPLORER_ID)
289469438Syokota	return FALSE;
289569438Syokota
289669438Syokota    sc->hw.hwid = id;
289769438Syokota    sc->hw.buttons = 5;		/* IntelliMouse Explorer XXX */
289869438Syokota
289958923Syokota    /*
290058923Syokota     * XXX: this is a kludge to fool some KVM switch products
290158923Syokota     * which think they are clever enough to know the 4-byte IntelliMouse
290258923Syokota     * protocol, and assume any other protocols use 3-byte packets.
290358923Syokota     * They don't convey 4-byte data packets from the IntelliMouse Explorer
290458923Syokota     * correctly to the host computer because of this!
290558923Syokota     * The following sequence is actually IntelliMouse's "wake up"
290658923Syokota     * sequence; it will make the KVM think the mouse is IntelliMouse
290758923Syokota     * when it is in fact IntelliMouse Explorer.
290858923Syokota     */
290958923Syokota    for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
291058923Syokota        if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
291169438Syokota	    break;
291258923Syokota    }
291358923Syokota    id = get_aux_id(kbdc);
291458923Syokota
291558230Syokota    return TRUE;
291658230Syokota}
291758230Syokota
291841016Sdfr/* MS IntelliMouse */
291941016Sdfrstatic int
292041016Sdfrenable_msintelli(struct psm_softc *sc)
292141016Sdfr{
292241016Sdfr    /*
292341016Sdfr     * Logitech MouseMan+ and FirstMouse+ will also respond to this
292441016Sdfr     * probe routine and act like IntelliMouse.
292541016Sdfr     */
292641016Sdfr
292741016Sdfr    static unsigned char rate[] = { 200, 100, 80, };
292841016Sdfr    KBDC kbdc = sc->kbdc;
292941016Sdfr    int id;
293041016Sdfr    int i;
293141016Sdfr
293241016Sdfr    /* the special sequence to enable the third button and the roller. */
293341016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
293441016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
293541016Sdfr	    return FALSE;
293641016Sdfr    }
293741016Sdfr    /* the device will give the genuine ID only after the above sequence */
293841016Sdfr    id = get_aux_id(kbdc);
293941016Sdfr    if (id != PSM_INTELLI_ID)
294041016Sdfr	return FALSE;
294141016Sdfr
294241016Sdfr    sc->hw.hwid = id;
294341016Sdfr    sc->hw.buttons = 3;
294441016Sdfr
294541016Sdfr    return TRUE;
294641016Sdfr}
294741016Sdfr
294858230Syokota/* A4 Tech 4D Mouse */
294958230Syokotastatic int
295058230Syokotaenable_4dmouse(struct psm_softc *sc)
295158230Syokota{
295258230Syokota    /*
295358230Syokota     * Newer wheel mice from A4 Tech may use the 4D+ protocol.
295458230Syokota     */
295558230Syokota
295658230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
295758230Syokota    KBDC kbdc = sc->kbdc;
295858230Syokota    int id;
295958230Syokota    int i;
296058230Syokota
296158230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
296258230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
296358230Syokota	    return FALSE;
296458230Syokota    }
296558230Syokota    id = get_aux_id(kbdc);
296658230Syokota    /*
296758923Syokota     * WinEasy 4D, 4 Way Scroll 4D: 6
296858230Syokota     * Cable-Free 4D: 8 (4DPLUS)
296958923Syokota     * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
297058230Syokota     */
297158230Syokota    if (id != PSM_4DMOUSE_ID)
297258230Syokota	return FALSE;
297358230Syokota
297458230Syokota    sc->hw.hwid = id;
297558230Syokota    sc->hw.buttons = 3;		/* XXX some 4D mice have 4? */
297658230Syokota
297758230Syokota    return TRUE;
297858230Syokota}
297958230Syokota
298058230Syokota/* A4 Tech 4D+ Mouse */
298158230Syokotastatic int
298258230Syokotaenable_4dplus(struct psm_softc *sc)
298358230Syokota{
298458230Syokota    /*
298558230Syokota     * Newer wheel mice from A4 Tech seem to use this protocol.
298658230Syokota     * Older models are recognized as either 4D Mouse or IntelliMouse.
298758230Syokota     */
298858230Syokota    KBDC kbdc = sc->kbdc;
298958230Syokota    int id;
299058230Syokota
299158230Syokota    /*
299258230Syokota     * enable_4dmouse() already issued the following ID sequence...
299358230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
299458230Syokota    int i;
299558230Syokota
299658230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
299758230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
299858230Syokota	    return FALSE;
299958230Syokota    }
300058230Syokota    */
300158230Syokota
300258230Syokota    id = get_aux_id(kbdc);
3003117478Smikeh    switch (id) {
3004117478Smikeh    case PSM_4DPLUS_ID:
3005117478Smikeh	    sc->hw.buttons = 4;
3006117478Smikeh	    break;
3007117478Smikeh    case PSM_4DPLUS_RFSW35_ID:
3008117478Smikeh	    sc->hw.buttons = 3;
3009117478Smikeh	    break;
3010117478Smikeh    default:
3011117478Smikeh	    return FALSE;
3012117478Smikeh    }
301358230Syokota
301458230Syokota    sc->hw.hwid = id;
301558230Syokota
301658230Syokota    return TRUE;
301758230Syokota}
301858230Syokota
3019132865Snjl/* Synaptics Touchpad */
3020132865Snjlstatic int
3021132865Snjlenable_synaptics(struct psm_softc *sc)
3022132865Snjl{
3023132865Snjl    int status[3];
3024132865Snjl    KBDC kbdc;
3025132865Snjl
3026132865Snjl    kbdc = sc->kbdc;
3027132865Snjl    disable_aux_dev(kbdc);
3028132865Snjl
3029132865Snjl    /* Just to be on the safe side */
3030132865Snjl    set_mouse_scaling(kbdc, 1);
3031132865Snjl
3032133295Sphilip    /* Identify the Touchpad version */
3033132865Snjl    if (mouse_ext_command(kbdc, 0) == 0)
3034132865Snjl	return (FALSE);
3035132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3036132865Snjl	return (FALSE);
3037132865Snjl    if (status[1] != 0x47)
3038132865Snjl	return (FALSE);
3039132865Snjl
3040132865Snjl    sc->synhw.infoMinor = status[0];
3041132865Snjl    sc->synhw.infoMajor = status[2] & 0x0f;
3042133295Sphilip
3043133295Sphilip    if (verbose >= 2)
3044133295Sphilip	printf("Synaptics Touchpad v%d.%d\n",
3045133295Sphilip	    sc->synhw.infoMajor, sc->synhw.infoMinor);
3046133295Sphilip
3047132865Snjl    if (sc->synhw.infoMajor < 4) {
3048133295Sphilip	printf("  Unsupported (pre-v4) Touchpad detected\n");
3049132865Snjl	return (FALSE);
3050132865Snjl    }
3051132865Snjl
3052133295Sphilip    /* Get the Touchpad model information */
3053132865Snjl    if (mouse_ext_command(kbdc, 3) == 0)
3054132865Snjl	return (FALSE);
3055132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3056132865Snjl	return (FALSE);
3057132865Snjl    if ((status[1] & 0x01) != 0) {
3058133295Sphilip	printf("  Failed to read model information\n");
3059132865Snjl	return (FALSE);
3060132865Snjl    }
3061132865Snjl
3062132865Snjl    sc->synhw.infoRot180   = (status[0] & 0x80) >> 7;
3063132865Snjl    sc->synhw.infoPortrait = (status[0] & 0x40) >> 6;
3064132865Snjl    sc->synhw.infoSensor   =  status[0] & 0x3f;
3065132865Snjl    sc->synhw.infoHardware = (status[1] & 0xfe) >> 1;
3066132865Snjl    sc->synhw.infoNewAbs   = (status[2] & 0x80) >> 7;
3067132865Snjl    sc->synhw.capPen       = (status[2] & 0x40) >> 6;
3068132865Snjl    sc->synhw.infoSimplC   = (status[2] & 0x20) >> 5;
3069132865Snjl    sc->synhw.infoGeometry =  status[2] & 0x0f;
3070133295Sphilip
3071132865Snjl    if (verbose >= 2) {
3072133295Sphilip	printf("  Model information:\n");
3073133295Sphilip	printf("   infoRot180: %d\n", sc->synhw.infoRot180);
3074133295Sphilip	printf("   infoPortrait: %d\n", sc->synhw.infoPortrait);
3075133295Sphilip	printf("   infoSensor: %d\n", sc->synhw.infoSensor);
3076133295Sphilip	printf("   infoHardware: %d\n", sc->synhw.infoHardware);
3077133295Sphilip	printf("   infoNewAbs: %d\n", sc->synhw.infoNewAbs);
3078133295Sphilip	printf("   capPen: %d\n", sc->synhw.capPen);
3079133295Sphilip	printf("   infoSimplC: %d\n", sc->synhw.infoSimplC);
3080133295Sphilip	printf("   infoGeometry: %d\n", sc->synhw.infoGeometry);
3081132865Snjl    }
3082132865Snjl
3083133295Sphilip    /* Read the extended capability bits */
3084132865Snjl    if (mouse_ext_command(kbdc, 2) == 0)
3085132865Snjl	return (FALSE);
3086132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3087132865Snjl	return (FALSE);
3088132865Snjl    if (status[1] != 0x47) {
3089133295Sphilip	printf("  Failed to read extended capability bits\n");
3090132865Snjl	return (FALSE);
3091132865Snjl    }
3092132865Snjl
3093133295Sphilip    /* Set the different capabilities when they exist */
3094133295Sphilip    if ((status[0] & 0x80) >> 7) {
3095133295Sphilip	sc->synhw.capExtended    = (status[0] & 0x80) >> 7;
3096133295Sphilip    	sc->synhw.capPassthrough = (status[2] & 0x80) >> 7;
3097133295Sphilip    	sc->synhw.capSleep       = (status[2] & 0x10) >> 4;
3098133295Sphilip    	sc->synhw.capFourButtons = (status[2] & 0x08) >> 3;
3099133295Sphilip    	sc->synhw.capMultiFinger = (status[2] & 0x02) >> 1;
3100133295Sphilip    	sc->synhw.capPalmDetect  = (status[2] & 0x01);
3101133295Sphilip
3102133295Sphilip	if (verbose >= 2) {
3103133295Sphilip	    printf("  Extended capabilities:\n");
3104133295Sphilip	    printf("   capExtended: %d\n", sc->synhw.capExtended);
3105133295Sphilip	    printf("   capPassthrough: %d\n", sc->synhw.capPassthrough);
3106133295Sphilip	    printf("   capSleep: %d\n", sc->synhw.capSleep);
3107133295Sphilip	    printf("   capFourButtons: %d\n", sc->synhw.capFourButtons);
3108133295Sphilip	    printf("   capMultiFinger: %d\n", sc->synhw.capMultiFinger);
3109133295Sphilip	    printf("   capPalmDetect: %d\n", sc->synhw.capPalmDetect);
3110133295Sphilip	}
3111133295Sphilip    } else {
3112133295Sphilip	sc->synhw.capExtended = 0;
3113133295Sphilip
3114133295Sphilip	if (verbose >= 2)
3115133295Sphilip	    printf("  No extended capabilities\n");
3116133295Sphilip    }
3117133295Sphilip
3118133295Sphilip    /*
3119133295Sphilip     * Read the mode byte
3120133295Sphilip     *
3121133295Sphilip     * XXX: Note the Synaptics documentation also defines the first
3122133295Sphilip     * byte of the response to this query to be a constant 0x3b, this
3123133295Sphilip     * does not appear to be true for Touchpads with guest devices.
3124133295Sphilip     */
3125132865Snjl    if (mouse_ext_command(kbdc, 1) == 0)
3126132865Snjl	return (FALSE);
3127132865Snjl    if (get_mouse_status(kbdc, status, 0, 3) != 3)
3128132865Snjl	return (FALSE);
3129133295Sphilip    if (status[1] != 0x47) {
3130133295Sphilip	printf("  Failed to read mode byte\n");
3131132865Snjl	return (FALSE);
3132132865Snjl    }
3133132865Snjl
3134133295Sphilip    /* Set the mode byte -- request wmode where available */
3135133295Sphilip    if (sc->synhw.capExtended)
3136133295Sphilip	mouse_ext_command(kbdc, 0xc1);
3137133295Sphilip    else
3138133295Sphilip	mouse_ext_command(kbdc, 0xc0);
3139132865Snjl
3140133295Sphilip    /* Reset the sampling rate */
3141132865Snjl    set_mouse_sampling_rate(kbdc, 20);
3142132865Snjl
3143132865Snjl    return (TRUE);
3144132865Snjl}
3145133295Sphilip
314649965Syokota/* Interlink electronics VersaPad */
314749965Syokotastatic int
314849965Syokotaenable_versapad(struct psm_softc *sc)
314949965Syokota{
315049965Syokota    KBDC kbdc = sc->kbdc;
315149965Syokota    int data[3];
315249965Syokota
315349965Syokota    set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
315449965Syokota    set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
315549965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
315649965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
315749965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
315849965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
315949965Syokota    if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
316049965Syokota	return FALSE;
316149965Syokota    if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
316249965Syokota	return FALSE;
316349965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
316449965Syokota
316558230Syokota    sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
316658230Syokota
316749965Syokota    return TRUE;				/* PS/2 absolute mode */
316849965Syokota}
316949965Syokota
317041016Sdfrstatic int
317154629Syokotapsmresume(device_t dev)
317241016Sdfr{
317354629Syokota    struct psm_softc *sc = device_get_softc(dev);
317454629Syokota    int unit = device_get_unit(dev);
317584880Syokota    int err;
317641016Sdfr
317741016Sdfr    if (verbose >= 2)
317854629Syokota        log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
317941016Sdfr
318058230Syokota    if (!(sc->config & PSM_CONFIG_HOOKRESUME))
318158230Syokota	return (0);
318258230Syokota
318384880Syokota    err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
318441016Sdfr
318541016Sdfr    if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
318641016Sdfr	/*
318741016Sdfr	 * Release the blocked process; it must be notified that the device
318841016Sdfr	 * cannot be accessed anymore.
318941016Sdfr	 */
319041016Sdfr        sc->state &= ~PSM_ASLP;
3191111748Sdes        wakeup(sc);
319241016Sdfr    }
319341016Sdfr
319441016Sdfr    if (verbose >= 2)
319554629Syokota        log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
319641016Sdfr
319741016Sdfr    return (err);
319841016Sdfr}
319941016Sdfr
320052997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
320183147Syokota
320283147Syokota/*
320383147Syokota * This sucks up assignments from PNPBIOS and ACPI.
320483147Syokota */
320583147Syokota
320683931Syokota/*
320783931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
320883931Syokota * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
320983931Syokota * can be probed and attached only after the AT keyboard controller is
321083931Syokota * attached, we shall quietly reserve the IRQ resource for later use.
321183931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
321283931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging
321383931Syokota * under the keyboard controller, then probe and attach it.
321483931Syokota */
321583147Syokota
321683147Syokotastatic	devclass_t			psmcpnp_devclass;
321783147Syokota
321883147Syokotastatic	device_probe_t			psmcpnp_probe;
321983147Syokotastatic	device_attach_t			psmcpnp_attach;
322083147Syokota
322183147Syokotastatic device_method_t psmcpnp_methods[] = {
322283147Syokota	DEVMETHOD(device_probe,		psmcpnp_probe),
322383147Syokota	DEVMETHOD(device_attach,	psmcpnp_attach),
322483147Syokota
322583147Syokota	{ 0, 0 }
322683147Syokota};
322783147Syokota
322883147Syokotastatic driver_t psmcpnp_driver = {
322983147Syokota	PSMCPNP_DRIVER_NAME,
323083147Syokota	psmcpnp_methods,
323183147Syokota	1,			/* no softc */
323283147Syokota};
323383147Syokota
323483147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = {
323588188Ssheldonh	{ 0x030fd041, "PS/2 mouse port" },		/* PNP0F03 */
323683147Syokota	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
323783147Syokota	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
3238117117Smikeh	{ 0x02002e4f, "Dell PS/2 mouse port" },		/* Lat. X200, Dell */
323983492Syokota	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
324084407Stakawata	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
3241109679Shsu	{ 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
3242109679Shsu	{ 0x0290d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9002, Vaio */
3243109710Smarcel	{ 0x0390d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9003, Vaio */
3244109679Shsu	{ 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
324583147Syokota	{ 0 }
324683147Syokota};
324783147Syokota
324883147Syokotastatic int
324983147Syokotacreate_a_copy(device_t atkbdc, device_t me)
325083147Syokota{
325183147Syokota	device_t psm;
325283147Syokota	u_long irq;
325383147Syokota
325483931Syokota	/* find the PS/2 mouse device instance under the keyboard controller */
325583931Syokota	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
325683931Syokota				device_get_unit(atkbdc));
325783147Syokota	if (psm == NULL)
325883147Syokota		return ENXIO;
325983931Syokota	if (device_get_state(psm) != DS_NOTPRESENT)
326083931Syokota		return 0;
326183147Syokota
326283931Syokota	/* move our resource to the found device */
326383931Syokota	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
326483147Syokota	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
326583147Syokota
326683147Syokota	/* ...then probe and attach it */
326783147Syokota	return device_probe_and_attach(psm);
326883147Syokota}
326983147Syokota
327083147Syokotastatic int
327183147Syokotapsmcpnp_probe(device_t dev)
327283147Syokota{
327383931Syokota	struct resource *res;
327483931Syokota	u_long irq;
327583931Syokota	int rid;
327683147Syokota
327783147Syokota	if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
327883147Syokota		return ENXIO;
327983147Syokota
328083931Syokota	/*
328183931Syokota	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
328283931Syokota	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
328383931Syokota	 * declares the PS/2 mouse device node without an IRQ resource!
328483931Syokota	 * If this happens, we shall refer to device hints.
328583931Syokota	 * If we still don't find it there, use a hardcoded value... XXX
328683931Syokota	 */
328783931Syokota	rid = 0;
328883931Syokota	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
328983931Syokota	if (irq <= 0) {
329083931Syokota		if (resource_long_value(PSM_DRIVER_NAME,
329183931Syokota					device_get_unit(dev), "irq", &irq) != 0)
329283931Syokota			irq = 12;	/* XXX */
329383931Syokota		device_printf(dev, "irq resource info is missing; "
329483931Syokota			      "assuming irq %ld\n", irq);
329583931Syokota		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
329683931Syokota	}
3297127135Snjl	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3298127135Snjl				     RF_SHAREABLE);
329983931Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
330083147Syokota
330183147Syokota	/* keep quiet */
330283147Syokota	if (!bootverbose)
330383147Syokota		device_quiet(dev);
330483492Syokota
330583931Syokota	return ((res == NULL) ? ENXIO : 0);
330683147Syokota}
330783147Syokota
330883147Syokotastatic int
330983147Syokotapsmcpnp_attach(device_t dev)
331083147Syokota{
331183492Syokota	device_t atkbdc;
331283931Syokota	int rid;
331383147Syokota
331483931Syokota	/* find the keyboard controller, which may be on acpi* or isa* bus */
331583931Syokota	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
331683931Syokota				     device_get_unit(dev));
331783931Syokota	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
331883492Syokota		create_a_copy(atkbdc, dev);
331983931Syokota	} else {
332083931Syokota		/*
332183931Syokota		 * If we don't have the AT keyboard controller yet,
332283931Syokota		 * just reserve the IRQ for later use...
332383931Syokota		 * (See psmidentify() above.)
332483931Syokota		 */
332583931Syokota		rid = 0;
3326127135Snjl		bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
332783931Syokota	}
332883492Syokota
332983147Syokota	return 0;
333083147Syokota}
333183147Syokota
3332123442Salfred/*
3333123442Salfred * Return true if 'now' is earlier than (start + (secs.usecs)).
3334123442Salfred * Now may be NULL and the function will fetch the current time from
3335123442Salfred * getmicrouptime(), or a cached 'now' can be passed in.
3336123442Salfred * All values should be numbers derived from getmicrouptime().
3337123442Salfred */
3338123442Salfredstatic int
3339123442Salfredtimeelapsed(start, secs, usecs, now)
3340123442Salfred	const struct timeval *start, *now;
3341123442Salfred	int secs, usecs;
3342123442Salfred{
3343123442Salfred	struct timeval snow, tv;
3344123442Salfred
3345123442Salfred	/* if there is no 'now' passed in, the get it as a convience. */
3346123442Salfred	if (now == NULL) {
3347123442Salfred		getmicrouptime(&snow);
3348123442Salfred		now = &snow;
3349123442Salfred	}
3350123442Salfred
3351123442Salfred	tv.tv_sec = secs;
3352123442Salfred	tv.tv_usec = usecs;
3353123442Salfred	timevaladd(&tv, start);
3354123442Salfred	return (timevalcmp(&tv, now, <));
3355123442Salfred}
3356123442Salfred
335783147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
335883147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
3359