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