psm.c revision 132270
141016Sdfr/*- 241016Sdfr * Copyright (c) 1992, 1993 Erik Forsberg. 341016Sdfr * Copyright (c) 1996, 1997 Kazutaka YOKOTA. 441016Sdfr * All rights reserved. 541016Sdfr * 641016Sdfr * Redistribution and use in source and binary forms, with or without 741016Sdfr * modification, are permitted provided that the following conditions 841016Sdfr * are met: 941016Sdfr * 1. Redistributions of source code must retain the above copyright 1041016Sdfr * notice, this list of conditions and the following disclaimer. 1141016Sdfr * 1241016Sdfr * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED 1341016Sdfr * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 1441016Sdfr * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 1541016Sdfr * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1641016Sdfr * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1741016Sdfr * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1841016Sdfr * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1941016Sdfr * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2041016Sdfr * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 2141016Sdfr * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2241016Sdfr */ 2341016Sdfr/* 2441016Sdfr * Ported to 386bsd Oct 17, 1992 2541016Sdfr * Sandi Donno, Computer Science, University of Cape Town, South Africa 2641016Sdfr * Please send bug reports to sandi@cs.uct.ac.za 2741016Sdfr * 2841016Sdfr * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca - 2941016Sdfr * although I was only partially successful in getting the alpha release 3041016Sdfr * of his "driver for the Logitech and ATI Inport Bus mice for use with 3141016Sdfr * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless 3241016Sdfr * found his code to be an invaluable reference when porting this driver 3341016Sdfr * to 386bsd. 3441016Sdfr * 3541016Sdfr * Further modifications for latest 386BSD+patchkit and port to NetBSD, 3641016Sdfr * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993 3741016Sdfr * 3841016Sdfr * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by 3941016Sdfr * Andrew Herbert - 12 June 1993 4041016Sdfr * 4141016Sdfr * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu> 4241016Sdfr * - 13 June 1993 4341016Sdfr * 4441016Sdfr * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp> 4541016Sdfr * - 24 October 1993 4641016Sdfr * 4741016Sdfr * Hardware access routines and probe logic rewritten by 4841016Sdfr * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp> 4941016Sdfr * - 3, 14, 22 October 1996. 5041016Sdfr * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'... 5141016Sdfr * - 14, 30 November 1996. Uses `kbdio.c'. 5241016Sdfr * - 13 December 1996. Uses queuing version of `kbdio.c'. 5341016Sdfr * - January/February 1997. Tweaked probe logic for 5441016Sdfr * HiNote UltraII/Latitude/Armada laptops. 5541016Sdfr * - 30 July 1997. Added APM support. 5641016Sdfr * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX). 5741016Sdfr * Improved sync check logic. 5841016Sdfr * Vendor specific support routines. 5941016Sdfr */ 6041016Sdfr 61116181Sobrien#include <sys/cdefs.h> 62116181Sobrien__FBSDID("$FreeBSD: head/sys/dev/atkbdc/psm.c 132270 2004-07-16 22:04:29Z mux $"); 63116181Sobrien 6441016Sdfr#include "opt_psm.h" 6541016Sdfr 6641016Sdfr#include <sys/param.h> 6741016Sdfr#include <sys/systm.h> 6841016Sdfr#include <sys/kernel.h> 6941016Sdfr#include <sys/module.h> 7041016Sdfr#include <sys/bus.h> 7141016Sdfr#include <sys/conf.h> 7241016Sdfr#include <sys/poll.h> 7341016Sdfr#include <sys/syslog.h> 7445720Speter#include <machine/bus.h> 7541181Sdfr#include <sys/rman.h> 7670834Swollman#include <sys/selinfo.h> 77123442Salfred#include <sys/sysctl.h> 7884880Syokota#include <sys/time.h> 7941016Sdfr#include <sys/uio.h> 8041016Sdfr 81114216Skan#include <sys/limits.h> 8266860Sphk#include <sys/mouse.h> 8341181Sdfr#include <machine/resource.h> 8441016Sdfr 8541016Sdfr#include <isa/isavar.h> 8643105Sdfr#include <dev/kbd/atkbdcreg.h> 8741016Sdfr 8841016Sdfr/* 8941016Sdfr * Driver specific options: the following options may be set by 9041016Sdfr * `options' statements in the kernel configuration file. 9141016Sdfr */ 9241016Sdfr 9341016Sdfr/* debugging */ 9441016Sdfr#ifndef PSM_DEBUG 9541016Sdfr#define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */ 9641016Sdfr#endif 9741016Sdfr 9884880Syokota#ifndef PSM_SYNCERR_THRESHOLD1 9984880Syokota#define PSM_SYNCERR_THRESHOLD1 20 10084880Syokota#endif 10184880Syokota 10284880Syokota#ifndef PSM_INPUT_TIMEOUT 10384880Syokota#define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */ 10484880Syokota#endif 10584880Syokota 10641016Sdfr/* end of driver specific options */ 10741016Sdfr 10883492Syokota#define PSM_DRIVER_NAME "psm" 10983931Syokota#define PSMCPNP_DRIVER_NAME "psmcpnp" 11083492Syokota 11141016Sdfr/* input queue */ 11241016Sdfr#define PSM_BUFSIZE 960 11341016Sdfr#define PSM_SMALLBUFSIZE 240 11441016Sdfr 11541016Sdfr/* operation levels */ 11641016Sdfr#define PSM_LEVEL_BASE 0 11741016Sdfr#define PSM_LEVEL_STANDARD 1 11841016Sdfr#define PSM_LEVEL_NATIVE 2 11941016Sdfr#define PSM_LEVEL_MIN PSM_LEVEL_BASE 12041016Sdfr#define PSM_LEVEL_MAX PSM_LEVEL_NATIVE 12141016Sdfr 12248778Syokota/* Logitech PS2++ protocol */ 12348778Syokota#define MOUSE_PS2PLUS_CHECKBITS(b) \ 12448778Syokota ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f)) 12548778Syokota#define MOUSE_PS2PLUS_PACKET_TYPE(b) \ 12648778Syokota (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4)) 12748778Syokota 12841016Sdfr/* some macros */ 12941016Sdfr#define PSM_UNIT(dev) (minor(dev) >> 1) 13041016Sdfr#define PSM_NBLOCKIO(dev) (minor(dev) & 1) 13141016Sdfr#define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1)) 13241016Sdfr 13341016Sdfr/* ring buffer */ 13441016Sdfrtypedef struct ringbuf { 13541016Sdfr int count; /* # of valid elements in the buffer */ 13641016Sdfr int head; /* head pointer */ 13741016Sdfr int tail; /* tail poiner */ 13841016Sdfr unsigned char buf[PSM_BUFSIZE]; 13941016Sdfr} ringbuf_t; 14041016Sdfr 141123442Salfred/* data buffer */ 142123442Salfredtypedef struct packetbuf { 143123442Salfred unsigned char ipacket[16]; /* interim input buffer */ 144123442Salfred int inputbytes; /* # of bytes in the input buffer */ 145123442Salfred} packetbuf_t; 146123442Salfred 147123442Salfred#ifndef PSM_PACKETQUEUE 148123442Salfred#define PSM_PACKETQUEUE 128 149123442Salfred#endif 150123442Salfred 15141016Sdfr/* driver control block */ 15241016Sdfrstruct psm_softc { /* Driver status information */ 15384880Syokota int unit; 15441016Sdfr struct selinfo rsel; /* Process selecting for Input */ 15541016Sdfr unsigned char state; /* Mouse driver state */ 15641016Sdfr int config; /* driver configuration flags */ 15741016Sdfr int flags; /* other flags */ 15841016Sdfr KBDC kbdc; /* handle to access the keyboard controller */ 15958230Syokota struct resource *intr; /* IRQ resource */ 16058230Syokota void *ih; /* interrupt handle */ 16141016Sdfr mousehw_t hw; /* hardware information */ 16241016Sdfr mousemode_t mode; /* operation mode */ 16341016Sdfr mousemode_t dflt_mode; /* default operation mode */ 16441016Sdfr mousestatus_t status; /* accumulated mouse movement */ 16541016Sdfr ringbuf_t queue; /* mouse status queue */ 166123442Salfred packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */ 167123442Salfred int pqueue_start; /* start of data in queue */ 168123442Salfred int pqueue_end; /* end of data in queue */ 16941016Sdfr int button; /* the latest button state */ 17049965Syokota int xold; /* previous absolute X position */ 17149965Syokota int yold; /* previous absolute Y position */ 172123442Salfred int syncerrors; /* XXX: KILL ME! */ 17384880Syokota struct timeval inputtimeout; 174123442Salfred struct timeval lastsoftintr; /* time of last soft interrupt */ 175123442Salfred struct timeval lastinputerr; /* time last sync error happened */ 17658230Syokota int watchdog; /* watchdog timer flag */ 17758230Syokota struct callout_handle callout; /* watchdog timer call out */ 178123442Salfred struct callout_handle softcallout; /* buffer timer call out */ 179130585Sphk struct cdev *dev; 180130585Sphk struct cdev *bdev; 181123442Salfred int lasterr; 182123442Salfred int cmdcount; 18341016Sdfr}; 184114293Smarkmstatic devclass_t psm_devclass; 18541016Sdfr#define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit)) 18641016Sdfr 18741016Sdfr/* driver state flags (state) */ 18841016Sdfr#define PSM_VALID 0x80 18941016Sdfr#define PSM_OPEN 1 /* Device is open */ 19041016Sdfr#define PSM_ASLP 2 /* Waiting for mouse data */ 191123442Salfred#define PSM_SOFTARMED 4 /* Software interrupt armed */ 19241016Sdfr 19341016Sdfr/* driver configuration flags (config) */ 19441016Sdfr#define PSM_CONFIG_RESOLUTION 0x000f /* resolution */ 19541016Sdfr#define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */ 19641016Sdfr#define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */ 19745789Speter#define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */ 19845789Speter#define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */ 19945789Speter#define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */ 20045789Speter#define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */ 20158230Syokota#define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */ 20258230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */ 20369439Syokota#define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */ 20441016Sdfr 20541016Sdfr#define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \ 20641016Sdfr | PSM_CONFIG_ACCEL \ 20745789Speter | PSM_CONFIG_NOCHECKSYNC \ 20869439Syokota | PSM_CONFIG_SYNCHACK \ 20945789Speter | PSM_CONFIG_NOIDPROBE \ 21045789Speter | PSM_CONFIG_NORESET \ 21145789Speter | PSM_CONFIG_FORCETAP \ 21258230Syokota | PSM_CONFIG_IGNPORTERROR \ 21358230Syokota | PSM_CONFIG_HOOKRESUME \ 21458230Syokota | PSM_CONFIG_INITAFTERSUSPEND) 21541016Sdfr 21641016Sdfr/* other flags (flags) */ 21749965Syokota#define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */ 21841016Sdfr 21941016Sdfr/* for backward compatibility */ 22041016Sdfr#define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t) 22141016Sdfr#define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t) 22241016Sdfr#define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t) 22341016Sdfr 22441016Sdfrtypedef struct old_mousehw { 22541016Sdfr int buttons; 22641016Sdfr int iftype; 22741016Sdfr int type; 22841016Sdfr int hwid; 22941016Sdfr} old_mousehw_t; 23041016Sdfr 23141016Sdfrtypedef struct old_mousemode { 23241016Sdfr int protocol; 23341016Sdfr int rate; 23441016Sdfr int resolution; 23541016Sdfr int accelfactor; 23641016Sdfr} old_mousemode_t; 23741016Sdfr 23841016Sdfr/* packet formatting function */ 23992756Salfredtypedef int packetfunc_t(struct psm_softc *, unsigned char *, 24092756Salfred int *, int, mousestatus_t *); 24141016Sdfr 24241016Sdfr/* function prototypes */ 24392756Salfredstatic void psmidentify(driver_t *, device_t); 24492756Salfredstatic int psmprobe(device_t); 24592756Salfredstatic int psmattach(device_t); 24692756Salfredstatic int psmdetach(device_t); 24792756Salfredstatic int psmresume(device_t); 24841016Sdfr 24941016Sdfrstatic d_open_t psmopen; 25041016Sdfrstatic d_close_t psmclose; 25141016Sdfrstatic d_read_t psmread; 25241016Sdfrstatic d_ioctl_t psmioctl; 25341016Sdfrstatic d_poll_t psmpoll; 25441016Sdfr 25592756Salfredstatic int enable_aux_dev(KBDC); 25692756Salfredstatic int disable_aux_dev(KBDC); 25792756Salfredstatic int get_mouse_status(KBDC, int *, int, int); 25892756Salfredstatic int get_aux_id(KBDC); 25992756Salfredstatic int set_mouse_sampling_rate(KBDC, int); 26092756Salfredstatic int set_mouse_scaling(KBDC, int); 26192756Salfredstatic int set_mouse_resolution(KBDC, int); 26292756Salfredstatic int set_mouse_mode(KBDC); 26392756Salfredstatic int get_mouse_buttons(KBDC); 26492756Salfredstatic int is_a_mouse(int); 26592756Salfredstatic void recover_from_error(KBDC); 26692756Salfredstatic int restore_controller(KBDC, int); 26792756Salfredstatic int doinitialize(struct psm_softc *, mousemode_t *); 26892756Salfredstatic int doopen(struct psm_softc *, int); 26992756Salfredstatic int reinitialize(struct psm_softc *, int); 27092756Salfredstatic char *model_name(int); 271123442Salfredstatic void psmsoftintr(void *); 27292756Salfredstatic void psmintr(void *); 27392756Salfredstatic void psmtimeout(void *); 274123442Salfredstatic int timeelapsed(const struct timeval *, 275123442Salfred int, int, const struct timeval *); 276123442Salfredstatic void dropqueue(struct psm_softc *); 277123442Salfredstatic void flushpackets(struct psm_softc *); 27841016Sdfr 27941016Sdfr/* vendor specific features */ 28092756Salfredtypedef int probefunc_t(struct psm_softc *); 28141016Sdfr 28292756Salfredstatic int mouse_id_proc1(KBDC, int, int, int *); 28392756Salfredstatic int mouse_ext_command(KBDC, int); 28441016Sdfrstatic probefunc_t enable_groller; 28541016Sdfrstatic probefunc_t enable_gmouse; 28641016Sdfrstatic probefunc_t enable_aglide; 28741016Sdfrstatic probefunc_t enable_kmouse; 28858230Syokotastatic probefunc_t enable_msexplorer; 28941016Sdfrstatic probefunc_t enable_msintelli; 29058230Syokotastatic probefunc_t enable_4dmouse; 29158230Syokotastatic probefunc_t enable_4dplus; 29241016Sdfrstatic probefunc_t enable_mmanplus; 29349965Syokotastatic probefunc_t enable_versapad; 294123442Salfredstatic int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, unsigned char *); 29541016Sdfr 29641016Sdfrstatic struct { 29741016Sdfr int model; 29841016Sdfr unsigned char syncmask; 29941016Sdfr int packetsize; 30041016Sdfr probefunc_t *probefunc; 30141016Sdfr} vendortype[] = { 30258230Syokota /* 30358230Syokota * WARNING: the order of probe is very important. Don't mess it 30458230Syokota * unless you know what you are doing. 30558230Syokota */ 30641016Sdfr { MOUSE_MODEL_NET, /* Genius NetMouse */ 30758230Syokota 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, }, 30841016Sdfr { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */ 30941016Sdfr 0xc8, 6, enable_groller, }, 31058230Syokota { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */ 31158230Syokota 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, }, 31258230Syokota { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */ 31358230Syokota 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, }, 31458230Syokota { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */ 31558230Syokota 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, }, 31658230Syokota { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */ 31758230Syokota 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, }, 31858230Syokota { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */ 31958230Syokota 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, }, 32041016Sdfr { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */ 32141016Sdfr 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, }, 32241016Sdfr { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */ 32341016Sdfr 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, }, 32449965Syokota { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */ 32549965Syokota 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, }, 32641016Sdfr { MOUSE_MODEL_GENERIC, 32741016Sdfr 0xc0, MOUSE_PS2_PACKETSIZE, NULL, }, 32841016Sdfr}; 32963951Syokota#define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1) 33041016Sdfr 33141016Sdfr/* device driver declarateion */ 33241016Sdfrstatic device_method_t psm_methods[] = { 33341016Sdfr /* Device interface */ 33483147Syokota DEVMETHOD(device_identify, psmidentify), 33541016Sdfr DEVMETHOD(device_probe, psmprobe), 33641016Sdfr DEVMETHOD(device_attach, psmattach), 33758230Syokota DEVMETHOD(device_detach, psmdetach), 33854629Syokota DEVMETHOD(device_resume, psmresume), 33941016Sdfr 34041016Sdfr { 0, 0 } 34141016Sdfr}; 34241016Sdfr 34341016Sdfrstatic driver_t psm_driver = { 34483492Syokota PSM_DRIVER_NAME, 34541016Sdfr psm_methods, 34641016Sdfr sizeof(struct psm_softc), 34741016Sdfr}; 34841016Sdfr 34941016Sdfr 35047625Sphkstatic struct cdevsw psm_cdevsw = { 351126080Sphk .d_version = D_VERSION, 352126080Sphk .d_flags = D_NEEDGIANT, 353111815Sphk .d_open = psmopen, 354111815Sphk .d_close = psmclose, 355111815Sphk .d_read = psmread, 356111815Sphk .d_ioctl = psmioctl, 357111815Sphk .d_poll = psmpoll, 358111815Sphk .d_name = PSM_DRIVER_NAME, 35941016Sdfr}; 36041016Sdfr 36141016Sdfr/* debug message level */ 36241016Sdfrstatic int verbose = PSM_DEBUG; 36341016Sdfr 36441016Sdfr/* device I/O routines */ 36541016Sdfrstatic int 36641016Sdfrenable_aux_dev(KBDC kbdc) 36741016Sdfr{ 36841016Sdfr int res; 36941016Sdfr 37041016Sdfr res = send_aux_command(kbdc, PSMC_ENABLE_DEV); 37141016Sdfr if (verbose >= 2) 37241016Sdfr log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res); 37341016Sdfr 37441016Sdfr return (res == PSM_ACK); 37541016Sdfr} 37641016Sdfr 37741016Sdfrstatic int 37841016Sdfrdisable_aux_dev(KBDC kbdc) 37941016Sdfr{ 38041016Sdfr int res; 38141016Sdfr 38241016Sdfr res = send_aux_command(kbdc, PSMC_DISABLE_DEV); 38341016Sdfr if (verbose >= 2) 38441016Sdfr log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res); 38541016Sdfr 38641016Sdfr return (res == PSM_ACK); 38741016Sdfr} 38841016Sdfr 38941016Sdfrstatic int 39041016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len) 39141016Sdfr{ 39241016Sdfr int cmd; 39341016Sdfr int res; 39441016Sdfr int i; 39541016Sdfr 39641016Sdfr switch (flag) { 39741016Sdfr case 0: 39841016Sdfr default: 39941016Sdfr cmd = PSMC_SEND_DEV_STATUS; 40041016Sdfr break; 40141016Sdfr case 1: 40241016Sdfr cmd = PSMC_SEND_DEV_DATA; 40341016Sdfr break; 40441016Sdfr } 40541016Sdfr empty_aux_buffer(kbdc, 5); 40641016Sdfr res = send_aux_command(kbdc, cmd); 40741016Sdfr if (verbose >= 2) 40841016Sdfr log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n", 40941016Sdfr (flag == 1) ? "DATA" : "STATUS", res); 41041016Sdfr if (res != PSM_ACK) 41141016Sdfr return 0; 41241016Sdfr 41341016Sdfr for (i = 0; i < len; ++i) { 41441016Sdfr status[i] = read_aux_data(kbdc); 41541016Sdfr if (status[i] < 0) 41641016Sdfr break; 41741016Sdfr } 41841016Sdfr 41941016Sdfr if (verbose) { 42041016Sdfr log(LOG_DEBUG, "psm: %s %02x %02x %02x\n", 42141016Sdfr (flag == 1) ? "data" : "status", status[0], status[1], status[2]); 42241016Sdfr } 42341016Sdfr 42441016Sdfr return i; 42541016Sdfr} 42641016Sdfr 42741016Sdfrstatic int 42841016Sdfrget_aux_id(KBDC kbdc) 42941016Sdfr{ 43041016Sdfr int res; 43141016Sdfr int id; 43241016Sdfr 43341016Sdfr empty_aux_buffer(kbdc, 5); 43441016Sdfr res = send_aux_command(kbdc, PSMC_SEND_DEV_ID); 43541016Sdfr if (verbose >= 2) 43641016Sdfr log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res); 43741016Sdfr if (res != PSM_ACK) 43841016Sdfr return (-1); 43941016Sdfr 44041016Sdfr /* 10ms delay */ 44141016Sdfr DELAY(10000); 44241016Sdfr 44341016Sdfr id = read_aux_data(kbdc); 44441016Sdfr if (verbose >= 2) 44541016Sdfr log(LOG_DEBUG, "psm: device ID: %04x\n", id); 44641016Sdfr 44741016Sdfr return id; 44841016Sdfr} 44941016Sdfr 45041016Sdfrstatic int 45141016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate) 45241016Sdfr{ 45341016Sdfr int res; 45441016Sdfr 45541016Sdfr res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate); 45641016Sdfr if (verbose >= 2) 45741016Sdfr log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res); 45841016Sdfr 45941016Sdfr return ((res == PSM_ACK) ? rate : -1); 46041016Sdfr} 46141016Sdfr 46241016Sdfrstatic int 46341016Sdfrset_mouse_scaling(KBDC kbdc, int scale) 46441016Sdfr{ 46541016Sdfr int res; 46641016Sdfr 46741016Sdfr switch (scale) { 46841016Sdfr case 1: 46941016Sdfr default: 47041016Sdfr scale = PSMC_SET_SCALING11; 47141016Sdfr break; 47241016Sdfr case 2: 47341016Sdfr scale = PSMC_SET_SCALING21; 47441016Sdfr break; 47541016Sdfr } 47641016Sdfr res = send_aux_command(kbdc, scale); 47741016Sdfr if (verbose >= 2) 47841016Sdfr log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n", 47941016Sdfr (scale == PSMC_SET_SCALING21) ? "21" : "11", res); 48041016Sdfr 48141016Sdfr return (res == PSM_ACK); 48241016Sdfr} 48341016Sdfr 48441016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */ 48541016Sdfrstatic int 48641016Sdfrset_mouse_resolution(KBDC kbdc, int val) 48741016Sdfr{ 48841016Sdfr int res; 48941016Sdfr 49041016Sdfr res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val); 49141016Sdfr if (verbose >= 2) 49241016Sdfr log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res); 49341016Sdfr 49441016Sdfr return ((res == PSM_ACK) ? val : -1); 49541016Sdfr} 49641016Sdfr 49741016Sdfr/* 49841016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be 49941016Sdfr * re-enabled by calling `enable_aux_dev()' 50041016Sdfr */ 50141016Sdfrstatic int 50241016Sdfrset_mouse_mode(KBDC kbdc) 50341016Sdfr{ 50441016Sdfr int res; 50541016Sdfr 50641016Sdfr res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE); 50741016Sdfr if (verbose >= 2) 50841016Sdfr log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res); 50941016Sdfr 51041016Sdfr return (res == PSM_ACK); 51141016Sdfr} 51241016Sdfr 51341016Sdfrstatic int 51441016Sdfrget_mouse_buttons(KBDC kbdc) 51541016Sdfr{ 51641016Sdfr int c = 2; /* assume two buttons by default */ 51741016Sdfr int status[3]; 51841016Sdfr 51941016Sdfr /* 52041016Sdfr * NOTE: a special sequence to obtain Logitech Mouse specific 52141016Sdfr * information: set resolution to 25 ppi, set scaling to 1:1, set 52241016Sdfr * scaling to 1:1, set scaling to 1:1. Then the second byte of the 52341016Sdfr * mouse status bytes is the number of available buttons. 52441016Sdfr * Some manufactures also support this sequence. 52541016Sdfr */ 52641016Sdfr if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 52741016Sdfr return c; 52841016Sdfr if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) 52941016Sdfr && set_mouse_scaling(kbdc, 1) 53041016Sdfr && (get_mouse_status(kbdc, status, 0, 3) >= 3)) { 53141016Sdfr if (status[1] != 0) 53241016Sdfr return status[1]; 53341016Sdfr } 53441016Sdfr return c; 53541016Sdfr} 53641016Sdfr 53741016Sdfr/* misc subroutines */ 53841016Sdfr/* 53941016Sdfr * Someday, I will get the complete list of valid pointing devices and 54041016Sdfr * their IDs... XXX 54141016Sdfr */ 54241016Sdfrstatic int 54341016Sdfris_a_mouse(int id) 54441016Sdfr{ 54541016Sdfr#if 0 54641016Sdfr static int valid_ids[] = { 54741016Sdfr PSM_MOUSE_ID, /* mouse */ 54841016Sdfr PSM_BALLPOINT_ID, /* ballpoint device */ 54941016Sdfr PSM_INTELLI_ID, /* Intellimouse */ 55058230Syokota PSM_EXPLORER_ID, /* Intellimouse Explorer */ 55141016Sdfr -1 /* end of table */ 55241016Sdfr }; 55341016Sdfr int i; 55441016Sdfr 55541016Sdfr for (i = 0; valid_ids[i] >= 0; ++i) 55641016Sdfr if (valid_ids[i] == id) 55741016Sdfr return TRUE; 55841016Sdfr return FALSE; 55941016Sdfr#else 56041016Sdfr return TRUE; 56141016Sdfr#endif 56241016Sdfr} 56341016Sdfr 56441016Sdfrstatic char * 56541016Sdfrmodel_name(int model) 56641016Sdfr{ 56741016Sdfr static struct { 56841016Sdfr int model_code; 56941016Sdfr char *model_name; 57041016Sdfr } models[] = { 57158230Syokota { MOUSE_MODEL_NETSCROLL, "NetScroll" }, 57258230Syokota { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" }, 57341016Sdfr { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" }, 57441016Sdfr { MOUSE_MODEL_THINK, "ThinkingMouse" }, 57541016Sdfr { MOUSE_MODEL_INTELLI, "IntelliMouse" }, 57641016Sdfr { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" }, 57749965Syokota { MOUSE_MODEL_VERSAPAD, "VersaPad" }, 57858230Syokota { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" }, 57958230Syokota { MOUSE_MODEL_4D, "4D Mouse" }, 58058230Syokota { MOUSE_MODEL_4DPLUS, "4D+ Mouse" }, 58141016Sdfr { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" }, 58241016Sdfr { MOUSE_MODEL_UNKNOWN, NULL }, 58341016Sdfr }; 58441016Sdfr int i; 58541016Sdfr 58641016Sdfr for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) { 58741016Sdfr if (models[i].model_code == model) 58841016Sdfr return models[i].model_name; 58941016Sdfr } 59041016Sdfr return "Unknown"; 59141016Sdfr} 59241016Sdfr 59341016Sdfrstatic void 59441016Sdfrrecover_from_error(KBDC kbdc) 59541016Sdfr{ 59641016Sdfr /* discard anything left in the output buffer */ 59741016Sdfr empty_both_buffers(kbdc, 10); 59841016Sdfr 59941016Sdfr#if 0 60041016Sdfr /* 60141016Sdfr * NOTE: KBDC_RESET_KBD may not restore the communication between the 60241016Sdfr * keyboard and the controller. 60341016Sdfr */ 60441016Sdfr reset_kbd(kbdc); 60541016Sdfr#else 60641016Sdfr /* 60741016Sdfr * NOTE: somehow diagnostic and keyboard port test commands bring the 60841016Sdfr * keyboard back. 60941016Sdfr */ 61041016Sdfr if (!test_controller(kbdc)) 61141016Sdfr log(LOG_ERR, "psm: keyboard controller failed.\n"); 61241016Sdfr /* if there isn't a keyboard in the system, the following error is OK */ 61341016Sdfr if (test_kbd_port(kbdc) != 0) { 61441016Sdfr if (verbose) 61541016Sdfr log(LOG_ERR, "psm: keyboard port failed.\n"); 61641016Sdfr } 61741016Sdfr#endif 61841016Sdfr} 61941016Sdfr 62041016Sdfrstatic int 62141016Sdfrrestore_controller(KBDC kbdc, int command_byte) 62241016Sdfr{ 62341016Sdfr empty_both_buffers(kbdc, 10); 62441016Sdfr 62541016Sdfr if (!set_controller_command_byte(kbdc, 0xff, command_byte)) { 62641016Sdfr log(LOG_ERR, "psm: failed to restore the keyboard controller " 62741016Sdfr "command byte.\n"); 62858230Syokota empty_both_buffers(kbdc, 10); 62941016Sdfr return FALSE; 63041016Sdfr } else { 63158230Syokota empty_both_buffers(kbdc, 10); 63241016Sdfr return TRUE; 63341016Sdfr } 63441016Sdfr} 63541016Sdfr 63641016Sdfr/* 63741016Sdfr * Re-initialize the aux port and device. The aux port must be enabled 63841016Sdfr * and its interrupt must be disabled before calling this routine. 63941016Sdfr * The aux device will be disabled before returning. 64041016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before 64141016Sdfr * calling this routine. 64241016Sdfr */ 64341016Sdfrstatic int 64484880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode) 64541016Sdfr{ 64641016Sdfr KBDC kbdc = sc->kbdc; 64741016Sdfr int stat[3]; 64841016Sdfr int i; 64941016Sdfr 65041016Sdfr switch((i = test_aux_port(kbdc))) { 651132270Smux case 1: /* ignore these errors */ 652132270Smux case 2: 653132270Smux case 3: 65441016Sdfr case PSM_ACK: 65541016Sdfr if (verbose) 65641016Sdfr log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n", 65784880Syokota sc->unit, i); 658102412Scharnier /* FALLTHROUGH */ 65941016Sdfr case 0: /* no error */ 66041016Sdfr break; 66141016Sdfr case -1: /* time out */ 66241016Sdfr default: /* error */ 66341016Sdfr recover_from_error(kbdc); 66445789Speter if (sc->config & PSM_CONFIG_IGNPORTERROR) 66545789Speter break; 66641016Sdfr log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n", 66784880Syokota sc->unit, i); 66841016Sdfr return FALSE; 66941016Sdfr } 67041016Sdfr 67145789Speter if (sc->config & PSM_CONFIG_NORESET) { 67245789Speter /* 67345789Speter * Don't try to reset the pointing device. It may possibly be 67445789Speter * left in the unknown state, though... 67545789Speter */ 67645789Speter } else { 67745789Speter /* 67845789Speter * NOTE: some controllers appears to hang the `keyboard' when 67945789Speter * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 68045789Speter */ 68145789Speter if (!reset_aux_dev(kbdc)) { 68245789Speter recover_from_error(kbdc); 68384880Syokota log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit); 68445789Speter return FALSE; 68545789Speter } 68641016Sdfr } 68741016Sdfr 68841016Sdfr /* 68941016Sdfr * both the aux port and the aux device is functioning, see 69041016Sdfr * if the device can be enabled. 69141016Sdfr */ 69241016Sdfr if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) { 69384880Syokota log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit); 69441016Sdfr return FALSE; 69541016Sdfr } 69641016Sdfr empty_both_buffers(kbdc, 10); /* remove stray data if any */ 69741016Sdfr 69845789Speter if (sc->config & PSM_CONFIG_NOIDPROBE) { 69945789Speter i = GENERIC_MOUSE_ENTRY; 70045789Speter } else { 70145789Speter /* FIXME: hardware ID, mouse buttons? */ 70241016Sdfr 70345789Speter /* other parameters */ 70445789Speter for (i = 0; vendortype[i].probefunc != NULL; ++i) { 70545789Speter if ((*vendortype[i].probefunc)(sc)) { 70645789Speter if (verbose >= 2) 70745789Speter log(LOG_ERR, "psm%d: found %s\n", 70884880Syokota sc->unit, model_name(vendortype[i].model)); 70945789Speter break; 71045789Speter } 71141016Sdfr } 71241016Sdfr } 71341016Sdfr 71441016Sdfr sc->hw.model = vendortype[i].model; 71541016Sdfr sc->mode.packetsize = vendortype[i].packetsize; 71641016Sdfr 71741016Sdfr /* set mouse parameters */ 71841016Sdfr if (mode != (mousemode_t *)NULL) { 71941016Sdfr if (mode->rate > 0) 72041016Sdfr mode->rate = set_mouse_sampling_rate(kbdc, mode->rate); 72141016Sdfr if (mode->resolution >= 0) 72241016Sdfr mode->resolution = set_mouse_resolution(kbdc, mode->resolution); 72341016Sdfr set_mouse_scaling(kbdc, 1); 72441016Sdfr set_mouse_mode(kbdc); 72541016Sdfr } 72641016Sdfr 72741016Sdfr /* request a data packet and extract sync. bits */ 72841016Sdfr if (get_mouse_status(kbdc, stat, 1, 3) < 3) { 72984880Syokota log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n", 73084880Syokota sc->unit); 73141016Sdfr sc->mode.syncmask[0] = 0; 73241016Sdfr } else { 73341016Sdfr sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 73441016Sdfr /* the NetScroll Mouse will send three more bytes... Ignore them */ 73541016Sdfr empty_aux_buffer(kbdc, 5); 73641016Sdfr } 73741016Sdfr 73841016Sdfr /* just check the status of the mouse */ 73941016Sdfr if (get_mouse_status(kbdc, stat, 0, 3) < 3) 74084880Syokota log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n", 74184880Syokota sc->unit); 74241016Sdfr 74341016Sdfr return TRUE; 74441016Sdfr} 74541016Sdfr 74641016Sdfrstatic int 74784880Syokotadoopen(struct psm_softc *sc, int command_byte) 74841016Sdfr{ 74941016Sdfr int stat[3]; 75041016Sdfr 75141016Sdfr /* enable the mouse device */ 75241016Sdfr if (!enable_aux_dev(sc->kbdc)) { 75341016Sdfr /* MOUSE ERROR: failed to enable the mouse because: 75441016Sdfr * 1) the mouse is faulty, 75541016Sdfr * 2) the mouse has been removed(!?) 75641016Sdfr * In the latter case, the keyboard may have hung, and need 75741016Sdfr * recovery procedure... 75841016Sdfr */ 75941016Sdfr recover_from_error(sc->kbdc); 76041016Sdfr#if 0 76141016Sdfr /* FIXME: we could reset the mouse here and try to enable 76241016Sdfr * it again. But it will take long time and it's not a good 76341016Sdfr * idea to disable the keyboard that long... 76441016Sdfr */ 76584880Syokota if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) { 76641016Sdfr recover_from_error(sc->kbdc); 76741016Sdfr#else 76841016Sdfr { 76941016Sdfr#endif 77041016Sdfr restore_controller(sc->kbdc, command_byte); 77141016Sdfr /* mark this device is no longer available */ 77241016Sdfr sc->state &= ~PSM_VALID; 77341016Sdfr log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n", 77484880Syokota sc->unit); 77541016Sdfr return (EIO); 77641016Sdfr } 77741016Sdfr } 77841016Sdfr 77941016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 78084880Syokota log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit); 78141016Sdfr 78241016Sdfr /* enable the aux port and interrupt */ 78341016Sdfr if (!set_controller_command_byte(sc->kbdc, 78441016Sdfr kbdc_get_device_mask(sc->kbdc), 78541016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 78641016Sdfr | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) { 78741016Sdfr /* CONTROLLER ERROR */ 78841016Sdfr disable_aux_dev(sc->kbdc); 78941016Sdfr restore_controller(sc->kbdc, command_byte); 79041016Sdfr log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n", 79184880Syokota sc->unit); 79241016Sdfr return (EIO); 79341016Sdfr } 79441016Sdfr 79558230Syokota /* start the watchdog timer */ 79658230Syokota sc->watchdog = FALSE; 79784880Syokota sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2); 79858230Syokota 79941016Sdfr return (0); 80041016Sdfr} 80141016Sdfr 80284880Syokotastatic int 80384880Syokotareinitialize(struct psm_softc *sc, int doinit) 80484880Syokota{ 80584880Syokota int err; 80684880Syokota int c; 80784880Syokota int s; 80884880Syokota 80984880Syokota /* don't let anybody mess with the aux device */ 81084880Syokota if (!kbdc_lock(sc->kbdc, TRUE)) 81184880Syokota return (EIO); 81284880Syokota s = spltty(); 81384880Syokota 81484880Syokota /* block our watchdog timer */ 81584880Syokota sc->watchdog = FALSE; 81684880Syokota untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout); 81784880Syokota callout_handle_init(&sc->callout); 81884880Syokota 81984880Syokota /* save the current controller command byte */ 82084880Syokota empty_both_buffers(sc->kbdc, 10); 82184880Syokota c = get_controller_command_byte(sc->kbdc); 82284880Syokota if (verbose >= 2) 82384880Syokota log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n", 82484880Syokota sc->unit, c); 82584880Syokota 82684880Syokota /* enable the aux port but disable the aux interrupt and the keyboard */ 82784880Syokota if ((c == -1) || !set_controller_command_byte(sc->kbdc, 82884880Syokota kbdc_get_device_mask(sc->kbdc), 82984880Syokota KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 83084880Syokota | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 83184880Syokota /* CONTROLLER ERROR */ 83284880Syokota splx(s); 83384880Syokota kbdc_lock(sc->kbdc, FALSE); 83484880Syokota log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n", 83584880Syokota sc->unit); 83684880Syokota return (EIO); 83784880Syokota } 83884880Syokota 83984880Syokota /* flush any data */ 84084880Syokota if (sc->state & PSM_VALID) { 84184880Syokota disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */ 84284880Syokota empty_aux_buffer(sc->kbdc, 10); 84384880Syokota } 844123442Salfred flushpackets(sc); 84584880Syokota sc->syncerrors = 0; 84684880Syokota 84784880Syokota /* try to detect the aux device; are you still there? */ 84884880Syokota err = 0; 84984880Syokota if (doinit) { 85084880Syokota if (doinitialize(sc, &sc->mode)) { 85184880Syokota /* yes */ 85284880Syokota sc->state |= PSM_VALID; 85384880Syokota } else { 85484880Syokota /* the device has gone! */ 85584880Syokota restore_controller(sc->kbdc, c); 85684880Syokota sc->state &= ~PSM_VALID; 85784880Syokota log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n", 85884880Syokota sc->unit); 85984880Syokota err = ENXIO; 86084880Syokota } 86184880Syokota } 86284880Syokota splx(s); 86384880Syokota 86484880Syokota /* restore the driver state */ 86584880Syokota if ((sc->state & PSM_OPEN) && (err == 0)) { 86684880Syokota /* enable the aux device and the port again */ 86784880Syokota err = doopen(sc, c); 86884880Syokota if (err != 0) 86984880Syokota log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n", 87084880Syokota sc->unit); 87184880Syokota } else { 87284880Syokota /* restore the keyboard port and disable the aux port */ 87384880Syokota if (!set_controller_command_byte(sc->kbdc, 87484880Syokota kbdc_get_device_mask(sc->kbdc), 87584880Syokota (c & KBD_KBD_CONTROL_BITS) 87684880Syokota | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 87784880Syokota /* CONTROLLER ERROR */ 87884880Syokota log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n", 87984880Syokota sc->unit); 88084880Syokota err = EIO; 88184880Syokota } 88284880Syokota } 88384880Syokota 88484880Syokota kbdc_lock(sc->kbdc, FALSE); 88584880Syokota return (err); 88684880Syokota} 88784880Syokota 88841016Sdfr/* psm driver entry points */ 88941016Sdfr 89083147Syokotastatic void 89183147Syokotapsmidentify(driver_t *driver, device_t parent) 89283147Syokota{ 89383931Syokota device_t psmc; 89483931Syokota device_t psm; 89583931Syokota u_long irq; 89683931Syokota int unit; 89783147Syokota 89883931Syokota unit = device_get_unit(parent); 89983931Syokota 90083147Syokota /* always add at least one child */ 90183931Syokota psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit); 90283931Syokota if (psm == NULL) 90383931Syokota return; 90483931Syokota 90583931Syokota irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX); 90683931Syokota if (irq > 0) 90783931Syokota return; 90883931Syokota 90983931Syokota /* 91083931Syokota * If the PS/2 mouse device has already been reported by ACPI or 91183931Syokota * PnP BIOS, obtain the IRQ resource from it. 91283931Syokota * (See psmcpnp_attach() below.) 91383931Syokota */ 91483931Syokota psmc = device_find_child(device_get_parent(parent), 91583931Syokota PSMCPNP_DRIVER_NAME, unit); 91683931Syokota if (psmc == NULL) 91783931Syokota return; 91883931Syokota irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0); 91983931Syokota if (irq <= 0) 92083931Syokota return; 92183931Syokota bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 92283147Syokota} 92383147Syokota 924115569Sphk#define endprobe(v) do { if (bootverbose) \ 92541016Sdfr --verbose; \ 92641016Sdfr kbdc_set_device_mask(sc->kbdc, mask); \ 92741016Sdfr kbdc_lock(sc->kbdc, FALSE); \ 92841016Sdfr return (v); \ 929115569Sphk } while (0) 93041016Sdfr 93141016Sdfrstatic int 93241016Sdfrpsmprobe(device_t dev) 93341016Sdfr{ 93441016Sdfr int unit = device_get_unit(dev); 93541016Sdfr struct psm_softc *sc = device_get_softc(dev); 93641016Sdfr int stat[3]; 93741016Sdfr int command_byte; 93841016Sdfr int mask; 93958230Syokota int rid; 94041016Sdfr int i; 94141016Sdfr 94241016Sdfr#if 0 94341016Sdfr kbdc_debug(TRUE); 94441016Sdfr#endif 94558230Syokota 94683147Syokota /* see if IRQ is available */ 94783147Syokota rid = KBDC_RID_AUX; 948127135Snjl sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 949127135Snjl RF_SHAREABLE | RF_ACTIVE); 95083147Syokota if (sc->intr == NULL) { 95183147Syokota if (bootverbose) 95283147Syokota device_printf(dev, "unable to allocate IRQ\n"); 95383147Syokota return (ENXIO); 95483147Syokota } 95583147Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 95658230Syokota 95784880Syokota sc->unit = unit; 95858230Syokota sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev))); 95983147Syokota sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS; 96058230Syokota /* XXX: for backward compatibility */ 96158230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM) 96258230Syokota sc->config |= 96358230Syokota#ifdef PSM_RESETAFTERSUSPEND 96458230Syokota PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 96558230Syokota#else 96658230Syokota PSM_CONFIG_HOOKRESUME; 96758230Syokota#endif 96858230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */ 96941016Sdfr sc->flags = 0; 97041016Sdfr if (bootverbose) 97141016Sdfr ++verbose; 97241016Sdfr 97343105Sdfr device_set_desc(dev, "PS/2 Mouse"); 97443105Sdfr 97541016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) { 97641016Sdfr printf("psm%d: unable to lock the controller.\n", unit); 97741016Sdfr if (bootverbose) 97841016Sdfr --verbose; 97941016Sdfr return (ENXIO); 98041016Sdfr } 98141016Sdfr 98241016Sdfr /* 98341016Sdfr * NOTE: two bits in the command byte controls the operation of the 98441016Sdfr * aux port (mouse port): the aux port disable bit (bit 5) and the aux 98541016Sdfr * port interrupt (IRQ 12) enable bit (bit 2). 98641016Sdfr */ 98741016Sdfr 98841016Sdfr /* discard anything left after the keyboard initialization */ 98941016Sdfr empty_both_buffers(sc->kbdc, 10); 99041016Sdfr 99141016Sdfr /* save the current command byte; it will be used later */ 99241016Sdfr mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS; 99341016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 99441016Sdfr if (verbose) 99541016Sdfr printf("psm%d: current command byte:%04x\n", unit, command_byte); 99641016Sdfr if (command_byte == -1) { 99741016Sdfr /* CONTROLLER ERROR */ 99841016Sdfr printf("psm%d: unable to get the current command byte value.\n", 99941016Sdfr unit); 100041016Sdfr endprobe(ENXIO); 100141016Sdfr } 100241016Sdfr 100341016Sdfr /* 100441016Sdfr * disable the keyboard port while probing the aux port, which must be 100541016Sdfr * enabled during this routine 100641016Sdfr */ 100741016Sdfr if (!set_controller_command_byte(sc->kbdc, 100841016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 100941016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 101041016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 101141016Sdfr /* 101241016Sdfr * this is CONTROLLER ERROR; I don't know how to recover 101341016Sdfr * from this error... 101441016Sdfr */ 101541016Sdfr restore_controller(sc->kbdc, command_byte); 101641016Sdfr printf("psm%d: unable to set the command byte.\n", unit); 101741016Sdfr endprobe(ENXIO); 101841016Sdfr } 101945789Speter write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT); 102041016Sdfr 102141016Sdfr /* 102241016Sdfr * NOTE: `test_aux_port()' is designed to return with zero if the aux 102341016Sdfr * port exists and is functioning. However, some controllers appears 102441016Sdfr * to respond with zero even when the aux port doesn't exist. (It may 102541016Sdfr * be that this is only the case when the controller DOES have the aux 102641016Sdfr * port but the port is not wired on the motherboard.) The keyboard 102741016Sdfr * controllers without the port, such as the original AT, are 102841016Sdfr * supporsed to return with an error code or simply time out. In any 102941016Sdfr * case, we have to continue probing the port even when the controller 103041016Sdfr * passes this test. 103141016Sdfr * 1032132270Smux * XXX: some controllers erroneously return the error code 1, 2 or 3 1033132270Smux * when it has the perfectly functional aux port. We have to ignore 1034132270Smux * this error code. Even if the controller HAS error with the aux 1035132270Smux * port, it will be detected later... 103641016Sdfr * XXX: another incompatible controller returns PSM_ACK (0xfa)... 103741016Sdfr */ 103841016Sdfr switch ((i = test_aux_port(sc->kbdc))) { 1039132270Smux case 1: /* ignore these errors */ 1040132270Smux case 2: 1041132270Smux case 3: 104241016Sdfr case PSM_ACK: 104341016Sdfr if (verbose) 104441016Sdfr printf("psm%d: strange result for test aux port (%d).\n", 104541016Sdfr unit, i); 1046102412Scharnier /* FALLTHROUGH */ 104741016Sdfr case 0: /* no error */ 104841016Sdfr break; 104941016Sdfr case -1: /* time out */ 105041016Sdfr default: /* error */ 105141016Sdfr recover_from_error(sc->kbdc); 105245789Speter if (sc->config & PSM_CONFIG_IGNPORTERROR) 105345789Speter break; 105441016Sdfr restore_controller(sc->kbdc, command_byte); 105541016Sdfr if (verbose) 105641016Sdfr printf("psm%d: the aux port is not functioning (%d).\n", 105741016Sdfr unit, i); 105841016Sdfr endprobe(ENXIO); 105941016Sdfr } 106041016Sdfr 106145789Speter if (sc->config & PSM_CONFIG_NORESET) { 106245789Speter /* 106345789Speter * Don't try to reset the pointing device. It may possibly be 106445789Speter * left in the unknown state, though... 106545789Speter */ 106645789Speter } else { 106745789Speter /* 106845789Speter * NOTE: some controllers appears to hang the `keyboard' when the aux 106945789Speter * port doesn't exist and `PSMC_RESET_DEV' is issued. 1070117302Smikeh * 1071117302Smikeh * Attempt to reset the controller twice -- this helps 1072117302Smikeh * pierce through some KVM switches. The second reset 1073117302Smikeh * is non-fatal. 107445789Speter */ 107545789Speter if (!reset_aux_dev(sc->kbdc)) { 107645789Speter recover_from_error(sc->kbdc); 107745789Speter restore_controller(sc->kbdc, command_byte); 107845789Speter if (verbose) 107945789Speter printf("psm%d: failed to reset the aux device.\n", unit); 108045789Speter endprobe(ENXIO); 1081117302Smikeh } else if (!reset_aux_dev(sc->kbdc)) { 1082117302Smikeh recover_from_error(sc->kbdc); 1083117302Smikeh if (verbose >= 2) 1084117302Smikeh printf("psm%d: failed to reset the aux device (2).\n", 1085117302Smikeh unit); 108645789Speter } 108741016Sdfr } 108845789Speter 108941016Sdfr /* 109041016Sdfr * both the aux port and the aux device is functioning, see if the 109141016Sdfr * device can be enabled. NOTE: when enabled, the device will start 109241016Sdfr * sending data; we shall immediately disable the device once we know 109341016Sdfr * the device can be enabled. 109441016Sdfr */ 109541016Sdfr if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) { 109645789Speter /* MOUSE ERROR */ 109745789Speter recover_from_error(sc->kbdc); 109845789Speter restore_controller(sc->kbdc, command_byte); 109945789Speter if (verbose) 110045789Speter printf("psm%d: failed to enable the aux device.\n", unit); 110141016Sdfr endprobe(ENXIO); 110241016Sdfr } 110341016Sdfr 110441016Sdfr /* save the default values after reset */ 110541016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) { 110641016Sdfr sc->dflt_mode.rate = sc->mode.rate = stat[2]; 110741016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 110841016Sdfr } else { 110941016Sdfr sc->dflt_mode.rate = sc->mode.rate = -1; 111041016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = -1; 111141016Sdfr } 111241016Sdfr 111341016Sdfr /* hardware information */ 111441016Sdfr sc->hw.iftype = MOUSE_IF_PS2; 111541016Sdfr 111641016Sdfr /* verify the device is a mouse */ 111741016Sdfr sc->hw.hwid = get_aux_id(sc->kbdc); 111841016Sdfr if (!is_a_mouse(sc->hw.hwid)) { 111941016Sdfr restore_controller(sc->kbdc, command_byte); 112041016Sdfr if (verbose) 112141016Sdfr printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid); 112241016Sdfr endprobe(ENXIO); 112341016Sdfr } 112441016Sdfr switch (sc->hw.hwid) { 112541016Sdfr case PSM_BALLPOINT_ID: 112641016Sdfr sc->hw.type = MOUSE_TRACKBALL; 112741016Sdfr break; 112841016Sdfr case PSM_MOUSE_ID: 112941016Sdfr case PSM_INTELLI_ID: 113058230Syokota case PSM_EXPLORER_ID: 113158230Syokota case PSM_4DMOUSE_ID: 113258230Syokota case PSM_4DPLUS_ID: 113341016Sdfr sc->hw.type = MOUSE_MOUSE; 113441016Sdfr break; 113541016Sdfr default: 113641016Sdfr sc->hw.type = MOUSE_UNKNOWN; 113741016Sdfr break; 113841016Sdfr } 113941016Sdfr 114045789Speter if (sc->config & PSM_CONFIG_NOIDPROBE) { 114145789Speter sc->hw.buttons = 2; 114245789Speter i = GENERIC_MOUSE_ENTRY; 114345789Speter } else { 114445789Speter /* # of buttons */ 114545789Speter sc->hw.buttons = get_mouse_buttons(sc->kbdc); 114641016Sdfr 114745789Speter /* other parameters */ 114845789Speter for (i = 0; vendortype[i].probefunc != NULL; ++i) { 114945789Speter if ((*vendortype[i].probefunc)(sc)) { 115045789Speter if (verbose >= 2) 115145789Speter printf("psm%d: found %s\n", 115245789Speter unit, model_name(vendortype[i].model)); 115345789Speter break; 115445789Speter } 115541016Sdfr } 115641016Sdfr } 115741016Sdfr 115841016Sdfr sc->hw.model = vendortype[i].model; 115941016Sdfr 116041016Sdfr sc->dflt_mode.level = PSM_LEVEL_BASE; 116141016Sdfr sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE; 116241016Sdfr sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4; 116341016Sdfr if (sc->config & PSM_CONFIG_NOCHECKSYNC) 116441016Sdfr sc->dflt_mode.syncmask[0] = 0; 116541016Sdfr else 116641016Sdfr sc->dflt_mode.syncmask[0] = vendortype[i].syncmask; 116745789Speter if (sc->config & PSM_CONFIG_FORCETAP) 116845789Speter sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP; 116941016Sdfr sc->dflt_mode.syncmask[1] = 0; /* syncbits */ 117041016Sdfr sc->mode = sc->dflt_mode; 117141016Sdfr sc->mode.packetsize = vendortype[i].packetsize; 117241016Sdfr 117341016Sdfr /* set mouse parameters */ 117448773Syokota#if 0 117548773Syokota /* 117648773Syokota * A version of Logitech FirstMouse+ won't report wheel movement, 117748773Syokota * if SET_DEFAULTS is sent... Don't use this command. 117848773Syokota * This fix was found by Takashi Nishida. 117948773Syokota */ 118041016Sdfr i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS); 118141016Sdfr if (verbose >= 2) 118241016Sdfr printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i); 118348773Syokota#endif 118441016Sdfr if (sc->config & PSM_CONFIG_RESOLUTION) { 118541016Sdfr sc->mode.resolution 118641016Sdfr = set_mouse_resolution(sc->kbdc, 118748773Syokota (sc->config & PSM_CONFIG_RESOLUTION) - 1); 118848773Syokota } else if (sc->mode.resolution >= 0) { 118948773Syokota sc->mode.resolution 119048773Syokota = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution); 119141016Sdfr } 119248773Syokota if (sc->mode.rate > 0) { 119348773Syokota sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate); 119448773Syokota } 119548773Syokota set_mouse_scaling(sc->kbdc, 1); 119641016Sdfr 119741016Sdfr /* request a data packet and extract sync. bits */ 119841016Sdfr if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) { 119941016Sdfr printf("psm%d: failed to get data.\n", unit); 120041016Sdfr sc->mode.syncmask[0] = 0; 120141016Sdfr } else { 120241016Sdfr sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 120341016Sdfr /* the NetScroll Mouse will send three more bytes... Ignore them */ 120441016Sdfr empty_aux_buffer(sc->kbdc, 5); 120541016Sdfr } 120641016Sdfr 120741016Sdfr /* just check the status of the mouse */ 120841016Sdfr /* 120941016Sdfr * NOTE: XXX there are some arcane controller/mouse combinations out 121041016Sdfr * there, which hung the controller unless there is data transmission 121141016Sdfr * after ACK from the mouse. 121241016Sdfr */ 121341016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) { 121441016Sdfr printf("psm%d: failed to get status.\n", unit); 121541016Sdfr } else { 121641016Sdfr /* 121741016Sdfr * When in its native mode, some mice operate with different 121841016Sdfr * default parameters than in the PS/2 compatible mode. 121941016Sdfr */ 122041016Sdfr sc->dflt_mode.rate = sc->mode.rate = stat[2]; 122141016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 122241016Sdfr } 122341016Sdfr 122441016Sdfr /* disable the aux port for now... */ 122541016Sdfr if (!set_controller_command_byte(sc->kbdc, 122641016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 122741016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 122841016Sdfr | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 122941016Sdfr /* 123041016Sdfr * this is CONTROLLER ERROR; I don't know the proper way to 123141016Sdfr * recover from this error... 123241016Sdfr */ 123341016Sdfr restore_controller(sc->kbdc, command_byte); 123441016Sdfr printf("psm%d: unable to set the command byte.\n", unit); 123541016Sdfr endprobe(ENXIO); 123641016Sdfr } 123741016Sdfr 123841016Sdfr /* done */ 123941016Sdfr kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS); 124041016Sdfr kbdc_lock(sc->kbdc, FALSE); 124141016Sdfr return (0); 124241016Sdfr} 124341016Sdfr 124441016Sdfrstatic int 124541016Sdfrpsmattach(device_t dev) 124641016Sdfr{ 124741016Sdfr int unit = device_get_unit(dev); 124841016Sdfr struct psm_softc *sc = device_get_softc(dev); 124958230Syokota int error; 125058230Syokota int rid; 125141016Sdfr 125241016Sdfr /* Setup initial state */ 125341016Sdfr sc->state = PSM_VALID; 125458230Syokota callout_handle_init(&sc->callout); 125541016Sdfr 125658230Syokota /* Setup our interrupt handler */ 125783147Syokota rid = KBDC_RID_AUX; 1258127135Snjl sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1259127135Snjl RF_SHAREABLE | RF_ACTIVE); 126058230Syokota if (sc->intr == NULL) 126158230Syokota return (ENXIO); 126283147Syokota error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih); 126358230Syokota if (error) { 126458230Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 126558230Syokota return (error); 126658230Syokota } 126758230Syokota 126841016Sdfr /* Done */ 126958230Syokota sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, 127058230Syokota "psm%d", unit); 127158230Syokota sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, 127258230Syokota "bpsm%d", unit); 127341016Sdfr 127441016Sdfr if (!verbose) { 127541016Sdfr printf("psm%d: model %s, device ID %d\n", 127648778Syokota unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff); 127741016Sdfr } else { 127848778Syokota printf("psm%d: model %s, device ID %d-%02x, %d buttons\n", 127948778Syokota unit, model_name(sc->hw.model), 128048778Syokota sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons); 128141016Sdfr printf("psm%d: config:%08x, flags:%08x, packet size:%d\n", 128241016Sdfr unit, sc->config, sc->flags, sc->mode.packetsize); 128341016Sdfr printf("psm%d: syncmask:%02x, syncbits:%02x\n", 128441016Sdfr unit, sc->mode.syncmask[0], sc->mode.syncmask[1]); 128541016Sdfr } 128641016Sdfr 128741016Sdfr if (bootverbose) 128841016Sdfr --verbose; 128941016Sdfr 129041016Sdfr return (0); 129141016Sdfr} 129241016Sdfr 129341016Sdfrstatic int 129458230Syokotapsmdetach(device_t dev) 129558230Syokota{ 129658230Syokota struct psm_softc *sc; 129758230Syokota int rid; 129858230Syokota 129958230Syokota sc = device_get_softc(dev); 130058230Syokota if (sc->state & PSM_OPEN) 130158230Syokota return EBUSY; 130258230Syokota 130383147Syokota rid = KBDC_RID_AUX; 130483147Syokota bus_teardown_intr(dev, sc->intr, sc->ih); 130558230Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 130658230Syokota 130758230Syokota destroy_dev(sc->dev); 130858230Syokota destroy_dev(sc->bdev); 130958230Syokota 131058230Syokota return 0; 131158230Syokota} 131258230Syokota 131358230Syokotastatic int 1314130585Sphkpsmopen(struct cdev *dev, int flag, int fmt, struct thread *td) 131541016Sdfr{ 131641016Sdfr int unit = PSM_UNIT(dev); 131741016Sdfr struct psm_softc *sc; 131841016Sdfr int command_byte; 131941016Sdfr int err; 132041016Sdfr int s; 132141016Sdfr 132241016Sdfr /* Get device data */ 132341016Sdfr sc = PSM_SOFTC(unit); 132441016Sdfr if ((sc == NULL) || (sc->state & PSM_VALID) == 0) 132541016Sdfr /* the device is no longer valid/functioning */ 132641016Sdfr return (ENXIO); 132741016Sdfr 132841016Sdfr /* Disallow multiple opens */ 132941016Sdfr if (sc->state & PSM_OPEN) 133041016Sdfr return (EBUSY); 133141016Sdfr 133241016Sdfr device_busy(devclass_get_device(psm_devclass, unit)); 133341016Sdfr 133441016Sdfr /* Initialize state */ 133541016Sdfr sc->mode.level = sc->dflt_mode.level; 133641016Sdfr sc->mode.protocol = sc->dflt_mode.protocol; 133758230Syokota sc->watchdog = FALSE; 133841016Sdfr 133941016Sdfr /* flush the event queue */ 134041016Sdfr sc->queue.count = 0; 134141016Sdfr sc->queue.head = 0; 134241016Sdfr sc->queue.tail = 0; 134341016Sdfr sc->status.flags = 0; 134441016Sdfr sc->status.button = 0; 134541016Sdfr sc->status.obutton = 0; 134641016Sdfr sc->status.dx = 0; 134741016Sdfr sc->status.dy = 0; 134841016Sdfr sc->status.dz = 0; 134941016Sdfr sc->button = 0; 1350123442Salfred sc->pqueue_start = 0; 1351123442Salfred sc->pqueue_end = 0; 135241016Sdfr 135341016Sdfr /* empty input buffer */ 1354123442Salfred flushpackets(sc); 135584880Syokota sc->syncerrors = 0; 135641016Sdfr 135741016Sdfr /* don't let timeout routines in the keyboard driver to poll the kbdc */ 135841016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 135941016Sdfr return (EIO); 136041016Sdfr 136141016Sdfr /* save the current controller command byte */ 136241016Sdfr s = spltty(); 136341016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 136441016Sdfr 136541016Sdfr /* enable the aux port and temporalily disable the keyboard */ 136641016Sdfr if ((command_byte == -1) 136741016Sdfr || !set_controller_command_byte(sc->kbdc, 136841016Sdfr kbdc_get_device_mask(sc->kbdc), 136941016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 137041016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 137141016Sdfr /* CONTROLLER ERROR; do you know how to get out of this? */ 137241016Sdfr kbdc_lock(sc->kbdc, FALSE); 137341016Sdfr splx(s); 137441016Sdfr log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n", 137541016Sdfr unit); 137641016Sdfr return (EIO); 137741016Sdfr } 137841016Sdfr /* 137941016Sdfr * Now that the keyboard controller is told not to generate 138041016Sdfr * the keyboard and mouse interrupts, call `splx()' to allow 138141016Sdfr * the other tty interrupts. The clock interrupt may also occur, 138241016Sdfr * but timeout routines will be blocked by the poll flag set 138341016Sdfr * via `kbdc_lock()' 138441016Sdfr */ 138541016Sdfr splx(s); 138641016Sdfr 138741016Sdfr /* enable the mouse device */ 138884880Syokota err = doopen(sc, command_byte); 138941016Sdfr 139041016Sdfr /* done */ 139141016Sdfr if (err == 0) 139241016Sdfr sc->state |= PSM_OPEN; 139341016Sdfr kbdc_lock(sc->kbdc, FALSE); 139441016Sdfr return (err); 139541016Sdfr} 139641016Sdfr 139741016Sdfrstatic int 1398130585Sphkpsmclose(struct cdev *dev, int flag, int fmt, struct thread *td) 139941016Sdfr{ 140041016Sdfr int unit = PSM_UNIT(dev); 140141016Sdfr struct psm_softc *sc = PSM_SOFTC(unit); 140241016Sdfr int stat[3]; 140341016Sdfr int command_byte; 140441016Sdfr int s; 140541016Sdfr 140641016Sdfr /* don't let timeout routines in the keyboard driver to poll the kbdc */ 140741016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 140841016Sdfr return (EIO); 140941016Sdfr 141041016Sdfr /* save the current controller command byte */ 141141016Sdfr s = spltty(); 141241016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 141341016Sdfr if (command_byte == -1) { 141441016Sdfr kbdc_lock(sc->kbdc, FALSE); 141541016Sdfr splx(s); 141641016Sdfr return (EIO); 141741016Sdfr } 141841016Sdfr 141941016Sdfr /* disable the aux interrupt and temporalily disable the keyboard */ 142041016Sdfr if (!set_controller_command_byte(sc->kbdc, 142141016Sdfr kbdc_get_device_mask(sc->kbdc), 142241016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 142341016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 142441016Sdfr log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n", 142558230Syokota unit); 142641016Sdfr /* CONTROLLER ERROR; 142741016Sdfr * NOTE: we shall force our way through. Because the only 142841016Sdfr * ill effect we shall see is that we may not be able 142941016Sdfr * to read ACK from the mouse, and it doesn't matter much 143041016Sdfr * so long as the mouse will accept the DISABLE command. 143141016Sdfr */ 143241016Sdfr } 143341016Sdfr splx(s); 143441016Sdfr 143558230Syokota /* stop the watchdog timer */ 143684880Syokota untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout); 143758230Syokota callout_handle_init(&sc->callout); 143858230Syokota 143941016Sdfr /* remove anything left in the output buffer */ 144041016Sdfr empty_aux_buffer(sc->kbdc, 10); 144141016Sdfr 144241016Sdfr /* disable the aux device, port and interrupt */ 144341016Sdfr if (sc->state & PSM_VALID) { 144441016Sdfr if (!disable_aux_dev(sc->kbdc)) { 144541016Sdfr /* MOUSE ERROR; 144641016Sdfr * NOTE: we don't return error and continue, pretending 144741016Sdfr * we have successfully disabled the device. It's OK because 144841016Sdfr * the interrupt routine will discard any data from the mouse 144941016Sdfr * hereafter. 145041016Sdfr */ 145141016Sdfr log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n", 145258230Syokota unit); 145341016Sdfr } 145441016Sdfr 145541016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 145641016Sdfr log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n", 145758230Syokota unit); 145841016Sdfr } 145941016Sdfr 146041016Sdfr if (!set_controller_command_byte(sc->kbdc, 146141016Sdfr kbdc_get_device_mask(sc->kbdc), 146241016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 146341016Sdfr | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 146441016Sdfr /* CONTROLLER ERROR; 146541016Sdfr * we shall ignore this error; see the above comment. 146641016Sdfr */ 146741016Sdfr log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n", 146858230Syokota unit); 146941016Sdfr } 147041016Sdfr 147141016Sdfr /* remove anything left in the output buffer */ 147241016Sdfr empty_aux_buffer(sc->kbdc, 10); 147341016Sdfr 147441016Sdfr /* close is almost always successful */ 147541016Sdfr sc->state &= ~PSM_OPEN; 147641016Sdfr kbdc_lock(sc->kbdc, FALSE); 147741016Sdfr device_unbusy(devclass_get_device(psm_devclass, unit)); 147841016Sdfr return (0); 147941016Sdfr} 148041016Sdfr 148141016Sdfrstatic int 1482123442Salfredtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, unsigned char *buf) 148341016Sdfr{ 148441016Sdfr static unsigned char butmapps2[8] = { 148541016Sdfr 0, 148641016Sdfr MOUSE_PS2_BUTTON1DOWN, 148741016Sdfr MOUSE_PS2_BUTTON2DOWN, 148841016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN, 148941016Sdfr MOUSE_PS2_BUTTON3DOWN, 149041016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN, 149141016Sdfr MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 149241016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 149341016Sdfr }; 149441016Sdfr static unsigned char butmapmsc[8] = { 149541016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 149641016Sdfr MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 149741016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 149841016Sdfr MOUSE_MSC_BUTTON3UP, 149941016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 150041016Sdfr MOUSE_MSC_BUTTON2UP, 150141016Sdfr MOUSE_MSC_BUTTON1UP, 150241016Sdfr 0, 150341016Sdfr }; 150441016Sdfr int mapped; 150541016Sdfr int i; 150641016Sdfr 150741016Sdfr if (sc->mode.level == PSM_LEVEL_BASE) { 150841016Sdfr mapped = status->button & ~MOUSE_BUTTON4DOWN; 150941016Sdfr if (status->button & MOUSE_BUTTON4DOWN) 151041016Sdfr mapped |= MOUSE_BUTTON1DOWN; 151141016Sdfr status->button = mapped; 151241016Sdfr buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS]; 1513109269Smdodd i = imax(imin(status->dx, 255), -256); 151441016Sdfr if (i < 0) 151541016Sdfr buf[0] |= MOUSE_PS2_XNEG; 151641016Sdfr buf[1] = i; 1517109269Smdodd i = imax(imin(status->dy, 255), -256); 151841016Sdfr if (i < 0) 151941016Sdfr buf[0] |= MOUSE_PS2_YNEG; 152041016Sdfr buf[2] = i; 152141016Sdfr return MOUSE_PS2_PACKETSIZE; 152241016Sdfr } else if (sc->mode.level == PSM_LEVEL_STANDARD) { 152341016Sdfr buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS]; 1524109269Smdodd i = imax(imin(status->dx, 255), -256); 152541016Sdfr buf[1] = i >> 1; 152641016Sdfr buf[3] = i - buf[1]; 1527109269Smdodd i = imax(imin(status->dy, 255), -256); 152841016Sdfr buf[2] = i >> 1; 152941016Sdfr buf[4] = i - buf[2]; 1530109269Smdodd i = imax(imin(status->dz, 127), -128); 153141016Sdfr buf[5] = (i >> 1) & 0x7f; 153241016Sdfr buf[6] = (i - (i >> 1)) & 0x7f; 153341016Sdfr buf[7] = (~status->button >> 3) & 0x7f; 153441016Sdfr return MOUSE_SYS_PACKETSIZE; 153541016Sdfr } 1536123442Salfred return pb->inputbytes; 153741016Sdfr} 153841016Sdfr 153941016Sdfrstatic int 1540130585Sphkpsmread(struct cdev *dev, struct uio *uio, int flag) 154141016Sdfr{ 154241016Sdfr register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 154341016Sdfr unsigned char buf[PSM_SMALLBUFSIZE]; 154441016Sdfr int error = 0; 154541016Sdfr int s; 154641016Sdfr int l; 154741016Sdfr 154841016Sdfr if ((sc->state & PSM_VALID) == 0) 154941016Sdfr return EIO; 155041016Sdfr 155141016Sdfr /* block until mouse activity occured */ 155241016Sdfr s = spltty(); 155341016Sdfr while (sc->queue.count <= 0) { 155441016Sdfr if (PSM_NBLOCKIO(dev)) { 155541016Sdfr splx(s); 155641016Sdfr return EWOULDBLOCK; 155741016Sdfr } 155841016Sdfr sc->state |= PSM_ASLP; 1559111748Sdes error = tsleep( sc, PZERO | PCATCH, "psmrea", 0); 156041016Sdfr sc->state &= ~PSM_ASLP; 156141016Sdfr if (error) { 156241016Sdfr splx(s); 156341016Sdfr return error; 156441016Sdfr } else if ((sc->state & PSM_VALID) == 0) { 156541016Sdfr /* the device disappeared! */ 156641016Sdfr splx(s); 156741016Sdfr return EIO; 156841016Sdfr } 156941016Sdfr } 157041016Sdfr splx(s); 157141016Sdfr 157241016Sdfr /* copy data to the user land */ 157341016Sdfr while ((sc->queue.count > 0) && (uio->uio_resid > 0)) { 157441016Sdfr s = spltty(); 1575109269Smdodd l = imin(sc->queue.count, uio->uio_resid); 157641016Sdfr if (l > sizeof(buf)) 157741016Sdfr l = sizeof(buf); 157841016Sdfr if (l > sizeof(sc->queue.buf) - sc->queue.head) { 157941016Sdfr bcopy(&sc->queue.buf[sc->queue.head], &buf[0], 158041016Sdfr sizeof(sc->queue.buf) - sc->queue.head); 158141016Sdfr bcopy(&sc->queue.buf[0], 158241016Sdfr &buf[sizeof(sc->queue.buf) - sc->queue.head], 158341016Sdfr l - (sizeof(sc->queue.buf) - sc->queue.head)); 158441016Sdfr } else { 158541016Sdfr bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l); 158641016Sdfr } 158741016Sdfr sc->queue.count -= l; 158841016Sdfr sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf); 158941016Sdfr splx(s); 159041016Sdfr error = uiomove(buf, l, uio); 159141016Sdfr if (error) 159241016Sdfr break; 159341016Sdfr } 159441016Sdfr 159541016Sdfr return error; 159641016Sdfr} 159741016Sdfr 159841016Sdfrstatic int 159941016Sdfrblock_mouse_data(struct psm_softc *sc, int *c) 160041016Sdfr{ 160141016Sdfr int s; 160241016Sdfr 160341016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 160441016Sdfr return EIO; 160541016Sdfr 160641016Sdfr s = spltty(); 160741016Sdfr *c = get_controller_command_byte(sc->kbdc); 160841016Sdfr if ((*c == -1) 160941016Sdfr || !set_controller_command_byte(sc->kbdc, 161041016Sdfr kbdc_get_device_mask(sc->kbdc), 161141016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 161241016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 161341016Sdfr /* this is CONTROLLER ERROR */ 161441016Sdfr splx(s); 161541016Sdfr kbdc_lock(sc->kbdc, FALSE); 161641016Sdfr return EIO; 161741016Sdfr } 161841016Sdfr 161941016Sdfr /* 162041016Sdfr * The device may be in the middle of status data transmission. 162141016Sdfr * The transmission will be interrupted, thus, incomplete status 162241016Sdfr * data must be discarded. Although the aux interrupt is disabled 162341016Sdfr * at the keyboard controller level, at most one aux interrupt 162441016Sdfr * may have already been pending and a data byte is in the 162541016Sdfr * output buffer; throw it away. Note that the second argument 162641016Sdfr * to `empty_aux_buffer()' is zero, so that the call will just 162741016Sdfr * flush the internal queue. 162841016Sdfr * `psmintr()' will be invoked after `splx()' if an interrupt is 162941016Sdfr * pending; it will see no data and returns immediately. 163041016Sdfr */ 163141016Sdfr empty_aux_buffer(sc->kbdc, 0); /* flush the queue */ 163241016Sdfr read_aux_data_no_wait(sc->kbdc); /* throw away data if any */ 1633123442Salfred flushpackets(sc); 163441016Sdfr splx(s); 163541016Sdfr 163641016Sdfr return 0; 163741016Sdfr} 163841016Sdfr 1639123442Salfredstatic void 1640123442Salfreddropqueue(struct psm_softc *sc) 1641123442Salfred{ 1642123442Salfred 1643123442Salfred sc->queue.count = 0; 1644123442Salfred sc->queue.head = 0; 1645123442Salfred sc->queue.tail = 0; 1646123442Salfred if ((sc->state & PSM_SOFTARMED) != 0) { 1647123442Salfred sc->state &= ~PSM_SOFTARMED; 1648123442Salfred untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout); 1649123442Salfred } 1650123442Salfred sc->pqueue_start = sc->pqueue_end; 1651123442Salfred} 1652123442Salfred 1653123442Salfredstatic void 1654123442Salfredflushpackets(struct psm_softc *sc) 1655123442Salfred{ 1656123442Salfred 1657123442Salfred dropqueue(sc); 1658123442Salfred bzero(&sc->pqueue, sizeof(sc->pqueue)); 1659123442Salfred} 1660123442Salfred 166141016Sdfrstatic int 166241016Sdfrunblock_mouse_data(struct psm_softc *sc, int c) 166341016Sdfr{ 166441016Sdfr int error = 0; 166541016Sdfr 166641016Sdfr /* 166741016Sdfr * We may have seen a part of status data during `set_mouse_XXX()'. 166841016Sdfr * they have been queued; flush it. 166941016Sdfr */ 167041016Sdfr empty_aux_buffer(sc->kbdc, 0); 167141016Sdfr 167241016Sdfr /* restore ports and interrupt */ 167341016Sdfr if (!set_controller_command_byte(sc->kbdc, 167441016Sdfr kbdc_get_device_mask(sc->kbdc), 167541016Sdfr c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 167641016Sdfr /* CONTROLLER ERROR; this is serious, we may have 167741016Sdfr * been left with the inaccessible keyboard and 167841016Sdfr * the disabled mouse interrupt. 167941016Sdfr */ 168041016Sdfr error = EIO; 168141016Sdfr } 168241016Sdfr 168341016Sdfr kbdc_lock(sc->kbdc, FALSE); 168441016Sdfr return error; 168541016Sdfr} 168641016Sdfr 168741016Sdfrstatic int 1688130585Sphkpsmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 168941016Sdfr{ 169041016Sdfr struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 169141016Sdfr mousemode_t mode; 169241016Sdfr mousestatus_t status; 169341016Sdfr#if (defined(MOUSE_GETVARS)) 169441016Sdfr mousevar_t *var; 169541016Sdfr#endif 169641016Sdfr mousedata_t *data; 169741016Sdfr int stat[3]; 169841016Sdfr int command_byte; 169941016Sdfr int error = 0; 170041016Sdfr int s; 170141016Sdfr 170241016Sdfr /* Perform IOCTL command */ 170341016Sdfr switch (cmd) { 170441016Sdfr 170541016Sdfr case OLD_MOUSE_GETHWINFO: 170641016Sdfr s = spltty(); 170741016Sdfr ((old_mousehw_t *)addr)->buttons = sc->hw.buttons; 170841016Sdfr ((old_mousehw_t *)addr)->iftype = sc->hw.iftype; 170941016Sdfr ((old_mousehw_t *)addr)->type = sc->hw.type; 171048778Syokota ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff; 171141016Sdfr splx(s); 171241016Sdfr break; 171341016Sdfr 171441016Sdfr case MOUSE_GETHWINFO: 171541016Sdfr s = spltty(); 171641016Sdfr *(mousehw_t *)addr = sc->hw; 171741016Sdfr if (sc->mode.level == PSM_LEVEL_BASE) 171841016Sdfr ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC; 171941016Sdfr splx(s); 172041016Sdfr break; 172141016Sdfr 172241016Sdfr case OLD_MOUSE_GETMODE: 172341016Sdfr s = spltty(); 172441016Sdfr switch (sc->mode.level) { 172541016Sdfr case PSM_LEVEL_BASE: 172641016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 172741016Sdfr break; 172841016Sdfr case PSM_LEVEL_STANDARD: 172941016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 173041016Sdfr break; 173141016Sdfr case PSM_LEVEL_NATIVE: 173241016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 173341016Sdfr break; 173441016Sdfr } 173541016Sdfr ((old_mousemode_t *)addr)->rate = sc->mode.rate; 173641016Sdfr ((old_mousemode_t *)addr)->resolution = sc->mode.resolution; 173741016Sdfr ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor; 173841016Sdfr splx(s); 173941016Sdfr break; 174041016Sdfr 174141016Sdfr case MOUSE_GETMODE: 174241016Sdfr s = spltty(); 174341016Sdfr *(mousemode_t *)addr = sc->mode; 174441016Sdfr ((mousemode_t *)addr)->resolution = 174541016Sdfr MOUSE_RES_LOW - sc->mode.resolution; 174641016Sdfr switch (sc->mode.level) { 174741016Sdfr case PSM_LEVEL_BASE: 174841016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 174941016Sdfr ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE; 175041016Sdfr break; 175141016Sdfr case PSM_LEVEL_STANDARD: 175241016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 175341016Sdfr ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE; 175441016Sdfr ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK; 175541016Sdfr ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC; 175641016Sdfr break; 175741016Sdfr case PSM_LEVEL_NATIVE: 175841016Sdfr /* FIXME: this isn't quite correct... XXX */ 175941016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 176041016Sdfr break; 176141016Sdfr } 176241016Sdfr splx(s); 176341016Sdfr break; 176441016Sdfr 176541016Sdfr case OLD_MOUSE_SETMODE: 176641016Sdfr case MOUSE_SETMODE: 176741016Sdfr if (cmd == OLD_MOUSE_SETMODE) { 176841016Sdfr mode.rate = ((old_mousemode_t *)addr)->rate; 176941016Sdfr /* 177041016Sdfr * resolution old I/F new I/F 177141016Sdfr * default 0 0 177241016Sdfr * low 1 -2 177341016Sdfr * medium low 2 -3 177441016Sdfr * medium high 3 -4 177541016Sdfr * high 4 -5 177641016Sdfr */ 177741016Sdfr if (((old_mousemode_t *)addr)->resolution > 0) 177841016Sdfr mode.resolution = -((old_mousemode_t *)addr)->resolution - 1; 177941016Sdfr mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor; 178041016Sdfr mode.level = -1; 178141016Sdfr } else { 178241016Sdfr mode = *(mousemode_t *)addr; 178341016Sdfr } 178441016Sdfr 178541016Sdfr /* adjust and validate parameters. */ 178641016Sdfr if (mode.rate > UCHAR_MAX) 178741016Sdfr return EINVAL; 178841016Sdfr if (mode.rate == 0) 178941016Sdfr mode.rate = sc->dflt_mode.rate; 179041016Sdfr else if (mode.rate == -1) 179141016Sdfr /* don't change the current setting */ 179241016Sdfr ; 179341016Sdfr else if (mode.rate < 0) 179441016Sdfr return EINVAL; 179541016Sdfr if (mode.resolution >= UCHAR_MAX) 179641016Sdfr return EINVAL; 179741016Sdfr if (mode.resolution >= 200) 179841016Sdfr mode.resolution = MOUSE_RES_HIGH; 179941016Sdfr else if (mode.resolution >= 100) 180041016Sdfr mode.resolution = MOUSE_RES_MEDIUMHIGH; 180141016Sdfr else if (mode.resolution >= 50) 180241016Sdfr mode.resolution = MOUSE_RES_MEDIUMLOW; 180341016Sdfr else if (mode.resolution > 0) 180441016Sdfr mode.resolution = MOUSE_RES_LOW; 180541016Sdfr if (mode.resolution == MOUSE_RES_DEFAULT) 180641016Sdfr mode.resolution = sc->dflt_mode.resolution; 180741016Sdfr else if (mode.resolution == -1) 180841016Sdfr /* don't change the current setting */ 180941016Sdfr ; 181041016Sdfr else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 181141016Sdfr mode.resolution = MOUSE_RES_LOW - mode.resolution; 181241016Sdfr if (mode.level == -1) 181341016Sdfr /* don't change the current setting */ 181441016Sdfr mode.level = sc->mode.level; 181541016Sdfr else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX)) 181641016Sdfr return EINVAL; 181741016Sdfr if (mode.accelfactor == -1) 181841016Sdfr /* don't change the current setting */ 181941016Sdfr mode.accelfactor = sc->mode.accelfactor; 182041016Sdfr else if (mode.accelfactor < 0) 182141016Sdfr return EINVAL; 182241016Sdfr 182341016Sdfr /* don't allow anybody to poll the keyboard controller */ 182441016Sdfr error = block_mouse_data(sc, &command_byte); 182541016Sdfr if (error) 182641016Sdfr return error; 182741016Sdfr 182841016Sdfr /* set mouse parameters */ 182941016Sdfr if (mode.rate > 0) 183041016Sdfr mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 183141016Sdfr if (mode.resolution >= 0) 183241016Sdfr mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 183341016Sdfr set_mouse_scaling(sc->kbdc, 1); 183441016Sdfr get_mouse_status(sc->kbdc, stat, 0, 3); 183541016Sdfr 183641016Sdfr s = spltty(); 183741016Sdfr sc->mode.rate = mode.rate; 183841016Sdfr sc->mode.resolution = mode.resolution; 183941016Sdfr sc->mode.accelfactor = mode.accelfactor; 184041016Sdfr sc->mode.level = mode.level; 184141016Sdfr splx(s); 184241016Sdfr 184341016Sdfr unblock_mouse_data(sc, command_byte); 184441016Sdfr break; 184541016Sdfr 184641016Sdfr case MOUSE_GETLEVEL: 184741016Sdfr *(int *)addr = sc->mode.level; 184841016Sdfr break; 184941016Sdfr 185041016Sdfr case MOUSE_SETLEVEL: 185141016Sdfr if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX)) 185241016Sdfr return EINVAL; 185341016Sdfr sc->mode.level = *(int *)addr; 185441016Sdfr break; 185541016Sdfr 185641016Sdfr case MOUSE_GETSTATUS: 185741016Sdfr s = spltty(); 185841016Sdfr status = sc->status; 185941016Sdfr sc->status.flags = 0; 186041016Sdfr sc->status.obutton = sc->status.button; 186141016Sdfr sc->status.button = 0; 186241016Sdfr sc->status.dx = 0; 186341016Sdfr sc->status.dy = 0; 186441016Sdfr sc->status.dz = 0; 186541016Sdfr splx(s); 186641016Sdfr *(mousestatus_t *)addr = status; 186741016Sdfr break; 186841016Sdfr 186941016Sdfr#if (defined(MOUSE_GETVARS)) 187041016Sdfr case MOUSE_GETVARS: 187141016Sdfr var = (mousevar_t *)addr; 187241016Sdfr bzero(var, sizeof(*var)); 187341016Sdfr s = spltty(); 187441016Sdfr var->var[0] = MOUSE_VARS_PS2_SIG; 187541016Sdfr var->var[1] = sc->config; 187641016Sdfr var->var[2] = sc->flags; 187741016Sdfr splx(s); 187841016Sdfr break; 187941016Sdfr 188041016Sdfr case MOUSE_SETVARS: 188141016Sdfr return ENODEV; 188241016Sdfr#endif /* MOUSE_GETVARS */ 188341016Sdfr 188441016Sdfr case MOUSE_READSTATE: 188541016Sdfr case MOUSE_READDATA: 188641016Sdfr data = (mousedata_t *)addr; 188741016Sdfr if (data->len > sizeof(data->buf)/sizeof(data->buf[0])) 188841016Sdfr return EINVAL; 188941016Sdfr 189041016Sdfr error = block_mouse_data(sc, &command_byte); 189141016Sdfr if (error) 189241016Sdfr return error; 189341016Sdfr if ((data->len = get_mouse_status(sc->kbdc, data->buf, 189441016Sdfr (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0) 189541016Sdfr error = EIO; 189641016Sdfr unblock_mouse_data(sc, command_byte); 189741016Sdfr break; 189841016Sdfr 189941016Sdfr#if (defined(MOUSE_SETRESOLUTION)) 190041016Sdfr case MOUSE_SETRESOLUTION: 190141016Sdfr mode.resolution = *(int *)addr; 190241016Sdfr if (mode.resolution >= UCHAR_MAX) 190341016Sdfr return EINVAL; 190441016Sdfr else if (mode.resolution >= 200) 190541016Sdfr mode.resolution = MOUSE_RES_HIGH; 190641016Sdfr else if (mode.resolution >= 100) 190741016Sdfr mode.resolution = MOUSE_RES_MEDIUMHIGH; 190841016Sdfr else if (mode.resolution >= 50) 190941016Sdfr mode.resolution = MOUSE_RES_MEDIUMLOW; 191041016Sdfr else if (mode.resolution > 0) 191141016Sdfr mode.resolution = MOUSE_RES_LOW; 191241016Sdfr if (mode.resolution == MOUSE_RES_DEFAULT) 191341016Sdfr mode.resolution = sc->dflt_mode.resolution; 191441016Sdfr else if (mode.resolution == -1) 191541016Sdfr mode.resolution = sc->mode.resolution; 191641016Sdfr else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 191741016Sdfr mode.resolution = MOUSE_RES_LOW - mode.resolution; 191841016Sdfr 191941016Sdfr error = block_mouse_data(sc, &command_byte); 192041016Sdfr if (error) 192141016Sdfr return error; 192241016Sdfr sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 192341016Sdfr if (sc->mode.resolution != mode.resolution) 192441016Sdfr error = EIO; 192541016Sdfr unblock_mouse_data(sc, command_byte); 192641016Sdfr break; 192741016Sdfr#endif /* MOUSE_SETRESOLUTION */ 192841016Sdfr 192941016Sdfr#if (defined(MOUSE_SETRATE)) 193041016Sdfr case MOUSE_SETRATE: 193141016Sdfr mode.rate = *(int *)addr; 193241016Sdfr if (mode.rate > UCHAR_MAX) 193341016Sdfr return EINVAL; 193441016Sdfr if (mode.rate == 0) 193541016Sdfr mode.rate = sc->dflt_mode.rate; 193641016Sdfr else if (mode.rate < 0) 193741016Sdfr mode.rate = sc->mode.rate; 193841016Sdfr 193941016Sdfr error = block_mouse_data(sc, &command_byte); 194041016Sdfr if (error) 194141016Sdfr return error; 194241016Sdfr sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 194341016Sdfr if (sc->mode.rate != mode.rate) 194441016Sdfr error = EIO; 194541016Sdfr unblock_mouse_data(sc, command_byte); 194641016Sdfr break; 194741016Sdfr#endif /* MOUSE_SETRATE */ 194841016Sdfr 194941016Sdfr#if (defined(MOUSE_SETSCALING)) 195041016Sdfr case MOUSE_SETSCALING: 195141016Sdfr if ((*(int *)addr <= 0) || (*(int *)addr > 2)) 195241016Sdfr return EINVAL; 195341016Sdfr 195441016Sdfr error = block_mouse_data(sc, &command_byte); 195541016Sdfr if (error) 195641016Sdfr return error; 195741016Sdfr if (!set_mouse_scaling(sc->kbdc, *(int *)addr)) 195841016Sdfr error = EIO; 195941016Sdfr unblock_mouse_data(sc, command_byte); 196041016Sdfr break; 196141016Sdfr#endif /* MOUSE_SETSCALING */ 196241016Sdfr 196341016Sdfr#if (defined(MOUSE_GETHWID)) 196441016Sdfr case MOUSE_GETHWID: 196541016Sdfr error = block_mouse_data(sc, &command_byte); 196641016Sdfr if (error) 196741016Sdfr return error; 196848778Syokota sc->hw.hwid &= ~0x00ff; 196948778Syokota sc->hw.hwid |= get_aux_id(sc->kbdc); 197048778Syokota *(int *)addr = sc->hw.hwid & 0x00ff; 197141016Sdfr unblock_mouse_data(sc, command_byte); 197241016Sdfr break; 197341016Sdfr#endif /* MOUSE_GETHWID */ 197441016Sdfr 197541016Sdfr default: 197641016Sdfr return ENOTTY; 197741016Sdfr } 197841016Sdfr 197941016Sdfr return error; 198041016Sdfr} 198141016Sdfr 198241016Sdfrstatic void 198358230Syokotapsmtimeout(void *arg) 198458230Syokota{ 198558230Syokota struct psm_softc *sc; 198665045Syokota int s; 198758230Syokota 198884880Syokota sc = (struct psm_softc *)arg; 198965045Syokota s = spltty(); 199063746Syokota if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) { 199158230Syokota if (verbose >= 4) 199284880Syokota log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit); 199358230Syokota psmintr(sc); 199463746Syokota kbdc_lock(sc->kbdc, FALSE); 199558230Syokota } 199658230Syokota sc->watchdog = TRUE; 199765045Syokota splx(s); 199884880Syokota sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz); 199958230Syokota} 200058230Syokota 2001123442Salfredstatic int psmhz = 20; 2002123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmhz, CTLFLAG_RW, &psmhz, 0, ""); 2003123442Salfred 2004123442Salfredstatic int psm_soft_timeout = 500000; /* 0.5 sec */ 2005123442SalfredSYSCTL_INT(_debug, OID_AUTO, psm_soft_timeout, CTLFLAG_RW, 2006123442Salfred &psm_soft_timeout, 0, ""); 2007123442Salfred 2008123442Salfredstatic int psmerrsecs = 2; 2009123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrsecs, CTLFLAG_RW, &psmerrsecs, 0, ""); 2010123442Salfredstatic int psmerrusecs = 0; 2011123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrusecs, CTLFLAG_RW, &psmerrusecs, 0, ""); 2012123442Salfredstatic int psmsecs = 0; 2013123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmsecs, CTLFLAG_RW, &psmsecs, 0, ""); 2014123442Salfredstatic int psmusecs = 500000; 2015123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmusecs, CTLFLAG_RW, &psmusecs, 0, ""); 2016123442Salfred 201758230Syokotastatic void 201841016Sdfrpsmintr(void *arg) 201941016Sdfr{ 2020123442Salfred struct psm_softc *sc = arg; 2021123442Salfred struct timeval now; 202241016Sdfr int c; 2023123442Salfred packetbuf_t *pb; 2024123442Salfred int haderror = 0; 202541016Sdfr 2026123442Salfred 202741016Sdfr /* read until there is nothing to read */ 202841016Sdfr while((c = read_aux_data_no_wait(sc->kbdc)) != -1) { 202941016Sdfr 2030123442Salfred pb = &sc->pqueue[sc->pqueue_end]; 203141016Sdfr /* discard the byte if the device is not open */ 203241016Sdfr if ((sc->state & PSM_OPEN) == 0) 203341016Sdfr continue; 203441016Sdfr 2035123442Salfred getmicrouptime(&now); 2036123442Salfred if ((pb->inputbytes > 0) && timevalcmp(&now, &sc->inputtimeout, >)) { 2037127841Smarkm#if DEBUG 203884880Syokota log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n"); 2039127841Smarkm#endif 2040123442Salfred pb->inputbytes = 0; 204184880Syokota sc->syncerrors = 0; 204284880Syokota } 204384880Syokota sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000; 204484880Syokota sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000; 2045123442Salfred timevaladd(&sc->inputtimeout, &now); 204684880Syokota 2047123442Salfred pb->ipacket[pb->inputbytes++] = c; 2048123442Salfred if (pb->inputbytes < sc->mode.packetsize) 204941016Sdfr continue; 205041016Sdfr 2051127841Smarkm#if DEBUG 205241016Sdfr log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n", 2053123442Salfred pb->ipacket[0], pb->ipacket[1], pb->ipacket[2], 2054123442Salfred pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]); 205541016Sdfr#endif 205641016Sdfr 2057123442Salfred c = pb->ipacket[0]; 205841016Sdfr 205963746Syokota if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) { 2060127841Smarkm#if DEBUG 2061123442Salfred log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x) %d" 2062123442Salfred " cmds since last error.\n", 2063123442Salfred c & sc->mode.syncmask[0], sc->mode.syncmask[1], 2064123442Salfred sc->cmdcount - sc->lasterr); 2065127841Smarkm#endif 2066123442Salfred haderror = 1; 2067123442Salfred sc->lasterr = sc->cmdcount; 2068123442Salfred dropqueue(sc); 206984880Syokota ++sc->syncerrors; 2070123442Salfred sc->lastinputerr = now; 207184880Syokota if (sc->syncerrors < sc->mode.packetsize) { 2072127841Smarkm#if DEBUG 207384880Syokota log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors); 2074127841Smarkm#endif 2075123442Salfred --pb->inputbytes; 2076123442Salfred bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes); 207784880Syokota } else if (sc->syncerrors == sc->mode.packetsize) { 2078127841Smarkm#if DEBUG 207969439Syokota log(LOG_DEBUG, "psmintr: re-enable the mouse.\n"); 2080127841Smarkm#endif 2081123442Salfred pb->inputbytes = 0; 208269439Syokota disable_aux_dev(sc->kbdc); 208369439Syokota enable_aux_dev(sc->kbdc); 208484880Syokota } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) { 2085127841Smarkm#if DEBUG 208684880Syokota log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors); 2087127841Smarkm#endif 2088123442Salfred --pb->inputbytes; 2089123442Salfred bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes); 209084880Syokota } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) { 2091127841Smarkm#if DEBUG 209284880Syokota log(LOG_DEBUG, "psmintr: reset the mouse.\n"); 2093127841Smarkm#endif 209484880Syokota reinitialize(sc, TRUE); 209569439Syokota } 209684880Syokota continue; 209763746Syokota } 2098123442Salfred /* if this packet is at all bogus then drop the packet. */ 2099123442Salfred if (haderror || 2100123442Salfred !timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, &now)) { 2101123442Salfred pb->inputbytes = 0; 2102123442Salfred haderror = 0; 2103123442Salfred continue; 2104123442Salfred } 210563746Syokota 2106123442Salfred sc->cmdcount++; 2107123442Salfred if (++sc->pqueue_end >= PSM_PACKETQUEUE) 2108123442Salfred sc->pqueue_end = 0; 2109123442Salfred /* 2110123442Salfred * If we've filled the queue then call the softintr ourselves, 2111123442Salfred * otherwise schedule the interrupt for later. 2112123442Salfred */ 2113123442Salfred if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) || 2114123442Salfred (sc->pqueue_end == sc->pqueue_start)) { 2115123442Salfred if ((sc->state & PSM_SOFTARMED) != 0) { 2116123442Salfred sc->state &= ~PSM_SOFTARMED; 2117123442Salfred untimeout(psmsoftintr, arg, sc->softcallout); 2118123442Salfred } 2119123442Salfred psmsoftintr(arg); 2120123442Salfred } else if ((sc->state & PSM_SOFTARMED) == 0) { 2121123442Salfred sc->state |= PSM_SOFTARMED; 2122123442Salfred sc->softcallout = timeout(psmsoftintr, arg, 2123123442Salfred psmhz < 1 ? 1 : (hz/psmhz)); 2124123442Salfred } 2125123442Salfred } 2126123442Salfred} 2127123442Salfred 2128123442Salfredstatic void 2129123442Salfredpsmsoftintr(void *arg) 2130123442Salfred{ 2131123442Salfred /* 2132123442Salfred * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN) 2133123442Salfred * into `mousestatus' button bits (MOUSE_BUTTON?DOWN). 2134123442Salfred */ 2135123442Salfred static int butmap[8] = { 2136123442Salfred 0, 2137123442Salfred MOUSE_BUTTON1DOWN, 2138123442Salfred MOUSE_BUTTON3DOWN, 2139123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 2140123442Salfred MOUSE_BUTTON2DOWN, 2141123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 2142123442Salfred MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 2143123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 2144123442Salfred }; 2145123442Salfred static int butmap_versapad[8] = { 2146123442Salfred 0, 2147123442Salfred MOUSE_BUTTON3DOWN, 2148123442Salfred 0, 2149123442Salfred MOUSE_BUTTON3DOWN, 2150123442Salfred MOUSE_BUTTON1DOWN, 2151123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 2152123442Salfred MOUSE_BUTTON1DOWN, 2153123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN 2154123442Salfred }; 2155123442Salfred register struct psm_softc *sc = arg; 2156123442Salfred mousestatus_t ms; 2157123442Salfred int x, y, z; 2158123442Salfred int c; 2159123442Salfred int l; 2160123442Salfred int x0, y0; 2161123442Salfred int s; 2162123442Salfred packetbuf_t *pb; 2163123442Salfred 2164123442Salfred getmicrouptime(&sc->lastsoftintr); 2165123442Salfred 2166123442Salfred s = spltty(); 2167123442Salfred 2168123442Salfred do { 2169123442Salfred 2170123442Salfred pb = &sc->pqueue[sc->pqueue_start]; 2171123442Salfred c = pb->ipacket[0]; 217249965Syokota /* 217341016Sdfr * A kludge for Kensington device! 217441016Sdfr * The MSB of the horizontal count appears to be stored in 217558230Syokota * a strange place. 217641016Sdfr */ 217758230Syokota if (sc->hw.model == MOUSE_MODEL_THINK) 2178123442Salfred pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0; 217941016Sdfr 218041016Sdfr /* ignore the overflow bits... */ 2181123442Salfred x = (c & MOUSE_PS2_XNEG) ? pb->ipacket[1] - 256 : pb->ipacket[1]; 2182123442Salfred y = (c & MOUSE_PS2_YNEG) ? pb->ipacket[2] - 256 : pb->ipacket[2]; 218341016Sdfr z = 0; 218441016Sdfr ms.obutton = sc->button; /* previous button state */ 218541016Sdfr ms.button = butmap[c & MOUSE_PS2_BUTTONS]; 218645789Speter /* `tapping' action */ 218745789Speter if (sc->config & PSM_CONFIG_FORCETAP) 218845789Speter ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 218941016Sdfr 219041016Sdfr switch (sc->hw.model) { 219141016Sdfr 219258230Syokota case MOUSE_MODEL_EXPLORER: 219358230Syokota /* 219458230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 219558230Syokota * byte 1: oy ox sy sx 1 M R L 219658230Syokota * byte 2: x x x x x x x x 219758230Syokota * byte 3: y y y y y y y y 219858230Syokota * byte 4: * * S2 S1 s d2 d1 d0 219958230Syokota * 220058230Syokota * L, M, R, S1, S2: left, middle, right and side buttons 220158230Syokota * s: wheel data sign bit 220258230Syokota * d2-d0: wheel data 220358230Syokota */ 2204123442Salfred z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) 2205123442Salfred ? (pb->ipacket[3] & 0x0f) - 16 : (pb->ipacket[3] & 0x0f); 2206123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) 220758230Syokota ? MOUSE_BUTTON4DOWN : 0; 2208123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) 220958230Syokota ? MOUSE_BUTTON5DOWN : 0; 221058230Syokota break; 221158230Syokota 221241016Sdfr case MOUSE_MODEL_INTELLI: 221341016Sdfr case MOUSE_MODEL_NET: 221441016Sdfr /* wheel data is in the fourth byte */ 2215123442Salfred z = (char)pb->ipacket[3]; 221658230Syokota /* some mice may send 7 when there is no Z movement?! XXX */ 221758230Syokota if ((z >= 7) || (z <= -7)) 221858230Syokota z = 0; 221958230Syokota /* some compatible mice have additional buttons */ 222058230Syokota ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) 222158230Syokota ? MOUSE_BUTTON4DOWN : 0; 222258230Syokota ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) 222358230Syokota ? MOUSE_BUTTON5DOWN : 0; 222441016Sdfr break; 222541016Sdfr 222641016Sdfr case MOUSE_MODEL_MOUSEMANPLUS: 222748778Syokota /* 222848778Syokota * PS2++ protocl packet 222948778Syokota * 223048778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 223148778Syokota * byte 1: * 1 p3 p2 1 * * * 223248778Syokota * byte 2: c1 c2 p1 p0 d1 d0 1 0 223348778Syokota * 223448778Syokota * p3-p0: packet type 223548778Syokota * c1, c2: c1 & c2 == 1, if p2 == 0 223648778Syokota * c1 & c2 == 0, if p2 == 1 223748778Syokota * 223848778Syokota * packet type: 0 (device type) 223948778Syokota * See comments in enable_mmanplus() below. 224048778Syokota * 224148778Syokota * packet type: 1 (wheel data) 224248778Syokota * 224348778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 224448778Syokota * byte 3: h * B5 B4 s d2 d1 d0 224548778Syokota * 224648778Syokota * h: 1, if horizontal roller data 224748778Syokota * 0, if vertical roller data 224848778Syokota * B4, B5: button 4 and 5 224948778Syokota * s: sign bit 225048778Syokota * d2-d0: roller data 225148778Syokota * 225248778Syokota * packet type: 2 (reserved) 225348778Syokota */ 225448778Syokota if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) 225548778Syokota && (abs(x) > 191) 2256123442Salfred && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) { 225741016Sdfr /* the extended data packet encodes button and wheel events */ 2258123442Salfred switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) { 225948778Syokota case 1: 226048778Syokota /* wheel data packet */ 226148778Syokota x = y = 0; 2262123442Salfred if (pb->ipacket[2] & 0x80) { 226348778Syokota /* horizontal roller count - ignore it XXX*/ 226448778Syokota } else { 226548778Syokota /* vertical roller count */ 2266123442Salfred z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) 2267123442Salfred ? (pb->ipacket[2] & 0x0f) - 16 2268123442Salfred : (pb->ipacket[2] & 0x0f); 226948778Syokota } 2270123442Salfred ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN) 227148778Syokota ? MOUSE_BUTTON4DOWN : 0; 2272123442Salfred ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN) 227348778Syokota ? MOUSE_BUTTON5DOWN : 0; 227448778Syokota break; 227548778Syokota case 2: 227658230Syokota /* this packet type is reserved by Logitech... */ 227758230Syokota /* 227858230Syokota * IBM ScrollPoint Mouse uses this packet type to 227958230Syokota * encode both vertical and horizontal scroll movement. 228058230Syokota */ 228158230Syokota x = y = 0; 228258230Syokota /* horizontal count */ 2283123442Salfred if (pb->ipacket[2] & 0x0f) 2284123442Salfred z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2; 228558230Syokota /* vertical count */ 2286123442Salfred if (pb->ipacket[2] & 0xf0) 2287123442Salfred z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1; 228858230Syokota#if 0 228958230Syokota /* vertical count */ 2290123442Salfred z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) 2291123442Salfred ? ((pb->ipacket[2] >> 4) & 0x0f) - 16 2292123442Salfred : ((pb->ipacket[2] >> 4) & 0x0f); 229358230Syokota /* horizontal count */ 2294123442Salfred w = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) 2295123442Salfred ? (pb->ipacket[2] & 0x0f) - 16 2296123442Salfred : (pb->ipacket[2] & 0x0f); 229758230Syokota#endif 229858230Syokota break; 229948778Syokota case 0: 230048778Syokota /* device type packet - shouldn't happen */ 2301102412Scharnier /* FALLTHROUGH */ 230248778Syokota default: 230348778Syokota x = y = 0; 230448778Syokota ms.button = ms.obutton; 230558230Syokota if (bootverbose) 230658230Syokota log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: " 230758230Syokota "0x%02x 0x%02x 0x%02x\n", 2308123442Salfred MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket), 2309123442Salfred pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]); 231048778Syokota break; 231148778Syokota } 231241016Sdfr } else { 231341016Sdfr /* preserve button states */ 231441016Sdfr ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 231541016Sdfr } 231641016Sdfr break; 231741016Sdfr 231841016Sdfr case MOUSE_MODEL_GLIDEPOINT: 231941016Sdfr /* `tapping' action */ 232041016Sdfr ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 232141016Sdfr break; 232241016Sdfr 232341016Sdfr case MOUSE_MODEL_NETSCROLL: 232458230Syokota /* three addtional bytes encode buttons and wheel events */ 2325123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) 232641016Sdfr ? MOUSE_BUTTON4DOWN : 0; 2327123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) 232858230Syokota ? MOUSE_BUTTON5DOWN : 0; 2329123442Salfred z = (pb->ipacket[3] & MOUSE_PS2_XNEG) 2330123442Salfred ? pb->ipacket[4] - 256 : pb->ipacket[4]; 233141016Sdfr break; 233241016Sdfr 233341016Sdfr case MOUSE_MODEL_THINK: 233441016Sdfr /* the fourth button state in the first byte */ 233541016Sdfr ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0; 233641016Sdfr break; 233741016Sdfr 233849965Syokota case MOUSE_MODEL_VERSAPAD: 233949965Syokota /* VersaPad PS/2 absolute mode message format 234049965Syokota * 234149965Syokota * [packet1] 7 6 5 4 3 2 1 0(LSB) 234249965Syokota * ipacket[0]: 1 1 0 A 1 L T R 234349965Syokota * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0 234449965Syokota * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0 234549965Syokota * ipacket[3]: 1 1 1 A 1 L T R 234649965Syokota * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8 234749965Syokota * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0 234849965Syokota * 234949965Syokota * [note] 235049965Syokota * R: right physical mouse button (1=on) 235149965Syokota * T: touch pad virtual button (1=tapping) 235249965Syokota * L: left physical mouse button (1=on) 235349965Syokota * A: position data is valid (1=valid) 235449965Syokota * H: horizontal data (12bit signed integer. H11 is sign bit.) 235549965Syokota * V: vertical data (12bit signed integer. V11 is sign bit.) 235649965Syokota * P: pressure data 235749965Syokota * 235849965Syokota * Tapping is mapped to MOUSE_BUTTON4. 235949965Syokota */ 236049965Syokota ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS]; 236149965Syokota ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 236249965Syokota x = y = 0; 236349965Syokota if (c & MOUSE_PS2VERSA_IN_USE) { 2364123442Salfred x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8); 2365123442Salfred y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4); 236649965Syokota if (x0 & 0x800) 236749965Syokota x0 -= 0x1000; 236849965Syokota if (y0 & 0x800) 236949965Syokota y0 -= 0x1000; 237049965Syokota if (sc->flags & PSM_FLAGS_FINGERDOWN) { 237149965Syokota x = sc->xold - x0; 237249965Syokota y = y0 - sc->yold; 237349965Syokota if (x < 0) /* XXX */ 237449965Syokota x++; 237549965Syokota else if (x) 237649965Syokota x--; 237749965Syokota if (y < 0) 237849965Syokota y++; 237949965Syokota else if (y) 238049965Syokota y--; 238149965Syokota } else { 238249965Syokota sc->flags |= PSM_FLAGS_FINGERDOWN; 238349965Syokota } 238449965Syokota sc->xold = x0; 238549965Syokota sc->yold = y0; 238649965Syokota } else { 238749965Syokota sc->flags &= ~PSM_FLAGS_FINGERDOWN; 238849965Syokota } 238949965Syokota c = ((x < 0) ? MOUSE_PS2_XNEG : 0) 239049965Syokota | ((y < 0) ? MOUSE_PS2_YNEG : 0); 239149965Syokota break; 239249965Syokota 239358230Syokota case MOUSE_MODEL_4D: 239458230Syokota /* 239558230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 239658230Syokota * byte 1: s2 d2 s1 d1 1 M R L 239758230Syokota * byte 2: sx x x x x x x x 239858230Syokota * byte 3: sy y y y y y y y 239958230Syokota * 240058230Syokota * s1: wheel 1 direction 240158230Syokota * d1: wheel 1 data 240258230Syokota * s2: wheel 2 direction 240358230Syokota * d2: wheel 2 data 240458230Syokota */ 2405123442Salfred x = (pb->ipacket[1] & 0x80) ? pb->ipacket[1] - 256 : pb->ipacket[1]; 2406123442Salfred y = (pb->ipacket[2] & 0x80) ? pb->ipacket[2] - 256 : pb->ipacket[2]; 240758230Syokota switch (c & MOUSE_4D_WHEELBITS) { 240858230Syokota case 0x10: 240958230Syokota z = 1; 241058230Syokota break; 241158230Syokota case 0x30: 241258230Syokota z = -1; 241358230Syokota break; 241458230Syokota case 0x40: /* 2nd wheel turning right XXX */ 241558230Syokota z = 2; 241658230Syokota break; 241758230Syokota case 0xc0: /* 2nd wheel turning left XXX */ 241858230Syokota z = -2; 241958230Syokota break; 242058230Syokota } 242158230Syokota break; 242258230Syokota 242358230Syokota case MOUSE_MODEL_4DPLUS: 242458230Syokota if ((x < 16 - 256) && (y < 16 - 256)) { 242558230Syokota /* 242658230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 242758230Syokota * byte 1: 0 0 1 1 1 M R L 242858230Syokota * byte 2: 0 0 0 0 1 0 0 0 242958230Syokota * byte 3: 0 0 0 0 S s d1 d0 243058230Syokota * 243158230Syokota * L, M, R, S: left, middle, right and side buttons 243258230Syokota * s: wheel data sign bit 243358230Syokota * d1-d0: wheel data 243458230Syokota */ 243558230Syokota x = y = 0; 2436123442Salfred if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN) 243758230Syokota ms.button |= MOUSE_BUTTON4DOWN; 2438123442Salfred z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) 2439123442Salfred ? ((pb->ipacket[2] & 0x07) - 8) 2440123442Salfred : (pb->ipacket[2] & 0x07) ; 244158230Syokota } else { 244258230Syokota /* preserve previous button states */ 244358230Syokota ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 244458230Syokota } 244558230Syokota break; 244658230Syokota 244741016Sdfr case MOUSE_MODEL_GENERIC: 244841016Sdfr default: 244941016Sdfr break; 245041016Sdfr } 245141016Sdfr 245241016Sdfr /* scale values */ 245341016Sdfr if (sc->mode.accelfactor >= 1) { 245441016Sdfr if (x != 0) { 245541016Sdfr x = x * x / sc->mode.accelfactor; 245641016Sdfr if (x == 0) 245741016Sdfr x = 1; 245841016Sdfr if (c & MOUSE_PS2_XNEG) 245941016Sdfr x = -x; 246041016Sdfr } 246141016Sdfr if (y != 0) { 246241016Sdfr y = y * y / sc->mode.accelfactor; 246341016Sdfr if (y == 0) 246441016Sdfr y = 1; 246541016Sdfr if (c & MOUSE_PS2_YNEG) 246641016Sdfr y = -y; 246741016Sdfr } 246841016Sdfr } 246941016Sdfr 247041016Sdfr ms.dx = x; 247141016Sdfr ms.dy = y; 247241016Sdfr ms.dz = z; 247341016Sdfr ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) 247441016Sdfr | (ms.obutton ^ ms.button); 247541016Sdfr 247641016Sdfr if (sc->mode.level < PSM_LEVEL_NATIVE) 2477123442Salfred pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket); 247841016Sdfr 247941016Sdfr sc->status.flags |= ms.flags; 248041016Sdfr sc->status.dx += ms.dx; 248141016Sdfr sc->status.dy += ms.dy; 248241016Sdfr sc->status.dz += ms.dz; 248341016Sdfr sc->status.button = ms.button; 248441016Sdfr sc->button = ms.button; 248541016Sdfr 248658230Syokota sc->watchdog = FALSE; 248758230Syokota 248841016Sdfr /* queue data */ 2489123442Salfred if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) { 2490123442Salfred l = imin(pb->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail); 2491123442Salfred bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l); 2492123442Salfred if (pb->inputbytes > l) 2493123442Salfred bcopy(&pb->ipacket[l], &sc->queue.buf[0], pb->inputbytes - l); 249441016Sdfr sc->queue.tail = 2495123442Salfred (sc->queue.tail + pb->inputbytes) % sizeof(sc->queue.buf); 2496123442Salfred sc->queue.count += pb->inputbytes; 249741016Sdfr } 2498123442Salfred pb->inputbytes = 0; 249941016Sdfr 2500123442Salfred if (++sc->pqueue_start >= PSM_PACKETQUEUE) 2501123442Salfred sc->pqueue_start = 0; 2502123442Salfred } while (sc->pqueue_start != sc->pqueue_end); 2503123442Salfred if (sc->state & PSM_ASLP) { 2504123442Salfred sc->state &= ~PSM_ASLP; 2505123442Salfred wakeup( sc); 250641016Sdfr } 2507123442Salfred selwakeuppri(&sc->rsel, PZERO); 2508123442Salfred sc->state &= ~PSM_SOFTARMED; 2509123442Salfred splx(s); 251041016Sdfr} 251141016Sdfr 251241016Sdfrstatic int 2513130585Sphkpsmpoll(struct cdev *dev, int events, struct thread *td) 251441016Sdfr{ 251541016Sdfr struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 251641016Sdfr int s; 251741016Sdfr int revents = 0; 251841016Sdfr 251941016Sdfr /* Return true if a mouse event available */ 252041016Sdfr s = spltty(); 252145789Speter if (events & (POLLIN | POLLRDNORM)) { 252241016Sdfr if (sc->queue.count > 0) 252341016Sdfr revents |= events & (POLLIN | POLLRDNORM); 252441016Sdfr else 252583366Sjulian selrecord(td, &sc->rsel); 252645789Speter } 252741016Sdfr splx(s); 252841016Sdfr 252941016Sdfr return (revents); 253041016Sdfr} 253141016Sdfr 253241016Sdfr/* vendor/model specific routines */ 253341016Sdfr 253441016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status) 253541016Sdfr{ 253641016Sdfr if (set_mouse_resolution(kbdc, res) != res) 253741016Sdfr return FALSE; 253841016Sdfr if (set_mouse_scaling(kbdc, scale) 253941016Sdfr && set_mouse_scaling(kbdc, scale) 254041016Sdfr && set_mouse_scaling(kbdc, scale) 254141016Sdfr && (get_mouse_status(kbdc, status, 0, 3) >= 3)) 254241016Sdfr return TRUE; 254341016Sdfr return FALSE; 254441016Sdfr} 254541016Sdfr 254669438Syokotastatic int 254769438Syokotamouse_ext_command(KBDC kbdc, int command) 254869438Syokota{ 254969438Syokota int c; 255069438Syokota 255169438Syokota c = (command >> 6) & 0x03; 255269438Syokota if (set_mouse_resolution(kbdc, c) != c) 255369438Syokota return FALSE; 255469438Syokota c = (command >> 4) & 0x03; 255569438Syokota if (set_mouse_resolution(kbdc, c) != c) 255669438Syokota return FALSE; 255769438Syokota c = (command >> 2) & 0x03; 255869438Syokota if (set_mouse_resolution(kbdc, c) != c) 255969438Syokota return FALSE; 256069438Syokota c = (command >> 0) & 0x03; 256169438Syokota if (set_mouse_resolution(kbdc, c) != c) 256269438Syokota return FALSE; 256369438Syokota return TRUE; 256469438Syokota} 256569438Syokota 256641016Sdfr#if notyet 256741016Sdfr/* Logitech MouseMan Cordless II */ 256841016Sdfrstatic int 256941016Sdfrenable_lcordless(struct psm_softc *sc) 257041016Sdfr{ 257141016Sdfr int status[3]; 257241016Sdfr int ch; 257341016Sdfr 257441016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status)) 257541016Sdfr return FALSE; 257641016Sdfr if (status[1] == PSMD_RES_HIGH) 257741016Sdfr return FALSE; 257841016Sdfr ch = (status[0] & 0x07) - 1; /* channel # */ 257941016Sdfr if ((ch <= 0) || (ch > 4)) 258041016Sdfr return FALSE; 258141016Sdfr /* 258241016Sdfr * status[1]: always one? 258341016Sdfr * status[2]: battery status? (0-100) 258441016Sdfr */ 258541016Sdfr return TRUE; 258641016Sdfr} 258741016Sdfr#endif /* notyet */ 258841016Sdfr 258958230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */ 259041016Sdfrstatic int 259141016Sdfrenable_groller(struct psm_softc *sc) 259241016Sdfr{ 259341016Sdfr int status[3]; 259441016Sdfr 259541016Sdfr /* 259641016Sdfr * The special sequence to enable the fourth button and the 259741016Sdfr * roller. Immediately after this sequence check status bytes. 259841016Sdfr * if the mouse is NetScroll, the second and the third bytes are 259941016Sdfr * '3' and 'D'. 260041016Sdfr */ 260141016Sdfr 260241016Sdfr /* 260341016Sdfr * If the mouse is an ordinary PS/2 mouse, the status bytes should 260441016Sdfr * look like the following. 260541016Sdfr * 260641016Sdfr * byte 1 bit 7 always 0 260741016Sdfr * bit 6 stream mode (0) 260841016Sdfr * bit 5 disabled (0) 260941016Sdfr * bit 4 1:1 scaling (0) 261041016Sdfr * bit 3 always 0 261141016Sdfr * bit 0-2 button status 261241016Sdfr * byte 2 resolution (PSMD_RES_HIGH) 261341016Sdfr * byte 3 report rate (?) 261441016Sdfr */ 261541016Sdfr 261641016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 261741016Sdfr return FALSE; 261841016Sdfr if ((status[1] != '3') || (status[2] != 'D')) 261941016Sdfr return FALSE; 262058230Syokota /* FIXME: SmartScroll Mouse has 5 buttons! XXX */ 262141016Sdfr sc->hw.buttons = 4; 262241016Sdfr return TRUE; 262341016Sdfr} 262441016Sdfr 262558230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */ 262641016Sdfrstatic int 262741016Sdfrenable_gmouse(struct psm_softc *sc) 262841016Sdfr{ 262941016Sdfr int status[3]; 263041016Sdfr 263141016Sdfr /* 263241016Sdfr * The special sequence to enable the middle, "rubber" button. 263341016Sdfr * Immediately after this sequence check status bytes. 263441016Sdfr * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse, 263541016Sdfr * the second and the third bytes are '3' and 'U'. 263641016Sdfr * NOTE: NetMouse reports that it has three buttons although it has 263741016Sdfr * two buttons and a rubber button. NetMouse Pro and MIE Mouse 263841016Sdfr * say they have three buttons too and they do have a button on the 263941016Sdfr * side... 264041016Sdfr */ 264141016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 264241016Sdfr return FALSE; 264341016Sdfr if ((status[1] != '3') || (status[2] != 'U')) 264441016Sdfr return FALSE; 264541016Sdfr return TRUE; 264641016Sdfr} 264741016Sdfr 264841016Sdfr/* ALPS GlidePoint */ 264941016Sdfrstatic int 265041016Sdfrenable_aglide(struct psm_softc *sc) 265141016Sdfr{ 265241016Sdfr int status[3]; 265341016Sdfr 265441016Sdfr /* 265541016Sdfr * The special sequence to obtain ALPS GlidePoint specific 265641016Sdfr * information. Immediately after this sequence, status bytes will 265741016Sdfr * contain something interesting. 265841016Sdfr * NOTE: ALPS produces several models of GlidePoint. Some of those 265941016Sdfr * do not respond to this sequence, thus, cannot be detected this way. 266041016Sdfr */ 266150149Syokota if (set_mouse_sampling_rate(sc->kbdc, 100) != 100) 266250149Syokota return FALSE; 266341016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status)) 266441016Sdfr return FALSE; 266550149Syokota if ((status[1] == PSMD_RES_LOW) || (status[2] == 100)) 266641016Sdfr return FALSE; 266741016Sdfr return TRUE; 266841016Sdfr} 266941016Sdfr 267041016Sdfr/* Kensington ThinkingMouse/Trackball */ 267141016Sdfrstatic int 267241016Sdfrenable_kmouse(struct psm_softc *sc) 267341016Sdfr{ 267441016Sdfr static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 }; 267541016Sdfr KBDC kbdc = sc->kbdc; 267641016Sdfr int status[3]; 267741016Sdfr int id1; 267841016Sdfr int id2; 267941016Sdfr int i; 268041016Sdfr 268141016Sdfr id1 = get_aux_id(kbdc); 268241016Sdfr if (set_mouse_sampling_rate(kbdc, 10) != 10) 268341016Sdfr return FALSE; 268441016Sdfr /* 268541016Sdfr * The device is now in the native mode? It returns a different 268641016Sdfr * ID value... 268741016Sdfr */ 268841016Sdfr id2 = get_aux_id(kbdc); 268941016Sdfr if ((id1 == id2) || (id2 != 2)) 269041016Sdfr return FALSE; 269141016Sdfr 269241016Sdfr if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 269341016Sdfr return FALSE; 269441016Sdfr#if PSM_DEBUG >= 2 269541016Sdfr /* at this point, resolution is LOW, sampling rate is 10/sec */ 269641016Sdfr if (get_mouse_status(kbdc, status, 0, 3) < 3) 269741016Sdfr return FALSE; 269841016Sdfr#endif 269941016Sdfr 270041016Sdfr /* 270141016Sdfr * The special sequence to enable the third and fourth buttons. 270241016Sdfr * Otherwise they behave like the first and second buttons. 270341016Sdfr */ 270441016Sdfr for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 270541016Sdfr if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 270641016Sdfr return FALSE; 270741016Sdfr } 270841016Sdfr 270941016Sdfr /* 271041016Sdfr * At this point, the device is using default resolution and 271141016Sdfr * sampling rate for the native mode. 271241016Sdfr */ 271341016Sdfr if (get_mouse_status(kbdc, status, 0, 3) < 3) 271441016Sdfr return FALSE; 271541016Sdfr if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1])) 271641016Sdfr return FALSE; 271741016Sdfr 271841016Sdfr /* the device appears be enabled by this sequence, diable it for now */ 271941016Sdfr disable_aux_dev(kbdc); 272041016Sdfr empty_aux_buffer(kbdc, 5); 272141016Sdfr 272241016Sdfr return TRUE; 272341016Sdfr} 272441016Sdfr 272558230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */ 272641016Sdfrstatic int 272741016Sdfrenable_mmanplus(struct psm_softc *sc) 272841016Sdfr{ 272941016Sdfr KBDC kbdc = sc->kbdc; 273041016Sdfr int data[3]; 273141016Sdfr 273241016Sdfr /* the special sequence to enable the fourth button and the roller. */ 273358230Syokota /* 273458230Syokota * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION 273558230Syokota * must be called exactly three times since the last RESET command 273658230Syokota * before this sequence. XXX 273758230Syokota */ 273869438Syokota if (!set_mouse_scaling(kbdc, 1)) 273969438Syokota return FALSE; 274069438Syokota if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb)) 274169438Syokota return FALSE; 274241016Sdfr if (get_mouse_status(kbdc, data, 1, 3) < 3) 274341016Sdfr return FALSE; 274441016Sdfr 274548778Syokota /* 274648778Syokota * PS2++ protocl, packet type 0 274741016Sdfr * 274848778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 274948778Syokota * byte 1: * 1 p3 p2 1 * * * 275048778Syokota * byte 2: 1 1 p1 p0 m1 m0 1 0 275148778Syokota * byte 3: m7 m6 m5 m4 m3 m2 m1 m0 275248778Syokota * 275348778Syokota * p3-p0: packet type: 0 275458230Syokota * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58... 275541016Sdfr */ 275648778Syokota /* check constant bits */ 275748778Syokota if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC) 275841016Sdfr return FALSE; 275948778Syokota if ((data[1] & 0xc3) != 0xc2) 276048778Syokota return FALSE; 276148778Syokota /* check d3-d0 in byte 2 */ 276248778Syokota if (!MOUSE_PS2PLUS_CHECKBITS(data)) 276348778Syokota return FALSE; 276448778Syokota /* check p3-p0 */ 276548778Syokota if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0) 276648778Syokota return FALSE; 276741016Sdfr 276848778Syokota sc->hw.hwid &= 0x00ff; 276948778Syokota sc->hw.hwid |= data[2] << 8; /* save model ID */ 277048778Syokota 277141016Sdfr /* 277241016Sdfr * MouseMan+ (or FirstMouse+) is now in its native mode, in which 277341016Sdfr * the wheel and the fourth button events are encoded in the 277441016Sdfr * special data packet. The mouse may be put in the IntelliMouse mode 277541016Sdfr * if it is initialized by the IntelliMouse's method. 277641016Sdfr */ 277741016Sdfr return TRUE; 277841016Sdfr} 277941016Sdfr 278058230Syokota/* MS IntelliMouse Explorer */ 278158230Syokotastatic int 278258230Syokotaenable_msexplorer(struct psm_softc *sc) 278358230Syokota{ 278458923Syokota static unsigned char rate0[] = { 200, 100, 80, }; 278558923Syokota static unsigned char rate1[] = { 200, 200, 80, }; 278658230Syokota KBDC kbdc = sc->kbdc; 278758230Syokota int id; 278858230Syokota int i; 278958230Syokota 279069438Syokota /* the special sequence to enable the extra buttons and the roller. */ 279169438Syokota for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) { 279269438Syokota if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i]) 279369438Syokota return FALSE; 279469438Syokota } 279569438Syokota /* the device will give the genuine ID only after the above sequence */ 279669438Syokota id = get_aux_id(kbdc); 279769438Syokota if (id != PSM_EXPLORER_ID) 279869438Syokota return FALSE; 279969438Syokota 280069438Syokota sc->hw.hwid = id; 280169438Syokota sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */ 280269438Syokota 280358923Syokota /* 280458923Syokota * XXX: this is a kludge to fool some KVM switch products 280558923Syokota * which think they are clever enough to know the 4-byte IntelliMouse 280658923Syokota * protocol, and assume any other protocols use 3-byte packets. 280758923Syokota * They don't convey 4-byte data packets from the IntelliMouse Explorer 280858923Syokota * correctly to the host computer because of this! 280958923Syokota * The following sequence is actually IntelliMouse's "wake up" 281058923Syokota * sequence; it will make the KVM think the mouse is IntelliMouse 281158923Syokota * when it is in fact IntelliMouse Explorer. 281258923Syokota */ 281358923Syokota for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) { 281458923Syokota if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i]) 281569438Syokota break; 281658923Syokota } 281758923Syokota id = get_aux_id(kbdc); 281858923Syokota 281958230Syokota return TRUE; 282058230Syokota} 282158230Syokota 282241016Sdfr/* MS IntelliMouse */ 282341016Sdfrstatic int 282441016Sdfrenable_msintelli(struct psm_softc *sc) 282541016Sdfr{ 282641016Sdfr /* 282741016Sdfr * Logitech MouseMan+ and FirstMouse+ will also respond to this 282841016Sdfr * probe routine and act like IntelliMouse. 282941016Sdfr */ 283041016Sdfr 283141016Sdfr static unsigned char rate[] = { 200, 100, 80, }; 283241016Sdfr KBDC kbdc = sc->kbdc; 283341016Sdfr int id; 283441016Sdfr int i; 283541016Sdfr 283641016Sdfr /* the special sequence to enable the third button and the roller. */ 283741016Sdfr for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 283841016Sdfr if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 283941016Sdfr return FALSE; 284041016Sdfr } 284141016Sdfr /* the device will give the genuine ID only after the above sequence */ 284241016Sdfr id = get_aux_id(kbdc); 284341016Sdfr if (id != PSM_INTELLI_ID) 284441016Sdfr return FALSE; 284541016Sdfr 284641016Sdfr sc->hw.hwid = id; 284741016Sdfr sc->hw.buttons = 3; 284841016Sdfr 284941016Sdfr return TRUE; 285041016Sdfr} 285141016Sdfr 285258230Syokota/* A4 Tech 4D Mouse */ 285358230Syokotastatic int 285458230Syokotaenable_4dmouse(struct psm_softc *sc) 285558230Syokota{ 285658230Syokota /* 285758230Syokota * Newer wheel mice from A4 Tech may use the 4D+ protocol. 285858230Syokota */ 285958230Syokota 286058230Syokota static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 286158230Syokota KBDC kbdc = sc->kbdc; 286258230Syokota int id; 286358230Syokota int i; 286458230Syokota 286558230Syokota for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 286658230Syokota if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 286758230Syokota return FALSE; 286858230Syokota } 286958230Syokota id = get_aux_id(kbdc); 287058230Syokota /* 287158923Syokota * WinEasy 4D, 4 Way Scroll 4D: 6 287258230Syokota * Cable-Free 4D: 8 (4DPLUS) 287358923Syokota * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS) 287458230Syokota */ 287558230Syokota if (id != PSM_4DMOUSE_ID) 287658230Syokota return FALSE; 287758230Syokota 287858230Syokota sc->hw.hwid = id; 287958230Syokota sc->hw.buttons = 3; /* XXX some 4D mice have 4? */ 288058230Syokota 288158230Syokota return TRUE; 288258230Syokota} 288358230Syokota 288458230Syokota/* A4 Tech 4D+ Mouse */ 288558230Syokotastatic int 288658230Syokotaenable_4dplus(struct psm_softc *sc) 288758230Syokota{ 288858230Syokota /* 288958230Syokota * Newer wheel mice from A4 Tech seem to use this protocol. 289058230Syokota * Older models are recognized as either 4D Mouse or IntelliMouse. 289158230Syokota */ 289258230Syokota KBDC kbdc = sc->kbdc; 289358230Syokota int id; 289458230Syokota 289558230Syokota /* 289658230Syokota * enable_4dmouse() already issued the following ID sequence... 289758230Syokota static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 289858230Syokota int i; 289958230Syokota 290058230Syokota for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 290158230Syokota if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 290258230Syokota return FALSE; 290358230Syokota } 290458230Syokota */ 290558230Syokota 290658230Syokota id = get_aux_id(kbdc); 2907117478Smikeh switch (id) { 2908117478Smikeh case PSM_4DPLUS_ID: 2909117478Smikeh sc->hw.buttons = 4; 2910117478Smikeh break; 2911117478Smikeh case PSM_4DPLUS_RFSW35_ID: 2912117478Smikeh sc->hw.buttons = 3; 2913117478Smikeh break; 2914117478Smikeh default: 2915117478Smikeh return FALSE; 2916117478Smikeh } 291758230Syokota 291858230Syokota sc->hw.hwid = id; 291958230Syokota 292058230Syokota return TRUE; 292158230Syokota} 292258230Syokota 292349965Syokota/* Interlink electronics VersaPad */ 292449965Syokotastatic int 292549965Syokotaenable_versapad(struct psm_softc *sc) 292649965Syokota{ 292749965Syokota KBDC kbdc = sc->kbdc; 292849965Syokota int data[3]; 292949965Syokota 293049965Syokota set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */ 293149965Syokota set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */ 293249965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 293349965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 293449965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 293549965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 293649965Syokota if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */ 293749965Syokota return FALSE; 293849965Syokota if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */ 293949965Syokota return FALSE; 294049965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 294149965Syokota 294258230Syokota sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 294358230Syokota 294449965Syokota return TRUE; /* PS/2 absolute mode */ 294549965Syokota} 294649965Syokota 294741016Sdfrstatic int 294854629Syokotapsmresume(device_t dev) 294941016Sdfr{ 295054629Syokota struct psm_softc *sc = device_get_softc(dev); 295154629Syokota int unit = device_get_unit(dev); 295284880Syokota int err; 295341016Sdfr 295441016Sdfr if (verbose >= 2) 295554629Syokota log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit); 295641016Sdfr 295758230Syokota if (!(sc->config & PSM_CONFIG_HOOKRESUME)) 295858230Syokota return (0); 295958230Syokota 296084880Syokota err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND); 296141016Sdfr 296241016Sdfr if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) { 296341016Sdfr /* 296441016Sdfr * Release the blocked process; it must be notified that the device 296541016Sdfr * cannot be accessed anymore. 296641016Sdfr */ 296741016Sdfr sc->state &= ~PSM_ASLP; 2968111748Sdes wakeup(sc); 296941016Sdfr } 297041016Sdfr 297141016Sdfr if (verbose >= 2) 297254629Syokota log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit); 297341016Sdfr 297441016Sdfr return (err); 297541016Sdfr} 297641016Sdfr 297752997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0); 297883147Syokota 297983147Syokota/* 298083147Syokota * This sucks up assignments from PNPBIOS and ACPI. 298183147Syokota */ 298283147Syokota 298383931Syokota/* 298483931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may 298583931Syokota * appear BEFORE the AT keyboard controller. As the PS/2 mouse device 298683931Syokota * can be probed and attached only after the AT keyboard controller is 298783931Syokota * attached, we shall quietly reserve the IRQ resource for later use. 298883931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller, 298983931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging 299083931Syokota * under the keyboard controller, then probe and attach it. 299183931Syokota */ 299283147Syokota 299383147Syokotastatic devclass_t psmcpnp_devclass; 299483147Syokota 299583147Syokotastatic device_probe_t psmcpnp_probe; 299683147Syokotastatic device_attach_t psmcpnp_attach; 299783147Syokota 299883147Syokotastatic device_method_t psmcpnp_methods[] = { 299983147Syokota DEVMETHOD(device_probe, psmcpnp_probe), 300083147Syokota DEVMETHOD(device_attach, psmcpnp_attach), 300183147Syokota 300283147Syokota { 0, 0 } 300383147Syokota}; 300483147Syokota 300583147Syokotastatic driver_t psmcpnp_driver = { 300683147Syokota PSMCPNP_DRIVER_NAME, 300783147Syokota psmcpnp_methods, 300883147Syokota 1, /* no softc */ 300983147Syokota}; 301083147Syokota 301183147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = { 301288188Ssheldonh { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */ 301383147Syokota { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */ 301483147Syokota { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */ 3015117117Smikeh { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */ 301683492Syokota { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */ 301784407Stakawata { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */ 3018109679Shsu { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */ 3019109679Shsu { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */ 3020109710Smarcel { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */ 3021109679Shsu { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */ 302283147Syokota { 0 } 302383147Syokota}; 302483147Syokota 302583147Syokotastatic int 302683147Syokotacreate_a_copy(device_t atkbdc, device_t me) 302783147Syokota{ 302883147Syokota device_t psm; 302983147Syokota u_long irq; 303083147Syokota 303183931Syokota /* find the PS/2 mouse device instance under the keyboard controller */ 303283931Syokota psm = device_find_child(atkbdc, PSM_DRIVER_NAME, 303383931Syokota device_get_unit(atkbdc)); 303483147Syokota if (psm == NULL) 303583147Syokota return ENXIO; 303683931Syokota if (device_get_state(psm) != DS_NOTPRESENT) 303783931Syokota return 0; 303883147Syokota 303983931Syokota /* move our resource to the found device */ 304083931Syokota irq = bus_get_resource_start(me, SYS_RES_IRQ, 0); 304183147Syokota bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 304283147Syokota 304383147Syokota /* ...then probe and attach it */ 304483147Syokota return device_probe_and_attach(psm); 304583147Syokota} 304683147Syokota 304783147Syokotastatic int 304883147Syokotapsmcpnp_probe(device_t dev) 304983147Syokota{ 305083931Syokota struct resource *res; 305183931Syokota u_long irq; 305283931Syokota int rid; 305383147Syokota 305483147Syokota if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids)) 305583147Syokota return ENXIO; 305683147Syokota 305783931Syokota /* 305883931Syokota * The PnP BIOS and ACPI are supposed to assign an IRQ (12) 305983931Syokota * to the PS/2 mouse device node. But, some buggy PnP BIOS 306083931Syokota * declares the PS/2 mouse device node without an IRQ resource! 306183931Syokota * If this happens, we shall refer to device hints. 306283931Syokota * If we still don't find it there, use a hardcoded value... XXX 306383931Syokota */ 306483931Syokota rid = 0; 306583931Syokota irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid); 306683931Syokota if (irq <= 0) { 306783931Syokota if (resource_long_value(PSM_DRIVER_NAME, 306883931Syokota device_get_unit(dev), "irq", &irq) != 0) 306983931Syokota irq = 12; /* XXX */ 307083931Syokota device_printf(dev, "irq resource info is missing; " 307183931Syokota "assuming irq %ld\n", irq); 307283931Syokota bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1); 307383931Syokota } 3074127135Snjl res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 3075127135Snjl RF_SHAREABLE); 307683931Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, res); 307783147Syokota 307883147Syokota /* keep quiet */ 307983147Syokota if (!bootverbose) 308083147Syokota device_quiet(dev); 308183492Syokota 308283931Syokota return ((res == NULL) ? ENXIO : 0); 308383147Syokota} 308483147Syokota 308583147Syokotastatic int 308683147Syokotapsmcpnp_attach(device_t dev) 308783147Syokota{ 308883492Syokota device_t atkbdc; 308983931Syokota int rid; 309083147Syokota 309183931Syokota /* find the keyboard controller, which may be on acpi* or isa* bus */ 309283931Syokota atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME), 309383931Syokota device_get_unit(dev)); 309483931Syokota if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) { 309583492Syokota create_a_copy(atkbdc, dev); 309683931Syokota } else { 309783931Syokota /* 309883931Syokota * If we don't have the AT keyboard controller yet, 309983931Syokota * just reserve the IRQ for later use... 310083931Syokota * (See psmidentify() above.) 310183931Syokota */ 310283931Syokota rid = 0; 3103127135Snjl bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE); 310483931Syokota } 310583492Syokota 310683147Syokota return 0; 310783147Syokota} 310883147Syokota 3109123442Salfred/* 3110123442Salfred * Return true if 'now' is earlier than (start + (secs.usecs)). 3111123442Salfred * Now may be NULL and the function will fetch the current time from 3112123442Salfred * getmicrouptime(), or a cached 'now' can be passed in. 3113123442Salfred * All values should be numbers derived from getmicrouptime(). 3114123442Salfred */ 3115123442Salfredstatic int 3116123442Salfredtimeelapsed(start, secs, usecs, now) 3117123442Salfred const struct timeval *start, *now; 3118123442Salfred int secs, usecs; 3119123442Salfred{ 3120123442Salfred struct timeval snow, tv; 3121123442Salfred 3122123442Salfred /* if there is no 'now' passed in, the get it as a convience. */ 3123123442Salfred if (now == NULL) { 3124123442Salfred getmicrouptime(&snow); 3125123442Salfred now = &snow; 3126123442Salfred } 3127123442Salfred 3128123442Salfred tv.tv_sec = secs; 3129123442Salfred tv.tv_usec = usecs; 3130123442Salfred timevaladd(&tv, start); 3131123442Salfred return (timevalcmp(&tv, now, <)); 3132123442Salfred} 3133123442Salfred 313483147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0); 313583147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0); 3136