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

Cache object: ec6c544b01b8c179eaeb6d92a5cdc06c


[ 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.