1 /*- 2 * Copyright (c) 1992, 1993 Erik Forsberg. 3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED 13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 15 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 * 23 * $FreeBSD: src/sys/isa/psm.c,v 1.23.2.7 2003/11/12 04:26:26 mikeh Exp $ 24 * $DragonFly: src/sys/dev/misc/psm/psm.c,v 1.9 2004/02/10 15:56:38 hmp 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 * Attempt to reset the controller twice -- this helps 1038 * pierce through some KVM switches. The second reset 1039 * is non-fatal. 1040 */ 1041 if (!reset_aux_dev(sc->kbdc)) { 1042 recover_from_error(sc->kbdc); 1043 restore_controller(sc->kbdc, command_byte); 1044 if (verbose) 1045 printf("psm%d: failed to reset the aux device.\n", unit); 1046 endprobe(ENXIO); 1047 } else if (!reset_aux_dev(sc->kbdc)) { 1048 recover_from_error(sc->kbdc); 1049 if (verbose >= 2) 1050 printf("psm%d: failed to reset the aux device (2).\n", 1051 unit); 1052 } 1053 } 1054 1055 /* 1056 * both the aux port and the aux device is functioning, see if the 1057 * device can be enabled. NOTE: when enabled, the device will start 1058 * sending data; we shall immediately disable the device once we know 1059 * the device can be enabled. 1060 */ 1061 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) { 1062 /* MOUSE ERROR */ 1063 recover_from_error(sc->kbdc); 1064 restore_controller(sc->kbdc, command_byte); 1065 if (verbose) 1066 printf("psm%d: failed to enable the aux device.\n", unit); 1067 endprobe(ENXIO); 1068 } 1069 1070 /* save the default values after reset */ 1071 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) { 1072 sc->dflt_mode.rate = sc->mode.rate = stat[2]; 1073 sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 1074 } else { 1075 sc->dflt_mode.rate = sc->mode.rate = -1; 1076 sc->dflt_mode.resolution = sc->mode.resolution = -1; 1077 } 1078 1079 /* hardware information */ 1080 sc->hw.iftype = MOUSE_IF_PS2; 1081 1082 /* verify the device is a mouse */ 1083 sc->hw.hwid = get_aux_id(sc->kbdc); 1084 if (!is_a_mouse(sc->hw.hwid)) { 1085 restore_controller(sc->kbdc, command_byte); 1086 if (verbose) 1087 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid); 1088 endprobe(ENXIO); 1089 } 1090 switch (sc->hw.hwid) { 1091 case PSM_BALLPOINT_ID: 1092 sc->hw.type = MOUSE_TRACKBALL; 1093 break; 1094 case PSM_MOUSE_ID: 1095 case PSM_INTELLI_ID: 1096 case PSM_EXPLORER_ID: 1097 case PSM_4DMOUSE_ID: 1098 case PSM_4DPLUS_ID: 1099 sc->hw.type = MOUSE_MOUSE; 1100 break; 1101 default: 1102 sc->hw.type = MOUSE_UNKNOWN; 1103 break; 1104 } 1105 1106 if (sc->config & PSM_CONFIG_NOIDPROBE) { 1107 sc->hw.buttons = 2; 1108 i = GENERIC_MOUSE_ENTRY; 1109 } else { 1110 /* # of buttons */ 1111 sc->hw.buttons = get_mouse_buttons(sc->kbdc); 1112 1113 /* other parameters */ 1114 for (i = 0; vendortype[i].probefunc != NULL; ++i) { 1115 if ((*vendortype[i].probefunc)(sc)) { 1116 if (verbose >= 2) 1117 printf("psm%d: found %s\n", 1118 unit, model_name(vendortype[i].model)); 1119 break; 1120 } 1121 } 1122 } 1123 1124 sc->hw.model = vendortype[i].model; 1125 1126 sc->dflt_mode.level = PSM_LEVEL_BASE; 1127 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE; 1128 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4; 1129 if (sc->config & PSM_CONFIG_NOCHECKSYNC) 1130 sc->dflt_mode.syncmask[0] = 0; 1131 else 1132 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask; 1133 if (sc->config & PSM_CONFIG_FORCETAP) 1134 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP; 1135 sc->dflt_mode.syncmask[1] = 0; /* syncbits */ 1136 sc->mode = sc->dflt_mode; 1137 sc->mode.packetsize = vendortype[i].packetsize; 1138 1139 /* set mouse parameters */ 1140 #if 0 1141 /* 1142 * A version of Logitech FirstMouse+ won't report wheel movement, 1143 * if SET_DEFAULTS is sent... Don't use this command. 1144 * This fix was found by Takashi Nishida. 1145 */ 1146 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS); 1147 if (verbose >= 2) 1148 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i); 1149 #endif 1150 if (sc->config & PSM_CONFIG_RESOLUTION) { 1151 sc->mode.resolution 1152 = set_mouse_resolution(sc->kbdc, 1153 (sc->config & PSM_CONFIG_RESOLUTION) - 1); 1154 } else if (sc->mode.resolution >= 0) { 1155 sc->mode.resolution 1156 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution); 1157 } 1158 if (sc->mode.rate > 0) { 1159 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate); 1160 } 1161 set_mouse_scaling(sc->kbdc, 1); 1162 1163 /* request a data packet and extract sync. bits */ 1164 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) { 1165 printf("psm%d: failed to get data.\n", unit); 1166 sc->mode.syncmask[0] = 0; 1167 } else { 1168 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 1169 /* the NetScroll Mouse will send three more bytes... Ignore them */ 1170 empty_aux_buffer(sc->kbdc, 5); 1171 } 1172 1173 /* just check the status of the mouse */ 1174 /* 1175 * NOTE: XXX there are some arcane controller/mouse combinations out 1176 * there, which hung the controller unless there is data transmission 1177 * after ACK from the mouse. 1178 */ 1179 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) { 1180 printf("psm%d: failed to get status.\n", unit); 1181 } else { 1182 /* 1183 * When in its native mode, some mice operate with different 1184 * default parameters than in the PS/2 compatible mode. 1185 */ 1186 sc->dflt_mode.rate = sc->mode.rate = stat[2]; 1187 sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 1188 } 1189 1190 /* disable the aux port for now... */ 1191 if (!set_controller_command_byte(sc->kbdc, 1192 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 1193 (command_byte & KBD_KBD_CONTROL_BITS) 1194 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1195 /* 1196 * this is CONTROLLER ERROR; I don't know the proper way to 1197 * recover from this error... 1198 */ 1199 restore_controller(sc->kbdc, command_byte); 1200 printf("psm%d: unable to set the command byte.\n", unit); 1201 endprobe(ENXIO); 1202 } 1203 1204 /* see if IRQ is available */ 1205 rid = 0; 1206 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1, 1207 RF_ACTIVE); 1208 if (sc->intr == NULL) { 1209 printf("psm%d: unable to allocate the IRQ resource (%d).\n", 1210 unit, irq); 1211 endprobe(ENXIO); 1212 } else { 1213 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 1214 } 1215 1216 /* done */ 1217 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS); 1218 kbdc_lock(sc->kbdc, FALSE); 1219 return (0); 1220 } 1221 1222 static int 1223 psmattach(device_t dev) 1224 { 1225 int unit = device_get_unit(dev); 1226 struct psm_softc *sc = device_get_softc(dev); 1227 uintptr_t irq; 1228 int error; 1229 int rid; 1230 1231 if (sc == NULL) /* shouldn't happen */ 1232 return (ENXIO); 1233 1234 /* Setup initial state */ 1235 sc->state = PSM_VALID; 1236 callout_handle_init(&sc->callout); 1237 1238 /* Setup our interrupt handler */ 1239 rid = 0; 1240 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq); 1241 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1, 1242 RF_ACTIVE); 1243 if (sc->intr == NULL) 1244 return (ENXIO); 1245 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr, 1246 INTR_TYPE_TTY, psmintr, sc, &sc->ih); 1247 if (error) { 1248 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 1249 return (error); 1250 } 1251 1252 /* Done */ 1253 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, 1254 "psm%d", unit); 1255 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, 1256 "bpsm%d", unit); 1257 1258 if (!verbose) { 1259 printf("psm%d: model %s, device ID %d\n", 1260 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff); 1261 } else { 1262 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n", 1263 unit, model_name(sc->hw.model), 1264 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons); 1265 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n", 1266 unit, sc->config, sc->flags, sc->mode.packetsize); 1267 printf("psm%d: syncmask:%02x, syncbits:%02x\n", 1268 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]); 1269 } 1270 1271 if (bootverbose) 1272 --verbose; 1273 1274 return (0); 1275 } 1276 1277 static int 1278 psmdetach(device_t dev) 1279 { 1280 struct psm_softc *sc; 1281 int rid; 1282 1283 sc = device_get_softc(dev); 1284 if (sc->state & PSM_OPEN) 1285 return EBUSY; 1286 1287 rid = 0; 1288 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih); 1289 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 1290 1291 destroy_dev(sc->dev); 1292 destroy_dev(sc->bdev); 1293 1294 return 0; 1295 } 1296 1297 static int 1298 psmopen(dev_t dev, int flag, int fmt, struct thread *td) 1299 { 1300 int unit = PSM_UNIT(dev); 1301 struct psm_softc *sc; 1302 int command_byte; 1303 int err; 1304 int s; 1305 1306 /* Get device data */ 1307 sc = PSM_SOFTC(unit); 1308 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) 1309 /* the device is no longer valid/functioning */ 1310 return (ENXIO); 1311 1312 /* Disallow multiple opens */ 1313 if (sc->state & PSM_OPEN) 1314 return (EBUSY); 1315 1316 device_busy(devclass_get_device(psm_devclass, unit)); 1317 1318 /* Initialize state */ 1319 sc->rsel.si_flags = 0; 1320 sc->rsel.si_pid = 0; 1321 sc->mode.level = sc->dflt_mode.level; 1322 sc->mode.protocol = sc->dflt_mode.protocol; 1323 sc->watchdog = FALSE; 1324 1325 /* flush the event queue */ 1326 sc->queue.count = 0; 1327 sc->queue.head = 0; 1328 sc->queue.tail = 0; 1329 sc->status.flags = 0; 1330 sc->status.button = 0; 1331 sc->status.obutton = 0; 1332 sc->status.dx = 0; 1333 sc->status.dy = 0; 1334 sc->status.dz = 0; 1335 sc->button = 0; 1336 1337 /* empty input buffer */ 1338 bzero(sc->ipacket, sizeof(sc->ipacket)); 1339 sc->inputbytes = 0; 1340 sc->syncerrors = 0; 1341 1342 /* don't let timeout routines in the keyboard driver to poll the kbdc */ 1343 if (!kbdc_lock(sc->kbdc, TRUE)) 1344 return (EIO); 1345 1346 /* save the current controller command byte */ 1347 s = spltty(); 1348 command_byte = get_controller_command_byte(sc->kbdc); 1349 1350 /* enable the aux port and temporalily disable the keyboard */ 1351 if ((command_byte == -1) 1352 || !set_controller_command_byte(sc->kbdc, 1353 kbdc_get_device_mask(sc->kbdc), 1354 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 1355 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1356 /* CONTROLLER ERROR; do you know how to get out of this? */ 1357 kbdc_lock(sc->kbdc, FALSE); 1358 splx(s); 1359 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n", 1360 unit); 1361 return (EIO); 1362 } 1363 /* 1364 * Now that the keyboard controller is told not to generate 1365 * the keyboard and mouse interrupts, call `splx()' to allow 1366 * the other tty interrupts. The clock interrupt may also occur, 1367 * but timeout routines will be blocked by the poll flag set 1368 * via `kbdc_lock()' 1369 */ 1370 splx(s); 1371 1372 /* enable the mouse device */ 1373 err = doopen(sc, command_byte); 1374 1375 /* done */ 1376 if (err == 0) 1377 sc->state |= PSM_OPEN; 1378 kbdc_lock(sc->kbdc, FALSE); 1379 return (err); 1380 } 1381 1382 static int 1383 psmclose(dev_t dev, int flag, int fmt, struct thread *td) 1384 { 1385 int unit = PSM_UNIT(dev); 1386 struct psm_softc *sc = PSM_SOFTC(unit); 1387 int stat[3]; 1388 int command_byte; 1389 int s; 1390 1391 /* don't let timeout routines in the keyboard driver to poll the kbdc */ 1392 if (!kbdc_lock(sc->kbdc, TRUE)) 1393 return (EIO); 1394 1395 /* save the current controller command byte */ 1396 s = spltty(); 1397 command_byte = get_controller_command_byte(sc->kbdc); 1398 if (command_byte == -1) { 1399 kbdc_lock(sc->kbdc, FALSE); 1400 splx(s); 1401 return (EIO); 1402 } 1403 1404 /* disable the aux interrupt and temporalily disable the keyboard */ 1405 if (!set_controller_command_byte(sc->kbdc, 1406 kbdc_get_device_mask(sc->kbdc), 1407 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 1408 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1409 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n", 1410 unit); 1411 /* CONTROLLER ERROR; 1412 * NOTE: we shall force our way through. Because the only 1413 * ill effect we shall see is that we may not be able 1414 * to read ACK from the mouse, and it doesn't matter much 1415 * so long as the mouse will accept the DISABLE command. 1416 */ 1417 } 1418 splx(s); 1419 1420 /* stop the watchdog timer */ 1421 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout); 1422 callout_handle_init(&sc->callout); 1423 1424 /* remove anything left in the output buffer */ 1425 empty_aux_buffer(sc->kbdc, 10); 1426 1427 /* disable the aux device, port and interrupt */ 1428 if (sc->state & PSM_VALID) { 1429 if (!disable_aux_dev(sc->kbdc)) { 1430 /* MOUSE ERROR; 1431 * NOTE: we don't return error and continue, pretending 1432 * we have successfully disabled the device. It's OK because 1433 * the interrupt routine will discard any data from the mouse 1434 * hereafter. 1435 */ 1436 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n", 1437 unit); 1438 } 1439 1440 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 1441 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n", 1442 unit); 1443 } 1444 1445 if (!set_controller_command_byte(sc->kbdc, 1446 kbdc_get_device_mask(sc->kbdc), 1447 (command_byte & KBD_KBD_CONTROL_BITS) 1448 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1449 /* CONTROLLER ERROR; 1450 * we shall ignore this error; see the above comment. 1451 */ 1452 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n", 1453 unit); 1454 } 1455 1456 /* remove anything left in the output buffer */ 1457 empty_aux_buffer(sc->kbdc, 10); 1458 1459 /* close is almost always successful */ 1460 sc->state &= ~PSM_OPEN; 1461 kbdc_lock(sc->kbdc, FALSE); 1462 device_unbusy(devclass_get_device(psm_devclass, unit)); 1463 return (0); 1464 } 1465 1466 static int 1467 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf) 1468 { 1469 static unsigned char butmapps2[8] = { 1470 0, 1471 MOUSE_PS2_BUTTON1DOWN, 1472 MOUSE_PS2_BUTTON2DOWN, 1473 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN, 1474 MOUSE_PS2_BUTTON3DOWN, 1475 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN, 1476 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 1477 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 1478 }; 1479 static unsigned char butmapmsc[8] = { 1480 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 1481 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 1482 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 1483 MOUSE_MSC_BUTTON3UP, 1484 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 1485 MOUSE_MSC_BUTTON2UP, 1486 MOUSE_MSC_BUTTON1UP, 1487 0, 1488 }; 1489 int mapped; 1490 int i; 1491 1492 if (sc->mode.level == PSM_LEVEL_BASE) { 1493 mapped = status->button & ~MOUSE_BUTTON4DOWN; 1494 if (status->button & MOUSE_BUTTON4DOWN) 1495 mapped |= MOUSE_BUTTON1DOWN; 1496 status->button = mapped; 1497 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS]; 1498 i = max(min(status->dx, 255), -256); 1499 if (i < 0) 1500 buf[0] |= MOUSE_PS2_XNEG; 1501 buf[1] = i; 1502 i = max(min(status->dy, 255), -256); 1503 if (i < 0) 1504 buf[0] |= MOUSE_PS2_YNEG; 1505 buf[2] = i; 1506 return MOUSE_PS2_PACKETSIZE; 1507 } else if (sc->mode.level == PSM_LEVEL_STANDARD) { 1508 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS]; 1509 i = max(min(status->dx, 255), -256); 1510 buf[1] = i >> 1; 1511 buf[3] = i - buf[1]; 1512 i = max(min(status->dy, 255), -256); 1513 buf[2] = i >> 1; 1514 buf[4] = i - buf[2]; 1515 i = max(min(status->dz, 127), -128); 1516 buf[5] = (i >> 1) & 0x7f; 1517 buf[6] = (i - (i >> 1)) & 0x7f; 1518 buf[7] = (~status->button >> 3) & 0x7f; 1519 return MOUSE_SYS_PACKETSIZE; 1520 } 1521 return sc->inputbytes;; 1522 } 1523 1524 static int 1525 psmread(dev_t dev, struct uio *uio, int flag) 1526 { 1527 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 1528 unsigned char buf[PSM_SMALLBUFSIZE]; 1529 int error = 0; 1530 int s; 1531 int l; 1532 1533 if ((sc->state & PSM_VALID) == 0) 1534 return EIO; 1535 1536 /* block until mouse activity occured */ 1537 s = spltty(); 1538 while (sc->queue.count <= 0) { 1539 if (PSM_NBLOCKIO(dev)) { 1540 splx(s); 1541 return EWOULDBLOCK; 1542 } 1543 sc->state |= PSM_ASLP; 1544 error = tsleep((caddr_t) sc, PCATCH, "psmrea", 0); 1545 sc->state &= ~PSM_ASLP; 1546 if (error) { 1547 splx(s); 1548 return error; 1549 } else if ((sc->state & PSM_VALID) == 0) { 1550 /* the device disappeared! */ 1551 splx(s); 1552 return EIO; 1553 } 1554 } 1555 splx(s); 1556 1557 /* copy data to the user land */ 1558 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) { 1559 s = spltty(); 1560 l = min(sc->queue.count, uio->uio_resid); 1561 if (l > sizeof(buf)) 1562 l = sizeof(buf); 1563 if (l > sizeof(sc->queue.buf) - sc->queue.head) { 1564 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], 1565 sizeof(sc->queue.buf) - sc->queue.head); 1566 bcopy(&sc->queue.buf[0], 1567 &buf[sizeof(sc->queue.buf) - sc->queue.head], 1568 l - (sizeof(sc->queue.buf) - sc->queue.head)); 1569 } else { 1570 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l); 1571 } 1572 sc->queue.count -= l; 1573 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf); 1574 splx(s); 1575 error = uiomove(buf, l, uio); 1576 if (error) 1577 break; 1578 } 1579 1580 return error; 1581 } 1582 1583 static int 1584 block_mouse_data(struct psm_softc *sc, int *c) 1585 { 1586 int s; 1587 1588 if (!kbdc_lock(sc->kbdc, TRUE)) 1589 return EIO; 1590 1591 s = spltty(); 1592 *c = get_controller_command_byte(sc->kbdc); 1593 if ((*c == -1) 1594 || !set_controller_command_byte(sc->kbdc, 1595 kbdc_get_device_mask(sc->kbdc), 1596 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 1597 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1598 /* this is CONTROLLER ERROR */ 1599 splx(s); 1600 kbdc_lock(sc->kbdc, FALSE); 1601 return EIO; 1602 } 1603 1604 /* 1605 * The device may be in the middle of status data transmission. 1606 * The transmission will be interrupted, thus, incomplete status 1607 * data must be discarded. Although the aux interrupt is disabled 1608 * at the keyboard controller level, at most one aux interrupt 1609 * may have already been pending and a data byte is in the 1610 * output buffer; throw it away. Note that the second argument 1611 * to `empty_aux_buffer()' is zero, so that the call will just 1612 * flush the internal queue. 1613 * `psmintr()' will be invoked after `splx()' if an interrupt is 1614 * pending; it will see no data and returns immediately. 1615 */ 1616 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */ 1617 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */ 1618 sc->inputbytes = 0; 1619 splx(s); 1620 1621 return 0; 1622 } 1623 1624 static int 1625 unblock_mouse_data(struct psm_softc *sc, int c) 1626 { 1627 int error = 0; 1628 1629 /* 1630 * We may have seen a part of status data during `set_mouse_XXX()'. 1631 * they have been queued; flush it. 1632 */ 1633 empty_aux_buffer(sc->kbdc, 0); 1634 1635 /* restore ports and interrupt */ 1636 if (!set_controller_command_byte(sc->kbdc, 1637 kbdc_get_device_mask(sc->kbdc), 1638 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 1639 /* CONTROLLER ERROR; this is serious, we may have 1640 * been left with the inaccessible keyboard and 1641 * the disabled mouse interrupt. 1642 */ 1643 error = EIO; 1644 } 1645 1646 kbdc_lock(sc->kbdc, FALSE); 1647 return error; 1648 } 1649 1650 static int 1651 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1652 { 1653 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 1654 mousemode_t mode; 1655 mousestatus_t status; 1656 #if (defined(MOUSE_GETVARS)) 1657 mousevar_t *var; 1658 #endif 1659 mousedata_t *data; 1660 int stat[3]; 1661 int command_byte; 1662 int error = 0; 1663 int s; 1664 1665 /* Perform IOCTL command */ 1666 switch (cmd) { 1667 1668 case OLD_MOUSE_GETHWINFO: 1669 s = spltty(); 1670 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons; 1671 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype; 1672 ((old_mousehw_t *)addr)->type = sc->hw.type; 1673 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff; 1674 splx(s); 1675 break; 1676 1677 case MOUSE_GETHWINFO: 1678 s = spltty(); 1679 *(mousehw_t *)addr = sc->hw; 1680 if (sc->mode.level == PSM_LEVEL_BASE) 1681 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC; 1682 splx(s); 1683 break; 1684 1685 case OLD_MOUSE_GETMODE: 1686 s = spltty(); 1687 switch (sc->mode.level) { 1688 case PSM_LEVEL_BASE: 1689 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 1690 break; 1691 case PSM_LEVEL_STANDARD: 1692 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 1693 break; 1694 case PSM_LEVEL_NATIVE: 1695 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 1696 break; 1697 } 1698 ((old_mousemode_t *)addr)->rate = sc->mode.rate; 1699 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution; 1700 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor; 1701 splx(s); 1702 break; 1703 1704 case MOUSE_GETMODE: 1705 s = spltty(); 1706 *(mousemode_t *)addr = sc->mode; 1707 ((mousemode_t *)addr)->resolution = 1708 MOUSE_RES_LOW - sc->mode.resolution; 1709 switch (sc->mode.level) { 1710 case PSM_LEVEL_BASE: 1711 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 1712 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE; 1713 break; 1714 case PSM_LEVEL_STANDARD: 1715 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 1716 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE; 1717 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK; 1718 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC; 1719 break; 1720 case PSM_LEVEL_NATIVE: 1721 /* FIXME: this isn't quite correct... XXX */ 1722 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 1723 break; 1724 } 1725 splx(s); 1726 break; 1727 1728 case OLD_MOUSE_SETMODE: 1729 case MOUSE_SETMODE: 1730 if (cmd == OLD_MOUSE_SETMODE) { 1731 mode.rate = ((old_mousemode_t *)addr)->rate; 1732 /* 1733 * resolution old I/F new I/F 1734 * default 0 0 1735 * low 1 -2 1736 * medium low 2 -3 1737 * medium high 3 -4 1738 * high 4 -5 1739 */ 1740 if (((old_mousemode_t *)addr)->resolution > 0) 1741 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1; 1742 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor; 1743 mode.level = -1; 1744 } else { 1745 mode = *(mousemode_t *)addr; 1746 } 1747 1748 /* adjust and validate parameters. */ 1749 if (mode.rate > UCHAR_MAX) 1750 return EINVAL; 1751 if (mode.rate == 0) 1752 mode.rate = sc->dflt_mode.rate; 1753 else if (mode.rate == -1) 1754 /* don't change the current setting */ 1755 ; 1756 else if (mode.rate < 0) 1757 return EINVAL; 1758 if (mode.resolution >= UCHAR_MAX) 1759 return EINVAL; 1760 if (mode.resolution >= 200) 1761 mode.resolution = MOUSE_RES_HIGH; 1762 else if (mode.resolution >= 100) 1763 mode.resolution = MOUSE_RES_MEDIUMHIGH; 1764 else if (mode.resolution >= 50) 1765 mode.resolution = MOUSE_RES_MEDIUMLOW; 1766 else if (mode.resolution > 0) 1767 mode.resolution = MOUSE_RES_LOW; 1768 if (mode.resolution == MOUSE_RES_DEFAULT) 1769 mode.resolution = sc->dflt_mode.resolution; 1770 else if (mode.resolution == -1) 1771 /* don't change the current setting */ 1772 ; 1773 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 1774 mode.resolution = MOUSE_RES_LOW - mode.resolution; 1775 if (mode.level == -1) 1776 /* don't change the current setting */ 1777 mode.level = sc->mode.level; 1778 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX)) 1779 return EINVAL; 1780 if (mode.accelfactor == -1) 1781 /* don't change the current setting */ 1782 mode.accelfactor = sc->mode.accelfactor; 1783 else if (mode.accelfactor < 0) 1784 return EINVAL; 1785 1786 /* don't allow anybody to poll the keyboard controller */ 1787 error = block_mouse_data(sc, &command_byte); 1788 if (error) 1789 return error; 1790 1791 /* set mouse parameters */ 1792 if (mode.rate > 0) 1793 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 1794 if (mode.resolution >= 0) 1795 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 1796 set_mouse_scaling(sc->kbdc, 1); 1797 get_mouse_status(sc->kbdc, stat, 0, 3); 1798 1799 s = spltty(); 1800 sc->mode.rate = mode.rate; 1801 sc->mode.resolution = mode.resolution; 1802 sc->mode.accelfactor = mode.accelfactor; 1803 sc->mode.level = mode.level; 1804 splx(s); 1805 1806 unblock_mouse_data(sc, command_byte); 1807 break; 1808 1809 case MOUSE_GETLEVEL: 1810 *(int *)addr = sc->mode.level; 1811 break; 1812 1813 case MOUSE_SETLEVEL: 1814 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX)) 1815 return EINVAL; 1816 sc->mode.level = *(int *)addr; 1817 break; 1818 1819 case MOUSE_GETSTATUS: 1820 s = spltty(); 1821 status = sc->status; 1822 sc->status.flags = 0; 1823 sc->status.obutton = sc->status.button; 1824 sc->status.button = 0; 1825 sc->status.dx = 0; 1826 sc->status.dy = 0; 1827 sc->status.dz = 0; 1828 splx(s); 1829 *(mousestatus_t *)addr = status; 1830 break; 1831 1832 #if (defined(MOUSE_GETVARS)) 1833 case MOUSE_GETVARS: 1834 var = (mousevar_t *)addr; 1835 bzero(var, sizeof(*var)); 1836 s = spltty(); 1837 var->var[0] = MOUSE_VARS_PS2_SIG; 1838 var->var[1] = sc->config; 1839 var->var[2] = sc->flags; 1840 splx(s); 1841 break; 1842 1843 case MOUSE_SETVARS: 1844 return ENODEV; 1845 #endif /* MOUSE_GETVARS */ 1846 1847 case MOUSE_READSTATE: 1848 case MOUSE_READDATA: 1849 data = (mousedata_t *)addr; 1850 if (data->len > sizeof(data->buf)/sizeof(data->buf[0])) 1851 return EINVAL; 1852 1853 error = block_mouse_data(sc, &command_byte); 1854 if (error) 1855 return error; 1856 if ((data->len = get_mouse_status(sc->kbdc, data->buf, 1857 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0) 1858 error = EIO; 1859 unblock_mouse_data(sc, command_byte); 1860 break; 1861 1862 #if (defined(MOUSE_SETRESOLUTION)) 1863 case MOUSE_SETRESOLUTION: 1864 mode.resolution = *(int *)addr; 1865 if (mode.resolution >= UCHAR_MAX) 1866 return EINVAL; 1867 else if (mode.resolution >= 200) 1868 mode.resolution = MOUSE_RES_HIGH; 1869 else if (mode.resolution >= 100) 1870 mode.resolution = MOUSE_RES_MEDIUMHIGH; 1871 else if (mode.resolution >= 50) 1872 mode.resolution = MOUSE_RES_MEDIUMLOW; 1873 else if (mode.resolution > 0) 1874 mode.resolution = MOUSE_RES_LOW; 1875 if (mode.resolution == MOUSE_RES_DEFAULT) 1876 mode.resolution = sc->dflt_mode.resolution; 1877 else if (mode.resolution == -1) 1878 mode.resolution = sc->mode.resolution; 1879 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 1880 mode.resolution = MOUSE_RES_LOW - mode.resolution; 1881 1882 error = block_mouse_data(sc, &command_byte); 1883 if (error) 1884 return error; 1885 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 1886 if (sc->mode.resolution != mode.resolution) 1887 error = EIO; 1888 unblock_mouse_data(sc, command_byte); 1889 break; 1890 #endif /* MOUSE_SETRESOLUTION */ 1891 1892 #if (defined(MOUSE_SETRATE)) 1893 case MOUSE_SETRATE: 1894 mode.rate = *(int *)addr; 1895 if (mode.rate > UCHAR_MAX) 1896 return EINVAL; 1897 if (mode.rate == 0) 1898 mode.rate = sc->dflt_mode.rate; 1899 else if (mode.rate < 0) 1900 mode.rate = sc->mode.rate; 1901 1902 error = block_mouse_data(sc, &command_byte); 1903 if (error) 1904 return error; 1905 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 1906 if (sc->mode.rate != mode.rate) 1907 error = EIO; 1908 unblock_mouse_data(sc, command_byte); 1909 break; 1910 #endif /* MOUSE_SETRATE */ 1911 1912 #if (defined(MOUSE_SETSCALING)) 1913 case MOUSE_SETSCALING: 1914 if ((*(int *)addr <= 0) || (*(int *)addr > 2)) 1915 return EINVAL; 1916 1917 error = block_mouse_data(sc, &command_byte); 1918 if (error) 1919 return error; 1920 if (!set_mouse_scaling(sc->kbdc, *(int *)addr)) 1921 error = EIO; 1922 unblock_mouse_data(sc, command_byte); 1923 break; 1924 #endif /* MOUSE_SETSCALING */ 1925 1926 #if (defined(MOUSE_GETHWID)) 1927 case MOUSE_GETHWID: 1928 error = block_mouse_data(sc, &command_byte); 1929 if (error) 1930 return error; 1931 sc->hw.hwid &= ~0x00ff; 1932 sc->hw.hwid |= get_aux_id(sc->kbdc); 1933 *(int *)addr = sc->hw.hwid & 0x00ff; 1934 unblock_mouse_data(sc, command_byte); 1935 break; 1936 #endif /* MOUSE_GETHWID */ 1937 1938 default: 1939 return ENOTTY; 1940 } 1941 1942 return error; 1943 } 1944 1945 static void 1946 psmtimeout(void *arg) 1947 { 1948 struct psm_softc *sc; 1949 int s; 1950 1951 sc = (struct psm_softc *)arg; 1952 s = spltty(); 1953 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) { 1954 if (verbose >= 4) 1955 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit); 1956 psmintr(sc); 1957 kbdc_lock(sc->kbdc, FALSE); 1958 } 1959 sc->watchdog = TRUE; 1960 splx(s); 1961 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz); 1962 } 1963 1964 static void 1965 psmintr(void *arg) 1966 { 1967 /* 1968 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN) 1969 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN). 1970 */ 1971 static int butmap[8] = { 1972 0, 1973 MOUSE_BUTTON1DOWN, 1974 MOUSE_BUTTON3DOWN, 1975 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 1976 MOUSE_BUTTON2DOWN, 1977 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 1978 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 1979 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 1980 }; 1981 static int butmap_versapad[8] = { 1982 0, 1983 MOUSE_BUTTON3DOWN, 1984 0, 1985 MOUSE_BUTTON3DOWN, 1986 MOUSE_BUTTON1DOWN, 1987 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 1988 MOUSE_BUTTON1DOWN, 1989 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN 1990 }; 1991 struct psm_softc *sc = arg; 1992 mousestatus_t ms; 1993 struct timeval tv; 1994 int x, y, z; 1995 int c; 1996 int l; 1997 int x0, y0; 1998 1999 /* read until there is nothing to read */ 2000 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) { 2001 2002 /* discard the byte if the device is not open */ 2003 if ((sc->state & PSM_OPEN) == 0) 2004 continue; 2005 2006 getmicrouptime(&tv); 2007 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) { 2008 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n"); 2009 sc->inputbytes = 0; 2010 sc->syncerrors = 0; 2011 } 2012 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000; 2013 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000; 2014 timevaladd(&sc->inputtimeout, &tv); 2015 2016 sc->ipacket[sc->inputbytes++] = c; 2017 if (sc->inputbytes < sc->mode.packetsize) 2018 continue; 2019 2020 #if 0 2021 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n", 2022 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2], 2023 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]); 2024 #endif 2025 2026 c = sc->ipacket[0]; 2027 2028 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) { 2029 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n", 2030 c & sc->mode.syncmask[0], sc->mode.syncmask[1]); 2031 ++sc->syncerrors; 2032 if (sc->syncerrors < sc->mode.packetsize) { 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 == sc->mode.packetsize) { 2037 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n"); 2038 sc->inputbytes = 0; 2039 disable_aux_dev(sc->kbdc); 2040 enable_aux_dev(sc->kbdc); 2041 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) { 2042 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors); 2043 --sc->inputbytes; 2044 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes); 2045 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) { 2046 log(LOG_DEBUG, "psmintr: reset the mouse.\n"); 2047 reinitialize(sc, TRUE); 2048 } 2049 continue; 2050 } 2051 2052 /* 2053 * A kludge for Kensington device! 2054 * The MSB of the horizontal count appears to be stored in 2055 * a strange place. 2056 */ 2057 if (sc->hw.model == MOUSE_MODEL_THINK) 2058 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0; 2059 2060 /* ignore the overflow bits... */ 2061 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1]; 2062 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2]; 2063 z = 0; 2064 ms.obutton = sc->button; /* previous button state */ 2065 ms.button = butmap[c & MOUSE_PS2_BUTTONS]; 2066 /* `tapping' action */ 2067 if (sc->config & PSM_CONFIG_FORCETAP) 2068 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 2069 2070 switch (sc->hw.model) { 2071 2072 case MOUSE_MODEL_EXPLORER: 2073 /* 2074 * b7 b6 b5 b4 b3 b2 b1 b0 2075 * byte 1: oy ox sy sx 1 M R L 2076 * byte 2: x x x x x x x x 2077 * byte 3: y y y y y y y y 2078 * byte 4: * * S2 S1 s d2 d1 d0 2079 * 2080 * L, M, R, S1, S2: left, middle, right and side buttons 2081 * s: wheel data sign bit 2082 * d2-d0: wheel data 2083 */ 2084 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG) 2085 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f); 2086 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) 2087 ? MOUSE_BUTTON4DOWN : 0; 2088 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) 2089 ? MOUSE_BUTTON5DOWN : 0; 2090 break; 2091 2092 case MOUSE_MODEL_INTELLI: 2093 case MOUSE_MODEL_NET: 2094 /* wheel data is in the fourth byte */ 2095 z = (char)sc->ipacket[3]; 2096 /* some mice may send 7 when there is no Z movement?! XXX */ 2097 if ((z >= 7) || (z <= -7)) 2098 z = 0; 2099 /* some compatible mice have additional buttons */ 2100 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) 2101 ? MOUSE_BUTTON4DOWN : 0; 2102 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) 2103 ? MOUSE_BUTTON5DOWN : 0; 2104 break; 2105 2106 case MOUSE_MODEL_MOUSEMANPLUS: 2107 /* 2108 * PS2++ protocl packet 2109 * 2110 * b7 b6 b5 b4 b3 b2 b1 b0 2111 * byte 1: * 1 p3 p2 1 * * * 2112 * byte 2: c1 c2 p1 p0 d1 d0 1 0 2113 * 2114 * p3-p0: packet type 2115 * c1, c2: c1 & c2 == 1, if p2 == 0 2116 * c1 & c2 == 0, if p2 == 1 2117 * 2118 * packet type: 0 (device type) 2119 * See comments in enable_mmanplus() below. 2120 * 2121 * packet type: 1 (wheel data) 2122 * 2123 * b7 b6 b5 b4 b3 b2 b1 b0 2124 * byte 3: h * B5 B4 s d2 d1 d0 2125 * 2126 * h: 1, if horizontal roller data 2127 * 0, if vertical roller data 2128 * B4, B5: button 4 and 5 2129 * s: sign bit 2130 * d2-d0: roller data 2131 * 2132 * packet type: 2 (reserved) 2133 */ 2134 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) 2135 && (abs(x) > 191) 2136 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) { 2137 /* the extended data packet encodes button and wheel events */ 2138 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) { 2139 case 1: 2140 /* wheel data packet */ 2141 x = y = 0; 2142 if (sc->ipacket[2] & 0x80) { 2143 /* horizontal roller count - ignore it XXX*/ 2144 } else { 2145 /* vertical roller count */ 2146 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG) 2147 ? (sc->ipacket[2] & 0x0f) - 16 2148 : (sc->ipacket[2] & 0x0f); 2149 } 2150 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN) 2151 ? MOUSE_BUTTON4DOWN : 0; 2152 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN) 2153 ? MOUSE_BUTTON5DOWN : 0; 2154 break; 2155 case 2: 2156 /* this packet type is reserved by Logitech... */ 2157 /* 2158 * IBM ScrollPoint Mouse uses this packet type to 2159 * encode both vertical and horizontal scroll movement. 2160 */ 2161 x = y = 0; 2162 /* horizontal count */ 2163 if (sc->ipacket[2] & 0x0f) 2164 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2; 2165 /* vertical count */ 2166 if (sc->ipacket[2] & 0xf0) 2167 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1; 2168 #if 0 2169 /* vertical count */ 2170 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) 2171 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16 2172 : ((sc->ipacket[2] >> 4) & 0x0f); 2173 /* horizontal count */ 2174 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) 2175 ? (sc->ipacket[2] & 0x0f) - 16 2176 : (sc->ipacket[2] & 0x0f); 2177 #endif 2178 break; 2179 case 0: 2180 /* device type packet - shouldn't happen */ 2181 /* FALL THROUGH */ 2182 default: 2183 x = y = 0; 2184 ms.button = ms.obutton; 2185 if (bootverbose) 2186 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: " 2187 "0x%02x 0x%02x 0x%02x\n", 2188 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket), 2189 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]); 2190 break; 2191 } 2192 } else { 2193 /* preserve button states */ 2194 ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 2195 } 2196 break; 2197 2198 case MOUSE_MODEL_GLIDEPOINT: 2199 /* `tapping' action */ 2200 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 2201 break; 2202 2203 case MOUSE_MODEL_NETSCROLL: 2204 /* three addtional bytes encode buttons and wheel events */ 2205 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) 2206 ? MOUSE_BUTTON4DOWN : 0; 2207 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) 2208 ? MOUSE_BUTTON5DOWN : 0; 2209 z = (sc->ipacket[3] & MOUSE_PS2_XNEG) 2210 ? sc->ipacket[4] - 256 : sc->ipacket[4]; 2211 break; 2212 2213 case MOUSE_MODEL_THINK: 2214 /* the fourth button state in the first byte */ 2215 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0; 2216 break; 2217 2218 case MOUSE_MODEL_VERSAPAD: 2219 /* VersaPad PS/2 absolute mode message format 2220 * 2221 * [packet1] 7 6 5 4 3 2 1 0(LSB) 2222 * ipacket[0]: 1 1 0 A 1 L T R 2223 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0 2224 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0 2225 * ipacket[3]: 1 1 1 A 1 L T R 2226 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8 2227 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0 2228 * 2229 * [note] 2230 * R: right physical mouse button (1=on) 2231 * T: touch pad virtual button (1=tapping) 2232 * L: left physical mouse button (1=on) 2233 * A: position data is valid (1=valid) 2234 * H: horizontal data (12bit signed integer. H11 is sign bit.) 2235 * V: vertical data (12bit signed integer. V11 is sign bit.) 2236 * P: pressure data 2237 * 2238 * Tapping is mapped to MOUSE_BUTTON4. 2239 */ 2240 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS]; 2241 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 2242 x = y = 0; 2243 if (c & MOUSE_PS2VERSA_IN_USE) { 2244 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8); 2245 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4); 2246 if (x0 & 0x800) 2247 x0 -= 0x1000; 2248 if (y0 & 0x800) 2249 y0 -= 0x1000; 2250 if (sc->flags & PSM_FLAGS_FINGERDOWN) { 2251 x = sc->xold - x0; 2252 y = y0 - sc->yold; 2253 if (x < 0) /* XXX */ 2254 x++; 2255 else if (x) 2256 x--; 2257 if (y < 0) 2258 y++; 2259 else if (y) 2260 y--; 2261 } else { 2262 sc->flags |= PSM_FLAGS_FINGERDOWN; 2263 } 2264 sc->xold = x0; 2265 sc->yold = y0; 2266 } else { 2267 sc->flags &= ~PSM_FLAGS_FINGERDOWN; 2268 } 2269 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) 2270 | ((y < 0) ? MOUSE_PS2_YNEG : 0); 2271 break; 2272 2273 case MOUSE_MODEL_4D: 2274 /* 2275 * b7 b6 b5 b4 b3 b2 b1 b0 2276 * byte 1: s2 d2 s1 d1 1 M R L 2277 * byte 2: sx x x x x x x x 2278 * byte 3: sy y y y y y y y 2279 * 2280 * s1: wheel 1 direction 2281 * d1: wheel 1 data 2282 * s2: wheel 2 direction 2283 * d2: wheel 2 data 2284 */ 2285 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1]; 2286 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2]; 2287 switch (c & MOUSE_4D_WHEELBITS) { 2288 case 0x10: 2289 z = 1; 2290 break; 2291 case 0x30: 2292 z = -1; 2293 break; 2294 case 0x40: /* 2nd wheel turning right XXX */ 2295 z = 2; 2296 break; 2297 case 0xc0: /* 2nd wheel turning left XXX */ 2298 z = -2; 2299 break; 2300 } 2301 break; 2302 2303 case MOUSE_MODEL_4DPLUS: 2304 if ((x < 16 - 256) && (y < 16 - 256)) { 2305 /* 2306 * b7 b6 b5 b4 b3 b2 b1 b0 2307 * byte 1: 0 0 1 1 1 M R L 2308 * byte 2: 0 0 0 0 1 0 0 0 2309 * byte 3: 0 0 0 0 S s d1 d0 2310 * 2311 * L, M, R, S: left, middle, right and side buttons 2312 * s: wheel data sign bit 2313 * d1-d0: wheel data 2314 */ 2315 x = y = 0; 2316 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN) 2317 ms.button |= MOUSE_BUTTON4DOWN; 2318 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG) 2319 ? ((sc->ipacket[2] & 0x07) - 8) 2320 : (sc->ipacket[2] & 0x07) ; 2321 } else { 2322 /* preserve previous button states */ 2323 ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 2324 } 2325 break; 2326 2327 case MOUSE_MODEL_GENERIC: 2328 default: 2329 break; 2330 } 2331 2332 /* scale values */ 2333 if (sc->mode.accelfactor >= 1) { 2334 if (x != 0) { 2335 x = x * x / sc->mode.accelfactor; 2336 if (x == 0) 2337 x = 1; 2338 if (c & MOUSE_PS2_XNEG) 2339 x = -x; 2340 } 2341 if (y != 0) { 2342 y = y * y / sc->mode.accelfactor; 2343 if (y == 0) 2344 y = 1; 2345 if (c & MOUSE_PS2_YNEG) 2346 y = -y; 2347 } 2348 } 2349 2350 ms.dx = x; 2351 ms.dy = y; 2352 ms.dz = z; 2353 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) 2354 | (ms.obutton ^ ms.button); 2355 2356 if (sc->mode.level < PSM_LEVEL_NATIVE) 2357 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket); 2358 2359 sc->status.flags |= ms.flags; 2360 sc->status.dx += ms.dx; 2361 sc->status.dy += ms.dy; 2362 sc->status.dz += ms.dz; 2363 sc->status.button = ms.button; 2364 sc->button = ms.button; 2365 2366 sc->watchdog = FALSE; 2367 2368 /* queue data */ 2369 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) { 2370 l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail); 2371 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l); 2372 if (sc->inputbytes > l) 2373 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l); 2374 sc->queue.tail = 2375 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf); 2376 sc->queue.count += sc->inputbytes; 2377 } 2378 sc->inputbytes = 0; 2379 2380 if (sc->state & PSM_ASLP) { 2381 sc->state &= ~PSM_ASLP; 2382 wakeup((caddr_t) sc); 2383 } 2384 selwakeup(&sc->rsel); 2385 } 2386 } 2387 2388 static int 2389 psmpoll(dev_t dev, int events, struct thread *td) 2390 { 2391 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 2392 int s; 2393 int revents = 0; 2394 2395 /* Return true if a mouse event available */ 2396 s = spltty(); 2397 if (events & (POLLIN | POLLRDNORM)) { 2398 if (sc->queue.count > 0) 2399 revents |= events & (POLLIN | POLLRDNORM); 2400 else 2401 selrecord(td, &sc->rsel); 2402 } 2403 splx(s); 2404 2405 return (revents); 2406 } 2407 2408 /* vendor/model specific routines */ 2409 2410 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status) 2411 { 2412 if (set_mouse_resolution(kbdc, res) != res) 2413 return FALSE; 2414 if (set_mouse_scaling(kbdc, scale) 2415 && set_mouse_scaling(kbdc, scale) 2416 && set_mouse_scaling(kbdc, scale) 2417 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) 2418 return TRUE; 2419 return FALSE; 2420 } 2421 2422 static int 2423 mouse_ext_command(KBDC kbdc, int command) 2424 { 2425 int c; 2426 2427 c = (command >> 6) & 0x03; 2428 if (set_mouse_resolution(kbdc, c) != c) 2429 return FALSE; 2430 c = (command >> 4) & 0x03; 2431 if (set_mouse_resolution(kbdc, c) != c) 2432 return FALSE; 2433 c = (command >> 2) & 0x03; 2434 if (set_mouse_resolution(kbdc, c) != c) 2435 return FALSE; 2436 c = (command >> 0) & 0x03; 2437 if (set_mouse_resolution(kbdc, c) != c) 2438 return FALSE; 2439 return TRUE; 2440 } 2441 2442 #if notyet 2443 /* Logitech MouseMan Cordless II */ 2444 static int 2445 enable_lcordless(struct psm_softc *sc) 2446 { 2447 int status[3]; 2448 int ch; 2449 2450 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status)) 2451 return FALSE; 2452 if (status[1] == PSMD_RES_HIGH) 2453 return FALSE; 2454 ch = (status[0] & 0x07) - 1; /* channel # */ 2455 if ((ch <= 0) || (ch > 4)) 2456 return FALSE; 2457 /* 2458 * status[1]: always one? 2459 * status[2]: battery status? (0-100) 2460 */ 2461 return TRUE; 2462 } 2463 #endif /* notyet */ 2464 2465 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */ 2466 static int 2467 enable_groller(struct psm_softc *sc) 2468 { 2469 int status[3]; 2470 2471 /* 2472 * The special sequence to enable the fourth button and the 2473 * roller. Immediately after this sequence check status bytes. 2474 * if the mouse is NetScroll, the second and the third bytes are 2475 * '3' and 'D'. 2476 */ 2477 2478 /* 2479 * If the mouse is an ordinary PS/2 mouse, the status bytes should 2480 * look like the following. 2481 * 2482 * byte 1 bit 7 always 0 2483 * bit 6 stream mode (0) 2484 * bit 5 disabled (0) 2485 * bit 4 1:1 scaling (0) 2486 * bit 3 always 0 2487 * bit 0-2 button status 2488 * byte 2 resolution (PSMD_RES_HIGH) 2489 * byte 3 report rate (?) 2490 */ 2491 2492 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 2493 return FALSE; 2494 if ((status[1] != '3') || (status[2] != 'D')) 2495 return FALSE; 2496 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */ 2497 sc->hw.buttons = 4; 2498 return TRUE; 2499 } 2500 2501 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */ 2502 static int 2503 enable_gmouse(struct psm_softc *sc) 2504 { 2505 int status[3]; 2506 2507 /* 2508 * The special sequence to enable the middle, "rubber" button. 2509 * Immediately after this sequence check status bytes. 2510 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse, 2511 * the second and the third bytes are '3' and 'U'. 2512 * NOTE: NetMouse reports that it has three buttons although it has 2513 * two buttons and a rubber button. NetMouse Pro and MIE Mouse 2514 * say they have three buttons too and they do have a button on the 2515 * side... 2516 */ 2517 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 2518 return FALSE; 2519 if ((status[1] != '3') || (status[2] != 'U')) 2520 return FALSE; 2521 return TRUE; 2522 } 2523 2524 /* ALPS GlidePoint */ 2525 static int 2526 enable_aglide(struct psm_softc *sc) 2527 { 2528 int status[3]; 2529 2530 /* 2531 * The special sequence to obtain ALPS GlidePoint specific 2532 * information. Immediately after this sequence, status bytes will 2533 * contain something interesting. 2534 * NOTE: ALPS produces several models of GlidePoint. Some of those 2535 * do not respond to this sequence, thus, cannot be detected this way. 2536 */ 2537 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100) 2538 return FALSE; 2539 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status)) 2540 return FALSE; 2541 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100)) 2542 return FALSE; 2543 return TRUE; 2544 } 2545 2546 /* Kensington ThinkingMouse/Trackball */ 2547 static int 2548 enable_kmouse(struct psm_softc *sc) 2549 { 2550 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 }; 2551 KBDC kbdc = sc->kbdc; 2552 int status[3]; 2553 int id1; 2554 int id2; 2555 int i; 2556 2557 id1 = get_aux_id(kbdc); 2558 if (set_mouse_sampling_rate(kbdc, 10) != 10) 2559 return FALSE; 2560 /* 2561 * The device is now in the native mode? It returns a different 2562 * ID value... 2563 */ 2564 id2 = get_aux_id(kbdc); 2565 if ((id1 == id2) || (id2 != 2)) 2566 return FALSE; 2567 2568 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 2569 return FALSE; 2570 #if PSM_DEBUG >= 2 2571 /* at this point, resolution is LOW, sampling rate is 10/sec */ 2572 if (get_mouse_status(kbdc, status, 0, 3) < 3) 2573 return FALSE; 2574 #endif 2575 2576 /* 2577 * The special sequence to enable the third and fourth buttons. 2578 * Otherwise they behave like the first and second buttons. 2579 */ 2580 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 2581 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 2582 return FALSE; 2583 } 2584 2585 /* 2586 * At this point, the device is using default resolution and 2587 * sampling rate for the native mode. 2588 */ 2589 if (get_mouse_status(kbdc, status, 0, 3) < 3) 2590 return FALSE; 2591 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1])) 2592 return FALSE; 2593 2594 /* the device appears be enabled by this sequence, diable it for now */ 2595 disable_aux_dev(kbdc); 2596 empty_aux_buffer(kbdc, 5); 2597 2598 return TRUE; 2599 } 2600 2601 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */ 2602 static int 2603 enable_mmanplus(struct psm_softc *sc) 2604 { 2605 KBDC kbdc = sc->kbdc; 2606 int data[3]; 2607 2608 /* the special sequence to enable the fourth button and the roller. */ 2609 /* 2610 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION 2611 * must be called exactly three times since the last RESET command 2612 * before this sequence. XXX 2613 */ 2614 if (!set_mouse_scaling(kbdc, 1)) 2615 return FALSE; 2616 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb)) 2617 return FALSE; 2618 if (get_mouse_status(kbdc, data, 1, 3) < 3) 2619 return FALSE; 2620 2621 /* 2622 * PS2++ protocl, packet type 0 2623 * 2624 * b7 b6 b5 b4 b3 b2 b1 b0 2625 * byte 1: * 1 p3 p2 1 * * * 2626 * byte 2: 1 1 p1 p0 m1 m0 1 0 2627 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0 2628 * 2629 * p3-p0: packet type: 0 2630 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58... 2631 */ 2632 /* check constant bits */ 2633 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC) 2634 return FALSE; 2635 if ((data[1] & 0xc3) != 0xc2) 2636 return FALSE; 2637 /* check d3-d0 in byte 2 */ 2638 if (!MOUSE_PS2PLUS_CHECKBITS(data)) 2639 return FALSE; 2640 /* check p3-p0 */ 2641 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0) 2642 return FALSE; 2643 2644 sc->hw.hwid &= 0x00ff; 2645 sc->hw.hwid |= data[2] << 8; /* save model ID */ 2646 2647 /* 2648 * MouseMan+ (or FirstMouse+) is now in its native mode, in which 2649 * the wheel and the fourth button events are encoded in the 2650 * special data packet. The mouse may be put in the IntelliMouse mode 2651 * if it is initialized by the IntelliMouse's method. 2652 */ 2653 return TRUE; 2654 } 2655 2656 /* MS IntelliMouse Explorer */ 2657 static int 2658 enable_msexplorer(struct psm_softc *sc) 2659 { 2660 static unsigned char rate0[] = { 200, 100, 80, }; 2661 static unsigned char rate1[] = { 200, 200, 80, }; 2662 KBDC kbdc = sc->kbdc; 2663 int id; 2664 int i; 2665 2666 /* the special sequence to enable the extra buttons and the roller. */ 2667 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) { 2668 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i]) 2669 return FALSE; 2670 } 2671 /* the device will give the genuine ID only after the above sequence */ 2672 id = get_aux_id(kbdc); 2673 if (id != PSM_EXPLORER_ID) 2674 return FALSE; 2675 2676 sc->hw.hwid = id; 2677 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */ 2678 2679 /* 2680 * XXX: this is a kludge to fool some KVM switch products 2681 * which think they are clever enough to know the 4-byte IntelliMouse 2682 * protocol, and assume any other protocols use 3-byte packets. 2683 * They don't convey 4-byte data packets from the IntelliMouse Explorer 2684 * correctly to the host computer because of this! 2685 * The following sequence is actually IntelliMouse's "wake up" 2686 * sequence; it will make the KVM think the mouse is IntelliMouse 2687 * when it is in fact IntelliMouse Explorer. 2688 */ 2689 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) { 2690 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i]) 2691 break; 2692 } 2693 id = get_aux_id(kbdc); 2694 2695 return TRUE; 2696 } 2697 2698 /* MS IntelliMouse */ 2699 static int 2700 enable_msintelli(struct psm_softc *sc) 2701 { 2702 /* 2703 * Logitech MouseMan+ and FirstMouse+ will also respond to this 2704 * probe routine and act like IntelliMouse. 2705 */ 2706 2707 static unsigned char rate[] = { 200, 100, 80, }; 2708 KBDC kbdc = sc->kbdc; 2709 int id; 2710 int i; 2711 2712 /* the special sequence to enable the third button and the roller. */ 2713 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 2714 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 2715 return FALSE; 2716 } 2717 /* the device will give the genuine ID only after the above sequence */ 2718 id = get_aux_id(kbdc); 2719 if (id != PSM_INTELLI_ID) 2720 return FALSE; 2721 2722 sc->hw.hwid = id; 2723 sc->hw.buttons = 3; 2724 2725 return TRUE; 2726 } 2727 2728 /* A4 Tech 4D Mouse */ 2729 static int 2730 enable_4dmouse(struct psm_softc *sc) 2731 { 2732 /* 2733 * Newer wheel mice from A4 Tech may use the 4D+ protocol. 2734 */ 2735 2736 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 2737 KBDC kbdc = sc->kbdc; 2738 int id; 2739 int i; 2740 2741 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 2742 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 2743 return FALSE; 2744 } 2745 id = get_aux_id(kbdc); 2746 /* 2747 * WinEasy 4D, 4 Way Scroll 4D: 6 2748 * Cable-Free 4D: 8 (4DPLUS) 2749 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS) 2750 */ 2751 if (id != PSM_4DMOUSE_ID) 2752 return FALSE; 2753 2754 sc->hw.hwid = id; 2755 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */ 2756 2757 return TRUE; 2758 } 2759 2760 /* A4 Tech 4D+ Mouse */ 2761 static int 2762 enable_4dplus(struct psm_softc *sc) 2763 { 2764 /* 2765 * Newer wheel mice from A4 Tech seem to use this protocol. 2766 * Older models are recognized as either 4D Mouse or IntelliMouse. 2767 */ 2768 KBDC kbdc = sc->kbdc; 2769 int id; 2770 2771 /* 2772 * enable_4dmouse() already issued the following ID sequence... 2773 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 2774 int i; 2775 2776 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 2777 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 2778 return FALSE; 2779 } 2780 */ 2781 2782 id = get_aux_id(kbdc); 2783 if (id != PSM_4DPLUS_ID) 2784 return FALSE; 2785 2786 sc->hw.hwid = id; 2787 sc->hw.buttons = 4; /* XXX */ 2788 2789 return TRUE; 2790 } 2791 2792 /* Interlink electronics VersaPad */ 2793 static int 2794 enable_versapad(struct psm_softc *sc) 2795 { 2796 KBDC kbdc = sc->kbdc; 2797 int data[3]; 2798 2799 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */ 2800 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */ 2801 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 2802 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 2803 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 2804 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 2805 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */ 2806 return FALSE; 2807 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */ 2808 return FALSE; 2809 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 2810 2811 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 2812 2813 return TRUE; /* PS/2 absolute mode */ 2814 } 2815 2816 static int 2817 psmresume(device_t dev) 2818 { 2819 struct psm_softc *sc = device_get_softc(dev); 2820 int unit = device_get_unit(dev); 2821 int err; 2822 2823 if (verbose >= 2) 2824 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit); 2825 2826 if (!(sc->config & PSM_CONFIG_HOOKRESUME)) 2827 return (0); 2828 2829 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND); 2830 2831 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) { 2832 /* 2833 * Release the blocked process; it must be notified that the device 2834 * cannot be accessed anymore. 2835 */ 2836 sc->state &= ~PSM_ASLP; 2837 wakeup((caddr_t)sc); 2838 } 2839 2840 if (verbose >= 2) 2841 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit); 2842 2843 return (err); 2844 } 2845 2846 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0); 2847