psm.c revision 84880
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 *
2350477Speter * $FreeBSD: head/sys/dev/atkbdc/psm.c 84880 2001-10-13 10:28:02Z yokota $
2441016Sdfr */
2541016Sdfr
2641016Sdfr/*
2741016Sdfr *  Ported to 386bsd Oct 17, 1992
2841016Sdfr *  Sandi Donno, Computer Science, University of Cape Town, South Africa
2941016Sdfr *  Please send bug reports to sandi@cs.uct.ac.za
3041016Sdfr *
3141016Sdfr *  Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
3241016Sdfr *  although I was only partially successful in getting the alpha release
3341016Sdfr *  of his "driver for the Logitech and ATI Inport Bus mice for use with
3441016Sdfr *  386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
3541016Sdfr *  found his code to be an invaluable reference when porting this driver
3641016Sdfr *  to 386bsd.
3741016Sdfr *
3841016Sdfr *  Further modifications for latest 386BSD+patchkit and port to NetBSD,
3941016Sdfr *  Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
4041016Sdfr *
4141016Sdfr *  Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
4241016Sdfr *  Andrew Herbert - 12 June 1993
4341016Sdfr *
4441016Sdfr *  Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
4541016Sdfr *  - 13 June 1993
4641016Sdfr *
4741016Sdfr *  Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
4841016Sdfr *  - 24 October 1993
4941016Sdfr *
5041016Sdfr *  Hardware access routines and probe logic rewritten by
5141016Sdfr *  Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5241016Sdfr *  - 3, 14, 22 October 1996.
5341016Sdfr *  - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
5441016Sdfr *  - 14, 30 November 1996. Uses `kbdio.c'.
5541016Sdfr *  - 13 December 1996. Uses queuing version of `kbdio.c'.
5641016Sdfr *  - January/February 1997. Tweaked probe logic for
5741016Sdfr *    HiNote UltraII/Latitude/Armada laptops.
5841016Sdfr *  - 30 July 1997. Added APM support.
5941016Sdfr *  - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
6041016Sdfr *    Improved sync check logic.
6141016Sdfr *    Vendor specific support routines.
6241016Sdfr */
6341016Sdfr
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>
7784880Syokota#include <sys/time.h>
7841016Sdfr#include <sys/uio.h>
7941016Sdfr
8041016Sdfr#include <machine/limits.h>
8166860Sphk#include <sys/mouse.h>
8241181Sdfr#include <machine/resource.h>
8341016Sdfr
8441016Sdfr#include <isa/isavar.h>
8543105Sdfr#include <dev/kbd/atkbdcreg.h>
8641016Sdfr
8741016Sdfr/*
8841016Sdfr * Driver specific options: the following options may be set by
8941016Sdfr * `options' statements in the kernel configuration file.
9041016Sdfr */
9141016Sdfr
9241016Sdfr/* debugging */
9341016Sdfr#ifndef PSM_DEBUG
9441016Sdfr#define PSM_DEBUG	0	/* logging: 0: none, 1: brief, 2: verbose */
9541016Sdfr#endif
9641016Sdfr
9784880Syokota#ifndef PSM_SYNCERR_THRESHOLD1
9884880Syokota#define PSM_SYNCERR_THRESHOLD1	20
9984880Syokota#endif
10084880Syokota
10184880Syokota#ifndef PSM_INPUT_TIMEOUT
10284880Syokota#define PSM_INPUT_TIMEOUT	2000000	/* 2 sec */
10384880Syokota#endif
10484880Syokota
10541016Sdfr/* end of driver specific options */
10641016Sdfr
10783492Syokota#define PSM_DRIVER_NAME		"psm"
10883931Syokota#define PSMCPNP_DRIVER_NAME	"psmcpnp"
10983492Syokota
11041016Sdfr/* input queue */
11141016Sdfr#define PSM_BUFSIZE		960
11241016Sdfr#define PSM_SMALLBUFSIZE	240
11341016Sdfr
11441016Sdfr/* operation levels */
11541016Sdfr#define PSM_LEVEL_BASE		0
11641016Sdfr#define PSM_LEVEL_STANDARD	1
11741016Sdfr#define PSM_LEVEL_NATIVE	2
11841016Sdfr#define PSM_LEVEL_MIN		PSM_LEVEL_BASE
11941016Sdfr#define PSM_LEVEL_MAX		PSM_LEVEL_NATIVE
12041016Sdfr
12148778Syokota/* Logitech PS2++ protocol */
12248778Syokota#define MOUSE_PS2PLUS_CHECKBITS(b)	\
12348778Syokota				((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
12448778Syokota#define MOUSE_PS2PLUS_PACKET_TYPE(b)	\
12548778Syokota				(((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
12648778Syokota
12741016Sdfr/* some macros */
12841016Sdfr#define PSM_UNIT(dev)		(minor(dev) >> 1)
12941016Sdfr#define PSM_NBLOCKIO(dev)	(minor(dev) & 1)
13041016Sdfr#define PSM_MKMINOR(unit,block)	(((unit) << 1) | ((block) ? 0:1))
13141016Sdfr
13241016Sdfr#ifndef max
13341016Sdfr#define max(x,y)		((x) > (y) ? (x) : (y))
13441016Sdfr#endif
13541016Sdfr#ifndef min
13641016Sdfr#define min(x,y)		((x) < (y) ? (x) : (y))
13741016Sdfr#endif
13841016Sdfr
13948778Syokota#define abs(x)			(((x) < 0) ? -(x) : (x))
14048778Syokota
14141016Sdfr/* ring buffer */
14241016Sdfrtypedef struct ringbuf {
14341016Sdfr    int           count;	/* # of valid elements in the buffer */
14441016Sdfr    int           head;		/* head pointer */
14541016Sdfr    int           tail;		/* tail poiner */
14641016Sdfr    unsigned char buf[PSM_BUFSIZE];
14741016Sdfr} ringbuf_t;
14841016Sdfr
14941016Sdfr/* driver control block */
15041016Sdfrstruct psm_softc {		/* Driver status information */
15184880Syokota    int		  unit;
15241016Sdfr    struct selinfo rsel;	/* Process selecting for Input */
15341016Sdfr    unsigned char state;	/* Mouse driver state */
15441016Sdfr    int           config;	/* driver configuration flags */
15541016Sdfr    int           flags;	/* other flags */
15641016Sdfr    KBDC          kbdc;		/* handle to access the keyboard controller */
15758230Syokota    struct resource *intr;	/* IRQ resource */
15858230Syokota    void	  *ih;		/* interrupt handle */
15941016Sdfr    mousehw_t     hw;		/* hardware information */
16041016Sdfr    mousemode_t   mode;		/* operation mode */
16141016Sdfr    mousemode_t   dflt_mode;	/* default operation mode */
16241016Sdfr    mousestatus_t status;	/* accumulated mouse movement */
16341016Sdfr    ringbuf_t     queue;	/* mouse status queue */
16441016Sdfr    unsigned char ipacket[16];	/* interim input buffer */
16541016Sdfr    int           inputbytes;	/* # of bytes in the input buffer */
16641016Sdfr    int           button;	/* the latest button state */
16749965Syokota    int		  xold;	/* previous absolute X position */
16849965Syokota    int		  yold;	/* previous absolute Y position */
16984880Syokota    int		  syncerrors;
17084880Syokota    struct timeval inputtimeout;
17158230Syokota    int		  watchdog;	/* watchdog timer flag */
17258230Syokota    struct callout_handle callout;	/* watchdog timer call out */
17358230Syokota    dev_t	  dev;
17458230Syokota    dev_t	  bdev;
17541016Sdfr};
17641016Sdfrdevclass_t psm_devclass;
17741016Sdfr#define PSM_SOFTC(unit)	((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
17841016Sdfr
17941016Sdfr/* driver state flags (state) */
18041016Sdfr#define PSM_VALID		0x80
18141016Sdfr#define PSM_OPEN		1	/* Device is open */
18241016Sdfr#define PSM_ASLP		2	/* Waiting for mouse data */
18341016Sdfr
18441016Sdfr/* driver configuration flags (config) */
18541016Sdfr#define PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
18641016Sdfr#define PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
18741016Sdfr#define PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
18845789Speter#define PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
18945789Speter#define PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
19045789Speter#define PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
19145789Speter#define PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
19258230Syokota#define PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
19358230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
19469439Syokota#define PSM_CONFIG_SYNCHACK	0x8000 /* enable `out-of-sync' hack */
19541016Sdfr
19641016Sdfr#define PSM_CONFIG_FLAGS	(PSM_CONFIG_RESOLUTION 		\
19741016Sdfr				    | PSM_CONFIG_ACCEL		\
19845789Speter				    | PSM_CONFIG_NOCHECKSYNC	\
19969439Syokota				    | PSM_CONFIG_SYNCHACK	\
20045789Speter				    | PSM_CONFIG_NOIDPROBE	\
20145789Speter				    | PSM_CONFIG_NORESET	\
20245789Speter				    | PSM_CONFIG_FORCETAP	\
20358230Syokota				    | PSM_CONFIG_IGNPORTERROR	\
20458230Syokota				    | PSM_CONFIG_HOOKRESUME	\
20558230Syokota				    | PSM_CONFIG_INITAFTERSUSPEND)
20641016Sdfr
20741016Sdfr/* other flags (flags) */
20849965Syokota#define PSM_FLAGS_FINGERDOWN	0x0001 /* VersaPad finger down */
20941016Sdfr
21041016Sdfr/* for backward compatibility */
21141016Sdfr#define OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
21241016Sdfr#define OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
21341016Sdfr#define OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
21441016Sdfr
21541016Sdfrtypedef struct old_mousehw {
21641016Sdfr    int buttons;
21741016Sdfr    int iftype;
21841016Sdfr    int type;
21941016Sdfr    int hwid;
22041016Sdfr} old_mousehw_t;
22141016Sdfr
22241016Sdfrtypedef struct old_mousemode {
22341016Sdfr    int protocol;
22441016Sdfr    int rate;
22541016Sdfr    int resolution;
22641016Sdfr    int accelfactor;
22741016Sdfr} old_mousemode_t;
22841016Sdfr
22941016Sdfr/* packet formatting function */
23041016Sdfrtypedef int packetfunc_t __P((struct psm_softc *, unsigned char *,
23141016Sdfr			      int *, int, mousestatus_t *));
23241016Sdfr
23341016Sdfr/* function prototypes */
23483147Syokotastatic void psmidentify __P((driver_t *, device_t));
23541016Sdfrstatic int psmprobe __P((device_t));
23641016Sdfrstatic int psmattach __P((device_t));
23758230Syokotastatic int psmdetach __P((device_t));
23854629Syokotastatic int psmresume __P((device_t));
23941016Sdfr
24041016Sdfrstatic d_open_t psmopen;
24141016Sdfrstatic d_close_t psmclose;
24241016Sdfrstatic d_read_t psmread;
24341016Sdfrstatic d_ioctl_t psmioctl;
24441016Sdfrstatic d_poll_t psmpoll;
24541016Sdfr
24641016Sdfrstatic int enable_aux_dev __P((KBDC));
24741016Sdfrstatic int disable_aux_dev __P((KBDC));
24841016Sdfrstatic int get_mouse_status __P((KBDC, int *, int, int));
24941016Sdfrstatic int get_aux_id __P((KBDC));
25041016Sdfrstatic int set_mouse_sampling_rate __P((KBDC, int));
25141016Sdfrstatic int set_mouse_scaling __P((KBDC, int));
25241016Sdfrstatic int set_mouse_resolution __P((KBDC, int));
25341016Sdfrstatic int set_mouse_mode __P((KBDC));
25441016Sdfrstatic int get_mouse_buttons __P((KBDC));
25541016Sdfrstatic int is_a_mouse __P((int));
25641016Sdfrstatic void recover_from_error __P((KBDC));
25741016Sdfrstatic int restore_controller __P((KBDC, int));
25884880Syokotastatic int doinitialize __P((struct psm_softc *, mousemode_t *));
25984880Syokotastatic int doopen __P((struct psm_softc *, int));
26084880Syokotastatic int reinitialize __P((struct psm_softc *, int));
26158230Syokotastatic char *model_name __P((int));
26258230Syokotastatic void psmintr __P((void *));
26358230Syokotastatic void psmtimeout __P((void *));
26441016Sdfr
26541016Sdfr/* vendor specific features */
26641016Sdfrtypedef int probefunc_t __P((struct psm_softc *));
26741016Sdfr
26841016Sdfrstatic int mouse_id_proc1 __P((KBDC, int, int, int *));
26969438Syokotastatic int mouse_ext_command __P((KBDC, int));
27041016Sdfrstatic probefunc_t enable_groller;
27141016Sdfrstatic probefunc_t enable_gmouse;
27241016Sdfrstatic probefunc_t enable_aglide;
27341016Sdfrstatic probefunc_t enable_kmouse;
27458230Syokotastatic probefunc_t enable_msexplorer;
27541016Sdfrstatic probefunc_t enable_msintelli;
27658230Syokotastatic probefunc_t enable_4dmouse;
27758230Syokotastatic probefunc_t enable_4dplus;
27841016Sdfrstatic probefunc_t enable_mmanplus;
27949965Syokotastatic probefunc_t enable_versapad;
28041016Sdfrstatic int tame_mouse __P((struct psm_softc *, mousestatus_t *, unsigned char *));
28141016Sdfr
28241016Sdfrstatic struct {
28341016Sdfr    int                 model;
28441016Sdfr    unsigned char	syncmask;
28541016Sdfr    int 		packetsize;
28641016Sdfr    probefunc_t 	*probefunc;
28741016Sdfr} vendortype[] = {
28858230Syokota    /*
28958230Syokota     * WARNING: the order of probe is very important.  Don't mess it
29058230Syokota     * unless you know what you are doing.
29158230Syokota     */
29241016Sdfr    { MOUSE_MODEL_NET,			/* Genius NetMouse */
29358230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
29441016Sdfr    { MOUSE_MODEL_NETSCROLL,		/* Genius NetScroll */
29541016Sdfr      0xc8, 6, enable_groller, },
29658230Syokota    { MOUSE_MODEL_MOUSEMANPLUS,		/* Logitech MouseMan+ */
29758230Syokota      0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
29858230Syokota    { MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
29958230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
30058230Syokota    { MOUSE_MODEL_4D,			/* A4 Tech 4D Mouse */
30158230Syokota      0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
30258230Syokota    { MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
30358230Syokota      0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
30458230Syokota    { MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
30558230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
30641016Sdfr    { MOUSE_MODEL_GLIDEPOINT,		/* ALPS GlidePoint */
30741016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
30841016Sdfr    { MOUSE_MODEL_THINK,		/* Kensignton ThinkingMouse */
30941016Sdfr      0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
31049965Syokota    { MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
31149965Syokota      0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
31241016Sdfr    { MOUSE_MODEL_GENERIC,
31341016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
31441016Sdfr};
31563951Syokota#define GENERIC_MOUSE_ENTRY	((sizeof(vendortype) / sizeof(*vendortype)) - 1)
31641016Sdfr
31741016Sdfr/* device driver declarateion */
31841016Sdfrstatic device_method_t psm_methods[] = {
31941016Sdfr	/* Device interface */
32083147Syokota	DEVMETHOD(device_identify,	psmidentify),
32141016Sdfr	DEVMETHOD(device_probe,		psmprobe),
32241016Sdfr	DEVMETHOD(device_attach,	psmattach),
32358230Syokota	DEVMETHOD(device_detach,	psmdetach),
32454629Syokota	DEVMETHOD(device_resume,	psmresume),
32541016Sdfr
32641016Sdfr	{ 0, 0 }
32741016Sdfr};
32841016Sdfr
32941016Sdfrstatic driver_t psm_driver = {
33083492Syokota    PSM_DRIVER_NAME,
33141016Sdfr    psm_methods,
33241016Sdfr    sizeof(struct psm_softc),
33341016Sdfr};
33441016Sdfr
33541016Sdfr#define CDEV_MAJOR        21
33641016Sdfr
33747625Sphkstatic struct cdevsw psm_cdevsw = {
33847625Sphk	/* open */	psmopen,
33947625Sphk	/* close */	psmclose,
34047625Sphk	/* read */	psmread,
34147625Sphk	/* write */	nowrite,
34247625Sphk	/* ioctl */	psmioctl,
34347625Sphk	/* poll */	psmpoll,
34447625Sphk	/* mmap */	nommap,
34547625Sphk	/* strategy */	nostrategy,
34683492Syokota	/* name */	PSM_DRIVER_NAME,
34747625Sphk	/* maj */	CDEV_MAJOR,
34847625Sphk	/* dump */	nodump,
34947625Sphk	/* psize */	nopsize,
35047625Sphk	/* flags */	0,
35141016Sdfr};
35241016Sdfr
35341016Sdfr/* debug message level */
35441016Sdfrstatic int verbose = PSM_DEBUG;
35541016Sdfr
35641016Sdfr/* device I/O routines */
35741016Sdfrstatic int
35841016Sdfrenable_aux_dev(KBDC kbdc)
35941016Sdfr{
36041016Sdfr    int res;
36141016Sdfr
36241016Sdfr    res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
36341016Sdfr    if (verbose >= 2)
36441016Sdfr        log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
36541016Sdfr
36641016Sdfr    return (res == PSM_ACK);
36741016Sdfr}
36841016Sdfr
36941016Sdfrstatic int
37041016Sdfrdisable_aux_dev(KBDC kbdc)
37141016Sdfr{
37241016Sdfr    int res;
37341016Sdfr
37441016Sdfr    res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
37541016Sdfr    if (verbose >= 2)
37641016Sdfr        log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
37741016Sdfr
37841016Sdfr    return (res == PSM_ACK);
37941016Sdfr}
38041016Sdfr
38141016Sdfrstatic int
38241016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
38341016Sdfr{
38441016Sdfr    int cmd;
38541016Sdfr    int res;
38641016Sdfr    int i;
38741016Sdfr
38841016Sdfr    switch (flag) {
38941016Sdfr    case 0:
39041016Sdfr    default:
39141016Sdfr	cmd = PSMC_SEND_DEV_STATUS;
39241016Sdfr	break;
39341016Sdfr    case 1:
39441016Sdfr	cmd = PSMC_SEND_DEV_DATA;
39541016Sdfr	break;
39641016Sdfr    }
39741016Sdfr    empty_aux_buffer(kbdc, 5);
39841016Sdfr    res = send_aux_command(kbdc, cmd);
39941016Sdfr    if (verbose >= 2)
40041016Sdfr        log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
40141016Sdfr	    (flag == 1) ? "DATA" : "STATUS", res);
40241016Sdfr    if (res != PSM_ACK)
40341016Sdfr        return 0;
40441016Sdfr
40541016Sdfr    for (i = 0; i < len; ++i) {
40641016Sdfr        status[i] = read_aux_data(kbdc);
40741016Sdfr	if (status[i] < 0)
40841016Sdfr	    break;
40941016Sdfr    }
41041016Sdfr
41141016Sdfr    if (verbose) {
41241016Sdfr        log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
41341016Sdfr            (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
41441016Sdfr    }
41541016Sdfr
41641016Sdfr    return i;
41741016Sdfr}
41841016Sdfr
41941016Sdfrstatic int
42041016Sdfrget_aux_id(KBDC kbdc)
42141016Sdfr{
42241016Sdfr    int res;
42341016Sdfr    int id;
42441016Sdfr
42541016Sdfr    empty_aux_buffer(kbdc, 5);
42641016Sdfr    res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
42741016Sdfr    if (verbose >= 2)
42841016Sdfr        log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
42941016Sdfr    if (res != PSM_ACK)
43041016Sdfr	return (-1);
43141016Sdfr
43241016Sdfr    /* 10ms delay */
43341016Sdfr    DELAY(10000);
43441016Sdfr
43541016Sdfr    id = read_aux_data(kbdc);
43641016Sdfr    if (verbose >= 2)
43741016Sdfr        log(LOG_DEBUG, "psm: device ID: %04x\n", id);
43841016Sdfr
43941016Sdfr    return id;
44041016Sdfr}
44141016Sdfr
44241016Sdfrstatic int
44341016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
44441016Sdfr{
44541016Sdfr    int res;
44641016Sdfr
44741016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
44841016Sdfr    if (verbose >= 2)
44941016Sdfr        log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
45041016Sdfr
45141016Sdfr    return ((res == PSM_ACK) ? rate : -1);
45241016Sdfr}
45341016Sdfr
45441016Sdfrstatic int
45541016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
45641016Sdfr{
45741016Sdfr    int res;
45841016Sdfr
45941016Sdfr    switch (scale) {
46041016Sdfr    case 1:
46141016Sdfr    default:
46241016Sdfr	scale = PSMC_SET_SCALING11;
46341016Sdfr	break;
46441016Sdfr    case 2:
46541016Sdfr	scale = PSMC_SET_SCALING21;
46641016Sdfr	break;
46741016Sdfr    }
46841016Sdfr    res = send_aux_command(kbdc, scale);
46941016Sdfr    if (verbose >= 2)
47041016Sdfr        log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
47141016Sdfr	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
47241016Sdfr
47341016Sdfr    return (res == PSM_ACK);
47441016Sdfr}
47541016Sdfr
47641016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
47741016Sdfrstatic int
47841016Sdfrset_mouse_resolution(KBDC kbdc, int val)
47941016Sdfr{
48041016Sdfr    int res;
48141016Sdfr
48241016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
48341016Sdfr    if (verbose >= 2)
48441016Sdfr        log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
48541016Sdfr
48641016Sdfr    return ((res == PSM_ACK) ? val : -1);
48741016Sdfr}
48841016Sdfr
48941016Sdfr/*
49041016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
49141016Sdfr * re-enabled by calling `enable_aux_dev()'
49241016Sdfr */
49341016Sdfrstatic int
49441016Sdfrset_mouse_mode(KBDC kbdc)
49541016Sdfr{
49641016Sdfr    int res;
49741016Sdfr
49841016Sdfr    res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
49941016Sdfr    if (verbose >= 2)
50041016Sdfr        log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
50141016Sdfr
50241016Sdfr    return (res == PSM_ACK);
50341016Sdfr}
50441016Sdfr
50541016Sdfrstatic int
50641016Sdfrget_mouse_buttons(KBDC kbdc)
50741016Sdfr{
50841016Sdfr    int c = 2;		/* assume two buttons by default */
50941016Sdfr    int status[3];
51041016Sdfr
51141016Sdfr    /*
51241016Sdfr     * NOTE: a special sequence to obtain Logitech Mouse specific
51341016Sdfr     * information: set resolution to 25 ppi, set scaling to 1:1, set
51441016Sdfr     * scaling to 1:1, set scaling to 1:1. Then the second byte of the
51541016Sdfr     * mouse status bytes is the number of available buttons.
51641016Sdfr     * Some manufactures also support this sequence.
51741016Sdfr     */
51841016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
51941016Sdfr        return c;
52041016Sdfr    if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
52141016Sdfr        && set_mouse_scaling(kbdc, 1)
52241016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
52341016Sdfr        if (status[1] != 0)
52441016Sdfr            return status[1];
52541016Sdfr    }
52641016Sdfr    return c;
52741016Sdfr}
52841016Sdfr
52941016Sdfr/* misc subroutines */
53041016Sdfr/*
53141016Sdfr * Someday, I will get the complete list of valid pointing devices and
53241016Sdfr * their IDs... XXX
53341016Sdfr */
53441016Sdfrstatic int
53541016Sdfris_a_mouse(int id)
53641016Sdfr{
53741016Sdfr#if 0
53841016Sdfr    static int valid_ids[] = {
53941016Sdfr        PSM_MOUSE_ID,		/* mouse */
54041016Sdfr        PSM_BALLPOINT_ID,	/* ballpoint device */
54141016Sdfr        PSM_INTELLI_ID,		/* Intellimouse */
54258230Syokota        PSM_EXPLORER_ID,	/* Intellimouse Explorer */
54341016Sdfr        -1			/* end of table */
54441016Sdfr    };
54541016Sdfr    int i;
54641016Sdfr
54741016Sdfr    for (i = 0; valid_ids[i] >= 0; ++i)
54841016Sdfr        if (valid_ids[i] == id)
54941016Sdfr            return TRUE;
55041016Sdfr    return FALSE;
55141016Sdfr#else
55241016Sdfr    return TRUE;
55341016Sdfr#endif
55441016Sdfr}
55541016Sdfr
55641016Sdfrstatic char *
55741016Sdfrmodel_name(int model)
55841016Sdfr{
55941016Sdfr    static struct {
56041016Sdfr	int model_code;
56141016Sdfr	char *model_name;
56241016Sdfr    } models[] = {
56358230Syokota        { MOUSE_MODEL_NETSCROLL,	"NetScroll" },
56458230Syokota        { MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
56541016Sdfr        { MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
56641016Sdfr        { MOUSE_MODEL_THINK,		"ThinkingMouse" },
56741016Sdfr        { MOUSE_MODEL_INTELLI,		"IntelliMouse" },
56841016Sdfr        { MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
56949965Syokota        { MOUSE_MODEL_VERSAPAD,		"VersaPad" },
57058230Syokota        { MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
57158230Syokota        { MOUSE_MODEL_4D,		"4D Mouse" },
57258230Syokota        { MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
57341016Sdfr        { MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
57441016Sdfr        { MOUSE_MODEL_UNKNOWN,		NULL },
57541016Sdfr    };
57641016Sdfr    int i;
57741016Sdfr
57841016Sdfr    for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
57941016Sdfr	if (models[i].model_code == model)
58041016Sdfr	    return models[i].model_name;
58141016Sdfr    }
58241016Sdfr    return "Unknown";
58341016Sdfr}
58441016Sdfr
58541016Sdfrstatic void
58641016Sdfrrecover_from_error(KBDC kbdc)
58741016Sdfr{
58841016Sdfr    /* discard anything left in the output buffer */
58941016Sdfr    empty_both_buffers(kbdc, 10);
59041016Sdfr
59141016Sdfr#if 0
59241016Sdfr    /*
59341016Sdfr     * NOTE: KBDC_RESET_KBD may not restore the communication between the
59441016Sdfr     * keyboard and the controller.
59541016Sdfr     */
59641016Sdfr    reset_kbd(kbdc);
59741016Sdfr#else
59841016Sdfr    /*
59941016Sdfr     * NOTE: somehow diagnostic and keyboard port test commands bring the
60041016Sdfr     * keyboard back.
60141016Sdfr     */
60241016Sdfr    if (!test_controller(kbdc))
60341016Sdfr        log(LOG_ERR, "psm: keyboard controller failed.\n");
60441016Sdfr    /* if there isn't a keyboard in the system, the following error is OK */
60541016Sdfr    if (test_kbd_port(kbdc) != 0) {
60641016Sdfr	if (verbose)
60741016Sdfr	    log(LOG_ERR, "psm: keyboard port failed.\n");
60841016Sdfr    }
60941016Sdfr#endif
61041016Sdfr}
61141016Sdfr
61241016Sdfrstatic int
61341016Sdfrrestore_controller(KBDC kbdc, int command_byte)
61441016Sdfr{
61541016Sdfr    empty_both_buffers(kbdc, 10);
61641016Sdfr
61741016Sdfr    if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
61841016Sdfr	log(LOG_ERR, "psm: failed to restore the keyboard controller "
61941016Sdfr		     "command byte.\n");
62058230Syokota	empty_both_buffers(kbdc, 10);
62141016Sdfr	return FALSE;
62241016Sdfr    } else {
62358230Syokota	empty_both_buffers(kbdc, 10);
62441016Sdfr	return TRUE;
62541016Sdfr    }
62641016Sdfr}
62741016Sdfr
62841016Sdfr/*
62941016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
63041016Sdfr * and its interrupt must be disabled before calling this routine.
63141016Sdfr * The aux device will be disabled before returning.
63241016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
63341016Sdfr * calling this routine.
63441016Sdfr */
63541016Sdfrstatic int
63684880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode)
63741016Sdfr{
63841016Sdfr    KBDC kbdc = sc->kbdc;
63941016Sdfr    int stat[3];
64041016Sdfr    int i;
64141016Sdfr
64241016Sdfr    switch((i = test_aux_port(kbdc))) {
64341016Sdfr    case 1:	/* ignore this error */
64441016Sdfr    case PSM_ACK:
64541016Sdfr	if (verbose)
64641016Sdfr	    log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
64784880Syokota	        sc->unit, i);
64841016Sdfr	/* fall though */
64941016Sdfr    case 0:	/* no error */
65041016Sdfr    	break;
65141016Sdfr    case -1: 	/* time out */
65241016Sdfr    default: 	/* error */
65341016Sdfr    	recover_from_error(kbdc);
65445789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
65545789Speter	    break;
65641016Sdfr    	log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
65784880Syokota    	    sc->unit, i);
65841016Sdfr    	return FALSE;
65941016Sdfr    }
66041016Sdfr
66145789Speter    if (sc->config & PSM_CONFIG_NORESET) {
66245789Speter	/*
66345789Speter	 * Don't try to reset the pointing device.  It may possibly be
66445789Speter	 * left in the unknown state, though...
66545789Speter	 */
66645789Speter    } else {
66745789Speter	/*
66845789Speter	 * NOTE: some controllers appears to hang the `keyboard' when
66945789Speter	 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
67045789Speter	 */
67145789Speter	if (!reset_aux_dev(kbdc)) {
67245789Speter            recover_from_error(kbdc);
67384880Syokota            log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
67445789Speter            return FALSE;
67545789Speter	}
67641016Sdfr    }
67741016Sdfr
67841016Sdfr    /*
67941016Sdfr     * both the aux port and the aux device is functioning, see
68041016Sdfr     * if the device can be enabled.
68141016Sdfr     */
68241016Sdfr    if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
68384880Syokota        log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
68441016Sdfr        return FALSE;
68541016Sdfr    }
68641016Sdfr    empty_both_buffers(kbdc, 10);	/* remove stray data if any */
68741016Sdfr
68845789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
68945789Speter	i = GENERIC_MOUSE_ENTRY;
69045789Speter    } else {
69145789Speter	/* FIXME: hardware ID, mouse buttons? */
69241016Sdfr
69345789Speter	/* other parameters */
69445789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
69545789Speter	    if ((*vendortype[i].probefunc)(sc)) {
69645789Speter		if (verbose >= 2)
69745789Speter		    log(LOG_ERR, "psm%d: found %s\n",
69884880Syokota			sc->unit, model_name(vendortype[i].model));
69945789Speter		break;
70045789Speter	    }
70141016Sdfr	}
70241016Sdfr    }
70341016Sdfr
70441016Sdfr    sc->hw.model = vendortype[i].model;
70541016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
70641016Sdfr
70741016Sdfr    /* set mouse parameters */
70841016Sdfr    if (mode != (mousemode_t *)NULL) {
70941016Sdfr	if (mode->rate > 0)
71041016Sdfr            mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
71141016Sdfr	if (mode->resolution >= 0)
71241016Sdfr            mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
71341016Sdfr        set_mouse_scaling(kbdc, 1);
71441016Sdfr        set_mouse_mode(kbdc);
71541016Sdfr    }
71641016Sdfr
71741016Sdfr    /* request a data packet and extract sync. bits */
71841016Sdfr    if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
71984880Syokota        log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
72084880Syokota	    sc->unit);
72141016Sdfr        sc->mode.syncmask[0] = 0;
72241016Sdfr    } else {
72341016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
72441016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
72541016Sdfr	empty_aux_buffer(kbdc, 5);
72641016Sdfr    }
72741016Sdfr
72841016Sdfr    /* just check the status of the mouse */
72941016Sdfr    if (get_mouse_status(kbdc, stat, 0, 3) < 3)
73084880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
73184880Syokota	    sc->unit);
73241016Sdfr
73341016Sdfr    return TRUE;
73441016Sdfr}
73541016Sdfr
73641016Sdfrstatic int
73784880Syokotadoopen(struct psm_softc *sc, int command_byte)
73841016Sdfr{
73941016Sdfr    int stat[3];
74041016Sdfr
74141016Sdfr    /* enable the mouse device */
74241016Sdfr    if (!enable_aux_dev(sc->kbdc)) {
74341016Sdfr	/* MOUSE ERROR: failed to enable the mouse because:
74441016Sdfr	 * 1) the mouse is faulty,
74541016Sdfr	 * 2) the mouse has been removed(!?)
74641016Sdfr	 * In the latter case, the keyboard may have hung, and need
74741016Sdfr	 * recovery procedure...
74841016Sdfr	 */
74941016Sdfr	recover_from_error(sc->kbdc);
75041016Sdfr#if 0
75141016Sdfr	/* FIXME: we could reset the mouse here and try to enable
75241016Sdfr	 * it again. But it will take long time and it's not a good
75341016Sdfr	 * idea to disable the keyboard that long...
75441016Sdfr	 */
75584880Syokota	if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
75641016Sdfr	    recover_from_error(sc->kbdc);
75741016Sdfr#else
75841016Sdfr        {
75941016Sdfr#endif
76041016Sdfr            restore_controller(sc->kbdc, command_byte);
76141016Sdfr	    /* mark this device is no longer available */
76241016Sdfr	    sc->state &= ~PSM_VALID;
76341016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
76484880Syokota		sc->unit);
76541016Sdfr	    return (EIO);
76641016Sdfr	}
76741016Sdfr    }
76841016Sdfr
76941016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
77084880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
77141016Sdfr
77241016Sdfr    /* enable the aux port and interrupt */
77341016Sdfr    if (!set_controller_command_byte(sc->kbdc,
77441016Sdfr	    kbdc_get_device_mask(sc->kbdc),
77541016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
77641016Sdfr		| KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
77741016Sdfr	/* CONTROLLER ERROR */
77841016Sdfr	disable_aux_dev(sc->kbdc);
77941016Sdfr        restore_controller(sc->kbdc, command_byte);
78041016Sdfr	log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
78184880Syokota	    sc->unit);
78241016Sdfr	return (EIO);
78341016Sdfr    }
78441016Sdfr
78558230Syokota    /* start the watchdog timer */
78658230Syokota    sc->watchdog = FALSE;
78784880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
78858230Syokota
78941016Sdfr    return (0);
79041016Sdfr}
79141016Sdfr
79284880Syokotastatic int
79384880Syokotareinitialize(struct psm_softc *sc, int doinit)
79484880Syokota{
79584880Syokota    int err;
79684880Syokota    int c;
79784880Syokota    int s;
79884880Syokota
79984880Syokota    /* don't let anybody mess with the aux device */
80084880Syokota    if (!kbdc_lock(sc->kbdc, TRUE))
80184880Syokota	return (EIO);
80284880Syokota    s = spltty();
80384880Syokota
80484880Syokota    /* block our watchdog timer */
80584880Syokota    sc->watchdog = FALSE;
80684880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
80784880Syokota    callout_handle_init(&sc->callout);
80884880Syokota
80984880Syokota    /* save the current controller command byte */
81084880Syokota    empty_both_buffers(sc->kbdc, 10);
81184880Syokota    c = get_controller_command_byte(sc->kbdc);
81284880Syokota    if (verbose >= 2)
81384880Syokota        log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
81484880Syokota	    sc->unit, c);
81584880Syokota
81684880Syokota    /* enable the aux port but disable the aux interrupt and the keyboard */
81784880Syokota    if ((c == -1) || !set_controller_command_byte(sc->kbdc,
81884880Syokota	    kbdc_get_device_mask(sc->kbdc),
81984880Syokota  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
82084880Syokota	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
82184880Syokota        /* CONTROLLER ERROR */
82284880Syokota	splx(s);
82384880Syokota        kbdc_lock(sc->kbdc, FALSE);
82484880Syokota	log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
82584880Syokota	    sc->unit);
82684880Syokota	return (EIO);
82784880Syokota    }
82884880Syokota
82984880Syokota    /* flush any data */
83084880Syokota    if (sc->state & PSM_VALID) {
83184880Syokota	disable_aux_dev(sc->kbdc);	/* this may fail; but never mind... */
83284880Syokota	empty_aux_buffer(sc->kbdc, 10);
83384880Syokota    }
83484880Syokota    sc->inputbytes = 0;
83584880Syokota    sc->syncerrors = 0;
83684880Syokota
83784880Syokota    /* try to detect the aux device; are you still there? */
83884880Syokota    err = 0;
83984880Syokota    if (doinit) {
84084880Syokota	if (doinitialize(sc, &sc->mode)) {
84184880Syokota	    /* yes */
84284880Syokota	    sc->state |= PSM_VALID;
84384880Syokota	} else {
84484880Syokota	    /* the device has gone! */
84584880Syokota	    restore_controller(sc->kbdc, c);
84684880Syokota	    sc->state &= ~PSM_VALID;
84784880Syokota	    log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
84884880Syokota		sc->unit);
84984880Syokota	    err = ENXIO;
85084880Syokota	}
85184880Syokota    }
85284880Syokota    splx(s);
85384880Syokota
85484880Syokota    /* restore the driver state */
85584880Syokota    if ((sc->state & PSM_OPEN) && (err == 0)) {
85684880Syokota        /* enable the aux device and the port again */
85784880Syokota	err = doopen(sc, c);
85884880Syokota	if (err != 0)
85984880Syokota	    log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
86084880Syokota		sc->unit);
86184880Syokota    } else {
86284880Syokota        /* restore the keyboard port and disable the aux port */
86384880Syokota        if (!set_controller_command_byte(sc->kbdc,
86484880Syokota                kbdc_get_device_mask(sc->kbdc),
86584880Syokota                (c & KBD_KBD_CONTROL_BITS)
86684880Syokota                    | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
86784880Syokota            /* CONTROLLER ERROR */
86884880Syokota            log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
86984880Syokota                sc->unit);
87084880Syokota            err = EIO;
87184880Syokota	}
87284880Syokota    }
87384880Syokota
87484880Syokota    kbdc_lock(sc->kbdc, FALSE);
87584880Syokota    return (err);
87684880Syokota}
87784880Syokota
87841016Sdfr/* psm driver entry points */
87941016Sdfr
88083147Syokotastatic void
88183147Syokotapsmidentify(driver_t *driver, device_t parent)
88283147Syokota{
88383931Syokota    device_t psmc;
88483931Syokota    device_t psm;
88583931Syokota    u_long irq;
88683931Syokota    int unit;
88783147Syokota
88883931Syokota    unit = device_get_unit(parent);
88983931Syokota
89083147Syokota    /* always add at least one child */
89183931Syokota    psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
89283931Syokota    if (psm == NULL)
89383931Syokota	return;
89483931Syokota
89583931Syokota    irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
89683931Syokota    if (irq > 0)
89783931Syokota	return;
89883931Syokota
89983931Syokota    /*
90083931Syokota     * If the PS/2 mouse device has already been reported by ACPI or
90183931Syokota     * PnP BIOS, obtain the IRQ resource from it.
90283931Syokota     * (See psmcpnp_attach() below.)
90383931Syokota     */
90483931Syokota    psmc = device_find_child(device_get_parent(parent),
90583931Syokota			     PSMCPNP_DRIVER_NAME, unit);
90683931Syokota    if (psmc == NULL)
90783931Syokota	return;
90883931Syokota    irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
90983931Syokota    if (irq <= 0)
91083931Syokota	return;
91183931Syokota    bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
91283147Syokota}
91383147Syokota
91441016Sdfr#define endprobe(v)	{   if (bootverbose) 				\
91541016Sdfr				--verbose;   				\
91641016Sdfr                            kbdc_set_device_mask(sc->kbdc, mask);	\
91741016Sdfr			    kbdc_lock(sc->kbdc, FALSE);			\
91841016Sdfr			    return (v);	     				\
91941016Sdfr			}
92041016Sdfr
92141016Sdfrstatic int
92241016Sdfrpsmprobe(device_t dev)
92341016Sdfr{
92441016Sdfr    int unit = device_get_unit(dev);
92541016Sdfr    struct psm_softc *sc = device_get_softc(dev);
92641016Sdfr    int stat[3];
92741016Sdfr    int command_byte;
92841016Sdfr    int mask;
92958230Syokota    int rid;
93041016Sdfr    int i;
93141016Sdfr
93241016Sdfr#if 0
93341016Sdfr    kbdc_debug(TRUE);
93441016Sdfr#endif
93558230Syokota
93683147Syokota    /* see if IRQ is available */
93783147Syokota    rid = KBDC_RID_AUX;
93883147Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
93983147Syokota				  RF_SHAREABLE | RF_ACTIVE);
94083147Syokota    if (sc->intr == NULL) {
94183147Syokota	if (bootverbose)
94283147Syokota            device_printf(dev, "unable to allocate IRQ\n");
94383147Syokota        return (ENXIO);
94483147Syokota    }
94583147Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
94658230Syokota
94784880Syokota    sc->unit = unit;
94858230Syokota    sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
94983147Syokota    sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
95058230Syokota    /* XXX: for backward compatibility */
95158230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
95258230Syokota    sc->config |=
95358230Syokota#ifdef PSM_RESETAFTERSUSPEND
95458230Syokota	PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
95558230Syokota#else
95658230Syokota	PSM_CONFIG_HOOKRESUME;
95758230Syokota#endif
95858230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
95941016Sdfr    sc->flags = 0;
96041016Sdfr    if (bootverbose)
96141016Sdfr        ++verbose;
96241016Sdfr
96343105Sdfr    device_set_desc(dev, "PS/2 Mouse");
96443105Sdfr
96541016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE)) {
96641016Sdfr        printf("psm%d: unable to lock the controller.\n", unit);
96741016Sdfr        if (bootverbose)
96841016Sdfr            --verbose;
96941016Sdfr	return (ENXIO);
97041016Sdfr    }
97141016Sdfr
97241016Sdfr    /*
97341016Sdfr     * NOTE: two bits in the command byte controls the operation of the
97441016Sdfr     * aux port (mouse port): the aux port disable bit (bit 5) and the aux
97541016Sdfr     * port interrupt (IRQ 12) enable bit (bit 2).
97641016Sdfr     */
97741016Sdfr
97841016Sdfr    /* discard anything left after the keyboard initialization */
97941016Sdfr    empty_both_buffers(sc->kbdc, 10);
98041016Sdfr
98141016Sdfr    /* save the current command byte; it will be used later */
98241016Sdfr    mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
98341016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
98441016Sdfr    if (verbose)
98541016Sdfr        printf("psm%d: current command byte:%04x\n", unit, command_byte);
98641016Sdfr    if (command_byte == -1) {
98741016Sdfr        /* CONTROLLER ERROR */
98841016Sdfr        printf("psm%d: unable to get the current command byte value.\n",
98941016Sdfr            unit);
99041016Sdfr        endprobe(ENXIO);
99141016Sdfr    }
99241016Sdfr
99341016Sdfr    /*
99441016Sdfr     * disable the keyboard port while probing the aux port, which must be
99541016Sdfr     * enabled during this routine
99641016Sdfr     */
99741016Sdfr    if (!set_controller_command_byte(sc->kbdc,
99841016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
99941016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
100041016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
100141016Sdfr        /*
100241016Sdfr	 * this is CONTROLLER ERROR; I don't know how to recover
100341016Sdfr         * from this error...
100441016Sdfr	 */
100541016Sdfr        restore_controller(sc->kbdc, command_byte);
100641016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
100741016Sdfr        endprobe(ENXIO);
100841016Sdfr    }
100945789Speter    write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
101041016Sdfr
101141016Sdfr    /*
101241016Sdfr     * NOTE: `test_aux_port()' is designed to return with zero if the aux
101341016Sdfr     * port exists and is functioning. However, some controllers appears
101441016Sdfr     * to respond with zero even when the aux port doesn't exist. (It may
101541016Sdfr     * be that this is only the case when the controller DOES have the aux
101641016Sdfr     * port but the port is not wired on the motherboard.) The keyboard
101741016Sdfr     * controllers without the port, such as the original AT, are
101841016Sdfr     * supporsed to return with an error code or simply time out. In any
101941016Sdfr     * case, we have to continue probing the port even when the controller
102041016Sdfr     * passes this test.
102141016Sdfr     *
102241016Sdfr     * XXX: some controllers erroneously return the error code 1 when
102341016Sdfr     * it has the perfectly functional aux port. We have to ignore this
102441016Sdfr     * error code. Even if the controller HAS error with the aux port,
102541016Sdfr     * it will be detected later...
102641016Sdfr     * XXX: another incompatible controller returns PSM_ACK (0xfa)...
102741016Sdfr     */
102841016Sdfr    switch ((i = test_aux_port(sc->kbdc))) {
102941016Sdfr    case 1:	   /* ignore this error */
103041016Sdfr    case PSM_ACK:
103141016Sdfr        if (verbose)
103241016Sdfr	    printf("psm%d: strange result for test aux port (%d).\n",
103341016Sdfr	        unit, i);
103441016Sdfr	/* fall though */
103541016Sdfr    case 0:        /* no error */
103641016Sdfr        break;
103741016Sdfr    case -1:        /* time out */
103841016Sdfr    default:        /* error */
103941016Sdfr        recover_from_error(sc->kbdc);
104045789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
104145789Speter	    break;
104241016Sdfr        restore_controller(sc->kbdc, command_byte);
104341016Sdfr        if (verbose)
104441016Sdfr            printf("psm%d: the aux port is not functioning (%d).\n",
104541016Sdfr                unit, i);
104641016Sdfr        endprobe(ENXIO);
104741016Sdfr    }
104841016Sdfr
104945789Speter    if (sc->config & PSM_CONFIG_NORESET) {
105045789Speter	/*
105145789Speter	 * Don't try to reset the pointing device.  It may possibly be
105245789Speter	 * left in the unknown state, though...
105345789Speter	 */
105445789Speter    } else {
105545789Speter	/*
105645789Speter	 * NOTE: some controllers appears to hang the `keyboard' when the aux
105745789Speter	 * port doesn't exist and `PSMC_RESET_DEV' is issued.
105845789Speter	 */
105945789Speter	if (!reset_aux_dev(sc->kbdc)) {
106045789Speter            recover_from_error(sc->kbdc);
106145789Speter            restore_controller(sc->kbdc, command_byte);
106245789Speter            if (verbose)
106345789Speter        	printf("psm%d: failed to reset the aux device.\n", unit);
106445789Speter            endprobe(ENXIO);
106545789Speter	}
106641016Sdfr    }
106745789Speter
106841016Sdfr    /*
106941016Sdfr     * both the aux port and the aux device is functioning, see if the
107041016Sdfr     * device can be enabled. NOTE: when enabled, the device will start
107141016Sdfr     * sending data; we shall immediately disable the device once we know
107241016Sdfr     * the device can be enabled.
107341016Sdfr     */
107441016Sdfr    if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
107545789Speter        /* MOUSE ERROR */
107645789Speter	recover_from_error(sc->kbdc);
107745789Speter	restore_controller(sc->kbdc, command_byte);
107845789Speter	if (verbose)
107945789Speter	    printf("psm%d: failed to enable the aux device.\n", unit);
108041016Sdfr        endprobe(ENXIO);
108141016Sdfr    }
108241016Sdfr
108341016Sdfr    /* save the default values after reset */
108441016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
108541016Sdfr	sc->dflt_mode.rate = sc->mode.rate = stat[2];
108641016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
108741016Sdfr    } else {
108841016Sdfr	sc->dflt_mode.rate = sc->mode.rate = -1;
108941016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = -1;
109041016Sdfr    }
109141016Sdfr
109241016Sdfr    /* hardware information */
109341016Sdfr    sc->hw.iftype = MOUSE_IF_PS2;
109441016Sdfr
109541016Sdfr    /* verify the device is a mouse */
109641016Sdfr    sc->hw.hwid = get_aux_id(sc->kbdc);
109741016Sdfr    if (!is_a_mouse(sc->hw.hwid)) {
109841016Sdfr        restore_controller(sc->kbdc, command_byte);
109941016Sdfr        if (verbose)
110041016Sdfr            printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
110141016Sdfr        endprobe(ENXIO);
110241016Sdfr    }
110341016Sdfr    switch (sc->hw.hwid) {
110441016Sdfr    case PSM_BALLPOINT_ID:
110541016Sdfr        sc->hw.type = MOUSE_TRACKBALL;
110641016Sdfr        break;
110741016Sdfr    case PSM_MOUSE_ID:
110841016Sdfr    case PSM_INTELLI_ID:
110958230Syokota    case PSM_EXPLORER_ID:
111058230Syokota    case PSM_4DMOUSE_ID:
111158230Syokota    case PSM_4DPLUS_ID:
111241016Sdfr        sc->hw.type = MOUSE_MOUSE;
111341016Sdfr        break;
111441016Sdfr    default:
111541016Sdfr        sc->hw.type = MOUSE_UNKNOWN;
111641016Sdfr        break;
111741016Sdfr    }
111841016Sdfr
111945789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
112045789Speter	sc->hw.buttons = 2;
112145789Speter	i = GENERIC_MOUSE_ENTRY;
112245789Speter    } else {
112345789Speter	/* # of buttons */
112445789Speter	sc->hw.buttons = get_mouse_buttons(sc->kbdc);
112541016Sdfr
112645789Speter	/* other parameters */
112745789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
112845789Speter	    if ((*vendortype[i].probefunc)(sc)) {
112945789Speter		if (verbose >= 2)
113045789Speter		    printf("psm%d: found %s\n",
113145789Speter			   unit, model_name(vendortype[i].model));
113245789Speter		break;
113345789Speter	    }
113441016Sdfr	}
113541016Sdfr    }
113641016Sdfr
113741016Sdfr    sc->hw.model = vendortype[i].model;
113841016Sdfr
113941016Sdfr    sc->dflt_mode.level = PSM_LEVEL_BASE;
114041016Sdfr    sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
114141016Sdfr    sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
114241016Sdfr    if (sc->config & PSM_CONFIG_NOCHECKSYNC)
114341016Sdfr        sc->dflt_mode.syncmask[0] = 0;
114441016Sdfr    else
114541016Sdfr        sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
114645789Speter    if (sc->config & PSM_CONFIG_FORCETAP)
114745789Speter        sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
114841016Sdfr    sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
114941016Sdfr    sc->mode = sc->dflt_mode;
115041016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
115141016Sdfr
115241016Sdfr    /* set mouse parameters */
115348773Syokota#if 0
115448773Syokota    /*
115548773Syokota     * A version of Logitech FirstMouse+ won't report wheel movement,
115648773Syokota     * if SET_DEFAULTS is sent...  Don't use this command.
115748773Syokota     * This fix was found by Takashi Nishida.
115848773Syokota     */
115941016Sdfr    i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
116041016Sdfr    if (verbose >= 2)
116141016Sdfr	printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
116248773Syokota#endif
116341016Sdfr    if (sc->config & PSM_CONFIG_RESOLUTION) {
116441016Sdfr        sc->mode.resolution
116541016Sdfr	    = set_mouse_resolution(sc->kbdc,
116648773Syokota				   (sc->config & PSM_CONFIG_RESOLUTION) - 1);
116748773Syokota    } else if (sc->mode.resolution >= 0) {
116848773Syokota	sc->mode.resolution
116948773Syokota	    = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
117041016Sdfr    }
117148773Syokota    if (sc->mode.rate > 0) {
117248773Syokota	sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
117348773Syokota    }
117448773Syokota    set_mouse_scaling(sc->kbdc, 1);
117541016Sdfr
117641016Sdfr    /* request a data packet and extract sync. bits */
117741016Sdfr    if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
117841016Sdfr        printf("psm%d: failed to get data.\n", unit);
117941016Sdfr        sc->mode.syncmask[0] = 0;
118041016Sdfr    } else {
118141016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
118241016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
118341016Sdfr	empty_aux_buffer(sc->kbdc, 5);
118441016Sdfr    }
118541016Sdfr
118641016Sdfr    /* just check the status of the mouse */
118741016Sdfr    /*
118841016Sdfr     * NOTE: XXX there are some arcane controller/mouse combinations out
118941016Sdfr     * there, which hung the controller unless there is data transmission
119041016Sdfr     * after ACK from the mouse.
119141016Sdfr     */
119241016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
119341016Sdfr        printf("psm%d: failed to get status.\n", unit);
119441016Sdfr    } else {
119541016Sdfr	/*
119641016Sdfr	 * When in its native mode, some mice operate with different
119741016Sdfr	 * default parameters than in the PS/2 compatible mode.
119841016Sdfr	 */
119941016Sdfr        sc->dflt_mode.rate = sc->mode.rate = stat[2];
120041016Sdfr        sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
120141016Sdfr     }
120241016Sdfr
120341016Sdfr    /* disable the aux port for now... */
120441016Sdfr    if (!set_controller_command_byte(sc->kbdc,
120541016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
120641016Sdfr            (command_byte & KBD_KBD_CONTROL_BITS)
120741016Sdfr                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
120841016Sdfr        /*
120941016Sdfr	 * this is CONTROLLER ERROR; I don't know the proper way to
121041016Sdfr         * recover from this error...
121141016Sdfr	 */
121241016Sdfr        restore_controller(sc->kbdc, command_byte);
121341016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
121441016Sdfr        endprobe(ENXIO);
121541016Sdfr    }
121641016Sdfr
121741016Sdfr    /* done */
121841016Sdfr    kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
121941016Sdfr    kbdc_lock(sc->kbdc, FALSE);
122041016Sdfr    return (0);
122141016Sdfr}
122241016Sdfr
122341016Sdfrstatic int
122441016Sdfrpsmattach(device_t dev)
122541016Sdfr{
122641016Sdfr    int unit = device_get_unit(dev);
122741016Sdfr    struct psm_softc *sc = device_get_softc(dev);
122858230Syokota    int error;
122958230Syokota    int rid;
123041016Sdfr
123141016Sdfr    if (sc == NULL)    /* shouldn't happen */
123241016Sdfr	return (ENXIO);
123341016Sdfr
123441016Sdfr    /* Setup initial state */
123541016Sdfr    sc->state = PSM_VALID;
123658230Syokota    callout_handle_init(&sc->callout);
123741016Sdfr
123858230Syokota    /* Setup our interrupt handler */
123983147Syokota    rid = KBDC_RID_AUX;
124083147Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
124183147Syokota				  RF_SHAREABLE | RF_ACTIVE);
124258230Syokota    if (sc->intr == NULL)
124358230Syokota	return (ENXIO);
124483147Syokota    error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
124558230Syokota    if (error) {
124658230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
124758230Syokota	return (error);
124858230Syokota    }
124958230Syokota
125041016Sdfr    /* Done */
125158230Syokota    sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
125258230Syokota		       "psm%d", unit);
125358230Syokota    sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
125458230Syokota			"bpsm%d", unit);
125541016Sdfr
125641016Sdfr    if (!verbose) {
125741016Sdfr        printf("psm%d: model %s, device ID %d\n",
125848778Syokota	    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
125941016Sdfr    } else {
126048778Syokota        printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
126148778Syokota	    unit, model_name(sc->hw.model),
126248778Syokota	    sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
126341016Sdfr	printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
126441016Sdfr	    unit, sc->config, sc->flags, sc->mode.packetsize);
126541016Sdfr	printf("psm%d: syncmask:%02x, syncbits:%02x\n",
126641016Sdfr	    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
126741016Sdfr    }
126841016Sdfr
126941016Sdfr    if (bootverbose)
127041016Sdfr        --verbose;
127141016Sdfr
127241016Sdfr    return (0);
127341016Sdfr}
127441016Sdfr
127541016Sdfrstatic int
127658230Syokotapsmdetach(device_t dev)
127758230Syokota{
127858230Syokota    struct psm_softc *sc;
127958230Syokota    int rid;
128058230Syokota
128158230Syokota    sc = device_get_softc(dev);
128258230Syokota    if (sc->state & PSM_OPEN)
128358230Syokota	return EBUSY;
128458230Syokota
128583147Syokota    rid = KBDC_RID_AUX;
128683147Syokota    bus_teardown_intr(dev, sc->intr, sc->ih);
128758230Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
128858230Syokota
128958230Syokota    destroy_dev(sc->dev);
129058230Syokota    destroy_dev(sc->bdev);
129158230Syokota
129258230Syokota    return 0;
129358230Syokota}
129458230Syokota
129558230Syokotastatic int
129683366Sjulianpsmopen(dev_t dev, int flag, int fmt, struct thread *td)
129741016Sdfr{
129841016Sdfr    int unit = PSM_UNIT(dev);
129941016Sdfr    struct psm_softc *sc;
130041016Sdfr    int command_byte;
130141016Sdfr    int err;
130241016Sdfr    int s;
130341016Sdfr
130441016Sdfr    /* Get device data */
130541016Sdfr    sc = PSM_SOFTC(unit);
130641016Sdfr    if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
130741016Sdfr	/* the device is no longer valid/functioning */
130841016Sdfr        return (ENXIO);
130941016Sdfr
131041016Sdfr    /* Disallow multiple opens */
131141016Sdfr    if (sc->state & PSM_OPEN)
131241016Sdfr        return (EBUSY);
131341016Sdfr
131441016Sdfr    device_busy(devclass_get_device(psm_devclass, unit));
131541016Sdfr
131641016Sdfr    /* Initialize state */
131741016Sdfr    sc->rsel.si_flags = 0;
131841016Sdfr    sc->rsel.si_pid = 0;
131941016Sdfr    sc->mode.level = sc->dflt_mode.level;
132041016Sdfr    sc->mode.protocol = sc->dflt_mode.protocol;
132158230Syokota    sc->watchdog = FALSE;
132241016Sdfr
132341016Sdfr    /* flush the event queue */
132441016Sdfr    sc->queue.count = 0;
132541016Sdfr    sc->queue.head = 0;
132641016Sdfr    sc->queue.tail = 0;
132741016Sdfr    sc->status.flags = 0;
132841016Sdfr    sc->status.button = 0;
132941016Sdfr    sc->status.obutton = 0;
133041016Sdfr    sc->status.dx = 0;
133141016Sdfr    sc->status.dy = 0;
133241016Sdfr    sc->status.dz = 0;
133341016Sdfr    sc->button = 0;
133441016Sdfr
133541016Sdfr    /* empty input buffer */
133641016Sdfr    bzero(sc->ipacket, sizeof(sc->ipacket));
133741016Sdfr    sc->inputbytes = 0;
133884880Syokota    sc->syncerrors = 0;
133941016Sdfr
134041016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
134141016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
134241016Sdfr	return (EIO);
134341016Sdfr
134441016Sdfr    /* save the current controller command byte */
134541016Sdfr    s = spltty();
134641016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
134741016Sdfr
134841016Sdfr    /* enable the aux port and temporalily disable the keyboard */
134941016Sdfr    if ((command_byte == -1)
135041016Sdfr        || !set_controller_command_byte(sc->kbdc,
135141016Sdfr	    kbdc_get_device_mask(sc->kbdc),
135241016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
135341016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
135441016Sdfr        /* CONTROLLER ERROR; do you know how to get out of this? */
135541016Sdfr        kbdc_lock(sc->kbdc, FALSE);
135641016Sdfr	splx(s);
135741016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
135841016Sdfr	    unit);
135941016Sdfr	return (EIO);
136041016Sdfr    }
136141016Sdfr    /*
136241016Sdfr     * Now that the keyboard controller is told not to generate
136341016Sdfr     * the keyboard and mouse interrupts, call `splx()' to allow
136441016Sdfr     * the other tty interrupts. The clock interrupt may also occur,
136541016Sdfr     * but timeout routines will be blocked by the poll flag set
136641016Sdfr     * via `kbdc_lock()'
136741016Sdfr     */
136841016Sdfr    splx(s);
136941016Sdfr
137041016Sdfr    /* enable the mouse device */
137184880Syokota    err = doopen(sc, command_byte);
137241016Sdfr
137341016Sdfr    /* done */
137441016Sdfr    if (err == 0)
137541016Sdfr        sc->state |= PSM_OPEN;
137641016Sdfr    kbdc_lock(sc->kbdc, FALSE);
137741016Sdfr    return (err);
137841016Sdfr}
137941016Sdfr
138041016Sdfrstatic int
138183366Sjulianpsmclose(dev_t dev, int flag, int fmt, struct thread *td)
138241016Sdfr{
138341016Sdfr    int unit = PSM_UNIT(dev);
138441016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
138541016Sdfr    int stat[3];
138641016Sdfr    int command_byte;
138741016Sdfr    int s;
138841016Sdfr
138941016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
139041016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
139141016Sdfr	return (EIO);
139241016Sdfr
139341016Sdfr    /* save the current controller command byte */
139441016Sdfr    s = spltty();
139541016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
139641016Sdfr    if (command_byte == -1) {
139741016Sdfr        kbdc_lock(sc->kbdc, FALSE);
139841016Sdfr	splx(s);
139941016Sdfr	return (EIO);
140041016Sdfr    }
140141016Sdfr
140241016Sdfr    /* disable the aux interrupt and temporalily disable the keyboard */
140341016Sdfr    if (!set_controller_command_byte(sc->kbdc,
140441016Sdfr	    kbdc_get_device_mask(sc->kbdc),
140541016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
140641016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
140741016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
140858230Syokota	    unit);
140941016Sdfr	/* CONTROLLER ERROR;
141041016Sdfr	 * NOTE: we shall force our way through. Because the only
141141016Sdfr	 * ill effect we shall see is that we may not be able
141241016Sdfr	 * to read ACK from the mouse, and it doesn't matter much
141341016Sdfr	 * so long as the mouse will accept the DISABLE command.
141441016Sdfr	 */
141541016Sdfr    }
141641016Sdfr    splx(s);
141741016Sdfr
141858230Syokota    /* stop the watchdog timer */
141984880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
142058230Syokota    callout_handle_init(&sc->callout);
142158230Syokota
142241016Sdfr    /* remove anything left in the output buffer */
142341016Sdfr    empty_aux_buffer(sc->kbdc, 10);
142441016Sdfr
142541016Sdfr    /* disable the aux device, port and interrupt */
142641016Sdfr    if (sc->state & PSM_VALID) {
142741016Sdfr        if (!disable_aux_dev(sc->kbdc)) {
142841016Sdfr	    /* MOUSE ERROR;
142941016Sdfr	     * NOTE: we don't return error and continue, pretending
143041016Sdfr	     * we have successfully disabled the device. It's OK because
143141016Sdfr	     * the interrupt routine will discard any data from the mouse
143241016Sdfr	     * hereafter.
143341016Sdfr	     */
143441016Sdfr	    log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
143558230Syokota		unit);
143641016Sdfr        }
143741016Sdfr
143841016Sdfr        if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
143941016Sdfr            log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
144058230Syokota		unit);
144141016Sdfr    }
144241016Sdfr
144341016Sdfr    if (!set_controller_command_byte(sc->kbdc,
144441016Sdfr	    kbdc_get_device_mask(sc->kbdc),
144541016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
144641016Sdfr	        | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
144741016Sdfr	/* CONTROLLER ERROR;
144841016Sdfr	 * we shall ignore this error; see the above comment.
144941016Sdfr	 */
145041016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
145158230Syokota	    unit);
145241016Sdfr    }
145341016Sdfr
145441016Sdfr    /* remove anything left in the output buffer */
145541016Sdfr    empty_aux_buffer(sc->kbdc, 10);
145641016Sdfr
145741016Sdfr    /* close is almost always successful */
145841016Sdfr    sc->state &= ~PSM_OPEN;
145941016Sdfr    kbdc_lock(sc->kbdc, FALSE);
146041016Sdfr    device_unbusy(devclass_get_device(psm_devclass, unit));
146141016Sdfr    return (0);
146241016Sdfr}
146341016Sdfr
146441016Sdfrstatic int
146541016Sdfrtame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
146641016Sdfr{
146741016Sdfr    static unsigned char butmapps2[8] = {
146841016Sdfr        0,
146941016Sdfr        MOUSE_PS2_BUTTON1DOWN,
147041016Sdfr        MOUSE_PS2_BUTTON2DOWN,
147141016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
147241016Sdfr        MOUSE_PS2_BUTTON3DOWN,
147341016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
147441016Sdfr        MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
147541016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
147641016Sdfr    };
147741016Sdfr    static unsigned char butmapmsc[8] = {
147841016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
147941016Sdfr        MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
148041016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
148141016Sdfr        MOUSE_MSC_BUTTON3UP,
148241016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
148341016Sdfr        MOUSE_MSC_BUTTON2UP,
148441016Sdfr        MOUSE_MSC_BUTTON1UP,
148541016Sdfr        0,
148641016Sdfr    };
148741016Sdfr    int mapped;
148841016Sdfr    int i;
148941016Sdfr
149041016Sdfr    if (sc->mode.level == PSM_LEVEL_BASE) {
149141016Sdfr        mapped = status->button & ~MOUSE_BUTTON4DOWN;
149241016Sdfr        if (status->button & MOUSE_BUTTON4DOWN)
149341016Sdfr	    mapped |= MOUSE_BUTTON1DOWN;
149441016Sdfr        status->button = mapped;
149541016Sdfr        buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
149641016Sdfr        i = max(min(status->dx, 255), -256);
149741016Sdfr	if (i < 0)
149841016Sdfr	    buf[0] |= MOUSE_PS2_XNEG;
149941016Sdfr        buf[1] = i;
150041016Sdfr        i = max(min(status->dy, 255), -256);
150141016Sdfr	if (i < 0)
150241016Sdfr	    buf[0] |= MOUSE_PS2_YNEG;
150341016Sdfr        buf[2] = i;
150441016Sdfr	return MOUSE_PS2_PACKETSIZE;
150541016Sdfr    } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
150641016Sdfr        buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
150741016Sdfr        i = max(min(status->dx, 255), -256);
150841016Sdfr        buf[1] = i >> 1;
150941016Sdfr        buf[3] = i - buf[1];
151041016Sdfr        i = max(min(status->dy, 255), -256);
151141016Sdfr        buf[2] = i >> 1;
151241016Sdfr        buf[4] = i - buf[2];
151341016Sdfr        i = max(min(status->dz, 127), -128);
151441016Sdfr        buf[5] = (i >> 1) & 0x7f;
151541016Sdfr        buf[6] = (i - (i >> 1)) & 0x7f;
151641016Sdfr        buf[7] = (~status->button >> 3) & 0x7f;
151741016Sdfr	return MOUSE_SYS_PACKETSIZE;
151841016Sdfr    }
151941016Sdfr    return sc->inputbytes;;
152041016Sdfr}
152141016Sdfr
152241016Sdfrstatic int
152341016Sdfrpsmread(dev_t dev, struct uio *uio, int flag)
152441016Sdfr{
152541016Sdfr    register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
152641016Sdfr    unsigned char buf[PSM_SMALLBUFSIZE];
152741016Sdfr    int error = 0;
152841016Sdfr    int s;
152941016Sdfr    int l;
153041016Sdfr
153141016Sdfr    if ((sc->state & PSM_VALID) == 0)
153241016Sdfr	return EIO;
153341016Sdfr
153441016Sdfr    /* block until mouse activity occured */
153541016Sdfr    s = spltty();
153641016Sdfr    while (sc->queue.count <= 0) {
153741016Sdfr        if (PSM_NBLOCKIO(dev)) {
153841016Sdfr            splx(s);
153941016Sdfr            return EWOULDBLOCK;
154041016Sdfr        }
154141016Sdfr        sc->state |= PSM_ASLP;
154241016Sdfr        error = tsleep((caddr_t) sc, PZERO | PCATCH, "psmrea", 0);
154341016Sdfr        sc->state &= ~PSM_ASLP;
154441016Sdfr        if (error) {
154541016Sdfr            splx(s);
154641016Sdfr            return error;
154741016Sdfr        } else if ((sc->state & PSM_VALID) == 0) {
154841016Sdfr            /* the device disappeared! */
154941016Sdfr            splx(s);
155041016Sdfr            return EIO;
155141016Sdfr	}
155241016Sdfr    }
155341016Sdfr    splx(s);
155441016Sdfr
155541016Sdfr    /* copy data to the user land */
155641016Sdfr    while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
155741016Sdfr        s = spltty();
155841016Sdfr	l = min(sc->queue.count, uio->uio_resid);
155941016Sdfr	if (l > sizeof(buf))
156041016Sdfr	    l = sizeof(buf);
156141016Sdfr	if (l > sizeof(sc->queue.buf) - sc->queue.head) {
156241016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
156341016Sdfr		sizeof(sc->queue.buf) - sc->queue.head);
156441016Sdfr	    bcopy(&sc->queue.buf[0],
156541016Sdfr		&buf[sizeof(sc->queue.buf) - sc->queue.head],
156641016Sdfr		l - (sizeof(sc->queue.buf) - sc->queue.head));
156741016Sdfr	} else {
156841016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
156941016Sdfr	}
157041016Sdfr	sc->queue.count -= l;
157141016Sdfr	sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
157241016Sdfr        splx(s);
157341016Sdfr        error = uiomove(buf, l, uio);
157441016Sdfr        if (error)
157541016Sdfr	    break;
157641016Sdfr    }
157741016Sdfr
157841016Sdfr    return error;
157941016Sdfr}
158041016Sdfr
158141016Sdfrstatic int
158241016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
158341016Sdfr{
158441016Sdfr    int s;
158541016Sdfr
158641016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
158741016Sdfr	return EIO;
158841016Sdfr
158941016Sdfr    s = spltty();
159041016Sdfr    *c = get_controller_command_byte(sc->kbdc);
159141016Sdfr    if ((*c == -1)
159241016Sdfr	|| !set_controller_command_byte(sc->kbdc,
159341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
159441016Sdfr            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
159541016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
159641016Sdfr        /* this is CONTROLLER ERROR */
159741016Sdfr	splx(s);
159841016Sdfr        kbdc_lock(sc->kbdc, FALSE);
159941016Sdfr	return EIO;
160041016Sdfr    }
160141016Sdfr
160241016Sdfr    /*
160341016Sdfr     * The device may be in the middle of status data transmission.
160441016Sdfr     * The transmission will be interrupted, thus, incomplete status
160541016Sdfr     * data must be discarded. Although the aux interrupt is disabled
160641016Sdfr     * at the keyboard controller level, at most one aux interrupt
160741016Sdfr     * may have already been pending and a data byte is in the
160841016Sdfr     * output buffer; throw it away. Note that the second argument
160941016Sdfr     * to `empty_aux_buffer()' is zero, so that the call will just
161041016Sdfr     * flush the internal queue.
161141016Sdfr     * `psmintr()' will be invoked after `splx()' if an interrupt is
161241016Sdfr     * pending; it will see no data and returns immediately.
161341016Sdfr     */
161441016Sdfr    empty_aux_buffer(sc->kbdc, 0);	/* flush the queue */
161541016Sdfr    read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
161641016Sdfr    sc->inputbytes = 0;
161741016Sdfr    splx(s);
161841016Sdfr
161941016Sdfr    return 0;
162041016Sdfr}
162141016Sdfr
162241016Sdfrstatic int
162341016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
162441016Sdfr{
162541016Sdfr    int error = 0;
162641016Sdfr
162741016Sdfr    /*
162841016Sdfr     * We may have seen a part of status data during `set_mouse_XXX()'.
162941016Sdfr     * they have been queued; flush it.
163041016Sdfr     */
163141016Sdfr    empty_aux_buffer(sc->kbdc, 0);
163241016Sdfr
163341016Sdfr    /* restore ports and interrupt */
163441016Sdfr    if (!set_controller_command_byte(sc->kbdc,
163541016Sdfr            kbdc_get_device_mask(sc->kbdc),
163641016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
163741016Sdfr        /* CONTROLLER ERROR; this is serious, we may have
163841016Sdfr         * been left with the inaccessible keyboard and
163941016Sdfr         * the disabled mouse interrupt.
164041016Sdfr         */
164141016Sdfr        error = EIO;
164241016Sdfr    }
164341016Sdfr
164441016Sdfr    kbdc_lock(sc->kbdc, FALSE);
164541016Sdfr    return error;
164641016Sdfr}
164741016Sdfr
164841016Sdfrstatic int
164983366Sjulianpsmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
165041016Sdfr{
165141016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
165241016Sdfr    mousemode_t mode;
165341016Sdfr    mousestatus_t status;
165441016Sdfr#if (defined(MOUSE_GETVARS))
165541016Sdfr    mousevar_t *var;
165641016Sdfr#endif
165741016Sdfr    mousedata_t *data;
165841016Sdfr    int stat[3];
165941016Sdfr    int command_byte;
166041016Sdfr    int error = 0;
166141016Sdfr    int s;
166241016Sdfr
166341016Sdfr    /* Perform IOCTL command */
166441016Sdfr    switch (cmd) {
166541016Sdfr
166641016Sdfr    case OLD_MOUSE_GETHWINFO:
166741016Sdfr	s = spltty();
166841016Sdfr        ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
166941016Sdfr        ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
167041016Sdfr        ((old_mousehw_t *)addr)->type = sc->hw.type;
167148778Syokota        ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
167241016Sdfr	splx(s);
167341016Sdfr        break;
167441016Sdfr
167541016Sdfr    case MOUSE_GETHWINFO:
167641016Sdfr	s = spltty();
167741016Sdfr        *(mousehw_t *)addr = sc->hw;
167841016Sdfr	if (sc->mode.level == PSM_LEVEL_BASE)
167941016Sdfr	    ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
168041016Sdfr	splx(s);
168141016Sdfr        break;
168241016Sdfr
168341016Sdfr    case OLD_MOUSE_GETMODE:
168441016Sdfr	s = spltty();
168541016Sdfr	switch (sc->mode.level) {
168641016Sdfr	case PSM_LEVEL_BASE:
168741016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
168841016Sdfr	    break;
168941016Sdfr	case PSM_LEVEL_STANDARD:
169041016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
169141016Sdfr	    break;
169241016Sdfr	case PSM_LEVEL_NATIVE:
169341016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
169441016Sdfr	    break;
169541016Sdfr	}
169641016Sdfr        ((old_mousemode_t *)addr)->rate = sc->mode.rate;
169741016Sdfr        ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
169841016Sdfr        ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
169941016Sdfr	splx(s);
170041016Sdfr        break;
170141016Sdfr
170241016Sdfr    case MOUSE_GETMODE:
170341016Sdfr	s = spltty();
170441016Sdfr        *(mousemode_t *)addr = sc->mode;
170541016Sdfr        ((mousemode_t *)addr)->resolution =
170641016Sdfr	    MOUSE_RES_LOW - sc->mode.resolution;
170741016Sdfr	switch (sc->mode.level) {
170841016Sdfr	case PSM_LEVEL_BASE:
170941016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
171041016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
171141016Sdfr	    break;
171241016Sdfr	case PSM_LEVEL_STANDARD:
171341016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
171441016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
171541016Sdfr	    ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
171641016Sdfr	    ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
171741016Sdfr	    break;
171841016Sdfr	case PSM_LEVEL_NATIVE:
171941016Sdfr	    /* FIXME: this isn't quite correct... XXX */
172041016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
172141016Sdfr	    break;
172241016Sdfr	}
172341016Sdfr	splx(s);
172441016Sdfr        break;
172541016Sdfr
172641016Sdfr    case OLD_MOUSE_SETMODE:
172741016Sdfr    case MOUSE_SETMODE:
172841016Sdfr	if (cmd == OLD_MOUSE_SETMODE) {
172941016Sdfr	    mode.rate = ((old_mousemode_t *)addr)->rate;
173041016Sdfr	    /*
173141016Sdfr	     * resolution  old I/F   new I/F
173241016Sdfr	     * default        0         0
173341016Sdfr	     * low            1        -2
173441016Sdfr	     * medium low     2        -3
173541016Sdfr	     * medium high    3        -4
173641016Sdfr	     * high           4        -5
173741016Sdfr	     */
173841016Sdfr	    if (((old_mousemode_t *)addr)->resolution > 0)
173941016Sdfr	        mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
174041016Sdfr	    mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
174141016Sdfr	    mode.level = -1;
174241016Sdfr	} else {
174341016Sdfr	    mode = *(mousemode_t *)addr;
174441016Sdfr	}
174541016Sdfr
174641016Sdfr	/* adjust and validate parameters. */
174741016Sdfr	if (mode.rate > UCHAR_MAX)
174841016Sdfr	    return EINVAL;
174941016Sdfr        if (mode.rate == 0)
175041016Sdfr            mode.rate = sc->dflt_mode.rate;
175141016Sdfr	else if (mode.rate == -1)
175241016Sdfr	    /* don't change the current setting */
175341016Sdfr	    ;
175441016Sdfr	else if (mode.rate < 0)
175541016Sdfr	    return EINVAL;
175641016Sdfr	if (mode.resolution >= UCHAR_MAX)
175741016Sdfr	    return EINVAL;
175841016Sdfr	if (mode.resolution >= 200)
175941016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
176041016Sdfr	else if (mode.resolution >= 100)
176141016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
176241016Sdfr	else if (mode.resolution >= 50)
176341016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
176441016Sdfr	else if (mode.resolution > 0)
176541016Sdfr	    mode.resolution = MOUSE_RES_LOW;
176641016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
176741016Sdfr            mode.resolution = sc->dflt_mode.resolution;
176841016Sdfr        else if (mode.resolution == -1)
176941016Sdfr	    /* don't change the current setting */
177041016Sdfr	    ;
177141016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
177241016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
177341016Sdfr	if (mode.level == -1)
177441016Sdfr	    /* don't change the current setting */
177541016Sdfr	    mode.level = sc->mode.level;
177641016Sdfr	else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
177741016Sdfr	    return EINVAL;
177841016Sdfr        if (mode.accelfactor == -1)
177941016Sdfr	    /* don't change the current setting */
178041016Sdfr	    mode.accelfactor = sc->mode.accelfactor;
178141016Sdfr        else if (mode.accelfactor < 0)
178241016Sdfr	    return EINVAL;
178341016Sdfr
178441016Sdfr	/* don't allow anybody to poll the keyboard controller */
178541016Sdfr	error = block_mouse_data(sc, &command_byte);
178641016Sdfr	if (error)
178741016Sdfr            return error;
178841016Sdfr
178941016Sdfr        /* set mouse parameters */
179041016Sdfr	if (mode.rate > 0)
179141016Sdfr	    mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
179241016Sdfr	if (mode.resolution >= 0)
179341016Sdfr	    mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
179441016Sdfr	set_mouse_scaling(sc->kbdc, 1);
179541016Sdfr	get_mouse_status(sc->kbdc, stat, 0, 3);
179641016Sdfr
179741016Sdfr        s = spltty();
179841016Sdfr    	sc->mode.rate = mode.rate;
179941016Sdfr    	sc->mode.resolution = mode.resolution;
180041016Sdfr    	sc->mode.accelfactor = mode.accelfactor;
180141016Sdfr    	sc->mode.level = mode.level;
180241016Sdfr        splx(s);
180341016Sdfr
180441016Sdfr	unblock_mouse_data(sc, command_byte);
180541016Sdfr        break;
180641016Sdfr
180741016Sdfr    case MOUSE_GETLEVEL:
180841016Sdfr	*(int *)addr = sc->mode.level;
180941016Sdfr        break;
181041016Sdfr
181141016Sdfr    case MOUSE_SETLEVEL:
181241016Sdfr	if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
181341016Sdfr	    return EINVAL;
181441016Sdfr	sc->mode.level = *(int *)addr;
181541016Sdfr        break;
181641016Sdfr
181741016Sdfr    case MOUSE_GETSTATUS:
181841016Sdfr        s = spltty();
181941016Sdfr	status = sc->status;
182041016Sdfr	sc->status.flags = 0;
182141016Sdfr	sc->status.obutton = sc->status.button;
182241016Sdfr	sc->status.button = 0;
182341016Sdfr	sc->status.dx = 0;
182441016Sdfr	sc->status.dy = 0;
182541016Sdfr	sc->status.dz = 0;
182641016Sdfr        splx(s);
182741016Sdfr        *(mousestatus_t *)addr = status;
182841016Sdfr        break;
182941016Sdfr
183041016Sdfr#if (defined(MOUSE_GETVARS))
183141016Sdfr    case MOUSE_GETVARS:
183241016Sdfr	var = (mousevar_t *)addr;
183341016Sdfr	bzero(var, sizeof(*var));
183441016Sdfr	s = spltty();
183541016Sdfr        var->var[0] = MOUSE_VARS_PS2_SIG;
183641016Sdfr        var->var[1] = sc->config;
183741016Sdfr        var->var[2] = sc->flags;
183841016Sdfr	splx(s);
183941016Sdfr        break;
184041016Sdfr
184141016Sdfr    case MOUSE_SETVARS:
184241016Sdfr	return ENODEV;
184341016Sdfr#endif /* MOUSE_GETVARS */
184441016Sdfr
184541016Sdfr    case MOUSE_READSTATE:
184641016Sdfr    case MOUSE_READDATA:
184741016Sdfr	data = (mousedata_t *)addr;
184841016Sdfr	if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
184941016Sdfr	    return EINVAL;
185041016Sdfr
185141016Sdfr	error = block_mouse_data(sc, &command_byte);
185241016Sdfr	if (error)
185341016Sdfr            return error;
185441016Sdfr        if ((data->len = get_mouse_status(sc->kbdc, data->buf,
185541016Sdfr		(cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
185641016Sdfr            error = EIO;
185741016Sdfr	unblock_mouse_data(sc, command_byte);
185841016Sdfr	break;
185941016Sdfr
186041016Sdfr#if (defined(MOUSE_SETRESOLUTION))
186141016Sdfr    case MOUSE_SETRESOLUTION:
186241016Sdfr	mode.resolution = *(int *)addr;
186341016Sdfr	if (mode.resolution >= UCHAR_MAX)
186441016Sdfr	    return EINVAL;
186541016Sdfr	else if (mode.resolution >= 200)
186641016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
186741016Sdfr	else if (mode.resolution >= 100)
186841016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
186941016Sdfr	else if (mode.resolution >= 50)
187041016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
187141016Sdfr	else if (mode.resolution > 0)
187241016Sdfr	    mode.resolution = MOUSE_RES_LOW;
187341016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
187441016Sdfr            mode.resolution = sc->dflt_mode.resolution;
187541016Sdfr        else if (mode.resolution == -1)
187641016Sdfr	    mode.resolution = sc->mode.resolution;
187741016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
187841016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
187941016Sdfr
188041016Sdfr	error = block_mouse_data(sc, &command_byte);
188141016Sdfr	if (error)
188241016Sdfr            return error;
188341016Sdfr        sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
188441016Sdfr	if (sc->mode.resolution != mode.resolution)
188541016Sdfr	    error = EIO;
188641016Sdfr	unblock_mouse_data(sc, command_byte);
188741016Sdfr        break;
188841016Sdfr#endif /* MOUSE_SETRESOLUTION */
188941016Sdfr
189041016Sdfr#if (defined(MOUSE_SETRATE))
189141016Sdfr    case MOUSE_SETRATE:
189241016Sdfr	mode.rate = *(int *)addr;
189341016Sdfr	if (mode.rate > UCHAR_MAX)
189441016Sdfr	    return EINVAL;
189541016Sdfr        if (mode.rate == 0)
189641016Sdfr            mode.rate = sc->dflt_mode.rate;
189741016Sdfr	else if (mode.rate < 0)
189841016Sdfr	    mode.rate = sc->mode.rate;
189941016Sdfr
190041016Sdfr	error = block_mouse_data(sc, &command_byte);
190141016Sdfr	if (error)
190241016Sdfr            return error;
190341016Sdfr        sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
190441016Sdfr	if (sc->mode.rate != mode.rate)
190541016Sdfr	    error = EIO;
190641016Sdfr	unblock_mouse_data(sc, command_byte);
190741016Sdfr        break;
190841016Sdfr#endif /* MOUSE_SETRATE */
190941016Sdfr
191041016Sdfr#if (defined(MOUSE_SETSCALING))
191141016Sdfr    case MOUSE_SETSCALING:
191241016Sdfr	if ((*(int *)addr <= 0) || (*(int *)addr > 2))
191341016Sdfr	    return EINVAL;
191441016Sdfr
191541016Sdfr	error = block_mouse_data(sc, &command_byte);
191641016Sdfr	if (error)
191741016Sdfr            return error;
191841016Sdfr        if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
191941016Sdfr	    error = EIO;
192041016Sdfr	unblock_mouse_data(sc, command_byte);
192141016Sdfr        break;
192241016Sdfr#endif /* MOUSE_SETSCALING */
192341016Sdfr
192441016Sdfr#if (defined(MOUSE_GETHWID))
192541016Sdfr    case MOUSE_GETHWID:
192641016Sdfr	error = block_mouse_data(sc, &command_byte);
192741016Sdfr	if (error)
192841016Sdfr            return error;
192948778Syokota        sc->hw.hwid &= ~0x00ff;
193048778Syokota        sc->hw.hwid |= get_aux_id(sc->kbdc);
193148778Syokota	*(int *)addr = sc->hw.hwid & 0x00ff;
193241016Sdfr	unblock_mouse_data(sc, command_byte);
193341016Sdfr        break;
193441016Sdfr#endif /* MOUSE_GETHWID */
193541016Sdfr
193641016Sdfr    default:
193741016Sdfr	return ENOTTY;
193841016Sdfr    }
193941016Sdfr
194041016Sdfr    return error;
194141016Sdfr}
194241016Sdfr
194341016Sdfrstatic void
194458230Syokotapsmtimeout(void *arg)
194558230Syokota{
194658230Syokota    struct psm_softc *sc;
194765045Syokota    int s;
194858230Syokota
194984880Syokota    sc = (struct psm_softc *)arg;
195065045Syokota    s = spltty();
195163746Syokota    if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
195258230Syokota	if (verbose >= 4)
195384880Syokota	    log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
195458230Syokota	psmintr(sc);
195563746Syokota	kbdc_lock(sc->kbdc, FALSE);
195658230Syokota    }
195758230Syokota    sc->watchdog = TRUE;
195865045Syokota    splx(s);
195984880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
196058230Syokota}
196158230Syokota
196258230Syokotastatic void
196341016Sdfrpsmintr(void *arg)
196441016Sdfr{
196541016Sdfr    /*
196641016Sdfr     * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
196741016Sdfr     * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
196841016Sdfr     */
196941016Sdfr    static int butmap[8] = {
197041016Sdfr        0,
197141016Sdfr	MOUSE_BUTTON1DOWN,
197241016Sdfr	MOUSE_BUTTON3DOWN,
197341016Sdfr	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
197441016Sdfr	MOUSE_BUTTON2DOWN,
197541016Sdfr	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
197641016Sdfr	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
197741016Sdfr        MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
197841016Sdfr    };
197949965Syokota    static int butmap_versapad[8] = {
198049965Syokota	0,
198149965Syokota	MOUSE_BUTTON3DOWN,
198249965Syokota	0,
198349965Syokota	MOUSE_BUTTON3DOWN,
198449965Syokota	MOUSE_BUTTON1DOWN,
198549965Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
198649965Syokota	MOUSE_BUTTON1DOWN,
198749965Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
198849965Syokota    };
198941016Sdfr    register struct psm_softc *sc = arg;
199041016Sdfr    mousestatus_t ms;
199184880Syokota    struct timeval tv;
199241016Sdfr    int x, y, z;
199341016Sdfr    int c;
199441016Sdfr    int l;
199549965Syokota    int x0, y0;
199641016Sdfr
199741016Sdfr    /* read until there is nothing to read */
199841016Sdfr    while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
199941016Sdfr
200041016Sdfr        /* discard the byte if the device is not open */
200141016Sdfr        if ((sc->state & PSM_OPEN) == 0)
200241016Sdfr            continue;
200341016Sdfr
200484880Syokota	getmicrouptime(&tv);
200584880Syokota	if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
200684880Syokota	    log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
200784880Syokota	    sc->inputbytes = 0;
200884880Syokota	    sc->syncerrors = 0;
200984880Syokota	}
201084880Syokota	sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
201184880Syokota	sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
201284880Syokota	timevaladd(&sc->inputtimeout, &tv);
201384880Syokota
201441016Sdfr        sc->ipacket[sc->inputbytes++] = c;
201541016Sdfr        if (sc->inputbytes < sc->mode.packetsize)
201641016Sdfr	    continue;
201741016Sdfr
201841016Sdfr#if 0
201941016Sdfr        log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
202041016Sdfr	    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
202141016Sdfr	    sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
202241016Sdfr#endif
202341016Sdfr
202441016Sdfr	c = sc->ipacket[0];
202541016Sdfr
202663746Syokota	if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
202763746Syokota            log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
202863746Syokota		c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
202984880Syokota	    ++sc->syncerrors;
203084880Syokota	    if (sc->syncerrors < sc->mode.packetsize) {
203184880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
203284880Syokota		--sc->inputbytes;
203384880Syokota		bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
203484880Syokota	    } else if (sc->syncerrors == sc->mode.packetsize) {
203569439Syokota		log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
203684880Syokota		sc->inputbytes = 0;
203769439Syokota		disable_aux_dev(sc->kbdc);
203869439Syokota		enable_aux_dev(sc->kbdc);
203984880Syokota	    } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
204084880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
204184880Syokota		--sc->inputbytes;
204284880Syokota		bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
204384880Syokota	    } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
204484880Syokota		log(LOG_DEBUG, "psmintr: reset the mouse.\n");
204584880Syokota		reinitialize(sc, TRUE);
204669439Syokota	    }
204784880Syokota	    continue;
204863746Syokota	}
204963746Syokota
205049965Syokota	/*
205141016Sdfr	 * A kludge for Kensington device!
205241016Sdfr	 * The MSB of the horizontal count appears to be stored in
205358230Syokota	 * a strange place.
205441016Sdfr	 */
205558230Syokota	if (sc->hw.model == MOUSE_MODEL_THINK)
205649965Syokota	    sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
205741016Sdfr
205841016Sdfr        /* ignore the overflow bits... */
205941016Sdfr        x = (c & MOUSE_PS2_XNEG) ?  sc->ipacket[1] - 256 : sc->ipacket[1];
206041016Sdfr        y = (c & MOUSE_PS2_YNEG) ?  sc->ipacket[2] - 256 : sc->ipacket[2];
206141016Sdfr	z = 0;
206241016Sdfr        ms.obutton = sc->button;		  /* previous button state */
206341016Sdfr        ms.button = butmap[c & MOUSE_PS2_BUTTONS];
206445789Speter	/* `tapping' action */
206545789Speter	if (sc->config & PSM_CONFIG_FORCETAP)
206645789Speter	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
206741016Sdfr
206841016Sdfr	switch (sc->hw.model) {
206941016Sdfr
207058230Syokota	case MOUSE_MODEL_EXPLORER:
207158230Syokota	    /*
207258230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
207358230Syokota	     * byte 1:  oy ox sy sx 1  M  R  L
207458230Syokota	     * byte 2:  x  x  x  x  x  x  x  x
207558230Syokota	     * byte 3:  y  y  y  y  y  y  y  y
207658230Syokota	     * byte 4:  *  *  S2 S1 s  d2 d1 d0
207758230Syokota	     *
207858230Syokota	     * L, M, R, S1, S2: left, middle, right and side buttons
207958230Syokota	     * s: wheel data sign bit
208058230Syokota	     * d2-d0: wheel data
208158230Syokota	     */
208258230Syokota	    z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
208358230Syokota		? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
208458230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
208558230Syokota		? MOUSE_BUTTON4DOWN : 0;
208658230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
208758230Syokota		? MOUSE_BUTTON5DOWN : 0;
208858230Syokota	    break;
208958230Syokota
209041016Sdfr	case MOUSE_MODEL_INTELLI:
209141016Sdfr	case MOUSE_MODEL_NET:
209241016Sdfr	    /* wheel data is in the fourth byte */
209341016Sdfr	    z = (char)sc->ipacket[3];
209458230Syokota	    /* some mice may send 7 when there is no Z movement?! XXX */
209558230Syokota	    if ((z >= 7) || (z <= -7))
209658230Syokota		z = 0;
209758230Syokota	    /* some compatible mice have additional buttons */
209858230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
209958230Syokota		? MOUSE_BUTTON4DOWN : 0;
210058230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
210158230Syokota		? MOUSE_BUTTON5DOWN : 0;
210241016Sdfr	    break;
210341016Sdfr
210441016Sdfr	case MOUSE_MODEL_MOUSEMANPLUS:
210548778Syokota	    /*
210648778Syokota	     * PS2++ protocl packet
210748778Syokota	     *
210848778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
210948778Syokota	     * byte 1:  *  1  p3 p2 1  *  *  *
211048778Syokota	     * byte 2:  c1 c2 p1 p0 d1 d0 1  0
211148778Syokota	     *
211248778Syokota	     * p3-p0: packet type
211348778Syokota	     * c1, c2: c1 & c2 == 1, if p2 == 0
211448778Syokota	     *         c1 & c2 == 0, if p2 == 1
211548778Syokota	     *
211648778Syokota	     * packet type: 0 (device type)
211748778Syokota	     * See comments in enable_mmanplus() below.
211848778Syokota	     *
211948778Syokota	     * packet type: 1 (wheel data)
212048778Syokota	     *
212148778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
212248778Syokota	     * byte 3:  h  *  B5 B4 s  d2 d1 d0
212348778Syokota	     *
212448778Syokota	     * h: 1, if horizontal roller data
212548778Syokota	     *    0, if vertical roller data
212648778Syokota	     * B4, B5: button 4 and 5
212748778Syokota	     * s: sign bit
212848778Syokota	     * d2-d0: roller data
212948778Syokota	     *
213048778Syokota	     * packet type: 2 (reserved)
213148778Syokota	     */
213248778Syokota	    if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
213348778Syokota		    && (abs(x) > 191)
213448778Syokota		    && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
213541016Sdfr		/* the extended data packet encodes button and wheel events */
213648778Syokota		switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
213748778Syokota		case 1:
213848778Syokota		    /* wheel data packet */
213948778Syokota		    x = y = 0;
214048778Syokota		    if (sc->ipacket[2] & 0x80) {
214148778Syokota			/* horizontal roller count - ignore it XXX*/
214248778Syokota		    } else {
214348778Syokota			/* vertical roller count */
214448778Syokota			z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
214548778Syokota			    ? (sc->ipacket[2] & 0x0f) - 16
214648778Syokota			    : (sc->ipacket[2] & 0x0f);
214748778Syokota		    }
214848778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
214948778Syokota			? MOUSE_BUTTON4DOWN : 0;
215048778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
215148778Syokota			? MOUSE_BUTTON5DOWN : 0;
215248778Syokota		    break;
215348778Syokota		case 2:
215458230Syokota		    /* this packet type is reserved by Logitech... */
215558230Syokota		    /*
215658230Syokota		     * IBM ScrollPoint Mouse uses this packet type to
215758230Syokota		     * encode both vertical and horizontal scroll movement.
215858230Syokota		     */
215958230Syokota		    x = y = 0;
216058230Syokota		    /* horizontal count */
216158230Syokota		    if (sc->ipacket[2] & 0x0f)
216258230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
216358230Syokota		    /* vertical count */
216458230Syokota		    if (sc->ipacket[2] & 0xf0)
216558230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
216658230Syokota#if 0
216758230Syokota		    /* vertical count */
216858230Syokota		    z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
216958230Syokota			? ((sc->ipacket[2] >> 4) & 0x0f) - 16
217058230Syokota			: ((sc->ipacket[2] >> 4) & 0x0f);
217158230Syokota		    /* horizontal count */
217258230Syokota		    w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
217358230Syokota			? (sc->ipacket[2] & 0x0f) - 16
217458230Syokota			: (sc->ipacket[2] & 0x0f);
217558230Syokota#endif
217658230Syokota		    break;
217748778Syokota		case 0:
217848778Syokota		    /* device type packet - shouldn't happen */
217948778Syokota		    /* FALL THROUGH */
218048778Syokota		default:
218148778Syokota		    x = y = 0;
218248778Syokota		    ms.button = ms.obutton;
218358230Syokota		    if (bootverbose)
218458230Syokota			log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
218558230Syokota				       "0x%02x 0x%02x 0x%02x\n",
218658230Syokota			    MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
218758230Syokota			    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
218848778Syokota		    break;
218948778Syokota		}
219041016Sdfr	    } else {
219141016Sdfr		/* preserve button states */
219241016Sdfr		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
219341016Sdfr	    }
219441016Sdfr	    break;
219541016Sdfr
219641016Sdfr	case MOUSE_MODEL_GLIDEPOINT:
219741016Sdfr	    /* `tapping' action */
219841016Sdfr	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
219941016Sdfr	    break;
220041016Sdfr
220141016Sdfr	case MOUSE_MODEL_NETSCROLL:
220258230Syokota	    /* three addtional bytes encode buttons and wheel events */
220358230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
220441016Sdfr		? MOUSE_BUTTON4DOWN : 0;
220558230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
220658230Syokota		? MOUSE_BUTTON5DOWN : 0;
220741016Sdfr	    z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
220841016Sdfr		? sc->ipacket[4] - 256 : sc->ipacket[4];
220941016Sdfr	    break;
221041016Sdfr
221141016Sdfr	case MOUSE_MODEL_THINK:
221241016Sdfr	    /* the fourth button state in the first byte */
221341016Sdfr	    ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
221441016Sdfr	    break;
221541016Sdfr
221649965Syokota	case MOUSE_MODEL_VERSAPAD:
221749965Syokota	    /* VersaPad PS/2 absolute mode message format
221849965Syokota	     *
221949965Syokota	     * [packet1]     7   6   5   4   3   2   1   0(LSB)
222049965Syokota	     *  ipacket[0]:  1   1   0   A   1   L   T   R
222149965Syokota	     *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
222249965Syokota	     *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
222349965Syokota	     *  ipacket[3]:  1   1   1   A   1   L   T   R
222449965Syokota	     *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
222549965Syokota	     *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
222649965Syokota	     *
222749965Syokota	     * [note]
222849965Syokota	     *  R: right physical mouse button (1=on)
222949965Syokota	     *  T: touch pad virtual button (1=tapping)
223049965Syokota	     *  L: left physical mouse button (1=on)
223149965Syokota	     *  A: position data is valid (1=valid)
223249965Syokota	     *  H: horizontal data (12bit signed integer. H11 is sign bit.)
223349965Syokota	     *  V: vertical data (12bit signed integer. V11 is sign bit.)
223449965Syokota	     *  P: pressure data
223549965Syokota	     *
223649965Syokota	     * Tapping is mapped to MOUSE_BUTTON4.
223749965Syokota	     */
223849965Syokota	    ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
223949965Syokota	    ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
224049965Syokota	    x = y = 0;
224149965Syokota	    if (c & MOUSE_PS2VERSA_IN_USE) {
224249965Syokota		x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
224349965Syokota		y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
224449965Syokota		if (x0 & 0x800)
224549965Syokota		    x0 -= 0x1000;
224649965Syokota		if (y0 & 0x800)
224749965Syokota		    y0 -= 0x1000;
224849965Syokota		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
224949965Syokota		    x = sc->xold - x0;
225049965Syokota		    y = y0 - sc->yold;
225149965Syokota		    if (x < 0)	/* XXX */
225249965Syokota			x++;
225349965Syokota		    else if (x)
225449965Syokota			x--;
225549965Syokota		    if (y < 0)
225649965Syokota			y++;
225749965Syokota		    else if (y)
225849965Syokota			y--;
225949965Syokota		} else {
226049965Syokota		    sc->flags |= PSM_FLAGS_FINGERDOWN;
226149965Syokota		}
226249965Syokota		sc->xold = x0;
226349965Syokota		sc->yold = y0;
226449965Syokota	    } else {
226549965Syokota		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
226649965Syokota	    }
226749965Syokota	    c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
226849965Syokota		| ((y < 0) ? MOUSE_PS2_YNEG : 0);
226949965Syokota	    break;
227049965Syokota
227158230Syokota	case MOUSE_MODEL_4D:
227258230Syokota	    /*
227358230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
227458230Syokota	     * byte 1:  s2 d2 s1 d1 1  M  R  L
227558230Syokota	     * byte 2:  sx x  x  x  x  x  x  x
227658230Syokota	     * byte 3:  sy y  y  y  y  y  y  y
227758230Syokota	     *
227858230Syokota	     * s1: wheel 1 direction
227958230Syokota	     * d1: wheel 1 data
228058230Syokota	     * s2: wheel 2 direction
228158230Syokota	     * d2: wheel 2 data
228258230Syokota	     */
228358230Syokota	    x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
228458230Syokota	    y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
228558230Syokota	    switch (c & MOUSE_4D_WHEELBITS) {
228658230Syokota	    case 0x10:
228758230Syokota		z = 1;
228858230Syokota		break;
228958230Syokota	    case 0x30:
229058230Syokota		z = -1;
229158230Syokota		break;
229258230Syokota	    case 0x40:	/* 2nd wheel turning right XXX */
229358230Syokota		z = 2;
229458230Syokota		break;
229558230Syokota	    case 0xc0:	/* 2nd wheel turning left XXX */
229658230Syokota		z = -2;
229758230Syokota		break;
229858230Syokota	    }
229958230Syokota	    break;
230058230Syokota
230158230Syokota	case MOUSE_MODEL_4DPLUS:
230258230Syokota	    if ((x < 16 - 256) && (y < 16 - 256)) {
230358230Syokota		/*
230458230Syokota		 *          b7 b6 b5 b4 b3 b2 b1 b0
230558230Syokota		 * byte 1:  0  0  1  1  1  M  R  L
230658230Syokota		 * byte 2:  0  0  0  0  1  0  0  0
230758230Syokota		 * byte 3:  0  0  0  0  S  s  d1 d0
230858230Syokota		 *
230958230Syokota		 * L, M, R, S: left, middle, right and side buttons
231058230Syokota		 * s: wheel data sign bit
231158230Syokota		 * d1-d0: wheel data
231258230Syokota		 */
231358230Syokota		x = y = 0;
231458230Syokota		if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
231558230Syokota		    ms.button |= MOUSE_BUTTON4DOWN;
231658230Syokota		z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
231758230Syokota			? ((sc->ipacket[2] & 0x07) - 8)
231858230Syokota			: (sc->ipacket[2] & 0x07) ;
231958230Syokota	    } else {
232058230Syokota		/* preserve previous button states */
232158230Syokota		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
232258230Syokota	    }
232358230Syokota	    break;
232458230Syokota
232541016Sdfr	case MOUSE_MODEL_GENERIC:
232641016Sdfr	default:
232741016Sdfr	    break;
232841016Sdfr	}
232941016Sdfr
233041016Sdfr        /* scale values */
233141016Sdfr        if (sc->mode.accelfactor >= 1) {
233241016Sdfr            if (x != 0) {
233341016Sdfr                x = x * x / sc->mode.accelfactor;
233441016Sdfr                if (x == 0)
233541016Sdfr                    x = 1;
233641016Sdfr                if (c & MOUSE_PS2_XNEG)
233741016Sdfr                    x = -x;
233841016Sdfr            }
233941016Sdfr            if (y != 0) {
234041016Sdfr                y = y * y / sc->mode.accelfactor;
234141016Sdfr                if (y == 0)
234241016Sdfr                    y = 1;
234341016Sdfr                if (c & MOUSE_PS2_YNEG)
234441016Sdfr                    y = -y;
234541016Sdfr            }
234641016Sdfr        }
234741016Sdfr
234841016Sdfr        ms.dx = x;
234941016Sdfr        ms.dy = y;
235041016Sdfr        ms.dz = z;
235141016Sdfr        ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
235241016Sdfr	    | (ms.obutton ^ ms.button);
235341016Sdfr
235441016Sdfr	if (sc->mode.level < PSM_LEVEL_NATIVE)
235541016Sdfr	    sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
235641016Sdfr
235741016Sdfr        sc->status.flags |= ms.flags;
235841016Sdfr        sc->status.dx += ms.dx;
235941016Sdfr        sc->status.dy += ms.dy;
236041016Sdfr        sc->status.dz += ms.dz;
236141016Sdfr        sc->status.button = ms.button;
236241016Sdfr        sc->button = ms.button;
236341016Sdfr
236458230Syokota	sc->watchdog = FALSE;
236558230Syokota
236641016Sdfr        /* queue data */
236741016Sdfr        if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
236841016Sdfr	    l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
236941016Sdfr	    bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
237041016Sdfr	    if (sc->inputbytes > l)
237141016Sdfr	        bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
237241016Sdfr            sc->queue.tail =
237341016Sdfr		(sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
237441016Sdfr            sc->queue.count += sc->inputbytes;
237541016Sdfr	}
237641016Sdfr        sc->inputbytes = 0;
237741016Sdfr
237841016Sdfr        if (sc->state & PSM_ASLP) {
237941016Sdfr            sc->state &= ~PSM_ASLP;
238041016Sdfr            wakeup((caddr_t) sc);
238141016Sdfr    	}
238241016Sdfr        selwakeup(&sc->rsel);
238341016Sdfr    }
238441016Sdfr}
238541016Sdfr
238641016Sdfrstatic int
238783366Sjulianpsmpoll(dev_t dev, int events, struct thread *td)
238841016Sdfr{
238941016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
239041016Sdfr    int s;
239141016Sdfr    int revents = 0;
239241016Sdfr
239341016Sdfr    /* Return true if a mouse event available */
239441016Sdfr    s = spltty();
239545789Speter    if (events & (POLLIN | POLLRDNORM)) {
239641016Sdfr	if (sc->queue.count > 0)
239741016Sdfr	    revents |= events & (POLLIN | POLLRDNORM);
239841016Sdfr	else
239983366Sjulian	    selrecord(td, &sc->rsel);
240045789Speter    }
240141016Sdfr    splx(s);
240241016Sdfr
240341016Sdfr    return (revents);
240441016Sdfr}
240541016Sdfr
240641016Sdfr/* vendor/model specific routines */
240741016Sdfr
240841016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
240941016Sdfr{
241041016Sdfr    if (set_mouse_resolution(kbdc, res) != res)
241141016Sdfr        return FALSE;
241241016Sdfr    if (set_mouse_scaling(kbdc, scale)
241341016Sdfr	&& set_mouse_scaling(kbdc, scale)
241441016Sdfr	&& set_mouse_scaling(kbdc, scale)
241541016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3))
241641016Sdfr	return TRUE;
241741016Sdfr    return FALSE;
241841016Sdfr}
241941016Sdfr
242069438Syokotastatic int
242169438Syokotamouse_ext_command(KBDC kbdc, int command)
242269438Syokota{
242369438Syokota    int c;
242469438Syokota
242569438Syokota    c = (command >> 6) & 0x03;
242669438Syokota    if (set_mouse_resolution(kbdc, c) != c)
242769438Syokota	return FALSE;
242869438Syokota    c = (command >> 4) & 0x03;
242969438Syokota    if (set_mouse_resolution(kbdc, c) != c)
243069438Syokota	return FALSE;
243169438Syokota    c = (command >> 2) & 0x03;
243269438Syokota    if (set_mouse_resolution(kbdc, c) != c)
243369438Syokota	return FALSE;
243469438Syokota    c = (command >> 0) & 0x03;
243569438Syokota    if (set_mouse_resolution(kbdc, c) != c)
243669438Syokota	return FALSE;
243769438Syokota    return TRUE;
243869438Syokota}
243969438Syokota
244041016Sdfr#if notyet
244141016Sdfr/* Logitech MouseMan Cordless II */
244241016Sdfrstatic int
244341016Sdfrenable_lcordless(struct psm_softc *sc)
244441016Sdfr{
244541016Sdfr    int status[3];
244641016Sdfr    int ch;
244741016Sdfr
244841016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
244941016Sdfr        return FALSE;
245041016Sdfr    if (status[1] == PSMD_RES_HIGH)
245141016Sdfr	return FALSE;
245241016Sdfr    ch = (status[0] & 0x07) - 1;	/* channel # */
245341016Sdfr    if ((ch <= 0) || (ch > 4))
245441016Sdfr	return FALSE;
245541016Sdfr    /*
245641016Sdfr     * status[1]: always one?
245741016Sdfr     * status[2]: battery status? (0-100)
245841016Sdfr     */
245941016Sdfr    return TRUE;
246041016Sdfr}
246141016Sdfr#endif /* notyet */
246241016Sdfr
246358230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
246441016Sdfrstatic int
246541016Sdfrenable_groller(struct psm_softc *sc)
246641016Sdfr{
246741016Sdfr    int status[3];
246841016Sdfr
246941016Sdfr    /*
247041016Sdfr     * The special sequence to enable the fourth button and the
247141016Sdfr     * roller. Immediately after this sequence check status bytes.
247241016Sdfr     * if the mouse is NetScroll, the second and the third bytes are
247341016Sdfr     * '3' and 'D'.
247441016Sdfr     */
247541016Sdfr
247641016Sdfr    /*
247741016Sdfr     * If the mouse is an ordinary PS/2 mouse, the status bytes should
247841016Sdfr     * look like the following.
247941016Sdfr     *
248041016Sdfr     * byte 1 bit 7 always 0
248141016Sdfr     *        bit 6 stream mode (0)
248241016Sdfr     *        bit 5 disabled (0)
248341016Sdfr     *        bit 4 1:1 scaling (0)
248441016Sdfr     *        bit 3 always 0
248541016Sdfr     *        bit 0-2 button status
248641016Sdfr     * byte 2 resolution (PSMD_RES_HIGH)
248741016Sdfr     * byte 3 report rate (?)
248841016Sdfr     */
248941016Sdfr
249041016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
249141016Sdfr        return FALSE;
249241016Sdfr    if ((status[1] != '3') || (status[2] != 'D'))
249341016Sdfr        return FALSE;
249458230Syokota    /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
249541016Sdfr    sc->hw.buttons = 4;
249641016Sdfr    return TRUE;
249741016Sdfr}
249841016Sdfr
249958230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
250041016Sdfrstatic int
250141016Sdfrenable_gmouse(struct psm_softc *sc)
250241016Sdfr{
250341016Sdfr    int status[3];
250441016Sdfr
250541016Sdfr    /*
250641016Sdfr     * The special sequence to enable the middle, "rubber" button.
250741016Sdfr     * Immediately after this sequence check status bytes.
250841016Sdfr     * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
250941016Sdfr     * the second and the third bytes are '3' and 'U'.
251041016Sdfr     * NOTE: NetMouse reports that it has three buttons although it has
251141016Sdfr     * two buttons and a rubber button. NetMouse Pro and MIE Mouse
251241016Sdfr     * say they have three buttons too and they do have a button on the
251341016Sdfr     * side...
251441016Sdfr     */
251541016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
251641016Sdfr        return FALSE;
251741016Sdfr    if ((status[1] != '3') || (status[2] != 'U'))
251841016Sdfr        return FALSE;
251941016Sdfr    return TRUE;
252041016Sdfr}
252141016Sdfr
252241016Sdfr/* ALPS GlidePoint */
252341016Sdfrstatic int
252441016Sdfrenable_aglide(struct psm_softc *sc)
252541016Sdfr{
252641016Sdfr    int status[3];
252741016Sdfr
252841016Sdfr    /*
252941016Sdfr     * The special sequence to obtain ALPS GlidePoint specific
253041016Sdfr     * information. Immediately after this sequence, status bytes will
253141016Sdfr     * contain something interesting.
253241016Sdfr     * NOTE: ALPS produces several models of GlidePoint. Some of those
253341016Sdfr     * do not respond to this sequence, thus, cannot be detected this way.
253441016Sdfr     */
253550149Syokota    if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
253650149Syokota	return FALSE;
253741016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
253841016Sdfr        return FALSE;
253950149Syokota    if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
254041016Sdfr        return FALSE;
254141016Sdfr    return TRUE;
254241016Sdfr}
254341016Sdfr
254441016Sdfr/* Kensington ThinkingMouse/Trackball */
254541016Sdfrstatic int
254641016Sdfrenable_kmouse(struct psm_softc *sc)
254741016Sdfr{
254841016Sdfr    static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
254941016Sdfr    KBDC kbdc = sc->kbdc;
255041016Sdfr    int status[3];
255141016Sdfr    int id1;
255241016Sdfr    int id2;
255341016Sdfr    int i;
255441016Sdfr
255541016Sdfr    id1 = get_aux_id(kbdc);
255641016Sdfr    if (set_mouse_sampling_rate(kbdc, 10) != 10)
255741016Sdfr	return FALSE;
255841016Sdfr    /*
255941016Sdfr     * The device is now in the native mode? It returns a different
256041016Sdfr     * ID value...
256141016Sdfr     */
256241016Sdfr    id2 = get_aux_id(kbdc);
256341016Sdfr    if ((id1 == id2) || (id2 != 2))
256441016Sdfr	return FALSE;
256541016Sdfr
256641016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
256741016Sdfr        return FALSE;
256841016Sdfr#if PSM_DEBUG >= 2
256941016Sdfr    /* at this point, resolution is LOW, sampling rate is 10/sec */
257041016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
257141016Sdfr        return FALSE;
257241016Sdfr#endif
257341016Sdfr
257441016Sdfr    /*
257541016Sdfr     * The special sequence to enable the third and fourth buttons.
257641016Sdfr     * Otherwise they behave like the first and second buttons.
257741016Sdfr     */
257841016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
257941016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
258041016Sdfr	    return FALSE;
258141016Sdfr    }
258241016Sdfr
258341016Sdfr    /*
258441016Sdfr     * At this point, the device is using default resolution and
258541016Sdfr     * sampling rate for the native mode.
258641016Sdfr     */
258741016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
258841016Sdfr        return FALSE;
258941016Sdfr    if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
259041016Sdfr        return FALSE;
259141016Sdfr
259241016Sdfr    /* the device appears be enabled by this sequence, diable it for now */
259341016Sdfr    disable_aux_dev(kbdc);
259441016Sdfr    empty_aux_buffer(kbdc, 5);
259541016Sdfr
259641016Sdfr    return TRUE;
259741016Sdfr}
259841016Sdfr
259958230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
260041016Sdfrstatic int
260141016Sdfrenable_mmanplus(struct psm_softc *sc)
260241016Sdfr{
260341016Sdfr    KBDC kbdc = sc->kbdc;
260441016Sdfr    int data[3];
260541016Sdfr
260641016Sdfr    /* the special sequence to enable the fourth button and the roller. */
260758230Syokota    /*
260858230Syokota     * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
260958230Syokota     * must be called exactly three times since the last RESET command
261058230Syokota     * before this sequence. XXX
261158230Syokota     */
261269438Syokota    if (!set_mouse_scaling(kbdc, 1))
261369438Syokota	return FALSE;
261469438Syokota    if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
261569438Syokota	return FALSE;
261641016Sdfr    if (get_mouse_status(kbdc, data, 1, 3) < 3)
261741016Sdfr        return FALSE;
261841016Sdfr
261948778Syokota    /*
262048778Syokota     * PS2++ protocl, packet type 0
262141016Sdfr     *
262248778Syokota     *          b7 b6 b5 b4 b3 b2 b1 b0
262348778Syokota     * byte 1:  *  1  p3 p2 1  *  *  *
262448778Syokota     * byte 2:  1  1  p1 p0 m1 m0 1  0
262548778Syokota     * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
262648778Syokota     *
262748778Syokota     * p3-p0: packet type: 0
262858230Syokota     * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
262941016Sdfr     */
263048778Syokota    /* check constant bits */
263148778Syokota    if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
263241016Sdfr        return FALSE;
263348778Syokota    if ((data[1] & 0xc3) != 0xc2)
263448778Syokota        return FALSE;
263548778Syokota    /* check d3-d0 in byte 2 */
263648778Syokota    if (!MOUSE_PS2PLUS_CHECKBITS(data))
263748778Syokota        return FALSE;
263848778Syokota    /* check p3-p0 */
263948778Syokota    if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
264048778Syokota        return FALSE;
264141016Sdfr
264248778Syokota    sc->hw.hwid &= 0x00ff;
264348778Syokota    sc->hw.hwid |= data[2] << 8;	/* save model ID */
264448778Syokota
264541016Sdfr    /*
264641016Sdfr     * MouseMan+ (or FirstMouse+) is now in its native mode, in which
264741016Sdfr     * the wheel and the fourth button events are encoded in the
264841016Sdfr     * special data packet. The mouse may be put in the IntelliMouse mode
264941016Sdfr     * if it is initialized by the IntelliMouse's method.
265041016Sdfr     */
265141016Sdfr    return TRUE;
265241016Sdfr}
265341016Sdfr
265458230Syokota/* MS IntelliMouse Explorer */
265558230Syokotastatic int
265658230Syokotaenable_msexplorer(struct psm_softc *sc)
265758230Syokota{
265858923Syokota    static unsigned char rate0[] = { 200, 100, 80, };
265958923Syokota    static unsigned char rate1[] = { 200, 200, 80, };
266058230Syokota    KBDC kbdc = sc->kbdc;
266158230Syokota    int id;
266258230Syokota    int i;
266358230Syokota
266469438Syokota    /* the special sequence to enable the extra buttons and the roller. */
266569438Syokota    for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
266669438Syokota        if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
266769438Syokota	    return FALSE;
266869438Syokota    }
266969438Syokota    /* the device will give the genuine ID only after the above sequence */
267069438Syokota    id = get_aux_id(kbdc);
267169438Syokota    if (id != PSM_EXPLORER_ID)
267269438Syokota	return FALSE;
267369438Syokota
267469438Syokota    sc->hw.hwid = id;
267569438Syokota    sc->hw.buttons = 5;		/* IntelliMouse Explorer XXX */
267669438Syokota
267758923Syokota    /*
267858923Syokota     * XXX: this is a kludge to fool some KVM switch products
267958923Syokota     * which think they are clever enough to know the 4-byte IntelliMouse
268058923Syokota     * protocol, and assume any other protocols use 3-byte packets.
268158923Syokota     * They don't convey 4-byte data packets from the IntelliMouse Explorer
268258923Syokota     * correctly to the host computer because of this!
268358923Syokota     * The following sequence is actually IntelliMouse's "wake up"
268458923Syokota     * sequence; it will make the KVM think the mouse is IntelliMouse
268558923Syokota     * when it is in fact IntelliMouse Explorer.
268658923Syokota     */
268758923Syokota    for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
268858923Syokota        if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
268969438Syokota	    break;
269058923Syokota    }
269158923Syokota    id = get_aux_id(kbdc);
269258923Syokota
269358230Syokota    return TRUE;
269458230Syokota}
269558230Syokota
269641016Sdfr/* MS IntelliMouse */
269741016Sdfrstatic int
269841016Sdfrenable_msintelli(struct psm_softc *sc)
269941016Sdfr{
270041016Sdfr    /*
270141016Sdfr     * Logitech MouseMan+ and FirstMouse+ will also respond to this
270241016Sdfr     * probe routine and act like IntelliMouse.
270341016Sdfr     */
270441016Sdfr
270541016Sdfr    static unsigned char rate[] = { 200, 100, 80, };
270641016Sdfr    KBDC kbdc = sc->kbdc;
270741016Sdfr    int id;
270841016Sdfr    int i;
270941016Sdfr
271041016Sdfr    /* the special sequence to enable the third button and the roller. */
271141016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
271241016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
271341016Sdfr	    return FALSE;
271441016Sdfr    }
271541016Sdfr    /* the device will give the genuine ID only after the above sequence */
271641016Sdfr    id = get_aux_id(kbdc);
271741016Sdfr    if (id != PSM_INTELLI_ID)
271841016Sdfr	return FALSE;
271941016Sdfr
272041016Sdfr    sc->hw.hwid = id;
272141016Sdfr    sc->hw.buttons = 3;
272241016Sdfr
272341016Sdfr    return TRUE;
272441016Sdfr}
272541016Sdfr
272658230Syokota/* A4 Tech 4D Mouse */
272758230Syokotastatic int
272858230Syokotaenable_4dmouse(struct psm_softc *sc)
272958230Syokota{
273058230Syokota    /*
273158230Syokota     * Newer wheel mice from A4 Tech may use the 4D+ protocol.
273258230Syokota     */
273358230Syokota
273458230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
273558230Syokota    KBDC kbdc = sc->kbdc;
273658230Syokota    int id;
273758230Syokota    int i;
273858230Syokota
273958230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
274058230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
274158230Syokota	    return FALSE;
274258230Syokota    }
274358230Syokota    id = get_aux_id(kbdc);
274458230Syokota    /*
274558923Syokota     * WinEasy 4D, 4 Way Scroll 4D: 6
274658230Syokota     * Cable-Free 4D: 8 (4DPLUS)
274758923Syokota     * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
274858230Syokota     */
274958230Syokota    if (id != PSM_4DMOUSE_ID)
275058230Syokota	return FALSE;
275158230Syokota
275258230Syokota    sc->hw.hwid = id;
275358230Syokota    sc->hw.buttons = 3;		/* XXX some 4D mice have 4? */
275458230Syokota
275558230Syokota    return TRUE;
275658230Syokota}
275758230Syokota
275858230Syokota/* A4 Tech 4D+ Mouse */
275958230Syokotastatic int
276058230Syokotaenable_4dplus(struct psm_softc *sc)
276158230Syokota{
276258230Syokota    /*
276358230Syokota     * Newer wheel mice from A4 Tech seem to use this protocol.
276458230Syokota     * Older models are recognized as either 4D Mouse or IntelliMouse.
276558230Syokota     */
276658230Syokota    KBDC kbdc = sc->kbdc;
276758230Syokota    int id;
276858230Syokota
276958230Syokota    /*
277058230Syokota     * enable_4dmouse() already issued the following ID sequence...
277158230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
277258230Syokota    int i;
277358230Syokota
277458230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
277558230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
277658230Syokota	    return FALSE;
277758230Syokota    }
277858230Syokota    */
277958230Syokota
278058230Syokota    id = get_aux_id(kbdc);
278158230Syokota    if (id != PSM_4DPLUS_ID)
278258230Syokota	return FALSE;
278358230Syokota
278458230Syokota    sc->hw.hwid = id;
278558230Syokota    sc->hw.buttons = 4;		/* XXX */
278658230Syokota
278758230Syokota    return TRUE;
278858230Syokota}
278958230Syokota
279049965Syokota/* Interlink electronics VersaPad */
279149965Syokotastatic int
279249965Syokotaenable_versapad(struct psm_softc *sc)
279349965Syokota{
279449965Syokota    KBDC kbdc = sc->kbdc;
279549965Syokota    int data[3];
279649965Syokota
279749965Syokota    set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
279849965Syokota    set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
279949965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
280049965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
280149965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
280249965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
280349965Syokota    if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
280449965Syokota	return FALSE;
280549965Syokota    if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
280649965Syokota	return FALSE;
280749965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
280849965Syokota
280958230Syokota    sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
281058230Syokota
281149965Syokota    return TRUE;				/* PS/2 absolute mode */
281249965Syokota}
281349965Syokota
281441016Sdfrstatic int
281554629Syokotapsmresume(device_t dev)
281641016Sdfr{
281754629Syokota    struct psm_softc *sc = device_get_softc(dev);
281854629Syokota    int unit = device_get_unit(dev);
281984880Syokota    int err;
282041016Sdfr
282141016Sdfr    if (verbose >= 2)
282254629Syokota        log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
282341016Sdfr
282458230Syokota    if (!(sc->config & PSM_CONFIG_HOOKRESUME))
282558230Syokota	return (0);
282658230Syokota
282784880Syokota    err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
282841016Sdfr
282941016Sdfr    if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
283041016Sdfr	/*
283141016Sdfr	 * Release the blocked process; it must be notified that the device
283241016Sdfr	 * cannot be accessed anymore.
283341016Sdfr	 */
283441016Sdfr        sc->state &= ~PSM_ASLP;
283541016Sdfr        wakeup((caddr_t)sc);
283641016Sdfr    }
283741016Sdfr
283841016Sdfr    if (verbose >= 2)
283954629Syokota        log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
284041016Sdfr
284141016Sdfr    return (err);
284241016Sdfr}
284341016Sdfr
284452997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
284583147Syokota
284683147Syokota/*
284783147Syokota * This sucks up assignments from PNPBIOS and ACPI.
284883147Syokota */
284983147Syokota
285083931Syokota/*
285183931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
285283931Syokota * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
285383931Syokota * can be probed and attached only after the AT keyboard controller is
285483931Syokota * attached, we shall quietly reserve the IRQ resource for later use.
285583931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
285683931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging
285783931Syokota * under the keyboard controller, then probe and attach it.
285883931Syokota */
285983147Syokota
286083147Syokotastatic	devclass_t			psmcpnp_devclass;
286183147Syokota
286283147Syokotastatic	device_probe_t			psmcpnp_probe;
286383147Syokotastatic	device_attach_t			psmcpnp_attach;
286483147Syokota
286583147Syokotastatic device_method_t psmcpnp_methods[] = {
286683147Syokota	DEVMETHOD(device_probe,		psmcpnp_probe),
286783147Syokota	DEVMETHOD(device_attach,	psmcpnp_attach),
286883147Syokota
286983147Syokota	{ 0, 0 }
287083147Syokota};
287183147Syokota
287283147Syokotastatic driver_t psmcpnp_driver = {
287383147Syokota	PSMCPNP_DRIVER_NAME,
287483147Syokota	psmcpnp_methods,
287583147Syokota	1,			/* no softc */
287683147Syokota};
287783147Syokota
287883147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = {
287983147Syokota	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
288083147Syokota	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
288183492Syokota	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
288284407Stakawata	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
288383147Syokota	{ 0 }
288483147Syokota};
288583147Syokota
288683147Syokotastatic int
288783147Syokotacreate_a_copy(device_t atkbdc, device_t me)
288883147Syokota{
288983147Syokota	device_t psm;
289083147Syokota	u_long irq;
289183147Syokota
289283931Syokota	/* find the PS/2 mouse device instance under the keyboard controller */
289383931Syokota	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
289483931Syokota				device_get_unit(atkbdc));
289583147Syokota	if (psm == NULL)
289683147Syokota		return ENXIO;
289783931Syokota	if (device_get_state(psm) != DS_NOTPRESENT)
289883931Syokota		return 0;
289983147Syokota
290083931Syokota	/* move our resource to the found device */
290183931Syokota	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
290283147Syokota	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
290383147Syokota
290483147Syokota	/* ...then probe and attach it */
290583147Syokota	return device_probe_and_attach(psm);
290683147Syokota}
290783147Syokota
290883147Syokotastatic int
290983147Syokotapsmcpnp_probe(device_t dev)
291083147Syokota{
291183931Syokota	struct resource *res;
291283931Syokota	u_long irq;
291383931Syokota	int rid;
291483147Syokota
291583147Syokota	if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
291683147Syokota		return ENXIO;
291783147Syokota
291883931Syokota	/*
291983931Syokota	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
292083931Syokota	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
292183931Syokota	 * declares the PS/2 mouse device node without an IRQ resource!
292283931Syokota	 * If this happens, we shall refer to device hints.
292383931Syokota	 * If we still don't find it there, use a hardcoded value... XXX
292483931Syokota	 */
292583931Syokota	rid = 0;
292683931Syokota	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
292783931Syokota	if (irq <= 0) {
292883931Syokota		if (resource_long_value(PSM_DRIVER_NAME,
292983931Syokota					device_get_unit(dev), "irq", &irq) != 0)
293083931Syokota			irq = 12;	/* XXX */
293183931Syokota		device_printf(dev, "irq resource info is missing; "
293283931Syokota			      "assuming irq %ld\n", irq);
293383931Syokota		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
293483931Syokota	}
293583931Syokota	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
293683931Syokota				 RF_SHAREABLE);
293783931Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
293883147Syokota
293983147Syokota	/* keep quiet */
294083147Syokota	if (!bootverbose)
294183147Syokota		device_quiet(dev);
294283492Syokota
294383931Syokota	return ((res == NULL) ? ENXIO : 0);
294483147Syokota}
294583147Syokota
294683147Syokotastatic int
294783147Syokotapsmcpnp_attach(device_t dev)
294883147Syokota{
294983492Syokota	device_t atkbdc;
295083931Syokota	int rid;
295183147Syokota
295283931Syokota	/* find the keyboard controller, which may be on acpi* or isa* bus */
295383931Syokota	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
295483931Syokota				     device_get_unit(dev));
295583931Syokota	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
295683492Syokota		create_a_copy(atkbdc, dev);
295783931Syokota	} else {
295883931Syokota		/*
295983931Syokota		 * If we don't have the AT keyboard controller yet,
296083931Syokota		 * just reserve the IRQ for later use...
296183931Syokota		 * (See psmidentify() above.)
296283931Syokota		 */
296383931Syokota		rid = 0;
296483931Syokota		bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
296583931Syokota				   RF_SHAREABLE);
296683931Syokota	}
296783492Syokota
296883147Syokota	return 0;
296983147Syokota}
297083147Syokota
297183147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
297283147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
2973