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