2 * Copyright (c) 1992, 1993 Erik Forsberg.
3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
15 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 * $FreeBSD: src/sys/isa/psm.c,v 1.23.2.6 2002/03/27 16:53:35 pb Exp $
27 * Ported to 386bsd Oct 17, 1992
28 * Sandi Donno, Computer Science, University of Cape Town, South Africa
29 * Please send bug reports to sandi@cs.uct.ac.za
31 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
32 * although I was only partially successful in getting the alpha release
33 * of his "driver for the Logitech and ATI Inport Bus mice for use with
34 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
35 * found his code to be an invaluable reference when porting this driver
38 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
39 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
41 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
42 * Andrew Herbert - 12 June 1993
44 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
47 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
50 * Hardware access routines and probe logic rewritten by
51 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
52 * - 3, 14, 22 October 1996.
53 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
54 * - 14, 30 November 1996. Uses `kbdio.c'.
55 * - 13 December 1996. Uses queuing version of `kbdio.c'.
56 * - January/February 1997. Tweaked probe logic for
57 * HiNote UltraII/Latitude/Armada laptops.
58 * - 30 July 1997. Added APM support.
59 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
60 * Improved sync check logic.
61 * Vendor specific support routines.
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/module.h>
73 #include <sys/syslog.h>
74 #include <sys/malloc.h>
75 #include <machine/bus.h>
77 #include <sys/select.h>
81 #include <machine/clock.h>
82 #include <machine/limits.h>
83 #include <machine/mouse.h>
84 #include <machine/resource.h>
86 #include <isa/isavar.h>
87 #include <dev/kbd/atkbdcreg.h>
90 * Driver specific options: the following options may be set by
91 * `options' statements in the kernel configuration file.
96 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */
99 #ifndef PSM_SYNCERR_THRESHOLD1
100 #define PSM_SYNCERR_THRESHOLD1 20
103 #ifndef PSM_INPUT_TIMEOUT
104 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
107 /* end of driver specific options */
109 #define PSM_DRIVER_NAME "psm"
110 #define PSMCPNP_DRIVER_NAME "psmcpnp"
113 #define PSM_BUFSIZE 960
114 #define PSM_SMALLBUFSIZE 240
116 /* operation levels */
117 #define PSM_LEVEL_BASE 0
118 #define PSM_LEVEL_STANDARD 1
119 #define PSM_LEVEL_NATIVE 2
120 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
121 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
123 /* Logitech PS2++ protocol */
124 #define MOUSE_PS2PLUS_CHECKBITS(b) \
125 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
126 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
127 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
130 #define PSM_UNIT(dev) (minor(dev) >> 1)
131 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
132 #define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1))
135 #define max(x,y) ((x) > (y) ? (x) : (y))
138 #define min(x,y) ((x) < (y) ? (x) : (y))
141 #define abs(x) (((x) < 0) ? -(x) : (x))
144 typedef struct ringbuf {
145 int count; /* # of valid elements in the buffer */
146 int head; /* head pointer */
147 int tail; /* tail poiner */
148 unsigned char buf[PSM_BUFSIZE];
151 /* driver control block */
152 struct psm_softc { /* Driver status information */
154 struct selinfo rsel; /* Process selecting for Input */
155 unsigned char state; /* Mouse driver state */
156 int config; /* driver configuration flags */
157 int flags; /* other flags */
158 KBDC kbdc; /* handle to access the keyboard controller */
159 struct resource *intr; /* IRQ resource */
160 void *ih; /* interrupt handle */
161 mousehw_t hw; /* hardware information */
162 mousemode_t mode; /* operation mode */
163 mousemode_t dflt_mode; /* default operation mode */
164 mousestatus_t status; /* accumulated mouse movement */
165 ringbuf_t queue; /* mouse status queue */
166 unsigned char ipacket[16]; /* interim input buffer */
167 int inputbytes; /* # of bytes in the input buffer */
168 int button; /* the latest button state */
169 int xold; /* previous absolute X position */
170 int yold; /* previous absolute Y position */
172 struct timeval inputtimeout;
173 int watchdog; /* watchdog timer flag */
174 struct callout_handle callout; /* watchdog timer call out */
178 devclass_t psm_devclass;
179 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
181 /* driver state flags (state) */
182 #define PSM_VALID 0x80
183 #define PSM_OPEN 1 /* Device is open */
184 #define PSM_ASLP 2 /* Waiting for mouse data */
186 /* driver configuration flags (config) */
187 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
188 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
189 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
190 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
191 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
192 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
193 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
194 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
195 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
196 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
198 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \
200 | PSM_CONFIG_NOCHECKSYNC \
201 | PSM_CONFIG_SYNCHACK \
202 | PSM_CONFIG_NOIDPROBE \
203 | PSM_CONFIG_NORESET \
204 | PSM_CONFIG_FORCETAP \
205 | PSM_CONFIG_IGNPORTERROR \
206 | PSM_CONFIG_HOOKRESUME \
207 | PSM_CONFIG_INITAFTERSUSPEND)
209 /* other flags (flags) */
210 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
212 /* for backward compatibility */
213 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
214 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
215 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
217 typedef struct old_mousehw {
224 typedef struct old_mousemode {
231 /* packet formatting function */
232 typedef int packetfunc_t __P((struct psm_softc *, unsigned char *,
233 int *, int, mousestatus_t *));
235 /* function prototypes */
236 static int psmprobe __P((device_t));
237 static int psmattach __P((device_t));
238 static int psmdetach __P((device_t));
239 static int psmresume __P((device_t));
241 static d_open_t psmopen;
242 static d_close_t psmclose;
243 static d_read_t psmread;
244 static d_ioctl_t psmioctl;
245 static d_poll_t psmpoll;
247 static int enable_aux_dev __P((KBDC));
248 static int disable_aux_dev __P((KBDC));
249 static int get_mouse_status __P((KBDC, int *, int, int));
250 static int get_aux_id __P((KBDC));
251 static int set_mouse_sampling_rate __P((KBDC, int));
252 static int set_mouse_scaling __P((KBDC, int));
253 static int set_mouse_resolution __P((KBDC, int));
254 static int set_mouse_mode __P((KBDC));
255 static int get_mouse_buttons __P((KBDC));
256 static int is_a_mouse __P((int));
257 static void recover_from_error __P((KBDC));
258 static int restore_controller __P((KBDC, int));
259 static int doinitialize __P((struct psm_softc *, mousemode_t *));
260 static int doopen __P((struct psm_softc *, int));
261 static int reinitialize __P((struct psm_softc *, int));
262 static char *model_name __P((int));
263 static void psmintr __P((void *));
264 static void psmtimeout __P((void *));
266 /* vendor specific features */
267 typedef int probefunc_t __P((struct psm_softc *));
269 static int mouse_id_proc1 __P((KBDC, int, int, int *));
270 static int mouse_ext_command __P((KBDC, int));
271 static probefunc_t enable_groller;
272 static probefunc_t enable_gmouse;
273 static probefunc_t enable_aglide;
274 static probefunc_t enable_kmouse;
275 static probefunc_t enable_msexplorer;
276 static probefunc_t enable_msintelli;
277 static probefunc_t enable_4dmouse;
278 static probefunc_t enable_4dplus;
279 static probefunc_t enable_mmanplus;
280 static probefunc_t enable_versapad;
281 static int tame_mouse __P((struct psm_softc *, mousestatus_t *, unsigned char *));
285 unsigned char syncmask;
287 probefunc_t *probefunc;
290 * WARNING: the order of probe is very important. Don't mess it
291 * unless you know what you are doing.
293 { MOUSE_MODEL_NET, /* Genius NetMouse */
294 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
295 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
296 0xc8, 6, enable_groller, },
297 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
298 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
299 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
300 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
301 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
302 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
303 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
304 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
305 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
306 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
307 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
308 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
309 { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */
310 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
311 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
312 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
313 { MOUSE_MODEL_GENERIC,
314 0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
316 #define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
318 /* device driver declarateion */
319 static device_method_t psm_methods[] = {
320 /* Device interface */
321 DEVMETHOD(device_probe, psmprobe),
322 DEVMETHOD(device_attach, psmattach),
323 DEVMETHOD(device_detach, psmdetach),
324 DEVMETHOD(device_resume, psmresume),
329 static driver_t psm_driver = {
332 sizeof(struct psm_softc),
336 static struct isa_pnp_id psm_ids[] = {
337 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
338 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
343 #define CDEV_MAJOR 21
345 static struct cdevsw psm_cdevsw = {
347 /* close */ psmclose,
350 /* ioctl */ psmioctl,
353 /* strategy */ nostrategy,
354 /* name */ PSM_DRIVER_NAME,
355 /* maj */ CDEV_MAJOR,
362 /* debug message level */
363 static int verbose = PSM_DEBUG;
365 /* device I/O routines */
367 enable_aux_dev(KBDC kbdc)
371 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
373 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
375 return (res == PSM_ACK);
379 disable_aux_dev(KBDC kbdc)
383 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
385 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
387 return (res == PSM_ACK);
391 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
400 cmd = PSMC_SEND_DEV_STATUS;
403 cmd = PSMC_SEND_DEV_DATA;
406 empty_aux_buffer(kbdc, 5);
407 res = send_aux_command(kbdc, cmd);
409 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
410 (flag == 1) ? "DATA" : "STATUS", res);
414 for (i = 0; i < len; ++i) {
415 status[i] = read_aux_data(kbdc);
421 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
422 (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
429 get_aux_id(KBDC kbdc)
434 empty_aux_buffer(kbdc, 5);
435 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
437 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
444 id = read_aux_data(kbdc);
446 log(LOG_DEBUG, "psm: device ID: %04x\n", id);
452 set_mouse_sampling_rate(KBDC kbdc, int rate)
456 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
458 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
460 return ((res == PSM_ACK) ? rate : -1);
464 set_mouse_scaling(KBDC kbdc, int scale)
471 scale = PSMC_SET_SCALING11;
474 scale = PSMC_SET_SCALING21;
477 res = send_aux_command(kbdc, scale);
479 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
480 (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
482 return (res == PSM_ACK);
485 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
487 set_mouse_resolution(KBDC kbdc, int val)
491 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
493 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
495 return ((res == PSM_ACK) ? val : -1);
499 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
500 * re-enabled by calling `enable_aux_dev()'
503 set_mouse_mode(KBDC kbdc)
507 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
509 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
511 return (res == PSM_ACK);
515 get_mouse_buttons(KBDC kbdc)
517 int c = 2; /* assume two buttons by default */
521 * NOTE: a special sequence to obtain Logitech Mouse specific
522 * information: set resolution to 25 ppi, set scaling to 1:1, set
523 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
524 * mouse status bytes is the number of available buttons.
525 * Some manufactures also support this sequence.
527 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
529 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
530 && set_mouse_scaling(kbdc, 1)
531 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
538 /* misc subroutines */
540 * Someday, I will get the complete list of valid pointing devices and
547 static int valid_ids[] = {
548 PSM_MOUSE_ID, /* mouse */
549 PSM_BALLPOINT_ID, /* ballpoint device */
550 PSM_INTELLI_ID, /* Intellimouse */
551 PSM_EXPLORER_ID, /* Intellimouse Explorer */
552 -1 /* end of table */
556 for (i = 0; valid_ids[i] >= 0; ++i)
557 if (valid_ids[i] == id)
566 model_name(int model)
572 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
573 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
574 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
575 { MOUSE_MODEL_THINK, "ThinkingMouse" },
576 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
577 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
578 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
579 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
580 { MOUSE_MODEL_4D, "4D Mouse" },
581 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
582 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
583 { MOUSE_MODEL_UNKNOWN, NULL },
587 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
588 if (models[i].model_code == model)
589 return models[i].model_name;
595 recover_from_error(KBDC kbdc)
597 /* discard anything left in the output buffer */
598 empty_both_buffers(kbdc, 10);
602 * NOTE: KBDC_RESET_KBD may not restore the communication between the
603 * keyboard and the controller.
608 * NOTE: somehow diagnostic and keyboard port test commands bring the
611 if (!test_controller(kbdc))
612 log(LOG_ERR, "psm: keyboard controller failed.\n");
613 /* if there isn't a keyboard in the system, the following error is OK */
614 if (test_kbd_port(kbdc) != 0) {
616 log(LOG_ERR, "psm: keyboard port failed.\n");
622 restore_controller(KBDC kbdc, int command_byte)
624 empty_both_buffers(kbdc, 10);
626 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
627 log(LOG_ERR, "psm: failed to restore the keyboard controller "
629 empty_both_buffers(kbdc, 10);
632 empty_both_buffers(kbdc, 10);
638 * Re-initialize the aux port and device. The aux port must be enabled
639 * and its interrupt must be disabled before calling this routine.
640 * The aux device will be disabled before returning.
641 * The keyboard controller must be locked via `kbdc_lock()' before
642 * calling this routine.
645 doinitialize(struct psm_softc *sc, mousemode_t *mode)
647 KBDC kbdc = sc->kbdc;
651 switch((i = test_aux_port(kbdc))) {
652 case 1: /* ignore this error */
655 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
658 case 0: /* no error */
660 case -1: /* time out */
662 recover_from_error(kbdc);
663 if (sc->config & PSM_CONFIG_IGNPORTERROR)
665 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
670 if (sc->config & PSM_CONFIG_NORESET) {
672 * Don't try to reset the pointing device. It may possibly be
673 * left in the unknown state, though...
677 * NOTE: some controllers appears to hang the `keyboard' when
678 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
680 if (!reset_aux_dev(kbdc)) {
681 recover_from_error(kbdc);
682 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
688 * both the aux port and the aux device is functioning, see
689 * if the device can be enabled.
691 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
692 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
695 empty_both_buffers(kbdc, 10); /* remove stray data if any */
697 if (sc->config & PSM_CONFIG_NOIDPROBE) {
698 i = GENERIC_MOUSE_ENTRY;
700 /* FIXME: hardware ID, mouse buttons? */
702 /* other parameters */
703 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
704 if ((*vendortype[i].probefunc)(sc)) {
706 log(LOG_ERR, "psm%d: found %s\n",
707 sc->unit, model_name(vendortype[i].model));
713 sc->hw.model = vendortype[i].model;
714 sc->mode.packetsize = vendortype[i].packetsize;
716 /* set mouse parameters */
717 if (mode != (mousemode_t *)NULL) {
719 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
720 if (mode->resolution >= 0)
721 mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
722 set_mouse_scaling(kbdc, 1);
723 set_mouse_mode(kbdc);
726 /* request a data packet and extract sync. bits */
727 if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
728 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
730 sc->mode.syncmask[0] = 0;
732 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
733 /* the NetScroll Mouse will send three more bytes... Ignore them */
734 empty_aux_buffer(kbdc, 5);
737 /* just check the status of the mouse */
738 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
739 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
746 doopen(struct psm_softc *sc, int command_byte)
750 /* enable the mouse device */
751 if (!enable_aux_dev(sc->kbdc)) {
752 /* MOUSE ERROR: failed to enable the mouse because:
753 * 1) the mouse is faulty,
754 * 2) the mouse has been removed(!?)
755 * In the latter case, the keyboard may have hung, and need
756 * recovery procedure...
758 recover_from_error(sc->kbdc);
760 /* FIXME: we could reset the mouse here and try to enable
761 * it again. But it will take long time and it's not a good
762 * idea to disable the keyboard that long...
764 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
765 recover_from_error(sc->kbdc);
769 restore_controller(sc->kbdc, command_byte);
770 /* mark this device is no longer available */
771 sc->state &= ~PSM_VALID;
772 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
778 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
779 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
781 /* enable the aux port and interrupt */
782 if (!set_controller_command_byte(sc->kbdc,
783 kbdc_get_device_mask(sc->kbdc),
784 (command_byte & KBD_KBD_CONTROL_BITS)
785 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
786 /* CONTROLLER ERROR */
787 disable_aux_dev(sc->kbdc);
788 restore_controller(sc->kbdc, command_byte);
789 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
794 /* start the watchdog timer */
795 sc->watchdog = FALSE;
796 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
802 reinitialize(struct psm_softc *sc, int doinit)
808 /* don't let anybody mess with the aux device */
809 if (!kbdc_lock(sc->kbdc, TRUE))
813 /* block our watchdog timer */
814 sc->watchdog = FALSE;
815 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
816 callout_handle_init(&sc->callout);
818 /* save the current controller command byte */
819 empty_both_buffers(sc->kbdc, 10);
820 c = get_controller_command_byte(sc->kbdc);
822 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
825 /* enable the aux port but disable the aux interrupt and the keyboard */
826 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
827 kbdc_get_device_mask(sc->kbdc),
828 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
829 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
830 /* CONTROLLER ERROR */
832 kbdc_lock(sc->kbdc, FALSE);
833 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
839 if (sc->state & PSM_VALID) {
840 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */
841 empty_aux_buffer(sc->kbdc, 10);
846 /* try to detect the aux device; are you still there? */
849 if (doinitialize(sc, &sc->mode)) {
851 sc->state |= PSM_VALID;
853 /* the device has gone! */
854 restore_controller(sc->kbdc, c);
855 sc->state &= ~PSM_VALID;
856 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
863 /* restore the driver state */
864 if ((sc->state & PSM_OPEN) && (err == 0)) {
865 /* enable the aux device and the port again */
868 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
871 /* restore the keyboard port and disable the aux port */
872 if (!set_controller_command_byte(sc->kbdc,
873 kbdc_get_device_mask(sc->kbdc),
874 (c & KBD_KBD_CONTROL_BITS)
875 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
876 /* CONTROLLER ERROR */
877 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
883 kbdc_lock(sc->kbdc, FALSE);
887 /* psm driver entry points */
889 #define endprobe(v) { if (bootverbose) \
891 kbdc_set_device_mask(sc->kbdc, mask); \
892 kbdc_lock(sc->kbdc, FALSE); \
897 psmprobe(device_t dev)
899 int unit = device_get_unit(dev);
900 struct psm_softc *sc = device_get_softc(dev);
915 if (XXX_PNP_PROBE(device_get_parent(dev), dev, psm_ids) == ENXIO)
919 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
920 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
923 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
924 sc->config = flags & PSM_CONFIG_FLAGS;
925 /* XXX: for backward compatibility */
926 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
928 #ifdef PSM_RESETAFTERSUSPEND
929 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
931 PSM_CONFIG_HOOKRESUME;
933 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
938 device_set_desc(dev, "PS/2 Mouse");
940 if (!kbdc_lock(sc->kbdc, TRUE)) {
941 printf("psm%d: unable to lock the controller.\n", unit);
948 * NOTE: two bits in the command byte controls the operation of the
949 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
950 * port interrupt (IRQ 12) enable bit (bit 2).
953 /* discard anything left after the keyboard initialization */
954 empty_both_buffers(sc->kbdc, 10);
956 /* save the current command byte; it will be used later */
957 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
958 command_byte = get_controller_command_byte(sc->kbdc);
960 printf("psm%d: current command byte:%04x\n", unit, command_byte);
961 if (command_byte == -1) {
962 /* CONTROLLER ERROR */
963 printf("psm%d: unable to get the current command byte value.\n",
969 * disable the keyboard port while probing the aux port, which must be
970 * enabled during this routine
972 if (!set_controller_command_byte(sc->kbdc,
973 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
974 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
975 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
977 * this is CONTROLLER ERROR; I don't know how to recover
980 restore_controller(sc->kbdc, command_byte);
981 printf("psm%d: unable to set the command byte.\n", unit);
984 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
987 * NOTE: `test_aux_port()' is designed to return with zero if the aux
988 * port exists and is functioning. However, some controllers appears
989 * to respond with zero even when the aux port doesn't exist. (It may
990 * be that this is only the case when the controller DOES have the aux
991 * port but the port is not wired on the motherboard.) The keyboard
992 * controllers without the port, such as the original AT, are
993 * supporsed to return with an error code or simply time out. In any
994 * case, we have to continue probing the port even when the controller
997 * XXX: some controllers erroneously return the error code 1 when
998 * it has the perfectly functional aux port. We have to ignore this
999 * error code. Even if the controller HAS error with the aux port,
1000 * it will be detected later...
1001 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1003 switch ((i = test_aux_port(sc->kbdc))) {
1004 case 1: /* ignore this error */
1007 printf("psm%d: strange result for test aux port (%d).\n",
1010 case 0: /* no error */
1012 case -1: /* time out */
1013 default: /* error */
1014 recover_from_error(sc->kbdc);
1015 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1017 restore_controller(sc->kbdc, command_byte);
1019 printf("psm%d: the aux port is not functioning (%d).\n",
1024 if (sc->config & PSM_CONFIG_NORESET) {
1026 * Don't try to reset the pointing device. It may possibly be
1027 * left in the unknown state, though...
1031 * NOTE: some controllers appears to hang the `keyboard' when the aux
1032 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1034 if (!reset_aux_dev(sc->kbdc)) {
1035 recover_from_error(sc->kbdc);
1036 restore_controller(sc->kbdc, command_byte);
1038 printf("psm%d: failed to reset the aux device.\n", unit);
1044 * both the aux port and the aux device is functioning, see if the
1045 * device can be enabled. NOTE: when enabled, the device will start
1046 * sending data; we shall immediately disable the device once we know
1047 * the device can be enabled.
1049 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1051 recover_from_error(sc->kbdc);
1052 restore_controller(sc->kbdc, command_byte);
1054 printf("psm%d: failed to enable the aux device.\n", unit);
1058 /* save the default values after reset */
1059 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1060 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1061 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1063 sc->dflt_mode.rate = sc->mode.rate = -1;
1064 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1067 /* hardware information */
1068 sc->hw.iftype = MOUSE_IF_PS2;
1070 /* verify the device is a mouse */
1071 sc->hw.hwid = get_aux_id(sc->kbdc);
1072 if (!is_a_mouse(sc->hw.hwid)) {
1073 restore_controller(sc->kbdc, command_byte);
1075 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1078 switch (sc->hw.hwid) {
1079 case PSM_BALLPOINT_ID:
1080 sc->hw.type = MOUSE_TRACKBALL;
1083 case PSM_INTELLI_ID:
1084 case PSM_EXPLORER_ID:
1085 case PSM_4DMOUSE_ID:
1087 sc->hw.type = MOUSE_MOUSE;
1090 sc->hw.type = MOUSE_UNKNOWN;
1094 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1096 i = GENERIC_MOUSE_ENTRY;
1099 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1101 /* other parameters */
1102 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1103 if ((*vendortype[i].probefunc)(sc)) {
1105 printf("psm%d: found %s\n",
1106 unit, model_name(vendortype[i].model));
1112 sc->hw.model = vendortype[i].model;
1114 sc->dflt_mode.level = PSM_LEVEL_BASE;
1115 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1116 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1117 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1118 sc->dflt_mode.syncmask[0] = 0;
1120 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1121 if (sc->config & PSM_CONFIG_FORCETAP)
1122 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1123 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1124 sc->mode = sc->dflt_mode;
1125 sc->mode.packetsize = vendortype[i].packetsize;
1127 /* set mouse parameters */
1130 * A version of Logitech FirstMouse+ won't report wheel movement,
1131 * if SET_DEFAULTS is sent... Don't use this command.
1132 * This fix was found by Takashi Nishida.
1134 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1136 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1138 if (sc->config & PSM_CONFIG_RESOLUTION) {
1140 = set_mouse_resolution(sc->kbdc,
1141 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1142 } else if (sc->mode.resolution >= 0) {
1144 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1146 if (sc->mode.rate > 0) {
1147 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1149 set_mouse_scaling(sc->kbdc, 1);
1151 /* request a data packet and extract sync. bits */
1152 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1153 printf("psm%d: failed to get data.\n", unit);
1154 sc->mode.syncmask[0] = 0;
1156 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1157 /* the NetScroll Mouse will send three more bytes... Ignore them */
1158 empty_aux_buffer(sc->kbdc, 5);
1161 /* just check the status of the mouse */
1163 * NOTE: XXX there are some arcane controller/mouse combinations out
1164 * there, which hung the controller unless there is data transmission
1165 * after ACK from the mouse.
1167 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1168 printf("psm%d: failed to get status.\n", unit);
1171 * When in its native mode, some mice operate with different
1172 * default parameters than in the PS/2 compatible mode.
1174 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1175 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1178 /* disable the aux port for now... */
1179 if (!set_controller_command_byte(sc->kbdc,
1180 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1181 (command_byte & KBD_KBD_CONTROL_BITS)
1182 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1184 * this is CONTROLLER ERROR; I don't know the proper way to
1185 * recover from this error...
1187 restore_controller(sc->kbdc, command_byte);
1188 printf("psm%d: unable to set the command byte.\n", unit);
1192 /* see if IRQ is available */
1194 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1196 if (sc->intr == NULL) {
1197 printf("psm%d: unable to allocate the IRQ resource (%d).\n",
1201 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1205 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1206 kbdc_lock(sc->kbdc, FALSE);
1211 psmattach(device_t dev)
1213 int unit = device_get_unit(dev);
1214 struct psm_softc *sc = device_get_softc(dev);
1219 if (sc == NULL) /* shouldn't happen */
1222 /* Setup initial state */
1223 sc->state = PSM_VALID;
1224 callout_handle_init(&sc->callout);
1226 /* Setup our interrupt handler */
1228 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1229 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1231 if (sc->intr == NULL)
1233 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
1234 INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1236 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1241 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1243 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1247 printf("psm%d: model %s, device ID %d\n",
1248 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1250 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1251 unit, model_name(sc->hw.model),
1252 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1253 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1254 unit, sc->config, sc->flags, sc->mode.packetsize);
1255 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1256 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1266 psmdetach(device_t dev)
1268 struct psm_softc *sc;
1271 sc = device_get_softc(dev);
1272 if (sc->state & PSM_OPEN)
1276 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih);
1277 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1279 destroy_dev(sc->dev);
1280 destroy_dev(sc->bdev);
1286 psmopen(dev_t dev, int flag, int fmt, struct proc *p)
1288 int unit = PSM_UNIT(dev);
1289 struct psm_softc *sc;
1294 /* Get device data */
1295 sc = PSM_SOFTC(unit);
1296 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1297 /* the device is no longer valid/functioning */
1300 /* Disallow multiple opens */
1301 if (sc->state & PSM_OPEN)
1304 device_busy(devclass_get_device(psm_devclass, unit));
1306 /* Initialize state */
1307 sc->rsel.si_flags = 0;
1308 sc->rsel.si_pid = 0;
1309 sc->mode.level = sc->dflt_mode.level;
1310 sc->mode.protocol = sc->dflt_mode.protocol;
1311 sc->watchdog = FALSE;
1313 /* flush the event queue */
1314 sc->queue.count = 0;
1317 sc->status.flags = 0;
1318 sc->status.button = 0;
1319 sc->status.obutton = 0;
1325 /* empty input buffer */
1326 bzero(sc->ipacket, sizeof(sc->ipacket));
1330 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1331 if (!kbdc_lock(sc->kbdc, TRUE))
1334 /* save the current controller command byte */
1336 command_byte = get_controller_command_byte(sc->kbdc);
1338 /* enable the aux port and temporalily disable the keyboard */
1339 if ((command_byte == -1)
1340 || !set_controller_command_byte(sc->kbdc,
1341 kbdc_get_device_mask(sc->kbdc),
1342 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1343 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1344 /* CONTROLLER ERROR; do you know how to get out of this? */
1345 kbdc_lock(sc->kbdc, FALSE);
1347 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1352 * Now that the keyboard controller is told not to generate
1353 * the keyboard and mouse interrupts, call `splx()' to allow
1354 * the other tty interrupts. The clock interrupt may also occur,
1355 * but timeout routines will be blocked by the poll flag set
1360 /* enable the mouse device */
1361 err = doopen(sc, command_byte);
1365 sc->state |= PSM_OPEN;
1366 kbdc_lock(sc->kbdc, FALSE);
1371 psmclose(dev_t dev, int flag, int fmt, struct proc *p)
1373 int unit = PSM_UNIT(dev);
1374 struct psm_softc *sc = PSM_SOFTC(unit);
1379 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1380 if (!kbdc_lock(sc->kbdc, TRUE))
1383 /* save the current controller command byte */
1385 command_byte = get_controller_command_byte(sc->kbdc);
1386 if (command_byte == -1) {
1387 kbdc_lock(sc->kbdc, FALSE);
1392 /* disable the aux interrupt and temporalily disable the keyboard */
1393 if (!set_controller_command_byte(sc->kbdc,
1394 kbdc_get_device_mask(sc->kbdc),
1395 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1396 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1397 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1399 /* CONTROLLER ERROR;
1400 * NOTE: we shall force our way through. Because the only
1401 * ill effect we shall see is that we may not be able
1402 * to read ACK from the mouse, and it doesn't matter much
1403 * so long as the mouse will accept the DISABLE command.
1408 /* stop the watchdog timer */
1409 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1410 callout_handle_init(&sc->callout);
1412 /* remove anything left in the output buffer */
1413 empty_aux_buffer(sc->kbdc, 10);
1415 /* disable the aux device, port and interrupt */
1416 if (sc->state & PSM_VALID) {
1417 if (!disable_aux_dev(sc->kbdc)) {
1419 * NOTE: we don't return error and continue, pretending
1420 * we have successfully disabled the device. It's OK because
1421 * the interrupt routine will discard any data from the mouse
1424 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1428 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1429 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1433 if (!set_controller_command_byte(sc->kbdc,
1434 kbdc_get_device_mask(sc->kbdc),
1435 (command_byte & KBD_KBD_CONTROL_BITS)
1436 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1437 /* CONTROLLER ERROR;
1438 * we shall ignore this error; see the above comment.
1440 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1444 /* remove anything left in the output buffer */
1445 empty_aux_buffer(sc->kbdc, 10);
1447 /* close is almost always successful */
1448 sc->state &= ~PSM_OPEN;
1449 kbdc_lock(sc->kbdc, FALSE);
1450 device_unbusy(devclass_get_device(psm_devclass, unit));
1455 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
1457 static unsigned char butmapps2[8] = {
1459 MOUSE_PS2_BUTTON1DOWN,
1460 MOUSE_PS2_BUTTON2DOWN,
1461 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1462 MOUSE_PS2_BUTTON3DOWN,
1463 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1464 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1465 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1467 static unsigned char butmapmsc[8] = {
1468 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1469 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1470 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1471 MOUSE_MSC_BUTTON3UP,
1472 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1473 MOUSE_MSC_BUTTON2UP,
1474 MOUSE_MSC_BUTTON1UP,
1480 if (sc->mode.level == PSM_LEVEL_BASE) {
1481 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1482 if (status->button & MOUSE_BUTTON4DOWN)
1483 mapped |= MOUSE_BUTTON1DOWN;
1484 status->button = mapped;
1485 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1486 i = max(min(status->dx, 255), -256);
1488 buf[0] |= MOUSE_PS2_XNEG;
1490 i = max(min(status->dy, 255), -256);
1492 buf[0] |= MOUSE_PS2_YNEG;
1494 return MOUSE_PS2_PACKETSIZE;
1495 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1496 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1497 i = max(min(status->dx, 255), -256);
1499 buf[3] = i - buf[1];
1500 i = max(min(status->dy, 255), -256);
1502 buf[4] = i - buf[2];
1503 i = max(min(status->dz, 127), -128);
1504 buf[5] = (i >> 1) & 0x7f;
1505 buf[6] = (i - (i >> 1)) & 0x7f;
1506 buf[7] = (~status->button >> 3) & 0x7f;
1507 return MOUSE_SYS_PACKETSIZE;
1509 return sc->inputbytes;;
1513 psmread(dev_t dev, struct uio *uio, int flag)
1515 register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1516 unsigned char buf[PSM_SMALLBUFSIZE];
1521 if ((sc->state & PSM_VALID) == 0)
1524 /* block until mouse activity occured */
1526 while (sc->queue.count <= 0) {
1527 if (PSM_NBLOCKIO(dev)) {
1531 sc->state |= PSM_ASLP;
1532 error = tsleep((caddr_t) sc, PZERO | PCATCH, "psmrea", 0);
1533 sc->state &= ~PSM_ASLP;
1537 } else if ((sc->state & PSM_VALID) == 0) {
1538 /* the device disappeared! */
1545 /* copy data to the user land */
1546 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1548 l = min(sc->queue.count, uio->uio_resid);
1549 if (l > sizeof(buf))
1551 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1552 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1553 sizeof(sc->queue.buf) - sc->queue.head);
1554 bcopy(&sc->queue.buf[0],
1555 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1556 l - (sizeof(sc->queue.buf) - sc->queue.head));
1558 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1560 sc->queue.count -= l;
1561 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1563 error = uiomove(buf, l, uio);
1572 block_mouse_data(struct psm_softc *sc, int *c)
1576 if (!kbdc_lock(sc->kbdc, TRUE))
1580 *c = get_controller_command_byte(sc->kbdc);
1582 || !set_controller_command_byte(sc->kbdc,
1583 kbdc_get_device_mask(sc->kbdc),
1584 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1585 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1586 /* this is CONTROLLER ERROR */
1588 kbdc_lock(sc->kbdc, FALSE);
1593 * The device may be in the middle of status data transmission.
1594 * The transmission will be interrupted, thus, incomplete status
1595 * data must be discarded. Although the aux interrupt is disabled
1596 * at the keyboard controller level, at most one aux interrupt
1597 * may have already been pending and a data byte is in the
1598 * output buffer; throw it away. Note that the second argument
1599 * to `empty_aux_buffer()' is zero, so that the call will just
1600 * flush the internal queue.
1601 * `psmintr()' will be invoked after `splx()' if an interrupt is
1602 * pending; it will see no data and returns immediately.
1604 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1605 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1613 unblock_mouse_data(struct psm_softc *sc, int c)
1618 * We may have seen a part of status data during `set_mouse_XXX()'.
1619 * they have been queued; flush it.
1621 empty_aux_buffer(sc->kbdc, 0);
1623 /* restore ports and interrupt */
1624 if (!set_controller_command_byte(sc->kbdc,
1625 kbdc_get_device_mask(sc->kbdc),
1626 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1627 /* CONTROLLER ERROR; this is serious, we may have
1628 * been left with the inaccessible keyboard and
1629 * the disabled mouse interrupt.
1634 kbdc_lock(sc->kbdc, FALSE);
1639 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1641 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1643 mousestatus_t status;
1644 #if (defined(MOUSE_GETVARS))
1653 /* Perform IOCTL command */
1656 case OLD_MOUSE_GETHWINFO:
1658 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1659 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1660 ((old_mousehw_t *)addr)->type = sc->hw.type;
1661 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1665 case MOUSE_GETHWINFO:
1667 *(mousehw_t *)addr = sc->hw;
1668 if (sc->mode.level == PSM_LEVEL_BASE)
1669 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1673 case OLD_MOUSE_GETMODE:
1675 switch (sc->mode.level) {
1676 case PSM_LEVEL_BASE:
1677 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1679 case PSM_LEVEL_STANDARD:
1680 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1682 case PSM_LEVEL_NATIVE:
1683 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1686 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1687 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1688 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1694 *(mousemode_t *)addr = sc->mode;
1695 ((mousemode_t *)addr)->resolution =
1696 MOUSE_RES_LOW - sc->mode.resolution;
1697 switch (sc->mode.level) {
1698 case PSM_LEVEL_BASE:
1699 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1700 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1702 case PSM_LEVEL_STANDARD:
1703 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1704 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1705 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1706 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1708 case PSM_LEVEL_NATIVE:
1709 /* FIXME: this isn't quite correct... XXX */
1710 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1716 case OLD_MOUSE_SETMODE:
1718 if (cmd == OLD_MOUSE_SETMODE) {
1719 mode.rate = ((old_mousemode_t *)addr)->rate;
1721 * resolution old I/F new I/F
1728 if (((old_mousemode_t *)addr)->resolution > 0)
1729 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1730 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1733 mode = *(mousemode_t *)addr;
1736 /* adjust and validate parameters. */
1737 if (mode.rate > UCHAR_MAX)
1740 mode.rate = sc->dflt_mode.rate;
1741 else if (mode.rate == -1)
1742 /* don't change the current setting */
1744 else if (mode.rate < 0)
1746 if (mode.resolution >= UCHAR_MAX)
1748 if (mode.resolution >= 200)
1749 mode.resolution = MOUSE_RES_HIGH;
1750 else if (mode.resolution >= 100)
1751 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1752 else if (mode.resolution >= 50)
1753 mode.resolution = MOUSE_RES_MEDIUMLOW;
1754 else if (mode.resolution > 0)
1755 mode.resolution = MOUSE_RES_LOW;
1756 if (mode.resolution == MOUSE_RES_DEFAULT)
1757 mode.resolution = sc->dflt_mode.resolution;
1758 else if (mode.resolution == -1)
1759 /* don't change the current setting */
1761 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1762 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1763 if (mode.level == -1)
1764 /* don't change the current setting */
1765 mode.level = sc->mode.level;
1766 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1768 if (mode.accelfactor == -1)
1769 /* don't change the current setting */
1770 mode.accelfactor = sc->mode.accelfactor;
1771 else if (mode.accelfactor < 0)
1774 /* don't allow anybody to poll the keyboard controller */
1775 error = block_mouse_data(sc, &command_byte);
1779 /* set mouse parameters */
1781 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1782 if (mode.resolution >= 0)
1783 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1784 set_mouse_scaling(sc->kbdc, 1);
1785 get_mouse_status(sc->kbdc, stat, 0, 3);
1788 sc->mode.rate = mode.rate;
1789 sc->mode.resolution = mode.resolution;
1790 sc->mode.accelfactor = mode.accelfactor;
1791 sc->mode.level = mode.level;
1794 unblock_mouse_data(sc, command_byte);
1797 case MOUSE_GETLEVEL:
1798 *(int *)addr = sc->mode.level;
1801 case MOUSE_SETLEVEL:
1802 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1804 sc->mode.level = *(int *)addr;
1807 case MOUSE_GETSTATUS:
1809 status = sc->status;
1810 sc->status.flags = 0;
1811 sc->status.obutton = sc->status.button;
1812 sc->status.button = 0;
1817 *(mousestatus_t *)addr = status;
1820 #if (defined(MOUSE_GETVARS))
1822 var = (mousevar_t *)addr;
1823 bzero(var, sizeof(*var));
1825 var->var[0] = MOUSE_VARS_PS2_SIG;
1826 var->var[1] = sc->config;
1827 var->var[2] = sc->flags;
1833 #endif /* MOUSE_GETVARS */
1835 case MOUSE_READSTATE:
1836 case MOUSE_READDATA:
1837 data = (mousedata_t *)addr;
1838 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1841 error = block_mouse_data(sc, &command_byte);
1844 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1845 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1847 unblock_mouse_data(sc, command_byte);
1850 #if (defined(MOUSE_SETRESOLUTION))
1851 case MOUSE_SETRESOLUTION:
1852 mode.resolution = *(int *)addr;
1853 if (mode.resolution >= UCHAR_MAX)
1855 else if (mode.resolution >= 200)
1856 mode.resolution = MOUSE_RES_HIGH;
1857 else if (mode.resolution >= 100)
1858 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1859 else if (mode.resolution >= 50)
1860 mode.resolution = MOUSE_RES_MEDIUMLOW;
1861 else if (mode.resolution > 0)
1862 mode.resolution = MOUSE_RES_LOW;
1863 if (mode.resolution == MOUSE_RES_DEFAULT)
1864 mode.resolution = sc->dflt_mode.resolution;
1865 else if (mode.resolution == -1)
1866 mode.resolution = sc->mode.resolution;
1867 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1868 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1870 error = block_mouse_data(sc, &command_byte);
1873 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1874 if (sc->mode.resolution != mode.resolution)
1876 unblock_mouse_data(sc, command_byte);
1878 #endif /* MOUSE_SETRESOLUTION */
1880 #if (defined(MOUSE_SETRATE))
1882 mode.rate = *(int *)addr;
1883 if (mode.rate > UCHAR_MAX)
1886 mode.rate = sc->dflt_mode.rate;
1887 else if (mode.rate < 0)
1888 mode.rate = sc->mode.rate;
1890 error = block_mouse_data(sc, &command_byte);
1893 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1894 if (sc->mode.rate != mode.rate)
1896 unblock_mouse_data(sc, command_byte);
1898 #endif /* MOUSE_SETRATE */
1900 #if (defined(MOUSE_SETSCALING))
1901 case MOUSE_SETSCALING:
1902 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1905 error = block_mouse_data(sc, &command_byte);
1908 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1910 unblock_mouse_data(sc, command_byte);
1912 #endif /* MOUSE_SETSCALING */
1914 #if (defined(MOUSE_GETHWID))
1916 error = block_mouse_data(sc, &command_byte);
1919 sc->hw.hwid &= ~0x00ff;
1920 sc->hw.hwid |= get_aux_id(sc->kbdc);
1921 *(int *)addr = sc->hw.hwid & 0x00ff;
1922 unblock_mouse_data(sc, command_byte);
1924 #endif /* MOUSE_GETHWID */
1934 psmtimeout(void *arg)
1936 struct psm_softc *sc;
1939 sc = (struct psm_softc *)arg;
1941 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1943 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1945 kbdc_lock(sc->kbdc, FALSE);
1947 sc->watchdog = TRUE;
1949 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
1956 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
1957 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
1959 static int butmap[8] = {
1963 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1965 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1966 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1967 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1969 static int butmap_versapad[8] = {
1975 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1977 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
1979 register struct psm_softc *sc = arg;
1987 /* read until there is nothing to read */
1988 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
1990 /* discard the byte if the device is not open */
1991 if ((sc->state & PSM_OPEN) == 0)
1994 getmicrouptime(&tv);
1995 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
1996 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2000 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2001 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2002 timevaladd(&sc->inputtimeout, &tv);
2004 sc->ipacket[sc->inputbytes++] = c;
2005 if (sc->inputbytes < sc->mode.packetsize)
2009 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2010 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
2011 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
2016 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2017 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
2018 c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
2020 if (sc->syncerrors < sc->mode.packetsize) {
2021 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2023 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2024 } else if (sc->syncerrors == sc->mode.packetsize) {
2025 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2027 disable_aux_dev(sc->kbdc);
2028 enable_aux_dev(sc->kbdc);
2029 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2030 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2032 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2033 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2034 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2035 reinitialize(sc, TRUE);
2041 * A kludge for Kensington device!
2042 * The MSB of the horizontal count appears to be stored in
2045 if (sc->hw.model == MOUSE_MODEL_THINK)
2046 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2048 /* ignore the overflow bits... */
2049 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2050 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2052 ms.obutton = sc->button; /* previous button state */
2053 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2054 /* `tapping' action */
2055 if (sc->config & PSM_CONFIG_FORCETAP)
2056 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2058 switch (sc->hw.model) {
2060 case MOUSE_MODEL_EXPLORER:
2062 * b7 b6 b5 b4 b3 b2 b1 b0
2063 * byte 1: oy ox sy sx 1 M R L
2064 * byte 2: x x x x x x x x
2065 * byte 3: y y y y y y y y
2066 * byte 4: * * S2 S1 s d2 d1 d0
2068 * L, M, R, S1, S2: left, middle, right and side buttons
2069 * s: wheel data sign bit
2072 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2073 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
2074 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2075 ? MOUSE_BUTTON4DOWN : 0;
2076 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2077 ? MOUSE_BUTTON5DOWN : 0;
2080 case MOUSE_MODEL_INTELLI:
2081 case MOUSE_MODEL_NET:
2082 /* wheel data is in the fourth byte */
2083 z = (char)sc->ipacket[3];
2084 /* some mice may send 7 when there is no Z movement?! XXX */
2085 if ((z >= 7) || (z <= -7))
2087 /* some compatible mice have additional buttons */
2088 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2089 ? MOUSE_BUTTON4DOWN : 0;
2090 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2091 ? MOUSE_BUTTON5DOWN : 0;
2094 case MOUSE_MODEL_MOUSEMANPLUS:
2096 * PS2++ protocl packet
2098 * b7 b6 b5 b4 b3 b2 b1 b0
2099 * byte 1: * 1 p3 p2 1 * * *
2100 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2102 * p3-p0: packet type
2103 * c1, c2: c1 & c2 == 1, if p2 == 0
2104 * c1 & c2 == 0, if p2 == 1
2106 * packet type: 0 (device type)
2107 * See comments in enable_mmanplus() below.
2109 * packet type: 1 (wheel data)
2111 * b7 b6 b5 b4 b3 b2 b1 b0
2112 * byte 3: h * B5 B4 s d2 d1 d0
2114 * h: 1, if horizontal roller data
2115 * 0, if vertical roller data
2116 * B4, B5: button 4 and 5
2118 * d2-d0: roller data
2120 * packet type: 2 (reserved)
2122 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2124 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
2125 /* the extended data packet encodes button and wheel events */
2126 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
2128 /* wheel data packet */
2130 if (sc->ipacket[2] & 0x80) {
2131 /* horizontal roller count - ignore it XXX*/
2133 /* vertical roller count */
2134 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2135 ? (sc->ipacket[2] & 0x0f) - 16
2136 : (sc->ipacket[2] & 0x0f);
2138 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2139 ? MOUSE_BUTTON4DOWN : 0;
2140 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2141 ? MOUSE_BUTTON5DOWN : 0;
2144 /* this packet type is reserved by Logitech... */
2146 * IBM ScrollPoint Mouse uses this packet type to
2147 * encode both vertical and horizontal scroll movement.
2150 /* horizontal count */
2151 if (sc->ipacket[2] & 0x0f)
2152 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2153 /* vertical count */
2154 if (sc->ipacket[2] & 0xf0)
2155 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2157 /* vertical count */
2158 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
2159 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16
2160 : ((sc->ipacket[2] >> 4) & 0x0f);
2161 /* horizontal count */
2162 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
2163 ? (sc->ipacket[2] & 0x0f) - 16
2164 : (sc->ipacket[2] & 0x0f);
2168 /* device type packet - shouldn't happen */
2172 ms.button = ms.obutton;
2174 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2175 "0x%02x 0x%02x 0x%02x\n",
2176 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
2177 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
2181 /* preserve button states */
2182 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2186 case MOUSE_MODEL_GLIDEPOINT:
2187 /* `tapping' action */
2188 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2191 case MOUSE_MODEL_NETSCROLL:
2192 /* three addtional bytes encode buttons and wheel events */
2193 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2194 ? MOUSE_BUTTON4DOWN : 0;
2195 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2196 ? MOUSE_BUTTON5DOWN : 0;
2197 z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
2198 ? sc->ipacket[4] - 256 : sc->ipacket[4];
2201 case MOUSE_MODEL_THINK:
2202 /* the fourth button state in the first byte */
2203 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2206 case MOUSE_MODEL_VERSAPAD:
2207 /* VersaPad PS/2 absolute mode message format
2209 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2210 * ipacket[0]: 1 1 0 A 1 L T R
2211 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2212 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2213 * ipacket[3]: 1 1 1 A 1 L T R
2214 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2215 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2218 * R: right physical mouse button (1=on)
2219 * T: touch pad virtual button (1=tapping)
2220 * L: left physical mouse button (1=on)
2221 * A: position data is valid (1=valid)
2222 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2223 * V: vertical data (12bit signed integer. V11 is sign bit.)
2226 * Tapping is mapped to MOUSE_BUTTON4.
2228 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2229 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2231 if (c & MOUSE_PS2VERSA_IN_USE) {
2232 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
2233 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
2238 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2241 if (x < 0) /* XXX */
2250 sc->flags |= PSM_FLAGS_FINGERDOWN;
2255 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2257 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2258 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2261 case MOUSE_MODEL_4D:
2263 * b7 b6 b5 b4 b3 b2 b1 b0
2264 * byte 1: s2 d2 s1 d1 1 M R L
2265 * byte 2: sx x x x x x x x
2266 * byte 3: sy y y y y y y y
2268 * s1: wheel 1 direction
2270 * s2: wheel 2 direction
2273 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2274 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2275 switch (c & MOUSE_4D_WHEELBITS) {
2282 case 0x40: /* 2nd wheel turning right XXX */
2285 case 0xc0: /* 2nd wheel turning left XXX */
2291 case MOUSE_MODEL_4DPLUS:
2292 if ((x < 16 - 256) && (y < 16 - 256)) {
2294 * b7 b6 b5 b4 b3 b2 b1 b0
2295 * byte 1: 0 0 1 1 1 M R L
2296 * byte 2: 0 0 0 0 1 0 0 0
2297 * byte 3: 0 0 0 0 S s d1 d0
2299 * L, M, R, S: left, middle, right and side buttons
2300 * s: wheel data sign bit
2304 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2305 ms.button |= MOUSE_BUTTON4DOWN;
2306 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2307 ? ((sc->ipacket[2] & 0x07) - 8)
2308 : (sc->ipacket[2] & 0x07) ;
2310 /* preserve previous button states */
2311 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2315 case MOUSE_MODEL_GENERIC:
2321 if (sc->mode.accelfactor >= 1) {
2323 x = x * x / sc->mode.accelfactor;
2326 if (c & MOUSE_PS2_XNEG)
2330 y = y * y / sc->mode.accelfactor;
2333 if (c & MOUSE_PS2_YNEG)
2341 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2342 | (ms.obutton ^ ms.button);
2344 if (sc->mode.level < PSM_LEVEL_NATIVE)
2345 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
2347 sc->status.flags |= ms.flags;
2348 sc->status.dx += ms.dx;
2349 sc->status.dy += ms.dy;
2350 sc->status.dz += ms.dz;
2351 sc->status.button = ms.button;
2352 sc->button = ms.button;
2354 sc->watchdog = FALSE;
2357 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2358 l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2359 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2360 if (sc->inputbytes > l)
2361 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
2363 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
2364 sc->queue.count += sc->inputbytes;
2368 if (sc->state & PSM_ASLP) {
2369 sc->state &= ~PSM_ASLP;
2370 wakeup((caddr_t) sc);
2372 selwakeup(&sc->rsel);
2377 psmpoll(dev_t dev, int events, struct proc *p)
2379 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2383 /* Return true if a mouse event available */
2385 if (events & (POLLIN | POLLRDNORM)) {
2386 if (sc->queue.count > 0)
2387 revents |= events & (POLLIN | POLLRDNORM);
2389 selrecord(p, &sc->rsel);
2396 /* vendor/model specific routines */
2398 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2400 if (set_mouse_resolution(kbdc, res) != res)
2402 if (set_mouse_scaling(kbdc, scale)
2403 && set_mouse_scaling(kbdc, scale)
2404 && set_mouse_scaling(kbdc, scale)
2405 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2411 mouse_ext_command(KBDC kbdc, int command)
2415 c = (command >> 6) & 0x03;
2416 if (set_mouse_resolution(kbdc, c) != c)
2418 c = (command >> 4) & 0x03;
2419 if (set_mouse_resolution(kbdc, c) != c)
2421 c = (command >> 2) & 0x03;
2422 if (set_mouse_resolution(kbdc, c) != c)
2424 c = (command >> 0) & 0x03;
2425 if (set_mouse_resolution(kbdc, c) != c)
2431 /* Logitech MouseMan Cordless II */
2433 enable_lcordless(struct psm_softc *sc)
2438 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2440 if (status[1] == PSMD_RES_HIGH)
2442 ch = (status[0] & 0x07) - 1; /* channel # */
2443 if ((ch <= 0) || (ch > 4))
2446 * status[1]: always one?
2447 * status[2]: battery status? (0-100)
2453 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2455 enable_groller(struct psm_softc *sc)
2460 * The special sequence to enable the fourth button and the
2461 * roller. Immediately after this sequence check status bytes.
2462 * if the mouse is NetScroll, the second and the third bytes are
2467 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2468 * look like the following.
2470 * byte 1 bit 7 always 0
2471 * bit 6 stream mode (0)
2472 * bit 5 disabled (0)
2473 * bit 4 1:1 scaling (0)
2475 * bit 0-2 button status
2476 * byte 2 resolution (PSMD_RES_HIGH)
2477 * byte 3 report rate (?)
2480 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2482 if ((status[1] != '3') || (status[2] != 'D'))
2484 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2489 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2491 enable_gmouse(struct psm_softc *sc)
2496 * The special sequence to enable the middle, "rubber" button.
2497 * Immediately after this sequence check status bytes.
2498 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2499 * the second and the third bytes are '3' and 'U'.
2500 * NOTE: NetMouse reports that it has three buttons although it has
2501 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2502 * say they have three buttons too and they do have a button on the
2505 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2507 if ((status[1] != '3') || (status[2] != 'U'))
2512 /* ALPS GlidePoint */
2514 enable_aglide(struct psm_softc *sc)
2519 * The special sequence to obtain ALPS GlidePoint specific
2520 * information. Immediately after this sequence, status bytes will
2521 * contain something interesting.
2522 * NOTE: ALPS produces several models of GlidePoint. Some of those
2523 * do not respond to this sequence, thus, cannot be detected this way.
2525 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2527 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2529 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2534 /* Kensington ThinkingMouse/Trackball */
2536 enable_kmouse(struct psm_softc *sc)
2538 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2539 KBDC kbdc = sc->kbdc;
2545 id1 = get_aux_id(kbdc);
2546 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2549 * The device is now in the native mode? It returns a different
2552 id2 = get_aux_id(kbdc);
2553 if ((id1 == id2) || (id2 != 2))
2556 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2559 /* at this point, resolution is LOW, sampling rate is 10/sec */
2560 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2565 * The special sequence to enable the third and fourth buttons.
2566 * Otherwise they behave like the first and second buttons.
2568 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2569 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2574 * At this point, the device is using default resolution and
2575 * sampling rate for the native mode.
2577 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2579 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2582 /* the device appears be enabled by this sequence, diable it for now */
2583 disable_aux_dev(kbdc);
2584 empty_aux_buffer(kbdc, 5);
2589 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2591 enable_mmanplus(struct psm_softc *sc)
2593 KBDC kbdc = sc->kbdc;
2596 /* the special sequence to enable the fourth button and the roller. */
2598 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2599 * must be called exactly three times since the last RESET command
2600 * before this sequence. XXX
2602 if (!set_mouse_scaling(kbdc, 1))
2604 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2606 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2610 * PS2++ protocl, packet type 0
2612 * b7 b6 b5 b4 b3 b2 b1 b0
2613 * byte 1: * 1 p3 p2 1 * * *
2614 * byte 2: 1 1 p1 p0 m1 m0 1 0
2615 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2617 * p3-p0: packet type: 0
2618 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2620 /* check constant bits */
2621 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2623 if ((data[1] & 0xc3) != 0xc2)
2625 /* check d3-d0 in byte 2 */
2626 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2629 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2632 sc->hw.hwid &= 0x00ff;
2633 sc->hw.hwid |= data[2] << 8; /* save model ID */
2636 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2637 * the wheel and the fourth button events are encoded in the
2638 * special data packet. The mouse may be put in the IntelliMouse mode
2639 * if it is initialized by the IntelliMouse's method.
2644 /* MS IntelliMouse Explorer */
2646 enable_msexplorer(struct psm_softc *sc)
2648 static unsigned char rate0[] = { 200, 100, 80, };
2649 static unsigned char rate1[] = { 200, 200, 80, };
2650 KBDC kbdc = sc->kbdc;
2654 /* the special sequence to enable the extra buttons and the roller. */
2655 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2656 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2659 /* the device will give the genuine ID only after the above sequence */
2660 id = get_aux_id(kbdc);
2661 if (id != PSM_EXPLORER_ID)
2665 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2668 * XXX: this is a kludge to fool some KVM switch products
2669 * which think they are clever enough to know the 4-byte IntelliMouse
2670 * protocol, and assume any other protocols use 3-byte packets.
2671 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2672 * correctly to the host computer because of this!
2673 * The following sequence is actually IntelliMouse's "wake up"
2674 * sequence; it will make the KVM think the mouse is IntelliMouse
2675 * when it is in fact IntelliMouse Explorer.
2677 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2678 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2681 id = get_aux_id(kbdc);
2686 /* MS IntelliMouse */
2688 enable_msintelli(struct psm_softc *sc)
2691 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2692 * probe routine and act like IntelliMouse.
2695 static unsigned char rate[] = { 200, 100, 80, };
2696 KBDC kbdc = sc->kbdc;
2700 /* the special sequence to enable the third button and the roller. */
2701 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2702 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2705 /* the device will give the genuine ID only after the above sequence */
2706 id = get_aux_id(kbdc);
2707 if (id != PSM_INTELLI_ID)
2716 /* A4 Tech 4D Mouse */
2718 enable_4dmouse(struct psm_softc *sc)
2721 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2724 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2725 KBDC kbdc = sc->kbdc;
2729 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2730 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2733 id = get_aux_id(kbdc);
2735 * WinEasy 4D, 4 Way Scroll 4D: 6
2736 * Cable-Free 4D: 8 (4DPLUS)
2737 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2739 if (id != PSM_4DMOUSE_ID)
2743 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2748 /* A4 Tech 4D+ Mouse */
2750 enable_4dplus(struct psm_softc *sc)
2753 * Newer wheel mice from A4 Tech seem to use this protocol.
2754 * Older models are recognized as either 4D Mouse or IntelliMouse.
2756 KBDC kbdc = sc->kbdc;
2760 * enable_4dmouse() already issued the following ID sequence...
2761 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2764 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2765 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2770 id = get_aux_id(kbdc);
2771 if (id != PSM_4DPLUS_ID)
2775 sc->hw.buttons = 4; /* XXX */
2780 /* Interlink electronics VersaPad */
2782 enable_versapad(struct psm_softc *sc)
2784 KBDC kbdc = sc->kbdc;
2787 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2788 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2789 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2790 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2791 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2792 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2793 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2795 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2797 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2799 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2801 return TRUE; /* PS/2 absolute mode */
2805 psmresume(device_t dev)
2807 struct psm_softc *sc = device_get_softc(dev);
2808 int unit = device_get_unit(dev);
2812 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2814 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2817 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2819 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2821 * Release the blocked process; it must be notified that the device
2822 * cannot be accessed anymore.
2824 sc->state &= ~PSM_ASLP;
2825 wakeup((caddr_t)sc);
2829 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2834 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);