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