psm.c revision 63951
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 63951 2000-07-28 03:48:46Z yokota $
2441016Sdfr */
2541016Sdfr
2641016Sdfr/*
2741016Sdfr *  Ported to 386bsd Oct 17, 1992
2841016Sdfr *  Sandi Donno, Computer Science, University of Cape Town, South Africa
2941016Sdfr *  Please send bug reports to sandi@cs.uct.ac.za
3041016Sdfr *
3141016Sdfr *  Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
3241016Sdfr *  although I was only partially successful in getting the alpha release
3341016Sdfr *  of his "driver for the Logitech and ATI Inport Bus mice for use with
3441016Sdfr *  386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
3541016Sdfr *  found his code to be an invaluable reference when porting this driver
3641016Sdfr *  to 386bsd.
3741016Sdfr *
3841016Sdfr *  Further modifications for latest 386BSD+patchkit and port to NetBSD,
3941016Sdfr *  Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
4041016Sdfr *
4141016Sdfr *  Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
4241016Sdfr *  Andrew Herbert - 12 June 1993
4341016Sdfr *
4441016Sdfr *  Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
4541016Sdfr *  - 13 June 1993
4641016Sdfr *
4741016Sdfr *  Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
4841016Sdfr *  - 24 October 1993
4941016Sdfr *
5041016Sdfr *  Hardware access routines and probe logic rewritten by
5141016Sdfr *  Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5241016Sdfr *  - 3, 14, 22 October 1996.
5341016Sdfr *  - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
5441016Sdfr *  - 14, 30 November 1996. Uses `kbdio.c'.
5541016Sdfr *  - 13 December 1996. Uses queuing version of `kbdio.c'.
5641016Sdfr *  - January/February 1997. Tweaked probe logic for
5741016Sdfr *    HiNote UltraII/Latitude/Armada laptops.
5841016Sdfr *  - 30 July 1997. Added APM support.
5941016Sdfr *  - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
6041016Sdfr *    Improved sync check logic.
6141016Sdfr *    Vendor specific support routines.
6241016Sdfr */
6341016Sdfr
6441016Sdfr#include "opt_psm.h"
6541016Sdfr
6641016Sdfr#include <sys/param.h>
6741016Sdfr#include <sys/systm.h>
6841016Sdfr#include <sys/kernel.h>
6941016Sdfr#include <sys/module.h>
7041016Sdfr#include <sys/bus.h>
7141016Sdfr#include <sys/conf.h>
7241016Sdfr#include <sys/poll.h>
7341016Sdfr#include <sys/syslog.h>
7445720Speter#include <machine/bus.h>
7541181Sdfr#include <sys/rman.h>
7641016Sdfr#include <sys/select.h>
7741016Sdfr#include <sys/uio.h>
7841016Sdfr
7941016Sdfr#include <machine/clock.h>
8041016Sdfr#include <machine/limits.h>
8141016Sdfr#include <machine/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
9741016Sdfr/* end of driver specific options */
9841016Sdfr
9941016Sdfr/* input queue */
10041016Sdfr#define PSM_BUFSIZE		960
10141016Sdfr#define PSM_SMALLBUFSIZE	240
10241016Sdfr
10341016Sdfr/* operation levels */
10441016Sdfr#define PSM_LEVEL_BASE		0
10541016Sdfr#define PSM_LEVEL_STANDARD	1
10641016Sdfr#define PSM_LEVEL_NATIVE	2
10741016Sdfr#define PSM_LEVEL_MIN		PSM_LEVEL_BASE
10841016Sdfr#define PSM_LEVEL_MAX		PSM_LEVEL_NATIVE
10941016Sdfr
11048778Syokota/* Logitech PS2++ protocol */
11148778Syokota#define MOUSE_PS2PLUS_CHECKBITS(b)	\
11248778Syokota				((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
11348778Syokota#define MOUSE_PS2PLUS_PACKET_TYPE(b)	\
11448778Syokota				(((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
11548778Syokota
11641016Sdfr/* some macros */
11741016Sdfr#define PSM_UNIT(dev)		(minor(dev) >> 1)
11841016Sdfr#define PSM_NBLOCKIO(dev)	(minor(dev) & 1)
11941016Sdfr#define PSM_MKMINOR(unit,block)	(((unit) << 1) | ((block) ? 0:1))
12041016Sdfr
12141016Sdfr#ifndef max
12241016Sdfr#define max(x,y)		((x) > (y) ? (x) : (y))
12341016Sdfr#endif
12441016Sdfr#ifndef min
12541016Sdfr#define min(x,y)		((x) < (y) ? (x) : (y))
12641016Sdfr#endif
12741016Sdfr
12848778Syokota#define abs(x)			(((x) < 0) ? -(x) : (x))
12948778Syokota
13041016Sdfr/* ring buffer */
13141016Sdfrtypedef struct ringbuf {
13241016Sdfr    int           count;	/* # of valid elements in the buffer */
13341016Sdfr    int           head;		/* head pointer */
13441016Sdfr    int           tail;		/* tail poiner */
13541016Sdfr    unsigned char buf[PSM_BUFSIZE];
13641016Sdfr} ringbuf_t;
13741016Sdfr
13841016Sdfr/* driver control block */
13941016Sdfrstruct psm_softc {		/* Driver status information */
14041016Sdfr    struct selinfo rsel;	/* Process selecting for Input */
14141016Sdfr    unsigned char state;	/* Mouse driver state */
14241016Sdfr    int           config;	/* driver configuration flags */
14341016Sdfr    int           flags;	/* other flags */
14441016Sdfr    KBDC          kbdc;		/* handle to access the keyboard controller */
14558230Syokota    struct resource *intr;	/* IRQ resource */
14658230Syokota    void	  *ih;		/* interrupt handle */
14741016Sdfr    mousehw_t     hw;		/* hardware information */
14841016Sdfr    mousemode_t   mode;		/* operation mode */
14941016Sdfr    mousemode_t   dflt_mode;	/* default operation mode */
15041016Sdfr    mousestatus_t status;	/* accumulated mouse movement */
15141016Sdfr    ringbuf_t     queue;	/* mouse status queue */
15241016Sdfr    unsigned char ipacket[16];	/* interim input buffer */
15341016Sdfr    int           inputbytes;	/* # of bytes in the input buffer */
15441016Sdfr    int           button;	/* the latest button state */
15549965Syokota    int		  xold;	/* previous absolute X position */
15649965Syokota    int		  yold;	/* previous absolute Y position */
15758230Syokota    int		  watchdog;	/* watchdog timer flag */
15858230Syokota    struct callout_handle callout;	/* watchdog timer call out */
15958230Syokota    dev_t	  dev;
16058230Syokota    dev_t	  bdev;
16141016Sdfr};
16241016Sdfrdevclass_t psm_devclass;
16341016Sdfr#define PSM_SOFTC(unit)	((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
16441016Sdfr
16541016Sdfr/* driver state flags (state) */
16641016Sdfr#define PSM_VALID		0x80
16741016Sdfr#define PSM_OPEN		1	/* Device is open */
16841016Sdfr#define PSM_ASLP		2	/* Waiting for mouse data */
16941016Sdfr
17041016Sdfr/* driver configuration flags (config) */
17141016Sdfr#define PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
17241016Sdfr#define PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
17341016Sdfr#define PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
17445789Speter#define PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
17545789Speter#define PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
17645789Speter#define PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
17745789Speter#define PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
17858230Syokota#define PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
17958230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
18041016Sdfr
18141016Sdfr#define PSM_CONFIG_FLAGS	(PSM_CONFIG_RESOLUTION 		\
18241016Sdfr				    | PSM_CONFIG_ACCEL		\
18345789Speter				    | PSM_CONFIG_NOCHECKSYNC	\
18445789Speter				    | PSM_CONFIG_NOIDPROBE	\
18545789Speter				    | PSM_CONFIG_NORESET	\
18645789Speter				    | PSM_CONFIG_FORCETAP	\
18758230Syokota				    | PSM_CONFIG_IGNPORTERROR	\
18858230Syokota				    | PSM_CONFIG_HOOKRESUME	\
18958230Syokota				    | PSM_CONFIG_INITAFTERSUSPEND)
19041016Sdfr
19141016Sdfr/* other flags (flags) */
19249965Syokota#define PSM_FLAGS_FINGERDOWN	0x0001 /* VersaPad finger down */
19341016Sdfr
19441016Sdfr/* for backward compatibility */
19541016Sdfr#define OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
19641016Sdfr#define OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
19741016Sdfr#define OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
19841016Sdfr
19941016Sdfrtypedef struct old_mousehw {
20041016Sdfr    int buttons;
20141016Sdfr    int iftype;
20241016Sdfr    int type;
20341016Sdfr    int hwid;
20441016Sdfr} old_mousehw_t;
20541016Sdfr
20641016Sdfrtypedef struct old_mousemode {
20741016Sdfr    int protocol;
20841016Sdfr    int rate;
20941016Sdfr    int resolution;
21041016Sdfr    int accelfactor;
21141016Sdfr} old_mousemode_t;
21241016Sdfr
21341016Sdfr/* packet formatting function */
21441016Sdfrtypedef int packetfunc_t __P((struct psm_softc *, unsigned char *,
21541016Sdfr			      int *, int, mousestatus_t *));
21641016Sdfr
21741016Sdfr/* function prototypes */
21841016Sdfrstatic int psmprobe __P((device_t));
21941016Sdfrstatic int psmattach __P((device_t));
22058230Syokotastatic int psmdetach __P((device_t));
22154629Syokotastatic int psmresume __P((device_t));
22241016Sdfr
22341016Sdfrstatic d_open_t psmopen;
22441016Sdfrstatic d_close_t psmclose;
22541016Sdfrstatic d_read_t psmread;
22641016Sdfrstatic d_ioctl_t psmioctl;
22741016Sdfrstatic d_poll_t psmpoll;
22841016Sdfr
22941016Sdfrstatic int enable_aux_dev __P((KBDC));
23041016Sdfrstatic int disable_aux_dev __P((KBDC));
23141016Sdfrstatic int get_mouse_status __P((KBDC, int *, int, int));
23241016Sdfrstatic int get_aux_id __P((KBDC));
23341016Sdfrstatic int set_mouse_sampling_rate __P((KBDC, int));
23441016Sdfrstatic int set_mouse_scaling __P((KBDC, int));
23541016Sdfrstatic int set_mouse_resolution __P((KBDC, int));
23641016Sdfrstatic int set_mouse_mode __P((KBDC));
23741016Sdfrstatic int get_mouse_buttons __P((KBDC));
23841016Sdfrstatic int is_a_mouse __P((int));
23941016Sdfrstatic void recover_from_error __P((KBDC));
24041016Sdfrstatic int restore_controller __P((KBDC, int));
24141016Sdfrstatic int reinitialize __P((int, mousemode_t *));
24241016Sdfrstatic int doopen __P((int, int));
24358230Syokotastatic char *model_name __P((int));
24458230Syokotastatic void psmintr __P((void *));
24558230Syokotastatic void psmtimeout __P((void *));
24641016Sdfr
24741016Sdfr/* vendor specific features */
24841016Sdfrtypedef int probefunc_t __P((struct psm_softc *));
24941016Sdfr
25041016Sdfrstatic int mouse_id_proc1 __P((KBDC, int, int, int *));
25141016Sdfrstatic probefunc_t enable_groller;
25241016Sdfrstatic probefunc_t enable_gmouse;
25341016Sdfrstatic probefunc_t enable_aglide;
25441016Sdfrstatic probefunc_t enable_kmouse;
25558230Syokotastatic probefunc_t enable_msexplorer;
25641016Sdfrstatic probefunc_t enable_msintelli;
25758230Syokotastatic probefunc_t enable_4dmouse;
25858230Syokotastatic probefunc_t enable_4dplus;
25941016Sdfrstatic probefunc_t enable_mmanplus;
26049965Syokotastatic probefunc_t enable_versapad;
26141016Sdfrstatic int tame_mouse __P((struct psm_softc *, mousestatus_t *, unsigned char *));
26241016Sdfr
26341016Sdfrstatic struct {
26441016Sdfr    int                 model;
26541016Sdfr    unsigned char	syncmask;
26641016Sdfr    int 		packetsize;
26741016Sdfr    probefunc_t 	*probefunc;
26841016Sdfr} vendortype[] = {
26958230Syokota    /*
27058230Syokota     * WARNING: the order of probe is very important.  Don't mess it
27158230Syokota     * unless you know what you are doing.
27258230Syokota     */
27341016Sdfr    { MOUSE_MODEL_NET,			/* Genius NetMouse */
27458230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
27541016Sdfr    { MOUSE_MODEL_NETSCROLL,		/* Genius NetScroll */
27641016Sdfr      0xc8, 6, enable_groller, },
27758230Syokota    { MOUSE_MODEL_MOUSEMANPLUS,		/* Logitech MouseMan+ */
27858230Syokota      0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
27958230Syokota    { MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
28058230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
28158230Syokota    { MOUSE_MODEL_4D,			/* A4 Tech 4D Mouse */
28258230Syokota      0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
28358230Syokota    { MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
28458230Syokota      0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
28558230Syokota    { MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
28658230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
28741016Sdfr    { MOUSE_MODEL_GLIDEPOINT,		/* ALPS GlidePoint */
28841016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
28941016Sdfr    { MOUSE_MODEL_THINK,		/* Kensignton ThinkingMouse */
29041016Sdfr      0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
29149965Syokota    { MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
29249965Syokota      0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
29341016Sdfr    { MOUSE_MODEL_GENERIC,
29441016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
29541016Sdfr};
29663951Syokota#define GENERIC_MOUSE_ENTRY	((sizeof(vendortype) / sizeof(*vendortype)) - 1)
29741016Sdfr
29841016Sdfr/* device driver declarateion */
29941016Sdfrstatic device_method_t psm_methods[] = {
30041016Sdfr	/* Device interface */
30141016Sdfr	DEVMETHOD(device_probe,		psmprobe),
30241016Sdfr	DEVMETHOD(device_attach,	psmattach),
30358230Syokota	DEVMETHOD(device_detach,	psmdetach),
30454629Syokota	DEVMETHOD(device_resume,	psmresume),
30541016Sdfr
30641016Sdfr	{ 0, 0 }
30741016Sdfr};
30841016Sdfr
30941016Sdfrstatic driver_t psm_driver = {
31041016Sdfr    "psm",
31141016Sdfr    psm_methods,
31241016Sdfr    sizeof(struct psm_softc),
31341016Sdfr};
31441016Sdfr
31558230Syokota#if notyet
31658230Syokotastatic struct isa_pnp_id psm_ids[] = {
31758230Syokota    { 0x130fd041, "PS/2 mouse port" },			/* PNP0F13 */
31858230Syokota    { 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
31958230Syokota    { 0 }
32058230Syokota};
32158230Syokota#endif
32258230Syokota
32341016Sdfr#define CDEV_MAJOR        21
32441016Sdfr
32547625Sphkstatic struct cdevsw psm_cdevsw = {
32647625Sphk	/* open */	psmopen,
32747625Sphk	/* close */	psmclose,
32847625Sphk	/* read */	psmread,
32947625Sphk	/* write */	nowrite,
33047625Sphk	/* ioctl */	psmioctl,
33147625Sphk	/* poll */	psmpoll,
33247625Sphk	/* mmap */	nommap,
33347625Sphk	/* strategy */	nostrategy,
33447625Sphk	/* name */	"psm",
33547625Sphk	/* maj */	CDEV_MAJOR,
33647625Sphk	/* dump */	nodump,
33747625Sphk	/* psize */	nopsize,
33847625Sphk	/* flags */	0,
33947625Sphk	/* bmaj */	-1
34041016Sdfr};
34141016Sdfr
34241016Sdfr/* debug message level */
34341016Sdfrstatic int verbose = PSM_DEBUG;
34441016Sdfr
34541016Sdfr/* device I/O routines */
34641016Sdfrstatic int
34741016Sdfrenable_aux_dev(KBDC kbdc)
34841016Sdfr{
34941016Sdfr    int res;
35041016Sdfr
35141016Sdfr    res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
35241016Sdfr    if (verbose >= 2)
35341016Sdfr        log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
35441016Sdfr
35541016Sdfr    return (res == PSM_ACK);
35641016Sdfr}
35741016Sdfr
35841016Sdfrstatic int
35941016Sdfrdisable_aux_dev(KBDC kbdc)
36041016Sdfr{
36141016Sdfr    int res;
36241016Sdfr
36341016Sdfr    res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
36441016Sdfr    if (verbose >= 2)
36541016Sdfr        log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
36641016Sdfr
36741016Sdfr    return (res == PSM_ACK);
36841016Sdfr}
36941016Sdfr
37041016Sdfrstatic int
37141016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
37241016Sdfr{
37341016Sdfr    int cmd;
37441016Sdfr    int res;
37541016Sdfr    int i;
37641016Sdfr
37741016Sdfr    switch (flag) {
37841016Sdfr    case 0:
37941016Sdfr    default:
38041016Sdfr	cmd = PSMC_SEND_DEV_STATUS;
38141016Sdfr	break;
38241016Sdfr    case 1:
38341016Sdfr	cmd = PSMC_SEND_DEV_DATA;
38441016Sdfr	break;
38541016Sdfr    }
38641016Sdfr    empty_aux_buffer(kbdc, 5);
38741016Sdfr    res = send_aux_command(kbdc, cmd);
38841016Sdfr    if (verbose >= 2)
38941016Sdfr        log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
39041016Sdfr	    (flag == 1) ? "DATA" : "STATUS", res);
39141016Sdfr    if (res != PSM_ACK)
39241016Sdfr        return 0;
39341016Sdfr
39441016Sdfr    for (i = 0; i < len; ++i) {
39541016Sdfr        status[i] = read_aux_data(kbdc);
39641016Sdfr	if (status[i] < 0)
39741016Sdfr	    break;
39841016Sdfr    }
39941016Sdfr
40041016Sdfr    if (verbose) {
40141016Sdfr        log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
40241016Sdfr            (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
40341016Sdfr    }
40441016Sdfr
40541016Sdfr    return i;
40641016Sdfr}
40741016Sdfr
40841016Sdfrstatic int
40941016Sdfrget_aux_id(KBDC kbdc)
41041016Sdfr{
41141016Sdfr    int res;
41241016Sdfr    int id;
41341016Sdfr
41441016Sdfr    empty_aux_buffer(kbdc, 5);
41541016Sdfr    res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
41641016Sdfr    if (verbose >= 2)
41741016Sdfr        log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
41841016Sdfr    if (res != PSM_ACK)
41941016Sdfr	return (-1);
42041016Sdfr
42141016Sdfr    /* 10ms delay */
42241016Sdfr    DELAY(10000);
42341016Sdfr
42441016Sdfr    id = read_aux_data(kbdc);
42541016Sdfr    if (verbose >= 2)
42641016Sdfr        log(LOG_DEBUG, "psm: device ID: %04x\n", id);
42741016Sdfr
42841016Sdfr    return id;
42941016Sdfr}
43041016Sdfr
43141016Sdfrstatic int
43241016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
43341016Sdfr{
43441016Sdfr    int res;
43541016Sdfr
43641016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
43741016Sdfr    if (verbose >= 2)
43841016Sdfr        log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
43941016Sdfr
44041016Sdfr    return ((res == PSM_ACK) ? rate : -1);
44141016Sdfr}
44241016Sdfr
44341016Sdfrstatic int
44441016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
44541016Sdfr{
44641016Sdfr    int res;
44741016Sdfr
44841016Sdfr    switch (scale) {
44941016Sdfr    case 1:
45041016Sdfr    default:
45141016Sdfr	scale = PSMC_SET_SCALING11;
45241016Sdfr	break;
45341016Sdfr    case 2:
45441016Sdfr	scale = PSMC_SET_SCALING21;
45541016Sdfr	break;
45641016Sdfr    }
45741016Sdfr    res = send_aux_command(kbdc, scale);
45841016Sdfr    if (verbose >= 2)
45941016Sdfr        log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
46041016Sdfr	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
46141016Sdfr
46241016Sdfr    return (res == PSM_ACK);
46341016Sdfr}
46441016Sdfr
46541016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
46641016Sdfrstatic int
46741016Sdfrset_mouse_resolution(KBDC kbdc, int val)
46841016Sdfr{
46941016Sdfr    int res;
47041016Sdfr
47141016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
47241016Sdfr    if (verbose >= 2)
47341016Sdfr        log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
47441016Sdfr
47541016Sdfr    return ((res == PSM_ACK) ? val : -1);
47641016Sdfr}
47741016Sdfr
47841016Sdfr/*
47941016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
48041016Sdfr * re-enabled by calling `enable_aux_dev()'
48141016Sdfr */
48241016Sdfrstatic int
48341016Sdfrset_mouse_mode(KBDC kbdc)
48441016Sdfr{
48541016Sdfr    int res;
48641016Sdfr
48741016Sdfr    res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
48841016Sdfr    if (verbose >= 2)
48941016Sdfr        log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
49041016Sdfr
49141016Sdfr    return (res == PSM_ACK);
49241016Sdfr}
49341016Sdfr
49441016Sdfrstatic int
49541016Sdfrget_mouse_buttons(KBDC kbdc)
49641016Sdfr{
49741016Sdfr    int c = 2;		/* assume two buttons by default */
49841016Sdfr    int status[3];
49941016Sdfr
50041016Sdfr    /*
50141016Sdfr     * NOTE: a special sequence to obtain Logitech Mouse specific
50241016Sdfr     * information: set resolution to 25 ppi, set scaling to 1:1, set
50341016Sdfr     * scaling to 1:1, set scaling to 1:1. Then the second byte of the
50441016Sdfr     * mouse status bytes is the number of available buttons.
50541016Sdfr     * Some manufactures also support this sequence.
50641016Sdfr     */
50741016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
50841016Sdfr        return c;
50941016Sdfr    if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
51041016Sdfr        && set_mouse_scaling(kbdc, 1)
51141016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
51241016Sdfr        if (status[1] != 0)
51341016Sdfr            return status[1];
51441016Sdfr    }
51541016Sdfr    return c;
51641016Sdfr}
51741016Sdfr
51841016Sdfr/* misc subroutines */
51941016Sdfr/*
52041016Sdfr * Someday, I will get the complete list of valid pointing devices and
52141016Sdfr * their IDs... XXX
52241016Sdfr */
52341016Sdfrstatic int
52441016Sdfris_a_mouse(int id)
52541016Sdfr{
52641016Sdfr#if 0
52741016Sdfr    static int valid_ids[] = {
52841016Sdfr        PSM_MOUSE_ID,		/* mouse */
52941016Sdfr        PSM_BALLPOINT_ID,	/* ballpoint device */
53041016Sdfr        PSM_INTELLI_ID,		/* Intellimouse */
53158230Syokota        PSM_EXPLORER_ID,	/* Intellimouse Explorer */
53241016Sdfr        -1			/* end of table */
53341016Sdfr    };
53441016Sdfr    int i;
53541016Sdfr
53641016Sdfr    for (i = 0; valid_ids[i] >= 0; ++i)
53741016Sdfr        if (valid_ids[i] == id)
53841016Sdfr            return TRUE;
53941016Sdfr    return FALSE;
54041016Sdfr#else
54141016Sdfr    return TRUE;
54241016Sdfr#endif
54341016Sdfr}
54441016Sdfr
54541016Sdfrstatic char *
54641016Sdfrmodel_name(int model)
54741016Sdfr{
54841016Sdfr    static struct {
54941016Sdfr	int model_code;
55041016Sdfr	char *model_name;
55141016Sdfr    } models[] = {
55258230Syokota        { MOUSE_MODEL_NETSCROLL,	"NetScroll" },
55358230Syokota        { MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
55441016Sdfr        { MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
55541016Sdfr        { MOUSE_MODEL_THINK,		"ThinkingMouse" },
55641016Sdfr        { MOUSE_MODEL_INTELLI,		"IntelliMouse" },
55741016Sdfr        { MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
55849965Syokota        { MOUSE_MODEL_VERSAPAD,		"VersaPad" },
55958230Syokota        { MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
56058230Syokota        { MOUSE_MODEL_4D,		"4D Mouse" },
56158230Syokota        { MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
56241016Sdfr        { MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
56341016Sdfr        { MOUSE_MODEL_UNKNOWN,		NULL },
56441016Sdfr    };
56541016Sdfr    int i;
56641016Sdfr
56741016Sdfr    for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
56841016Sdfr	if (models[i].model_code == model)
56941016Sdfr	    return models[i].model_name;
57041016Sdfr    }
57141016Sdfr    return "Unknown";
57241016Sdfr}
57341016Sdfr
57441016Sdfrstatic void
57541016Sdfrrecover_from_error(KBDC kbdc)
57641016Sdfr{
57741016Sdfr    /* discard anything left in the output buffer */
57841016Sdfr    empty_both_buffers(kbdc, 10);
57941016Sdfr
58041016Sdfr#if 0
58141016Sdfr    /*
58241016Sdfr     * NOTE: KBDC_RESET_KBD may not restore the communication between the
58341016Sdfr     * keyboard and the controller.
58441016Sdfr     */
58541016Sdfr    reset_kbd(kbdc);
58641016Sdfr#else
58741016Sdfr    /*
58841016Sdfr     * NOTE: somehow diagnostic and keyboard port test commands bring the
58941016Sdfr     * keyboard back.
59041016Sdfr     */
59141016Sdfr    if (!test_controller(kbdc))
59241016Sdfr        log(LOG_ERR, "psm: keyboard controller failed.\n");
59341016Sdfr    /* if there isn't a keyboard in the system, the following error is OK */
59441016Sdfr    if (test_kbd_port(kbdc) != 0) {
59541016Sdfr	if (verbose)
59641016Sdfr	    log(LOG_ERR, "psm: keyboard port failed.\n");
59741016Sdfr    }
59841016Sdfr#endif
59941016Sdfr}
60041016Sdfr
60141016Sdfrstatic int
60241016Sdfrrestore_controller(KBDC kbdc, int command_byte)
60341016Sdfr{
60441016Sdfr    empty_both_buffers(kbdc, 10);
60541016Sdfr
60641016Sdfr    if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
60741016Sdfr	log(LOG_ERR, "psm: failed to restore the keyboard controller "
60841016Sdfr		     "command byte.\n");
60958230Syokota	empty_both_buffers(kbdc, 10);
61041016Sdfr	return FALSE;
61141016Sdfr    } else {
61258230Syokota	empty_both_buffers(kbdc, 10);
61341016Sdfr	return TRUE;
61441016Sdfr    }
61541016Sdfr}
61641016Sdfr
61741016Sdfr/*
61841016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
61941016Sdfr * and its interrupt must be disabled before calling this routine.
62041016Sdfr * The aux device will be disabled before returning.
62141016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
62241016Sdfr * calling this routine.
62341016Sdfr */
62441016Sdfrstatic int
62541016Sdfrreinitialize(int unit, mousemode_t *mode)
62641016Sdfr{
62741016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
62841016Sdfr    KBDC kbdc = sc->kbdc;
62941016Sdfr    int stat[3];
63041016Sdfr    int i;
63141016Sdfr
63241016Sdfr    switch((i = test_aux_port(kbdc))) {
63341016Sdfr    case 1:	/* ignore this error */
63441016Sdfr    case PSM_ACK:
63541016Sdfr	if (verbose)
63641016Sdfr	    log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
63741016Sdfr	        unit, i);
63841016Sdfr	/* fall though */
63941016Sdfr    case 0:	/* no error */
64041016Sdfr    	break;
64141016Sdfr    case -1: 	/* time out */
64241016Sdfr    default: 	/* error */
64341016Sdfr    	recover_from_error(kbdc);
64445789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
64545789Speter	    break;
64641016Sdfr    	log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
64741016Sdfr    	    unit, i);
64841016Sdfr    	return FALSE;
64941016Sdfr    }
65041016Sdfr
65145789Speter    if (sc->config & PSM_CONFIG_NORESET) {
65245789Speter	/*
65345789Speter	 * Don't try to reset the pointing device.  It may possibly be
65445789Speter	 * left in the unknown state, though...
65545789Speter	 */
65645789Speter    } else {
65745789Speter	/*
65845789Speter	 * NOTE: some controllers appears to hang the `keyboard' when
65945789Speter	 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
66045789Speter	 */
66145789Speter	if (!reset_aux_dev(kbdc)) {
66245789Speter            recover_from_error(kbdc);
66345789Speter            log(LOG_ERR, "psm%d: failed to reset the aux device.\n", unit);
66445789Speter            return FALSE;
66545789Speter	}
66641016Sdfr    }
66741016Sdfr
66841016Sdfr    /*
66941016Sdfr     * both the aux port and the aux device is functioning, see
67041016Sdfr     * if the device can be enabled.
67141016Sdfr     */
67241016Sdfr    if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
67341016Sdfr        log(LOG_ERR, "psm%d: failed to enable the aux device.\n", unit);
67441016Sdfr        return FALSE;
67541016Sdfr    }
67641016Sdfr    empty_both_buffers(kbdc, 10);	/* remove stray data if any */
67741016Sdfr
67845789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
67945789Speter	i = GENERIC_MOUSE_ENTRY;
68045789Speter    } else {
68145789Speter	/* FIXME: hardware ID, mouse buttons? */
68241016Sdfr
68345789Speter	/* other parameters */
68445789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
68545789Speter	    if ((*vendortype[i].probefunc)(sc)) {
68645789Speter		if (verbose >= 2)
68745789Speter		    log(LOG_ERR, "psm%d: found %s\n",
68845789Speter			unit, model_name(vendortype[i].model));
68945789Speter		break;
69045789Speter	    }
69141016Sdfr	}
69241016Sdfr    }
69341016Sdfr
69441016Sdfr    sc->hw.model = vendortype[i].model;
69541016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
69641016Sdfr
69741016Sdfr    /* set mouse parameters */
69841016Sdfr    if (mode != (mousemode_t *)NULL) {
69941016Sdfr	if (mode->rate > 0)
70041016Sdfr            mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
70141016Sdfr	if (mode->resolution >= 0)
70241016Sdfr            mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
70341016Sdfr        set_mouse_scaling(kbdc, 1);
70441016Sdfr        set_mouse_mode(kbdc);
70541016Sdfr    }
70641016Sdfr
70741016Sdfr    /* request a data packet and extract sync. bits */
70841016Sdfr    if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
70941016Sdfr        log(LOG_DEBUG, "psm%d: failed to get data (reinitialize).\n", unit);
71041016Sdfr        sc->mode.syncmask[0] = 0;
71141016Sdfr    } else {
71241016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
71341016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
71441016Sdfr	empty_aux_buffer(kbdc, 5);
71541016Sdfr    }
71641016Sdfr
71741016Sdfr    /* just check the status of the mouse */
71841016Sdfr    if (get_mouse_status(kbdc, stat, 0, 3) < 3)
71941016Sdfr        log(LOG_DEBUG, "psm%d: failed to get status (reinitialize).\n", unit);
72041016Sdfr
72141016Sdfr    return TRUE;
72241016Sdfr}
72341016Sdfr
72441016Sdfrstatic int
72541016Sdfrdoopen(int unit, int command_byte)
72641016Sdfr{
72741016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
72841016Sdfr    int stat[3];
72941016Sdfr
73041016Sdfr    /* enable the mouse device */
73141016Sdfr    if (!enable_aux_dev(sc->kbdc)) {
73241016Sdfr	/* MOUSE ERROR: failed to enable the mouse because:
73341016Sdfr	 * 1) the mouse is faulty,
73441016Sdfr	 * 2) the mouse has been removed(!?)
73541016Sdfr	 * In the latter case, the keyboard may have hung, and need
73641016Sdfr	 * recovery procedure...
73741016Sdfr	 */
73841016Sdfr	recover_from_error(sc->kbdc);
73941016Sdfr#if 0
74041016Sdfr	/* FIXME: we could reset the mouse here and try to enable
74141016Sdfr	 * it again. But it will take long time and it's not a good
74241016Sdfr	 * idea to disable the keyboard that long...
74341016Sdfr	 */
74441016Sdfr	if (!reinitialize(unit, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
74541016Sdfr	    recover_from_error(sc->kbdc);
74641016Sdfr#else
74741016Sdfr        {
74841016Sdfr#endif
74941016Sdfr            restore_controller(sc->kbdc, command_byte);
75041016Sdfr	    /* mark this device is no longer available */
75141016Sdfr	    sc->state &= ~PSM_VALID;
75241016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
75341016Sdfr		unit);
75441016Sdfr	    return (EIO);
75541016Sdfr	}
75641016Sdfr    }
75741016Sdfr
75841016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
75941016Sdfr        log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", unit);
76041016Sdfr
76141016Sdfr    /* enable the aux port and interrupt */
76241016Sdfr    if (!set_controller_command_byte(sc->kbdc,
76341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
76441016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
76541016Sdfr		| KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
76641016Sdfr	/* CONTROLLER ERROR */
76741016Sdfr	disable_aux_dev(sc->kbdc);
76841016Sdfr        restore_controller(sc->kbdc, command_byte);
76941016Sdfr	log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
77041016Sdfr	    unit);
77141016Sdfr	return (EIO);
77241016Sdfr    }
77341016Sdfr
77458230Syokota    /* start the watchdog timer */
77558230Syokota    sc->watchdog = FALSE;
77658230Syokota    sc->callout = timeout(psmtimeout, (void *)unit, hz*2);
77758230Syokota
77841016Sdfr    return (0);
77941016Sdfr}
78041016Sdfr
78141016Sdfr/* psm driver entry points */
78241016Sdfr
78341016Sdfr#define endprobe(v)	{   if (bootverbose) 				\
78441016Sdfr				--verbose;   				\
78541016Sdfr                            kbdc_set_device_mask(sc->kbdc, mask);	\
78641016Sdfr			    kbdc_lock(sc->kbdc, FALSE);			\
78741016Sdfr			    return (v);	     				\
78841016Sdfr			}
78941016Sdfr
79041016Sdfrstatic int
79141016Sdfrpsmprobe(device_t dev)
79241016Sdfr{
79341016Sdfr    int unit = device_get_unit(dev);
79441016Sdfr    struct psm_softc *sc = device_get_softc(dev);
79558230Syokota    uintptr_t irq;
79645720Speter    uintptr_t flags;
79741016Sdfr    int stat[3];
79841016Sdfr    int command_byte;
79941016Sdfr    int mask;
80058230Syokota    int rid;
80141016Sdfr    int i;
80241016Sdfr
80341016Sdfr#if 0
80441016Sdfr    kbdc_debug(TRUE);
80541016Sdfr#endif
80658230Syokota
80758230Syokota#if notyet
80858230Syokota    /* check PnP IDs */
80958230Syokota    if (XXX_PNP_PROBE(device_get_parent(dev), dev, psm_ids) == ENXIO)
81058230Syokota	return ENXIO;
81158230Syokota#endif
81258230Syokota
81358230Syokota    BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
81443105Sdfr    BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
81543105Sdfr
81658230Syokota    sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
81743105Sdfr    sc->config = flags & PSM_CONFIG_FLAGS;
81858230Syokota    /* XXX: for backward compatibility */
81958230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
82058230Syokota    sc->config |=
82158230Syokota#ifdef PSM_RESETAFTERSUSPEND
82258230Syokota	PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
82358230Syokota#else
82458230Syokota	PSM_CONFIG_HOOKRESUME;
82558230Syokota#endif
82658230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
82741016Sdfr    sc->flags = 0;
82841016Sdfr    if (bootverbose)
82941016Sdfr        ++verbose;
83041016Sdfr
83143105Sdfr    device_set_desc(dev, "PS/2 Mouse");
83243105Sdfr
83341016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE)) {
83441016Sdfr        printf("psm%d: unable to lock the controller.\n", unit);
83541016Sdfr        if (bootverbose)
83641016Sdfr            --verbose;
83741016Sdfr	return (ENXIO);
83841016Sdfr    }
83941016Sdfr
84041016Sdfr    /*
84141016Sdfr     * NOTE: two bits in the command byte controls the operation of the
84241016Sdfr     * aux port (mouse port): the aux port disable bit (bit 5) and the aux
84341016Sdfr     * port interrupt (IRQ 12) enable bit (bit 2).
84441016Sdfr     */
84541016Sdfr
84641016Sdfr    /* discard anything left after the keyboard initialization */
84741016Sdfr    empty_both_buffers(sc->kbdc, 10);
84841016Sdfr
84941016Sdfr    /* save the current command byte; it will be used later */
85041016Sdfr    mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
85141016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
85241016Sdfr    if (verbose)
85341016Sdfr        printf("psm%d: current command byte:%04x\n", unit, command_byte);
85441016Sdfr    if (command_byte == -1) {
85541016Sdfr        /* CONTROLLER ERROR */
85641016Sdfr        printf("psm%d: unable to get the current command byte value.\n",
85741016Sdfr            unit);
85841016Sdfr        endprobe(ENXIO);
85941016Sdfr    }
86041016Sdfr
86141016Sdfr    /*
86241016Sdfr     * disable the keyboard port while probing the aux port, which must be
86341016Sdfr     * enabled during this routine
86441016Sdfr     */
86541016Sdfr    if (!set_controller_command_byte(sc->kbdc,
86641016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
86741016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
86841016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
86941016Sdfr        /*
87041016Sdfr	 * this is CONTROLLER ERROR; I don't know how to recover
87141016Sdfr         * from this error...
87241016Sdfr	 */
87341016Sdfr        restore_controller(sc->kbdc, command_byte);
87441016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
87541016Sdfr        endprobe(ENXIO);
87641016Sdfr    }
87745789Speter    write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
87841016Sdfr
87941016Sdfr    /*
88041016Sdfr     * NOTE: `test_aux_port()' is designed to return with zero if the aux
88141016Sdfr     * port exists and is functioning. However, some controllers appears
88241016Sdfr     * to respond with zero even when the aux port doesn't exist. (It may
88341016Sdfr     * be that this is only the case when the controller DOES have the aux
88441016Sdfr     * port but the port is not wired on the motherboard.) The keyboard
88541016Sdfr     * controllers without the port, such as the original AT, are
88641016Sdfr     * supporsed to return with an error code or simply time out. In any
88741016Sdfr     * case, we have to continue probing the port even when the controller
88841016Sdfr     * passes this test.
88941016Sdfr     *
89041016Sdfr     * XXX: some controllers erroneously return the error code 1 when
89141016Sdfr     * it has the perfectly functional aux port. We have to ignore this
89241016Sdfr     * error code. Even if the controller HAS error with the aux port,
89341016Sdfr     * it will be detected later...
89441016Sdfr     * XXX: another incompatible controller returns PSM_ACK (0xfa)...
89541016Sdfr     */
89641016Sdfr    switch ((i = test_aux_port(sc->kbdc))) {
89741016Sdfr    case 1:	   /* ignore this error */
89841016Sdfr    case PSM_ACK:
89941016Sdfr        if (verbose)
90041016Sdfr	    printf("psm%d: strange result for test aux port (%d).\n",
90141016Sdfr	        unit, i);
90241016Sdfr	/* fall though */
90341016Sdfr    case 0:        /* no error */
90441016Sdfr        break;
90541016Sdfr    case -1:        /* time out */
90641016Sdfr    default:        /* error */
90741016Sdfr        recover_from_error(sc->kbdc);
90845789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
90945789Speter	    break;
91041016Sdfr        restore_controller(sc->kbdc, command_byte);
91141016Sdfr        if (verbose)
91241016Sdfr            printf("psm%d: the aux port is not functioning (%d).\n",
91341016Sdfr                unit, i);
91441016Sdfr        endprobe(ENXIO);
91541016Sdfr    }
91641016Sdfr
91745789Speter    if (sc->config & PSM_CONFIG_NORESET) {
91845789Speter	/*
91945789Speter	 * Don't try to reset the pointing device.  It may possibly be
92045789Speter	 * left in the unknown state, though...
92145789Speter	 */
92245789Speter    } else {
92345789Speter	/*
92445789Speter	 * NOTE: some controllers appears to hang the `keyboard' when the aux
92545789Speter	 * port doesn't exist and `PSMC_RESET_DEV' is issued.
92645789Speter	 */
92745789Speter	if (!reset_aux_dev(sc->kbdc)) {
92845789Speter            recover_from_error(sc->kbdc);
92945789Speter            restore_controller(sc->kbdc, command_byte);
93045789Speter            if (verbose)
93145789Speter        	printf("psm%d: failed to reset the aux device.\n", unit);
93245789Speter            endprobe(ENXIO);
93345789Speter	}
93441016Sdfr    }
93545789Speter
93641016Sdfr    /*
93741016Sdfr     * both the aux port and the aux device is functioning, see if the
93841016Sdfr     * device can be enabled. NOTE: when enabled, the device will start
93941016Sdfr     * sending data; we shall immediately disable the device once we know
94041016Sdfr     * the device can be enabled.
94141016Sdfr     */
94241016Sdfr    if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
94345789Speter        /* MOUSE ERROR */
94445789Speter	recover_from_error(sc->kbdc);
94545789Speter	restore_controller(sc->kbdc, command_byte);
94645789Speter	if (verbose)
94745789Speter	    printf("psm%d: failed to enable the aux device.\n", unit);
94841016Sdfr        endprobe(ENXIO);
94941016Sdfr    }
95041016Sdfr
95141016Sdfr    /* save the default values after reset */
95241016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
95341016Sdfr	sc->dflt_mode.rate = sc->mode.rate = stat[2];
95441016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
95541016Sdfr    } else {
95641016Sdfr	sc->dflt_mode.rate = sc->mode.rate = -1;
95741016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = -1;
95841016Sdfr    }
95941016Sdfr
96041016Sdfr    /* hardware information */
96141016Sdfr    sc->hw.iftype = MOUSE_IF_PS2;
96241016Sdfr
96341016Sdfr    /* verify the device is a mouse */
96441016Sdfr    sc->hw.hwid = get_aux_id(sc->kbdc);
96541016Sdfr    if (!is_a_mouse(sc->hw.hwid)) {
96641016Sdfr        restore_controller(sc->kbdc, command_byte);
96741016Sdfr        if (verbose)
96841016Sdfr            printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
96941016Sdfr        endprobe(ENXIO);
97041016Sdfr    }
97141016Sdfr    switch (sc->hw.hwid) {
97241016Sdfr    case PSM_BALLPOINT_ID:
97341016Sdfr        sc->hw.type = MOUSE_TRACKBALL;
97441016Sdfr        break;
97541016Sdfr    case PSM_MOUSE_ID:
97641016Sdfr    case PSM_INTELLI_ID:
97758230Syokota    case PSM_EXPLORER_ID:
97858230Syokota    case PSM_4DMOUSE_ID:
97958230Syokota    case PSM_4DPLUS_ID:
98041016Sdfr        sc->hw.type = MOUSE_MOUSE;
98141016Sdfr        break;
98241016Sdfr    default:
98341016Sdfr        sc->hw.type = MOUSE_UNKNOWN;
98441016Sdfr        break;
98541016Sdfr    }
98641016Sdfr
98745789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
98845789Speter	sc->hw.buttons = 2;
98945789Speter	i = GENERIC_MOUSE_ENTRY;
99045789Speter    } else {
99145789Speter	/* # of buttons */
99245789Speter	sc->hw.buttons = get_mouse_buttons(sc->kbdc);
99341016Sdfr
99445789Speter	/* other parameters */
99545789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
99645789Speter	    if ((*vendortype[i].probefunc)(sc)) {
99745789Speter		if (verbose >= 2)
99845789Speter		    printf("psm%d: found %s\n",
99945789Speter			   unit, model_name(vendortype[i].model));
100045789Speter		break;
100145789Speter	    }
100241016Sdfr	}
100341016Sdfr    }
100441016Sdfr
100541016Sdfr    sc->hw.model = vendortype[i].model;
100641016Sdfr
100741016Sdfr    sc->dflt_mode.level = PSM_LEVEL_BASE;
100841016Sdfr    sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
100941016Sdfr    sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
101041016Sdfr    if (sc->config & PSM_CONFIG_NOCHECKSYNC)
101141016Sdfr        sc->dflt_mode.syncmask[0] = 0;
101241016Sdfr    else
101341016Sdfr        sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
101445789Speter    if (sc->config & PSM_CONFIG_FORCETAP)
101545789Speter        sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
101641016Sdfr    sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
101741016Sdfr    sc->mode = sc->dflt_mode;
101841016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
101941016Sdfr
102041016Sdfr    /* set mouse parameters */
102148773Syokota#if 0
102248773Syokota    /*
102348773Syokota     * A version of Logitech FirstMouse+ won't report wheel movement,
102448773Syokota     * if SET_DEFAULTS is sent...  Don't use this command.
102548773Syokota     * This fix was found by Takashi Nishida.
102648773Syokota     */
102741016Sdfr    i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
102841016Sdfr    if (verbose >= 2)
102941016Sdfr	printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
103048773Syokota#endif
103141016Sdfr    if (sc->config & PSM_CONFIG_RESOLUTION) {
103241016Sdfr        sc->mode.resolution
103341016Sdfr	    = set_mouse_resolution(sc->kbdc,
103448773Syokota				   (sc->config & PSM_CONFIG_RESOLUTION) - 1);
103548773Syokota    } else if (sc->mode.resolution >= 0) {
103648773Syokota	sc->mode.resolution
103748773Syokota	    = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
103841016Sdfr    }
103948773Syokota    if (sc->mode.rate > 0) {
104048773Syokota	sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
104148773Syokota    }
104248773Syokota    set_mouse_scaling(sc->kbdc, 1);
104341016Sdfr
104441016Sdfr    /* request a data packet and extract sync. bits */
104541016Sdfr    if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
104641016Sdfr        printf("psm%d: failed to get data.\n", unit);
104741016Sdfr        sc->mode.syncmask[0] = 0;
104841016Sdfr    } else {
104941016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
105041016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
105141016Sdfr	empty_aux_buffer(sc->kbdc, 5);
105241016Sdfr    }
105341016Sdfr
105441016Sdfr    /* just check the status of the mouse */
105541016Sdfr    /*
105641016Sdfr     * NOTE: XXX there are some arcane controller/mouse combinations out
105741016Sdfr     * there, which hung the controller unless there is data transmission
105841016Sdfr     * after ACK from the mouse.
105941016Sdfr     */
106041016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
106141016Sdfr        printf("psm%d: failed to get status.\n", unit);
106241016Sdfr    } else {
106341016Sdfr	/*
106441016Sdfr	 * When in its native mode, some mice operate with different
106541016Sdfr	 * default parameters than in the PS/2 compatible mode.
106641016Sdfr	 */
106741016Sdfr        sc->dflt_mode.rate = sc->mode.rate = stat[2];
106841016Sdfr        sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
106941016Sdfr     }
107041016Sdfr
107141016Sdfr    /* disable the aux port for now... */
107241016Sdfr    if (!set_controller_command_byte(sc->kbdc,
107341016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
107441016Sdfr            (command_byte & KBD_KBD_CONTROL_BITS)
107541016Sdfr                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
107641016Sdfr        /*
107741016Sdfr	 * this is CONTROLLER ERROR; I don't know the proper way to
107841016Sdfr         * recover from this error...
107941016Sdfr	 */
108041016Sdfr        restore_controller(sc->kbdc, command_byte);
108141016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
108241016Sdfr        endprobe(ENXIO);
108341016Sdfr    }
108441016Sdfr
108558230Syokota    /* see if IRQ is available */
108658230Syokota    rid = 0;
108758230Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
108858230Syokota				  RF_ACTIVE);
108958230Syokota    if (sc->intr == NULL) {
109058230Syokota        printf("psm%d: unable to allocate the IRQ resource (%d).\n",
109158230Syokota	       unit, irq);
109258230Syokota        endprobe(ENXIO);
109358230Syokota    } else {
109458230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
109558230Syokota    }
109658230Syokota
109741016Sdfr    /* done */
109841016Sdfr    kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
109941016Sdfr    kbdc_lock(sc->kbdc, FALSE);
110041016Sdfr    return (0);
110141016Sdfr}
110241016Sdfr
110341016Sdfrstatic int
110441016Sdfrpsmattach(device_t dev)
110541016Sdfr{
110641016Sdfr    int unit = device_get_unit(dev);
110741016Sdfr    struct psm_softc *sc = device_get_softc(dev);
110845720Speter    uintptr_t irq;
110958230Syokota    int error;
111058230Syokota    int rid;
111141016Sdfr
111241016Sdfr    if (sc == NULL)    /* shouldn't happen */
111341016Sdfr	return (ENXIO);
111441016Sdfr
111541016Sdfr    /* Setup initial state */
111641016Sdfr    sc->state = PSM_VALID;
111758230Syokota    callout_handle_init(&sc->callout);
111841016Sdfr
111958230Syokota    /* Setup our interrupt handler */
112058230Syokota    rid = 0;
112158230Syokota    BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
112258230Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
112358230Syokota				  RF_ACTIVE);
112458230Syokota    if (sc->intr == NULL)
112558230Syokota	return (ENXIO);
112658230Syokota    error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
112758230Syokota			   INTR_TYPE_TTY, psmintr, sc, &sc->ih);
112858230Syokota    if (error) {
112958230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
113058230Syokota	return (error);
113158230Syokota    }
113258230Syokota
113341016Sdfr    /* Done */
113458230Syokota    sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
113558230Syokota		       "psm%d", unit);
113658230Syokota    sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
113758230Syokota			"bpsm%d", unit);
113841016Sdfr
113941016Sdfr    if (!verbose) {
114041016Sdfr        printf("psm%d: model %s, device ID %d\n",
114148778Syokota	    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
114241016Sdfr    } else {
114348778Syokota        printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
114448778Syokota	    unit, model_name(sc->hw.model),
114548778Syokota	    sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
114641016Sdfr	printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
114741016Sdfr	    unit, sc->config, sc->flags, sc->mode.packetsize);
114841016Sdfr	printf("psm%d: syncmask:%02x, syncbits:%02x\n",
114941016Sdfr	    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
115041016Sdfr    }
115141016Sdfr
115241016Sdfr    if (bootverbose)
115341016Sdfr        --verbose;
115441016Sdfr
115541016Sdfr    return (0);
115641016Sdfr}
115741016Sdfr
115841016Sdfrstatic int
115958230Syokotapsmdetach(device_t dev)
116058230Syokota{
116158230Syokota    struct psm_softc *sc;
116258230Syokota    int rid;
116358230Syokota
116458230Syokota    sc = device_get_softc(dev);
116558230Syokota    if (sc->state & PSM_OPEN)
116658230Syokota	return EBUSY;
116758230Syokota
116858230Syokota    rid = 0;
116958230Syokota    BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih);
117058230Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
117158230Syokota
117258230Syokota    destroy_dev(sc->dev);
117358230Syokota    destroy_dev(sc->bdev);
117458230Syokota
117558230Syokota    return 0;
117658230Syokota}
117758230Syokota
117858230Syokotastatic int
117941016Sdfrpsmopen(dev_t dev, int flag, int fmt, struct proc *p)
118041016Sdfr{
118141016Sdfr    int unit = PSM_UNIT(dev);
118241016Sdfr    struct psm_softc *sc;
118341016Sdfr    int command_byte;
118441016Sdfr    int err;
118541016Sdfr    int s;
118641016Sdfr
118741016Sdfr    /* Get device data */
118841016Sdfr    sc = PSM_SOFTC(unit);
118941016Sdfr    if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
119041016Sdfr	/* the device is no longer valid/functioning */
119141016Sdfr        return (ENXIO);
119241016Sdfr
119341016Sdfr    /* Disallow multiple opens */
119441016Sdfr    if (sc->state & PSM_OPEN)
119541016Sdfr        return (EBUSY);
119641016Sdfr
119741016Sdfr    device_busy(devclass_get_device(psm_devclass, unit));
119841016Sdfr
119941016Sdfr    /* Initialize state */
120041016Sdfr    sc->rsel.si_flags = 0;
120141016Sdfr    sc->rsel.si_pid = 0;
120241016Sdfr    sc->mode.level = sc->dflt_mode.level;
120341016Sdfr    sc->mode.protocol = sc->dflt_mode.protocol;
120458230Syokota    sc->watchdog = FALSE;
120541016Sdfr
120641016Sdfr    /* flush the event queue */
120741016Sdfr    sc->queue.count = 0;
120841016Sdfr    sc->queue.head = 0;
120941016Sdfr    sc->queue.tail = 0;
121041016Sdfr    sc->status.flags = 0;
121141016Sdfr    sc->status.button = 0;
121241016Sdfr    sc->status.obutton = 0;
121341016Sdfr    sc->status.dx = 0;
121441016Sdfr    sc->status.dy = 0;
121541016Sdfr    sc->status.dz = 0;
121641016Sdfr    sc->button = 0;
121741016Sdfr
121841016Sdfr    /* empty input buffer */
121941016Sdfr    bzero(sc->ipacket, sizeof(sc->ipacket));
122041016Sdfr    sc->inputbytes = 0;
122141016Sdfr
122241016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
122341016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
122441016Sdfr	return (EIO);
122541016Sdfr
122641016Sdfr    /* save the current controller command byte */
122741016Sdfr    s = spltty();
122841016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
122941016Sdfr
123041016Sdfr    /* enable the aux port and temporalily disable the keyboard */
123141016Sdfr    if ((command_byte == -1)
123241016Sdfr        || !set_controller_command_byte(sc->kbdc,
123341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
123441016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
123541016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
123641016Sdfr        /* CONTROLLER ERROR; do you know how to get out of this? */
123741016Sdfr        kbdc_lock(sc->kbdc, FALSE);
123841016Sdfr	splx(s);
123941016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
124041016Sdfr	    unit);
124141016Sdfr	return (EIO);
124241016Sdfr    }
124341016Sdfr    /*
124441016Sdfr     * Now that the keyboard controller is told not to generate
124541016Sdfr     * the keyboard and mouse interrupts, call `splx()' to allow
124641016Sdfr     * the other tty interrupts. The clock interrupt may also occur,
124741016Sdfr     * but timeout routines will be blocked by the poll flag set
124841016Sdfr     * via `kbdc_lock()'
124941016Sdfr     */
125041016Sdfr    splx(s);
125141016Sdfr
125241016Sdfr    /* enable the mouse device */
125341016Sdfr    err = doopen(unit, command_byte);
125441016Sdfr
125541016Sdfr    /* done */
125641016Sdfr    if (err == 0)
125741016Sdfr        sc->state |= PSM_OPEN;
125841016Sdfr    kbdc_lock(sc->kbdc, FALSE);
125941016Sdfr    return (err);
126041016Sdfr}
126141016Sdfr
126241016Sdfrstatic int
126341016Sdfrpsmclose(dev_t dev, int flag, int fmt, struct proc *p)
126441016Sdfr{
126541016Sdfr    int unit = PSM_UNIT(dev);
126641016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
126741016Sdfr    int stat[3];
126841016Sdfr    int command_byte;
126941016Sdfr    int s;
127041016Sdfr
127141016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
127241016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
127341016Sdfr	return (EIO);
127441016Sdfr
127541016Sdfr    /* save the current controller command byte */
127641016Sdfr    s = spltty();
127741016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
127841016Sdfr    if (command_byte == -1) {
127941016Sdfr        kbdc_lock(sc->kbdc, FALSE);
128041016Sdfr	splx(s);
128141016Sdfr	return (EIO);
128241016Sdfr    }
128341016Sdfr
128441016Sdfr    /* disable the aux interrupt and temporalily disable the keyboard */
128541016Sdfr    if (!set_controller_command_byte(sc->kbdc,
128641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
128741016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
128841016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
128941016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
129058230Syokota	    unit);
129141016Sdfr	/* CONTROLLER ERROR;
129241016Sdfr	 * NOTE: we shall force our way through. Because the only
129341016Sdfr	 * ill effect we shall see is that we may not be able
129441016Sdfr	 * to read ACK from the mouse, and it doesn't matter much
129541016Sdfr	 * so long as the mouse will accept the DISABLE command.
129641016Sdfr	 */
129741016Sdfr    }
129841016Sdfr    splx(s);
129941016Sdfr
130058230Syokota    /* stop the watchdog timer */
130158230Syokota    untimeout(psmtimeout, (void *)unit, sc->callout);
130258230Syokota    callout_handle_init(&sc->callout);
130358230Syokota
130441016Sdfr    /* remove anything left in the output buffer */
130541016Sdfr    empty_aux_buffer(sc->kbdc, 10);
130641016Sdfr
130741016Sdfr    /* disable the aux device, port and interrupt */
130841016Sdfr    if (sc->state & PSM_VALID) {
130941016Sdfr        if (!disable_aux_dev(sc->kbdc)) {
131041016Sdfr	    /* MOUSE ERROR;
131141016Sdfr	     * NOTE: we don't return error and continue, pretending
131241016Sdfr	     * we have successfully disabled the device. It's OK because
131341016Sdfr	     * the interrupt routine will discard any data from the mouse
131441016Sdfr	     * hereafter.
131541016Sdfr	     */
131641016Sdfr	    log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
131758230Syokota		unit);
131841016Sdfr        }
131941016Sdfr
132041016Sdfr        if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
132141016Sdfr            log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
132258230Syokota		unit);
132341016Sdfr    }
132441016Sdfr
132541016Sdfr    if (!set_controller_command_byte(sc->kbdc,
132641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
132741016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
132841016Sdfr	        | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
132941016Sdfr	/* CONTROLLER ERROR;
133041016Sdfr	 * we shall ignore this error; see the above comment.
133141016Sdfr	 */
133241016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
133358230Syokota	    unit);
133441016Sdfr    }
133541016Sdfr
133641016Sdfr    /* remove anything left in the output buffer */
133741016Sdfr    empty_aux_buffer(sc->kbdc, 10);
133841016Sdfr
133941016Sdfr    /* close is almost always successful */
134041016Sdfr    sc->state &= ~PSM_OPEN;
134141016Sdfr    kbdc_lock(sc->kbdc, FALSE);
134241016Sdfr    device_unbusy(devclass_get_device(psm_devclass, unit));
134341016Sdfr    return (0);
134441016Sdfr}
134541016Sdfr
134641016Sdfrstatic int
134741016Sdfrtame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
134841016Sdfr{
134941016Sdfr    static unsigned char butmapps2[8] = {
135041016Sdfr        0,
135141016Sdfr        MOUSE_PS2_BUTTON1DOWN,
135241016Sdfr        MOUSE_PS2_BUTTON2DOWN,
135341016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
135441016Sdfr        MOUSE_PS2_BUTTON3DOWN,
135541016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
135641016Sdfr        MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
135741016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
135841016Sdfr    };
135941016Sdfr    static unsigned char butmapmsc[8] = {
136041016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
136141016Sdfr        MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
136241016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
136341016Sdfr        MOUSE_MSC_BUTTON3UP,
136441016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
136541016Sdfr        MOUSE_MSC_BUTTON2UP,
136641016Sdfr        MOUSE_MSC_BUTTON1UP,
136741016Sdfr        0,
136841016Sdfr    };
136941016Sdfr    int mapped;
137041016Sdfr    int i;
137141016Sdfr
137241016Sdfr    if (sc->mode.level == PSM_LEVEL_BASE) {
137341016Sdfr        mapped = status->button & ~MOUSE_BUTTON4DOWN;
137441016Sdfr        if (status->button & MOUSE_BUTTON4DOWN)
137541016Sdfr	    mapped |= MOUSE_BUTTON1DOWN;
137641016Sdfr        status->button = mapped;
137741016Sdfr        buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
137841016Sdfr        i = max(min(status->dx, 255), -256);
137941016Sdfr	if (i < 0)
138041016Sdfr	    buf[0] |= MOUSE_PS2_XNEG;
138141016Sdfr        buf[1] = i;
138241016Sdfr        i = max(min(status->dy, 255), -256);
138341016Sdfr	if (i < 0)
138441016Sdfr	    buf[0] |= MOUSE_PS2_YNEG;
138541016Sdfr        buf[2] = i;
138641016Sdfr	return MOUSE_PS2_PACKETSIZE;
138741016Sdfr    } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
138841016Sdfr        buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
138941016Sdfr        i = max(min(status->dx, 255), -256);
139041016Sdfr        buf[1] = i >> 1;
139141016Sdfr        buf[3] = i - buf[1];
139241016Sdfr        i = max(min(status->dy, 255), -256);
139341016Sdfr        buf[2] = i >> 1;
139441016Sdfr        buf[4] = i - buf[2];
139541016Sdfr        i = max(min(status->dz, 127), -128);
139641016Sdfr        buf[5] = (i >> 1) & 0x7f;
139741016Sdfr        buf[6] = (i - (i >> 1)) & 0x7f;
139841016Sdfr        buf[7] = (~status->button >> 3) & 0x7f;
139941016Sdfr	return MOUSE_SYS_PACKETSIZE;
140041016Sdfr    }
140141016Sdfr    return sc->inputbytes;;
140241016Sdfr}
140341016Sdfr
140441016Sdfrstatic int
140541016Sdfrpsmread(dev_t dev, struct uio *uio, int flag)
140641016Sdfr{
140741016Sdfr    register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
140841016Sdfr    unsigned char buf[PSM_SMALLBUFSIZE];
140941016Sdfr    int error = 0;
141041016Sdfr    int s;
141141016Sdfr    int l;
141241016Sdfr
141341016Sdfr    if ((sc->state & PSM_VALID) == 0)
141441016Sdfr	return EIO;
141541016Sdfr
141641016Sdfr    /* block until mouse activity occured */
141741016Sdfr    s = spltty();
141841016Sdfr    while (sc->queue.count <= 0) {
141941016Sdfr        if (PSM_NBLOCKIO(dev)) {
142041016Sdfr            splx(s);
142141016Sdfr            return EWOULDBLOCK;
142241016Sdfr        }
142341016Sdfr        sc->state |= PSM_ASLP;
142441016Sdfr        error = tsleep((caddr_t) sc, PZERO | PCATCH, "psmrea", 0);
142541016Sdfr        sc->state &= ~PSM_ASLP;
142641016Sdfr        if (error) {
142741016Sdfr            splx(s);
142841016Sdfr            return error;
142941016Sdfr        } else if ((sc->state & PSM_VALID) == 0) {
143041016Sdfr            /* the device disappeared! */
143141016Sdfr            splx(s);
143241016Sdfr            return EIO;
143341016Sdfr	}
143441016Sdfr    }
143541016Sdfr    splx(s);
143641016Sdfr
143741016Sdfr    /* copy data to the user land */
143841016Sdfr    while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
143941016Sdfr        s = spltty();
144041016Sdfr	l = min(sc->queue.count, uio->uio_resid);
144141016Sdfr	if (l > sizeof(buf))
144241016Sdfr	    l = sizeof(buf);
144341016Sdfr	if (l > sizeof(sc->queue.buf) - sc->queue.head) {
144441016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
144541016Sdfr		sizeof(sc->queue.buf) - sc->queue.head);
144641016Sdfr	    bcopy(&sc->queue.buf[0],
144741016Sdfr		&buf[sizeof(sc->queue.buf) - sc->queue.head],
144841016Sdfr		l - (sizeof(sc->queue.buf) - sc->queue.head));
144941016Sdfr	} else {
145041016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
145141016Sdfr	}
145241016Sdfr	sc->queue.count -= l;
145341016Sdfr	sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
145441016Sdfr        splx(s);
145541016Sdfr        error = uiomove(buf, l, uio);
145641016Sdfr        if (error)
145741016Sdfr	    break;
145841016Sdfr    }
145941016Sdfr
146041016Sdfr    return error;
146141016Sdfr}
146241016Sdfr
146341016Sdfrstatic int
146441016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
146541016Sdfr{
146641016Sdfr    int s;
146741016Sdfr
146841016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
146941016Sdfr	return EIO;
147041016Sdfr
147141016Sdfr    s = spltty();
147241016Sdfr    *c = get_controller_command_byte(sc->kbdc);
147341016Sdfr    if ((*c == -1)
147441016Sdfr	|| !set_controller_command_byte(sc->kbdc,
147541016Sdfr	    kbdc_get_device_mask(sc->kbdc),
147641016Sdfr            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
147741016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
147841016Sdfr        /* this is CONTROLLER ERROR */
147941016Sdfr	splx(s);
148041016Sdfr        kbdc_lock(sc->kbdc, FALSE);
148141016Sdfr	return EIO;
148241016Sdfr    }
148341016Sdfr
148441016Sdfr    /*
148541016Sdfr     * The device may be in the middle of status data transmission.
148641016Sdfr     * The transmission will be interrupted, thus, incomplete status
148741016Sdfr     * data must be discarded. Although the aux interrupt is disabled
148841016Sdfr     * at the keyboard controller level, at most one aux interrupt
148941016Sdfr     * may have already been pending and a data byte is in the
149041016Sdfr     * output buffer; throw it away. Note that the second argument
149141016Sdfr     * to `empty_aux_buffer()' is zero, so that the call will just
149241016Sdfr     * flush the internal queue.
149341016Sdfr     * `psmintr()' will be invoked after `splx()' if an interrupt is
149441016Sdfr     * pending; it will see no data and returns immediately.
149541016Sdfr     */
149641016Sdfr    empty_aux_buffer(sc->kbdc, 0);	/* flush the queue */
149741016Sdfr    read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
149841016Sdfr    sc->inputbytes = 0;
149941016Sdfr    splx(s);
150041016Sdfr
150141016Sdfr    return 0;
150241016Sdfr}
150341016Sdfr
150441016Sdfrstatic int
150541016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
150641016Sdfr{
150741016Sdfr    int error = 0;
150841016Sdfr
150941016Sdfr    /*
151041016Sdfr     * We may have seen a part of status data during `set_mouse_XXX()'.
151141016Sdfr     * they have been queued; flush it.
151241016Sdfr     */
151341016Sdfr    empty_aux_buffer(sc->kbdc, 0);
151441016Sdfr
151541016Sdfr    /* restore ports and interrupt */
151641016Sdfr    if (!set_controller_command_byte(sc->kbdc,
151741016Sdfr            kbdc_get_device_mask(sc->kbdc),
151841016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
151941016Sdfr        /* CONTROLLER ERROR; this is serious, we may have
152041016Sdfr         * been left with the inaccessible keyboard and
152141016Sdfr         * the disabled mouse interrupt.
152241016Sdfr         */
152341016Sdfr        error = EIO;
152441016Sdfr    }
152541016Sdfr
152641016Sdfr    kbdc_lock(sc->kbdc, FALSE);
152741016Sdfr    return error;
152841016Sdfr}
152941016Sdfr
153041016Sdfrstatic int
153141016Sdfrpsmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
153241016Sdfr{
153341016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
153441016Sdfr    mousemode_t mode;
153541016Sdfr    mousestatus_t status;
153641016Sdfr#if (defined(MOUSE_GETVARS))
153741016Sdfr    mousevar_t *var;
153841016Sdfr#endif
153941016Sdfr    mousedata_t *data;
154041016Sdfr    int stat[3];
154141016Sdfr    int command_byte;
154241016Sdfr    int error = 0;
154341016Sdfr    int s;
154441016Sdfr
154541016Sdfr    /* Perform IOCTL command */
154641016Sdfr    switch (cmd) {
154741016Sdfr
154841016Sdfr    case OLD_MOUSE_GETHWINFO:
154941016Sdfr	s = spltty();
155041016Sdfr        ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
155141016Sdfr        ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
155241016Sdfr        ((old_mousehw_t *)addr)->type = sc->hw.type;
155348778Syokota        ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
155441016Sdfr	splx(s);
155541016Sdfr        break;
155641016Sdfr
155741016Sdfr    case MOUSE_GETHWINFO:
155841016Sdfr	s = spltty();
155941016Sdfr        *(mousehw_t *)addr = sc->hw;
156041016Sdfr	if (sc->mode.level == PSM_LEVEL_BASE)
156141016Sdfr	    ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
156241016Sdfr	splx(s);
156341016Sdfr        break;
156441016Sdfr
156541016Sdfr    case OLD_MOUSE_GETMODE:
156641016Sdfr	s = spltty();
156741016Sdfr	switch (sc->mode.level) {
156841016Sdfr	case PSM_LEVEL_BASE:
156941016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
157041016Sdfr	    break;
157141016Sdfr	case PSM_LEVEL_STANDARD:
157241016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
157341016Sdfr	    break;
157441016Sdfr	case PSM_LEVEL_NATIVE:
157541016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
157641016Sdfr	    break;
157741016Sdfr	}
157841016Sdfr        ((old_mousemode_t *)addr)->rate = sc->mode.rate;
157941016Sdfr        ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
158041016Sdfr        ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
158141016Sdfr	splx(s);
158241016Sdfr        break;
158341016Sdfr
158441016Sdfr    case MOUSE_GETMODE:
158541016Sdfr	s = spltty();
158641016Sdfr        *(mousemode_t *)addr = sc->mode;
158741016Sdfr        ((mousemode_t *)addr)->resolution =
158841016Sdfr	    MOUSE_RES_LOW - sc->mode.resolution;
158941016Sdfr	switch (sc->mode.level) {
159041016Sdfr	case PSM_LEVEL_BASE:
159141016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
159241016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
159341016Sdfr	    break;
159441016Sdfr	case PSM_LEVEL_STANDARD:
159541016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
159641016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
159741016Sdfr	    ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
159841016Sdfr	    ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
159941016Sdfr	    break;
160041016Sdfr	case PSM_LEVEL_NATIVE:
160141016Sdfr	    /* FIXME: this isn't quite correct... XXX */
160241016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
160341016Sdfr	    break;
160441016Sdfr	}
160541016Sdfr	splx(s);
160641016Sdfr        break;
160741016Sdfr
160841016Sdfr    case OLD_MOUSE_SETMODE:
160941016Sdfr    case MOUSE_SETMODE:
161041016Sdfr	if (cmd == OLD_MOUSE_SETMODE) {
161141016Sdfr	    mode.rate = ((old_mousemode_t *)addr)->rate;
161241016Sdfr	    /*
161341016Sdfr	     * resolution  old I/F   new I/F
161441016Sdfr	     * default        0         0
161541016Sdfr	     * low            1        -2
161641016Sdfr	     * medium low     2        -3
161741016Sdfr	     * medium high    3        -4
161841016Sdfr	     * high           4        -5
161941016Sdfr	     */
162041016Sdfr	    if (((old_mousemode_t *)addr)->resolution > 0)
162141016Sdfr	        mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
162241016Sdfr	    mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
162341016Sdfr	    mode.level = -1;
162441016Sdfr	} else {
162541016Sdfr	    mode = *(mousemode_t *)addr;
162641016Sdfr	}
162741016Sdfr
162841016Sdfr	/* adjust and validate parameters. */
162941016Sdfr	if (mode.rate > UCHAR_MAX)
163041016Sdfr	    return EINVAL;
163141016Sdfr        if (mode.rate == 0)
163241016Sdfr            mode.rate = sc->dflt_mode.rate;
163341016Sdfr	else if (mode.rate == -1)
163441016Sdfr	    /* don't change the current setting */
163541016Sdfr	    ;
163641016Sdfr	else if (mode.rate < 0)
163741016Sdfr	    return EINVAL;
163841016Sdfr	if (mode.resolution >= UCHAR_MAX)
163941016Sdfr	    return EINVAL;
164041016Sdfr	if (mode.resolution >= 200)
164141016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
164241016Sdfr	else if (mode.resolution >= 100)
164341016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
164441016Sdfr	else if (mode.resolution >= 50)
164541016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
164641016Sdfr	else if (mode.resolution > 0)
164741016Sdfr	    mode.resolution = MOUSE_RES_LOW;
164841016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
164941016Sdfr            mode.resolution = sc->dflt_mode.resolution;
165041016Sdfr        else if (mode.resolution == -1)
165141016Sdfr	    /* don't change the current setting */
165241016Sdfr	    ;
165341016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
165441016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
165541016Sdfr	if (mode.level == -1)
165641016Sdfr	    /* don't change the current setting */
165741016Sdfr	    mode.level = sc->mode.level;
165841016Sdfr	else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
165941016Sdfr	    return EINVAL;
166041016Sdfr        if (mode.accelfactor == -1)
166141016Sdfr	    /* don't change the current setting */
166241016Sdfr	    mode.accelfactor = sc->mode.accelfactor;
166341016Sdfr        else if (mode.accelfactor < 0)
166441016Sdfr	    return EINVAL;
166541016Sdfr
166641016Sdfr	/* don't allow anybody to poll the keyboard controller */
166741016Sdfr	error = block_mouse_data(sc, &command_byte);
166841016Sdfr	if (error)
166941016Sdfr            return error;
167041016Sdfr
167141016Sdfr        /* set mouse parameters */
167241016Sdfr	if (mode.rate > 0)
167341016Sdfr	    mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
167441016Sdfr	if (mode.resolution >= 0)
167541016Sdfr	    mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
167641016Sdfr	set_mouse_scaling(sc->kbdc, 1);
167741016Sdfr	get_mouse_status(sc->kbdc, stat, 0, 3);
167841016Sdfr
167941016Sdfr        s = spltty();
168041016Sdfr    	sc->mode.rate = mode.rate;
168141016Sdfr    	sc->mode.resolution = mode.resolution;
168241016Sdfr    	sc->mode.accelfactor = mode.accelfactor;
168341016Sdfr    	sc->mode.level = mode.level;
168441016Sdfr        splx(s);
168541016Sdfr
168641016Sdfr	unblock_mouse_data(sc, command_byte);
168741016Sdfr        break;
168841016Sdfr
168941016Sdfr    case MOUSE_GETLEVEL:
169041016Sdfr	*(int *)addr = sc->mode.level;
169141016Sdfr        break;
169241016Sdfr
169341016Sdfr    case MOUSE_SETLEVEL:
169441016Sdfr	if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
169541016Sdfr	    return EINVAL;
169641016Sdfr	sc->mode.level = *(int *)addr;
169741016Sdfr        break;
169841016Sdfr
169941016Sdfr    case MOUSE_GETSTATUS:
170041016Sdfr        s = spltty();
170141016Sdfr	status = sc->status;
170241016Sdfr	sc->status.flags = 0;
170341016Sdfr	sc->status.obutton = sc->status.button;
170441016Sdfr	sc->status.button = 0;
170541016Sdfr	sc->status.dx = 0;
170641016Sdfr	sc->status.dy = 0;
170741016Sdfr	sc->status.dz = 0;
170841016Sdfr        splx(s);
170941016Sdfr        *(mousestatus_t *)addr = status;
171041016Sdfr        break;
171141016Sdfr
171241016Sdfr#if (defined(MOUSE_GETVARS))
171341016Sdfr    case MOUSE_GETVARS:
171441016Sdfr	var = (mousevar_t *)addr;
171541016Sdfr	bzero(var, sizeof(*var));
171641016Sdfr	s = spltty();
171741016Sdfr        var->var[0] = MOUSE_VARS_PS2_SIG;
171841016Sdfr        var->var[1] = sc->config;
171941016Sdfr        var->var[2] = sc->flags;
172041016Sdfr	splx(s);
172141016Sdfr        break;
172241016Sdfr
172341016Sdfr    case MOUSE_SETVARS:
172441016Sdfr	return ENODEV;
172541016Sdfr#endif /* MOUSE_GETVARS */
172641016Sdfr
172741016Sdfr    case MOUSE_READSTATE:
172841016Sdfr    case MOUSE_READDATA:
172941016Sdfr	data = (mousedata_t *)addr;
173041016Sdfr	if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
173141016Sdfr	    return EINVAL;
173241016Sdfr
173341016Sdfr	error = block_mouse_data(sc, &command_byte);
173441016Sdfr	if (error)
173541016Sdfr            return error;
173641016Sdfr        if ((data->len = get_mouse_status(sc->kbdc, data->buf,
173741016Sdfr		(cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
173841016Sdfr            error = EIO;
173941016Sdfr	unblock_mouse_data(sc, command_byte);
174041016Sdfr	break;
174141016Sdfr
174241016Sdfr#if (defined(MOUSE_SETRESOLUTION))
174341016Sdfr    case MOUSE_SETRESOLUTION:
174441016Sdfr	mode.resolution = *(int *)addr;
174541016Sdfr	if (mode.resolution >= UCHAR_MAX)
174641016Sdfr	    return EINVAL;
174741016Sdfr	else 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	    mode.resolution = sc->mode.resolution;
175941016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
176041016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
176141016Sdfr
176241016Sdfr	error = block_mouse_data(sc, &command_byte);
176341016Sdfr	if (error)
176441016Sdfr            return error;
176541016Sdfr        sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
176641016Sdfr	if (sc->mode.resolution != mode.resolution)
176741016Sdfr	    error = EIO;
176841016Sdfr	unblock_mouse_data(sc, command_byte);
176941016Sdfr        break;
177041016Sdfr#endif /* MOUSE_SETRESOLUTION */
177141016Sdfr
177241016Sdfr#if (defined(MOUSE_SETRATE))
177341016Sdfr    case MOUSE_SETRATE:
177441016Sdfr	mode.rate = *(int *)addr;
177541016Sdfr	if (mode.rate > UCHAR_MAX)
177641016Sdfr	    return EINVAL;
177741016Sdfr        if (mode.rate == 0)
177841016Sdfr            mode.rate = sc->dflt_mode.rate;
177941016Sdfr	else if (mode.rate < 0)
178041016Sdfr	    mode.rate = sc->mode.rate;
178141016Sdfr
178241016Sdfr	error = block_mouse_data(sc, &command_byte);
178341016Sdfr	if (error)
178441016Sdfr            return error;
178541016Sdfr        sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
178641016Sdfr	if (sc->mode.rate != mode.rate)
178741016Sdfr	    error = EIO;
178841016Sdfr	unblock_mouse_data(sc, command_byte);
178941016Sdfr        break;
179041016Sdfr#endif /* MOUSE_SETRATE */
179141016Sdfr
179241016Sdfr#if (defined(MOUSE_SETSCALING))
179341016Sdfr    case MOUSE_SETSCALING:
179441016Sdfr	if ((*(int *)addr <= 0) || (*(int *)addr > 2))
179541016Sdfr	    return EINVAL;
179641016Sdfr
179741016Sdfr	error = block_mouse_data(sc, &command_byte);
179841016Sdfr	if (error)
179941016Sdfr            return error;
180041016Sdfr        if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
180141016Sdfr	    error = EIO;
180241016Sdfr	unblock_mouse_data(sc, command_byte);
180341016Sdfr        break;
180441016Sdfr#endif /* MOUSE_SETSCALING */
180541016Sdfr
180641016Sdfr#if (defined(MOUSE_GETHWID))
180741016Sdfr    case MOUSE_GETHWID:
180841016Sdfr	error = block_mouse_data(sc, &command_byte);
180941016Sdfr	if (error)
181041016Sdfr            return error;
181148778Syokota        sc->hw.hwid &= ~0x00ff;
181248778Syokota        sc->hw.hwid |= get_aux_id(sc->kbdc);
181348778Syokota	*(int *)addr = sc->hw.hwid & 0x00ff;
181441016Sdfr	unblock_mouse_data(sc, command_byte);
181541016Sdfr        break;
181641016Sdfr#endif /* MOUSE_GETHWID */
181741016Sdfr
181841016Sdfr    default:
181941016Sdfr	return ENOTTY;
182041016Sdfr    }
182141016Sdfr
182241016Sdfr    return error;
182341016Sdfr}
182441016Sdfr
182541016Sdfrstatic void
182658230Syokotapsmtimeout(void *arg)
182758230Syokota{
182858230Syokota    struct psm_softc *sc;
182958230Syokota    int unit;
183058230Syokota
183158230Syokota    unit = (int)arg;
183258230Syokota    sc = devclass_get_softc(psm_devclass, unit);
183363746Syokota    if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
183458230Syokota	if (verbose >= 4)
183558230Syokota	    log(LOG_DEBUG, "psm%d: lost interrupt?\n", unit);
183658230Syokota	psmintr(sc);
183763746Syokota	kbdc_lock(sc->kbdc, FALSE);
183858230Syokota    }
183958230Syokota    sc->watchdog = TRUE;
184058230Syokota    sc->callout = timeout(psmtimeout, (void *)unit, hz);
184158230Syokota}
184258230Syokota
184358230Syokotastatic void
184441016Sdfrpsmintr(void *arg)
184541016Sdfr{
184641016Sdfr    /*
184741016Sdfr     * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
184841016Sdfr     * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
184941016Sdfr     */
185041016Sdfr    static int butmap[8] = {
185141016Sdfr        0,
185241016Sdfr	MOUSE_BUTTON1DOWN,
185341016Sdfr	MOUSE_BUTTON3DOWN,
185441016Sdfr	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
185541016Sdfr	MOUSE_BUTTON2DOWN,
185641016Sdfr	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
185741016Sdfr	MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
185841016Sdfr        MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
185941016Sdfr    };
186049965Syokota    static int butmap_versapad[8] = {
186149965Syokota	0,
186249965Syokota	MOUSE_BUTTON3DOWN,
186349965Syokota	0,
186449965Syokota	MOUSE_BUTTON3DOWN,
186549965Syokota	MOUSE_BUTTON1DOWN,
186649965Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
186749965Syokota	MOUSE_BUTTON1DOWN,
186849965Syokota	MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
186949965Syokota    };
187041016Sdfr    register struct psm_softc *sc = arg;
187141016Sdfr    mousestatus_t ms;
187241016Sdfr    int x, y, z;
187341016Sdfr    int c;
187441016Sdfr    int l;
187549965Syokota    int x0, y0;
187641016Sdfr
187741016Sdfr    /* read until there is nothing to read */
187841016Sdfr    while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
187941016Sdfr
188041016Sdfr        /* discard the byte if the device is not open */
188141016Sdfr        if ((sc->state & PSM_OPEN) == 0)
188241016Sdfr            continue;
188341016Sdfr
188441016Sdfr        sc->ipacket[sc->inputbytes++] = c;
188541016Sdfr        if (sc->inputbytes < sc->mode.packetsize)
188641016Sdfr	    continue;
188741016Sdfr
188841016Sdfr#if 0
188941016Sdfr        log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
189041016Sdfr	    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
189141016Sdfr	    sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
189241016Sdfr#endif
189341016Sdfr
189441016Sdfr	c = sc->ipacket[0];
189541016Sdfr
189663746Syokota	if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
189763746Syokota            log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
189863746Syokota		c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
189963746Syokota	    sc->inputbytes = 0;
190063746Syokota            continue;
190163746Syokota	}
190263746Syokota
190349965Syokota	/*
190441016Sdfr	 * A kludge for Kensington device!
190541016Sdfr	 * The MSB of the horizontal count appears to be stored in
190658230Syokota	 * a strange place.
190741016Sdfr	 */
190858230Syokota	if (sc->hw.model == MOUSE_MODEL_THINK)
190949965Syokota	    sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
191041016Sdfr
191141016Sdfr        /* ignore the overflow bits... */
191241016Sdfr        x = (c & MOUSE_PS2_XNEG) ?  sc->ipacket[1] - 256 : sc->ipacket[1];
191341016Sdfr        y = (c & MOUSE_PS2_YNEG) ?  sc->ipacket[2] - 256 : sc->ipacket[2];
191441016Sdfr	z = 0;
191541016Sdfr        ms.obutton = sc->button;		  /* previous button state */
191641016Sdfr        ms.button = butmap[c & MOUSE_PS2_BUTTONS];
191745789Speter	/* `tapping' action */
191845789Speter	if (sc->config & PSM_CONFIG_FORCETAP)
191945789Speter	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
192041016Sdfr
192141016Sdfr	switch (sc->hw.model) {
192241016Sdfr
192358230Syokota	case MOUSE_MODEL_EXPLORER:
192458230Syokota	    /*
192558230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
192658230Syokota	     * byte 1:  oy ox sy sx 1  M  R  L
192758230Syokota	     * byte 2:  x  x  x  x  x  x  x  x
192858230Syokota	     * byte 3:  y  y  y  y  y  y  y  y
192958230Syokota	     * byte 4:  *  *  S2 S1 s  d2 d1 d0
193058230Syokota	     *
193158230Syokota	     * L, M, R, S1, S2: left, middle, right and side buttons
193258230Syokota	     * s: wheel data sign bit
193358230Syokota	     * d2-d0: wheel data
193458230Syokota	     */
193558230Syokota	    z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
193658230Syokota		? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
193758230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
193858230Syokota		? MOUSE_BUTTON4DOWN : 0;
193958230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
194058230Syokota		? MOUSE_BUTTON5DOWN : 0;
194158230Syokota	    break;
194258230Syokota
194341016Sdfr	case MOUSE_MODEL_INTELLI:
194441016Sdfr	case MOUSE_MODEL_NET:
194541016Sdfr	    /* wheel data is in the fourth byte */
194641016Sdfr	    z = (char)sc->ipacket[3];
194758230Syokota	    /* some mice may send 7 when there is no Z movement?! XXX */
194858230Syokota	    if ((z >= 7) || (z <= -7))
194958230Syokota		z = 0;
195058230Syokota	    /* some compatible mice have additional buttons */
195158230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
195258230Syokota		? MOUSE_BUTTON4DOWN : 0;
195358230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
195458230Syokota		? MOUSE_BUTTON5DOWN : 0;
195541016Sdfr	    break;
195641016Sdfr
195741016Sdfr	case MOUSE_MODEL_MOUSEMANPLUS:
195848778Syokota	    /*
195948778Syokota	     * PS2++ protocl packet
196048778Syokota	     *
196148778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
196248778Syokota	     * byte 1:  *  1  p3 p2 1  *  *  *
196348778Syokota	     * byte 2:  c1 c2 p1 p0 d1 d0 1  0
196448778Syokota	     *
196548778Syokota	     * p3-p0: packet type
196648778Syokota	     * c1, c2: c1 & c2 == 1, if p2 == 0
196748778Syokota	     *         c1 & c2 == 0, if p2 == 1
196848778Syokota	     *
196948778Syokota	     * packet type: 0 (device type)
197048778Syokota	     * See comments in enable_mmanplus() below.
197148778Syokota	     *
197248778Syokota	     * packet type: 1 (wheel data)
197348778Syokota	     *
197448778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
197548778Syokota	     * byte 3:  h  *  B5 B4 s  d2 d1 d0
197648778Syokota	     *
197748778Syokota	     * h: 1, if horizontal roller data
197848778Syokota	     *    0, if vertical roller data
197948778Syokota	     * B4, B5: button 4 and 5
198048778Syokota	     * s: sign bit
198148778Syokota	     * d2-d0: roller data
198248778Syokota	     *
198348778Syokota	     * packet type: 2 (reserved)
198448778Syokota	     */
198548778Syokota	    if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
198648778Syokota		    && (abs(x) > 191)
198748778Syokota		    && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
198841016Sdfr		/* the extended data packet encodes button and wheel events */
198948778Syokota		switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
199048778Syokota		case 1:
199148778Syokota		    /* wheel data packet */
199248778Syokota		    x = y = 0;
199348778Syokota		    if (sc->ipacket[2] & 0x80) {
199448778Syokota			/* horizontal roller count - ignore it XXX*/
199548778Syokota		    } else {
199648778Syokota			/* vertical roller count */
199748778Syokota			z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
199848778Syokota			    ? (sc->ipacket[2] & 0x0f) - 16
199948778Syokota			    : (sc->ipacket[2] & 0x0f);
200048778Syokota		    }
200148778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
200248778Syokota			? MOUSE_BUTTON4DOWN : 0;
200348778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
200448778Syokota			? MOUSE_BUTTON5DOWN : 0;
200548778Syokota		    break;
200648778Syokota		case 2:
200758230Syokota		    /* this packet type is reserved by Logitech... */
200858230Syokota		    /*
200958230Syokota		     * IBM ScrollPoint Mouse uses this packet type to
201058230Syokota		     * encode both vertical and horizontal scroll movement.
201158230Syokota		     */
201258230Syokota		    x = y = 0;
201358230Syokota		    /* horizontal count */
201458230Syokota		    if (sc->ipacket[2] & 0x0f)
201558230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
201658230Syokota		    /* vertical count */
201758230Syokota		    if (sc->ipacket[2] & 0xf0)
201858230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
201958230Syokota#if 0
202058230Syokota		    /* vertical count */
202158230Syokota		    z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
202258230Syokota			? ((sc->ipacket[2] >> 4) & 0x0f) - 16
202358230Syokota			: ((sc->ipacket[2] >> 4) & 0x0f);
202458230Syokota		    /* horizontal count */
202558230Syokota		    w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
202658230Syokota			? (sc->ipacket[2] & 0x0f) - 16
202758230Syokota			: (sc->ipacket[2] & 0x0f);
202858230Syokota#endif
202958230Syokota		    break;
203048778Syokota		case 0:
203148778Syokota		    /* device type packet - shouldn't happen */
203248778Syokota		    /* FALL THROUGH */
203348778Syokota		default:
203448778Syokota		    x = y = 0;
203548778Syokota		    ms.button = ms.obutton;
203658230Syokota		    if (bootverbose)
203758230Syokota			log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
203858230Syokota				       "0x%02x 0x%02x 0x%02x\n",
203958230Syokota			    MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
204058230Syokota			    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
204148778Syokota		    break;
204248778Syokota		}
204341016Sdfr	    } else {
204441016Sdfr		/* preserve button states */
204541016Sdfr		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
204641016Sdfr	    }
204741016Sdfr	    break;
204841016Sdfr
204941016Sdfr	case MOUSE_MODEL_GLIDEPOINT:
205041016Sdfr	    /* `tapping' action */
205141016Sdfr	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
205241016Sdfr	    break;
205341016Sdfr
205441016Sdfr	case MOUSE_MODEL_NETSCROLL:
205558230Syokota	    /* three addtional bytes encode buttons and wheel events */
205658230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
205741016Sdfr		? MOUSE_BUTTON4DOWN : 0;
205858230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
205958230Syokota		? MOUSE_BUTTON5DOWN : 0;
206041016Sdfr	    z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
206141016Sdfr		? sc->ipacket[4] - 256 : sc->ipacket[4];
206241016Sdfr	    break;
206341016Sdfr
206441016Sdfr	case MOUSE_MODEL_THINK:
206541016Sdfr	    /* the fourth button state in the first byte */
206641016Sdfr	    ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
206741016Sdfr	    break;
206841016Sdfr
206949965Syokota	case MOUSE_MODEL_VERSAPAD:
207049965Syokota	    /* VersaPad PS/2 absolute mode message format
207149965Syokota	     *
207249965Syokota	     * [packet1]     7   6   5   4   3   2   1   0(LSB)
207349965Syokota	     *  ipacket[0]:  1   1   0   A   1   L   T   R
207449965Syokota	     *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
207549965Syokota	     *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
207649965Syokota	     *  ipacket[3]:  1   1   1   A   1   L   T   R
207749965Syokota	     *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
207849965Syokota	     *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
207949965Syokota	     *
208049965Syokota	     * [note]
208149965Syokota	     *  R: right physical mouse button (1=on)
208249965Syokota	     *  T: touch pad virtual button (1=tapping)
208349965Syokota	     *  L: left physical mouse button (1=on)
208449965Syokota	     *  A: position data is valid (1=valid)
208549965Syokota	     *  H: horizontal data (12bit signed integer. H11 is sign bit.)
208649965Syokota	     *  V: vertical data (12bit signed integer. V11 is sign bit.)
208749965Syokota	     *  P: pressure data
208849965Syokota	     *
208949965Syokota	     * Tapping is mapped to MOUSE_BUTTON4.
209049965Syokota	     */
209149965Syokota	    ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
209249965Syokota	    ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
209349965Syokota	    x = y = 0;
209449965Syokota	    if (c & MOUSE_PS2VERSA_IN_USE) {
209549965Syokota		x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
209649965Syokota		y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
209749965Syokota		if (x0 & 0x800)
209849965Syokota		    x0 -= 0x1000;
209949965Syokota		if (y0 & 0x800)
210049965Syokota		    y0 -= 0x1000;
210149965Syokota		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
210249965Syokota		    x = sc->xold - x0;
210349965Syokota		    y = y0 - sc->yold;
210449965Syokota		    if (x < 0)	/* XXX */
210549965Syokota			x++;
210649965Syokota		    else if (x)
210749965Syokota			x--;
210849965Syokota		    if (y < 0)
210949965Syokota			y++;
211049965Syokota		    else if (y)
211149965Syokota			y--;
211249965Syokota		} else {
211349965Syokota		    sc->flags |= PSM_FLAGS_FINGERDOWN;
211449965Syokota		}
211549965Syokota		sc->xold = x0;
211649965Syokota		sc->yold = y0;
211749965Syokota	    } else {
211849965Syokota		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
211949965Syokota	    }
212049965Syokota	    c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
212149965Syokota		| ((y < 0) ? MOUSE_PS2_YNEG : 0);
212249965Syokota	    break;
212349965Syokota
212458230Syokota	case MOUSE_MODEL_4D:
212558230Syokota	    /*
212658230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
212758230Syokota	     * byte 1:  s2 d2 s1 d1 1  M  R  L
212858230Syokota	     * byte 2:  sx x  x  x  x  x  x  x
212958230Syokota	     * byte 3:  sy y  y  y  y  y  y  y
213058230Syokota	     *
213158230Syokota	     * s1: wheel 1 direction
213258230Syokota	     * d1: wheel 1 data
213358230Syokota	     * s2: wheel 2 direction
213458230Syokota	     * d2: wheel 2 data
213558230Syokota	     */
213658230Syokota	    x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
213758230Syokota	    y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
213858230Syokota	    switch (c & MOUSE_4D_WHEELBITS) {
213958230Syokota	    case 0x10:
214058230Syokota		z = 1;
214158230Syokota		break;
214258230Syokota	    case 0x30:
214358230Syokota		z = -1;
214458230Syokota		break;
214558230Syokota	    case 0x40:	/* 2nd wheel turning right XXX */
214658230Syokota		z = 2;
214758230Syokota		break;
214858230Syokota	    case 0xc0:	/* 2nd wheel turning left XXX */
214958230Syokota		z = -2;
215058230Syokota		break;
215158230Syokota	    }
215258230Syokota	    break;
215358230Syokota
215458230Syokota	case MOUSE_MODEL_4DPLUS:
215558230Syokota	    if ((x < 16 - 256) && (y < 16 - 256)) {
215658230Syokota		/*
215758230Syokota		 *          b7 b6 b5 b4 b3 b2 b1 b0
215858230Syokota		 * byte 1:  0  0  1  1  1  M  R  L
215958230Syokota		 * byte 2:  0  0  0  0  1  0  0  0
216058230Syokota		 * byte 3:  0  0  0  0  S  s  d1 d0
216158230Syokota		 *
216258230Syokota		 * L, M, R, S: left, middle, right and side buttons
216358230Syokota		 * s: wheel data sign bit
216458230Syokota		 * d1-d0: wheel data
216558230Syokota		 */
216658230Syokota		x = y = 0;
216758230Syokota		if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
216858230Syokota		    ms.button |= MOUSE_BUTTON4DOWN;
216958230Syokota		z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
217058230Syokota			? ((sc->ipacket[2] & 0x07) - 8)
217158230Syokota			: (sc->ipacket[2] & 0x07) ;
217258230Syokota	    } else {
217358230Syokota		/* preserve previous button states */
217458230Syokota		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
217558230Syokota	    }
217658230Syokota	    break;
217758230Syokota
217841016Sdfr	case MOUSE_MODEL_GENERIC:
217941016Sdfr	default:
218041016Sdfr	    break;
218141016Sdfr	}
218241016Sdfr
218341016Sdfr        /* scale values */
218441016Sdfr        if (sc->mode.accelfactor >= 1) {
218541016Sdfr            if (x != 0) {
218641016Sdfr                x = x * x / sc->mode.accelfactor;
218741016Sdfr                if (x == 0)
218841016Sdfr                    x = 1;
218941016Sdfr                if (c & MOUSE_PS2_XNEG)
219041016Sdfr                    x = -x;
219141016Sdfr            }
219241016Sdfr            if (y != 0) {
219341016Sdfr                y = y * y / sc->mode.accelfactor;
219441016Sdfr                if (y == 0)
219541016Sdfr                    y = 1;
219641016Sdfr                if (c & MOUSE_PS2_YNEG)
219741016Sdfr                    y = -y;
219841016Sdfr            }
219941016Sdfr        }
220041016Sdfr
220141016Sdfr        ms.dx = x;
220241016Sdfr        ms.dy = y;
220341016Sdfr        ms.dz = z;
220441016Sdfr        ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
220541016Sdfr	    | (ms.obutton ^ ms.button);
220641016Sdfr
220741016Sdfr	if (sc->mode.level < PSM_LEVEL_NATIVE)
220841016Sdfr	    sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
220941016Sdfr
221041016Sdfr        sc->status.flags |= ms.flags;
221141016Sdfr        sc->status.dx += ms.dx;
221241016Sdfr        sc->status.dy += ms.dy;
221341016Sdfr        sc->status.dz += ms.dz;
221441016Sdfr        sc->status.button = ms.button;
221541016Sdfr        sc->button = ms.button;
221641016Sdfr
221758230Syokota	sc->watchdog = FALSE;
221858230Syokota
221941016Sdfr        /* queue data */
222041016Sdfr        if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
222141016Sdfr	    l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
222241016Sdfr	    bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
222341016Sdfr	    if (sc->inputbytes > l)
222441016Sdfr	        bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
222541016Sdfr            sc->queue.tail =
222641016Sdfr		(sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
222741016Sdfr            sc->queue.count += sc->inputbytes;
222841016Sdfr	}
222941016Sdfr        sc->inputbytes = 0;
223041016Sdfr
223141016Sdfr        if (sc->state & PSM_ASLP) {
223241016Sdfr            sc->state &= ~PSM_ASLP;
223341016Sdfr            wakeup((caddr_t) sc);
223441016Sdfr    	}
223541016Sdfr        selwakeup(&sc->rsel);
223641016Sdfr    }
223741016Sdfr}
223841016Sdfr
223941016Sdfrstatic int
224041016Sdfrpsmpoll(dev_t dev, int events, struct proc *p)
224141016Sdfr{
224241016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
224341016Sdfr    int s;
224441016Sdfr    int revents = 0;
224541016Sdfr
224641016Sdfr    /* Return true if a mouse event available */
224741016Sdfr    s = spltty();
224845789Speter    if (events & (POLLIN | POLLRDNORM)) {
224941016Sdfr	if (sc->queue.count > 0)
225041016Sdfr	    revents |= events & (POLLIN | POLLRDNORM);
225141016Sdfr	else
225241016Sdfr	    selrecord(p, &sc->rsel);
225345789Speter    }
225441016Sdfr    splx(s);
225541016Sdfr
225641016Sdfr    return (revents);
225741016Sdfr}
225841016Sdfr
225941016Sdfr/* vendor/model specific routines */
226041016Sdfr
226141016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
226241016Sdfr{
226341016Sdfr    if (set_mouse_resolution(kbdc, res) != res)
226441016Sdfr        return FALSE;
226541016Sdfr    if (set_mouse_scaling(kbdc, scale)
226641016Sdfr	&& set_mouse_scaling(kbdc, scale)
226741016Sdfr	&& set_mouse_scaling(kbdc, scale)
226841016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3))
226941016Sdfr	return TRUE;
227041016Sdfr    return FALSE;
227141016Sdfr}
227241016Sdfr
227341016Sdfr#if notyet
227441016Sdfr/* Logitech MouseMan Cordless II */
227541016Sdfrstatic int
227641016Sdfrenable_lcordless(struct psm_softc *sc)
227741016Sdfr{
227841016Sdfr    int status[3];
227941016Sdfr    int ch;
228041016Sdfr
228141016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
228241016Sdfr        return FALSE;
228341016Sdfr    if (status[1] == PSMD_RES_HIGH)
228441016Sdfr	return FALSE;
228541016Sdfr    ch = (status[0] & 0x07) - 1;	/* channel # */
228641016Sdfr    if ((ch <= 0) || (ch > 4))
228741016Sdfr	return FALSE;
228841016Sdfr    /*
228941016Sdfr     * status[1]: always one?
229041016Sdfr     * status[2]: battery status? (0-100)
229141016Sdfr     */
229241016Sdfr    return TRUE;
229341016Sdfr}
229441016Sdfr#endif /* notyet */
229541016Sdfr
229658230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
229741016Sdfrstatic int
229841016Sdfrenable_groller(struct psm_softc *sc)
229941016Sdfr{
230041016Sdfr    int status[3];
230141016Sdfr
230241016Sdfr    /*
230341016Sdfr     * The special sequence to enable the fourth button and the
230441016Sdfr     * roller. Immediately after this sequence check status bytes.
230541016Sdfr     * if the mouse is NetScroll, the second and the third bytes are
230641016Sdfr     * '3' and 'D'.
230741016Sdfr     */
230841016Sdfr
230941016Sdfr    /*
231041016Sdfr     * If the mouse is an ordinary PS/2 mouse, the status bytes should
231141016Sdfr     * look like the following.
231241016Sdfr     *
231341016Sdfr     * byte 1 bit 7 always 0
231441016Sdfr     *        bit 6 stream mode (0)
231541016Sdfr     *        bit 5 disabled (0)
231641016Sdfr     *        bit 4 1:1 scaling (0)
231741016Sdfr     *        bit 3 always 0
231841016Sdfr     *        bit 0-2 button status
231941016Sdfr     * byte 2 resolution (PSMD_RES_HIGH)
232041016Sdfr     * byte 3 report rate (?)
232141016Sdfr     */
232241016Sdfr
232341016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
232441016Sdfr        return FALSE;
232541016Sdfr    if ((status[1] != '3') || (status[2] != 'D'))
232641016Sdfr        return FALSE;
232758230Syokota    /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
232841016Sdfr    sc->hw.buttons = 4;
232941016Sdfr    return TRUE;
233041016Sdfr}
233141016Sdfr
233258230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
233341016Sdfrstatic int
233441016Sdfrenable_gmouse(struct psm_softc *sc)
233541016Sdfr{
233641016Sdfr    int status[3];
233741016Sdfr
233841016Sdfr    /*
233941016Sdfr     * The special sequence to enable the middle, "rubber" button.
234041016Sdfr     * Immediately after this sequence check status bytes.
234141016Sdfr     * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
234241016Sdfr     * the second and the third bytes are '3' and 'U'.
234341016Sdfr     * NOTE: NetMouse reports that it has three buttons although it has
234441016Sdfr     * two buttons and a rubber button. NetMouse Pro and MIE Mouse
234541016Sdfr     * say they have three buttons too and they do have a button on the
234641016Sdfr     * side...
234741016Sdfr     */
234841016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
234941016Sdfr        return FALSE;
235041016Sdfr    if ((status[1] != '3') || (status[2] != 'U'))
235141016Sdfr        return FALSE;
235241016Sdfr    return TRUE;
235341016Sdfr}
235441016Sdfr
235541016Sdfr/* ALPS GlidePoint */
235641016Sdfrstatic int
235741016Sdfrenable_aglide(struct psm_softc *sc)
235841016Sdfr{
235941016Sdfr    int status[3];
236041016Sdfr
236141016Sdfr    /*
236241016Sdfr     * The special sequence to obtain ALPS GlidePoint specific
236341016Sdfr     * information. Immediately after this sequence, status bytes will
236441016Sdfr     * contain something interesting.
236541016Sdfr     * NOTE: ALPS produces several models of GlidePoint. Some of those
236641016Sdfr     * do not respond to this sequence, thus, cannot be detected this way.
236741016Sdfr     */
236850149Syokota    if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
236950149Syokota	return FALSE;
237041016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
237141016Sdfr        return FALSE;
237250149Syokota    if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
237341016Sdfr        return FALSE;
237441016Sdfr    return TRUE;
237541016Sdfr}
237641016Sdfr
237741016Sdfr/* Kensington ThinkingMouse/Trackball */
237841016Sdfrstatic int
237941016Sdfrenable_kmouse(struct psm_softc *sc)
238041016Sdfr{
238141016Sdfr    static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
238241016Sdfr    KBDC kbdc = sc->kbdc;
238341016Sdfr    int status[3];
238441016Sdfr    int id1;
238541016Sdfr    int id2;
238641016Sdfr    int i;
238741016Sdfr
238841016Sdfr    id1 = get_aux_id(kbdc);
238941016Sdfr    if (set_mouse_sampling_rate(kbdc, 10) != 10)
239041016Sdfr	return FALSE;
239141016Sdfr    /*
239241016Sdfr     * The device is now in the native mode? It returns a different
239341016Sdfr     * ID value...
239441016Sdfr     */
239541016Sdfr    id2 = get_aux_id(kbdc);
239641016Sdfr    if ((id1 == id2) || (id2 != 2))
239741016Sdfr	return FALSE;
239841016Sdfr
239941016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
240041016Sdfr        return FALSE;
240141016Sdfr#if PSM_DEBUG >= 2
240241016Sdfr    /* at this point, resolution is LOW, sampling rate is 10/sec */
240341016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
240441016Sdfr        return FALSE;
240541016Sdfr#endif
240641016Sdfr
240741016Sdfr    /*
240841016Sdfr     * The special sequence to enable the third and fourth buttons.
240941016Sdfr     * Otherwise they behave like the first and second buttons.
241041016Sdfr     */
241141016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
241241016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
241341016Sdfr	    return FALSE;
241441016Sdfr    }
241541016Sdfr
241641016Sdfr    /*
241741016Sdfr     * At this point, the device is using default resolution and
241841016Sdfr     * sampling rate for the native mode.
241941016Sdfr     */
242041016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
242141016Sdfr        return FALSE;
242241016Sdfr    if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
242341016Sdfr        return FALSE;
242441016Sdfr
242541016Sdfr    /* the device appears be enabled by this sequence, diable it for now */
242641016Sdfr    disable_aux_dev(kbdc);
242741016Sdfr    empty_aux_buffer(kbdc, 5);
242841016Sdfr
242941016Sdfr    return TRUE;
243041016Sdfr}
243141016Sdfr
243258230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
243341016Sdfrstatic int
243441016Sdfrenable_mmanplus(struct psm_softc *sc)
243541016Sdfr{
243641016Sdfr    static char res[] = {
243741016Sdfr	-1, PSMD_RES_LOW, PSMD_RES_HIGH, PSMD_RES_MEDIUM_HIGH,
243841016Sdfr	PSMD_RES_MEDIUM_LOW, -1, PSMD_RES_HIGH, PSMD_RES_MEDIUM_LOW,
243941016Sdfr	PSMD_RES_MEDIUM_HIGH, PSMD_RES_HIGH,
244041016Sdfr    };
244141016Sdfr    KBDC kbdc = sc->kbdc;
244241016Sdfr    int data[3];
244341016Sdfr    int i;
244441016Sdfr
244541016Sdfr    /* the special sequence to enable the fourth button and the roller. */
244658230Syokota    /*
244758230Syokota     * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
244858230Syokota     * must be called exactly three times since the last RESET command
244958230Syokota     * before this sequence. XXX
245058230Syokota     */
245141016Sdfr    for (i = 0; i < sizeof(res)/sizeof(res[0]); ++i) {
245241016Sdfr	if (res[i] < 0) {
245341016Sdfr	    if (!set_mouse_scaling(kbdc, 1))
245441016Sdfr		return FALSE;
245541016Sdfr	} else {
245641016Sdfr	    if (set_mouse_resolution(kbdc, res[i]) != res[i])
245741016Sdfr		return FALSE;
245841016Sdfr	}
245941016Sdfr    }
246041016Sdfr
246141016Sdfr    if (get_mouse_status(kbdc, data, 1, 3) < 3)
246241016Sdfr        return FALSE;
246341016Sdfr
246448778Syokota    /*
246548778Syokota     * PS2++ protocl, packet type 0
246641016Sdfr     *
246748778Syokota     *          b7 b6 b5 b4 b3 b2 b1 b0
246848778Syokota     * byte 1:  *  1  p3 p2 1  *  *  *
246948778Syokota     * byte 2:  1  1  p1 p0 m1 m0 1  0
247048778Syokota     * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
247148778Syokota     *
247248778Syokota     * p3-p0: packet type: 0
247358230Syokota     * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
247441016Sdfr     */
247548778Syokota    /* check constant bits */
247648778Syokota    if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
247741016Sdfr        return FALSE;
247848778Syokota    if ((data[1] & 0xc3) != 0xc2)
247948778Syokota        return FALSE;
248048778Syokota    /* check d3-d0 in byte 2 */
248148778Syokota    if (!MOUSE_PS2PLUS_CHECKBITS(data))
248248778Syokota        return FALSE;
248348778Syokota    /* check p3-p0 */
248448778Syokota    if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
248548778Syokota        return FALSE;
248641016Sdfr
248748778Syokota    sc->hw.hwid &= 0x00ff;
248848778Syokota    sc->hw.hwid |= data[2] << 8;	/* save model ID */
248948778Syokota
249041016Sdfr    /*
249141016Sdfr     * MouseMan+ (or FirstMouse+) is now in its native mode, in which
249241016Sdfr     * the wheel and the fourth button events are encoded in the
249341016Sdfr     * special data packet. The mouse may be put in the IntelliMouse mode
249441016Sdfr     * if it is initialized by the IntelliMouse's method.
249541016Sdfr     */
249641016Sdfr    return TRUE;
249741016Sdfr}
249841016Sdfr
249958230Syokota/* MS IntelliMouse Explorer */
250058230Syokotastatic int
250158230Syokotaenable_msexplorer(struct psm_softc *sc)
250258230Syokota{
250358923Syokota    static unsigned char rate0[] = { 200, 100, 80, };
250458923Syokota    static unsigned char rate1[] = { 200, 200, 80, };
250558230Syokota    KBDC kbdc = sc->kbdc;
250658230Syokota    int id;
250758230Syokota    int i;
250858230Syokota
250958923Syokota    /*
251058923Syokota     * XXX: this is a kludge to fool some KVM switch products
251158923Syokota     * which think they are clever enough to know the 4-byte IntelliMouse
251258923Syokota     * protocol, and assume any other protocols use 3-byte packets.
251358923Syokota     * They don't convey 4-byte data packets from the IntelliMouse Explorer
251458923Syokota     * correctly to the host computer because of this!
251558923Syokota     * The following sequence is actually IntelliMouse's "wake up"
251658923Syokota     * sequence; it will make the KVM think the mouse is IntelliMouse
251758923Syokota     * when it is in fact IntelliMouse Explorer.
251858923Syokota     */
251958923Syokota    for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
252058923Syokota        if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
252158923Syokota	    return FALSE;
252258923Syokota    }
252358923Syokota    id = get_aux_id(kbdc);
252458923Syokota
252558230Syokota    /* the special sequence to enable the extra buttons and the roller. */
252658923Syokota    for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
252758923Syokota        if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
252858230Syokota	    return FALSE;
252958230Syokota    }
253058230Syokota    /* the device will give the genuine ID only after the above sequence */
253158230Syokota    id = get_aux_id(kbdc);
253258230Syokota    if (id != PSM_EXPLORER_ID)
253358230Syokota	return FALSE;
253458230Syokota
253558230Syokota    sc->hw.hwid = id;
253658230Syokota    sc->hw.buttons = 5;		/* IntelliMouse Explorer XXX */
253758230Syokota
253858230Syokota    return TRUE;
253958230Syokota}
254058230Syokota
254141016Sdfr/* MS IntelliMouse */
254241016Sdfrstatic int
254341016Sdfrenable_msintelli(struct psm_softc *sc)
254441016Sdfr{
254541016Sdfr    /*
254641016Sdfr     * Logitech MouseMan+ and FirstMouse+ will also respond to this
254741016Sdfr     * probe routine and act like IntelliMouse.
254841016Sdfr     */
254941016Sdfr
255041016Sdfr    static unsigned char rate[] = { 200, 100, 80, };
255141016Sdfr    KBDC kbdc = sc->kbdc;
255241016Sdfr    int id;
255341016Sdfr    int i;
255441016Sdfr
255541016Sdfr    /* the special sequence to enable the third button and the roller. */
255641016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
255741016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
255841016Sdfr	    return FALSE;
255941016Sdfr    }
256041016Sdfr    /* the device will give the genuine ID only after the above sequence */
256141016Sdfr    id = get_aux_id(kbdc);
256241016Sdfr    if (id != PSM_INTELLI_ID)
256341016Sdfr	return FALSE;
256441016Sdfr
256541016Sdfr    sc->hw.hwid = id;
256641016Sdfr    sc->hw.buttons = 3;
256741016Sdfr
256841016Sdfr    return TRUE;
256941016Sdfr}
257041016Sdfr
257158230Syokota/* A4 Tech 4D Mouse */
257258230Syokotastatic int
257358230Syokotaenable_4dmouse(struct psm_softc *sc)
257458230Syokota{
257558230Syokota    /*
257658230Syokota     * Newer wheel mice from A4 Tech may use the 4D+ protocol.
257758230Syokota     */
257858230Syokota
257958230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
258058230Syokota    KBDC kbdc = sc->kbdc;
258158230Syokota    int id;
258258230Syokota    int i;
258358230Syokota
258458230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
258558230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
258658230Syokota	    return FALSE;
258758230Syokota    }
258858230Syokota    id = get_aux_id(kbdc);
258958230Syokota    /*
259058923Syokota     * WinEasy 4D, 4 Way Scroll 4D: 6
259158230Syokota     * Cable-Free 4D: 8 (4DPLUS)
259258923Syokota     * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
259358230Syokota     */
259458230Syokota    if (id != PSM_4DMOUSE_ID)
259558230Syokota	return FALSE;
259658230Syokota
259758230Syokota    sc->hw.hwid = id;
259858230Syokota    sc->hw.buttons = 3;		/* XXX some 4D mice have 4? */
259958230Syokota
260058230Syokota    return TRUE;
260158230Syokota}
260258230Syokota
260358230Syokota/* A4 Tech 4D+ Mouse */
260458230Syokotastatic int
260558230Syokotaenable_4dplus(struct psm_softc *sc)
260658230Syokota{
260758230Syokota    /*
260858230Syokota     * Newer wheel mice from A4 Tech seem to use this protocol.
260958230Syokota     * Older models are recognized as either 4D Mouse or IntelliMouse.
261058230Syokota     */
261158230Syokota    KBDC kbdc = sc->kbdc;
261258230Syokota    int id;
261358230Syokota
261458230Syokota    /*
261558230Syokota     * enable_4dmouse() already issued the following ID sequence...
261658230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
261758230Syokota    int i;
261858230Syokota
261958230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
262058230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
262158230Syokota	    return FALSE;
262258230Syokota    }
262358230Syokota    */
262458230Syokota
262558230Syokota    id = get_aux_id(kbdc);
262658230Syokota    if (id != PSM_4DPLUS_ID)
262758230Syokota	return FALSE;
262858230Syokota
262958230Syokota    sc->hw.hwid = id;
263058230Syokota    sc->hw.buttons = 4;		/* XXX */
263158230Syokota
263258230Syokota    return TRUE;
263358230Syokota}
263458230Syokota
263549965Syokota/* Interlink electronics VersaPad */
263649965Syokotastatic int
263749965Syokotaenable_versapad(struct psm_softc *sc)
263849965Syokota{
263949965Syokota    KBDC kbdc = sc->kbdc;
264049965Syokota    int data[3];
264149965Syokota
264249965Syokota    set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
264349965Syokota    set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
264449965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
264549965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
264649965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
264749965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
264849965Syokota    if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
264949965Syokota	return FALSE;
265049965Syokota    if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
265149965Syokota	return FALSE;
265249965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
265349965Syokota
265458230Syokota    sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
265558230Syokota
265649965Syokota    return TRUE;				/* PS/2 absolute mode */
265749965Syokota}
265849965Syokota
265941016Sdfrstatic int
266054629Syokotapsmresume(device_t dev)
266141016Sdfr{
266254629Syokota    struct psm_softc *sc = device_get_softc(dev);
266354629Syokota    int unit = device_get_unit(dev);
266441016Sdfr    int err = 0;
266541016Sdfr    int s;
266641016Sdfr    int c;
266741016Sdfr
266841016Sdfr    if (verbose >= 2)
266954629Syokota        log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
267041016Sdfr
267158230Syokota    if (!(sc->config & PSM_CONFIG_HOOKRESUME))
267258230Syokota	return (0);
267358230Syokota
267441016Sdfr    /* don't let anybody mess with the aux device */
267541016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
267641016Sdfr	return (EIO);
267741016Sdfr    s = spltty();
267841016Sdfr
267958230Syokota    /* block our watchdog timer */
268058230Syokota    sc->watchdog = FALSE;
268158230Syokota    untimeout(psmtimeout, (void *)unit, sc->callout);
268258230Syokota    callout_handle_init(&sc->callout);
268358230Syokota
268441016Sdfr    /* save the current controller command byte */
268541016Sdfr    empty_both_buffers(sc->kbdc, 10);
268641016Sdfr    c = get_controller_command_byte(sc->kbdc);
268741016Sdfr    if (verbose >= 2)
268841016Sdfr        log(LOG_DEBUG, "psm%d: current command byte: %04x (psmresume).\n",
268941016Sdfr	    unit, c);
269041016Sdfr
269141016Sdfr    /* enable the aux port but disable the aux interrupt and the keyboard */
269241016Sdfr    if ((c == -1) || !set_controller_command_byte(sc->kbdc,
269341016Sdfr	    kbdc_get_device_mask(sc->kbdc),
269441016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
269541016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
269641016Sdfr        /* CONTROLLER ERROR */
269741016Sdfr	splx(s);
269841016Sdfr        kbdc_lock(sc->kbdc, FALSE);
269941016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmresume).\n",
270041016Sdfr	    unit);
270141016Sdfr	return (EIO);
270241016Sdfr    }
270341016Sdfr
270441016Sdfr    /* flush any data */
270541016Sdfr    if (sc->state & PSM_VALID) {
270641016Sdfr	disable_aux_dev(sc->kbdc);	/* this may fail; but never mind... */
270741016Sdfr	empty_aux_buffer(sc->kbdc, 10);
270841016Sdfr    }
270941016Sdfr    sc->inputbytes = 0;
271041016Sdfr
271141016Sdfr    /* try to detect the aux device; are you still there? */
271258230Syokota    if (sc->config & PSM_CONFIG_INITAFTERSUSPEND) {
271358230Syokota	if (reinitialize(unit, &sc->mode)) {
271458230Syokota	    /* yes */
271558230Syokota	    sc->state |= PSM_VALID;
271658230Syokota	} else {
271758230Syokota	    /* the device has gone! */
271858230Syokota	    restore_controller(sc->kbdc, c);
271958230Syokota	    sc->state &= ~PSM_VALID;
272058230Syokota	    log(LOG_ERR, "psm%d: the aux device has gone! (psmresume).\n",
272158230Syokota		unit);
272258230Syokota	    err = ENXIO;
272358230Syokota	}
272441016Sdfr    }
272541016Sdfr    splx(s);
272641016Sdfr
272741016Sdfr    /* restore the driver state */
272841016Sdfr    if ((sc->state & PSM_OPEN) && (err == 0)) {
272941016Sdfr        /* enable the aux device and the port again */
273041016Sdfr	err = doopen(unit, c);
273141016Sdfr	if (err != 0)
273241016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (psmresume).\n",
273341016Sdfr		unit);
273441016Sdfr    } else {
273541016Sdfr        /* restore the keyboard port and disable the aux port */
273641016Sdfr        if (!set_controller_command_byte(sc->kbdc,
273741016Sdfr                kbdc_get_device_mask(sc->kbdc),
273841016Sdfr                (c & KBD_KBD_CONTROL_BITS)
273941016Sdfr                    | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
274041016Sdfr            /* CONTROLLER ERROR */
274141016Sdfr            log(LOG_ERR, "psm%d: failed to disable the aux port (psmresume).\n",
274241016Sdfr                unit);
274341016Sdfr            err = EIO;
274441016Sdfr	}
274541016Sdfr    }
274641016Sdfr
274741016Sdfr    /* done */
274841016Sdfr    kbdc_lock(sc->kbdc, FALSE);
274941016Sdfr    if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
275041016Sdfr	/*
275141016Sdfr	 * Release the blocked process; it must be notified that the device
275241016Sdfr	 * cannot be accessed anymore.
275341016Sdfr	 */
275441016Sdfr        sc->state &= ~PSM_ASLP;
275541016Sdfr        wakeup((caddr_t)sc);
275641016Sdfr    }
275741016Sdfr
275841016Sdfr    if (verbose >= 2)
275954629Syokota        log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
276041016Sdfr
276141016Sdfr    return (err);
276241016Sdfr}
276341016Sdfr
276452997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
2765