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