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