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