psm.c revision 127841
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 127841 2004-04-04 16:36:21Z markm $"); 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 */ 17958230Syokota dev_t dev; 18058230Syokota dev_t 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))) { 65141016Sdfr case 1: /* ignore this error */ 65241016Sdfr case PSM_ACK: 65341016Sdfr if (verbose) 65441016Sdfr log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n", 65584880Syokota sc->unit, i); 656102412Scharnier /* FALLTHROUGH */ 65741016Sdfr case 0: /* no error */ 65841016Sdfr break; 65941016Sdfr case -1: /* time out */ 66041016Sdfr default: /* error */ 66141016Sdfr recover_from_error(kbdc); 66245789Speter if (sc->config & PSM_CONFIG_IGNPORTERROR) 66345789Speter break; 66441016Sdfr log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n", 66584880Syokota sc->unit, i); 66641016Sdfr return FALSE; 66741016Sdfr } 66841016Sdfr 66945789Speter if (sc->config & PSM_CONFIG_NORESET) { 67045789Speter /* 67145789Speter * Don't try to reset the pointing device. It may possibly be 67245789Speter * left in the unknown state, though... 67345789Speter */ 67445789Speter } else { 67545789Speter /* 67645789Speter * NOTE: some controllers appears to hang the `keyboard' when 67745789Speter * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 67845789Speter */ 67945789Speter if (!reset_aux_dev(kbdc)) { 68045789Speter recover_from_error(kbdc); 68184880Syokota log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit); 68245789Speter return FALSE; 68345789Speter } 68441016Sdfr } 68541016Sdfr 68641016Sdfr /* 68741016Sdfr * both the aux port and the aux device is functioning, see 68841016Sdfr * if the device can be enabled. 68941016Sdfr */ 69041016Sdfr if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) { 69184880Syokota log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit); 69241016Sdfr return FALSE; 69341016Sdfr } 69441016Sdfr empty_both_buffers(kbdc, 10); /* remove stray data if any */ 69541016Sdfr 69645789Speter if (sc->config & PSM_CONFIG_NOIDPROBE) { 69745789Speter i = GENERIC_MOUSE_ENTRY; 69845789Speter } else { 69945789Speter /* FIXME: hardware ID, mouse buttons? */ 70041016Sdfr 70145789Speter /* other parameters */ 70245789Speter for (i = 0; vendortype[i].probefunc != NULL; ++i) { 70345789Speter if ((*vendortype[i].probefunc)(sc)) { 70445789Speter if (verbose >= 2) 70545789Speter log(LOG_ERR, "psm%d: found %s\n", 70684880Syokota sc->unit, model_name(vendortype[i].model)); 70745789Speter break; 70845789Speter } 70941016Sdfr } 71041016Sdfr } 71141016Sdfr 71241016Sdfr sc->hw.model = vendortype[i].model; 71341016Sdfr sc->mode.packetsize = vendortype[i].packetsize; 71441016Sdfr 71541016Sdfr /* set mouse parameters */ 71641016Sdfr if (mode != (mousemode_t *)NULL) { 71741016Sdfr if (mode->rate > 0) 71841016Sdfr mode->rate = set_mouse_sampling_rate(kbdc, mode->rate); 71941016Sdfr if (mode->resolution >= 0) 72041016Sdfr mode->resolution = set_mouse_resolution(kbdc, mode->resolution); 72141016Sdfr set_mouse_scaling(kbdc, 1); 72241016Sdfr set_mouse_mode(kbdc); 72341016Sdfr } 72441016Sdfr 72541016Sdfr /* request a data packet and extract sync. bits */ 72641016Sdfr if (get_mouse_status(kbdc, stat, 1, 3) < 3) { 72784880Syokota log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n", 72884880Syokota sc->unit); 72941016Sdfr sc->mode.syncmask[0] = 0; 73041016Sdfr } else { 73141016Sdfr sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 73241016Sdfr /* the NetScroll Mouse will send three more bytes... Ignore them */ 73341016Sdfr empty_aux_buffer(kbdc, 5); 73441016Sdfr } 73541016Sdfr 73641016Sdfr /* just check the status of the mouse */ 73741016Sdfr if (get_mouse_status(kbdc, stat, 0, 3) < 3) 73884880Syokota log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n", 73984880Syokota sc->unit); 74041016Sdfr 74141016Sdfr return TRUE; 74241016Sdfr} 74341016Sdfr 74441016Sdfrstatic int 74584880Syokotadoopen(struct psm_softc *sc, int command_byte) 74641016Sdfr{ 74741016Sdfr int stat[3]; 74841016Sdfr 74941016Sdfr /* enable the mouse device */ 75041016Sdfr if (!enable_aux_dev(sc->kbdc)) { 75141016Sdfr /* MOUSE ERROR: failed to enable the mouse because: 75241016Sdfr * 1) the mouse is faulty, 75341016Sdfr * 2) the mouse has been removed(!?) 75441016Sdfr * In the latter case, the keyboard may have hung, and need 75541016Sdfr * recovery procedure... 75641016Sdfr */ 75741016Sdfr recover_from_error(sc->kbdc); 75841016Sdfr#if 0 75941016Sdfr /* FIXME: we could reset the mouse here and try to enable 76041016Sdfr * it again. But it will take long time and it's not a good 76141016Sdfr * idea to disable the keyboard that long... 76241016Sdfr */ 76384880Syokota if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) { 76441016Sdfr recover_from_error(sc->kbdc); 76541016Sdfr#else 76641016Sdfr { 76741016Sdfr#endif 76841016Sdfr restore_controller(sc->kbdc, command_byte); 76941016Sdfr /* mark this device is no longer available */ 77041016Sdfr sc->state &= ~PSM_VALID; 77141016Sdfr log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n", 77284880Syokota sc->unit); 77341016Sdfr return (EIO); 77441016Sdfr } 77541016Sdfr } 77641016Sdfr 77741016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 77884880Syokota log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit); 77941016Sdfr 78041016Sdfr /* enable the aux port and interrupt */ 78141016Sdfr if (!set_controller_command_byte(sc->kbdc, 78241016Sdfr kbdc_get_device_mask(sc->kbdc), 78341016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 78441016Sdfr | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) { 78541016Sdfr /* CONTROLLER ERROR */ 78641016Sdfr disable_aux_dev(sc->kbdc); 78741016Sdfr restore_controller(sc->kbdc, command_byte); 78841016Sdfr log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n", 78984880Syokota sc->unit); 79041016Sdfr return (EIO); 79141016Sdfr } 79241016Sdfr 79358230Syokota /* start the watchdog timer */ 79458230Syokota sc->watchdog = FALSE; 79584880Syokota sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2); 79658230Syokota 79741016Sdfr return (0); 79841016Sdfr} 79941016Sdfr 80084880Syokotastatic int 80184880Syokotareinitialize(struct psm_softc *sc, int doinit) 80284880Syokota{ 80384880Syokota int err; 80484880Syokota int c; 80584880Syokota int s; 80684880Syokota 80784880Syokota /* don't let anybody mess with the aux device */ 80884880Syokota if (!kbdc_lock(sc->kbdc, TRUE)) 80984880Syokota return (EIO); 81084880Syokota s = spltty(); 81184880Syokota 81284880Syokota /* block our watchdog timer */ 81384880Syokota sc->watchdog = FALSE; 81484880Syokota untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout); 81584880Syokota callout_handle_init(&sc->callout); 81684880Syokota 81784880Syokota /* save the current controller command byte */ 81884880Syokota empty_both_buffers(sc->kbdc, 10); 81984880Syokota c = get_controller_command_byte(sc->kbdc); 82084880Syokota if (verbose >= 2) 82184880Syokota log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n", 82284880Syokota sc->unit, c); 82384880Syokota 82484880Syokota /* enable the aux port but disable the aux interrupt and the keyboard */ 82584880Syokota if ((c == -1) || !set_controller_command_byte(sc->kbdc, 82684880Syokota kbdc_get_device_mask(sc->kbdc), 82784880Syokota KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 82884880Syokota | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 82984880Syokota /* CONTROLLER ERROR */ 83084880Syokota splx(s); 83184880Syokota kbdc_lock(sc->kbdc, FALSE); 83284880Syokota log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n", 83384880Syokota sc->unit); 83484880Syokota return (EIO); 83584880Syokota } 83684880Syokota 83784880Syokota /* flush any data */ 83884880Syokota if (sc->state & PSM_VALID) { 83984880Syokota disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */ 84084880Syokota empty_aux_buffer(sc->kbdc, 10); 84184880Syokota } 842123442Salfred flushpackets(sc); 84384880Syokota sc->syncerrors = 0; 84484880Syokota 84584880Syokota /* try to detect the aux device; are you still there? */ 84684880Syokota err = 0; 84784880Syokota if (doinit) { 84884880Syokota if (doinitialize(sc, &sc->mode)) { 84984880Syokota /* yes */ 85084880Syokota sc->state |= PSM_VALID; 85184880Syokota } else { 85284880Syokota /* the device has gone! */ 85384880Syokota restore_controller(sc->kbdc, c); 85484880Syokota sc->state &= ~PSM_VALID; 85584880Syokota log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n", 85684880Syokota sc->unit); 85784880Syokota err = ENXIO; 85884880Syokota } 85984880Syokota } 86084880Syokota splx(s); 86184880Syokota 86284880Syokota /* restore the driver state */ 86384880Syokota if ((sc->state & PSM_OPEN) && (err == 0)) { 86484880Syokota /* enable the aux device and the port again */ 86584880Syokota err = doopen(sc, c); 86684880Syokota if (err != 0) 86784880Syokota log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n", 86884880Syokota sc->unit); 86984880Syokota } else { 87084880Syokota /* restore the keyboard port and disable the aux port */ 87184880Syokota if (!set_controller_command_byte(sc->kbdc, 87284880Syokota kbdc_get_device_mask(sc->kbdc), 87384880Syokota (c & KBD_KBD_CONTROL_BITS) 87484880Syokota | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 87584880Syokota /* CONTROLLER ERROR */ 87684880Syokota log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n", 87784880Syokota sc->unit); 87884880Syokota err = EIO; 87984880Syokota } 88084880Syokota } 88184880Syokota 88284880Syokota kbdc_lock(sc->kbdc, FALSE); 88384880Syokota return (err); 88484880Syokota} 88584880Syokota 88641016Sdfr/* psm driver entry points */ 88741016Sdfr 88883147Syokotastatic void 88983147Syokotapsmidentify(driver_t *driver, device_t parent) 89083147Syokota{ 89183931Syokota device_t psmc; 89283931Syokota device_t psm; 89383931Syokota u_long irq; 89483931Syokota int unit; 89583147Syokota 89683931Syokota unit = device_get_unit(parent); 89783931Syokota 89883147Syokota /* always add at least one child */ 89983931Syokota psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit); 90083931Syokota if (psm == NULL) 90183931Syokota return; 90283931Syokota 90383931Syokota irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX); 90483931Syokota if (irq > 0) 90583931Syokota return; 90683931Syokota 90783931Syokota /* 90883931Syokota * If the PS/2 mouse device has already been reported by ACPI or 90983931Syokota * PnP BIOS, obtain the IRQ resource from it. 91083931Syokota * (See psmcpnp_attach() below.) 91183931Syokota */ 91283931Syokota psmc = device_find_child(device_get_parent(parent), 91383931Syokota PSMCPNP_DRIVER_NAME, unit); 91483931Syokota if (psmc == NULL) 91583931Syokota return; 91683931Syokota irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0); 91783931Syokota if (irq <= 0) 91883931Syokota return; 91983931Syokota bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 92083147Syokota} 92183147Syokota 922115569Sphk#define endprobe(v) do { if (bootverbose) \ 92341016Sdfr --verbose; \ 92441016Sdfr kbdc_set_device_mask(sc->kbdc, mask); \ 92541016Sdfr kbdc_lock(sc->kbdc, FALSE); \ 92641016Sdfr return (v); \ 927115569Sphk } while (0) 92841016Sdfr 92941016Sdfrstatic int 93041016Sdfrpsmprobe(device_t dev) 93141016Sdfr{ 93241016Sdfr int unit = device_get_unit(dev); 93341016Sdfr struct psm_softc *sc = device_get_softc(dev); 93441016Sdfr int stat[3]; 93541016Sdfr int command_byte; 93641016Sdfr int mask; 93758230Syokota int rid; 93841016Sdfr int i; 93941016Sdfr 94041016Sdfr#if 0 94141016Sdfr kbdc_debug(TRUE); 94241016Sdfr#endif 94358230Syokota 94483147Syokota /* see if IRQ is available */ 94583147Syokota rid = KBDC_RID_AUX; 946127135Snjl sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 947127135Snjl RF_SHAREABLE | RF_ACTIVE); 94883147Syokota if (sc->intr == NULL) { 94983147Syokota if (bootverbose) 95083147Syokota device_printf(dev, "unable to allocate IRQ\n"); 95183147Syokota return (ENXIO); 95283147Syokota } 95383147Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 95458230Syokota 95584880Syokota sc->unit = unit; 95658230Syokota sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev))); 95783147Syokota sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS; 95858230Syokota /* XXX: for backward compatibility */ 95958230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM) 96058230Syokota sc->config |= 96158230Syokota#ifdef PSM_RESETAFTERSUSPEND 96258230Syokota PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 96358230Syokota#else 96458230Syokota PSM_CONFIG_HOOKRESUME; 96558230Syokota#endif 96658230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */ 96741016Sdfr sc->flags = 0; 96841016Sdfr if (bootverbose) 96941016Sdfr ++verbose; 97041016Sdfr 97143105Sdfr device_set_desc(dev, "PS/2 Mouse"); 97243105Sdfr 97341016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) { 97441016Sdfr printf("psm%d: unable to lock the controller.\n", unit); 97541016Sdfr if (bootverbose) 97641016Sdfr --verbose; 97741016Sdfr return (ENXIO); 97841016Sdfr } 97941016Sdfr 98041016Sdfr /* 98141016Sdfr * NOTE: two bits in the command byte controls the operation of the 98241016Sdfr * aux port (mouse port): the aux port disable bit (bit 5) and the aux 98341016Sdfr * port interrupt (IRQ 12) enable bit (bit 2). 98441016Sdfr */ 98541016Sdfr 98641016Sdfr /* discard anything left after the keyboard initialization */ 98741016Sdfr empty_both_buffers(sc->kbdc, 10); 98841016Sdfr 98941016Sdfr /* save the current command byte; it will be used later */ 99041016Sdfr mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS; 99141016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 99241016Sdfr if (verbose) 99341016Sdfr printf("psm%d: current command byte:%04x\n", unit, command_byte); 99441016Sdfr if (command_byte == -1) { 99541016Sdfr /* CONTROLLER ERROR */ 99641016Sdfr printf("psm%d: unable to get the current command byte value.\n", 99741016Sdfr unit); 99841016Sdfr endprobe(ENXIO); 99941016Sdfr } 100041016Sdfr 100141016Sdfr /* 100241016Sdfr * disable the keyboard port while probing the aux port, which must be 100341016Sdfr * enabled during this routine 100441016Sdfr */ 100541016Sdfr if (!set_controller_command_byte(sc->kbdc, 100641016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 100741016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 100841016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 100941016Sdfr /* 101041016Sdfr * this is CONTROLLER ERROR; I don't know how to recover 101141016Sdfr * from this error... 101241016Sdfr */ 101341016Sdfr restore_controller(sc->kbdc, command_byte); 101441016Sdfr printf("psm%d: unable to set the command byte.\n", unit); 101541016Sdfr endprobe(ENXIO); 101641016Sdfr } 101745789Speter write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT); 101841016Sdfr 101941016Sdfr /* 102041016Sdfr * NOTE: `test_aux_port()' is designed to return with zero if the aux 102141016Sdfr * port exists and is functioning. However, some controllers appears 102241016Sdfr * to respond with zero even when the aux port doesn't exist. (It may 102341016Sdfr * be that this is only the case when the controller DOES have the aux 102441016Sdfr * port but the port is not wired on the motherboard.) The keyboard 102541016Sdfr * controllers without the port, such as the original AT, are 102641016Sdfr * supporsed to return with an error code or simply time out. In any 102741016Sdfr * case, we have to continue probing the port even when the controller 102841016Sdfr * passes this test. 102941016Sdfr * 103041016Sdfr * XXX: some controllers erroneously return the error code 1 when 103141016Sdfr * it has the perfectly functional aux port. We have to ignore this 103241016Sdfr * error code. Even if the controller HAS error with the aux port, 103341016Sdfr * it will be detected later... 103441016Sdfr * XXX: another incompatible controller returns PSM_ACK (0xfa)... 103541016Sdfr */ 103641016Sdfr switch ((i = test_aux_port(sc->kbdc))) { 103741016Sdfr case 1: /* ignore this error */ 103841016Sdfr case PSM_ACK: 103941016Sdfr if (verbose) 104041016Sdfr printf("psm%d: strange result for test aux port (%d).\n", 104141016Sdfr unit, i); 1042102412Scharnier /* FALLTHROUGH */ 104341016Sdfr case 0: /* no error */ 104441016Sdfr break; 104541016Sdfr case -1: /* time out */ 104641016Sdfr default: /* error */ 104741016Sdfr recover_from_error(sc->kbdc); 104845789Speter if (sc->config & PSM_CONFIG_IGNPORTERROR) 104945789Speter break; 105041016Sdfr restore_controller(sc->kbdc, command_byte); 105141016Sdfr if (verbose) 105241016Sdfr printf("psm%d: the aux port is not functioning (%d).\n", 105341016Sdfr unit, i); 105441016Sdfr endprobe(ENXIO); 105541016Sdfr } 105641016Sdfr 105745789Speter if (sc->config & PSM_CONFIG_NORESET) { 105845789Speter /* 105945789Speter * Don't try to reset the pointing device. It may possibly be 106045789Speter * left in the unknown state, though... 106145789Speter */ 106245789Speter } else { 106345789Speter /* 106445789Speter * NOTE: some controllers appears to hang the `keyboard' when the aux 106545789Speter * port doesn't exist and `PSMC_RESET_DEV' is issued. 1066117302Smikeh * 1067117302Smikeh * Attempt to reset the controller twice -- this helps 1068117302Smikeh * pierce through some KVM switches. The second reset 1069117302Smikeh * is non-fatal. 107045789Speter */ 107145789Speter if (!reset_aux_dev(sc->kbdc)) { 107245789Speter recover_from_error(sc->kbdc); 107345789Speter restore_controller(sc->kbdc, command_byte); 107445789Speter if (verbose) 107545789Speter printf("psm%d: failed to reset the aux device.\n", unit); 107645789Speter endprobe(ENXIO); 1077117302Smikeh } else if (!reset_aux_dev(sc->kbdc)) { 1078117302Smikeh recover_from_error(sc->kbdc); 1079117302Smikeh if (verbose >= 2) 1080117302Smikeh printf("psm%d: failed to reset the aux device (2).\n", 1081117302Smikeh unit); 108245789Speter } 108341016Sdfr } 108445789Speter 108541016Sdfr /* 108641016Sdfr * both the aux port and the aux device is functioning, see if the 108741016Sdfr * device can be enabled. NOTE: when enabled, the device will start 108841016Sdfr * sending data; we shall immediately disable the device once we know 108941016Sdfr * the device can be enabled. 109041016Sdfr */ 109141016Sdfr if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) { 109245789Speter /* MOUSE ERROR */ 109345789Speter recover_from_error(sc->kbdc); 109445789Speter restore_controller(sc->kbdc, command_byte); 109545789Speter if (verbose) 109645789Speter printf("psm%d: failed to enable the aux device.\n", unit); 109741016Sdfr endprobe(ENXIO); 109841016Sdfr } 109941016Sdfr 110041016Sdfr /* save the default values after reset */ 110141016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) { 110241016Sdfr sc->dflt_mode.rate = sc->mode.rate = stat[2]; 110341016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 110441016Sdfr } else { 110541016Sdfr sc->dflt_mode.rate = sc->mode.rate = -1; 110641016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = -1; 110741016Sdfr } 110841016Sdfr 110941016Sdfr /* hardware information */ 111041016Sdfr sc->hw.iftype = MOUSE_IF_PS2; 111141016Sdfr 111241016Sdfr /* verify the device is a mouse */ 111341016Sdfr sc->hw.hwid = get_aux_id(sc->kbdc); 111441016Sdfr if (!is_a_mouse(sc->hw.hwid)) { 111541016Sdfr restore_controller(sc->kbdc, command_byte); 111641016Sdfr if (verbose) 111741016Sdfr printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid); 111841016Sdfr endprobe(ENXIO); 111941016Sdfr } 112041016Sdfr switch (sc->hw.hwid) { 112141016Sdfr case PSM_BALLPOINT_ID: 112241016Sdfr sc->hw.type = MOUSE_TRACKBALL; 112341016Sdfr break; 112441016Sdfr case PSM_MOUSE_ID: 112541016Sdfr case PSM_INTELLI_ID: 112658230Syokota case PSM_EXPLORER_ID: 112758230Syokota case PSM_4DMOUSE_ID: 112858230Syokota case PSM_4DPLUS_ID: 112941016Sdfr sc->hw.type = MOUSE_MOUSE; 113041016Sdfr break; 113141016Sdfr default: 113241016Sdfr sc->hw.type = MOUSE_UNKNOWN; 113341016Sdfr break; 113441016Sdfr } 113541016Sdfr 113645789Speter if (sc->config & PSM_CONFIG_NOIDPROBE) { 113745789Speter sc->hw.buttons = 2; 113845789Speter i = GENERIC_MOUSE_ENTRY; 113945789Speter } else { 114045789Speter /* # of buttons */ 114145789Speter sc->hw.buttons = get_mouse_buttons(sc->kbdc); 114241016Sdfr 114345789Speter /* other parameters */ 114445789Speter for (i = 0; vendortype[i].probefunc != NULL; ++i) { 114545789Speter if ((*vendortype[i].probefunc)(sc)) { 114645789Speter if (verbose >= 2) 114745789Speter printf("psm%d: found %s\n", 114845789Speter unit, model_name(vendortype[i].model)); 114945789Speter break; 115045789Speter } 115141016Sdfr } 115241016Sdfr } 115341016Sdfr 115441016Sdfr sc->hw.model = vendortype[i].model; 115541016Sdfr 115641016Sdfr sc->dflt_mode.level = PSM_LEVEL_BASE; 115741016Sdfr sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE; 115841016Sdfr sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4; 115941016Sdfr if (sc->config & PSM_CONFIG_NOCHECKSYNC) 116041016Sdfr sc->dflt_mode.syncmask[0] = 0; 116141016Sdfr else 116241016Sdfr sc->dflt_mode.syncmask[0] = vendortype[i].syncmask; 116345789Speter if (sc->config & PSM_CONFIG_FORCETAP) 116445789Speter sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP; 116541016Sdfr sc->dflt_mode.syncmask[1] = 0; /* syncbits */ 116641016Sdfr sc->mode = sc->dflt_mode; 116741016Sdfr sc->mode.packetsize = vendortype[i].packetsize; 116841016Sdfr 116941016Sdfr /* set mouse parameters */ 117048773Syokota#if 0 117148773Syokota /* 117248773Syokota * A version of Logitech FirstMouse+ won't report wheel movement, 117348773Syokota * if SET_DEFAULTS is sent... Don't use this command. 117448773Syokota * This fix was found by Takashi Nishida. 117548773Syokota */ 117641016Sdfr i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS); 117741016Sdfr if (verbose >= 2) 117841016Sdfr printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i); 117948773Syokota#endif 118041016Sdfr if (sc->config & PSM_CONFIG_RESOLUTION) { 118141016Sdfr sc->mode.resolution 118241016Sdfr = set_mouse_resolution(sc->kbdc, 118348773Syokota (sc->config & PSM_CONFIG_RESOLUTION) - 1); 118448773Syokota } else if (sc->mode.resolution >= 0) { 118548773Syokota sc->mode.resolution 118648773Syokota = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution); 118741016Sdfr } 118848773Syokota if (sc->mode.rate > 0) { 118948773Syokota sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate); 119048773Syokota } 119148773Syokota set_mouse_scaling(sc->kbdc, 1); 119241016Sdfr 119341016Sdfr /* request a data packet and extract sync. bits */ 119441016Sdfr if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) { 119541016Sdfr printf("psm%d: failed to get data.\n", unit); 119641016Sdfr sc->mode.syncmask[0] = 0; 119741016Sdfr } else { 119841016Sdfr sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 119941016Sdfr /* the NetScroll Mouse will send three more bytes... Ignore them */ 120041016Sdfr empty_aux_buffer(sc->kbdc, 5); 120141016Sdfr } 120241016Sdfr 120341016Sdfr /* just check the status of the mouse */ 120441016Sdfr /* 120541016Sdfr * NOTE: XXX there are some arcane controller/mouse combinations out 120641016Sdfr * there, which hung the controller unless there is data transmission 120741016Sdfr * after ACK from the mouse. 120841016Sdfr */ 120941016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) { 121041016Sdfr printf("psm%d: failed to get status.\n", unit); 121141016Sdfr } else { 121241016Sdfr /* 121341016Sdfr * When in its native mode, some mice operate with different 121441016Sdfr * default parameters than in the PS/2 compatible mode. 121541016Sdfr */ 121641016Sdfr sc->dflt_mode.rate = sc->mode.rate = stat[2]; 121741016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 121841016Sdfr } 121941016Sdfr 122041016Sdfr /* disable the aux port for now... */ 122141016Sdfr if (!set_controller_command_byte(sc->kbdc, 122241016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 122341016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 122441016Sdfr | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 122541016Sdfr /* 122641016Sdfr * this is CONTROLLER ERROR; I don't know the proper way to 122741016Sdfr * recover from this error... 122841016Sdfr */ 122941016Sdfr restore_controller(sc->kbdc, command_byte); 123041016Sdfr printf("psm%d: unable to set the command byte.\n", unit); 123141016Sdfr endprobe(ENXIO); 123241016Sdfr } 123341016Sdfr 123441016Sdfr /* done */ 123541016Sdfr kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS); 123641016Sdfr kbdc_lock(sc->kbdc, FALSE); 123741016Sdfr return (0); 123841016Sdfr} 123941016Sdfr 124041016Sdfrstatic int 124141016Sdfrpsmattach(device_t dev) 124241016Sdfr{ 124341016Sdfr int unit = device_get_unit(dev); 124441016Sdfr struct psm_softc *sc = device_get_softc(dev); 124558230Syokota int error; 124658230Syokota int rid; 124741016Sdfr 124841016Sdfr if (sc == NULL) /* shouldn't happen */ 124941016Sdfr return (ENXIO); 125041016Sdfr 125141016Sdfr /* Setup initial state */ 125241016Sdfr sc->state = PSM_VALID; 125358230Syokota callout_handle_init(&sc->callout); 125441016Sdfr 125558230Syokota /* Setup our interrupt handler */ 125683147Syokota rid = KBDC_RID_AUX; 1257127135Snjl sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1258127135Snjl RF_SHAREABLE | RF_ACTIVE); 125958230Syokota if (sc->intr == NULL) 126058230Syokota return (ENXIO); 126183147Syokota error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih); 126258230Syokota if (error) { 126358230Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 126458230Syokota return (error); 126558230Syokota } 126658230Syokota 126741016Sdfr /* Done */ 126858230Syokota sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, 126958230Syokota "psm%d", unit); 127058230Syokota sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, 127158230Syokota "bpsm%d", unit); 127241016Sdfr 127341016Sdfr if (!verbose) { 127441016Sdfr printf("psm%d: model %s, device ID %d\n", 127548778Syokota unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff); 127641016Sdfr } else { 127748778Syokota printf("psm%d: model %s, device ID %d-%02x, %d buttons\n", 127848778Syokota unit, model_name(sc->hw.model), 127948778Syokota sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons); 128041016Sdfr printf("psm%d: config:%08x, flags:%08x, packet size:%d\n", 128141016Sdfr unit, sc->config, sc->flags, sc->mode.packetsize); 128241016Sdfr printf("psm%d: syncmask:%02x, syncbits:%02x\n", 128341016Sdfr unit, sc->mode.syncmask[0], sc->mode.syncmask[1]); 128441016Sdfr } 128541016Sdfr 128641016Sdfr if (bootverbose) 128741016Sdfr --verbose; 128841016Sdfr 128941016Sdfr return (0); 129041016Sdfr} 129141016Sdfr 129241016Sdfrstatic int 129358230Syokotapsmdetach(device_t dev) 129458230Syokota{ 129558230Syokota struct psm_softc *sc; 129658230Syokota int rid; 129758230Syokota 129858230Syokota sc = device_get_softc(dev); 129958230Syokota if (sc->state & PSM_OPEN) 130058230Syokota return EBUSY; 130158230Syokota 130283147Syokota rid = KBDC_RID_AUX; 130383147Syokota bus_teardown_intr(dev, sc->intr, sc->ih); 130458230Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 130558230Syokota 130658230Syokota destroy_dev(sc->dev); 130758230Syokota destroy_dev(sc->bdev); 130858230Syokota 130958230Syokota return 0; 131058230Syokota} 131158230Syokota 131258230Syokotastatic int 131383366Sjulianpsmopen(dev_t dev, int flag, int fmt, struct thread *td) 131441016Sdfr{ 131541016Sdfr int unit = PSM_UNIT(dev); 131641016Sdfr struct psm_softc *sc; 131741016Sdfr int command_byte; 131841016Sdfr int err; 131941016Sdfr int s; 132041016Sdfr 132141016Sdfr /* Get device data */ 132241016Sdfr sc = PSM_SOFTC(unit); 132341016Sdfr if ((sc == NULL) || (sc->state & PSM_VALID) == 0) 132441016Sdfr /* the device is no longer valid/functioning */ 132541016Sdfr return (ENXIO); 132641016Sdfr 132741016Sdfr /* Disallow multiple opens */ 132841016Sdfr if (sc->state & PSM_OPEN) 132941016Sdfr return (EBUSY); 133041016Sdfr 133141016Sdfr device_busy(devclass_get_device(psm_devclass, unit)); 133241016Sdfr 133341016Sdfr /* Initialize state */ 133441016Sdfr sc->mode.level = sc->dflt_mode.level; 133541016Sdfr sc->mode.protocol = sc->dflt_mode.protocol; 133658230Syokota sc->watchdog = FALSE; 133741016Sdfr 133841016Sdfr /* flush the event queue */ 133941016Sdfr sc->queue.count = 0; 134041016Sdfr sc->queue.head = 0; 134141016Sdfr sc->queue.tail = 0; 134241016Sdfr sc->status.flags = 0; 134341016Sdfr sc->status.button = 0; 134441016Sdfr sc->status.obutton = 0; 134541016Sdfr sc->status.dx = 0; 134641016Sdfr sc->status.dy = 0; 134741016Sdfr sc->status.dz = 0; 134841016Sdfr sc->button = 0; 1349123442Salfred sc->pqueue_start = 0; 1350123442Salfred sc->pqueue_end = 0; 135141016Sdfr 135241016Sdfr /* empty input buffer */ 1353123442Salfred flushpackets(sc); 135484880Syokota sc->syncerrors = 0; 135541016Sdfr 135641016Sdfr /* don't let timeout routines in the keyboard driver to poll the kbdc */ 135741016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 135841016Sdfr return (EIO); 135941016Sdfr 136041016Sdfr /* save the current controller command byte */ 136141016Sdfr s = spltty(); 136241016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 136341016Sdfr 136441016Sdfr /* enable the aux port and temporalily disable the keyboard */ 136541016Sdfr if ((command_byte == -1) 136641016Sdfr || !set_controller_command_byte(sc->kbdc, 136741016Sdfr kbdc_get_device_mask(sc->kbdc), 136841016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 136941016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 137041016Sdfr /* CONTROLLER ERROR; do you know how to get out of this? */ 137141016Sdfr kbdc_lock(sc->kbdc, FALSE); 137241016Sdfr splx(s); 137341016Sdfr log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n", 137441016Sdfr unit); 137541016Sdfr return (EIO); 137641016Sdfr } 137741016Sdfr /* 137841016Sdfr * Now that the keyboard controller is told not to generate 137941016Sdfr * the keyboard and mouse interrupts, call `splx()' to allow 138041016Sdfr * the other tty interrupts. The clock interrupt may also occur, 138141016Sdfr * but timeout routines will be blocked by the poll flag set 138241016Sdfr * via `kbdc_lock()' 138341016Sdfr */ 138441016Sdfr splx(s); 138541016Sdfr 138641016Sdfr /* enable the mouse device */ 138784880Syokota err = doopen(sc, command_byte); 138841016Sdfr 138941016Sdfr /* done */ 139041016Sdfr if (err == 0) 139141016Sdfr sc->state |= PSM_OPEN; 139241016Sdfr kbdc_lock(sc->kbdc, FALSE); 139341016Sdfr return (err); 139441016Sdfr} 139541016Sdfr 139641016Sdfrstatic int 139783366Sjulianpsmclose(dev_t dev, int flag, int fmt, struct thread *td) 139841016Sdfr{ 139941016Sdfr int unit = PSM_UNIT(dev); 140041016Sdfr struct psm_softc *sc = PSM_SOFTC(unit); 140141016Sdfr int stat[3]; 140241016Sdfr int command_byte; 140341016Sdfr int s; 140441016Sdfr 140541016Sdfr /* don't let timeout routines in the keyboard driver to poll the kbdc */ 140641016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 140741016Sdfr return (EIO); 140841016Sdfr 140941016Sdfr /* save the current controller command byte */ 141041016Sdfr s = spltty(); 141141016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 141241016Sdfr if (command_byte == -1) { 141341016Sdfr kbdc_lock(sc->kbdc, FALSE); 141441016Sdfr splx(s); 141541016Sdfr return (EIO); 141641016Sdfr } 141741016Sdfr 141841016Sdfr /* disable the aux interrupt and temporalily disable the keyboard */ 141941016Sdfr if (!set_controller_command_byte(sc->kbdc, 142041016Sdfr kbdc_get_device_mask(sc->kbdc), 142141016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 142241016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 142341016Sdfr log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n", 142458230Syokota unit); 142541016Sdfr /* CONTROLLER ERROR; 142641016Sdfr * NOTE: we shall force our way through. Because the only 142741016Sdfr * ill effect we shall see is that we may not be able 142841016Sdfr * to read ACK from the mouse, and it doesn't matter much 142941016Sdfr * so long as the mouse will accept the DISABLE command. 143041016Sdfr */ 143141016Sdfr } 143241016Sdfr splx(s); 143341016Sdfr 143458230Syokota /* stop the watchdog timer */ 143584880Syokota untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout); 143658230Syokota callout_handle_init(&sc->callout); 143758230Syokota 143841016Sdfr /* remove anything left in the output buffer */ 143941016Sdfr empty_aux_buffer(sc->kbdc, 10); 144041016Sdfr 144141016Sdfr /* disable the aux device, port and interrupt */ 144241016Sdfr if (sc->state & PSM_VALID) { 144341016Sdfr if (!disable_aux_dev(sc->kbdc)) { 144441016Sdfr /* MOUSE ERROR; 144541016Sdfr * NOTE: we don't return error and continue, pretending 144641016Sdfr * we have successfully disabled the device. It's OK because 144741016Sdfr * the interrupt routine will discard any data from the mouse 144841016Sdfr * hereafter. 144941016Sdfr */ 145041016Sdfr log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n", 145158230Syokota unit); 145241016Sdfr } 145341016Sdfr 145441016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 145541016Sdfr log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n", 145658230Syokota unit); 145741016Sdfr } 145841016Sdfr 145941016Sdfr if (!set_controller_command_byte(sc->kbdc, 146041016Sdfr kbdc_get_device_mask(sc->kbdc), 146141016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 146241016Sdfr | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 146341016Sdfr /* CONTROLLER ERROR; 146441016Sdfr * we shall ignore this error; see the above comment. 146541016Sdfr */ 146641016Sdfr log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n", 146758230Syokota unit); 146841016Sdfr } 146941016Sdfr 147041016Sdfr /* remove anything left in the output buffer */ 147141016Sdfr empty_aux_buffer(sc->kbdc, 10); 147241016Sdfr 147341016Sdfr /* close is almost always successful */ 147441016Sdfr sc->state &= ~PSM_OPEN; 147541016Sdfr kbdc_lock(sc->kbdc, FALSE); 147641016Sdfr device_unbusy(devclass_get_device(psm_devclass, unit)); 147741016Sdfr return (0); 147841016Sdfr} 147941016Sdfr 148041016Sdfrstatic int 1481123442Salfredtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, unsigned char *buf) 148241016Sdfr{ 148341016Sdfr static unsigned char butmapps2[8] = { 148441016Sdfr 0, 148541016Sdfr MOUSE_PS2_BUTTON1DOWN, 148641016Sdfr MOUSE_PS2_BUTTON2DOWN, 148741016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN, 148841016Sdfr MOUSE_PS2_BUTTON3DOWN, 148941016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN, 149041016Sdfr MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 149141016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 149241016Sdfr }; 149341016Sdfr static unsigned char butmapmsc[8] = { 149441016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 149541016Sdfr MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 149641016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 149741016Sdfr MOUSE_MSC_BUTTON3UP, 149841016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 149941016Sdfr MOUSE_MSC_BUTTON2UP, 150041016Sdfr MOUSE_MSC_BUTTON1UP, 150141016Sdfr 0, 150241016Sdfr }; 150341016Sdfr int mapped; 150441016Sdfr int i; 150541016Sdfr 150641016Sdfr if (sc->mode.level == PSM_LEVEL_BASE) { 150741016Sdfr mapped = status->button & ~MOUSE_BUTTON4DOWN; 150841016Sdfr if (status->button & MOUSE_BUTTON4DOWN) 150941016Sdfr mapped |= MOUSE_BUTTON1DOWN; 151041016Sdfr status->button = mapped; 151141016Sdfr buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS]; 1512109269Smdodd i = imax(imin(status->dx, 255), -256); 151341016Sdfr if (i < 0) 151441016Sdfr buf[0] |= MOUSE_PS2_XNEG; 151541016Sdfr buf[1] = i; 1516109269Smdodd i = imax(imin(status->dy, 255), -256); 151741016Sdfr if (i < 0) 151841016Sdfr buf[0] |= MOUSE_PS2_YNEG; 151941016Sdfr buf[2] = i; 152041016Sdfr return MOUSE_PS2_PACKETSIZE; 152141016Sdfr } else if (sc->mode.level == PSM_LEVEL_STANDARD) { 152241016Sdfr buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS]; 1523109269Smdodd i = imax(imin(status->dx, 255), -256); 152441016Sdfr buf[1] = i >> 1; 152541016Sdfr buf[3] = i - buf[1]; 1526109269Smdodd i = imax(imin(status->dy, 255), -256); 152741016Sdfr buf[2] = i >> 1; 152841016Sdfr buf[4] = i - buf[2]; 1529109269Smdodd i = imax(imin(status->dz, 127), -128); 153041016Sdfr buf[5] = (i >> 1) & 0x7f; 153141016Sdfr buf[6] = (i - (i >> 1)) & 0x7f; 153241016Sdfr buf[7] = (~status->button >> 3) & 0x7f; 153341016Sdfr return MOUSE_SYS_PACKETSIZE; 153441016Sdfr } 1535123442Salfred return pb->inputbytes; 153641016Sdfr} 153741016Sdfr 153841016Sdfrstatic int 153941016Sdfrpsmread(dev_t dev, struct uio *uio, int flag) 154041016Sdfr{ 154141016Sdfr register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 154241016Sdfr unsigned char buf[PSM_SMALLBUFSIZE]; 154341016Sdfr int error = 0; 154441016Sdfr int s; 154541016Sdfr int l; 154641016Sdfr 154741016Sdfr if ((sc->state & PSM_VALID) == 0) 154841016Sdfr return EIO; 154941016Sdfr 155041016Sdfr /* block until mouse activity occured */ 155141016Sdfr s = spltty(); 155241016Sdfr while (sc->queue.count <= 0) { 155341016Sdfr if (PSM_NBLOCKIO(dev)) { 155441016Sdfr splx(s); 155541016Sdfr return EWOULDBLOCK; 155641016Sdfr } 155741016Sdfr sc->state |= PSM_ASLP; 1558111748Sdes error = tsleep( sc, PZERO | PCATCH, "psmrea", 0); 155941016Sdfr sc->state &= ~PSM_ASLP; 156041016Sdfr if (error) { 156141016Sdfr splx(s); 156241016Sdfr return error; 156341016Sdfr } else if ((sc->state & PSM_VALID) == 0) { 156441016Sdfr /* the device disappeared! */ 156541016Sdfr splx(s); 156641016Sdfr return EIO; 156741016Sdfr } 156841016Sdfr } 156941016Sdfr splx(s); 157041016Sdfr 157141016Sdfr /* copy data to the user land */ 157241016Sdfr while ((sc->queue.count > 0) && (uio->uio_resid > 0)) { 157341016Sdfr s = spltty(); 1574109269Smdodd l = imin(sc->queue.count, uio->uio_resid); 157541016Sdfr if (l > sizeof(buf)) 157641016Sdfr l = sizeof(buf); 157741016Sdfr if (l > sizeof(sc->queue.buf) - sc->queue.head) { 157841016Sdfr bcopy(&sc->queue.buf[sc->queue.head], &buf[0], 157941016Sdfr sizeof(sc->queue.buf) - sc->queue.head); 158041016Sdfr bcopy(&sc->queue.buf[0], 158141016Sdfr &buf[sizeof(sc->queue.buf) - sc->queue.head], 158241016Sdfr l - (sizeof(sc->queue.buf) - sc->queue.head)); 158341016Sdfr } else { 158441016Sdfr bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l); 158541016Sdfr } 158641016Sdfr sc->queue.count -= l; 158741016Sdfr sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf); 158841016Sdfr splx(s); 158941016Sdfr error = uiomove(buf, l, uio); 159041016Sdfr if (error) 159141016Sdfr break; 159241016Sdfr } 159341016Sdfr 159441016Sdfr return error; 159541016Sdfr} 159641016Sdfr 159741016Sdfrstatic int 159841016Sdfrblock_mouse_data(struct psm_softc *sc, int *c) 159941016Sdfr{ 160041016Sdfr int s; 160141016Sdfr 160241016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 160341016Sdfr return EIO; 160441016Sdfr 160541016Sdfr s = spltty(); 160641016Sdfr *c = get_controller_command_byte(sc->kbdc); 160741016Sdfr if ((*c == -1) 160841016Sdfr || !set_controller_command_byte(sc->kbdc, 160941016Sdfr kbdc_get_device_mask(sc->kbdc), 161041016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 161141016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 161241016Sdfr /* this is CONTROLLER ERROR */ 161341016Sdfr splx(s); 161441016Sdfr kbdc_lock(sc->kbdc, FALSE); 161541016Sdfr return EIO; 161641016Sdfr } 161741016Sdfr 161841016Sdfr /* 161941016Sdfr * The device may be in the middle of status data transmission. 162041016Sdfr * The transmission will be interrupted, thus, incomplete status 162141016Sdfr * data must be discarded. Although the aux interrupt is disabled 162241016Sdfr * at the keyboard controller level, at most one aux interrupt 162341016Sdfr * may have already been pending and a data byte is in the 162441016Sdfr * output buffer; throw it away. Note that the second argument 162541016Sdfr * to `empty_aux_buffer()' is zero, so that the call will just 162641016Sdfr * flush the internal queue. 162741016Sdfr * `psmintr()' will be invoked after `splx()' if an interrupt is 162841016Sdfr * pending; it will see no data and returns immediately. 162941016Sdfr */ 163041016Sdfr empty_aux_buffer(sc->kbdc, 0); /* flush the queue */ 163141016Sdfr read_aux_data_no_wait(sc->kbdc); /* throw away data if any */ 1632123442Salfred flushpackets(sc); 163341016Sdfr splx(s); 163441016Sdfr 163541016Sdfr return 0; 163641016Sdfr} 163741016Sdfr 1638123442Salfredstatic void 1639123442Salfreddropqueue(struct psm_softc *sc) 1640123442Salfred{ 1641123442Salfred 1642123442Salfred sc->queue.count = 0; 1643123442Salfred sc->queue.head = 0; 1644123442Salfred sc->queue.tail = 0; 1645123442Salfred if ((sc->state & PSM_SOFTARMED) != 0) { 1646123442Salfred sc->state &= ~PSM_SOFTARMED; 1647123442Salfred untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout); 1648123442Salfred } 1649123442Salfred sc->pqueue_start = sc->pqueue_end; 1650123442Salfred} 1651123442Salfred 1652123442Salfredstatic void 1653123442Salfredflushpackets(struct psm_softc *sc) 1654123442Salfred{ 1655123442Salfred 1656123442Salfred dropqueue(sc); 1657123442Salfred bzero(&sc->pqueue, sizeof(sc->pqueue)); 1658123442Salfred} 1659123442Salfred 166041016Sdfrstatic int 166141016Sdfrunblock_mouse_data(struct psm_softc *sc, int c) 166241016Sdfr{ 166341016Sdfr int error = 0; 166441016Sdfr 166541016Sdfr /* 166641016Sdfr * We may have seen a part of status data during `set_mouse_XXX()'. 166741016Sdfr * they have been queued; flush it. 166841016Sdfr */ 166941016Sdfr empty_aux_buffer(sc->kbdc, 0); 167041016Sdfr 167141016Sdfr /* restore ports and interrupt */ 167241016Sdfr if (!set_controller_command_byte(sc->kbdc, 167341016Sdfr kbdc_get_device_mask(sc->kbdc), 167441016Sdfr c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 167541016Sdfr /* CONTROLLER ERROR; this is serious, we may have 167641016Sdfr * been left with the inaccessible keyboard and 167741016Sdfr * the disabled mouse interrupt. 167841016Sdfr */ 167941016Sdfr error = EIO; 168041016Sdfr } 168141016Sdfr 168241016Sdfr kbdc_lock(sc->kbdc, FALSE); 168341016Sdfr return error; 168441016Sdfr} 168541016Sdfr 168641016Sdfrstatic int 168783366Sjulianpsmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 168841016Sdfr{ 168941016Sdfr struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 169041016Sdfr mousemode_t mode; 169141016Sdfr mousestatus_t status; 169241016Sdfr#if (defined(MOUSE_GETVARS)) 169341016Sdfr mousevar_t *var; 169441016Sdfr#endif 169541016Sdfr mousedata_t *data; 169641016Sdfr int stat[3]; 169741016Sdfr int command_byte; 169841016Sdfr int error = 0; 169941016Sdfr int s; 170041016Sdfr 170141016Sdfr /* Perform IOCTL command */ 170241016Sdfr switch (cmd) { 170341016Sdfr 170441016Sdfr case OLD_MOUSE_GETHWINFO: 170541016Sdfr s = spltty(); 170641016Sdfr ((old_mousehw_t *)addr)->buttons = sc->hw.buttons; 170741016Sdfr ((old_mousehw_t *)addr)->iftype = sc->hw.iftype; 170841016Sdfr ((old_mousehw_t *)addr)->type = sc->hw.type; 170948778Syokota ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff; 171041016Sdfr splx(s); 171141016Sdfr break; 171241016Sdfr 171341016Sdfr case MOUSE_GETHWINFO: 171441016Sdfr s = spltty(); 171541016Sdfr *(mousehw_t *)addr = sc->hw; 171641016Sdfr if (sc->mode.level == PSM_LEVEL_BASE) 171741016Sdfr ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC; 171841016Sdfr splx(s); 171941016Sdfr break; 172041016Sdfr 172141016Sdfr case OLD_MOUSE_GETMODE: 172241016Sdfr s = spltty(); 172341016Sdfr switch (sc->mode.level) { 172441016Sdfr case PSM_LEVEL_BASE: 172541016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 172641016Sdfr break; 172741016Sdfr case PSM_LEVEL_STANDARD: 172841016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 172941016Sdfr break; 173041016Sdfr case PSM_LEVEL_NATIVE: 173141016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 173241016Sdfr break; 173341016Sdfr } 173441016Sdfr ((old_mousemode_t *)addr)->rate = sc->mode.rate; 173541016Sdfr ((old_mousemode_t *)addr)->resolution = sc->mode.resolution; 173641016Sdfr ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor; 173741016Sdfr splx(s); 173841016Sdfr break; 173941016Sdfr 174041016Sdfr case MOUSE_GETMODE: 174141016Sdfr s = spltty(); 174241016Sdfr *(mousemode_t *)addr = sc->mode; 174341016Sdfr ((mousemode_t *)addr)->resolution = 174441016Sdfr MOUSE_RES_LOW - sc->mode.resolution; 174541016Sdfr switch (sc->mode.level) { 174641016Sdfr case PSM_LEVEL_BASE: 174741016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 174841016Sdfr ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE; 174941016Sdfr break; 175041016Sdfr case PSM_LEVEL_STANDARD: 175141016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 175241016Sdfr ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE; 175341016Sdfr ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK; 175441016Sdfr ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC; 175541016Sdfr break; 175641016Sdfr case PSM_LEVEL_NATIVE: 175741016Sdfr /* FIXME: this isn't quite correct... XXX */ 175841016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 175941016Sdfr break; 176041016Sdfr } 176141016Sdfr splx(s); 176241016Sdfr break; 176341016Sdfr 176441016Sdfr case OLD_MOUSE_SETMODE: 176541016Sdfr case MOUSE_SETMODE: 176641016Sdfr if (cmd == OLD_MOUSE_SETMODE) { 176741016Sdfr mode.rate = ((old_mousemode_t *)addr)->rate; 176841016Sdfr /* 176941016Sdfr * resolution old I/F new I/F 177041016Sdfr * default 0 0 177141016Sdfr * low 1 -2 177241016Sdfr * medium low 2 -3 177341016Sdfr * medium high 3 -4 177441016Sdfr * high 4 -5 177541016Sdfr */ 177641016Sdfr if (((old_mousemode_t *)addr)->resolution > 0) 177741016Sdfr mode.resolution = -((old_mousemode_t *)addr)->resolution - 1; 177841016Sdfr mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor; 177941016Sdfr mode.level = -1; 178041016Sdfr } else { 178141016Sdfr mode = *(mousemode_t *)addr; 178241016Sdfr } 178341016Sdfr 178441016Sdfr /* adjust and validate parameters. */ 178541016Sdfr if (mode.rate > UCHAR_MAX) 178641016Sdfr return EINVAL; 178741016Sdfr if (mode.rate == 0) 178841016Sdfr mode.rate = sc->dflt_mode.rate; 178941016Sdfr else if (mode.rate == -1) 179041016Sdfr /* don't change the current setting */ 179141016Sdfr ; 179241016Sdfr else if (mode.rate < 0) 179341016Sdfr return EINVAL; 179441016Sdfr if (mode.resolution >= UCHAR_MAX) 179541016Sdfr return EINVAL; 179641016Sdfr if (mode.resolution >= 200) 179741016Sdfr mode.resolution = MOUSE_RES_HIGH; 179841016Sdfr else if (mode.resolution >= 100) 179941016Sdfr mode.resolution = MOUSE_RES_MEDIUMHIGH; 180041016Sdfr else if (mode.resolution >= 50) 180141016Sdfr mode.resolution = MOUSE_RES_MEDIUMLOW; 180241016Sdfr else if (mode.resolution > 0) 180341016Sdfr mode.resolution = MOUSE_RES_LOW; 180441016Sdfr if (mode.resolution == MOUSE_RES_DEFAULT) 180541016Sdfr mode.resolution = sc->dflt_mode.resolution; 180641016Sdfr else if (mode.resolution == -1) 180741016Sdfr /* don't change the current setting */ 180841016Sdfr ; 180941016Sdfr else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 181041016Sdfr mode.resolution = MOUSE_RES_LOW - mode.resolution; 181141016Sdfr if (mode.level == -1) 181241016Sdfr /* don't change the current setting */ 181341016Sdfr mode.level = sc->mode.level; 181441016Sdfr else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX)) 181541016Sdfr return EINVAL; 181641016Sdfr if (mode.accelfactor == -1) 181741016Sdfr /* don't change the current setting */ 181841016Sdfr mode.accelfactor = sc->mode.accelfactor; 181941016Sdfr else if (mode.accelfactor < 0) 182041016Sdfr return EINVAL; 182141016Sdfr 182241016Sdfr /* don't allow anybody to poll the keyboard controller */ 182341016Sdfr error = block_mouse_data(sc, &command_byte); 182441016Sdfr if (error) 182541016Sdfr return error; 182641016Sdfr 182741016Sdfr /* set mouse parameters */ 182841016Sdfr if (mode.rate > 0) 182941016Sdfr mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 183041016Sdfr if (mode.resolution >= 0) 183141016Sdfr mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 183241016Sdfr set_mouse_scaling(sc->kbdc, 1); 183341016Sdfr get_mouse_status(sc->kbdc, stat, 0, 3); 183441016Sdfr 183541016Sdfr s = spltty(); 183641016Sdfr sc->mode.rate = mode.rate; 183741016Sdfr sc->mode.resolution = mode.resolution; 183841016Sdfr sc->mode.accelfactor = mode.accelfactor; 183941016Sdfr sc->mode.level = mode.level; 184041016Sdfr splx(s); 184141016Sdfr 184241016Sdfr unblock_mouse_data(sc, command_byte); 184341016Sdfr break; 184441016Sdfr 184541016Sdfr case MOUSE_GETLEVEL: 184641016Sdfr *(int *)addr = sc->mode.level; 184741016Sdfr break; 184841016Sdfr 184941016Sdfr case MOUSE_SETLEVEL: 185041016Sdfr if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX)) 185141016Sdfr return EINVAL; 185241016Sdfr sc->mode.level = *(int *)addr; 185341016Sdfr break; 185441016Sdfr 185541016Sdfr case MOUSE_GETSTATUS: 185641016Sdfr s = spltty(); 185741016Sdfr status = sc->status; 185841016Sdfr sc->status.flags = 0; 185941016Sdfr sc->status.obutton = sc->status.button; 186041016Sdfr sc->status.button = 0; 186141016Sdfr sc->status.dx = 0; 186241016Sdfr sc->status.dy = 0; 186341016Sdfr sc->status.dz = 0; 186441016Sdfr splx(s); 186541016Sdfr *(mousestatus_t *)addr = status; 186641016Sdfr break; 186741016Sdfr 186841016Sdfr#if (defined(MOUSE_GETVARS)) 186941016Sdfr case MOUSE_GETVARS: 187041016Sdfr var = (mousevar_t *)addr; 187141016Sdfr bzero(var, sizeof(*var)); 187241016Sdfr s = spltty(); 187341016Sdfr var->var[0] = MOUSE_VARS_PS2_SIG; 187441016Sdfr var->var[1] = sc->config; 187541016Sdfr var->var[2] = sc->flags; 187641016Sdfr splx(s); 187741016Sdfr break; 187841016Sdfr 187941016Sdfr case MOUSE_SETVARS: 188041016Sdfr return ENODEV; 188141016Sdfr#endif /* MOUSE_GETVARS */ 188241016Sdfr 188341016Sdfr case MOUSE_READSTATE: 188441016Sdfr case MOUSE_READDATA: 188541016Sdfr data = (mousedata_t *)addr; 188641016Sdfr if (data->len > sizeof(data->buf)/sizeof(data->buf[0])) 188741016Sdfr return EINVAL; 188841016Sdfr 188941016Sdfr error = block_mouse_data(sc, &command_byte); 189041016Sdfr if (error) 189141016Sdfr return error; 189241016Sdfr if ((data->len = get_mouse_status(sc->kbdc, data->buf, 189341016Sdfr (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0) 189441016Sdfr error = EIO; 189541016Sdfr unblock_mouse_data(sc, command_byte); 189641016Sdfr break; 189741016Sdfr 189841016Sdfr#if (defined(MOUSE_SETRESOLUTION)) 189941016Sdfr case MOUSE_SETRESOLUTION: 190041016Sdfr mode.resolution = *(int *)addr; 190141016Sdfr if (mode.resolution >= UCHAR_MAX) 190241016Sdfr return EINVAL; 190341016Sdfr else if (mode.resolution >= 200) 190441016Sdfr mode.resolution = MOUSE_RES_HIGH; 190541016Sdfr else if (mode.resolution >= 100) 190641016Sdfr mode.resolution = MOUSE_RES_MEDIUMHIGH; 190741016Sdfr else if (mode.resolution >= 50) 190841016Sdfr mode.resolution = MOUSE_RES_MEDIUMLOW; 190941016Sdfr else if (mode.resolution > 0) 191041016Sdfr mode.resolution = MOUSE_RES_LOW; 191141016Sdfr if (mode.resolution == MOUSE_RES_DEFAULT) 191241016Sdfr mode.resolution = sc->dflt_mode.resolution; 191341016Sdfr else if (mode.resolution == -1) 191441016Sdfr mode.resolution = sc->mode.resolution; 191541016Sdfr else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 191641016Sdfr mode.resolution = MOUSE_RES_LOW - mode.resolution; 191741016Sdfr 191841016Sdfr error = block_mouse_data(sc, &command_byte); 191941016Sdfr if (error) 192041016Sdfr return error; 192141016Sdfr sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 192241016Sdfr if (sc->mode.resolution != mode.resolution) 192341016Sdfr error = EIO; 192441016Sdfr unblock_mouse_data(sc, command_byte); 192541016Sdfr break; 192641016Sdfr#endif /* MOUSE_SETRESOLUTION */ 192741016Sdfr 192841016Sdfr#if (defined(MOUSE_SETRATE)) 192941016Sdfr case MOUSE_SETRATE: 193041016Sdfr mode.rate = *(int *)addr; 193141016Sdfr if (mode.rate > UCHAR_MAX) 193241016Sdfr return EINVAL; 193341016Sdfr if (mode.rate == 0) 193441016Sdfr mode.rate = sc->dflt_mode.rate; 193541016Sdfr else if (mode.rate < 0) 193641016Sdfr mode.rate = sc->mode.rate; 193741016Sdfr 193841016Sdfr error = block_mouse_data(sc, &command_byte); 193941016Sdfr if (error) 194041016Sdfr return error; 194141016Sdfr sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 194241016Sdfr if (sc->mode.rate != mode.rate) 194341016Sdfr error = EIO; 194441016Sdfr unblock_mouse_data(sc, command_byte); 194541016Sdfr break; 194641016Sdfr#endif /* MOUSE_SETRATE */ 194741016Sdfr 194841016Sdfr#if (defined(MOUSE_SETSCALING)) 194941016Sdfr case MOUSE_SETSCALING: 195041016Sdfr if ((*(int *)addr <= 0) || (*(int *)addr > 2)) 195141016Sdfr return EINVAL; 195241016Sdfr 195341016Sdfr error = block_mouse_data(sc, &command_byte); 195441016Sdfr if (error) 195541016Sdfr return error; 195641016Sdfr if (!set_mouse_scaling(sc->kbdc, *(int *)addr)) 195741016Sdfr error = EIO; 195841016Sdfr unblock_mouse_data(sc, command_byte); 195941016Sdfr break; 196041016Sdfr#endif /* MOUSE_SETSCALING */ 196141016Sdfr 196241016Sdfr#if (defined(MOUSE_GETHWID)) 196341016Sdfr case MOUSE_GETHWID: 196441016Sdfr error = block_mouse_data(sc, &command_byte); 196541016Sdfr if (error) 196641016Sdfr return error; 196748778Syokota sc->hw.hwid &= ~0x00ff; 196848778Syokota sc->hw.hwid |= get_aux_id(sc->kbdc); 196948778Syokota *(int *)addr = sc->hw.hwid & 0x00ff; 197041016Sdfr unblock_mouse_data(sc, command_byte); 197141016Sdfr break; 197241016Sdfr#endif /* MOUSE_GETHWID */ 197341016Sdfr 197441016Sdfr default: 197541016Sdfr return ENOTTY; 197641016Sdfr } 197741016Sdfr 197841016Sdfr return error; 197941016Sdfr} 198041016Sdfr 198141016Sdfrstatic void 198258230Syokotapsmtimeout(void *arg) 198358230Syokota{ 198458230Syokota struct psm_softc *sc; 198565045Syokota int s; 198658230Syokota 198784880Syokota sc = (struct psm_softc *)arg; 198865045Syokota s = spltty(); 198963746Syokota if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) { 199058230Syokota if (verbose >= 4) 199184880Syokota log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit); 199258230Syokota psmintr(sc); 199363746Syokota kbdc_lock(sc->kbdc, FALSE); 199458230Syokota } 199558230Syokota sc->watchdog = TRUE; 199665045Syokota splx(s); 199784880Syokota sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz); 199858230Syokota} 199958230Syokota 2000123442Salfredstatic int psmhz = 20; 2001123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmhz, CTLFLAG_RW, &psmhz, 0, ""); 2002123442Salfred 2003123442Salfredstatic int psm_soft_timeout = 500000; /* 0.5 sec */ 2004123442SalfredSYSCTL_INT(_debug, OID_AUTO, psm_soft_timeout, CTLFLAG_RW, 2005123442Salfred &psm_soft_timeout, 0, ""); 2006123442Salfred 2007123442Salfredstatic int psmerrsecs = 2; 2008123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrsecs, CTLFLAG_RW, &psmerrsecs, 0, ""); 2009123442Salfredstatic int psmerrusecs = 0; 2010123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrusecs, CTLFLAG_RW, &psmerrusecs, 0, ""); 2011123442Salfredstatic int psmsecs = 0; 2012123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmsecs, CTLFLAG_RW, &psmsecs, 0, ""); 2013123442Salfredstatic int psmusecs = 500000; 2014123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmusecs, CTLFLAG_RW, &psmusecs, 0, ""); 2015123442Salfred 201658230Syokotastatic void 201741016Sdfrpsmintr(void *arg) 201841016Sdfr{ 2019123442Salfred struct psm_softc *sc = arg; 2020123442Salfred struct timeval now; 202141016Sdfr int c; 2022123442Salfred packetbuf_t *pb; 2023123442Salfred int haderror = 0; 202441016Sdfr 2025123442Salfred 202641016Sdfr /* read until there is nothing to read */ 202741016Sdfr while((c = read_aux_data_no_wait(sc->kbdc)) != -1) { 202841016Sdfr 2029123442Salfred pb = &sc->pqueue[sc->pqueue_end]; 203041016Sdfr /* discard the byte if the device is not open */ 203141016Sdfr if ((sc->state & PSM_OPEN) == 0) 203241016Sdfr continue; 203341016Sdfr 2034123442Salfred getmicrouptime(&now); 2035123442Salfred if ((pb->inputbytes > 0) && timevalcmp(&now, &sc->inputtimeout, >)) { 2036127841Smarkm#if DEBUG 203784880Syokota log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n"); 2038127841Smarkm#endif 2039123442Salfred pb->inputbytes = 0; 204084880Syokota sc->syncerrors = 0; 204184880Syokota } 204284880Syokota sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000; 204384880Syokota sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000; 2044123442Salfred timevaladd(&sc->inputtimeout, &now); 204584880Syokota 2046123442Salfred pb->ipacket[pb->inputbytes++] = c; 2047123442Salfred if (pb->inputbytes < sc->mode.packetsize) 204841016Sdfr continue; 204941016Sdfr 2050127841Smarkm#if DEBUG 205141016Sdfr log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n", 2052123442Salfred pb->ipacket[0], pb->ipacket[1], pb->ipacket[2], 2053123442Salfred pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]); 205441016Sdfr#endif 205541016Sdfr 2056123442Salfred c = pb->ipacket[0]; 205741016Sdfr 205863746Syokota if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) { 2059127841Smarkm#if DEBUG 2060123442Salfred log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x) %d" 2061123442Salfred " cmds since last error.\n", 2062123442Salfred c & sc->mode.syncmask[0], sc->mode.syncmask[1], 2063123442Salfred sc->cmdcount - sc->lasterr); 2064127841Smarkm#endif 2065123442Salfred haderror = 1; 2066123442Salfred sc->lasterr = sc->cmdcount; 2067123442Salfred dropqueue(sc); 206884880Syokota ++sc->syncerrors; 2069123442Salfred sc->lastinputerr = now; 207084880Syokota if (sc->syncerrors < sc->mode.packetsize) { 2071127841Smarkm#if DEBUG 207284880Syokota log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors); 2073127841Smarkm#endif 2074123442Salfred --pb->inputbytes; 2075123442Salfred bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes); 207684880Syokota } else if (sc->syncerrors == sc->mode.packetsize) { 2077127841Smarkm#if DEBUG 207869439Syokota log(LOG_DEBUG, "psmintr: re-enable the mouse.\n"); 2079127841Smarkm#endif 2080123442Salfred pb->inputbytes = 0; 208169439Syokota disable_aux_dev(sc->kbdc); 208269439Syokota enable_aux_dev(sc->kbdc); 208384880Syokota } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) { 2084127841Smarkm#if DEBUG 208584880Syokota log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors); 2086127841Smarkm#endif 2087123442Salfred --pb->inputbytes; 2088123442Salfred bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes); 208984880Syokota } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) { 2090127841Smarkm#if DEBUG 209184880Syokota log(LOG_DEBUG, "psmintr: reset the mouse.\n"); 2092127841Smarkm#endif 209384880Syokota reinitialize(sc, TRUE); 209469439Syokota } 209584880Syokota continue; 209663746Syokota } 2097123442Salfred /* if this packet is at all bogus then drop the packet. */ 2098123442Salfred if (haderror || 2099123442Salfred !timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, &now)) { 2100123442Salfred pb->inputbytes = 0; 2101123442Salfred haderror = 0; 2102123442Salfred continue; 2103123442Salfred } 210463746Syokota 2105123442Salfred sc->cmdcount++; 2106123442Salfred if (++sc->pqueue_end >= PSM_PACKETQUEUE) 2107123442Salfred sc->pqueue_end = 0; 2108123442Salfred /* 2109123442Salfred * If we've filled the queue then call the softintr ourselves, 2110123442Salfred * otherwise schedule the interrupt for later. 2111123442Salfred */ 2112123442Salfred if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) || 2113123442Salfred (sc->pqueue_end == sc->pqueue_start)) { 2114123442Salfred if ((sc->state & PSM_SOFTARMED) != 0) { 2115123442Salfred sc->state &= ~PSM_SOFTARMED; 2116123442Salfred untimeout(psmsoftintr, arg, sc->softcallout); 2117123442Salfred } 2118123442Salfred psmsoftintr(arg); 2119123442Salfred } else if ((sc->state & PSM_SOFTARMED) == 0) { 2120123442Salfred sc->state |= PSM_SOFTARMED; 2121123442Salfred sc->softcallout = timeout(psmsoftintr, arg, 2122123442Salfred psmhz < 1 ? 1 : (hz/psmhz)); 2123123442Salfred } 2124123442Salfred } 2125123442Salfred} 2126123442Salfred 2127123442Salfredstatic void 2128123442Salfredpsmsoftintr(void *arg) 2129123442Salfred{ 2130123442Salfred /* 2131123442Salfred * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN) 2132123442Salfred * into `mousestatus' button bits (MOUSE_BUTTON?DOWN). 2133123442Salfred */ 2134123442Salfred static int butmap[8] = { 2135123442Salfred 0, 2136123442Salfred MOUSE_BUTTON1DOWN, 2137123442Salfred MOUSE_BUTTON3DOWN, 2138123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 2139123442Salfred MOUSE_BUTTON2DOWN, 2140123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 2141123442Salfred MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 2142123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 2143123442Salfred }; 2144123442Salfred static int butmap_versapad[8] = { 2145123442Salfred 0, 2146123442Salfred MOUSE_BUTTON3DOWN, 2147123442Salfred 0, 2148123442Salfred MOUSE_BUTTON3DOWN, 2149123442Salfred MOUSE_BUTTON1DOWN, 2150123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 2151123442Salfred MOUSE_BUTTON1DOWN, 2152123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN 2153123442Salfred }; 2154123442Salfred register struct psm_softc *sc = arg; 2155123442Salfred mousestatus_t ms; 2156123442Salfred int x, y, z; 2157123442Salfred int c; 2158123442Salfred int l; 2159123442Salfred int x0, y0; 2160123442Salfred int s; 2161123442Salfred packetbuf_t *pb; 2162123442Salfred 2163123442Salfred getmicrouptime(&sc->lastsoftintr); 2164123442Salfred 2165123442Salfred s = spltty(); 2166123442Salfred 2167123442Salfred do { 2168123442Salfred 2169123442Salfred pb = &sc->pqueue[sc->pqueue_start]; 2170123442Salfred c = pb->ipacket[0]; 217149965Syokota /* 217241016Sdfr * A kludge for Kensington device! 217341016Sdfr * The MSB of the horizontal count appears to be stored in 217458230Syokota * a strange place. 217541016Sdfr */ 217658230Syokota if (sc->hw.model == MOUSE_MODEL_THINK) 2177123442Salfred pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0; 217841016Sdfr 217941016Sdfr /* ignore the overflow bits... */ 2180123442Salfred x = (c & MOUSE_PS2_XNEG) ? pb->ipacket[1] - 256 : pb->ipacket[1]; 2181123442Salfred y = (c & MOUSE_PS2_YNEG) ? pb->ipacket[2] - 256 : pb->ipacket[2]; 218241016Sdfr z = 0; 218341016Sdfr ms.obutton = sc->button; /* previous button state */ 218441016Sdfr ms.button = butmap[c & MOUSE_PS2_BUTTONS]; 218545789Speter /* `tapping' action */ 218645789Speter if (sc->config & PSM_CONFIG_FORCETAP) 218745789Speter ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 218841016Sdfr 218941016Sdfr switch (sc->hw.model) { 219041016Sdfr 219158230Syokota case MOUSE_MODEL_EXPLORER: 219258230Syokota /* 219358230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 219458230Syokota * byte 1: oy ox sy sx 1 M R L 219558230Syokota * byte 2: x x x x x x x x 219658230Syokota * byte 3: y y y y y y y y 219758230Syokota * byte 4: * * S2 S1 s d2 d1 d0 219858230Syokota * 219958230Syokota * L, M, R, S1, S2: left, middle, right and side buttons 220058230Syokota * s: wheel data sign bit 220158230Syokota * d2-d0: wheel data 220258230Syokota */ 2203123442Salfred z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) 2204123442Salfred ? (pb->ipacket[3] & 0x0f) - 16 : (pb->ipacket[3] & 0x0f); 2205123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) 220658230Syokota ? MOUSE_BUTTON4DOWN : 0; 2207123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) 220858230Syokota ? MOUSE_BUTTON5DOWN : 0; 220958230Syokota break; 221058230Syokota 221141016Sdfr case MOUSE_MODEL_INTELLI: 221241016Sdfr case MOUSE_MODEL_NET: 221341016Sdfr /* wheel data is in the fourth byte */ 2214123442Salfred z = (char)pb->ipacket[3]; 221558230Syokota /* some mice may send 7 when there is no Z movement?! XXX */ 221658230Syokota if ((z >= 7) || (z <= -7)) 221758230Syokota z = 0; 221858230Syokota /* some compatible mice have additional buttons */ 221958230Syokota ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) 222058230Syokota ? MOUSE_BUTTON4DOWN : 0; 222158230Syokota ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) 222258230Syokota ? MOUSE_BUTTON5DOWN : 0; 222341016Sdfr break; 222441016Sdfr 222541016Sdfr case MOUSE_MODEL_MOUSEMANPLUS: 222648778Syokota /* 222748778Syokota * PS2++ protocl packet 222848778Syokota * 222948778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 223048778Syokota * byte 1: * 1 p3 p2 1 * * * 223148778Syokota * byte 2: c1 c2 p1 p0 d1 d0 1 0 223248778Syokota * 223348778Syokota * p3-p0: packet type 223448778Syokota * c1, c2: c1 & c2 == 1, if p2 == 0 223548778Syokota * c1 & c2 == 0, if p2 == 1 223648778Syokota * 223748778Syokota * packet type: 0 (device type) 223848778Syokota * See comments in enable_mmanplus() below. 223948778Syokota * 224048778Syokota * packet type: 1 (wheel data) 224148778Syokota * 224248778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 224348778Syokota * byte 3: h * B5 B4 s d2 d1 d0 224448778Syokota * 224548778Syokota * h: 1, if horizontal roller data 224648778Syokota * 0, if vertical roller data 224748778Syokota * B4, B5: button 4 and 5 224848778Syokota * s: sign bit 224948778Syokota * d2-d0: roller data 225048778Syokota * 225148778Syokota * packet type: 2 (reserved) 225248778Syokota */ 225348778Syokota if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) 225448778Syokota && (abs(x) > 191) 2255123442Salfred && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) { 225641016Sdfr /* the extended data packet encodes button and wheel events */ 2257123442Salfred switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) { 225848778Syokota case 1: 225948778Syokota /* wheel data packet */ 226048778Syokota x = y = 0; 2261123442Salfred if (pb->ipacket[2] & 0x80) { 226248778Syokota /* horizontal roller count - ignore it XXX*/ 226348778Syokota } else { 226448778Syokota /* vertical roller count */ 2265123442Salfred z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) 2266123442Salfred ? (pb->ipacket[2] & 0x0f) - 16 2267123442Salfred : (pb->ipacket[2] & 0x0f); 226848778Syokota } 2269123442Salfred ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN) 227048778Syokota ? MOUSE_BUTTON4DOWN : 0; 2271123442Salfred ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN) 227248778Syokota ? MOUSE_BUTTON5DOWN : 0; 227348778Syokota break; 227448778Syokota case 2: 227558230Syokota /* this packet type is reserved by Logitech... */ 227658230Syokota /* 227758230Syokota * IBM ScrollPoint Mouse uses this packet type to 227858230Syokota * encode both vertical and horizontal scroll movement. 227958230Syokota */ 228058230Syokota x = y = 0; 228158230Syokota /* horizontal count */ 2282123442Salfred if (pb->ipacket[2] & 0x0f) 2283123442Salfred z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2; 228458230Syokota /* vertical count */ 2285123442Salfred if (pb->ipacket[2] & 0xf0) 2286123442Salfred z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1; 228758230Syokota#if 0 228858230Syokota /* vertical count */ 2289123442Salfred z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) 2290123442Salfred ? ((pb->ipacket[2] >> 4) & 0x0f) - 16 2291123442Salfred : ((pb->ipacket[2] >> 4) & 0x0f); 229258230Syokota /* horizontal count */ 2293123442Salfred w = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) 2294123442Salfred ? (pb->ipacket[2] & 0x0f) - 16 2295123442Salfred : (pb->ipacket[2] & 0x0f); 229658230Syokota#endif 229758230Syokota break; 229848778Syokota case 0: 229948778Syokota /* device type packet - shouldn't happen */ 2300102412Scharnier /* FALLTHROUGH */ 230148778Syokota default: 230248778Syokota x = y = 0; 230348778Syokota ms.button = ms.obutton; 230458230Syokota if (bootverbose) 230558230Syokota log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: " 230658230Syokota "0x%02x 0x%02x 0x%02x\n", 2307123442Salfred MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket), 2308123442Salfred pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]); 230948778Syokota break; 231048778Syokota } 231141016Sdfr } else { 231241016Sdfr /* preserve button states */ 231341016Sdfr ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 231441016Sdfr } 231541016Sdfr break; 231641016Sdfr 231741016Sdfr case MOUSE_MODEL_GLIDEPOINT: 231841016Sdfr /* `tapping' action */ 231941016Sdfr ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 232041016Sdfr break; 232141016Sdfr 232241016Sdfr case MOUSE_MODEL_NETSCROLL: 232358230Syokota /* three addtional bytes encode buttons and wheel events */ 2324123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) 232541016Sdfr ? MOUSE_BUTTON4DOWN : 0; 2326123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) 232758230Syokota ? MOUSE_BUTTON5DOWN : 0; 2328123442Salfred z = (pb->ipacket[3] & MOUSE_PS2_XNEG) 2329123442Salfred ? pb->ipacket[4] - 256 : pb->ipacket[4]; 233041016Sdfr break; 233141016Sdfr 233241016Sdfr case MOUSE_MODEL_THINK: 233341016Sdfr /* the fourth button state in the first byte */ 233441016Sdfr ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0; 233541016Sdfr break; 233641016Sdfr 233749965Syokota case MOUSE_MODEL_VERSAPAD: 233849965Syokota /* VersaPad PS/2 absolute mode message format 233949965Syokota * 234049965Syokota * [packet1] 7 6 5 4 3 2 1 0(LSB) 234149965Syokota * ipacket[0]: 1 1 0 A 1 L T R 234249965Syokota * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0 234349965Syokota * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0 234449965Syokota * ipacket[3]: 1 1 1 A 1 L T R 234549965Syokota * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8 234649965Syokota * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0 234749965Syokota * 234849965Syokota * [note] 234949965Syokota * R: right physical mouse button (1=on) 235049965Syokota * T: touch pad virtual button (1=tapping) 235149965Syokota * L: left physical mouse button (1=on) 235249965Syokota * A: position data is valid (1=valid) 235349965Syokota * H: horizontal data (12bit signed integer. H11 is sign bit.) 235449965Syokota * V: vertical data (12bit signed integer. V11 is sign bit.) 235549965Syokota * P: pressure data 235649965Syokota * 235749965Syokota * Tapping is mapped to MOUSE_BUTTON4. 235849965Syokota */ 235949965Syokota ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS]; 236049965Syokota ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 236149965Syokota x = y = 0; 236249965Syokota if (c & MOUSE_PS2VERSA_IN_USE) { 2363123442Salfred x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8); 2364123442Salfred y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4); 236549965Syokota if (x0 & 0x800) 236649965Syokota x0 -= 0x1000; 236749965Syokota if (y0 & 0x800) 236849965Syokota y0 -= 0x1000; 236949965Syokota if (sc->flags & PSM_FLAGS_FINGERDOWN) { 237049965Syokota x = sc->xold - x0; 237149965Syokota y = y0 - sc->yold; 237249965Syokota if (x < 0) /* XXX */ 237349965Syokota x++; 237449965Syokota else if (x) 237549965Syokota x--; 237649965Syokota if (y < 0) 237749965Syokota y++; 237849965Syokota else if (y) 237949965Syokota y--; 238049965Syokota } else { 238149965Syokota sc->flags |= PSM_FLAGS_FINGERDOWN; 238249965Syokota } 238349965Syokota sc->xold = x0; 238449965Syokota sc->yold = y0; 238549965Syokota } else { 238649965Syokota sc->flags &= ~PSM_FLAGS_FINGERDOWN; 238749965Syokota } 238849965Syokota c = ((x < 0) ? MOUSE_PS2_XNEG : 0) 238949965Syokota | ((y < 0) ? MOUSE_PS2_YNEG : 0); 239049965Syokota break; 239149965Syokota 239258230Syokota case MOUSE_MODEL_4D: 239358230Syokota /* 239458230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 239558230Syokota * byte 1: s2 d2 s1 d1 1 M R L 239658230Syokota * byte 2: sx x x x x x x x 239758230Syokota * byte 3: sy y y y y y y y 239858230Syokota * 239958230Syokota * s1: wheel 1 direction 240058230Syokota * d1: wheel 1 data 240158230Syokota * s2: wheel 2 direction 240258230Syokota * d2: wheel 2 data 240358230Syokota */ 2404123442Salfred x = (pb->ipacket[1] & 0x80) ? pb->ipacket[1] - 256 : pb->ipacket[1]; 2405123442Salfred y = (pb->ipacket[2] & 0x80) ? pb->ipacket[2] - 256 : pb->ipacket[2]; 240658230Syokota switch (c & MOUSE_4D_WHEELBITS) { 240758230Syokota case 0x10: 240858230Syokota z = 1; 240958230Syokota break; 241058230Syokota case 0x30: 241158230Syokota z = -1; 241258230Syokota break; 241358230Syokota case 0x40: /* 2nd wheel turning right XXX */ 241458230Syokota z = 2; 241558230Syokota break; 241658230Syokota case 0xc0: /* 2nd wheel turning left XXX */ 241758230Syokota z = -2; 241858230Syokota break; 241958230Syokota } 242058230Syokota break; 242158230Syokota 242258230Syokota case MOUSE_MODEL_4DPLUS: 242358230Syokota if ((x < 16 - 256) && (y < 16 - 256)) { 242458230Syokota /* 242558230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 242658230Syokota * byte 1: 0 0 1 1 1 M R L 242758230Syokota * byte 2: 0 0 0 0 1 0 0 0 242858230Syokota * byte 3: 0 0 0 0 S s d1 d0 242958230Syokota * 243058230Syokota * L, M, R, S: left, middle, right and side buttons 243158230Syokota * s: wheel data sign bit 243258230Syokota * d1-d0: wheel data 243358230Syokota */ 243458230Syokota x = y = 0; 2435123442Salfred if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN) 243658230Syokota ms.button |= MOUSE_BUTTON4DOWN; 2437123442Salfred z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) 2438123442Salfred ? ((pb->ipacket[2] & 0x07) - 8) 2439123442Salfred : (pb->ipacket[2] & 0x07) ; 244058230Syokota } else { 244158230Syokota /* preserve previous button states */ 244258230Syokota ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 244358230Syokota } 244458230Syokota break; 244558230Syokota 244641016Sdfr case MOUSE_MODEL_GENERIC: 244741016Sdfr default: 244841016Sdfr break; 244941016Sdfr } 245041016Sdfr 245141016Sdfr /* scale values */ 245241016Sdfr if (sc->mode.accelfactor >= 1) { 245341016Sdfr if (x != 0) { 245441016Sdfr x = x * x / sc->mode.accelfactor; 245541016Sdfr if (x == 0) 245641016Sdfr x = 1; 245741016Sdfr if (c & MOUSE_PS2_XNEG) 245841016Sdfr x = -x; 245941016Sdfr } 246041016Sdfr if (y != 0) { 246141016Sdfr y = y * y / sc->mode.accelfactor; 246241016Sdfr if (y == 0) 246341016Sdfr y = 1; 246441016Sdfr if (c & MOUSE_PS2_YNEG) 246541016Sdfr y = -y; 246641016Sdfr } 246741016Sdfr } 246841016Sdfr 246941016Sdfr ms.dx = x; 247041016Sdfr ms.dy = y; 247141016Sdfr ms.dz = z; 247241016Sdfr ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) 247341016Sdfr | (ms.obutton ^ ms.button); 247441016Sdfr 247541016Sdfr if (sc->mode.level < PSM_LEVEL_NATIVE) 2476123442Salfred pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket); 247741016Sdfr 247841016Sdfr sc->status.flags |= ms.flags; 247941016Sdfr sc->status.dx += ms.dx; 248041016Sdfr sc->status.dy += ms.dy; 248141016Sdfr sc->status.dz += ms.dz; 248241016Sdfr sc->status.button = ms.button; 248341016Sdfr sc->button = ms.button; 248441016Sdfr 248558230Syokota sc->watchdog = FALSE; 248658230Syokota 248741016Sdfr /* queue data */ 2488123442Salfred if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) { 2489123442Salfred l = imin(pb->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail); 2490123442Salfred bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l); 2491123442Salfred if (pb->inputbytes > l) 2492123442Salfred bcopy(&pb->ipacket[l], &sc->queue.buf[0], pb->inputbytes - l); 249341016Sdfr sc->queue.tail = 2494123442Salfred (sc->queue.tail + pb->inputbytes) % sizeof(sc->queue.buf); 2495123442Salfred sc->queue.count += pb->inputbytes; 249641016Sdfr } 2497123442Salfred pb->inputbytes = 0; 249841016Sdfr 2499123442Salfred if (++sc->pqueue_start >= PSM_PACKETQUEUE) 2500123442Salfred sc->pqueue_start = 0; 2501123442Salfred } while (sc->pqueue_start != sc->pqueue_end); 2502123442Salfred if (sc->state & PSM_ASLP) { 2503123442Salfred sc->state &= ~PSM_ASLP; 2504123442Salfred wakeup( sc); 250541016Sdfr } 2506123442Salfred selwakeuppri(&sc->rsel, PZERO); 2507123442Salfred sc->state &= ~PSM_SOFTARMED; 2508123442Salfred splx(s); 250941016Sdfr} 251041016Sdfr 251141016Sdfrstatic int 251283366Sjulianpsmpoll(dev_t dev, int events, struct thread *td) 251341016Sdfr{ 251441016Sdfr struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 251541016Sdfr int s; 251641016Sdfr int revents = 0; 251741016Sdfr 251841016Sdfr /* Return true if a mouse event available */ 251941016Sdfr s = spltty(); 252045789Speter if (events & (POLLIN | POLLRDNORM)) { 252141016Sdfr if (sc->queue.count > 0) 252241016Sdfr revents |= events & (POLLIN | POLLRDNORM); 252341016Sdfr else 252483366Sjulian selrecord(td, &sc->rsel); 252545789Speter } 252641016Sdfr splx(s); 252741016Sdfr 252841016Sdfr return (revents); 252941016Sdfr} 253041016Sdfr 253141016Sdfr/* vendor/model specific routines */ 253241016Sdfr 253341016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status) 253441016Sdfr{ 253541016Sdfr if (set_mouse_resolution(kbdc, res) != res) 253641016Sdfr return FALSE; 253741016Sdfr if (set_mouse_scaling(kbdc, scale) 253841016Sdfr && set_mouse_scaling(kbdc, scale) 253941016Sdfr && set_mouse_scaling(kbdc, scale) 254041016Sdfr && (get_mouse_status(kbdc, status, 0, 3) >= 3)) 254141016Sdfr return TRUE; 254241016Sdfr return FALSE; 254341016Sdfr} 254441016Sdfr 254569438Syokotastatic int 254669438Syokotamouse_ext_command(KBDC kbdc, int command) 254769438Syokota{ 254869438Syokota int c; 254969438Syokota 255069438Syokota c = (command >> 6) & 0x03; 255169438Syokota if (set_mouse_resolution(kbdc, c) != c) 255269438Syokota return FALSE; 255369438Syokota c = (command >> 4) & 0x03; 255469438Syokota if (set_mouse_resolution(kbdc, c) != c) 255569438Syokota return FALSE; 255669438Syokota c = (command >> 2) & 0x03; 255769438Syokota if (set_mouse_resolution(kbdc, c) != c) 255869438Syokota return FALSE; 255969438Syokota c = (command >> 0) & 0x03; 256069438Syokota if (set_mouse_resolution(kbdc, c) != c) 256169438Syokota return FALSE; 256269438Syokota return TRUE; 256369438Syokota} 256469438Syokota 256541016Sdfr#if notyet 256641016Sdfr/* Logitech MouseMan Cordless II */ 256741016Sdfrstatic int 256841016Sdfrenable_lcordless(struct psm_softc *sc) 256941016Sdfr{ 257041016Sdfr int status[3]; 257141016Sdfr int ch; 257241016Sdfr 257341016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status)) 257441016Sdfr return FALSE; 257541016Sdfr if (status[1] == PSMD_RES_HIGH) 257641016Sdfr return FALSE; 257741016Sdfr ch = (status[0] & 0x07) - 1; /* channel # */ 257841016Sdfr if ((ch <= 0) || (ch > 4)) 257941016Sdfr return FALSE; 258041016Sdfr /* 258141016Sdfr * status[1]: always one? 258241016Sdfr * status[2]: battery status? (0-100) 258341016Sdfr */ 258441016Sdfr return TRUE; 258541016Sdfr} 258641016Sdfr#endif /* notyet */ 258741016Sdfr 258858230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */ 258941016Sdfrstatic int 259041016Sdfrenable_groller(struct psm_softc *sc) 259141016Sdfr{ 259241016Sdfr int status[3]; 259341016Sdfr 259441016Sdfr /* 259541016Sdfr * The special sequence to enable the fourth button and the 259641016Sdfr * roller. Immediately after this sequence check status bytes. 259741016Sdfr * if the mouse is NetScroll, the second and the third bytes are 259841016Sdfr * '3' and 'D'. 259941016Sdfr */ 260041016Sdfr 260141016Sdfr /* 260241016Sdfr * If the mouse is an ordinary PS/2 mouse, the status bytes should 260341016Sdfr * look like the following. 260441016Sdfr * 260541016Sdfr * byte 1 bit 7 always 0 260641016Sdfr * bit 6 stream mode (0) 260741016Sdfr * bit 5 disabled (0) 260841016Sdfr * bit 4 1:1 scaling (0) 260941016Sdfr * bit 3 always 0 261041016Sdfr * bit 0-2 button status 261141016Sdfr * byte 2 resolution (PSMD_RES_HIGH) 261241016Sdfr * byte 3 report rate (?) 261341016Sdfr */ 261441016Sdfr 261541016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 261641016Sdfr return FALSE; 261741016Sdfr if ((status[1] != '3') || (status[2] != 'D')) 261841016Sdfr return FALSE; 261958230Syokota /* FIXME: SmartScroll Mouse has 5 buttons! XXX */ 262041016Sdfr sc->hw.buttons = 4; 262141016Sdfr return TRUE; 262241016Sdfr} 262341016Sdfr 262458230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */ 262541016Sdfrstatic int 262641016Sdfrenable_gmouse(struct psm_softc *sc) 262741016Sdfr{ 262841016Sdfr int status[3]; 262941016Sdfr 263041016Sdfr /* 263141016Sdfr * The special sequence to enable the middle, "rubber" button. 263241016Sdfr * Immediately after this sequence check status bytes. 263341016Sdfr * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse, 263441016Sdfr * the second and the third bytes are '3' and 'U'. 263541016Sdfr * NOTE: NetMouse reports that it has three buttons although it has 263641016Sdfr * two buttons and a rubber button. NetMouse Pro and MIE Mouse 263741016Sdfr * say they have three buttons too and they do have a button on the 263841016Sdfr * side... 263941016Sdfr */ 264041016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 264141016Sdfr return FALSE; 264241016Sdfr if ((status[1] != '3') || (status[2] != 'U')) 264341016Sdfr return FALSE; 264441016Sdfr return TRUE; 264541016Sdfr} 264641016Sdfr 264741016Sdfr/* ALPS GlidePoint */ 264841016Sdfrstatic int 264941016Sdfrenable_aglide(struct psm_softc *sc) 265041016Sdfr{ 265141016Sdfr int status[3]; 265241016Sdfr 265341016Sdfr /* 265441016Sdfr * The special sequence to obtain ALPS GlidePoint specific 265541016Sdfr * information. Immediately after this sequence, status bytes will 265641016Sdfr * contain something interesting. 265741016Sdfr * NOTE: ALPS produces several models of GlidePoint. Some of those 265841016Sdfr * do not respond to this sequence, thus, cannot be detected this way. 265941016Sdfr */ 266050149Syokota if (set_mouse_sampling_rate(sc->kbdc, 100) != 100) 266150149Syokota return FALSE; 266241016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status)) 266341016Sdfr return FALSE; 266450149Syokota if ((status[1] == PSMD_RES_LOW) || (status[2] == 100)) 266541016Sdfr return FALSE; 266641016Sdfr return TRUE; 266741016Sdfr} 266841016Sdfr 266941016Sdfr/* Kensington ThinkingMouse/Trackball */ 267041016Sdfrstatic int 267141016Sdfrenable_kmouse(struct psm_softc *sc) 267241016Sdfr{ 267341016Sdfr static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 }; 267441016Sdfr KBDC kbdc = sc->kbdc; 267541016Sdfr int status[3]; 267641016Sdfr int id1; 267741016Sdfr int id2; 267841016Sdfr int i; 267941016Sdfr 268041016Sdfr id1 = get_aux_id(kbdc); 268141016Sdfr if (set_mouse_sampling_rate(kbdc, 10) != 10) 268241016Sdfr return FALSE; 268341016Sdfr /* 268441016Sdfr * The device is now in the native mode? It returns a different 268541016Sdfr * ID value... 268641016Sdfr */ 268741016Sdfr id2 = get_aux_id(kbdc); 268841016Sdfr if ((id1 == id2) || (id2 != 2)) 268941016Sdfr return FALSE; 269041016Sdfr 269141016Sdfr if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 269241016Sdfr return FALSE; 269341016Sdfr#if PSM_DEBUG >= 2 269441016Sdfr /* at this point, resolution is LOW, sampling rate is 10/sec */ 269541016Sdfr if (get_mouse_status(kbdc, status, 0, 3) < 3) 269641016Sdfr return FALSE; 269741016Sdfr#endif 269841016Sdfr 269941016Sdfr /* 270041016Sdfr * The special sequence to enable the third and fourth buttons. 270141016Sdfr * Otherwise they behave like the first and second buttons. 270241016Sdfr */ 270341016Sdfr for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 270441016Sdfr if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 270541016Sdfr return FALSE; 270641016Sdfr } 270741016Sdfr 270841016Sdfr /* 270941016Sdfr * At this point, the device is using default resolution and 271041016Sdfr * sampling rate for the native mode. 271141016Sdfr */ 271241016Sdfr if (get_mouse_status(kbdc, status, 0, 3) < 3) 271341016Sdfr return FALSE; 271441016Sdfr if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1])) 271541016Sdfr return FALSE; 271641016Sdfr 271741016Sdfr /* the device appears be enabled by this sequence, diable it for now */ 271841016Sdfr disable_aux_dev(kbdc); 271941016Sdfr empty_aux_buffer(kbdc, 5); 272041016Sdfr 272141016Sdfr return TRUE; 272241016Sdfr} 272341016Sdfr 272458230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */ 272541016Sdfrstatic int 272641016Sdfrenable_mmanplus(struct psm_softc *sc) 272741016Sdfr{ 272841016Sdfr KBDC kbdc = sc->kbdc; 272941016Sdfr int data[3]; 273041016Sdfr 273141016Sdfr /* the special sequence to enable the fourth button and the roller. */ 273258230Syokota /* 273358230Syokota * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION 273458230Syokota * must be called exactly three times since the last RESET command 273558230Syokota * before this sequence. XXX 273658230Syokota */ 273769438Syokota if (!set_mouse_scaling(kbdc, 1)) 273869438Syokota return FALSE; 273969438Syokota if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb)) 274069438Syokota return FALSE; 274141016Sdfr if (get_mouse_status(kbdc, data, 1, 3) < 3) 274241016Sdfr return FALSE; 274341016Sdfr 274448778Syokota /* 274548778Syokota * PS2++ protocl, packet type 0 274641016Sdfr * 274748778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 274848778Syokota * byte 1: * 1 p3 p2 1 * * * 274948778Syokota * byte 2: 1 1 p1 p0 m1 m0 1 0 275048778Syokota * byte 3: m7 m6 m5 m4 m3 m2 m1 m0 275148778Syokota * 275248778Syokota * p3-p0: packet type: 0 275358230Syokota * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58... 275441016Sdfr */ 275548778Syokota /* check constant bits */ 275648778Syokota if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC) 275741016Sdfr return FALSE; 275848778Syokota if ((data[1] & 0xc3) != 0xc2) 275948778Syokota return FALSE; 276048778Syokota /* check d3-d0 in byte 2 */ 276148778Syokota if (!MOUSE_PS2PLUS_CHECKBITS(data)) 276248778Syokota return FALSE; 276348778Syokota /* check p3-p0 */ 276448778Syokota if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0) 276548778Syokota return FALSE; 276641016Sdfr 276748778Syokota sc->hw.hwid &= 0x00ff; 276848778Syokota sc->hw.hwid |= data[2] << 8; /* save model ID */ 276948778Syokota 277041016Sdfr /* 277141016Sdfr * MouseMan+ (or FirstMouse+) is now in its native mode, in which 277241016Sdfr * the wheel and the fourth button events are encoded in the 277341016Sdfr * special data packet. The mouse may be put in the IntelliMouse mode 277441016Sdfr * if it is initialized by the IntelliMouse's method. 277541016Sdfr */ 277641016Sdfr return TRUE; 277741016Sdfr} 277841016Sdfr 277958230Syokota/* MS IntelliMouse Explorer */ 278058230Syokotastatic int 278158230Syokotaenable_msexplorer(struct psm_softc *sc) 278258230Syokota{ 278358923Syokota static unsigned char rate0[] = { 200, 100, 80, }; 278458923Syokota static unsigned char rate1[] = { 200, 200, 80, }; 278558230Syokota KBDC kbdc = sc->kbdc; 278658230Syokota int id; 278758230Syokota int i; 278858230Syokota 278969438Syokota /* the special sequence to enable the extra buttons and the roller. */ 279069438Syokota for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) { 279169438Syokota if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i]) 279269438Syokota return FALSE; 279369438Syokota } 279469438Syokota /* the device will give the genuine ID only after the above sequence */ 279569438Syokota id = get_aux_id(kbdc); 279669438Syokota if (id != PSM_EXPLORER_ID) 279769438Syokota return FALSE; 279869438Syokota 279969438Syokota sc->hw.hwid = id; 280069438Syokota sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */ 280169438Syokota 280258923Syokota /* 280358923Syokota * XXX: this is a kludge to fool some KVM switch products 280458923Syokota * which think they are clever enough to know the 4-byte IntelliMouse 280558923Syokota * protocol, and assume any other protocols use 3-byte packets. 280658923Syokota * They don't convey 4-byte data packets from the IntelliMouse Explorer 280758923Syokota * correctly to the host computer because of this! 280858923Syokota * The following sequence is actually IntelliMouse's "wake up" 280958923Syokota * sequence; it will make the KVM think the mouse is IntelliMouse 281058923Syokota * when it is in fact IntelliMouse Explorer. 281158923Syokota */ 281258923Syokota for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) { 281358923Syokota if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i]) 281469438Syokota break; 281558923Syokota } 281658923Syokota id = get_aux_id(kbdc); 281758923Syokota 281858230Syokota return TRUE; 281958230Syokota} 282058230Syokota 282141016Sdfr/* MS IntelliMouse */ 282241016Sdfrstatic int 282341016Sdfrenable_msintelli(struct psm_softc *sc) 282441016Sdfr{ 282541016Sdfr /* 282641016Sdfr * Logitech MouseMan+ and FirstMouse+ will also respond to this 282741016Sdfr * probe routine and act like IntelliMouse. 282841016Sdfr */ 282941016Sdfr 283041016Sdfr static unsigned char rate[] = { 200, 100, 80, }; 283141016Sdfr KBDC kbdc = sc->kbdc; 283241016Sdfr int id; 283341016Sdfr int i; 283441016Sdfr 283541016Sdfr /* the special sequence to enable the third button and the roller. */ 283641016Sdfr for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 283741016Sdfr if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 283841016Sdfr return FALSE; 283941016Sdfr } 284041016Sdfr /* the device will give the genuine ID only after the above sequence */ 284141016Sdfr id = get_aux_id(kbdc); 284241016Sdfr if (id != PSM_INTELLI_ID) 284341016Sdfr return FALSE; 284441016Sdfr 284541016Sdfr sc->hw.hwid = id; 284641016Sdfr sc->hw.buttons = 3; 284741016Sdfr 284841016Sdfr return TRUE; 284941016Sdfr} 285041016Sdfr 285158230Syokota/* A4 Tech 4D Mouse */ 285258230Syokotastatic int 285358230Syokotaenable_4dmouse(struct psm_softc *sc) 285458230Syokota{ 285558230Syokota /* 285658230Syokota * Newer wheel mice from A4 Tech may use the 4D+ protocol. 285758230Syokota */ 285858230Syokota 285958230Syokota static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 286058230Syokota KBDC kbdc = sc->kbdc; 286158230Syokota int id; 286258230Syokota int i; 286358230Syokota 286458230Syokota for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 286558230Syokota if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 286658230Syokota return FALSE; 286758230Syokota } 286858230Syokota id = get_aux_id(kbdc); 286958230Syokota /* 287058923Syokota * WinEasy 4D, 4 Way Scroll 4D: 6 287158230Syokota * Cable-Free 4D: 8 (4DPLUS) 287258923Syokota * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS) 287358230Syokota */ 287458230Syokota if (id != PSM_4DMOUSE_ID) 287558230Syokota return FALSE; 287658230Syokota 287758230Syokota sc->hw.hwid = id; 287858230Syokota sc->hw.buttons = 3; /* XXX some 4D mice have 4? */ 287958230Syokota 288058230Syokota return TRUE; 288158230Syokota} 288258230Syokota 288358230Syokota/* A4 Tech 4D+ Mouse */ 288458230Syokotastatic int 288558230Syokotaenable_4dplus(struct psm_softc *sc) 288658230Syokota{ 288758230Syokota /* 288858230Syokota * Newer wheel mice from A4 Tech seem to use this protocol. 288958230Syokota * Older models are recognized as either 4D Mouse or IntelliMouse. 289058230Syokota */ 289158230Syokota KBDC kbdc = sc->kbdc; 289258230Syokota int id; 289358230Syokota 289458230Syokota /* 289558230Syokota * enable_4dmouse() already issued the following ID sequence... 289658230Syokota static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 289758230Syokota int i; 289858230Syokota 289958230Syokota for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 290058230Syokota if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 290158230Syokota return FALSE; 290258230Syokota } 290358230Syokota */ 290458230Syokota 290558230Syokota id = get_aux_id(kbdc); 2906117478Smikeh switch (id) { 2907117478Smikeh case PSM_4DPLUS_ID: 2908117478Smikeh sc->hw.buttons = 4; 2909117478Smikeh break; 2910117478Smikeh case PSM_4DPLUS_RFSW35_ID: 2911117478Smikeh sc->hw.buttons = 3; 2912117478Smikeh break; 2913117478Smikeh default: 2914117478Smikeh return FALSE; 2915117478Smikeh } 291658230Syokota 291758230Syokota sc->hw.hwid = id; 291858230Syokota 291958230Syokota return TRUE; 292058230Syokota} 292158230Syokota 292249965Syokota/* Interlink electronics VersaPad */ 292349965Syokotastatic int 292449965Syokotaenable_versapad(struct psm_softc *sc) 292549965Syokota{ 292649965Syokota KBDC kbdc = sc->kbdc; 292749965Syokota int data[3]; 292849965Syokota 292949965Syokota set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */ 293049965Syokota set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */ 293149965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 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 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */ 293649965Syokota return FALSE; 293749965Syokota if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */ 293849965Syokota return FALSE; 293949965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 294049965Syokota 294158230Syokota sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 294258230Syokota 294349965Syokota return TRUE; /* PS/2 absolute mode */ 294449965Syokota} 294549965Syokota 294641016Sdfrstatic int 294754629Syokotapsmresume(device_t dev) 294841016Sdfr{ 294954629Syokota struct psm_softc *sc = device_get_softc(dev); 295054629Syokota int unit = device_get_unit(dev); 295184880Syokota int err; 295241016Sdfr 295341016Sdfr if (verbose >= 2) 295454629Syokota log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit); 295541016Sdfr 295658230Syokota if (!(sc->config & PSM_CONFIG_HOOKRESUME)) 295758230Syokota return (0); 295858230Syokota 295984880Syokota err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND); 296041016Sdfr 296141016Sdfr if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) { 296241016Sdfr /* 296341016Sdfr * Release the blocked process; it must be notified that the device 296441016Sdfr * cannot be accessed anymore. 296541016Sdfr */ 296641016Sdfr sc->state &= ~PSM_ASLP; 2967111748Sdes wakeup(sc); 296841016Sdfr } 296941016Sdfr 297041016Sdfr if (verbose >= 2) 297154629Syokota log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit); 297241016Sdfr 297341016Sdfr return (err); 297441016Sdfr} 297541016Sdfr 297652997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0); 297783147Syokota 297883147Syokota/* 297983147Syokota * This sucks up assignments from PNPBIOS and ACPI. 298083147Syokota */ 298183147Syokota 298283931Syokota/* 298383931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may 298483931Syokota * appear BEFORE the AT keyboard controller. As the PS/2 mouse device 298583931Syokota * can be probed and attached only after the AT keyboard controller is 298683931Syokota * attached, we shall quietly reserve the IRQ resource for later use. 298783931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller, 298883931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging 298983931Syokota * under the keyboard controller, then probe and attach it. 299083931Syokota */ 299183147Syokota 299283147Syokotastatic devclass_t psmcpnp_devclass; 299383147Syokota 299483147Syokotastatic device_probe_t psmcpnp_probe; 299583147Syokotastatic device_attach_t psmcpnp_attach; 299683147Syokota 299783147Syokotastatic device_method_t psmcpnp_methods[] = { 299883147Syokota DEVMETHOD(device_probe, psmcpnp_probe), 299983147Syokota DEVMETHOD(device_attach, psmcpnp_attach), 300083147Syokota 300183147Syokota { 0, 0 } 300283147Syokota}; 300383147Syokota 300483147Syokotastatic driver_t psmcpnp_driver = { 300583147Syokota PSMCPNP_DRIVER_NAME, 300683147Syokota psmcpnp_methods, 300783147Syokota 1, /* no softc */ 300883147Syokota}; 300983147Syokota 301083147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = { 301188188Ssheldonh { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */ 301283147Syokota { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */ 301383147Syokota { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */ 3014117117Smikeh { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */ 301583492Syokota { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */ 301684407Stakawata { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */ 3017109679Shsu { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */ 3018109679Shsu { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */ 3019109710Smarcel { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */ 3020109679Shsu { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */ 302183147Syokota { 0 } 302283147Syokota}; 302383147Syokota 302483147Syokotastatic int 302583147Syokotacreate_a_copy(device_t atkbdc, device_t me) 302683147Syokota{ 302783147Syokota device_t psm; 302883147Syokota u_long irq; 302983147Syokota 303083931Syokota /* find the PS/2 mouse device instance under the keyboard controller */ 303183931Syokota psm = device_find_child(atkbdc, PSM_DRIVER_NAME, 303283931Syokota device_get_unit(atkbdc)); 303383147Syokota if (psm == NULL) 303483147Syokota return ENXIO; 303583931Syokota if (device_get_state(psm) != DS_NOTPRESENT) 303683931Syokota return 0; 303783147Syokota 303883931Syokota /* move our resource to the found device */ 303983931Syokota irq = bus_get_resource_start(me, SYS_RES_IRQ, 0); 304083147Syokota bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 304183147Syokota 304283147Syokota /* ...then probe and attach it */ 304383147Syokota return device_probe_and_attach(psm); 304483147Syokota} 304583147Syokota 304683147Syokotastatic int 304783147Syokotapsmcpnp_probe(device_t dev) 304883147Syokota{ 304983931Syokota struct resource *res; 305083931Syokota u_long irq; 305183931Syokota int rid; 305283147Syokota 305383147Syokota if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids)) 305483147Syokota return ENXIO; 305583147Syokota 305683931Syokota /* 305783931Syokota * The PnP BIOS and ACPI are supposed to assign an IRQ (12) 305883931Syokota * to the PS/2 mouse device node. But, some buggy PnP BIOS 305983931Syokota * declares the PS/2 mouse device node without an IRQ resource! 306083931Syokota * If this happens, we shall refer to device hints. 306183931Syokota * If we still don't find it there, use a hardcoded value... XXX 306283931Syokota */ 306383931Syokota rid = 0; 306483931Syokota irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid); 306583931Syokota if (irq <= 0) { 306683931Syokota if (resource_long_value(PSM_DRIVER_NAME, 306783931Syokota device_get_unit(dev), "irq", &irq) != 0) 306883931Syokota irq = 12; /* XXX */ 306983931Syokota device_printf(dev, "irq resource info is missing; " 307083931Syokota "assuming irq %ld\n", irq); 307183931Syokota bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1); 307283931Syokota } 3073127135Snjl res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 3074127135Snjl RF_SHAREABLE); 307583931Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, res); 307683147Syokota 307783147Syokota /* keep quiet */ 307883147Syokota if (!bootverbose) 307983147Syokota device_quiet(dev); 308083492Syokota 308183931Syokota return ((res == NULL) ? ENXIO : 0); 308283147Syokota} 308383147Syokota 308483147Syokotastatic int 308583147Syokotapsmcpnp_attach(device_t dev) 308683147Syokota{ 308783492Syokota device_t atkbdc; 308883931Syokota int rid; 308983147Syokota 309083931Syokota /* find the keyboard controller, which may be on acpi* or isa* bus */ 309183931Syokota atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME), 309283931Syokota device_get_unit(dev)); 309383931Syokota if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) { 309483492Syokota create_a_copy(atkbdc, dev); 309583931Syokota } else { 309683931Syokota /* 309783931Syokota * If we don't have the AT keyboard controller yet, 309883931Syokota * just reserve the IRQ for later use... 309983931Syokota * (See psmidentify() above.) 310083931Syokota */ 310183931Syokota rid = 0; 3102127135Snjl bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE); 310383931Syokota } 310483492Syokota 310583147Syokota return 0; 310683147Syokota} 310783147Syokota 3108123442Salfred/* 3109123442Salfred * Return true if 'now' is earlier than (start + (secs.usecs)). 3110123442Salfred * Now may be NULL and the function will fetch the current time from 3111123442Salfred * getmicrouptime(), or a cached 'now' can be passed in. 3112123442Salfred * All values should be numbers derived from getmicrouptime(). 3113123442Salfred */ 3114123442Salfredstatic int 3115123442Salfredtimeelapsed(start, secs, usecs, now) 3116123442Salfred const struct timeval *start, *now; 3117123442Salfred int secs, usecs; 3118123442Salfred{ 3119123442Salfred struct timeval snow, tv; 3120123442Salfred 3121123442Salfred /* if there is no 'now' passed in, the get it as a convience. */ 3122123442Salfred if (now == NULL) { 3123123442Salfred getmicrouptime(&snow); 3124123442Salfred now = &snow; 3125123442Salfred } 3126123442Salfred 3127123442Salfred tv.tv_sec = secs; 3128123442Salfred tv.tv_usec = usecs; 3129123442Salfred timevaladd(&tv, start); 3130123442Salfred return (timevalcmp(&tv, now, <)); 3131123442Salfred} 3132123442Salfred 313383147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0); 313483147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0); 3135