psm.c revision 111815
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 111815 2003-03-03 12:15:54Z phk $
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/* ring buffer */
13341016Sdfrtypedef struct ringbuf {
13441016Sdfr    int           count;	/* # of valid elements in the buffer */
13541016Sdfr    int           head;		/* head pointer */
13641016Sdfr    int           tail;		/* tail poiner */
13741016Sdfr    unsigned char buf[PSM_BUFSIZE];
13841016Sdfr} ringbuf_t;
13941016Sdfr
14041016Sdfr/* driver control block */
14141016Sdfrstruct psm_softc {		/* Driver status information */
14284880Syokota    int		  unit;
14341016Sdfr    struct selinfo rsel;	/* Process selecting for Input */
14441016Sdfr    unsigned char state;	/* Mouse driver state */
14541016Sdfr    int           config;	/* driver configuration flags */
14641016Sdfr    int           flags;	/* other flags */
14741016Sdfr    KBDC          kbdc;		/* handle to access the keyboard controller */
14858230Syokota    struct resource *intr;	/* IRQ resource */
14958230Syokota    void	  *ih;		/* interrupt handle */
15041016Sdfr    mousehw_t     hw;		/* hardware information */
15141016Sdfr    mousemode_t   mode;		/* operation mode */
15241016Sdfr    mousemode_t   dflt_mode;	/* default operation mode */
15341016Sdfr    mousestatus_t status;	/* accumulated mouse movement */
15441016Sdfr    ringbuf_t     queue;	/* mouse status queue */
15541016Sdfr    unsigned char ipacket[16];	/* interim input buffer */
15641016Sdfr    int           inputbytes;	/* # of bytes in the input buffer */
15741016Sdfr    int           button;	/* the latest button state */
15849965Syokota    int		  xold;	/* previous absolute X position */
15949965Syokota    int		  yold;	/* previous absolute Y position */
16084880Syokota    int		  syncerrors;
16184880Syokota    struct timeval inputtimeout;
16258230Syokota    int		  watchdog;	/* watchdog timer flag */
16358230Syokota    struct callout_handle callout;	/* watchdog timer call out */
16458230Syokota    dev_t	  dev;
16558230Syokota    dev_t	  bdev;
16641016Sdfr};
16741016Sdfrdevclass_t psm_devclass;
16841016Sdfr#define PSM_SOFTC(unit)	((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
16941016Sdfr
17041016Sdfr/* driver state flags (state) */
17141016Sdfr#define PSM_VALID		0x80
17241016Sdfr#define PSM_OPEN		1	/* Device is open */
17341016Sdfr#define PSM_ASLP		2	/* Waiting for mouse data */
17441016Sdfr
17541016Sdfr/* driver configuration flags (config) */
17641016Sdfr#define PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
17741016Sdfr#define PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
17841016Sdfr#define PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
17945789Speter#define PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
18045789Speter#define PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
18145789Speter#define PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
18245789Speter#define PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
18358230Syokota#define PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
18458230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
18569439Syokota#define PSM_CONFIG_SYNCHACK	0x8000 /* enable `out-of-sync' hack */
18641016Sdfr
18741016Sdfr#define PSM_CONFIG_FLAGS	(PSM_CONFIG_RESOLUTION 		\
18841016Sdfr				    | PSM_CONFIG_ACCEL		\
18945789Speter				    | PSM_CONFIG_NOCHECKSYNC	\
19069439Syokota				    | PSM_CONFIG_SYNCHACK	\
19145789Speter				    | PSM_CONFIG_NOIDPROBE	\
19245789Speter				    | PSM_CONFIG_NORESET	\
19345789Speter				    | PSM_CONFIG_FORCETAP	\
19458230Syokota				    | PSM_CONFIG_IGNPORTERROR	\
19558230Syokota				    | PSM_CONFIG_HOOKRESUME	\
19658230Syokota				    | PSM_CONFIG_INITAFTERSUSPEND)
19741016Sdfr
19841016Sdfr/* other flags (flags) */
19949965Syokota#define PSM_FLAGS_FINGERDOWN	0x0001 /* VersaPad finger down */
20041016Sdfr
20141016Sdfr/* for backward compatibility */
20241016Sdfr#define OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
20341016Sdfr#define OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
20441016Sdfr#define OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
20541016Sdfr
20641016Sdfrtypedef struct old_mousehw {
20741016Sdfr    int buttons;
20841016Sdfr    int iftype;
20941016Sdfr    int type;
21041016Sdfr    int hwid;
21141016Sdfr} old_mousehw_t;
21241016Sdfr
21341016Sdfrtypedef struct old_mousemode {
21441016Sdfr    int protocol;
21541016Sdfr    int rate;
21641016Sdfr    int resolution;
21741016Sdfr    int accelfactor;
21841016Sdfr} old_mousemode_t;
21941016Sdfr
22041016Sdfr/* packet formatting function */
22192756Salfredtypedef int packetfunc_t(struct psm_softc *, unsigned char *,
22292756Salfred			      int *, int, mousestatus_t *);
22341016Sdfr
22441016Sdfr/* function prototypes */
22592756Salfredstatic void psmidentify(driver_t *, device_t);
22692756Salfredstatic int psmprobe(device_t);
22792756Salfredstatic int psmattach(device_t);
22892756Salfredstatic int psmdetach(device_t);
22992756Salfredstatic int psmresume(device_t);
23041016Sdfr
23141016Sdfrstatic d_open_t psmopen;
23241016Sdfrstatic d_close_t psmclose;
23341016Sdfrstatic d_read_t psmread;
23441016Sdfrstatic d_ioctl_t psmioctl;
23541016Sdfrstatic d_poll_t psmpoll;
23641016Sdfr
23792756Salfredstatic int enable_aux_dev(KBDC);
23892756Salfredstatic int disable_aux_dev(KBDC);
23992756Salfredstatic int get_mouse_status(KBDC, int *, int, int);
24092756Salfredstatic int get_aux_id(KBDC);
24192756Salfredstatic int set_mouse_sampling_rate(KBDC, int);
24292756Salfredstatic int set_mouse_scaling(KBDC, int);
24392756Salfredstatic int set_mouse_resolution(KBDC, int);
24492756Salfredstatic int set_mouse_mode(KBDC);
24592756Salfredstatic int get_mouse_buttons(KBDC);
24692756Salfredstatic int is_a_mouse(int);
24792756Salfredstatic void recover_from_error(KBDC);
24892756Salfredstatic int restore_controller(KBDC, int);
24992756Salfredstatic int doinitialize(struct psm_softc *, mousemode_t *);
25092756Salfredstatic int doopen(struct psm_softc *, int);
25192756Salfredstatic int reinitialize(struct psm_softc *, int);
25292756Salfredstatic char *model_name(int);
25392756Salfredstatic void psmintr(void *);
25492756Salfredstatic void psmtimeout(void *);
25541016Sdfr
25641016Sdfr/* vendor specific features */
25792756Salfredtypedef int probefunc_t(struct psm_softc *);
25841016Sdfr
25992756Salfredstatic int mouse_id_proc1(KBDC, int, int, int *);
26092756Salfredstatic int mouse_ext_command(KBDC, int);
26141016Sdfrstatic probefunc_t enable_groller;
26241016Sdfrstatic probefunc_t enable_gmouse;
26341016Sdfrstatic probefunc_t enable_aglide;
26441016Sdfrstatic probefunc_t enable_kmouse;
26558230Syokotastatic probefunc_t enable_msexplorer;
26641016Sdfrstatic probefunc_t enable_msintelli;
26758230Syokotastatic probefunc_t enable_4dmouse;
26858230Syokotastatic probefunc_t enable_4dplus;
26941016Sdfrstatic probefunc_t enable_mmanplus;
27049965Syokotastatic probefunc_t enable_versapad;
27192756Salfredstatic int tame_mouse(struct psm_softc *, mousestatus_t *, unsigned char *);
27241016Sdfr
27341016Sdfrstatic struct {
27441016Sdfr    int                 model;
27541016Sdfr    unsigned char	syncmask;
27641016Sdfr    int 		packetsize;
27741016Sdfr    probefunc_t 	*probefunc;
27841016Sdfr} vendortype[] = {
27958230Syokota    /*
28058230Syokota     * WARNING: the order of probe is very important.  Don't mess it
28158230Syokota     * unless you know what you are doing.
28258230Syokota     */
28341016Sdfr    { MOUSE_MODEL_NET,			/* Genius NetMouse */
28458230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
28541016Sdfr    { MOUSE_MODEL_NETSCROLL,		/* Genius NetScroll */
28641016Sdfr      0xc8, 6, enable_groller, },
28758230Syokota    { MOUSE_MODEL_MOUSEMANPLUS,		/* Logitech MouseMan+ */
28858230Syokota      0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
28958230Syokota    { MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
29058230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
29158230Syokota    { MOUSE_MODEL_4D,			/* A4 Tech 4D Mouse */
29258230Syokota      0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
29358230Syokota    { MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
29458230Syokota      0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
29558230Syokota    { MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
29658230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
29741016Sdfr    { MOUSE_MODEL_GLIDEPOINT,		/* ALPS GlidePoint */
29841016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
29941016Sdfr    { MOUSE_MODEL_THINK,		/* Kensignton ThinkingMouse */
30041016Sdfr      0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
30149965Syokota    { MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
30249965Syokota      0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
30341016Sdfr    { MOUSE_MODEL_GENERIC,
30441016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
30541016Sdfr};
30663951Syokota#define GENERIC_MOUSE_ENTRY	((sizeof(vendortype) / sizeof(*vendortype)) - 1)
30741016Sdfr
30841016Sdfr/* device driver declarateion */
30941016Sdfrstatic device_method_t psm_methods[] = {
31041016Sdfr	/* Device interface */
31183147Syokota	DEVMETHOD(device_identify,	psmidentify),
31241016Sdfr	DEVMETHOD(device_probe,		psmprobe),
31341016Sdfr	DEVMETHOD(device_attach,	psmattach),
31458230Syokota	DEVMETHOD(device_detach,	psmdetach),
31554629Syokota	DEVMETHOD(device_resume,	psmresume),
31641016Sdfr
31741016Sdfr	{ 0, 0 }
31841016Sdfr};
31941016Sdfr
32041016Sdfrstatic driver_t psm_driver = {
32183492Syokota    PSM_DRIVER_NAME,
32241016Sdfr    psm_methods,
32341016Sdfr    sizeof(struct psm_softc),
32441016Sdfr};
32541016Sdfr
32641016Sdfr#define CDEV_MAJOR        21
32741016Sdfr
32847625Sphkstatic struct cdevsw psm_cdevsw = {
329111815Sphk	.d_open =	psmopen,
330111815Sphk	.d_close =	psmclose,
331111815Sphk	.d_read =	psmread,
332111815Sphk	.d_ioctl =	psmioctl,
333111815Sphk	.d_poll =	psmpoll,
334111815Sphk	.d_name =	PSM_DRIVER_NAME,
335111815Sphk	.d_maj =	CDEV_MAJOR,
33641016Sdfr};
33741016Sdfr
33841016Sdfr/* debug message level */
33941016Sdfrstatic int verbose = PSM_DEBUG;
34041016Sdfr
34141016Sdfr/* device I/O routines */
34241016Sdfrstatic int
34341016Sdfrenable_aux_dev(KBDC kbdc)
34441016Sdfr{
34541016Sdfr    int res;
34641016Sdfr
34741016Sdfr    res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
34841016Sdfr    if (verbose >= 2)
34941016Sdfr        log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
35041016Sdfr
35141016Sdfr    return (res == PSM_ACK);
35241016Sdfr}
35341016Sdfr
35441016Sdfrstatic int
35541016Sdfrdisable_aux_dev(KBDC kbdc)
35641016Sdfr{
35741016Sdfr    int res;
35841016Sdfr
35941016Sdfr    res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
36041016Sdfr    if (verbose >= 2)
36141016Sdfr        log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
36241016Sdfr
36341016Sdfr    return (res == PSM_ACK);
36441016Sdfr}
36541016Sdfr
36641016Sdfrstatic int
36741016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
36841016Sdfr{
36941016Sdfr    int cmd;
37041016Sdfr    int res;
37141016Sdfr    int i;
37241016Sdfr
37341016Sdfr    switch (flag) {
37441016Sdfr    case 0:
37541016Sdfr    default:
37641016Sdfr	cmd = PSMC_SEND_DEV_STATUS;
37741016Sdfr	break;
37841016Sdfr    case 1:
37941016Sdfr	cmd = PSMC_SEND_DEV_DATA;
38041016Sdfr	break;
38141016Sdfr    }
38241016Sdfr    empty_aux_buffer(kbdc, 5);
38341016Sdfr    res = send_aux_command(kbdc, cmd);
38441016Sdfr    if (verbose >= 2)
38541016Sdfr        log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
38641016Sdfr	    (flag == 1) ? "DATA" : "STATUS", res);
38741016Sdfr    if (res != PSM_ACK)
38841016Sdfr        return 0;
38941016Sdfr
39041016Sdfr    for (i = 0; i < len; ++i) {
39141016Sdfr        status[i] = read_aux_data(kbdc);
39241016Sdfr	if (status[i] < 0)
39341016Sdfr	    break;
39441016Sdfr    }
39541016Sdfr
39641016Sdfr    if (verbose) {
39741016Sdfr        log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
39841016Sdfr            (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
39941016Sdfr    }
40041016Sdfr
40141016Sdfr    return i;
40241016Sdfr}
40341016Sdfr
40441016Sdfrstatic int
40541016Sdfrget_aux_id(KBDC kbdc)
40641016Sdfr{
40741016Sdfr    int res;
40841016Sdfr    int id;
40941016Sdfr
41041016Sdfr    empty_aux_buffer(kbdc, 5);
41141016Sdfr    res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
41241016Sdfr    if (verbose >= 2)
41341016Sdfr        log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
41441016Sdfr    if (res != PSM_ACK)
41541016Sdfr	return (-1);
41641016Sdfr
41741016Sdfr    /* 10ms delay */
41841016Sdfr    DELAY(10000);
41941016Sdfr
42041016Sdfr    id = read_aux_data(kbdc);
42141016Sdfr    if (verbose >= 2)
42241016Sdfr        log(LOG_DEBUG, "psm: device ID: %04x\n", id);
42341016Sdfr
42441016Sdfr    return id;
42541016Sdfr}
42641016Sdfr
42741016Sdfrstatic int
42841016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
42941016Sdfr{
43041016Sdfr    int res;
43141016Sdfr
43241016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
43341016Sdfr    if (verbose >= 2)
43441016Sdfr        log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
43541016Sdfr
43641016Sdfr    return ((res == PSM_ACK) ? rate : -1);
43741016Sdfr}
43841016Sdfr
43941016Sdfrstatic int
44041016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
44141016Sdfr{
44241016Sdfr    int res;
44341016Sdfr
44441016Sdfr    switch (scale) {
44541016Sdfr    case 1:
44641016Sdfr    default:
44741016Sdfr	scale = PSMC_SET_SCALING11;
44841016Sdfr	break;
44941016Sdfr    case 2:
45041016Sdfr	scale = PSMC_SET_SCALING21;
45141016Sdfr	break;
45241016Sdfr    }
45341016Sdfr    res = send_aux_command(kbdc, scale);
45441016Sdfr    if (verbose >= 2)
45541016Sdfr        log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
45641016Sdfr	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
45741016Sdfr
45841016Sdfr    return (res == PSM_ACK);
45941016Sdfr}
46041016Sdfr
46141016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
46241016Sdfrstatic int
46341016Sdfrset_mouse_resolution(KBDC kbdc, int val)
46441016Sdfr{
46541016Sdfr    int res;
46641016Sdfr
46741016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
46841016Sdfr    if (verbose >= 2)
46941016Sdfr        log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
47041016Sdfr
47141016Sdfr    return ((res == PSM_ACK) ? val : -1);
47241016Sdfr}
47341016Sdfr
47441016Sdfr/*
47541016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
47641016Sdfr * re-enabled by calling `enable_aux_dev()'
47741016Sdfr */
47841016Sdfrstatic int
47941016Sdfrset_mouse_mode(KBDC kbdc)
48041016Sdfr{
48141016Sdfr    int res;
48241016Sdfr
48341016Sdfr    res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
48441016Sdfr    if (verbose >= 2)
48541016Sdfr        log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
48641016Sdfr
48741016Sdfr    return (res == PSM_ACK);
48841016Sdfr}
48941016Sdfr
49041016Sdfrstatic int
49141016Sdfrget_mouse_buttons(KBDC kbdc)
49241016Sdfr{
49341016Sdfr    int c = 2;		/* assume two buttons by default */
49441016Sdfr    int status[3];
49541016Sdfr
49641016Sdfr    /*
49741016Sdfr     * NOTE: a special sequence to obtain Logitech Mouse specific
49841016Sdfr     * information: set resolution to 25 ppi, set scaling to 1:1, set
49941016Sdfr     * scaling to 1:1, set scaling to 1:1. Then the second byte of the
50041016Sdfr     * mouse status bytes is the number of available buttons.
50141016Sdfr     * Some manufactures also support this sequence.
50241016Sdfr     */
50341016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
50441016Sdfr        return c;
50541016Sdfr    if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
50641016Sdfr        && set_mouse_scaling(kbdc, 1)
50741016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
50841016Sdfr        if (status[1] != 0)
50941016Sdfr            return status[1];
51041016Sdfr    }
51141016Sdfr    return c;
51241016Sdfr}
51341016Sdfr
51441016Sdfr/* misc subroutines */
51541016Sdfr/*
51641016Sdfr * Someday, I will get the complete list of valid pointing devices and
51741016Sdfr * their IDs... XXX
51841016Sdfr */
51941016Sdfrstatic int
52041016Sdfris_a_mouse(int id)
52141016Sdfr{
52241016Sdfr#if 0
52341016Sdfr    static int valid_ids[] = {
52441016Sdfr        PSM_MOUSE_ID,		/* mouse */
52541016Sdfr        PSM_BALLPOINT_ID,	/* ballpoint device */
52641016Sdfr        PSM_INTELLI_ID,		/* Intellimouse */
52758230Syokota        PSM_EXPLORER_ID,	/* Intellimouse Explorer */
52841016Sdfr        -1			/* end of table */
52941016Sdfr    };
53041016Sdfr    int i;
53141016Sdfr
53241016Sdfr    for (i = 0; valid_ids[i] >= 0; ++i)
53341016Sdfr        if (valid_ids[i] == id)
53441016Sdfr            return TRUE;
53541016Sdfr    return FALSE;
53641016Sdfr#else
53741016Sdfr    return TRUE;
53841016Sdfr#endif
53941016Sdfr}
54041016Sdfr
54141016Sdfrstatic char *
54241016Sdfrmodel_name(int model)
54341016Sdfr{
54441016Sdfr    static struct {
54541016Sdfr	int model_code;
54641016Sdfr	char *model_name;
54741016Sdfr    } models[] = {
54858230Syokota        { MOUSE_MODEL_NETSCROLL,	"NetScroll" },
54958230Syokota        { MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
55041016Sdfr        { MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
55141016Sdfr        { MOUSE_MODEL_THINK,		"ThinkingMouse" },
55241016Sdfr        { MOUSE_MODEL_INTELLI,		"IntelliMouse" },
55341016Sdfr        { MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
55449965Syokota        { MOUSE_MODEL_VERSAPAD,		"VersaPad" },
55558230Syokota        { MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
55658230Syokota        { MOUSE_MODEL_4D,		"4D Mouse" },
55758230Syokota        { MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
55841016Sdfr        { MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
55941016Sdfr        { MOUSE_MODEL_UNKNOWN,		NULL },
56041016Sdfr    };
56141016Sdfr    int i;
56241016Sdfr
56341016Sdfr    for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
56441016Sdfr	if (models[i].model_code == model)
56541016Sdfr	    return models[i].model_name;
56641016Sdfr    }
56741016Sdfr    return "Unknown";
56841016Sdfr}
56941016Sdfr
57041016Sdfrstatic void
57141016Sdfrrecover_from_error(KBDC kbdc)
57241016Sdfr{
57341016Sdfr    /* discard anything left in the output buffer */
57441016Sdfr    empty_both_buffers(kbdc, 10);
57541016Sdfr
57641016Sdfr#if 0
57741016Sdfr    /*
57841016Sdfr     * NOTE: KBDC_RESET_KBD may not restore the communication between the
57941016Sdfr     * keyboard and the controller.
58041016Sdfr     */
58141016Sdfr    reset_kbd(kbdc);
58241016Sdfr#else
58341016Sdfr    /*
58441016Sdfr     * NOTE: somehow diagnostic and keyboard port test commands bring the
58541016Sdfr     * keyboard back.
58641016Sdfr     */
58741016Sdfr    if (!test_controller(kbdc))
58841016Sdfr        log(LOG_ERR, "psm: keyboard controller failed.\n");
58941016Sdfr    /* if there isn't a keyboard in the system, the following error is OK */
59041016Sdfr    if (test_kbd_port(kbdc) != 0) {
59141016Sdfr	if (verbose)
59241016Sdfr	    log(LOG_ERR, "psm: keyboard port failed.\n");
59341016Sdfr    }
59441016Sdfr#endif
59541016Sdfr}
59641016Sdfr
59741016Sdfrstatic int
59841016Sdfrrestore_controller(KBDC kbdc, int command_byte)
59941016Sdfr{
60041016Sdfr    empty_both_buffers(kbdc, 10);
60141016Sdfr
60241016Sdfr    if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
60341016Sdfr	log(LOG_ERR, "psm: failed to restore the keyboard controller "
60441016Sdfr		     "command byte.\n");
60558230Syokota	empty_both_buffers(kbdc, 10);
60641016Sdfr	return FALSE;
60741016Sdfr    } else {
60858230Syokota	empty_both_buffers(kbdc, 10);
60941016Sdfr	return TRUE;
61041016Sdfr    }
61141016Sdfr}
61241016Sdfr
61341016Sdfr/*
61441016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
61541016Sdfr * and its interrupt must be disabled before calling this routine.
61641016Sdfr * The aux device will be disabled before returning.
61741016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
61841016Sdfr * calling this routine.
61941016Sdfr */
62041016Sdfrstatic int
62184880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode)
62241016Sdfr{
62341016Sdfr    KBDC kbdc = sc->kbdc;
62441016Sdfr    int stat[3];
62541016Sdfr    int i;
62641016Sdfr
62741016Sdfr    switch((i = test_aux_port(kbdc))) {
62841016Sdfr    case 1:	/* ignore this error */
62941016Sdfr    case PSM_ACK:
63041016Sdfr	if (verbose)
63141016Sdfr	    log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
63284880Syokota	        sc->unit, i);
633102412Scharnier	/* FALLTHROUGH */
63441016Sdfr    case 0:	/* no error */
63541016Sdfr    	break;
63641016Sdfr    case -1: 	/* time out */
63741016Sdfr    default: 	/* error */
63841016Sdfr    	recover_from_error(kbdc);
63945789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
64045789Speter	    break;
64141016Sdfr    	log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
64284880Syokota    	    sc->unit, i);
64341016Sdfr    	return FALSE;
64441016Sdfr    }
64541016Sdfr
64645789Speter    if (sc->config & PSM_CONFIG_NORESET) {
64745789Speter	/*
64845789Speter	 * Don't try to reset the pointing device.  It may possibly be
64945789Speter	 * left in the unknown state, though...
65045789Speter	 */
65145789Speter    } else {
65245789Speter	/*
65345789Speter	 * NOTE: some controllers appears to hang the `keyboard' when
65445789Speter	 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
65545789Speter	 */
65645789Speter	if (!reset_aux_dev(kbdc)) {
65745789Speter            recover_from_error(kbdc);
65884880Syokota            log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
65945789Speter            return FALSE;
66045789Speter	}
66141016Sdfr    }
66241016Sdfr
66341016Sdfr    /*
66441016Sdfr     * both the aux port and the aux device is functioning, see
66541016Sdfr     * if the device can be enabled.
66641016Sdfr     */
66741016Sdfr    if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
66884880Syokota        log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
66941016Sdfr        return FALSE;
67041016Sdfr    }
67141016Sdfr    empty_both_buffers(kbdc, 10);	/* remove stray data if any */
67241016Sdfr
67345789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
67445789Speter	i = GENERIC_MOUSE_ENTRY;
67545789Speter    } else {
67645789Speter	/* FIXME: hardware ID, mouse buttons? */
67741016Sdfr
67845789Speter	/* other parameters */
67945789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
68045789Speter	    if ((*vendortype[i].probefunc)(sc)) {
68145789Speter		if (verbose >= 2)
68245789Speter		    log(LOG_ERR, "psm%d: found %s\n",
68384880Syokota			sc->unit, model_name(vendortype[i].model));
68445789Speter		break;
68545789Speter	    }
68641016Sdfr	}
68741016Sdfr    }
68841016Sdfr
68941016Sdfr    sc->hw.model = vendortype[i].model;
69041016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
69141016Sdfr
69241016Sdfr    /* set mouse parameters */
69341016Sdfr    if (mode != (mousemode_t *)NULL) {
69441016Sdfr	if (mode->rate > 0)
69541016Sdfr            mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
69641016Sdfr	if (mode->resolution >= 0)
69741016Sdfr            mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
69841016Sdfr        set_mouse_scaling(kbdc, 1);
69941016Sdfr        set_mouse_mode(kbdc);
70041016Sdfr    }
70141016Sdfr
70241016Sdfr    /* request a data packet and extract sync. bits */
70341016Sdfr    if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
70484880Syokota        log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
70584880Syokota	    sc->unit);
70641016Sdfr        sc->mode.syncmask[0] = 0;
70741016Sdfr    } else {
70841016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
70941016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
71041016Sdfr	empty_aux_buffer(kbdc, 5);
71141016Sdfr    }
71241016Sdfr
71341016Sdfr    /* just check the status of the mouse */
71441016Sdfr    if (get_mouse_status(kbdc, stat, 0, 3) < 3)
71584880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
71684880Syokota	    sc->unit);
71741016Sdfr
71841016Sdfr    return TRUE;
71941016Sdfr}
72041016Sdfr
72141016Sdfrstatic int
72284880Syokotadoopen(struct psm_softc *sc, int command_byte)
72341016Sdfr{
72441016Sdfr    int stat[3];
72541016Sdfr
72641016Sdfr    /* enable the mouse device */
72741016Sdfr    if (!enable_aux_dev(sc->kbdc)) {
72841016Sdfr	/* MOUSE ERROR: failed to enable the mouse because:
72941016Sdfr	 * 1) the mouse is faulty,
73041016Sdfr	 * 2) the mouse has been removed(!?)
73141016Sdfr	 * In the latter case, the keyboard may have hung, and need
73241016Sdfr	 * recovery procedure...
73341016Sdfr	 */
73441016Sdfr	recover_from_error(sc->kbdc);
73541016Sdfr#if 0
73641016Sdfr	/* FIXME: we could reset the mouse here and try to enable
73741016Sdfr	 * it again. But it will take long time and it's not a good
73841016Sdfr	 * idea to disable the keyboard that long...
73941016Sdfr	 */
74084880Syokota	if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
74141016Sdfr	    recover_from_error(sc->kbdc);
74241016Sdfr#else
74341016Sdfr        {
74441016Sdfr#endif
74541016Sdfr            restore_controller(sc->kbdc, command_byte);
74641016Sdfr	    /* mark this device is no longer available */
74741016Sdfr	    sc->state &= ~PSM_VALID;
74841016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
74984880Syokota		sc->unit);
75041016Sdfr	    return (EIO);
75141016Sdfr	}
75241016Sdfr    }
75341016Sdfr
75441016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
75584880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
75641016Sdfr
75741016Sdfr    /* enable the aux port and interrupt */
75841016Sdfr    if (!set_controller_command_byte(sc->kbdc,
75941016Sdfr	    kbdc_get_device_mask(sc->kbdc),
76041016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
76141016Sdfr		| KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
76241016Sdfr	/* CONTROLLER ERROR */
76341016Sdfr	disable_aux_dev(sc->kbdc);
76441016Sdfr        restore_controller(sc->kbdc, command_byte);
76541016Sdfr	log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
76684880Syokota	    sc->unit);
76741016Sdfr	return (EIO);
76841016Sdfr    }
76941016Sdfr
77058230Syokota    /* start the watchdog timer */
77158230Syokota    sc->watchdog = FALSE;
77284880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
77358230Syokota
77441016Sdfr    return (0);
77541016Sdfr}
77641016Sdfr
77784880Syokotastatic int
77884880Syokotareinitialize(struct psm_softc *sc, int doinit)
77984880Syokota{
78084880Syokota    int err;
78184880Syokota    int c;
78284880Syokota    int s;
78384880Syokota
78484880Syokota    /* don't let anybody mess with the aux device */
78584880Syokota    if (!kbdc_lock(sc->kbdc, TRUE))
78684880Syokota	return (EIO);
78784880Syokota    s = spltty();
78884880Syokota
78984880Syokota    /* block our watchdog timer */
79084880Syokota    sc->watchdog = FALSE;
79184880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
79284880Syokota    callout_handle_init(&sc->callout);
79384880Syokota
79484880Syokota    /* save the current controller command byte */
79584880Syokota    empty_both_buffers(sc->kbdc, 10);
79684880Syokota    c = get_controller_command_byte(sc->kbdc);
79784880Syokota    if (verbose >= 2)
79884880Syokota        log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
79984880Syokota	    sc->unit, c);
80084880Syokota
80184880Syokota    /* enable the aux port but disable the aux interrupt and the keyboard */
80284880Syokota    if ((c == -1) || !set_controller_command_byte(sc->kbdc,
80384880Syokota	    kbdc_get_device_mask(sc->kbdc),
80484880Syokota  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
80584880Syokota	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
80684880Syokota        /* CONTROLLER ERROR */
80784880Syokota	splx(s);
80884880Syokota        kbdc_lock(sc->kbdc, FALSE);
80984880Syokota	log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
81084880Syokota	    sc->unit);
81184880Syokota	return (EIO);
81284880Syokota    }
81384880Syokota
81484880Syokota    /* flush any data */
81584880Syokota    if (sc->state & PSM_VALID) {
81684880Syokota	disable_aux_dev(sc->kbdc);	/* this may fail; but never mind... */
81784880Syokota	empty_aux_buffer(sc->kbdc, 10);
81884880Syokota    }
81984880Syokota    sc->inputbytes = 0;
82084880Syokota    sc->syncerrors = 0;
82184880Syokota
82284880Syokota    /* try to detect the aux device; are you still there? */
82384880Syokota    err = 0;
82484880Syokota    if (doinit) {
82584880Syokota	if (doinitialize(sc, &sc->mode)) {
82684880Syokota	    /* yes */
82784880Syokota	    sc->state |= PSM_VALID;
82884880Syokota	} else {
82984880Syokota	    /* the device has gone! */
83084880Syokota	    restore_controller(sc->kbdc, c);
83184880Syokota	    sc->state &= ~PSM_VALID;
83284880Syokota	    log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
83384880Syokota		sc->unit);
83484880Syokota	    err = ENXIO;
83584880Syokota	}
83684880Syokota    }
83784880Syokota    splx(s);
83884880Syokota
83984880Syokota    /* restore the driver state */
84084880Syokota    if ((sc->state & PSM_OPEN) && (err == 0)) {
84184880Syokota        /* enable the aux device and the port again */
84284880Syokota	err = doopen(sc, c);
84384880Syokota	if (err != 0)
84484880Syokota	    log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
84584880Syokota		sc->unit);
84684880Syokota    } else {
84784880Syokota        /* restore the keyboard port and disable the aux port */
84884880Syokota        if (!set_controller_command_byte(sc->kbdc,
84984880Syokota                kbdc_get_device_mask(sc->kbdc),
85084880Syokota                (c & KBD_KBD_CONTROL_BITS)
85184880Syokota                    | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
85284880Syokota            /* CONTROLLER ERROR */
85384880Syokota            log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
85484880Syokota                sc->unit);
85584880Syokota            err = EIO;
85684880Syokota	}
85784880Syokota    }
85884880Syokota
85984880Syokota    kbdc_lock(sc->kbdc, FALSE);
86084880Syokota    return (err);
86184880Syokota}
86284880Syokota
86341016Sdfr/* psm driver entry points */
86441016Sdfr
86583147Syokotastatic void
86683147Syokotapsmidentify(driver_t *driver, device_t parent)
86783147Syokota{
86883931Syokota    device_t psmc;
86983931Syokota    device_t psm;
87083931Syokota    u_long irq;
87183931Syokota    int unit;
87283147Syokota
87383931Syokota    unit = device_get_unit(parent);
87483931Syokota
87583147Syokota    /* always add at least one child */
87683931Syokota    psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
87783931Syokota    if (psm == NULL)
87883931Syokota	return;
87983931Syokota
88083931Syokota    irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
88183931Syokota    if (irq > 0)
88283931Syokota	return;
88383931Syokota
88483931Syokota    /*
88583931Syokota     * If the PS/2 mouse device has already been reported by ACPI or
88683931Syokota     * PnP BIOS, obtain the IRQ resource from it.
88783931Syokota     * (See psmcpnp_attach() below.)
88883931Syokota     */
88983931Syokota    psmc = device_find_child(device_get_parent(parent),
89083931Syokota			     PSMCPNP_DRIVER_NAME, unit);
89183931Syokota    if (psmc == NULL)
89283931Syokota	return;
89383931Syokota    irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
89483931Syokota    if (irq <= 0)
89583931Syokota	return;
89683931Syokota    bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
89783147Syokota}
89883147Syokota
89941016Sdfr#define endprobe(v)	{   if (bootverbose) 				\
90041016Sdfr				--verbose;   				\
90141016Sdfr                            kbdc_set_device_mask(sc->kbdc, mask);	\
90241016Sdfr			    kbdc_lock(sc->kbdc, FALSE);			\
90341016Sdfr			    return (v);	     				\
90441016Sdfr			}
90541016Sdfr
90641016Sdfrstatic int
90741016Sdfrpsmprobe(device_t dev)
90841016Sdfr{
90941016Sdfr    int unit = device_get_unit(dev);
91041016Sdfr    struct psm_softc *sc = device_get_softc(dev);
91141016Sdfr    int stat[3];
91241016Sdfr    int command_byte;
91341016Sdfr    int mask;
91458230Syokota    int rid;
91541016Sdfr    int i;
91641016Sdfr
91741016Sdfr#if 0
91841016Sdfr    kbdc_debug(TRUE);
91941016Sdfr#endif
92058230Syokota
92183147Syokota    /* see if IRQ is available */
92283147Syokota    rid = KBDC_RID_AUX;
92383147Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
92483147Syokota				  RF_SHAREABLE | RF_ACTIVE);
92583147Syokota    if (sc->intr == NULL) {
92683147Syokota	if (bootverbose)
92783147Syokota            device_printf(dev, "unable to allocate IRQ\n");
92883147Syokota        return (ENXIO);
92983147Syokota    }
93083147Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
93158230Syokota
93284880Syokota    sc->unit = unit;
93358230Syokota    sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
93483147Syokota    sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
93558230Syokota    /* XXX: for backward compatibility */
93658230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
93758230Syokota    sc->config |=
93858230Syokota#ifdef PSM_RESETAFTERSUSPEND
93958230Syokota	PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
94058230Syokota#else
94158230Syokota	PSM_CONFIG_HOOKRESUME;
94258230Syokota#endif
94358230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
94441016Sdfr    sc->flags = 0;
94541016Sdfr    if (bootverbose)
94641016Sdfr        ++verbose;
94741016Sdfr
94843105Sdfr    device_set_desc(dev, "PS/2 Mouse");
94943105Sdfr
95041016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE)) {
95141016Sdfr        printf("psm%d: unable to lock the controller.\n", unit);
95241016Sdfr        if (bootverbose)
95341016Sdfr            --verbose;
95441016Sdfr	return (ENXIO);
95541016Sdfr    }
95641016Sdfr
95741016Sdfr    /*
95841016Sdfr     * NOTE: two bits in the command byte controls the operation of the
95941016Sdfr     * aux port (mouse port): the aux port disable bit (bit 5) and the aux
96041016Sdfr     * port interrupt (IRQ 12) enable bit (bit 2).
96141016Sdfr     */
96241016Sdfr
96341016Sdfr    /* discard anything left after the keyboard initialization */
96441016Sdfr    empty_both_buffers(sc->kbdc, 10);
96541016Sdfr
96641016Sdfr    /* save the current command byte; it will be used later */
96741016Sdfr    mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
96841016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
96941016Sdfr    if (verbose)
97041016Sdfr        printf("psm%d: current command byte:%04x\n", unit, command_byte);
97141016Sdfr    if (command_byte == -1) {
97241016Sdfr        /* CONTROLLER ERROR */
97341016Sdfr        printf("psm%d: unable to get the current command byte value.\n",
97441016Sdfr            unit);
97541016Sdfr        endprobe(ENXIO);
97641016Sdfr    }
97741016Sdfr
97841016Sdfr    /*
97941016Sdfr     * disable the keyboard port while probing the aux port, which must be
98041016Sdfr     * enabled during this routine
98141016Sdfr     */
98241016Sdfr    if (!set_controller_command_byte(sc->kbdc,
98341016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
98441016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
98541016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
98641016Sdfr        /*
98741016Sdfr	 * this is CONTROLLER ERROR; I don't know how to recover
98841016Sdfr         * from this error...
98941016Sdfr	 */
99041016Sdfr        restore_controller(sc->kbdc, command_byte);
99141016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
99241016Sdfr        endprobe(ENXIO);
99341016Sdfr    }
99445789Speter    write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
99541016Sdfr
99641016Sdfr    /*
99741016Sdfr     * NOTE: `test_aux_port()' is designed to return with zero if the aux
99841016Sdfr     * port exists and is functioning. However, some controllers appears
99941016Sdfr     * to respond with zero even when the aux port doesn't exist. (It may
100041016Sdfr     * be that this is only the case when the controller DOES have the aux
100141016Sdfr     * port but the port is not wired on the motherboard.) The keyboard
100241016Sdfr     * controllers without the port, such as the original AT, are
100341016Sdfr     * supporsed to return with an error code or simply time out. In any
100441016Sdfr     * case, we have to continue probing the port even when the controller
100541016Sdfr     * passes this test.
100641016Sdfr     *
100741016Sdfr     * XXX: some controllers erroneously return the error code 1 when
100841016Sdfr     * it has the perfectly functional aux port. We have to ignore this
100941016Sdfr     * error code. Even if the controller HAS error with the aux port,
101041016Sdfr     * it will be detected later...
101141016Sdfr     * XXX: another incompatible controller returns PSM_ACK (0xfa)...
101241016Sdfr     */
101341016Sdfr    switch ((i = test_aux_port(sc->kbdc))) {
101441016Sdfr    case 1:	   /* ignore this error */
101541016Sdfr    case PSM_ACK:
101641016Sdfr        if (verbose)
101741016Sdfr	    printf("psm%d: strange result for test aux port (%d).\n",
101841016Sdfr	        unit, i);
1019102412Scharnier	/* FALLTHROUGH */
102041016Sdfr    case 0:        /* no error */
102141016Sdfr        break;
102241016Sdfr    case -1:        /* time out */
102341016Sdfr    default:        /* error */
102441016Sdfr        recover_from_error(sc->kbdc);
102545789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
102645789Speter	    break;
102741016Sdfr        restore_controller(sc->kbdc, command_byte);
102841016Sdfr        if (verbose)
102941016Sdfr            printf("psm%d: the aux port is not functioning (%d).\n",
103041016Sdfr                unit, i);
103141016Sdfr        endprobe(ENXIO);
103241016Sdfr    }
103341016Sdfr
103445789Speter    if (sc->config & PSM_CONFIG_NORESET) {
103545789Speter	/*
103645789Speter	 * Don't try to reset the pointing device.  It may possibly be
103745789Speter	 * left in the unknown state, though...
103845789Speter	 */
103945789Speter    } else {
104045789Speter	/*
104145789Speter	 * NOTE: some controllers appears to hang the `keyboard' when the aux
104245789Speter	 * port doesn't exist and `PSMC_RESET_DEV' is issued.
104345789Speter	 */
104445789Speter	if (!reset_aux_dev(sc->kbdc)) {
104545789Speter            recover_from_error(sc->kbdc);
104645789Speter            restore_controller(sc->kbdc, command_byte);
104745789Speter            if (verbose)
104845789Speter        	printf("psm%d: failed to reset the aux device.\n", unit);
104945789Speter            endprobe(ENXIO);
105045789Speter	}
105141016Sdfr    }
105245789Speter
105341016Sdfr    /*
105441016Sdfr     * both the aux port and the aux device is functioning, see if the
105541016Sdfr     * device can be enabled. NOTE: when enabled, the device will start
105641016Sdfr     * sending data; we shall immediately disable the device once we know
105741016Sdfr     * the device can be enabled.
105841016Sdfr     */
105941016Sdfr    if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
106045789Speter        /* MOUSE ERROR */
106145789Speter	recover_from_error(sc->kbdc);
106245789Speter	restore_controller(sc->kbdc, command_byte);
106345789Speter	if (verbose)
106445789Speter	    printf("psm%d: failed to enable the aux device.\n", unit);
106541016Sdfr        endprobe(ENXIO);
106641016Sdfr    }
106741016Sdfr
106841016Sdfr    /* save the default values after reset */
106941016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
107041016Sdfr	sc->dflt_mode.rate = sc->mode.rate = stat[2];
107141016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
107241016Sdfr    } else {
107341016Sdfr	sc->dflt_mode.rate = sc->mode.rate = -1;
107441016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = -1;
107541016Sdfr    }
107641016Sdfr
107741016Sdfr    /* hardware information */
107841016Sdfr    sc->hw.iftype = MOUSE_IF_PS2;
107941016Sdfr
108041016Sdfr    /* verify the device is a mouse */
108141016Sdfr    sc->hw.hwid = get_aux_id(sc->kbdc);
108241016Sdfr    if (!is_a_mouse(sc->hw.hwid)) {
108341016Sdfr        restore_controller(sc->kbdc, command_byte);
108441016Sdfr        if (verbose)
108541016Sdfr            printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
108641016Sdfr        endprobe(ENXIO);
108741016Sdfr    }
108841016Sdfr    switch (sc->hw.hwid) {
108941016Sdfr    case PSM_BALLPOINT_ID:
109041016Sdfr        sc->hw.type = MOUSE_TRACKBALL;
109141016Sdfr        break;
109241016Sdfr    case PSM_MOUSE_ID:
109341016Sdfr    case PSM_INTELLI_ID:
109458230Syokota    case PSM_EXPLORER_ID:
109558230Syokota    case PSM_4DMOUSE_ID:
109658230Syokota    case PSM_4DPLUS_ID:
109741016Sdfr        sc->hw.type = MOUSE_MOUSE;
109841016Sdfr        break;
109941016Sdfr    default:
110041016Sdfr        sc->hw.type = MOUSE_UNKNOWN;
110141016Sdfr        break;
110241016Sdfr    }
110341016Sdfr
110445789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
110545789Speter	sc->hw.buttons = 2;
110645789Speter	i = GENERIC_MOUSE_ENTRY;
110745789Speter    } else {
110845789Speter	/* # of buttons */
110945789Speter	sc->hw.buttons = get_mouse_buttons(sc->kbdc);
111041016Sdfr
111145789Speter	/* other parameters */
111245789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
111345789Speter	    if ((*vendortype[i].probefunc)(sc)) {
111445789Speter		if (verbose >= 2)
111545789Speter		    printf("psm%d: found %s\n",
111645789Speter			   unit, model_name(vendortype[i].model));
111745789Speter		break;
111845789Speter	    }
111941016Sdfr	}
112041016Sdfr    }
112141016Sdfr
112241016Sdfr    sc->hw.model = vendortype[i].model;
112341016Sdfr
112441016Sdfr    sc->dflt_mode.level = PSM_LEVEL_BASE;
112541016Sdfr    sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
112641016Sdfr    sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
112741016Sdfr    if (sc->config & PSM_CONFIG_NOCHECKSYNC)
112841016Sdfr        sc->dflt_mode.syncmask[0] = 0;
112941016Sdfr    else
113041016Sdfr        sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
113145789Speter    if (sc->config & PSM_CONFIG_FORCETAP)
113245789Speter        sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
113341016Sdfr    sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
113441016Sdfr    sc->mode = sc->dflt_mode;
113541016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
113641016Sdfr
113741016Sdfr    /* set mouse parameters */
113848773Syokota#if 0
113948773Syokota    /*
114048773Syokota     * A version of Logitech FirstMouse+ won't report wheel movement,
114148773Syokota     * if SET_DEFAULTS is sent...  Don't use this command.
114248773Syokota     * This fix was found by Takashi Nishida.
114348773Syokota     */
114441016Sdfr    i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
114541016Sdfr    if (verbose >= 2)
114641016Sdfr	printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
114748773Syokota#endif
114841016Sdfr    if (sc->config & PSM_CONFIG_RESOLUTION) {
114941016Sdfr        sc->mode.resolution
115041016Sdfr	    = set_mouse_resolution(sc->kbdc,
115148773Syokota				   (sc->config & PSM_CONFIG_RESOLUTION) - 1);
115248773Syokota    } else if (sc->mode.resolution >= 0) {
115348773Syokota	sc->mode.resolution
115448773Syokota	    = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
115541016Sdfr    }
115648773Syokota    if (sc->mode.rate > 0) {
115748773Syokota	sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
115848773Syokota    }
115948773Syokota    set_mouse_scaling(sc->kbdc, 1);
116041016Sdfr
116141016Sdfr    /* request a data packet and extract sync. bits */
116241016Sdfr    if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
116341016Sdfr        printf("psm%d: failed to get data.\n", unit);
116441016Sdfr        sc->mode.syncmask[0] = 0;
116541016Sdfr    } else {
116641016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
116741016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
116841016Sdfr	empty_aux_buffer(sc->kbdc, 5);
116941016Sdfr    }
117041016Sdfr
117141016Sdfr    /* just check the status of the mouse */
117241016Sdfr    /*
117341016Sdfr     * NOTE: XXX there are some arcane controller/mouse combinations out
117441016Sdfr     * there, which hung the controller unless there is data transmission
117541016Sdfr     * after ACK from the mouse.
117641016Sdfr     */
117741016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
117841016Sdfr        printf("psm%d: failed to get status.\n", unit);
117941016Sdfr    } else {
118041016Sdfr	/*
118141016Sdfr	 * When in its native mode, some mice operate with different
118241016Sdfr	 * default parameters than in the PS/2 compatible mode.
118341016Sdfr	 */
118441016Sdfr        sc->dflt_mode.rate = sc->mode.rate = stat[2];
118541016Sdfr        sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
118641016Sdfr     }
118741016Sdfr
118841016Sdfr    /* disable the aux port for now... */
118941016Sdfr    if (!set_controller_command_byte(sc->kbdc,
119041016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
119141016Sdfr            (command_byte & KBD_KBD_CONTROL_BITS)
119241016Sdfr                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
119341016Sdfr        /*
119441016Sdfr	 * this is CONTROLLER ERROR; I don't know the proper way to
119541016Sdfr         * recover from this error...
119641016Sdfr	 */
119741016Sdfr        restore_controller(sc->kbdc, command_byte);
119841016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
119941016Sdfr        endprobe(ENXIO);
120041016Sdfr    }
120141016Sdfr
120241016Sdfr    /* done */
120341016Sdfr    kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
120441016Sdfr    kbdc_lock(sc->kbdc, FALSE);
120541016Sdfr    return (0);
120641016Sdfr}
120741016Sdfr
120841016Sdfrstatic int
120941016Sdfrpsmattach(device_t dev)
121041016Sdfr{
121141016Sdfr    int unit = device_get_unit(dev);
121241016Sdfr    struct psm_softc *sc = device_get_softc(dev);
121358230Syokota    int error;
121458230Syokota    int rid;
121541016Sdfr
121641016Sdfr    if (sc == NULL)    /* shouldn't happen */
121741016Sdfr	return (ENXIO);
121841016Sdfr
121941016Sdfr    /* Setup initial state */
122041016Sdfr    sc->state = PSM_VALID;
122158230Syokota    callout_handle_init(&sc->callout);
122241016Sdfr
122358230Syokota    /* Setup our interrupt handler */
122483147Syokota    rid = KBDC_RID_AUX;
122583147Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
122683147Syokota				  RF_SHAREABLE | RF_ACTIVE);
122758230Syokota    if (sc->intr == NULL)
122858230Syokota	return (ENXIO);
122983147Syokota    error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
123058230Syokota    if (error) {
123158230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
123258230Syokota	return (error);
123358230Syokota    }
123458230Syokota
123541016Sdfr    /* Done */
123658230Syokota    sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
123758230Syokota		       "psm%d", unit);
123858230Syokota    sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
123958230Syokota			"bpsm%d", unit);
124041016Sdfr
124141016Sdfr    if (!verbose) {
124241016Sdfr        printf("psm%d: model %s, device ID %d\n",
124348778Syokota	    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
124441016Sdfr    } else {
124548778Syokota        printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
124648778Syokota	    unit, model_name(sc->hw.model),
124748778Syokota	    sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
124841016Sdfr	printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
124941016Sdfr	    unit, sc->config, sc->flags, sc->mode.packetsize);
125041016Sdfr	printf("psm%d: syncmask:%02x, syncbits:%02x\n",
125141016Sdfr	    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
125241016Sdfr    }
125341016Sdfr
125441016Sdfr    if (bootverbose)
125541016Sdfr        --verbose;
125641016Sdfr
125741016Sdfr    return (0);
125841016Sdfr}
125941016Sdfr
126041016Sdfrstatic int
126158230Syokotapsmdetach(device_t dev)
126258230Syokota{
126358230Syokota    struct psm_softc *sc;
126458230Syokota    int rid;
126558230Syokota
126658230Syokota    sc = device_get_softc(dev);
126758230Syokota    if (sc->state & PSM_OPEN)
126858230Syokota	return EBUSY;
126958230Syokota
127083147Syokota    rid = KBDC_RID_AUX;
127183147Syokota    bus_teardown_intr(dev, sc->intr, sc->ih);
127258230Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
127358230Syokota
127458230Syokota    destroy_dev(sc->dev);
127558230Syokota    destroy_dev(sc->bdev);
127658230Syokota
127758230Syokota    return 0;
127858230Syokota}
127958230Syokota
128058230Syokotastatic int
128183366Sjulianpsmopen(dev_t dev, int flag, int fmt, struct thread *td)
128241016Sdfr{
128341016Sdfr    int unit = PSM_UNIT(dev);
128441016Sdfr    struct psm_softc *sc;
128541016Sdfr    int command_byte;
128641016Sdfr    int err;
128741016Sdfr    int s;
128841016Sdfr
128941016Sdfr    /* Get device data */
129041016Sdfr    sc = PSM_SOFTC(unit);
129141016Sdfr    if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
129241016Sdfr	/* the device is no longer valid/functioning */
129341016Sdfr        return (ENXIO);
129441016Sdfr
129541016Sdfr    /* Disallow multiple opens */
129641016Sdfr    if (sc->state & PSM_OPEN)
129741016Sdfr        return (EBUSY);
129841016Sdfr
129941016Sdfr    device_busy(devclass_get_device(psm_devclass, unit));
130041016Sdfr
130141016Sdfr    /* Initialize state */
130241016Sdfr    sc->mode.level = sc->dflt_mode.level;
130341016Sdfr    sc->mode.protocol = sc->dflt_mode.protocol;
130458230Syokota    sc->watchdog = FALSE;
130541016Sdfr
130641016Sdfr    /* flush the event queue */
130741016Sdfr    sc->queue.count = 0;
130841016Sdfr    sc->queue.head = 0;
130941016Sdfr    sc->queue.tail = 0;
131041016Sdfr    sc->status.flags = 0;
131141016Sdfr    sc->status.button = 0;
131241016Sdfr    sc->status.obutton = 0;
131341016Sdfr    sc->status.dx = 0;
131441016Sdfr    sc->status.dy = 0;
131541016Sdfr    sc->status.dz = 0;
131641016Sdfr    sc->button = 0;
131741016Sdfr
131841016Sdfr    /* empty input buffer */
131941016Sdfr    bzero(sc->ipacket, sizeof(sc->ipacket));
132041016Sdfr    sc->inputbytes = 0;
132184880Syokota    sc->syncerrors = 0;
132241016Sdfr
132341016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
132441016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
132541016Sdfr	return (EIO);
132641016Sdfr
132741016Sdfr    /* save the current controller command byte */
132841016Sdfr    s = spltty();
132941016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
133041016Sdfr
133141016Sdfr    /* enable the aux port and temporalily disable the keyboard */
133241016Sdfr    if ((command_byte == -1)
133341016Sdfr        || !set_controller_command_byte(sc->kbdc,
133441016Sdfr	    kbdc_get_device_mask(sc->kbdc),
133541016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
133641016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
133741016Sdfr        /* CONTROLLER ERROR; do you know how to get out of this? */
133841016Sdfr        kbdc_lock(sc->kbdc, FALSE);
133941016Sdfr	splx(s);
134041016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
134141016Sdfr	    unit);
134241016Sdfr	return (EIO);
134341016Sdfr    }
134441016Sdfr    /*
134541016Sdfr     * Now that the keyboard controller is told not to generate
134641016Sdfr     * the keyboard and mouse interrupts, call `splx()' to allow
134741016Sdfr     * the other tty interrupts. The clock interrupt may also occur,
134841016Sdfr     * but timeout routines will be blocked by the poll flag set
134941016Sdfr     * via `kbdc_lock()'
135041016Sdfr     */
135141016Sdfr    splx(s);
135241016Sdfr
135341016Sdfr    /* enable the mouse device */
135484880Syokota    err = doopen(sc, command_byte);
135541016Sdfr
135641016Sdfr    /* done */
135741016Sdfr    if (err == 0)
135841016Sdfr        sc->state |= PSM_OPEN;
135941016Sdfr    kbdc_lock(sc->kbdc, FALSE);
136041016Sdfr    return (err);
136141016Sdfr}
136241016Sdfr
136341016Sdfrstatic int
136483366Sjulianpsmclose(dev_t dev, int flag, int fmt, struct thread *td)
136541016Sdfr{
136641016Sdfr    int unit = PSM_UNIT(dev);
136741016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
136841016Sdfr    int stat[3];
136941016Sdfr    int command_byte;
137041016Sdfr    int s;
137141016Sdfr
137241016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
137341016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
137441016Sdfr	return (EIO);
137541016Sdfr
137641016Sdfr    /* save the current controller command byte */
137741016Sdfr    s = spltty();
137841016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
137941016Sdfr    if (command_byte == -1) {
138041016Sdfr        kbdc_lock(sc->kbdc, FALSE);
138141016Sdfr	splx(s);
138241016Sdfr	return (EIO);
138341016Sdfr    }
138441016Sdfr
138541016Sdfr    /* disable the aux interrupt and temporalily disable the keyboard */
138641016Sdfr    if (!set_controller_command_byte(sc->kbdc,
138741016Sdfr	    kbdc_get_device_mask(sc->kbdc),
138841016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
138941016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
139041016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
139158230Syokota	    unit);
139241016Sdfr	/* CONTROLLER ERROR;
139341016Sdfr	 * NOTE: we shall force our way through. Because the only
139441016Sdfr	 * ill effect we shall see is that we may not be able
139541016Sdfr	 * to read ACK from the mouse, and it doesn't matter much
139641016Sdfr	 * so long as the mouse will accept the DISABLE command.
139741016Sdfr	 */
139841016Sdfr    }
139941016Sdfr    splx(s);
140041016Sdfr
140158230Syokota    /* stop the watchdog timer */
140284880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
140358230Syokota    callout_handle_init(&sc->callout);
140458230Syokota
140541016Sdfr    /* remove anything left in the output buffer */
140641016Sdfr    empty_aux_buffer(sc->kbdc, 10);
140741016Sdfr
140841016Sdfr    /* disable the aux device, port and interrupt */
140941016Sdfr    if (sc->state & PSM_VALID) {
141041016Sdfr        if (!disable_aux_dev(sc->kbdc)) {
141141016Sdfr	    /* MOUSE ERROR;
141241016Sdfr	     * NOTE: we don't return error and continue, pretending
141341016Sdfr	     * we have successfully disabled the device. It's OK because
141441016Sdfr	     * the interrupt routine will discard any data from the mouse
141541016Sdfr	     * hereafter.
141641016Sdfr	     */
141741016Sdfr	    log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
141858230Syokota		unit);
141941016Sdfr        }
142041016Sdfr
142141016Sdfr        if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
142241016Sdfr            log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
142358230Syokota		unit);
142441016Sdfr    }
142541016Sdfr
142641016Sdfr    if (!set_controller_command_byte(sc->kbdc,
142741016Sdfr	    kbdc_get_device_mask(sc->kbdc),
142841016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
142941016Sdfr	        | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
143041016Sdfr	/* CONTROLLER ERROR;
143141016Sdfr	 * we shall ignore this error; see the above comment.
143241016Sdfr	 */
143341016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
143458230Syokota	    unit);
143541016Sdfr    }
143641016Sdfr
143741016Sdfr    /* remove anything left in the output buffer */
143841016Sdfr    empty_aux_buffer(sc->kbdc, 10);
143941016Sdfr
144041016Sdfr    /* close is almost always successful */
144141016Sdfr    sc->state &= ~PSM_OPEN;
144241016Sdfr    kbdc_lock(sc->kbdc, FALSE);
144341016Sdfr    device_unbusy(devclass_get_device(psm_devclass, unit));
144441016Sdfr    return (0);
144541016Sdfr}
144641016Sdfr
144741016Sdfrstatic int
144841016Sdfrtame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
144941016Sdfr{
145041016Sdfr    static unsigned char butmapps2[8] = {
145141016Sdfr        0,
145241016Sdfr        MOUSE_PS2_BUTTON1DOWN,
145341016Sdfr        MOUSE_PS2_BUTTON2DOWN,
145441016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
145541016Sdfr        MOUSE_PS2_BUTTON3DOWN,
145641016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
145741016Sdfr        MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
145841016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
145941016Sdfr    };
146041016Sdfr    static unsigned char butmapmsc[8] = {
146141016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
146241016Sdfr        MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
146341016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
146441016Sdfr        MOUSE_MSC_BUTTON3UP,
146541016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
146641016Sdfr        MOUSE_MSC_BUTTON2UP,
146741016Sdfr        MOUSE_MSC_BUTTON1UP,
146841016Sdfr        0,
146941016Sdfr    };
147041016Sdfr    int mapped;
147141016Sdfr    int i;
147241016Sdfr
147341016Sdfr    if (sc->mode.level == PSM_LEVEL_BASE) {
147441016Sdfr        mapped = status->button & ~MOUSE_BUTTON4DOWN;
147541016Sdfr        if (status->button & MOUSE_BUTTON4DOWN)
147641016Sdfr	    mapped |= MOUSE_BUTTON1DOWN;
147741016Sdfr        status->button = mapped;
147841016Sdfr        buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1479109269Smdodd        i = imax(imin(status->dx, 255), -256);
148041016Sdfr	if (i < 0)
148141016Sdfr	    buf[0] |= MOUSE_PS2_XNEG;
148241016Sdfr        buf[1] = i;
1483109269Smdodd        i = imax(imin(status->dy, 255), -256);
148441016Sdfr	if (i < 0)
148541016Sdfr	    buf[0] |= MOUSE_PS2_YNEG;
148641016Sdfr        buf[2] = i;
148741016Sdfr	return MOUSE_PS2_PACKETSIZE;
148841016Sdfr    } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
148941016Sdfr        buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1490109269Smdodd        i = imax(imin(status->dx, 255), -256);
149141016Sdfr        buf[1] = i >> 1;
149241016Sdfr        buf[3] = i - buf[1];
1493109269Smdodd        i = imax(imin(status->dy, 255), -256);
149441016Sdfr        buf[2] = i >> 1;
149541016Sdfr        buf[4] = i - buf[2];
1496109269Smdodd        i = imax(imin(status->dz, 127), -128);
149741016Sdfr        buf[5] = (i >> 1) & 0x7f;
149841016Sdfr        buf[6] = (i - (i >> 1)) & 0x7f;
149941016Sdfr        buf[7] = (~status->button >> 3) & 0x7f;
150041016Sdfr	return MOUSE_SYS_PACKETSIZE;
150141016Sdfr    }
150241016Sdfr    return sc->inputbytes;;
150341016Sdfr}
150441016Sdfr
150541016Sdfrstatic int
150641016Sdfrpsmread(dev_t dev, struct uio *uio, int flag)
150741016Sdfr{
150841016Sdfr    register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
150941016Sdfr    unsigned char buf[PSM_SMALLBUFSIZE];
151041016Sdfr    int error = 0;
151141016Sdfr    int s;
151241016Sdfr    int l;
151341016Sdfr
151441016Sdfr    if ((sc->state & PSM_VALID) == 0)
151541016Sdfr	return EIO;
151641016Sdfr
151741016Sdfr    /* block until mouse activity occured */
151841016Sdfr    s = spltty();
151941016Sdfr    while (sc->queue.count <= 0) {
152041016Sdfr        if (PSM_NBLOCKIO(dev)) {
152141016Sdfr            splx(s);
152241016Sdfr            return EWOULDBLOCK;
152341016Sdfr        }
152441016Sdfr        sc->state |= PSM_ASLP;
1525111748Sdes        error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
152641016Sdfr        sc->state &= ~PSM_ASLP;
152741016Sdfr        if (error) {
152841016Sdfr            splx(s);
152941016Sdfr            return error;
153041016Sdfr        } else if ((sc->state & PSM_VALID) == 0) {
153141016Sdfr            /* the device disappeared! */
153241016Sdfr            splx(s);
153341016Sdfr            return EIO;
153441016Sdfr	}
153541016Sdfr    }
153641016Sdfr    splx(s);
153741016Sdfr
153841016Sdfr    /* copy data to the user land */
153941016Sdfr    while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
154041016Sdfr        s = spltty();
1541109269Smdodd	l = imin(sc->queue.count, uio->uio_resid);
154241016Sdfr	if (l > sizeof(buf))
154341016Sdfr	    l = sizeof(buf);
154441016Sdfr	if (l > sizeof(sc->queue.buf) - sc->queue.head) {
154541016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
154641016Sdfr		sizeof(sc->queue.buf) - sc->queue.head);
154741016Sdfr	    bcopy(&sc->queue.buf[0],
154841016Sdfr		&buf[sizeof(sc->queue.buf) - sc->queue.head],
154941016Sdfr		l - (sizeof(sc->queue.buf) - sc->queue.head));
155041016Sdfr	} else {
155141016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
155241016Sdfr	}
155341016Sdfr	sc->queue.count -= l;
155441016Sdfr	sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
155541016Sdfr        splx(s);
155641016Sdfr        error = uiomove(buf, l, uio);
155741016Sdfr        if (error)
155841016Sdfr	    break;
155941016Sdfr    }
156041016Sdfr
156141016Sdfr    return error;
156241016Sdfr}
156341016Sdfr
156441016Sdfrstatic int
156541016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
156641016Sdfr{
156741016Sdfr    int s;
156841016Sdfr
156941016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
157041016Sdfr	return EIO;
157141016Sdfr
157241016Sdfr    s = spltty();
157341016Sdfr    *c = get_controller_command_byte(sc->kbdc);
157441016Sdfr    if ((*c == -1)
157541016Sdfr	|| !set_controller_command_byte(sc->kbdc,
157641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
157741016Sdfr            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
157841016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
157941016Sdfr        /* this is CONTROLLER ERROR */
158041016Sdfr	splx(s);
158141016Sdfr        kbdc_lock(sc->kbdc, FALSE);
158241016Sdfr	return EIO;
158341016Sdfr    }
158441016Sdfr
158541016Sdfr    /*
158641016Sdfr     * The device may be in the middle of status data transmission.
158741016Sdfr     * The transmission will be interrupted, thus, incomplete status
158841016Sdfr     * data must be discarded. Although the aux interrupt is disabled
158941016Sdfr     * at the keyboard controller level, at most one aux interrupt
159041016Sdfr     * may have already been pending and a data byte is in the
159141016Sdfr     * output buffer; throw it away. Note that the second argument
159241016Sdfr     * to `empty_aux_buffer()' is zero, so that the call will just
159341016Sdfr     * flush the internal queue.
159441016Sdfr     * `psmintr()' will be invoked after `splx()' if an interrupt is
159541016Sdfr     * pending; it will see no data and returns immediately.
159641016Sdfr     */
159741016Sdfr    empty_aux_buffer(sc->kbdc, 0);	/* flush the queue */
159841016Sdfr    read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
159941016Sdfr    sc->inputbytes = 0;
160041016Sdfr    splx(s);
160141016Sdfr
160241016Sdfr    return 0;
160341016Sdfr}
160441016Sdfr
160541016Sdfrstatic int
160641016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
160741016Sdfr{
160841016Sdfr    int error = 0;
160941016Sdfr
161041016Sdfr    /*
161141016Sdfr     * We may have seen a part of status data during `set_mouse_XXX()'.
161241016Sdfr     * they have been queued; flush it.
161341016Sdfr     */
161441016Sdfr    empty_aux_buffer(sc->kbdc, 0);
161541016Sdfr
161641016Sdfr    /* restore ports and interrupt */
161741016Sdfr    if (!set_controller_command_byte(sc->kbdc,
161841016Sdfr            kbdc_get_device_mask(sc->kbdc),
161941016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
162041016Sdfr        /* CONTROLLER ERROR; this is serious, we may have
162141016Sdfr         * been left with the inaccessible keyboard and
162241016Sdfr         * the disabled mouse interrupt.
162341016Sdfr         */
162441016Sdfr        error = EIO;
162541016Sdfr    }
162641016Sdfr
162741016Sdfr    kbdc_lock(sc->kbdc, FALSE);
162841016Sdfr    return error;
162941016Sdfr}
163041016Sdfr
163141016Sdfrstatic int
163283366Sjulianpsmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
163341016Sdfr{
163441016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
163541016Sdfr    mousemode_t mode;
163641016Sdfr    mousestatus_t status;
163741016Sdfr#if (defined(MOUSE_GETVARS))
163841016Sdfr    mousevar_t *var;
163941016Sdfr#endif
164041016Sdfr    mousedata_t *data;
164141016Sdfr    int stat[3];
164241016Sdfr    int command_byte;
164341016Sdfr    int error = 0;
164441016Sdfr    int s;
164541016Sdfr
164641016Sdfr    /* Perform IOCTL command */
164741016Sdfr    switch (cmd) {
164841016Sdfr
164941016Sdfr    case OLD_MOUSE_GETHWINFO:
165041016Sdfr	s = spltty();
165141016Sdfr        ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
165241016Sdfr        ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
165341016Sdfr        ((old_mousehw_t *)addr)->type = sc->hw.type;
165448778Syokota        ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
165541016Sdfr	splx(s);
165641016Sdfr        break;
165741016Sdfr
165841016Sdfr    case MOUSE_GETHWINFO:
165941016Sdfr	s = spltty();
166041016Sdfr        *(mousehw_t *)addr = sc->hw;
166141016Sdfr	if (sc->mode.level == PSM_LEVEL_BASE)
166241016Sdfr	    ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
166341016Sdfr	splx(s);
166441016Sdfr        break;
166541016Sdfr
166641016Sdfr    case OLD_MOUSE_GETMODE:
166741016Sdfr	s = spltty();
166841016Sdfr	switch (sc->mode.level) {
166941016Sdfr	case PSM_LEVEL_BASE:
167041016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
167141016Sdfr	    break;
167241016Sdfr	case PSM_LEVEL_STANDARD:
167341016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
167441016Sdfr	    break;
167541016Sdfr	case PSM_LEVEL_NATIVE:
167641016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
167741016Sdfr	    break;
167841016Sdfr	}
167941016Sdfr        ((old_mousemode_t *)addr)->rate = sc->mode.rate;
168041016Sdfr        ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
168141016Sdfr        ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
168241016Sdfr	splx(s);
168341016Sdfr        break;
168441016Sdfr
168541016Sdfr    case MOUSE_GETMODE:
168641016Sdfr	s = spltty();
168741016Sdfr        *(mousemode_t *)addr = sc->mode;
168841016Sdfr        ((mousemode_t *)addr)->resolution =
168941016Sdfr	    MOUSE_RES_LOW - sc->mode.resolution;
169041016Sdfr	switch (sc->mode.level) {
169141016Sdfr	case PSM_LEVEL_BASE:
169241016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
169341016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
169441016Sdfr	    break;
169541016Sdfr	case PSM_LEVEL_STANDARD:
169641016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
169741016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
169841016Sdfr	    ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
169941016Sdfr	    ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
170041016Sdfr	    break;
170141016Sdfr	case PSM_LEVEL_NATIVE:
170241016Sdfr	    /* FIXME: this isn't quite correct... XXX */
170341016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
170441016Sdfr	    break;
170541016Sdfr	}
170641016Sdfr	splx(s);
170741016Sdfr        break;
170841016Sdfr
170941016Sdfr    case OLD_MOUSE_SETMODE:
171041016Sdfr    case MOUSE_SETMODE:
171141016Sdfr	if (cmd == OLD_MOUSE_SETMODE) {
171241016Sdfr	    mode.rate = ((old_mousemode_t *)addr)->rate;
171341016Sdfr	    /*
171441016Sdfr	     * resolution  old I/F   new I/F
171541016Sdfr	     * default        0         0
171641016Sdfr	     * low            1        -2
171741016Sdfr	     * medium low     2        -3
171841016Sdfr	     * medium high    3        -4
171941016Sdfr	     * high           4        -5
172041016Sdfr	     */
172141016Sdfr	    if (((old_mousemode_t *)addr)->resolution > 0)
172241016Sdfr	        mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
172341016Sdfr	    mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
172441016Sdfr	    mode.level = -1;
172541016Sdfr	} else {
172641016Sdfr	    mode = *(mousemode_t *)addr;
172741016Sdfr	}
172841016Sdfr
172941016Sdfr	/* adjust and validate parameters. */
173041016Sdfr	if (mode.rate > UCHAR_MAX)
173141016Sdfr	    return EINVAL;
173241016Sdfr        if (mode.rate == 0)
173341016Sdfr            mode.rate = sc->dflt_mode.rate;
173441016Sdfr	else if (mode.rate == -1)
173541016Sdfr	    /* don't change the current setting */
173641016Sdfr	    ;
173741016Sdfr	else if (mode.rate < 0)
173841016Sdfr	    return EINVAL;
173941016Sdfr	if (mode.resolution >= UCHAR_MAX)
174041016Sdfr	    return EINVAL;
174141016Sdfr	if (mode.resolution >= 200)
174241016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
174341016Sdfr	else if (mode.resolution >= 100)
174441016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
174541016Sdfr	else if (mode.resolution >= 50)
174641016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
174741016Sdfr	else if (mode.resolution > 0)
174841016Sdfr	    mode.resolution = MOUSE_RES_LOW;
174941016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
175041016Sdfr            mode.resolution = sc->dflt_mode.resolution;
175141016Sdfr        else if (mode.resolution == -1)
175241016Sdfr	    /* don't change the current setting */
175341016Sdfr	    ;
175441016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
175541016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
175641016Sdfr	if (mode.level == -1)
175741016Sdfr	    /* don't change the current setting */
175841016Sdfr	    mode.level = sc->mode.level;
175941016Sdfr	else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
176041016Sdfr	    return EINVAL;
176141016Sdfr        if (mode.accelfactor == -1)
176241016Sdfr	    /* don't change the current setting */
176341016Sdfr	    mode.accelfactor = sc->mode.accelfactor;
176441016Sdfr        else if (mode.accelfactor < 0)
176541016Sdfr	    return EINVAL;
176641016Sdfr
176741016Sdfr	/* don't allow anybody to poll the keyboard controller */
176841016Sdfr	error = block_mouse_data(sc, &command_byte);
176941016Sdfr	if (error)
177041016Sdfr            return error;
177141016Sdfr
177241016Sdfr        /* set mouse parameters */
177341016Sdfr	if (mode.rate > 0)
177441016Sdfr	    mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
177541016Sdfr	if (mode.resolution >= 0)
177641016Sdfr	    mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
177741016Sdfr	set_mouse_scaling(sc->kbdc, 1);
177841016Sdfr	get_mouse_status(sc->kbdc, stat, 0, 3);
177941016Sdfr
178041016Sdfr        s = spltty();
178141016Sdfr    	sc->mode.rate = mode.rate;
178241016Sdfr    	sc->mode.resolution = mode.resolution;
178341016Sdfr    	sc->mode.accelfactor = mode.accelfactor;
178441016Sdfr    	sc->mode.level = mode.level;
178541016Sdfr        splx(s);
178641016Sdfr
178741016Sdfr	unblock_mouse_data(sc, command_byte);
178841016Sdfr        break;
178941016Sdfr
179041016Sdfr    case MOUSE_GETLEVEL:
179141016Sdfr	*(int *)addr = sc->mode.level;
179241016Sdfr        break;
179341016Sdfr
179441016Sdfr    case MOUSE_SETLEVEL:
179541016Sdfr	if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
179641016Sdfr	    return EINVAL;
179741016Sdfr	sc->mode.level = *(int *)addr;
179841016Sdfr        break;
179941016Sdfr
180041016Sdfr    case MOUSE_GETSTATUS:
180141016Sdfr        s = spltty();
180241016Sdfr	status = sc->status;
180341016Sdfr	sc->status.flags = 0;
180441016Sdfr	sc->status.obutton = sc->status.button;
180541016Sdfr	sc->status.button = 0;
180641016Sdfr	sc->status.dx = 0;
180741016Sdfr	sc->status.dy = 0;
180841016Sdfr	sc->status.dz = 0;
180941016Sdfr        splx(s);
181041016Sdfr        *(mousestatus_t *)addr = status;
181141016Sdfr        break;
181241016Sdfr
181341016Sdfr#if (defined(MOUSE_GETVARS))
181441016Sdfr    case MOUSE_GETVARS:
181541016Sdfr	var = (mousevar_t *)addr;
181641016Sdfr	bzero(var, sizeof(*var));
181741016Sdfr	s = spltty();
181841016Sdfr        var->var[0] = MOUSE_VARS_PS2_SIG;
181941016Sdfr        var->var[1] = sc->config;
182041016Sdfr        var->var[2] = sc->flags;
182141016Sdfr	splx(s);
182241016Sdfr        break;
182341016Sdfr
182441016Sdfr    case MOUSE_SETVARS:
182541016Sdfr	return ENODEV;
182641016Sdfr#endif /* MOUSE_GETVARS */
182741016Sdfr
182841016Sdfr    case MOUSE_READSTATE:
182941016Sdfr    case MOUSE_READDATA:
183041016Sdfr	data = (mousedata_t *)addr;
183141016Sdfr	if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
183241016Sdfr	    return EINVAL;
183341016Sdfr
183441016Sdfr	error = block_mouse_data(sc, &command_byte);
183541016Sdfr	if (error)
183641016Sdfr            return error;
183741016Sdfr        if ((data->len = get_mouse_status(sc->kbdc, data->buf,
183841016Sdfr		(cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
183941016Sdfr            error = EIO;
184041016Sdfr	unblock_mouse_data(sc, command_byte);
184141016Sdfr	break;
184241016Sdfr
184341016Sdfr#if (defined(MOUSE_SETRESOLUTION))
184441016Sdfr    case MOUSE_SETRESOLUTION:
184541016Sdfr	mode.resolution = *(int *)addr;
184641016Sdfr	if (mode.resolution >= UCHAR_MAX)
184741016Sdfr	    return EINVAL;
184841016Sdfr	else if (mode.resolution >= 200)
184941016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
185041016Sdfr	else if (mode.resolution >= 100)
185141016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
185241016Sdfr	else if (mode.resolution >= 50)
185341016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
185441016Sdfr	else if (mode.resolution > 0)
185541016Sdfr	    mode.resolution = MOUSE_RES_LOW;
185641016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
185741016Sdfr            mode.resolution = sc->dflt_mode.resolution;
185841016Sdfr        else if (mode.resolution == -1)
185941016Sdfr	    mode.resolution = sc->mode.resolution;
186041016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
186141016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
186241016Sdfr
186341016Sdfr	error = block_mouse_data(sc, &command_byte);
186441016Sdfr	if (error)
186541016Sdfr            return error;
186641016Sdfr        sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
186741016Sdfr	if (sc->mode.resolution != mode.resolution)
186841016Sdfr	    error = EIO;
186941016Sdfr	unblock_mouse_data(sc, command_byte);
187041016Sdfr        break;
187141016Sdfr#endif /* MOUSE_SETRESOLUTION */
187241016Sdfr
187341016Sdfr#if (defined(MOUSE_SETRATE))
187441016Sdfr    case MOUSE_SETRATE:
187541016Sdfr	mode.rate = *(int *)addr;
187641016Sdfr	if (mode.rate > UCHAR_MAX)
187741016Sdfr	    return EINVAL;
187841016Sdfr        if (mode.rate == 0)
187941016Sdfr            mode.rate = sc->dflt_mode.rate;
188041016Sdfr	else if (mode.rate < 0)
188141016Sdfr	    mode.rate = sc->mode.rate;
188241016Sdfr
188341016Sdfr	error = block_mouse_data(sc, &command_byte);
188441016Sdfr	if (error)
188541016Sdfr            return error;
188641016Sdfr        sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
188741016Sdfr	if (sc->mode.rate != mode.rate)
188841016Sdfr	    error = EIO;
188941016Sdfr	unblock_mouse_data(sc, command_byte);
189041016Sdfr        break;
189141016Sdfr#endif /* MOUSE_SETRATE */
189241016Sdfr
189341016Sdfr#if (defined(MOUSE_SETSCALING))
189441016Sdfr    case MOUSE_SETSCALING:
189541016Sdfr	if ((*(int *)addr <= 0) || (*(int *)addr > 2))
189641016Sdfr	    return EINVAL;
189741016Sdfr
189841016Sdfr	error = block_mouse_data(sc, &command_byte);
189941016Sdfr	if (error)
190041016Sdfr            return error;
190141016Sdfr        if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
190241016Sdfr	    error = EIO;
190341016Sdfr	unblock_mouse_data(sc, command_byte);
190441016Sdfr        break;
190541016Sdfr#endif /* MOUSE_SETSCALING */
190641016Sdfr
190741016Sdfr#if (defined(MOUSE_GETHWID))
190841016Sdfr    case MOUSE_GETHWID:
190941016Sdfr	error = block_mouse_data(sc, &command_byte);
191041016Sdfr	if (error)
191141016Sdfr            return error;
191248778Syokota        sc->hw.hwid &= ~0x00ff;
191348778Syokota        sc->hw.hwid |= get_aux_id(sc->kbdc);
191448778Syokota	*(int *)addr = sc->hw.hwid & 0x00ff;
191541016Sdfr	unblock_mouse_data(sc, command_byte);
191641016Sdfr        break;
191741016Sdfr#endif /* MOUSE_GETHWID */
191841016Sdfr
191941016Sdfr    default:
192041016Sdfr	return ENOTTY;
192141016Sdfr    }
192241016Sdfr
192341016Sdfr    return error;
192441016Sdfr}
192541016Sdfr
192641016Sdfrstatic void
192758230Syokotapsmtimeout(void *arg)
192858230Syokota{
192958230Syokota    struct psm_softc *sc;
193065045Syokota    int s;
193158230Syokota
193284880Syokota    sc = (struct psm_softc *)arg;
193365045Syokota    s = spltty();
193463746Syokota    if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
193558230Syokota	if (verbose >= 4)
193684880Syokota	    log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
193758230Syokota	psmintr(sc);
193863746Syokota	kbdc_lock(sc->kbdc, FALSE);
193958230Syokota    }
194058230Syokota    sc->watchdog = TRUE;
194165045Syokota    splx(s);
194284880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
194358230Syokota}
194458230Syokota
194558230Syokotastatic void
194641016Sdfrpsmintr(void *arg)
194741016Sdfr{
194841016Sdfr    /*
194941016Sdfr     * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
195041016Sdfr     * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
195141016Sdfr     */
195241016Sdfr    static int butmap[8] = {
195341016Sdfr        0,
195441016Sdfr	MOUSE_BUTTON1DOWN,
195541016Sdfr	MOUSE_BUTTON3DOWN,
195641016Sdfr	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
195741016Sdfr	MOUSE_BUTTON2DOWN,
195841016Sdfr	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
195941016Sdfr	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
196041016Sdfr        MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
196141016Sdfr    };
196249965Syokota    static int butmap_versapad[8] = {
196349965Syokota	0,
196449965Syokota	MOUSE_BUTTON3DOWN,
196549965Syokota	0,
196649965Syokota	MOUSE_BUTTON3DOWN,
196749965Syokota	MOUSE_BUTTON1DOWN,
196849965Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
196949965Syokota	MOUSE_BUTTON1DOWN,
197049965Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
197149965Syokota    };
197241016Sdfr    register struct psm_softc *sc = arg;
197341016Sdfr    mousestatus_t ms;
197484880Syokota    struct timeval tv;
197541016Sdfr    int x, y, z;
197641016Sdfr    int c;
197741016Sdfr    int l;
197849965Syokota    int x0, y0;
197941016Sdfr
198041016Sdfr    /* read until there is nothing to read */
198141016Sdfr    while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
198241016Sdfr
198341016Sdfr        /* discard the byte if the device is not open */
198441016Sdfr        if ((sc->state & PSM_OPEN) == 0)
198541016Sdfr            continue;
198641016Sdfr
198784880Syokota	getmicrouptime(&tv);
198884880Syokota	if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
198984880Syokota	    log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
199084880Syokota	    sc->inputbytes = 0;
199184880Syokota	    sc->syncerrors = 0;
199284880Syokota	}
199384880Syokota	sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
199484880Syokota	sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
199584880Syokota	timevaladd(&sc->inputtimeout, &tv);
199684880Syokota
199741016Sdfr        sc->ipacket[sc->inputbytes++] = c;
199841016Sdfr        if (sc->inputbytes < sc->mode.packetsize)
199941016Sdfr	    continue;
200041016Sdfr
200141016Sdfr#if 0
200241016Sdfr        log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
200341016Sdfr	    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
200441016Sdfr	    sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
200541016Sdfr#endif
200641016Sdfr
200741016Sdfr	c = sc->ipacket[0];
200841016Sdfr
200963746Syokota	if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
201063746Syokota            log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
201163746Syokota		c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
201284880Syokota	    ++sc->syncerrors;
201384880Syokota	    if (sc->syncerrors < sc->mode.packetsize) {
201484880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
201584880Syokota		--sc->inputbytes;
201684880Syokota		bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
201784880Syokota	    } else if (sc->syncerrors == sc->mode.packetsize) {
201869439Syokota		log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
201984880Syokota		sc->inputbytes = 0;
202069439Syokota		disable_aux_dev(sc->kbdc);
202169439Syokota		enable_aux_dev(sc->kbdc);
202284880Syokota	    } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
202384880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
202484880Syokota		--sc->inputbytes;
202584880Syokota		bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
202684880Syokota	    } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
202784880Syokota		log(LOG_DEBUG, "psmintr: reset the mouse.\n");
202884880Syokota		reinitialize(sc, TRUE);
202969439Syokota	    }
203084880Syokota	    continue;
203163746Syokota	}
203263746Syokota
203349965Syokota	/*
203441016Sdfr	 * A kludge for Kensington device!
203541016Sdfr	 * The MSB of the horizontal count appears to be stored in
203658230Syokota	 * a strange place.
203741016Sdfr	 */
203858230Syokota	if (sc->hw.model == MOUSE_MODEL_THINK)
203949965Syokota	    sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
204041016Sdfr
204141016Sdfr        /* ignore the overflow bits... */
204241016Sdfr        x = (c & MOUSE_PS2_XNEG) ?  sc->ipacket[1] - 256 : sc->ipacket[1];
204341016Sdfr        y = (c & MOUSE_PS2_YNEG) ?  sc->ipacket[2] - 256 : sc->ipacket[2];
204441016Sdfr	z = 0;
204541016Sdfr        ms.obutton = sc->button;		  /* previous button state */
204641016Sdfr        ms.button = butmap[c & MOUSE_PS2_BUTTONS];
204745789Speter	/* `tapping' action */
204845789Speter	if (sc->config & PSM_CONFIG_FORCETAP)
204945789Speter	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
205041016Sdfr
205141016Sdfr	switch (sc->hw.model) {
205241016Sdfr
205358230Syokota	case MOUSE_MODEL_EXPLORER:
205458230Syokota	    /*
205558230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
205658230Syokota	     * byte 1:  oy ox sy sx 1  M  R  L
205758230Syokota	     * byte 2:  x  x  x  x  x  x  x  x
205858230Syokota	     * byte 3:  y  y  y  y  y  y  y  y
205958230Syokota	     * byte 4:  *  *  S2 S1 s  d2 d1 d0
206058230Syokota	     *
206158230Syokota	     * L, M, R, S1, S2: left, middle, right and side buttons
206258230Syokota	     * s: wheel data sign bit
206358230Syokota	     * d2-d0: wheel data
206458230Syokota	     */
206558230Syokota	    z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
206658230Syokota		? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
206758230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
206858230Syokota		? MOUSE_BUTTON4DOWN : 0;
206958230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
207058230Syokota		? MOUSE_BUTTON5DOWN : 0;
207158230Syokota	    break;
207258230Syokota
207341016Sdfr	case MOUSE_MODEL_INTELLI:
207441016Sdfr	case MOUSE_MODEL_NET:
207541016Sdfr	    /* wheel data is in the fourth byte */
207641016Sdfr	    z = (char)sc->ipacket[3];
207758230Syokota	    /* some mice may send 7 when there is no Z movement?! XXX */
207858230Syokota	    if ((z >= 7) || (z <= -7))
207958230Syokota		z = 0;
208058230Syokota	    /* some compatible mice have additional buttons */
208158230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
208258230Syokota		? MOUSE_BUTTON4DOWN : 0;
208358230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
208458230Syokota		? MOUSE_BUTTON5DOWN : 0;
208541016Sdfr	    break;
208641016Sdfr
208741016Sdfr	case MOUSE_MODEL_MOUSEMANPLUS:
208848778Syokota	    /*
208948778Syokota	     * PS2++ protocl packet
209048778Syokota	     *
209148778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
209248778Syokota	     * byte 1:  *  1  p3 p2 1  *  *  *
209348778Syokota	     * byte 2:  c1 c2 p1 p0 d1 d0 1  0
209448778Syokota	     *
209548778Syokota	     * p3-p0: packet type
209648778Syokota	     * c1, c2: c1 & c2 == 1, if p2 == 0
209748778Syokota	     *         c1 & c2 == 0, if p2 == 1
209848778Syokota	     *
209948778Syokota	     * packet type: 0 (device type)
210048778Syokota	     * See comments in enable_mmanplus() below.
210148778Syokota	     *
210248778Syokota	     * packet type: 1 (wheel data)
210348778Syokota	     *
210448778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
210548778Syokota	     * byte 3:  h  *  B5 B4 s  d2 d1 d0
210648778Syokota	     *
210748778Syokota	     * h: 1, if horizontal roller data
210848778Syokota	     *    0, if vertical roller data
210948778Syokota	     * B4, B5: button 4 and 5
211048778Syokota	     * s: sign bit
211148778Syokota	     * d2-d0: roller data
211248778Syokota	     *
211348778Syokota	     * packet type: 2 (reserved)
211448778Syokota	     */
211548778Syokota	    if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
211648778Syokota		    && (abs(x) > 191)
211748778Syokota		    && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
211841016Sdfr		/* the extended data packet encodes button and wheel events */
211948778Syokota		switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
212048778Syokota		case 1:
212148778Syokota		    /* wheel data packet */
212248778Syokota		    x = y = 0;
212348778Syokota		    if (sc->ipacket[2] & 0x80) {
212448778Syokota			/* horizontal roller count - ignore it XXX*/
212548778Syokota		    } else {
212648778Syokota			/* vertical roller count */
212748778Syokota			z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
212848778Syokota			    ? (sc->ipacket[2] & 0x0f) - 16
212948778Syokota			    : (sc->ipacket[2] & 0x0f);
213048778Syokota		    }
213148778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
213248778Syokota			? MOUSE_BUTTON4DOWN : 0;
213348778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
213448778Syokota			? MOUSE_BUTTON5DOWN : 0;
213548778Syokota		    break;
213648778Syokota		case 2:
213758230Syokota		    /* this packet type is reserved by Logitech... */
213858230Syokota		    /*
213958230Syokota		     * IBM ScrollPoint Mouse uses this packet type to
214058230Syokota		     * encode both vertical and horizontal scroll movement.
214158230Syokota		     */
214258230Syokota		    x = y = 0;
214358230Syokota		    /* horizontal count */
214458230Syokota		    if (sc->ipacket[2] & 0x0f)
214558230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
214658230Syokota		    /* vertical count */
214758230Syokota		    if (sc->ipacket[2] & 0xf0)
214858230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
214958230Syokota#if 0
215058230Syokota		    /* vertical count */
215158230Syokota		    z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
215258230Syokota			? ((sc->ipacket[2] >> 4) & 0x0f) - 16
215358230Syokota			: ((sc->ipacket[2] >> 4) & 0x0f);
215458230Syokota		    /* horizontal count */
215558230Syokota		    w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
215658230Syokota			? (sc->ipacket[2] & 0x0f) - 16
215758230Syokota			: (sc->ipacket[2] & 0x0f);
215858230Syokota#endif
215958230Syokota		    break;
216048778Syokota		case 0:
216148778Syokota		    /* device type packet - shouldn't happen */
2162102412Scharnier		    /* FALLTHROUGH */
216348778Syokota		default:
216448778Syokota		    x = y = 0;
216548778Syokota		    ms.button = ms.obutton;
216658230Syokota		    if (bootverbose)
216758230Syokota			log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
216858230Syokota				       "0x%02x 0x%02x 0x%02x\n",
216958230Syokota			    MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
217058230Syokota			    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
217148778Syokota		    break;
217248778Syokota		}
217341016Sdfr	    } else {
217441016Sdfr		/* preserve button states */
217541016Sdfr		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
217641016Sdfr	    }
217741016Sdfr	    break;
217841016Sdfr
217941016Sdfr	case MOUSE_MODEL_GLIDEPOINT:
218041016Sdfr	    /* `tapping' action */
218141016Sdfr	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
218241016Sdfr	    break;
218341016Sdfr
218441016Sdfr	case MOUSE_MODEL_NETSCROLL:
218558230Syokota	    /* three addtional bytes encode buttons and wheel events */
218658230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
218741016Sdfr		? MOUSE_BUTTON4DOWN : 0;
218858230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
218958230Syokota		? MOUSE_BUTTON5DOWN : 0;
219041016Sdfr	    z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
219141016Sdfr		? sc->ipacket[4] - 256 : sc->ipacket[4];
219241016Sdfr	    break;
219341016Sdfr
219441016Sdfr	case MOUSE_MODEL_THINK:
219541016Sdfr	    /* the fourth button state in the first byte */
219641016Sdfr	    ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
219741016Sdfr	    break;
219841016Sdfr
219949965Syokota	case MOUSE_MODEL_VERSAPAD:
220049965Syokota	    /* VersaPad PS/2 absolute mode message format
220149965Syokota	     *
220249965Syokota	     * [packet1]     7   6   5   4   3   2   1   0(LSB)
220349965Syokota	     *  ipacket[0]:  1   1   0   A   1   L   T   R
220449965Syokota	     *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
220549965Syokota	     *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
220649965Syokota	     *  ipacket[3]:  1   1   1   A   1   L   T   R
220749965Syokota	     *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
220849965Syokota	     *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
220949965Syokota	     *
221049965Syokota	     * [note]
221149965Syokota	     *  R: right physical mouse button (1=on)
221249965Syokota	     *  T: touch pad virtual button (1=tapping)
221349965Syokota	     *  L: left physical mouse button (1=on)
221449965Syokota	     *  A: position data is valid (1=valid)
221549965Syokota	     *  H: horizontal data (12bit signed integer. H11 is sign bit.)
221649965Syokota	     *  V: vertical data (12bit signed integer. V11 is sign bit.)
221749965Syokota	     *  P: pressure data
221849965Syokota	     *
221949965Syokota	     * Tapping is mapped to MOUSE_BUTTON4.
222049965Syokota	     */
222149965Syokota	    ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
222249965Syokota	    ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
222349965Syokota	    x = y = 0;
222449965Syokota	    if (c & MOUSE_PS2VERSA_IN_USE) {
222549965Syokota		x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
222649965Syokota		y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
222749965Syokota		if (x0 & 0x800)
222849965Syokota		    x0 -= 0x1000;
222949965Syokota		if (y0 & 0x800)
223049965Syokota		    y0 -= 0x1000;
223149965Syokota		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
223249965Syokota		    x = sc->xold - x0;
223349965Syokota		    y = y0 - sc->yold;
223449965Syokota		    if (x < 0)	/* XXX */
223549965Syokota			x++;
223649965Syokota		    else if (x)
223749965Syokota			x--;
223849965Syokota		    if (y < 0)
223949965Syokota			y++;
224049965Syokota		    else if (y)
224149965Syokota			y--;
224249965Syokota		} else {
224349965Syokota		    sc->flags |= PSM_FLAGS_FINGERDOWN;
224449965Syokota		}
224549965Syokota		sc->xold = x0;
224649965Syokota		sc->yold = y0;
224749965Syokota	    } else {
224849965Syokota		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
224949965Syokota	    }
225049965Syokota	    c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
225149965Syokota		| ((y < 0) ? MOUSE_PS2_YNEG : 0);
225249965Syokota	    break;
225349965Syokota
225458230Syokota	case MOUSE_MODEL_4D:
225558230Syokota	    /*
225658230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
225758230Syokota	     * byte 1:  s2 d2 s1 d1 1  M  R  L
225858230Syokota	     * byte 2:  sx x  x  x  x  x  x  x
225958230Syokota	     * byte 3:  sy y  y  y  y  y  y  y
226058230Syokota	     *
226158230Syokota	     * s1: wheel 1 direction
226258230Syokota	     * d1: wheel 1 data
226358230Syokota	     * s2: wheel 2 direction
226458230Syokota	     * d2: wheel 2 data
226558230Syokota	     */
226658230Syokota	    x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
226758230Syokota	    y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
226858230Syokota	    switch (c & MOUSE_4D_WHEELBITS) {
226958230Syokota	    case 0x10:
227058230Syokota		z = 1;
227158230Syokota		break;
227258230Syokota	    case 0x30:
227358230Syokota		z = -1;
227458230Syokota		break;
227558230Syokota	    case 0x40:	/* 2nd wheel turning right XXX */
227658230Syokota		z = 2;
227758230Syokota		break;
227858230Syokota	    case 0xc0:	/* 2nd wheel turning left XXX */
227958230Syokota		z = -2;
228058230Syokota		break;
228158230Syokota	    }
228258230Syokota	    break;
228358230Syokota
228458230Syokota	case MOUSE_MODEL_4DPLUS:
228558230Syokota	    if ((x < 16 - 256) && (y < 16 - 256)) {
228658230Syokota		/*
228758230Syokota		 *          b7 b6 b5 b4 b3 b2 b1 b0
228858230Syokota		 * byte 1:  0  0  1  1  1  M  R  L
228958230Syokota		 * byte 2:  0  0  0  0  1  0  0  0
229058230Syokota		 * byte 3:  0  0  0  0  S  s  d1 d0
229158230Syokota		 *
229258230Syokota		 * L, M, R, S: left, middle, right and side buttons
229358230Syokota		 * s: wheel data sign bit
229458230Syokota		 * d1-d0: wheel data
229558230Syokota		 */
229658230Syokota		x = y = 0;
229758230Syokota		if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
229858230Syokota		    ms.button |= MOUSE_BUTTON4DOWN;
229958230Syokota		z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
230058230Syokota			? ((sc->ipacket[2] & 0x07) - 8)
230158230Syokota			: (sc->ipacket[2] & 0x07) ;
230258230Syokota	    } else {
230358230Syokota		/* preserve previous button states */
230458230Syokota		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
230558230Syokota	    }
230658230Syokota	    break;
230758230Syokota
230841016Sdfr	case MOUSE_MODEL_GENERIC:
230941016Sdfr	default:
231041016Sdfr	    break;
231141016Sdfr	}
231241016Sdfr
231341016Sdfr        /* scale values */
231441016Sdfr        if (sc->mode.accelfactor >= 1) {
231541016Sdfr            if (x != 0) {
231641016Sdfr                x = x * x / sc->mode.accelfactor;
231741016Sdfr                if (x == 0)
231841016Sdfr                    x = 1;
231941016Sdfr                if (c & MOUSE_PS2_XNEG)
232041016Sdfr                    x = -x;
232141016Sdfr            }
232241016Sdfr            if (y != 0) {
232341016Sdfr                y = y * y / sc->mode.accelfactor;
232441016Sdfr                if (y == 0)
232541016Sdfr                    y = 1;
232641016Sdfr                if (c & MOUSE_PS2_YNEG)
232741016Sdfr                    y = -y;
232841016Sdfr            }
232941016Sdfr        }
233041016Sdfr
233141016Sdfr        ms.dx = x;
233241016Sdfr        ms.dy = y;
233341016Sdfr        ms.dz = z;
233441016Sdfr        ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
233541016Sdfr	    | (ms.obutton ^ ms.button);
233641016Sdfr
233741016Sdfr	if (sc->mode.level < PSM_LEVEL_NATIVE)
233841016Sdfr	    sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
233941016Sdfr
234041016Sdfr        sc->status.flags |= ms.flags;
234141016Sdfr        sc->status.dx += ms.dx;
234241016Sdfr        sc->status.dy += ms.dy;
234341016Sdfr        sc->status.dz += ms.dz;
234441016Sdfr        sc->status.button = ms.button;
234541016Sdfr        sc->button = ms.button;
234641016Sdfr
234758230Syokota	sc->watchdog = FALSE;
234858230Syokota
234941016Sdfr        /* queue data */
235041016Sdfr        if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2351109269Smdodd	    l = imin(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
235241016Sdfr	    bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
235341016Sdfr	    if (sc->inputbytes > l)
235441016Sdfr	        bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
235541016Sdfr            sc->queue.tail =
235641016Sdfr		(sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
235741016Sdfr            sc->queue.count += sc->inputbytes;
235841016Sdfr	}
235941016Sdfr        sc->inputbytes = 0;
236041016Sdfr
236141016Sdfr        if (sc->state & PSM_ASLP) {
236241016Sdfr            sc->state &= ~PSM_ASLP;
2363111748Sdes            wakeup( sc);
236441016Sdfr    	}
236541016Sdfr        selwakeup(&sc->rsel);
236641016Sdfr    }
236741016Sdfr}
236841016Sdfr
236941016Sdfrstatic int
237083366Sjulianpsmpoll(dev_t dev, int events, struct thread *td)
237141016Sdfr{
237241016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
237341016Sdfr    int s;
237441016Sdfr    int revents = 0;
237541016Sdfr
237641016Sdfr    /* Return true if a mouse event available */
237741016Sdfr    s = spltty();
237845789Speter    if (events & (POLLIN | POLLRDNORM)) {
237941016Sdfr	if (sc->queue.count > 0)
238041016Sdfr	    revents |= events & (POLLIN | POLLRDNORM);
238141016Sdfr	else
238283366Sjulian	    selrecord(td, &sc->rsel);
238345789Speter    }
238441016Sdfr    splx(s);
238541016Sdfr
238641016Sdfr    return (revents);
238741016Sdfr}
238841016Sdfr
238941016Sdfr/* vendor/model specific routines */
239041016Sdfr
239141016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
239241016Sdfr{
239341016Sdfr    if (set_mouse_resolution(kbdc, res) != res)
239441016Sdfr        return FALSE;
239541016Sdfr    if (set_mouse_scaling(kbdc, scale)
239641016Sdfr	&& set_mouse_scaling(kbdc, scale)
239741016Sdfr	&& set_mouse_scaling(kbdc, scale)
239841016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3))
239941016Sdfr	return TRUE;
240041016Sdfr    return FALSE;
240141016Sdfr}
240241016Sdfr
240369438Syokotastatic int
240469438Syokotamouse_ext_command(KBDC kbdc, int command)
240569438Syokota{
240669438Syokota    int c;
240769438Syokota
240869438Syokota    c = (command >> 6) & 0x03;
240969438Syokota    if (set_mouse_resolution(kbdc, c) != c)
241069438Syokota	return FALSE;
241169438Syokota    c = (command >> 4) & 0x03;
241269438Syokota    if (set_mouse_resolution(kbdc, c) != c)
241369438Syokota	return FALSE;
241469438Syokota    c = (command >> 2) & 0x03;
241569438Syokota    if (set_mouse_resolution(kbdc, c) != c)
241669438Syokota	return FALSE;
241769438Syokota    c = (command >> 0) & 0x03;
241869438Syokota    if (set_mouse_resolution(kbdc, c) != c)
241969438Syokota	return FALSE;
242069438Syokota    return TRUE;
242169438Syokota}
242269438Syokota
242341016Sdfr#if notyet
242441016Sdfr/* Logitech MouseMan Cordless II */
242541016Sdfrstatic int
242641016Sdfrenable_lcordless(struct psm_softc *sc)
242741016Sdfr{
242841016Sdfr    int status[3];
242941016Sdfr    int ch;
243041016Sdfr
243141016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
243241016Sdfr        return FALSE;
243341016Sdfr    if (status[1] == PSMD_RES_HIGH)
243441016Sdfr	return FALSE;
243541016Sdfr    ch = (status[0] & 0x07) - 1;	/* channel # */
243641016Sdfr    if ((ch <= 0) || (ch > 4))
243741016Sdfr	return FALSE;
243841016Sdfr    /*
243941016Sdfr     * status[1]: always one?
244041016Sdfr     * status[2]: battery status? (0-100)
244141016Sdfr     */
244241016Sdfr    return TRUE;
244341016Sdfr}
244441016Sdfr#endif /* notyet */
244541016Sdfr
244658230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
244741016Sdfrstatic int
244841016Sdfrenable_groller(struct psm_softc *sc)
244941016Sdfr{
245041016Sdfr    int status[3];
245141016Sdfr
245241016Sdfr    /*
245341016Sdfr     * The special sequence to enable the fourth button and the
245441016Sdfr     * roller. Immediately after this sequence check status bytes.
245541016Sdfr     * if the mouse is NetScroll, the second and the third bytes are
245641016Sdfr     * '3' and 'D'.
245741016Sdfr     */
245841016Sdfr
245941016Sdfr    /*
246041016Sdfr     * If the mouse is an ordinary PS/2 mouse, the status bytes should
246141016Sdfr     * look like the following.
246241016Sdfr     *
246341016Sdfr     * byte 1 bit 7 always 0
246441016Sdfr     *        bit 6 stream mode (0)
246541016Sdfr     *        bit 5 disabled (0)
246641016Sdfr     *        bit 4 1:1 scaling (0)
246741016Sdfr     *        bit 3 always 0
246841016Sdfr     *        bit 0-2 button status
246941016Sdfr     * byte 2 resolution (PSMD_RES_HIGH)
247041016Sdfr     * byte 3 report rate (?)
247141016Sdfr     */
247241016Sdfr
247341016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
247441016Sdfr        return FALSE;
247541016Sdfr    if ((status[1] != '3') || (status[2] != 'D'))
247641016Sdfr        return FALSE;
247758230Syokota    /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
247841016Sdfr    sc->hw.buttons = 4;
247941016Sdfr    return TRUE;
248041016Sdfr}
248141016Sdfr
248258230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
248341016Sdfrstatic int
248441016Sdfrenable_gmouse(struct psm_softc *sc)
248541016Sdfr{
248641016Sdfr    int status[3];
248741016Sdfr
248841016Sdfr    /*
248941016Sdfr     * The special sequence to enable the middle, "rubber" button.
249041016Sdfr     * Immediately after this sequence check status bytes.
249141016Sdfr     * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
249241016Sdfr     * the second and the third bytes are '3' and 'U'.
249341016Sdfr     * NOTE: NetMouse reports that it has three buttons although it has
249441016Sdfr     * two buttons and a rubber button. NetMouse Pro and MIE Mouse
249541016Sdfr     * say they have three buttons too and they do have a button on the
249641016Sdfr     * side...
249741016Sdfr     */
249841016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
249941016Sdfr        return FALSE;
250041016Sdfr    if ((status[1] != '3') || (status[2] != 'U'))
250141016Sdfr        return FALSE;
250241016Sdfr    return TRUE;
250341016Sdfr}
250441016Sdfr
250541016Sdfr/* ALPS GlidePoint */
250641016Sdfrstatic int
250741016Sdfrenable_aglide(struct psm_softc *sc)
250841016Sdfr{
250941016Sdfr    int status[3];
251041016Sdfr
251141016Sdfr    /*
251241016Sdfr     * The special sequence to obtain ALPS GlidePoint specific
251341016Sdfr     * information. Immediately after this sequence, status bytes will
251441016Sdfr     * contain something interesting.
251541016Sdfr     * NOTE: ALPS produces several models of GlidePoint. Some of those
251641016Sdfr     * do not respond to this sequence, thus, cannot be detected this way.
251741016Sdfr     */
251850149Syokota    if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
251950149Syokota	return FALSE;
252041016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
252141016Sdfr        return FALSE;
252250149Syokota    if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
252341016Sdfr        return FALSE;
252441016Sdfr    return TRUE;
252541016Sdfr}
252641016Sdfr
252741016Sdfr/* Kensington ThinkingMouse/Trackball */
252841016Sdfrstatic int
252941016Sdfrenable_kmouse(struct psm_softc *sc)
253041016Sdfr{
253141016Sdfr    static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
253241016Sdfr    KBDC kbdc = sc->kbdc;
253341016Sdfr    int status[3];
253441016Sdfr    int id1;
253541016Sdfr    int id2;
253641016Sdfr    int i;
253741016Sdfr
253841016Sdfr    id1 = get_aux_id(kbdc);
253941016Sdfr    if (set_mouse_sampling_rate(kbdc, 10) != 10)
254041016Sdfr	return FALSE;
254141016Sdfr    /*
254241016Sdfr     * The device is now in the native mode? It returns a different
254341016Sdfr     * ID value...
254441016Sdfr     */
254541016Sdfr    id2 = get_aux_id(kbdc);
254641016Sdfr    if ((id1 == id2) || (id2 != 2))
254741016Sdfr	return FALSE;
254841016Sdfr
254941016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
255041016Sdfr        return FALSE;
255141016Sdfr#if PSM_DEBUG >= 2
255241016Sdfr    /* at this point, resolution is LOW, sampling rate is 10/sec */
255341016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
255441016Sdfr        return FALSE;
255541016Sdfr#endif
255641016Sdfr
255741016Sdfr    /*
255841016Sdfr     * The special sequence to enable the third and fourth buttons.
255941016Sdfr     * Otherwise they behave like the first and second buttons.
256041016Sdfr     */
256141016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
256241016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
256341016Sdfr	    return FALSE;
256441016Sdfr    }
256541016Sdfr
256641016Sdfr    /*
256741016Sdfr     * At this point, the device is using default resolution and
256841016Sdfr     * sampling rate for the native mode.
256941016Sdfr     */
257041016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
257141016Sdfr        return FALSE;
257241016Sdfr    if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
257341016Sdfr        return FALSE;
257441016Sdfr
257541016Sdfr    /* the device appears be enabled by this sequence, diable it for now */
257641016Sdfr    disable_aux_dev(kbdc);
257741016Sdfr    empty_aux_buffer(kbdc, 5);
257841016Sdfr
257941016Sdfr    return TRUE;
258041016Sdfr}
258141016Sdfr
258258230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
258341016Sdfrstatic int
258441016Sdfrenable_mmanplus(struct psm_softc *sc)
258541016Sdfr{
258641016Sdfr    KBDC kbdc = sc->kbdc;
258741016Sdfr    int data[3];
258841016Sdfr
258941016Sdfr    /* the special sequence to enable the fourth button and the roller. */
259058230Syokota    /*
259158230Syokota     * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
259258230Syokota     * must be called exactly three times since the last RESET command
259358230Syokota     * before this sequence. XXX
259458230Syokota     */
259569438Syokota    if (!set_mouse_scaling(kbdc, 1))
259669438Syokota	return FALSE;
259769438Syokota    if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
259869438Syokota	return FALSE;
259941016Sdfr    if (get_mouse_status(kbdc, data, 1, 3) < 3)
260041016Sdfr        return FALSE;
260141016Sdfr
260248778Syokota    /*
260348778Syokota     * PS2++ protocl, packet type 0
260441016Sdfr     *
260548778Syokota     *          b7 b6 b5 b4 b3 b2 b1 b0
260648778Syokota     * byte 1:  *  1  p3 p2 1  *  *  *
260748778Syokota     * byte 2:  1  1  p1 p0 m1 m0 1  0
260848778Syokota     * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
260948778Syokota     *
261048778Syokota     * p3-p0: packet type: 0
261158230Syokota     * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
261241016Sdfr     */
261348778Syokota    /* check constant bits */
261448778Syokota    if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
261541016Sdfr        return FALSE;
261648778Syokota    if ((data[1] & 0xc3) != 0xc2)
261748778Syokota        return FALSE;
261848778Syokota    /* check d3-d0 in byte 2 */
261948778Syokota    if (!MOUSE_PS2PLUS_CHECKBITS(data))
262048778Syokota        return FALSE;
262148778Syokota    /* check p3-p0 */
262248778Syokota    if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
262348778Syokota        return FALSE;
262441016Sdfr
262548778Syokota    sc->hw.hwid &= 0x00ff;
262648778Syokota    sc->hw.hwid |= data[2] << 8;	/* save model ID */
262748778Syokota
262841016Sdfr    /*
262941016Sdfr     * MouseMan+ (or FirstMouse+) is now in its native mode, in which
263041016Sdfr     * the wheel and the fourth button events are encoded in the
263141016Sdfr     * special data packet. The mouse may be put in the IntelliMouse mode
263241016Sdfr     * if it is initialized by the IntelliMouse's method.
263341016Sdfr     */
263441016Sdfr    return TRUE;
263541016Sdfr}
263641016Sdfr
263758230Syokota/* MS IntelliMouse Explorer */
263858230Syokotastatic int
263958230Syokotaenable_msexplorer(struct psm_softc *sc)
264058230Syokota{
264158923Syokota    static unsigned char rate0[] = { 200, 100, 80, };
264258923Syokota    static unsigned char rate1[] = { 200, 200, 80, };
264358230Syokota    KBDC kbdc = sc->kbdc;
264458230Syokota    int id;
264558230Syokota    int i;
264658230Syokota
264769438Syokota    /* the special sequence to enable the extra buttons and the roller. */
264869438Syokota    for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
264969438Syokota        if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
265069438Syokota	    return FALSE;
265169438Syokota    }
265269438Syokota    /* the device will give the genuine ID only after the above sequence */
265369438Syokota    id = get_aux_id(kbdc);
265469438Syokota    if (id != PSM_EXPLORER_ID)
265569438Syokota	return FALSE;
265669438Syokota
265769438Syokota    sc->hw.hwid = id;
265869438Syokota    sc->hw.buttons = 5;		/* IntelliMouse Explorer XXX */
265969438Syokota
266058923Syokota    /*
266158923Syokota     * XXX: this is a kludge to fool some KVM switch products
266258923Syokota     * which think they are clever enough to know the 4-byte IntelliMouse
266358923Syokota     * protocol, and assume any other protocols use 3-byte packets.
266458923Syokota     * They don't convey 4-byte data packets from the IntelliMouse Explorer
266558923Syokota     * correctly to the host computer because of this!
266658923Syokota     * The following sequence is actually IntelliMouse's "wake up"
266758923Syokota     * sequence; it will make the KVM think the mouse is IntelliMouse
266858923Syokota     * when it is in fact IntelliMouse Explorer.
266958923Syokota     */
267058923Syokota    for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
267158923Syokota        if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
267269438Syokota	    break;
267358923Syokota    }
267458923Syokota    id = get_aux_id(kbdc);
267558923Syokota
267658230Syokota    return TRUE;
267758230Syokota}
267858230Syokota
267941016Sdfr/* MS IntelliMouse */
268041016Sdfrstatic int
268141016Sdfrenable_msintelli(struct psm_softc *sc)
268241016Sdfr{
268341016Sdfr    /*
268441016Sdfr     * Logitech MouseMan+ and FirstMouse+ will also respond to this
268541016Sdfr     * probe routine and act like IntelliMouse.
268641016Sdfr     */
268741016Sdfr
268841016Sdfr    static unsigned char rate[] = { 200, 100, 80, };
268941016Sdfr    KBDC kbdc = sc->kbdc;
269041016Sdfr    int id;
269141016Sdfr    int i;
269241016Sdfr
269341016Sdfr    /* the special sequence to enable the third button and the roller. */
269441016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
269541016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
269641016Sdfr	    return FALSE;
269741016Sdfr    }
269841016Sdfr    /* the device will give the genuine ID only after the above sequence */
269941016Sdfr    id = get_aux_id(kbdc);
270041016Sdfr    if (id != PSM_INTELLI_ID)
270141016Sdfr	return FALSE;
270241016Sdfr
270341016Sdfr    sc->hw.hwid = id;
270441016Sdfr    sc->hw.buttons = 3;
270541016Sdfr
270641016Sdfr    return TRUE;
270741016Sdfr}
270841016Sdfr
270958230Syokota/* A4 Tech 4D Mouse */
271058230Syokotastatic int
271158230Syokotaenable_4dmouse(struct psm_softc *sc)
271258230Syokota{
271358230Syokota    /*
271458230Syokota     * Newer wheel mice from A4 Tech may use the 4D+ protocol.
271558230Syokota     */
271658230Syokota
271758230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
271858230Syokota    KBDC kbdc = sc->kbdc;
271958230Syokota    int id;
272058230Syokota    int i;
272158230Syokota
272258230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
272358230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
272458230Syokota	    return FALSE;
272558230Syokota    }
272658230Syokota    id = get_aux_id(kbdc);
272758230Syokota    /*
272858923Syokota     * WinEasy 4D, 4 Way Scroll 4D: 6
272958230Syokota     * Cable-Free 4D: 8 (4DPLUS)
273058923Syokota     * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
273158230Syokota     */
273258230Syokota    if (id != PSM_4DMOUSE_ID)
273358230Syokota	return FALSE;
273458230Syokota
273558230Syokota    sc->hw.hwid = id;
273658230Syokota    sc->hw.buttons = 3;		/* XXX some 4D mice have 4? */
273758230Syokota
273858230Syokota    return TRUE;
273958230Syokota}
274058230Syokota
274158230Syokota/* A4 Tech 4D+ Mouse */
274258230Syokotastatic int
274358230Syokotaenable_4dplus(struct psm_softc *sc)
274458230Syokota{
274558230Syokota    /*
274658230Syokota     * Newer wheel mice from A4 Tech seem to use this protocol.
274758230Syokota     * Older models are recognized as either 4D Mouse or IntelliMouse.
274858230Syokota     */
274958230Syokota    KBDC kbdc = sc->kbdc;
275058230Syokota    int id;
275158230Syokota
275258230Syokota    /*
275358230Syokota     * enable_4dmouse() already issued the following ID sequence...
275458230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
275558230Syokota    int i;
275658230Syokota
275758230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
275858230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
275958230Syokota	    return FALSE;
276058230Syokota    }
276158230Syokota    */
276258230Syokota
276358230Syokota    id = get_aux_id(kbdc);
276458230Syokota    if (id != PSM_4DPLUS_ID)
276558230Syokota	return FALSE;
276658230Syokota
276758230Syokota    sc->hw.hwid = id;
276858230Syokota    sc->hw.buttons = 4;		/* XXX */
276958230Syokota
277058230Syokota    return TRUE;
277158230Syokota}
277258230Syokota
277349965Syokota/* Interlink electronics VersaPad */
277449965Syokotastatic int
277549965Syokotaenable_versapad(struct psm_softc *sc)
277649965Syokota{
277749965Syokota    KBDC kbdc = sc->kbdc;
277849965Syokota    int data[3];
277949965Syokota
278049965Syokota    set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
278149965Syokota    set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
278249965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
278349965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
278449965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
278549965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
278649965Syokota    if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
278749965Syokota	return FALSE;
278849965Syokota    if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
278949965Syokota	return FALSE;
279049965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
279149965Syokota
279258230Syokota    sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
279358230Syokota
279449965Syokota    return TRUE;				/* PS/2 absolute mode */
279549965Syokota}
279649965Syokota
279741016Sdfrstatic int
279854629Syokotapsmresume(device_t dev)
279941016Sdfr{
280054629Syokota    struct psm_softc *sc = device_get_softc(dev);
280154629Syokota    int unit = device_get_unit(dev);
280284880Syokota    int err;
280341016Sdfr
280441016Sdfr    if (verbose >= 2)
280554629Syokota        log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
280641016Sdfr
280758230Syokota    if (!(sc->config & PSM_CONFIG_HOOKRESUME))
280858230Syokota	return (0);
280958230Syokota
281084880Syokota    err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
281141016Sdfr
281241016Sdfr    if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
281341016Sdfr	/*
281441016Sdfr	 * Release the blocked process; it must be notified that the device
281541016Sdfr	 * cannot be accessed anymore.
281641016Sdfr	 */
281741016Sdfr        sc->state &= ~PSM_ASLP;
2818111748Sdes        wakeup(sc);
281941016Sdfr    }
282041016Sdfr
282141016Sdfr    if (verbose >= 2)
282254629Syokota        log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
282341016Sdfr
282441016Sdfr    return (err);
282541016Sdfr}
282641016Sdfr
282752997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
282883147Syokota
282983147Syokota/*
283083147Syokota * This sucks up assignments from PNPBIOS and ACPI.
283183147Syokota */
283283147Syokota
283383931Syokota/*
283483931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
283583931Syokota * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
283683931Syokota * can be probed and attached only after the AT keyboard controller is
283783931Syokota * attached, we shall quietly reserve the IRQ resource for later use.
283883931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
283983931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging
284083931Syokota * under the keyboard controller, then probe and attach it.
284183931Syokota */
284283147Syokota
284383147Syokotastatic	devclass_t			psmcpnp_devclass;
284483147Syokota
284583147Syokotastatic	device_probe_t			psmcpnp_probe;
284683147Syokotastatic	device_attach_t			psmcpnp_attach;
284783147Syokota
284883147Syokotastatic device_method_t psmcpnp_methods[] = {
284983147Syokota	DEVMETHOD(device_probe,		psmcpnp_probe),
285083147Syokota	DEVMETHOD(device_attach,	psmcpnp_attach),
285183147Syokota
285283147Syokota	{ 0, 0 }
285383147Syokota};
285483147Syokota
285583147Syokotastatic driver_t psmcpnp_driver = {
285683147Syokota	PSMCPNP_DRIVER_NAME,
285783147Syokota	psmcpnp_methods,
285883147Syokota	1,			/* no softc */
285983147Syokota};
286083147Syokota
286183147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = {
286288188Ssheldonh	{ 0x030fd041, "PS/2 mouse port" },		/* PNP0F03 */
286383147Syokota	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
286483147Syokota	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
286583492Syokota	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
286684407Stakawata	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
2867109679Shsu	{ 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
2868109679Shsu	{ 0x0290d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9002, Vaio */
2869109710Smarcel	{ 0x0390d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9003, Vaio */
2870109679Shsu	{ 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
287183147Syokota	{ 0 }
287283147Syokota};
287383147Syokota
287483147Syokotastatic int
287583147Syokotacreate_a_copy(device_t atkbdc, device_t me)
287683147Syokota{
287783147Syokota	device_t psm;
287883147Syokota	u_long irq;
287983147Syokota
288083931Syokota	/* find the PS/2 mouse device instance under the keyboard controller */
288183931Syokota	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
288283931Syokota				device_get_unit(atkbdc));
288383147Syokota	if (psm == NULL)
288483147Syokota		return ENXIO;
288583931Syokota	if (device_get_state(psm) != DS_NOTPRESENT)
288683931Syokota		return 0;
288783147Syokota
288883931Syokota	/* move our resource to the found device */
288983931Syokota	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
289083147Syokota	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
289183147Syokota
289283147Syokota	/* ...then probe and attach it */
289383147Syokota	return device_probe_and_attach(psm);
289483147Syokota}
289583147Syokota
289683147Syokotastatic int
289783147Syokotapsmcpnp_probe(device_t dev)
289883147Syokota{
289983931Syokota	struct resource *res;
290083931Syokota	u_long irq;
290183931Syokota	int rid;
290283147Syokota
290383147Syokota	if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
290483147Syokota		return ENXIO;
290583147Syokota
290683931Syokota	/*
290783931Syokota	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
290883931Syokota	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
290983931Syokota	 * declares the PS/2 mouse device node without an IRQ resource!
291083931Syokota	 * If this happens, we shall refer to device hints.
291183931Syokota	 * If we still don't find it there, use a hardcoded value... XXX
291283931Syokota	 */
291383931Syokota	rid = 0;
291483931Syokota	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
291583931Syokota	if (irq <= 0) {
291683931Syokota		if (resource_long_value(PSM_DRIVER_NAME,
291783931Syokota					device_get_unit(dev), "irq", &irq) != 0)
291883931Syokota			irq = 12;	/* XXX */
291983931Syokota		device_printf(dev, "irq resource info is missing; "
292083931Syokota			      "assuming irq %ld\n", irq);
292183931Syokota		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
292283931Syokota	}
292383931Syokota	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
292483931Syokota				 RF_SHAREABLE);
292583931Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
292683147Syokota
292783147Syokota	/* keep quiet */
292883147Syokota	if (!bootverbose)
292983147Syokota		device_quiet(dev);
293083492Syokota
293183931Syokota	return ((res == NULL) ? ENXIO : 0);
293283147Syokota}
293383147Syokota
293483147Syokotastatic int
293583147Syokotapsmcpnp_attach(device_t dev)
293683147Syokota{
293783492Syokota	device_t atkbdc;
293883931Syokota	int rid;
293983147Syokota
294083931Syokota	/* find the keyboard controller, which may be on acpi* or isa* bus */
294183931Syokota	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
294283931Syokota				     device_get_unit(dev));
294383931Syokota	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
294483492Syokota		create_a_copy(atkbdc, dev);
294583931Syokota	} else {
294683931Syokota		/*
294783931Syokota		 * If we don't have the AT keyboard controller yet,
294883931Syokota		 * just reserve the IRQ for later use...
294983931Syokota		 * (See psmidentify() above.)
295083931Syokota		 */
295183931Syokota		rid = 0;
295283931Syokota		bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
295383931Syokota				   RF_SHAREABLE);
295483931Syokota	}
295583492Syokota
295683147Syokota	return 0;
295783147Syokota}
295883147Syokota
295983147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
296083147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
2961