psm.c revision 133882
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 133882 2004-08-16 20:19:09Z philip $"); 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 106133297Sphilip#ifndef PSM_TAP_TIMEOUT 107133297Sphilip#define PSM_TAP_TIMEOUT 125000 108133297Sphilip#endif 109133297Sphilip 110133297Sphilip#ifndef PSM_TAP_THRESHOLD 111133297Sphilip#define PSM_TAP_THRESHOLD 25 112133297Sphilip#endif 113133297Sphilip 11441016Sdfr/* end of driver specific options */ 11541016Sdfr 11683492Syokota#define PSM_DRIVER_NAME "psm" 11783931Syokota#define PSMCPNP_DRIVER_NAME "psmcpnp" 11883492Syokota 11941016Sdfr/* input queue */ 12041016Sdfr#define PSM_BUFSIZE 960 12141016Sdfr#define PSM_SMALLBUFSIZE 240 12241016Sdfr 12341016Sdfr/* operation levels */ 12441016Sdfr#define PSM_LEVEL_BASE 0 12541016Sdfr#define PSM_LEVEL_STANDARD 1 12641016Sdfr#define PSM_LEVEL_NATIVE 2 12741016Sdfr#define PSM_LEVEL_MIN PSM_LEVEL_BASE 12841016Sdfr#define PSM_LEVEL_MAX PSM_LEVEL_NATIVE 12941016Sdfr 13048778Syokota/* Logitech PS2++ protocol */ 13148778Syokota#define MOUSE_PS2PLUS_CHECKBITS(b) \ 13248778Syokota ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f)) 13348778Syokota#define MOUSE_PS2PLUS_PACKET_TYPE(b) \ 13448778Syokota (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4)) 13548778Syokota 13641016Sdfr/* some macros */ 13741016Sdfr#define PSM_UNIT(dev) (minor(dev) >> 1) 13841016Sdfr#define PSM_NBLOCKIO(dev) (minor(dev) & 1) 13941016Sdfr#define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1)) 14041016Sdfr 14141016Sdfr/* ring buffer */ 14241016Sdfrtypedef struct ringbuf { 14341016Sdfr int count; /* # of valid elements in the buffer */ 14441016Sdfr int head; /* head pointer */ 14541016Sdfr int tail; /* tail poiner */ 14641016Sdfr unsigned char buf[PSM_BUFSIZE]; 14741016Sdfr} ringbuf_t; 14841016Sdfr 149123442Salfred/* data buffer */ 150123442Salfredtypedef struct packetbuf { 151123442Salfred unsigned char ipacket[16]; /* interim input buffer */ 152123442Salfred int inputbytes; /* # of bytes in the input buffer */ 153123442Salfred} packetbuf_t; 154123442Salfred 155123442Salfred#ifndef PSM_PACKETQUEUE 156123442Salfred#define PSM_PACKETQUEUE 128 157123442Salfred#endif 158123442Salfred 15941016Sdfr/* driver control block */ 16041016Sdfrstruct psm_softc { /* Driver status information */ 16184880Syokota int unit; 16241016Sdfr struct selinfo rsel; /* Process selecting for Input */ 16341016Sdfr unsigned char state; /* Mouse driver state */ 16441016Sdfr int config; /* driver configuration flags */ 16541016Sdfr int flags; /* other flags */ 16641016Sdfr KBDC kbdc; /* handle to access the keyboard controller */ 16758230Syokota struct resource *intr; /* IRQ resource */ 16858230Syokota void *ih; /* interrupt handle */ 16941016Sdfr mousehw_t hw; /* hardware information */ 170132865Snjl synapticshw_t synhw; /* Synaptics-specific hardware information */ 17141016Sdfr mousemode_t mode; /* operation mode */ 17241016Sdfr mousemode_t dflt_mode; /* default operation mode */ 17341016Sdfr mousestatus_t status; /* accumulated mouse movement */ 17441016Sdfr ringbuf_t queue; /* mouse status queue */ 175123442Salfred packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */ 176123442Salfred int pqueue_start; /* start of data in queue */ 177123442Salfred int pqueue_end; /* end of data in queue */ 17841016Sdfr int button; /* the latest button state */ 17949965Syokota int xold; /* previous absolute X position */ 18049965Syokota int yold; /* previous absolute Y position */ 181133297Sphilip int zmax; /* maximum pressure value for touchpads */ 182123442Salfred int syncerrors; /* XXX: KILL ME! */ 18384880Syokota struct timeval inputtimeout; 184123442Salfred struct timeval lastsoftintr; /* time of last soft interrupt */ 185123442Salfred struct timeval lastinputerr; /* time last sync error happened */ 186133297Sphilip struct timeval taptimeout; /* tap timeout for touchpads */ 18758230Syokota int watchdog; /* watchdog timer flag */ 18858230Syokota struct callout_handle callout; /* watchdog timer call out */ 189123442Salfred struct callout_handle softcallout; /* buffer timer call out */ 190130585Sphk struct cdev *dev; 191130585Sphk struct cdev *bdev; 192123442Salfred int lasterr; 193123442Salfred int cmdcount; 19441016Sdfr}; 195114293Smarkmstatic devclass_t psm_devclass; 19641016Sdfr#define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit)) 19741016Sdfr 19841016Sdfr/* driver state flags (state) */ 19941016Sdfr#define PSM_VALID 0x80 20041016Sdfr#define PSM_OPEN 1 /* Device is open */ 20141016Sdfr#define PSM_ASLP 2 /* Waiting for mouse data */ 202123442Salfred#define PSM_SOFTARMED 4 /* Software interrupt armed */ 20341016Sdfr 20441016Sdfr/* driver configuration flags (config) */ 20541016Sdfr#define PSM_CONFIG_RESOLUTION 0x000f /* resolution */ 20641016Sdfr#define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */ 20741016Sdfr#define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */ 20845789Speter#define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */ 20945789Speter#define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */ 21045789Speter#define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */ 21145789Speter#define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */ 21258230Syokota#define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */ 21358230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */ 21469439Syokota#define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */ 21541016Sdfr 21641016Sdfr#define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \ 21741016Sdfr | PSM_CONFIG_ACCEL \ 21845789Speter | PSM_CONFIG_NOCHECKSYNC \ 21969439Syokota | PSM_CONFIG_SYNCHACK \ 22045789Speter | PSM_CONFIG_NOIDPROBE \ 22145789Speter | PSM_CONFIG_NORESET \ 22245789Speter | PSM_CONFIG_FORCETAP \ 22358230Syokota | PSM_CONFIG_IGNPORTERROR \ 22458230Syokota | PSM_CONFIG_HOOKRESUME \ 22558230Syokota | PSM_CONFIG_INITAFTERSUSPEND) 22641016Sdfr 22741016Sdfr/* other flags (flags) */ 22849965Syokota#define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */ 22941016Sdfr 23041016Sdfr/* for backward compatibility */ 23141016Sdfr#define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t) 23241016Sdfr#define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t) 23341016Sdfr#define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t) 23441016Sdfr 23541016Sdfrtypedef struct old_mousehw { 23641016Sdfr int buttons; 23741016Sdfr int iftype; 23841016Sdfr int type; 23941016Sdfr int hwid; 24041016Sdfr} old_mousehw_t; 24141016Sdfr 24241016Sdfrtypedef struct old_mousemode { 24341016Sdfr int protocol; 24441016Sdfr int rate; 24541016Sdfr int resolution; 24641016Sdfr int accelfactor; 24741016Sdfr} old_mousemode_t; 24841016Sdfr 24941016Sdfr/* packet formatting function */ 25092756Salfredtypedef int packetfunc_t(struct psm_softc *, unsigned char *, 25192756Salfred int *, int, mousestatus_t *); 25241016Sdfr 25341016Sdfr/* function prototypes */ 25492756Salfredstatic void psmidentify(driver_t *, device_t); 25592756Salfredstatic int psmprobe(device_t); 25692756Salfredstatic int psmattach(device_t); 25792756Salfredstatic int psmdetach(device_t); 25892756Salfredstatic int psmresume(device_t); 25941016Sdfr 26041016Sdfrstatic d_open_t psmopen; 26141016Sdfrstatic d_close_t psmclose; 26241016Sdfrstatic d_read_t psmread; 26341016Sdfrstatic d_ioctl_t psmioctl; 26441016Sdfrstatic d_poll_t psmpoll; 26541016Sdfr 26692756Salfredstatic int enable_aux_dev(KBDC); 26792756Salfredstatic int disable_aux_dev(KBDC); 26892756Salfredstatic int get_mouse_status(KBDC, int *, int, int); 26992756Salfredstatic int get_aux_id(KBDC); 27092756Salfredstatic int set_mouse_sampling_rate(KBDC, int); 27192756Salfredstatic int set_mouse_scaling(KBDC, int); 27292756Salfredstatic int set_mouse_resolution(KBDC, int); 27392756Salfredstatic int set_mouse_mode(KBDC); 27492756Salfredstatic int get_mouse_buttons(KBDC); 27592756Salfredstatic int is_a_mouse(int); 27692756Salfredstatic void recover_from_error(KBDC); 27792756Salfredstatic int restore_controller(KBDC, int); 27892756Salfredstatic int doinitialize(struct psm_softc *, mousemode_t *); 27992756Salfredstatic int doopen(struct psm_softc *, int); 28092756Salfredstatic int reinitialize(struct psm_softc *, int); 28192756Salfredstatic char *model_name(int); 282123442Salfredstatic void psmsoftintr(void *); 28392756Salfredstatic void psmintr(void *); 28492756Salfredstatic void psmtimeout(void *); 285123442Salfredstatic int timeelapsed(const struct timeval *, 286123442Salfred int, int, const struct timeval *); 287123442Salfredstatic void dropqueue(struct psm_softc *); 288123442Salfredstatic void flushpackets(struct psm_softc *); 28941016Sdfr 29041016Sdfr/* vendor specific features */ 29192756Salfredtypedef int probefunc_t(struct psm_softc *); 29241016Sdfr 29392756Salfredstatic int mouse_id_proc1(KBDC, int, int, int *); 29492756Salfredstatic int mouse_ext_command(KBDC, int); 29541016Sdfrstatic probefunc_t enable_groller; 29641016Sdfrstatic probefunc_t enable_gmouse; 29741016Sdfrstatic probefunc_t enable_aglide; 29841016Sdfrstatic probefunc_t enable_kmouse; 29958230Syokotastatic probefunc_t enable_msexplorer; 30041016Sdfrstatic probefunc_t enable_msintelli; 30158230Syokotastatic probefunc_t enable_4dmouse; 30258230Syokotastatic probefunc_t enable_4dplus; 30341016Sdfrstatic probefunc_t enable_mmanplus; 304132865Snjlstatic probefunc_t enable_synaptics; 30549965Syokotastatic probefunc_t enable_versapad; 306123442Salfredstatic int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, unsigned char *); 30741016Sdfr 30841016Sdfrstatic struct { 30941016Sdfr int model; 31041016Sdfr unsigned char syncmask; 31141016Sdfr int packetsize; 31241016Sdfr probefunc_t *probefunc; 31341016Sdfr} vendortype[] = { 31458230Syokota /* 31558230Syokota * WARNING: the order of probe is very important. Don't mess it 31658230Syokota * unless you know what you are doing. 31758230Syokota */ 31841016Sdfr { MOUSE_MODEL_NET, /* Genius NetMouse */ 31958230Syokota 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, }, 32041016Sdfr { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */ 32141016Sdfr 0xc8, 6, enable_groller, }, 32258230Syokota { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */ 32358230Syokota 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, }, 32458230Syokota { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */ 32558230Syokota 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, }, 32658230Syokota { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */ 32758230Syokota 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, }, 32858230Syokota { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */ 32958230Syokota 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, }, 33058230Syokota { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */ 33158230Syokota 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, }, 33241016Sdfr { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */ 33341016Sdfr 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, }, 33441016Sdfr { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */ 33541016Sdfr 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, }, 33649965Syokota { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */ 33749965Syokota 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, }, 338132865Snjl { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */ 339132865Snjl 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics, }, 34041016Sdfr { MOUSE_MODEL_GENERIC, 34141016Sdfr 0xc0, MOUSE_PS2_PACKETSIZE, NULL, }, 34241016Sdfr}; 34363951Syokota#define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1) 34441016Sdfr 34541016Sdfr/* device driver declarateion */ 34641016Sdfrstatic device_method_t psm_methods[] = { 34741016Sdfr /* Device interface */ 34883147Syokota DEVMETHOD(device_identify, psmidentify), 34941016Sdfr DEVMETHOD(device_probe, psmprobe), 35041016Sdfr DEVMETHOD(device_attach, psmattach), 35158230Syokota DEVMETHOD(device_detach, psmdetach), 35254629Syokota DEVMETHOD(device_resume, psmresume), 35341016Sdfr 35441016Sdfr { 0, 0 } 35541016Sdfr}; 35641016Sdfr 35741016Sdfrstatic driver_t psm_driver = { 35883492Syokota PSM_DRIVER_NAME, 35941016Sdfr psm_methods, 36041016Sdfr sizeof(struct psm_softc), 36141016Sdfr}; 36241016Sdfr 36341016Sdfr 36447625Sphkstatic struct cdevsw psm_cdevsw = { 365126080Sphk .d_version = D_VERSION, 366126080Sphk .d_flags = D_NEEDGIANT, 367111815Sphk .d_open = psmopen, 368111815Sphk .d_close = psmclose, 369111815Sphk .d_read = psmread, 370111815Sphk .d_ioctl = psmioctl, 371111815Sphk .d_poll = psmpoll, 372111815Sphk .d_name = PSM_DRIVER_NAME, 37341016Sdfr}; 37441016Sdfr 37541016Sdfr/* debug message level */ 37641016Sdfrstatic int verbose = PSM_DEBUG; 37741016Sdfr 37841016Sdfr/* device I/O routines */ 37941016Sdfrstatic int 38041016Sdfrenable_aux_dev(KBDC kbdc) 38141016Sdfr{ 38241016Sdfr int res; 38341016Sdfr 38441016Sdfr res = send_aux_command(kbdc, PSMC_ENABLE_DEV); 38541016Sdfr if (verbose >= 2) 38641016Sdfr log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res); 38741016Sdfr 38841016Sdfr return (res == PSM_ACK); 38941016Sdfr} 39041016Sdfr 39141016Sdfrstatic int 39241016Sdfrdisable_aux_dev(KBDC kbdc) 39341016Sdfr{ 39441016Sdfr int res; 39541016Sdfr 39641016Sdfr res = send_aux_command(kbdc, PSMC_DISABLE_DEV); 39741016Sdfr if (verbose >= 2) 39841016Sdfr log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res); 39941016Sdfr 40041016Sdfr return (res == PSM_ACK); 40141016Sdfr} 40241016Sdfr 40341016Sdfrstatic int 40441016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len) 40541016Sdfr{ 40641016Sdfr int cmd; 40741016Sdfr int res; 40841016Sdfr int i; 40941016Sdfr 41041016Sdfr switch (flag) { 41141016Sdfr case 0: 41241016Sdfr default: 41341016Sdfr cmd = PSMC_SEND_DEV_STATUS; 41441016Sdfr break; 41541016Sdfr case 1: 41641016Sdfr cmd = PSMC_SEND_DEV_DATA; 41741016Sdfr break; 41841016Sdfr } 41941016Sdfr empty_aux_buffer(kbdc, 5); 42041016Sdfr res = send_aux_command(kbdc, cmd); 42141016Sdfr if (verbose >= 2) 42241016Sdfr log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n", 42341016Sdfr (flag == 1) ? "DATA" : "STATUS", res); 42441016Sdfr if (res != PSM_ACK) 42541016Sdfr return 0; 42641016Sdfr 42741016Sdfr for (i = 0; i < len; ++i) { 42841016Sdfr status[i] = read_aux_data(kbdc); 42941016Sdfr if (status[i] < 0) 43041016Sdfr break; 43141016Sdfr } 43241016Sdfr 43341016Sdfr if (verbose) { 43441016Sdfr log(LOG_DEBUG, "psm: %s %02x %02x %02x\n", 43541016Sdfr (flag == 1) ? "data" : "status", status[0], status[1], status[2]); 43641016Sdfr } 43741016Sdfr 43841016Sdfr return i; 43941016Sdfr} 44041016Sdfr 44141016Sdfrstatic int 44241016Sdfrget_aux_id(KBDC kbdc) 44341016Sdfr{ 44441016Sdfr int res; 44541016Sdfr int id; 44641016Sdfr 44741016Sdfr empty_aux_buffer(kbdc, 5); 44841016Sdfr res = send_aux_command(kbdc, PSMC_SEND_DEV_ID); 44941016Sdfr if (verbose >= 2) 45041016Sdfr log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res); 45141016Sdfr if (res != PSM_ACK) 45241016Sdfr return (-1); 45341016Sdfr 45441016Sdfr /* 10ms delay */ 45541016Sdfr DELAY(10000); 45641016Sdfr 45741016Sdfr id = read_aux_data(kbdc); 45841016Sdfr if (verbose >= 2) 45941016Sdfr log(LOG_DEBUG, "psm: device ID: %04x\n", id); 46041016Sdfr 46141016Sdfr return id; 46241016Sdfr} 46341016Sdfr 46441016Sdfrstatic int 46541016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate) 46641016Sdfr{ 46741016Sdfr int res; 46841016Sdfr 46941016Sdfr res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate); 47041016Sdfr if (verbose >= 2) 47141016Sdfr log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res); 47241016Sdfr 47341016Sdfr return ((res == PSM_ACK) ? rate : -1); 47441016Sdfr} 47541016Sdfr 47641016Sdfrstatic int 47741016Sdfrset_mouse_scaling(KBDC kbdc, int scale) 47841016Sdfr{ 47941016Sdfr int res; 48041016Sdfr 48141016Sdfr switch (scale) { 48241016Sdfr case 1: 48341016Sdfr default: 48441016Sdfr scale = PSMC_SET_SCALING11; 48541016Sdfr break; 48641016Sdfr case 2: 48741016Sdfr scale = PSMC_SET_SCALING21; 48841016Sdfr break; 48941016Sdfr } 49041016Sdfr res = send_aux_command(kbdc, scale); 49141016Sdfr if (verbose >= 2) 49241016Sdfr log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n", 49341016Sdfr (scale == PSMC_SET_SCALING21) ? "21" : "11", res); 49441016Sdfr 49541016Sdfr return (res == PSM_ACK); 49641016Sdfr} 49741016Sdfr 49841016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */ 49941016Sdfrstatic int 50041016Sdfrset_mouse_resolution(KBDC kbdc, int val) 50141016Sdfr{ 50241016Sdfr int res; 50341016Sdfr 50441016Sdfr res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val); 50541016Sdfr if (verbose >= 2) 50641016Sdfr log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res); 50741016Sdfr 50841016Sdfr return ((res == PSM_ACK) ? val : -1); 50941016Sdfr} 51041016Sdfr 51141016Sdfr/* 51241016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be 51341016Sdfr * re-enabled by calling `enable_aux_dev()' 51441016Sdfr */ 51541016Sdfrstatic int 51641016Sdfrset_mouse_mode(KBDC kbdc) 51741016Sdfr{ 51841016Sdfr int res; 51941016Sdfr 52041016Sdfr res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE); 52141016Sdfr if (verbose >= 2) 52241016Sdfr log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res); 52341016Sdfr 52441016Sdfr return (res == PSM_ACK); 52541016Sdfr} 52641016Sdfr 52741016Sdfrstatic int 52841016Sdfrget_mouse_buttons(KBDC kbdc) 52941016Sdfr{ 53041016Sdfr int c = 2; /* assume two buttons by default */ 53141016Sdfr int status[3]; 53241016Sdfr 53341016Sdfr /* 53441016Sdfr * NOTE: a special sequence to obtain Logitech Mouse specific 53541016Sdfr * information: set resolution to 25 ppi, set scaling to 1:1, set 53641016Sdfr * scaling to 1:1, set scaling to 1:1. Then the second byte of the 53741016Sdfr * mouse status bytes is the number of available buttons. 53841016Sdfr * Some manufactures also support this sequence. 53941016Sdfr */ 54041016Sdfr if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 54141016Sdfr return c; 54241016Sdfr if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) 54341016Sdfr && set_mouse_scaling(kbdc, 1) 54441016Sdfr && (get_mouse_status(kbdc, status, 0, 3) >= 3)) { 54541016Sdfr if (status[1] != 0) 54641016Sdfr return status[1]; 54741016Sdfr } 54841016Sdfr return c; 54941016Sdfr} 55041016Sdfr 55141016Sdfr/* misc subroutines */ 55241016Sdfr/* 55341016Sdfr * Someday, I will get the complete list of valid pointing devices and 55441016Sdfr * their IDs... XXX 55541016Sdfr */ 55641016Sdfrstatic int 55741016Sdfris_a_mouse(int id) 55841016Sdfr{ 55941016Sdfr#if 0 56041016Sdfr static int valid_ids[] = { 56141016Sdfr PSM_MOUSE_ID, /* mouse */ 56241016Sdfr PSM_BALLPOINT_ID, /* ballpoint device */ 56341016Sdfr PSM_INTELLI_ID, /* Intellimouse */ 56458230Syokota PSM_EXPLORER_ID, /* Intellimouse Explorer */ 56541016Sdfr -1 /* end of table */ 56641016Sdfr }; 56741016Sdfr int i; 56841016Sdfr 56941016Sdfr for (i = 0; valid_ids[i] >= 0; ++i) 57041016Sdfr if (valid_ids[i] == id) 57141016Sdfr return TRUE; 57241016Sdfr return FALSE; 57341016Sdfr#else 57441016Sdfr return TRUE; 57541016Sdfr#endif 57641016Sdfr} 57741016Sdfr 57841016Sdfrstatic char * 57941016Sdfrmodel_name(int model) 58041016Sdfr{ 58141016Sdfr static struct { 58241016Sdfr int model_code; 58341016Sdfr char *model_name; 58441016Sdfr } models[] = { 58558230Syokota { MOUSE_MODEL_NETSCROLL, "NetScroll" }, 58658230Syokota { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" }, 58741016Sdfr { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" }, 58841016Sdfr { MOUSE_MODEL_THINK, "ThinkingMouse" }, 58941016Sdfr { MOUSE_MODEL_INTELLI, "IntelliMouse" }, 59041016Sdfr { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" }, 59149965Syokota { MOUSE_MODEL_VERSAPAD, "VersaPad" }, 59258230Syokota { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" }, 59358230Syokota { MOUSE_MODEL_4D, "4D Mouse" }, 59458230Syokota { MOUSE_MODEL_4DPLUS, "4D+ Mouse" }, 595132865Snjl { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" }, 59641016Sdfr { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" }, 59741016Sdfr { MOUSE_MODEL_UNKNOWN, NULL }, 59841016Sdfr }; 59941016Sdfr int i; 60041016Sdfr 60141016Sdfr for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) { 60241016Sdfr if (models[i].model_code == model) 60341016Sdfr return models[i].model_name; 60441016Sdfr } 60541016Sdfr return "Unknown"; 60641016Sdfr} 60741016Sdfr 60841016Sdfrstatic void 60941016Sdfrrecover_from_error(KBDC kbdc) 61041016Sdfr{ 61141016Sdfr /* discard anything left in the output buffer */ 61241016Sdfr empty_both_buffers(kbdc, 10); 61341016Sdfr 61441016Sdfr#if 0 61541016Sdfr /* 61641016Sdfr * NOTE: KBDC_RESET_KBD may not restore the communication between the 61741016Sdfr * keyboard and the controller. 61841016Sdfr */ 61941016Sdfr reset_kbd(kbdc); 62041016Sdfr#else 62141016Sdfr /* 62241016Sdfr * NOTE: somehow diagnostic and keyboard port test commands bring the 62341016Sdfr * keyboard back. 62441016Sdfr */ 62541016Sdfr if (!test_controller(kbdc)) 62641016Sdfr log(LOG_ERR, "psm: keyboard controller failed.\n"); 62741016Sdfr /* if there isn't a keyboard in the system, the following error is OK */ 62841016Sdfr if (test_kbd_port(kbdc) != 0) { 62941016Sdfr if (verbose) 63041016Sdfr log(LOG_ERR, "psm: keyboard port failed.\n"); 63141016Sdfr } 63241016Sdfr#endif 63341016Sdfr} 63441016Sdfr 63541016Sdfrstatic int 63641016Sdfrrestore_controller(KBDC kbdc, int command_byte) 63741016Sdfr{ 63841016Sdfr empty_both_buffers(kbdc, 10); 63941016Sdfr 64041016Sdfr if (!set_controller_command_byte(kbdc, 0xff, command_byte)) { 64141016Sdfr log(LOG_ERR, "psm: failed to restore the keyboard controller " 64241016Sdfr "command byte.\n"); 64358230Syokota empty_both_buffers(kbdc, 10); 64441016Sdfr return FALSE; 64541016Sdfr } else { 64658230Syokota empty_both_buffers(kbdc, 10); 64741016Sdfr return TRUE; 64841016Sdfr } 64941016Sdfr} 65041016Sdfr 65141016Sdfr/* 65241016Sdfr * Re-initialize the aux port and device. The aux port must be enabled 65341016Sdfr * and its interrupt must be disabled before calling this routine. 65441016Sdfr * The aux device will be disabled before returning. 65541016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before 65641016Sdfr * calling this routine. 65741016Sdfr */ 65841016Sdfrstatic int 65984880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode) 66041016Sdfr{ 66141016Sdfr KBDC kbdc = sc->kbdc; 66241016Sdfr int stat[3]; 66341016Sdfr int i; 66441016Sdfr 66541016Sdfr switch((i = test_aux_port(kbdc))) { 666132270Smux case 1: /* ignore these errors */ 667132270Smux case 2: 668132270Smux case 3: 66941016Sdfr case PSM_ACK: 67041016Sdfr if (verbose) 67141016Sdfr log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n", 67284880Syokota sc->unit, i); 673102412Scharnier /* FALLTHROUGH */ 67441016Sdfr case 0: /* no error */ 67541016Sdfr break; 67641016Sdfr case -1: /* time out */ 67741016Sdfr default: /* error */ 67841016Sdfr recover_from_error(kbdc); 67945789Speter if (sc->config & PSM_CONFIG_IGNPORTERROR) 68045789Speter break; 68141016Sdfr log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n", 68284880Syokota sc->unit, i); 68341016Sdfr return FALSE; 68441016Sdfr } 68541016Sdfr 68645789Speter if (sc->config & PSM_CONFIG_NORESET) { 68745789Speter /* 68845789Speter * Don't try to reset the pointing device. It may possibly be 68945789Speter * left in the unknown state, though... 69045789Speter */ 69145789Speter } else { 69245789Speter /* 69345789Speter * NOTE: some controllers appears to hang the `keyboard' when 69445789Speter * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 69545789Speter */ 69645789Speter if (!reset_aux_dev(kbdc)) { 69745789Speter recover_from_error(kbdc); 69884880Syokota log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit); 69945789Speter return FALSE; 70045789Speter } 70141016Sdfr } 70241016Sdfr 70341016Sdfr /* 70441016Sdfr * both the aux port and the aux device is functioning, see 70541016Sdfr * if the device can be enabled. 70641016Sdfr */ 70741016Sdfr if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) { 70884880Syokota log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit); 70941016Sdfr return FALSE; 71041016Sdfr } 71141016Sdfr empty_both_buffers(kbdc, 10); /* remove stray data if any */ 71241016Sdfr 71345789Speter if (sc->config & PSM_CONFIG_NOIDPROBE) { 71445789Speter i = GENERIC_MOUSE_ENTRY; 71545789Speter } else { 71645789Speter /* FIXME: hardware ID, mouse buttons? */ 71741016Sdfr 71845789Speter /* other parameters */ 71945789Speter for (i = 0; vendortype[i].probefunc != NULL; ++i) { 72045789Speter if ((*vendortype[i].probefunc)(sc)) { 72145789Speter if (verbose >= 2) 72245789Speter log(LOG_ERR, "psm%d: found %s\n", 72384880Syokota sc->unit, model_name(vendortype[i].model)); 72445789Speter break; 72545789Speter } 72641016Sdfr } 72741016Sdfr } 72841016Sdfr 72941016Sdfr sc->hw.model = vendortype[i].model; 73041016Sdfr sc->mode.packetsize = vendortype[i].packetsize; 73141016Sdfr 73241016Sdfr /* set mouse parameters */ 73341016Sdfr if (mode != (mousemode_t *)NULL) { 73441016Sdfr if (mode->rate > 0) 73541016Sdfr mode->rate = set_mouse_sampling_rate(kbdc, mode->rate); 73641016Sdfr if (mode->resolution >= 0) 73741016Sdfr mode->resolution = set_mouse_resolution(kbdc, mode->resolution); 73841016Sdfr set_mouse_scaling(kbdc, 1); 73941016Sdfr set_mouse_mode(kbdc); 74041016Sdfr } 74141016Sdfr 74241016Sdfr /* request a data packet and extract sync. bits */ 74341016Sdfr if (get_mouse_status(kbdc, stat, 1, 3) < 3) { 74484880Syokota log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n", 74584880Syokota sc->unit); 74641016Sdfr sc->mode.syncmask[0] = 0; 74741016Sdfr } else { 74841016Sdfr sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 74941016Sdfr /* the NetScroll Mouse will send three more bytes... Ignore them */ 75041016Sdfr empty_aux_buffer(kbdc, 5); 75141016Sdfr } 75241016Sdfr 75341016Sdfr /* just check the status of the mouse */ 75441016Sdfr if (get_mouse_status(kbdc, stat, 0, 3) < 3) 75584880Syokota log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n", 75684880Syokota sc->unit); 75741016Sdfr 75841016Sdfr return TRUE; 75941016Sdfr} 76041016Sdfr 76141016Sdfrstatic int 76284880Syokotadoopen(struct psm_softc *sc, int command_byte) 76341016Sdfr{ 76441016Sdfr int stat[3]; 76541016Sdfr 76641016Sdfr /* enable the mouse device */ 76741016Sdfr if (!enable_aux_dev(sc->kbdc)) { 76841016Sdfr /* MOUSE ERROR: failed to enable the mouse because: 76941016Sdfr * 1) the mouse is faulty, 77041016Sdfr * 2) the mouse has been removed(!?) 77141016Sdfr * In the latter case, the keyboard may have hung, and need 77241016Sdfr * recovery procedure... 77341016Sdfr */ 77441016Sdfr recover_from_error(sc->kbdc); 77541016Sdfr#if 0 77641016Sdfr /* FIXME: we could reset the mouse here and try to enable 77741016Sdfr * it again. But it will take long time and it's not a good 77841016Sdfr * idea to disable the keyboard that long... 77941016Sdfr */ 78084880Syokota if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) { 78141016Sdfr recover_from_error(sc->kbdc); 78241016Sdfr#else 78341016Sdfr { 78441016Sdfr#endif 78541016Sdfr restore_controller(sc->kbdc, command_byte); 78641016Sdfr /* mark this device is no longer available */ 78741016Sdfr sc->state &= ~PSM_VALID; 78841016Sdfr log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n", 78984880Syokota sc->unit); 79041016Sdfr return (EIO); 79141016Sdfr } 79241016Sdfr } 79341016Sdfr 79441016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 79584880Syokota log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit); 79641016Sdfr 79741016Sdfr /* enable the aux port and interrupt */ 79841016Sdfr if (!set_controller_command_byte(sc->kbdc, 79941016Sdfr kbdc_get_device_mask(sc->kbdc), 80041016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 80141016Sdfr | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) { 80241016Sdfr /* CONTROLLER ERROR */ 80341016Sdfr disable_aux_dev(sc->kbdc); 80441016Sdfr restore_controller(sc->kbdc, command_byte); 80541016Sdfr log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n", 80684880Syokota sc->unit); 80741016Sdfr return (EIO); 80841016Sdfr } 80941016Sdfr 81058230Syokota /* start the watchdog timer */ 81158230Syokota sc->watchdog = FALSE; 81284880Syokota sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2); 81358230Syokota 81441016Sdfr return (0); 81541016Sdfr} 81641016Sdfr 81784880Syokotastatic int 81884880Syokotareinitialize(struct psm_softc *sc, int doinit) 81984880Syokota{ 82084880Syokota int err; 82184880Syokota int c; 82284880Syokota int s; 82384880Syokota 82484880Syokota /* don't let anybody mess with the aux device */ 82584880Syokota if (!kbdc_lock(sc->kbdc, TRUE)) 82684880Syokota return (EIO); 82784880Syokota s = spltty(); 82884880Syokota 82984880Syokota /* block our watchdog timer */ 83084880Syokota sc->watchdog = FALSE; 83184880Syokota untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout); 83284880Syokota callout_handle_init(&sc->callout); 83384880Syokota 83484880Syokota /* save the current controller command byte */ 83584880Syokota empty_both_buffers(sc->kbdc, 10); 83684880Syokota c = get_controller_command_byte(sc->kbdc); 83784880Syokota if (verbose >= 2) 83884880Syokota log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n", 83984880Syokota sc->unit, c); 84084880Syokota 84184880Syokota /* enable the aux port but disable the aux interrupt and the keyboard */ 84284880Syokota if ((c == -1) || !set_controller_command_byte(sc->kbdc, 84384880Syokota kbdc_get_device_mask(sc->kbdc), 84484880Syokota KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 84584880Syokota | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 84684880Syokota /* CONTROLLER ERROR */ 84784880Syokota splx(s); 84884880Syokota kbdc_lock(sc->kbdc, FALSE); 84984880Syokota log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n", 85084880Syokota sc->unit); 85184880Syokota return (EIO); 85284880Syokota } 85384880Syokota 85484880Syokota /* flush any data */ 85584880Syokota if (sc->state & PSM_VALID) { 85684880Syokota disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */ 85784880Syokota empty_aux_buffer(sc->kbdc, 10); 85884880Syokota } 859123442Salfred flushpackets(sc); 86084880Syokota sc->syncerrors = 0; 86184880Syokota 86284880Syokota /* try to detect the aux device; are you still there? */ 86384880Syokota err = 0; 86484880Syokota if (doinit) { 86584880Syokota if (doinitialize(sc, &sc->mode)) { 86684880Syokota /* yes */ 86784880Syokota sc->state |= PSM_VALID; 86884880Syokota } else { 86984880Syokota /* the device has gone! */ 87084880Syokota restore_controller(sc->kbdc, c); 87184880Syokota sc->state &= ~PSM_VALID; 87284880Syokota log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n", 87384880Syokota sc->unit); 87484880Syokota err = ENXIO; 87584880Syokota } 87684880Syokota } 87784880Syokota splx(s); 87884880Syokota 87984880Syokota /* restore the driver state */ 88084880Syokota if ((sc->state & PSM_OPEN) && (err == 0)) { 88184880Syokota /* enable the aux device and the port again */ 88284880Syokota err = doopen(sc, c); 88384880Syokota if (err != 0) 88484880Syokota log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n", 88584880Syokota sc->unit); 88684880Syokota } else { 88784880Syokota /* restore the keyboard port and disable the aux port */ 88884880Syokota if (!set_controller_command_byte(sc->kbdc, 88984880Syokota kbdc_get_device_mask(sc->kbdc), 89084880Syokota (c & KBD_KBD_CONTROL_BITS) 89184880Syokota | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 89284880Syokota /* CONTROLLER ERROR */ 89384880Syokota log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n", 89484880Syokota sc->unit); 89584880Syokota err = EIO; 89684880Syokota } 89784880Syokota } 89884880Syokota 89984880Syokota kbdc_lock(sc->kbdc, FALSE); 90084880Syokota return (err); 90184880Syokota} 90284880Syokota 90341016Sdfr/* psm driver entry points */ 90441016Sdfr 90583147Syokotastatic void 90683147Syokotapsmidentify(driver_t *driver, device_t parent) 90783147Syokota{ 90883931Syokota device_t psmc; 90983931Syokota device_t psm; 91083931Syokota u_long irq; 91183931Syokota int unit; 91283147Syokota 91383931Syokota unit = device_get_unit(parent); 91483931Syokota 91583147Syokota /* always add at least one child */ 91683931Syokota psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit); 91783931Syokota if (psm == NULL) 91883931Syokota return; 91983931Syokota 92083931Syokota irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX); 92183931Syokota if (irq > 0) 92283931Syokota return; 92383931Syokota 92483931Syokota /* 92583931Syokota * If the PS/2 mouse device has already been reported by ACPI or 92683931Syokota * PnP BIOS, obtain the IRQ resource from it. 92783931Syokota * (See psmcpnp_attach() below.) 92883931Syokota */ 92983931Syokota psmc = device_find_child(device_get_parent(parent), 93083931Syokota PSMCPNP_DRIVER_NAME, unit); 93183931Syokota if (psmc == NULL) 93283931Syokota return; 93383931Syokota irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0); 93483931Syokota if (irq <= 0) 93583931Syokota return; 93683931Syokota bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 93783147Syokota} 93883147Syokota 939115569Sphk#define endprobe(v) do { if (bootverbose) \ 94041016Sdfr --verbose; \ 94141016Sdfr kbdc_set_device_mask(sc->kbdc, mask); \ 94241016Sdfr kbdc_lock(sc->kbdc, FALSE); \ 94341016Sdfr return (v); \ 944115569Sphk } while (0) 94541016Sdfr 94641016Sdfrstatic int 94741016Sdfrpsmprobe(device_t dev) 94841016Sdfr{ 94941016Sdfr int unit = device_get_unit(dev); 95041016Sdfr struct psm_softc *sc = device_get_softc(dev); 95141016Sdfr int stat[3]; 95241016Sdfr int command_byte; 95341016Sdfr int mask; 95458230Syokota int rid; 95541016Sdfr int i; 95641016Sdfr 95741016Sdfr#if 0 95841016Sdfr kbdc_debug(TRUE); 95941016Sdfr#endif 96058230Syokota 96183147Syokota /* see if IRQ is available */ 96283147Syokota rid = KBDC_RID_AUX; 963127135Snjl sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 964127135Snjl RF_SHAREABLE | RF_ACTIVE); 96583147Syokota if (sc->intr == NULL) { 96683147Syokota if (bootverbose) 96783147Syokota device_printf(dev, "unable to allocate IRQ\n"); 96883147Syokota return (ENXIO); 96983147Syokota } 97083147Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 97158230Syokota 97284880Syokota sc->unit = unit; 97358230Syokota sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev))); 97483147Syokota sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS; 97558230Syokota /* XXX: for backward compatibility */ 97658230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM) 97758230Syokota sc->config |= 97858230Syokota#ifdef PSM_RESETAFTERSUSPEND 97958230Syokota PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 98058230Syokota#else 98158230Syokota PSM_CONFIG_HOOKRESUME; 98258230Syokota#endif 98358230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */ 98441016Sdfr sc->flags = 0; 98541016Sdfr if (bootverbose) 98641016Sdfr ++verbose; 98741016Sdfr 98843105Sdfr device_set_desc(dev, "PS/2 Mouse"); 98943105Sdfr 99041016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) { 99141016Sdfr printf("psm%d: unable to lock the controller.\n", unit); 99241016Sdfr if (bootverbose) 99341016Sdfr --verbose; 99441016Sdfr return (ENXIO); 99541016Sdfr } 99641016Sdfr 99741016Sdfr /* 99841016Sdfr * NOTE: two bits in the command byte controls the operation of the 99941016Sdfr * aux port (mouse port): the aux port disable bit (bit 5) and the aux 100041016Sdfr * port interrupt (IRQ 12) enable bit (bit 2). 100141016Sdfr */ 100241016Sdfr 100341016Sdfr /* discard anything left after the keyboard initialization */ 100441016Sdfr empty_both_buffers(sc->kbdc, 10); 100541016Sdfr 100641016Sdfr /* save the current command byte; it will be used later */ 100741016Sdfr mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS; 100841016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 100941016Sdfr if (verbose) 101041016Sdfr printf("psm%d: current command byte:%04x\n", unit, command_byte); 101141016Sdfr if (command_byte == -1) { 101241016Sdfr /* CONTROLLER ERROR */ 101341016Sdfr printf("psm%d: unable to get the current command byte value.\n", 101441016Sdfr unit); 101541016Sdfr endprobe(ENXIO); 101641016Sdfr } 101741016Sdfr 101841016Sdfr /* 101941016Sdfr * disable the keyboard port while probing the aux port, which must be 102041016Sdfr * enabled during this routine 102141016Sdfr */ 102241016Sdfr if (!set_controller_command_byte(sc->kbdc, 102341016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 102441016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 102541016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 102641016Sdfr /* 102741016Sdfr * this is CONTROLLER ERROR; I don't know how to recover 102841016Sdfr * from this error... 102941016Sdfr */ 103041016Sdfr restore_controller(sc->kbdc, command_byte); 103141016Sdfr printf("psm%d: unable to set the command byte.\n", unit); 103241016Sdfr endprobe(ENXIO); 103341016Sdfr } 103445789Speter write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT); 103541016Sdfr 103641016Sdfr /* 103741016Sdfr * NOTE: `test_aux_port()' is designed to return with zero if the aux 103841016Sdfr * port exists and is functioning. However, some controllers appears 103941016Sdfr * to respond with zero even when the aux port doesn't exist. (It may 104041016Sdfr * be that this is only the case when the controller DOES have the aux 104141016Sdfr * port but the port is not wired on the motherboard.) The keyboard 104241016Sdfr * controllers without the port, such as the original AT, are 104341016Sdfr * supporsed to return with an error code or simply time out. In any 104441016Sdfr * case, we have to continue probing the port even when the controller 104541016Sdfr * passes this test. 104641016Sdfr * 1047132270Smux * XXX: some controllers erroneously return the error code 1, 2 or 3 1048132270Smux * when it has the perfectly functional aux port. We have to ignore 1049132270Smux * this error code. Even if the controller HAS error with the aux 1050132270Smux * port, it will be detected later... 105141016Sdfr * XXX: another incompatible controller returns PSM_ACK (0xfa)... 105241016Sdfr */ 105341016Sdfr switch ((i = test_aux_port(sc->kbdc))) { 1054132270Smux case 1: /* ignore these errors */ 1055132270Smux case 2: 1056132270Smux case 3: 105741016Sdfr case PSM_ACK: 105841016Sdfr if (verbose) 105941016Sdfr printf("psm%d: strange result for test aux port (%d).\n", 106041016Sdfr unit, i); 1061102412Scharnier /* FALLTHROUGH */ 106241016Sdfr case 0: /* no error */ 106341016Sdfr break; 106441016Sdfr case -1: /* time out */ 106541016Sdfr default: /* error */ 106641016Sdfr recover_from_error(sc->kbdc); 106745789Speter if (sc->config & PSM_CONFIG_IGNPORTERROR) 106845789Speter break; 106941016Sdfr restore_controller(sc->kbdc, command_byte); 107041016Sdfr if (verbose) 107141016Sdfr printf("psm%d: the aux port is not functioning (%d).\n", 107241016Sdfr unit, i); 107341016Sdfr endprobe(ENXIO); 107441016Sdfr } 107541016Sdfr 107645789Speter if (sc->config & PSM_CONFIG_NORESET) { 107745789Speter /* 107845789Speter * Don't try to reset the pointing device. It may possibly be 107945789Speter * left in the unknown state, though... 108045789Speter */ 108145789Speter } else { 108245789Speter /* 108345789Speter * NOTE: some controllers appears to hang the `keyboard' when the aux 108445789Speter * port doesn't exist and `PSMC_RESET_DEV' is issued. 1085117302Smikeh * 1086117302Smikeh * Attempt to reset the controller twice -- this helps 1087117302Smikeh * pierce through some KVM switches. The second reset 1088117302Smikeh * is non-fatal. 108945789Speter */ 109045789Speter if (!reset_aux_dev(sc->kbdc)) { 109145789Speter recover_from_error(sc->kbdc); 109245789Speter restore_controller(sc->kbdc, command_byte); 109345789Speter if (verbose) 109445789Speter printf("psm%d: failed to reset the aux device.\n", unit); 109545789Speter endprobe(ENXIO); 1096117302Smikeh } else if (!reset_aux_dev(sc->kbdc)) { 1097117302Smikeh recover_from_error(sc->kbdc); 1098117302Smikeh if (verbose >= 2) 1099117302Smikeh printf("psm%d: failed to reset the aux device (2).\n", 1100117302Smikeh unit); 110145789Speter } 110241016Sdfr } 110345789Speter 110441016Sdfr /* 110541016Sdfr * both the aux port and the aux device is functioning, see if the 110641016Sdfr * device can be enabled. NOTE: when enabled, the device will start 110741016Sdfr * sending data; we shall immediately disable the device once we know 110841016Sdfr * the device can be enabled. 110941016Sdfr */ 111041016Sdfr if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) { 111145789Speter /* MOUSE ERROR */ 111245789Speter recover_from_error(sc->kbdc); 111345789Speter restore_controller(sc->kbdc, command_byte); 111445789Speter if (verbose) 111545789Speter printf("psm%d: failed to enable the aux device.\n", unit); 111641016Sdfr endprobe(ENXIO); 111741016Sdfr } 111841016Sdfr 111941016Sdfr /* save the default values after reset */ 112041016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) { 112141016Sdfr sc->dflt_mode.rate = sc->mode.rate = stat[2]; 112241016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 112341016Sdfr } else { 112441016Sdfr sc->dflt_mode.rate = sc->mode.rate = -1; 112541016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = -1; 112641016Sdfr } 112741016Sdfr 112841016Sdfr /* hardware information */ 112941016Sdfr sc->hw.iftype = MOUSE_IF_PS2; 113041016Sdfr 113141016Sdfr /* verify the device is a mouse */ 113241016Sdfr sc->hw.hwid = get_aux_id(sc->kbdc); 113341016Sdfr if (!is_a_mouse(sc->hw.hwid)) { 113441016Sdfr restore_controller(sc->kbdc, command_byte); 113541016Sdfr if (verbose) 113641016Sdfr printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid); 113741016Sdfr endprobe(ENXIO); 113841016Sdfr } 113941016Sdfr switch (sc->hw.hwid) { 114041016Sdfr case PSM_BALLPOINT_ID: 114141016Sdfr sc->hw.type = MOUSE_TRACKBALL; 114241016Sdfr break; 114341016Sdfr case PSM_MOUSE_ID: 114441016Sdfr case PSM_INTELLI_ID: 114558230Syokota case PSM_EXPLORER_ID: 114658230Syokota case PSM_4DMOUSE_ID: 114758230Syokota case PSM_4DPLUS_ID: 114841016Sdfr sc->hw.type = MOUSE_MOUSE; 114941016Sdfr break; 115041016Sdfr default: 115141016Sdfr sc->hw.type = MOUSE_UNKNOWN; 115241016Sdfr break; 115341016Sdfr } 115441016Sdfr 115545789Speter if (sc->config & PSM_CONFIG_NOIDPROBE) { 115645789Speter sc->hw.buttons = 2; 115745789Speter i = GENERIC_MOUSE_ENTRY; 115845789Speter } else { 115945789Speter /* # of buttons */ 116045789Speter sc->hw.buttons = get_mouse_buttons(sc->kbdc); 116141016Sdfr 116245789Speter /* other parameters */ 116345789Speter for (i = 0; vendortype[i].probefunc != NULL; ++i) { 116445789Speter if ((*vendortype[i].probefunc)(sc)) { 116545789Speter if (verbose >= 2) 116645789Speter printf("psm%d: found %s\n", 116745789Speter unit, model_name(vendortype[i].model)); 116845789Speter break; 116945789Speter } 117041016Sdfr } 117141016Sdfr } 117241016Sdfr 117341016Sdfr sc->hw.model = vendortype[i].model; 117441016Sdfr 117541016Sdfr sc->dflt_mode.level = PSM_LEVEL_BASE; 117641016Sdfr sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE; 117741016Sdfr sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4; 117841016Sdfr if (sc->config & PSM_CONFIG_NOCHECKSYNC) 117941016Sdfr sc->dflt_mode.syncmask[0] = 0; 118041016Sdfr else 118141016Sdfr sc->dflt_mode.syncmask[0] = vendortype[i].syncmask; 118245789Speter if (sc->config & PSM_CONFIG_FORCETAP) 118345789Speter sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP; 118441016Sdfr sc->dflt_mode.syncmask[1] = 0; /* syncbits */ 118541016Sdfr sc->mode = sc->dflt_mode; 118641016Sdfr sc->mode.packetsize = vendortype[i].packetsize; 118741016Sdfr 118841016Sdfr /* set mouse parameters */ 118948773Syokota#if 0 119048773Syokota /* 119148773Syokota * A version of Logitech FirstMouse+ won't report wheel movement, 119248773Syokota * if SET_DEFAULTS is sent... Don't use this command. 119348773Syokota * This fix was found by Takashi Nishida. 119448773Syokota */ 119541016Sdfr i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS); 119641016Sdfr if (verbose >= 2) 119741016Sdfr printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i); 119848773Syokota#endif 119941016Sdfr if (sc->config & PSM_CONFIG_RESOLUTION) { 120041016Sdfr sc->mode.resolution 120141016Sdfr = set_mouse_resolution(sc->kbdc, 120248773Syokota (sc->config & PSM_CONFIG_RESOLUTION) - 1); 120348773Syokota } else if (sc->mode.resolution >= 0) { 120448773Syokota sc->mode.resolution 120548773Syokota = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution); 120641016Sdfr } 120748773Syokota if (sc->mode.rate > 0) { 120848773Syokota sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate); 120948773Syokota } 121048773Syokota set_mouse_scaling(sc->kbdc, 1); 121141016Sdfr 121241016Sdfr /* request a data packet and extract sync. bits */ 121341016Sdfr if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) { 121441016Sdfr printf("psm%d: failed to get data.\n", unit); 121541016Sdfr sc->mode.syncmask[0] = 0; 121641016Sdfr } else { 121741016Sdfr sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 121841016Sdfr /* the NetScroll Mouse will send three more bytes... Ignore them */ 121941016Sdfr empty_aux_buffer(sc->kbdc, 5); 122041016Sdfr } 122141016Sdfr 122241016Sdfr /* just check the status of the mouse */ 122341016Sdfr /* 122441016Sdfr * NOTE: XXX there are some arcane controller/mouse combinations out 122541016Sdfr * there, which hung the controller unless there is data transmission 122641016Sdfr * after ACK from the mouse. 122741016Sdfr */ 122841016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) { 122941016Sdfr printf("psm%d: failed to get status.\n", unit); 123041016Sdfr } else { 123141016Sdfr /* 123241016Sdfr * When in its native mode, some mice operate with different 123341016Sdfr * default parameters than in the PS/2 compatible mode. 123441016Sdfr */ 123541016Sdfr sc->dflt_mode.rate = sc->mode.rate = stat[2]; 123641016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 123741016Sdfr } 123841016Sdfr 123941016Sdfr /* disable the aux port for now... */ 124041016Sdfr if (!set_controller_command_byte(sc->kbdc, 124141016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 124241016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 124341016Sdfr | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 124441016Sdfr /* 124541016Sdfr * this is CONTROLLER ERROR; I don't know the proper way to 124641016Sdfr * recover from this error... 124741016Sdfr */ 124841016Sdfr restore_controller(sc->kbdc, command_byte); 124941016Sdfr printf("psm%d: unable to set the command byte.\n", unit); 125041016Sdfr endprobe(ENXIO); 125141016Sdfr } 125241016Sdfr 125341016Sdfr /* done */ 125441016Sdfr kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS); 125541016Sdfr kbdc_lock(sc->kbdc, FALSE); 125641016Sdfr return (0); 125741016Sdfr} 125841016Sdfr 125941016Sdfrstatic int 126041016Sdfrpsmattach(device_t dev) 126141016Sdfr{ 126241016Sdfr int unit = device_get_unit(dev); 126341016Sdfr struct psm_softc *sc = device_get_softc(dev); 126458230Syokota int error; 126558230Syokota int rid; 126641016Sdfr 126741016Sdfr /* Setup initial state */ 126841016Sdfr sc->state = PSM_VALID; 126958230Syokota callout_handle_init(&sc->callout); 127041016Sdfr 127158230Syokota /* Setup our interrupt handler */ 127283147Syokota rid = KBDC_RID_AUX; 1273127135Snjl sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1274127135Snjl RF_SHAREABLE | RF_ACTIVE); 127558230Syokota if (sc->intr == NULL) 127658230Syokota return (ENXIO); 127783147Syokota error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih); 127858230Syokota if (error) { 127958230Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 128058230Syokota return (error); 128158230Syokota } 128258230Syokota 128341016Sdfr /* Done */ 128458230Syokota sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, 128558230Syokota "psm%d", unit); 128658230Syokota sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, 128758230Syokota "bpsm%d", unit); 128841016Sdfr 128941016Sdfr if (!verbose) { 129041016Sdfr printf("psm%d: model %s, device ID %d\n", 129148778Syokota unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff); 129241016Sdfr } else { 129348778Syokota printf("psm%d: model %s, device ID %d-%02x, %d buttons\n", 129448778Syokota unit, model_name(sc->hw.model), 129548778Syokota sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons); 129641016Sdfr printf("psm%d: config:%08x, flags:%08x, packet size:%d\n", 129741016Sdfr unit, sc->config, sc->flags, sc->mode.packetsize); 129841016Sdfr printf("psm%d: syncmask:%02x, syncbits:%02x\n", 129941016Sdfr unit, sc->mode.syncmask[0], sc->mode.syncmask[1]); 130041016Sdfr } 130141016Sdfr 130241016Sdfr if (bootverbose) 130341016Sdfr --verbose; 130441016Sdfr 130541016Sdfr return (0); 130641016Sdfr} 130741016Sdfr 130841016Sdfrstatic int 130958230Syokotapsmdetach(device_t dev) 131058230Syokota{ 131158230Syokota struct psm_softc *sc; 131258230Syokota int rid; 131358230Syokota 131458230Syokota sc = device_get_softc(dev); 131558230Syokota if (sc->state & PSM_OPEN) 131658230Syokota return EBUSY; 131758230Syokota 131883147Syokota rid = KBDC_RID_AUX; 131983147Syokota bus_teardown_intr(dev, sc->intr, sc->ih); 132058230Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 132158230Syokota 132258230Syokota destroy_dev(sc->dev); 132358230Syokota destroy_dev(sc->bdev); 132458230Syokota 132558230Syokota return 0; 132658230Syokota} 132758230Syokota 132858230Syokotastatic int 1329130585Sphkpsmopen(struct cdev *dev, int flag, int fmt, struct thread *td) 133041016Sdfr{ 133141016Sdfr int unit = PSM_UNIT(dev); 133241016Sdfr struct psm_softc *sc; 133341016Sdfr int command_byte; 133441016Sdfr int err; 133541016Sdfr int s; 133641016Sdfr 133741016Sdfr /* Get device data */ 133841016Sdfr sc = PSM_SOFTC(unit); 133941016Sdfr if ((sc == NULL) || (sc->state & PSM_VALID) == 0) 134041016Sdfr /* the device is no longer valid/functioning */ 134141016Sdfr return (ENXIO); 134241016Sdfr 134341016Sdfr /* Disallow multiple opens */ 134441016Sdfr if (sc->state & PSM_OPEN) 134541016Sdfr return (EBUSY); 134641016Sdfr 134741016Sdfr device_busy(devclass_get_device(psm_devclass, unit)); 134841016Sdfr 134941016Sdfr /* Initialize state */ 135041016Sdfr sc->mode.level = sc->dflt_mode.level; 135141016Sdfr sc->mode.protocol = sc->dflt_mode.protocol; 135258230Syokota sc->watchdog = FALSE; 135341016Sdfr 135441016Sdfr /* flush the event queue */ 135541016Sdfr sc->queue.count = 0; 135641016Sdfr sc->queue.head = 0; 135741016Sdfr sc->queue.tail = 0; 135841016Sdfr sc->status.flags = 0; 135941016Sdfr sc->status.button = 0; 136041016Sdfr sc->status.obutton = 0; 136141016Sdfr sc->status.dx = 0; 136241016Sdfr sc->status.dy = 0; 136341016Sdfr sc->status.dz = 0; 136441016Sdfr sc->button = 0; 1365123442Salfred sc->pqueue_start = 0; 1366123442Salfred sc->pqueue_end = 0; 136741016Sdfr 136841016Sdfr /* empty input buffer */ 1369123442Salfred flushpackets(sc); 137084880Syokota sc->syncerrors = 0; 137141016Sdfr 137241016Sdfr /* don't let timeout routines in the keyboard driver to poll the kbdc */ 137341016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 137441016Sdfr return (EIO); 137541016Sdfr 137641016Sdfr /* save the current controller command byte */ 137741016Sdfr s = spltty(); 137841016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 137941016Sdfr 138041016Sdfr /* enable the aux port and temporalily disable the keyboard */ 138141016Sdfr if ((command_byte == -1) 138241016Sdfr || !set_controller_command_byte(sc->kbdc, 138341016Sdfr kbdc_get_device_mask(sc->kbdc), 138441016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 138541016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 138641016Sdfr /* CONTROLLER ERROR; do you know how to get out of this? */ 138741016Sdfr kbdc_lock(sc->kbdc, FALSE); 138841016Sdfr splx(s); 138941016Sdfr log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n", 139041016Sdfr unit); 139141016Sdfr return (EIO); 139241016Sdfr } 139341016Sdfr /* 139441016Sdfr * Now that the keyboard controller is told not to generate 139541016Sdfr * the keyboard and mouse interrupts, call `splx()' to allow 139641016Sdfr * the other tty interrupts. The clock interrupt may also occur, 139741016Sdfr * but timeout routines will be blocked by the poll flag set 139841016Sdfr * via `kbdc_lock()' 139941016Sdfr */ 140041016Sdfr splx(s); 140141016Sdfr 140241016Sdfr /* enable the mouse device */ 140384880Syokota err = doopen(sc, command_byte); 140441016Sdfr 140541016Sdfr /* done */ 140641016Sdfr if (err == 0) 140741016Sdfr sc->state |= PSM_OPEN; 140841016Sdfr kbdc_lock(sc->kbdc, FALSE); 140941016Sdfr return (err); 141041016Sdfr} 141141016Sdfr 141241016Sdfrstatic int 1413130585Sphkpsmclose(struct cdev *dev, int flag, int fmt, struct thread *td) 141441016Sdfr{ 141541016Sdfr int unit = PSM_UNIT(dev); 141641016Sdfr struct psm_softc *sc = PSM_SOFTC(unit); 141741016Sdfr int stat[3]; 141841016Sdfr int command_byte; 141941016Sdfr int s; 142041016Sdfr 142141016Sdfr /* don't let timeout routines in the keyboard driver to poll the kbdc */ 142241016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 142341016Sdfr return (EIO); 142441016Sdfr 142541016Sdfr /* save the current controller command byte */ 142641016Sdfr s = spltty(); 142741016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 142841016Sdfr if (command_byte == -1) { 142941016Sdfr kbdc_lock(sc->kbdc, FALSE); 143041016Sdfr splx(s); 143141016Sdfr return (EIO); 143241016Sdfr } 143341016Sdfr 143441016Sdfr /* disable the aux interrupt and temporalily disable the keyboard */ 143541016Sdfr if (!set_controller_command_byte(sc->kbdc, 143641016Sdfr kbdc_get_device_mask(sc->kbdc), 143741016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 143841016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 143941016Sdfr log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n", 144058230Syokota unit); 144141016Sdfr /* CONTROLLER ERROR; 144241016Sdfr * NOTE: we shall force our way through. Because the only 144341016Sdfr * ill effect we shall see is that we may not be able 144441016Sdfr * to read ACK from the mouse, and it doesn't matter much 144541016Sdfr * so long as the mouse will accept the DISABLE command. 144641016Sdfr */ 144741016Sdfr } 144841016Sdfr splx(s); 144941016Sdfr 145058230Syokota /* stop the watchdog timer */ 145184880Syokota untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout); 145258230Syokota callout_handle_init(&sc->callout); 145358230Syokota 145441016Sdfr /* remove anything left in the output buffer */ 145541016Sdfr empty_aux_buffer(sc->kbdc, 10); 145641016Sdfr 145741016Sdfr /* disable the aux device, port and interrupt */ 145841016Sdfr if (sc->state & PSM_VALID) { 145941016Sdfr if (!disable_aux_dev(sc->kbdc)) { 146041016Sdfr /* MOUSE ERROR; 146141016Sdfr * NOTE: we don't return error and continue, pretending 146241016Sdfr * we have successfully disabled the device. It's OK because 146341016Sdfr * the interrupt routine will discard any data from the mouse 146441016Sdfr * hereafter. 146541016Sdfr */ 146641016Sdfr log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n", 146758230Syokota unit); 146841016Sdfr } 146941016Sdfr 147041016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 147141016Sdfr log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n", 147258230Syokota unit); 147341016Sdfr } 147441016Sdfr 147541016Sdfr if (!set_controller_command_byte(sc->kbdc, 147641016Sdfr kbdc_get_device_mask(sc->kbdc), 147741016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 147841016Sdfr | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 147941016Sdfr /* CONTROLLER ERROR; 148041016Sdfr * we shall ignore this error; see the above comment. 148141016Sdfr */ 148241016Sdfr log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n", 148358230Syokota unit); 148441016Sdfr } 148541016Sdfr 148641016Sdfr /* remove anything left in the output buffer */ 148741016Sdfr empty_aux_buffer(sc->kbdc, 10); 148841016Sdfr 148941016Sdfr /* close is almost always successful */ 149041016Sdfr sc->state &= ~PSM_OPEN; 149141016Sdfr kbdc_lock(sc->kbdc, FALSE); 149241016Sdfr device_unbusy(devclass_get_device(psm_devclass, unit)); 149341016Sdfr return (0); 149441016Sdfr} 149541016Sdfr 149641016Sdfrstatic int 1497123442Salfredtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, unsigned char *buf) 149841016Sdfr{ 149941016Sdfr static unsigned char butmapps2[8] = { 150041016Sdfr 0, 150141016Sdfr MOUSE_PS2_BUTTON1DOWN, 150241016Sdfr MOUSE_PS2_BUTTON2DOWN, 150341016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN, 150441016Sdfr MOUSE_PS2_BUTTON3DOWN, 150541016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN, 150641016Sdfr MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 150741016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 150841016Sdfr }; 150941016Sdfr static unsigned char butmapmsc[8] = { 151041016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 151141016Sdfr MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 151241016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 151341016Sdfr MOUSE_MSC_BUTTON3UP, 151441016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 151541016Sdfr MOUSE_MSC_BUTTON2UP, 151641016Sdfr MOUSE_MSC_BUTTON1UP, 151741016Sdfr 0, 151841016Sdfr }; 151941016Sdfr int mapped; 152041016Sdfr int i; 152141016Sdfr 152241016Sdfr if (sc->mode.level == PSM_LEVEL_BASE) { 152341016Sdfr mapped = status->button & ~MOUSE_BUTTON4DOWN; 152441016Sdfr if (status->button & MOUSE_BUTTON4DOWN) 152541016Sdfr mapped |= MOUSE_BUTTON1DOWN; 152641016Sdfr status->button = mapped; 152741016Sdfr buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS]; 1528109269Smdodd i = imax(imin(status->dx, 255), -256); 152941016Sdfr if (i < 0) 153041016Sdfr buf[0] |= MOUSE_PS2_XNEG; 153141016Sdfr buf[1] = i; 1532109269Smdodd i = imax(imin(status->dy, 255), -256); 153341016Sdfr if (i < 0) 153441016Sdfr buf[0] |= MOUSE_PS2_YNEG; 153541016Sdfr buf[2] = i; 153641016Sdfr return MOUSE_PS2_PACKETSIZE; 153741016Sdfr } else if (sc->mode.level == PSM_LEVEL_STANDARD) { 153841016Sdfr buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS]; 1539109269Smdodd i = imax(imin(status->dx, 255), -256); 154041016Sdfr buf[1] = i >> 1; 154141016Sdfr buf[3] = i - buf[1]; 1542109269Smdodd i = imax(imin(status->dy, 255), -256); 154341016Sdfr buf[2] = i >> 1; 154441016Sdfr buf[4] = i - buf[2]; 1545109269Smdodd i = imax(imin(status->dz, 127), -128); 154641016Sdfr buf[5] = (i >> 1) & 0x7f; 154741016Sdfr buf[6] = (i - (i >> 1)) & 0x7f; 154841016Sdfr buf[7] = (~status->button >> 3) & 0x7f; 154941016Sdfr return MOUSE_SYS_PACKETSIZE; 155041016Sdfr } 1551123442Salfred return pb->inputbytes; 155241016Sdfr} 155341016Sdfr 155441016Sdfrstatic int 1555130585Sphkpsmread(struct cdev *dev, struct uio *uio, int flag) 155641016Sdfr{ 155741016Sdfr register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 155841016Sdfr unsigned char buf[PSM_SMALLBUFSIZE]; 155941016Sdfr int error = 0; 156041016Sdfr int s; 156141016Sdfr int l; 156241016Sdfr 156341016Sdfr if ((sc->state & PSM_VALID) == 0) 156441016Sdfr return EIO; 156541016Sdfr 156641016Sdfr /* block until mouse activity occured */ 156741016Sdfr s = spltty(); 156841016Sdfr while (sc->queue.count <= 0) { 156941016Sdfr if (PSM_NBLOCKIO(dev)) { 157041016Sdfr splx(s); 157141016Sdfr return EWOULDBLOCK; 157241016Sdfr } 157341016Sdfr sc->state |= PSM_ASLP; 1574111748Sdes error = tsleep( sc, PZERO | PCATCH, "psmrea", 0); 157541016Sdfr sc->state &= ~PSM_ASLP; 157641016Sdfr if (error) { 157741016Sdfr splx(s); 157841016Sdfr return error; 157941016Sdfr } else if ((sc->state & PSM_VALID) == 0) { 158041016Sdfr /* the device disappeared! */ 158141016Sdfr splx(s); 158241016Sdfr return EIO; 158341016Sdfr } 158441016Sdfr } 158541016Sdfr splx(s); 158641016Sdfr 158741016Sdfr /* copy data to the user land */ 158841016Sdfr while ((sc->queue.count > 0) && (uio->uio_resid > 0)) { 158941016Sdfr s = spltty(); 1590109269Smdodd l = imin(sc->queue.count, uio->uio_resid); 159141016Sdfr if (l > sizeof(buf)) 159241016Sdfr l = sizeof(buf); 159341016Sdfr if (l > sizeof(sc->queue.buf) - sc->queue.head) { 159441016Sdfr bcopy(&sc->queue.buf[sc->queue.head], &buf[0], 159541016Sdfr sizeof(sc->queue.buf) - sc->queue.head); 159641016Sdfr bcopy(&sc->queue.buf[0], 159741016Sdfr &buf[sizeof(sc->queue.buf) - sc->queue.head], 159841016Sdfr l - (sizeof(sc->queue.buf) - sc->queue.head)); 159941016Sdfr } else { 160041016Sdfr bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l); 160141016Sdfr } 160241016Sdfr sc->queue.count -= l; 160341016Sdfr sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf); 160441016Sdfr splx(s); 160541016Sdfr error = uiomove(buf, l, uio); 160641016Sdfr if (error) 160741016Sdfr break; 160841016Sdfr } 160941016Sdfr 161041016Sdfr return error; 161141016Sdfr} 161241016Sdfr 161341016Sdfrstatic int 161441016Sdfrblock_mouse_data(struct psm_softc *sc, int *c) 161541016Sdfr{ 161641016Sdfr int s; 161741016Sdfr 161841016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 161941016Sdfr return EIO; 162041016Sdfr 162141016Sdfr s = spltty(); 162241016Sdfr *c = get_controller_command_byte(sc->kbdc); 162341016Sdfr if ((*c == -1) 162441016Sdfr || !set_controller_command_byte(sc->kbdc, 162541016Sdfr kbdc_get_device_mask(sc->kbdc), 162641016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 162741016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 162841016Sdfr /* this is CONTROLLER ERROR */ 162941016Sdfr splx(s); 163041016Sdfr kbdc_lock(sc->kbdc, FALSE); 163141016Sdfr return EIO; 163241016Sdfr } 163341016Sdfr 163441016Sdfr /* 163541016Sdfr * The device may be in the middle of status data transmission. 163641016Sdfr * The transmission will be interrupted, thus, incomplete status 163741016Sdfr * data must be discarded. Although the aux interrupt is disabled 163841016Sdfr * at the keyboard controller level, at most one aux interrupt 163941016Sdfr * may have already been pending and a data byte is in the 164041016Sdfr * output buffer; throw it away. Note that the second argument 164141016Sdfr * to `empty_aux_buffer()' is zero, so that the call will just 164241016Sdfr * flush the internal queue. 164341016Sdfr * `psmintr()' will be invoked after `splx()' if an interrupt is 164441016Sdfr * pending; it will see no data and returns immediately. 164541016Sdfr */ 164641016Sdfr empty_aux_buffer(sc->kbdc, 0); /* flush the queue */ 164741016Sdfr read_aux_data_no_wait(sc->kbdc); /* throw away data if any */ 1648123442Salfred flushpackets(sc); 164941016Sdfr splx(s); 165041016Sdfr 165141016Sdfr return 0; 165241016Sdfr} 165341016Sdfr 1654123442Salfredstatic void 1655123442Salfreddropqueue(struct psm_softc *sc) 1656123442Salfred{ 1657123442Salfred 1658123442Salfred sc->queue.count = 0; 1659123442Salfred sc->queue.head = 0; 1660123442Salfred sc->queue.tail = 0; 1661123442Salfred if ((sc->state & PSM_SOFTARMED) != 0) { 1662123442Salfred sc->state &= ~PSM_SOFTARMED; 1663123442Salfred untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout); 1664123442Salfred } 1665123442Salfred sc->pqueue_start = sc->pqueue_end; 1666123442Salfred} 1667123442Salfred 1668123442Salfredstatic void 1669123442Salfredflushpackets(struct psm_softc *sc) 1670123442Salfred{ 1671123442Salfred 1672123442Salfred dropqueue(sc); 1673123442Salfred bzero(&sc->pqueue, sizeof(sc->pqueue)); 1674123442Salfred} 1675123442Salfred 167641016Sdfrstatic int 167741016Sdfrunblock_mouse_data(struct psm_softc *sc, int c) 167841016Sdfr{ 167941016Sdfr int error = 0; 168041016Sdfr 168141016Sdfr /* 168241016Sdfr * We may have seen a part of status data during `set_mouse_XXX()'. 168341016Sdfr * they have been queued; flush it. 168441016Sdfr */ 168541016Sdfr empty_aux_buffer(sc->kbdc, 0); 168641016Sdfr 168741016Sdfr /* restore ports and interrupt */ 168841016Sdfr if (!set_controller_command_byte(sc->kbdc, 168941016Sdfr kbdc_get_device_mask(sc->kbdc), 169041016Sdfr c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 169141016Sdfr /* CONTROLLER ERROR; this is serious, we may have 169241016Sdfr * been left with the inaccessible keyboard and 169341016Sdfr * the disabled mouse interrupt. 169441016Sdfr */ 169541016Sdfr error = EIO; 169641016Sdfr } 169741016Sdfr 169841016Sdfr kbdc_lock(sc->kbdc, FALSE); 169941016Sdfr return error; 170041016Sdfr} 170141016Sdfr 170241016Sdfrstatic int 1703130585Sphkpsmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 170441016Sdfr{ 170541016Sdfr struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 170641016Sdfr mousemode_t mode; 170741016Sdfr mousestatus_t status; 170841016Sdfr#if (defined(MOUSE_GETVARS)) 170941016Sdfr mousevar_t *var; 171041016Sdfr#endif 171141016Sdfr mousedata_t *data; 171241016Sdfr int stat[3]; 171341016Sdfr int command_byte; 171441016Sdfr int error = 0; 171541016Sdfr int s; 171641016Sdfr 171741016Sdfr /* Perform IOCTL command */ 171841016Sdfr switch (cmd) { 171941016Sdfr 172041016Sdfr case OLD_MOUSE_GETHWINFO: 172141016Sdfr s = spltty(); 172241016Sdfr ((old_mousehw_t *)addr)->buttons = sc->hw.buttons; 172341016Sdfr ((old_mousehw_t *)addr)->iftype = sc->hw.iftype; 172441016Sdfr ((old_mousehw_t *)addr)->type = sc->hw.type; 172548778Syokota ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff; 172641016Sdfr splx(s); 172741016Sdfr break; 172841016Sdfr 172941016Sdfr case MOUSE_GETHWINFO: 173041016Sdfr s = spltty(); 173141016Sdfr *(mousehw_t *)addr = sc->hw; 173241016Sdfr if (sc->mode.level == PSM_LEVEL_BASE) 173341016Sdfr ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC; 173441016Sdfr splx(s); 173541016Sdfr break; 173641016Sdfr 1737132865Snjl case MOUSE_SYN_GETHWINFO: 1738132865Snjl s = spltty(); 1739132865Snjl if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) 1740132865Snjl *(synapticshw_t *)addr = sc->synhw; 1741132865Snjl else 1742132865Snjl error = EINVAL; 1743132865Snjl splx(s); 1744132865Snjl break; 1745132865Snjl 174641016Sdfr case OLD_MOUSE_GETMODE: 174741016Sdfr s = spltty(); 174841016Sdfr switch (sc->mode.level) { 174941016Sdfr case PSM_LEVEL_BASE: 175041016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 175141016Sdfr break; 175241016Sdfr case PSM_LEVEL_STANDARD: 175341016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 175441016Sdfr break; 175541016Sdfr case PSM_LEVEL_NATIVE: 175641016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 175741016Sdfr break; 175841016Sdfr } 175941016Sdfr ((old_mousemode_t *)addr)->rate = sc->mode.rate; 176041016Sdfr ((old_mousemode_t *)addr)->resolution = sc->mode.resolution; 176141016Sdfr ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor; 176241016Sdfr splx(s); 176341016Sdfr break; 176441016Sdfr 176541016Sdfr case MOUSE_GETMODE: 176641016Sdfr s = spltty(); 176741016Sdfr *(mousemode_t *)addr = sc->mode; 176841016Sdfr ((mousemode_t *)addr)->resolution = 176941016Sdfr MOUSE_RES_LOW - sc->mode.resolution; 177041016Sdfr switch (sc->mode.level) { 177141016Sdfr case PSM_LEVEL_BASE: 177241016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 177341016Sdfr ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE; 177441016Sdfr break; 177541016Sdfr case PSM_LEVEL_STANDARD: 177641016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 177741016Sdfr ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE; 177841016Sdfr ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK; 177941016Sdfr ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC; 178041016Sdfr break; 178141016Sdfr case PSM_LEVEL_NATIVE: 178241016Sdfr /* FIXME: this isn't quite correct... XXX */ 178341016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 178441016Sdfr break; 178541016Sdfr } 178641016Sdfr splx(s); 178741016Sdfr break; 178841016Sdfr 178941016Sdfr case OLD_MOUSE_SETMODE: 179041016Sdfr case MOUSE_SETMODE: 179141016Sdfr if (cmd == OLD_MOUSE_SETMODE) { 179241016Sdfr mode.rate = ((old_mousemode_t *)addr)->rate; 179341016Sdfr /* 179441016Sdfr * resolution old I/F new I/F 179541016Sdfr * default 0 0 179641016Sdfr * low 1 -2 179741016Sdfr * medium low 2 -3 179841016Sdfr * medium high 3 -4 179941016Sdfr * high 4 -5 180041016Sdfr */ 180141016Sdfr if (((old_mousemode_t *)addr)->resolution > 0) 180241016Sdfr mode.resolution = -((old_mousemode_t *)addr)->resolution - 1; 180341016Sdfr mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor; 180441016Sdfr mode.level = -1; 180541016Sdfr } else { 180641016Sdfr mode = *(mousemode_t *)addr; 180741016Sdfr } 180841016Sdfr 180941016Sdfr /* adjust and validate parameters. */ 181041016Sdfr if (mode.rate > UCHAR_MAX) 181141016Sdfr return EINVAL; 181241016Sdfr if (mode.rate == 0) 181341016Sdfr mode.rate = sc->dflt_mode.rate; 181441016Sdfr else if (mode.rate == -1) 181541016Sdfr /* don't change the current setting */ 181641016Sdfr ; 181741016Sdfr else if (mode.rate < 0) 181841016Sdfr return EINVAL; 181941016Sdfr if (mode.resolution >= UCHAR_MAX) 182041016Sdfr return EINVAL; 182141016Sdfr if (mode.resolution >= 200) 182241016Sdfr mode.resolution = MOUSE_RES_HIGH; 182341016Sdfr else if (mode.resolution >= 100) 182441016Sdfr mode.resolution = MOUSE_RES_MEDIUMHIGH; 182541016Sdfr else if (mode.resolution >= 50) 182641016Sdfr mode.resolution = MOUSE_RES_MEDIUMLOW; 182741016Sdfr else if (mode.resolution > 0) 182841016Sdfr mode.resolution = MOUSE_RES_LOW; 182941016Sdfr if (mode.resolution == MOUSE_RES_DEFAULT) 183041016Sdfr mode.resolution = sc->dflt_mode.resolution; 183141016Sdfr else if (mode.resolution == -1) 183241016Sdfr /* don't change the current setting */ 183341016Sdfr ; 183441016Sdfr else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 183541016Sdfr mode.resolution = MOUSE_RES_LOW - mode.resolution; 183641016Sdfr if (mode.level == -1) 183741016Sdfr /* don't change the current setting */ 183841016Sdfr mode.level = sc->mode.level; 183941016Sdfr else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX)) 184041016Sdfr return EINVAL; 184141016Sdfr if (mode.accelfactor == -1) 184241016Sdfr /* don't change the current setting */ 184341016Sdfr mode.accelfactor = sc->mode.accelfactor; 184441016Sdfr else if (mode.accelfactor < 0) 184541016Sdfr return EINVAL; 184641016Sdfr 184741016Sdfr /* don't allow anybody to poll the keyboard controller */ 184841016Sdfr error = block_mouse_data(sc, &command_byte); 184941016Sdfr if (error) 185041016Sdfr return error; 185141016Sdfr 185241016Sdfr /* set mouse parameters */ 185341016Sdfr if (mode.rate > 0) 185441016Sdfr mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 185541016Sdfr if (mode.resolution >= 0) 185641016Sdfr mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 185741016Sdfr set_mouse_scaling(sc->kbdc, 1); 185841016Sdfr get_mouse_status(sc->kbdc, stat, 0, 3); 185941016Sdfr 186041016Sdfr s = spltty(); 186141016Sdfr sc->mode.rate = mode.rate; 186241016Sdfr sc->mode.resolution = mode.resolution; 186341016Sdfr sc->mode.accelfactor = mode.accelfactor; 186441016Sdfr sc->mode.level = mode.level; 186541016Sdfr splx(s); 186641016Sdfr 186741016Sdfr unblock_mouse_data(sc, command_byte); 186841016Sdfr break; 186941016Sdfr 187041016Sdfr case MOUSE_GETLEVEL: 187141016Sdfr *(int *)addr = sc->mode.level; 187241016Sdfr break; 187341016Sdfr 187441016Sdfr case MOUSE_SETLEVEL: 187541016Sdfr if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX)) 187641016Sdfr return EINVAL; 187741016Sdfr sc->mode.level = *(int *)addr; 187841016Sdfr break; 187941016Sdfr 188041016Sdfr case MOUSE_GETSTATUS: 188141016Sdfr s = spltty(); 188241016Sdfr status = sc->status; 188341016Sdfr sc->status.flags = 0; 188441016Sdfr sc->status.obutton = sc->status.button; 188541016Sdfr sc->status.button = 0; 188641016Sdfr sc->status.dx = 0; 188741016Sdfr sc->status.dy = 0; 188841016Sdfr sc->status.dz = 0; 188941016Sdfr splx(s); 189041016Sdfr *(mousestatus_t *)addr = status; 189141016Sdfr break; 189241016Sdfr 189341016Sdfr#if (defined(MOUSE_GETVARS)) 189441016Sdfr case MOUSE_GETVARS: 189541016Sdfr var = (mousevar_t *)addr; 189641016Sdfr bzero(var, sizeof(*var)); 189741016Sdfr s = spltty(); 189841016Sdfr var->var[0] = MOUSE_VARS_PS2_SIG; 189941016Sdfr var->var[1] = sc->config; 190041016Sdfr var->var[2] = sc->flags; 190141016Sdfr splx(s); 190241016Sdfr break; 190341016Sdfr 190441016Sdfr case MOUSE_SETVARS: 190541016Sdfr return ENODEV; 190641016Sdfr#endif /* MOUSE_GETVARS */ 190741016Sdfr 190841016Sdfr case MOUSE_READSTATE: 190941016Sdfr case MOUSE_READDATA: 191041016Sdfr data = (mousedata_t *)addr; 191141016Sdfr if (data->len > sizeof(data->buf)/sizeof(data->buf[0])) 191241016Sdfr return EINVAL; 191341016Sdfr 191441016Sdfr error = block_mouse_data(sc, &command_byte); 191541016Sdfr if (error) 191641016Sdfr return error; 191741016Sdfr if ((data->len = get_mouse_status(sc->kbdc, data->buf, 191841016Sdfr (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0) 191941016Sdfr error = EIO; 192041016Sdfr unblock_mouse_data(sc, command_byte); 192141016Sdfr break; 192241016Sdfr 192341016Sdfr#if (defined(MOUSE_SETRESOLUTION)) 192441016Sdfr case MOUSE_SETRESOLUTION: 192541016Sdfr mode.resolution = *(int *)addr; 192641016Sdfr if (mode.resolution >= UCHAR_MAX) 192741016Sdfr return EINVAL; 192841016Sdfr else if (mode.resolution >= 200) 192941016Sdfr mode.resolution = MOUSE_RES_HIGH; 193041016Sdfr else if (mode.resolution >= 100) 193141016Sdfr mode.resolution = MOUSE_RES_MEDIUMHIGH; 193241016Sdfr else if (mode.resolution >= 50) 193341016Sdfr mode.resolution = MOUSE_RES_MEDIUMLOW; 193441016Sdfr else if (mode.resolution > 0) 193541016Sdfr mode.resolution = MOUSE_RES_LOW; 193641016Sdfr if (mode.resolution == MOUSE_RES_DEFAULT) 193741016Sdfr mode.resolution = sc->dflt_mode.resolution; 193841016Sdfr else if (mode.resolution == -1) 193941016Sdfr mode.resolution = sc->mode.resolution; 194041016Sdfr else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 194141016Sdfr mode.resolution = MOUSE_RES_LOW - mode.resolution; 194241016Sdfr 194341016Sdfr error = block_mouse_data(sc, &command_byte); 194441016Sdfr if (error) 194541016Sdfr return error; 194641016Sdfr sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 194741016Sdfr if (sc->mode.resolution != mode.resolution) 194841016Sdfr error = EIO; 194941016Sdfr unblock_mouse_data(sc, command_byte); 195041016Sdfr break; 195141016Sdfr#endif /* MOUSE_SETRESOLUTION */ 195241016Sdfr 195341016Sdfr#if (defined(MOUSE_SETRATE)) 195441016Sdfr case MOUSE_SETRATE: 195541016Sdfr mode.rate = *(int *)addr; 195641016Sdfr if (mode.rate > UCHAR_MAX) 195741016Sdfr return EINVAL; 195841016Sdfr if (mode.rate == 0) 195941016Sdfr mode.rate = sc->dflt_mode.rate; 196041016Sdfr else if (mode.rate < 0) 196141016Sdfr mode.rate = sc->mode.rate; 196241016Sdfr 196341016Sdfr error = block_mouse_data(sc, &command_byte); 196441016Sdfr if (error) 196541016Sdfr return error; 196641016Sdfr sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 196741016Sdfr if (sc->mode.rate != mode.rate) 196841016Sdfr error = EIO; 196941016Sdfr unblock_mouse_data(sc, command_byte); 197041016Sdfr break; 197141016Sdfr#endif /* MOUSE_SETRATE */ 197241016Sdfr 197341016Sdfr#if (defined(MOUSE_SETSCALING)) 197441016Sdfr case MOUSE_SETSCALING: 197541016Sdfr if ((*(int *)addr <= 0) || (*(int *)addr > 2)) 197641016Sdfr return EINVAL; 197741016Sdfr 197841016Sdfr error = block_mouse_data(sc, &command_byte); 197941016Sdfr if (error) 198041016Sdfr return error; 198141016Sdfr if (!set_mouse_scaling(sc->kbdc, *(int *)addr)) 198241016Sdfr error = EIO; 198341016Sdfr unblock_mouse_data(sc, command_byte); 198441016Sdfr break; 198541016Sdfr#endif /* MOUSE_SETSCALING */ 198641016Sdfr 198741016Sdfr#if (defined(MOUSE_GETHWID)) 198841016Sdfr case MOUSE_GETHWID: 198941016Sdfr error = block_mouse_data(sc, &command_byte); 199041016Sdfr if (error) 199141016Sdfr return error; 199248778Syokota sc->hw.hwid &= ~0x00ff; 199348778Syokota sc->hw.hwid |= get_aux_id(sc->kbdc); 199448778Syokota *(int *)addr = sc->hw.hwid & 0x00ff; 199541016Sdfr unblock_mouse_data(sc, command_byte); 199641016Sdfr break; 199741016Sdfr#endif /* MOUSE_GETHWID */ 199841016Sdfr 199941016Sdfr default: 200041016Sdfr return ENOTTY; 200141016Sdfr } 200241016Sdfr 200341016Sdfr return error; 200441016Sdfr} 200541016Sdfr 200641016Sdfrstatic void 200758230Syokotapsmtimeout(void *arg) 200858230Syokota{ 200958230Syokota struct psm_softc *sc; 201065045Syokota int s; 201158230Syokota 201284880Syokota sc = (struct psm_softc *)arg; 201365045Syokota s = spltty(); 201463746Syokota if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) { 201558230Syokota if (verbose >= 4) 201684880Syokota log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit); 201758230Syokota psmintr(sc); 201863746Syokota kbdc_lock(sc->kbdc, FALSE); 201958230Syokota } 202058230Syokota sc->watchdog = TRUE; 202165045Syokota splx(s); 202284880Syokota sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz); 202358230Syokota} 202458230Syokota 2025123442Salfredstatic int psmhz = 20; 2026123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmhz, CTLFLAG_RW, &psmhz, 0, ""); 2027123442Salfred 2028123442Salfredstatic int psm_soft_timeout = 500000; /* 0.5 sec */ 2029123442SalfredSYSCTL_INT(_debug, OID_AUTO, psm_soft_timeout, CTLFLAG_RW, 2030123442Salfred &psm_soft_timeout, 0, ""); 2031123442Salfred 2032123442Salfredstatic int psmerrsecs = 2; 2033123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrsecs, CTLFLAG_RW, &psmerrsecs, 0, ""); 2034123442Salfredstatic int psmerrusecs = 0; 2035123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmerrusecs, CTLFLAG_RW, &psmerrusecs, 0, ""); 2036123442Salfredstatic int psmsecs = 0; 2037123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmsecs, CTLFLAG_RW, &psmsecs, 0, ""); 2038123442Salfredstatic int psmusecs = 500000; 2039123442SalfredSYSCTL_INT(_debug, OID_AUTO, psmusecs, CTLFLAG_RW, &psmusecs, 0, ""); 2040123442Salfred 204158230Syokotastatic void 204241016Sdfrpsmintr(void *arg) 204341016Sdfr{ 2044123442Salfred struct psm_softc *sc = arg; 2045123442Salfred struct timeval now; 204641016Sdfr int c; 2047123442Salfred packetbuf_t *pb; 2048123442Salfred int haderror = 0; 204941016Sdfr 2050123442Salfred 205141016Sdfr /* read until there is nothing to read */ 205241016Sdfr while((c = read_aux_data_no_wait(sc->kbdc)) != -1) { 205341016Sdfr 2054123442Salfred pb = &sc->pqueue[sc->pqueue_end]; 205541016Sdfr /* discard the byte if the device is not open */ 205641016Sdfr if ((sc->state & PSM_OPEN) == 0) 205741016Sdfr continue; 205841016Sdfr 2059123442Salfred getmicrouptime(&now); 2060123442Salfred if ((pb->inputbytes > 0) && timevalcmp(&now, &sc->inputtimeout, >)) { 2061127841Smarkm#if DEBUG 206284880Syokota log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n"); 2063127841Smarkm#endif 2064123442Salfred pb->inputbytes = 0; 206584880Syokota sc->syncerrors = 0; 206684880Syokota } 206784880Syokota sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000; 206884880Syokota sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000; 2069123442Salfred timevaladd(&sc->inputtimeout, &now); 207084880Syokota 2071123442Salfred pb->ipacket[pb->inputbytes++] = c; 2072123442Salfred if (pb->inputbytes < sc->mode.packetsize) 207341016Sdfr continue; 207441016Sdfr 2075127841Smarkm#if DEBUG 207641016Sdfr log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n", 2077123442Salfred pb->ipacket[0], pb->ipacket[1], pb->ipacket[2], 2078123442Salfred pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]); 207941016Sdfr#endif 208041016Sdfr 2081123442Salfred c = pb->ipacket[0]; 208241016Sdfr 208363746Syokota if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) { 2084127841Smarkm#if DEBUG 2085123442Salfred log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x) %d" 2086123442Salfred " cmds since last error.\n", 2087123442Salfred c & sc->mode.syncmask[0], sc->mode.syncmask[1], 2088123442Salfred sc->cmdcount - sc->lasterr); 2089127841Smarkm#endif 2090123442Salfred haderror = 1; 2091123442Salfred sc->lasterr = sc->cmdcount; 2092123442Salfred dropqueue(sc); 209384880Syokota ++sc->syncerrors; 2094123442Salfred sc->lastinputerr = now; 209584880Syokota if (sc->syncerrors < sc->mode.packetsize) { 2096127841Smarkm#if DEBUG 209784880Syokota log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors); 2098127841Smarkm#endif 2099123442Salfred --pb->inputbytes; 2100123442Salfred bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes); 210184880Syokota } else if (sc->syncerrors == sc->mode.packetsize) { 2102127841Smarkm#if DEBUG 210369439Syokota log(LOG_DEBUG, "psmintr: re-enable the mouse.\n"); 2104127841Smarkm#endif 2105123442Salfred pb->inputbytes = 0; 210669439Syokota disable_aux_dev(sc->kbdc); 210769439Syokota enable_aux_dev(sc->kbdc); 210884880Syokota } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) { 2109127841Smarkm#if DEBUG 211084880Syokota log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors); 2111127841Smarkm#endif 2112123442Salfred --pb->inputbytes; 2113123442Salfred bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes); 211484880Syokota } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) { 2115127841Smarkm#if DEBUG 211684880Syokota log(LOG_DEBUG, "psmintr: reset the mouse.\n"); 2117127841Smarkm#endif 211884880Syokota reinitialize(sc, TRUE); 211969439Syokota } 212084880Syokota continue; 212163746Syokota } 2122123442Salfred /* if this packet is at all bogus then drop the packet. */ 2123123442Salfred if (haderror || 2124123442Salfred !timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, &now)) { 2125123442Salfred pb->inputbytes = 0; 2126123442Salfred haderror = 0; 2127123442Salfred continue; 2128123442Salfred } 212963746Syokota 2130123442Salfred sc->cmdcount++; 2131123442Salfred if (++sc->pqueue_end >= PSM_PACKETQUEUE) 2132123442Salfred sc->pqueue_end = 0; 2133123442Salfred /* 2134123442Salfred * If we've filled the queue then call the softintr ourselves, 2135123442Salfred * otherwise schedule the interrupt for later. 2136123442Salfred */ 2137123442Salfred if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) || 2138123442Salfred (sc->pqueue_end == sc->pqueue_start)) { 2139123442Salfred if ((sc->state & PSM_SOFTARMED) != 0) { 2140123442Salfred sc->state &= ~PSM_SOFTARMED; 2141123442Salfred untimeout(psmsoftintr, arg, sc->softcallout); 2142123442Salfred } 2143123442Salfred psmsoftintr(arg); 2144123442Salfred } else if ((sc->state & PSM_SOFTARMED) == 0) { 2145123442Salfred sc->state |= PSM_SOFTARMED; 2146123442Salfred sc->softcallout = timeout(psmsoftintr, arg, 2147123442Salfred psmhz < 1 ? 1 : (hz/psmhz)); 2148123442Salfred } 2149123442Salfred } 2150123442Salfred} 2151123442Salfred 2152123442Salfredstatic void 2153123442Salfredpsmsoftintr(void *arg) 2154123442Salfred{ 2155123442Salfred /* 2156123442Salfred * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN) 2157123442Salfred * into `mousestatus' button bits (MOUSE_BUTTON?DOWN). 2158123442Salfred */ 2159123442Salfred static int butmap[8] = { 2160123442Salfred 0, 2161123442Salfred MOUSE_BUTTON1DOWN, 2162123442Salfred MOUSE_BUTTON3DOWN, 2163123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 2164123442Salfred MOUSE_BUTTON2DOWN, 2165123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 2166123442Salfred MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 2167123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 2168123442Salfred }; 2169123442Salfred static int butmap_versapad[8] = { 2170123442Salfred 0, 2171123442Salfred MOUSE_BUTTON3DOWN, 2172123442Salfred 0, 2173123442Salfred MOUSE_BUTTON3DOWN, 2174123442Salfred MOUSE_BUTTON1DOWN, 2175123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 2176123442Salfred MOUSE_BUTTON1DOWN, 2177123442Salfred MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN 2178123442Salfred }; 2179133882Sphilip static int touchpad_buttons; 2180133882Sphilip static int guest_buttons; 2181123442Salfred register struct psm_softc *sc = arg; 2182123442Salfred mousestatus_t ms; 2183132865Snjl int w, x, y, z; 2184123442Salfred int c; 2185123442Salfred int l; 2186123442Salfred int x0, y0; 2187123442Salfred int s; 2188123442Salfred packetbuf_t *pb; 2189123442Salfred 2190123442Salfred getmicrouptime(&sc->lastsoftintr); 2191123442Salfred 2192123442Salfred s = spltty(); 2193123442Salfred 2194123442Salfred do { 2195123442Salfred 2196123442Salfred pb = &sc->pqueue[sc->pqueue_start]; 2197123442Salfred c = pb->ipacket[0]; 219849965Syokota /* 219941016Sdfr * A kludge for Kensington device! 220041016Sdfr * The MSB of the horizontal count appears to be stored in 220158230Syokota * a strange place. 220241016Sdfr */ 220358230Syokota if (sc->hw.model == MOUSE_MODEL_THINK) 2204123442Salfred pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0; 220541016Sdfr 220641016Sdfr /* ignore the overflow bits... */ 2207123442Salfred x = (c & MOUSE_PS2_XNEG) ? pb->ipacket[1] - 256 : pb->ipacket[1]; 2208123442Salfred y = (c & MOUSE_PS2_YNEG) ? pb->ipacket[2] - 256 : pb->ipacket[2]; 220941016Sdfr z = 0; 221041016Sdfr ms.obutton = sc->button; /* previous button state */ 221141016Sdfr ms.button = butmap[c & MOUSE_PS2_BUTTONS]; 221245789Speter /* `tapping' action */ 221345789Speter if (sc->config & PSM_CONFIG_FORCETAP) 221445789Speter ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 221541016Sdfr 221641016Sdfr switch (sc->hw.model) { 221741016Sdfr 221858230Syokota case MOUSE_MODEL_EXPLORER: 221958230Syokota /* 222058230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 222158230Syokota * byte 1: oy ox sy sx 1 M R L 222258230Syokota * byte 2: x x x x x x x x 222358230Syokota * byte 3: y y y y y y y y 222458230Syokota * byte 4: * * S2 S1 s d2 d1 d0 222558230Syokota * 222658230Syokota * L, M, R, S1, S2: left, middle, right and side buttons 222758230Syokota * s: wheel data sign bit 222858230Syokota * d2-d0: wheel data 222958230Syokota */ 2230123442Salfred z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) 2231123442Salfred ? (pb->ipacket[3] & 0x0f) - 16 : (pb->ipacket[3] & 0x0f); 2232123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) 223358230Syokota ? MOUSE_BUTTON4DOWN : 0; 2234123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) 223558230Syokota ? MOUSE_BUTTON5DOWN : 0; 223658230Syokota break; 223758230Syokota 223841016Sdfr case MOUSE_MODEL_INTELLI: 223941016Sdfr case MOUSE_MODEL_NET: 224041016Sdfr /* wheel data is in the fourth byte */ 2241123442Salfred z = (char)pb->ipacket[3]; 224258230Syokota /* some mice may send 7 when there is no Z movement?! XXX */ 224358230Syokota if ((z >= 7) || (z <= -7)) 224458230Syokota z = 0; 224558230Syokota /* some compatible mice have additional buttons */ 224658230Syokota ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) 224758230Syokota ? MOUSE_BUTTON4DOWN : 0; 224858230Syokota ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) 224958230Syokota ? MOUSE_BUTTON5DOWN : 0; 225041016Sdfr break; 225141016Sdfr 225241016Sdfr case MOUSE_MODEL_MOUSEMANPLUS: 225348778Syokota /* 225448778Syokota * PS2++ protocl packet 225548778Syokota * 225648778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 225748778Syokota * byte 1: * 1 p3 p2 1 * * * 225848778Syokota * byte 2: c1 c2 p1 p0 d1 d0 1 0 225948778Syokota * 226048778Syokota * p3-p0: packet type 226148778Syokota * c1, c2: c1 & c2 == 1, if p2 == 0 226248778Syokota * c1 & c2 == 0, if p2 == 1 226348778Syokota * 226448778Syokota * packet type: 0 (device type) 226548778Syokota * See comments in enable_mmanplus() below. 226648778Syokota * 226748778Syokota * packet type: 1 (wheel data) 226848778Syokota * 226948778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 227048778Syokota * byte 3: h * B5 B4 s d2 d1 d0 227148778Syokota * 227248778Syokota * h: 1, if horizontal roller data 227348778Syokota * 0, if vertical roller data 227448778Syokota * B4, B5: button 4 and 5 227548778Syokota * s: sign bit 227648778Syokota * d2-d0: roller data 227748778Syokota * 227848778Syokota * packet type: 2 (reserved) 227948778Syokota */ 228048778Syokota if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) 228148778Syokota && (abs(x) > 191) 2282123442Salfred && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) { 228341016Sdfr /* the extended data packet encodes button and wheel events */ 2284123442Salfred switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) { 228548778Syokota case 1: 228648778Syokota /* wheel data packet */ 228748778Syokota x = y = 0; 2288123442Salfred if (pb->ipacket[2] & 0x80) { 228948778Syokota /* horizontal roller count - ignore it XXX*/ 229048778Syokota } else { 229148778Syokota /* vertical roller count */ 2292123442Salfred z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) 2293123442Salfred ? (pb->ipacket[2] & 0x0f) - 16 2294123442Salfred : (pb->ipacket[2] & 0x0f); 229548778Syokota } 2296123442Salfred ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN) 229748778Syokota ? MOUSE_BUTTON4DOWN : 0; 2298123442Salfred ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN) 229948778Syokota ? MOUSE_BUTTON5DOWN : 0; 230048778Syokota break; 230148778Syokota case 2: 230258230Syokota /* this packet type is reserved by Logitech... */ 230358230Syokota /* 230458230Syokota * IBM ScrollPoint Mouse uses this packet type to 230558230Syokota * encode both vertical and horizontal scroll movement. 230658230Syokota */ 230758230Syokota x = y = 0; 230858230Syokota /* horizontal count */ 2309123442Salfred if (pb->ipacket[2] & 0x0f) 2310123442Salfred z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2; 231158230Syokota /* vertical count */ 2312123442Salfred if (pb->ipacket[2] & 0xf0) 2313123442Salfred z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1; 231458230Syokota#if 0 231558230Syokota /* vertical count */ 2316123442Salfred z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) 2317123442Salfred ? ((pb->ipacket[2] >> 4) & 0x0f) - 16 2318123442Salfred : ((pb->ipacket[2] >> 4) & 0x0f); 231958230Syokota /* horizontal count */ 2320123442Salfred w = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) 2321123442Salfred ? (pb->ipacket[2] & 0x0f) - 16 2322123442Salfred : (pb->ipacket[2] & 0x0f); 232358230Syokota#endif 232458230Syokota break; 232548778Syokota case 0: 232648778Syokota /* device type packet - shouldn't happen */ 2327102412Scharnier /* FALLTHROUGH */ 232848778Syokota default: 232948778Syokota x = y = 0; 233048778Syokota ms.button = ms.obutton; 233158230Syokota if (bootverbose) 233258230Syokota log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: " 233358230Syokota "0x%02x 0x%02x 0x%02x\n", 2334123442Salfred MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket), 2335123442Salfred pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]); 233648778Syokota break; 233748778Syokota } 233841016Sdfr } else { 233941016Sdfr /* preserve button states */ 234041016Sdfr ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 234141016Sdfr } 234241016Sdfr break; 234341016Sdfr 234441016Sdfr case MOUSE_MODEL_GLIDEPOINT: 234541016Sdfr /* `tapping' action */ 234641016Sdfr ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 234741016Sdfr break; 234841016Sdfr 234941016Sdfr case MOUSE_MODEL_NETSCROLL: 235058230Syokota /* three addtional bytes encode buttons and wheel events */ 2351123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) 235241016Sdfr ? MOUSE_BUTTON4DOWN : 0; 2353123442Salfred ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) 235458230Syokota ? MOUSE_BUTTON5DOWN : 0; 2355123442Salfred z = (pb->ipacket[3] & MOUSE_PS2_XNEG) 2356123442Salfred ? pb->ipacket[4] - 256 : pb->ipacket[4]; 235741016Sdfr break; 235841016Sdfr 235941016Sdfr case MOUSE_MODEL_THINK: 236041016Sdfr /* the fourth button state in the first byte */ 236141016Sdfr ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0; 236241016Sdfr break; 236341016Sdfr 236449965Syokota case MOUSE_MODEL_VERSAPAD: 236549965Syokota /* VersaPad PS/2 absolute mode message format 236649965Syokota * 236749965Syokota * [packet1] 7 6 5 4 3 2 1 0(LSB) 236849965Syokota * ipacket[0]: 1 1 0 A 1 L T R 236949965Syokota * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0 237049965Syokota * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0 237149965Syokota * ipacket[3]: 1 1 1 A 1 L T R 237249965Syokota * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8 237349965Syokota * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0 237449965Syokota * 237549965Syokota * [note] 237649965Syokota * R: right physical mouse button (1=on) 237749965Syokota * T: touch pad virtual button (1=tapping) 237849965Syokota * L: left physical mouse button (1=on) 237949965Syokota * A: position data is valid (1=valid) 238049965Syokota * H: horizontal data (12bit signed integer. H11 is sign bit.) 238149965Syokota * V: vertical data (12bit signed integer. V11 is sign bit.) 238249965Syokota * P: pressure data 238349965Syokota * 238449965Syokota * Tapping is mapped to MOUSE_BUTTON4. 238549965Syokota */ 238649965Syokota ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS]; 238749965Syokota ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 238849965Syokota x = y = 0; 238949965Syokota if (c & MOUSE_PS2VERSA_IN_USE) { 2390123442Salfred x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8); 2391123442Salfred y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4); 239249965Syokota if (x0 & 0x800) 239349965Syokota x0 -= 0x1000; 239449965Syokota if (y0 & 0x800) 239549965Syokota y0 -= 0x1000; 239649965Syokota if (sc->flags & PSM_FLAGS_FINGERDOWN) { 239749965Syokota x = sc->xold - x0; 239849965Syokota y = y0 - sc->yold; 239949965Syokota if (x < 0) /* XXX */ 240049965Syokota x++; 240149965Syokota else if (x) 240249965Syokota x--; 240349965Syokota if (y < 0) 240449965Syokota y++; 240549965Syokota else if (y) 240649965Syokota y--; 240749965Syokota } else { 240849965Syokota sc->flags |= PSM_FLAGS_FINGERDOWN; 240949965Syokota } 241049965Syokota sc->xold = x0; 241149965Syokota sc->yold = y0; 241249965Syokota } else { 241349965Syokota sc->flags &= ~PSM_FLAGS_FINGERDOWN; 241449965Syokota } 241549965Syokota c = ((x < 0) ? MOUSE_PS2_XNEG : 0) 241649965Syokota | ((y < 0) ? MOUSE_PS2_YNEG : 0); 241749965Syokota break; 241849965Syokota 241958230Syokota case MOUSE_MODEL_4D: 242058230Syokota /* 242158230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 242258230Syokota * byte 1: s2 d2 s1 d1 1 M R L 242358230Syokota * byte 2: sx x x x x x x x 242458230Syokota * byte 3: sy y y y y y y y 242558230Syokota * 242658230Syokota * s1: wheel 1 direction 242758230Syokota * d1: wheel 1 data 242858230Syokota * s2: wheel 2 direction 242958230Syokota * d2: wheel 2 data 243058230Syokota */ 2431123442Salfred x = (pb->ipacket[1] & 0x80) ? pb->ipacket[1] - 256 : pb->ipacket[1]; 2432123442Salfred y = (pb->ipacket[2] & 0x80) ? pb->ipacket[2] - 256 : pb->ipacket[2]; 243358230Syokota switch (c & MOUSE_4D_WHEELBITS) { 243458230Syokota case 0x10: 243558230Syokota z = 1; 243658230Syokota break; 243758230Syokota case 0x30: 243858230Syokota z = -1; 243958230Syokota break; 244058230Syokota case 0x40: /* 2nd wheel turning right XXX */ 244158230Syokota z = 2; 244258230Syokota break; 244358230Syokota case 0xc0: /* 2nd wheel turning left XXX */ 244458230Syokota z = -2; 244558230Syokota break; 244658230Syokota } 244758230Syokota break; 244858230Syokota 244958230Syokota case MOUSE_MODEL_4DPLUS: 245058230Syokota if ((x < 16 - 256) && (y < 16 - 256)) { 245158230Syokota /* 245258230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 245358230Syokota * byte 1: 0 0 1 1 1 M R L 245458230Syokota * byte 2: 0 0 0 0 1 0 0 0 245558230Syokota * byte 3: 0 0 0 0 S s d1 d0 245658230Syokota * 245758230Syokota * L, M, R, S: left, middle, right and side buttons 245858230Syokota * s: wheel data sign bit 245958230Syokota * d1-d0: wheel data 246058230Syokota */ 246158230Syokota x = y = 0; 2462123442Salfred if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN) 246358230Syokota ms.button |= MOUSE_BUTTON4DOWN; 2464123442Salfred z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) 2465123442Salfred ? ((pb->ipacket[2] & 0x07) - 8) 2466123442Salfred : (pb->ipacket[2] & 0x07) ; 246758230Syokota } else { 246858230Syokota /* preserve previous button states */ 246958230Syokota ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 247058230Syokota } 247158230Syokota break; 247258230Syokota 2473132865Snjl case MOUSE_MODEL_SYNAPTICS: 2474132865Snjl /* TouchPad PS/2 absolute mode message format 2475132865Snjl * 2476132865Snjl * Bits: 7 6 5 4 3 2 1 0 (LSB) 2477132865Snjl * ------------------------------------------------ 2478132865Snjl * ipacket[0]: 1 0 W3 W2 0 W1 R L 2479132865Snjl * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8 2480132865Snjl * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0 2481132865Snjl * ipacket[3]: 1 1 Yc Xc 0 W0 D U 2482132865Snjl * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0 2483132865Snjl * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 2484132865Snjl * 2485132865Snjl * Legend: 2486132865Snjl * L: left physical mouse button 2487132865Snjl * R: right physical mouse button 2488132865Snjl * D: down button 2489132865Snjl * U: up button 2490132865Snjl * W: "wrist" value 2491132865Snjl * X: x position 2492132865Snjl * Y: x position 2493132865Snjl * Z: pressure 2494132865Snjl * 2495132865Snjl * Absolute reportable limits: 0 - 6143. 2496132865Snjl * Typical bezel limits: 1472 - 5472. 2497132865Snjl * Typical edge marings: 1632 - 5312. 2498132865Snjl * 2499132865Snjl * w = 3 Passthrough Packet 2500132865Snjl * 2501132865Snjl * Byte 2,5,6 == Byte 1,2,3 of "Guest" 2502132865Snjl */ 2503132865Snjl 2504132865Snjl /* Sanity check for out of sync packets. */ 2505132865Snjl if ((pb->ipacket[0] & 0xc8) != 0x80 || 2506132865Snjl (pb->ipacket[3] & 0xc8) != 0xc0) 2507132865Snjl continue; 2508132865Snjl 2509132865Snjl x = y = x0 = y0 = 0; 2510132865Snjl 2511132865Snjl /* Pressure value. */ 2512132865Snjl z = pb->ipacket[2]; 2513132865Snjl 2514133301Sphilip /* Finger width value */ 2515133301Sphilip if (sc->synhw.capExtended) { 2516133301Sphilip w = ((pb->ipacket[0] & 0x30) >> 2) | 2517133301Sphilip ((pb->ipacket[0] & 0x04) >> 1) | 2518133301Sphilip ((pb->ipacket[3] & 0x04) >> 2); 2519133301Sphilip } else { 2520133301Sphilip /* Assume a finger of regular width */ 2521133301Sphilip w = 4; 2522133301Sphilip } 2523133301Sphilip 2524133868Sphilip /* Handle packets from the guest device */ 2525133868Sphilip if (w == 3 && sc->synhw.capPassthrough) { 2526133868Sphilip x = ((pb->ipacket[1] & 0x10) ? 2527133868Sphilip pb->ipacket[4] - 256 : pb->ipacket[4]); 2528133868Sphilip y = ((pb->ipacket[1] & 0x20) ? 2529133868Sphilip pb->ipacket[5] - 256 : pb->ipacket[5]); 2530133868Sphilip z = 0; 2531133868Sphilip 2532133868Sphilip guest_buttons = 0; 2533133868Sphilip if (pb->ipacket[1] & 0x01) 2534133868Sphilip guest_buttons |= MOUSE_BUTTON1DOWN; 2535133868Sphilip if (pb->ipacket[1] & 0x04) 2536133868Sphilip guest_buttons |= MOUSE_BUTTON2DOWN; 2537133868Sphilip if (pb->ipacket[1] & 0x02) 2538133868Sphilip guest_buttons |= MOUSE_BUTTON3DOWN; 2539133868Sphilip 2540133868Sphilip ms.button = touchpad_buttons | guest_buttons; 2541133868Sphilip break; 2542133868Sphilip } 2543133868Sphilip 2544133296Sphilip /* Button presses */ 2545133868Sphilip touchpad_buttons = 0; 2546132865Snjl if (pb->ipacket[0] & 0x01) 2547133868Sphilip touchpad_buttons |= MOUSE_BUTTON1DOWN; 2548132865Snjl if (pb->ipacket[0] & 0x02) 2549133868Sphilip touchpad_buttons |= MOUSE_BUTTON3DOWN; 2550132865Snjl 2551133296Sphilip if (sc->synhw.capExtended && sc->synhw.capFourButtons) { 2552133296Sphilip if ((pb->ipacket[3] & 0x01) && (pb->ipacket[0] & 0x01) == 0) 2553133868Sphilip touchpad_buttons |= MOUSE_BUTTON4DOWN; 2554133296Sphilip if ((pb->ipacket[3] & 0x02) && (pb->ipacket[0] & 0x02) == 0) 2555133868Sphilip touchpad_buttons |= MOUSE_BUTTON5DOWN; 2556133296Sphilip } 2557132865Snjl 2558133868Sphilip ms.button = touchpad_buttons | guest_buttons; 2559133868Sphilip 2560132865Snjl /* There is a finger on the pad. */ 2561132865Snjl if ((w >= 4 && w <= 7) && (z >= 16 && z < 200)) { 2562132865Snjl x0 = ((pb->ipacket[3] & 0x10) << 8) | 2563132865Snjl ((pb->ipacket[1] & 0x0f) << 8) | 2564132865Snjl pb->ipacket[4]; 2565132865Snjl y0 = ((pb->ipacket[3] & 0x20) << 7) | 2566132865Snjl ((pb->ipacket[1] & 0xf0) << 4) | 2567132865Snjl pb->ipacket[5]; 2568132865Snjl 2569132865Snjl if (sc->flags & PSM_FLAGS_FINGERDOWN) { 2570132865Snjl x0 = (x0 + sc->xold * 3) / 4; 2571132865Snjl y0 = (y0 + sc->yold * 3) / 4; 2572132865Snjl 2573133298Sphilip x = (x0 - sc->xold) * 10 / 85; 2574133298Sphilip y = (y0 - sc->yold) * 10 / 85; 2575132865Snjl } else { 2576132865Snjl sc->flags |= PSM_FLAGS_FINGERDOWN; 2577132865Snjl } 2578132865Snjl 2579132865Snjl sc->xold = x0; 2580132865Snjl sc->yold = y0; 2581133297Sphilip sc->zmax = imax(z, sc->zmax); 2582132865Snjl } else { 2583132865Snjl sc->flags &= ~PSM_FLAGS_FINGERDOWN; 2584133297Sphilip 2585133297Sphilip if (sc->zmax > PSM_TAP_THRESHOLD && 2586133297Sphilip timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) { 2587133297Sphilip if (w == 0) 2588133297Sphilip ms.button |= MOUSE_BUTTON3DOWN; 2589133297Sphilip else if (w == 1) 2590133297Sphilip ms.button |= MOUSE_BUTTON2DOWN; 2591133297Sphilip else 2592133297Sphilip ms.button |= MOUSE_BUTTON1DOWN; 2593133297Sphilip } 2594133297Sphilip 2595133297Sphilip sc->zmax = 0; 2596133297Sphilip sc->taptimeout.tv_sec = PSM_TAP_TIMEOUT / 1000000; 2597133297Sphilip sc->taptimeout.tv_usec = PSM_TAP_TIMEOUT % 1000000; 2598133297Sphilip timevaladd(&sc->taptimeout, &sc->lastsoftintr); 2599132865Snjl } 2600133296Sphilip 2601133296Sphilip /* Use the extra buttons as a scrollwheel */ 2602133296Sphilip if (ms.button & MOUSE_BUTTON4DOWN) 2603133296Sphilip z = -1; 2604133296Sphilip else if (ms.button & MOUSE_BUTTON5DOWN) 2605133296Sphilip z = 1; 2606133296Sphilip else 2607133296Sphilip z = 0; 2608133296Sphilip 2609132865Snjl break; 2610132865Snjl 261141016Sdfr case MOUSE_MODEL_GENERIC: 261241016Sdfr default: 261341016Sdfr break; 261441016Sdfr } 261541016Sdfr 261641016Sdfr /* scale values */ 261741016Sdfr if (sc->mode.accelfactor >= 1) { 261841016Sdfr if (x != 0) { 261941016Sdfr x = x * x / sc->mode.accelfactor; 262041016Sdfr if (x == 0) 262141016Sdfr x = 1; 262241016Sdfr if (c & MOUSE_PS2_XNEG) 262341016Sdfr x = -x; 262441016Sdfr } 262541016Sdfr if (y != 0) { 262641016Sdfr y = y * y / sc->mode.accelfactor; 262741016Sdfr if (y == 0) 262841016Sdfr y = 1; 262941016Sdfr if (c & MOUSE_PS2_YNEG) 263041016Sdfr y = -y; 263141016Sdfr } 263241016Sdfr } 263341016Sdfr 263441016Sdfr ms.dx = x; 263541016Sdfr ms.dy = y; 263641016Sdfr ms.dz = z; 263741016Sdfr ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) 263841016Sdfr | (ms.obutton ^ ms.button); 263941016Sdfr 264041016Sdfr if (sc->mode.level < PSM_LEVEL_NATIVE) 2641123442Salfred pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket); 264241016Sdfr 264341016Sdfr sc->status.flags |= ms.flags; 264441016Sdfr sc->status.dx += ms.dx; 264541016Sdfr sc->status.dy += ms.dy; 264641016Sdfr sc->status.dz += ms.dz; 264741016Sdfr sc->status.button = ms.button; 264841016Sdfr sc->button = ms.button; 264941016Sdfr 265058230Syokota sc->watchdog = FALSE; 265158230Syokota 265241016Sdfr /* queue data */ 2653123442Salfred if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) { 2654123442Salfred l = imin(pb->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail); 2655123442Salfred bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l); 2656123442Salfred if (pb->inputbytes > l) 2657123442Salfred bcopy(&pb->ipacket[l], &sc->queue.buf[0], pb->inputbytes - l); 265841016Sdfr sc->queue.tail = 2659123442Salfred (sc->queue.tail + pb->inputbytes) % sizeof(sc->queue.buf); 2660123442Salfred sc->queue.count += pb->inputbytes; 266141016Sdfr } 2662123442Salfred pb->inputbytes = 0; 266341016Sdfr 2664123442Salfred if (++sc->pqueue_start >= PSM_PACKETQUEUE) 2665123442Salfred sc->pqueue_start = 0; 2666123442Salfred } while (sc->pqueue_start != sc->pqueue_end); 2667123442Salfred if (sc->state & PSM_ASLP) { 2668123442Salfred sc->state &= ~PSM_ASLP; 2669123442Salfred wakeup( sc); 267041016Sdfr } 2671123442Salfred selwakeuppri(&sc->rsel, PZERO); 2672123442Salfred sc->state &= ~PSM_SOFTARMED; 2673123442Salfred splx(s); 267441016Sdfr} 267541016Sdfr 267641016Sdfrstatic int 2677130585Sphkpsmpoll(struct cdev *dev, int events, struct thread *td) 267841016Sdfr{ 267941016Sdfr struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 268041016Sdfr int s; 268141016Sdfr int revents = 0; 268241016Sdfr 268341016Sdfr /* Return true if a mouse event available */ 268441016Sdfr s = spltty(); 268545789Speter if (events & (POLLIN | POLLRDNORM)) { 268641016Sdfr if (sc->queue.count > 0) 268741016Sdfr revents |= events & (POLLIN | POLLRDNORM); 268841016Sdfr else 268983366Sjulian selrecord(td, &sc->rsel); 269045789Speter } 269141016Sdfr splx(s); 269241016Sdfr 269341016Sdfr return (revents); 269441016Sdfr} 269541016Sdfr 269641016Sdfr/* vendor/model specific routines */ 269741016Sdfr 269841016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status) 269941016Sdfr{ 270041016Sdfr if (set_mouse_resolution(kbdc, res) != res) 270141016Sdfr return FALSE; 270241016Sdfr if (set_mouse_scaling(kbdc, scale) 270341016Sdfr && set_mouse_scaling(kbdc, scale) 270441016Sdfr && set_mouse_scaling(kbdc, scale) 270541016Sdfr && (get_mouse_status(kbdc, status, 0, 3) >= 3)) 270641016Sdfr return TRUE; 270741016Sdfr return FALSE; 270841016Sdfr} 270941016Sdfr 271069438Syokotastatic int 271169438Syokotamouse_ext_command(KBDC kbdc, int command) 271269438Syokota{ 271369438Syokota int c; 271469438Syokota 271569438Syokota c = (command >> 6) & 0x03; 271669438Syokota if (set_mouse_resolution(kbdc, c) != c) 271769438Syokota return FALSE; 271869438Syokota c = (command >> 4) & 0x03; 271969438Syokota if (set_mouse_resolution(kbdc, c) != c) 272069438Syokota return FALSE; 272169438Syokota c = (command >> 2) & 0x03; 272269438Syokota if (set_mouse_resolution(kbdc, c) != c) 272369438Syokota return FALSE; 272469438Syokota c = (command >> 0) & 0x03; 272569438Syokota if (set_mouse_resolution(kbdc, c) != c) 272669438Syokota return FALSE; 272769438Syokota return TRUE; 272869438Syokota} 272969438Syokota 273041016Sdfr#if notyet 273141016Sdfr/* Logitech MouseMan Cordless II */ 273241016Sdfrstatic int 273341016Sdfrenable_lcordless(struct psm_softc *sc) 273441016Sdfr{ 273541016Sdfr int status[3]; 273641016Sdfr int ch; 273741016Sdfr 273841016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status)) 273941016Sdfr return FALSE; 274041016Sdfr if (status[1] == PSMD_RES_HIGH) 274141016Sdfr return FALSE; 274241016Sdfr ch = (status[0] & 0x07) - 1; /* channel # */ 274341016Sdfr if ((ch <= 0) || (ch > 4)) 274441016Sdfr return FALSE; 274541016Sdfr /* 274641016Sdfr * status[1]: always one? 274741016Sdfr * status[2]: battery status? (0-100) 274841016Sdfr */ 274941016Sdfr return TRUE; 275041016Sdfr} 275141016Sdfr#endif /* notyet */ 275241016Sdfr 275358230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */ 275441016Sdfrstatic int 275541016Sdfrenable_groller(struct psm_softc *sc) 275641016Sdfr{ 275741016Sdfr int status[3]; 275841016Sdfr 275941016Sdfr /* 276041016Sdfr * The special sequence to enable the fourth button and the 276141016Sdfr * roller. Immediately after this sequence check status bytes. 276241016Sdfr * if the mouse is NetScroll, the second and the third bytes are 276341016Sdfr * '3' and 'D'. 276441016Sdfr */ 276541016Sdfr 276641016Sdfr /* 276741016Sdfr * If the mouse is an ordinary PS/2 mouse, the status bytes should 276841016Sdfr * look like the following. 276941016Sdfr * 277041016Sdfr * byte 1 bit 7 always 0 277141016Sdfr * bit 6 stream mode (0) 277241016Sdfr * bit 5 disabled (0) 277341016Sdfr * bit 4 1:1 scaling (0) 277441016Sdfr * bit 3 always 0 277541016Sdfr * bit 0-2 button status 277641016Sdfr * byte 2 resolution (PSMD_RES_HIGH) 277741016Sdfr * byte 3 report rate (?) 277841016Sdfr */ 277941016Sdfr 278041016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 278141016Sdfr return FALSE; 278241016Sdfr if ((status[1] != '3') || (status[2] != 'D')) 278341016Sdfr return FALSE; 278458230Syokota /* FIXME: SmartScroll Mouse has 5 buttons! XXX */ 278541016Sdfr sc->hw.buttons = 4; 278641016Sdfr return TRUE; 278741016Sdfr} 278841016Sdfr 278958230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */ 279041016Sdfrstatic int 279141016Sdfrenable_gmouse(struct psm_softc *sc) 279241016Sdfr{ 279341016Sdfr int status[3]; 279441016Sdfr 279541016Sdfr /* 279641016Sdfr * The special sequence to enable the middle, "rubber" button. 279741016Sdfr * Immediately after this sequence check status bytes. 279841016Sdfr * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse, 279941016Sdfr * the second and the third bytes are '3' and 'U'. 280041016Sdfr * NOTE: NetMouse reports that it has three buttons although it has 280141016Sdfr * two buttons and a rubber button. NetMouse Pro and MIE Mouse 280241016Sdfr * say they have three buttons too and they do have a button on the 280341016Sdfr * side... 280441016Sdfr */ 280541016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 280641016Sdfr return FALSE; 280741016Sdfr if ((status[1] != '3') || (status[2] != 'U')) 280841016Sdfr return FALSE; 280941016Sdfr return TRUE; 281041016Sdfr} 281141016Sdfr 281241016Sdfr/* ALPS GlidePoint */ 281341016Sdfrstatic int 281441016Sdfrenable_aglide(struct psm_softc *sc) 281541016Sdfr{ 281641016Sdfr int status[3]; 281741016Sdfr 281841016Sdfr /* 281941016Sdfr * The special sequence to obtain ALPS GlidePoint specific 282041016Sdfr * information. Immediately after this sequence, status bytes will 282141016Sdfr * contain something interesting. 282241016Sdfr * NOTE: ALPS produces several models of GlidePoint. Some of those 282341016Sdfr * do not respond to this sequence, thus, cannot be detected this way. 282441016Sdfr */ 282550149Syokota if (set_mouse_sampling_rate(sc->kbdc, 100) != 100) 282650149Syokota return FALSE; 282741016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status)) 282841016Sdfr return FALSE; 282950149Syokota if ((status[1] == PSMD_RES_LOW) || (status[2] == 100)) 283041016Sdfr return FALSE; 283141016Sdfr return TRUE; 283241016Sdfr} 283341016Sdfr 283441016Sdfr/* Kensington ThinkingMouse/Trackball */ 283541016Sdfrstatic int 283641016Sdfrenable_kmouse(struct psm_softc *sc) 283741016Sdfr{ 283841016Sdfr static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 }; 283941016Sdfr KBDC kbdc = sc->kbdc; 284041016Sdfr int status[3]; 284141016Sdfr int id1; 284241016Sdfr int id2; 284341016Sdfr int i; 284441016Sdfr 284541016Sdfr id1 = get_aux_id(kbdc); 284641016Sdfr if (set_mouse_sampling_rate(kbdc, 10) != 10) 284741016Sdfr return FALSE; 284841016Sdfr /* 284941016Sdfr * The device is now in the native mode? It returns a different 285041016Sdfr * ID value... 285141016Sdfr */ 285241016Sdfr id2 = get_aux_id(kbdc); 285341016Sdfr if ((id1 == id2) || (id2 != 2)) 285441016Sdfr return FALSE; 285541016Sdfr 285641016Sdfr if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 285741016Sdfr return FALSE; 285841016Sdfr#if PSM_DEBUG >= 2 285941016Sdfr /* at this point, resolution is LOW, sampling rate is 10/sec */ 286041016Sdfr if (get_mouse_status(kbdc, status, 0, 3) < 3) 286141016Sdfr return FALSE; 286241016Sdfr#endif 286341016Sdfr 286441016Sdfr /* 286541016Sdfr * The special sequence to enable the third and fourth buttons. 286641016Sdfr * Otherwise they behave like the first and second buttons. 286741016Sdfr */ 286841016Sdfr for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 286941016Sdfr if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 287041016Sdfr return FALSE; 287141016Sdfr } 287241016Sdfr 287341016Sdfr /* 287441016Sdfr * At this point, the device is using default resolution and 287541016Sdfr * sampling rate for the native mode. 287641016Sdfr */ 287741016Sdfr if (get_mouse_status(kbdc, status, 0, 3) < 3) 287841016Sdfr return FALSE; 287941016Sdfr if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1])) 288041016Sdfr return FALSE; 288141016Sdfr 288241016Sdfr /* the device appears be enabled by this sequence, diable it for now */ 288341016Sdfr disable_aux_dev(kbdc); 288441016Sdfr empty_aux_buffer(kbdc, 5); 288541016Sdfr 288641016Sdfr return TRUE; 288741016Sdfr} 288841016Sdfr 288958230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */ 289041016Sdfrstatic int 289141016Sdfrenable_mmanplus(struct psm_softc *sc) 289241016Sdfr{ 289341016Sdfr KBDC kbdc = sc->kbdc; 289441016Sdfr int data[3]; 289541016Sdfr 289641016Sdfr /* the special sequence to enable the fourth button and the roller. */ 289758230Syokota /* 289858230Syokota * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION 289958230Syokota * must be called exactly three times since the last RESET command 290058230Syokota * before this sequence. XXX 290158230Syokota */ 290269438Syokota if (!set_mouse_scaling(kbdc, 1)) 290369438Syokota return FALSE; 290469438Syokota if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb)) 290569438Syokota return FALSE; 290641016Sdfr if (get_mouse_status(kbdc, data, 1, 3) < 3) 290741016Sdfr return FALSE; 290841016Sdfr 290948778Syokota /* 291048778Syokota * PS2++ protocl, packet type 0 291141016Sdfr * 291248778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 291348778Syokota * byte 1: * 1 p3 p2 1 * * * 291448778Syokota * byte 2: 1 1 p1 p0 m1 m0 1 0 291548778Syokota * byte 3: m7 m6 m5 m4 m3 m2 m1 m0 291648778Syokota * 291748778Syokota * p3-p0: packet type: 0 291858230Syokota * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58... 291941016Sdfr */ 292048778Syokota /* check constant bits */ 292148778Syokota if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC) 292241016Sdfr return FALSE; 292348778Syokota if ((data[1] & 0xc3) != 0xc2) 292448778Syokota return FALSE; 292548778Syokota /* check d3-d0 in byte 2 */ 292648778Syokota if (!MOUSE_PS2PLUS_CHECKBITS(data)) 292748778Syokota return FALSE; 292848778Syokota /* check p3-p0 */ 292948778Syokota if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0) 293048778Syokota return FALSE; 293141016Sdfr 293248778Syokota sc->hw.hwid &= 0x00ff; 293348778Syokota sc->hw.hwid |= data[2] << 8; /* save model ID */ 293448778Syokota 293541016Sdfr /* 293641016Sdfr * MouseMan+ (or FirstMouse+) is now in its native mode, in which 293741016Sdfr * the wheel and the fourth button events are encoded in the 293841016Sdfr * special data packet. The mouse may be put in the IntelliMouse mode 293941016Sdfr * if it is initialized by the IntelliMouse's method. 294041016Sdfr */ 294141016Sdfr return TRUE; 294241016Sdfr} 294341016Sdfr 294458230Syokota/* MS IntelliMouse Explorer */ 294558230Syokotastatic int 294658230Syokotaenable_msexplorer(struct psm_softc *sc) 294758230Syokota{ 294858923Syokota static unsigned char rate0[] = { 200, 100, 80, }; 294958923Syokota static unsigned char rate1[] = { 200, 200, 80, }; 295058230Syokota KBDC kbdc = sc->kbdc; 295158230Syokota int id; 295258230Syokota int i; 295358230Syokota 295469438Syokota /* the special sequence to enable the extra buttons and the roller. */ 295569438Syokota for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) { 295669438Syokota if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i]) 295769438Syokota return FALSE; 295869438Syokota } 295969438Syokota /* the device will give the genuine ID only after the above sequence */ 296069438Syokota id = get_aux_id(kbdc); 296169438Syokota if (id != PSM_EXPLORER_ID) 296269438Syokota return FALSE; 296369438Syokota 296469438Syokota sc->hw.hwid = id; 296569438Syokota sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */ 296669438Syokota 296758923Syokota /* 296858923Syokota * XXX: this is a kludge to fool some KVM switch products 296958923Syokota * which think they are clever enough to know the 4-byte IntelliMouse 297058923Syokota * protocol, and assume any other protocols use 3-byte packets. 297158923Syokota * They don't convey 4-byte data packets from the IntelliMouse Explorer 297258923Syokota * correctly to the host computer because of this! 297358923Syokota * The following sequence is actually IntelliMouse's "wake up" 297458923Syokota * sequence; it will make the KVM think the mouse is IntelliMouse 297558923Syokota * when it is in fact IntelliMouse Explorer. 297658923Syokota */ 297758923Syokota for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) { 297858923Syokota if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i]) 297969438Syokota break; 298058923Syokota } 298158923Syokota id = get_aux_id(kbdc); 298258923Syokota 298358230Syokota return TRUE; 298458230Syokota} 298558230Syokota 298641016Sdfr/* MS IntelliMouse */ 298741016Sdfrstatic int 298841016Sdfrenable_msintelli(struct psm_softc *sc) 298941016Sdfr{ 299041016Sdfr /* 299141016Sdfr * Logitech MouseMan+ and FirstMouse+ will also respond to this 299241016Sdfr * probe routine and act like IntelliMouse. 299341016Sdfr */ 299441016Sdfr 299541016Sdfr static unsigned char rate[] = { 200, 100, 80, }; 299641016Sdfr KBDC kbdc = sc->kbdc; 299741016Sdfr int id; 299841016Sdfr int i; 299941016Sdfr 300041016Sdfr /* the special sequence to enable the third button and the roller. */ 300141016Sdfr for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 300241016Sdfr if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 300341016Sdfr return FALSE; 300441016Sdfr } 300541016Sdfr /* the device will give the genuine ID only after the above sequence */ 300641016Sdfr id = get_aux_id(kbdc); 300741016Sdfr if (id != PSM_INTELLI_ID) 300841016Sdfr return FALSE; 300941016Sdfr 301041016Sdfr sc->hw.hwid = id; 301141016Sdfr sc->hw.buttons = 3; 301241016Sdfr 301341016Sdfr return TRUE; 301441016Sdfr} 301541016Sdfr 301658230Syokota/* A4 Tech 4D Mouse */ 301758230Syokotastatic int 301858230Syokotaenable_4dmouse(struct psm_softc *sc) 301958230Syokota{ 302058230Syokota /* 302158230Syokota * Newer wheel mice from A4 Tech may use the 4D+ protocol. 302258230Syokota */ 302358230Syokota 302458230Syokota static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 302558230Syokota KBDC kbdc = sc->kbdc; 302658230Syokota int id; 302758230Syokota int i; 302858230Syokota 302958230Syokota for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 303058230Syokota if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 303158230Syokota return FALSE; 303258230Syokota } 303358230Syokota id = get_aux_id(kbdc); 303458230Syokota /* 303558923Syokota * WinEasy 4D, 4 Way Scroll 4D: 6 303658230Syokota * Cable-Free 4D: 8 (4DPLUS) 303758923Syokota * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS) 303858230Syokota */ 303958230Syokota if (id != PSM_4DMOUSE_ID) 304058230Syokota return FALSE; 304158230Syokota 304258230Syokota sc->hw.hwid = id; 304358230Syokota sc->hw.buttons = 3; /* XXX some 4D mice have 4? */ 304458230Syokota 304558230Syokota return TRUE; 304658230Syokota} 304758230Syokota 304858230Syokota/* A4 Tech 4D+ Mouse */ 304958230Syokotastatic int 305058230Syokotaenable_4dplus(struct psm_softc *sc) 305158230Syokota{ 305258230Syokota /* 305358230Syokota * Newer wheel mice from A4 Tech seem to use this protocol. 305458230Syokota * Older models are recognized as either 4D Mouse or IntelliMouse. 305558230Syokota */ 305658230Syokota KBDC kbdc = sc->kbdc; 305758230Syokota int id; 305858230Syokota 305958230Syokota /* 306058230Syokota * enable_4dmouse() already issued the following ID sequence... 306158230Syokota static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 306258230Syokota int i; 306358230Syokota 306458230Syokota for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 306558230Syokota if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 306658230Syokota return FALSE; 306758230Syokota } 306858230Syokota */ 306958230Syokota 307058230Syokota id = get_aux_id(kbdc); 3071117478Smikeh switch (id) { 3072117478Smikeh case PSM_4DPLUS_ID: 3073117478Smikeh sc->hw.buttons = 4; 3074117478Smikeh break; 3075117478Smikeh case PSM_4DPLUS_RFSW35_ID: 3076117478Smikeh sc->hw.buttons = 3; 3077117478Smikeh break; 3078117478Smikeh default: 3079117478Smikeh return FALSE; 3080117478Smikeh } 308158230Syokota 308258230Syokota sc->hw.hwid = id; 308358230Syokota 308458230Syokota return TRUE; 308558230Syokota} 308658230Syokota 3087132865Snjl/* Synaptics Touchpad */ 3088132865Snjlstatic int 3089132865Snjlenable_synaptics(struct psm_softc *sc) 3090132865Snjl{ 3091132865Snjl int status[3]; 3092132865Snjl KBDC kbdc; 3093132865Snjl 3094132865Snjl kbdc = sc->kbdc; 3095132865Snjl disable_aux_dev(kbdc); 3096132865Snjl 3097132865Snjl /* Just to be on the safe side */ 3098132865Snjl set_mouse_scaling(kbdc, 1); 3099132865Snjl 3100133295Sphilip /* Identify the Touchpad version */ 3101132865Snjl if (mouse_ext_command(kbdc, 0) == 0) 3102132865Snjl return (FALSE); 3103132865Snjl if (get_mouse_status(kbdc, status, 0, 3) != 3) 3104132865Snjl return (FALSE); 3105132865Snjl if (status[1] != 0x47) 3106132865Snjl return (FALSE); 3107132865Snjl 3108132865Snjl sc->synhw.infoMinor = status[0]; 3109132865Snjl sc->synhw.infoMajor = status[2] & 0x0f; 3110133295Sphilip 3111133295Sphilip if (verbose >= 2) 3112133295Sphilip printf("Synaptics Touchpad v%d.%d\n", 3113133295Sphilip sc->synhw.infoMajor, sc->synhw.infoMinor); 3114133295Sphilip 3115132865Snjl if (sc->synhw.infoMajor < 4) { 3116133295Sphilip printf(" Unsupported (pre-v4) Touchpad detected\n"); 3117132865Snjl return (FALSE); 3118132865Snjl } 3119132865Snjl 3120133295Sphilip /* Get the Touchpad model information */ 3121132865Snjl if (mouse_ext_command(kbdc, 3) == 0) 3122132865Snjl return (FALSE); 3123132865Snjl if (get_mouse_status(kbdc, status, 0, 3) != 3) 3124132865Snjl return (FALSE); 3125132865Snjl if ((status[1] & 0x01) != 0) { 3126133295Sphilip printf(" Failed to read model information\n"); 3127132865Snjl return (FALSE); 3128132865Snjl } 3129132865Snjl 3130132865Snjl sc->synhw.infoRot180 = (status[0] & 0x80) >> 7; 3131132865Snjl sc->synhw.infoPortrait = (status[0] & 0x40) >> 6; 3132132865Snjl sc->synhw.infoSensor = status[0] & 0x3f; 3133132865Snjl sc->synhw.infoHardware = (status[1] & 0xfe) >> 1; 3134132865Snjl sc->synhw.infoNewAbs = (status[2] & 0x80) >> 7; 3135132865Snjl sc->synhw.capPen = (status[2] & 0x40) >> 6; 3136132865Snjl sc->synhw.infoSimplC = (status[2] & 0x20) >> 5; 3137132865Snjl sc->synhw.infoGeometry = status[2] & 0x0f; 3138133295Sphilip 3139132865Snjl if (verbose >= 2) { 3140133295Sphilip printf(" Model information:\n"); 3141133295Sphilip printf(" infoRot180: %d\n", sc->synhw.infoRot180); 3142133295Sphilip printf(" infoPortrait: %d\n", sc->synhw.infoPortrait); 3143133295Sphilip printf(" infoSensor: %d\n", sc->synhw.infoSensor); 3144133295Sphilip printf(" infoHardware: %d\n", sc->synhw.infoHardware); 3145133295Sphilip printf(" infoNewAbs: %d\n", sc->synhw.infoNewAbs); 3146133295Sphilip printf(" capPen: %d\n", sc->synhw.capPen); 3147133295Sphilip printf(" infoSimplC: %d\n", sc->synhw.infoSimplC); 3148133295Sphilip printf(" infoGeometry: %d\n", sc->synhw.infoGeometry); 3149132865Snjl } 3150132865Snjl 3151133295Sphilip /* Read the extended capability bits */ 3152132865Snjl if (mouse_ext_command(kbdc, 2) == 0) 3153132865Snjl return (FALSE); 3154132865Snjl if (get_mouse_status(kbdc, status, 0, 3) != 3) 3155132865Snjl return (FALSE); 3156132865Snjl if (status[1] != 0x47) { 3157133295Sphilip printf(" Failed to read extended capability bits\n"); 3158132865Snjl return (FALSE); 3159132865Snjl } 3160132865Snjl 3161133295Sphilip /* Set the different capabilities when they exist */ 3162133295Sphilip if ((status[0] & 0x80) >> 7) { 3163133295Sphilip sc->synhw.capExtended = (status[0] & 0x80) >> 7; 3164133295Sphilip sc->synhw.capPassthrough = (status[2] & 0x80) >> 7; 3165133295Sphilip sc->synhw.capSleep = (status[2] & 0x10) >> 4; 3166133295Sphilip sc->synhw.capFourButtons = (status[2] & 0x08) >> 3; 3167133295Sphilip sc->synhw.capMultiFinger = (status[2] & 0x02) >> 1; 3168133295Sphilip sc->synhw.capPalmDetect = (status[2] & 0x01); 3169133295Sphilip 3170133295Sphilip if (verbose >= 2) { 3171133295Sphilip printf(" Extended capabilities:\n"); 3172133295Sphilip printf(" capExtended: %d\n", sc->synhw.capExtended); 3173133295Sphilip printf(" capPassthrough: %d\n", sc->synhw.capPassthrough); 3174133295Sphilip printf(" capSleep: %d\n", sc->synhw.capSleep); 3175133295Sphilip printf(" capFourButtons: %d\n", sc->synhw.capFourButtons); 3176133295Sphilip printf(" capMultiFinger: %d\n", sc->synhw.capMultiFinger); 3177133295Sphilip printf(" capPalmDetect: %d\n", sc->synhw.capPalmDetect); 3178133295Sphilip } 3179133295Sphilip } else { 3180133295Sphilip sc->synhw.capExtended = 0; 3181133295Sphilip 3182133295Sphilip if (verbose >= 2) 3183133295Sphilip printf(" No extended capabilities\n"); 3184133295Sphilip } 3185133295Sphilip 3186133295Sphilip /* 3187133295Sphilip * Read the mode byte 3188133295Sphilip * 3189133295Sphilip * XXX: Note the Synaptics documentation also defines the first 3190133295Sphilip * byte of the response to this query to be a constant 0x3b, this 3191133295Sphilip * does not appear to be true for Touchpads with guest devices. 3192133295Sphilip */ 3193132865Snjl if (mouse_ext_command(kbdc, 1) == 0) 3194132865Snjl return (FALSE); 3195132865Snjl if (get_mouse_status(kbdc, status, 0, 3) != 3) 3196132865Snjl return (FALSE); 3197133295Sphilip if (status[1] != 0x47) { 3198133295Sphilip printf(" Failed to read mode byte\n"); 3199132865Snjl return (FALSE); 3200132865Snjl } 3201132865Snjl 3202133295Sphilip /* Set the mode byte -- request wmode where available */ 3203133295Sphilip if (sc->synhw.capExtended) 3204133295Sphilip mouse_ext_command(kbdc, 0xc1); 3205133295Sphilip else 3206133295Sphilip mouse_ext_command(kbdc, 0xc0); 3207132865Snjl 3208133295Sphilip /* Reset the sampling rate */ 3209132865Snjl set_mouse_sampling_rate(kbdc, 20); 3210132865Snjl 3211133296Sphilip /* 3212133296Sphilip * Report the correct number of buttons 3213133296Sphilip * 3214133296Sphilip * XXX: I'm not sure this is used anywhere. 3215133296Sphilip */ 3216133296Sphilip if (sc->synhw.capExtended && sc->synhw.capFourButtons) 3217133296Sphilip sc->hw.buttons = 4; 3218133296Sphilip else 3219133296Sphilip sc->hw.buttons = 3; 3220133296Sphilip 3221132865Snjl return (TRUE); 3222132865Snjl} 3223133295Sphilip 322449965Syokota/* Interlink electronics VersaPad */ 322549965Syokotastatic int 322649965Syokotaenable_versapad(struct psm_softc *sc) 322749965Syokota{ 322849965Syokota KBDC kbdc = sc->kbdc; 322949965Syokota int data[3]; 323049965Syokota 323149965Syokota set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */ 323249965Syokota set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */ 323349965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 323449965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 323549965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 323649965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 323749965Syokota if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */ 323849965Syokota return FALSE; 323949965Syokota if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */ 324049965Syokota return FALSE; 324149965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 324249965Syokota 324358230Syokota sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 324458230Syokota 324549965Syokota return TRUE; /* PS/2 absolute mode */ 324649965Syokota} 324749965Syokota 324841016Sdfrstatic int 324954629Syokotapsmresume(device_t dev) 325041016Sdfr{ 325154629Syokota struct psm_softc *sc = device_get_softc(dev); 325254629Syokota int unit = device_get_unit(dev); 325384880Syokota int err; 325441016Sdfr 325541016Sdfr if (verbose >= 2) 325654629Syokota log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit); 325741016Sdfr 325858230Syokota if (!(sc->config & PSM_CONFIG_HOOKRESUME)) 325958230Syokota return (0); 326058230Syokota 326184880Syokota err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND); 326241016Sdfr 326341016Sdfr if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) { 326441016Sdfr /* 326541016Sdfr * Release the blocked process; it must be notified that the device 326641016Sdfr * cannot be accessed anymore. 326741016Sdfr */ 326841016Sdfr sc->state &= ~PSM_ASLP; 3269111748Sdes wakeup(sc); 327041016Sdfr } 327141016Sdfr 327241016Sdfr if (verbose >= 2) 327354629Syokota log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit); 327441016Sdfr 327541016Sdfr return (err); 327641016Sdfr} 327741016Sdfr 327852997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0); 327983147Syokota 328083147Syokota/* 328183147Syokota * This sucks up assignments from PNPBIOS and ACPI. 328283147Syokota */ 328383147Syokota 328483931Syokota/* 328583931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may 328683931Syokota * appear BEFORE the AT keyboard controller. As the PS/2 mouse device 328783931Syokota * can be probed and attached only after the AT keyboard controller is 328883931Syokota * attached, we shall quietly reserve the IRQ resource for later use. 328983931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller, 329083931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging 329183931Syokota * under the keyboard controller, then probe and attach it. 329283931Syokota */ 329383147Syokota 329483147Syokotastatic devclass_t psmcpnp_devclass; 329583147Syokota 329683147Syokotastatic device_probe_t psmcpnp_probe; 329783147Syokotastatic device_attach_t psmcpnp_attach; 329883147Syokota 329983147Syokotastatic device_method_t psmcpnp_methods[] = { 330083147Syokota DEVMETHOD(device_probe, psmcpnp_probe), 330183147Syokota DEVMETHOD(device_attach, psmcpnp_attach), 330283147Syokota 330383147Syokota { 0, 0 } 330483147Syokota}; 330583147Syokota 330683147Syokotastatic driver_t psmcpnp_driver = { 330783147Syokota PSMCPNP_DRIVER_NAME, 330883147Syokota psmcpnp_methods, 330983147Syokota 1, /* no softc */ 331083147Syokota}; 331183147Syokota 331283147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = { 331388188Ssheldonh { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */ 331483147Syokota { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */ 331583147Syokota { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */ 3316117117Smikeh { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */ 331783492Syokota { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */ 331884407Stakawata { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */ 3319109679Shsu { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */ 3320109679Shsu { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */ 3321109710Smarcel { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */ 3322109679Shsu { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */ 332383147Syokota { 0 } 332483147Syokota}; 332583147Syokota 332683147Syokotastatic int 332783147Syokotacreate_a_copy(device_t atkbdc, device_t me) 332883147Syokota{ 332983147Syokota device_t psm; 333083147Syokota u_long irq; 333183147Syokota 333283931Syokota /* find the PS/2 mouse device instance under the keyboard controller */ 333383931Syokota psm = device_find_child(atkbdc, PSM_DRIVER_NAME, 333483931Syokota device_get_unit(atkbdc)); 333583147Syokota if (psm == NULL) 333683147Syokota return ENXIO; 333783931Syokota if (device_get_state(psm) != DS_NOTPRESENT) 333883931Syokota return 0; 333983147Syokota 334083931Syokota /* move our resource to the found device */ 334183931Syokota irq = bus_get_resource_start(me, SYS_RES_IRQ, 0); 334283147Syokota bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 334383147Syokota 334483147Syokota /* ...then probe and attach it */ 334583147Syokota return device_probe_and_attach(psm); 334683147Syokota} 334783147Syokota 334883147Syokotastatic int 334983147Syokotapsmcpnp_probe(device_t dev) 335083147Syokota{ 335183931Syokota struct resource *res; 335283931Syokota u_long irq; 335383931Syokota int rid; 335483147Syokota 335583147Syokota if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids)) 335683147Syokota return ENXIO; 335783147Syokota 335883931Syokota /* 335983931Syokota * The PnP BIOS and ACPI are supposed to assign an IRQ (12) 336083931Syokota * to the PS/2 mouse device node. But, some buggy PnP BIOS 336183931Syokota * declares the PS/2 mouse device node without an IRQ resource! 336283931Syokota * If this happens, we shall refer to device hints. 336383931Syokota * If we still don't find it there, use a hardcoded value... XXX 336483931Syokota */ 336583931Syokota rid = 0; 336683931Syokota irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid); 336783931Syokota if (irq <= 0) { 336883931Syokota if (resource_long_value(PSM_DRIVER_NAME, 336983931Syokota device_get_unit(dev), "irq", &irq) != 0) 337083931Syokota irq = 12; /* XXX */ 337183931Syokota device_printf(dev, "irq resource info is missing; " 337283931Syokota "assuming irq %ld\n", irq); 337383931Syokota bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1); 337483931Syokota } 3375127135Snjl res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 3376127135Snjl RF_SHAREABLE); 337783931Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, res); 337883147Syokota 337983147Syokota /* keep quiet */ 338083147Syokota if (!bootverbose) 338183147Syokota device_quiet(dev); 338283492Syokota 338383931Syokota return ((res == NULL) ? ENXIO : 0); 338483147Syokota} 338583147Syokota 338683147Syokotastatic int 338783147Syokotapsmcpnp_attach(device_t dev) 338883147Syokota{ 338983492Syokota device_t atkbdc; 339083931Syokota int rid; 339183147Syokota 339283931Syokota /* find the keyboard controller, which may be on acpi* or isa* bus */ 339383931Syokota atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME), 339483931Syokota device_get_unit(dev)); 339583931Syokota if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) { 339683492Syokota create_a_copy(atkbdc, dev); 339783931Syokota } else { 339883931Syokota /* 339983931Syokota * If we don't have the AT keyboard controller yet, 340083931Syokota * just reserve the IRQ for later use... 340183931Syokota * (See psmidentify() above.) 340283931Syokota */ 340383931Syokota rid = 0; 3404127135Snjl bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE); 340583931Syokota } 340683492Syokota 340783147Syokota return 0; 340883147Syokota} 340983147Syokota 3410123442Salfred/* 3411123442Salfred * Return true if 'now' is earlier than (start + (secs.usecs)). 3412123442Salfred * Now may be NULL and the function will fetch the current time from 3413123442Salfred * getmicrouptime(), or a cached 'now' can be passed in. 3414123442Salfred * All values should be numbers derived from getmicrouptime(). 3415123442Salfred */ 3416123442Salfredstatic int 3417123442Salfredtimeelapsed(start, secs, usecs, now) 3418123442Salfred const struct timeval *start, *now; 3419123442Salfred int secs, usecs; 3420123442Salfred{ 3421123442Salfred struct timeval snow, tv; 3422123442Salfred 3423123442Salfred /* if there is no 'now' passed in, the get it as a convience. */ 3424123442Salfred if (now == NULL) { 3425123442Salfred getmicrouptime(&snow); 3426123442Salfred now = &snow; 3427123442Salfred } 3428123442Salfred 3429123442Salfred tv.tv_sec = secs; 3430123442Salfred tv.tv_usec = usecs; 3431123442Salfred timevaladd(&tv, start); 3432123442Salfred return (timevalcmp(&tv, now, <)); 3433123442Salfred} 3434123442Salfred 343583147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0); 343683147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0); 3437