The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/isa/psm.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1992, 1993 Erik Forsberg.
    3  * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  *
   12  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
   13  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   14  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
   15  * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   16  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   17  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   18  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   19  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   20  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   21  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   22  */
   23 /*
   24  *  Ported to 386bsd Oct 17, 1992
   25  *  Sandi Donno, Computer Science, University of Cape Town, South Africa
   26  *  Please send bug reports to sandi@cs.uct.ac.za
   27  *
   28  *  Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
   29  *  although I was only partially successful in getting the alpha release
   30  *  of his "driver for the Logitech and ATI Inport Bus mice for use with
   31  *  386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
   32  *  found his code to be an invaluable reference when porting this driver
   33  *  to 386bsd.
   34  *
   35  *  Further modifications for latest 386BSD+patchkit and port to NetBSD,
   36  *  Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
   37  *
   38  *  Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
   39  *  Andrew Herbert - 12 June 1993
   40  *
   41  *  Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
   42  *  - 13 June 1993
   43  *
   44  *  Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
   45  *  - 24 October 1993
   46  *
   47  *  Hardware access routines and probe logic rewritten by
   48  *  Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
   49  *  - 3, 14, 22 October 1996.
   50  *  - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
   51  *  - 14, 30 November 1996. Uses `kbdio.c'.
   52  *  - 13 December 1996. Uses queuing version of `kbdio.c'.
   53  *  - January/February 1997. Tweaked probe logic for 
   54  *    HiNote UltraII/Latitude/Armada laptops.
   55  *  - 30 July 1997. Added APM support.
   56  *  - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX). 
   57  *    Improved sync check logic.
   58  *    Vendor specific support routines.
   59  */
   60 
   61 #include <sys/cdefs.h>
   62 __FBSDID("$FreeBSD$");
   63 
   64 #include "opt_psm.h"
   65 
   66 #include <sys/param.h>
   67 #include <sys/systm.h>
   68 #include <sys/kernel.h>
   69 #include <sys/module.h>
   70 #include <sys/bus.h>
   71 #include <sys/conf.h>
   72 #include <sys/poll.h>
   73 #include <sys/syslog.h>
   74 #include <machine/bus.h>
   75 #include <sys/rman.h>
   76 #include <sys/selinfo.h>
   77 #include <sys/sysctl.h>
   78 #include <sys/time.h>
   79 #include <sys/uio.h>
   80 
   81 #include <sys/limits.h>
   82 #include <sys/mouse.h>
   83 #include <machine/resource.h>
   84 
   85 #include <isa/isavar.h>
   86 #include <dev/kbd/atkbdcreg.h>
   87 
   88 /*
   89  * Driver specific options: the following options may be set by
   90  * `options' statements in the kernel configuration file.
   91  */
   92 
   93 /* debugging */
   94 #ifndef PSM_DEBUG
   95 #define PSM_DEBUG       0       /*
   96                                  * logging: 0: none, 1: brief, 2: verbose
   97                                  *          3: sync errors, 4: all packets
   98                                  */
   99 #endif
  100 #define VLOG(level, args)       \
  101 do {                            \
  102         if (verbose >= level)   \
  103                 log args;       \
  104 } while (0)
  105 
  106 #ifndef PSM_INPUT_TIMEOUT
  107 #define PSM_INPUT_TIMEOUT       2000000 /* 2 sec */
  108 #endif
  109 
  110 #ifndef PSM_TAP_TIMEOUT
  111 #define PSM_TAP_TIMEOUT         125000
  112 #endif
  113 
  114 #ifndef PSM_TAP_THRESHOLD
  115 #define PSM_TAP_THRESHOLD       25
  116 #endif
  117 
  118 /* end of driver specific options */
  119 
  120 #define PSM_DRIVER_NAME         "psm"
  121 #define PSMCPNP_DRIVER_NAME     "psmcpnp"
  122 
  123 /* input queue */
  124 #define PSM_BUFSIZE             960
  125 #define PSM_SMALLBUFSIZE        240
  126 
  127 /* operation levels */
  128 #define PSM_LEVEL_BASE          0
  129 #define PSM_LEVEL_STANDARD      1
  130 #define PSM_LEVEL_NATIVE        2
  131 #define PSM_LEVEL_MIN           PSM_LEVEL_BASE
  132 #define PSM_LEVEL_MAX           PSM_LEVEL_NATIVE
  133 
  134 /* Logitech PS2++ protocol */
  135 #define MOUSE_PS2PLUS_CHECKBITS(b)      \
  136                                 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
  137 #define MOUSE_PS2PLUS_PACKET_TYPE(b)    \
  138                                 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
  139 
  140 /* some macros */
  141 #define PSM_UNIT(dev)           (minor(dev) >> 1)
  142 #define PSM_NBLOCKIO(dev)       (minor(dev) & 1)
  143 #define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1))
  144 
  145 /* ring buffer */
  146 typedef struct ringbuf {
  147     int           count;        /* # of valid elements in the buffer */
  148     int           head;         /* head pointer */
  149     int           tail;         /* tail poiner */
  150     unsigned char buf[PSM_BUFSIZE];
  151 } ringbuf_t;
  152 
  153 /* data buffer */
  154 typedef struct packetbuf {
  155     unsigned char ipacket[16];  /* interim input buffer */
  156     int           inputbytes;   /* # of bytes in the input buffer */
  157 } packetbuf_t;
  158 
  159 #ifndef PSM_PACKETQUEUE
  160 #define PSM_PACKETQUEUE 128
  161 #endif
  162 
  163 /* driver control block */
  164 struct psm_softc {              /* Driver status information */
  165     int           unit;
  166     struct selinfo rsel;        /* Process selecting for Input */
  167     unsigned char state;        /* Mouse driver state */
  168     int           config;       /* driver configuration flags */
  169     int           flags;        /* other flags */
  170     KBDC          kbdc;         /* handle to access the keyboard controller */
  171     struct resource *intr;      /* IRQ resource */
  172     void          *ih;          /* interrupt handle */
  173     mousehw_t     hw;           /* hardware information */
  174     synapticshw_t synhw;        /* Synaptics-specific hardware information */
  175     mousemode_t   mode;         /* operation mode */
  176     mousemode_t   dflt_mode;    /* default operation mode */
  177     mousestatus_t status;       /* accumulated mouse movement */
  178     ringbuf_t     queue;        /* mouse status queue */
  179     packetbuf_t   pqueue[PSM_PACKETQUEUE];      /* mouse data queue */
  180     int           pqueue_start; /* start of data in queue */
  181     int           pqueue_end;   /* end of data in queue */
  182     int           button;       /* the latest button state */
  183     int           xold; /* previous absolute X position */
  184     int           yold; /* previous absolute Y position */
  185     int           zmax; /* maximum pressure value for touchpads */
  186     int           syncerrors;   /* # of bytes discarded searching for sync */
  187     int           pkterrors;    /* # of packets failed during quaranteen. */
  188     struct timeval inputtimeout;
  189     struct timeval lastsoftintr;        /* time of last soft interrupt */
  190     struct timeval lastinputerr;        /* time last sync error happened */
  191     struct timeval taptimeout;          /* tap timeout for touchpads */
  192     int           watchdog;     /* watchdog timer flag */
  193     struct callout_handle callout;      /* watchdog timer call out */
  194     struct callout_handle softcallout;  /* buffer timer call out */
  195     struct cdev *dev;
  196     struct cdev *bdev;
  197     int           lasterr;
  198     int           cmdcount;
  199 };
  200 static devclass_t psm_devclass;
  201 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
  202 
  203 /* driver state flags (state) */
  204 #define PSM_VALID               0x80
  205 #define PSM_OPEN                1       /* Device is open */
  206 #define PSM_ASLP                2       /* Waiting for mouse data */
  207 #define PSM_SOFTARMED           4       /* Software interrupt armed */
  208 #define PSM_NEED_SYNCBITS       8       /* Set syncbits using next data pkt */
  209 
  210 /* driver configuration flags (config) */
  211 #define PSM_CONFIG_RESOLUTION   0x000f  /* resolution */
  212 #define PSM_CONFIG_ACCEL        0x00f0  /* acceleration factor */
  213 #define PSM_CONFIG_NOCHECKSYNC  0x0100  /* disable sync. test */
  214 #define PSM_CONFIG_NOIDPROBE    0x0200  /* disable mouse model probe */
  215 #define PSM_CONFIG_NORESET      0x0400  /* don't reset the mouse */
  216 #define PSM_CONFIG_FORCETAP     0x0800  /* assume `tap' action exists */
  217 #define PSM_CONFIG_IGNPORTERROR 0x1000  /* ignore error in aux port test */
  218 #define PSM_CONFIG_HOOKRESUME   0x2000  /* hook the system resume event */
  219 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
  220 #define PSM_CONFIG_SYNCHACK     0x8000 /* enable `out-of-sync' hack */
  221 
  222 #define PSM_CONFIG_FLAGS        (PSM_CONFIG_RESOLUTION          \
  223                                     | PSM_CONFIG_ACCEL          \
  224                                     | PSM_CONFIG_NOCHECKSYNC    \
  225                                     | PSM_CONFIG_SYNCHACK       \
  226                                     | PSM_CONFIG_NOIDPROBE      \
  227                                     | PSM_CONFIG_NORESET        \
  228                                     | PSM_CONFIG_FORCETAP       \
  229                                     | PSM_CONFIG_IGNPORTERROR   \
  230                                     | PSM_CONFIG_HOOKRESUME     \
  231                                     | PSM_CONFIG_INITAFTERSUSPEND)
  232 
  233 /* other flags (flags) */
  234 #define PSM_FLAGS_FINGERDOWN    0x0001 /* VersaPad finger down */
  235 
  236 /* Tunables */
  237 static int synaptics_support = 0;
  238 TUNABLE_INT("hw.psm.synaptics_support", &synaptics_support);
  239 
  240 /* for backward compatibility */
  241 #define OLD_MOUSE_GETHWINFO     _IOR('M', 1, old_mousehw_t)
  242 #define OLD_MOUSE_GETMODE       _IOR('M', 2, old_mousemode_t)
  243 #define OLD_MOUSE_SETMODE       _IOW('M', 3, old_mousemode_t)
  244 
  245 typedef struct old_mousehw {
  246     int buttons;
  247     int iftype;
  248     int type;
  249     int hwid;
  250 } old_mousehw_t;
  251 
  252 typedef struct old_mousemode {
  253     int protocol;
  254     int rate;
  255     int resolution;
  256     int accelfactor;
  257 } old_mousemode_t;
  258 
  259 /* packet formatting function */
  260 typedef int packetfunc_t(struct psm_softc *, unsigned char *,
  261                               int *, int, mousestatus_t *);
  262 
  263 /* function prototypes */
  264 static void psmidentify(driver_t *, device_t);
  265 static int psmprobe(device_t);
  266 static int psmattach(device_t);
  267 static int psmdetach(device_t);
  268 static int psmresume(device_t);
  269 
  270 static d_open_t psmopen;
  271 static d_close_t psmclose;
  272 static d_read_t psmread;
  273 static d_ioctl_t psmioctl;
  274 static d_poll_t psmpoll;
  275 
  276 static int enable_aux_dev(KBDC);
  277 static int disable_aux_dev(KBDC);
  278 static int get_mouse_status(KBDC, int *, int, int);
  279 static int get_aux_id(KBDC);
  280 static int set_mouse_sampling_rate(KBDC, int);
  281 static int set_mouse_scaling(KBDC, int);
  282 static int set_mouse_resolution(KBDC, int);
  283 static int set_mouse_mode(KBDC);
  284 static int get_mouse_buttons(KBDC);
  285 static int is_a_mouse(int);
  286 static void recover_from_error(KBDC);
  287 static int restore_controller(KBDC, int);
  288 static int doinitialize(struct psm_softc *, mousemode_t *);
  289 static int doopen(struct psm_softc *, int);
  290 static int reinitialize(struct psm_softc *, int);
  291 static char *model_name(int);
  292 static void psmsoftintr(void *);
  293 static void psmintr(void *);
  294 static void psmtimeout(void *);
  295 static int timeelapsed(const struct timeval *,
  296     int, int, const struct timeval *);
  297 static void dropqueue(struct psm_softc *);
  298 static void flushpackets(struct psm_softc *);
  299 
  300 /* vendor specific features */
  301 typedef int probefunc_t(struct psm_softc *);
  302 
  303 static int mouse_id_proc1(KBDC, int, int, int *);
  304 static int mouse_ext_command(KBDC, int);
  305 static probefunc_t enable_groller;
  306 static probefunc_t enable_gmouse;
  307 static probefunc_t enable_aglide; 
  308 static probefunc_t enable_kmouse;
  309 static probefunc_t enable_msexplorer;
  310 static probefunc_t enable_msintelli;
  311 static probefunc_t enable_4dmouse;
  312 static probefunc_t enable_4dplus;
  313 static probefunc_t enable_mmanplus;
  314 static probefunc_t enable_synaptics;
  315 static probefunc_t enable_versapad;
  316 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, unsigned char *);
  317 
  318 static struct {
  319     int                 model;
  320     unsigned char       syncmask;
  321     int                 packetsize;
  322     probefunc_t         *probefunc;
  323 } vendortype[] = {
  324     /*
  325      * WARNING: the order of probe is very important.  Don't mess it
  326      * unless you know what you are doing.
  327      */
  328     { MOUSE_MODEL_NET,                  /* Genius NetMouse */
  329       0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
  330     { MOUSE_MODEL_NETSCROLL,            /* Genius NetScroll */
  331       0xc8, 6, enable_groller, },
  332     { MOUSE_MODEL_MOUSEMANPLUS,         /* Logitech MouseMan+ */
  333       0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
  334     { MOUSE_MODEL_EXPLORER,             /* Microsoft IntelliMouse Explorer */
  335       0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
  336     { MOUSE_MODEL_4D,                   /* A4 Tech 4D Mouse */
  337       0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
  338     { MOUSE_MODEL_4DPLUS,               /* A4 Tech 4D+ Mouse */
  339       0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
  340     { MOUSE_MODEL_INTELLI,              /* Microsoft IntelliMouse */
  341       0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
  342     { MOUSE_MODEL_GLIDEPOINT,           /* ALPS GlidePoint */
  343       0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
  344     { MOUSE_MODEL_THINK,                /* Kensignton ThinkingMouse */
  345       0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
  346     { MOUSE_MODEL_VERSAPAD,             /* Interlink electronics VersaPad */
  347       0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
  348     { MOUSE_MODEL_SYNAPTICS,            /* Synaptics Touchpad */
  349       0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics, },
  350     { MOUSE_MODEL_GENERIC,
  351       0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
  352 };
  353 #define GENERIC_MOUSE_ENTRY     ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
  354 
  355 /* device driver declarateion */
  356 static device_method_t psm_methods[] = {
  357         /* Device interface */
  358         DEVMETHOD(device_identify,      psmidentify),
  359         DEVMETHOD(device_probe,         psmprobe),
  360         DEVMETHOD(device_attach,        psmattach),
  361         DEVMETHOD(device_detach,        psmdetach),
  362         DEVMETHOD(device_resume,        psmresume),
  363 
  364         { 0, 0 }
  365 };
  366 
  367 static driver_t psm_driver = {
  368     PSM_DRIVER_NAME,
  369     psm_methods,
  370     sizeof(struct psm_softc),
  371 };
  372 
  373 
  374 static struct cdevsw psm_cdevsw = {
  375         .d_version =    D_VERSION,
  376         .d_flags =      D_NEEDGIANT,
  377         .d_open =       psmopen,
  378         .d_close =      psmclose,
  379         .d_read =       psmread,
  380         .d_ioctl =      psmioctl,
  381         .d_poll =       psmpoll,
  382         .d_name =       PSM_DRIVER_NAME,
  383 };
  384 
  385 /* debug message level */
  386 static int verbose = PSM_DEBUG;
  387 
  388 /* device I/O routines */
  389 static int
  390 enable_aux_dev(KBDC kbdc)
  391 {
  392     int res;
  393 
  394     res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
  395     VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
  396 
  397     return (res == PSM_ACK);
  398 }
  399 
  400 static int
  401 disable_aux_dev(KBDC kbdc)
  402 {
  403     int res;
  404 
  405     res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
  406     VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
  407 
  408     return (res == PSM_ACK);
  409 }
  410 
  411 static int
  412 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
  413 {
  414     int cmd;
  415     int res;
  416     int i;
  417 
  418     switch (flag) {
  419     case 0:
  420     default:
  421         cmd = PSMC_SEND_DEV_STATUS;
  422         break;
  423     case 1:
  424         cmd = PSMC_SEND_DEV_DATA;
  425         break;
  426     }
  427     empty_aux_buffer(kbdc, 5);
  428     res = send_aux_command(kbdc, cmd);
  429     VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n", 
  430          (flag == 1) ? "DATA" : "STATUS", res));
  431     if (res != PSM_ACK)
  432         return 0;
  433 
  434     for (i = 0; i < len; ++i) {
  435         status[i] = read_aux_data(kbdc);
  436         if (status[i] < 0)
  437             break;
  438     }
  439 
  440     VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
  441          (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
  442 
  443     return i;
  444 }
  445 
  446 static int
  447 get_aux_id(KBDC kbdc)
  448 {
  449     int res;
  450     int id;
  451 
  452     empty_aux_buffer(kbdc, 5);
  453     res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
  454     VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
  455     if (res != PSM_ACK)
  456         return (-1);
  457 
  458     /* 10ms delay */
  459     DELAY(10000);
  460 
  461     id = read_aux_data(kbdc);
  462     VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
  463 
  464     return id;
  465 }
  466 
  467 static int
  468 set_mouse_sampling_rate(KBDC kbdc, int rate)
  469 {
  470     int res;
  471 
  472     res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
  473     VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
  474 
  475     return ((res == PSM_ACK) ? rate : -1);
  476 }
  477 
  478 static int
  479 set_mouse_scaling(KBDC kbdc, int scale)
  480 {
  481     int res;
  482 
  483     switch (scale) {
  484     case 1:
  485     default:
  486         scale = PSMC_SET_SCALING11;
  487         break;
  488     case 2:
  489         scale = PSMC_SET_SCALING21;
  490         break;
  491     }
  492     res = send_aux_command(kbdc, scale);
  493     VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n", 
  494          (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
  495 
  496     return (res == PSM_ACK);
  497 }
  498 
  499 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
  500 static int
  501 set_mouse_resolution(KBDC kbdc, int val)
  502 {
  503     int res;
  504 
  505     res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
  506     VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
  507 
  508     return ((res == PSM_ACK) ? val : -1);
  509 }
  510 
  511 /*
  512  * NOTE: once `set_mouse_mode()' is called, the mouse device must be
  513  * re-enabled by calling `enable_aux_dev()'
  514  */
  515 static int
  516 set_mouse_mode(KBDC kbdc)
  517 {
  518     int res;
  519 
  520     res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
  521     VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
  522 
  523     return (res == PSM_ACK);
  524 }
  525 
  526 static int
  527 get_mouse_buttons(KBDC kbdc)
  528 {
  529     int c = 2;          /* assume two buttons by default */
  530     int status[3];
  531 
  532     /*
  533      * NOTE: a special sequence to obtain Logitech Mouse specific
  534      * information: set resolution to 25 ppi, set scaling to 1:1, set
  535      * scaling to 1:1, set scaling to 1:1. Then the second byte of the
  536      * mouse status bytes is the number of available buttons.
  537      * Some manufactures also support this sequence.
  538      */
  539     if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
  540         return c;
  541     if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
  542         && set_mouse_scaling(kbdc, 1) 
  543         && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
  544         if (status[1] != 0)
  545             return status[1];
  546     }
  547     return c;
  548 }
  549 
  550 /* misc subroutines */
  551 /*
  552  * Someday, I will get the complete list of valid pointing devices and
  553  * their IDs... XXX
  554  */
  555 static int
  556 is_a_mouse(int id)
  557 {
  558 #if 0
  559     static int valid_ids[] = {
  560         PSM_MOUSE_ID,           /* mouse */
  561         PSM_BALLPOINT_ID,       /* ballpoint device */
  562         PSM_INTELLI_ID,         /* Intellimouse */
  563         PSM_EXPLORER_ID,        /* Intellimouse Explorer */
  564         -1                      /* end of table */
  565     };
  566     int i;
  567 
  568     for (i = 0; valid_ids[i] >= 0; ++i)
  569         if (valid_ids[i] == id)
  570             return TRUE;
  571     return FALSE;
  572 #else
  573     return TRUE;
  574 #endif
  575 }
  576 
  577 static char *
  578 model_name(int model)
  579 {
  580     static struct {
  581         int model_code;
  582         char *model_name;
  583     } models[] = {
  584         { MOUSE_MODEL_NETSCROLL,        "NetScroll" },
  585         { MOUSE_MODEL_NET,              "NetMouse/NetScroll Optical" },
  586         { MOUSE_MODEL_GLIDEPOINT,       "GlidePoint" },
  587         { MOUSE_MODEL_THINK,            "ThinkingMouse" },
  588         { MOUSE_MODEL_INTELLI,          "IntelliMouse" },
  589         { MOUSE_MODEL_MOUSEMANPLUS,     "MouseMan+" },
  590         { MOUSE_MODEL_VERSAPAD,         "VersaPad" },
  591         { MOUSE_MODEL_EXPLORER,         "IntelliMouse Explorer" },
  592         { MOUSE_MODEL_4D,               "4D Mouse" },
  593         { MOUSE_MODEL_4DPLUS,           "4D+ Mouse" },
  594         { MOUSE_MODEL_SYNAPTICS,        "Synaptics Touchpad" },
  595         { MOUSE_MODEL_GENERIC,          "Generic PS/2 mouse" },
  596         { MOUSE_MODEL_UNKNOWN,          NULL },
  597     };
  598     int i;
  599 
  600     for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
  601         if (models[i].model_code == model)
  602             return models[i].model_name;
  603     }
  604     return "Unknown";
  605 }
  606 
  607 static void
  608 recover_from_error(KBDC kbdc)
  609 {
  610     /* discard anything left in the output buffer */
  611     empty_both_buffers(kbdc, 10);
  612 
  613 #if 0
  614     /*
  615      * NOTE: KBDC_RESET_KBD may not restore the communication between the
  616      * keyboard and the controller.
  617      */
  618     reset_kbd(kbdc);
  619 #else
  620     /*
  621      * NOTE: somehow diagnostic and keyboard port test commands bring the
  622      * keyboard back.
  623      */
  624     if (!test_controller(kbdc)) 
  625         log(LOG_ERR, "psm: keyboard controller failed.\n");
  626     /* if there isn't a keyboard in the system, the following error is OK */
  627     if (test_kbd_port(kbdc) != 0)
  628         VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
  629 #endif
  630 }
  631 
  632 static int
  633 restore_controller(KBDC kbdc, int command_byte)
  634 {
  635     empty_both_buffers(kbdc, 10);
  636 
  637     if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
  638         log(LOG_ERR, "psm: failed to restore the keyboard controller "
  639                      "command byte.\n");
  640         empty_both_buffers(kbdc, 10);
  641         return FALSE;
  642     } else {
  643         empty_both_buffers(kbdc, 10);
  644         return TRUE;
  645     }
  646 }
  647 
  648 /* 
  649  * Re-initialize the aux port and device. The aux port must be enabled
  650  * and its interrupt must be disabled before calling this routine. 
  651  * The aux device will be disabled before returning.
  652  * The keyboard controller must be locked via `kbdc_lock()' before
  653  * calling this routine.
  654  */
  655 static int
  656 doinitialize(struct psm_softc *sc, mousemode_t *mode)
  657 {
  658     KBDC kbdc = sc->kbdc;
  659     int stat[3];
  660     int i;
  661 
  662     switch((i = test_aux_port(kbdc))) {
  663     case 1:     /* ignore these errors */
  664     case 2:
  665     case 3:
  666     case PSM_ACK:
  667         if (verbose)
  668             log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
  669                 sc->unit, i);
  670         /* FALLTHROUGH */
  671     case 0:     /* no error */
  672         break;
  673     case -1:    /* time out */
  674     default:    /* error */
  675         recover_from_error(kbdc);
  676         if (sc->config & PSM_CONFIG_IGNPORTERROR)
  677             break;
  678         log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
  679             sc->unit, i);
  680         return FALSE;
  681     }
  682 
  683     if (sc->config & PSM_CONFIG_NORESET) {
  684         /* 
  685          * Don't try to reset the pointing device.  It may possibly be
  686          * left in the unknown state, though...
  687          */
  688     } else {
  689         /* 
  690          * NOTE: some controllers appears to hang the `keyboard' when
  691          * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 
  692          */
  693         if (!reset_aux_dev(kbdc)) {
  694             recover_from_error(kbdc);
  695             log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
  696             return FALSE;
  697         }
  698     }
  699 
  700     /* 
  701      * both the aux port and the aux device is functioning, see
  702      * if the device can be enabled. 
  703      */
  704     if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
  705         log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
  706         return FALSE;
  707     }
  708     empty_both_buffers(kbdc, 10);       /* remove stray data if any */
  709 
  710     if (sc->config & PSM_CONFIG_NOIDPROBE) {
  711         i = GENERIC_MOUSE_ENTRY;
  712     } else {
  713         /* FIXME: hardware ID, mouse buttons? */
  714 
  715         /* other parameters */
  716         for (i = 0; vendortype[i].probefunc != NULL; ++i) {
  717             if ((*vendortype[i].probefunc)(sc)) {
  718                 if (verbose >= 2)
  719                     log(LOG_ERR, "psm%d: found %s\n", 
  720                         sc->unit, model_name(vendortype[i].model));
  721                 break;
  722             }
  723         }
  724     }
  725 
  726     sc->hw.model = vendortype[i].model;
  727     sc->mode.packetsize = vendortype[i].packetsize;
  728 
  729     /* set mouse parameters */
  730     if (mode != (mousemode_t *)NULL) {
  731         if (mode->rate > 0)
  732             mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
  733         if (mode->resolution >= 0)
  734             mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
  735         set_mouse_scaling(kbdc, 1);
  736         set_mouse_mode(kbdc);   
  737     }
  738 
  739     /* Record sync on the next data packet we see. */
  740     sc->flags |= PSM_NEED_SYNCBITS;
  741 
  742     /* just check the status of the mouse */
  743     if (get_mouse_status(kbdc, stat, 0, 3) < 3)
  744         log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
  745             sc->unit);
  746 
  747     return TRUE;
  748 }
  749 
  750 static int
  751 doopen(struct psm_softc *sc, int command_byte)
  752 {
  753     int stat[3];
  754 
  755     /* enable the mouse device */
  756     if (!enable_aux_dev(sc->kbdc)) {
  757         /* MOUSE ERROR: failed to enable the mouse because:
  758          * 1) the mouse is faulty,
  759          * 2) the mouse has been removed(!?)
  760          * In the latter case, the keyboard may have hung, and need 
  761          * recovery procedure...
  762          */
  763         recover_from_error(sc->kbdc);
  764 #if 0
  765         /* FIXME: we could reset the mouse here and try to enable
  766          * it again. But it will take long time and it's not a good
  767          * idea to disable the keyboard that long...
  768          */
  769         if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
  770             recover_from_error(sc->kbdc);
  771 #else
  772         {
  773 #endif
  774             restore_controller(sc->kbdc, command_byte);
  775             /* mark this device is no longer available */
  776             sc->state &= ~PSM_VALID;    
  777             log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
  778                 sc->unit);
  779             return (EIO);
  780         }
  781     }
  782 
  783     if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 
  784         log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
  785 
  786     /* enable the aux port and interrupt */
  787     if (!set_controller_command_byte(sc->kbdc, 
  788             kbdc_get_device_mask(sc->kbdc),
  789             (command_byte & KBD_KBD_CONTROL_BITS)
  790                 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
  791         /* CONTROLLER ERROR */
  792         disable_aux_dev(sc->kbdc);
  793         restore_controller(sc->kbdc, command_byte);
  794         log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
  795             sc->unit);
  796         return (EIO);
  797     }
  798 
  799     /* start the watchdog timer */
  800     sc->watchdog = FALSE;
  801     sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
  802 
  803     return (0);
  804 }
  805 
  806 static int
  807 reinitialize(struct psm_softc *sc, int doinit)
  808 {
  809     int err;
  810     int c;
  811     int s;
  812 
  813     /* don't let anybody mess with the aux device */
  814     if (!kbdc_lock(sc->kbdc, TRUE))
  815         return (EIO);
  816     s = spltty();
  817 
  818     /* block our watchdog timer */
  819     sc->watchdog = FALSE;
  820     untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
  821     callout_handle_init(&sc->callout);
  822 
  823     /* save the current controller command byte */
  824     empty_both_buffers(sc->kbdc, 10);
  825     c = get_controller_command_byte(sc->kbdc);
  826     VLOG(2, (LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n", 
  827          sc->unit, c));
  828 
  829     /* enable the aux port but disable the aux interrupt and the keyboard */
  830     if ((c == -1) || !set_controller_command_byte(sc->kbdc,
  831             kbdc_get_device_mask(sc->kbdc),
  832             KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
  833                 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
  834         /* CONTROLLER ERROR */
  835         splx(s);
  836         kbdc_lock(sc->kbdc, FALSE);
  837         log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
  838             sc->unit);
  839         return (EIO);
  840     }
  841 
  842     /* flush any data */
  843     if (sc->state & PSM_VALID) {
  844         disable_aux_dev(sc->kbdc);      /* this may fail; but never mind... */
  845         empty_aux_buffer(sc->kbdc, 10);
  846     }
  847     flushpackets(sc);
  848     sc->syncerrors = 0;
  849     sc->pkterrors = 0;
  850     memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
  851 
  852     /* try to detect the aux device; are you still there? */
  853     err = 0;
  854     if (doinit) {
  855         if (doinitialize(sc, &sc->mode)) {
  856             /* yes */
  857             sc->state |= PSM_VALID;
  858         } else {
  859             /* the device has gone! */
  860             restore_controller(sc->kbdc, c);
  861             sc->state &= ~PSM_VALID;
  862             log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
  863                 sc->unit);
  864             err = ENXIO;
  865         }
  866     }
  867     splx(s);
  868 
  869     /* restore the driver state */
  870     if ((sc->state & PSM_OPEN) && (err == 0)) {
  871         /* enable the aux device and the port again */
  872         err = doopen(sc, c);
  873         if (err != 0) 
  874             log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
  875                 sc->unit);
  876     } else {
  877         /* restore the keyboard port and disable the aux port */
  878         if (!set_controller_command_byte(sc->kbdc, 
  879                 kbdc_get_device_mask(sc->kbdc),
  880                 (c & KBD_KBD_CONTROL_BITS)
  881                     | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
  882             /* CONTROLLER ERROR */
  883             log(LOG_ERR,
  884                 "psm%d: failed to disable the aux port (reinitialize).\n",
  885                 sc->unit);
  886             err = EIO;
  887         }
  888     }
  889 
  890     kbdc_lock(sc->kbdc, FALSE);
  891     return (err);
  892 }
  893 
  894 /* psm driver entry points */
  895 
  896 static void
  897 psmidentify(driver_t *driver, device_t parent)
  898 {
  899     device_t psmc;
  900     device_t psm;
  901     u_long irq;
  902     int unit;
  903 
  904     unit = device_get_unit(parent);
  905 
  906     /* always add at least one child */
  907     psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
  908     if (psm == NULL)
  909         return;
  910 
  911     irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
  912     if (irq > 0)
  913         return;
  914 
  915     /*
  916      * If the PS/2 mouse device has already been reported by ACPI or
  917      * PnP BIOS, obtain the IRQ resource from it.
  918      * (See psmcpnp_attach() below.)
  919      */
  920     psmc = device_find_child(device_get_parent(parent),
  921                              PSMCPNP_DRIVER_NAME, unit);
  922     if (psmc == NULL)
  923         return;
  924     irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
  925     if (irq <= 0)
  926         return;
  927     bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
  928 }
  929 
  930 #define endprobe(v)     do {   if (bootverbose)                         \
  931                                 --verbose;                              \
  932                             kbdc_set_device_mask(sc->kbdc, mask);       \
  933                             kbdc_lock(sc->kbdc, FALSE);                 \
  934                             return (v);                                 \
  935                         } while (0)
  936 
  937 static int
  938 psmprobe(device_t dev)
  939 {
  940     int unit = device_get_unit(dev);
  941     struct psm_softc *sc = device_get_softc(dev);
  942     int stat[3];
  943     int command_byte;
  944     int mask;
  945     int rid;
  946     int i;
  947 
  948 #if 0
  949     kbdc_debug(TRUE);
  950 #endif
  951 
  952     /* see if IRQ is available */
  953     rid = KBDC_RID_AUX;
  954     sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
  955                                       RF_SHAREABLE | RF_ACTIVE);
  956     if (sc->intr == NULL) {
  957         if (bootverbose)
  958             device_printf(dev, "unable to allocate IRQ\n");
  959         return (ENXIO);
  960     }
  961     bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
  962 
  963     sc->unit = unit;
  964     sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
  965     sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
  966     /* XXX: for backward compatibility */
  967 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
  968     sc->config |= 
  969 #ifdef PSM_RESETAFTERSUSPEND
  970         PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
  971 #else
  972         PSM_CONFIG_HOOKRESUME;
  973 #endif
  974 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
  975     sc->flags = 0;
  976     if (bootverbose)
  977         ++verbose;
  978 
  979     device_set_desc(dev, "PS/2 Mouse");
  980 
  981     if (!kbdc_lock(sc->kbdc, TRUE)) {
  982         printf("psm%d: unable to lock the controller.\n", unit);
  983         if (bootverbose)
  984             --verbose;
  985         return (ENXIO);
  986     }
  987 
  988     /*
  989      * NOTE: two bits in the command byte controls the operation of the
  990      * aux port (mouse port): the aux port disable bit (bit 5) and the aux
  991      * port interrupt (IRQ 12) enable bit (bit 2).
  992      */
  993 
  994     /* discard anything left after the keyboard initialization */
  995     empty_both_buffers(sc->kbdc, 10);
  996 
  997     /* save the current command byte; it will be used later */
  998     mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
  999     command_byte = get_controller_command_byte(sc->kbdc);
 1000     if (verbose) 
 1001         printf("psm%d: current command byte:%04x\n", unit, command_byte);
 1002     if (command_byte == -1) {
 1003         /* CONTROLLER ERROR */
 1004         printf("psm%d: unable to get the current command byte value.\n",
 1005             unit);
 1006         endprobe(ENXIO);
 1007     }
 1008 
 1009     /*
 1010      * disable the keyboard port while probing the aux port, which must be
 1011      * enabled during this routine
 1012      */
 1013     if (!set_controller_command_byte(sc->kbdc,
 1014             KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
 1015             KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
 1016                 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
 1017         /* 
 1018          * this is CONTROLLER ERROR; I don't know how to recover 
 1019          * from this error... 
 1020          */
 1021         restore_controller(sc->kbdc, command_byte);
 1022         printf("psm%d: unable to set the command byte.\n", unit);
 1023         endprobe(ENXIO);
 1024     }
 1025     write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
 1026 
 1027     /*
 1028      * NOTE: `test_aux_port()' is designed to return with zero if the aux
 1029      * port exists and is functioning. However, some controllers appears
 1030      * to respond with zero even when the aux port doesn't exist. (It may
 1031      * be that this is only the case when the controller DOES have the aux
 1032      * port but the port is not wired on the motherboard.) The keyboard
 1033      * controllers without the port, such as the original AT, are
 1034      * supporsed to return with an error code or simply time out. In any
 1035      * case, we have to continue probing the port even when the controller
 1036      * passes this test.
 1037      *
 1038      * XXX: some controllers erroneously return the error code 1, 2 or 3
 1039      * when it has the perfectly functional aux port. We have to ignore
 1040      * this error code. Even if the controller HAS error with the aux
 1041      * port, it will be detected later...
 1042      * XXX: another incompatible controller returns PSM_ACK (0xfa)...
 1043      */
 1044     switch ((i = test_aux_port(sc->kbdc))) {
 1045     case 1:        /* ignore these errors */
 1046     case 2:
 1047     case 3:
 1048     case PSM_ACK:
 1049         if (verbose)
 1050             printf("psm%d: strange result for test aux port (%d).\n",
 1051                 unit, i);
 1052         /* FALLTHROUGH */
 1053     case 0:        /* no error */
 1054         break;
 1055     case -1:        /* time out */
 1056     default:        /* error */
 1057         recover_from_error(sc->kbdc);
 1058         if (sc->config & PSM_CONFIG_IGNPORTERROR)
 1059             break;
 1060         restore_controller(sc->kbdc, command_byte);
 1061         if (verbose)
 1062             printf("psm%d: the aux port is not functioning (%d).\n",
 1063                 unit, i);
 1064         endprobe(ENXIO);
 1065     }
 1066 
 1067     if (sc->config & PSM_CONFIG_NORESET) {
 1068         /* 
 1069          * Don't try to reset the pointing device.  It may possibly be
 1070          * left in the unknown state, though...
 1071          */
 1072     } else {
 1073         /*
 1074          * NOTE: some controllers appears to hang the `keyboard' when the aux
 1075          * port doesn't exist and `PSMC_RESET_DEV' is issued.
 1076          *
 1077          * Attempt to reset the controller twice -- this helps
 1078          * pierce through some KVM switches. The second reset
 1079          * is non-fatal.
 1080          */
 1081         if (!reset_aux_dev(sc->kbdc)) {
 1082             recover_from_error(sc->kbdc);
 1083             restore_controller(sc->kbdc, command_byte);
 1084             if (verbose)
 1085                 printf("psm%d: failed to reset the aux device.\n", unit);
 1086             endprobe(ENXIO);
 1087         } else if (!reset_aux_dev(sc->kbdc)) {
 1088             recover_from_error(sc->kbdc);
 1089             if (verbose >= 2)
 1090                 printf("psm%d: failed to reset the aux device (2).\n",
 1091                     unit);
 1092         }
 1093     }
 1094 
 1095     /*
 1096      * both the aux port and the aux device is functioning, see if the
 1097      * device can be enabled. NOTE: when enabled, the device will start
 1098      * sending data; we shall immediately disable the device once we know
 1099      * the device can be enabled.
 1100      */
 1101     if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
 1102         /* MOUSE ERROR */
 1103         recover_from_error(sc->kbdc);
 1104         restore_controller(sc->kbdc, command_byte);
 1105         if (verbose)
 1106             printf("psm%d: failed to enable the aux device.\n", unit);
 1107         endprobe(ENXIO);
 1108     }
 1109 
 1110     /* save the default values after reset */
 1111     if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
 1112         sc->dflt_mode.rate = sc->mode.rate = stat[2];
 1113         sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
 1114     } else {
 1115         sc->dflt_mode.rate = sc->mode.rate = -1;
 1116         sc->dflt_mode.resolution = sc->mode.resolution = -1;
 1117     }
 1118 
 1119     /* hardware information */
 1120     sc->hw.iftype = MOUSE_IF_PS2;
 1121 
 1122     /* verify the device is a mouse */
 1123     sc->hw.hwid = get_aux_id(sc->kbdc);
 1124     if (!is_a_mouse(sc->hw.hwid)) {
 1125         restore_controller(sc->kbdc, command_byte);
 1126         if (verbose)
 1127             printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
 1128         endprobe(ENXIO);
 1129     }
 1130     switch (sc->hw.hwid) {
 1131     case PSM_BALLPOINT_ID:
 1132         sc->hw.type = MOUSE_TRACKBALL;
 1133         break;
 1134     case PSM_MOUSE_ID:
 1135     case PSM_INTELLI_ID:
 1136     case PSM_EXPLORER_ID:
 1137     case PSM_4DMOUSE_ID:
 1138     case PSM_4DPLUS_ID:
 1139         sc->hw.type = MOUSE_MOUSE;
 1140         break;
 1141     default:
 1142         sc->hw.type = MOUSE_UNKNOWN;
 1143         break;
 1144     }
 1145 
 1146     if (sc->config & PSM_CONFIG_NOIDPROBE) {
 1147         sc->hw.buttons = 2;
 1148         i = GENERIC_MOUSE_ENTRY;
 1149     } else {
 1150         /* # of buttons */
 1151         sc->hw.buttons = get_mouse_buttons(sc->kbdc);
 1152 
 1153         /* other parameters */
 1154         for (i = 0; vendortype[i].probefunc != NULL; ++i) {
 1155             if ((*vendortype[i].probefunc)(sc)) {
 1156                 if (verbose >= 2)
 1157                     printf("psm%d: found %s\n",
 1158                            unit, model_name(vendortype[i].model));
 1159                 break;
 1160             }
 1161         }
 1162     }
 1163 
 1164     sc->hw.model = vendortype[i].model;
 1165 
 1166     sc->dflt_mode.level = PSM_LEVEL_BASE;
 1167     sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
 1168     sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
 1169     if (sc->config & PSM_CONFIG_NOCHECKSYNC)
 1170         sc->dflt_mode.syncmask[0] = 0;
 1171     else
 1172         sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
 1173     if (sc->config & PSM_CONFIG_FORCETAP)
 1174         sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
 1175     sc->dflt_mode.syncmask[1] = 0;      /* syncbits */
 1176     sc->mode = sc->dflt_mode;
 1177     sc->mode.packetsize = vendortype[i].packetsize;
 1178 
 1179     /* set mouse parameters */
 1180 #if 0
 1181     /* 
 1182      * A version of Logitech FirstMouse+ won't report wheel movement,
 1183      * if SET_DEFAULTS is sent...  Don't use this command.
 1184      * This fix was found by Takashi Nishida.
 1185      */
 1186     i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
 1187     if (verbose >= 2)
 1188         printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
 1189 #endif
 1190     if (sc->config & PSM_CONFIG_RESOLUTION) {
 1191         sc->mode.resolution
 1192             = set_mouse_resolution(sc->kbdc, 
 1193                                    (sc->config & PSM_CONFIG_RESOLUTION) - 1);
 1194     } else if (sc->mode.resolution >= 0) {
 1195         sc->mode.resolution
 1196             = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
 1197     }
 1198     if (sc->mode.rate > 0) {
 1199         sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
 1200     }
 1201     set_mouse_scaling(sc->kbdc, 1);
 1202 
 1203     /* Record sync on the next data packet we see. */
 1204     sc->flags |= PSM_NEED_SYNCBITS;
 1205 
 1206     /* just check the status of the mouse */
 1207     /* 
 1208      * NOTE: XXX there are some arcane controller/mouse combinations out 
 1209      * there, which hung the controller unless there is data transmission 
 1210      * after ACK from the mouse.
 1211      */
 1212     if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
 1213         printf("psm%d: failed to get status.\n", unit);
 1214     } else {
 1215         /* 
 1216          * When in its native mode, some mice operate with different 
 1217          * default parameters than in the PS/2 compatible mode.
 1218          */
 1219         sc->dflt_mode.rate = sc->mode.rate = stat[2];
 1220         sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
 1221      }
 1222 
 1223     /* disable the aux port for now... */
 1224     if (!set_controller_command_byte(sc->kbdc, 
 1225             KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
 1226             (command_byte & KBD_KBD_CONTROL_BITS)
 1227                 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
 1228         /* 
 1229          * this is CONTROLLER ERROR; I don't know the proper way to 
 1230          * recover from this error... 
 1231          */
 1232         restore_controller(sc->kbdc, command_byte);
 1233         printf("psm%d: unable to set the command byte.\n", unit);
 1234         endprobe(ENXIO);
 1235     }
 1236 
 1237     /* done */
 1238     kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
 1239     kbdc_lock(sc->kbdc, FALSE);
 1240     return (0);
 1241 }
 1242 
 1243 static int
 1244 psmattach(device_t dev)
 1245 {
 1246     int unit = device_get_unit(dev);
 1247     struct psm_softc *sc = device_get_softc(dev);
 1248     int error;
 1249     int rid;
 1250 
 1251     /* Setup initial state */
 1252     sc->state = PSM_VALID;
 1253     callout_handle_init(&sc->callout);
 1254 
 1255     /* Setup our interrupt handler */
 1256     rid = KBDC_RID_AUX;
 1257     sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
 1258                                       RF_SHAREABLE | RF_ACTIVE);
 1259     if (sc->intr == NULL)
 1260         return (ENXIO);
 1261     error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
 1262     if (error) {
 1263         bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
 1264         return (error);
 1265     }
 1266 
 1267     /* Done */
 1268     sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
 1269                        "psm%d", unit);
 1270     sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
 1271                         "bpsm%d", unit);
 1272 
 1273     if (!verbose) {
 1274         printf("psm%d: model %s, device ID %d\n", 
 1275             unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
 1276     } else {
 1277         printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
 1278             unit, model_name(sc->hw.model),
 1279             sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
 1280         printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
 1281             unit, sc->config, sc->flags, sc->mode.packetsize);
 1282         printf("psm%d: syncmask:%02x, syncbits:%02x\n",
 1283             unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
 1284     }
 1285 
 1286     if (bootverbose)
 1287         --verbose;
 1288 
 1289     return (0);
 1290 }
 1291 
 1292 static int
 1293 psmdetach(device_t dev)
 1294 {
 1295     struct psm_softc *sc;
 1296     int rid;
 1297 
 1298     sc = device_get_softc(dev);
 1299     if (sc->state & PSM_OPEN)
 1300         return EBUSY;
 1301 
 1302     rid = KBDC_RID_AUX;
 1303     bus_teardown_intr(dev, sc->intr, sc->ih);
 1304     bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
 1305 
 1306     destroy_dev(sc->dev);
 1307     destroy_dev(sc->bdev);
 1308 
 1309     return 0;
 1310 }
 1311 
 1312 static int
 1313 psmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
 1314 {
 1315     int unit = PSM_UNIT(dev);
 1316     struct psm_softc *sc;
 1317     int command_byte;
 1318     int err;
 1319     int s;
 1320 
 1321     /* Get device data */
 1322     sc = PSM_SOFTC(unit);
 1323     if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
 1324         /* the device is no longer valid/functioning */
 1325         return (ENXIO);
 1326 
 1327     /* Disallow multiple opens */
 1328     if (sc->state & PSM_OPEN)
 1329         return (EBUSY);
 1330 
 1331     device_busy(devclass_get_device(psm_devclass, unit));
 1332 
 1333     /* Initialize state */
 1334     sc->mode.level = sc->dflt_mode.level;
 1335     sc->mode.protocol = sc->dflt_mode.protocol;
 1336     sc->watchdog = FALSE;
 1337 
 1338     /* flush the event queue */
 1339     sc->queue.count = 0;
 1340     sc->queue.head = 0;
 1341     sc->queue.tail = 0;
 1342     sc->status.flags = 0;
 1343     sc->status.button = 0;
 1344     sc->status.obutton = 0;
 1345     sc->status.dx = 0;
 1346     sc->status.dy = 0;
 1347     sc->status.dz = 0;
 1348     sc->button = 0;
 1349     sc->pqueue_start = 0;
 1350     sc->pqueue_end = 0;
 1351 
 1352     /* empty input buffer */
 1353     flushpackets(sc);
 1354     sc->syncerrors = 0;
 1355     sc->pkterrors = 0;
 1356 
 1357     /* don't let timeout routines in the keyboard driver to poll the kbdc */
 1358     if (!kbdc_lock(sc->kbdc, TRUE))
 1359         return (EIO);
 1360 
 1361     /* save the current controller command byte */
 1362     s = spltty();
 1363     command_byte = get_controller_command_byte(sc->kbdc);
 1364 
 1365     /* enable the aux port and temporalily disable the keyboard */
 1366     if ((command_byte == -1) 
 1367         || !set_controller_command_byte(sc->kbdc,
 1368             kbdc_get_device_mask(sc->kbdc),
 1369             KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
 1370                 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
 1371         /* CONTROLLER ERROR; do you know how to get out of this? */
 1372         kbdc_lock(sc->kbdc, FALSE);
 1373         splx(s);
 1374         log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
 1375             unit);
 1376         return (EIO);
 1377     }
 1378     /* 
 1379      * Now that the keyboard controller is told not to generate 
 1380      * the keyboard and mouse interrupts, call `splx()' to allow 
 1381      * the other tty interrupts. The clock interrupt may also occur, 
 1382      * but timeout routines will be blocked by the poll flag set 
 1383      * via `kbdc_lock()'
 1384      */
 1385     splx(s);
 1386   
 1387     /* enable the mouse device */
 1388     err = doopen(sc, command_byte);
 1389 
 1390     /* done */
 1391     if (err == 0) 
 1392         sc->state |= PSM_OPEN;
 1393     kbdc_lock(sc->kbdc, FALSE);
 1394     return (err);
 1395 }
 1396 
 1397 static int
 1398 psmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
 1399 {
 1400     int unit = PSM_UNIT(dev);
 1401     struct psm_softc *sc = PSM_SOFTC(unit);
 1402     int stat[3];
 1403     int command_byte;
 1404     int s;
 1405 
 1406     /* don't let timeout routines in the keyboard driver to poll the kbdc */
 1407     if (!kbdc_lock(sc->kbdc, TRUE))
 1408         return (EIO);
 1409 
 1410     /* save the current controller command byte */
 1411     s = spltty();
 1412     command_byte = get_controller_command_byte(sc->kbdc);
 1413     if (command_byte == -1) {
 1414         kbdc_lock(sc->kbdc, FALSE);
 1415         splx(s);
 1416         return (EIO);
 1417     }
 1418 
 1419     /* disable the aux interrupt and temporalily disable the keyboard */
 1420     if (!set_controller_command_byte(sc->kbdc, 
 1421             kbdc_get_device_mask(sc->kbdc),
 1422             KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
 1423                 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
 1424         log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
 1425             unit);
 1426         /* CONTROLLER ERROR;
 1427          * NOTE: we shall force our way through. Because the only
 1428          * ill effect we shall see is that we may not be able
 1429          * to read ACK from the mouse, and it doesn't matter much 
 1430          * so long as the mouse will accept the DISABLE command.
 1431          */
 1432     }
 1433     splx(s);
 1434 
 1435     /* stop the watchdog timer */
 1436     untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
 1437     callout_handle_init(&sc->callout);
 1438 
 1439     /* remove anything left in the output buffer */
 1440     empty_aux_buffer(sc->kbdc, 10);
 1441 
 1442     /* disable the aux device, port and interrupt */
 1443     if (sc->state & PSM_VALID) {
 1444         if (!disable_aux_dev(sc->kbdc)) {
 1445             /* MOUSE ERROR; 
 1446              * NOTE: we don't return error and continue, pretending 
 1447              * we have successfully disabled the device. It's OK because 
 1448              * the interrupt routine will discard any data from the mouse
 1449              * hereafter. 
 1450              */
 1451             log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
 1452                 unit);
 1453         }
 1454 
 1455         if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
 1456             log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n", unit);
 1457     }
 1458 
 1459     if (!set_controller_command_byte(sc->kbdc, 
 1460             kbdc_get_device_mask(sc->kbdc),
 1461             (command_byte & KBD_KBD_CONTROL_BITS)
 1462                 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
 1463         /* CONTROLLER ERROR; 
 1464          * we shall ignore this error; see the above comment.
 1465          */
 1466         log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
 1467             unit);
 1468     }
 1469 
 1470     /* remove anything left in the output buffer */
 1471     empty_aux_buffer(sc->kbdc, 10);
 1472 
 1473     /* close is almost always successful */
 1474     sc->state &= ~PSM_OPEN;
 1475     kbdc_lock(sc->kbdc, FALSE);
 1476     device_unbusy(devclass_get_device(psm_devclass, unit));
 1477     return (0);
 1478 }
 1479 
 1480 static int
 1481 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, unsigned char *buf)
 1482 {
 1483     static unsigned char butmapps2[8] = {
 1484         0,
 1485         MOUSE_PS2_BUTTON1DOWN, 
 1486         MOUSE_PS2_BUTTON2DOWN,
 1487         MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
 1488         MOUSE_PS2_BUTTON3DOWN,
 1489         MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
 1490         MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
 1491         MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
 1492     };
 1493     static unsigned char butmapmsc[8] = {
 1494         MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
 1495         MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
 1496         MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
 1497         MOUSE_MSC_BUTTON3UP,
 1498         MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
 1499         MOUSE_MSC_BUTTON2UP,
 1500         MOUSE_MSC_BUTTON1UP, 
 1501         0,
 1502     };
 1503     int mapped;
 1504     int i;
 1505 
 1506     if (sc->mode.level == PSM_LEVEL_BASE) {
 1507         mapped = status->button & ~MOUSE_BUTTON4DOWN;
 1508         if (status->button & MOUSE_BUTTON4DOWN) 
 1509             mapped |= MOUSE_BUTTON1DOWN;
 1510         status->button = mapped;
 1511         buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
 1512         i = imax(imin(status->dx, 255), -256);
 1513         if (i < 0)
 1514             buf[0] |= MOUSE_PS2_XNEG;
 1515         buf[1] = i;
 1516         i = imax(imin(status->dy, 255), -256);
 1517         if (i < 0)
 1518             buf[0] |= MOUSE_PS2_YNEG;
 1519         buf[2] = i;
 1520         return MOUSE_PS2_PACKETSIZE;
 1521     } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
 1522         buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
 1523         i = imax(imin(status->dx, 255), -256);
 1524         buf[1] = i >> 1;
 1525         buf[3] = i - buf[1];
 1526         i = imax(imin(status->dy, 255), -256);
 1527         buf[2] = i >> 1;
 1528         buf[4] = i - buf[2];
 1529         i = imax(imin(status->dz, 127), -128);
 1530         buf[5] = (i >> 1) & 0x7f;
 1531         buf[6] = (i - (i >> 1)) & 0x7f;
 1532         buf[7] = (~status->button >> 3) & 0x7f;
 1533         return MOUSE_SYS_PACKETSIZE;
 1534     }
 1535     return pb->inputbytes;
 1536 }
 1537 
 1538 static int
 1539 psmread(struct cdev *dev, struct uio *uio, int flag)
 1540 {
 1541     register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
 1542     unsigned char buf[PSM_SMALLBUFSIZE];
 1543     int error = 0;
 1544     int s;
 1545     int l;
 1546 
 1547     if ((sc->state & PSM_VALID) == 0)
 1548         return EIO;
 1549 
 1550     /* block until mouse activity occured */
 1551     s = spltty();
 1552     while (sc->queue.count <= 0) {
 1553         if (PSM_NBLOCKIO(dev)) {
 1554             splx(s);
 1555             return EWOULDBLOCK;
 1556         }
 1557         sc->state |= PSM_ASLP;
 1558         error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
 1559         sc->state &= ~PSM_ASLP;
 1560         if (error) {
 1561             splx(s);
 1562             return error;
 1563         } else if ((sc->state & PSM_VALID) == 0) {
 1564             /* the device disappeared! */
 1565             splx(s);
 1566             return EIO;
 1567         }
 1568     }
 1569     splx(s);
 1570 
 1571     /* copy data to the user land */
 1572     while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
 1573         s = spltty();
 1574         l = imin(sc->queue.count, uio->uio_resid);
 1575         if (l > sizeof(buf))
 1576             l = sizeof(buf);
 1577         if (l > sizeof(sc->queue.buf) - sc->queue.head) {
 1578             bcopy(&sc->queue.buf[sc->queue.head], &buf[0], 
 1579                 sizeof(sc->queue.buf) - sc->queue.head);
 1580             bcopy(&sc->queue.buf[0], 
 1581                 &buf[sizeof(sc->queue.buf) - sc->queue.head],
 1582                 l - (sizeof(sc->queue.buf) - sc->queue.head));
 1583         } else {
 1584             bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
 1585         }
 1586         sc->queue.count -= l;
 1587         sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
 1588         splx(s);
 1589         error = uiomove(buf, l, uio);
 1590         if (error)
 1591             break;
 1592     }
 1593 
 1594     return error;
 1595 }
 1596 
 1597 static int
 1598 block_mouse_data(struct psm_softc *sc, int *c)
 1599 {
 1600     int s;
 1601 
 1602     if (!kbdc_lock(sc->kbdc, TRUE)) 
 1603         return EIO;
 1604 
 1605     s = spltty();
 1606     *c = get_controller_command_byte(sc->kbdc);
 1607     if ((*c == -1) 
 1608         || !set_controller_command_byte(sc->kbdc, 
 1609             kbdc_get_device_mask(sc->kbdc),
 1610             KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
 1611                 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
 1612         /* this is CONTROLLER ERROR */
 1613         splx(s);
 1614         kbdc_lock(sc->kbdc, FALSE);
 1615         return EIO;
 1616     }
 1617 
 1618     /* 
 1619      * The device may be in the middle of status data transmission.
 1620      * The transmission will be interrupted, thus, incomplete status 
 1621      * data must be discarded. Although the aux interrupt is disabled 
 1622      * at the keyboard controller level, at most one aux interrupt 
 1623      * may have already been pending and a data byte is in the 
 1624      * output buffer; throw it away. Note that the second argument 
 1625      * to `empty_aux_buffer()' is zero, so that the call will just 
 1626      * flush the internal queue.
 1627      * `psmintr()' will be invoked after `splx()' if an interrupt is
 1628      * pending; it will see no data and returns immediately.
 1629      */
 1630     empty_aux_buffer(sc->kbdc, 0);      /* flush the queue */
 1631     read_aux_data_no_wait(sc->kbdc);    /* throw away data if any */
 1632     flushpackets(sc);
 1633     splx(s);
 1634 
 1635     return 0;
 1636 }
 1637 
 1638 static void
 1639 dropqueue(struct psm_softc *sc)
 1640 {
 1641 
 1642         sc->queue.count = 0;
 1643         sc->queue.head = 0;
 1644         sc->queue.tail = 0;
 1645         if ((sc->state & PSM_SOFTARMED) != 0) {
 1646                 sc->state &= ~PSM_SOFTARMED;
 1647                 untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout);
 1648         }
 1649         sc->pqueue_start = sc->pqueue_end;
 1650 }
 1651 
 1652 static void
 1653 flushpackets(struct psm_softc *sc)
 1654 {
 1655 
 1656         dropqueue(sc);
 1657         bzero(&sc->pqueue, sizeof(sc->pqueue));
 1658 }
 1659 
 1660 static int
 1661 unblock_mouse_data(struct psm_softc *sc, int c)
 1662 {
 1663     int error = 0;
 1664 
 1665     /* 
 1666      * We may have seen a part of status data during `set_mouse_XXX()'.
 1667      * they have been queued; flush it.
 1668      */
 1669     empty_aux_buffer(sc->kbdc, 0);
 1670 
 1671     /* restore ports and interrupt */
 1672     if (!set_controller_command_byte(sc->kbdc, 
 1673             kbdc_get_device_mask(sc->kbdc),
 1674             c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
 1675         /* CONTROLLER ERROR; this is serious, we may have
 1676          * been left with the inaccessible keyboard and
 1677          * the disabled mouse interrupt. 
 1678          */
 1679         error = EIO;
 1680     }
 1681 
 1682     kbdc_lock(sc->kbdc, FALSE);
 1683     return error;
 1684 }
 1685 
 1686 static int
 1687 psmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
 1688 {
 1689     struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
 1690     mousemode_t mode;
 1691     mousestatus_t status;
 1692 #if (defined(MOUSE_GETVARS))
 1693     mousevar_t *var;
 1694 #endif
 1695     mousedata_t *data;
 1696     int stat[3];
 1697     int command_byte;
 1698     int error = 0;
 1699     int s;
 1700 
 1701     /* Perform IOCTL command */
 1702     switch (cmd) {
 1703 
 1704     case OLD_MOUSE_GETHWINFO:
 1705         s = spltty();
 1706         ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
 1707         ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
 1708         ((old_mousehw_t *)addr)->type = sc->hw.type;
 1709         ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
 1710         splx(s);
 1711         break;
 1712 
 1713     case MOUSE_GETHWINFO:
 1714         s = spltty();
 1715         *(mousehw_t *)addr = sc->hw;
 1716         if (sc->mode.level == PSM_LEVEL_BASE)
 1717             ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
 1718         splx(s);
 1719         break;
 1720 
 1721     case MOUSE_SYN_GETHWINFO:
 1722         s = spltty();
 1723         if (synaptics_support && sc->hw.model == MOUSE_MODEL_SYNAPTICS)
 1724             *(synapticshw_t *)addr = sc->synhw;
 1725         else
 1726             error = EINVAL;
 1727         splx(s);
 1728         break;
 1729 
 1730     case OLD_MOUSE_GETMODE:
 1731         s = spltty();
 1732         switch (sc->mode.level) {
 1733         case PSM_LEVEL_BASE:
 1734             ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
 1735             break;
 1736         case PSM_LEVEL_STANDARD:
 1737             ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
 1738             break;
 1739         case PSM_LEVEL_NATIVE:
 1740             ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
 1741             break;
 1742         }
 1743         ((old_mousemode_t *)addr)->rate = sc->mode.rate;
 1744         ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
 1745         ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
 1746         splx(s);
 1747         break;
 1748 
 1749     case MOUSE_GETMODE:
 1750         s = spltty();
 1751         *(mousemode_t *)addr = sc->mode;
 1752         if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
 1753             ((mousemode_t *)addr)->syncmask[0] = 0;
 1754             ((mousemode_t *)addr)->syncmask[1] = 0;
 1755         }
 1756         ((mousemode_t *)addr)->resolution = 
 1757             MOUSE_RES_LOW - sc->mode.resolution;
 1758         switch (sc->mode.level) {
 1759         case PSM_LEVEL_BASE:
 1760             ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
 1761             ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
 1762             break;
 1763         case PSM_LEVEL_STANDARD:
 1764             ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
 1765             ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
 1766             ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
 1767             ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
 1768             break;
 1769         case PSM_LEVEL_NATIVE:
 1770             /* FIXME: this isn't quite correct... XXX */
 1771             ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
 1772             break;
 1773         }
 1774         splx(s);
 1775         break;
 1776 
 1777     case OLD_MOUSE_SETMODE:
 1778     case MOUSE_SETMODE:
 1779         if (cmd == OLD_MOUSE_SETMODE) {
 1780             mode.rate = ((old_mousemode_t *)addr)->rate;
 1781             /*
 1782              * resolution  old I/F   new I/F
 1783              * default        0         0
 1784              * low            1        -2
 1785              * medium low     2        -3
 1786              * medium high    3        -4
 1787              * high           4        -5
 1788              */
 1789             if (((old_mousemode_t *)addr)->resolution > 0)
 1790                 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
 1791             mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
 1792             mode.level = -1;
 1793         } else {
 1794             mode = *(mousemode_t *)addr;
 1795         }
 1796 
 1797         /* adjust and validate parameters. */
 1798         if (mode.rate > UCHAR_MAX)
 1799             return EINVAL;
 1800         if (mode.rate == 0)
 1801             mode.rate = sc->dflt_mode.rate;
 1802         else if (mode.rate == -1)
 1803             /* don't change the current setting */
 1804             ;
 1805         else if (mode.rate < 0)
 1806             return EINVAL;
 1807         if (mode.resolution >= UCHAR_MAX)
 1808             return EINVAL;
 1809         if (mode.resolution >= 200)
 1810             mode.resolution = MOUSE_RES_HIGH;
 1811         else if (mode.resolution >= 100)
 1812             mode.resolution = MOUSE_RES_MEDIUMHIGH;
 1813         else if (mode.resolution >= 50)
 1814             mode.resolution = MOUSE_RES_MEDIUMLOW;
 1815         else if (mode.resolution > 0)
 1816             mode.resolution = MOUSE_RES_LOW;
 1817         if (mode.resolution == MOUSE_RES_DEFAULT)
 1818             mode.resolution = sc->dflt_mode.resolution;
 1819         else if (mode.resolution == -1)
 1820             /* don't change the current setting */
 1821             ;
 1822         else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
 1823             mode.resolution = MOUSE_RES_LOW - mode.resolution;
 1824         if (mode.level == -1)
 1825             /* don't change the current setting */
 1826             mode.level = sc->mode.level;
 1827         else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
 1828             return EINVAL;
 1829         if (mode.accelfactor == -1)
 1830             /* don't change the current setting */
 1831             mode.accelfactor = sc->mode.accelfactor;
 1832         else if (mode.accelfactor < 0)
 1833             return EINVAL;
 1834 
 1835         /* don't allow anybody to poll the keyboard controller */
 1836         error = block_mouse_data(sc, &command_byte);
 1837         if (error)
 1838             return error;
 1839 
 1840         /* set mouse parameters */
 1841         if (mode.rate > 0)
 1842             mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
 1843         if (mode.resolution >= 0)
 1844             mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
 1845         set_mouse_scaling(sc->kbdc, 1);
 1846         get_mouse_status(sc->kbdc, stat, 0, 3);
 1847 
 1848         s = spltty();
 1849         sc->mode.rate = mode.rate;
 1850         sc->mode.resolution = mode.resolution;
 1851         sc->mode.accelfactor = mode.accelfactor;
 1852         sc->mode.level = mode.level;
 1853         splx(s);
 1854 
 1855         unblock_mouse_data(sc, command_byte);
 1856         break;
 1857 
 1858     case MOUSE_GETLEVEL:
 1859         *(int *)addr = sc->mode.level;
 1860         break;
 1861 
 1862     case MOUSE_SETLEVEL:
 1863         if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
 1864             return EINVAL;
 1865         sc->mode.level = *(int *)addr;
 1866         break;
 1867 
 1868     case MOUSE_GETSTATUS:
 1869         s = spltty();
 1870         status = sc->status;
 1871         sc->status.flags = 0;
 1872         sc->status.obutton = sc->status.button;
 1873         sc->status.button = 0;
 1874         sc->status.dx = 0;
 1875         sc->status.dy = 0;
 1876         sc->status.dz = 0;
 1877         splx(s);
 1878         *(mousestatus_t *)addr = status;
 1879         break;
 1880 
 1881 #if (defined(MOUSE_GETVARS))
 1882     case MOUSE_GETVARS:
 1883         var = (mousevar_t *)addr;
 1884         bzero(var, sizeof(*var));
 1885         s = spltty();
 1886         var->var[0] = MOUSE_VARS_PS2_SIG;
 1887         var->var[1] = sc->config;
 1888         var->var[2] = sc->flags;
 1889         splx(s);
 1890         break;
 1891 
 1892     case MOUSE_SETVARS:
 1893         return ENODEV;
 1894 #endif /* MOUSE_GETVARS */
 1895 
 1896     case MOUSE_READSTATE:
 1897     case MOUSE_READDATA:
 1898         data = (mousedata_t *)addr;
 1899         if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
 1900             return EINVAL;
 1901 
 1902         error = block_mouse_data(sc, &command_byte);
 1903         if (error)
 1904             return error;
 1905         if ((data->len = get_mouse_status(sc->kbdc, data->buf, 
 1906                 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
 1907             error = EIO;
 1908         unblock_mouse_data(sc, command_byte);
 1909         break;
 1910 
 1911 #if (defined(MOUSE_SETRESOLUTION))
 1912     case MOUSE_SETRESOLUTION:
 1913         mode.resolution = *(int *)addr;
 1914         if (mode.resolution >= UCHAR_MAX)
 1915             return EINVAL;
 1916         else if (mode.resolution >= 200)
 1917             mode.resolution = MOUSE_RES_HIGH;
 1918         else if (mode.resolution >= 100)
 1919             mode.resolution = MOUSE_RES_MEDIUMHIGH;
 1920         else if (mode.resolution >= 50)
 1921             mode.resolution = MOUSE_RES_MEDIUMLOW;
 1922         else if (mode.resolution > 0)
 1923             mode.resolution = MOUSE_RES_LOW;
 1924         if (mode.resolution == MOUSE_RES_DEFAULT)
 1925             mode.resolution = sc->dflt_mode.resolution;
 1926         else if (mode.resolution == -1)
 1927             mode.resolution = sc->mode.resolution;
 1928         else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
 1929             mode.resolution = MOUSE_RES_LOW - mode.resolution;
 1930 
 1931         error = block_mouse_data(sc, &command_byte);
 1932         if (error)
 1933             return error;
 1934         sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
 1935         if (sc->mode.resolution != mode.resolution)
 1936             error = EIO;
 1937         unblock_mouse_data(sc, command_byte);
 1938         break;
 1939 #endif /* MOUSE_SETRESOLUTION */
 1940 
 1941 #if (defined(MOUSE_SETRATE))
 1942     case MOUSE_SETRATE:
 1943         mode.rate = *(int *)addr;
 1944         if (mode.rate > UCHAR_MAX)
 1945             return EINVAL;
 1946         if (mode.rate == 0)
 1947             mode.rate = sc->dflt_mode.rate;
 1948         else if (mode.rate < 0)
 1949             mode.rate = sc->mode.rate;
 1950 
 1951         error = block_mouse_data(sc, &command_byte);
 1952         if (error)
 1953             return error;
 1954         sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
 1955         if (sc->mode.rate != mode.rate)
 1956             error = EIO;
 1957         unblock_mouse_data(sc, command_byte);
 1958         break;
 1959 #endif /* MOUSE_SETRATE */
 1960 
 1961 #if (defined(MOUSE_SETSCALING))
 1962     case MOUSE_SETSCALING:
 1963         if ((*(int *)addr <= 0) || (*(int *)addr > 2))
 1964             return EINVAL;
 1965 
 1966         error = block_mouse_data(sc, &command_byte);
 1967         if (error)
 1968             return error;
 1969         if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
 1970             error = EIO;
 1971         unblock_mouse_data(sc, command_byte);
 1972         break;
 1973 #endif /* MOUSE_SETSCALING */
 1974 
 1975 #if (defined(MOUSE_GETHWID))
 1976     case MOUSE_GETHWID:
 1977         error = block_mouse_data(sc, &command_byte);
 1978         if (error)
 1979             return error;
 1980         sc->hw.hwid &= ~0x00ff;
 1981         sc->hw.hwid |= get_aux_id(sc->kbdc);
 1982         *(int *)addr = sc->hw.hwid & 0x00ff;
 1983         unblock_mouse_data(sc, command_byte);
 1984         break;
 1985 #endif /* MOUSE_GETHWID */
 1986 
 1987     default:
 1988         return ENOTTY;
 1989     }
 1990 
 1991     return error;
 1992 }
 1993 
 1994 static void
 1995 psmtimeout(void *arg)
 1996 {
 1997     struct psm_softc *sc;
 1998     int s;
 1999 
 2000     sc = (struct psm_softc *)arg;
 2001     s = spltty();
 2002     if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
 2003         VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
 2004         psmintr(sc);
 2005         kbdc_lock(sc->kbdc, FALSE);
 2006     }
 2007     sc->watchdog = TRUE;
 2008     splx(s);
 2009     sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
 2010 }
 2011 
 2012 static int psmhz = 20;
 2013 SYSCTL_INT(_debug, OID_AUTO, psmhz, CTLFLAG_RW, &psmhz, 0, "");
 2014 
 2015 static int psm_soft_timeout = 500000; /* 0.5 sec */
 2016 SYSCTL_INT(_debug, OID_AUTO, psm_soft_timeout, CTLFLAG_RW,
 2017     &psm_soft_timeout, 0, "");
 2018 
 2019 static int psmerrsecs = 2;
 2020 SYSCTL_INT(_debug, OID_AUTO, psmerrsecs, CTLFLAG_RW, &psmerrsecs, 0, "");
 2021 static int psmerrusecs = 0;
 2022 SYSCTL_INT(_debug, OID_AUTO, psmerrusecs, CTLFLAG_RW, &psmerrusecs, 0, "");
 2023 static int psmsecs = 0;
 2024 SYSCTL_INT(_debug, OID_AUTO, psmsecs, CTLFLAG_RW, &psmsecs, 0, "");
 2025 static int psmusecs = 500000;
 2026 SYSCTL_INT(_debug, OID_AUTO, psmusecs, CTLFLAG_RW, &psmusecs, 0, "");
 2027 
 2028 static int psmpkterrthresh = 2;
 2029 SYSCTL_INT(_debug, OID_AUTO, psmpkterrthresh, CTLFLAG_RW,
 2030     &psmpkterrthresh, 0, "");
 2031 
 2032 SYSCTL_INT(_debug, OID_AUTO, psmloglevel, CTLFLAG_RW, &verbose, 0, "");
 2033 
 2034 
 2035 static void
 2036 psmintr(void *arg)
 2037 {
 2038     struct psm_softc *sc = arg;
 2039     struct timeval now;
 2040     int c;
 2041     packetbuf_t *pb;
 2042 
 2043 
 2044     /* read until there is nothing to read */
 2045     while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
 2046     
 2047         pb = &sc->pqueue[sc->pqueue_end];
 2048         /* discard the byte if the device is not open */
 2049         if ((sc->state & PSM_OPEN) == 0)
 2050             continue;
 2051     
 2052         getmicrouptime(&now);
 2053         if ((pb->inputbytes > 0) && timevalcmp(&now, &sc->inputtimeout, >)) {
 2054             VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
 2055                  "resetting byte count\n"));
 2056             pb->inputbytes = 0;
 2057             sc->syncerrors = 0;
 2058             sc->pkterrors = 0;
 2059         }
 2060         sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
 2061         sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
 2062         timevaladd(&sc->inputtimeout, &now);
 2063 
 2064         pb->ipacket[pb->inputbytes++] = c;
 2065         if (pb->inputbytes < sc->mode.packetsize) 
 2066             continue;
 2067 
 2068         VLOG(4, (LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
 2069              pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
 2070              pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
 2071 
 2072         c = pb->ipacket[0];
 2073 
 2074         if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
 2075             sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
 2076             sc->flags &= ~PSM_NEED_SYNCBITS;
 2077             VLOG(2, (LOG_DEBUG, "psmintr: Sync bytes now %04x,%04x\n",
 2078                  sc->mode.syncmask[0], sc->mode.syncmask[0]));
 2079         } else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
 2080             VLOG(3, (LOG_DEBUG, "psmintr: out of sync (%04x != %04x) %d"
 2081                  " cmds since last error.\n", 
 2082                  c & sc->mode.syncmask[0], sc->mode.syncmask[1],
 2083                  sc->cmdcount - sc->lasterr));
 2084             sc->lasterr = sc->cmdcount;
 2085             /*
 2086              * The sync byte test is a weak measure of packet
 2087              * validity.  Conservatively discard any input yet
 2088              * to be seen by userland when we detect a sync
 2089              * error since there is a good chance some of
 2090              * the queued packets have undetected errors.
 2091              */
 2092             dropqueue(sc);
 2093             if (sc->syncerrors == 0)
 2094                 sc->pkterrors++;
 2095             ++sc->syncerrors;
 2096             sc->lastinputerr = now;
 2097             if (sc->syncerrors >= sc->mode.packetsize * 2 ||
 2098                 sc->pkterrors >= psmpkterrthresh) {
 2099 
 2100                 /*
 2101                  * If we've failed to find a single sync byte in 2
 2102                  * packets worth of data, or we've seen persistent
 2103                  * packet errors during the validation period,
 2104                  * reinitialize the mouse in hopes of returning it
 2105                  * to the expected mode.
 2106                  */
 2107                 VLOG(3, (LOG_DEBUG, "psmintr: reset the mouse.\n"));
 2108                 reinitialize(sc, TRUE);
 2109             } else if (sc->syncerrors == sc->mode.packetsize) {
 2110 
 2111                 /*
 2112                  * Try a soft reset after searching for a sync
 2113                  * byte through a packet length of bytes.
 2114                  */
 2115                 VLOG(3, (LOG_DEBUG, "psmintr: re-enable the mouse.\n"));
 2116                 pb->inputbytes = 0;
 2117                 disable_aux_dev(sc->kbdc);
 2118                 enable_aux_dev(sc->kbdc);
 2119             } else {
 2120                 VLOG(3, (LOG_DEBUG, "psmintr: discard a byte (%d)\n",
 2121                      sc->syncerrors));
 2122                 pb->inputbytes--;
 2123                 bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
 2124             }
 2125             continue;
 2126         }
 2127 
 2128         /*
 2129          * We have what appears to be a valid packet.
 2130          * Reset the error counters.
 2131          */
 2132         sc->syncerrors = 0;
 2133 
 2134         /*
 2135          * Drop even good packets if they occur within a timeout
 2136          * period of a sync error.  This allows the detection of
 2137          * a change in the mouse's packet mode without exposing
 2138          * erratic mouse behavior to the user.  Some KVMs forget
 2139          * enhanced mouse modes during switch events.
 2140          */
 2141         if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, &now)) {
 2142                 pb->inputbytes = 0;
 2143                 continue;
 2144         }
 2145 
 2146         /*
 2147          * Now that we're out of the validation period, reset
 2148          * the packet error count.
 2149          */
 2150         sc->pkterrors = 0;
 2151 
 2152         sc->cmdcount++;
 2153         if (++sc->pqueue_end >= PSM_PACKETQUEUE)
 2154                 sc->pqueue_end = 0;
 2155         /*
 2156          * If we've filled the queue then call the softintr ourselves,
 2157          * otherwise schedule the interrupt for later.
 2158          */
 2159         if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
 2160             (sc->pqueue_end == sc->pqueue_start)) {
 2161                 if ((sc->state & PSM_SOFTARMED) != 0) {
 2162                         sc->state &= ~PSM_SOFTARMED;
 2163                         untimeout(psmsoftintr, arg, sc->softcallout);
 2164                 }
 2165                 psmsoftintr(arg);
 2166         } else if ((sc->state & PSM_SOFTARMED) == 0) {
 2167                 sc->state |= PSM_SOFTARMED;
 2168                 sc->softcallout = timeout(psmsoftintr, arg,
 2169                     psmhz < 1 ? 1 : (hz/psmhz));
 2170         }
 2171     }
 2172 }
 2173 
 2174 static void
 2175 psmsoftintr(void *arg)
 2176 {
 2177     /*
 2178      * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
 2179      * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
 2180      */
 2181     static int butmap[8] = {
 2182         0, 
 2183         MOUSE_BUTTON1DOWN, 
 2184         MOUSE_BUTTON3DOWN, 
 2185         MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
 2186         MOUSE_BUTTON2DOWN, 
 2187         MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 
 2188         MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
 2189         MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
 2190     };
 2191     static int butmap_versapad[8] = {
 2192         0, 
 2193         MOUSE_BUTTON3DOWN, 
 2194         0, 
 2195         MOUSE_BUTTON3DOWN, 
 2196         MOUSE_BUTTON1DOWN, 
 2197         MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
 2198         MOUSE_BUTTON1DOWN,
 2199         MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
 2200     };
 2201     static int touchpad_buttons;
 2202     static int guest_buttons;
 2203     register struct psm_softc *sc = arg;
 2204     mousestatus_t ms;
 2205     int w, x, y, z;
 2206     int c;
 2207     int l;
 2208     int x0, y0;
 2209     int s;
 2210     packetbuf_t *pb;
 2211 
 2212     getmicrouptime(&sc->lastsoftintr);
 2213 
 2214     s = spltty();
 2215 
 2216     do {
 2217         
 2218         pb = &sc->pqueue[sc->pqueue_start];
 2219         c = pb->ipacket[0];
 2220         /* 
 2221          * A kludge for Kensington device! 
 2222          * The MSB of the horizontal count appears to be stored in 
 2223          * a strange place.
 2224          */
 2225         if (sc->hw.model == MOUSE_MODEL_THINK)
 2226             pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
 2227 
 2228         /* ignore the overflow bits... */
 2229         x = (c & MOUSE_PS2_XNEG) ?  pb->ipacket[1] - 256 : pb->ipacket[1];
 2230         y = (c & MOUSE_PS2_YNEG) ?  pb->ipacket[2] - 256 : pb->ipacket[2];
 2231         z = 0;
 2232         ms.obutton = sc->button;                  /* previous button state */
 2233         ms.button = butmap[c & MOUSE_PS2_BUTTONS];
 2234         /* `tapping' action */
 2235         if (sc->config & PSM_CONFIG_FORCETAP)
 2236             ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
 2237 
 2238         switch (sc->hw.model) {
 2239 
 2240         case MOUSE_MODEL_EXPLORER:
 2241             /*
 2242              *          b7 b6 b5 b4 b3 b2 b1 b0
 2243              * byte 1:  oy ox sy sx 1  M  R  L
 2244              * byte 2:  x  x  x  x  x  x  x  x
 2245              * byte 3:  y  y  y  y  y  y  y  y
 2246              * byte 4:  *  *  S2 S1 s  d2 d1 d0
 2247              *
 2248              * L, M, R, S1, S2: left, middle, right and side buttons
 2249              * s: wheel data sign bit
 2250              * d2-d0: wheel data
 2251              */
 2252             z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG)
 2253                 ? (pb->ipacket[3] & 0x0f) - 16 : (pb->ipacket[3] & 0x0f);
 2254             ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
 2255                 ? MOUSE_BUTTON4DOWN : 0;
 2256             ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
 2257                 ? MOUSE_BUTTON5DOWN : 0;
 2258             break;
 2259 
 2260         case MOUSE_MODEL_INTELLI:
 2261         case MOUSE_MODEL_NET:
 2262             /* wheel data is in the fourth byte */
 2263             z = (char)pb->ipacket[3];
 2264             /* some mice may send 7 when there is no Z movement?! XXX */
 2265             if ((z >= 7) || (z <= -7))
 2266                 z = 0;
 2267             /* some compatible mice have additional buttons */
 2268             ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
 2269                 ? MOUSE_BUTTON4DOWN : 0;
 2270             ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
 2271                 ? MOUSE_BUTTON5DOWN : 0;
 2272             break;
 2273 
 2274         case MOUSE_MODEL_MOUSEMANPLUS:
 2275             /*
 2276              * PS2++ protocl packet
 2277              *
 2278              *          b7 b6 b5 b4 b3 b2 b1 b0
 2279              * byte 1:  *  1  p3 p2 1  *  *  *
 2280              * byte 2:  c1 c2 p1 p0 d1 d0 1  0
 2281              *
 2282              * p3-p0: packet type
 2283              * c1, c2: c1 & c2 == 1, if p2 == 0
 2284              *         c1 & c2 == 0, if p2 == 1
 2285              *
 2286              * packet type: 0 (device type)
 2287              * See comments in enable_mmanplus() below.
 2288              * 
 2289              * packet type: 1 (wheel data)
 2290              *
 2291              *          b7 b6 b5 b4 b3 b2 b1 b0
 2292              * byte 3:  h  *  B5 B4 s  d2 d1 d0
 2293              *
 2294              * h: 1, if horizontal roller data
 2295              *    0, if vertical roller data
 2296              * B4, B5: button 4 and 5
 2297              * s: sign bit
 2298              * d2-d0: roller data
 2299              *
 2300              * packet type: 2 (reserved)
 2301              */
 2302             if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
 2303                     && (abs(x) > 191)
 2304                     && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
 2305                 /* the extended data packet encodes button and wheel events */
 2306                 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
 2307                 case 1:
 2308                     /* wheel data packet */
 2309                     x = y = 0;
 2310                     if (pb->ipacket[2] & 0x80) {
 2311                         /* horizontal roller count - ignore it XXX*/
 2312                     } else {
 2313                         /* vertical roller count */
 2314                         z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
 2315                             ? (pb->ipacket[2] & 0x0f) - 16
 2316                             : (pb->ipacket[2] & 0x0f);
 2317                     }
 2318                     ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
 2319                         ? MOUSE_BUTTON4DOWN : 0;
 2320                     ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
 2321                         ? MOUSE_BUTTON5DOWN : 0;
 2322                     break;
 2323                 case 2:
 2324                     /* this packet type is reserved by Logitech... */
 2325                     /*
 2326                      * IBM ScrollPoint Mouse uses this packet type to
 2327                      * encode both vertical and horizontal scroll movement.
 2328                      */
 2329                     x = y = 0;
 2330                     /* horizontal count */
 2331                     if (pb->ipacket[2] & 0x0f)
 2332                         z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
 2333                     /* vertical count */
 2334                     if (pb->ipacket[2] & 0xf0)
 2335                         z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
 2336 #if 0
 2337                     /* vertical count */
 2338                     z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG)
 2339                         ? ((pb->ipacket[2] >> 4) & 0x0f) - 16
 2340                         : ((pb->ipacket[2] >> 4) & 0x0f);
 2341                     /* horizontal count */
 2342                     w = (pb->ipacket[2] & MOUSE_SPOINT_WNEG)
 2343                         ? (pb->ipacket[2] & 0x0f) - 16
 2344                         : (pb->ipacket[2] & 0x0f);
 2345 #endif
 2346                     break;
 2347                 case 0:
 2348                     /* device type packet - shouldn't happen */
 2349                     /* FALLTHROUGH */
 2350                 default:
 2351                     x = y = 0;
 2352                     ms.button = ms.obutton;
 2353                     VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet type %d:"
 2354                          " 0x%02x 0x%02x 0x%02x\n",
 2355                          MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
 2356                          pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
 2357                     break;
 2358                 }
 2359             } else {
 2360                 /* preserve button states */
 2361                 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
 2362             }
 2363             break;
 2364 
 2365         case MOUSE_MODEL_GLIDEPOINT:
 2366             /* `tapping' action */
 2367             ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
 2368             break;
 2369 
 2370         case MOUSE_MODEL_NETSCROLL:
 2371             /* three addtional bytes encode buttons and wheel events */
 2372             ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
 2373                 ? MOUSE_BUTTON4DOWN : 0;
 2374             ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
 2375                 ? MOUSE_BUTTON5DOWN : 0;
 2376             z = (pb->ipacket[3] & MOUSE_PS2_XNEG) 
 2377                 ? pb->ipacket[4] - 256 : pb->ipacket[4];
 2378             break;
 2379 
 2380         case MOUSE_MODEL_THINK:
 2381             /* the fourth button state in the first byte */
 2382             ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
 2383             break;
 2384 
 2385         case MOUSE_MODEL_VERSAPAD:
 2386             /* VersaPad PS/2 absolute mode message format
 2387              *
 2388              * [packet1]     7   6   5   4   3   2   1   0(LSB)
 2389              *  ipacket[0]:  1   1   0   A   1   L   T   R
 2390              *  ipacket[1]: H7  H6  H5  H4  H3  H2  H1  H0
 2391              *  ipacket[2]: V7  V6  V5  V4  V3  V2  V1  V0
 2392              *  ipacket[3]:  1   1   1   A   1   L   T   R
 2393              *  ipacket[4]:V11 V10  V9  V8 H11 H10  H9  H8
 2394              *  ipacket[5]:  0  P6  P5  P4  P3  P2  P1  P0
 2395              *
 2396              * [note]
 2397              *  R: right physical mouse button (1=on)
 2398              *  T: touch pad virtual button (1=tapping)
 2399              *  L: left physical mouse button (1=on)
 2400              *  A: position data is valid (1=valid)
 2401              *  H: horizontal data (12bit signed integer. H11 is sign bit.)
 2402              *  V: vertical data (12bit signed integer. V11 is sign bit.)
 2403              *  P: pressure data
 2404              *
 2405              * Tapping is mapped to MOUSE_BUTTON4.
 2406              */
 2407             ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
 2408             ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
 2409             x = y = 0;
 2410             if (c & MOUSE_PS2VERSA_IN_USE) {
 2411                 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
 2412                 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
 2413                 if (x0 & 0x800)
 2414                     x0 -= 0x1000;
 2415                 if (y0 & 0x800)
 2416                     y0 -= 0x1000;
 2417                 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
 2418                     x = sc->xold - x0;
 2419                     y = y0 - sc->yold;
 2420                     if (x < 0)  /* XXX */
 2421                         x++;
 2422                     else if (x)
 2423                         x--;
 2424                     if (y < 0)
 2425                         y++;
 2426                     else if (y)
 2427                         y--;
 2428                 } else {
 2429                     sc->flags |= PSM_FLAGS_FINGERDOWN;
 2430                 }
 2431                 sc->xold = x0;
 2432                 sc->yold = y0;
 2433             } else {
 2434                 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
 2435             }
 2436             c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
 2437                 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
 2438             break;
 2439 
 2440         case MOUSE_MODEL_4D:
 2441             /*
 2442              *          b7 b6 b5 b4 b3 b2 b1 b0
 2443              * byte 1:  s2 d2 s1 d1 1  M  R  L
 2444              * byte 2:  sx x  x  x  x  x  x  x
 2445              * byte 3:  sy y  y  y  y  y  y  y
 2446              *
 2447              * s1: wheel 1 direction
 2448              * d1: wheel 1 data
 2449              * s2: wheel 2 direction
 2450              * d2: wheel 2 data
 2451              */
 2452             x = (pb->ipacket[1] & 0x80) ? pb->ipacket[1] - 256 : pb->ipacket[1];
 2453             y = (pb->ipacket[2] & 0x80) ? pb->ipacket[2] - 256 : pb->ipacket[2];
 2454             switch (c & MOUSE_4D_WHEELBITS) {
 2455             case 0x10:
 2456                 z = 1;
 2457                 break;
 2458             case 0x30:
 2459                 z = -1;
 2460                 break;
 2461             case 0x40:  /* 2nd wheel turning right XXX */
 2462                 z = 2;
 2463                 break;
 2464             case 0xc0:  /* 2nd wheel turning left XXX */
 2465                 z = -2;
 2466                 break;
 2467             }
 2468             break;
 2469 
 2470         case MOUSE_MODEL_4DPLUS:
 2471             if ((x < 16 - 256) && (y < 16 - 256)) {
 2472                 /*
 2473                  *          b7 b6 b5 b4 b3 b2 b1 b0
 2474                  * byte 1:  0  0  1  1  1  M  R  L
 2475                  * byte 2:  0  0  0  0  1  0  0  0
 2476                  * byte 3:  0  0  0  0  S  s  d1 d0
 2477                  *
 2478                  * L, M, R, S: left, middle, right and side buttons
 2479                  * s: wheel data sign bit
 2480                  * d1-d0: wheel data
 2481                  */
 2482                 x = y = 0;
 2483                 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
 2484                     ms.button |= MOUSE_BUTTON4DOWN;
 2485                 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG)
 2486                         ? ((pb->ipacket[2] & 0x07) - 8)
 2487                         : (pb->ipacket[2] & 0x07) ;
 2488             } else {
 2489                 /* preserve previous button states */
 2490                 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
 2491             }
 2492             break;
 2493 
 2494         case MOUSE_MODEL_SYNAPTICS:
 2495             /* TouchPad PS/2 absolute mode message format
 2496              *
 2497              *  Bits:        7   6   5   4   3   2   1   0 (LSB)
 2498              *  ------------------------------------------------
 2499              *  ipacket[0]:  1   0  W3  W2   0  W1   R   L
 2500              *  ipacket[1]: Yb  Ya  Y9  Y8  Xb  Xa  X9  X8
 2501              *  ipacket[2]: Z7  Z6  Z5  Z4  Z3  Z2  Z1  Z0
 2502              *  ipacket[3]:  1   1  Yc  Xc   0  W0   D   U
 2503              *  ipacket[4]: X7  X6  X5  X4  X3  X2  X1  X0
 2504              *  ipacket[5]: Y7  Y6  Y5  Y4  Y3  Y2  Y1  Y0
 2505              *
 2506              * Legend:
 2507              *  L: left physical mouse button
 2508              *  R: right physical mouse button
 2509              *  D: down button
 2510              *  U: up button
 2511              *  W: "wrist" value
 2512              *  X: x position
 2513              *  Y: x position
 2514              *  Z: pressure
 2515              *
 2516              * Absolute reportable limits:    0 - 6143.
 2517              * Typical bezel limits:       1472 - 5472.
 2518              * Typical edge marings:       1632 - 5312.
 2519              *
 2520              * w = 3 Passthrough Packet
 2521              *
 2522              * Byte 2,5,6 == Byte 1,2,3 of "Guest"
 2523              */
 2524 
 2525             if (!synaptics_support)
 2526                 break;
 2527 
 2528             /* Sanity check for out of sync packets. */
 2529             if ((pb->ipacket[0] & 0xc8) != 0x80 ||
 2530                 (pb->ipacket[3] & 0xc8) != 0xc0)
 2531                 continue;
 2532 
 2533             x = y = x0 = y0 = 0;
 2534 
 2535             /* Pressure value. */
 2536             z = pb->ipacket[2];
 2537 
 2538             /* Finger width value */
 2539             if (sc->synhw.capExtended) {
 2540                 w = ((pb->ipacket[0] & 0x30) >> 2) |
 2541                     ((pb->ipacket[0] & 0x04) >> 1) |
 2542                     ((pb->ipacket[3] & 0x04) >> 2);
 2543             } else {
 2544                 /* Assume a finger of regular width */
 2545                 w = 4;
 2546             }
 2547 
 2548             /* Handle packets from the guest device */
 2549             if (w == 3 && sc->synhw.capPassthrough) {
 2550                 x = ((pb->ipacket[1] & 0x10) ?
 2551                     pb->ipacket[4] - 256 : pb->ipacket[4]);
 2552                 y = ((pb->ipacket[1] & 0x20) ?
 2553                     pb->ipacket[5] - 256 : pb->ipacket[5]);
 2554                 z = 0;
 2555 
 2556                 guest_buttons = 0;
 2557                 if (pb->ipacket[1] & 0x01)
 2558                     guest_buttons |= MOUSE_BUTTON1DOWN;
 2559                 if (pb->ipacket[1] & 0x04)
 2560                     guest_buttons |= MOUSE_BUTTON2DOWN;
 2561                 if (pb->ipacket[1] & 0x02)
 2562                     guest_buttons |= MOUSE_BUTTON3DOWN;
 2563 
 2564                 ms.button = touchpad_buttons | guest_buttons;
 2565                 break;
 2566             }
 2567 
 2568             /* Button presses */
 2569             touchpad_buttons = 0;
 2570             if (pb->ipacket[0] & 0x01)
 2571                   touchpad_buttons |= MOUSE_BUTTON1DOWN;
 2572             if (pb->ipacket[0] & 0x02)
 2573                   touchpad_buttons |= MOUSE_BUTTON3DOWN;
 2574 
 2575             if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
 2576                 if ((pb->ipacket[3] & 0x01) && (pb->ipacket[0] & 0x01) == 0)
 2577                     touchpad_buttons |= MOUSE_BUTTON4DOWN;
 2578                 if ((pb->ipacket[3] & 0x02) && (pb->ipacket[0] & 0x02) == 0)
 2579                     touchpad_buttons |= MOUSE_BUTTON5DOWN;
 2580             }
 2581 
 2582             ms.button = touchpad_buttons | guest_buttons;
 2583                 
 2584             /* There is a finger on the pad. */
 2585             if ((w >= 4 && w <= 7) && (z >= 16 && z < 200)) {
 2586                 x0 = ((pb->ipacket[3] & 0x10) << 8) |
 2587                     ((pb->ipacket[1] & 0x0f) << 8) |
 2588                     pb->ipacket[4];
 2589                 y0 = ((pb->ipacket[3] & 0x20) << 7) |
 2590                     ((pb->ipacket[1] & 0xf0) << 4) |
 2591                     pb->ipacket[5];
 2592 
 2593                 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
 2594                     x0 = (x0 + sc->xold * 3) / 4;
 2595                     y0 = (y0 + sc->yold * 3) / 4;
 2596 
 2597                     x = (x0 - sc->xold) * 10 / 85;
 2598                     y = (y0 - sc->yold) * 10 / 85;
 2599                 } else {
 2600                     sc->flags |= PSM_FLAGS_FINGERDOWN;
 2601                 }
 2602 
 2603                 sc->xold = x0;
 2604                 sc->yold = y0;
 2605                 sc->zmax = imax(z, sc->zmax);
 2606             } else {
 2607                 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
 2608 
 2609                 if (sc->zmax > PSM_TAP_THRESHOLD &&
 2610                     timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) {
 2611                         if (w == 0)
 2612                             ms.button |= MOUSE_BUTTON3DOWN;
 2613                         else if (w == 1)
 2614                             ms.button |= MOUSE_BUTTON2DOWN;
 2615                         else
 2616                             ms.button |= MOUSE_BUTTON1DOWN;
 2617                 }
 2618 
 2619                 sc->zmax = 0;
 2620                 sc->taptimeout.tv_sec = PSM_TAP_TIMEOUT / 1000000;
 2621                 sc->taptimeout.tv_usec = PSM_TAP_TIMEOUT % 1000000;
 2622                 timevaladd(&sc->taptimeout, &sc->lastsoftintr);
 2623             }
 2624 
 2625             /* Use the extra buttons as a scrollwheel */
 2626             if (ms.button & MOUSE_BUTTON4DOWN)
 2627                 z = -1;
 2628             else if (ms.button & MOUSE_BUTTON5DOWN)
 2629                 z = 1;
 2630             else
 2631                 z = 0;
 2632 
 2633             break;
 2634 
 2635         case MOUSE_MODEL_GENERIC:
 2636         default:
 2637             break;
 2638         }
 2639 
 2640         /* scale values */
 2641         if (sc->mode.accelfactor >= 1) {
 2642             if (x != 0) {
 2643                 x = x * x / sc->mode.accelfactor;
 2644                 if (x == 0)
 2645                     x = 1;
 2646                 if (c & MOUSE_PS2_XNEG)
 2647                     x = -x;
 2648             }
 2649             if (y != 0) {
 2650                 y = y * y / sc->mode.accelfactor;
 2651                 if (y == 0)
 2652                     y = 1;
 2653                 if (c & MOUSE_PS2_YNEG)
 2654                     y = -y;
 2655             }
 2656         }
 2657 
 2658         ms.dx = x;
 2659         ms.dy = y;
 2660         ms.dz = z;
 2661         ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) 
 2662             | (ms.obutton ^ ms.button);
 2663 
 2664         if (sc->mode.level < PSM_LEVEL_NATIVE)
 2665             pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
 2666 
 2667         sc->status.flags |= ms.flags;
 2668         sc->status.dx += ms.dx;
 2669         sc->status.dy += ms.dy;
 2670         sc->status.dz += ms.dz;
 2671         sc->status.button = ms.button;
 2672         sc->button = ms.button;
 2673 
 2674         sc->watchdog = FALSE;
 2675 
 2676         /* queue data */
 2677         if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
 2678             l = imin(pb->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
 2679             bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
 2680             if (pb->inputbytes > l)
 2681                 bcopy(&pb->ipacket[l], &sc->queue.buf[0], pb->inputbytes - l);
 2682             sc->queue.tail = 
 2683                 (sc->queue.tail + pb->inputbytes) % sizeof(sc->queue.buf);
 2684             sc->queue.count += pb->inputbytes;
 2685         }
 2686         pb->inputbytes = 0;
 2687 
 2688         if (++sc->pqueue_start >= PSM_PACKETQUEUE)
 2689                 sc->pqueue_start = 0;
 2690     } while (sc->pqueue_start != sc->pqueue_end);
 2691     if (sc->state & PSM_ASLP) {
 2692         sc->state &= ~PSM_ASLP;
 2693         wakeup( sc);
 2694     }
 2695     selwakeuppri(&sc->rsel, PZERO);
 2696     sc->state &= ~PSM_SOFTARMED;
 2697     splx(s);
 2698 }
 2699 
 2700 static int
 2701 psmpoll(struct cdev *dev, int events, struct thread *td)
 2702 {
 2703     struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
 2704     int s;
 2705     int revents = 0;
 2706 
 2707     /* Return true if a mouse event available */
 2708     s = spltty();
 2709     if (events & (POLLIN | POLLRDNORM)) {
 2710         if (sc->queue.count > 0)
 2711             revents |= events & (POLLIN | POLLRDNORM);
 2712         else
 2713             selrecord(td, &sc->rsel);
 2714     }
 2715     splx(s);
 2716 
 2717     return (revents);
 2718 }
 2719 
 2720 /* vendor/model specific routines */
 2721 
 2722 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
 2723 {
 2724     if (set_mouse_resolution(kbdc, res) != res)
 2725         return FALSE;
 2726     if (set_mouse_scaling(kbdc, scale)
 2727         && set_mouse_scaling(kbdc, scale)
 2728         && set_mouse_scaling(kbdc, scale) 
 2729         && (get_mouse_status(kbdc, status, 0, 3) >= 3)) 
 2730         return TRUE;
 2731     return FALSE;
 2732 }
 2733 
 2734 static int 
 2735 mouse_ext_command(KBDC kbdc, int command)
 2736 {
 2737     int c;
 2738 
 2739     c = (command >> 6) & 0x03;
 2740     if (set_mouse_resolution(kbdc, c) != c)
 2741         return FALSE;
 2742     c = (command >> 4) & 0x03;
 2743     if (set_mouse_resolution(kbdc, c) != c)
 2744         return FALSE;
 2745     c = (command >> 2) & 0x03;
 2746     if (set_mouse_resolution(kbdc, c) != c)
 2747         return FALSE;
 2748     c = (command >> 0) & 0x03;
 2749     if (set_mouse_resolution(kbdc, c) != c)
 2750         return FALSE;
 2751     return TRUE;
 2752 }
 2753 
 2754 #if notyet
 2755 /* Logitech MouseMan Cordless II */
 2756 static int
 2757 enable_lcordless(struct psm_softc *sc)
 2758 {
 2759     int status[3];
 2760     int ch;
 2761 
 2762     if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
 2763         return FALSE;
 2764     if (status[1] == PSMD_RES_HIGH)
 2765         return FALSE;
 2766     ch = (status[0] & 0x07) - 1;        /* channel # */
 2767     if ((ch <= 0) || (ch > 4))
 2768         return FALSE;
 2769     /* 
 2770      * status[1]: always one?
 2771      * status[2]: battery status? (0-100)
 2772      */
 2773     return TRUE;
 2774 }
 2775 #endif /* notyet */
 2776 
 2777 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
 2778 static int
 2779 enable_groller(struct psm_softc *sc)
 2780 {
 2781     int status[3];
 2782 
 2783     /*
 2784      * The special sequence to enable the fourth button and the
 2785      * roller. Immediately after this sequence check status bytes.
 2786      * if the mouse is NetScroll, the second and the third bytes are 
 2787      * '3' and 'D'.
 2788      */
 2789 
 2790     /*
 2791      * If the mouse is an ordinary PS/2 mouse, the status bytes should
 2792      * look like the following.
 2793      * 
 2794      * byte 1 bit 7 always 0
 2795      *        bit 6 stream mode (0)
 2796      *        bit 5 disabled (0)
 2797      *        bit 4 1:1 scaling (0)
 2798      *        bit 3 always 0
 2799      *        bit 0-2 button status
 2800      * byte 2 resolution (PSMD_RES_HIGH)
 2801      * byte 3 report rate (?)
 2802      */
 2803 
 2804     if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
 2805         return FALSE;
 2806     if ((status[1] != '3') || (status[2] != 'D'))
 2807         return FALSE;
 2808     /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
 2809     sc->hw.buttons = 4;
 2810     return TRUE;
 2811 }
 2812 
 2813 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
 2814 static int
 2815 enable_gmouse(struct psm_softc *sc)
 2816 {
 2817     int status[3];
 2818 
 2819     /*
 2820      * The special sequence to enable the middle, "rubber" button. 
 2821      * Immediately after this sequence check status bytes.
 2822      * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse, 
 2823      * the second and the third bytes are '3' and 'U'.
 2824      * NOTE: NetMouse reports that it has three buttons although it has
 2825      * two buttons and a rubber button. NetMouse Pro and MIE Mouse
 2826      * say they have three buttons too and they do have a button on the
 2827      * side...
 2828      */
 2829     if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
 2830         return FALSE;
 2831     if ((status[1] != '3') || (status[2] != 'U'))
 2832         return FALSE;
 2833     return TRUE;
 2834 }
 2835 
 2836 /* ALPS GlidePoint */
 2837 static int
 2838 enable_aglide(struct psm_softc *sc)
 2839 {
 2840     int status[3];
 2841 
 2842     /*
 2843      * The special sequence to obtain ALPS GlidePoint specific
 2844      * information. Immediately after this sequence, status bytes will 
 2845      * contain something interesting.
 2846      * NOTE: ALPS produces several models of GlidePoint. Some of those
 2847      * do not respond to this sequence, thus, cannot be detected this way.
 2848      */
 2849     if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
 2850         return FALSE;
 2851     if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
 2852         return FALSE;
 2853     if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
 2854         return FALSE;
 2855     return TRUE;
 2856 }
 2857 
 2858 /* Kensington ThinkingMouse/Trackball */
 2859 static int
 2860 enable_kmouse(struct psm_softc *sc)
 2861 {
 2862     static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
 2863     KBDC kbdc = sc->kbdc;
 2864     int status[3];
 2865     int id1;
 2866     int id2;
 2867     int i;
 2868 
 2869     id1 = get_aux_id(kbdc);
 2870     if (set_mouse_sampling_rate(kbdc, 10) != 10)
 2871         return FALSE;
 2872     /* 
 2873      * The device is now in the native mode? It returns a different
 2874      * ID value...
 2875      */
 2876     id2 = get_aux_id(kbdc);
 2877     if ((id1 == id2) || (id2 != 2))
 2878         return FALSE;
 2879 
 2880     if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
 2881         return FALSE;
 2882 #if PSM_DEBUG >= 2
 2883     /* at this point, resolution is LOW, sampling rate is 10/sec */
 2884     if (get_mouse_status(kbdc, status, 0, 3) < 3)
 2885         return FALSE;
 2886 #endif
 2887 
 2888     /*
 2889      * The special sequence to enable the third and fourth buttons.
 2890      * Otherwise they behave like the first and second buttons.
 2891      */
 2892     for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
 2893         if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
 2894             return FALSE;
 2895     }
 2896 
 2897     /* 
 2898      * At this point, the device is using default resolution and
 2899      * sampling rate for the native mode. 
 2900      */
 2901     if (get_mouse_status(kbdc, status, 0, 3) < 3)
 2902         return FALSE;
 2903     if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
 2904         return FALSE;
 2905 
 2906     /* the device appears be enabled by this sequence, diable it for now */
 2907     disable_aux_dev(kbdc);
 2908     empty_aux_buffer(kbdc, 5);
 2909 
 2910     return TRUE;
 2911 }
 2912 
 2913 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
 2914 static int
 2915 enable_mmanplus(struct psm_softc *sc)
 2916 {
 2917     KBDC kbdc = sc->kbdc;
 2918     int data[3];
 2919 
 2920     /* the special sequence to enable the fourth button and the roller. */
 2921     /*
 2922      * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
 2923      * must be called exactly three times since the last RESET command
 2924      * before this sequence. XXX
 2925      */
 2926     if (!set_mouse_scaling(kbdc, 1))
 2927         return FALSE;
 2928     if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
 2929         return FALSE;
 2930     if (get_mouse_status(kbdc, data, 1, 3) < 3)
 2931         return FALSE;
 2932 
 2933     /*
 2934      * PS2++ protocl, packet type 0
 2935      *
 2936      *          b7 b6 b5 b4 b3 b2 b1 b0
 2937      * byte 1:  *  1  p3 p2 1  *  *  *
 2938      * byte 2:  1  1  p1 p0 m1 m0 1  0
 2939      * byte 3:  m7 m6 m5 m4 m3 m2 m1 m0
 2940      *
 2941      * p3-p0: packet type: 0
 2942      * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
 2943      */
 2944     /* check constant bits */
 2945     if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
 2946         return FALSE;
 2947     if ((data[1] & 0xc3) != 0xc2)
 2948         return FALSE;
 2949     /* check d3-d0 in byte 2 */
 2950     if (!MOUSE_PS2PLUS_CHECKBITS(data))
 2951         return FALSE;
 2952     /* check p3-p0 */
 2953     if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
 2954         return FALSE;
 2955 
 2956     sc->hw.hwid &= 0x00ff;
 2957     sc->hw.hwid |= data[2] << 8;        /* save model ID */
 2958 
 2959     /*
 2960      * MouseMan+ (or FirstMouse+) is now in its native mode, in which
 2961      * the wheel and the fourth button events are encoded in the
 2962      * special data packet. The mouse may be put in the IntelliMouse mode
 2963      * if it is initialized by the IntelliMouse's method.
 2964      */
 2965     return TRUE;
 2966 }
 2967 
 2968 /* MS IntelliMouse Explorer */
 2969 static int
 2970 enable_msexplorer(struct psm_softc *sc)
 2971 {
 2972     static unsigned char rate0[] = { 200, 100, 80, };
 2973     static unsigned char rate1[] = { 200, 200, 80, };
 2974     KBDC kbdc = sc->kbdc;
 2975     int id;
 2976     int i;
 2977 
 2978     /* the special sequence to enable the extra buttons and the roller. */
 2979     for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
 2980         if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
 2981             return FALSE;
 2982     }
 2983     /* the device will give the genuine ID only after the above sequence */
 2984     id = get_aux_id(kbdc);
 2985     if (id != PSM_EXPLORER_ID)
 2986         return FALSE;
 2987 
 2988     sc->hw.hwid = id;
 2989     sc->hw.buttons = 5;         /* IntelliMouse Explorer XXX */
 2990 
 2991     /*
 2992      * XXX: this is a kludge to fool some KVM switch products
 2993      * which think they are clever enough to know the 4-byte IntelliMouse
 2994      * protocol, and assume any other protocols use 3-byte packets.
 2995      * They don't convey 4-byte data packets from the IntelliMouse Explorer 
 2996      * correctly to the host computer because of this!
 2997      * The following sequence is actually IntelliMouse's "wake up"
 2998      * sequence; it will make the KVM think the mouse is IntelliMouse
 2999      * when it is in fact IntelliMouse Explorer.
 3000      */
 3001     for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
 3002         if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
 3003             break;
 3004     }
 3005     id = get_aux_id(kbdc);
 3006 
 3007     return TRUE;
 3008 }
 3009 
 3010 /* MS IntelliMouse */
 3011 static int
 3012 enable_msintelli(struct psm_softc *sc)
 3013 {
 3014     /*
 3015      * Logitech MouseMan+ and FirstMouse+ will also respond to this
 3016      * probe routine and act like IntelliMouse.
 3017      */
 3018 
 3019     static unsigned char rate[] = { 200, 100, 80, };
 3020     KBDC kbdc = sc->kbdc;
 3021     int id;
 3022     int i;
 3023 
 3024     /* the special sequence to enable the third button and the roller. */
 3025     for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
 3026         if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
 3027             return FALSE;
 3028     }
 3029     /* the device will give the genuine ID only after the above sequence */
 3030     id = get_aux_id(kbdc);
 3031     if (id != PSM_INTELLI_ID)
 3032         return FALSE;
 3033 
 3034     sc->hw.hwid = id;
 3035     sc->hw.buttons = 3;
 3036 
 3037     return TRUE;
 3038 }
 3039 
 3040 /* A4 Tech 4D Mouse */
 3041 static int
 3042 enable_4dmouse(struct psm_softc *sc)
 3043 {
 3044     /*
 3045      * Newer wheel mice from A4 Tech may use the 4D+ protocol.
 3046      */
 3047 
 3048     static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
 3049     KBDC kbdc = sc->kbdc;
 3050     int id;
 3051     int i;
 3052 
 3053     for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
 3054         if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
 3055             return FALSE;
 3056     }
 3057     id = get_aux_id(kbdc);
 3058     /*
 3059      * WinEasy 4D, 4 Way Scroll 4D: 6
 3060      * Cable-Free 4D: 8 (4DPLUS)
 3061      * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
 3062      */
 3063     if (id != PSM_4DMOUSE_ID)
 3064         return FALSE;
 3065 
 3066     sc->hw.hwid = id;
 3067     sc->hw.buttons = 3;         /* XXX some 4D mice have 4? */
 3068 
 3069     return TRUE;
 3070 }
 3071 
 3072 /* A4 Tech 4D+ Mouse */
 3073 static int
 3074 enable_4dplus(struct psm_softc *sc)
 3075 {
 3076     /*
 3077      * Newer wheel mice from A4 Tech seem to use this protocol.
 3078      * Older models are recognized as either 4D Mouse or IntelliMouse.
 3079      */
 3080     KBDC kbdc = sc->kbdc;
 3081     int id;
 3082 
 3083     /*
 3084      * enable_4dmouse() already issued the following ID sequence...
 3085     static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
 3086     int i;
 3087 
 3088     for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
 3089         if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
 3090             return FALSE;
 3091     }
 3092     */
 3093 
 3094     id = get_aux_id(kbdc);
 3095     switch (id) {
 3096     case PSM_4DPLUS_ID:
 3097             sc->hw.buttons = 4;
 3098             break;
 3099     case PSM_4DPLUS_RFSW35_ID:
 3100             sc->hw.buttons = 3;
 3101             break;
 3102     default:
 3103             return FALSE;
 3104     }
 3105 
 3106     sc->hw.hwid = id;
 3107 
 3108     return TRUE;
 3109 }
 3110 
 3111 /* Synaptics Touchpad */
 3112 static int
 3113 enable_synaptics(struct psm_softc *sc)
 3114 {
 3115     int status[3];
 3116     KBDC kbdc;
 3117 
 3118     if (!synaptics_support)
 3119         return (FALSE);
 3120 
 3121     kbdc = sc->kbdc;
 3122     disable_aux_dev(kbdc);
 3123 
 3124     /* Just to be on the safe side */
 3125     set_mouse_scaling(kbdc, 1);
 3126  
 3127     /* Identify the Touchpad version */
 3128     if (mouse_ext_command(kbdc, 0) == 0)
 3129         return (FALSE);
 3130     if (get_mouse_status(kbdc, status, 0, 3) != 3)
 3131         return (FALSE);
 3132     if (status[1] != 0x47)
 3133         return (FALSE);
 3134 
 3135     sc->synhw.infoMinor = status[0];
 3136     sc->synhw.infoMajor = status[2] & 0x0f;
 3137 
 3138     if (verbose >= 2)
 3139         printf("Synaptics Touchpad v%d.%d\n",
 3140             sc->synhw.infoMajor, sc->synhw.infoMinor);
 3141 
 3142     if (sc->synhw.infoMajor < 4) {
 3143         printf("  Unsupported (pre-v4) Touchpad detected\n");
 3144         return (FALSE);
 3145     }
 3146 
 3147     /* Get the Touchpad model information */
 3148     if (mouse_ext_command(kbdc, 3) == 0)
 3149         return (FALSE);
 3150     if (get_mouse_status(kbdc, status, 0, 3) != 3)
 3151         return (FALSE);
 3152     if ((status[1] & 0x01) != 0) {
 3153         printf("  Failed to read model information\n");
 3154         return (FALSE);
 3155     }
 3156 
 3157     sc->synhw.infoRot180   = (status[0] & 0x80) >> 7;
 3158     sc->synhw.infoPortrait = (status[0] & 0x40) >> 6;
 3159     sc->synhw.infoSensor   =  status[0] & 0x3f;
 3160     sc->synhw.infoHardware = (status[1] & 0xfe) >> 1;
 3161     sc->synhw.infoNewAbs   = (status[2] & 0x80) >> 7;
 3162     sc->synhw.capPen       = (status[2] & 0x40) >> 6;
 3163     sc->synhw.infoSimplC   = (status[2] & 0x20) >> 5;
 3164     sc->synhw.infoGeometry =  status[2] & 0x0f;
 3165 
 3166     if (verbose >= 2) {
 3167         printf("  Model information:\n");
 3168         printf("   infoRot180: %d\n", sc->synhw.infoRot180);
 3169         printf("   infoPortrait: %d\n", sc->synhw.infoPortrait);
 3170         printf("   infoSensor: %d\n", sc->synhw.infoSensor);
 3171         printf("   infoHardware: %d\n", sc->synhw.infoHardware);
 3172         printf("   infoNewAbs: %d\n", sc->synhw.infoNewAbs);
 3173         printf("   capPen: %d\n", sc->synhw.capPen);
 3174         printf("   infoSimplC: %d\n", sc->synhw.infoSimplC);
 3175         printf("   infoGeometry: %d\n", sc->synhw.infoGeometry);
 3176     }
 3177 
 3178     /* Read the extended capability bits */
 3179     if (mouse_ext_command(kbdc, 2) == 0)
 3180         return (FALSE);
 3181     if (get_mouse_status(kbdc, status, 0, 3) != 3)
 3182         return (FALSE);
 3183     if (status[1] != 0x47) {
 3184         printf("  Failed to read extended capability bits\n");
 3185         return (FALSE);
 3186     }
 3187 
 3188     /* Set the different capabilities when they exist */
 3189     if ((status[0] & 0x80) >> 7) {
 3190         sc->synhw.capExtended    = (status[0] & 0x80) >> 7;
 3191         sc->synhw.capPassthrough = (status[2] & 0x80) >> 7;
 3192         sc->synhw.capSleep       = (status[2] & 0x10) >> 4;
 3193         sc->synhw.capFourButtons = (status[2] & 0x08) >> 3;
 3194         sc->synhw.capMultiFinger = (status[2] & 0x02) >> 1;
 3195         sc->synhw.capPalmDetect  = (status[2] & 0x01);
 3196         
 3197         if (verbose >= 2) {
 3198             printf("  Extended capabilities:\n");
 3199             printf("   capExtended: %d\n", sc->synhw.capExtended);
 3200             printf("   capPassthrough: %d\n", sc->synhw.capPassthrough);
 3201             printf("   capSleep: %d\n", sc->synhw.capSleep);
 3202             printf("   capFourButtons: %d\n", sc->synhw.capFourButtons);
 3203             printf("   capMultiFinger: %d\n", sc->synhw.capMultiFinger);
 3204             printf("   capPalmDetect: %d\n", sc->synhw.capPalmDetect);
 3205         }
 3206     } else {
 3207         sc->synhw.capExtended = 0;
 3208             
 3209         if (verbose >= 2)
 3210             printf("  No extended capabilities\n");
 3211     }
 3212 
 3213     /*
 3214      * Read the mode byte
 3215      *
 3216      * XXX: Note the Synaptics documentation also defines the first
 3217      * byte of the response to this query to be a constant 0x3b, this
 3218      * does not appear to be true for Touchpads with guest devices.
 3219      */
 3220     if (mouse_ext_command(kbdc, 1) == 0)
 3221         return (FALSE);
 3222     if (get_mouse_status(kbdc, status, 0, 3) != 3)
 3223         return (FALSE);
 3224     if (status[1] != 0x47) {
 3225         printf("  Failed to read mode byte\n");
 3226         return (FALSE);
 3227     }
 3228 
 3229     /* Set the mode byte -- request wmode where available */
 3230     if (sc->synhw.capExtended)
 3231         mouse_ext_command(kbdc, 0xc1);
 3232     else
 3233         mouse_ext_command(kbdc, 0xc0);
 3234 
 3235     /* Reset the sampling rate */
 3236     set_mouse_sampling_rate(kbdc, 20);
 3237 
 3238     /* 
 3239      * Report the correct number of buttons
 3240      *
 3241      * XXX: I'm not sure this is used anywhere.
 3242      */
 3243     if (sc->synhw.capExtended && sc->synhw.capFourButtons)
 3244         sc->hw.buttons = 4;
 3245     else
 3246         sc->hw.buttons = 3;
 3247 
 3248     return (TRUE);
 3249 }
 3250 
 3251 /* Interlink electronics VersaPad */
 3252 static int
 3253 enable_versapad(struct psm_softc *sc)
 3254 {
 3255     KBDC kbdc = sc->kbdc;
 3256     int data[3];
 3257 
 3258     set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
 3259     set_mouse_sampling_rate(kbdc, 100);         /* set rate 100 */
 3260     set_mouse_scaling(kbdc, 1);                 /* set scale 1:1 */
 3261     set_mouse_scaling(kbdc, 1);                 /* set scale 1:1 */
 3262     set_mouse_scaling(kbdc, 1);                 /* set scale 1:1 */
 3263     set_mouse_scaling(kbdc, 1);                 /* set scale 1:1 */
 3264     if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
 3265         return FALSE;
 3266     if (data[2] != 0xa || data[1] != 0 )        /* rate == 0xa && res. == 0 */
 3267         return FALSE;
 3268     set_mouse_scaling(kbdc, 1);                 /* set scale 1:1 */
 3269 
 3270     sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
 3271 
 3272     return TRUE;                                /* PS/2 absolute mode */
 3273 }
 3274 
 3275 static int
 3276 psmresume(device_t dev)
 3277 {
 3278     struct psm_softc *sc = device_get_softc(dev);
 3279     int unit = device_get_unit(dev);
 3280     int err;
 3281 
 3282     VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
 3283 
 3284     if (!(sc->config & PSM_CONFIG_HOOKRESUME))
 3285         return (0);
 3286 
 3287     err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
 3288 
 3289     if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
 3290         /* 
 3291          * Release the blocked process; it must be notified that the device
 3292          * cannot be accessed anymore.
 3293          */
 3294         sc->state &= ~PSM_ASLP;
 3295         wakeup(sc);
 3296     }
 3297 
 3298     VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
 3299 
 3300     return (err);
 3301 }
 3302 
 3303 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
 3304 
 3305 /*
 3306  * This sucks up assignments from PNPBIOS and ACPI.
 3307  */
 3308 
 3309 /*
 3310  * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
 3311  * appear BEFORE the AT keyboard controller.  As the PS/2 mouse device
 3312  * can be probed and attached only after the AT keyboard controller is
 3313  * attached, we shall quietly reserve the IRQ resource for later use.
 3314  * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
 3315  * copy the IRQ resource to the PS/2 mouse device instance hanging
 3316  * under the keyboard controller, then probe and attach it.
 3317  */
 3318 
 3319 static  devclass_t                      psmcpnp_devclass;
 3320 
 3321 static  device_probe_t                  psmcpnp_probe;
 3322 static  device_attach_t                 psmcpnp_attach;
 3323 
 3324 static device_method_t psmcpnp_methods[] = {
 3325         DEVMETHOD(device_probe,         psmcpnp_probe),
 3326         DEVMETHOD(device_attach,        psmcpnp_attach),
 3327         
 3328         { 0, 0 }
 3329 };
 3330 
 3331 static driver_t psmcpnp_driver = {
 3332         PSMCPNP_DRIVER_NAME,
 3333         psmcpnp_methods,
 3334         1,                      /* no softc */
 3335 };
 3336 
 3337 static struct isa_pnp_id psmcpnp_ids[] = {
 3338         { 0x030fd041, "PS/2 mouse port" },              /* PNP0F03 */
 3339         { 0x130fd041, "PS/2 mouse port" },              /* PNP0F13 */
 3340         { 0x1303d041, "PS/2 port" },                    /* PNP0313, XXX */
 3341         { 0x02002e4f, "Dell PS/2 mouse port" },         /* Lat. X200, Dell */
 3342         { 0x80374d24, "IBM PS/2 mouse port" },          /* IBM3780, ThinkPad */
 3343         { 0x81374d24, "IBM PS/2 mouse port" },          /* IBM3781, ThinkPad */
 3344         { 0x0190d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9001, Vaio */
 3345         { 0x0290d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9002, Vaio */
 3346         { 0x0390d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9003, Vaio */
 3347         { 0x0490d94d, "SONY VAIO PS/2 mouse port"},     /* SNY9004, Vaio */
 3348         { 0 }
 3349 };
 3350 
 3351 static int
 3352 create_a_copy(device_t atkbdc, device_t me)
 3353 {
 3354         device_t psm;
 3355         u_long irq;
 3356 
 3357         /* find the PS/2 mouse device instance under the keyboard controller */
 3358         psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
 3359                                 device_get_unit(atkbdc));
 3360         if (psm == NULL)
 3361                 return ENXIO;
 3362         if (device_get_state(psm) != DS_NOTPRESENT)
 3363                 return 0;
 3364 
 3365         /* move our resource to the found device */
 3366         irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
 3367         bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
 3368 
 3369         /* ...then probe and attach it */
 3370         return device_probe_and_attach(psm);
 3371 }
 3372 
 3373 static int
 3374 psmcpnp_probe(device_t dev)
 3375 {
 3376         struct resource *res;
 3377         u_long irq;
 3378         int rid;
 3379 
 3380         if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
 3381                 return ENXIO;
 3382 
 3383         /*
 3384          * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
 3385          * to the PS/2 mouse device node. But, some buggy PnP BIOS
 3386          * declares the PS/2 mouse device node without an IRQ resource!
 3387          * If this happens, we shall refer to device hints.
 3388          * If we still don't find it there, use a hardcoded value... XXX
 3389          */
 3390         rid = 0;
 3391         irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
 3392         if (irq <= 0) {
 3393                 if (resource_long_value(PSM_DRIVER_NAME,
 3394                                         device_get_unit(dev), "irq", &irq) != 0)
 3395                         irq = 12;       /* XXX */
 3396                 device_printf(dev, "irq resource info is missing; "
 3397                               "assuming irq %ld\n", irq);
 3398                 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
 3399         }
 3400         res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
 3401                                      RF_SHAREABLE);
 3402         bus_release_resource(dev, SYS_RES_IRQ, rid, res);
 3403 
 3404         /* keep quiet */
 3405         if (!bootverbose)
 3406                 device_quiet(dev);
 3407 
 3408         return ((res == NULL) ? ENXIO : 0);
 3409 }
 3410 
 3411 static int
 3412 psmcpnp_attach(device_t dev)
 3413 {
 3414         device_t atkbdc;
 3415         int rid;
 3416 
 3417         /* find the keyboard controller, which may be on acpi* or isa* bus */
 3418         atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
 3419                                      device_get_unit(dev));
 3420         if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
 3421                 create_a_copy(atkbdc, dev);
 3422         } else {
 3423                 /*
 3424                  * If we don't have the AT keyboard controller yet,
 3425                  * just reserve the IRQ for later use...
 3426                  * (See psmidentify() above.)
 3427                  */
 3428                 rid = 0;
 3429                 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
 3430         }
 3431 
 3432         return 0;
 3433 }
 3434 
 3435 /*
 3436  * Return true if 'now' is earlier than (start + (secs.usecs)).
 3437  * Now may be NULL and the function will fetch the current time from
 3438  * getmicrouptime(), or a cached 'now' can be passed in.
 3439  * All values should be numbers derived from getmicrouptime().
 3440  */
 3441 static int
 3442 timeelapsed(start, secs, usecs, now)
 3443         const struct timeval *start, *now;
 3444         int secs, usecs;
 3445 {
 3446         struct timeval snow, tv;
 3447 
 3448         /* if there is no 'now' passed in, the get it as a convience. */
 3449         if (now == NULL) {
 3450                 getmicrouptime(&snow);
 3451                 now = &snow;
 3452         }
 3453         
 3454         tv.tv_sec = secs;
 3455         tv.tv_usec = usecs;
 3456         timevaladd(&tv, start);
 3457         return (timevalcmp(&tv, now, <));
 3458 }
 3459 
 3460 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
 3461 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);

Cache object: 4afa9ca149d77a9217fa36885bd09557


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.