psm.c revision 92756
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 * 2350477Speter * $FreeBSD: head/sys/dev/atkbdc/psm.c 92756 2002-03-20 05:14:42Z alfred $ 2441016Sdfr */ 2541016Sdfr 2641016Sdfr/* 2741016Sdfr * Ported to 386bsd Oct 17, 1992 2841016Sdfr * Sandi Donno, Computer Science, University of Cape Town, South Africa 2941016Sdfr * Please send bug reports to sandi@cs.uct.ac.za 3041016Sdfr * 3141016Sdfr * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca - 3241016Sdfr * although I was only partially successful in getting the alpha release 3341016Sdfr * of his "driver for the Logitech and ATI Inport Bus mice for use with 3441016Sdfr * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless 3541016Sdfr * found his code to be an invaluable reference when porting this driver 3641016Sdfr * to 386bsd. 3741016Sdfr * 3841016Sdfr * Further modifications for latest 386BSD+patchkit and port to NetBSD, 3941016Sdfr * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993 4041016Sdfr * 4141016Sdfr * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by 4241016Sdfr * Andrew Herbert - 12 June 1993 4341016Sdfr * 4441016Sdfr * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu> 4541016Sdfr * - 13 June 1993 4641016Sdfr * 4741016Sdfr * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp> 4841016Sdfr * - 24 October 1993 4941016Sdfr * 5041016Sdfr * Hardware access routines and probe logic rewritten by 5141016Sdfr * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp> 5241016Sdfr * - 3, 14, 22 October 1996. 5341016Sdfr * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'... 5441016Sdfr * - 14, 30 November 1996. Uses `kbdio.c'. 5541016Sdfr * - 13 December 1996. Uses queuing version of `kbdio.c'. 5641016Sdfr * - January/February 1997. Tweaked probe logic for 5741016Sdfr * HiNote UltraII/Latitude/Armada laptops. 5841016Sdfr * - 30 July 1997. Added APM support. 5941016Sdfr * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX). 6041016Sdfr * Improved sync check logic. 6141016Sdfr * Vendor specific support routines. 6241016Sdfr */ 6341016Sdfr 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> 7784880Syokota#include <sys/time.h> 7841016Sdfr#include <sys/uio.h> 7941016Sdfr 8041016Sdfr#include <machine/limits.h> 8166860Sphk#include <sys/mouse.h> 8241181Sdfr#include <machine/resource.h> 8341016Sdfr 8441016Sdfr#include <isa/isavar.h> 8543105Sdfr#include <dev/kbd/atkbdcreg.h> 8641016Sdfr 8741016Sdfr/* 8841016Sdfr * Driver specific options: the following options may be set by 8941016Sdfr * `options' statements in the kernel configuration file. 9041016Sdfr */ 9141016Sdfr 9241016Sdfr/* debugging */ 9341016Sdfr#ifndef PSM_DEBUG 9441016Sdfr#define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */ 9541016Sdfr#endif 9641016Sdfr 9784880Syokota#ifndef PSM_SYNCERR_THRESHOLD1 9884880Syokota#define PSM_SYNCERR_THRESHOLD1 20 9984880Syokota#endif 10084880Syokota 10184880Syokota#ifndef PSM_INPUT_TIMEOUT 10284880Syokota#define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */ 10384880Syokota#endif 10484880Syokota 10541016Sdfr/* end of driver specific options */ 10641016Sdfr 10783492Syokota#define PSM_DRIVER_NAME "psm" 10883931Syokota#define PSMCPNP_DRIVER_NAME "psmcpnp" 10983492Syokota 11041016Sdfr/* input queue */ 11141016Sdfr#define PSM_BUFSIZE 960 11241016Sdfr#define PSM_SMALLBUFSIZE 240 11341016Sdfr 11441016Sdfr/* operation levels */ 11541016Sdfr#define PSM_LEVEL_BASE 0 11641016Sdfr#define PSM_LEVEL_STANDARD 1 11741016Sdfr#define PSM_LEVEL_NATIVE 2 11841016Sdfr#define PSM_LEVEL_MIN PSM_LEVEL_BASE 11941016Sdfr#define PSM_LEVEL_MAX PSM_LEVEL_NATIVE 12041016Sdfr 12148778Syokota/* Logitech PS2++ protocol */ 12248778Syokota#define MOUSE_PS2PLUS_CHECKBITS(b) \ 12348778Syokota ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f)) 12448778Syokota#define MOUSE_PS2PLUS_PACKET_TYPE(b) \ 12548778Syokota (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4)) 12648778Syokota 12741016Sdfr/* some macros */ 12841016Sdfr#define PSM_UNIT(dev) (minor(dev) >> 1) 12941016Sdfr#define PSM_NBLOCKIO(dev) (minor(dev) & 1) 13041016Sdfr#define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1)) 13141016Sdfr 13241016Sdfr#ifndef max 13341016Sdfr#define max(x,y) ((x) > (y) ? (x) : (y)) 13441016Sdfr#endif 13541016Sdfr#ifndef min 13641016Sdfr#define min(x,y) ((x) < (y) ? (x) : (y)) 13741016Sdfr#endif 13841016Sdfr 13948778Syokota#define abs(x) (((x) < 0) ? -(x) : (x)) 14048778Syokota 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 14941016Sdfr/* driver control block */ 15041016Sdfrstruct psm_softc { /* Driver status information */ 15184880Syokota int unit; 15241016Sdfr struct selinfo rsel; /* Process selecting for Input */ 15341016Sdfr unsigned char state; /* Mouse driver state */ 15441016Sdfr int config; /* driver configuration flags */ 15541016Sdfr int flags; /* other flags */ 15641016Sdfr KBDC kbdc; /* handle to access the keyboard controller */ 15758230Syokota struct resource *intr; /* IRQ resource */ 15858230Syokota void *ih; /* interrupt handle */ 15941016Sdfr mousehw_t hw; /* hardware information */ 16041016Sdfr mousemode_t mode; /* operation mode */ 16141016Sdfr mousemode_t dflt_mode; /* default operation mode */ 16241016Sdfr mousestatus_t status; /* accumulated mouse movement */ 16341016Sdfr ringbuf_t queue; /* mouse status queue */ 16441016Sdfr unsigned char ipacket[16]; /* interim input buffer */ 16541016Sdfr int inputbytes; /* # of bytes in the input buffer */ 16641016Sdfr int button; /* the latest button state */ 16749965Syokota int xold; /* previous absolute X position */ 16849965Syokota int yold; /* previous absolute Y position */ 16984880Syokota int syncerrors; 17084880Syokota struct timeval inputtimeout; 17158230Syokota int watchdog; /* watchdog timer flag */ 17258230Syokota struct callout_handle callout; /* watchdog timer call out */ 17358230Syokota dev_t dev; 17458230Syokota dev_t bdev; 17541016Sdfr}; 17641016Sdfrdevclass_t psm_devclass; 17741016Sdfr#define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit)) 17841016Sdfr 17941016Sdfr/* driver state flags (state) */ 18041016Sdfr#define PSM_VALID 0x80 18141016Sdfr#define PSM_OPEN 1 /* Device is open */ 18241016Sdfr#define PSM_ASLP 2 /* Waiting for mouse data */ 18341016Sdfr 18441016Sdfr/* driver configuration flags (config) */ 18541016Sdfr#define PSM_CONFIG_RESOLUTION 0x000f /* resolution */ 18641016Sdfr#define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */ 18741016Sdfr#define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */ 18845789Speter#define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */ 18945789Speter#define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */ 19045789Speter#define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */ 19145789Speter#define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */ 19258230Syokota#define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */ 19358230Syokota#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */ 19469439Syokota#define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */ 19541016Sdfr 19641016Sdfr#define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \ 19741016Sdfr | PSM_CONFIG_ACCEL \ 19845789Speter | PSM_CONFIG_NOCHECKSYNC \ 19969439Syokota | PSM_CONFIG_SYNCHACK \ 20045789Speter | PSM_CONFIG_NOIDPROBE \ 20145789Speter | PSM_CONFIG_NORESET \ 20245789Speter | PSM_CONFIG_FORCETAP \ 20358230Syokota | PSM_CONFIG_IGNPORTERROR \ 20458230Syokota | PSM_CONFIG_HOOKRESUME \ 20558230Syokota | PSM_CONFIG_INITAFTERSUSPEND) 20641016Sdfr 20741016Sdfr/* other flags (flags) */ 20849965Syokota#define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */ 20941016Sdfr 21041016Sdfr/* for backward compatibility */ 21141016Sdfr#define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t) 21241016Sdfr#define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t) 21341016Sdfr#define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t) 21441016Sdfr 21541016Sdfrtypedef struct old_mousehw { 21641016Sdfr int buttons; 21741016Sdfr int iftype; 21841016Sdfr int type; 21941016Sdfr int hwid; 22041016Sdfr} old_mousehw_t; 22141016Sdfr 22241016Sdfrtypedef struct old_mousemode { 22341016Sdfr int protocol; 22441016Sdfr int rate; 22541016Sdfr int resolution; 22641016Sdfr int accelfactor; 22741016Sdfr} old_mousemode_t; 22841016Sdfr 22941016Sdfr/* packet formatting function */ 23092756Salfredtypedef int packetfunc_t(struct psm_softc *, unsigned char *, 23192756Salfred int *, int, mousestatus_t *); 23241016Sdfr 23341016Sdfr/* function prototypes */ 23492756Salfredstatic void psmidentify(driver_t *, device_t); 23592756Salfredstatic int psmprobe(device_t); 23692756Salfredstatic int psmattach(device_t); 23792756Salfredstatic int psmdetach(device_t); 23892756Salfredstatic int psmresume(device_t); 23941016Sdfr 24041016Sdfrstatic d_open_t psmopen; 24141016Sdfrstatic d_close_t psmclose; 24241016Sdfrstatic d_read_t psmread; 24341016Sdfrstatic d_ioctl_t psmioctl; 24441016Sdfrstatic d_poll_t psmpoll; 24541016Sdfr 24692756Salfredstatic int enable_aux_dev(KBDC); 24792756Salfredstatic int disable_aux_dev(KBDC); 24892756Salfredstatic int get_mouse_status(KBDC, int *, int, int); 24992756Salfredstatic int get_aux_id(KBDC); 25092756Salfredstatic int set_mouse_sampling_rate(KBDC, int); 25192756Salfredstatic int set_mouse_scaling(KBDC, int); 25292756Salfredstatic int set_mouse_resolution(KBDC, int); 25392756Salfredstatic int set_mouse_mode(KBDC); 25492756Salfredstatic int get_mouse_buttons(KBDC); 25592756Salfredstatic int is_a_mouse(int); 25692756Salfredstatic void recover_from_error(KBDC); 25792756Salfredstatic int restore_controller(KBDC, int); 25892756Salfredstatic int doinitialize(struct psm_softc *, mousemode_t *); 25992756Salfredstatic int doopen(struct psm_softc *, int); 26092756Salfredstatic int reinitialize(struct psm_softc *, int); 26192756Salfredstatic char *model_name(int); 26292756Salfredstatic void psmintr(void *); 26392756Salfredstatic void psmtimeout(void *); 26441016Sdfr 26541016Sdfr/* vendor specific features */ 26692756Salfredtypedef int probefunc_t(struct psm_softc *); 26741016Sdfr 26892756Salfredstatic int mouse_id_proc1(KBDC, int, int, int *); 26992756Salfredstatic int mouse_ext_command(KBDC, int); 27041016Sdfrstatic probefunc_t enable_groller; 27141016Sdfrstatic probefunc_t enable_gmouse; 27241016Sdfrstatic probefunc_t enable_aglide; 27341016Sdfrstatic probefunc_t enable_kmouse; 27458230Syokotastatic probefunc_t enable_msexplorer; 27541016Sdfrstatic probefunc_t enable_msintelli; 27658230Syokotastatic probefunc_t enable_4dmouse; 27758230Syokotastatic probefunc_t enable_4dplus; 27841016Sdfrstatic probefunc_t enable_mmanplus; 27949965Syokotastatic probefunc_t enable_versapad; 28092756Salfredstatic int tame_mouse(struct psm_softc *, mousestatus_t *, unsigned char *); 28141016Sdfr 28241016Sdfrstatic struct { 28341016Sdfr int model; 28441016Sdfr unsigned char syncmask; 28541016Sdfr int packetsize; 28641016Sdfr probefunc_t *probefunc; 28741016Sdfr} vendortype[] = { 28858230Syokota /* 28958230Syokota * WARNING: the order of probe is very important. Don't mess it 29058230Syokota * unless you know what you are doing. 29158230Syokota */ 29241016Sdfr { MOUSE_MODEL_NET, /* Genius NetMouse */ 29358230Syokota 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, }, 29441016Sdfr { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */ 29541016Sdfr 0xc8, 6, enable_groller, }, 29658230Syokota { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */ 29758230Syokota 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, }, 29858230Syokota { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */ 29958230Syokota 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, }, 30058230Syokota { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */ 30158230Syokota 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, }, 30258230Syokota { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */ 30358230Syokota 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, }, 30458230Syokota { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */ 30558230Syokota 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, }, 30641016Sdfr { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */ 30741016Sdfr 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, }, 30841016Sdfr { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */ 30941016Sdfr 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, }, 31049965Syokota { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */ 31149965Syokota 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, }, 31241016Sdfr { MOUSE_MODEL_GENERIC, 31341016Sdfr 0xc0, MOUSE_PS2_PACKETSIZE, NULL, }, 31441016Sdfr}; 31563951Syokota#define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1) 31641016Sdfr 31741016Sdfr/* device driver declarateion */ 31841016Sdfrstatic device_method_t psm_methods[] = { 31941016Sdfr /* Device interface */ 32083147Syokota DEVMETHOD(device_identify, psmidentify), 32141016Sdfr DEVMETHOD(device_probe, psmprobe), 32241016Sdfr DEVMETHOD(device_attach, psmattach), 32358230Syokota DEVMETHOD(device_detach, psmdetach), 32454629Syokota DEVMETHOD(device_resume, psmresume), 32541016Sdfr 32641016Sdfr { 0, 0 } 32741016Sdfr}; 32841016Sdfr 32941016Sdfrstatic driver_t psm_driver = { 33083492Syokota PSM_DRIVER_NAME, 33141016Sdfr psm_methods, 33241016Sdfr sizeof(struct psm_softc), 33341016Sdfr}; 33441016Sdfr 33541016Sdfr#define CDEV_MAJOR 21 33641016Sdfr 33747625Sphkstatic struct cdevsw psm_cdevsw = { 33847625Sphk /* open */ psmopen, 33947625Sphk /* close */ psmclose, 34047625Sphk /* read */ psmread, 34147625Sphk /* write */ nowrite, 34247625Sphk /* ioctl */ psmioctl, 34347625Sphk /* poll */ psmpoll, 34447625Sphk /* mmap */ nommap, 34547625Sphk /* strategy */ nostrategy, 34683492Syokota /* name */ PSM_DRIVER_NAME, 34747625Sphk /* maj */ CDEV_MAJOR, 34847625Sphk /* dump */ nodump, 34947625Sphk /* psize */ nopsize, 35047625Sphk /* flags */ 0, 35141016Sdfr}; 35241016Sdfr 35341016Sdfr/* debug message level */ 35441016Sdfrstatic int verbose = PSM_DEBUG; 35541016Sdfr 35641016Sdfr/* device I/O routines */ 35741016Sdfrstatic int 35841016Sdfrenable_aux_dev(KBDC kbdc) 35941016Sdfr{ 36041016Sdfr int res; 36141016Sdfr 36241016Sdfr res = send_aux_command(kbdc, PSMC_ENABLE_DEV); 36341016Sdfr if (verbose >= 2) 36441016Sdfr log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res); 36541016Sdfr 36641016Sdfr return (res == PSM_ACK); 36741016Sdfr} 36841016Sdfr 36941016Sdfrstatic int 37041016Sdfrdisable_aux_dev(KBDC kbdc) 37141016Sdfr{ 37241016Sdfr int res; 37341016Sdfr 37441016Sdfr res = send_aux_command(kbdc, PSMC_DISABLE_DEV); 37541016Sdfr if (verbose >= 2) 37641016Sdfr log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res); 37741016Sdfr 37841016Sdfr return (res == PSM_ACK); 37941016Sdfr} 38041016Sdfr 38141016Sdfrstatic int 38241016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len) 38341016Sdfr{ 38441016Sdfr int cmd; 38541016Sdfr int res; 38641016Sdfr int i; 38741016Sdfr 38841016Sdfr switch (flag) { 38941016Sdfr case 0: 39041016Sdfr default: 39141016Sdfr cmd = PSMC_SEND_DEV_STATUS; 39241016Sdfr break; 39341016Sdfr case 1: 39441016Sdfr cmd = PSMC_SEND_DEV_DATA; 39541016Sdfr break; 39641016Sdfr } 39741016Sdfr empty_aux_buffer(kbdc, 5); 39841016Sdfr res = send_aux_command(kbdc, cmd); 39941016Sdfr if (verbose >= 2) 40041016Sdfr log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n", 40141016Sdfr (flag == 1) ? "DATA" : "STATUS", res); 40241016Sdfr if (res != PSM_ACK) 40341016Sdfr return 0; 40441016Sdfr 40541016Sdfr for (i = 0; i < len; ++i) { 40641016Sdfr status[i] = read_aux_data(kbdc); 40741016Sdfr if (status[i] < 0) 40841016Sdfr break; 40941016Sdfr } 41041016Sdfr 41141016Sdfr if (verbose) { 41241016Sdfr log(LOG_DEBUG, "psm: %s %02x %02x %02x\n", 41341016Sdfr (flag == 1) ? "data" : "status", status[0], status[1], status[2]); 41441016Sdfr } 41541016Sdfr 41641016Sdfr return i; 41741016Sdfr} 41841016Sdfr 41941016Sdfrstatic int 42041016Sdfrget_aux_id(KBDC kbdc) 42141016Sdfr{ 42241016Sdfr int res; 42341016Sdfr int id; 42441016Sdfr 42541016Sdfr empty_aux_buffer(kbdc, 5); 42641016Sdfr res = send_aux_command(kbdc, PSMC_SEND_DEV_ID); 42741016Sdfr if (verbose >= 2) 42841016Sdfr log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res); 42941016Sdfr if (res != PSM_ACK) 43041016Sdfr return (-1); 43141016Sdfr 43241016Sdfr /* 10ms delay */ 43341016Sdfr DELAY(10000); 43441016Sdfr 43541016Sdfr id = read_aux_data(kbdc); 43641016Sdfr if (verbose >= 2) 43741016Sdfr log(LOG_DEBUG, "psm: device ID: %04x\n", id); 43841016Sdfr 43941016Sdfr return id; 44041016Sdfr} 44141016Sdfr 44241016Sdfrstatic int 44341016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate) 44441016Sdfr{ 44541016Sdfr int res; 44641016Sdfr 44741016Sdfr res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate); 44841016Sdfr if (verbose >= 2) 44941016Sdfr log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res); 45041016Sdfr 45141016Sdfr return ((res == PSM_ACK) ? rate : -1); 45241016Sdfr} 45341016Sdfr 45441016Sdfrstatic int 45541016Sdfrset_mouse_scaling(KBDC kbdc, int scale) 45641016Sdfr{ 45741016Sdfr int res; 45841016Sdfr 45941016Sdfr switch (scale) { 46041016Sdfr case 1: 46141016Sdfr default: 46241016Sdfr scale = PSMC_SET_SCALING11; 46341016Sdfr break; 46441016Sdfr case 2: 46541016Sdfr scale = PSMC_SET_SCALING21; 46641016Sdfr break; 46741016Sdfr } 46841016Sdfr res = send_aux_command(kbdc, scale); 46941016Sdfr if (verbose >= 2) 47041016Sdfr log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n", 47141016Sdfr (scale == PSMC_SET_SCALING21) ? "21" : "11", res); 47241016Sdfr 47341016Sdfr return (res == PSM_ACK); 47441016Sdfr} 47541016Sdfr 47641016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */ 47741016Sdfrstatic int 47841016Sdfrset_mouse_resolution(KBDC kbdc, int val) 47941016Sdfr{ 48041016Sdfr int res; 48141016Sdfr 48241016Sdfr res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val); 48341016Sdfr if (verbose >= 2) 48441016Sdfr log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res); 48541016Sdfr 48641016Sdfr return ((res == PSM_ACK) ? val : -1); 48741016Sdfr} 48841016Sdfr 48941016Sdfr/* 49041016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be 49141016Sdfr * re-enabled by calling `enable_aux_dev()' 49241016Sdfr */ 49341016Sdfrstatic int 49441016Sdfrset_mouse_mode(KBDC kbdc) 49541016Sdfr{ 49641016Sdfr int res; 49741016Sdfr 49841016Sdfr res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE); 49941016Sdfr if (verbose >= 2) 50041016Sdfr log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res); 50141016Sdfr 50241016Sdfr return (res == PSM_ACK); 50341016Sdfr} 50441016Sdfr 50541016Sdfrstatic int 50641016Sdfrget_mouse_buttons(KBDC kbdc) 50741016Sdfr{ 50841016Sdfr int c = 2; /* assume two buttons by default */ 50941016Sdfr int status[3]; 51041016Sdfr 51141016Sdfr /* 51241016Sdfr * NOTE: a special sequence to obtain Logitech Mouse specific 51341016Sdfr * information: set resolution to 25 ppi, set scaling to 1:1, set 51441016Sdfr * scaling to 1:1, set scaling to 1:1. Then the second byte of the 51541016Sdfr * mouse status bytes is the number of available buttons. 51641016Sdfr * Some manufactures also support this sequence. 51741016Sdfr */ 51841016Sdfr if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 51941016Sdfr return c; 52041016Sdfr if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) 52141016Sdfr && set_mouse_scaling(kbdc, 1) 52241016Sdfr && (get_mouse_status(kbdc, status, 0, 3) >= 3)) { 52341016Sdfr if (status[1] != 0) 52441016Sdfr return status[1]; 52541016Sdfr } 52641016Sdfr return c; 52741016Sdfr} 52841016Sdfr 52941016Sdfr/* misc subroutines */ 53041016Sdfr/* 53141016Sdfr * Someday, I will get the complete list of valid pointing devices and 53241016Sdfr * their IDs... XXX 53341016Sdfr */ 53441016Sdfrstatic int 53541016Sdfris_a_mouse(int id) 53641016Sdfr{ 53741016Sdfr#if 0 53841016Sdfr static int valid_ids[] = { 53941016Sdfr PSM_MOUSE_ID, /* mouse */ 54041016Sdfr PSM_BALLPOINT_ID, /* ballpoint device */ 54141016Sdfr PSM_INTELLI_ID, /* Intellimouse */ 54258230Syokota PSM_EXPLORER_ID, /* Intellimouse Explorer */ 54341016Sdfr -1 /* end of table */ 54441016Sdfr }; 54541016Sdfr int i; 54641016Sdfr 54741016Sdfr for (i = 0; valid_ids[i] >= 0; ++i) 54841016Sdfr if (valid_ids[i] == id) 54941016Sdfr return TRUE; 55041016Sdfr return FALSE; 55141016Sdfr#else 55241016Sdfr return TRUE; 55341016Sdfr#endif 55441016Sdfr} 55541016Sdfr 55641016Sdfrstatic char * 55741016Sdfrmodel_name(int model) 55841016Sdfr{ 55941016Sdfr static struct { 56041016Sdfr int model_code; 56141016Sdfr char *model_name; 56241016Sdfr } models[] = { 56358230Syokota { MOUSE_MODEL_NETSCROLL, "NetScroll" }, 56458230Syokota { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" }, 56541016Sdfr { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" }, 56641016Sdfr { MOUSE_MODEL_THINK, "ThinkingMouse" }, 56741016Sdfr { MOUSE_MODEL_INTELLI, "IntelliMouse" }, 56841016Sdfr { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" }, 56949965Syokota { MOUSE_MODEL_VERSAPAD, "VersaPad" }, 57058230Syokota { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" }, 57158230Syokota { MOUSE_MODEL_4D, "4D Mouse" }, 57258230Syokota { MOUSE_MODEL_4DPLUS, "4D+ Mouse" }, 57341016Sdfr { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" }, 57441016Sdfr { MOUSE_MODEL_UNKNOWN, NULL }, 57541016Sdfr }; 57641016Sdfr int i; 57741016Sdfr 57841016Sdfr for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) { 57941016Sdfr if (models[i].model_code == model) 58041016Sdfr return models[i].model_name; 58141016Sdfr } 58241016Sdfr return "Unknown"; 58341016Sdfr} 58441016Sdfr 58541016Sdfrstatic void 58641016Sdfrrecover_from_error(KBDC kbdc) 58741016Sdfr{ 58841016Sdfr /* discard anything left in the output buffer */ 58941016Sdfr empty_both_buffers(kbdc, 10); 59041016Sdfr 59141016Sdfr#if 0 59241016Sdfr /* 59341016Sdfr * NOTE: KBDC_RESET_KBD may not restore the communication between the 59441016Sdfr * keyboard and the controller. 59541016Sdfr */ 59641016Sdfr reset_kbd(kbdc); 59741016Sdfr#else 59841016Sdfr /* 59941016Sdfr * NOTE: somehow diagnostic and keyboard port test commands bring the 60041016Sdfr * keyboard back. 60141016Sdfr */ 60241016Sdfr if (!test_controller(kbdc)) 60341016Sdfr log(LOG_ERR, "psm: keyboard controller failed.\n"); 60441016Sdfr /* if there isn't a keyboard in the system, the following error is OK */ 60541016Sdfr if (test_kbd_port(kbdc) != 0) { 60641016Sdfr if (verbose) 60741016Sdfr log(LOG_ERR, "psm: keyboard port failed.\n"); 60841016Sdfr } 60941016Sdfr#endif 61041016Sdfr} 61141016Sdfr 61241016Sdfrstatic int 61341016Sdfrrestore_controller(KBDC kbdc, int command_byte) 61441016Sdfr{ 61541016Sdfr empty_both_buffers(kbdc, 10); 61641016Sdfr 61741016Sdfr if (!set_controller_command_byte(kbdc, 0xff, command_byte)) { 61841016Sdfr log(LOG_ERR, "psm: failed to restore the keyboard controller " 61941016Sdfr "command byte.\n"); 62058230Syokota empty_both_buffers(kbdc, 10); 62141016Sdfr return FALSE; 62241016Sdfr } else { 62358230Syokota empty_both_buffers(kbdc, 10); 62441016Sdfr return TRUE; 62541016Sdfr } 62641016Sdfr} 62741016Sdfr 62841016Sdfr/* 62941016Sdfr * Re-initialize the aux port and device. The aux port must be enabled 63041016Sdfr * and its interrupt must be disabled before calling this routine. 63141016Sdfr * The aux device will be disabled before returning. 63241016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before 63341016Sdfr * calling this routine. 63441016Sdfr */ 63541016Sdfrstatic int 63684880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode) 63741016Sdfr{ 63841016Sdfr KBDC kbdc = sc->kbdc; 63941016Sdfr int stat[3]; 64041016Sdfr int i; 64141016Sdfr 64241016Sdfr switch((i = test_aux_port(kbdc))) { 64341016Sdfr case 1: /* ignore this error */ 64441016Sdfr case PSM_ACK: 64541016Sdfr if (verbose) 64641016Sdfr log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n", 64784880Syokota sc->unit, i); 64841016Sdfr /* fall though */ 64941016Sdfr case 0: /* no error */ 65041016Sdfr break; 65141016Sdfr case -1: /* time out */ 65241016Sdfr default: /* error */ 65341016Sdfr recover_from_error(kbdc); 65445789Speter if (sc->config & PSM_CONFIG_IGNPORTERROR) 65545789Speter break; 65641016Sdfr log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n", 65784880Syokota sc->unit, i); 65841016Sdfr return FALSE; 65941016Sdfr } 66041016Sdfr 66145789Speter if (sc->config & PSM_CONFIG_NORESET) { 66245789Speter /* 66345789Speter * Don't try to reset the pointing device. It may possibly be 66445789Speter * left in the unknown state, though... 66545789Speter */ 66645789Speter } else { 66745789Speter /* 66845789Speter * NOTE: some controllers appears to hang the `keyboard' when 66945789Speter * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 67045789Speter */ 67145789Speter if (!reset_aux_dev(kbdc)) { 67245789Speter recover_from_error(kbdc); 67384880Syokota log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit); 67445789Speter return FALSE; 67545789Speter } 67641016Sdfr } 67741016Sdfr 67841016Sdfr /* 67941016Sdfr * both the aux port and the aux device is functioning, see 68041016Sdfr * if the device can be enabled. 68141016Sdfr */ 68241016Sdfr if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) { 68384880Syokota log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit); 68441016Sdfr return FALSE; 68541016Sdfr } 68641016Sdfr empty_both_buffers(kbdc, 10); /* remove stray data if any */ 68741016Sdfr 68845789Speter if (sc->config & PSM_CONFIG_NOIDPROBE) { 68945789Speter i = GENERIC_MOUSE_ENTRY; 69045789Speter } else { 69145789Speter /* FIXME: hardware ID, mouse buttons? */ 69241016Sdfr 69345789Speter /* other parameters */ 69445789Speter for (i = 0; vendortype[i].probefunc != NULL; ++i) { 69545789Speter if ((*vendortype[i].probefunc)(sc)) { 69645789Speter if (verbose >= 2) 69745789Speter log(LOG_ERR, "psm%d: found %s\n", 69884880Syokota sc->unit, model_name(vendortype[i].model)); 69945789Speter break; 70045789Speter } 70141016Sdfr } 70241016Sdfr } 70341016Sdfr 70441016Sdfr sc->hw.model = vendortype[i].model; 70541016Sdfr sc->mode.packetsize = vendortype[i].packetsize; 70641016Sdfr 70741016Sdfr /* set mouse parameters */ 70841016Sdfr if (mode != (mousemode_t *)NULL) { 70941016Sdfr if (mode->rate > 0) 71041016Sdfr mode->rate = set_mouse_sampling_rate(kbdc, mode->rate); 71141016Sdfr if (mode->resolution >= 0) 71241016Sdfr mode->resolution = set_mouse_resolution(kbdc, mode->resolution); 71341016Sdfr set_mouse_scaling(kbdc, 1); 71441016Sdfr set_mouse_mode(kbdc); 71541016Sdfr } 71641016Sdfr 71741016Sdfr /* request a data packet and extract sync. bits */ 71841016Sdfr if (get_mouse_status(kbdc, stat, 1, 3) < 3) { 71984880Syokota log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n", 72084880Syokota sc->unit); 72141016Sdfr sc->mode.syncmask[0] = 0; 72241016Sdfr } else { 72341016Sdfr sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 72441016Sdfr /* the NetScroll Mouse will send three more bytes... Ignore them */ 72541016Sdfr empty_aux_buffer(kbdc, 5); 72641016Sdfr } 72741016Sdfr 72841016Sdfr /* just check the status of the mouse */ 72941016Sdfr if (get_mouse_status(kbdc, stat, 0, 3) < 3) 73084880Syokota log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n", 73184880Syokota sc->unit); 73241016Sdfr 73341016Sdfr return TRUE; 73441016Sdfr} 73541016Sdfr 73641016Sdfrstatic int 73784880Syokotadoopen(struct psm_softc *sc, int command_byte) 73841016Sdfr{ 73941016Sdfr int stat[3]; 74041016Sdfr 74141016Sdfr /* enable the mouse device */ 74241016Sdfr if (!enable_aux_dev(sc->kbdc)) { 74341016Sdfr /* MOUSE ERROR: failed to enable the mouse because: 74441016Sdfr * 1) the mouse is faulty, 74541016Sdfr * 2) the mouse has been removed(!?) 74641016Sdfr * In the latter case, the keyboard may have hung, and need 74741016Sdfr * recovery procedure... 74841016Sdfr */ 74941016Sdfr recover_from_error(sc->kbdc); 75041016Sdfr#if 0 75141016Sdfr /* FIXME: we could reset the mouse here and try to enable 75241016Sdfr * it again. But it will take long time and it's not a good 75341016Sdfr * idea to disable the keyboard that long... 75441016Sdfr */ 75584880Syokota if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) { 75641016Sdfr recover_from_error(sc->kbdc); 75741016Sdfr#else 75841016Sdfr { 75941016Sdfr#endif 76041016Sdfr restore_controller(sc->kbdc, command_byte); 76141016Sdfr /* mark this device is no longer available */ 76241016Sdfr sc->state &= ~PSM_VALID; 76341016Sdfr log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n", 76484880Syokota sc->unit); 76541016Sdfr return (EIO); 76641016Sdfr } 76741016Sdfr } 76841016Sdfr 76941016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 77084880Syokota log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit); 77141016Sdfr 77241016Sdfr /* enable the aux port and interrupt */ 77341016Sdfr if (!set_controller_command_byte(sc->kbdc, 77441016Sdfr kbdc_get_device_mask(sc->kbdc), 77541016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 77641016Sdfr | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) { 77741016Sdfr /* CONTROLLER ERROR */ 77841016Sdfr disable_aux_dev(sc->kbdc); 77941016Sdfr restore_controller(sc->kbdc, command_byte); 78041016Sdfr log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n", 78184880Syokota sc->unit); 78241016Sdfr return (EIO); 78341016Sdfr } 78441016Sdfr 78558230Syokota /* start the watchdog timer */ 78658230Syokota sc->watchdog = FALSE; 78784880Syokota sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2); 78858230Syokota 78941016Sdfr return (0); 79041016Sdfr} 79141016Sdfr 79284880Syokotastatic int 79384880Syokotareinitialize(struct psm_softc *sc, int doinit) 79484880Syokota{ 79584880Syokota int err; 79684880Syokota int c; 79784880Syokota int s; 79884880Syokota 79984880Syokota /* don't let anybody mess with the aux device */ 80084880Syokota if (!kbdc_lock(sc->kbdc, TRUE)) 80184880Syokota return (EIO); 80284880Syokota s = spltty(); 80384880Syokota 80484880Syokota /* block our watchdog timer */ 80584880Syokota sc->watchdog = FALSE; 80684880Syokota untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout); 80784880Syokota callout_handle_init(&sc->callout); 80884880Syokota 80984880Syokota /* save the current controller command byte */ 81084880Syokota empty_both_buffers(sc->kbdc, 10); 81184880Syokota c = get_controller_command_byte(sc->kbdc); 81284880Syokota if (verbose >= 2) 81384880Syokota log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n", 81484880Syokota sc->unit, c); 81584880Syokota 81684880Syokota /* enable the aux port but disable the aux interrupt and the keyboard */ 81784880Syokota if ((c == -1) || !set_controller_command_byte(sc->kbdc, 81884880Syokota kbdc_get_device_mask(sc->kbdc), 81984880Syokota KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 82084880Syokota | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 82184880Syokota /* CONTROLLER ERROR */ 82284880Syokota splx(s); 82384880Syokota kbdc_lock(sc->kbdc, FALSE); 82484880Syokota log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n", 82584880Syokota sc->unit); 82684880Syokota return (EIO); 82784880Syokota } 82884880Syokota 82984880Syokota /* flush any data */ 83084880Syokota if (sc->state & PSM_VALID) { 83184880Syokota disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */ 83284880Syokota empty_aux_buffer(sc->kbdc, 10); 83384880Syokota } 83484880Syokota sc->inputbytes = 0; 83584880Syokota sc->syncerrors = 0; 83684880Syokota 83784880Syokota /* try to detect the aux device; are you still there? */ 83884880Syokota err = 0; 83984880Syokota if (doinit) { 84084880Syokota if (doinitialize(sc, &sc->mode)) { 84184880Syokota /* yes */ 84284880Syokota sc->state |= PSM_VALID; 84384880Syokota } else { 84484880Syokota /* the device has gone! */ 84584880Syokota restore_controller(sc->kbdc, c); 84684880Syokota sc->state &= ~PSM_VALID; 84784880Syokota log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n", 84884880Syokota sc->unit); 84984880Syokota err = ENXIO; 85084880Syokota } 85184880Syokota } 85284880Syokota splx(s); 85384880Syokota 85484880Syokota /* restore the driver state */ 85584880Syokota if ((sc->state & PSM_OPEN) && (err == 0)) { 85684880Syokota /* enable the aux device and the port again */ 85784880Syokota err = doopen(sc, c); 85884880Syokota if (err != 0) 85984880Syokota log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n", 86084880Syokota sc->unit); 86184880Syokota } else { 86284880Syokota /* restore the keyboard port and disable the aux port */ 86384880Syokota if (!set_controller_command_byte(sc->kbdc, 86484880Syokota kbdc_get_device_mask(sc->kbdc), 86584880Syokota (c & KBD_KBD_CONTROL_BITS) 86684880Syokota | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 86784880Syokota /* CONTROLLER ERROR */ 86884880Syokota log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n", 86984880Syokota sc->unit); 87084880Syokota err = EIO; 87184880Syokota } 87284880Syokota } 87384880Syokota 87484880Syokota kbdc_lock(sc->kbdc, FALSE); 87584880Syokota return (err); 87684880Syokota} 87784880Syokota 87841016Sdfr/* psm driver entry points */ 87941016Sdfr 88083147Syokotastatic void 88183147Syokotapsmidentify(driver_t *driver, device_t parent) 88283147Syokota{ 88383931Syokota device_t psmc; 88483931Syokota device_t psm; 88583931Syokota u_long irq; 88683931Syokota int unit; 88783147Syokota 88883931Syokota unit = device_get_unit(parent); 88983931Syokota 89083147Syokota /* always add at least one child */ 89183931Syokota psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit); 89283931Syokota if (psm == NULL) 89383931Syokota return; 89483931Syokota 89583931Syokota irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX); 89683931Syokota if (irq > 0) 89783931Syokota return; 89883931Syokota 89983931Syokota /* 90083931Syokota * If the PS/2 mouse device has already been reported by ACPI or 90183931Syokota * PnP BIOS, obtain the IRQ resource from it. 90283931Syokota * (See psmcpnp_attach() below.) 90383931Syokota */ 90483931Syokota psmc = device_find_child(device_get_parent(parent), 90583931Syokota PSMCPNP_DRIVER_NAME, unit); 90683931Syokota if (psmc == NULL) 90783931Syokota return; 90883931Syokota irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0); 90983931Syokota if (irq <= 0) 91083931Syokota return; 91183931Syokota bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 91283147Syokota} 91383147Syokota 91441016Sdfr#define endprobe(v) { if (bootverbose) \ 91541016Sdfr --verbose; \ 91641016Sdfr kbdc_set_device_mask(sc->kbdc, mask); \ 91741016Sdfr kbdc_lock(sc->kbdc, FALSE); \ 91841016Sdfr return (v); \ 91941016Sdfr } 92041016Sdfr 92141016Sdfrstatic int 92241016Sdfrpsmprobe(device_t dev) 92341016Sdfr{ 92441016Sdfr int unit = device_get_unit(dev); 92541016Sdfr struct psm_softc *sc = device_get_softc(dev); 92641016Sdfr int stat[3]; 92741016Sdfr int command_byte; 92841016Sdfr int mask; 92958230Syokota int rid; 93041016Sdfr int i; 93141016Sdfr 93241016Sdfr#if 0 93341016Sdfr kbdc_debug(TRUE); 93441016Sdfr#endif 93558230Syokota 93683147Syokota /* see if IRQ is available */ 93783147Syokota rid = KBDC_RID_AUX; 93883147Syokota sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 93983147Syokota RF_SHAREABLE | RF_ACTIVE); 94083147Syokota if (sc->intr == NULL) { 94183147Syokota if (bootverbose) 94283147Syokota device_printf(dev, "unable to allocate IRQ\n"); 94383147Syokota return (ENXIO); 94483147Syokota } 94583147Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 94658230Syokota 94784880Syokota sc->unit = unit; 94858230Syokota sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev))); 94983147Syokota sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS; 95058230Syokota /* XXX: for backward compatibility */ 95158230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM) 95258230Syokota sc->config |= 95358230Syokota#ifdef PSM_RESETAFTERSUSPEND 95458230Syokota PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 95558230Syokota#else 95658230Syokota PSM_CONFIG_HOOKRESUME; 95758230Syokota#endif 95858230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */ 95941016Sdfr sc->flags = 0; 96041016Sdfr if (bootverbose) 96141016Sdfr ++verbose; 96241016Sdfr 96343105Sdfr device_set_desc(dev, "PS/2 Mouse"); 96443105Sdfr 96541016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) { 96641016Sdfr printf("psm%d: unable to lock the controller.\n", unit); 96741016Sdfr if (bootverbose) 96841016Sdfr --verbose; 96941016Sdfr return (ENXIO); 97041016Sdfr } 97141016Sdfr 97241016Sdfr /* 97341016Sdfr * NOTE: two bits in the command byte controls the operation of the 97441016Sdfr * aux port (mouse port): the aux port disable bit (bit 5) and the aux 97541016Sdfr * port interrupt (IRQ 12) enable bit (bit 2). 97641016Sdfr */ 97741016Sdfr 97841016Sdfr /* discard anything left after the keyboard initialization */ 97941016Sdfr empty_both_buffers(sc->kbdc, 10); 98041016Sdfr 98141016Sdfr /* save the current command byte; it will be used later */ 98241016Sdfr mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS; 98341016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 98441016Sdfr if (verbose) 98541016Sdfr printf("psm%d: current command byte:%04x\n", unit, command_byte); 98641016Sdfr if (command_byte == -1) { 98741016Sdfr /* CONTROLLER ERROR */ 98841016Sdfr printf("psm%d: unable to get the current command byte value.\n", 98941016Sdfr unit); 99041016Sdfr endprobe(ENXIO); 99141016Sdfr } 99241016Sdfr 99341016Sdfr /* 99441016Sdfr * disable the keyboard port while probing the aux port, which must be 99541016Sdfr * enabled during this routine 99641016Sdfr */ 99741016Sdfr if (!set_controller_command_byte(sc->kbdc, 99841016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 99941016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 100041016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 100141016Sdfr /* 100241016Sdfr * this is CONTROLLER ERROR; I don't know how to recover 100341016Sdfr * from this error... 100441016Sdfr */ 100541016Sdfr restore_controller(sc->kbdc, command_byte); 100641016Sdfr printf("psm%d: unable to set the command byte.\n", unit); 100741016Sdfr endprobe(ENXIO); 100841016Sdfr } 100945789Speter write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT); 101041016Sdfr 101141016Sdfr /* 101241016Sdfr * NOTE: `test_aux_port()' is designed to return with zero if the aux 101341016Sdfr * port exists and is functioning. However, some controllers appears 101441016Sdfr * to respond with zero even when the aux port doesn't exist. (It may 101541016Sdfr * be that this is only the case when the controller DOES have the aux 101641016Sdfr * port but the port is not wired on the motherboard.) The keyboard 101741016Sdfr * controllers without the port, such as the original AT, are 101841016Sdfr * supporsed to return with an error code or simply time out. In any 101941016Sdfr * case, we have to continue probing the port even when the controller 102041016Sdfr * passes this test. 102141016Sdfr * 102241016Sdfr * XXX: some controllers erroneously return the error code 1 when 102341016Sdfr * it has the perfectly functional aux port. We have to ignore this 102441016Sdfr * error code. Even if the controller HAS error with the aux port, 102541016Sdfr * it will be detected later... 102641016Sdfr * XXX: another incompatible controller returns PSM_ACK (0xfa)... 102741016Sdfr */ 102841016Sdfr switch ((i = test_aux_port(sc->kbdc))) { 102941016Sdfr case 1: /* ignore this error */ 103041016Sdfr case PSM_ACK: 103141016Sdfr if (verbose) 103241016Sdfr printf("psm%d: strange result for test aux port (%d).\n", 103341016Sdfr unit, i); 103441016Sdfr /* fall though */ 103541016Sdfr case 0: /* no error */ 103641016Sdfr break; 103741016Sdfr case -1: /* time out */ 103841016Sdfr default: /* error */ 103941016Sdfr recover_from_error(sc->kbdc); 104045789Speter if (sc->config & PSM_CONFIG_IGNPORTERROR) 104145789Speter break; 104241016Sdfr restore_controller(sc->kbdc, command_byte); 104341016Sdfr if (verbose) 104441016Sdfr printf("psm%d: the aux port is not functioning (%d).\n", 104541016Sdfr unit, i); 104641016Sdfr endprobe(ENXIO); 104741016Sdfr } 104841016Sdfr 104945789Speter if (sc->config & PSM_CONFIG_NORESET) { 105045789Speter /* 105145789Speter * Don't try to reset the pointing device. It may possibly be 105245789Speter * left in the unknown state, though... 105345789Speter */ 105445789Speter } else { 105545789Speter /* 105645789Speter * NOTE: some controllers appears to hang the `keyboard' when the aux 105745789Speter * port doesn't exist and `PSMC_RESET_DEV' is issued. 105845789Speter */ 105945789Speter if (!reset_aux_dev(sc->kbdc)) { 106045789Speter recover_from_error(sc->kbdc); 106145789Speter restore_controller(sc->kbdc, command_byte); 106245789Speter if (verbose) 106345789Speter printf("psm%d: failed to reset the aux device.\n", unit); 106445789Speter endprobe(ENXIO); 106545789Speter } 106641016Sdfr } 106745789Speter 106841016Sdfr /* 106941016Sdfr * both the aux port and the aux device is functioning, see if the 107041016Sdfr * device can be enabled. NOTE: when enabled, the device will start 107141016Sdfr * sending data; we shall immediately disable the device once we know 107241016Sdfr * the device can be enabled. 107341016Sdfr */ 107441016Sdfr if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) { 107545789Speter /* MOUSE ERROR */ 107645789Speter recover_from_error(sc->kbdc); 107745789Speter restore_controller(sc->kbdc, command_byte); 107845789Speter if (verbose) 107945789Speter printf("psm%d: failed to enable the aux device.\n", unit); 108041016Sdfr endprobe(ENXIO); 108141016Sdfr } 108241016Sdfr 108341016Sdfr /* save the default values after reset */ 108441016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) { 108541016Sdfr sc->dflt_mode.rate = sc->mode.rate = stat[2]; 108641016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 108741016Sdfr } else { 108841016Sdfr sc->dflt_mode.rate = sc->mode.rate = -1; 108941016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = -1; 109041016Sdfr } 109141016Sdfr 109241016Sdfr /* hardware information */ 109341016Sdfr sc->hw.iftype = MOUSE_IF_PS2; 109441016Sdfr 109541016Sdfr /* verify the device is a mouse */ 109641016Sdfr sc->hw.hwid = get_aux_id(sc->kbdc); 109741016Sdfr if (!is_a_mouse(sc->hw.hwid)) { 109841016Sdfr restore_controller(sc->kbdc, command_byte); 109941016Sdfr if (verbose) 110041016Sdfr printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid); 110141016Sdfr endprobe(ENXIO); 110241016Sdfr } 110341016Sdfr switch (sc->hw.hwid) { 110441016Sdfr case PSM_BALLPOINT_ID: 110541016Sdfr sc->hw.type = MOUSE_TRACKBALL; 110641016Sdfr break; 110741016Sdfr case PSM_MOUSE_ID: 110841016Sdfr case PSM_INTELLI_ID: 110958230Syokota case PSM_EXPLORER_ID: 111058230Syokota case PSM_4DMOUSE_ID: 111158230Syokota case PSM_4DPLUS_ID: 111241016Sdfr sc->hw.type = MOUSE_MOUSE; 111341016Sdfr break; 111441016Sdfr default: 111541016Sdfr sc->hw.type = MOUSE_UNKNOWN; 111641016Sdfr break; 111741016Sdfr } 111841016Sdfr 111945789Speter if (sc->config & PSM_CONFIG_NOIDPROBE) { 112045789Speter sc->hw.buttons = 2; 112145789Speter i = GENERIC_MOUSE_ENTRY; 112245789Speter } else { 112345789Speter /* # of buttons */ 112445789Speter sc->hw.buttons = get_mouse_buttons(sc->kbdc); 112541016Sdfr 112645789Speter /* other parameters */ 112745789Speter for (i = 0; vendortype[i].probefunc != NULL; ++i) { 112845789Speter if ((*vendortype[i].probefunc)(sc)) { 112945789Speter if (verbose >= 2) 113045789Speter printf("psm%d: found %s\n", 113145789Speter unit, model_name(vendortype[i].model)); 113245789Speter break; 113345789Speter } 113441016Sdfr } 113541016Sdfr } 113641016Sdfr 113741016Sdfr sc->hw.model = vendortype[i].model; 113841016Sdfr 113941016Sdfr sc->dflt_mode.level = PSM_LEVEL_BASE; 114041016Sdfr sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE; 114141016Sdfr sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4; 114241016Sdfr if (sc->config & PSM_CONFIG_NOCHECKSYNC) 114341016Sdfr sc->dflt_mode.syncmask[0] = 0; 114441016Sdfr else 114541016Sdfr sc->dflt_mode.syncmask[0] = vendortype[i].syncmask; 114645789Speter if (sc->config & PSM_CONFIG_FORCETAP) 114745789Speter sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP; 114841016Sdfr sc->dflt_mode.syncmask[1] = 0; /* syncbits */ 114941016Sdfr sc->mode = sc->dflt_mode; 115041016Sdfr sc->mode.packetsize = vendortype[i].packetsize; 115141016Sdfr 115241016Sdfr /* set mouse parameters */ 115348773Syokota#if 0 115448773Syokota /* 115548773Syokota * A version of Logitech FirstMouse+ won't report wheel movement, 115648773Syokota * if SET_DEFAULTS is sent... Don't use this command. 115748773Syokota * This fix was found by Takashi Nishida. 115848773Syokota */ 115941016Sdfr i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS); 116041016Sdfr if (verbose >= 2) 116141016Sdfr printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i); 116248773Syokota#endif 116341016Sdfr if (sc->config & PSM_CONFIG_RESOLUTION) { 116441016Sdfr sc->mode.resolution 116541016Sdfr = set_mouse_resolution(sc->kbdc, 116648773Syokota (sc->config & PSM_CONFIG_RESOLUTION) - 1); 116748773Syokota } else if (sc->mode.resolution >= 0) { 116848773Syokota sc->mode.resolution 116948773Syokota = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution); 117041016Sdfr } 117148773Syokota if (sc->mode.rate > 0) { 117248773Syokota sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate); 117348773Syokota } 117448773Syokota set_mouse_scaling(sc->kbdc, 1); 117541016Sdfr 117641016Sdfr /* request a data packet and extract sync. bits */ 117741016Sdfr if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) { 117841016Sdfr printf("psm%d: failed to get data.\n", unit); 117941016Sdfr sc->mode.syncmask[0] = 0; 118041016Sdfr } else { 118141016Sdfr sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */ 118241016Sdfr /* the NetScroll Mouse will send three more bytes... Ignore them */ 118341016Sdfr empty_aux_buffer(sc->kbdc, 5); 118441016Sdfr } 118541016Sdfr 118641016Sdfr /* just check the status of the mouse */ 118741016Sdfr /* 118841016Sdfr * NOTE: XXX there are some arcane controller/mouse combinations out 118941016Sdfr * there, which hung the controller unless there is data transmission 119041016Sdfr * after ACK from the mouse. 119141016Sdfr */ 119241016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) { 119341016Sdfr printf("psm%d: failed to get status.\n", unit); 119441016Sdfr } else { 119541016Sdfr /* 119641016Sdfr * When in its native mode, some mice operate with different 119741016Sdfr * default parameters than in the PS/2 compatible mode. 119841016Sdfr */ 119941016Sdfr sc->dflt_mode.rate = sc->mode.rate = stat[2]; 120041016Sdfr sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 120141016Sdfr } 120241016Sdfr 120341016Sdfr /* disable the aux port for now... */ 120441016Sdfr if (!set_controller_command_byte(sc->kbdc, 120541016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 120641016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 120741016Sdfr | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 120841016Sdfr /* 120941016Sdfr * this is CONTROLLER ERROR; I don't know the proper way to 121041016Sdfr * recover from this error... 121141016Sdfr */ 121241016Sdfr restore_controller(sc->kbdc, command_byte); 121341016Sdfr printf("psm%d: unable to set the command byte.\n", unit); 121441016Sdfr endprobe(ENXIO); 121541016Sdfr } 121641016Sdfr 121741016Sdfr /* done */ 121841016Sdfr kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS); 121941016Sdfr kbdc_lock(sc->kbdc, FALSE); 122041016Sdfr return (0); 122141016Sdfr} 122241016Sdfr 122341016Sdfrstatic int 122441016Sdfrpsmattach(device_t dev) 122541016Sdfr{ 122641016Sdfr int unit = device_get_unit(dev); 122741016Sdfr struct psm_softc *sc = device_get_softc(dev); 122858230Syokota int error; 122958230Syokota int rid; 123041016Sdfr 123141016Sdfr if (sc == NULL) /* shouldn't happen */ 123241016Sdfr return (ENXIO); 123341016Sdfr 123441016Sdfr /* Setup initial state */ 123541016Sdfr sc->state = PSM_VALID; 123658230Syokota callout_handle_init(&sc->callout); 123741016Sdfr 123858230Syokota /* Setup our interrupt handler */ 123983147Syokota rid = KBDC_RID_AUX; 124083147Syokota sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 124183147Syokota RF_SHAREABLE | RF_ACTIVE); 124258230Syokota if (sc->intr == NULL) 124358230Syokota return (ENXIO); 124483147Syokota error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih); 124558230Syokota if (error) { 124658230Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 124758230Syokota return (error); 124858230Syokota } 124958230Syokota 125041016Sdfr /* Done */ 125158230Syokota sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, 125258230Syokota "psm%d", unit); 125358230Syokota sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, 125458230Syokota "bpsm%d", unit); 125541016Sdfr 125641016Sdfr if (!verbose) { 125741016Sdfr printf("psm%d: model %s, device ID %d\n", 125848778Syokota unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff); 125941016Sdfr } else { 126048778Syokota printf("psm%d: model %s, device ID %d-%02x, %d buttons\n", 126148778Syokota unit, model_name(sc->hw.model), 126248778Syokota sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons); 126341016Sdfr printf("psm%d: config:%08x, flags:%08x, packet size:%d\n", 126441016Sdfr unit, sc->config, sc->flags, sc->mode.packetsize); 126541016Sdfr printf("psm%d: syncmask:%02x, syncbits:%02x\n", 126641016Sdfr unit, sc->mode.syncmask[0], sc->mode.syncmask[1]); 126741016Sdfr } 126841016Sdfr 126941016Sdfr if (bootverbose) 127041016Sdfr --verbose; 127141016Sdfr 127241016Sdfr return (0); 127341016Sdfr} 127441016Sdfr 127541016Sdfrstatic int 127658230Syokotapsmdetach(device_t dev) 127758230Syokota{ 127858230Syokota struct psm_softc *sc; 127958230Syokota int rid; 128058230Syokota 128158230Syokota sc = device_get_softc(dev); 128258230Syokota if (sc->state & PSM_OPEN) 128358230Syokota return EBUSY; 128458230Syokota 128583147Syokota rid = KBDC_RID_AUX; 128683147Syokota bus_teardown_intr(dev, sc->intr, sc->ih); 128758230Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 128858230Syokota 128958230Syokota destroy_dev(sc->dev); 129058230Syokota destroy_dev(sc->bdev); 129158230Syokota 129258230Syokota return 0; 129358230Syokota} 129458230Syokota 129558230Syokotastatic int 129683366Sjulianpsmopen(dev_t dev, int flag, int fmt, struct thread *td) 129741016Sdfr{ 129841016Sdfr int unit = PSM_UNIT(dev); 129941016Sdfr struct psm_softc *sc; 130041016Sdfr int command_byte; 130141016Sdfr int err; 130241016Sdfr int s; 130341016Sdfr 130441016Sdfr /* Get device data */ 130541016Sdfr sc = PSM_SOFTC(unit); 130641016Sdfr if ((sc == NULL) || (sc->state & PSM_VALID) == 0) 130741016Sdfr /* the device is no longer valid/functioning */ 130841016Sdfr return (ENXIO); 130941016Sdfr 131041016Sdfr /* Disallow multiple opens */ 131141016Sdfr if (sc->state & PSM_OPEN) 131241016Sdfr return (EBUSY); 131341016Sdfr 131441016Sdfr device_busy(devclass_get_device(psm_devclass, unit)); 131541016Sdfr 131641016Sdfr /* Initialize state */ 131741016Sdfr sc->mode.level = sc->dflt_mode.level; 131841016Sdfr sc->mode.protocol = sc->dflt_mode.protocol; 131958230Syokota sc->watchdog = FALSE; 132041016Sdfr 132141016Sdfr /* flush the event queue */ 132241016Sdfr sc->queue.count = 0; 132341016Sdfr sc->queue.head = 0; 132441016Sdfr sc->queue.tail = 0; 132541016Sdfr sc->status.flags = 0; 132641016Sdfr sc->status.button = 0; 132741016Sdfr sc->status.obutton = 0; 132841016Sdfr sc->status.dx = 0; 132941016Sdfr sc->status.dy = 0; 133041016Sdfr sc->status.dz = 0; 133141016Sdfr sc->button = 0; 133241016Sdfr 133341016Sdfr /* empty input buffer */ 133441016Sdfr bzero(sc->ipacket, sizeof(sc->ipacket)); 133541016Sdfr sc->inputbytes = 0; 133684880Syokota sc->syncerrors = 0; 133741016Sdfr 133841016Sdfr /* don't let timeout routines in the keyboard driver to poll the kbdc */ 133941016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 134041016Sdfr return (EIO); 134141016Sdfr 134241016Sdfr /* save the current controller command byte */ 134341016Sdfr s = spltty(); 134441016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 134541016Sdfr 134641016Sdfr /* enable the aux port and temporalily disable the keyboard */ 134741016Sdfr if ((command_byte == -1) 134841016Sdfr || !set_controller_command_byte(sc->kbdc, 134941016Sdfr kbdc_get_device_mask(sc->kbdc), 135041016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 135141016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 135241016Sdfr /* CONTROLLER ERROR; do you know how to get out of this? */ 135341016Sdfr kbdc_lock(sc->kbdc, FALSE); 135441016Sdfr splx(s); 135541016Sdfr log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n", 135641016Sdfr unit); 135741016Sdfr return (EIO); 135841016Sdfr } 135941016Sdfr /* 136041016Sdfr * Now that the keyboard controller is told not to generate 136141016Sdfr * the keyboard and mouse interrupts, call `splx()' to allow 136241016Sdfr * the other tty interrupts. The clock interrupt may also occur, 136341016Sdfr * but timeout routines will be blocked by the poll flag set 136441016Sdfr * via `kbdc_lock()' 136541016Sdfr */ 136641016Sdfr splx(s); 136741016Sdfr 136841016Sdfr /* enable the mouse device */ 136984880Syokota err = doopen(sc, command_byte); 137041016Sdfr 137141016Sdfr /* done */ 137241016Sdfr if (err == 0) 137341016Sdfr sc->state |= PSM_OPEN; 137441016Sdfr kbdc_lock(sc->kbdc, FALSE); 137541016Sdfr return (err); 137641016Sdfr} 137741016Sdfr 137841016Sdfrstatic int 137983366Sjulianpsmclose(dev_t dev, int flag, int fmt, struct thread *td) 138041016Sdfr{ 138141016Sdfr int unit = PSM_UNIT(dev); 138241016Sdfr struct psm_softc *sc = PSM_SOFTC(unit); 138341016Sdfr int stat[3]; 138441016Sdfr int command_byte; 138541016Sdfr int s; 138641016Sdfr 138741016Sdfr /* don't let timeout routines in the keyboard driver to poll the kbdc */ 138841016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 138941016Sdfr return (EIO); 139041016Sdfr 139141016Sdfr /* save the current controller command byte */ 139241016Sdfr s = spltty(); 139341016Sdfr command_byte = get_controller_command_byte(sc->kbdc); 139441016Sdfr if (command_byte == -1) { 139541016Sdfr kbdc_lock(sc->kbdc, FALSE); 139641016Sdfr splx(s); 139741016Sdfr return (EIO); 139841016Sdfr } 139941016Sdfr 140041016Sdfr /* disable the aux interrupt and temporalily disable the keyboard */ 140141016Sdfr if (!set_controller_command_byte(sc->kbdc, 140241016Sdfr kbdc_get_device_mask(sc->kbdc), 140341016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 140441016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 140541016Sdfr log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n", 140658230Syokota unit); 140741016Sdfr /* CONTROLLER ERROR; 140841016Sdfr * NOTE: we shall force our way through. Because the only 140941016Sdfr * ill effect we shall see is that we may not be able 141041016Sdfr * to read ACK from the mouse, and it doesn't matter much 141141016Sdfr * so long as the mouse will accept the DISABLE command. 141241016Sdfr */ 141341016Sdfr } 141441016Sdfr splx(s); 141541016Sdfr 141658230Syokota /* stop the watchdog timer */ 141784880Syokota untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout); 141858230Syokota callout_handle_init(&sc->callout); 141958230Syokota 142041016Sdfr /* remove anything left in the output buffer */ 142141016Sdfr empty_aux_buffer(sc->kbdc, 10); 142241016Sdfr 142341016Sdfr /* disable the aux device, port and interrupt */ 142441016Sdfr if (sc->state & PSM_VALID) { 142541016Sdfr if (!disable_aux_dev(sc->kbdc)) { 142641016Sdfr /* MOUSE ERROR; 142741016Sdfr * NOTE: we don't return error and continue, pretending 142841016Sdfr * we have successfully disabled the device. It's OK because 142941016Sdfr * the interrupt routine will discard any data from the mouse 143041016Sdfr * hereafter. 143141016Sdfr */ 143241016Sdfr log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n", 143358230Syokota unit); 143441016Sdfr } 143541016Sdfr 143641016Sdfr if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 143741016Sdfr log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n", 143858230Syokota unit); 143941016Sdfr } 144041016Sdfr 144141016Sdfr if (!set_controller_command_byte(sc->kbdc, 144241016Sdfr kbdc_get_device_mask(sc->kbdc), 144341016Sdfr (command_byte & KBD_KBD_CONTROL_BITS) 144441016Sdfr | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 144541016Sdfr /* CONTROLLER ERROR; 144641016Sdfr * we shall ignore this error; see the above comment. 144741016Sdfr */ 144841016Sdfr log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n", 144958230Syokota unit); 145041016Sdfr } 145141016Sdfr 145241016Sdfr /* remove anything left in the output buffer */ 145341016Sdfr empty_aux_buffer(sc->kbdc, 10); 145441016Sdfr 145541016Sdfr /* close is almost always successful */ 145641016Sdfr sc->state &= ~PSM_OPEN; 145741016Sdfr kbdc_lock(sc->kbdc, FALSE); 145841016Sdfr device_unbusy(devclass_get_device(psm_devclass, unit)); 145941016Sdfr return (0); 146041016Sdfr} 146141016Sdfr 146241016Sdfrstatic int 146341016Sdfrtame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf) 146441016Sdfr{ 146541016Sdfr static unsigned char butmapps2[8] = { 146641016Sdfr 0, 146741016Sdfr MOUSE_PS2_BUTTON1DOWN, 146841016Sdfr MOUSE_PS2_BUTTON2DOWN, 146941016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN, 147041016Sdfr MOUSE_PS2_BUTTON3DOWN, 147141016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN, 147241016Sdfr MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 147341016Sdfr MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 147441016Sdfr }; 147541016Sdfr static unsigned char butmapmsc[8] = { 147641016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 147741016Sdfr MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 147841016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 147941016Sdfr MOUSE_MSC_BUTTON3UP, 148041016Sdfr MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 148141016Sdfr MOUSE_MSC_BUTTON2UP, 148241016Sdfr MOUSE_MSC_BUTTON1UP, 148341016Sdfr 0, 148441016Sdfr }; 148541016Sdfr int mapped; 148641016Sdfr int i; 148741016Sdfr 148841016Sdfr if (sc->mode.level == PSM_LEVEL_BASE) { 148941016Sdfr mapped = status->button & ~MOUSE_BUTTON4DOWN; 149041016Sdfr if (status->button & MOUSE_BUTTON4DOWN) 149141016Sdfr mapped |= MOUSE_BUTTON1DOWN; 149241016Sdfr status->button = mapped; 149341016Sdfr buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS]; 149441016Sdfr i = max(min(status->dx, 255), -256); 149541016Sdfr if (i < 0) 149641016Sdfr buf[0] |= MOUSE_PS2_XNEG; 149741016Sdfr buf[1] = i; 149841016Sdfr i = max(min(status->dy, 255), -256); 149941016Sdfr if (i < 0) 150041016Sdfr buf[0] |= MOUSE_PS2_YNEG; 150141016Sdfr buf[2] = i; 150241016Sdfr return MOUSE_PS2_PACKETSIZE; 150341016Sdfr } else if (sc->mode.level == PSM_LEVEL_STANDARD) { 150441016Sdfr buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS]; 150541016Sdfr i = max(min(status->dx, 255), -256); 150641016Sdfr buf[1] = i >> 1; 150741016Sdfr buf[3] = i - buf[1]; 150841016Sdfr i = max(min(status->dy, 255), -256); 150941016Sdfr buf[2] = i >> 1; 151041016Sdfr buf[4] = i - buf[2]; 151141016Sdfr i = max(min(status->dz, 127), -128); 151241016Sdfr buf[5] = (i >> 1) & 0x7f; 151341016Sdfr buf[6] = (i - (i >> 1)) & 0x7f; 151441016Sdfr buf[7] = (~status->button >> 3) & 0x7f; 151541016Sdfr return MOUSE_SYS_PACKETSIZE; 151641016Sdfr } 151741016Sdfr return sc->inputbytes;; 151841016Sdfr} 151941016Sdfr 152041016Sdfrstatic int 152141016Sdfrpsmread(dev_t dev, struct uio *uio, int flag) 152241016Sdfr{ 152341016Sdfr register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 152441016Sdfr unsigned char buf[PSM_SMALLBUFSIZE]; 152541016Sdfr int error = 0; 152641016Sdfr int s; 152741016Sdfr int l; 152841016Sdfr 152941016Sdfr if ((sc->state & PSM_VALID) == 0) 153041016Sdfr return EIO; 153141016Sdfr 153241016Sdfr /* block until mouse activity occured */ 153341016Sdfr s = spltty(); 153441016Sdfr while (sc->queue.count <= 0) { 153541016Sdfr if (PSM_NBLOCKIO(dev)) { 153641016Sdfr splx(s); 153741016Sdfr return EWOULDBLOCK; 153841016Sdfr } 153941016Sdfr sc->state |= PSM_ASLP; 154041016Sdfr error = tsleep((caddr_t) sc, PZERO | PCATCH, "psmrea", 0); 154141016Sdfr sc->state &= ~PSM_ASLP; 154241016Sdfr if (error) { 154341016Sdfr splx(s); 154441016Sdfr return error; 154541016Sdfr } else if ((sc->state & PSM_VALID) == 0) { 154641016Sdfr /* the device disappeared! */ 154741016Sdfr splx(s); 154841016Sdfr return EIO; 154941016Sdfr } 155041016Sdfr } 155141016Sdfr splx(s); 155241016Sdfr 155341016Sdfr /* copy data to the user land */ 155441016Sdfr while ((sc->queue.count > 0) && (uio->uio_resid > 0)) { 155541016Sdfr s = spltty(); 155641016Sdfr l = min(sc->queue.count, uio->uio_resid); 155741016Sdfr if (l > sizeof(buf)) 155841016Sdfr l = sizeof(buf); 155941016Sdfr if (l > sizeof(sc->queue.buf) - sc->queue.head) { 156041016Sdfr bcopy(&sc->queue.buf[sc->queue.head], &buf[0], 156141016Sdfr sizeof(sc->queue.buf) - sc->queue.head); 156241016Sdfr bcopy(&sc->queue.buf[0], 156341016Sdfr &buf[sizeof(sc->queue.buf) - sc->queue.head], 156441016Sdfr l - (sizeof(sc->queue.buf) - sc->queue.head)); 156541016Sdfr } else { 156641016Sdfr bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l); 156741016Sdfr } 156841016Sdfr sc->queue.count -= l; 156941016Sdfr sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf); 157041016Sdfr splx(s); 157141016Sdfr error = uiomove(buf, l, uio); 157241016Sdfr if (error) 157341016Sdfr break; 157441016Sdfr } 157541016Sdfr 157641016Sdfr return error; 157741016Sdfr} 157841016Sdfr 157941016Sdfrstatic int 158041016Sdfrblock_mouse_data(struct psm_softc *sc, int *c) 158141016Sdfr{ 158241016Sdfr int s; 158341016Sdfr 158441016Sdfr if (!kbdc_lock(sc->kbdc, TRUE)) 158541016Sdfr return EIO; 158641016Sdfr 158741016Sdfr s = spltty(); 158841016Sdfr *c = get_controller_command_byte(sc->kbdc); 158941016Sdfr if ((*c == -1) 159041016Sdfr || !set_controller_command_byte(sc->kbdc, 159141016Sdfr kbdc_get_device_mask(sc->kbdc), 159241016Sdfr KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 159341016Sdfr | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 159441016Sdfr /* this is CONTROLLER ERROR */ 159541016Sdfr splx(s); 159641016Sdfr kbdc_lock(sc->kbdc, FALSE); 159741016Sdfr return EIO; 159841016Sdfr } 159941016Sdfr 160041016Sdfr /* 160141016Sdfr * The device may be in the middle of status data transmission. 160241016Sdfr * The transmission will be interrupted, thus, incomplete status 160341016Sdfr * data must be discarded. Although the aux interrupt is disabled 160441016Sdfr * at the keyboard controller level, at most one aux interrupt 160541016Sdfr * may have already been pending and a data byte is in the 160641016Sdfr * output buffer; throw it away. Note that the second argument 160741016Sdfr * to `empty_aux_buffer()' is zero, so that the call will just 160841016Sdfr * flush the internal queue. 160941016Sdfr * `psmintr()' will be invoked after `splx()' if an interrupt is 161041016Sdfr * pending; it will see no data and returns immediately. 161141016Sdfr */ 161241016Sdfr empty_aux_buffer(sc->kbdc, 0); /* flush the queue */ 161341016Sdfr read_aux_data_no_wait(sc->kbdc); /* throw away data if any */ 161441016Sdfr sc->inputbytes = 0; 161541016Sdfr splx(s); 161641016Sdfr 161741016Sdfr return 0; 161841016Sdfr} 161941016Sdfr 162041016Sdfrstatic int 162141016Sdfrunblock_mouse_data(struct psm_softc *sc, int c) 162241016Sdfr{ 162341016Sdfr int error = 0; 162441016Sdfr 162541016Sdfr /* 162641016Sdfr * We may have seen a part of status data during `set_mouse_XXX()'. 162741016Sdfr * they have been queued; flush it. 162841016Sdfr */ 162941016Sdfr empty_aux_buffer(sc->kbdc, 0); 163041016Sdfr 163141016Sdfr /* restore ports and interrupt */ 163241016Sdfr if (!set_controller_command_byte(sc->kbdc, 163341016Sdfr kbdc_get_device_mask(sc->kbdc), 163441016Sdfr c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 163541016Sdfr /* CONTROLLER ERROR; this is serious, we may have 163641016Sdfr * been left with the inaccessible keyboard and 163741016Sdfr * the disabled mouse interrupt. 163841016Sdfr */ 163941016Sdfr error = EIO; 164041016Sdfr } 164141016Sdfr 164241016Sdfr kbdc_lock(sc->kbdc, FALSE); 164341016Sdfr return error; 164441016Sdfr} 164541016Sdfr 164641016Sdfrstatic int 164783366Sjulianpsmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 164841016Sdfr{ 164941016Sdfr struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 165041016Sdfr mousemode_t mode; 165141016Sdfr mousestatus_t status; 165241016Sdfr#if (defined(MOUSE_GETVARS)) 165341016Sdfr mousevar_t *var; 165441016Sdfr#endif 165541016Sdfr mousedata_t *data; 165641016Sdfr int stat[3]; 165741016Sdfr int command_byte; 165841016Sdfr int error = 0; 165941016Sdfr int s; 166041016Sdfr 166141016Sdfr /* Perform IOCTL command */ 166241016Sdfr switch (cmd) { 166341016Sdfr 166441016Sdfr case OLD_MOUSE_GETHWINFO: 166541016Sdfr s = spltty(); 166641016Sdfr ((old_mousehw_t *)addr)->buttons = sc->hw.buttons; 166741016Sdfr ((old_mousehw_t *)addr)->iftype = sc->hw.iftype; 166841016Sdfr ((old_mousehw_t *)addr)->type = sc->hw.type; 166948778Syokota ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff; 167041016Sdfr splx(s); 167141016Sdfr break; 167241016Sdfr 167341016Sdfr case MOUSE_GETHWINFO: 167441016Sdfr s = spltty(); 167541016Sdfr *(mousehw_t *)addr = sc->hw; 167641016Sdfr if (sc->mode.level == PSM_LEVEL_BASE) 167741016Sdfr ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC; 167841016Sdfr splx(s); 167941016Sdfr break; 168041016Sdfr 168141016Sdfr case OLD_MOUSE_GETMODE: 168241016Sdfr s = spltty(); 168341016Sdfr switch (sc->mode.level) { 168441016Sdfr case PSM_LEVEL_BASE: 168541016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 168641016Sdfr break; 168741016Sdfr case PSM_LEVEL_STANDARD: 168841016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 168941016Sdfr break; 169041016Sdfr case PSM_LEVEL_NATIVE: 169141016Sdfr ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 169241016Sdfr break; 169341016Sdfr } 169441016Sdfr ((old_mousemode_t *)addr)->rate = sc->mode.rate; 169541016Sdfr ((old_mousemode_t *)addr)->resolution = sc->mode.resolution; 169641016Sdfr ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor; 169741016Sdfr splx(s); 169841016Sdfr break; 169941016Sdfr 170041016Sdfr case MOUSE_GETMODE: 170141016Sdfr s = spltty(); 170241016Sdfr *(mousemode_t *)addr = sc->mode; 170341016Sdfr ((mousemode_t *)addr)->resolution = 170441016Sdfr MOUSE_RES_LOW - sc->mode.resolution; 170541016Sdfr switch (sc->mode.level) { 170641016Sdfr case PSM_LEVEL_BASE: 170741016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 170841016Sdfr ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE; 170941016Sdfr break; 171041016Sdfr case PSM_LEVEL_STANDARD: 171141016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 171241016Sdfr ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE; 171341016Sdfr ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK; 171441016Sdfr ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC; 171541016Sdfr break; 171641016Sdfr case PSM_LEVEL_NATIVE: 171741016Sdfr /* FIXME: this isn't quite correct... XXX */ 171841016Sdfr ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 171941016Sdfr break; 172041016Sdfr } 172141016Sdfr splx(s); 172241016Sdfr break; 172341016Sdfr 172441016Sdfr case OLD_MOUSE_SETMODE: 172541016Sdfr case MOUSE_SETMODE: 172641016Sdfr if (cmd == OLD_MOUSE_SETMODE) { 172741016Sdfr mode.rate = ((old_mousemode_t *)addr)->rate; 172841016Sdfr /* 172941016Sdfr * resolution old I/F new I/F 173041016Sdfr * default 0 0 173141016Sdfr * low 1 -2 173241016Sdfr * medium low 2 -3 173341016Sdfr * medium high 3 -4 173441016Sdfr * high 4 -5 173541016Sdfr */ 173641016Sdfr if (((old_mousemode_t *)addr)->resolution > 0) 173741016Sdfr mode.resolution = -((old_mousemode_t *)addr)->resolution - 1; 173841016Sdfr mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor; 173941016Sdfr mode.level = -1; 174041016Sdfr } else { 174141016Sdfr mode = *(mousemode_t *)addr; 174241016Sdfr } 174341016Sdfr 174441016Sdfr /* adjust and validate parameters. */ 174541016Sdfr if (mode.rate > UCHAR_MAX) 174641016Sdfr return EINVAL; 174741016Sdfr if (mode.rate == 0) 174841016Sdfr mode.rate = sc->dflt_mode.rate; 174941016Sdfr else if (mode.rate == -1) 175041016Sdfr /* don't change the current setting */ 175141016Sdfr ; 175241016Sdfr else if (mode.rate < 0) 175341016Sdfr return EINVAL; 175441016Sdfr if (mode.resolution >= UCHAR_MAX) 175541016Sdfr return EINVAL; 175641016Sdfr if (mode.resolution >= 200) 175741016Sdfr mode.resolution = MOUSE_RES_HIGH; 175841016Sdfr else if (mode.resolution >= 100) 175941016Sdfr mode.resolution = MOUSE_RES_MEDIUMHIGH; 176041016Sdfr else if (mode.resolution >= 50) 176141016Sdfr mode.resolution = MOUSE_RES_MEDIUMLOW; 176241016Sdfr else if (mode.resolution > 0) 176341016Sdfr mode.resolution = MOUSE_RES_LOW; 176441016Sdfr if (mode.resolution == MOUSE_RES_DEFAULT) 176541016Sdfr mode.resolution = sc->dflt_mode.resolution; 176641016Sdfr else if (mode.resolution == -1) 176741016Sdfr /* don't change the current setting */ 176841016Sdfr ; 176941016Sdfr else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 177041016Sdfr mode.resolution = MOUSE_RES_LOW - mode.resolution; 177141016Sdfr if (mode.level == -1) 177241016Sdfr /* don't change the current setting */ 177341016Sdfr mode.level = sc->mode.level; 177441016Sdfr else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX)) 177541016Sdfr return EINVAL; 177641016Sdfr if (mode.accelfactor == -1) 177741016Sdfr /* don't change the current setting */ 177841016Sdfr mode.accelfactor = sc->mode.accelfactor; 177941016Sdfr else if (mode.accelfactor < 0) 178041016Sdfr return EINVAL; 178141016Sdfr 178241016Sdfr /* don't allow anybody to poll the keyboard controller */ 178341016Sdfr error = block_mouse_data(sc, &command_byte); 178441016Sdfr if (error) 178541016Sdfr return error; 178641016Sdfr 178741016Sdfr /* set mouse parameters */ 178841016Sdfr if (mode.rate > 0) 178941016Sdfr mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 179041016Sdfr if (mode.resolution >= 0) 179141016Sdfr mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 179241016Sdfr set_mouse_scaling(sc->kbdc, 1); 179341016Sdfr get_mouse_status(sc->kbdc, stat, 0, 3); 179441016Sdfr 179541016Sdfr s = spltty(); 179641016Sdfr sc->mode.rate = mode.rate; 179741016Sdfr sc->mode.resolution = mode.resolution; 179841016Sdfr sc->mode.accelfactor = mode.accelfactor; 179941016Sdfr sc->mode.level = mode.level; 180041016Sdfr splx(s); 180141016Sdfr 180241016Sdfr unblock_mouse_data(sc, command_byte); 180341016Sdfr break; 180441016Sdfr 180541016Sdfr case MOUSE_GETLEVEL: 180641016Sdfr *(int *)addr = sc->mode.level; 180741016Sdfr break; 180841016Sdfr 180941016Sdfr case MOUSE_SETLEVEL: 181041016Sdfr if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX)) 181141016Sdfr return EINVAL; 181241016Sdfr sc->mode.level = *(int *)addr; 181341016Sdfr break; 181441016Sdfr 181541016Sdfr case MOUSE_GETSTATUS: 181641016Sdfr s = spltty(); 181741016Sdfr status = sc->status; 181841016Sdfr sc->status.flags = 0; 181941016Sdfr sc->status.obutton = sc->status.button; 182041016Sdfr sc->status.button = 0; 182141016Sdfr sc->status.dx = 0; 182241016Sdfr sc->status.dy = 0; 182341016Sdfr sc->status.dz = 0; 182441016Sdfr splx(s); 182541016Sdfr *(mousestatus_t *)addr = status; 182641016Sdfr break; 182741016Sdfr 182841016Sdfr#if (defined(MOUSE_GETVARS)) 182941016Sdfr case MOUSE_GETVARS: 183041016Sdfr var = (mousevar_t *)addr; 183141016Sdfr bzero(var, sizeof(*var)); 183241016Sdfr s = spltty(); 183341016Sdfr var->var[0] = MOUSE_VARS_PS2_SIG; 183441016Sdfr var->var[1] = sc->config; 183541016Sdfr var->var[2] = sc->flags; 183641016Sdfr splx(s); 183741016Sdfr break; 183841016Sdfr 183941016Sdfr case MOUSE_SETVARS: 184041016Sdfr return ENODEV; 184141016Sdfr#endif /* MOUSE_GETVARS */ 184241016Sdfr 184341016Sdfr case MOUSE_READSTATE: 184441016Sdfr case MOUSE_READDATA: 184541016Sdfr data = (mousedata_t *)addr; 184641016Sdfr if (data->len > sizeof(data->buf)/sizeof(data->buf[0])) 184741016Sdfr return EINVAL; 184841016Sdfr 184941016Sdfr error = block_mouse_data(sc, &command_byte); 185041016Sdfr if (error) 185141016Sdfr return error; 185241016Sdfr if ((data->len = get_mouse_status(sc->kbdc, data->buf, 185341016Sdfr (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0) 185441016Sdfr error = EIO; 185541016Sdfr unblock_mouse_data(sc, command_byte); 185641016Sdfr break; 185741016Sdfr 185841016Sdfr#if (defined(MOUSE_SETRESOLUTION)) 185941016Sdfr case MOUSE_SETRESOLUTION: 186041016Sdfr mode.resolution = *(int *)addr; 186141016Sdfr if (mode.resolution >= UCHAR_MAX) 186241016Sdfr return EINVAL; 186341016Sdfr else if (mode.resolution >= 200) 186441016Sdfr mode.resolution = MOUSE_RES_HIGH; 186541016Sdfr else if (mode.resolution >= 100) 186641016Sdfr mode.resolution = MOUSE_RES_MEDIUMHIGH; 186741016Sdfr else if (mode.resolution >= 50) 186841016Sdfr mode.resolution = MOUSE_RES_MEDIUMLOW; 186941016Sdfr else if (mode.resolution > 0) 187041016Sdfr mode.resolution = MOUSE_RES_LOW; 187141016Sdfr if (mode.resolution == MOUSE_RES_DEFAULT) 187241016Sdfr mode.resolution = sc->dflt_mode.resolution; 187341016Sdfr else if (mode.resolution == -1) 187441016Sdfr mode.resolution = sc->mode.resolution; 187541016Sdfr else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 187641016Sdfr mode.resolution = MOUSE_RES_LOW - mode.resolution; 187741016Sdfr 187841016Sdfr error = block_mouse_data(sc, &command_byte); 187941016Sdfr if (error) 188041016Sdfr return error; 188141016Sdfr sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution); 188241016Sdfr if (sc->mode.resolution != mode.resolution) 188341016Sdfr error = EIO; 188441016Sdfr unblock_mouse_data(sc, command_byte); 188541016Sdfr break; 188641016Sdfr#endif /* MOUSE_SETRESOLUTION */ 188741016Sdfr 188841016Sdfr#if (defined(MOUSE_SETRATE)) 188941016Sdfr case MOUSE_SETRATE: 189041016Sdfr mode.rate = *(int *)addr; 189141016Sdfr if (mode.rate > UCHAR_MAX) 189241016Sdfr return EINVAL; 189341016Sdfr if (mode.rate == 0) 189441016Sdfr mode.rate = sc->dflt_mode.rate; 189541016Sdfr else if (mode.rate < 0) 189641016Sdfr mode.rate = sc->mode.rate; 189741016Sdfr 189841016Sdfr error = block_mouse_data(sc, &command_byte); 189941016Sdfr if (error) 190041016Sdfr return error; 190141016Sdfr sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 190241016Sdfr if (sc->mode.rate != mode.rate) 190341016Sdfr error = EIO; 190441016Sdfr unblock_mouse_data(sc, command_byte); 190541016Sdfr break; 190641016Sdfr#endif /* MOUSE_SETRATE */ 190741016Sdfr 190841016Sdfr#if (defined(MOUSE_SETSCALING)) 190941016Sdfr case MOUSE_SETSCALING: 191041016Sdfr if ((*(int *)addr <= 0) || (*(int *)addr > 2)) 191141016Sdfr return EINVAL; 191241016Sdfr 191341016Sdfr error = block_mouse_data(sc, &command_byte); 191441016Sdfr if (error) 191541016Sdfr return error; 191641016Sdfr if (!set_mouse_scaling(sc->kbdc, *(int *)addr)) 191741016Sdfr error = EIO; 191841016Sdfr unblock_mouse_data(sc, command_byte); 191941016Sdfr break; 192041016Sdfr#endif /* MOUSE_SETSCALING */ 192141016Sdfr 192241016Sdfr#if (defined(MOUSE_GETHWID)) 192341016Sdfr case MOUSE_GETHWID: 192441016Sdfr error = block_mouse_data(sc, &command_byte); 192541016Sdfr if (error) 192641016Sdfr return error; 192748778Syokota sc->hw.hwid &= ~0x00ff; 192848778Syokota sc->hw.hwid |= get_aux_id(sc->kbdc); 192948778Syokota *(int *)addr = sc->hw.hwid & 0x00ff; 193041016Sdfr unblock_mouse_data(sc, command_byte); 193141016Sdfr break; 193241016Sdfr#endif /* MOUSE_GETHWID */ 193341016Sdfr 193441016Sdfr default: 193541016Sdfr return ENOTTY; 193641016Sdfr } 193741016Sdfr 193841016Sdfr return error; 193941016Sdfr} 194041016Sdfr 194141016Sdfrstatic void 194258230Syokotapsmtimeout(void *arg) 194358230Syokota{ 194458230Syokota struct psm_softc *sc; 194565045Syokota int s; 194658230Syokota 194784880Syokota sc = (struct psm_softc *)arg; 194865045Syokota s = spltty(); 194963746Syokota if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) { 195058230Syokota if (verbose >= 4) 195184880Syokota log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit); 195258230Syokota psmintr(sc); 195363746Syokota kbdc_lock(sc->kbdc, FALSE); 195458230Syokota } 195558230Syokota sc->watchdog = TRUE; 195665045Syokota splx(s); 195784880Syokota sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz); 195858230Syokota} 195958230Syokota 196058230Syokotastatic void 196141016Sdfrpsmintr(void *arg) 196241016Sdfr{ 196341016Sdfr /* 196441016Sdfr * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN) 196541016Sdfr * into `mousestatus' button bits (MOUSE_BUTTON?DOWN). 196641016Sdfr */ 196741016Sdfr static int butmap[8] = { 196841016Sdfr 0, 196941016Sdfr MOUSE_BUTTON1DOWN, 197041016Sdfr MOUSE_BUTTON3DOWN, 197141016Sdfr MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 197241016Sdfr MOUSE_BUTTON2DOWN, 197341016Sdfr MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 197441016Sdfr MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 197541016Sdfr MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 197641016Sdfr }; 197749965Syokota static int butmap_versapad[8] = { 197849965Syokota 0, 197949965Syokota MOUSE_BUTTON3DOWN, 198049965Syokota 0, 198149965Syokota MOUSE_BUTTON3DOWN, 198249965Syokota MOUSE_BUTTON1DOWN, 198349965Syokota MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 198449965Syokota MOUSE_BUTTON1DOWN, 198549965Syokota MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN 198649965Syokota }; 198741016Sdfr register struct psm_softc *sc = arg; 198841016Sdfr mousestatus_t ms; 198984880Syokota struct timeval tv; 199041016Sdfr int x, y, z; 199141016Sdfr int c; 199241016Sdfr int l; 199349965Syokota int x0, y0; 199441016Sdfr 199541016Sdfr /* read until there is nothing to read */ 199641016Sdfr while((c = read_aux_data_no_wait(sc->kbdc)) != -1) { 199741016Sdfr 199841016Sdfr /* discard the byte if the device is not open */ 199941016Sdfr if ((sc->state & PSM_OPEN) == 0) 200041016Sdfr continue; 200141016Sdfr 200284880Syokota getmicrouptime(&tv); 200384880Syokota if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) { 200484880Syokota log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n"); 200584880Syokota sc->inputbytes = 0; 200684880Syokota sc->syncerrors = 0; 200784880Syokota } 200884880Syokota sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000; 200984880Syokota sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000; 201084880Syokota timevaladd(&sc->inputtimeout, &tv); 201184880Syokota 201241016Sdfr sc->ipacket[sc->inputbytes++] = c; 201341016Sdfr if (sc->inputbytes < sc->mode.packetsize) 201441016Sdfr continue; 201541016Sdfr 201641016Sdfr#if 0 201741016Sdfr log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n", 201841016Sdfr sc->ipacket[0], sc->ipacket[1], sc->ipacket[2], 201941016Sdfr sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]); 202041016Sdfr#endif 202141016Sdfr 202241016Sdfr c = sc->ipacket[0]; 202341016Sdfr 202463746Syokota if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) { 202563746Syokota log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n", 202663746Syokota c & sc->mode.syncmask[0], sc->mode.syncmask[1]); 202784880Syokota ++sc->syncerrors; 202884880Syokota if (sc->syncerrors < sc->mode.packetsize) { 202984880Syokota log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors); 203084880Syokota --sc->inputbytes; 203184880Syokota bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes); 203284880Syokota } else if (sc->syncerrors == sc->mode.packetsize) { 203369439Syokota log(LOG_DEBUG, "psmintr: re-enable the mouse.\n"); 203484880Syokota sc->inputbytes = 0; 203569439Syokota disable_aux_dev(sc->kbdc); 203669439Syokota enable_aux_dev(sc->kbdc); 203784880Syokota } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) { 203884880Syokota log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors); 203984880Syokota --sc->inputbytes; 204084880Syokota bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes); 204184880Syokota } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) { 204284880Syokota log(LOG_DEBUG, "psmintr: reset the mouse.\n"); 204384880Syokota reinitialize(sc, TRUE); 204469439Syokota } 204584880Syokota continue; 204663746Syokota } 204763746Syokota 204849965Syokota /* 204941016Sdfr * A kludge for Kensington device! 205041016Sdfr * The MSB of the horizontal count appears to be stored in 205158230Syokota * a strange place. 205241016Sdfr */ 205358230Syokota if (sc->hw.model == MOUSE_MODEL_THINK) 205449965Syokota sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0; 205541016Sdfr 205641016Sdfr /* ignore the overflow bits... */ 205741016Sdfr x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1]; 205841016Sdfr y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2]; 205941016Sdfr z = 0; 206041016Sdfr ms.obutton = sc->button; /* previous button state */ 206141016Sdfr ms.button = butmap[c & MOUSE_PS2_BUTTONS]; 206245789Speter /* `tapping' action */ 206345789Speter if (sc->config & PSM_CONFIG_FORCETAP) 206445789Speter ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 206541016Sdfr 206641016Sdfr switch (sc->hw.model) { 206741016Sdfr 206858230Syokota case MOUSE_MODEL_EXPLORER: 206958230Syokota /* 207058230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 207158230Syokota * byte 1: oy ox sy sx 1 M R L 207258230Syokota * byte 2: x x x x x x x x 207358230Syokota * byte 3: y y y y y y y y 207458230Syokota * byte 4: * * S2 S1 s d2 d1 d0 207558230Syokota * 207658230Syokota * L, M, R, S1, S2: left, middle, right and side buttons 207758230Syokota * s: wheel data sign bit 207858230Syokota * d2-d0: wheel data 207958230Syokota */ 208058230Syokota z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG) 208158230Syokota ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f); 208258230Syokota ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) 208358230Syokota ? MOUSE_BUTTON4DOWN : 0; 208458230Syokota ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) 208558230Syokota ? MOUSE_BUTTON5DOWN : 0; 208658230Syokota break; 208758230Syokota 208841016Sdfr case MOUSE_MODEL_INTELLI: 208941016Sdfr case MOUSE_MODEL_NET: 209041016Sdfr /* wheel data is in the fourth byte */ 209141016Sdfr z = (char)sc->ipacket[3]; 209258230Syokota /* some mice may send 7 when there is no Z movement?! XXX */ 209358230Syokota if ((z >= 7) || (z <= -7)) 209458230Syokota z = 0; 209558230Syokota /* some compatible mice have additional buttons */ 209658230Syokota ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) 209758230Syokota ? MOUSE_BUTTON4DOWN : 0; 209858230Syokota ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) 209958230Syokota ? MOUSE_BUTTON5DOWN : 0; 210041016Sdfr break; 210141016Sdfr 210241016Sdfr case MOUSE_MODEL_MOUSEMANPLUS: 210348778Syokota /* 210448778Syokota * PS2++ protocl packet 210548778Syokota * 210648778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 210748778Syokota * byte 1: * 1 p3 p2 1 * * * 210848778Syokota * byte 2: c1 c2 p1 p0 d1 d0 1 0 210948778Syokota * 211048778Syokota * p3-p0: packet type 211148778Syokota * c1, c2: c1 & c2 == 1, if p2 == 0 211248778Syokota * c1 & c2 == 0, if p2 == 1 211348778Syokota * 211448778Syokota * packet type: 0 (device type) 211548778Syokota * See comments in enable_mmanplus() below. 211648778Syokota * 211748778Syokota * packet type: 1 (wheel data) 211848778Syokota * 211948778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 212048778Syokota * byte 3: h * B5 B4 s d2 d1 d0 212148778Syokota * 212248778Syokota * h: 1, if horizontal roller data 212348778Syokota * 0, if vertical roller data 212448778Syokota * B4, B5: button 4 and 5 212548778Syokota * s: sign bit 212648778Syokota * d2-d0: roller data 212748778Syokota * 212848778Syokota * packet type: 2 (reserved) 212948778Syokota */ 213048778Syokota if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) 213148778Syokota && (abs(x) > 191) 213248778Syokota && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) { 213341016Sdfr /* the extended data packet encodes button and wheel events */ 213448778Syokota switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) { 213548778Syokota case 1: 213648778Syokota /* wheel data packet */ 213748778Syokota x = y = 0; 213848778Syokota if (sc->ipacket[2] & 0x80) { 213948778Syokota /* horizontal roller count - ignore it XXX*/ 214048778Syokota } else { 214148778Syokota /* vertical roller count */ 214248778Syokota z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG) 214348778Syokota ? (sc->ipacket[2] & 0x0f) - 16 214448778Syokota : (sc->ipacket[2] & 0x0f); 214548778Syokota } 214648778Syokota ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN) 214748778Syokota ? MOUSE_BUTTON4DOWN : 0; 214848778Syokota ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN) 214948778Syokota ? MOUSE_BUTTON5DOWN : 0; 215048778Syokota break; 215148778Syokota case 2: 215258230Syokota /* this packet type is reserved by Logitech... */ 215358230Syokota /* 215458230Syokota * IBM ScrollPoint Mouse uses this packet type to 215558230Syokota * encode both vertical and horizontal scroll movement. 215658230Syokota */ 215758230Syokota x = y = 0; 215858230Syokota /* horizontal count */ 215958230Syokota if (sc->ipacket[2] & 0x0f) 216058230Syokota z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2; 216158230Syokota /* vertical count */ 216258230Syokota if (sc->ipacket[2] & 0xf0) 216358230Syokota z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1; 216458230Syokota#if 0 216558230Syokota /* vertical count */ 216658230Syokota z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) 216758230Syokota ? ((sc->ipacket[2] >> 4) & 0x0f) - 16 216858230Syokota : ((sc->ipacket[2] >> 4) & 0x0f); 216958230Syokota /* horizontal count */ 217058230Syokota w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) 217158230Syokota ? (sc->ipacket[2] & 0x0f) - 16 217258230Syokota : (sc->ipacket[2] & 0x0f); 217358230Syokota#endif 217458230Syokota break; 217548778Syokota case 0: 217648778Syokota /* device type packet - shouldn't happen */ 217748778Syokota /* FALL THROUGH */ 217848778Syokota default: 217948778Syokota x = y = 0; 218048778Syokota ms.button = ms.obutton; 218158230Syokota if (bootverbose) 218258230Syokota log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: " 218358230Syokota "0x%02x 0x%02x 0x%02x\n", 218458230Syokota MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket), 218558230Syokota sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]); 218648778Syokota break; 218748778Syokota } 218841016Sdfr } else { 218941016Sdfr /* preserve button states */ 219041016Sdfr ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 219141016Sdfr } 219241016Sdfr break; 219341016Sdfr 219441016Sdfr case MOUSE_MODEL_GLIDEPOINT: 219541016Sdfr /* `tapping' action */ 219641016Sdfr ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN; 219741016Sdfr break; 219841016Sdfr 219941016Sdfr case MOUSE_MODEL_NETSCROLL: 220058230Syokota /* three addtional bytes encode buttons and wheel events */ 220158230Syokota ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) 220241016Sdfr ? MOUSE_BUTTON4DOWN : 0; 220358230Syokota ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) 220458230Syokota ? MOUSE_BUTTON5DOWN : 0; 220541016Sdfr z = (sc->ipacket[3] & MOUSE_PS2_XNEG) 220641016Sdfr ? sc->ipacket[4] - 256 : sc->ipacket[4]; 220741016Sdfr break; 220841016Sdfr 220941016Sdfr case MOUSE_MODEL_THINK: 221041016Sdfr /* the fourth button state in the first byte */ 221141016Sdfr ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0; 221241016Sdfr break; 221341016Sdfr 221449965Syokota case MOUSE_MODEL_VERSAPAD: 221549965Syokota /* VersaPad PS/2 absolute mode message format 221649965Syokota * 221749965Syokota * [packet1] 7 6 5 4 3 2 1 0(LSB) 221849965Syokota * ipacket[0]: 1 1 0 A 1 L T R 221949965Syokota * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0 222049965Syokota * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0 222149965Syokota * ipacket[3]: 1 1 1 A 1 L T R 222249965Syokota * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8 222349965Syokota * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0 222449965Syokota * 222549965Syokota * [note] 222649965Syokota * R: right physical mouse button (1=on) 222749965Syokota * T: touch pad virtual button (1=tapping) 222849965Syokota * L: left physical mouse button (1=on) 222949965Syokota * A: position data is valid (1=valid) 223049965Syokota * H: horizontal data (12bit signed integer. H11 is sign bit.) 223149965Syokota * V: vertical data (12bit signed integer. V11 is sign bit.) 223249965Syokota * P: pressure data 223349965Syokota * 223449965Syokota * Tapping is mapped to MOUSE_BUTTON4. 223549965Syokota */ 223649965Syokota ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS]; 223749965Syokota ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 223849965Syokota x = y = 0; 223949965Syokota if (c & MOUSE_PS2VERSA_IN_USE) { 224049965Syokota x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8); 224149965Syokota y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4); 224249965Syokota if (x0 & 0x800) 224349965Syokota x0 -= 0x1000; 224449965Syokota if (y0 & 0x800) 224549965Syokota y0 -= 0x1000; 224649965Syokota if (sc->flags & PSM_FLAGS_FINGERDOWN) { 224749965Syokota x = sc->xold - x0; 224849965Syokota y = y0 - sc->yold; 224949965Syokota if (x < 0) /* XXX */ 225049965Syokota x++; 225149965Syokota else if (x) 225249965Syokota x--; 225349965Syokota if (y < 0) 225449965Syokota y++; 225549965Syokota else if (y) 225649965Syokota y--; 225749965Syokota } else { 225849965Syokota sc->flags |= PSM_FLAGS_FINGERDOWN; 225949965Syokota } 226049965Syokota sc->xold = x0; 226149965Syokota sc->yold = y0; 226249965Syokota } else { 226349965Syokota sc->flags &= ~PSM_FLAGS_FINGERDOWN; 226449965Syokota } 226549965Syokota c = ((x < 0) ? MOUSE_PS2_XNEG : 0) 226649965Syokota | ((y < 0) ? MOUSE_PS2_YNEG : 0); 226749965Syokota break; 226849965Syokota 226958230Syokota case MOUSE_MODEL_4D: 227058230Syokota /* 227158230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 227258230Syokota * byte 1: s2 d2 s1 d1 1 M R L 227358230Syokota * byte 2: sx x x x x x x x 227458230Syokota * byte 3: sy y y y y y y y 227558230Syokota * 227658230Syokota * s1: wheel 1 direction 227758230Syokota * d1: wheel 1 data 227858230Syokota * s2: wheel 2 direction 227958230Syokota * d2: wheel 2 data 228058230Syokota */ 228158230Syokota x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1]; 228258230Syokota y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2]; 228358230Syokota switch (c & MOUSE_4D_WHEELBITS) { 228458230Syokota case 0x10: 228558230Syokota z = 1; 228658230Syokota break; 228758230Syokota case 0x30: 228858230Syokota z = -1; 228958230Syokota break; 229058230Syokota case 0x40: /* 2nd wheel turning right XXX */ 229158230Syokota z = 2; 229258230Syokota break; 229358230Syokota case 0xc0: /* 2nd wheel turning left XXX */ 229458230Syokota z = -2; 229558230Syokota break; 229658230Syokota } 229758230Syokota break; 229858230Syokota 229958230Syokota case MOUSE_MODEL_4DPLUS: 230058230Syokota if ((x < 16 - 256) && (y < 16 - 256)) { 230158230Syokota /* 230258230Syokota * b7 b6 b5 b4 b3 b2 b1 b0 230358230Syokota * byte 1: 0 0 1 1 1 M R L 230458230Syokota * byte 2: 0 0 0 0 1 0 0 0 230558230Syokota * byte 3: 0 0 0 0 S s d1 d0 230658230Syokota * 230758230Syokota * L, M, R, S: left, middle, right and side buttons 230858230Syokota * s: wheel data sign bit 230958230Syokota * d1-d0: wheel data 231058230Syokota */ 231158230Syokota x = y = 0; 231258230Syokota if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN) 231358230Syokota ms.button |= MOUSE_BUTTON4DOWN; 231458230Syokota z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG) 231558230Syokota ? ((sc->ipacket[2] & 0x07) - 8) 231658230Syokota : (sc->ipacket[2] & 0x07) ; 231758230Syokota } else { 231858230Syokota /* preserve previous button states */ 231958230Syokota ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 232058230Syokota } 232158230Syokota break; 232258230Syokota 232341016Sdfr case MOUSE_MODEL_GENERIC: 232441016Sdfr default: 232541016Sdfr break; 232641016Sdfr } 232741016Sdfr 232841016Sdfr /* scale values */ 232941016Sdfr if (sc->mode.accelfactor >= 1) { 233041016Sdfr if (x != 0) { 233141016Sdfr x = x * x / sc->mode.accelfactor; 233241016Sdfr if (x == 0) 233341016Sdfr x = 1; 233441016Sdfr if (c & MOUSE_PS2_XNEG) 233541016Sdfr x = -x; 233641016Sdfr } 233741016Sdfr if (y != 0) { 233841016Sdfr y = y * y / sc->mode.accelfactor; 233941016Sdfr if (y == 0) 234041016Sdfr y = 1; 234141016Sdfr if (c & MOUSE_PS2_YNEG) 234241016Sdfr y = -y; 234341016Sdfr } 234441016Sdfr } 234541016Sdfr 234641016Sdfr ms.dx = x; 234741016Sdfr ms.dy = y; 234841016Sdfr ms.dz = z; 234941016Sdfr ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) 235041016Sdfr | (ms.obutton ^ ms.button); 235141016Sdfr 235241016Sdfr if (sc->mode.level < PSM_LEVEL_NATIVE) 235341016Sdfr sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket); 235441016Sdfr 235541016Sdfr sc->status.flags |= ms.flags; 235641016Sdfr sc->status.dx += ms.dx; 235741016Sdfr sc->status.dy += ms.dy; 235841016Sdfr sc->status.dz += ms.dz; 235941016Sdfr sc->status.button = ms.button; 236041016Sdfr sc->button = ms.button; 236141016Sdfr 236258230Syokota sc->watchdog = FALSE; 236358230Syokota 236441016Sdfr /* queue data */ 236541016Sdfr if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) { 236641016Sdfr l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail); 236741016Sdfr bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l); 236841016Sdfr if (sc->inputbytes > l) 236941016Sdfr bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l); 237041016Sdfr sc->queue.tail = 237141016Sdfr (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf); 237241016Sdfr sc->queue.count += sc->inputbytes; 237341016Sdfr } 237441016Sdfr sc->inputbytes = 0; 237541016Sdfr 237641016Sdfr if (sc->state & PSM_ASLP) { 237741016Sdfr sc->state &= ~PSM_ASLP; 237841016Sdfr wakeup((caddr_t) sc); 237941016Sdfr } 238041016Sdfr selwakeup(&sc->rsel); 238141016Sdfr } 238241016Sdfr} 238341016Sdfr 238441016Sdfrstatic int 238583366Sjulianpsmpoll(dev_t dev, int events, struct thread *td) 238641016Sdfr{ 238741016Sdfr struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 238841016Sdfr int s; 238941016Sdfr int revents = 0; 239041016Sdfr 239141016Sdfr /* Return true if a mouse event available */ 239241016Sdfr s = spltty(); 239345789Speter if (events & (POLLIN | POLLRDNORM)) { 239441016Sdfr if (sc->queue.count > 0) 239541016Sdfr revents |= events & (POLLIN | POLLRDNORM); 239641016Sdfr else 239783366Sjulian selrecord(td, &sc->rsel); 239845789Speter } 239941016Sdfr splx(s); 240041016Sdfr 240141016Sdfr return (revents); 240241016Sdfr} 240341016Sdfr 240441016Sdfr/* vendor/model specific routines */ 240541016Sdfr 240641016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status) 240741016Sdfr{ 240841016Sdfr if (set_mouse_resolution(kbdc, res) != res) 240941016Sdfr return FALSE; 241041016Sdfr if (set_mouse_scaling(kbdc, scale) 241141016Sdfr && set_mouse_scaling(kbdc, scale) 241241016Sdfr && set_mouse_scaling(kbdc, scale) 241341016Sdfr && (get_mouse_status(kbdc, status, 0, 3) >= 3)) 241441016Sdfr return TRUE; 241541016Sdfr return FALSE; 241641016Sdfr} 241741016Sdfr 241869438Syokotastatic int 241969438Syokotamouse_ext_command(KBDC kbdc, int command) 242069438Syokota{ 242169438Syokota int c; 242269438Syokota 242369438Syokota c = (command >> 6) & 0x03; 242469438Syokota if (set_mouse_resolution(kbdc, c) != c) 242569438Syokota return FALSE; 242669438Syokota c = (command >> 4) & 0x03; 242769438Syokota if (set_mouse_resolution(kbdc, c) != c) 242869438Syokota return FALSE; 242969438Syokota c = (command >> 2) & 0x03; 243069438Syokota if (set_mouse_resolution(kbdc, c) != c) 243169438Syokota return FALSE; 243269438Syokota c = (command >> 0) & 0x03; 243369438Syokota if (set_mouse_resolution(kbdc, c) != c) 243469438Syokota return FALSE; 243569438Syokota return TRUE; 243669438Syokota} 243769438Syokota 243841016Sdfr#if notyet 243941016Sdfr/* Logitech MouseMan Cordless II */ 244041016Sdfrstatic int 244141016Sdfrenable_lcordless(struct psm_softc *sc) 244241016Sdfr{ 244341016Sdfr int status[3]; 244441016Sdfr int ch; 244541016Sdfr 244641016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status)) 244741016Sdfr return FALSE; 244841016Sdfr if (status[1] == PSMD_RES_HIGH) 244941016Sdfr return FALSE; 245041016Sdfr ch = (status[0] & 0x07) - 1; /* channel # */ 245141016Sdfr if ((ch <= 0) || (ch > 4)) 245241016Sdfr return FALSE; 245341016Sdfr /* 245441016Sdfr * status[1]: always one? 245541016Sdfr * status[2]: battery status? (0-100) 245641016Sdfr */ 245741016Sdfr return TRUE; 245841016Sdfr} 245941016Sdfr#endif /* notyet */ 246041016Sdfr 246158230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */ 246241016Sdfrstatic int 246341016Sdfrenable_groller(struct psm_softc *sc) 246441016Sdfr{ 246541016Sdfr int status[3]; 246641016Sdfr 246741016Sdfr /* 246841016Sdfr * The special sequence to enable the fourth button and the 246941016Sdfr * roller. Immediately after this sequence check status bytes. 247041016Sdfr * if the mouse is NetScroll, the second and the third bytes are 247141016Sdfr * '3' and 'D'. 247241016Sdfr */ 247341016Sdfr 247441016Sdfr /* 247541016Sdfr * If the mouse is an ordinary PS/2 mouse, the status bytes should 247641016Sdfr * look like the following. 247741016Sdfr * 247841016Sdfr * byte 1 bit 7 always 0 247941016Sdfr * bit 6 stream mode (0) 248041016Sdfr * bit 5 disabled (0) 248141016Sdfr * bit 4 1:1 scaling (0) 248241016Sdfr * bit 3 always 0 248341016Sdfr * bit 0-2 button status 248441016Sdfr * byte 2 resolution (PSMD_RES_HIGH) 248541016Sdfr * byte 3 report rate (?) 248641016Sdfr */ 248741016Sdfr 248841016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 248941016Sdfr return FALSE; 249041016Sdfr if ((status[1] != '3') || (status[2] != 'D')) 249141016Sdfr return FALSE; 249258230Syokota /* FIXME: SmartScroll Mouse has 5 buttons! XXX */ 249341016Sdfr sc->hw.buttons = 4; 249441016Sdfr return TRUE; 249541016Sdfr} 249641016Sdfr 249758230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */ 249841016Sdfrstatic int 249941016Sdfrenable_gmouse(struct psm_softc *sc) 250041016Sdfr{ 250141016Sdfr int status[3]; 250241016Sdfr 250341016Sdfr /* 250441016Sdfr * The special sequence to enable the middle, "rubber" button. 250541016Sdfr * Immediately after this sequence check status bytes. 250641016Sdfr * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse, 250741016Sdfr * the second and the third bytes are '3' and 'U'. 250841016Sdfr * NOTE: NetMouse reports that it has three buttons although it has 250941016Sdfr * two buttons and a rubber button. NetMouse Pro and MIE Mouse 251041016Sdfr * say they have three buttons too and they do have a button on the 251141016Sdfr * side... 251241016Sdfr */ 251341016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status)) 251441016Sdfr return FALSE; 251541016Sdfr if ((status[1] != '3') || (status[2] != 'U')) 251641016Sdfr return FALSE; 251741016Sdfr return TRUE; 251841016Sdfr} 251941016Sdfr 252041016Sdfr/* ALPS GlidePoint */ 252141016Sdfrstatic int 252241016Sdfrenable_aglide(struct psm_softc *sc) 252341016Sdfr{ 252441016Sdfr int status[3]; 252541016Sdfr 252641016Sdfr /* 252741016Sdfr * The special sequence to obtain ALPS GlidePoint specific 252841016Sdfr * information. Immediately after this sequence, status bytes will 252941016Sdfr * contain something interesting. 253041016Sdfr * NOTE: ALPS produces several models of GlidePoint. Some of those 253141016Sdfr * do not respond to this sequence, thus, cannot be detected this way. 253241016Sdfr */ 253350149Syokota if (set_mouse_sampling_rate(sc->kbdc, 100) != 100) 253450149Syokota return FALSE; 253541016Sdfr if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status)) 253641016Sdfr return FALSE; 253750149Syokota if ((status[1] == PSMD_RES_LOW) || (status[2] == 100)) 253841016Sdfr return FALSE; 253941016Sdfr return TRUE; 254041016Sdfr} 254141016Sdfr 254241016Sdfr/* Kensington ThinkingMouse/Trackball */ 254341016Sdfrstatic int 254441016Sdfrenable_kmouse(struct psm_softc *sc) 254541016Sdfr{ 254641016Sdfr static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 }; 254741016Sdfr KBDC kbdc = sc->kbdc; 254841016Sdfr int status[3]; 254941016Sdfr int id1; 255041016Sdfr int id2; 255141016Sdfr int i; 255241016Sdfr 255341016Sdfr id1 = get_aux_id(kbdc); 255441016Sdfr if (set_mouse_sampling_rate(kbdc, 10) != 10) 255541016Sdfr return FALSE; 255641016Sdfr /* 255741016Sdfr * The device is now in the native mode? It returns a different 255841016Sdfr * ID value... 255941016Sdfr */ 256041016Sdfr id2 = get_aux_id(kbdc); 256141016Sdfr if ((id1 == id2) || (id2 != 2)) 256241016Sdfr return FALSE; 256341016Sdfr 256441016Sdfr if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 256541016Sdfr return FALSE; 256641016Sdfr#if PSM_DEBUG >= 2 256741016Sdfr /* at this point, resolution is LOW, sampling rate is 10/sec */ 256841016Sdfr if (get_mouse_status(kbdc, status, 0, 3) < 3) 256941016Sdfr return FALSE; 257041016Sdfr#endif 257141016Sdfr 257241016Sdfr /* 257341016Sdfr * The special sequence to enable the third and fourth buttons. 257441016Sdfr * Otherwise they behave like the first and second buttons. 257541016Sdfr */ 257641016Sdfr for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 257741016Sdfr if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 257841016Sdfr return FALSE; 257941016Sdfr } 258041016Sdfr 258141016Sdfr /* 258241016Sdfr * At this point, the device is using default resolution and 258341016Sdfr * sampling rate for the native mode. 258441016Sdfr */ 258541016Sdfr if (get_mouse_status(kbdc, status, 0, 3) < 3) 258641016Sdfr return FALSE; 258741016Sdfr if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1])) 258841016Sdfr return FALSE; 258941016Sdfr 259041016Sdfr /* the device appears be enabled by this sequence, diable it for now */ 259141016Sdfr disable_aux_dev(kbdc); 259241016Sdfr empty_aux_buffer(kbdc, 5); 259341016Sdfr 259441016Sdfr return TRUE; 259541016Sdfr} 259641016Sdfr 259758230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */ 259841016Sdfrstatic int 259941016Sdfrenable_mmanplus(struct psm_softc *sc) 260041016Sdfr{ 260141016Sdfr KBDC kbdc = sc->kbdc; 260241016Sdfr int data[3]; 260341016Sdfr 260441016Sdfr /* the special sequence to enable the fourth button and the roller. */ 260558230Syokota /* 260658230Syokota * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION 260758230Syokota * must be called exactly three times since the last RESET command 260858230Syokota * before this sequence. XXX 260958230Syokota */ 261069438Syokota if (!set_mouse_scaling(kbdc, 1)) 261169438Syokota return FALSE; 261269438Syokota if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb)) 261369438Syokota return FALSE; 261441016Sdfr if (get_mouse_status(kbdc, data, 1, 3) < 3) 261541016Sdfr return FALSE; 261641016Sdfr 261748778Syokota /* 261848778Syokota * PS2++ protocl, packet type 0 261941016Sdfr * 262048778Syokota * b7 b6 b5 b4 b3 b2 b1 b0 262148778Syokota * byte 1: * 1 p3 p2 1 * * * 262248778Syokota * byte 2: 1 1 p1 p0 m1 m0 1 0 262348778Syokota * byte 3: m7 m6 m5 m4 m3 m2 m1 m0 262448778Syokota * 262548778Syokota * p3-p0: packet type: 0 262658230Syokota * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58... 262741016Sdfr */ 262848778Syokota /* check constant bits */ 262948778Syokota if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC) 263041016Sdfr return FALSE; 263148778Syokota if ((data[1] & 0xc3) != 0xc2) 263248778Syokota return FALSE; 263348778Syokota /* check d3-d0 in byte 2 */ 263448778Syokota if (!MOUSE_PS2PLUS_CHECKBITS(data)) 263548778Syokota return FALSE; 263648778Syokota /* check p3-p0 */ 263748778Syokota if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0) 263848778Syokota return FALSE; 263941016Sdfr 264048778Syokota sc->hw.hwid &= 0x00ff; 264148778Syokota sc->hw.hwid |= data[2] << 8; /* save model ID */ 264248778Syokota 264341016Sdfr /* 264441016Sdfr * MouseMan+ (or FirstMouse+) is now in its native mode, in which 264541016Sdfr * the wheel and the fourth button events are encoded in the 264641016Sdfr * special data packet. The mouse may be put in the IntelliMouse mode 264741016Sdfr * if it is initialized by the IntelliMouse's method. 264841016Sdfr */ 264941016Sdfr return TRUE; 265041016Sdfr} 265141016Sdfr 265258230Syokota/* MS IntelliMouse Explorer */ 265358230Syokotastatic int 265458230Syokotaenable_msexplorer(struct psm_softc *sc) 265558230Syokota{ 265658923Syokota static unsigned char rate0[] = { 200, 100, 80, }; 265758923Syokota static unsigned char rate1[] = { 200, 200, 80, }; 265858230Syokota KBDC kbdc = sc->kbdc; 265958230Syokota int id; 266058230Syokota int i; 266158230Syokota 266269438Syokota /* the special sequence to enable the extra buttons and the roller. */ 266369438Syokota for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) { 266469438Syokota if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i]) 266569438Syokota return FALSE; 266669438Syokota } 266769438Syokota /* the device will give the genuine ID only after the above sequence */ 266869438Syokota id = get_aux_id(kbdc); 266969438Syokota if (id != PSM_EXPLORER_ID) 267069438Syokota return FALSE; 267169438Syokota 267269438Syokota sc->hw.hwid = id; 267369438Syokota sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */ 267469438Syokota 267558923Syokota /* 267658923Syokota * XXX: this is a kludge to fool some KVM switch products 267758923Syokota * which think they are clever enough to know the 4-byte IntelliMouse 267858923Syokota * protocol, and assume any other protocols use 3-byte packets. 267958923Syokota * They don't convey 4-byte data packets from the IntelliMouse Explorer 268058923Syokota * correctly to the host computer because of this! 268158923Syokota * The following sequence is actually IntelliMouse's "wake up" 268258923Syokota * sequence; it will make the KVM think the mouse is IntelliMouse 268358923Syokota * when it is in fact IntelliMouse Explorer. 268458923Syokota */ 268558923Syokota for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) { 268658923Syokota if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i]) 268769438Syokota break; 268858923Syokota } 268958923Syokota id = get_aux_id(kbdc); 269058923Syokota 269158230Syokota return TRUE; 269258230Syokota} 269358230Syokota 269441016Sdfr/* MS IntelliMouse */ 269541016Sdfrstatic int 269641016Sdfrenable_msintelli(struct psm_softc *sc) 269741016Sdfr{ 269841016Sdfr /* 269941016Sdfr * Logitech MouseMan+ and FirstMouse+ will also respond to this 270041016Sdfr * probe routine and act like IntelliMouse. 270141016Sdfr */ 270241016Sdfr 270341016Sdfr static unsigned char rate[] = { 200, 100, 80, }; 270441016Sdfr KBDC kbdc = sc->kbdc; 270541016Sdfr int id; 270641016Sdfr int i; 270741016Sdfr 270841016Sdfr /* the special sequence to enable the third button and the roller. */ 270941016Sdfr for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 271041016Sdfr if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 271141016Sdfr return FALSE; 271241016Sdfr } 271341016Sdfr /* the device will give the genuine ID only after the above sequence */ 271441016Sdfr id = get_aux_id(kbdc); 271541016Sdfr if (id != PSM_INTELLI_ID) 271641016Sdfr return FALSE; 271741016Sdfr 271841016Sdfr sc->hw.hwid = id; 271941016Sdfr sc->hw.buttons = 3; 272041016Sdfr 272141016Sdfr return TRUE; 272241016Sdfr} 272341016Sdfr 272458230Syokota/* A4 Tech 4D Mouse */ 272558230Syokotastatic int 272658230Syokotaenable_4dmouse(struct psm_softc *sc) 272758230Syokota{ 272858230Syokota /* 272958230Syokota * Newer wheel mice from A4 Tech may use the 4D+ protocol. 273058230Syokota */ 273158230Syokota 273258230Syokota static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 273358230Syokota KBDC kbdc = sc->kbdc; 273458230Syokota int id; 273558230Syokota int i; 273658230Syokota 273758230Syokota for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 273858230Syokota if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 273958230Syokota return FALSE; 274058230Syokota } 274158230Syokota id = get_aux_id(kbdc); 274258230Syokota /* 274358923Syokota * WinEasy 4D, 4 Way Scroll 4D: 6 274458230Syokota * Cable-Free 4D: 8 (4DPLUS) 274558923Syokota * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS) 274658230Syokota */ 274758230Syokota if (id != PSM_4DMOUSE_ID) 274858230Syokota return FALSE; 274958230Syokota 275058230Syokota sc->hw.hwid = id; 275158230Syokota sc->hw.buttons = 3; /* XXX some 4D mice have 4? */ 275258230Syokota 275358230Syokota return TRUE; 275458230Syokota} 275558230Syokota 275658230Syokota/* A4 Tech 4D+ Mouse */ 275758230Syokotastatic int 275858230Syokotaenable_4dplus(struct psm_softc *sc) 275958230Syokota{ 276058230Syokota /* 276158230Syokota * Newer wheel mice from A4 Tech seem to use this protocol. 276258230Syokota * Older models are recognized as either 4D Mouse or IntelliMouse. 276358230Syokota */ 276458230Syokota KBDC kbdc = sc->kbdc; 276558230Syokota int id; 276658230Syokota 276758230Syokota /* 276858230Syokota * enable_4dmouse() already issued the following ID sequence... 276958230Syokota static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 }; 277058230Syokota int i; 277158230Syokota 277258230Syokota for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) { 277358230Syokota if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 277458230Syokota return FALSE; 277558230Syokota } 277658230Syokota */ 277758230Syokota 277858230Syokota id = get_aux_id(kbdc); 277958230Syokota if (id != PSM_4DPLUS_ID) 278058230Syokota return FALSE; 278158230Syokota 278258230Syokota sc->hw.hwid = id; 278358230Syokota sc->hw.buttons = 4; /* XXX */ 278458230Syokota 278558230Syokota return TRUE; 278658230Syokota} 278758230Syokota 278849965Syokota/* Interlink electronics VersaPad */ 278949965Syokotastatic int 279049965Syokotaenable_versapad(struct psm_softc *sc) 279149965Syokota{ 279249965Syokota KBDC kbdc = sc->kbdc; 279349965Syokota int data[3]; 279449965Syokota 279549965Syokota set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */ 279649965Syokota set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */ 279749965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 279849965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 279949965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 280049965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 280149965Syokota if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */ 280249965Syokota return FALSE; 280349965Syokota if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */ 280449965Syokota return FALSE; 280549965Syokota set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 280649965Syokota 280758230Syokota sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 280858230Syokota 280949965Syokota return TRUE; /* PS/2 absolute mode */ 281049965Syokota} 281149965Syokota 281241016Sdfrstatic int 281354629Syokotapsmresume(device_t dev) 281441016Sdfr{ 281554629Syokota struct psm_softc *sc = device_get_softc(dev); 281654629Syokota int unit = device_get_unit(dev); 281784880Syokota int err; 281841016Sdfr 281941016Sdfr if (verbose >= 2) 282054629Syokota log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit); 282141016Sdfr 282258230Syokota if (!(sc->config & PSM_CONFIG_HOOKRESUME)) 282358230Syokota return (0); 282458230Syokota 282584880Syokota err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND); 282641016Sdfr 282741016Sdfr if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) { 282841016Sdfr /* 282941016Sdfr * Release the blocked process; it must be notified that the device 283041016Sdfr * cannot be accessed anymore. 283141016Sdfr */ 283241016Sdfr sc->state &= ~PSM_ASLP; 283341016Sdfr wakeup((caddr_t)sc); 283441016Sdfr } 283541016Sdfr 283641016Sdfr if (verbose >= 2) 283754629Syokota log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit); 283841016Sdfr 283941016Sdfr return (err); 284041016Sdfr} 284141016Sdfr 284252997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0); 284383147Syokota 284483147Syokota/* 284583147Syokota * This sucks up assignments from PNPBIOS and ACPI. 284683147Syokota */ 284783147Syokota 284883931Syokota/* 284983931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may 285083931Syokota * appear BEFORE the AT keyboard controller. As the PS/2 mouse device 285183931Syokota * can be probed and attached only after the AT keyboard controller is 285283931Syokota * attached, we shall quietly reserve the IRQ resource for later use. 285383931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller, 285483931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging 285583931Syokota * under the keyboard controller, then probe and attach it. 285683931Syokota */ 285783147Syokota 285883147Syokotastatic devclass_t psmcpnp_devclass; 285983147Syokota 286083147Syokotastatic device_probe_t psmcpnp_probe; 286183147Syokotastatic device_attach_t psmcpnp_attach; 286283147Syokota 286383147Syokotastatic device_method_t psmcpnp_methods[] = { 286483147Syokota DEVMETHOD(device_probe, psmcpnp_probe), 286583147Syokota DEVMETHOD(device_attach, psmcpnp_attach), 286683147Syokota 286783147Syokota { 0, 0 } 286883147Syokota}; 286983147Syokota 287083147Syokotastatic driver_t psmcpnp_driver = { 287183147Syokota PSMCPNP_DRIVER_NAME, 287283147Syokota psmcpnp_methods, 287383147Syokota 1, /* no softc */ 287483147Syokota}; 287583147Syokota 287683147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = { 287788188Ssheldonh { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */ 287883147Syokota { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */ 287983147Syokota { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */ 288083492Syokota { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */ 288184407Stakawata { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */ 288292000Stakawata { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio*/ 288383147Syokota { 0 } 288483147Syokota}; 288583147Syokota 288683147Syokotastatic int 288783147Syokotacreate_a_copy(device_t atkbdc, device_t me) 288883147Syokota{ 288983147Syokota device_t psm; 289083147Syokota u_long irq; 289183147Syokota 289283931Syokota /* find the PS/2 mouse device instance under the keyboard controller */ 289383931Syokota psm = device_find_child(atkbdc, PSM_DRIVER_NAME, 289483931Syokota device_get_unit(atkbdc)); 289583147Syokota if (psm == NULL) 289683147Syokota return ENXIO; 289783931Syokota if (device_get_state(psm) != DS_NOTPRESENT) 289883931Syokota return 0; 289983147Syokota 290083931Syokota /* move our resource to the found device */ 290183931Syokota irq = bus_get_resource_start(me, SYS_RES_IRQ, 0); 290283147Syokota bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 290383147Syokota 290483147Syokota /* ...then probe and attach it */ 290583147Syokota return device_probe_and_attach(psm); 290683147Syokota} 290783147Syokota 290883147Syokotastatic int 290983147Syokotapsmcpnp_probe(device_t dev) 291083147Syokota{ 291183931Syokota struct resource *res; 291283931Syokota u_long irq; 291383931Syokota int rid; 291483147Syokota 291583147Syokota if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids)) 291683147Syokota return ENXIO; 291783147Syokota 291883931Syokota /* 291983931Syokota * The PnP BIOS and ACPI are supposed to assign an IRQ (12) 292083931Syokota * to the PS/2 mouse device node. But, some buggy PnP BIOS 292183931Syokota * declares the PS/2 mouse device node without an IRQ resource! 292283931Syokota * If this happens, we shall refer to device hints. 292383931Syokota * If we still don't find it there, use a hardcoded value... XXX 292483931Syokota */ 292583931Syokota rid = 0; 292683931Syokota irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid); 292783931Syokota if (irq <= 0) { 292883931Syokota if (resource_long_value(PSM_DRIVER_NAME, 292983931Syokota device_get_unit(dev), "irq", &irq) != 0) 293083931Syokota irq = 12; /* XXX */ 293183931Syokota device_printf(dev, "irq resource info is missing; " 293283931Syokota "assuming irq %ld\n", irq); 293383931Syokota bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1); 293483931Syokota } 293583931Syokota res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 293683931Syokota RF_SHAREABLE); 293783931Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, res); 293883147Syokota 293983147Syokota /* keep quiet */ 294083147Syokota if (!bootverbose) 294183147Syokota device_quiet(dev); 294283492Syokota 294383931Syokota return ((res == NULL) ? ENXIO : 0); 294483147Syokota} 294583147Syokota 294683147Syokotastatic int 294783147Syokotapsmcpnp_attach(device_t dev) 294883147Syokota{ 294983492Syokota device_t atkbdc; 295083931Syokota int rid; 295183147Syokota 295283931Syokota /* find the keyboard controller, which may be on acpi* or isa* bus */ 295383931Syokota atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME), 295483931Syokota device_get_unit(dev)); 295583931Syokota if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) { 295683492Syokota create_a_copy(atkbdc, dev); 295783931Syokota } else { 295883931Syokota /* 295983931Syokota * If we don't have the AT keyboard controller yet, 296083931Syokota * just reserve the IRQ for later use... 296183931Syokota * (See psmidentify() above.) 296283931Syokota */ 296383931Syokota rid = 0; 296483931Syokota bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 296583931Syokota RF_SHAREABLE); 296683931Syokota } 296783492Syokota 296883147Syokota return 0; 296983147Syokota} 297083147Syokota 297183147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0); 297283147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0); 2973