psm.c revision 111748
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 111748 2003-03-02 16:54:40Z des $
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 = {
32947625Sphk	/* open */	psmopen,
33047625Sphk	/* close */	psmclose,
33147625Sphk	/* read */	psmread,
33247625Sphk	/* write */	nowrite,
33347625Sphk	/* ioctl */	psmioctl,
33447625Sphk	/* poll */	psmpoll,
33547625Sphk	/* mmap */	nommap,
33647625Sphk	/* strategy */	nostrategy,
33783492Syokota	/* name */	PSM_DRIVER_NAME,
33847625Sphk	/* maj */	CDEV_MAJOR,
33947625Sphk	/* dump */	nodump,
34047625Sphk	/* psize */	nopsize,
34147625Sphk	/* flags */	0,
34241016Sdfr};
34341016Sdfr
34441016Sdfr/* debug message level */
34541016Sdfrstatic int verbose = PSM_DEBUG;
34641016Sdfr
34741016Sdfr/* device I/O routines */
34841016Sdfrstatic int
34941016Sdfrenable_aux_dev(KBDC kbdc)
35041016Sdfr{
35141016Sdfr    int res;
35241016Sdfr
35341016Sdfr    res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
35441016Sdfr    if (verbose >= 2)
35541016Sdfr        log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
35641016Sdfr
35741016Sdfr    return (res == PSM_ACK);
35841016Sdfr}
35941016Sdfr
36041016Sdfrstatic int
36141016Sdfrdisable_aux_dev(KBDC kbdc)
36241016Sdfr{
36341016Sdfr    int res;
36441016Sdfr
36541016Sdfr    res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
36641016Sdfr    if (verbose >= 2)
36741016Sdfr        log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
36841016Sdfr
36941016Sdfr    return (res == PSM_ACK);
37041016Sdfr}
37141016Sdfr
37241016Sdfrstatic int
37341016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
37441016Sdfr{
37541016Sdfr    int cmd;
37641016Sdfr    int res;
37741016Sdfr    int i;
37841016Sdfr
37941016Sdfr    switch (flag) {
38041016Sdfr    case 0:
38141016Sdfr    default:
38241016Sdfr	cmd = PSMC_SEND_DEV_STATUS;
38341016Sdfr	break;
38441016Sdfr    case 1:
38541016Sdfr	cmd = PSMC_SEND_DEV_DATA;
38641016Sdfr	break;
38741016Sdfr    }
38841016Sdfr    empty_aux_buffer(kbdc, 5);
38941016Sdfr    res = send_aux_command(kbdc, cmd);
39041016Sdfr    if (verbose >= 2)
39141016Sdfr        log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
39241016Sdfr	    (flag == 1) ? "DATA" : "STATUS", res);
39341016Sdfr    if (res != PSM_ACK)
39441016Sdfr        return 0;
39541016Sdfr
39641016Sdfr    for (i = 0; i < len; ++i) {
39741016Sdfr        status[i] = read_aux_data(kbdc);
39841016Sdfr	if (status[i] < 0)
39941016Sdfr	    break;
40041016Sdfr    }
40141016Sdfr
40241016Sdfr    if (verbose) {
40341016Sdfr        log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
40441016Sdfr            (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
40541016Sdfr    }
40641016Sdfr
40741016Sdfr    return i;
40841016Sdfr}
40941016Sdfr
41041016Sdfrstatic int
41141016Sdfrget_aux_id(KBDC kbdc)
41241016Sdfr{
41341016Sdfr    int res;
41441016Sdfr    int id;
41541016Sdfr
41641016Sdfr    empty_aux_buffer(kbdc, 5);
41741016Sdfr    res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
41841016Sdfr    if (verbose >= 2)
41941016Sdfr        log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
42041016Sdfr    if (res != PSM_ACK)
42141016Sdfr	return (-1);
42241016Sdfr
42341016Sdfr    /* 10ms delay */
42441016Sdfr    DELAY(10000);
42541016Sdfr
42641016Sdfr    id = read_aux_data(kbdc);
42741016Sdfr    if (verbose >= 2)
42841016Sdfr        log(LOG_DEBUG, "psm: device ID: %04x\n", id);
42941016Sdfr
43041016Sdfr    return id;
43141016Sdfr}
43241016Sdfr
43341016Sdfrstatic int
43441016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
43541016Sdfr{
43641016Sdfr    int res;
43741016Sdfr
43841016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
43941016Sdfr    if (verbose >= 2)
44041016Sdfr        log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
44141016Sdfr
44241016Sdfr    return ((res == PSM_ACK) ? rate : -1);
44341016Sdfr}
44441016Sdfr
44541016Sdfrstatic int
44641016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
44741016Sdfr{
44841016Sdfr    int res;
44941016Sdfr
45041016Sdfr    switch (scale) {
45141016Sdfr    case 1:
45241016Sdfr    default:
45341016Sdfr	scale = PSMC_SET_SCALING11;
45441016Sdfr	break;
45541016Sdfr    case 2:
45641016Sdfr	scale = PSMC_SET_SCALING21;
45741016Sdfr	break;
45841016Sdfr    }
45941016Sdfr    res = send_aux_command(kbdc, scale);
46041016Sdfr    if (verbose >= 2)
46141016Sdfr        log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
46241016Sdfr	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
46341016Sdfr
46441016Sdfr    return (res == PSM_ACK);
46541016Sdfr}
46641016Sdfr
46741016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
46841016Sdfrstatic int
46941016Sdfrset_mouse_resolution(KBDC kbdc, int val)
47041016Sdfr{
47141016Sdfr    int res;
47241016Sdfr
47341016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
47441016Sdfr    if (verbose >= 2)
47541016Sdfr        log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
47641016Sdfr
47741016Sdfr    return ((res == PSM_ACK) ? val : -1);
47841016Sdfr}
47941016Sdfr
48041016Sdfr/*
48141016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
48241016Sdfr * re-enabled by calling `enable_aux_dev()'
48341016Sdfr */
48441016Sdfrstatic int
48541016Sdfrset_mouse_mode(KBDC kbdc)
48641016Sdfr{
48741016Sdfr    int res;
48841016Sdfr
48941016Sdfr    res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
49041016Sdfr    if (verbose >= 2)
49141016Sdfr        log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
49241016Sdfr
49341016Sdfr    return (res == PSM_ACK);
49441016Sdfr}
49541016Sdfr
49641016Sdfrstatic int
49741016Sdfrget_mouse_buttons(KBDC kbdc)
49841016Sdfr{
49941016Sdfr    int c = 2;		/* assume two buttons by default */
50041016Sdfr    int status[3];
50141016Sdfr
50241016Sdfr    /*
50341016Sdfr     * NOTE: a special sequence to obtain Logitech Mouse specific
50441016Sdfr     * information: set resolution to 25 ppi, set scaling to 1:1, set
50541016Sdfr     * scaling to 1:1, set scaling to 1:1. Then the second byte of the
50641016Sdfr     * mouse status bytes is the number of available buttons.
50741016Sdfr     * Some manufactures also support this sequence.
50841016Sdfr     */
50941016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
51041016Sdfr        return c;
51141016Sdfr    if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
51241016Sdfr        && set_mouse_scaling(kbdc, 1)
51341016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
51441016Sdfr        if (status[1] != 0)
51541016Sdfr            return status[1];
51641016Sdfr    }
51741016Sdfr    return c;
51841016Sdfr}
51941016Sdfr
52041016Sdfr/* misc subroutines */
52141016Sdfr/*
52241016Sdfr * Someday, I will get the complete list of valid pointing devices and
52341016Sdfr * their IDs... XXX
52441016Sdfr */
52541016Sdfrstatic int
52641016Sdfris_a_mouse(int id)
52741016Sdfr{
52841016Sdfr#if 0
52941016Sdfr    static int valid_ids[] = {
53041016Sdfr        PSM_MOUSE_ID,		/* mouse */
53141016Sdfr        PSM_BALLPOINT_ID,	/* ballpoint device */
53241016Sdfr        PSM_INTELLI_ID,		/* Intellimouse */
53358230Syokota        PSM_EXPLORER_ID,	/* Intellimouse Explorer */
53441016Sdfr        -1			/* end of table */
53541016Sdfr    };
53641016Sdfr    int i;
53741016Sdfr
53841016Sdfr    for (i = 0; valid_ids[i] >= 0; ++i)
53941016Sdfr        if (valid_ids[i] == id)
54041016Sdfr            return TRUE;
54141016Sdfr    return FALSE;
54241016Sdfr#else
54341016Sdfr    return TRUE;
54441016Sdfr#endif
54541016Sdfr}
54641016Sdfr
54741016Sdfrstatic char *
54841016Sdfrmodel_name(int model)
54941016Sdfr{
55041016Sdfr    static struct {
55141016Sdfr	int model_code;
55241016Sdfr	char *model_name;
55341016Sdfr    } models[] = {
55458230Syokota        { MOUSE_MODEL_NETSCROLL,	"NetScroll" },
55558230Syokota        { MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
55641016Sdfr        { MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
55741016Sdfr        { MOUSE_MODEL_THINK,		"ThinkingMouse" },
55841016Sdfr        { MOUSE_MODEL_INTELLI,		"IntelliMouse" },
55941016Sdfr        { MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
56049965Syokota        { MOUSE_MODEL_VERSAPAD,		"VersaPad" },
56158230Syokota        { MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
56258230Syokota        { MOUSE_MODEL_4D,		"4D Mouse" },
56358230Syokota        { MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
56441016Sdfr        { MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
56541016Sdfr        { MOUSE_MODEL_UNKNOWN,		NULL },
56641016Sdfr    };
56741016Sdfr    int i;
56841016Sdfr
56941016Sdfr    for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
57041016Sdfr	if (models[i].model_code == model)
57141016Sdfr	    return models[i].model_name;
57241016Sdfr    }
57341016Sdfr    return "Unknown";
57441016Sdfr}
57541016Sdfr
57641016Sdfrstatic void
57741016Sdfrrecover_from_error(KBDC kbdc)
57841016Sdfr{
57941016Sdfr    /* discard anything left in the output buffer */
58041016Sdfr    empty_both_buffers(kbdc, 10);
58141016Sdfr
58241016Sdfr#if 0
58341016Sdfr    /*
58441016Sdfr     * NOTE: KBDC_RESET_KBD may not restore the communication between the
58541016Sdfr     * keyboard and the controller.
58641016Sdfr     */
58741016Sdfr    reset_kbd(kbdc);
58841016Sdfr#else
58941016Sdfr    /*
59041016Sdfr     * NOTE: somehow diagnostic and keyboard port test commands bring the
59141016Sdfr     * keyboard back.
59241016Sdfr     */
59341016Sdfr    if (!test_controller(kbdc))
59441016Sdfr        log(LOG_ERR, "psm: keyboard controller failed.\n");
59541016Sdfr    /* if there isn't a keyboard in the system, the following error is OK */
59641016Sdfr    if (test_kbd_port(kbdc) != 0) {
59741016Sdfr	if (verbose)
59841016Sdfr	    log(LOG_ERR, "psm: keyboard port failed.\n");
59941016Sdfr    }
60041016Sdfr#endif
60141016Sdfr}
60241016Sdfr
60341016Sdfrstatic int
60441016Sdfrrestore_controller(KBDC kbdc, int command_byte)
60541016Sdfr{
60641016Sdfr    empty_both_buffers(kbdc, 10);
60741016Sdfr
60841016Sdfr    if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
60941016Sdfr	log(LOG_ERR, "psm: failed to restore the keyboard controller "
61041016Sdfr		     "command byte.\n");
61158230Syokota	empty_both_buffers(kbdc, 10);
61241016Sdfr	return FALSE;
61341016Sdfr    } else {
61458230Syokota	empty_both_buffers(kbdc, 10);
61541016Sdfr	return TRUE;
61641016Sdfr    }
61741016Sdfr}
61841016Sdfr
61941016Sdfr/*
62041016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
62141016Sdfr * and its interrupt must be disabled before calling this routine.
62241016Sdfr * The aux device will be disabled before returning.
62341016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
62441016Sdfr * calling this routine.
62541016Sdfr */
62641016Sdfrstatic int
62784880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode)
62841016Sdfr{
62941016Sdfr    KBDC kbdc = sc->kbdc;
63041016Sdfr    int stat[3];
63141016Sdfr    int i;
63241016Sdfr
63341016Sdfr    switch((i = test_aux_port(kbdc))) {
63441016Sdfr    case 1:	/* ignore this error */
63541016Sdfr    case PSM_ACK:
63641016Sdfr	if (verbose)
63741016Sdfr	    log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
63884880Syokota	        sc->unit, i);
639102412Scharnier	/* FALLTHROUGH */
64041016Sdfr    case 0:	/* no error */
64141016Sdfr    	break;
64241016Sdfr    case -1: 	/* time out */
64341016Sdfr    default: 	/* error */
64441016Sdfr    	recover_from_error(kbdc);
64545789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
64645789Speter	    break;
64741016Sdfr    	log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
64884880Syokota    	    sc->unit, i);
64941016Sdfr    	return FALSE;
65041016Sdfr    }
65141016Sdfr
65245789Speter    if (sc->config & PSM_CONFIG_NORESET) {
65345789Speter	/*
65445789Speter	 * Don't try to reset the pointing device.  It may possibly be
65545789Speter	 * left in the unknown state, though...
65645789Speter	 */
65745789Speter    } else {
65845789Speter	/*
65945789Speter	 * NOTE: some controllers appears to hang the `keyboard' when
66045789Speter	 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
66145789Speter	 */
66245789Speter	if (!reset_aux_dev(kbdc)) {
66345789Speter            recover_from_error(kbdc);
66484880Syokota            log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
66545789Speter            return FALSE;
66645789Speter	}
66741016Sdfr    }
66841016Sdfr
66941016Sdfr    /*
67041016Sdfr     * both the aux port and the aux device is functioning, see
67141016Sdfr     * if the device can be enabled.
67241016Sdfr     */
67341016Sdfr    if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
67484880Syokota        log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
67541016Sdfr        return FALSE;
67641016Sdfr    }
67741016Sdfr    empty_both_buffers(kbdc, 10);	/* remove stray data if any */
67841016Sdfr
67945789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
68045789Speter	i = GENERIC_MOUSE_ENTRY;
68145789Speter    } else {
68245789Speter	/* FIXME: hardware ID, mouse buttons? */
68341016Sdfr
68445789Speter	/* other parameters */
68545789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
68645789Speter	    if ((*vendortype[i].probefunc)(sc)) {
68745789Speter		if (verbose >= 2)
68845789Speter		    log(LOG_ERR, "psm%d: found %s\n",
68984880Syokota			sc->unit, model_name(vendortype[i].model));
69045789Speter		break;
69145789Speter	    }
69241016Sdfr	}
69341016Sdfr    }
69441016Sdfr
69541016Sdfr    sc->hw.model = vendortype[i].model;
69641016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
69741016Sdfr
69841016Sdfr    /* set mouse parameters */
69941016Sdfr    if (mode != (mousemode_t *)NULL) {
70041016Sdfr	if (mode->rate > 0)
70141016Sdfr            mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
70241016Sdfr	if (mode->resolution >= 0)
70341016Sdfr            mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
70441016Sdfr        set_mouse_scaling(kbdc, 1);
70541016Sdfr        set_mouse_mode(kbdc);
70641016Sdfr    }
70741016Sdfr
70841016Sdfr    /* request a data packet and extract sync. bits */
70941016Sdfr    if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
71084880Syokota        log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
71184880Syokota	    sc->unit);
71241016Sdfr        sc->mode.syncmask[0] = 0;
71341016Sdfr    } else {
71441016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
71541016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
71641016Sdfr	empty_aux_buffer(kbdc, 5);
71741016Sdfr    }
71841016Sdfr
71941016Sdfr    /* just check the status of the mouse */
72041016Sdfr    if (get_mouse_status(kbdc, stat, 0, 3) < 3)
72184880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
72284880Syokota	    sc->unit);
72341016Sdfr
72441016Sdfr    return TRUE;
72541016Sdfr}
72641016Sdfr
72741016Sdfrstatic int
72884880Syokotadoopen(struct psm_softc *sc, int command_byte)
72941016Sdfr{
73041016Sdfr    int stat[3];
73141016Sdfr
73241016Sdfr    /* enable the mouse device */
73341016Sdfr    if (!enable_aux_dev(sc->kbdc)) {
73441016Sdfr	/* MOUSE ERROR: failed to enable the mouse because:
73541016Sdfr	 * 1) the mouse is faulty,
73641016Sdfr	 * 2) the mouse has been removed(!?)
73741016Sdfr	 * In the latter case, the keyboard may have hung, and need
73841016Sdfr	 * recovery procedure...
73941016Sdfr	 */
74041016Sdfr	recover_from_error(sc->kbdc);
74141016Sdfr#if 0
74241016Sdfr	/* FIXME: we could reset the mouse here and try to enable
74341016Sdfr	 * it again. But it will take long time and it's not a good
74441016Sdfr	 * idea to disable the keyboard that long...
74541016Sdfr	 */
74684880Syokota	if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
74741016Sdfr	    recover_from_error(sc->kbdc);
74841016Sdfr#else
74941016Sdfr        {
75041016Sdfr#endif
75141016Sdfr            restore_controller(sc->kbdc, command_byte);
75241016Sdfr	    /* mark this device is no longer available */
75341016Sdfr	    sc->state &= ~PSM_VALID;
75441016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
75584880Syokota		sc->unit);
75641016Sdfr	    return (EIO);
75741016Sdfr	}
75841016Sdfr    }
75941016Sdfr
76041016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
76184880Syokota        log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
76241016Sdfr
76341016Sdfr    /* enable the aux port and interrupt */
76441016Sdfr    if (!set_controller_command_byte(sc->kbdc,
76541016Sdfr	    kbdc_get_device_mask(sc->kbdc),
76641016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
76741016Sdfr		| KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
76841016Sdfr	/* CONTROLLER ERROR */
76941016Sdfr	disable_aux_dev(sc->kbdc);
77041016Sdfr        restore_controller(sc->kbdc, command_byte);
77141016Sdfr	log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
77284880Syokota	    sc->unit);
77341016Sdfr	return (EIO);
77441016Sdfr    }
77541016Sdfr
77658230Syokota    /* start the watchdog timer */
77758230Syokota    sc->watchdog = FALSE;
77884880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
77958230Syokota
78041016Sdfr    return (0);
78141016Sdfr}
78241016Sdfr
78384880Syokotastatic int
78484880Syokotareinitialize(struct psm_softc *sc, int doinit)
78584880Syokota{
78684880Syokota    int err;
78784880Syokota    int c;
78884880Syokota    int s;
78984880Syokota
79084880Syokota    /* don't let anybody mess with the aux device */
79184880Syokota    if (!kbdc_lock(sc->kbdc, TRUE))
79284880Syokota	return (EIO);
79384880Syokota    s = spltty();
79484880Syokota
79584880Syokota    /* block our watchdog timer */
79684880Syokota    sc->watchdog = FALSE;
79784880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
79884880Syokota    callout_handle_init(&sc->callout);
79984880Syokota
80084880Syokota    /* save the current controller command byte */
80184880Syokota    empty_both_buffers(sc->kbdc, 10);
80284880Syokota    c = get_controller_command_byte(sc->kbdc);
80384880Syokota    if (verbose >= 2)
80484880Syokota        log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
80584880Syokota	    sc->unit, c);
80684880Syokota
80784880Syokota    /* enable the aux port but disable the aux interrupt and the keyboard */
80884880Syokota    if ((c == -1) || !set_controller_command_byte(sc->kbdc,
80984880Syokota	    kbdc_get_device_mask(sc->kbdc),
81084880Syokota  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
81184880Syokota	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
81284880Syokota        /* CONTROLLER ERROR */
81384880Syokota	splx(s);
81484880Syokota        kbdc_lock(sc->kbdc, FALSE);
81584880Syokota	log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
81684880Syokota	    sc->unit);
81784880Syokota	return (EIO);
81884880Syokota    }
81984880Syokota
82084880Syokota    /* flush any data */
82184880Syokota    if (sc->state & PSM_VALID) {
82284880Syokota	disable_aux_dev(sc->kbdc);	/* this may fail; but never mind... */
82384880Syokota	empty_aux_buffer(sc->kbdc, 10);
82484880Syokota    }
82584880Syokota    sc->inputbytes = 0;
82684880Syokota    sc->syncerrors = 0;
82784880Syokota
82884880Syokota    /* try to detect the aux device; are you still there? */
82984880Syokota    err = 0;
83084880Syokota    if (doinit) {
83184880Syokota	if (doinitialize(sc, &sc->mode)) {
83284880Syokota	    /* yes */
83384880Syokota	    sc->state |= PSM_VALID;
83484880Syokota	} else {
83584880Syokota	    /* the device has gone! */
83684880Syokota	    restore_controller(sc->kbdc, c);
83784880Syokota	    sc->state &= ~PSM_VALID;
83884880Syokota	    log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
83984880Syokota		sc->unit);
84084880Syokota	    err = ENXIO;
84184880Syokota	}
84284880Syokota    }
84384880Syokota    splx(s);
84484880Syokota
84584880Syokota    /* restore the driver state */
84684880Syokota    if ((sc->state & PSM_OPEN) && (err == 0)) {
84784880Syokota        /* enable the aux device and the port again */
84884880Syokota	err = doopen(sc, c);
84984880Syokota	if (err != 0)
85084880Syokota	    log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
85184880Syokota		sc->unit);
85284880Syokota    } else {
85384880Syokota        /* restore the keyboard port and disable the aux port */
85484880Syokota        if (!set_controller_command_byte(sc->kbdc,
85584880Syokota                kbdc_get_device_mask(sc->kbdc),
85684880Syokota                (c & KBD_KBD_CONTROL_BITS)
85784880Syokota                    | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
85884880Syokota            /* CONTROLLER ERROR */
85984880Syokota            log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
86084880Syokota                sc->unit);
86184880Syokota            err = EIO;
86284880Syokota	}
86384880Syokota    }
86484880Syokota
86584880Syokota    kbdc_lock(sc->kbdc, FALSE);
86684880Syokota    return (err);
86784880Syokota}
86884880Syokota
86941016Sdfr/* psm driver entry points */
87041016Sdfr
87183147Syokotastatic void
87283147Syokotapsmidentify(driver_t *driver, device_t parent)
87383147Syokota{
87483931Syokota    device_t psmc;
87583931Syokota    device_t psm;
87683931Syokota    u_long irq;
87783931Syokota    int unit;
87883147Syokota
87983931Syokota    unit = device_get_unit(parent);
88083931Syokota
88183147Syokota    /* always add at least one child */
88283931Syokota    psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
88383931Syokota    if (psm == NULL)
88483931Syokota	return;
88583931Syokota
88683931Syokota    irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
88783931Syokota    if (irq > 0)
88883931Syokota	return;
88983931Syokota
89083931Syokota    /*
89183931Syokota     * If the PS/2 mouse device has already been reported by ACPI or
89283931Syokota     * PnP BIOS, obtain the IRQ resource from it.
89383931Syokota     * (See psmcpnp_attach() below.)
89483931Syokota     */
89583931Syokota    psmc = device_find_child(device_get_parent(parent),
89683931Syokota			     PSMCPNP_DRIVER_NAME, unit);
89783931Syokota    if (psmc == NULL)
89883931Syokota	return;
89983931Syokota    irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
90083931Syokota    if (irq <= 0)
90183931Syokota	return;
90283931Syokota    bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
90383147Syokota}
90483147Syokota
90541016Sdfr#define endprobe(v)	{   if (bootverbose) 				\
90641016Sdfr				--verbose;   				\
90741016Sdfr                            kbdc_set_device_mask(sc->kbdc, mask);	\
90841016Sdfr			    kbdc_lock(sc->kbdc, FALSE);			\
90941016Sdfr			    return (v);	     				\
91041016Sdfr			}
91141016Sdfr
91241016Sdfrstatic int
91341016Sdfrpsmprobe(device_t dev)
91441016Sdfr{
91541016Sdfr    int unit = device_get_unit(dev);
91641016Sdfr    struct psm_softc *sc = device_get_softc(dev);
91741016Sdfr    int stat[3];
91841016Sdfr    int command_byte;
91941016Sdfr    int mask;
92058230Syokota    int rid;
92141016Sdfr    int i;
92241016Sdfr
92341016Sdfr#if 0
92441016Sdfr    kbdc_debug(TRUE);
92541016Sdfr#endif
92658230Syokota
92783147Syokota    /* see if IRQ is available */
92883147Syokota    rid = KBDC_RID_AUX;
92983147Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
93083147Syokota				  RF_SHAREABLE | RF_ACTIVE);
93183147Syokota    if (sc->intr == NULL) {
93283147Syokota	if (bootverbose)
93383147Syokota            device_printf(dev, "unable to allocate IRQ\n");
93483147Syokota        return (ENXIO);
93583147Syokota    }
93683147Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
93758230Syokota
93884880Syokota    sc->unit = unit;
93958230Syokota    sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
94083147Syokota    sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
94158230Syokota    /* XXX: for backward compatibility */
94258230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
94358230Syokota    sc->config |=
94458230Syokota#ifdef PSM_RESETAFTERSUSPEND
94558230Syokota	PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
94658230Syokota#else
94758230Syokota	PSM_CONFIG_HOOKRESUME;
94858230Syokota#endif
94958230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
95041016Sdfr    sc->flags = 0;
95141016Sdfr    if (bootverbose)
95241016Sdfr        ++verbose;
95341016Sdfr
95443105Sdfr    device_set_desc(dev, "PS/2 Mouse");
95543105Sdfr
95641016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE)) {
95741016Sdfr        printf("psm%d: unable to lock the controller.\n", unit);
95841016Sdfr        if (bootverbose)
95941016Sdfr            --verbose;
96041016Sdfr	return (ENXIO);
96141016Sdfr    }
96241016Sdfr
96341016Sdfr    /*
96441016Sdfr     * NOTE: two bits in the command byte controls the operation of the
96541016Sdfr     * aux port (mouse port): the aux port disable bit (bit 5) and the aux
96641016Sdfr     * port interrupt (IRQ 12) enable bit (bit 2).
96741016Sdfr     */
96841016Sdfr
96941016Sdfr    /* discard anything left after the keyboard initialization */
97041016Sdfr    empty_both_buffers(sc->kbdc, 10);
97141016Sdfr
97241016Sdfr    /* save the current command byte; it will be used later */
97341016Sdfr    mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
97441016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
97541016Sdfr    if (verbose)
97641016Sdfr        printf("psm%d: current command byte:%04x\n", unit, command_byte);
97741016Sdfr    if (command_byte == -1) {
97841016Sdfr        /* CONTROLLER ERROR */
97941016Sdfr        printf("psm%d: unable to get the current command byte value.\n",
98041016Sdfr            unit);
98141016Sdfr        endprobe(ENXIO);
98241016Sdfr    }
98341016Sdfr
98441016Sdfr    /*
98541016Sdfr     * disable the keyboard port while probing the aux port, which must be
98641016Sdfr     * enabled during this routine
98741016Sdfr     */
98841016Sdfr    if (!set_controller_command_byte(sc->kbdc,
98941016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
99041016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
99141016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
99241016Sdfr        /*
99341016Sdfr	 * this is CONTROLLER ERROR; I don't know how to recover
99441016Sdfr         * from this error...
99541016Sdfr	 */
99641016Sdfr        restore_controller(sc->kbdc, command_byte);
99741016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
99841016Sdfr        endprobe(ENXIO);
99941016Sdfr    }
100045789Speter    write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
100141016Sdfr
100241016Sdfr    /*
100341016Sdfr     * NOTE: `test_aux_port()' is designed to return with zero if the aux
100441016Sdfr     * port exists and is functioning. However, some controllers appears
100541016Sdfr     * to respond with zero even when the aux port doesn't exist. (It may
100641016Sdfr     * be that this is only the case when the controller DOES have the aux
100741016Sdfr     * port but the port is not wired on the motherboard.) The keyboard
100841016Sdfr     * controllers without the port, such as the original AT, are
100941016Sdfr     * supporsed to return with an error code or simply time out. In any
101041016Sdfr     * case, we have to continue probing the port even when the controller
101141016Sdfr     * passes this test.
101241016Sdfr     *
101341016Sdfr     * XXX: some controllers erroneously return the error code 1 when
101441016Sdfr     * it has the perfectly functional aux port. We have to ignore this
101541016Sdfr     * error code. Even if the controller HAS error with the aux port,
101641016Sdfr     * it will be detected later...
101741016Sdfr     * XXX: another incompatible controller returns PSM_ACK (0xfa)...
101841016Sdfr     */
101941016Sdfr    switch ((i = test_aux_port(sc->kbdc))) {
102041016Sdfr    case 1:	   /* ignore this error */
102141016Sdfr    case PSM_ACK:
102241016Sdfr        if (verbose)
102341016Sdfr	    printf("psm%d: strange result for test aux port (%d).\n",
102441016Sdfr	        unit, i);
1025102412Scharnier	/* FALLTHROUGH */
102641016Sdfr    case 0:        /* no error */
102741016Sdfr        break;
102841016Sdfr    case -1:        /* time out */
102941016Sdfr    default:        /* error */
103041016Sdfr        recover_from_error(sc->kbdc);
103145789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
103245789Speter	    break;
103341016Sdfr        restore_controller(sc->kbdc, command_byte);
103441016Sdfr        if (verbose)
103541016Sdfr            printf("psm%d: the aux port is not functioning (%d).\n",
103641016Sdfr                unit, i);
103741016Sdfr        endprobe(ENXIO);
103841016Sdfr    }
103941016Sdfr
104045789Speter    if (sc->config & PSM_CONFIG_NORESET) {
104145789Speter	/*
104245789Speter	 * Don't try to reset the pointing device.  It may possibly be
104345789Speter	 * left in the unknown state, though...
104445789Speter	 */
104545789Speter    } else {
104645789Speter	/*
104745789Speter	 * NOTE: some controllers appears to hang the `keyboard' when the aux
104845789Speter	 * port doesn't exist and `PSMC_RESET_DEV' is issued.
104945789Speter	 */
105045789Speter	if (!reset_aux_dev(sc->kbdc)) {
105145789Speter            recover_from_error(sc->kbdc);
105245789Speter            restore_controller(sc->kbdc, command_byte);
105345789Speter            if (verbose)
105445789Speter        	printf("psm%d: failed to reset the aux device.\n", unit);
105545789Speter            endprobe(ENXIO);
105645789Speter	}
105741016Sdfr    }
105845789Speter
105941016Sdfr    /*
106041016Sdfr     * both the aux port and the aux device is functioning, see if the
106141016Sdfr     * device can be enabled. NOTE: when enabled, the device will start
106241016Sdfr     * sending data; we shall immediately disable the device once we know
106341016Sdfr     * the device can be enabled.
106441016Sdfr     */
106541016Sdfr    if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
106645789Speter        /* MOUSE ERROR */
106745789Speter	recover_from_error(sc->kbdc);
106845789Speter	restore_controller(sc->kbdc, command_byte);
106945789Speter	if (verbose)
107045789Speter	    printf("psm%d: failed to enable the aux device.\n", unit);
107141016Sdfr        endprobe(ENXIO);
107241016Sdfr    }
107341016Sdfr
107441016Sdfr    /* save the default values after reset */
107541016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
107641016Sdfr	sc->dflt_mode.rate = sc->mode.rate = stat[2];
107741016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
107841016Sdfr    } else {
107941016Sdfr	sc->dflt_mode.rate = sc->mode.rate = -1;
108041016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = -1;
108141016Sdfr    }
108241016Sdfr
108341016Sdfr    /* hardware information */
108441016Sdfr    sc->hw.iftype = MOUSE_IF_PS2;
108541016Sdfr
108641016Sdfr    /* verify the device is a mouse */
108741016Sdfr    sc->hw.hwid = get_aux_id(sc->kbdc);
108841016Sdfr    if (!is_a_mouse(sc->hw.hwid)) {
108941016Sdfr        restore_controller(sc->kbdc, command_byte);
109041016Sdfr        if (verbose)
109141016Sdfr            printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
109241016Sdfr        endprobe(ENXIO);
109341016Sdfr    }
109441016Sdfr    switch (sc->hw.hwid) {
109541016Sdfr    case PSM_BALLPOINT_ID:
109641016Sdfr        sc->hw.type = MOUSE_TRACKBALL;
109741016Sdfr        break;
109841016Sdfr    case PSM_MOUSE_ID:
109941016Sdfr    case PSM_INTELLI_ID:
110058230Syokota    case PSM_EXPLORER_ID:
110158230Syokota    case PSM_4DMOUSE_ID:
110258230Syokota    case PSM_4DPLUS_ID:
110341016Sdfr        sc->hw.type = MOUSE_MOUSE;
110441016Sdfr        break;
110541016Sdfr    default:
110641016Sdfr        sc->hw.type = MOUSE_UNKNOWN;
110741016Sdfr        break;
110841016Sdfr    }
110941016Sdfr
111045789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
111145789Speter	sc->hw.buttons = 2;
111245789Speter	i = GENERIC_MOUSE_ENTRY;
111345789Speter    } else {
111445789Speter	/* # of buttons */
111545789Speter	sc->hw.buttons = get_mouse_buttons(sc->kbdc);
111641016Sdfr
111745789Speter	/* other parameters */
111845789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
111945789Speter	    if ((*vendortype[i].probefunc)(sc)) {
112045789Speter		if (verbose >= 2)
112145789Speter		    printf("psm%d: found %s\n",
112245789Speter			   unit, model_name(vendortype[i].model));
112345789Speter		break;
112445789Speter	    }
112541016Sdfr	}
112641016Sdfr    }
112741016Sdfr
112841016Sdfr    sc->hw.model = vendortype[i].model;
112941016Sdfr
113041016Sdfr    sc->dflt_mode.level = PSM_LEVEL_BASE;
113141016Sdfr    sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
113241016Sdfr    sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
113341016Sdfr    if (sc->config & PSM_CONFIG_NOCHECKSYNC)
113441016Sdfr        sc->dflt_mode.syncmask[0] = 0;
113541016Sdfr    else
113641016Sdfr        sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
113745789Speter    if (sc->config & PSM_CONFIG_FORCETAP)
113845789Speter        sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
113941016Sdfr    sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
114041016Sdfr    sc->mode = sc->dflt_mode;
114141016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
114241016Sdfr
114341016Sdfr    /* set mouse parameters */
114448773Syokota#if 0
114548773Syokota    /*
114648773Syokota     * A version of Logitech FirstMouse+ won't report wheel movement,
114748773Syokota     * if SET_DEFAULTS is sent...  Don't use this command.
114848773Syokota     * This fix was found by Takashi Nishida.
114948773Syokota     */
115041016Sdfr    i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
115141016Sdfr    if (verbose >= 2)
115241016Sdfr	printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
115348773Syokota#endif
115441016Sdfr    if (sc->config & PSM_CONFIG_RESOLUTION) {
115541016Sdfr        sc->mode.resolution
115641016Sdfr	    = set_mouse_resolution(sc->kbdc,
115748773Syokota				   (sc->config & PSM_CONFIG_RESOLUTION) - 1);
115848773Syokota    } else if (sc->mode.resolution >= 0) {
115948773Syokota	sc->mode.resolution
116048773Syokota	    = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
116141016Sdfr    }
116248773Syokota    if (sc->mode.rate > 0) {
116348773Syokota	sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
116448773Syokota    }
116548773Syokota    set_mouse_scaling(sc->kbdc, 1);
116641016Sdfr
116741016Sdfr    /* request a data packet and extract sync. bits */
116841016Sdfr    if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
116941016Sdfr        printf("psm%d: failed to get data.\n", unit);
117041016Sdfr        sc->mode.syncmask[0] = 0;
117141016Sdfr    } else {
117241016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
117341016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
117441016Sdfr	empty_aux_buffer(sc->kbdc, 5);
117541016Sdfr    }
117641016Sdfr
117741016Sdfr    /* just check the status of the mouse */
117841016Sdfr    /*
117941016Sdfr     * NOTE: XXX there are some arcane controller/mouse combinations out
118041016Sdfr     * there, which hung the controller unless there is data transmission
118141016Sdfr     * after ACK from the mouse.
118241016Sdfr     */
118341016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
118441016Sdfr        printf("psm%d: failed to get status.\n", unit);
118541016Sdfr    } else {
118641016Sdfr	/*
118741016Sdfr	 * When in its native mode, some mice operate with different
118841016Sdfr	 * default parameters than in the PS/2 compatible mode.
118941016Sdfr	 */
119041016Sdfr        sc->dflt_mode.rate = sc->mode.rate = stat[2];
119141016Sdfr        sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
119241016Sdfr     }
119341016Sdfr
119441016Sdfr    /* disable the aux port for now... */
119541016Sdfr    if (!set_controller_command_byte(sc->kbdc,
119641016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
119741016Sdfr            (command_byte & KBD_KBD_CONTROL_BITS)
119841016Sdfr                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
119941016Sdfr        /*
120041016Sdfr	 * this is CONTROLLER ERROR; I don't know the proper way to
120141016Sdfr         * recover from this error...
120241016Sdfr	 */
120341016Sdfr        restore_controller(sc->kbdc, command_byte);
120441016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
120541016Sdfr        endprobe(ENXIO);
120641016Sdfr    }
120741016Sdfr
120841016Sdfr    /* done */
120941016Sdfr    kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
121041016Sdfr    kbdc_lock(sc->kbdc, FALSE);
121141016Sdfr    return (0);
121241016Sdfr}
121341016Sdfr
121441016Sdfrstatic int
121541016Sdfrpsmattach(device_t dev)
121641016Sdfr{
121741016Sdfr    int unit = device_get_unit(dev);
121841016Sdfr    struct psm_softc *sc = device_get_softc(dev);
121958230Syokota    int error;
122058230Syokota    int rid;
122141016Sdfr
122241016Sdfr    if (sc == NULL)    /* shouldn't happen */
122341016Sdfr	return (ENXIO);
122441016Sdfr
122541016Sdfr    /* Setup initial state */
122641016Sdfr    sc->state = PSM_VALID;
122758230Syokota    callout_handle_init(&sc->callout);
122841016Sdfr
122958230Syokota    /* Setup our interrupt handler */
123083147Syokota    rid = KBDC_RID_AUX;
123183147Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
123283147Syokota				  RF_SHAREABLE | RF_ACTIVE);
123358230Syokota    if (sc->intr == NULL)
123458230Syokota	return (ENXIO);
123583147Syokota    error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
123658230Syokota    if (error) {
123758230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
123858230Syokota	return (error);
123958230Syokota    }
124058230Syokota
124141016Sdfr    /* Done */
124258230Syokota    sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
124358230Syokota		       "psm%d", unit);
124458230Syokota    sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
124558230Syokota			"bpsm%d", unit);
124641016Sdfr
124741016Sdfr    if (!verbose) {
124841016Sdfr        printf("psm%d: model %s, device ID %d\n",
124948778Syokota	    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
125041016Sdfr    } else {
125148778Syokota        printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
125248778Syokota	    unit, model_name(sc->hw.model),
125348778Syokota	    sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
125441016Sdfr	printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
125541016Sdfr	    unit, sc->config, sc->flags, sc->mode.packetsize);
125641016Sdfr	printf("psm%d: syncmask:%02x, syncbits:%02x\n",
125741016Sdfr	    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
125841016Sdfr    }
125941016Sdfr
126041016Sdfr    if (bootverbose)
126141016Sdfr        --verbose;
126241016Sdfr
126341016Sdfr    return (0);
126441016Sdfr}
126541016Sdfr
126641016Sdfrstatic int
126758230Syokotapsmdetach(device_t dev)
126858230Syokota{
126958230Syokota    struct psm_softc *sc;
127058230Syokota    int rid;
127158230Syokota
127258230Syokota    sc = device_get_softc(dev);
127358230Syokota    if (sc->state & PSM_OPEN)
127458230Syokota	return EBUSY;
127558230Syokota
127683147Syokota    rid = KBDC_RID_AUX;
127783147Syokota    bus_teardown_intr(dev, sc->intr, sc->ih);
127858230Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
127958230Syokota
128058230Syokota    destroy_dev(sc->dev);
128158230Syokota    destroy_dev(sc->bdev);
128258230Syokota
128358230Syokota    return 0;
128458230Syokota}
128558230Syokota
128658230Syokotastatic int
128783366Sjulianpsmopen(dev_t dev, int flag, int fmt, struct thread *td)
128841016Sdfr{
128941016Sdfr    int unit = PSM_UNIT(dev);
129041016Sdfr    struct psm_softc *sc;
129141016Sdfr    int command_byte;
129241016Sdfr    int err;
129341016Sdfr    int s;
129441016Sdfr
129541016Sdfr    /* Get device data */
129641016Sdfr    sc = PSM_SOFTC(unit);
129741016Sdfr    if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
129841016Sdfr	/* the device is no longer valid/functioning */
129941016Sdfr        return (ENXIO);
130041016Sdfr
130141016Sdfr    /* Disallow multiple opens */
130241016Sdfr    if (sc->state & PSM_OPEN)
130341016Sdfr        return (EBUSY);
130441016Sdfr
130541016Sdfr    device_busy(devclass_get_device(psm_devclass, unit));
130641016Sdfr
130741016Sdfr    /* Initialize state */
130841016Sdfr    sc->mode.level = sc->dflt_mode.level;
130941016Sdfr    sc->mode.protocol = sc->dflt_mode.protocol;
131058230Syokota    sc->watchdog = FALSE;
131141016Sdfr
131241016Sdfr    /* flush the event queue */
131341016Sdfr    sc->queue.count = 0;
131441016Sdfr    sc->queue.head = 0;
131541016Sdfr    sc->queue.tail = 0;
131641016Sdfr    sc->status.flags = 0;
131741016Sdfr    sc->status.button = 0;
131841016Sdfr    sc->status.obutton = 0;
131941016Sdfr    sc->status.dx = 0;
132041016Sdfr    sc->status.dy = 0;
132141016Sdfr    sc->status.dz = 0;
132241016Sdfr    sc->button = 0;
132341016Sdfr
132441016Sdfr    /* empty input buffer */
132541016Sdfr    bzero(sc->ipacket, sizeof(sc->ipacket));
132641016Sdfr    sc->inputbytes = 0;
132784880Syokota    sc->syncerrors = 0;
132841016Sdfr
132941016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
133041016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
133141016Sdfr	return (EIO);
133241016Sdfr
133341016Sdfr    /* save the current controller command byte */
133441016Sdfr    s = spltty();
133541016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
133641016Sdfr
133741016Sdfr    /* enable the aux port and temporalily disable the keyboard */
133841016Sdfr    if ((command_byte == -1)
133941016Sdfr        || !set_controller_command_byte(sc->kbdc,
134041016Sdfr	    kbdc_get_device_mask(sc->kbdc),
134141016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
134241016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
134341016Sdfr        /* CONTROLLER ERROR; do you know how to get out of this? */
134441016Sdfr        kbdc_lock(sc->kbdc, FALSE);
134541016Sdfr	splx(s);
134641016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
134741016Sdfr	    unit);
134841016Sdfr	return (EIO);
134941016Sdfr    }
135041016Sdfr    /*
135141016Sdfr     * Now that the keyboard controller is told not to generate
135241016Sdfr     * the keyboard and mouse interrupts, call `splx()' to allow
135341016Sdfr     * the other tty interrupts. The clock interrupt may also occur,
135441016Sdfr     * but timeout routines will be blocked by the poll flag set
135541016Sdfr     * via `kbdc_lock()'
135641016Sdfr     */
135741016Sdfr    splx(s);
135841016Sdfr
135941016Sdfr    /* enable the mouse device */
136084880Syokota    err = doopen(sc, command_byte);
136141016Sdfr
136241016Sdfr    /* done */
136341016Sdfr    if (err == 0)
136441016Sdfr        sc->state |= PSM_OPEN;
136541016Sdfr    kbdc_lock(sc->kbdc, FALSE);
136641016Sdfr    return (err);
136741016Sdfr}
136841016Sdfr
136941016Sdfrstatic int
137083366Sjulianpsmclose(dev_t dev, int flag, int fmt, struct thread *td)
137141016Sdfr{
137241016Sdfr    int unit = PSM_UNIT(dev);
137341016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
137441016Sdfr    int stat[3];
137541016Sdfr    int command_byte;
137641016Sdfr    int s;
137741016Sdfr
137841016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
137941016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
138041016Sdfr	return (EIO);
138141016Sdfr
138241016Sdfr    /* save the current controller command byte */
138341016Sdfr    s = spltty();
138441016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
138541016Sdfr    if (command_byte == -1) {
138641016Sdfr        kbdc_lock(sc->kbdc, FALSE);
138741016Sdfr	splx(s);
138841016Sdfr	return (EIO);
138941016Sdfr    }
139041016Sdfr
139141016Sdfr    /* disable the aux interrupt and temporalily disable the keyboard */
139241016Sdfr    if (!set_controller_command_byte(sc->kbdc,
139341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
139441016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
139541016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
139641016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
139758230Syokota	    unit);
139841016Sdfr	/* CONTROLLER ERROR;
139941016Sdfr	 * NOTE: we shall force our way through. Because the only
140041016Sdfr	 * ill effect we shall see is that we may not be able
140141016Sdfr	 * to read ACK from the mouse, and it doesn't matter much
140241016Sdfr	 * so long as the mouse will accept the DISABLE command.
140341016Sdfr	 */
140441016Sdfr    }
140541016Sdfr    splx(s);
140641016Sdfr
140758230Syokota    /* stop the watchdog timer */
140884880Syokota    untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
140958230Syokota    callout_handle_init(&sc->callout);
141058230Syokota
141141016Sdfr    /* remove anything left in the output buffer */
141241016Sdfr    empty_aux_buffer(sc->kbdc, 10);
141341016Sdfr
141441016Sdfr    /* disable the aux device, port and interrupt */
141541016Sdfr    if (sc->state & PSM_VALID) {
141641016Sdfr        if (!disable_aux_dev(sc->kbdc)) {
141741016Sdfr	    /* MOUSE ERROR;
141841016Sdfr	     * NOTE: we don't return error and continue, pretending
141941016Sdfr	     * we have successfully disabled the device. It's OK because
142041016Sdfr	     * the interrupt routine will discard any data from the mouse
142141016Sdfr	     * hereafter.
142241016Sdfr	     */
142341016Sdfr	    log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
142458230Syokota		unit);
142541016Sdfr        }
142641016Sdfr
142741016Sdfr        if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
142841016Sdfr            log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
142958230Syokota		unit);
143041016Sdfr    }
143141016Sdfr
143241016Sdfr    if (!set_controller_command_byte(sc->kbdc,
143341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
143441016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
143541016Sdfr	        | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
143641016Sdfr	/* CONTROLLER ERROR;
143741016Sdfr	 * we shall ignore this error; see the above comment.
143841016Sdfr	 */
143941016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
144058230Syokota	    unit);
144141016Sdfr    }
144241016Sdfr
144341016Sdfr    /* remove anything left in the output buffer */
144441016Sdfr    empty_aux_buffer(sc->kbdc, 10);
144541016Sdfr
144641016Sdfr    /* close is almost always successful */
144741016Sdfr    sc->state &= ~PSM_OPEN;
144841016Sdfr    kbdc_lock(sc->kbdc, FALSE);
144941016Sdfr    device_unbusy(devclass_get_device(psm_devclass, unit));
145041016Sdfr    return (0);
145141016Sdfr}
145241016Sdfr
145341016Sdfrstatic int
145441016Sdfrtame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
145541016Sdfr{
145641016Sdfr    static unsigned char butmapps2[8] = {
145741016Sdfr        0,
145841016Sdfr        MOUSE_PS2_BUTTON1DOWN,
145941016Sdfr        MOUSE_PS2_BUTTON2DOWN,
146041016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
146141016Sdfr        MOUSE_PS2_BUTTON3DOWN,
146241016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
146341016Sdfr        MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
146441016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
146541016Sdfr    };
146641016Sdfr    static unsigned char butmapmsc[8] = {
146741016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
146841016Sdfr        MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
146941016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
147041016Sdfr        MOUSE_MSC_BUTTON3UP,
147141016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
147241016Sdfr        MOUSE_MSC_BUTTON2UP,
147341016Sdfr        MOUSE_MSC_BUTTON1UP,
147441016Sdfr        0,
147541016Sdfr    };
147641016Sdfr    int mapped;
147741016Sdfr    int i;
147841016Sdfr
147941016Sdfr    if (sc->mode.level == PSM_LEVEL_BASE) {
148041016Sdfr        mapped = status->button & ~MOUSE_BUTTON4DOWN;
148141016Sdfr        if (status->button & MOUSE_BUTTON4DOWN)
148241016Sdfr	    mapped |= MOUSE_BUTTON1DOWN;
148341016Sdfr        status->button = mapped;
148441016Sdfr        buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1485109269Smdodd        i = imax(imin(status->dx, 255), -256);
148641016Sdfr	if (i < 0)
148741016Sdfr	    buf[0] |= MOUSE_PS2_XNEG;
148841016Sdfr        buf[1] = i;
1489109269Smdodd        i = imax(imin(status->dy, 255), -256);
149041016Sdfr	if (i < 0)
149141016Sdfr	    buf[0] |= MOUSE_PS2_YNEG;
149241016Sdfr        buf[2] = i;
149341016Sdfr	return MOUSE_PS2_PACKETSIZE;
149441016Sdfr    } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
149541016Sdfr        buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1496109269Smdodd        i = imax(imin(status->dx, 255), -256);
149741016Sdfr        buf[1] = i >> 1;
149841016Sdfr        buf[3] = i - buf[1];
1499109269Smdodd        i = imax(imin(status->dy, 255), -256);
150041016Sdfr        buf[2] = i >> 1;
150141016Sdfr        buf[4] = i - buf[2];
1502109269Smdodd        i = imax(imin(status->dz, 127), -128);
150341016Sdfr        buf[5] = (i >> 1) & 0x7f;
150441016Sdfr        buf[6] = (i - (i >> 1)) & 0x7f;
150541016Sdfr        buf[7] = (~status->button >> 3) & 0x7f;
150641016Sdfr	return MOUSE_SYS_PACKETSIZE;
150741016Sdfr    }
150841016Sdfr    return sc->inputbytes;;
150941016Sdfr}
151041016Sdfr
151141016Sdfrstatic int
151241016Sdfrpsmread(dev_t dev, struct uio *uio, int flag)
151341016Sdfr{
151441016Sdfr    register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
151541016Sdfr    unsigned char buf[PSM_SMALLBUFSIZE];
151641016Sdfr    int error = 0;
151741016Sdfr    int s;
151841016Sdfr    int l;
151941016Sdfr
152041016Sdfr    if ((sc->state & PSM_VALID) == 0)
152141016Sdfr	return EIO;
152241016Sdfr
152341016Sdfr    /* block until mouse activity occured */
152441016Sdfr    s = spltty();
152541016Sdfr    while (sc->queue.count <= 0) {
152641016Sdfr        if (PSM_NBLOCKIO(dev)) {
152741016Sdfr            splx(s);
152841016Sdfr            return EWOULDBLOCK;
152941016Sdfr        }
153041016Sdfr        sc->state |= PSM_ASLP;
1531111748Sdes        error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
153241016Sdfr        sc->state &= ~PSM_ASLP;
153341016Sdfr        if (error) {
153441016Sdfr            splx(s);
153541016Sdfr            return error;
153641016Sdfr        } else if ((sc->state & PSM_VALID) == 0) {
153741016Sdfr            /* the device disappeared! */
153841016Sdfr            splx(s);
153941016Sdfr            return EIO;
154041016Sdfr	}
154141016Sdfr    }
154241016Sdfr    splx(s);
154341016Sdfr
154441016Sdfr    /* copy data to the user land */
154541016Sdfr    while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
154641016Sdfr        s = spltty();
1547109269Smdodd	l = imin(sc->queue.count, uio->uio_resid);
154841016Sdfr	if (l > sizeof(buf))
154941016Sdfr	    l = sizeof(buf);
155041016Sdfr	if (l > sizeof(sc->queue.buf) - sc->queue.head) {
155141016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
155241016Sdfr		sizeof(sc->queue.buf) - sc->queue.head);
155341016Sdfr	    bcopy(&sc->queue.buf[0],
155441016Sdfr		&buf[sizeof(sc->queue.buf) - sc->queue.head],
155541016Sdfr		l - (sizeof(sc->queue.buf) - sc->queue.head));
155641016Sdfr	} else {
155741016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
155841016Sdfr	}
155941016Sdfr	sc->queue.count -= l;
156041016Sdfr	sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
156141016Sdfr        splx(s);
156241016Sdfr        error = uiomove(buf, l, uio);
156341016Sdfr        if (error)
156441016Sdfr	    break;
156541016Sdfr    }
156641016Sdfr
156741016Sdfr    return error;
156841016Sdfr}
156941016Sdfr
157041016Sdfrstatic int
157141016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
157241016Sdfr{
157341016Sdfr    int s;
157441016Sdfr
157541016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
157641016Sdfr	return EIO;
157741016Sdfr
157841016Sdfr    s = spltty();
157941016Sdfr    *c = get_controller_command_byte(sc->kbdc);
158041016Sdfr    if ((*c == -1)
158141016Sdfr	|| !set_controller_command_byte(sc->kbdc,
158241016Sdfr	    kbdc_get_device_mask(sc->kbdc),
158341016Sdfr            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
158441016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
158541016Sdfr        /* this is CONTROLLER ERROR */
158641016Sdfr	splx(s);
158741016Sdfr        kbdc_lock(sc->kbdc, FALSE);
158841016Sdfr	return EIO;
158941016Sdfr    }
159041016Sdfr
159141016Sdfr    /*
159241016Sdfr     * The device may be in the middle of status data transmission.
159341016Sdfr     * The transmission will be interrupted, thus, incomplete status
159441016Sdfr     * data must be discarded. Although the aux interrupt is disabled
159541016Sdfr     * at the keyboard controller level, at most one aux interrupt
159641016Sdfr     * may have already been pending and a data byte is in the
159741016Sdfr     * output buffer; throw it away. Note that the second argument
159841016Sdfr     * to `empty_aux_buffer()' is zero, so that the call will just
159941016Sdfr     * flush the internal queue.
160041016Sdfr     * `psmintr()' will be invoked after `splx()' if an interrupt is
160141016Sdfr     * pending; it will see no data and returns immediately.
160241016Sdfr     */
160341016Sdfr    empty_aux_buffer(sc->kbdc, 0);	/* flush the queue */
160441016Sdfr    read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
160541016Sdfr    sc->inputbytes = 0;
160641016Sdfr    splx(s);
160741016Sdfr
160841016Sdfr    return 0;
160941016Sdfr}
161041016Sdfr
161141016Sdfrstatic int
161241016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
161341016Sdfr{
161441016Sdfr    int error = 0;
161541016Sdfr
161641016Sdfr    /*
161741016Sdfr     * We may have seen a part of status data during `set_mouse_XXX()'.
161841016Sdfr     * they have been queued; flush it.
161941016Sdfr     */
162041016Sdfr    empty_aux_buffer(sc->kbdc, 0);
162141016Sdfr
162241016Sdfr    /* restore ports and interrupt */
162341016Sdfr    if (!set_controller_command_byte(sc->kbdc,
162441016Sdfr            kbdc_get_device_mask(sc->kbdc),
162541016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
162641016Sdfr        /* CONTROLLER ERROR; this is serious, we may have
162741016Sdfr         * been left with the inaccessible keyboard and
162841016Sdfr         * the disabled mouse interrupt.
162941016Sdfr         */
163041016Sdfr        error = EIO;
163141016Sdfr    }
163241016Sdfr
163341016Sdfr    kbdc_lock(sc->kbdc, FALSE);
163441016Sdfr    return error;
163541016Sdfr}
163641016Sdfr
163741016Sdfrstatic int
163883366Sjulianpsmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
163941016Sdfr{
164041016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
164141016Sdfr    mousemode_t mode;
164241016Sdfr    mousestatus_t status;
164341016Sdfr#if (defined(MOUSE_GETVARS))
164441016Sdfr    mousevar_t *var;
164541016Sdfr#endif
164641016Sdfr    mousedata_t *data;
164741016Sdfr    int stat[3];
164841016Sdfr    int command_byte;
164941016Sdfr    int error = 0;
165041016Sdfr    int s;
165141016Sdfr
165241016Sdfr    /* Perform IOCTL command */
165341016Sdfr    switch (cmd) {
165441016Sdfr
165541016Sdfr    case OLD_MOUSE_GETHWINFO:
165641016Sdfr	s = spltty();
165741016Sdfr        ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
165841016Sdfr        ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
165941016Sdfr        ((old_mousehw_t *)addr)->type = sc->hw.type;
166048778Syokota        ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
166141016Sdfr	splx(s);
166241016Sdfr        break;
166341016Sdfr
166441016Sdfr    case MOUSE_GETHWINFO:
166541016Sdfr	s = spltty();
166641016Sdfr        *(mousehw_t *)addr = sc->hw;
166741016Sdfr	if (sc->mode.level == PSM_LEVEL_BASE)
166841016Sdfr	    ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
166941016Sdfr	splx(s);
167041016Sdfr        break;
167141016Sdfr
167241016Sdfr    case OLD_MOUSE_GETMODE:
167341016Sdfr	s = spltty();
167441016Sdfr	switch (sc->mode.level) {
167541016Sdfr	case PSM_LEVEL_BASE:
167641016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
167741016Sdfr	    break;
167841016Sdfr	case PSM_LEVEL_STANDARD:
167941016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
168041016Sdfr	    break;
168141016Sdfr	case PSM_LEVEL_NATIVE:
168241016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
168341016Sdfr	    break;
168441016Sdfr	}
168541016Sdfr        ((old_mousemode_t *)addr)->rate = sc->mode.rate;
168641016Sdfr        ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
168741016Sdfr        ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
168841016Sdfr	splx(s);
168941016Sdfr        break;
169041016Sdfr
169141016Sdfr    case MOUSE_GETMODE:
169241016Sdfr	s = spltty();
169341016Sdfr        *(mousemode_t *)addr = sc->mode;
169441016Sdfr        ((mousemode_t *)addr)->resolution =
169541016Sdfr	    MOUSE_RES_LOW - sc->mode.resolution;
169641016Sdfr	switch (sc->mode.level) {
169741016Sdfr	case PSM_LEVEL_BASE:
169841016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
169941016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
170041016Sdfr	    break;
170141016Sdfr	case PSM_LEVEL_STANDARD:
170241016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
170341016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
170441016Sdfr	    ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
170541016Sdfr	    ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
170641016Sdfr	    break;
170741016Sdfr	case PSM_LEVEL_NATIVE:
170841016Sdfr	    /* FIXME: this isn't quite correct... XXX */
170941016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
171041016Sdfr	    break;
171141016Sdfr	}
171241016Sdfr	splx(s);
171341016Sdfr        break;
171441016Sdfr
171541016Sdfr    case OLD_MOUSE_SETMODE:
171641016Sdfr    case MOUSE_SETMODE:
171741016Sdfr	if (cmd == OLD_MOUSE_SETMODE) {
171841016Sdfr	    mode.rate = ((old_mousemode_t *)addr)->rate;
171941016Sdfr	    /*
172041016Sdfr	     * resolution  old I/F   new I/F
172141016Sdfr	     * default        0         0
172241016Sdfr	     * low            1        -2
172341016Sdfr	     * medium low     2        -3
172441016Sdfr	     * medium high    3        -4
172541016Sdfr	     * high           4        -5
172641016Sdfr	     */
172741016Sdfr	    if (((old_mousemode_t *)addr)->resolution > 0)
172841016Sdfr	        mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
172941016Sdfr	    mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
173041016Sdfr	    mode.level = -1;
173141016Sdfr	} else {
173241016Sdfr	    mode = *(mousemode_t *)addr;
173341016Sdfr	}
173441016Sdfr
173541016Sdfr	/* adjust and validate parameters. */
173641016Sdfr	if (mode.rate > UCHAR_MAX)
173741016Sdfr	    return EINVAL;
173841016Sdfr        if (mode.rate == 0)
173941016Sdfr            mode.rate = sc->dflt_mode.rate;
174041016Sdfr	else if (mode.rate == -1)
174141016Sdfr	    /* don't change the current setting */
174241016Sdfr	    ;
174341016Sdfr	else if (mode.rate < 0)
174441016Sdfr	    return EINVAL;
174541016Sdfr	if (mode.resolution >= UCHAR_MAX)
174641016Sdfr	    return EINVAL;
174741016Sdfr	if (mode.resolution >= 200)
174841016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
174941016Sdfr	else if (mode.resolution >= 100)
175041016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
175141016Sdfr	else if (mode.resolution >= 50)
175241016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
175341016Sdfr	else if (mode.resolution > 0)
175441016Sdfr	    mode.resolution = MOUSE_RES_LOW;
175541016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
175641016Sdfr            mode.resolution = sc->dflt_mode.resolution;
175741016Sdfr        else if (mode.resolution == -1)
175841016Sdfr	    /* don't change the current setting */
175941016Sdfr	    ;
176041016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
176141016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
176241016Sdfr	if (mode.level == -1)
176341016Sdfr	    /* don't change the current setting */
176441016Sdfr	    mode.level = sc->mode.level;
176541016Sdfr	else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
176641016Sdfr	    return EINVAL;
176741016Sdfr        if (mode.accelfactor == -1)
176841016Sdfr	    /* don't change the current setting */
176941016Sdfr	    mode.accelfactor = sc->mode.accelfactor;
177041016Sdfr        else if (mode.accelfactor < 0)
177141016Sdfr	    return EINVAL;
177241016Sdfr
177341016Sdfr	/* don't allow anybody to poll the keyboard controller */
177441016Sdfr	error = block_mouse_data(sc, &command_byte);
177541016Sdfr	if (error)
177641016Sdfr            return error;
177741016Sdfr
177841016Sdfr        /* set mouse parameters */
177941016Sdfr	if (mode.rate > 0)
178041016Sdfr	    mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
178141016Sdfr	if (mode.resolution >= 0)
178241016Sdfr	    mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
178341016Sdfr	set_mouse_scaling(sc->kbdc, 1);
178441016Sdfr	get_mouse_status(sc->kbdc, stat, 0, 3);
178541016Sdfr
178641016Sdfr        s = spltty();
178741016Sdfr    	sc->mode.rate = mode.rate;
178841016Sdfr    	sc->mode.resolution = mode.resolution;
178941016Sdfr    	sc->mode.accelfactor = mode.accelfactor;
179041016Sdfr    	sc->mode.level = mode.level;
179141016Sdfr        splx(s);
179241016Sdfr
179341016Sdfr	unblock_mouse_data(sc, command_byte);
179441016Sdfr        break;
179541016Sdfr
179641016Sdfr    case MOUSE_GETLEVEL:
179741016Sdfr	*(int *)addr = sc->mode.level;
179841016Sdfr        break;
179941016Sdfr
180041016Sdfr    case MOUSE_SETLEVEL:
180141016Sdfr	if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
180241016Sdfr	    return EINVAL;
180341016Sdfr	sc->mode.level = *(int *)addr;
180441016Sdfr        break;
180541016Sdfr
180641016Sdfr    case MOUSE_GETSTATUS:
180741016Sdfr        s = spltty();
180841016Sdfr	status = sc->status;
180941016Sdfr	sc->status.flags = 0;
181041016Sdfr	sc->status.obutton = sc->status.button;
181141016Sdfr	sc->status.button = 0;
181241016Sdfr	sc->status.dx = 0;
181341016Sdfr	sc->status.dy = 0;
181441016Sdfr	sc->status.dz = 0;
181541016Sdfr        splx(s);
181641016Sdfr        *(mousestatus_t *)addr = status;
181741016Sdfr        break;
181841016Sdfr
181941016Sdfr#if (defined(MOUSE_GETVARS))
182041016Sdfr    case MOUSE_GETVARS:
182141016Sdfr	var = (mousevar_t *)addr;
182241016Sdfr	bzero(var, sizeof(*var));
182341016Sdfr	s = spltty();
182441016Sdfr        var->var[0] = MOUSE_VARS_PS2_SIG;
182541016Sdfr        var->var[1] = sc->config;
182641016Sdfr        var->var[2] = sc->flags;
182741016Sdfr	splx(s);
182841016Sdfr        break;
182941016Sdfr
183041016Sdfr    case MOUSE_SETVARS:
183141016Sdfr	return ENODEV;
183241016Sdfr#endif /* MOUSE_GETVARS */
183341016Sdfr
183441016Sdfr    case MOUSE_READSTATE:
183541016Sdfr    case MOUSE_READDATA:
183641016Sdfr	data = (mousedata_t *)addr;
183741016Sdfr	if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
183841016Sdfr	    return EINVAL;
183941016Sdfr
184041016Sdfr	error = block_mouse_data(sc, &command_byte);
184141016Sdfr	if (error)
184241016Sdfr            return error;
184341016Sdfr        if ((data->len = get_mouse_status(sc->kbdc, data->buf,
184441016Sdfr		(cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
184541016Sdfr            error = EIO;
184641016Sdfr	unblock_mouse_data(sc, command_byte);
184741016Sdfr	break;
184841016Sdfr
184941016Sdfr#if (defined(MOUSE_SETRESOLUTION))
185041016Sdfr    case MOUSE_SETRESOLUTION:
185141016Sdfr	mode.resolution = *(int *)addr;
185241016Sdfr	if (mode.resolution >= UCHAR_MAX)
185341016Sdfr	    return EINVAL;
185441016Sdfr	else if (mode.resolution >= 200)
185541016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
185641016Sdfr	else if (mode.resolution >= 100)
185741016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
185841016Sdfr	else if (mode.resolution >= 50)
185941016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
186041016Sdfr	else if (mode.resolution > 0)
186141016Sdfr	    mode.resolution = MOUSE_RES_LOW;
186241016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
186341016Sdfr            mode.resolution = sc->dflt_mode.resolution;
186441016Sdfr        else if (mode.resolution == -1)
186541016Sdfr	    mode.resolution = sc->mode.resolution;
186641016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
186741016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
186841016Sdfr
186941016Sdfr	error = block_mouse_data(sc, &command_byte);
187041016Sdfr	if (error)
187141016Sdfr            return error;
187241016Sdfr        sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
187341016Sdfr	if (sc->mode.resolution != mode.resolution)
187441016Sdfr	    error = EIO;
187541016Sdfr	unblock_mouse_data(sc, command_byte);
187641016Sdfr        break;
187741016Sdfr#endif /* MOUSE_SETRESOLUTION */
187841016Sdfr
187941016Sdfr#if (defined(MOUSE_SETRATE))
188041016Sdfr    case MOUSE_SETRATE:
188141016Sdfr	mode.rate = *(int *)addr;
188241016Sdfr	if (mode.rate > UCHAR_MAX)
188341016Sdfr	    return EINVAL;
188441016Sdfr        if (mode.rate == 0)
188541016Sdfr            mode.rate = sc->dflt_mode.rate;
188641016Sdfr	else if (mode.rate < 0)
188741016Sdfr	    mode.rate = sc->mode.rate;
188841016Sdfr
188941016Sdfr	error = block_mouse_data(sc, &command_byte);
189041016Sdfr	if (error)
189141016Sdfr            return error;
189241016Sdfr        sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
189341016Sdfr	if (sc->mode.rate != mode.rate)
189441016Sdfr	    error = EIO;
189541016Sdfr	unblock_mouse_data(sc, command_byte);
189641016Sdfr        break;
189741016Sdfr#endif /* MOUSE_SETRATE */
189841016Sdfr
189941016Sdfr#if (defined(MOUSE_SETSCALING))
190041016Sdfr    case MOUSE_SETSCALING:
190141016Sdfr	if ((*(int *)addr <= 0) || (*(int *)addr > 2))
190241016Sdfr	    return EINVAL;
190341016Sdfr
190441016Sdfr	error = block_mouse_data(sc, &command_byte);
190541016Sdfr	if (error)
190641016Sdfr            return error;
190741016Sdfr        if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
190841016Sdfr	    error = EIO;
190941016Sdfr	unblock_mouse_data(sc, command_byte);
191041016Sdfr        break;
191141016Sdfr#endif /* MOUSE_SETSCALING */
191241016Sdfr
191341016Sdfr#if (defined(MOUSE_GETHWID))
191441016Sdfr    case MOUSE_GETHWID:
191541016Sdfr	error = block_mouse_data(sc, &command_byte);
191641016Sdfr	if (error)
191741016Sdfr            return error;
191848778Syokota        sc->hw.hwid &= ~0x00ff;
191948778Syokota        sc->hw.hwid |= get_aux_id(sc->kbdc);
192048778Syokota	*(int *)addr = sc->hw.hwid & 0x00ff;
192141016Sdfr	unblock_mouse_data(sc, command_byte);
192241016Sdfr        break;
192341016Sdfr#endif /* MOUSE_GETHWID */
192441016Sdfr
192541016Sdfr    default:
192641016Sdfr	return ENOTTY;
192741016Sdfr    }
192841016Sdfr
192941016Sdfr    return error;
193041016Sdfr}
193141016Sdfr
193241016Sdfrstatic void
193358230Syokotapsmtimeout(void *arg)
193458230Syokota{
193558230Syokota    struct psm_softc *sc;
193665045Syokota    int s;
193758230Syokota
193884880Syokota    sc = (struct psm_softc *)arg;
193965045Syokota    s = spltty();
194063746Syokota    if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
194158230Syokota	if (verbose >= 4)
194284880Syokota	    log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
194358230Syokota	psmintr(sc);
194463746Syokota	kbdc_lock(sc->kbdc, FALSE);
194558230Syokota    }
194658230Syokota    sc->watchdog = TRUE;
194765045Syokota    splx(s);
194884880Syokota    sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
194958230Syokota}
195058230Syokota
195158230Syokotastatic void
195241016Sdfrpsmintr(void *arg)
195341016Sdfr{
195441016Sdfr    /*
195541016Sdfr     * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
195641016Sdfr     * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
195741016Sdfr     */
195841016Sdfr    static int butmap[8] = {
195941016Sdfr        0,
196041016Sdfr	MOUSE_BUTTON1DOWN,
196141016Sdfr	MOUSE_BUTTON3DOWN,
196241016Sdfr	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
196341016Sdfr	MOUSE_BUTTON2DOWN,
196441016Sdfr	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
196541016Sdfr	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
196641016Sdfr        MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
196741016Sdfr    };
196849965Syokota    static int butmap_versapad[8] = {
196949965Syokota	0,
197049965Syokota	MOUSE_BUTTON3DOWN,
197149965Syokota	0,
197249965Syokota	MOUSE_BUTTON3DOWN,
197349965Syokota	MOUSE_BUTTON1DOWN,
197449965Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
197549965Syokota	MOUSE_BUTTON1DOWN,
197649965Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
197749965Syokota    };
197841016Sdfr    register struct psm_softc *sc = arg;
197941016Sdfr    mousestatus_t ms;
198084880Syokota    struct timeval tv;
198141016Sdfr    int x, y, z;
198241016Sdfr    int c;
198341016Sdfr    int l;
198449965Syokota    int x0, y0;
198541016Sdfr
198641016Sdfr    /* read until there is nothing to read */
198741016Sdfr    while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
198841016Sdfr
198941016Sdfr        /* discard the byte if the device is not open */
199041016Sdfr        if ((sc->state & PSM_OPEN) == 0)
199141016Sdfr            continue;
199241016Sdfr
199384880Syokota	getmicrouptime(&tv);
199484880Syokota	if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
199584880Syokota	    log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
199684880Syokota	    sc->inputbytes = 0;
199784880Syokota	    sc->syncerrors = 0;
199884880Syokota	}
199984880Syokota	sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
200084880Syokota	sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
200184880Syokota	timevaladd(&sc->inputtimeout, &tv);
200284880Syokota
200341016Sdfr        sc->ipacket[sc->inputbytes++] = c;
200441016Sdfr        if (sc->inputbytes < sc->mode.packetsize)
200541016Sdfr	    continue;
200641016Sdfr
200741016Sdfr#if 0
200841016Sdfr        log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
200941016Sdfr	    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
201041016Sdfr	    sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
201141016Sdfr#endif
201241016Sdfr
201341016Sdfr	c = sc->ipacket[0];
201441016Sdfr
201563746Syokota	if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
201663746Syokota            log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
201763746Syokota		c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
201884880Syokota	    ++sc->syncerrors;
201984880Syokota	    if (sc->syncerrors < sc->mode.packetsize) {
202084880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
202184880Syokota		--sc->inputbytes;
202284880Syokota		bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
202384880Syokota	    } else if (sc->syncerrors == sc->mode.packetsize) {
202469439Syokota		log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
202584880Syokota		sc->inputbytes = 0;
202669439Syokota		disable_aux_dev(sc->kbdc);
202769439Syokota		enable_aux_dev(sc->kbdc);
202884880Syokota	    } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
202984880Syokota		log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
203084880Syokota		--sc->inputbytes;
203184880Syokota		bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
203284880Syokota	    } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
203384880Syokota		log(LOG_DEBUG, "psmintr: reset the mouse.\n");
203484880Syokota		reinitialize(sc, TRUE);
203569439Syokota	    }
203684880Syokota	    continue;
203763746Syokota	}
203863746Syokota
203949965Syokota	/*
204041016Sdfr	 * A kludge for Kensington device!
204141016Sdfr	 * The MSB of the horizontal count appears to be stored in
204258230Syokota	 * a strange place.
204341016Sdfr	 */
204458230Syokota	if (sc->hw.model == MOUSE_MODEL_THINK)
204549965Syokota	    sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
204641016Sdfr
204741016Sdfr        /* ignore the overflow bits... */
204841016Sdfr        x = (c & MOUSE_PS2_XNEG) ?  sc->ipacket[1] - 256 : sc->ipacket[1];
204941016Sdfr        y = (c & MOUSE_PS2_YNEG) ?  sc->ipacket[2] - 256 : sc->ipacket[2];
205041016Sdfr	z = 0;
205141016Sdfr        ms.obutton = sc->button;		  /* previous button state */
205241016Sdfr        ms.button = butmap[c & MOUSE_PS2_BUTTONS];
205345789Speter	/* `tapping' action */
205445789Speter	if (sc->config & PSM_CONFIG_FORCETAP)
205545789Speter	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
205641016Sdfr
205741016Sdfr	switch (sc->hw.model) {
205841016Sdfr
205958230Syokota	case MOUSE_MODEL_EXPLORER:
206058230Syokota	    /*
206158230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
206258230Syokota	     * byte 1:  oy ox sy sx 1  M  R  L
206358230Syokota	     * byte 2:  x  x  x  x  x  x  x  x
206458230Syokota	     * byte 3:  y  y  y  y  y  y  y  y
206558230Syokota	     * byte 4:  *  *  S2 S1 s  d2 d1 d0
206658230Syokota	     *
206758230Syokota	     * L, M, R, S1, S2: left, middle, right and side buttons
206858230Syokota	     * s: wheel data sign bit
206958230Syokota	     * d2-d0: wheel data
207058230Syokota	     */
207158230Syokota	    z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
207258230Syokota		? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
207358230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
207458230Syokota		? MOUSE_BUTTON4DOWN : 0;
207558230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
207658230Syokota		? MOUSE_BUTTON5DOWN : 0;
207758230Syokota	    break;
207858230Syokota
207941016Sdfr	case MOUSE_MODEL_INTELLI:
208041016Sdfr	case MOUSE_MODEL_NET:
208141016Sdfr	    /* wheel data is in the fourth byte */
208241016Sdfr	    z = (char)sc->ipacket[3];
208358230Syokota	    /* some mice may send 7 when there is no Z movement?! XXX */
208458230Syokota	    if ((z >= 7) || (z <= -7))
208558230Syokota		z = 0;
208658230Syokota	    /* some compatible mice have additional buttons */
208758230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
208858230Syokota		? MOUSE_BUTTON4DOWN : 0;
208958230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
209058230Syokota		? MOUSE_BUTTON5DOWN : 0;
209141016Sdfr	    break;
209241016Sdfr
209341016Sdfr	case MOUSE_MODEL_MOUSEMANPLUS:
209448778Syokota	    /*
209548778Syokota	     * PS2++ protocl packet
209648778Syokota	     *
209748778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
209848778Syokota	     * byte 1:  *  1  p3 p2 1  *  *  *
209948778Syokota	     * byte 2:  c1 c2 p1 p0 d1 d0 1  0
210048778Syokota	     *
210148778Syokota	     * p3-p0: packet type
210248778Syokota	     * c1, c2: c1 & c2 == 1, if p2 == 0
210348778Syokota	     *         c1 & c2 == 0, if p2 == 1
210448778Syokota	     *
210548778Syokota	     * packet type: 0 (device type)
210648778Syokota	     * See comments in enable_mmanplus() below.
210748778Syokota	     *
210848778Syokota	     * packet type: 1 (wheel data)
210948778Syokota	     *
211048778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
211148778Syokota	     * byte 3:  h  *  B5 B4 s  d2 d1 d0
211248778Syokota	     *
211348778Syokota	     * h: 1, if horizontal roller data
211448778Syokota	     *    0, if vertical roller data
211548778Syokota	     * B4, B5: button 4 and 5
211648778Syokota	     * s: sign bit
211748778Syokota	     * d2-d0: roller data
211848778Syokota	     *
211948778Syokota	     * packet type: 2 (reserved)
212048778Syokota	     */
212148778Syokota	    if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
212248778Syokota		    && (abs(x) > 191)
212348778Syokota		    && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
212441016Sdfr		/* the extended data packet encodes button and wheel events */
212548778Syokota		switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
212648778Syokota		case 1:
212748778Syokota		    /* wheel data packet */
212848778Syokota		    x = y = 0;
212948778Syokota		    if (sc->ipacket[2] & 0x80) {
213048778Syokota			/* horizontal roller count - ignore it XXX*/
213148778Syokota		    } else {
213248778Syokota			/* vertical roller count */
213348778Syokota			z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
213448778Syokota			    ? (sc->ipacket[2] & 0x0f) - 16
213548778Syokota			    : (sc->ipacket[2] & 0x0f);
213648778Syokota		    }
213748778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
213848778Syokota			? MOUSE_BUTTON4DOWN : 0;
213948778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
214048778Syokota			? MOUSE_BUTTON5DOWN : 0;
214148778Syokota		    break;
214248778Syokota		case 2:
214358230Syokota		    /* this packet type is reserved by Logitech... */
214458230Syokota		    /*
214558230Syokota		     * IBM ScrollPoint Mouse uses this packet type to
214658230Syokota		     * encode both vertical and horizontal scroll movement.
214758230Syokota		     */
214858230Syokota		    x = y = 0;
214958230Syokota		    /* horizontal count */
215058230Syokota		    if (sc->ipacket[2] & 0x0f)
215158230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
215258230Syokota		    /* vertical count */
215358230Syokota		    if (sc->ipacket[2] & 0xf0)
215458230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
215558230Syokota#if 0
215658230Syokota		    /* vertical count */
215758230Syokota		    z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
215858230Syokota			? ((sc->ipacket[2] >> 4) & 0x0f) - 16
215958230Syokota			: ((sc->ipacket[2] >> 4) & 0x0f);
216058230Syokota		    /* horizontal count */
216158230Syokota		    w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
216258230Syokota			? (sc->ipacket[2] & 0x0f) - 16
216358230Syokota			: (sc->ipacket[2] & 0x0f);
216458230Syokota#endif
216558230Syokota		    break;
216648778Syokota		case 0:
216748778Syokota		    /* device type packet - shouldn't happen */
2168102412Scharnier		    /* FALLTHROUGH */
216948778Syokota		default:
217048778Syokota		    x = y = 0;
217148778Syokota		    ms.button = ms.obutton;
217258230Syokota		    if (bootverbose)
217358230Syokota			log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
217458230Syokota				       "0x%02x 0x%02x 0x%02x\n",
217558230Syokota			    MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
217658230Syokota			    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
217748778Syokota		    break;
217848778Syokota		}
217941016Sdfr	    } else {
218041016Sdfr		/* preserve button states */
218141016Sdfr		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
218241016Sdfr	    }
218341016Sdfr	    break;
218441016Sdfr
218541016Sdfr	case MOUSE_MODEL_GLIDEPOINT:
218641016Sdfr	    /* `tapping' action */
218741016Sdfr	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
218841016Sdfr	    break;
218941016Sdfr
219041016Sdfr	case MOUSE_MODEL_NETSCROLL:
219158230Syokota	    /* three addtional bytes encode buttons and wheel events */
219258230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
219341016Sdfr		? MOUSE_BUTTON4DOWN : 0;
219458230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
219558230Syokota		? MOUSE_BUTTON5DOWN : 0;
219641016Sdfr	    z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
219741016Sdfr		? sc->ipacket[4] - 256 : sc->ipacket[4];
219841016Sdfr	    break;
219941016Sdfr
220041016Sdfr	case MOUSE_MODEL_THINK:
220141016Sdfr	    /* the fourth button state in the first byte */
220241016Sdfr	    ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
220341016Sdfr	    break;
220441016Sdfr
220549965Syokota	case MOUSE_MODEL_VERSAPAD:
220649965Syokota	    /* VersaPad PS/2 absolute mode message format
220749965Syokota	     *
220849965Syokota	     * [packet1]     7   6   5   4   3   2   1   0(LSB)
220949965Syokota	     *  ipacket[0]:  1   1   0   A   1   L   T   R
221049965Syokota	     *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
221149965Syokota	     *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
221249965Syokota	     *  ipacket[3]:  1   1   1   A   1   L   T   R
221349965Syokota	     *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
221449965Syokota	     *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
221549965Syokota	     *
221649965Syokota	     * [note]
221749965Syokota	     *  R: right physical mouse button (1=on)
221849965Syokota	     *  T: touch pad virtual button (1=tapping)
221949965Syokota	     *  L: left physical mouse button (1=on)
222049965Syokota	     *  A: position data is valid (1=valid)
222149965Syokota	     *  H: horizontal data (12bit signed integer. H11 is sign bit.)
222249965Syokota	     *  V: vertical data (12bit signed integer. V11 is sign bit.)
222349965Syokota	     *  P: pressure data
222449965Syokota	     *
222549965Syokota	     * Tapping is mapped to MOUSE_BUTTON4.
222649965Syokota	     */
222749965Syokota	    ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
222849965Syokota	    ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
222949965Syokota	    x = y = 0;
223049965Syokota	    if (c & MOUSE_PS2VERSA_IN_USE) {
223149965Syokota		x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
223249965Syokota		y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
223349965Syokota		if (x0 & 0x800)
223449965Syokota		    x0 -= 0x1000;
223549965Syokota		if (y0 & 0x800)
223649965Syokota		    y0 -= 0x1000;
223749965Syokota		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
223849965Syokota		    x = sc->xold - x0;
223949965Syokota		    y = y0 - sc->yold;
224049965Syokota		    if (x < 0)	/* XXX */
224149965Syokota			x++;
224249965Syokota		    else if (x)
224349965Syokota			x--;
224449965Syokota		    if (y < 0)
224549965Syokota			y++;
224649965Syokota		    else if (y)
224749965Syokota			y--;
224849965Syokota		} else {
224949965Syokota		    sc->flags |= PSM_FLAGS_FINGERDOWN;
225049965Syokota		}
225149965Syokota		sc->xold = x0;
225249965Syokota		sc->yold = y0;
225349965Syokota	    } else {
225449965Syokota		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
225549965Syokota	    }
225649965Syokota	    c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
225749965Syokota		| ((y < 0) ? MOUSE_PS2_YNEG : 0);
225849965Syokota	    break;
225949965Syokota
226058230Syokota	case MOUSE_MODEL_4D:
226158230Syokota	    /*
226258230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
226358230Syokota	     * byte 1:  s2 d2 s1 d1 1  M  R  L
226458230Syokota	     * byte 2:  sx x  x  x  x  x  x  x
226558230Syokota	     * byte 3:  sy y  y  y  y  y  y  y
226658230Syokota	     *
226758230Syokota	     * s1: wheel 1 direction
226858230Syokota	     * d1: wheel 1 data
226958230Syokota	     * s2: wheel 2 direction
227058230Syokota	     * d2: wheel 2 data
227158230Syokota	     */
227258230Syokota	    x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
227358230Syokota	    y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
227458230Syokota	    switch (c & MOUSE_4D_WHEELBITS) {
227558230Syokota	    case 0x10:
227658230Syokota		z = 1;
227758230Syokota		break;
227858230Syokota	    case 0x30:
227958230Syokota		z = -1;
228058230Syokota		break;
228158230Syokota	    case 0x40:	/* 2nd wheel turning right XXX */
228258230Syokota		z = 2;
228358230Syokota		break;
228458230Syokota	    case 0xc0:	/* 2nd wheel turning left XXX */
228558230Syokota		z = -2;
228658230Syokota		break;
228758230Syokota	    }
228858230Syokota	    break;
228958230Syokota
229058230Syokota	case MOUSE_MODEL_4DPLUS:
229158230Syokota	    if ((x < 16 - 256) && (y < 16 - 256)) {
229258230Syokota		/*
229358230Syokota		 *          b7 b6 b5 b4 b3 b2 b1 b0
229458230Syokota		 * byte 1:  0  0  1  1  1  M  R  L
229558230Syokota		 * byte 2:  0  0  0  0  1  0  0  0
229658230Syokota		 * byte 3:  0  0  0  0  S  s  d1 d0
229758230Syokota		 *
229858230Syokota		 * L, M, R, S: left, middle, right and side buttons
229958230Syokota		 * s: wheel data sign bit
230058230Syokota		 * d1-d0: wheel data
230158230Syokota		 */
230258230Syokota		x = y = 0;
230358230Syokota		if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
230458230Syokota		    ms.button |= MOUSE_BUTTON4DOWN;
230558230Syokota		z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
230658230Syokota			? ((sc->ipacket[2] & 0x07) - 8)
230758230Syokota			: (sc->ipacket[2] & 0x07) ;
230858230Syokota	    } else {
230958230Syokota		/* preserve previous button states */
231058230Syokota		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
231158230Syokota	    }
231258230Syokota	    break;
231358230Syokota
231441016Sdfr	case MOUSE_MODEL_GENERIC:
231541016Sdfr	default:
231641016Sdfr	    break;
231741016Sdfr	}
231841016Sdfr
231941016Sdfr        /* scale values */
232041016Sdfr        if (sc->mode.accelfactor >= 1) {
232141016Sdfr            if (x != 0) {
232241016Sdfr                x = x * x / sc->mode.accelfactor;
232341016Sdfr                if (x == 0)
232441016Sdfr                    x = 1;
232541016Sdfr                if (c & MOUSE_PS2_XNEG)
232641016Sdfr                    x = -x;
232741016Sdfr            }
232841016Sdfr            if (y != 0) {
232941016Sdfr                y = y * y / sc->mode.accelfactor;
233041016Sdfr                if (y == 0)
233141016Sdfr                    y = 1;
233241016Sdfr                if (c & MOUSE_PS2_YNEG)
233341016Sdfr                    y = -y;
233441016Sdfr            }
233541016Sdfr        }
233641016Sdfr
233741016Sdfr        ms.dx = x;
233841016Sdfr        ms.dy = y;
233941016Sdfr        ms.dz = z;
234041016Sdfr        ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
234141016Sdfr	    | (ms.obutton ^ ms.button);
234241016Sdfr
234341016Sdfr	if (sc->mode.level < PSM_LEVEL_NATIVE)
234441016Sdfr	    sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
234541016Sdfr
234641016Sdfr        sc->status.flags |= ms.flags;
234741016Sdfr        sc->status.dx += ms.dx;
234841016Sdfr        sc->status.dy += ms.dy;
234941016Sdfr        sc->status.dz += ms.dz;
235041016Sdfr        sc->status.button = ms.button;
235141016Sdfr        sc->button = ms.button;
235241016Sdfr
235358230Syokota	sc->watchdog = FALSE;
235458230Syokota
235541016Sdfr        /* queue data */
235641016Sdfr        if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2357109269Smdodd	    l = imin(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
235841016Sdfr	    bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
235941016Sdfr	    if (sc->inputbytes > l)
236041016Sdfr	        bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
236141016Sdfr            sc->queue.tail =
236241016Sdfr		(sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
236341016Sdfr            sc->queue.count += sc->inputbytes;
236441016Sdfr	}
236541016Sdfr        sc->inputbytes = 0;
236641016Sdfr
236741016Sdfr        if (sc->state & PSM_ASLP) {
236841016Sdfr            sc->state &= ~PSM_ASLP;
2369111748Sdes            wakeup( sc);
237041016Sdfr    	}
237141016Sdfr        selwakeup(&sc->rsel);
237241016Sdfr    }
237341016Sdfr}
237441016Sdfr
237541016Sdfrstatic int
237683366Sjulianpsmpoll(dev_t dev, int events, struct thread *td)
237741016Sdfr{
237841016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
237941016Sdfr    int s;
238041016Sdfr    int revents = 0;
238141016Sdfr
238241016Sdfr    /* Return true if a mouse event available */
238341016Sdfr    s = spltty();
238445789Speter    if (events & (POLLIN | POLLRDNORM)) {
238541016Sdfr	if (sc->queue.count > 0)
238641016Sdfr	    revents |= events & (POLLIN | POLLRDNORM);
238741016Sdfr	else
238883366Sjulian	    selrecord(td, &sc->rsel);
238945789Speter    }
239041016Sdfr    splx(s);
239141016Sdfr
239241016Sdfr    return (revents);
239341016Sdfr}
239441016Sdfr
239541016Sdfr/* vendor/model specific routines */
239641016Sdfr
239741016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
239841016Sdfr{
239941016Sdfr    if (set_mouse_resolution(kbdc, res) != res)
240041016Sdfr        return FALSE;
240141016Sdfr    if (set_mouse_scaling(kbdc, scale)
240241016Sdfr	&& set_mouse_scaling(kbdc, scale)
240341016Sdfr	&& set_mouse_scaling(kbdc, scale)
240441016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3))
240541016Sdfr	return TRUE;
240641016Sdfr    return FALSE;
240741016Sdfr}
240841016Sdfr
240969438Syokotastatic int
241069438Syokotamouse_ext_command(KBDC kbdc, int command)
241169438Syokota{
241269438Syokota    int c;
241369438Syokota
241469438Syokota    c = (command >> 6) & 0x03;
241569438Syokota    if (set_mouse_resolution(kbdc, c) != c)
241669438Syokota	return FALSE;
241769438Syokota    c = (command >> 4) & 0x03;
241869438Syokota    if (set_mouse_resolution(kbdc, c) != c)
241969438Syokota	return FALSE;
242069438Syokota    c = (command >> 2) & 0x03;
242169438Syokota    if (set_mouse_resolution(kbdc, c) != c)
242269438Syokota	return FALSE;
242369438Syokota    c = (command >> 0) & 0x03;
242469438Syokota    if (set_mouse_resolution(kbdc, c) != c)
242569438Syokota	return FALSE;
242669438Syokota    return TRUE;
242769438Syokota}
242869438Syokota
242941016Sdfr#if notyet
243041016Sdfr/* Logitech MouseMan Cordless II */
243141016Sdfrstatic int
243241016Sdfrenable_lcordless(struct psm_softc *sc)
243341016Sdfr{
243441016Sdfr    int status[3];
243541016Sdfr    int ch;
243641016Sdfr
243741016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
243841016Sdfr        return FALSE;
243941016Sdfr    if (status[1] == PSMD_RES_HIGH)
244041016Sdfr	return FALSE;
244141016Sdfr    ch = (status[0] & 0x07) - 1;	/* channel # */
244241016Sdfr    if ((ch <= 0) || (ch > 4))
244341016Sdfr	return FALSE;
244441016Sdfr    /*
244541016Sdfr     * status[1]: always one?
244641016Sdfr     * status[2]: battery status? (0-100)
244741016Sdfr     */
244841016Sdfr    return TRUE;
244941016Sdfr}
245041016Sdfr#endif /* notyet */
245141016Sdfr
245258230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
245341016Sdfrstatic int
245441016Sdfrenable_groller(struct psm_softc *sc)
245541016Sdfr{
245641016Sdfr    int status[3];
245741016Sdfr
245841016Sdfr    /*
245941016Sdfr     * The special sequence to enable the fourth button and the
246041016Sdfr     * roller. Immediately after this sequence check status bytes.
246141016Sdfr     * if the mouse is NetScroll, the second and the third bytes are
246241016Sdfr     * '3' and 'D'.
246341016Sdfr     */
246441016Sdfr
246541016Sdfr    /*
246641016Sdfr     * If the mouse is an ordinary PS/2 mouse, the status bytes should
246741016Sdfr     * look like the following.
246841016Sdfr     *
246941016Sdfr     * byte 1 bit 7 always 0
247041016Sdfr     *        bit 6 stream mode (0)
247141016Sdfr     *        bit 5 disabled (0)
247241016Sdfr     *        bit 4 1:1 scaling (0)
247341016Sdfr     *        bit 3 always 0
247441016Sdfr     *        bit 0-2 button status
247541016Sdfr     * byte 2 resolution (PSMD_RES_HIGH)
247641016Sdfr     * byte 3 report rate (?)
247741016Sdfr     */
247841016Sdfr
247941016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
248041016Sdfr        return FALSE;
248141016Sdfr    if ((status[1] != '3') || (status[2] != 'D'))
248241016Sdfr        return FALSE;
248358230Syokota    /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
248441016Sdfr    sc->hw.buttons = 4;
248541016Sdfr    return TRUE;
248641016Sdfr}
248741016Sdfr
248858230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
248941016Sdfrstatic int
249041016Sdfrenable_gmouse(struct psm_softc *sc)
249141016Sdfr{
249241016Sdfr    int status[3];
249341016Sdfr
249441016Sdfr    /*
249541016Sdfr     * The special sequence to enable the middle, "rubber" button.
249641016Sdfr     * Immediately after this sequence check status bytes.
249741016Sdfr     * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
249841016Sdfr     * the second and the third bytes are '3' and 'U'.
249941016Sdfr     * NOTE: NetMouse reports that it has three buttons although it has
250041016Sdfr     * two buttons and a rubber button. NetMouse Pro and MIE Mouse
250141016Sdfr     * say they have three buttons too and they do have a button on the
250241016Sdfr     * side...
250341016Sdfr     */
250441016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
250541016Sdfr        return FALSE;
250641016Sdfr    if ((status[1] != '3') || (status[2] != 'U'))
250741016Sdfr        return FALSE;
250841016Sdfr    return TRUE;
250941016Sdfr}
251041016Sdfr
251141016Sdfr/* ALPS GlidePoint */
251241016Sdfrstatic int
251341016Sdfrenable_aglide(struct psm_softc *sc)
251441016Sdfr{
251541016Sdfr    int status[3];
251641016Sdfr
251741016Sdfr    /*
251841016Sdfr     * The special sequence to obtain ALPS GlidePoint specific
251941016Sdfr     * information. Immediately after this sequence, status bytes will
252041016Sdfr     * contain something interesting.
252141016Sdfr     * NOTE: ALPS produces several models of GlidePoint. Some of those
252241016Sdfr     * do not respond to this sequence, thus, cannot be detected this way.
252341016Sdfr     */
252450149Syokota    if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
252550149Syokota	return FALSE;
252641016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
252741016Sdfr        return FALSE;
252850149Syokota    if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
252941016Sdfr        return FALSE;
253041016Sdfr    return TRUE;
253141016Sdfr}
253241016Sdfr
253341016Sdfr/* Kensington ThinkingMouse/Trackball */
253441016Sdfrstatic int
253541016Sdfrenable_kmouse(struct psm_softc *sc)
253641016Sdfr{
253741016Sdfr    static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
253841016Sdfr    KBDC kbdc = sc->kbdc;
253941016Sdfr    int status[3];
254041016Sdfr    int id1;
254141016Sdfr    int id2;
254241016Sdfr    int i;
254341016Sdfr
254441016Sdfr    id1 = get_aux_id(kbdc);
254541016Sdfr    if (set_mouse_sampling_rate(kbdc, 10) != 10)
254641016Sdfr	return FALSE;
254741016Sdfr    /*
254841016Sdfr     * The device is now in the native mode? It returns a different
254941016Sdfr     * ID value...
255041016Sdfr     */
255141016Sdfr    id2 = get_aux_id(kbdc);
255241016Sdfr    if ((id1 == id2) || (id2 != 2))
255341016Sdfr	return FALSE;
255441016Sdfr
255541016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
255641016Sdfr        return FALSE;
255741016Sdfr#if PSM_DEBUG >= 2
255841016Sdfr    /* at this point, resolution is LOW, sampling rate is 10/sec */
255941016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
256041016Sdfr        return FALSE;
256141016Sdfr#endif
256241016Sdfr
256341016Sdfr    /*
256441016Sdfr     * The special sequence to enable the third and fourth buttons.
256541016Sdfr     * Otherwise they behave like the first and second buttons.
256641016Sdfr     */
256741016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
256841016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
256941016Sdfr	    return FALSE;
257041016Sdfr    }
257141016Sdfr
257241016Sdfr    /*
257341016Sdfr     * At this point, the device is using default resolution and
257441016Sdfr     * sampling rate for the native mode.
257541016Sdfr     */
257641016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
257741016Sdfr        return FALSE;
257841016Sdfr    if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
257941016Sdfr        return FALSE;
258041016Sdfr
258141016Sdfr    /* the device appears be enabled by this sequence, diable it for now */
258241016Sdfr    disable_aux_dev(kbdc);
258341016Sdfr    empty_aux_buffer(kbdc, 5);
258441016Sdfr
258541016Sdfr    return TRUE;
258641016Sdfr}
258741016Sdfr
258858230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
258941016Sdfrstatic int
259041016Sdfrenable_mmanplus(struct psm_softc *sc)
259141016Sdfr{
259241016Sdfr    KBDC kbdc = sc->kbdc;
259341016Sdfr    int data[3];
259441016Sdfr
259541016Sdfr    /* the special sequence to enable the fourth button and the roller. */
259658230Syokota    /*
259758230Syokota     * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
259858230Syokota     * must be called exactly three times since the last RESET command
259958230Syokota     * before this sequence. XXX
260058230Syokota     */
260169438Syokota    if (!set_mouse_scaling(kbdc, 1))
260269438Syokota	return FALSE;
260369438Syokota    if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
260469438Syokota	return FALSE;
260541016Sdfr    if (get_mouse_status(kbdc, data, 1, 3) < 3)
260641016Sdfr        return FALSE;
260741016Sdfr
260848778Syokota    /*
260948778Syokota     * PS2++ protocl, packet type 0
261041016Sdfr     *
261148778Syokota     *          b7 b6 b5 b4 b3 b2 b1 b0
261248778Syokota     * byte 1:  *  1  p3 p2 1  *  *  *
261348778Syokota     * byte 2:  1  1  p1 p0 m1 m0 1  0
261448778Syokota     * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
261548778Syokota     *
261648778Syokota     * p3-p0: packet type: 0
261758230Syokota     * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
261841016Sdfr     */
261948778Syokota    /* check constant bits */
262048778Syokota    if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
262141016Sdfr        return FALSE;
262248778Syokota    if ((data[1] & 0xc3) != 0xc2)
262348778Syokota        return FALSE;
262448778Syokota    /* check d3-d0 in byte 2 */
262548778Syokota    if (!MOUSE_PS2PLUS_CHECKBITS(data))
262648778Syokota        return FALSE;
262748778Syokota    /* check p3-p0 */
262848778Syokota    if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
262948778Syokota        return FALSE;
263041016Sdfr
263148778Syokota    sc->hw.hwid &= 0x00ff;
263248778Syokota    sc->hw.hwid |= data[2] << 8;	/* save model ID */
263348778Syokota
263441016Sdfr    /*
263541016Sdfr     * MouseMan+ (or FirstMouse+) is now in its native mode, in which
263641016Sdfr     * the wheel and the fourth button events are encoded in the
263741016Sdfr     * special data packet. The mouse may be put in the IntelliMouse mode
263841016Sdfr     * if it is initialized by the IntelliMouse's method.
263941016Sdfr     */
264041016Sdfr    return TRUE;
264141016Sdfr}
264241016Sdfr
264358230Syokota/* MS IntelliMouse Explorer */
264458230Syokotastatic int
264558230Syokotaenable_msexplorer(struct psm_softc *sc)
264658230Syokota{
264758923Syokota    static unsigned char rate0[] = { 200, 100, 80, };
264858923Syokota    static unsigned char rate1[] = { 200, 200, 80, };
264958230Syokota    KBDC kbdc = sc->kbdc;
265058230Syokota    int id;
265158230Syokota    int i;
265258230Syokota
265369438Syokota    /* the special sequence to enable the extra buttons and the roller. */
265469438Syokota    for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
265569438Syokota        if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
265669438Syokota	    return FALSE;
265769438Syokota    }
265869438Syokota    /* the device will give the genuine ID only after the above sequence */
265969438Syokota    id = get_aux_id(kbdc);
266069438Syokota    if (id != PSM_EXPLORER_ID)
266169438Syokota	return FALSE;
266269438Syokota
266369438Syokota    sc->hw.hwid = id;
266469438Syokota    sc->hw.buttons = 5;		/* IntelliMouse Explorer XXX */
266569438Syokota
266658923Syokota    /*
266758923Syokota     * XXX: this is a kludge to fool some KVM switch products
266858923Syokota     * which think they are clever enough to know the 4-byte IntelliMouse
266958923Syokota     * protocol, and assume any other protocols use 3-byte packets.
267058923Syokota     * They don't convey 4-byte data packets from the IntelliMouse Explorer
267158923Syokota     * correctly to the host computer because of this!
267258923Syokota     * The following sequence is actually IntelliMouse's "wake up"
267358923Syokota     * sequence; it will make the KVM think the mouse is IntelliMouse
267458923Syokota     * when it is in fact IntelliMouse Explorer.
267558923Syokota     */
267658923Syokota    for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
267758923Syokota        if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
267869438Syokota	    break;
267958923Syokota    }
268058923Syokota    id = get_aux_id(kbdc);
268158923Syokota
268258230Syokota    return TRUE;
268358230Syokota}
268458230Syokota
268541016Sdfr/* MS IntelliMouse */
268641016Sdfrstatic int
268741016Sdfrenable_msintelli(struct psm_softc *sc)
268841016Sdfr{
268941016Sdfr    /*
269041016Sdfr     * Logitech MouseMan+ and FirstMouse+ will also respond to this
269141016Sdfr     * probe routine and act like IntelliMouse.
269241016Sdfr     */
269341016Sdfr
269441016Sdfr    static unsigned char rate[] = { 200, 100, 80, };
269541016Sdfr    KBDC kbdc = sc->kbdc;
269641016Sdfr    int id;
269741016Sdfr    int i;
269841016Sdfr
269941016Sdfr    /* the special sequence to enable the third button and the roller. */
270041016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
270141016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
270241016Sdfr	    return FALSE;
270341016Sdfr    }
270441016Sdfr    /* the device will give the genuine ID only after the above sequence */
270541016Sdfr    id = get_aux_id(kbdc);
270641016Sdfr    if (id != PSM_INTELLI_ID)
270741016Sdfr	return FALSE;
270841016Sdfr
270941016Sdfr    sc->hw.hwid = id;
271041016Sdfr    sc->hw.buttons = 3;
271141016Sdfr
271241016Sdfr    return TRUE;
271341016Sdfr}
271441016Sdfr
271558230Syokota/* A4 Tech 4D Mouse */
271658230Syokotastatic int
271758230Syokotaenable_4dmouse(struct psm_softc *sc)
271858230Syokota{
271958230Syokota    /*
272058230Syokota     * Newer wheel mice from A4 Tech may use the 4D+ protocol.
272158230Syokota     */
272258230Syokota
272358230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
272458230Syokota    KBDC kbdc = sc->kbdc;
272558230Syokota    int id;
272658230Syokota    int i;
272758230Syokota
272858230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
272958230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
273058230Syokota	    return FALSE;
273158230Syokota    }
273258230Syokota    id = get_aux_id(kbdc);
273358230Syokota    /*
273458923Syokota     * WinEasy 4D, 4 Way Scroll 4D: 6
273558230Syokota     * Cable-Free 4D: 8 (4DPLUS)
273658923Syokota     * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
273758230Syokota     */
273858230Syokota    if (id != PSM_4DMOUSE_ID)
273958230Syokota	return FALSE;
274058230Syokota
274158230Syokota    sc->hw.hwid = id;
274258230Syokota    sc->hw.buttons = 3;		/* XXX some 4D mice have 4? */
274358230Syokota
274458230Syokota    return TRUE;
274558230Syokota}
274658230Syokota
274758230Syokota/* A4 Tech 4D+ Mouse */
274858230Syokotastatic int
274958230Syokotaenable_4dplus(struct psm_softc *sc)
275058230Syokota{
275158230Syokota    /*
275258230Syokota     * Newer wheel mice from A4 Tech seem to use this protocol.
275358230Syokota     * Older models are recognized as either 4D Mouse or IntelliMouse.
275458230Syokota     */
275558230Syokota    KBDC kbdc = sc->kbdc;
275658230Syokota    int id;
275758230Syokota
275858230Syokota    /*
275958230Syokota     * enable_4dmouse() already issued the following ID sequence...
276058230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
276158230Syokota    int i;
276258230Syokota
276358230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
276458230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
276558230Syokota	    return FALSE;
276658230Syokota    }
276758230Syokota    */
276858230Syokota
276958230Syokota    id = get_aux_id(kbdc);
277058230Syokota    if (id != PSM_4DPLUS_ID)
277158230Syokota	return FALSE;
277258230Syokota
277358230Syokota    sc->hw.hwid = id;
277458230Syokota    sc->hw.buttons = 4;		/* XXX */
277558230Syokota
277658230Syokota    return TRUE;
277758230Syokota}
277858230Syokota
277949965Syokota/* Interlink electronics VersaPad */
278049965Syokotastatic int
278149965Syokotaenable_versapad(struct psm_softc *sc)
278249965Syokota{
278349965Syokota    KBDC kbdc = sc->kbdc;
278449965Syokota    int data[3];
278549965Syokota
278649965Syokota    set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
278749965Syokota    set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
278849965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
278949965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
279049965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
279149965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
279249965Syokota    if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
279349965Syokota	return FALSE;
279449965Syokota    if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
279549965Syokota	return FALSE;
279649965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
279749965Syokota
279858230Syokota    sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
279958230Syokota
280049965Syokota    return TRUE;				/* PS/2 absolute mode */
280149965Syokota}
280249965Syokota
280341016Sdfrstatic int
280454629Syokotapsmresume(device_t dev)
280541016Sdfr{
280654629Syokota    struct psm_softc *sc = device_get_softc(dev);
280754629Syokota    int unit = device_get_unit(dev);
280884880Syokota    int err;
280941016Sdfr
281041016Sdfr    if (verbose >= 2)
281154629Syokota        log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
281241016Sdfr
281358230Syokota    if (!(sc->config & PSM_CONFIG_HOOKRESUME))
281458230Syokota	return (0);
281558230Syokota
281684880Syokota    err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
281741016Sdfr
281841016Sdfr    if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
281941016Sdfr	/*
282041016Sdfr	 * Release the blocked process; it must be notified that the device
282141016Sdfr	 * cannot be accessed anymore.
282241016Sdfr	 */
282341016Sdfr        sc->state &= ~PSM_ASLP;
2824111748Sdes        wakeup(sc);
282541016Sdfr    }
282641016Sdfr
282741016Sdfr    if (verbose >= 2)
282854629Syokota        log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
282941016Sdfr
283041016Sdfr    return (err);
283141016Sdfr}
283241016Sdfr
283352997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
283483147Syokota
283583147Syokota/*
283683147Syokota * This sucks up assignments from PNPBIOS and ACPI.
283783147Syokota */
283883147Syokota
283983931Syokota/*
284083931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
284183931Syokota * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
284283931Syokota * can be probed and attached only after the AT keyboard controller is
284383931Syokota * attached, we shall quietly reserve the IRQ resource for later use.
284483931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
284583931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging
284683931Syokota * under the keyboard controller, then probe and attach it.
284783931Syokota */
284883147Syokota
284983147Syokotastatic	devclass_t			psmcpnp_devclass;
285083147Syokota
285183147Syokotastatic	device_probe_t			psmcpnp_probe;
285283147Syokotastatic	device_attach_t			psmcpnp_attach;
285383147Syokota
285483147Syokotastatic device_method_t psmcpnp_methods[] = {
285583147Syokota	DEVMETHOD(device_probe,		psmcpnp_probe),
285683147Syokota	DEVMETHOD(device_attach,	psmcpnp_attach),
285783147Syokota
285883147Syokota	{ 0, 0 }
285983147Syokota};
286083147Syokota
286183147Syokotastatic driver_t psmcpnp_driver = {
286283147Syokota	PSMCPNP_DRIVER_NAME,
286383147Syokota	psmcpnp_methods,
286483147Syokota	1,			/* no softc */
286583147Syokota};
286683147Syokota
286783147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = {
286888188Ssheldonh	{ 0x030fd041, "PS/2 mouse port" },		/* PNP0F03 */
286983147Syokota	{ 0x130fd041, "PS/2 mouse port" },		/* PNP0F13 */
287083147Syokota	{ 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
287183492Syokota	{ 0x80374d24, "IBM PS/2 mouse port" },		/* IBM3780, ThinkPad */
287284407Stakawata	{ 0x81374d24, "IBM PS/2 mouse port" },		/* IBM3781, ThinkPad */
2873109679Shsu	{ 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
2874109679Shsu	{ 0x0290d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9002, Vaio */
2875109710Smarcel	{ 0x0390d94d, "SONY VAIO PS/2 mouse port"},	/* SNY9003, Vaio */
2876109679Shsu	{ 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
287783147Syokota	{ 0 }
287883147Syokota};
287983147Syokota
288083147Syokotastatic int
288183147Syokotacreate_a_copy(device_t atkbdc, device_t me)
288283147Syokota{
288383147Syokota	device_t psm;
288483147Syokota	u_long irq;
288583147Syokota
288683931Syokota	/* find the PS/2 mouse device instance under the keyboard controller */
288783931Syokota	psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
288883931Syokota				device_get_unit(atkbdc));
288983147Syokota	if (psm == NULL)
289083147Syokota		return ENXIO;
289183931Syokota	if (device_get_state(psm) != DS_NOTPRESENT)
289283931Syokota		return 0;
289383147Syokota
289483931Syokota	/* move our resource to the found device */
289583931Syokota	irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
289683147Syokota	bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
289783147Syokota
289883147Syokota	/* ...then probe and attach it */
289983147Syokota	return device_probe_and_attach(psm);
290083147Syokota}
290183147Syokota
290283147Syokotastatic int
290383147Syokotapsmcpnp_probe(device_t dev)
290483147Syokota{
290583931Syokota	struct resource *res;
290683931Syokota	u_long irq;
290783931Syokota	int rid;
290883147Syokota
290983147Syokota	if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
291083147Syokota		return ENXIO;
291183147Syokota
291283931Syokota	/*
291383931Syokota	 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
291483931Syokota	 * to the PS/2 mouse device node. But, some buggy PnP BIOS
291583931Syokota	 * declares the PS/2 mouse device node without an IRQ resource!
291683931Syokota	 * If this happens, we shall refer to device hints.
291783931Syokota	 * If we still don't find it there, use a hardcoded value... XXX
291883931Syokota	 */
291983931Syokota	rid = 0;
292083931Syokota	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
292183931Syokota	if (irq <= 0) {
292283931Syokota		if (resource_long_value(PSM_DRIVER_NAME,
292383931Syokota					device_get_unit(dev), "irq", &irq) != 0)
292483931Syokota			irq = 12;	/* XXX */
292583931Syokota		device_printf(dev, "irq resource info is missing; "
292683931Syokota			      "assuming irq %ld\n", irq);
292783931Syokota		bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
292883931Syokota	}
292983931Syokota	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
293083931Syokota				 RF_SHAREABLE);
293183931Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
293283147Syokota
293383147Syokota	/* keep quiet */
293483147Syokota	if (!bootverbose)
293583147Syokota		device_quiet(dev);
293683492Syokota
293783931Syokota	return ((res == NULL) ? ENXIO : 0);
293883147Syokota}
293983147Syokota
294083147Syokotastatic int
294183147Syokotapsmcpnp_attach(device_t dev)
294283147Syokota{
294383492Syokota	device_t atkbdc;
294483931Syokota	int rid;
294583147Syokota
294683931Syokota	/* find the keyboard controller, which may be on acpi* or isa* bus */
294783931Syokota	atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
294883931Syokota				     device_get_unit(dev));
294983931Syokota	if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
295083492Syokota		create_a_copy(atkbdc, dev);
295183931Syokota	} else {
295283931Syokota		/*
295383931Syokota		 * If we don't have the AT keyboard controller yet,
295483931Syokota		 * just reserve the IRQ for later use...
295583931Syokota		 * (See psmidentify() above.)
295683931Syokota		 */
295783931Syokota		rid = 0;
295883931Syokota		bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
295983931Syokota				   RF_SHAREABLE);
296083931Syokota	}
296183492Syokota
296283147Syokota	return 0;
296383147Syokota}
296483147Syokota
296583147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
296683147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
2967