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