psm.c revision 58230
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 58230 2000-03-18 15:21:40Z 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>
7441016Sdfr#include <sys/malloc.h>
7545720Speter#include <machine/bus.h>
7641181Sdfr#include <sys/rman.h>
7741016Sdfr#include <sys/select.h>
7841016Sdfr#include <sys/uio.h>
7941016Sdfr
8041016Sdfr#include <machine/clock.h>
8141016Sdfr#include <machine/limits.h>
8241016Sdfr#include <machine/mouse.h>
8341181Sdfr#include <machine/resource.h>
8441016Sdfr
8541016Sdfr#include <isa/isavar.h>
8643105Sdfr#include <dev/kbd/atkbdcreg.h>
8741016Sdfr
8841016Sdfr/*
8941016Sdfr * Driver specific options: the following options may be set by
9041016Sdfr * `options' statements in the kernel configuration file.
9141016Sdfr */
9241016Sdfr
9341016Sdfr/* debugging */
9441016Sdfr#ifndef PSM_DEBUG
9541016Sdfr#define PSM_DEBUG	0	/* logging: 0: none, 1: brief, 2: verbose */
9641016Sdfr#endif
9741016Sdfr
9841016Sdfr/* end of driver specific options */
9941016Sdfr
10041016Sdfr/* input queue */
10141016Sdfr#define PSM_BUFSIZE		960
10241016Sdfr#define PSM_SMALLBUFSIZE	240
10341016Sdfr
10441016Sdfr/* operation levels */
10541016Sdfr#define PSM_LEVEL_BASE		0
10641016Sdfr#define PSM_LEVEL_STANDARD	1
10741016Sdfr#define PSM_LEVEL_NATIVE	2
10841016Sdfr#define PSM_LEVEL_MIN		PSM_LEVEL_BASE
10941016Sdfr#define PSM_LEVEL_MAX		PSM_LEVEL_NATIVE
11041016Sdfr
11148778Syokota/* Logitech PS2++ protocol */
11248778Syokota#define MOUSE_PS2PLUS_CHECKBITS(b)	\
11348778Syokota				((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
11448778Syokota#define MOUSE_PS2PLUS_PACKET_TYPE(b)	\
11548778Syokota				(((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
11648778Syokota
11741016Sdfr/* some macros */
11841016Sdfr#define PSM_UNIT(dev)		(minor(dev) >> 1)
11941016Sdfr#define PSM_NBLOCKIO(dev)	(minor(dev) & 1)
12041016Sdfr#define PSM_MKMINOR(unit,block)	(((unit) << 1) | ((block) ? 0:1))
12141016Sdfr
12241016Sdfr#ifndef max
12341016Sdfr#define max(x,y)		((x) > (y) ? (x) : (y))
12441016Sdfr#endif
12541016Sdfr#ifndef min
12641016Sdfr#define min(x,y)		((x) < (y) ? (x) : (y))
12741016Sdfr#endif
12841016Sdfr
12948778Syokota#define abs(x)			(((x) < 0) ? -(x) : (x))
13048778Syokota
13141016Sdfr/* ring buffer */
13241016Sdfrtypedef struct ringbuf {
13341016Sdfr    int           count;	/* # of valid elements in the buffer */
13441016Sdfr    int           head;		/* head pointer */
13541016Sdfr    int           tail;		/* tail poiner */
13641016Sdfr    unsigned char buf[PSM_BUFSIZE];
13741016Sdfr} ringbuf_t;
13841016Sdfr
13941016Sdfr/* driver control block */
14041016Sdfrstruct psm_softc {		/* Driver status information */
14141016Sdfr    struct selinfo rsel;	/* Process selecting for Input */
14241016Sdfr    unsigned char state;	/* Mouse driver state */
14341016Sdfr    int           config;	/* driver configuration flags */
14441016Sdfr    int           flags;	/* other flags */
14541016Sdfr    KBDC          kbdc;		/* handle to access the keyboard controller */
14658230Syokota    struct resource *intr;	/* IRQ resource */
14758230Syokota    void	  *ih;		/* interrupt handle */
14841016Sdfr    mousehw_t     hw;		/* hardware information */
14941016Sdfr    mousemode_t   mode;		/* operation mode */
15041016Sdfr    mousemode_t   dflt_mode;	/* default operation mode */
15141016Sdfr    mousestatus_t status;	/* accumulated mouse movement */
15241016Sdfr    ringbuf_t     queue;	/* mouse status queue */
15341016Sdfr    unsigned char ipacket[16];	/* interim input buffer */
15441016Sdfr    int           inputbytes;	/* # of bytes in the input buffer */
15541016Sdfr    int           button;	/* the latest button state */
15649965Syokota    int		  xold;	/* previous absolute X position */
15749965Syokota    int		  yold;	/* previous absolute Y position */
15858230Syokota    int		  watchdog;	/* watchdog timer flag */
15958230Syokota    struct callout_handle callout;	/* watchdog timer call out */
16058230Syokota    dev_t	  dev;
16158230Syokota    dev_t	  bdev;
16241016Sdfr};
16341016Sdfrdevclass_t psm_devclass;
16441016Sdfr#define PSM_SOFTC(unit)	((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
16541016Sdfr
16641016Sdfr/* driver state flags (state) */
16741016Sdfr#define PSM_VALID		0x80
16841016Sdfr#define PSM_OPEN		1	/* Device is open */
16941016Sdfr#define PSM_ASLP		2	/* Waiting for mouse data */
17041016Sdfr
17141016Sdfr/* driver configuration flags (config) */
17241016Sdfr#define PSM_CONFIG_RESOLUTION	0x000f	/* resolution */
17341016Sdfr#define PSM_CONFIG_ACCEL	0x00f0  /* acceleration factor */
17441016Sdfr#define PSM_CONFIG_NOCHECKSYNC	0x0100  /* disable sync. test */
17545789Speter#define PSM_CONFIG_NOIDPROBE	0x0200  /* disable mouse model probe */
17645789Speter#define PSM_CONFIG_NORESET	0x0400  /* don't reset the mouse */
17745789Speter#define PSM_CONFIG_FORCETAP	0x0800  /* assume `tap' action exists */
17845789Speter#define PSM_CONFIG_IGNPORTERROR	0x1000  /* ignore error in aux port test */
17958230Syokota#define PSM_CONFIG_HOOKRESUME	0x2000	/* hook the system resume event */
18058230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
18141016Sdfr
18241016Sdfr#define PSM_CONFIG_FLAGS	(PSM_CONFIG_RESOLUTION 		\
18341016Sdfr				    | PSM_CONFIG_ACCEL		\
18445789Speter				    | PSM_CONFIG_NOCHECKSYNC	\
18545789Speter				    | PSM_CONFIG_NOIDPROBE	\
18645789Speter				    | PSM_CONFIG_NORESET	\
18745789Speter				    | PSM_CONFIG_FORCETAP	\
18858230Syokota				    | PSM_CONFIG_IGNPORTERROR	\
18958230Syokota				    | PSM_CONFIG_HOOKRESUME	\
19058230Syokota				    | PSM_CONFIG_INITAFTERSUSPEND)
19141016Sdfr
19241016Sdfr/* other flags (flags) */
19349965Syokota#define PSM_FLAGS_FINGERDOWN	0x0001 /* VersaPad finger down */
19441016Sdfr
19541016Sdfr/* for backward compatibility */
19641016Sdfr#define OLD_MOUSE_GETHWINFO	_IOR('M', 1, old_mousehw_t)
19741016Sdfr#define OLD_MOUSE_GETMODE	_IOR('M', 2, old_mousemode_t)
19841016Sdfr#define OLD_MOUSE_SETMODE	_IOW('M', 3, old_mousemode_t)
19941016Sdfr
20041016Sdfrtypedef struct old_mousehw {
20141016Sdfr    int buttons;
20241016Sdfr    int iftype;
20341016Sdfr    int type;
20441016Sdfr    int hwid;
20541016Sdfr} old_mousehw_t;
20641016Sdfr
20741016Sdfrtypedef struct old_mousemode {
20841016Sdfr    int protocol;
20941016Sdfr    int rate;
21041016Sdfr    int resolution;
21141016Sdfr    int accelfactor;
21241016Sdfr} old_mousemode_t;
21341016Sdfr
21441016Sdfr/* packet formatting function */
21541016Sdfrtypedef int packetfunc_t __P((struct psm_softc *, unsigned char *,
21641016Sdfr			      int *, int, mousestatus_t *));
21741016Sdfr
21841016Sdfr/* function prototypes */
21941016Sdfrstatic int psmprobe __P((device_t));
22041016Sdfrstatic int psmattach __P((device_t));
22158230Syokotastatic int psmdetach __P((device_t));
22254629Syokotastatic int psmresume __P((device_t));
22341016Sdfr
22441016Sdfrstatic d_open_t psmopen;
22541016Sdfrstatic d_close_t psmclose;
22641016Sdfrstatic d_read_t psmread;
22741016Sdfrstatic d_ioctl_t psmioctl;
22841016Sdfrstatic d_poll_t psmpoll;
22941016Sdfr
23041016Sdfrstatic int enable_aux_dev __P((KBDC));
23141016Sdfrstatic int disable_aux_dev __P((KBDC));
23241016Sdfrstatic int get_mouse_status __P((KBDC, int *, int, int));
23341016Sdfrstatic int get_aux_id __P((KBDC));
23441016Sdfrstatic int set_mouse_sampling_rate __P((KBDC, int));
23541016Sdfrstatic int set_mouse_scaling __P((KBDC, int));
23641016Sdfrstatic int set_mouse_resolution __P((KBDC, int));
23741016Sdfrstatic int set_mouse_mode __P((KBDC));
23841016Sdfrstatic int get_mouse_buttons __P((KBDC));
23941016Sdfrstatic int is_a_mouse __P((int));
24041016Sdfrstatic void recover_from_error __P((KBDC));
24141016Sdfrstatic int restore_controller __P((KBDC, int));
24241016Sdfrstatic int reinitialize __P((int, mousemode_t *));
24341016Sdfrstatic int doopen __P((int, int));
24458230Syokotastatic char *model_name __P((int));
24558230Syokotastatic void psmintr __P((void *));
24658230Syokotastatic void psmtimeout __P((void *));
24741016Sdfr
24841016Sdfr/* vendor specific features */
24941016Sdfrtypedef int probefunc_t __P((struct psm_softc *));
25041016Sdfr
25141016Sdfrstatic int mouse_id_proc1 __P((KBDC, int, int, int *));
25241016Sdfrstatic probefunc_t enable_groller;
25341016Sdfrstatic probefunc_t enable_gmouse;
25441016Sdfrstatic probefunc_t enable_aglide;
25541016Sdfrstatic probefunc_t enable_kmouse;
25658230Syokotastatic probefunc_t enable_msexplorer;
25741016Sdfrstatic probefunc_t enable_msintelli;
25858230Syokotastatic probefunc_t enable_4dmouse;
25958230Syokotastatic probefunc_t enable_4dplus;
26041016Sdfrstatic probefunc_t enable_mmanplus;
26149965Syokotastatic probefunc_t enable_versapad;
26241016Sdfrstatic int tame_mouse __P((struct psm_softc *, mousestatus_t *, unsigned char *));
26341016Sdfr
26441016Sdfrstatic struct {
26541016Sdfr    int                 model;
26641016Sdfr    unsigned char	syncmask;
26741016Sdfr    int 		packetsize;
26841016Sdfr    probefunc_t 	*probefunc;
26941016Sdfr} vendortype[] = {
27058230Syokota    /*
27158230Syokota     * WARNING: the order of probe is very important.  Don't mess it
27258230Syokota     * unless you know what you are doing.
27358230Syokota     */
27441016Sdfr    { MOUSE_MODEL_NET,			/* Genius NetMouse */
27558230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
27641016Sdfr    { MOUSE_MODEL_NETSCROLL,		/* Genius NetScroll */
27741016Sdfr      0xc8, 6, enable_groller, },
27858230Syokota    { MOUSE_MODEL_MOUSEMANPLUS,		/* Logitech MouseMan+ */
27958230Syokota      0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
28058230Syokota    { MOUSE_MODEL_EXPLORER,		/* Microsoft IntelliMouse Explorer */
28158230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
28258230Syokota    { MOUSE_MODEL_4D,			/* A4 Tech 4D Mouse */
28358230Syokota      0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
28458230Syokota    { MOUSE_MODEL_4DPLUS,		/* A4 Tech 4D+ Mouse */
28558230Syokota      0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
28658230Syokota    { MOUSE_MODEL_INTELLI,		/* Microsoft IntelliMouse */
28758230Syokota      0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
28841016Sdfr    { MOUSE_MODEL_GLIDEPOINT,		/* ALPS GlidePoint */
28941016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
29041016Sdfr    { MOUSE_MODEL_THINK,		/* Kensignton ThinkingMouse */
29141016Sdfr      0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
29249965Syokota    { MOUSE_MODEL_VERSAPAD,		/* Interlink electronics VersaPad */
29349965Syokota      0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
29441016Sdfr    { MOUSE_MODEL_GENERIC,
29541016Sdfr      0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
29641016Sdfr};
29749965Syokota#define GENERIC_MOUSE_ENTRY	7
29841016Sdfr
29941016Sdfr/* device driver declarateion */
30041016Sdfrstatic device_method_t psm_methods[] = {
30141016Sdfr	/* Device interface */
30241016Sdfr	DEVMETHOD(device_probe,		psmprobe),
30341016Sdfr	DEVMETHOD(device_attach,	psmattach),
30458230Syokota	DEVMETHOD(device_detach,	psmdetach),
30554629Syokota	DEVMETHOD(device_resume,	psmresume),
30641016Sdfr
30741016Sdfr	{ 0, 0 }
30841016Sdfr};
30941016Sdfr
31041016Sdfrstatic driver_t psm_driver = {
31141016Sdfr    "psm",
31241016Sdfr    psm_methods,
31341016Sdfr    sizeof(struct psm_softc),
31441016Sdfr};
31541016Sdfr
31658230Syokota#if notyet
31758230Syokotastatic struct isa_pnp_id psm_ids[] = {
31858230Syokota    { 0x130fd041, "PS/2 mouse port" },			/* PNP0F13 */
31958230Syokota    { 0x1303d041, "PS/2 port" },			/* PNP0313, XXX */
32058230Syokota    { 0 }
32158230Syokota};
32258230Syokota#endif
32358230Syokota
32441016Sdfr#define CDEV_MAJOR        21
32541016Sdfr
32647625Sphkstatic struct cdevsw psm_cdevsw = {
32747625Sphk	/* open */	psmopen,
32847625Sphk	/* close */	psmclose,
32947625Sphk	/* read */	psmread,
33047625Sphk	/* write */	nowrite,
33147625Sphk	/* ioctl */	psmioctl,
33247625Sphk	/* poll */	psmpoll,
33347625Sphk	/* mmap */	nommap,
33447625Sphk	/* strategy */	nostrategy,
33547625Sphk	/* name */	"psm",
33647625Sphk	/* maj */	CDEV_MAJOR,
33747625Sphk	/* dump */	nodump,
33847625Sphk	/* psize */	nopsize,
33947625Sphk	/* flags */	0,
34047625Sphk	/* bmaj */	-1
34141016Sdfr};
34241016Sdfr
34341016Sdfr/* debug message level */
34441016Sdfrstatic int verbose = PSM_DEBUG;
34541016Sdfr
34641016Sdfr/* device I/O routines */
34741016Sdfrstatic int
34841016Sdfrenable_aux_dev(KBDC kbdc)
34941016Sdfr{
35041016Sdfr    int res;
35141016Sdfr
35241016Sdfr    res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
35341016Sdfr    if (verbose >= 2)
35441016Sdfr        log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
35541016Sdfr
35641016Sdfr    return (res == PSM_ACK);
35741016Sdfr}
35841016Sdfr
35941016Sdfrstatic int
36041016Sdfrdisable_aux_dev(KBDC kbdc)
36141016Sdfr{
36241016Sdfr    int res;
36341016Sdfr
36441016Sdfr    res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
36541016Sdfr    if (verbose >= 2)
36641016Sdfr        log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
36741016Sdfr
36841016Sdfr    return (res == PSM_ACK);
36941016Sdfr}
37041016Sdfr
37141016Sdfrstatic int
37241016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len)
37341016Sdfr{
37441016Sdfr    int cmd;
37541016Sdfr    int res;
37641016Sdfr    int i;
37741016Sdfr
37841016Sdfr    switch (flag) {
37941016Sdfr    case 0:
38041016Sdfr    default:
38141016Sdfr	cmd = PSMC_SEND_DEV_STATUS;
38241016Sdfr	break;
38341016Sdfr    case 1:
38441016Sdfr	cmd = PSMC_SEND_DEV_DATA;
38541016Sdfr	break;
38641016Sdfr    }
38741016Sdfr    empty_aux_buffer(kbdc, 5);
38841016Sdfr    res = send_aux_command(kbdc, cmd);
38941016Sdfr    if (verbose >= 2)
39041016Sdfr        log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
39141016Sdfr	    (flag == 1) ? "DATA" : "STATUS", res);
39241016Sdfr    if (res != PSM_ACK)
39341016Sdfr        return 0;
39441016Sdfr
39541016Sdfr    for (i = 0; i < len; ++i) {
39641016Sdfr        status[i] = read_aux_data(kbdc);
39741016Sdfr	if (status[i] < 0)
39841016Sdfr	    break;
39941016Sdfr    }
40041016Sdfr
40141016Sdfr    if (verbose) {
40241016Sdfr        log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
40341016Sdfr            (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
40441016Sdfr    }
40541016Sdfr
40641016Sdfr    return i;
40741016Sdfr}
40841016Sdfr
40941016Sdfrstatic int
41041016Sdfrget_aux_id(KBDC kbdc)
41141016Sdfr{
41241016Sdfr    int res;
41341016Sdfr    int id;
41441016Sdfr
41541016Sdfr    empty_aux_buffer(kbdc, 5);
41641016Sdfr    res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
41741016Sdfr    if (verbose >= 2)
41841016Sdfr        log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
41941016Sdfr    if (res != PSM_ACK)
42041016Sdfr	return (-1);
42141016Sdfr
42241016Sdfr    /* 10ms delay */
42341016Sdfr    DELAY(10000);
42441016Sdfr
42541016Sdfr    id = read_aux_data(kbdc);
42641016Sdfr    if (verbose >= 2)
42741016Sdfr        log(LOG_DEBUG, "psm: device ID: %04x\n", id);
42841016Sdfr
42941016Sdfr    return id;
43041016Sdfr}
43141016Sdfr
43241016Sdfrstatic int
43341016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate)
43441016Sdfr{
43541016Sdfr    int res;
43641016Sdfr
43741016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
43841016Sdfr    if (verbose >= 2)
43941016Sdfr        log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
44041016Sdfr
44141016Sdfr    return ((res == PSM_ACK) ? rate : -1);
44241016Sdfr}
44341016Sdfr
44441016Sdfrstatic int
44541016Sdfrset_mouse_scaling(KBDC kbdc, int scale)
44641016Sdfr{
44741016Sdfr    int res;
44841016Sdfr
44941016Sdfr    switch (scale) {
45041016Sdfr    case 1:
45141016Sdfr    default:
45241016Sdfr	scale = PSMC_SET_SCALING11;
45341016Sdfr	break;
45441016Sdfr    case 2:
45541016Sdfr	scale = PSMC_SET_SCALING21;
45641016Sdfr	break;
45741016Sdfr    }
45841016Sdfr    res = send_aux_command(kbdc, scale);
45941016Sdfr    if (verbose >= 2)
46041016Sdfr        log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
46141016Sdfr	    (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
46241016Sdfr
46341016Sdfr    return (res == PSM_ACK);
46441016Sdfr}
46541016Sdfr
46641016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */
46741016Sdfrstatic int
46841016Sdfrset_mouse_resolution(KBDC kbdc, int val)
46941016Sdfr{
47041016Sdfr    int res;
47141016Sdfr
47241016Sdfr    res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
47341016Sdfr    if (verbose >= 2)
47441016Sdfr        log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
47541016Sdfr
47641016Sdfr    return ((res == PSM_ACK) ? val : -1);
47741016Sdfr}
47841016Sdfr
47941016Sdfr/*
48041016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be
48141016Sdfr * re-enabled by calling `enable_aux_dev()'
48241016Sdfr */
48341016Sdfrstatic int
48441016Sdfrset_mouse_mode(KBDC kbdc)
48541016Sdfr{
48641016Sdfr    int res;
48741016Sdfr
48841016Sdfr    res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
48941016Sdfr    if (verbose >= 2)
49041016Sdfr        log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
49141016Sdfr
49241016Sdfr    return (res == PSM_ACK);
49341016Sdfr}
49441016Sdfr
49541016Sdfrstatic int
49641016Sdfrget_mouse_buttons(KBDC kbdc)
49741016Sdfr{
49841016Sdfr    int c = 2;		/* assume two buttons by default */
49941016Sdfr    int status[3];
50041016Sdfr
50141016Sdfr    /*
50241016Sdfr     * NOTE: a special sequence to obtain Logitech Mouse specific
50341016Sdfr     * information: set resolution to 25 ppi, set scaling to 1:1, set
50441016Sdfr     * scaling to 1:1, set scaling to 1:1. Then the second byte of the
50541016Sdfr     * mouse status bytes is the number of available buttons.
50641016Sdfr     * Some manufactures also support this sequence.
50741016Sdfr     */
50841016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
50941016Sdfr        return c;
51041016Sdfr    if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
51141016Sdfr        && set_mouse_scaling(kbdc, 1)
51241016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
51341016Sdfr        if (status[1] != 0)
51441016Sdfr            return status[1];
51541016Sdfr    }
51641016Sdfr    return c;
51741016Sdfr}
51841016Sdfr
51941016Sdfr/* misc subroutines */
52041016Sdfr/*
52141016Sdfr * Someday, I will get the complete list of valid pointing devices and
52241016Sdfr * their IDs... XXX
52341016Sdfr */
52441016Sdfrstatic int
52541016Sdfris_a_mouse(int id)
52641016Sdfr{
52741016Sdfr#if 0
52841016Sdfr    static int valid_ids[] = {
52941016Sdfr        PSM_MOUSE_ID,		/* mouse */
53041016Sdfr        PSM_BALLPOINT_ID,	/* ballpoint device */
53141016Sdfr        PSM_INTELLI_ID,		/* Intellimouse */
53258230Syokota        PSM_EXPLORER_ID,	/* Intellimouse Explorer */
53341016Sdfr        -1			/* end of table */
53441016Sdfr    };
53541016Sdfr    int i;
53641016Sdfr
53741016Sdfr    for (i = 0; valid_ids[i] >= 0; ++i)
53841016Sdfr        if (valid_ids[i] == id)
53941016Sdfr            return TRUE;
54041016Sdfr    return FALSE;
54141016Sdfr#else
54241016Sdfr    return TRUE;
54341016Sdfr#endif
54441016Sdfr}
54541016Sdfr
54641016Sdfrstatic char *
54741016Sdfrmodel_name(int model)
54841016Sdfr{
54941016Sdfr    static struct {
55041016Sdfr	int model_code;
55141016Sdfr	char *model_name;
55241016Sdfr    } models[] = {
55358230Syokota        { MOUSE_MODEL_NETSCROLL,	"NetScroll" },
55458230Syokota        { MOUSE_MODEL_NET,		"NetMouse/NetScroll Optical" },
55541016Sdfr        { MOUSE_MODEL_GLIDEPOINT,	"GlidePoint" },
55641016Sdfr        { MOUSE_MODEL_THINK,		"ThinkingMouse" },
55741016Sdfr        { MOUSE_MODEL_INTELLI,		"IntelliMouse" },
55841016Sdfr        { MOUSE_MODEL_MOUSEMANPLUS,	"MouseMan+" },
55949965Syokota        { MOUSE_MODEL_VERSAPAD,		"VersaPad" },
56058230Syokota        { MOUSE_MODEL_EXPLORER,		"IntelliMouse Explorer" },
56158230Syokota        { MOUSE_MODEL_4D,		"4D Mouse" },
56258230Syokota        { MOUSE_MODEL_4DPLUS,		"4D+ Mouse" },
56341016Sdfr        { MOUSE_MODEL_GENERIC,		"Generic PS/2 mouse" },
56441016Sdfr        { MOUSE_MODEL_UNKNOWN,		NULL },
56541016Sdfr    };
56641016Sdfr    int i;
56741016Sdfr
56841016Sdfr    for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
56941016Sdfr	if (models[i].model_code == model)
57041016Sdfr	    return models[i].model_name;
57141016Sdfr    }
57241016Sdfr    return "Unknown";
57341016Sdfr}
57441016Sdfr
57541016Sdfrstatic void
57641016Sdfrrecover_from_error(KBDC kbdc)
57741016Sdfr{
57841016Sdfr    /* discard anything left in the output buffer */
57941016Sdfr    empty_both_buffers(kbdc, 10);
58041016Sdfr
58141016Sdfr#if 0
58241016Sdfr    /*
58341016Sdfr     * NOTE: KBDC_RESET_KBD may not restore the communication between the
58441016Sdfr     * keyboard and the controller.
58541016Sdfr     */
58641016Sdfr    reset_kbd(kbdc);
58741016Sdfr#else
58841016Sdfr    /*
58941016Sdfr     * NOTE: somehow diagnostic and keyboard port test commands bring the
59041016Sdfr     * keyboard back.
59141016Sdfr     */
59241016Sdfr    if (!test_controller(kbdc))
59341016Sdfr        log(LOG_ERR, "psm: keyboard controller failed.\n");
59441016Sdfr    /* if there isn't a keyboard in the system, the following error is OK */
59541016Sdfr    if (test_kbd_port(kbdc) != 0) {
59641016Sdfr	if (verbose)
59741016Sdfr	    log(LOG_ERR, "psm: keyboard port failed.\n");
59841016Sdfr    }
59941016Sdfr#endif
60041016Sdfr}
60141016Sdfr
60241016Sdfrstatic int
60341016Sdfrrestore_controller(KBDC kbdc, int command_byte)
60441016Sdfr{
60541016Sdfr    empty_both_buffers(kbdc, 10);
60641016Sdfr
60741016Sdfr    if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
60841016Sdfr	log(LOG_ERR, "psm: failed to restore the keyboard controller "
60941016Sdfr		     "command byte.\n");
61058230Syokota	empty_both_buffers(kbdc, 10);
61141016Sdfr	return FALSE;
61241016Sdfr    } else {
61358230Syokota	empty_both_buffers(kbdc, 10);
61441016Sdfr	return TRUE;
61541016Sdfr    }
61641016Sdfr}
61741016Sdfr
61841016Sdfr/*
61941016Sdfr * Re-initialize the aux port and device. The aux port must be enabled
62041016Sdfr * and its interrupt must be disabled before calling this routine.
62141016Sdfr * The aux device will be disabled before returning.
62241016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before
62341016Sdfr * calling this routine.
62441016Sdfr */
62541016Sdfrstatic int
62641016Sdfrreinitialize(int unit, mousemode_t *mode)
62741016Sdfr{
62841016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
62941016Sdfr    KBDC kbdc = sc->kbdc;
63041016Sdfr    int stat[3];
63141016Sdfr    int i;
63241016Sdfr
63341016Sdfr    switch((i = test_aux_port(kbdc))) {
63441016Sdfr    case 1:	/* ignore this error */
63541016Sdfr    case PSM_ACK:
63641016Sdfr	if (verbose)
63741016Sdfr	    log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
63841016Sdfr	        unit, i);
63941016Sdfr	/* fall though */
64041016Sdfr    case 0:	/* no error */
64141016Sdfr    	break;
64241016Sdfr    case -1: 	/* time out */
64341016Sdfr    default: 	/* error */
64441016Sdfr    	recover_from_error(kbdc);
64545789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
64645789Speter	    break;
64741016Sdfr    	log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
64841016Sdfr    	    unit, i);
64941016Sdfr    	return FALSE;
65041016Sdfr    }
65141016Sdfr
65245789Speter    if (sc->config & PSM_CONFIG_NORESET) {
65345789Speter	/*
65445789Speter	 * Don't try to reset the pointing device.  It may possibly be
65545789Speter	 * left in the unknown state, though...
65645789Speter	 */
65745789Speter    } else {
65845789Speter	/*
65945789Speter	 * NOTE: some controllers appears to hang the `keyboard' when
66045789Speter	 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
66145789Speter	 */
66245789Speter	if (!reset_aux_dev(kbdc)) {
66345789Speter            recover_from_error(kbdc);
66445789Speter            log(LOG_ERR, "psm%d: failed to reset the aux device.\n", unit);
66545789Speter            return FALSE;
66645789Speter	}
66741016Sdfr    }
66841016Sdfr
66941016Sdfr    /*
67041016Sdfr     * both the aux port and the aux device is functioning, see
67141016Sdfr     * if the device can be enabled.
67241016Sdfr     */
67341016Sdfr    if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
67441016Sdfr        log(LOG_ERR, "psm%d: failed to enable the aux device.\n", unit);
67541016Sdfr        return FALSE;
67641016Sdfr    }
67741016Sdfr    empty_both_buffers(kbdc, 10);	/* remove stray data if any */
67841016Sdfr
67945789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
68045789Speter	i = GENERIC_MOUSE_ENTRY;
68145789Speter    } else {
68245789Speter	/* FIXME: hardware ID, mouse buttons? */
68341016Sdfr
68445789Speter	/* other parameters */
68545789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
68645789Speter	    if ((*vendortype[i].probefunc)(sc)) {
68745789Speter		if (verbose >= 2)
68845789Speter		    log(LOG_ERR, "psm%d: found %s\n",
68945789Speter			unit, model_name(vendortype[i].model));
69045789Speter		break;
69145789Speter	    }
69241016Sdfr	}
69341016Sdfr    }
69441016Sdfr
69541016Sdfr    sc->hw.model = vendortype[i].model;
69641016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
69741016Sdfr
69841016Sdfr    /* set mouse parameters */
69941016Sdfr    if (mode != (mousemode_t *)NULL) {
70041016Sdfr	if (mode->rate > 0)
70141016Sdfr            mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
70241016Sdfr	if (mode->resolution >= 0)
70341016Sdfr            mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
70441016Sdfr        set_mouse_scaling(kbdc, 1);
70541016Sdfr        set_mouse_mode(kbdc);
70641016Sdfr    }
70741016Sdfr
70841016Sdfr    /* request a data packet and extract sync. bits */
70941016Sdfr    if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
71041016Sdfr        log(LOG_DEBUG, "psm%d: failed to get data (reinitialize).\n", unit);
71141016Sdfr        sc->mode.syncmask[0] = 0;
71241016Sdfr    } else {
71341016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
71441016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
71541016Sdfr	empty_aux_buffer(kbdc, 5);
71641016Sdfr    }
71741016Sdfr
71841016Sdfr    /* just check the status of the mouse */
71941016Sdfr    if (get_mouse_status(kbdc, stat, 0, 3) < 3)
72041016Sdfr        log(LOG_DEBUG, "psm%d: failed to get status (reinitialize).\n", unit);
72141016Sdfr
72241016Sdfr    return TRUE;
72341016Sdfr}
72441016Sdfr
72541016Sdfrstatic int
72641016Sdfrdoopen(int unit, int command_byte)
72741016Sdfr{
72841016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
72941016Sdfr    int stat[3];
73041016Sdfr
73141016Sdfr    /* enable the mouse device */
73241016Sdfr    if (!enable_aux_dev(sc->kbdc)) {
73341016Sdfr	/* MOUSE ERROR: failed to enable the mouse because:
73441016Sdfr	 * 1) the mouse is faulty,
73541016Sdfr	 * 2) the mouse has been removed(!?)
73641016Sdfr	 * In the latter case, the keyboard may have hung, and need
73741016Sdfr	 * recovery procedure...
73841016Sdfr	 */
73941016Sdfr	recover_from_error(sc->kbdc);
74041016Sdfr#if 0
74141016Sdfr	/* FIXME: we could reset the mouse here and try to enable
74241016Sdfr	 * it again. But it will take long time and it's not a good
74341016Sdfr	 * idea to disable the keyboard that long...
74441016Sdfr	 */
74541016Sdfr	if (!reinitialize(unit, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
74641016Sdfr	    recover_from_error(sc->kbdc);
74741016Sdfr#else
74841016Sdfr        {
74941016Sdfr#endif
75041016Sdfr            restore_controller(sc->kbdc, command_byte);
75141016Sdfr	    /* mark this device is no longer available */
75241016Sdfr	    sc->state &= ~PSM_VALID;
75341016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
75441016Sdfr		unit);
75541016Sdfr	    return (EIO);
75641016Sdfr	}
75741016Sdfr    }
75841016Sdfr
75941016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
76041016Sdfr        log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", unit);
76141016Sdfr
76241016Sdfr    /* enable the aux port and interrupt */
76341016Sdfr    if (!set_controller_command_byte(sc->kbdc,
76441016Sdfr	    kbdc_get_device_mask(sc->kbdc),
76541016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
76641016Sdfr		| KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
76741016Sdfr	/* CONTROLLER ERROR */
76841016Sdfr	disable_aux_dev(sc->kbdc);
76941016Sdfr        restore_controller(sc->kbdc, command_byte);
77041016Sdfr	log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
77141016Sdfr	    unit);
77241016Sdfr	return (EIO);
77341016Sdfr    }
77441016Sdfr
77558230Syokota    /* start the watchdog timer */
77658230Syokota    sc->watchdog = FALSE;
77758230Syokota    sc->callout = timeout(psmtimeout, (void *)unit, hz*2);
77858230Syokota
77941016Sdfr    return (0);
78041016Sdfr}
78141016Sdfr
78241016Sdfr/* psm driver entry points */
78341016Sdfr
78441016Sdfr#define endprobe(v)	{   if (bootverbose) 				\
78541016Sdfr				--verbose;   				\
78641016Sdfr                            kbdc_set_device_mask(sc->kbdc, mask);	\
78741016Sdfr			    kbdc_lock(sc->kbdc, FALSE);			\
78841016Sdfr			    return (v);	     				\
78941016Sdfr			}
79041016Sdfr
79141016Sdfrstatic int
79241016Sdfrpsmprobe(device_t dev)
79341016Sdfr{
79441016Sdfr    int unit = device_get_unit(dev);
79541016Sdfr    struct psm_softc *sc = device_get_softc(dev);
79658230Syokota    uintptr_t irq;
79745720Speter    uintptr_t flags;
79841016Sdfr    int stat[3];
79941016Sdfr    int command_byte;
80041016Sdfr    int mask;
80158230Syokota    int rid;
80241016Sdfr    int i;
80341016Sdfr
80441016Sdfr#if 0
80541016Sdfr    kbdc_debug(TRUE);
80641016Sdfr#endif
80758230Syokota
80858230Syokota#if notyet
80958230Syokota    /* check PnP IDs */
81058230Syokota    if (XXX_PNP_PROBE(device_get_parent(dev), dev, psm_ids) == ENXIO)
81158230Syokota	return ENXIO;
81258230Syokota#endif
81358230Syokota
81458230Syokota    BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
81543105Sdfr    BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
81643105Sdfr
81758230Syokota    sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
81843105Sdfr    sc->config = flags & PSM_CONFIG_FLAGS;
81958230Syokota    /* XXX: for backward compatibility */
82058230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
82158230Syokota    sc->config |=
82258230Syokota#ifdef PSM_RESETAFTERSUSPEND
82358230Syokota	PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
82458230Syokota#else
82558230Syokota	PSM_CONFIG_HOOKRESUME;
82658230Syokota#endif
82758230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
82841016Sdfr    sc->flags = 0;
82941016Sdfr    if (bootverbose)
83041016Sdfr        ++verbose;
83141016Sdfr
83243105Sdfr    device_set_desc(dev, "PS/2 Mouse");
83343105Sdfr
83441016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE)) {
83541016Sdfr        printf("psm%d: unable to lock the controller.\n", unit);
83641016Sdfr        if (bootverbose)
83741016Sdfr            --verbose;
83841016Sdfr	return (ENXIO);
83941016Sdfr    }
84041016Sdfr
84141016Sdfr    /*
84241016Sdfr     * NOTE: two bits in the command byte controls the operation of the
84341016Sdfr     * aux port (mouse port): the aux port disable bit (bit 5) and the aux
84441016Sdfr     * port interrupt (IRQ 12) enable bit (bit 2).
84541016Sdfr     */
84641016Sdfr
84741016Sdfr    /* discard anything left after the keyboard initialization */
84841016Sdfr    empty_both_buffers(sc->kbdc, 10);
84941016Sdfr
85041016Sdfr    /* save the current command byte; it will be used later */
85141016Sdfr    mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
85241016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
85341016Sdfr    if (verbose)
85441016Sdfr        printf("psm%d: current command byte:%04x\n", unit, command_byte);
85541016Sdfr    if (command_byte == -1) {
85641016Sdfr        /* CONTROLLER ERROR */
85741016Sdfr        printf("psm%d: unable to get the current command byte value.\n",
85841016Sdfr            unit);
85941016Sdfr        endprobe(ENXIO);
86041016Sdfr    }
86141016Sdfr
86241016Sdfr    /*
86341016Sdfr     * disable the keyboard port while probing the aux port, which must be
86441016Sdfr     * enabled during this routine
86541016Sdfr     */
86641016Sdfr    if (!set_controller_command_byte(sc->kbdc,
86741016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
86841016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
86941016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
87041016Sdfr        /*
87141016Sdfr	 * this is CONTROLLER ERROR; I don't know how to recover
87241016Sdfr         * from this error...
87341016Sdfr	 */
87441016Sdfr        restore_controller(sc->kbdc, command_byte);
87541016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
87641016Sdfr        endprobe(ENXIO);
87741016Sdfr    }
87845789Speter    write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
87941016Sdfr
88041016Sdfr    /*
88141016Sdfr     * NOTE: `test_aux_port()' is designed to return with zero if the aux
88241016Sdfr     * port exists and is functioning. However, some controllers appears
88341016Sdfr     * to respond with zero even when the aux port doesn't exist. (It may
88441016Sdfr     * be that this is only the case when the controller DOES have the aux
88541016Sdfr     * port but the port is not wired on the motherboard.) The keyboard
88641016Sdfr     * controllers without the port, such as the original AT, are
88741016Sdfr     * supporsed to return with an error code or simply time out. In any
88841016Sdfr     * case, we have to continue probing the port even when the controller
88941016Sdfr     * passes this test.
89041016Sdfr     *
89141016Sdfr     * XXX: some controllers erroneously return the error code 1 when
89241016Sdfr     * it has the perfectly functional aux port. We have to ignore this
89341016Sdfr     * error code. Even if the controller HAS error with the aux port,
89441016Sdfr     * it will be detected later...
89541016Sdfr     * XXX: another incompatible controller returns PSM_ACK (0xfa)...
89641016Sdfr     */
89741016Sdfr    switch ((i = test_aux_port(sc->kbdc))) {
89841016Sdfr    case 1:	   /* ignore this error */
89941016Sdfr    case PSM_ACK:
90041016Sdfr        if (verbose)
90141016Sdfr	    printf("psm%d: strange result for test aux port (%d).\n",
90241016Sdfr	        unit, i);
90341016Sdfr	/* fall though */
90441016Sdfr    case 0:        /* no error */
90541016Sdfr        break;
90641016Sdfr    case -1:        /* time out */
90741016Sdfr    default:        /* error */
90841016Sdfr        recover_from_error(sc->kbdc);
90945789Speter	if (sc->config & PSM_CONFIG_IGNPORTERROR)
91045789Speter	    break;
91141016Sdfr        restore_controller(sc->kbdc, command_byte);
91241016Sdfr        if (verbose)
91341016Sdfr            printf("psm%d: the aux port is not functioning (%d).\n",
91441016Sdfr                unit, i);
91541016Sdfr        endprobe(ENXIO);
91641016Sdfr    }
91741016Sdfr
91845789Speter    if (sc->config & PSM_CONFIG_NORESET) {
91945789Speter	/*
92045789Speter	 * Don't try to reset the pointing device.  It may possibly be
92145789Speter	 * left in the unknown state, though...
92245789Speter	 */
92345789Speter    } else {
92445789Speter	/*
92545789Speter	 * NOTE: some controllers appears to hang the `keyboard' when the aux
92645789Speter	 * port doesn't exist and `PSMC_RESET_DEV' is issued.
92745789Speter	 */
92845789Speter	if (!reset_aux_dev(sc->kbdc)) {
92945789Speter            recover_from_error(sc->kbdc);
93045789Speter            restore_controller(sc->kbdc, command_byte);
93145789Speter            if (verbose)
93245789Speter        	printf("psm%d: failed to reset the aux device.\n", unit);
93345789Speter            endprobe(ENXIO);
93445789Speter	}
93541016Sdfr    }
93645789Speter
93741016Sdfr    /*
93841016Sdfr     * both the aux port and the aux device is functioning, see if the
93941016Sdfr     * device can be enabled. NOTE: when enabled, the device will start
94041016Sdfr     * sending data; we shall immediately disable the device once we know
94141016Sdfr     * the device can be enabled.
94241016Sdfr     */
94341016Sdfr    if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
94445789Speter        /* MOUSE ERROR */
94545789Speter	recover_from_error(sc->kbdc);
94645789Speter	restore_controller(sc->kbdc, command_byte);
94745789Speter	if (verbose)
94845789Speter	    printf("psm%d: failed to enable the aux device.\n", unit);
94941016Sdfr        endprobe(ENXIO);
95041016Sdfr    }
95141016Sdfr
95241016Sdfr    /* save the default values after reset */
95341016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
95441016Sdfr	sc->dflt_mode.rate = sc->mode.rate = stat[2];
95541016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
95641016Sdfr    } else {
95741016Sdfr	sc->dflt_mode.rate = sc->mode.rate = -1;
95841016Sdfr	sc->dflt_mode.resolution = sc->mode.resolution = -1;
95941016Sdfr    }
96041016Sdfr
96141016Sdfr    /* hardware information */
96241016Sdfr    sc->hw.iftype = MOUSE_IF_PS2;
96341016Sdfr
96441016Sdfr    /* verify the device is a mouse */
96541016Sdfr    sc->hw.hwid = get_aux_id(sc->kbdc);
96641016Sdfr    if (!is_a_mouse(sc->hw.hwid)) {
96741016Sdfr        restore_controller(sc->kbdc, command_byte);
96841016Sdfr        if (verbose)
96941016Sdfr            printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
97041016Sdfr        endprobe(ENXIO);
97141016Sdfr    }
97241016Sdfr    switch (sc->hw.hwid) {
97341016Sdfr    case PSM_BALLPOINT_ID:
97441016Sdfr        sc->hw.type = MOUSE_TRACKBALL;
97541016Sdfr        break;
97641016Sdfr    case PSM_MOUSE_ID:
97741016Sdfr    case PSM_INTELLI_ID:
97858230Syokota    case PSM_EXPLORER_ID:
97958230Syokota    case PSM_4DMOUSE_ID:
98058230Syokota    case PSM_4DPLUS_ID:
98141016Sdfr        sc->hw.type = MOUSE_MOUSE;
98241016Sdfr        break;
98341016Sdfr    default:
98441016Sdfr        sc->hw.type = MOUSE_UNKNOWN;
98541016Sdfr        break;
98641016Sdfr    }
98741016Sdfr
98845789Speter    if (sc->config & PSM_CONFIG_NOIDPROBE) {
98945789Speter	sc->hw.buttons = 2;
99045789Speter	i = GENERIC_MOUSE_ENTRY;
99145789Speter    } else {
99245789Speter	/* # of buttons */
99345789Speter	sc->hw.buttons = get_mouse_buttons(sc->kbdc);
99441016Sdfr
99545789Speter	/* other parameters */
99645789Speter	for (i = 0; vendortype[i].probefunc != NULL; ++i) {
99745789Speter	    if ((*vendortype[i].probefunc)(sc)) {
99845789Speter		if (verbose >= 2)
99945789Speter		    printf("psm%d: found %s\n",
100045789Speter			   unit, model_name(vendortype[i].model));
100145789Speter		break;
100245789Speter	    }
100341016Sdfr	}
100441016Sdfr    }
100541016Sdfr
100641016Sdfr    sc->hw.model = vendortype[i].model;
100741016Sdfr
100841016Sdfr    sc->dflt_mode.level = PSM_LEVEL_BASE;
100941016Sdfr    sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
101041016Sdfr    sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
101141016Sdfr    if (sc->config & PSM_CONFIG_NOCHECKSYNC)
101241016Sdfr        sc->dflt_mode.syncmask[0] = 0;
101341016Sdfr    else
101441016Sdfr        sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
101545789Speter    if (sc->config & PSM_CONFIG_FORCETAP)
101645789Speter        sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
101741016Sdfr    sc->dflt_mode.syncmask[1] = 0;	/* syncbits */
101841016Sdfr    sc->mode = sc->dflt_mode;
101941016Sdfr    sc->mode.packetsize = vendortype[i].packetsize;
102041016Sdfr
102141016Sdfr    /* set mouse parameters */
102248773Syokota#if 0
102348773Syokota    /*
102448773Syokota     * A version of Logitech FirstMouse+ won't report wheel movement,
102548773Syokota     * if SET_DEFAULTS is sent...  Don't use this command.
102648773Syokota     * This fix was found by Takashi Nishida.
102748773Syokota     */
102841016Sdfr    i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
102941016Sdfr    if (verbose >= 2)
103041016Sdfr	printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
103148773Syokota#endif
103241016Sdfr    if (sc->config & PSM_CONFIG_RESOLUTION) {
103341016Sdfr        sc->mode.resolution
103441016Sdfr	    = set_mouse_resolution(sc->kbdc,
103548773Syokota				   (sc->config & PSM_CONFIG_RESOLUTION) - 1);
103648773Syokota    } else if (sc->mode.resolution >= 0) {
103748773Syokota	sc->mode.resolution
103848773Syokota	    = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
103941016Sdfr    }
104048773Syokota    if (sc->mode.rate > 0) {
104148773Syokota	sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
104248773Syokota    }
104348773Syokota    set_mouse_scaling(sc->kbdc, 1);
104441016Sdfr
104541016Sdfr    /* request a data packet and extract sync. bits */
104641016Sdfr    if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
104741016Sdfr        printf("psm%d: failed to get data.\n", unit);
104841016Sdfr        sc->mode.syncmask[0] = 0;
104941016Sdfr    } else {
105041016Sdfr        sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0];	/* syncbits */
105141016Sdfr	/* the NetScroll Mouse will send three more bytes... Ignore them */
105241016Sdfr	empty_aux_buffer(sc->kbdc, 5);
105341016Sdfr    }
105441016Sdfr
105541016Sdfr    /* just check the status of the mouse */
105641016Sdfr    /*
105741016Sdfr     * NOTE: XXX there are some arcane controller/mouse combinations out
105841016Sdfr     * there, which hung the controller unless there is data transmission
105941016Sdfr     * after ACK from the mouse.
106041016Sdfr     */
106141016Sdfr    if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
106241016Sdfr        printf("psm%d: failed to get status.\n", unit);
106341016Sdfr    } else {
106441016Sdfr	/*
106541016Sdfr	 * When in its native mode, some mice operate with different
106641016Sdfr	 * default parameters than in the PS/2 compatible mode.
106741016Sdfr	 */
106841016Sdfr        sc->dflt_mode.rate = sc->mode.rate = stat[2];
106941016Sdfr        sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
107041016Sdfr     }
107141016Sdfr
107241016Sdfr    /* disable the aux port for now... */
107341016Sdfr    if (!set_controller_command_byte(sc->kbdc,
107441016Sdfr	    KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
107541016Sdfr            (command_byte & KBD_KBD_CONTROL_BITS)
107641016Sdfr                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
107741016Sdfr        /*
107841016Sdfr	 * this is CONTROLLER ERROR; I don't know the proper way to
107941016Sdfr         * recover from this error...
108041016Sdfr	 */
108141016Sdfr        restore_controller(sc->kbdc, command_byte);
108241016Sdfr        printf("psm%d: unable to set the command byte.\n", unit);
108341016Sdfr        endprobe(ENXIO);
108441016Sdfr    }
108541016Sdfr
108658230Syokota    /* see if IRQ is available */
108758230Syokota    rid = 0;
108858230Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
108958230Syokota				  RF_ACTIVE);
109058230Syokota    if (sc->intr == NULL) {
109158230Syokota        printf("psm%d: unable to allocate the IRQ resource (%d).\n",
109258230Syokota	       unit, irq);
109358230Syokota        endprobe(ENXIO);
109458230Syokota    } else {
109558230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
109658230Syokota    }
109758230Syokota
109841016Sdfr    /* done */
109941016Sdfr    kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
110041016Sdfr    kbdc_lock(sc->kbdc, FALSE);
110141016Sdfr    return (0);
110241016Sdfr}
110341016Sdfr
110441016Sdfrstatic int
110541016Sdfrpsmattach(device_t dev)
110641016Sdfr{
110741016Sdfr    int unit = device_get_unit(dev);
110841016Sdfr    struct psm_softc *sc = device_get_softc(dev);
110945720Speter    uintptr_t irq;
111058230Syokota    int error;
111158230Syokota    int rid;
111241016Sdfr
111341016Sdfr    if (sc == NULL)    /* shouldn't happen */
111441016Sdfr	return (ENXIO);
111541016Sdfr
111641016Sdfr    /* Setup initial state */
111741016Sdfr    sc->state = PSM_VALID;
111858230Syokota    callout_handle_init(&sc->callout);
111941016Sdfr
112058230Syokota    /* Setup our interrupt handler */
112158230Syokota    rid = 0;
112258230Syokota    BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
112358230Syokota    sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
112458230Syokota				  RF_ACTIVE);
112558230Syokota    if (sc->intr == NULL)
112658230Syokota	return (ENXIO);
112758230Syokota    error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
112858230Syokota			   INTR_TYPE_TTY, psmintr, sc, &sc->ih);
112958230Syokota    if (error) {
113058230Syokota	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
113158230Syokota	return (error);
113258230Syokota    }
113358230Syokota
113441016Sdfr    /* Done */
113558230Syokota    sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
113658230Syokota		       "psm%d", unit);
113758230Syokota    sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
113858230Syokota			"bpsm%d", unit);
113941016Sdfr
114041016Sdfr    if (!verbose) {
114141016Sdfr        printf("psm%d: model %s, device ID %d\n",
114248778Syokota	    unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
114341016Sdfr    } else {
114448778Syokota        printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
114548778Syokota	    unit, model_name(sc->hw.model),
114648778Syokota	    sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
114741016Sdfr	printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
114841016Sdfr	    unit, sc->config, sc->flags, sc->mode.packetsize);
114941016Sdfr	printf("psm%d: syncmask:%02x, syncbits:%02x\n",
115041016Sdfr	    unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
115141016Sdfr    }
115241016Sdfr
115341016Sdfr    if (bootverbose)
115441016Sdfr        --verbose;
115541016Sdfr
115641016Sdfr    return (0);
115741016Sdfr}
115841016Sdfr
115941016Sdfrstatic int
116058230Syokotapsmdetach(device_t dev)
116158230Syokota{
116258230Syokota    struct psm_softc *sc;
116358230Syokota    int rid;
116458230Syokota
116558230Syokota    sc = device_get_softc(dev);
116658230Syokota    if (sc->state & PSM_OPEN)
116758230Syokota	return EBUSY;
116858230Syokota
116958230Syokota    rid = 0;
117058230Syokota    BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih);
117158230Syokota    bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
117258230Syokota
117358230Syokota    destroy_dev(sc->dev);
117458230Syokota    destroy_dev(sc->bdev);
117558230Syokota
117658230Syokota    return 0;
117758230Syokota}
117858230Syokota
117958230Syokotastatic int
118041016Sdfrpsmopen(dev_t dev, int flag, int fmt, struct proc *p)
118141016Sdfr{
118241016Sdfr    int unit = PSM_UNIT(dev);
118341016Sdfr    struct psm_softc *sc;
118441016Sdfr    int command_byte;
118541016Sdfr    int err;
118641016Sdfr    int s;
118741016Sdfr
118841016Sdfr    /* Get device data */
118941016Sdfr    sc = PSM_SOFTC(unit);
119041016Sdfr    if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
119141016Sdfr	/* the device is no longer valid/functioning */
119241016Sdfr        return (ENXIO);
119341016Sdfr
119441016Sdfr    /* Disallow multiple opens */
119541016Sdfr    if (sc->state & PSM_OPEN)
119641016Sdfr        return (EBUSY);
119741016Sdfr
119841016Sdfr    device_busy(devclass_get_device(psm_devclass, unit));
119941016Sdfr
120041016Sdfr    /* Initialize state */
120141016Sdfr    sc->rsel.si_flags = 0;
120241016Sdfr    sc->rsel.si_pid = 0;
120341016Sdfr    sc->mode.level = sc->dflt_mode.level;
120441016Sdfr    sc->mode.protocol = sc->dflt_mode.protocol;
120558230Syokota    sc->watchdog = FALSE;
120641016Sdfr
120741016Sdfr    /* flush the event queue */
120841016Sdfr    sc->queue.count = 0;
120941016Sdfr    sc->queue.head = 0;
121041016Sdfr    sc->queue.tail = 0;
121141016Sdfr    sc->status.flags = 0;
121241016Sdfr    sc->status.button = 0;
121341016Sdfr    sc->status.obutton = 0;
121441016Sdfr    sc->status.dx = 0;
121541016Sdfr    sc->status.dy = 0;
121641016Sdfr    sc->status.dz = 0;
121741016Sdfr    sc->button = 0;
121841016Sdfr
121941016Sdfr    /* empty input buffer */
122041016Sdfr    bzero(sc->ipacket, sizeof(sc->ipacket));
122141016Sdfr    sc->inputbytes = 0;
122241016Sdfr
122341016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
122441016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
122541016Sdfr	return (EIO);
122641016Sdfr
122741016Sdfr    /* save the current controller command byte */
122841016Sdfr    s = spltty();
122941016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
123041016Sdfr
123141016Sdfr    /* enable the aux port and temporalily disable the keyboard */
123241016Sdfr    if ((command_byte == -1)
123341016Sdfr        || !set_controller_command_byte(sc->kbdc,
123441016Sdfr	    kbdc_get_device_mask(sc->kbdc),
123541016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
123641016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
123741016Sdfr        /* CONTROLLER ERROR; do you know how to get out of this? */
123841016Sdfr        kbdc_lock(sc->kbdc, FALSE);
123941016Sdfr	splx(s);
124041016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
124141016Sdfr	    unit);
124241016Sdfr	return (EIO);
124341016Sdfr    }
124441016Sdfr    /*
124541016Sdfr     * Now that the keyboard controller is told not to generate
124641016Sdfr     * the keyboard and mouse interrupts, call `splx()' to allow
124741016Sdfr     * the other tty interrupts. The clock interrupt may also occur,
124841016Sdfr     * but timeout routines will be blocked by the poll flag set
124941016Sdfr     * via `kbdc_lock()'
125041016Sdfr     */
125141016Sdfr    splx(s);
125241016Sdfr
125341016Sdfr    /* enable the mouse device */
125441016Sdfr    err = doopen(unit, command_byte);
125541016Sdfr
125641016Sdfr    /* done */
125741016Sdfr    if (err == 0)
125841016Sdfr        sc->state |= PSM_OPEN;
125941016Sdfr    kbdc_lock(sc->kbdc, FALSE);
126041016Sdfr    return (err);
126141016Sdfr}
126241016Sdfr
126341016Sdfrstatic int
126441016Sdfrpsmclose(dev_t dev, int flag, int fmt, struct proc *p)
126541016Sdfr{
126641016Sdfr    int unit = PSM_UNIT(dev);
126741016Sdfr    struct psm_softc *sc = PSM_SOFTC(unit);
126841016Sdfr    int stat[3];
126941016Sdfr    int command_byte;
127041016Sdfr    int s;
127141016Sdfr
127241016Sdfr    /* don't let timeout routines in the keyboard driver to poll the kbdc */
127341016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
127441016Sdfr	return (EIO);
127541016Sdfr
127641016Sdfr    /* save the current controller command byte */
127741016Sdfr    s = spltty();
127841016Sdfr    command_byte = get_controller_command_byte(sc->kbdc);
127941016Sdfr    if (command_byte == -1) {
128041016Sdfr        kbdc_lock(sc->kbdc, FALSE);
128141016Sdfr	splx(s);
128241016Sdfr	return (EIO);
128341016Sdfr    }
128441016Sdfr
128541016Sdfr    /* disable the aux interrupt and temporalily disable the keyboard */
128641016Sdfr    if (!set_controller_command_byte(sc->kbdc,
128741016Sdfr	    kbdc_get_device_mask(sc->kbdc),
128841016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
128941016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
129041016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
129158230Syokota	    unit);
129241016Sdfr	/* CONTROLLER ERROR;
129341016Sdfr	 * NOTE: we shall force our way through. Because the only
129441016Sdfr	 * ill effect we shall see is that we may not be able
129541016Sdfr	 * to read ACK from the mouse, and it doesn't matter much
129641016Sdfr	 * so long as the mouse will accept the DISABLE command.
129741016Sdfr	 */
129841016Sdfr    }
129941016Sdfr    splx(s);
130041016Sdfr
130158230Syokota    /* stop the watchdog timer */
130258230Syokota    untimeout(psmtimeout, (void *)unit, sc->callout);
130358230Syokota    callout_handle_init(&sc->callout);
130458230Syokota
130541016Sdfr    /* remove anything left in the output buffer */
130641016Sdfr    empty_aux_buffer(sc->kbdc, 10);
130741016Sdfr
130841016Sdfr    /* disable the aux device, port and interrupt */
130941016Sdfr    if (sc->state & PSM_VALID) {
131041016Sdfr        if (!disable_aux_dev(sc->kbdc)) {
131141016Sdfr	    /* MOUSE ERROR;
131241016Sdfr	     * NOTE: we don't return error and continue, pretending
131341016Sdfr	     * we have successfully disabled the device. It's OK because
131441016Sdfr	     * the interrupt routine will discard any data from the mouse
131541016Sdfr	     * hereafter.
131641016Sdfr	     */
131741016Sdfr	    log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
131858230Syokota		unit);
131941016Sdfr        }
132041016Sdfr
132141016Sdfr        if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
132241016Sdfr            log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
132358230Syokota		unit);
132441016Sdfr    }
132541016Sdfr
132641016Sdfr    if (!set_controller_command_byte(sc->kbdc,
132741016Sdfr	    kbdc_get_device_mask(sc->kbdc),
132841016Sdfr	    (command_byte & KBD_KBD_CONTROL_BITS)
132941016Sdfr	        | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
133041016Sdfr	/* CONTROLLER ERROR;
133141016Sdfr	 * we shall ignore this error; see the above comment.
133241016Sdfr	 */
133341016Sdfr	log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
133458230Syokota	    unit);
133541016Sdfr    }
133641016Sdfr
133741016Sdfr    /* remove anything left in the output buffer */
133841016Sdfr    empty_aux_buffer(sc->kbdc, 10);
133941016Sdfr
134041016Sdfr    /* close is almost always successful */
134141016Sdfr    sc->state &= ~PSM_OPEN;
134241016Sdfr    kbdc_lock(sc->kbdc, FALSE);
134341016Sdfr    device_unbusy(devclass_get_device(psm_devclass, unit));
134441016Sdfr    return (0);
134541016Sdfr}
134641016Sdfr
134741016Sdfrstatic int
134841016Sdfrtame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
134941016Sdfr{
135041016Sdfr    static unsigned char butmapps2[8] = {
135141016Sdfr        0,
135241016Sdfr        MOUSE_PS2_BUTTON1DOWN,
135341016Sdfr        MOUSE_PS2_BUTTON2DOWN,
135441016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
135541016Sdfr        MOUSE_PS2_BUTTON3DOWN,
135641016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
135741016Sdfr        MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
135841016Sdfr        MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
135941016Sdfr    };
136041016Sdfr    static unsigned char butmapmsc[8] = {
136141016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
136241016Sdfr        MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
136341016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
136441016Sdfr        MOUSE_MSC_BUTTON3UP,
136541016Sdfr        MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
136641016Sdfr        MOUSE_MSC_BUTTON2UP,
136741016Sdfr        MOUSE_MSC_BUTTON1UP,
136841016Sdfr        0,
136941016Sdfr    };
137041016Sdfr    int mapped;
137141016Sdfr    int i;
137241016Sdfr
137341016Sdfr    if (sc->mode.level == PSM_LEVEL_BASE) {
137441016Sdfr        mapped = status->button & ~MOUSE_BUTTON4DOWN;
137541016Sdfr        if (status->button & MOUSE_BUTTON4DOWN)
137641016Sdfr	    mapped |= MOUSE_BUTTON1DOWN;
137741016Sdfr        status->button = mapped;
137841016Sdfr        buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
137941016Sdfr        i = max(min(status->dx, 255), -256);
138041016Sdfr	if (i < 0)
138141016Sdfr	    buf[0] |= MOUSE_PS2_XNEG;
138241016Sdfr        buf[1] = i;
138341016Sdfr        i = max(min(status->dy, 255), -256);
138441016Sdfr	if (i < 0)
138541016Sdfr	    buf[0] |= MOUSE_PS2_YNEG;
138641016Sdfr        buf[2] = i;
138741016Sdfr	return MOUSE_PS2_PACKETSIZE;
138841016Sdfr    } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
138941016Sdfr        buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
139041016Sdfr        i = max(min(status->dx, 255), -256);
139141016Sdfr        buf[1] = i >> 1;
139241016Sdfr        buf[3] = i - buf[1];
139341016Sdfr        i = max(min(status->dy, 255), -256);
139441016Sdfr        buf[2] = i >> 1;
139541016Sdfr        buf[4] = i - buf[2];
139641016Sdfr        i = max(min(status->dz, 127), -128);
139741016Sdfr        buf[5] = (i >> 1) & 0x7f;
139841016Sdfr        buf[6] = (i - (i >> 1)) & 0x7f;
139941016Sdfr        buf[7] = (~status->button >> 3) & 0x7f;
140041016Sdfr	return MOUSE_SYS_PACKETSIZE;
140141016Sdfr    }
140241016Sdfr    return sc->inputbytes;;
140341016Sdfr}
140441016Sdfr
140541016Sdfrstatic int
140641016Sdfrpsmread(dev_t dev, struct uio *uio, int flag)
140741016Sdfr{
140841016Sdfr    register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
140941016Sdfr    unsigned char buf[PSM_SMALLBUFSIZE];
141041016Sdfr    int error = 0;
141141016Sdfr    int s;
141241016Sdfr    int l;
141341016Sdfr
141441016Sdfr    if ((sc->state & PSM_VALID) == 0)
141541016Sdfr	return EIO;
141641016Sdfr
141741016Sdfr    /* block until mouse activity occured */
141841016Sdfr    s = spltty();
141941016Sdfr    while (sc->queue.count <= 0) {
142041016Sdfr        if (PSM_NBLOCKIO(dev)) {
142141016Sdfr            splx(s);
142241016Sdfr            return EWOULDBLOCK;
142341016Sdfr        }
142441016Sdfr        sc->state |= PSM_ASLP;
142541016Sdfr        error = tsleep((caddr_t) sc, PZERO | PCATCH, "psmrea", 0);
142641016Sdfr        sc->state &= ~PSM_ASLP;
142741016Sdfr        if (error) {
142841016Sdfr            splx(s);
142941016Sdfr            return error;
143041016Sdfr        } else if ((sc->state & PSM_VALID) == 0) {
143141016Sdfr            /* the device disappeared! */
143241016Sdfr            splx(s);
143341016Sdfr            return EIO;
143441016Sdfr	}
143541016Sdfr    }
143641016Sdfr    splx(s);
143741016Sdfr
143841016Sdfr    /* copy data to the user land */
143941016Sdfr    while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
144041016Sdfr        s = spltty();
144141016Sdfr	l = min(sc->queue.count, uio->uio_resid);
144241016Sdfr	if (l > sizeof(buf))
144341016Sdfr	    l = sizeof(buf);
144441016Sdfr	if (l > sizeof(sc->queue.buf) - sc->queue.head) {
144541016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
144641016Sdfr		sizeof(sc->queue.buf) - sc->queue.head);
144741016Sdfr	    bcopy(&sc->queue.buf[0],
144841016Sdfr		&buf[sizeof(sc->queue.buf) - sc->queue.head],
144941016Sdfr		l - (sizeof(sc->queue.buf) - sc->queue.head));
145041016Sdfr	} else {
145141016Sdfr	    bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
145241016Sdfr	}
145341016Sdfr	sc->queue.count -= l;
145441016Sdfr	sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
145541016Sdfr        splx(s);
145641016Sdfr        error = uiomove(buf, l, uio);
145741016Sdfr        if (error)
145841016Sdfr	    break;
145941016Sdfr    }
146041016Sdfr
146141016Sdfr    return error;
146241016Sdfr}
146341016Sdfr
146441016Sdfrstatic int
146541016Sdfrblock_mouse_data(struct psm_softc *sc, int *c)
146641016Sdfr{
146741016Sdfr    int s;
146841016Sdfr
146941016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
147041016Sdfr	return EIO;
147141016Sdfr
147241016Sdfr    s = spltty();
147341016Sdfr    *c = get_controller_command_byte(sc->kbdc);
147441016Sdfr    if ((*c == -1)
147541016Sdfr	|| !set_controller_command_byte(sc->kbdc,
147641016Sdfr	    kbdc_get_device_mask(sc->kbdc),
147741016Sdfr            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
147841016Sdfr                | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
147941016Sdfr        /* this is CONTROLLER ERROR */
148041016Sdfr	splx(s);
148141016Sdfr        kbdc_lock(sc->kbdc, FALSE);
148241016Sdfr	return EIO;
148341016Sdfr    }
148441016Sdfr
148541016Sdfr    /*
148641016Sdfr     * The device may be in the middle of status data transmission.
148741016Sdfr     * The transmission will be interrupted, thus, incomplete status
148841016Sdfr     * data must be discarded. Although the aux interrupt is disabled
148941016Sdfr     * at the keyboard controller level, at most one aux interrupt
149041016Sdfr     * may have already been pending and a data byte is in the
149141016Sdfr     * output buffer; throw it away. Note that the second argument
149241016Sdfr     * to `empty_aux_buffer()' is zero, so that the call will just
149341016Sdfr     * flush the internal queue.
149441016Sdfr     * `psmintr()' will be invoked after `splx()' if an interrupt is
149541016Sdfr     * pending; it will see no data and returns immediately.
149641016Sdfr     */
149741016Sdfr    empty_aux_buffer(sc->kbdc, 0);	/* flush the queue */
149841016Sdfr    read_aux_data_no_wait(sc->kbdc);	/* throw away data if any */
149941016Sdfr    sc->inputbytes = 0;
150041016Sdfr    splx(s);
150141016Sdfr
150241016Sdfr    return 0;
150341016Sdfr}
150441016Sdfr
150541016Sdfrstatic int
150641016Sdfrunblock_mouse_data(struct psm_softc *sc, int c)
150741016Sdfr{
150841016Sdfr    int error = 0;
150941016Sdfr
151041016Sdfr    /*
151141016Sdfr     * We may have seen a part of status data during `set_mouse_XXX()'.
151241016Sdfr     * they have been queued; flush it.
151341016Sdfr     */
151441016Sdfr    empty_aux_buffer(sc->kbdc, 0);
151541016Sdfr
151641016Sdfr    /* restore ports and interrupt */
151741016Sdfr    if (!set_controller_command_byte(sc->kbdc,
151841016Sdfr            kbdc_get_device_mask(sc->kbdc),
151941016Sdfr	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
152041016Sdfr        /* CONTROLLER ERROR; this is serious, we may have
152141016Sdfr         * been left with the inaccessible keyboard and
152241016Sdfr         * the disabled mouse interrupt.
152341016Sdfr         */
152441016Sdfr        error = EIO;
152541016Sdfr    }
152641016Sdfr
152741016Sdfr    kbdc_lock(sc->kbdc, FALSE);
152841016Sdfr    return error;
152941016Sdfr}
153041016Sdfr
153141016Sdfrstatic int
153241016Sdfrpsmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
153341016Sdfr{
153441016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
153541016Sdfr    mousemode_t mode;
153641016Sdfr    mousestatus_t status;
153741016Sdfr#if (defined(MOUSE_GETVARS))
153841016Sdfr    mousevar_t *var;
153941016Sdfr#endif
154041016Sdfr    mousedata_t *data;
154141016Sdfr    int stat[3];
154241016Sdfr    int command_byte;
154341016Sdfr    int error = 0;
154441016Sdfr    int s;
154541016Sdfr
154641016Sdfr    /* Perform IOCTL command */
154741016Sdfr    switch (cmd) {
154841016Sdfr
154941016Sdfr    case OLD_MOUSE_GETHWINFO:
155041016Sdfr	s = spltty();
155141016Sdfr        ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
155241016Sdfr        ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
155341016Sdfr        ((old_mousehw_t *)addr)->type = sc->hw.type;
155448778Syokota        ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
155541016Sdfr	splx(s);
155641016Sdfr        break;
155741016Sdfr
155841016Sdfr    case MOUSE_GETHWINFO:
155941016Sdfr	s = spltty();
156041016Sdfr        *(mousehw_t *)addr = sc->hw;
156141016Sdfr	if (sc->mode.level == PSM_LEVEL_BASE)
156241016Sdfr	    ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
156341016Sdfr	splx(s);
156441016Sdfr        break;
156541016Sdfr
156641016Sdfr    case OLD_MOUSE_GETMODE:
156741016Sdfr	s = spltty();
156841016Sdfr	switch (sc->mode.level) {
156941016Sdfr	case PSM_LEVEL_BASE:
157041016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
157141016Sdfr	    break;
157241016Sdfr	case PSM_LEVEL_STANDARD:
157341016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
157441016Sdfr	    break;
157541016Sdfr	case PSM_LEVEL_NATIVE:
157641016Sdfr	    ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
157741016Sdfr	    break;
157841016Sdfr	}
157941016Sdfr        ((old_mousemode_t *)addr)->rate = sc->mode.rate;
158041016Sdfr        ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
158141016Sdfr        ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
158241016Sdfr	splx(s);
158341016Sdfr        break;
158441016Sdfr
158541016Sdfr    case MOUSE_GETMODE:
158641016Sdfr	s = spltty();
158741016Sdfr        *(mousemode_t *)addr = sc->mode;
158841016Sdfr        ((mousemode_t *)addr)->resolution =
158941016Sdfr	    MOUSE_RES_LOW - sc->mode.resolution;
159041016Sdfr	switch (sc->mode.level) {
159141016Sdfr	case PSM_LEVEL_BASE:
159241016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
159341016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
159441016Sdfr	    break;
159541016Sdfr	case PSM_LEVEL_STANDARD:
159641016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
159741016Sdfr	    ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
159841016Sdfr	    ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
159941016Sdfr	    ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
160041016Sdfr	    break;
160141016Sdfr	case PSM_LEVEL_NATIVE:
160241016Sdfr	    /* FIXME: this isn't quite correct... XXX */
160341016Sdfr	    ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
160441016Sdfr	    break;
160541016Sdfr	}
160641016Sdfr	splx(s);
160741016Sdfr        break;
160841016Sdfr
160941016Sdfr    case OLD_MOUSE_SETMODE:
161041016Sdfr    case MOUSE_SETMODE:
161141016Sdfr	if (cmd == OLD_MOUSE_SETMODE) {
161241016Sdfr	    mode.rate = ((old_mousemode_t *)addr)->rate;
161341016Sdfr	    /*
161441016Sdfr	     * resolution  old I/F   new I/F
161541016Sdfr	     * default        0         0
161641016Sdfr	     * low            1        -2
161741016Sdfr	     * medium low     2        -3
161841016Sdfr	     * medium high    3        -4
161941016Sdfr	     * high           4        -5
162041016Sdfr	     */
162141016Sdfr	    if (((old_mousemode_t *)addr)->resolution > 0)
162241016Sdfr	        mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
162341016Sdfr	    mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
162441016Sdfr	    mode.level = -1;
162541016Sdfr	} else {
162641016Sdfr	    mode = *(mousemode_t *)addr;
162741016Sdfr	}
162841016Sdfr
162941016Sdfr	/* adjust and validate parameters. */
163041016Sdfr	if (mode.rate > UCHAR_MAX)
163141016Sdfr	    return EINVAL;
163241016Sdfr        if (mode.rate == 0)
163341016Sdfr            mode.rate = sc->dflt_mode.rate;
163441016Sdfr	else if (mode.rate == -1)
163541016Sdfr	    /* don't change the current setting */
163641016Sdfr	    ;
163741016Sdfr	else if (mode.rate < 0)
163841016Sdfr	    return EINVAL;
163941016Sdfr	if (mode.resolution >= UCHAR_MAX)
164041016Sdfr	    return EINVAL;
164141016Sdfr	if (mode.resolution >= 200)
164241016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
164341016Sdfr	else if (mode.resolution >= 100)
164441016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
164541016Sdfr	else if (mode.resolution >= 50)
164641016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
164741016Sdfr	else if (mode.resolution > 0)
164841016Sdfr	    mode.resolution = MOUSE_RES_LOW;
164941016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
165041016Sdfr            mode.resolution = sc->dflt_mode.resolution;
165141016Sdfr        else if (mode.resolution == -1)
165241016Sdfr	    /* don't change the current setting */
165341016Sdfr	    ;
165441016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
165541016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
165641016Sdfr	if (mode.level == -1)
165741016Sdfr	    /* don't change the current setting */
165841016Sdfr	    mode.level = sc->mode.level;
165941016Sdfr	else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
166041016Sdfr	    return EINVAL;
166141016Sdfr        if (mode.accelfactor == -1)
166241016Sdfr	    /* don't change the current setting */
166341016Sdfr	    mode.accelfactor = sc->mode.accelfactor;
166441016Sdfr        else if (mode.accelfactor < 0)
166541016Sdfr	    return EINVAL;
166641016Sdfr
166741016Sdfr	/* don't allow anybody to poll the keyboard controller */
166841016Sdfr	error = block_mouse_data(sc, &command_byte);
166941016Sdfr	if (error)
167041016Sdfr            return error;
167141016Sdfr
167241016Sdfr        /* set mouse parameters */
167341016Sdfr	if (mode.rate > 0)
167441016Sdfr	    mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
167541016Sdfr	if (mode.resolution >= 0)
167641016Sdfr	    mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
167741016Sdfr	set_mouse_scaling(sc->kbdc, 1);
167841016Sdfr	get_mouse_status(sc->kbdc, stat, 0, 3);
167941016Sdfr
168041016Sdfr        s = spltty();
168141016Sdfr    	sc->mode.rate = mode.rate;
168241016Sdfr    	sc->mode.resolution = mode.resolution;
168341016Sdfr    	sc->mode.accelfactor = mode.accelfactor;
168441016Sdfr    	sc->mode.level = mode.level;
168541016Sdfr        splx(s);
168641016Sdfr
168741016Sdfr	unblock_mouse_data(sc, command_byte);
168841016Sdfr        break;
168941016Sdfr
169041016Sdfr    case MOUSE_GETLEVEL:
169141016Sdfr	*(int *)addr = sc->mode.level;
169241016Sdfr        break;
169341016Sdfr
169441016Sdfr    case MOUSE_SETLEVEL:
169541016Sdfr	if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
169641016Sdfr	    return EINVAL;
169741016Sdfr	sc->mode.level = *(int *)addr;
169841016Sdfr        break;
169941016Sdfr
170041016Sdfr    case MOUSE_GETSTATUS:
170141016Sdfr        s = spltty();
170241016Sdfr	status = sc->status;
170341016Sdfr	sc->status.flags = 0;
170441016Sdfr	sc->status.obutton = sc->status.button;
170541016Sdfr	sc->status.button = 0;
170641016Sdfr	sc->status.dx = 0;
170741016Sdfr	sc->status.dy = 0;
170841016Sdfr	sc->status.dz = 0;
170941016Sdfr        splx(s);
171041016Sdfr        *(mousestatus_t *)addr = status;
171141016Sdfr        break;
171241016Sdfr
171341016Sdfr#if (defined(MOUSE_GETVARS))
171441016Sdfr    case MOUSE_GETVARS:
171541016Sdfr	var = (mousevar_t *)addr;
171641016Sdfr	bzero(var, sizeof(*var));
171741016Sdfr	s = spltty();
171841016Sdfr        var->var[0] = MOUSE_VARS_PS2_SIG;
171941016Sdfr        var->var[1] = sc->config;
172041016Sdfr        var->var[2] = sc->flags;
172141016Sdfr	splx(s);
172241016Sdfr        break;
172341016Sdfr
172441016Sdfr    case MOUSE_SETVARS:
172541016Sdfr	return ENODEV;
172641016Sdfr#endif /* MOUSE_GETVARS */
172741016Sdfr
172841016Sdfr    case MOUSE_READSTATE:
172941016Sdfr    case MOUSE_READDATA:
173041016Sdfr	data = (mousedata_t *)addr;
173141016Sdfr	if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
173241016Sdfr	    return EINVAL;
173341016Sdfr
173441016Sdfr	error = block_mouse_data(sc, &command_byte);
173541016Sdfr	if (error)
173641016Sdfr            return error;
173741016Sdfr        if ((data->len = get_mouse_status(sc->kbdc, data->buf,
173841016Sdfr		(cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
173941016Sdfr            error = EIO;
174041016Sdfr	unblock_mouse_data(sc, command_byte);
174141016Sdfr	break;
174241016Sdfr
174341016Sdfr#if (defined(MOUSE_SETRESOLUTION))
174441016Sdfr    case MOUSE_SETRESOLUTION:
174541016Sdfr	mode.resolution = *(int *)addr;
174641016Sdfr	if (mode.resolution >= UCHAR_MAX)
174741016Sdfr	    return EINVAL;
174841016Sdfr	else if (mode.resolution >= 200)
174941016Sdfr	    mode.resolution = MOUSE_RES_HIGH;
175041016Sdfr	else if (mode.resolution >= 100)
175141016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMHIGH;
175241016Sdfr	else if (mode.resolution >= 50)
175341016Sdfr	    mode.resolution = MOUSE_RES_MEDIUMLOW;
175441016Sdfr	else if (mode.resolution > 0)
175541016Sdfr	    mode.resolution = MOUSE_RES_LOW;
175641016Sdfr        if (mode.resolution == MOUSE_RES_DEFAULT)
175741016Sdfr            mode.resolution = sc->dflt_mode.resolution;
175841016Sdfr        else if (mode.resolution == -1)
175941016Sdfr	    mode.resolution = sc->mode.resolution;
176041016Sdfr        else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
176141016Sdfr            mode.resolution = MOUSE_RES_LOW - mode.resolution;
176241016Sdfr
176341016Sdfr	error = block_mouse_data(sc, &command_byte);
176441016Sdfr	if (error)
176541016Sdfr            return error;
176641016Sdfr        sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
176741016Sdfr	if (sc->mode.resolution != mode.resolution)
176841016Sdfr	    error = EIO;
176941016Sdfr	unblock_mouse_data(sc, command_byte);
177041016Sdfr        break;
177141016Sdfr#endif /* MOUSE_SETRESOLUTION */
177241016Sdfr
177341016Sdfr#if (defined(MOUSE_SETRATE))
177441016Sdfr    case MOUSE_SETRATE:
177541016Sdfr	mode.rate = *(int *)addr;
177641016Sdfr	if (mode.rate > UCHAR_MAX)
177741016Sdfr	    return EINVAL;
177841016Sdfr        if (mode.rate == 0)
177941016Sdfr            mode.rate = sc->dflt_mode.rate;
178041016Sdfr	else if (mode.rate < 0)
178141016Sdfr	    mode.rate = sc->mode.rate;
178241016Sdfr
178341016Sdfr	error = block_mouse_data(sc, &command_byte);
178441016Sdfr	if (error)
178541016Sdfr            return error;
178641016Sdfr        sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
178741016Sdfr	if (sc->mode.rate != mode.rate)
178841016Sdfr	    error = EIO;
178941016Sdfr	unblock_mouse_data(sc, command_byte);
179041016Sdfr        break;
179141016Sdfr#endif /* MOUSE_SETRATE */
179241016Sdfr
179341016Sdfr#if (defined(MOUSE_SETSCALING))
179441016Sdfr    case MOUSE_SETSCALING:
179541016Sdfr	if ((*(int *)addr <= 0) || (*(int *)addr > 2))
179641016Sdfr	    return EINVAL;
179741016Sdfr
179841016Sdfr	error = block_mouse_data(sc, &command_byte);
179941016Sdfr	if (error)
180041016Sdfr            return error;
180141016Sdfr        if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
180241016Sdfr	    error = EIO;
180341016Sdfr	unblock_mouse_data(sc, command_byte);
180441016Sdfr        break;
180541016Sdfr#endif /* MOUSE_SETSCALING */
180641016Sdfr
180741016Sdfr#if (defined(MOUSE_GETHWID))
180841016Sdfr    case MOUSE_GETHWID:
180941016Sdfr	error = block_mouse_data(sc, &command_byte);
181041016Sdfr	if (error)
181141016Sdfr            return error;
181248778Syokota        sc->hw.hwid &= ~0x00ff;
181348778Syokota        sc->hw.hwid |= get_aux_id(sc->kbdc);
181448778Syokota	*(int *)addr = sc->hw.hwid & 0x00ff;
181541016Sdfr	unblock_mouse_data(sc, command_byte);
181641016Sdfr        break;
181741016Sdfr#endif /* MOUSE_GETHWID */
181841016Sdfr
181941016Sdfr    default:
182041016Sdfr	return ENOTTY;
182141016Sdfr    }
182241016Sdfr
182341016Sdfr    return error;
182441016Sdfr}
182541016Sdfr
182641016Sdfrstatic void
182758230Syokotapsmtimeout(void *arg)
182858230Syokota{
182958230Syokota    struct psm_softc *sc;
183058230Syokota    int unit;
183158230Syokota
183258230Syokota    unit = (int)arg;
183358230Syokota    sc = devclass_get_softc(psm_devclass, unit);
183458230Syokota    if (sc->watchdog) {
183558230Syokota	if (verbose >= 4)
183658230Syokota	    log(LOG_DEBUG, "psm%d: lost interrupt?\n", unit);
183758230Syokota	psmintr(sc);
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        /*
188541016Sdfr	 * Check sync bits. We check for overflow bits and the bit 3
188641016Sdfr	 * for most mice. True, the code doesn't work if overflow
188741016Sdfr	 * condition occurs. But we expect it rarely happens...
188841016Sdfr	 */
188941016Sdfr	if ((sc->inputbytes == 0)
189041016Sdfr		&& ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1])) {
189141016Sdfr            log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
189241016Sdfr		c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
189341016Sdfr            continue;
189441016Sdfr	}
189541016Sdfr
189641016Sdfr        sc->ipacket[sc->inputbytes++] = c;
189741016Sdfr        if (sc->inputbytes < sc->mode.packetsize)
189841016Sdfr	    continue;
189941016Sdfr
190041016Sdfr#if 0
190141016Sdfr        log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
190241016Sdfr	    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
190341016Sdfr	    sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
190441016Sdfr#endif
190541016Sdfr
190641016Sdfr	c = sc->ipacket[0];
190741016Sdfr
190849965Syokota	/*
190941016Sdfr	 * A kludge for Kensington device!
191041016Sdfr	 * The MSB of the horizontal count appears to be stored in
191158230Syokota	 * a strange place.
191241016Sdfr	 */
191358230Syokota	if (sc->hw.model == MOUSE_MODEL_THINK)
191449965Syokota	    sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
191541016Sdfr
191641016Sdfr        /* ignore the overflow bits... */
191741016Sdfr        x = (c & MOUSE_PS2_XNEG) ?  sc->ipacket[1] - 256 : sc->ipacket[1];
191841016Sdfr        y = (c & MOUSE_PS2_YNEG) ?  sc->ipacket[2] - 256 : sc->ipacket[2];
191941016Sdfr	z = 0;
192041016Sdfr        ms.obutton = sc->button;		  /* previous button state */
192141016Sdfr        ms.button = butmap[c & MOUSE_PS2_BUTTONS];
192245789Speter	/* `tapping' action */
192345789Speter	if (sc->config & PSM_CONFIG_FORCETAP)
192445789Speter	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
192541016Sdfr
192641016Sdfr	switch (sc->hw.model) {
192741016Sdfr
192858230Syokota	case MOUSE_MODEL_EXPLORER:
192958230Syokota	    /*
193058230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
193158230Syokota	     * byte 1:  oy ox sy sx 1  M  R  L
193258230Syokota	     * byte 2:  x  x  x  x  x  x  x  x
193358230Syokota	     * byte 3:  y  y  y  y  y  y  y  y
193458230Syokota	     * byte 4:  *  *  S2 S1 s  d2 d1 d0
193558230Syokota	     *
193658230Syokota	     * L, M, R, S1, S2: left, middle, right and side buttons
193758230Syokota	     * s: wheel data sign bit
193858230Syokota	     * d2-d0: wheel data
193958230Syokota	     */
194058230Syokota	    z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
194158230Syokota		? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
194258230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
194358230Syokota		? MOUSE_BUTTON4DOWN : 0;
194458230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
194558230Syokota		? MOUSE_BUTTON5DOWN : 0;
194658230Syokota	    break;
194758230Syokota
194841016Sdfr	case MOUSE_MODEL_INTELLI:
194941016Sdfr	case MOUSE_MODEL_NET:
195041016Sdfr	    /* wheel data is in the fourth byte */
195141016Sdfr	    z = (char)sc->ipacket[3];
195258230Syokota	    /* some mice may send 7 when there is no Z movement?! XXX */
195358230Syokota	    if ((z >= 7) || (z <= -7))
195458230Syokota		z = 0;
195558230Syokota	    /* some compatible mice have additional buttons */
195658230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
195758230Syokota		? MOUSE_BUTTON4DOWN : 0;
195858230Syokota	    ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
195958230Syokota		? MOUSE_BUTTON5DOWN : 0;
196041016Sdfr	    break;
196141016Sdfr
196241016Sdfr	case MOUSE_MODEL_MOUSEMANPLUS:
196348778Syokota	    /*
196448778Syokota	     * PS2++ protocl packet
196548778Syokota	     *
196648778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
196748778Syokota	     * byte 1:  *  1  p3 p2 1  *  *  *
196848778Syokota	     * byte 2:  c1 c2 p1 p0 d1 d0 1  0
196948778Syokota	     *
197048778Syokota	     * p3-p0: packet type
197148778Syokota	     * c1, c2: c1 & c2 == 1, if p2 == 0
197248778Syokota	     *         c1 & c2 == 0, if p2 == 1
197348778Syokota	     *
197448778Syokota	     * packet type: 0 (device type)
197548778Syokota	     * See comments in enable_mmanplus() below.
197648778Syokota	     *
197748778Syokota	     * packet type: 1 (wheel data)
197848778Syokota	     *
197948778Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
198048778Syokota	     * byte 3:  h  *  B5 B4 s  d2 d1 d0
198148778Syokota	     *
198248778Syokota	     * h: 1, if horizontal roller data
198348778Syokota	     *    0, if vertical roller data
198448778Syokota	     * B4, B5: button 4 and 5
198548778Syokota	     * s: sign bit
198648778Syokota	     * d2-d0: roller data
198748778Syokota	     *
198848778Syokota	     * packet type: 2 (reserved)
198948778Syokota	     */
199048778Syokota	    if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
199148778Syokota		    && (abs(x) > 191)
199248778Syokota		    && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
199341016Sdfr		/* the extended data packet encodes button and wheel events */
199448778Syokota		switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
199548778Syokota		case 1:
199648778Syokota		    /* wheel data packet */
199748778Syokota		    x = y = 0;
199848778Syokota		    if (sc->ipacket[2] & 0x80) {
199948778Syokota			/* horizontal roller count - ignore it XXX*/
200048778Syokota		    } else {
200148778Syokota			/* vertical roller count */
200248778Syokota			z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
200348778Syokota			    ? (sc->ipacket[2] & 0x0f) - 16
200448778Syokota			    : (sc->ipacket[2] & 0x0f);
200548778Syokota		    }
200648778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
200748778Syokota			? MOUSE_BUTTON4DOWN : 0;
200848778Syokota		    ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
200948778Syokota			? MOUSE_BUTTON5DOWN : 0;
201048778Syokota		    break;
201148778Syokota		case 2:
201258230Syokota		    /* this packet type is reserved by Logitech... */
201358230Syokota		    /*
201458230Syokota		     * IBM ScrollPoint Mouse uses this packet type to
201558230Syokota		     * encode both vertical and horizontal scroll movement.
201658230Syokota		     */
201758230Syokota		    x = y = 0;
201858230Syokota		    /* horizontal count */
201958230Syokota		    if (sc->ipacket[2] & 0x0f)
202058230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
202158230Syokota		    /* vertical count */
202258230Syokota		    if (sc->ipacket[2] & 0xf0)
202358230Syokota			z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
202458230Syokota#if 0
202558230Syokota		    /* vertical count */
202658230Syokota		    z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
202758230Syokota			? ((sc->ipacket[2] >> 4) & 0x0f) - 16
202858230Syokota			: ((sc->ipacket[2] >> 4) & 0x0f);
202958230Syokota		    /* horizontal count */
203058230Syokota		    w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
203158230Syokota			? (sc->ipacket[2] & 0x0f) - 16
203258230Syokota			: (sc->ipacket[2] & 0x0f);
203358230Syokota#endif
203458230Syokota		    break;
203548778Syokota		case 0:
203648778Syokota		    /* device type packet - shouldn't happen */
203748778Syokota		    /* FALL THROUGH */
203848778Syokota		default:
203948778Syokota		    x = y = 0;
204048778Syokota		    ms.button = ms.obutton;
204158230Syokota		    if (bootverbose)
204258230Syokota			log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
204358230Syokota				       "0x%02x 0x%02x 0x%02x\n",
204458230Syokota			    MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
204558230Syokota			    sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
204648778Syokota		    break;
204748778Syokota		}
204841016Sdfr	    } else {
204941016Sdfr		/* preserve button states */
205041016Sdfr		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
205141016Sdfr	    }
205241016Sdfr	    break;
205341016Sdfr
205441016Sdfr	case MOUSE_MODEL_GLIDEPOINT:
205541016Sdfr	    /* `tapping' action */
205641016Sdfr	    ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
205741016Sdfr	    break;
205841016Sdfr
205941016Sdfr	case MOUSE_MODEL_NETSCROLL:
206058230Syokota	    /* three addtional bytes encode buttons and wheel events */
206158230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
206241016Sdfr		? MOUSE_BUTTON4DOWN : 0;
206358230Syokota	    ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
206458230Syokota		? MOUSE_BUTTON5DOWN : 0;
206541016Sdfr	    z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
206641016Sdfr		? sc->ipacket[4] - 256 : sc->ipacket[4];
206741016Sdfr	    break;
206841016Sdfr
206941016Sdfr	case MOUSE_MODEL_THINK:
207041016Sdfr	    /* the fourth button state in the first byte */
207141016Sdfr	    ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
207241016Sdfr	    break;
207341016Sdfr
207449965Syokota	case MOUSE_MODEL_VERSAPAD:
207549965Syokota	    /* VersaPad PS/2 absolute mode message format
207649965Syokota	     *
207749965Syokota	     * [packet1]     7   6   5   4   3   2   1   0(LSB)
207849965Syokota	     *  ipacket[0]:  1   1   0   A   1   L   T   R
207949965Syokota	     *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
208049965Syokota	     *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
208149965Syokota	     *  ipacket[3]:  1   1   1   A   1   L   T   R
208249965Syokota	     *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
208349965Syokota	     *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
208449965Syokota	     *
208549965Syokota	     * [note]
208649965Syokota	     *  R: right physical mouse button (1=on)
208749965Syokota	     *  T: touch pad virtual button (1=tapping)
208849965Syokota	     *  L: left physical mouse button (1=on)
208949965Syokota	     *  A: position data is valid (1=valid)
209049965Syokota	     *  H: horizontal data (12bit signed integer. H11 is sign bit.)
209149965Syokota	     *  V: vertical data (12bit signed integer. V11 is sign bit.)
209249965Syokota	     *  P: pressure data
209349965Syokota	     *
209449965Syokota	     * Tapping is mapped to MOUSE_BUTTON4.
209549965Syokota	     */
209649965Syokota	    ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
209749965Syokota	    ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
209849965Syokota	    x = y = 0;
209949965Syokota	    if (c & MOUSE_PS2VERSA_IN_USE) {
210049965Syokota		x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
210149965Syokota		y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
210249965Syokota		if (x0 & 0x800)
210349965Syokota		    x0 -= 0x1000;
210449965Syokota		if (y0 & 0x800)
210549965Syokota		    y0 -= 0x1000;
210649965Syokota		if (sc->flags & PSM_FLAGS_FINGERDOWN) {
210749965Syokota		    x = sc->xold - x0;
210849965Syokota		    y = y0 - sc->yold;
210949965Syokota		    if (x < 0)	/* XXX */
211049965Syokota			x++;
211149965Syokota		    else if (x)
211249965Syokota			x--;
211349965Syokota		    if (y < 0)
211449965Syokota			y++;
211549965Syokota		    else if (y)
211649965Syokota			y--;
211749965Syokota		} else {
211849965Syokota		    sc->flags |= PSM_FLAGS_FINGERDOWN;
211949965Syokota		}
212049965Syokota		sc->xold = x0;
212149965Syokota		sc->yold = y0;
212249965Syokota	    } else {
212349965Syokota		sc->flags &= ~PSM_FLAGS_FINGERDOWN;
212449965Syokota	    }
212549965Syokota	    c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
212649965Syokota		| ((y < 0) ? MOUSE_PS2_YNEG : 0);
212749965Syokota	    break;
212849965Syokota
212958230Syokota	case MOUSE_MODEL_4D:
213058230Syokota	    /*
213158230Syokota	     *          b7 b6 b5 b4 b3 b2 b1 b0
213258230Syokota	     * byte 1:  s2 d2 s1 d1 1  M  R  L
213358230Syokota	     * byte 2:  sx x  x  x  x  x  x  x
213458230Syokota	     * byte 3:  sy y  y  y  y  y  y  y
213558230Syokota	     *
213658230Syokota	     * s1: wheel 1 direction
213758230Syokota	     * d1: wheel 1 data
213858230Syokota	     * s2: wheel 2 direction
213958230Syokota	     * d2: wheel 2 data
214058230Syokota	     */
214158230Syokota	    x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
214258230Syokota	    y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
214358230Syokota	    switch (c & MOUSE_4D_WHEELBITS) {
214458230Syokota	    case 0x10:
214558230Syokota		z = 1;
214658230Syokota		break;
214758230Syokota	    case 0x30:
214858230Syokota		z = -1;
214958230Syokota		break;
215058230Syokota	    case 0x40:	/* 2nd wheel turning right XXX */
215158230Syokota		z = 2;
215258230Syokota		break;
215358230Syokota	    case 0xc0:	/* 2nd wheel turning left XXX */
215458230Syokota		z = -2;
215558230Syokota		break;
215658230Syokota	    }
215758230Syokota	    break;
215858230Syokota
215958230Syokota	case MOUSE_MODEL_4DPLUS:
216058230Syokota	    if ((x < 16 - 256) && (y < 16 - 256)) {
216158230Syokota		/*
216258230Syokota		 *          b7 b6 b5 b4 b3 b2 b1 b0
216358230Syokota		 * byte 1:  0  0  1  1  1  M  R  L
216458230Syokota		 * byte 2:  0  0  0  0  1  0  0  0
216558230Syokota		 * byte 3:  0  0  0  0  S  s  d1 d0
216658230Syokota		 *
216758230Syokota		 * L, M, R, S: left, middle, right and side buttons
216858230Syokota		 * s: wheel data sign bit
216958230Syokota		 * d1-d0: wheel data
217058230Syokota		 */
217158230Syokota		x = y = 0;
217258230Syokota		if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
217358230Syokota		    ms.button |= MOUSE_BUTTON4DOWN;
217458230Syokota		z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
217558230Syokota			? ((sc->ipacket[2] & 0x07) - 8)
217658230Syokota			: (sc->ipacket[2] & 0x07) ;
217758230Syokota	    } else {
217858230Syokota		/* preserve previous button states */
217958230Syokota		ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
218058230Syokota	    }
218158230Syokota	    break;
218258230Syokota
218341016Sdfr	case MOUSE_MODEL_GENERIC:
218441016Sdfr	default:
218541016Sdfr	    break;
218641016Sdfr	}
218741016Sdfr
218841016Sdfr        /* scale values */
218941016Sdfr        if (sc->mode.accelfactor >= 1) {
219041016Sdfr            if (x != 0) {
219141016Sdfr                x = x * x / sc->mode.accelfactor;
219241016Sdfr                if (x == 0)
219341016Sdfr                    x = 1;
219441016Sdfr                if (c & MOUSE_PS2_XNEG)
219541016Sdfr                    x = -x;
219641016Sdfr            }
219741016Sdfr            if (y != 0) {
219841016Sdfr                y = y * y / sc->mode.accelfactor;
219941016Sdfr                if (y == 0)
220041016Sdfr                    y = 1;
220141016Sdfr                if (c & MOUSE_PS2_YNEG)
220241016Sdfr                    y = -y;
220341016Sdfr            }
220441016Sdfr        }
220541016Sdfr
220641016Sdfr        ms.dx = x;
220741016Sdfr        ms.dy = y;
220841016Sdfr        ms.dz = z;
220941016Sdfr        ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
221041016Sdfr	    | (ms.obutton ^ ms.button);
221141016Sdfr
221241016Sdfr	if (sc->mode.level < PSM_LEVEL_NATIVE)
221341016Sdfr	    sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
221441016Sdfr
221541016Sdfr        sc->status.flags |= ms.flags;
221641016Sdfr        sc->status.dx += ms.dx;
221741016Sdfr        sc->status.dy += ms.dy;
221841016Sdfr        sc->status.dz += ms.dz;
221941016Sdfr        sc->status.button = ms.button;
222041016Sdfr        sc->button = ms.button;
222141016Sdfr
222258230Syokota	sc->watchdog = FALSE;
222358230Syokota
222441016Sdfr        /* queue data */
222541016Sdfr        if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
222641016Sdfr	    l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
222741016Sdfr	    bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
222841016Sdfr	    if (sc->inputbytes > l)
222941016Sdfr	        bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
223041016Sdfr            sc->queue.tail =
223141016Sdfr		(sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
223241016Sdfr            sc->queue.count += sc->inputbytes;
223341016Sdfr	}
223441016Sdfr        sc->inputbytes = 0;
223541016Sdfr
223641016Sdfr        if (sc->state & PSM_ASLP) {
223741016Sdfr            sc->state &= ~PSM_ASLP;
223841016Sdfr            wakeup((caddr_t) sc);
223941016Sdfr    	}
224041016Sdfr        selwakeup(&sc->rsel);
224141016Sdfr    }
224241016Sdfr}
224341016Sdfr
224441016Sdfrstatic int
224541016Sdfrpsmpoll(dev_t dev, int events, struct proc *p)
224641016Sdfr{
224741016Sdfr    struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
224841016Sdfr    int s;
224941016Sdfr    int revents = 0;
225041016Sdfr
225141016Sdfr    /* Return true if a mouse event available */
225241016Sdfr    s = spltty();
225345789Speter    if (events & (POLLIN | POLLRDNORM)) {
225441016Sdfr	if (sc->queue.count > 0)
225541016Sdfr	    revents |= events & (POLLIN | POLLRDNORM);
225641016Sdfr	else
225741016Sdfr	    selrecord(p, &sc->rsel);
225845789Speter    }
225941016Sdfr    splx(s);
226041016Sdfr
226141016Sdfr    return (revents);
226241016Sdfr}
226341016Sdfr
226441016Sdfr/* vendor/model specific routines */
226541016Sdfr
226641016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
226741016Sdfr{
226841016Sdfr    if (set_mouse_resolution(kbdc, res) != res)
226941016Sdfr        return FALSE;
227041016Sdfr    if (set_mouse_scaling(kbdc, scale)
227141016Sdfr	&& set_mouse_scaling(kbdc, scale)
227241016Sdfr	&& set_mouse_scaling(kbdc, scale)
227341016Sdfr	&& (get_mouse_status(kbdc, status, 0, 3) >= 3))
227441016Sdfr	return TRUE;
227541016Sdfr    return FALSE;
227641016Sdfr}
227741016Sdfr
227841016Sdfr#if notyet
227941016Sdfr/* Logitech MouseMan Cordless II */
228041016Sdfrstatic int
228141016Sdfrenable_lcordless(struct psm_softc *sc)
228241016Sdfr{
228341016Sdfr    int status[3];
228441016Sdfr    int ch;
228541016Sdfr
228641016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
228741016Sdfr        return FALSE;
228841016Sdfr    if (status[1] == PSMD_RES_HIGH)
228941016Sdfr	return FALSE;
229041016Sdfr    ch = (status[0] & 0x07) - 1;	/* channel # */
229141016Sdfr    if ((ch <= 0) || (ch > 4))
229241016Sdfr	return FALSE;
229341016Sdfr    /*
229441016Sdfr     * status[1]: always one?
229541016Sdfr     * status[2]: battery status? (0-100)
229641016Sdfr     */
229741016Sdfr    return TRUE;
229841016Sdfr}
229941016Sdfr#endif /* notyet */
230041016Sdfr
230158230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
230241016Sdfrstatic int
230341016Sdfrenable_groller(struct psm_softc *sc)
230441016Sdfr{
230541016Sdfr    int status[3];
230641016Sdfr
230741016Sdfr    /*
230841016Sdfr     * The special sequence to enable the fourth button and the
230941016Sdfr     * roller. Immediately after this sequence check status bytes.
231041016Sdfr     * if the mouse is NetScroll, the second and the third bytes are
231141016Sdfr     * '3' and 'D'.
231241016Sdfr     */
231341016Sdfr
231441016Sdfr    /*
231541016Sdfr     * If the mouse is an ordinary PS/2 mouse, the status bytes should
231641016Sdfr     * look like the following.
231741016Sdfr     *
231841016Sdfr     * byte 1 bit 7 always 0
231941016Sdfr     *        bit 6 stream mode (0)
232041016Sdfr     *        bit 5 disabled (0)
232141016Sdfr     *        bit 4 1:1 scaling (0)
232241016Sdfr     *        bit 3 always 0
232341016Sdfr     *        bit 0-2 button status
232441016Sdfr     * byte 2 resolution (PSMD_RES_HIGH)
232541016Sdfr     * byte 3 report rate (?)
232641016Sdfr     */
232741016Sdfr
232841016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
232941016Sdfr        return FALSE;
233041016Sdfr    if ((status[1] != '3') || (status[2] != 'D'))
233141016Sdfr        return FALSE;
233258230Syokota    /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
233341016Sdfr    sc->hw.buttons = 4;
233441016Sdfr    return TRUE;
233541016Sdfr}
233641016Sdfr
233758230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
233841016Sdfrstatic int
233941016Sdfrenable_gmouse(struct psm_softc *sc)
234041016Sdfr{
234141016Sdfr    int status[3];
234241016Sdfr
234341016Sdfr    /*
234441016Sdfr     * The special sequence to enable the middle, "rubber" button.
234541016Sdfr     * Immediately after this sequence check status bytes.
234641016Sdfr     * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
234741016Sdfr     * the second and the third bytes are '3' and 'U'.
234841016Sdfr     * NOTE: NetMouse reports that it has three buttons although it has
234941016Sdfr     * two buttons and a rubber button. NetMouse Pro and MIE Mouse
235041016Sdfr     * say they have three buttons too and they do have a button on the
235141016Sdfr     * side...
235241016Sdfr     */
235341016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
235441016Sdfr        return FALSE;
235541016Sdfr    if ((status[1] != '3') || (status[2] != 'U'))
235641016Sdfr        return FALSE;
235741016Sdfr    return TRUE;
235841016Sdfr}
235941016Sdfr
236041016Sdfr/* ALPS GlidePoint */
236141016Sdfrstatic int
236241016Sdfrenable_aglide(struct psm_softc *sc)
236341016Sdfr{
236441016Sdfr    int status[3];
236541016Sdfr
236641016Sdfr    /*
236741016Sdfr     * The special sequence to obtain ALPS GlidePoint specific
236841016Sdfr     * information. Immediately after this sequence, status bytes will
236941016Sdfr     * contain something interesting.
237041016Sdfr     * NOTE: ALPS produces several models of GlidePoint. Some of those
237141016Sdfr     * do not respond to this sequence, thus, cannot be detected this way.
237241016Sdfr     */
237350149Syokota    if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
237450149Syokota	return FALSE;
237541016Sdfr    if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
237641016Sdfr        return FALSE;
237750149Syokota    if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
237841016Sdfr        return FALSE;
237941016Sdfr    return TRUE;
238041016Sdfr}
238141016Sdfr
238241016Sdfr/* Kensington ThinkingMouse/Trackball */
238341016Sdfrstatic int
238441016Sdfrenable_kmouse(struct psm_softc *sc)
238541016Sdfr{
238641016Sdfr    static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
238741016Sdfr    KBDC kbdc = sc->kbdc;
238841016Sdfr    int status[3];
238941016Sdfr    int id1;
239041016Sdfr    int id2;
239141016Sdfr    int i;
239241016Sdfr
239341016Sdfr    id1 = get_aux_id(kbdc);
239441016Sdfr    if (set_mouse_sampling_rate(kbdc, 10) != 10)
239541016Sdfr	return FALSE;
239641016Sdfr    /*
239741016Sdfr     * The device is now in the native mode? It returns a different
239841016Sdfr     * ID value...
239941016Sdfr     */
240041016Sdfr    id2 = get_aux_id(kbdc);
240141016Sdfr    if ((id1 == id2) || (id2 != 2))
240241016Sdfr	return FALSE;
240341016Sdfr
240441016Sdfr    if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
240541016Sdfr        return FALSE;
240641016Sdfr#if PSM_DEBUG >= 2
240741016Sdfr    /* at this point, resolution is LOW, sampling rate is 10/sec */
240841016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
240941016Sdfr        return FALSE;
241041016Sdfr#endif
241141016Sdfr
241241016Sdfr    /*
241341016Sdfr     * The special sequence to enable the third and fourth buttons.
241441016Sdfr     * Otherwise they behave like the first and second buttons.
241541016Sdfr     */
241641016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
241741016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
241841016Sdfr	    return FALSE;
241941016Sdfr    }
242041016Sdfr
242141016Sdfr    /*
242241016Sdfr     * At this point, the device is using default resolution and
242341016Sdfr     * sampling rate for the native mode.
242441016Sdfr     */
242541016Sdfr    if (get_mouse_status(kbdc, status, 0, 3) < 3)
242641016Sdfr        return FALSE;
242741016Sdfr    if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
242841016Sdfr        return FALSE;
242941016Sdfr
243041016Sdfr    /* the device appears be enabled by this sequence, diable it for now */
243141016Sdfr    disable_aux_dev(kbdc);
243241016Sdfr    empty_aux_buffer(kbdc, 5);
243341016Sdfr
243441016Sdfr    return TRUE;
243541016Sdfr}
243641016Sdfr
243758230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
243841016Sdfrstatic int
243941016Sdfrenable_mmanplus(struct psm_softc *sc)
244041016Sdfr{
244141016Sdfr    static char res[] = {
244241016Sdfr	-1, PSMD_RES_LOW, PSMD_RES_HIGH, PSMD_RES_MEDIUM_HIGH,
244341016Sdfr	PSMD_RES_MEDIUM_LOW, -1, PSMD_RES_HIGH, PSMD_RES_MEDIUM_LOW,
244441016Sdfr	PSMD_RES_MEDIUM_HIGH, PSMD_RES_HIGH,
244541016Sdfr    };
244641016Sdfr    KBDC kbdc = sc->kbdc;
244741016Sdfr    int data[3];
244841016Sdfr    int i;
244941016Sdfr
245041016Sdfr    /* the special sequence to enable the fourth button and the roller. */
245158230Syokota    /*
245258230Syokota     * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
245358230Syokota     * must be called exactly three times since the last RESET command
245458230Syokota     * before this sequence. XXX
245558230Syokota     */
245641016Sdfr    for (i = 0; i < sizeof(res)/sizeof(res[0]); ++i) {
245741016Sdfr	if (res[i] < 0) {
245841016Sdfr	    if (!set_mouse_scaling(kbdc, 1))
245941016Sdfr		return FALSE;
246041016Sdfr	} else {
246141016Sdfr	    if (set_mouse_resolution(kbdc, res[i]) != res[i])
246241016Sdfr		return FALSE;
246341016Sdfr	}
246441016Sdfr    }
246541016Sdfr
246641016Sdfr    if (get_mouse_status(kbdc, data, 1, 3) < 3)
246741016Sdfr        return FALSE;
246841016Sdfr
246948778Syokota    /*
247048778Syokota     * PS2++ protocl, packet type 0
247141016Sdfr     *
247248778Syokota     *          b7 b6 b5 b4 b3 b2 b1 b0
247348778Syokota     * byte 1:  *  1  p3 p2 1  *  *  *
247448778Syokota     * byte 2:  1  1  p1 p0 m1 m0 1  0
247548778Syokota     * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
247648778Syokota     *
247748778Syokota     * p3-p0: packet type: 0
247858230Syokota     * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
247941016Sdfr     */
248048778Syokota    /* check constant bits */
248148778Syokota    if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
248241016Sdfr        return FALSE;
248348778Syokota    if ((data[1] & 0xc3) != 0xc2)
248448778Syokota        return FALSE;
248548778Syokota    /* check d3-d0 in byte 2 */
248648778Syokota    if (!MOUSE_PS2PLUS_CHECKBITS(data))
248748778Syokota        return FALSE;
248848778Syokota    /* check p3-p0 */
248948778Syokota    if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
249048778Syokota        return FALSE;
249141016Sdfr
249248778Syokota    sc->hw.hwid &= 0x00ff;
249348778Syokota    sc->hw.hwid |= data[2] << 8;	/* save model ID */
249448778Syokota
249541016Sdfr    /*
249641016Sdfr     * MouseMan+ (or FirstMouse+) is now in its native mode, in which
249741016Sdfr     * the wheel and the fourth button events are encoded in the
249841016Sdfr     * special data packet. The mouse may be put in the IntelliMouse mode
249941016Sdfr     * if it is initialized by the IntelliMouse's method.
250041016Sdfr     */
250141016Sdfr    return TRUE;
250241016Sdfr}
250341016Sdfr
250458230Syokota/* MS IntelliMouse Explorer */
250558230Syokotastatic int
250658230Syokotaenable_msexplorer(struct psm_softc *sc)
250758230Syokota{
250858230Syokota    static unsigned char rate[] = { 200, 200, 80, };
250958230Syokota    KBDC kbdc = sc->kbdc;
251058230Syokota    int id;
251158230Syokota    int i;
251258230Syokota
251358230Syokota    /* the special sequence to enable the extra buttons and the roller. */
251458230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
251558230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
251658230Syokota	    return FALSE;
251758230Syokota    }
251858230Syokota    /* the device will give the genuine ID only after the above sequence */
251958230Syokota    id = get_aux_id(kbdc);
252058230Syokota    if (id != PSM_EXPLORER_ID)
252158230Syokota	return FALSE;
252258230Syokota
252358230Syokota    sc->hw.hwid = id;
252458230Syokota    sc->hw.buttons = 5;		/* IntelliMouse Explorer XXX */
252558230Syokota
252658230Syokota    return TRUE;
252758230Syokota}
252858230Syokota
252941016Sdfr/* MS IntelliMouse */
253041016Sdfrstatic int
253141016Sdfrenable_msintelli(struct psm_softc *sc)
253241016Sdfr{
253341016Sdfr    /*
253441016Sdfr     * Logitech MouseMan+ and FirstMouse+ will also respond to this
253541016Sdfr     * probe routine and act like IntelliMouse.
253641016Sdfr     */
253741016Sdfr
253841016Sdfr    static unsigned char rate[] = { 200, 100, 80, };
253941016Sdfr    KBDC kbdc = sc->kbdc;
254041016Sdfr    int id;
254141016Sdfr    int i;
254241016Sdfr
254341016Sdfr    /* the special sequence to enable the third button and the roller. */
254441016Sdfr    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
254541016Sdfr        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
254641016Sdfr	    return FALSE;
254741016Sdfr    }
254841016Sdfr    /* the device will give the genuine ID only after the above sequence */
254941016Sdfr    id = get_aux_id(kbdc);
255041016Sdfr    if (id != PSM_INTELLI_ID)
255141016Sdfr	return FALSE;
255241016Sdfr
255341016Sdfr    sc->hw.hwid = id;
255441016Sdfr    sc->hw.buttons = 3;
255541016Sdfr
255641016Sdfr    return TRUE;
255741016Sdfr}
255841016Sdfr
255958230Syokota/* A4 Tech 4D Mouse */
256058230Syokotastatic int
256158230Syokotaenable_4dmouse(struct psm_softc *sc)
256258230Syokota{
256358230Syokota    /*
256458230Syokota     * Newer wheel mice from A4 Tech may use the 4D+ protocol.
256558230Syokota     */
256658230Syokota
256758230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
256858230Syokota    KBDC kbdc = sc->kbdc;
256958230Syokota    int id;
257058230Syokota    int i;
257158230Syokota
257258230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
257358230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
257458230Syokota	    return FALSE;
257558230Syokota    }
257658230Syokota    id = get_aux_id(kbdc);
257758230Syokota    /*
257858230Syokota     * WinEasy 4D: 6
257958230Syokota     * Cable-Free 4D: 8 (4DPLUS)
258058230Syokota     * 4 Way ScrollMouse 4D+: 8 (4DPLUS)
258158230Syokota     */
258258230Syokota    if (id != PSM_4DMOUSE_ID)
258358230Syokota	return FALSE;
258458230Syokota
258558230Syokota    sc->hw.hwid = id;
258658230Syokota    sc->hw.buttons = 3;		/* XXX some 4D mice have 4? */
258758230Syokota
258858230Syokota    return TRUE;
258958230Syokota}
259058230Syokota
259158230Syokota/* A4 Tech 4D+ Mouse */
259258230Syokotastatic int
259358230Syokotaenable_4dplus(struct psm_softc *sc)
259458230Syokota{
259558230Syokota    /*
259658230Syokota     * Newer wheel mice from A4 Tech seem to use this protocol.
259758230Syokota     * Older models are recognized as either 4D Mouse or IntelliMouse.
259858230Syokota     */
259958230Syokota    KBDC kbdc = sc->kbdc;
260058230Syokota    int id;
260158230Syokota
260258230Syokota    /*
260358230Syokota     * enable_4dmouse() already issued the following ID sequence...
260458230Syokota    static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
260558230Syokota    int i;
260658230Syokota
260758230Syokota    for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
260858230Syokota        if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
260958230Syokota	    return FALSE;
261058230Syokota    }
261158230Syokota    */
261258230Syokota
261358230Syokota    id = get_aux_id(kbdc);
261458230Syokota    if (id != PSM_4DPLUS_ID)
261558230Syokota	return FALSE;
261658230Syokota
261758230Syokota    sc->hw.hwid = id;
261858230Syokota    sc->hw.buttons = 4;		/* XXX */
261958230Syokota
262058230Syokota    return TRUE;
262158230Syokota}
262258230Syokota
262349965Syokota/* Interlink electronics VersaPad */
262449965Syokotastatic int
262549965Syokotaenable_versapad(struct psm_softc *sc)
262649965Syokota{
262749965Syokota    KBDC kbdc = sc->kbdc;
262849965Syokota    int data[3];
262949965Syokota
263049965Syokota    set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
263149965Syokota    set_mouse_sampling_rate(kbdc, 100);		/* set rate 100 */
263249965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
263349965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
263449965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
263549965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
263649965Syokota    if (get_mouse_status(kbdc, data, 0, 3) < 3)	/* get status */
263749965Syokota	return FALSE;
263849965Syokota    if (data[2] != 0xa || data[1] != 0 )	/* rate == 0xa && res. == 0 */
263949965Syokota	return FALSE;
264049965Syokota    set_mouse_scaling(kbdc, 1);			/* set scale 1:1 */
264149965Syokota
264258230Syokota    sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
264358230Syokota
264449965Syokota    return TRUE;				/* PS/2 absolute mode */
264549965Syokota}
264649965Syokota
264741016Sdfrstatic int
264854629Syokotapsmresume(device_t dev)
264941016Sdfr{
265054629Syokota    struct psm_softc *sc = device_get_softc(dev);
265154629Syokota    int unit = device_get_unit(dev);
265241016Sdfr    int err = 0;
265341016Sdfr    int s;
265441016Sdfr    int c;
265541016Sdfr
265641016Sdfr    if (verbose >= 2)
265754629Syokota        log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
265841016Sdfr
265958230Syokota    if (!(sc->config & PSM_CONFIG_HOOKRESUME))
266058230Syokota	return (0);
266158230Syokota
266241016Sdfr    /* don't let anybody mess with the aux device */
266341016Sdfr    if (!kbdc_lock(sc->kbdc, TRUE))
266441016Sdfr	return (EIO);
266541016Sdfr    s = spltty();
266641016Sdfr
266758230Syokota    /* block our watchdog timer */
266858230Syokota    sc->watchdog = FALSE;
266958230Syokota    untimeout(psmtimeout, (void *)unit, sc->callout);
267058230Syokota    callout_handle_init(&sc->callout);
267158230Syokota
267241016Sdfr    /* save the current controller command byte */
267341016Sdfr    empty_both_buffers(sc->kbdc, 10);
267441016Sdfr    c = get_controller_command_byte(sc->kbdc);
267541016Sdfr    if (verbose >= 2)
267641016Sdfr        log(LOG_DEBUG, "psm%d: current command byte: %04x (psmresume).\n",
267741016Sdfr	    unit, c);
267841016Sdfr
267941016Sdfr    /* enable the aux port but disable the aux interrupt and the keyboard */
268041016Sdfr    if ((c == -1) || !set_controller_command_byte(sc->kbdc,
268141016Sdfr	    kbdc_get_device_mask(sc->kbdc),
268241016Sdfr  	    KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
268341016Sdfr	        | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
268441016Sdfr        /* CONTROLLER ERROR */
268541016Sdfr	splx(s);
268641016Sdfr        kbdc_lock(sc->kbdc, FALSE);
268741016Sdfr	log(LOG_ERR, "psm%d: unable to set the command byte (psmresume).\n",
268841016Sdfr	    unit);
268941016Sdfr	return (EIO);
269041016Sdfr    }
269141016Sdfr
269241016Sdfr    /* flush any data */
269341016Sdfr    if (sc->state & PSM_VALID) {
269441016Sdfr	disable_aux_dev(sc->kbdc);	/* this may fail; but never mind... */
269541016Sdfr	empty_aux_buffer(sc->kbdc, 10);
269641016Sdfr    }
269741016Sdfr    sc->inputbytes = 0;
269841016Sdfr
269941016Sdfr    /* try to detect the aux device; are you still there? */
270058230Syokota    if (sc->config & PSM_CONFIG_INITAFTERSUSPEND) {
270158230Syokota	if (reinitialize(unit, &sc->mode)) {
270258230Syokota	    /* yes */
270358230Syokota	    sc->state |= PSM_VALID;
270458230Syokota	} else {
270558230Syokota	    /* the device has gone! */
270658230Syokota	    restore_controller(sc->kbdc, c);
270758230Syokota	    sc->state &= ~PSM_VALID;
270858230Syokota	    log(LOG_ERR, "psm%d: the aux device has gone! (psmresume).\n",
270958230Syokota		unit);
271058230Syokota	    err = ENXIO;
271158230Syokota	}
271241016Sdfr    }
271341016Sdfr    splx(s);
271441016Sdfr
271541016Sdfr    /* restore the driver state */
271641016Sdfr    if ((sc->state & PSM_OPEN) && (err == 0)) {
271741016Sdfr        /* enable the aux device and the port again */
271841016Sdfr	err = doopen(unit, c);
271941016Sdfr	if (err != 0)
272041016Sdfr	    log(LOG_ERR, "psm%d: failed to enable the device (psmresume).\n",
272141016Sdfr		unit);
272241016Sdfr    } else {
272341016Sdfr        /* restore the keyboard port and disable the aux port */
272441016Sdfr        if (!set_controller_command_byte(sc->kbdc,
272541016Sdfr                kbdc_get_device_mask(sc->kbdc),
272641016Sdfr                (c & KBD_KBD_CONTROL_BITS)
272741016Sdfr                    | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
272841016Sdfr            /* CONTROLLER ERROR */
272941016Sdfr            log(LOG_ERR, "psm%d: failed to disable the aux port (psmresume).\n",
273041016Sdfr                unit);
273141016Sdfr            err = EIO;
273241016Sdfr	}
273341016Sdfr    }
273441016Sdfr
273541016Sdfr    /* done */
273641016Sdfr    kbdc_lock(sc->kbdc, FALSE);
273741016Sdfr    if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
273841016Sdfr	/*
273941016Sdfr	 * Release the blocked process; it must be notified that the device
274041016Sdfr	 * cannot be accessed anymore.
274141016Sdfr	 */
274241016Sdfr        sc->state &= ~PSM_ASLP;
274341016Sdfr        wakeup((caddr_t)sc);
274441016Sdfr    }
274541016Sdfr
274641016Sdfr    if (verbose >= 2)
274754629Syokota        log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
274841016Sdfr
274941016Sdfr    return (err);
275041016Sdfr}
275141016Sdfr
275252997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
2753