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.13 2004/09/18 19:08:08 dillon Exp $ 25 */ 26 27 /* 28 * Ported to 386bsd Oct 17, 1992 29 * Sandi Donno, Computer Science, University of Cape Town, South Africa 30 * Please send bug reports to sandi@cs.uct.ac.za 31 * 32 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca - 33 * although I was only partially successful in getting the alpha release 34 * of his "driver for the Logitech and ATI Inport Bus mice for use with 35 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless 36 * found his code to be an invaluable reference when porting this driver 37 * to 386bsd. 38 * 39 * Further modifications for latest 386BSD+patchkit and port to NetBSD, 40 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993 41 * 42 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by 43 * Andrew Herbert - 12 June 1993 44 * 45 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu> 46 * - 13 June 1993 47 * 48 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp> 49 * - 24 October 1993 50 * 51 * Hardware access routines and probe logic rewritten by 52 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp> 53 * - 3, 14, 22 October 1996. 54 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'... 55 * - 14, 30 November 1996. Uses `kbdio.c'. 56 * - 13 December 1996. Uses queuing version of `kbdio.c'. 57 * - January/February 1997. Tweaked probe logic for 58 * HiNote UltraII/Latitude/Armada laptops. 59 * - 30 July 1997. Added APM support. 60 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX). 61 * Improved sync check logic. 62 * Vendor specific support routines. 63 */ 64 65 #include "opt_psm.h" 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/kernel.h> 70 #include <sys/module.h> 71 #include <sys/bus.h> 72 #include <sys/conf.h> 73 #include <sys/poll.h> 74 #include <sys/syslog.h> 75 #include <sys/malloc.h> 76 #include <machine/bus.h> 77 #include <sys/rman.h> 78 #include <sys/select.h> 79 #include <sys/time.h> 80 #include <sys/uio.h> 81 82 #include <machine/clock.h> 83 #include <machine/limits.h> 84 #include <machine/mouse.h> 85 #include <machine/resource.h> 86 87 #include <bus/isa/isavar.h> 88 #include <dev/misc/kbd/atkbdcreg.h> 89 90 /* 91 * Driver specific options: the following options may be set by 92 * `options' statements in the kernel configuration file. 93 */ 94 95 /* debugging */ 96 #ifndef PSM_DEBUG 97 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */ 98 #endif 99 100 #ifndef PSM_SYNCERR_THRESHOLD1 101 #define PSM_SYNCERR_THRESHOLD1 20 102 #endif 103 104 #ifndef PSM_INPUT_TIMEOUT 105 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */ 106 #endif 107 108 /* end of driver specific options */ 109 110 #define PSM_DRIVER_NAME "psm" 111 #define PSMCPNP_DRIVER_NAME "psmcpnp" 112 113 /* input queue */ 114 #define PSM_BUFSIZE 960 115 #define PSM_SMALLBUFSIZE 240 116 117 /* operation levels */ 118 #define PSM_LEVEL_BASE 0 119 #define PSM_LEVEL_STANDARD 1 120 #define PSM_LEVEL_NATIVE 2 121 #define PSM_LEVEL_MIN PSM_LEVEL_BASE 122 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE 123 124 /* Logitech PS2++ protocol */ 125 #define MOUSE_PS2PLUS_CHECKBITS(b) \ 126 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f)) 127 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \ 128 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4)) 129 130 /* some macros */ 131 #define PSM_UNIT(dev) (minor(dev) >> 1) 132 #define PSM_NBLOCKIO(dev) (minor(dev) & 1) 133 #define PSM_MKMINOR(unit,block) ((((unit) & 0xff) << 1) | ((block) ? 0:1)) 134 135 #ifndef max 136 #define max(x,y) ((x) > (y) ? (x) : (y)) 137 #endif 138 #ifndef min 139 #define min(x,y) ((x) < (y) ? (x) : (y)) 140 #endif 141 142 #define abs(x) (((x) < 0) ? -(x) : (x)) 143 144 /* ring buffer */ 145 typedef struct ringbuf { 146 int count; /* # of valid elements in the buffer */ 147 int head; /* head pointer */ 148 int tail; /* tail poiner */ 149 unsigned char buf[PSM_BUFSIZE]; 150 } ringbuf_t; 151 152 /* driver control block */ 153 struct psm_softc { /* Driver status information */ 154 int unit; 155 struct selinfo rsel; /* Process selecting for Input */ 156 unsigned char state; /* Mouse driver state */ 157 int config; /* driver configuration flags */ 158 int flags; /* other flags */ 159 KBDC kbdc; /* handle to access the keyboard controller */ 160 struct resource *intr; /* IRQ resource */ 161 void *ih; /* interrupt handle */ 162 mousehw_t hw; /* hardware information */ 163 mousemode_t mode; /* operation mode */ 164 mousemode_t dflt_mode; /* default operation mode */ 165 mousestatus_t status; /* accumulated mouse movement */ 166 ringbuf_t queue; /* mouse status queue */ 167 unsigned char ipacket[16]; /* interim input buffer */ 168 int inputbytes; /* # of bytes in the input buffer */ 169 int button; /* the latest button state */ 170 int xold; /* previous absolute X position */ 171 int yold; /* previous absolute Y position */ 172 int syncerrors; 173 struct timeval inputtimeout; 174 int watchdog; /* watchdog timer flag */ 175 struct callout callout; /* watchdog timer call out */ 176 }; 177 devclass_t psm_devclass; 178 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit)) 179 180 /* driver state flags (state) */ 181 #define PSM_VALID 0x80 182 #define PSM_OPEN 1 /* Device is open */ 183 #define PSM_ASLP 2 /* Waiting for mouse data */ 184 185 /* driver configuration flags (config) */ 186 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */ 187 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */ 188 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */ 189 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */ 190 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */ 191 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */ 192 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */ 193 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */ 194 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */ 195 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */ 196 197 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \ 198 | PSM_CONFIG_ACCEL \ 199 | PSM_CONFIG_NOCHECKSYNC \ 200 | PSM_CONFIG_SYNCHACK \ 201 | PSM_CONFIG_NOIDPROBE \ 202 | PSM_CONFIG_NORESET \ 203 | PSM_CONFIG_FORCETAP \ 204 | PSM_CONFIG_IGNPORTERROR \ 205 | PSM_CONFIG_HOOKRESUME \ 206 | PSM_CONFIG_INITAFTERSUSPEND) 207 208 /* other flags (flags) */ 209 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */ 210 211 /* for backward compatibility */ 212 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t) 213 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t) 214 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t) 215 216 typedef struct old_mousehw { 217 int buttons; 218 int iftype; 219 int type; 220 int hwid; 221 } old_mousehw_t; 222 223 typedef struct old_mousemode { 224 int protocol; 225 int rate; 226 int resolution; 227 int accelfactor; 228 } old_mousemode_t; 229 230 /* packet formatting function */ 231 typedef int packetfunc_t (struct psm_softc *, unsigned char *, 232 int *, int, mousestatus_t *); 233 234 /* function prototypes */ 235 static int psmprobe (device_t); 236 static int psmattach (device_t); 237 static int psmdetach (device_t); 238 static int psmresume (device_t); 239 240 static d_open_t psmopen; 241 static d_close_t psmclose; 242 static d_read_t psmread; 243 static d_ioctl_t psmioctl; 244 static d_poll_t psmpoll; 245 246 static int enable_aux_dev (KBDC); 247 static int disable_aux_dev (KBDC); 248 static int get_mouse_status (KBDC, int *, int, int); 249 static int get_aux_id (KBDC); 250 static int set_mouse_sampling_rate (KBDC, int); 251 static int set_mouse_scaling (KBDC, int); 252 static int set_mouse_resolution (KBDC, int); 253 static int set_mouse_mode (KBDC); 254 static int get_mouse_buttons (KBDC); 255 static int is_a_mouse (int); 256 static void recover_from_error (KBDC); 257 static int restore_controller (KBDC, int); 258 static int doinitialize (struct psm_softc *, mousemode_t *); 259 static int doopen (struct psm_softc *, int); 260 static int reinitialize (struct psm_softc *, int); 261 static char *model_name (int); 262 static void psmintr (void *); 263 static void psmtimeout (void *); 264 265 /* vendor specific features */ 266 typedef int probefunc_t (struct psm_softc *); 267 268 static int mouse_id_proc1 (KBDC, int, int, int *); 269 static int mouse_ext_command (KBDC, int); 270 static probefunc_t enable_groller; 271 static probefunc_t enable_gmouse; 272 static probefunc_t enable_aglide; 273 static probefunc_t enable_kmouse; 274 static probefunc_t enable_msexplorer; 275 static probefunc_t enable_msintelli; 276 static probefunc_t enable_4dmouse; 277 static probefunc_t enable_4dplus; 278 static probefunc_t enable_mmanplus; 279 static probefunc_t enable_versapad; 280 static int tame_mouse (struct psm_softc *, mousestatus_t *, unsigned char *); 281 282 static struct { 283 int model; 284 unsigned char syncmask; 285 int packetsize; 286 probefunc_t *probefunc; 287 } vendortype[] = { 288 /* 289 * WARNING: the order of probe is very important. Don't mess it 290 * unless you know what you are doing. 291 */ 292 { MOUSE_MODEL_NET, /* Genius NetMouse */ 293 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, }, 294 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */ 295 0xc8, 6, enable_groller, }, 296 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */ 297 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, }, 298 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */ 299 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, }, 300 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */ 301 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, }, 302 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */ 303 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, }, 304 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */ 305 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, }, 306 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */ 307 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, }, 308 { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */ 309 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, }, 310 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */ 311 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, }, 312 { MOUSE_MODEL_GENERIC, 313 0xc0, MOUSE_PS2_PACKETSIZE, NULL, }, 314 }; 315 #define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1) 316 317 /* device driver declarateion */ 318 static device_method_t psm_methods[] = { 319 /* Device interface */ 320 DEVMETHOD(device_probe, psmprobe), 321 DEVMETHOD(device_attach, psmattach), 322 DEVMETHOD(device_detach, psmdetach), 323 DEVMETHOD(device_resume, psmresume), 324 325 { 0, 0 } 326 }; 327 328 static driver_t psm_driver = { 329 PSM_DRIVER_NAME, 330 psm_methods, 331 sizeof(struct psm_softc), 332 }; 333 334 #if notyet 335 static struct isa_pnp_id psm_ids[] = { 336 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */ 337 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */ 338 { 0 } 339 }; 340 #endif 341 342 #define CDEV_MAJOR 21 343 344 static struct cdevsw psm_cdevsw = { 345 /* name */ PSM_DRIVER_NAME, 346 /* maj */ CDEV_MAJOR, 347 /* flags */ 0, 348 /* port */ NULL, 349 /* clone */ NULL, 350 351 /* open */ psmopen, 352 /* close */ psmclose, 353 /* read */ psmread, 354 /* write */ nowrite, 355 /* ioctl */ psmioctl, 356 /* poll */ psmpoll, 357 /* mmap */ nommap, 358 /* strategy */ nostrategy, 359 /* dump */ nodump, 360 /* psize */ nopsize 361 }; 362 363 /* debug message level */ 364 static int verbose = PSM_DEBUG; 365 366 /* device I/O routines */ 367 static int 368 enable_aux_dev(KBDC kbdc) 369 { 370 int res; 371 372 res = send_aux_command(kbdc, PSMC_ENABLE_DEV); 373 if (verbose >= 2) 374 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res); 375 376 return (res == PSM_ACK); 377 } 378 379 static int 380 disable_aux_dev(KBDC kbdc) 381 { 382 int res; 383 384 res = send_aux_command(kbdc, PSMC_DISABLE_DEV); 385 if (verbose >= 2) 386 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res); 387 388 return (res == PSM_ACK); 389 } 390 391 static int 392 get_mouse_status(KBDC kbdc, int *status, int flag, int len) 393 { 394 int cmd; 395 int res; 396 int i; 397 398 switch (flag) { 399 case 0: 400 default: 401 cmd = PSMC_SEND_DEV_STATUS; 402 break; 403 case 1: 404 cmd = PSMC_SEND_DEV_DATA; 405 break; 406 } 407 empty_aux_buffer(kbdc, 5); 408 res = send_aux_command(kbdc, cmd); 409 if (verbose >= 2) 410 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n", 411 (flag == 1) ? "DATA" : "STATUS", res); 412 if (res != PSM_ACK) 413 return 0; 414 415 for (i = 0; i < len; ++i) { 416 status[i] = read_aux_data(kbdc); 417 if (status[i] < 0) 418 break; 419 } 420 421 if (verbose) { 422 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n", 423 (flag == 1) ? "data" : "status", status[0], status[1], status[2]); 424 } 425 426 return i; 427 } 428 429 static int 430 get_aux_id(KBDC kbdc) 431 { 432 int res; 433 int id; 434 435 empty_aux_buffer(kbdc, 5); 436 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID); 437 if (verbose >= 2) 438 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res); 439 if (res != PSM_ACK) 440 return (-1); 441 442 /* 10ms delay */ 443 DELAY(10000); 444 445 id = read_aux_data(kbdc); 446 if (verbose >= 2) 447 log(LOG_DEBUG, "psm: device ID: %04x\n", id); 448 449 return id; 450 } 451 452 static int 453 set_mouse_sampling_rate(KBDC kbdc, int rate) 454 { 455 int res; 456 457 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate); 458 if (verbose >= 2) 459 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res); 460 461 return ((res == PSM_ACK) ? rate : -1); 462 } 463 464 static int 465 set_mouse_scaling(KBDC kbdc, int scale) 466 { 467 int res; 468 469 switch (scale) { 470 case 1: 471 default: 472 scale = PSMC_SET_SCALING11; 473 break; 474 case 2: 475 scale = PSMC_SET_SCALING21; 476 break; 477 } 478 res = send_aux_command(kbdc, scale); 479 if (verbose >= 2) 480 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n", 481 (scale == PSMC_SET_SCALING21) ? "21" : "11", res); 482 483 return (res == PSM_ACK); 484 } 485 486 /* `val' must be 0 through PSMD_MAX_RESOLUTION */ 487 static int 488 set_mouse_resolution(KBDC kbdc, int val) 489 { 490 int res; 491 492 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val); 493 if (verbose >= 2) 494 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res); 495 496 return ((res == PSM_ACK) ? val : -1); 497 } 498 499 /* 500 * NOTE: once `set_mouse_mode()' is called, the mouse device must be 501 * re-enabled by calling `enable_aux_dev()' 502 */ 503 static int 504 set_mouse_mode(KBDC kbdc) 505 { 506 int res; 507 508 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE); 509 if (verbose >= 2) 510 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res); 511 512 return (res == PSM_ACK); 513 } 514 515 static int 516 get_mouse_buttons(KBDC kbdc) 517 { 518 int c = 2; /* assume two buttons by default */ 519 int status[3]; 520 521 /* 522 * NOTE: a special sequence to obtain Logitech Mouse specific 523 * information: set resolution to 25 ppi, set scaling to 1:1, set 524 * scaling to 1:1, set scaling to 1:1. Then the second byte of the 525 * mouse status bytes is the number of available buttons. 526 * Some manufactures also support this sequence. 527 */ 528 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 529 return c; 530 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) 531 && set_mouse_scaling(kbdc, 1) 532 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) { 533 if (status[1] != 0) 534 return status[1]; 535 } 536 return c; 537 } 538 539 /* misc subroutines */ 540 /* 541 * Someday, I will get the complete list of valid pointing devices and 542 * their IDs... XXX 543 */ 544 static int 545 is_a_mouse(int id) 546 { 547 #if 0 548 static int valid_ids[] = { 549 PSM_MOUSE_ID, /* mouse */ 550 PSM_BALLPOINT_ID, /* ballpoint device */ 551 PSM_INTELLI_ID, /* Intellimouse */ 552 PSM_EXPLORER_ID, /* Intellimouse Explorer */ 553 -1 /* end of table */ 554 }; 555 int i; 556 557 for (i = 0; valid_ids[i] >= 0; ++i) 558 if (valid_ids[i] == id) 559 return TRUE; 560 return FALSE; 561 #else 562 return TRUE; 563 #endif 564 } 565 566 static char * 567 model_name(int model) 568 { 569 static struct { 570 int model_code; 571 char *model_name; 572 } models[] = { 573 { MOUSE_MODEL_NETSCROLL, "NetScroll" }, 574 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" }, 575 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" }, 576 { MOUSE_MODEL_THINK, "ThinkingMouse" }, 577 { MOUSE_MODEL_INTELLI, "IntelliMouse" }, 578 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" }, 579 { MOUSE_MODEL_VERSAPAD, "VersaPad" }, 580 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" }, 581 { MOUSE_MODEL_4D, "4D Mouse" }, 582 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" }, 583 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" }, 584 { MOUSE_MODEL_UNKNOWN, NULL }, 585 }; 586 int i; 587 588 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) { 589 if (models[i].model_code == model) 590 return models[i].model_name; 591 } 592 return "Unknown"; 593 } 594 595 static void 596 recover_from_error(KBDC kbdc) 597 { 598 /* discard anything left in the output buffer */ 599 empty_both_buffers(kbdc, 10); 600 601 #if 0 602 /* 603 * NOTE: KBDC_RESET_KBD may not restore the communication between the 604 * keyboard and the controller. 605 */ 606 reset_kbd(kbdc); 607 #else 608 /* 609 * NOTE: somehow diagnostic and keyboard port test commands bring the 610 * keyboard back. 611 */ 612 if (!test_controller(kbdc)) 613 log(LOG_ERR, "psm: keyboard controller failed.\n"); 614 /* if there isn't a keyboard in the system, the following error is OK */ 615 if (test_kbd_port(kbdc) != 0) { 616 if (verbose) 617 log(LOG_ERR, "psm: keyboard port failed.\n"); 618 } 619 #endif 620 } 621 622 static int 623 restore_controller(KBDC kbdc, int command_byte) 624 { 625 empty_both_buffers(kbdc, 10); 626 627 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) { 628 log(LOG_ERR, "psm: failed to restore the keyboard controller " 629 "command byte.\n"); 630 empty_both_buffers(kbdc, 10); 631 return FALSE; 632 } else { 633 empty_both_buffers(kbdc, 10); 634 return TRUE; 635 } 636 } 637 638 /* 639 * Re-initialize the aux port and device. The aux port must be enabled 640 * and its interrupt must be disabled before calling this routine. 641 * The aux device will be disabled before returning. 642 * The keyboard controller must be locked via `kbdc_lock()' before 643 * calling this routine. 644 */ 645 static int 646 doinitialize(struct psm_softc *sc, mousemode_t *mode) 647 { 648 KBDC kbdc = sc->kbdc; 649 int stat[3]; 650 int i; 651 652 switch((i = test_aux_port(kbdc))) { 653 case 1: /* ignore this error */ 654 case 2: /* Ignore 2 and 3 for use with some acer and compal laptops */ 655 case 3: 656 case PSM_ACK: 657 if (verbose) 658 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n", 659 sc->unit, i); 660 /* fall though */ 661 case 0: /* no error */ 662 break; 663 case -1: /* time out */ 664 default: /* error */ 665 recover_from_error(kbdc); 666 if (sc->config & PSM_CONFIG_IGNPORTERROR) 667 break; 668 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n", 669 sc->unit, i); 670 return FALSE; 671 } 672 673 if (sc->config & PSM_CONFIG_NORESET) { 674 /* 675 * Don't try to reset the pointing device. It may possibly be 676 * left in the unknown state, though... 677 */ 678 } else { 679 /* 680 * NOTE: some controllers appears to hang the `keyboard' when 681 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 682 */ 683 if (!reset_aux_dev(kbdc)) { 684 recover_from_error(kbdc); 685 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit); 686 return FALSE; 687 } 688 } 689 690 /* 691 * both the aux port and the aux device is functioning, see 692 * if the device can be enabled. 693 */ 694 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) { 695 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit); 696 return FALSE; 697 } 698 empty_both_buffers(kbdc, 10); /* remove stray data if any */ 699 700 if (sc->config & PSM_CONFIG_NOIDPROBE) { 701 i = GENERIC_MOUSE_ENTRY; 702 } else { 703 /* FIXME: hardware ID, mouse buttons? */ 704 705 /* other parameters */ 706 for (i = 0; vendortype[i].probefunc != NULL; ++i) { 707 if ((*vendortype[i].probefunc)(sc)) { 708 if (verbose >= 2) 709 log(LOG_ERR, "psm%d: found %s\n", 710 sc->unit, model_name(vendortype[i].model)); 711 break; 712 } 713 } 714 } 715 716 sc->hw.model = vendortype[i].model; 717 sc->mode.packetsize = vendortype[i].packetsize; 718 719 /* set mouse parameters */ 720 if (mode != (mousemode_t *)NULL) { 721 if (mode->rate > 0) 722 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate); 723 if (mode->resolution >= 0) 724 mode->resolution = set_mouse_resolution(kbdc, mode->resolution); 725 set_mouse_scaling(kbdc, 1); 726 set_mouse_mode(kbdc); 727 } 728 729 /* request a data packet and extract sync. bits */ 730 if (get_mouse_status(kbdc, stat, 1, 3) < 3) { 731 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n", 732 sc->unit); 733 sc->mode.syncmask[0] = 0; 734 } else { 735 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 736 /* the NetScroll Mouse will send three more bytes... Ignore them */ 737 empty_aux_buffer(kbdc, 5); 738 } 739 740 /* just check the status of the mouse */ 741 if (get_mouse_status(kbdc, stat, 0, 3) < 3) 742 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n", 743 sc->unit); 744 745 return TRUE; 746 } 747 748 static int 749 doopen(struct psm_softc *sc, int command_byte) 750 { 751 int stat[3]; 752 753 /* enable the mouse device */ 754 if (!enable_aux_dev(sc->kbdc)) { 755 /* MOUSE ERROR: failed to enable the mouse because: 756 * 1) the mouse is faulty, 757 * 2) the mouse has been removed(!?) 758 * In the latter case, the keyboard may have hung, and need 759 * recovery procedure... 760 */ 761 recover_from_error(sc->kbdc); 762 #if 0 763 /* FIXME: we could reset the mouse here and try to enable 764 * it again. But it will take long time and it's not a good 765 * idea to disable the keyboard that long... 766 */ 767 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) { 768 recover_from_error(sc->kbdc); 769 #else 770 { 771 #endif 772 restore_controller(sc->kbdc, command_byte); 773 /* mark this device is no longer available */ 774 sc->state &= ~PSM_VALID; 775 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n", 776 sc->unit); 777 return (EIO); 778 } 779 } 780 781 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 782 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit); 783 784 /* enable the aux port and interrupt */ 785 if (!set_controller_command_byte(sc->kbdc, 786 kbdc_get_device_mask(sc->kbdc), 787 (command_byte & KBD_KBD_CONTROL_BITS) 788 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) { 789 /* CONTROLLER ERROR */ 790 disable_aux_dev(sc->kbdc); 791 restore_controller(sc->kbdc, command_byte); 792 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n", 793 sc->unit); 794 return (EIO); 795 } 796 797 /* start the watchdog timer */ 798 sc->watchdog = FALSE; 799 callout_reset(&sc->callout, hz * 2, psmtimeout, (void *)(uintptr_t)sc); 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 callout_stop(&sc->callout); 819 820 /* save the current controller command byte */ 821 empty_both_buffers(sc->kbdc, 10); 822 c = get_controller_command_byte(sc->kbdc); 823 if (verbose >= 2) 824 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n", 825 sc->unit, c); 826 827 /* enable the aux port but disable the aux interrupt and the keyboard */ 828 if ((c == -1) || !set_controller_command_byte(sc->kbdc, 829 kbdc_get_device_mask(sc->kbdc), 830 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 831 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 832 /* CONTROLLER ERROR */ 833 splx(s); 834 kbdc_lock(sc->kbdc, FALSE); 835 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n", 836 sc->unit); 837 return (EIO); 838 } 839 840 /* flush any data */ 841 if (sc->state & PSM_VALID) { 842 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */ 843 empty_aux_buffer(sc->kbdc, 10); 844 } 845 sc->inputbytes = 0; 846 sc->syncerrors = 0; 847 848 /* try to detect the aux device; are you still there? */ 849 err = 0; 850 if (doinit) { 851 if (doinitialize(sc, &sc->mode)) { 852 /* yes */ 853 sc->state |= PSM_VALID; 854 } else { 855 /* the device has gone! */ 856 restore_controller(sc->kbdc, c); 857 sc->state &= ~PSM_VALID; 858 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n", 859 sc->unit); 860 err = ENXIO; 861 } 862 } 863 splx(s); 864 865 /* restore the driver state */ 866 if ((sc->state & PSM_OPEN) && (err == 0)) { 867 /* enable the aux device and the port again */ 868 err = doopen(sc, c); 869 if (err != 0) 870 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n", 871 sc->unit); 872 } else { 873 /* restore the keyboard port and disable the aux port */ 874 if (!set_controller_command_byte(sc->kbdc, 875 kbdc_get_device_mask(sc->kbdc), 876 (c & KBD_KBD_CONTROL_BITS) 877 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 878 /* CONTROLLER ERROR */ 879 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n", 880 sc->unit); 881 err = EIO; 882 } 883 } 884 885 kbdc_lock(sc->kbdc, FALSE); 886 return (err); 887 } 888 889 /* psm driver entry points */ 890 891 #define endprobe(v) { if (bootverbose) \ 892 --verbose; \ 893 kbdc_set_device_mask(sc->kbdc, mask); \ 894 kbdc_lock(sc->kbdc, FALSE); \ 895 return (v); \ 896 } 897 898 static int 899 psmprobe(device_t dev) 900 { 901 int unit = device_get_unit(dev); 902 struct psm_softc *sc = device_get_softc(dev); 903 uintptr_t irq; 904 uintptr_t flags; 905 int stat[3]; 906 int command_byte; 907 int mask; 908 int rid; 909 int i; 910 911 #if 0 912 kbdc_debug(TRUE); 913 #endif 914 915 #if notyet 916 /* check PnP IDs */ 917 if (XXX_PNP_PROBE(device_get_parent(dev), dev, psm_ids) == ENXIO) 918 return ENXIO; 919 #endif 920 921 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq); 922 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags); 923 924 sc->unit = unit; 925 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev))); 926 sc->config = flags & PSM_CONFIG_FLAGS; 927 /* XXX: for backward compatibility */ 928 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM) 929 sc->config |= 930 #ifdef PSM_RESETAFTERSUSPEND 931 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 932 #else 933 PSM_CONFIG_HOOKRESUME; 934 #endif 935 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */ 936 sc->flags = 0; 937 if (bootverbose) 938 ++verbose; 939 940 device_set_desc(dev, "PS/2 Mouse"); 941 942 if (!kbdc_lock(sc->kbdc, TRUE)) { 943 printf("psm%d: unable to lock the controller.\n", unit); 944 if (bootverbose) 945 --verbose; 946 return (ENXIO); 947 } 948 949 /* 950 * NOTE: two bits in the command byte controls the operation of the 951 * aux port (mouse port): the aux port disable bit (bit 5) and the aux 952 * port interrupt (IRQ 12) enable bit (bit 2). 953 */ 954 955 /* discard anything left after the keyboard initialization */ 956 empty_both_buffers(sc->kbdc, 10); 957 958 /* save the current command byte; it will be used later */ 959 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS; 960 command_byte = get_controller_command_byte(sc->kbdc); 961 if (verbose) 962 printf("psm%d: current command byte:%04x\n", unit, command_byte); 963 if (command_byte == -1) { 964 /* CONTROLLER ERROR */ 965 printf("psm%d: unable to get the current command byte value.\n", 966 unit); 967 endprobe(ENXIO); 968 } 969 970 /* 971 * disable the keyboard port while probing the aux port, which must be 972 * enabled during this routine 973 */ 974 if (!set_controller_command_byte(sc->kbdc, 975 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 976 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 977 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 978 /* 979 * this is CONTROLLER ERROR; I don't know how to recover 980 * from this error... 981 */ 982 restore_controller(sc->kbdc, command_byte); 983 printf("psm%d: unable to set the command byte.\n", unit); 984 endprobe(ENXIO); 985 } 986 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT); 987 988 /* 989 * NOTE: `test_aux_port()' is designed to return with zero if the aux 990 * port exists and is functioning. However, some controllers appears 991 * to respond with zero even when the aux port doesn't exist. (It may 992 * be that this is only the case when the controller DOES have the aux 993 * port but the port is not wired on the motherboard.) The keyboard 994 * controllers without the port, such as the original AT, are 995 * supporsed to return with an error code or simply time out. In any 996 * case, we have to continue probing the port even when the controller 997 * passes this test. 998 * 999 * XXX: some controllers erroneously return the error code 1 when 1000 * it has the perfectly functional aux port. We have to ignore this 1001 * error code. Even if the controller HAS error with the aux port, 1002 * it will be detected later... 1003 * XXX: another incompatible controller returns PSM_ACK (0xfa)... 1004 */ 1005 switch ((i = test_aux_port(sc->kbdc))) { 1006 case 1: /* ignore this error */ 1007 case 2: /* Ignore 2 and 3 for use with some acer and compal laptops */ 1008 case 3: 1009 case PSM_ACK: 1010 if (verbose) 1011 printf("psm%d: strange result for test aux port (%d).\n", 1012 unit, i); 1013 /* fall though */ 1014 case 0: /* no error */ 1015 break; 1016 case -1: /* time out */ 1017 default: /* error */ 1018 recover_from_error(sc->kbdc); 1019 if (sc->config & PSM_CONFIG_IGNPORTERROR) 1020 break; 1021 restore_controller(sc->kbdc, command_byte); 1022 if (verbose) 1023 printf("psm%d: the aux port is not functioning (%d).\n", 1024 unit, i); 1025 endprobe(ENXIO); 1026 } 1027 1028 if (sc->config & PSM_CONFIG_NORESET) { 1029 /* 1030 * Don't try to reset the pointing device. It may possibly be 1031 * left in the unknown state, though... 1032 */ 1033 } else { 1034 /* 1035 * NOTE: some controllers appears to hang the `keyboard' when the aux 1036 * port doesn't exist and `PSMC_RESET_DEV' is issued. 1037 * 1038 * Attempt to reset the controller twice -- this helps 1039 * pierce through some KVM switches. The second reset 1040 * is non-fatal. 1041 */ 1042 if (!reset_aux_dev(sc->kbdc)) { 1043 recover_from_error(sc->kbdc); 1044 restore_controller(sc->kbdc, command_byte); 1045 if (verbose) 1046 printf("psm%d: failed to reset the aux device.\n", unit); 1047 endprobe(ENXIO); 1048 } else if (!reset_aux_dev(sc->kbdc)) { 1049 recover_from_error(sc->kbdc); 1050 if (verbose >= 2) 1051 printf("psm%d: failed to reset the aux device (2).\n", 1052 unit); 1053 } 1054 } 1055 1056 /* 1057 * both the aux port and the aux device is functioning, see if the 1058 * device can be enabled. NOTE: when enabled, the device will start 1059 * sending data; we shall immediately disable the device once we know 1060 * the device can be enabled. 1061 */ 1062 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) { 1063 /* MOUSE ERROR */ 1064 recover_from_error(sc->kbdc); 1065 restore_controller(sc->kbdc, command_byte); 1066 if (verbose) 1067 printf("psm%d: failed to enable the aux device.\n", unit); 1068 endprobe(ENXIO); 1069 } 1070 1071 /* save the default values after reset */ 1072 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) { 1073 sc->dflt_mode.rate = sc->mode.rate = stat[2]; 1074 sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 1075 } else { 1076 sc->dflt_mode.rate = sc->mode.rate = -1; 1077 sc->dflt_mode.resolution = sc->mode.resolution = -1; 1078 } 1079 1080 /* hardware information */ 1081 sc->hw.iftype = MOUSE_IF_PS2; 1082 1083 /* verify the device is a mouse */ 1084 sc->hw.hwid = get_aux_id(sc->kbdc); 1085 if (!is_a_mouse(sc->hw.hwid)) { 1086 restore_controller(sc->kbdc, command_byte); 1087 if (verbose) 1088 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid); 1089 endprobe(ENXIO); 1090 } 1091 switch (sc->hw.hwid) { 1092 case PSM_BALLPOINT_ID: 1093 sc->hw.type = MOUSE_TRACKBALL; 1094 break; 1095 case PSM_MOUSE_ID: 1096 case PSM_INTELLI_ID: 1097 case PSM_EXPLORER_ID: 1098 case PSM_4DMOUSE_ID: 1099 case PSM_4DPLUS_ID: 1100 sc->hw.type = MOUSE_MOUSE; 1101 break; 1102 default: 1103 sc->hw.type = MOUSE_UNKNOWN; 1104 break; 1105 } 1106 1107 if (sc->config & PSM_CONFIG_NOIDPROBE) { 1108 sc->hw.buttons = 2; 1109 i = GENERIC_MOUSE_ENTRY; 1110 } else { 1111 /* # of buttons */ 1112 sc->hw.buttons = get_mouse_buttons(sc->kbdc); 1113 1114 /* other parameters */ 1115 for (i = 0; vendortype[i].probefunc != NULL; ++i) { 1116 if ((*vendortype[i].probefunc)(sc)) { 1117 if (verbose >= 2) 1118 printf("psm%d: found %s\n", 1119 unit, model_name(vendortype[i].model)); 1120 break; 1121 } 1122 } 1123 } 1124 1125 sc->hw.model = vendortype[i].model; 1126 1127 sc->dflt_mode.level = PSM_LEVEL_BASE; 1128 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE; 1129 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4; 1130 if (sc->config & PSM_CONFIG_NOCHECKSYNC) 1131 sc->dflt_mode.syncmask[0] = 0; 1132 else 1133 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask; 1134 if (sc->config & PSM_CONFIG_FORCETAP) 1135 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP; 1136 sc->dflt_mode.syncmask[1] = 0; /* syncbits */ 1137 sc->mode = sc->dflt_mode; 1138 sc->mode.packetsize = vendortype[i].packetsize; 1139 1140 /* set mouse parameters */ 1141 #if 0 1142 /* 1143 * A version of Logitech FirstMouse+ won't report wheel movement, 1144 * if SET_DEFAULTS is sent... Don't use this command. 1145 * This fix was found by Takashi Nishida. 1146 */ 1147 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS); 1148 if (verbose >= 2) 1149 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i); 1150 #endif 1151 if (sc->config & PSM_CONFIG_RESOLUTION) { 1152 sc->mode.resolution 1153 = set_mouse_resolution(sc->kbdc, 1154 (sc->config & PSM_CONFIG_RESOLUTION) - 1); 1155 } else if (sc->mode.resolution >= 0) { 1156 sc->mode.resolution 1157 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution); 1158 } 1159 if (sc->mode.rate > 0) { 1160 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate); 1161 } 1162 set_mouse_scaling(sc->kbdc, 1); 1163 1164 /* request a data packet and extract sync. bits */ 1165 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) { 1166 printf("psm%d: failed to get data.\n", unit); 1167 sc->mode.syncmask[0] = 0; 1168 } else { 1169 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 1170 /* the NetScroll Mouse will send three more bytes... Ignore them */ 1171 empty_aux_buffer(sc->kbdc, 5); 1172 } 1173 1174 /* just check the status of the mouse */ 1175 /* 1176 * NOTE: XXX there are some arcane controller/mouse combinations out 1177 * there, which hung the controller unless there is data transmission 1178 * after ACK from the mouse. 1179 */ 1180 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) { 1181 printf("psm%d: failed to get status.\n", unit); 1182 } else { 1183 /* 1184 * When in its native mode, some mice operate with different 1185 * default parameters than in the PS/2 compatible mode. 1186 */ 1187 sc->dflt_mode.rate = sc->mode.rate = stat[2]; 1188 sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 1189 } 1190 1191 /* disable the aux port for now... */ 1192 if (!set_controller_command_byte(sc->kbdc, 1193 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 1194 (command_byte & KBD_KBD_CONTROL_BITS) 1195 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1196 /* 1197 * this is CONTROLLER ERROR; I don't know the proper way to 1198 * recover from this error... 1199 */ 1200 restore_controller(sc->kbdc, command_byte); 1201 printf("psm%d: unable to set the command byte.\n", unit); 1202 endprobe(ENXIO); 1203 } 1204 1205 /* see if IRQ is available */ 1206 rid = 0; 1207 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1, 1208 RF_ACTIVE); 1209 if (sc->intr == NULL) { 1210 printf("psm%d: unable to allocate the IRQ resource (%d).\n", 1211 unit, irq); 1212 endprobe(ENXIO); 1213 } else { 1214 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 1215 } 1216 1217 /* done */ 1218 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS); 1219 kbdc_lock(sc->kbdc, FALSE); 1220 return (0); 1221 } 1222 1223 static int 1224 psmattach(device_t dev) 1225 { 1226 int unit = device_get_unit(dev); 1227 struct psm_softc *sc = device_get_softc(dev); 1228 uintptr_t irq; 1229 int error; 1230 int rid; 1231 1232 if (sc == NULL) /* shouldn't happen */ 1233 return (ENXIO); 1234 1235 /* Setup initial state */ 1236 sc->state = PSM_VALID; 1237 callout_init(&sc->callout); 1238 1239 /* Setup our interrupt handler */ 1240 rid = 0; 1241 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq); 1242 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1, 1243 RF_ACTIVE); 1244 if (sc->intr == NULL) 1245 return (ENXIO); 1246 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr, 1247 INTR_TYPE_TTY, psmintr, sc, &sc->ih); 1248 if (error) { 1249 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 1250 return (error); 1251 } 1252 1253 /* Done */ 1254 cdevsw_add(&psm_cdevsw, PSM_MKMINOR(-1, 0), PSM_MKMINOR(unit, 0)); 1255 make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, "psm%d", unit); 1256 make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, "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 int unit; 1283 1284 sc = device_get_softc(dev); 1285 if (sc->state & PSM_OPEN) 1286 return EBUSY; 1287 1288 unit = device_get_unit(dev); 1289 1290 rid = 0; 1291 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih); 1292 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 1293 cdevsw_remove(&psm_cdevsw, PSM_MKMINOR(-1, 0), PSM_MKMINOR(unit, 0)); 1294 1295 return 0; 1296 } 1297 1298 static int 1299 psmopen(dev_t dev, int flag, int fmt, struct thread *td) 1300 { 1301 int unit = PSM_UNIT(dev); 1302 struct psm_softc *sc; 1303 int command_byte; 1304 int err; 1305 int s; 1306 1307 /* Get device data */ 1308 sc = PSM_SOFTC(unit); 1309 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) 1310 /* the device is no longer valid/functioning */ 1311 return (ENXIO); 1312 1313 /* Disallow multiple opens */ 1314 if (sc->state & PSM_OPEN) 1315 return (EBUSY); 1316 1317 device_busy(devclass_get_device(psm_devclass, unit)); 1318 1319 /* Initialize state */ 1320 sc->rsel.si_flags = 0; 1321 sc->rsel.si_pid = 0; 1322 sc->mode.level = sc->dflt_mode.level; 1323 sc->mode.protocol = sc->dflt_mode.protocol; 1324 sc->watchdog = FALSE; 1325 1326 /* flush the event queue */ 1327 sc->queue.count = 0; 1328 sc->queue.head = 0; 1329 sc->queue.tail = 0; 1330 sc->status.flags = 0; 1331 sc->status.button = 0; 1332 sc->status.obutton = 0; 1333 sc->status.dx = 0; 1334 sc->status.dy = 0; 1335 sc->status.dz = 0; 1336 sc->button = 0; 1337 1338 /* empty input buffer */ 1339 bzero(sc->ipacket, sizeof(sc->ipacket)); 1340 sc->inputbytes = 0; 1341 sc->syncerrors = 0; 1342 1343 /* don't let timeout routines in the keyboard driver to poll the kbdc */ 1344 if (!kbdc_lock(sc->kbdc, TRUE)) 1345 return (EIO); 1346 1347 /* save the current controller command byte */ 1348 s = spltty(); 1349 command_byte = get_controller_command_byte(sc->kbdc); 1350 1351 /* enable the aux port and temporalily disable the keyboard */ 1352 if ((command_byte == -1) 1353 || !set_controller_command_byte(sc->kbdc, 1354 kbdc_get_device_mask(sc->kbdc), 1355 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 1356 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1357 /* CONTROLLER ERROR; do you know how to get out of this? */ 1358 kbdc_lock(sc->kbdc, FALSE); 1359 splx(s); 1360 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n", 1361 unit); 1362 return (EIO); 1363 } 1364 /* 1365 * Now that the keyboard controller is told not to generate 1366 * the keyboard and mouse interrupts, call `splx()' to allow 1367 * the other tty interrupts. The clock interrupt may also occur, 1368 * but timeout routines will be blocked by the poll flag set 1369 * via `kbdc_lock()' 1370 */ 1371 splx(s); 1372 1373 /* enable the mouse device */ 1374 err = doopen(sc, command_byte); 1375 1376 /* done */ 1377 if (err == 0) 1378 sc->state |= PSM_OPEN; 1379 kbdc_lock(sc->kbdc, FALSE); 1380 return (err); 1381 } 1382 1383 static int 1384 psmclose(dev_t dev, int flag, int fmt, struct thread *td) 1385 { 1386 int unit = PSM_UNIT(dev); 1387 struct psm_softc *sc = PSM_SOFTC(unit); 1388 int stat[3]; 1389 int command_byte; 1390 int s; 1391 1392 /* don't let timeout routines in the keyboard driver to poll the kbdc */ 1393 if (!kbdc_lock(sc->kbdc, TRUE)) 1394 return (EIO); 1395 1396 /* save the current controller command byte */ 1397 s = spltty(); 1398 command_byte = get_controller_command_byte(sc->kbdc); 1399 if (command_byte == -1) { 1400 kbdc_lock(sc->kbdc, FALSE); 1401 splx(s); 1402 return (EIO); 1403 } 1404 1405 /* disable the aux interrupt and temporalily disable the keyboard */ 1406 if (!set_controller_command_byte(sc->kbdc, 1407 kbdc_get_device_mask(sc->kbdc), 1408 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 1409 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1410 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n", 1411 unit); 1412 /* CONTROLLER ERROR; 1413 * NOTE: we shall force our way through. Because the only 1414 * ill effect we shall see is that we may not be able 1415 * to read ACK from the mouse, and it doesn't matter much 1416 * so long as the mouse will accept the DISABLE command. 1417 */ 1418 } 1419 splx(s); 1420 1421 /* stop the watchdog timer */ 1422 callout_stop(&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 callout_reset(&sc->callout, hz, psmtimeout, (void *)(uintptr_t)sc); 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