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.7 2003/11/12 04:26:26 mikeh Exp $
24 * $DragonFly: src/sys/dev/misc/psm/psm.c,v 1.11 2004/05/19 22:52:44 dillon Exp $
28 * Ported to 386bsd Oct 17, 1992
29 * Sandi Donno, Computer Science, University of Cape Town, South Africa
30 * Please send bug reports to sandi@cs.uct.ac.za
32 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
33 * although I was only partially successful in getting the alpha release
34 * of his "driver for the Logitech and ATI Inport Bus mice for use with
35 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
36 * found his code to be an invaluable reference when porting this driver
39 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
40 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
42 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
43 * Andrew Herbert - 12 June 1993
45 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
48 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
51 * Hardware access routines and probe logic rewritten by
52 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
53 * - 3, 14, 22 October 1996.
54 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
55 * - 14, 30 November 1996. Uses `kbdio.c'.
56 * - 13 December 1996. Uses queuing version of `kbdio.c'.
57 * - January/February 1997. Tweaked probe logic for
58 * HiNote UltraII/Latitude/Armada laptops.
59 * - 30 July 1997. Added APM support.
60 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
61 * Improved sync check logic.
62 * Vendor specific support routines.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/module.h>
74 #include <sys/syslog.h>
75 #include <sys/malloc.h>
76 #include <machine/bus.h>
78 #include <sys/select.h>
82 #include <machine/clock.h>
83 #include <machine/limits.h>
84 #include <machine/mouse.h>
85 #include <machine/resource.h>
87 #include <bus/isa/isavar.h>
88 #include <dev/misc/kbd/atkbdcreg.h>
91 * Driver specific options: the following options may be set by
92 * `options' statements in the kernel configuration file.
97 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */
100 #ifndef PSM_SYNCERR_THRESHOLD1
101 #define PSM_SYNCERR_THRESHOLD1 20
104 #ifndef PSM_INPUT_TIMEOUT
105 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
108 /* end of driver specific options */
110 #define PSM_DRIVER_NAME "psm"
111 #define PSMCPNP_DRIVER_NAME "psmcpnp"
114 #define PSM_BUFSIZE 960
115 #define PSM_SMALLBUFSIZE 240
117 /* operation levels */
118 #define PSM_LEVEL_BASE 0
119 #define PSM_LEVEL_STANDARD 1
120 #define PSM_LEVEL_NATIVE 2
121 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
122 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
124 /* Logitech PS2++ protocol */
125 #define MOUSE_PS2PLUS_CHECKBITS(b) \
126 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
127 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
128 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
131 #define PSM_UNIT(dev) (minor(dev) >> 1)
132 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
133 #define PSM_MKMINOR(unit,block) ((((unit) & 0xff) << 1) | ((block) ? 0:1))
136 #define max(x,y) ((x) > (y) ? (x) : (y))
139 #define min(x,y) ((x) < (y) ? (x) : (y))
142 #define abs(x) (((x) < 0) ? -(x) : (x))
145 typedef struct ringbuf {
146 int count; /* # of valid elements in the buffer */
147 int head; /* head pointer */
148 int tail; /* tail poiner */
149 unsigned char buf[PSM_BUFSIZE];
152 /* driver control block */
153 struct psm_softc { /* Driver status information */
155 struct selinfo rsel; /* Process selecting for Input */
156 unsigned char state; /* Mouse driver state */
157 int config; /* driver configuration flags */
158 int flags; /* other flags */
159 KBDC kbdc; /* handle to access the keyboard controller */
160 struct resource *intr; /* IRQ resource */
161 void *ih; /* interrupt handle */
162 mousehw_t hw; /* hardware information */
163 mousemode_t mode; /* operation mode */
164 mousemode_t dflt_mode; /* default operation mode */
165 mousestatus_t status; /* accumulated mouse movement */
166 ringbuf_t queue; /* mouse status queue */
167 unsigned char ipacket[16]; /* interim input buffer */
168 int inputbytes; /* # of bytes in the input buffer */
169 int button; /* the latest button state */
170 int xold; /* previous absolute X position */
171 int yold; /* previous absolute Y position */
173 struct timeval inputtimeout;
174 int watchdog; /* watchdog timer flag */
175 struct callout_handle callout; /* watchdog timer call out */
177 devclass_t psm_devclass;
178 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
180 /* driver state flags (state) */
181 #define PSM_VALID 0x80
182 #define PSM_OPEN 1 /* Device is open */
183 #define PSM_ASLP 2 /* Waiting for mouse data */
185 /* driver configuration flags (config) */
186 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
187 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
188 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
189 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
190 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
191 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
192 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
193 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
194 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
195 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
197 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \
199 | PSM_CONFIG_NOCHECKSYNC \
200 | PSM_CONFIG_SYNCHACK \
201 | PSM_CONFIG_NOIDPROBE \
202 | PSM_CONFIG_NORESET \
203 | PSM_CONFIG_FORCETAP \
204 | PSM_CONFIG_IGNPORTERROR \
205 | PSM_CONFIG_HOOKRESUME \
206 | PSM_CONFIG_INITAFTERSUSPEND)
208 /* other flags (flags) */
209 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
211 /* for backward compatibility */
212 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
213 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
214 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
216 typedef struct old_mousehw {
223 typedef struct old_mousemode {
230 /* packet formatting function */
231 typedef int packetfunc_t (struct psm_softc *, unsigned char *,
232 int *, int, mousestatus_t *);
234 /* function prototypes */
235 static int psmprobe (device_t);
236 static int psmattach (device_t);
237 static int psmdetach (device_t);
238 static int psmresume (device_t);
240 static d_open_t psmopen;
241 static d_close_t psmclose;
242 static d_read_t psmread;
243 static d_ioctl_t psmioctl;
244 static d_poll_t psmpoll;
246 static int enable_aux_dev (KBDC);
247 static int disable_aux_dev (KBDC);
248 static int get_mouse_status (KBDC, int *, int, int);
249 static int get_aux_id (KBDC);
250 static int set_mouse_sampling_rate (KBDC, int);
251 static int set_mouse_scaling (KBDC, int);
252 static int set_mouse_resolution (KBDC, int);
253 static int set_mouse_mode (KBDC);
254 static int get_mouse_buttons (KBDC);
255 static int is_a_mouse (int);
256 static void recover_from_error (KBDC);
257 static int restore_controller (KBDC, int);
258 static int doinitialize (struct psm_softc *, mousemode_t *);
259 static int doopen (struct psm_softc *, int);
260 static int reinitialize (struct psm_softc *, int);
261 static char *model_name (int);
262 static void psmintr (void *);
263 static void psmtimeout (void *);
265 /* vendor specific features */
266 typedef int probefunc_t (struct psm_softc *);
268 static int mouse_id_proc1 (KBDC, int, int, int *);
269 static int mouse_ext_command (KBDC, int);
270 static probefunc_t enable_groller;
271 static probefunc_t enable_gmouse;
272 static probefunc_t enable_aglide;
273 static probefunc_t enable_kmouse;
274 static probefunc_t enable_msexplorer;
275 static probefunc_t enable_msintelli;
276 static probefunc_t enable_4dmouse;
277 static probefunc_t enable_4dplus;
278 static probefunc_t enable_mmanplus;
279 static probefunc_t enable_versapad;
280 static int tame_mouse (struct psm_softc *, mousestatus_t *, unsigned char *);
284 unsigned char syncmask;
286 probefunc_t *probefunc;
289 * WARNING: the order of probe is very important. Don't mess it
290 * unless you know what you are doing.
292 { MOUSE_MODEL_NET, /* Genius NetMouse */
293 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
294 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
295 0xc8, 6, enable_groller, },
296 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
297 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
298 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
299 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
300 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
301 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
302 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
303 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
304 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
305 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
306 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
307 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
308 { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */
309 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
310 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
311 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
312 { MOUSE_MODEL_GENERIC,
313 0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
315 #define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
317 /* device driver declarateion */
318 static device_method_t psm_methods[] = {
319 /* Device interface */
320 DEVMETHOD(device_probe, psmprobe),
321 DEVMETHOD(device_attach, psmattach),
322 DEVMETHOD(device_detach, psmdetach),
323 DEVMETHOD(device_resume, psmresume),
328 static driver_t psm_driver = {
331 sizeof(struct psm_softc),
335 static struct isa_pnp_id psm_ids[] = {
336 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
337 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
342 #define CDEV_MAJOR 21
344 static struct cdevsw psm_cdevsw = {
345 /* name */ PSM_DRIVER_NAME,
346 /* maj */ CDEV_MAJOR,
352 /* close */ psmclose,
355 /* ioctl */ psmioctl,
358 /* strategy */ nostrategy,
363 /* debug message level */
364 static int verbose = PSM_DEBUG;
366 /* device I/O routines */
368 enable_aux_dev(KBDC kbdc)
372 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
374 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
376 return (res == PSM_ACK);
380 disable_aux_dev(KBDC kbdc)
384 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
386 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
388 return (res == PSM_ACK);
392 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
401 cmd = PSMC_SEND_DEV_STATUS;
404 cmd = PSMC_SEND_DEV_DATA;
407 empty_aux_buffer(kbdc, 5);
408 res = send_aux_command(kbdc, cmd);
410 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
411 (flag == 1) ? "DATA" : "STATUS", res);
415 for (i = 0; i < len; ++i) {
416 status[i] = read_aux_data(kbdc);
422 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
423 (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
430 get_aux_id(KBDC kbdc)
435 empty_aux_buffer(kbdc, 5);
436 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
438 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
445 id = read_aux_data(kbdc);
447 log(LOG_DEBUG, "psm: device ID: %04x\n", id);
453 set_mouse_sampling_rate(KBDC kbdc, int rate)
457 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
459 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
461 return ((res == PSM_ACK) ? rate : -1);
465 set_mouse_scaling(KBDC kbdc, int scale)
472 scale = PSMC_SET_SCALING11;
475 scale = PSMC_SET_SCALING21;
478 res = send_aux_command(kbdc, scale);
480 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
481 (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
483 return (res == PSM_ACK);
486 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
488 set_mouse_resolution(KBDC kbdc, int val)
492 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
494 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
496 return ((res == PSM_ACK) ? val : -1);
500 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
501 * re-enabled by calling `enable_aux_dev()'
504 set_mouse_mode(KBDC kbdc)
508 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
510 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
512 return (res == PSM_ACK);
516 get_mouse_buttons(KBDC kbdc)
518 int c = 2; /* assume two buttons by default */
522 * NOTE: a special sequence to obtain Logitech Mouse specific
523 * information: set resolution to 25 ppi, set scaling to 1:1, set
524 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
525 * mouse status bytes is the number of available buttons.
526 * Some manufactures also support this sequence.
528 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
530 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
531 && set_mouse_scaling(kbdc, 1)
532 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
539 /* misc subroutines */
541 * Someday, I will get the complete list of valid pointing devices and
548 static int valid_ids[] = {
549 PSM_MOUSE_ID, /* mouse */
550 PSM_BALLPOINT_ID, /* ballpoint device */
551 PSM_INTELLI_ID, /* Intellimouse */
552 PSM_EXPLORER_ID, /* Intellimouse Explorer */
553 -1 /* end of table */
557 for (i = 0; valid_ids[i] >= 0; ++i)
558 if (valid_ids[i] == id)
567 model_name(int model)
573 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
574 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
575 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
576 { MOUSE_MODEL_THINK, "ThinkingMouse" },
577 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
578 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
579 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
580 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
581 { MOUSE_MODEL_4D, "4D Mouse" },
582 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
583 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
584 { MOUSE_MODEL_UNKNOWN, NULL },
588 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
589 if (models[i].model_code == model)
590 return models[i].model_name;
596 recover_from_error(KBDC kbdc)
598 /* discard anything left in the output buffer */
599 empty_both_buffers(kbdc, 10);
603 * NOTE: KBDC_RESET_KBD may not restore the communication between the
604 * keyboard and the controller.
609 * NOTE: somehow diagnostic and keyboard port test commands bring the
612 if (!test_controller(kbdc))
613 log(LOG_ERR, "psm: keyboard controller failed.\n");
614 /* if there isn't a keyboard in the system, the following error is OK */
615 if (test_kbd_port(kbdc) != 0) {
617 log(LOG_ERR, "psm: keyboard port failed.\n");
623 restore_controller(KBDC kbdc, int command_byte)
625 empty_both_buffers(kbdc, 10);
627 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
628 log(LOG_ERR, "psm: failed to restore the keyboard controller "
630 empty_both_buffers(kbdc, 10);
633 empty_both_buffers(kbdc, 10);
639 * Re-initialize the aux port and device. The aux port must be enabled
640 * and its interrupt must be disabled before calling this routine.
641 * The aux device will be disabled before returning.
642 * The keyboard controller must be locked via `kbdc_lock()' before
643 * calling this routine.
646 doinitialize(struct psm_softc *sc, mousemode_t *mode)
648 KBDC kbdc = sc->kbdc;
652 switch((i = test_aux_port(kbdc))) {
653 case 1: /* ignore this error */
656 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
659 case 0: /* no error */
661 case -1: /* time out */
663 recover_from_error(kbdc);
664 if (sc->config & PSM_CONFIG_IGNPORTERROR)
666 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
671 if (sc->config & PSM_CONFIG_NORESET) {
673 * Don't try to reset the pointing device. It may possibly be
674 * left in the unknown state, though...
678 * NOTE: some controllers appears to hang the `keyboard' when
679 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
681 if (!reset_aux_dev(kbdc)) {
682 recover_from_error(kbdc);
683 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
689 * both the aux port and the aux device is functioning, see
690 * if the device can be enabled.
692 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
693 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
696 empty_both_buffers(kbdc, 10); /* remove stray data if any */
698 if (sc->config & PSM_CONFIG_NOIDPROBE) {
699 i = GENERIC_MOUSE_ENTRY;
701 /* FIXME: hardware ID, mouse buttons? */
703 /* other parameters */
704 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
705 if ((*vendortype[i].probefunc)(sc)) {
707 log(LOG_ERR, "psm%d: found %s\n",
708 sc->unit, model_name(vendortype[i].model));
714 sc->hw.model = vendortype[i].model;
715 sc->mode.packetsize = vendortype[i].packetsize;
717 /* set mouse parameters */
718 if (mode != (mousemode_t *)NULL) {
720 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
721 if (mode->resolution >= 0)
722 mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
723 set_mouse_scaling(kbdc, 1);
724 set_mouse_mode(kbdc);
727 /* request a data packet and extract sync. bits */
728 if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
729 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
731 sc->mode.syncmask[0] = 0;
733 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
734 /* the NetScroll Mouse will send three more bytes... Ignore them */
735 empty_aux_buffer(kbdc, 5);
738 /* just check the status of the mouse */
739 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
740 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
747 doopen(struct psm_softc *sc, int command_byte)
751 /* enable the mouse device */
752 if (!enable_aux_dev(sc->kbdc)) {
753 /* MOUSE ERROR: failed to enable the mouse because:
754 * 1) the mouse is faulty,
755 * 2) the mouse has been removed(!?)
756 * In the latter case, the keyboard may have hung, and need
757 * recovery procedure...
759 recover_from_error(sc->kbdc);
761 /* FIXME: we could reset the mouse here and try to enable
762 * it again. But it will take long time and it's not a good
763 * idea to disable the keyboard that long...
765 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
766 recover_from_error(sc->kbdc);
770 restore_controller(sc->kbdc, command_byte);
771 /* mark this device is no longer available */
772 sc->state &= ~PSM_VALID;
773 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
779 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
780 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
782 /* enable the aux port and interrupt */
783 if (!set_controller_command_byte(sc->kbdc,
784 kbdc_get_device_mask(sc->kbdc),
785 (command_byte & KBD_KBD_CONTROL_BITS)
786 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
787 /* CONTROLLER ERROR */
788 disable_aux_dev(sc->kbdc);
789 restore_controller(sc->kbdc, command_byte);
790 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
795 /* start the watchdog timer */
796 sc->watchdog = FALSE;
797 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
803 reinitialize(struct psm_softc *sc, int doinit)
809 /* don't let anybody mess with the aux device */
810 if (!kbdc_lock(sc->kbdc, TRUE))
814 /* block our watchdog timer */
815 sc->watchdog = FALSE;
816 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
817 callout_handle_init(&sc->callout);
819 /* save the current controller command byte */
820 empty_both_buffers(sc->kbdc, 10);
821 c = get_controller_command_byte(sc->kbdc);
823 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
826 /* enable the aux port but disable the aux interrupt and the keyboard */
827 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
828 kbdc_get_device_mask(sc->kbdc),
829 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
830 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
831 /* CONTROLLER ERROR */
833 kbdc_lock(sc->kbdc, FALSE);
834 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
840 if (sc->state & PSM_VALID) {
841 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */
842 empty_aux_buffer(sc->kbdc, 10);
847 /* try to detect the aux device; are you still there? */
850 if (doinitialize(sc, &sc->mode)) {
852 sc->state |= PSM_VALID;
854 /* the device has gone! */
855 restore_controller(sc->kbdc, c);
856 sc->state &= ~PSM_VALID;
857 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
864 /* restore the driver state */
865 if ((sc->state & PSM_OPEN) && (err == 0)) {
866 /* enable the aux device and the port again */
869 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
872 /* restore the keyboard port and disable the aux port */
873 if (!set_controller_command_byte(sc->kbdc,
874 kbdc_get_device_mask(sc->kbdc),
875 (c & KBD_KBD_CONTROL_BITS)
876 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
877 /* CONTROLLER ERROR */
878 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
884 kbdc_lock(sc->kbdc, FALSE);
888 /* psm driver entry points */
890 #define endprobe(v) { if (bootverbose) \
892 kbdc_set_device_mask(sc->kbdc, mask); \
893 kbdc_lock(sc->kbdc, FALSE); \
898 psmprobe(device_t dev)
900 int unit = device_get_unit(dev);
901 struct psm_softc *sc = device_get_softc(dev);
916 if (XXX_PNP_PROBE(device_get_parent(dev), dev, psm_ids) == ENXIO)
920 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
921 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
924 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
925 sc->config = flags & PSM_CONFIG_FLAGS;
926 /* XXX: for backward compatibility */
927 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
929 #ifdef PSM_RESETAFTERSUSPEND
930 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
932 PSM_CONFIG_HOOKRESUME;
934 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
939 device_set_desc(dev, "PS/2 Mouse");
941 if (!kbdc_lock(sc->kbdc, TRUE)) {
942 printf("psm%d: unable to lock the controller.\n", unit);
949 * NOTE: two bits in the command byte controls the operation of the
950 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
951 * port interrupt (IRQ 12) enable bit (bit 2).
954 /* discard anything left after the keyboard initialization */
955 empty_both_buffers(sc->kbdc, 10);
957 /* save the current command byte; it will be used later */
958 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
959 command_byte = get_controller_command_byte(sc->kbdc);
961 printf("psm%d: current command byte:%04x\n", unit, command_byte);
962 if (command_byte == -1) {
963 /* CONTROLLER ERROR */
964 printf("psm%d: unable to get the current command byte value.\n",
970 * disable the keyboard port while probing the aux port, which must be
971 * enabled during this routine
973 if (!set_controller_command_byte(sc->kbdc,
974 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
975 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
976 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
978 * this is CONTROLLER ERROR; I don't know how to recover
981 restore_controller(sc->kbdc, command_byte);
982 printf("psm%d: unable to set the command byte.\n", unit);
985 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
988 * NOTE: `test_aux_port()' is designed to return with zero if the aux
989 * port exists and is functioning. However, some controllers appears
990 * to respond with zero even when the aux port doesn't exist. (It may
991 * be that this is only the case when the controller DOES have the aux
992 * port but the port is not wired on the motherboard.) The keyboard
993 * controllers without the port, such as the original AT, are
994 * supporsed to return with an error code or simply time out. In any
995 * case, we have to continue probing the port even when the controller
998 * XXX: some controllers erroneously return the error code 1 when
999 * it has the perfectly functional aux port. We have to ignore this
1000 * error code. Even if the controller HAS error with the aux port,
1001 * it will be detected later...
1002 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1004 switch ((i = test_aux_port(sc->kbdc))) {
1005 case 1: /* ignore this error */
1008 printf("psm%d: strange result for test aux port (%d).\n",
1011 case 0: /* no error */
1013 case -1: /* time out */
1014 default: /* error */
1015 recover_from_error(sc->kbdc);
1016 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1018 restore_controller(sc->kbdc, command_byte);
1020 printf("psm%d: the aux port is not functioning (%d).\n",
1025 if (sc->config & PSM_CONFIG_NORESET) {
1027 * Don't try to reset the pointing device. It may possibly be
1028 * left in the unknown state, though...
1032 * NOTE: some controllers appears to hang the `keyboard' when the aux
1033 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1035 * Attempt to reset the controller twice -- this helps
1036 * pierce through some KVM switches. The second reset
1039 if (!reset_aux_dev(sc->kbdc)) {
1040 recover_from_error(sc->kbdc);
1041 restore_controller(sc->kbdc, command_byte);
1043 printf("psm%d: failed to reset the aux device.\n", unit);
1045 } else if (!reset_aux_dev(sc->kbdc)) {
1046 recover_from_error(sc->kbdc);
1048 printf("psm%d: failed to reset the aux device (2).\n",
1054 * both the aux port and the aux device is functioning, see if the
1055 * device can be enabled. NOTE: when enabled, the device will start
1056 * sending data; we shall immediately disable the device once we know
1057 * the device can be enabled.
1059 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1061 recover_from_error(sc->kbdc);
1062 restore_controller(sc->kbdc, command_byte);
1064 printf("psm%d: failed to enable the aux device.\n", unit);
1068 /* save the default values after reset */
1069 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1070 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1071 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1073 sc->dflt_mode.rate = sc->mode.rate = -1;
1074 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1077 /* hardware information */
1078 sc->hw.iftype = MOUSE_IF_PS2;
1080 /* verify the device is a mouse */
1081 sc->hw.hwid = get_aux_id(sc->kbdc);
1082 if (!is_a_mouse(sc->hw.hwid)) {
1083 restore_controller(sc->kbdc, command_byte);
1085 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1088 switch (sc->hw.hwid) {
1089 case PSM_BALLPOINT_ID:
1090 sc->hw.type = MOUSE_TRACKBALL;
1093 case PSM_INTELLI_ID:
1094 case PSM_EXPLORER_ID:
1095 case PSM_4DMOUSE_ID:
1097 sc->hw.type = MOUSE_MOUSE;
1100 sc->hw.type = MOUSE_UNKNOWN;
1104 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1106 i = GENERIC_MOUSE_ENTRY;
1109 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1111 /* other parameters */
1112 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1113 if ((*vendortype[i].probefunc)(sc)) {
1115 printf("psm%d: found %s\n",
1116 unit, model_name(vendortype[i].model));
1122 sc->hw.model = vendortype[i].model;
1124 sc->dflt_mode.level = PSM_LEVEL_BASE;
1125 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1126 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1127 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1128 sc->dflt_mode.syncmask[0] = 0;
1130 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1131 if (sc->config & PSM_CONFIG_FORCETAP)
1132 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1133 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1134 sc->mode = sc->dflt_mode;
1135 sc->mode.packetsize = vendortype[i].packetsize;
1137 /* set mouse parameters */
1140 * A version of Logitech FirstMouse+ won't report wheel movement,
1141 * if SET_DEFAULTS is sent... Don't use this command.
1142 * This fix was found by Takashi Nishida.
1144 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1146 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1148 if (sc->config & PSM_CONFIG_RESOLUTION) {
1150 = set_mouse_resolution(sc->kbdc,
1151 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1152 } else if (sc->mode.resolution >= 0) {
1154 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1156 if (sc->mode.rate > 0) {
1157 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1159 set_mouse_scaling(sc->kbdc, 1);
1161 /* request a data packet and extract sync. bits */
1162 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1163 printf("psm%d: failed to get data.\n", unit);
1164 sc->mode.syncmask[0] = 0;
1166 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1167 /* the NetScroll Mouse will send three more bytes... Ignore them */
1168 empty_aux_buffer(sc->kbdc, 5);
1171 /* just check the status of the mouse */
1173 * NOTE: XXX there are some arcane controller/mouse combinations out
1174 * there, which hung the controller unless there is data transmission
1175 * after ACK from the mouse.
1177 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1178 printf("psm%d: failed to get status.\n", unit);
1181 * When in its native mode, some mice operate with different
1182 * default parameters than in the PS/2 compatible mode.
1184 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1185 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1188 /* disable the aux port for now... */
1189 if (!set_controller_command_byte(sc->kbdc,
1190 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1191 (command_byte & KBD_KBD_CONTROL_BITS)
1192 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1194 * this is CONTROLLER ERROR; I don't know the proper way to
1195 * recover from this error...
1197 restore_controller(sc->kbdc, command_byte);
1198 printf("psm%d: unable to set the command byte.\n", unit);
1202 /* see if IRQ is available */
1204 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1206 if (sc->intr == NULL) {
1207 printf("psm%d: unable to allocate the IRQ resource (%d).\n",
1211 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1215 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1216 kbdc_lock(sc->kbdc, FALSE);
1221 psmattach(device_t dev)
1223 int unit = device_get_unit(dev);
1224 struct psm_softc *sc = device_get_softc(dev);
1229 if (sc == NULL) /* shouldn't happen */
1232 /* Setup initial state */
1233 sc->state = PSM_VALID;
1234 callout_handle_init(&sc->callout);
1236 /* Setup our interrupt handler */
1238 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1239 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1241 if (sc->intr == NULL)
1243 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
1244 INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1246 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1251 cdevsw_add(&psm_cdevsw, PSM_MKMINOR(-1, 0), PSM_MKMINOR(unit, 0));
1252 make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, "psm%d", unit);
1253 make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, "bpsm%d", unit);
1256 printf("psm%d: model %s, device ID %d\n",
1257 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1259 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1260 unit, model_name(sc->hw.model),
1261 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1262 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1263 unit, sc->config, sc->flags, sc->mode.packetsize);
1264 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1265 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1275 psmdetach(device_t dev)
1277 struct psm_softc *sc;
1281 sc = device_get_softc(dev);
1282 if (sc->state & PSM_OPEN)
1285 unit = device_get_unit(dev);
1288 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih);
1289 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1290 cdevsw_remove(&psm_cdevsw, PSM_MKMINOR(-1, 0), PSM_MKMINOR(unit, 0));
1296 psmopen(dev_t dev, int flag, int fmt, struct thread *td)
1298 int unit = PSM_UNIT(dev);
1299 struct psm_softc *sc;
1304 /* Get device data */
1305 sc = PSM_SOFTC(unit);
1306 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1307 /* the device is no longer valid/functioning */
1310 /* Disallow multiple opens */
1311 if (sc->state & PSM_OPEN)
1314 device_busy(devclass_get_device(psm_devclass, unit));
1316 /* Initialize state */
1317 sc->rsel.si_flags = 0;
1318 sc->rsel.si_pid = 0;
1319 sc->mode.level = sc->dflt_mode.level;
1320 sc->mode.protocol = sc->dflt_mode.protocol;
1321 sc->watchdog = FALSE;
1323 /* flush the event queue */
1324 sc->queue.count = 0;
1327 sc->status.flags = 0;
1328 sc->status.button = 0;
1329 sc->status.obutton = 0;
1335 /* empty input buffer */
1336 bzero(sc->ipacket, sizeof(sc->ipacket));
1340 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1341 if (!kbdc_lock(sc->kbdc, TRUE))
1344 /* save the current controller command byte */
1346 command_byte = get_controller_command_byte(sc->kbdc);
1348 /* enable the aux port and temporalily disable the keyboard */
1349 if ((command_byte == -1)
1350 || !set_controller_command_byte(sc->kbdc,
1351 kbdc_get_device_mask(sc->kbdc),
1352 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1353 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1354 /* CONTROLLER ERROR; do you know how to get out of this? */
1355 kbdc_lock(sc->kbdc, FALSE);
1357 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1362 * Now that the keyboard controller is told not to generate
1363 * the keyboard and mouse interrupts, call `splx()' to allow
1364 * the other tty interrupts. The clock interrupt may also occur,
1365 * but timeout routines will be blocked by the poll flag set
1370 /* enable the mouse device */
1371 err = doopen(sc, command_byte);
1375 sc->state |= PSM_OPEN;
1376 kbdc_lock(sc->kbdc, FALSE);
1381 psmclose(dev_t dev, int flag, int fmt, struct thread *td)
1383 int unit = PSM_UNIT(dev);
1384 struct psm_softc *sc = PSM_SOFTC(unit);
1389 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1390 if (!kbdc_lock(sc->kbdc, TRUE))
1393 /* save the current controller command byte */
1395 command_byte = get_controller_command_byte(sc->kbdc);
1396 if (command_byte == -1) {
1397 kbdc_lock(sc->kbdc, FALSE);
1402 /* disable the aux interrupt and temporalily disable the keyboard */
1403 if (!set_controller_command_byte(sc->kbdc,
1404 kbdc_get_device_mask(sc->kbdc),
1405 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1406 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1407 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1409 /* CONTROLLER ERROR;
1410 * NOTE: we shall force our way through. Because the only
1411 * ill effect we shall see is that we may not be able
1412 * to read ACK from the mouse, and it doesn't matter much
1413 * so long as the mouse will accept the DISABLE command.
1418 /* stop the watchdog timer */
1419 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1420 callout_handle_init(&sc->callout);
1422 /* remove anything left in the output buffer */
1423 empty_aux_buffer(sc->kbdc, 10);
1425 /* disable the aux device, port and interrupt */
1426 if (sc->state & PSM_VALID) {
1427 if (!disable_aux_dev(sc->kbdc)) {
1429 * NOTE: we don't return error and continue, pretending
1430 * we have successfully disabled the device. It's OK because
1431 * the interrupt routine will discard any data from the mouse
1434 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1438 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1439 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1443 if (!set_controller_command_byte(sc->kbdc,
1444 kbdc_get_device_mask(sc->kbdc),
1445 (command_byte & KBD_KBD_CONTROL_BITS)
1446 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1447 /* CONTROLLER ERROR;
1448 * we shall ignore this error; see the above comment.
1450 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1454 /* remove anything left in the output buffer */
1455 empty_aux_buffer(sc->kbdc, 10);
1457 /* close is almost always successful */
1458 sc->state &= ~PSM_OPEN;
1459 kbdc_lock(sc->kbdc, FALSE);
1460 device_unbusy(devclass_get_device(psm_devclass, unit));
1465 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
1467 static unsigned char butmapps2[8] = {
1469 MOUSE_PS2_BUTTON1DOWN,
1470 MOUSE_PS2_BUTTON2DOWN,
1471 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1472 MOUSE_PS2_BUTTON3DOWN,
1473 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1474 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1475 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1477 static unsigned char butmapmsc[8] = {
1478 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1479 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1480 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1481 MOUSE_MSC_BUTTON3UP,
1482 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1483 MOUSE_MSC_BUTTON2UP,
1484 MOUSE_MSC_BUTTON1UP,
1490 if (sc->mode.level == PSM_LEVEL_BASE) {
1491 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1492 if (status->button & MOUSE_BUTTON4DOWN)
1493 mapped |= MOUSE_BUTTON1DOWN;
1494 status->button = mapped;
1495 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1496 i = max(min(status->dx, 255), -256);
1498 buf[0] |= MOUSE_PS2_XNEG;
1500 i = max(min(status->dy, 255), -256);
1502 buf[0] |= MOUSE_PS2_YNEG;
1504 return MOUSE_PS2_PACKETSIZE;
1505 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1506 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1507 i = max(min(status->dx, 255), -256);
1509 buf[3] = i - buf[1];
1510 i = max(min(status->dy, 255), -256);
1512 buf[4] = i - buf[2];
1513 i = max(min(status->dz, 127), -128);
1514 buf[5] = (i >> 1) & 0x7f;
1515 buf[6] = (i - (i >> 1)) & 0x7f;
1516 buf[7] = (~status->button >> 3) & 0x7f;
1517 return MOUSE_SYS_PACKETSIZE;
1519 return sc->inputbytes;;
1523 psmread(dev_t dev, struct uio *uio, int flag)
1525 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1526 unsigned char buf[PSM_SMALLBUFSIZE];
1531 if ((sc->state & PSM_VALID) == 0)
1534 /* block until mouse activity occured */
1536 while (sc->queue.count <= 0) {
1537 if (PSM_NBLOCKIO(dev)) {
1541 sc->state |= PSM_ASLP;
1542 error = tsleep((caddr_t) sc, PCATCH, "psmrea", 0);
1543 sc->state &= ~PSM_ASLP;
1547 } else if ((sc->state & PSM_VALID) == 0) {
1548 /* the device disappeared! */
1555 /* copy data to the user land */
1556 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1558 l = min(sc->queue.count, uio->uio_resid);
1559 if (l > sizeof(buf))
1561 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1562 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1563 sizeof(sc->queue.buf) - sc->queue.head);
1564 bcopy(&sc->queue.buf[0],
1565 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1566 l - (sizeof(sc->queue.buf) - sc->queue.head));
1568 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1570 sc->queue.count -= l;
1571 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1573 error = uiomove(buf, l, uio);
1582 block_mouse_data(struct psm_softc *sc, int *c)
1586 if (!kbdc_lock(sc->kbdc, TRUE))
1590 *c = get_controller_command_byte(sc->kbdc);
1592 || !set_controller_command_byte(sc->kbdc,
1593 kbdc_get_device_mask(sc->kbdc),
1594 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1595 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1596 /* this is CONTROLLER ERROR */
1598 kbdc_lock(sc->kbdc, FALSE);
1603 * The device may be in the middle of status data transmission.
1604 * The transmission will be interrupted, thus, incomplete status
1605 * data must be discarded. Although the aux interrupt is disabled
1606 * at the keyboard controller level, at most one aux interrupt
1607 * may have already been pending and a data byte is in the
1608 * output buffer; throw it away. Note that the second argument
1609 * to `empty_aux_buffer()' is zero, so that the call will just
1610 * flush the internal queue.
1611 * `psmintr()' will be invoked after `splx()' if an interrupt is
1612 * pending; it will see no data and returns immediately.
1614 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1615 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1623 unblock_mouse_data(struct psm_softc *sc, int c)
1628 * We may have seen a part of status data during `set_mouse_XXX()'.
1629 * they have been queued; flush it.
1631 empty_aux_buffer(sc->kbdc, 0);
1633 /* restore ports and interrupt */
1634 if (!set_controller_command_byte(sc->kbdc,
1635 kbdc_get_device_mask(sc->kbdc),
1636 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1637 /* CONTROLLER ERROR; this is serious, we may have
1638 * been left with the inaccessible keyboard and
1639 * the disabled mouse interrupt.
1644 kbdc_lock(sc->kbdc, FALSE);
1649 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1651 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1653 mousestatus_t status;
1654 #if (defined(MOUSE_GETVARS))
1663 /* Perform IOCTL command */
1666 case OLD_MOUSE_GETHWINFO:
1668 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1669 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1670 ((old_mousehw_t *)addr)->type = sc->hw.type;
1671 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1675 case MOUSE_GETHWINFO:
1677 *(mousehw_t *)addr = sc->hw;
1678 if (sc->mode.level == PSM_LEVEL_BASE)
1679 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1683 case OLD_MOUSE_GETMODE:
1685 switch (sc->mode.level) {
1686 case PSM_LEVEL_BASE:
1687 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1689 case PSM_LEVEL_STANDARD:
1690 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1692 case PSM_LEVEL_NATIVE:
1693 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1696 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1697 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1698 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1704 *(mousemode_t *)addr = sc->mode;
1705 ((mousemode_t *)addr)->resolution =
1706 MOUSE_RES_LOW - sc->mode.resolution;
1707 switch (sc->mode.level) {
1708 case PSM_LEVEL_BASE:
1709 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1710 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1712 case PSM_LEVEL_STANDARD:
1713 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1714 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1715 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1716 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1718 case PSM_LEVEL_NATIVE:
1719 /* FIXME: this isn't quite correct... XXX */
1720 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1726 case OLD_MOUSE_SETMODE:
1728 if (cmd == OLD_MOUSE_SETMODE) {
1729 mode.rate = ((old_mousemode_t *)addr)->rate;
1731 * resolution old I/F new I/F
1738 if (((old_mousemode_t *)addr)->resolution > 0)
1739 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1740 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1743 mode = *(mousemode_t *)addr;
1746 /* adjust and validate parameters. */
1747 if (mode.rate > UCHAR_MAX)
1750 mode.rate = sc->dflt_mode.rate;
1751 else if (mode.rate == -1)
1752 /* don't change the current setting */
1754 else if (mode.rate < 0)
1756 if (mode.resolution >= UCHAR_MAX)
1758 if (mode.resolution >= 200)
1759 mode.resolution = MOUSE_RES_HIGH;
1760 else if (mode.resolution >= 100)
1761 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1762 else if (mode.resolution >= 50)
1763 mode.resolution = MOUSE_RES_MEDIUMLOW;
1764 else if (mode.resolution > 0)
1765 mode.resolution = MOUSE_RES_LOW;
1766 if (mode.resolution == MOUSE_RES_DEFAULT)
1767 mode.resolution = sc->dflt_mode.resolution;
1768 else if (mode.resolution == -1)
1769 /* don't change the current setting */
1771 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1772 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1773 if (mode.level == -1)
1774 /* don't change the current setting */
1775 mode.level = sc->mode.level;
1776 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1778 if (mode.accelfactor == -1)
1779 /* don't change the current setting */
1780 mode.accelfactor = sc->mode.accelfactor;
1781 else if (mode.accelfactor < 0)
1784 /* don't allow anybody to poll the keyboard controller */
1785 error = block_mouse_data(sc, &command_byte);
1789 /* set mouse parameters */
1791 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1792 if (mode.resolution >= 0)
1793 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1794 set_mouse_scaling(sc->kbdc, 1);
1795 get_mouse_status(sc->kbdc, stat, 0, 3);
1798 sc->mode.rate = mode.rate;
1799 sc->mode.resolution = mode.resolution;
1800 sc->mode.accelfactor = mode.accelfactor;
1801 sc->mode.level = mode.level;
1804 unblock_mouse_data(sc, command_byte);
1807 case MOUSE_GETLEVEL:
1808 *(int *)addr = sc->mode.level;
1811 case MOUSE_SETLEVEL:
1812 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1814 sc->mode.level = *(int *)addr;
1817 case MOUSE_GETSTATUS:
1819 status = sc->status;
1820 sc->status.flags = 0;
1821 sc->status.obutton = sc->status.button;
1822 sc->status.button = 0;
1827 *(mousestatus_t *)addr = status;
1830 #if (defined(MOUSE_GETVARS))
1832 var = (mousevar_t *)addr;
1833 bzero(var, sizeof(*var));
1835 var->var[0] = MOUSE_VARS_PS2_SIG;
1836 var->var[1] = sc->config;
1837 var->var[2] = sc->flags;
1843 #endif /* MOUSE_GETVARS */
1845 case MOUSE_READSTATE:
1846 case MOUSE_READDATA:
1847 data = (mousedata_t *)addr;
1848 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1851 error = block_mouse_data(sc, &command_byte);
1854 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1855 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1857 unblock_mouse_data(sc, command_byte);
1860 #if (defined(MOUSE_SETRESOLUTION))
1861 case MOUSE_SETRESOLUTION:
1862 mode.resolution = *(int *)addr;
1863 if (mode.resolution >= UCHAR_MAX)
1865 else if (mode.resolution >= 200)
1866 mode.resolution = MOUSE_RES_HIGH;
1867 else if (mode.resolution >= 100)
1868 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1869 else if (mode.resolution >= 50)
1870 mode.resolution = MOUSE_RES_MEDIUMLOW;
1871 else if (mode.resolution > 0)
1872 mode.resolution = MOUSE_RES_LOW;
1873 if (mode.resolution == MOUSE_RES_DEFAULT)
1874 mode.resolution = sc->dflt_mode.resolution;
1875 else if (mode.resolution == -1)
1876 mode.resolution = sc->mode.resolution;
1877 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1878 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1880 error = block_mouse_data(sc, &command_byte);
1883 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1884 if (sc->mode.resolution != mode.resolution)
1886 unblock_mouse_data(sc, command_byte);
1888 #endif /* MOUSE_SETRESOLUTION */
1890 #if (defined(MOUSE_SETRATE))
1892 mode.rate = *(int *)addr;
1893 if (mode.rate > UCHAR_MAX)
1896 mode.rate = sc->dflt_mode.rate;
1897 else if (mode.rate < 0)
1898 mode.rate = sc->mode.rate;
1900 error = block_mouse_data(sc, &command_byte);
1903 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1904 if (sc->mode.rate != mode.rate)
1906 unblock_mouse_data(sc, command_byte);
1908 #endif /* MOUSE_SETRATE */
1910 #if (defined(MOUSE_SETSCALING))
1911 case MOUSE_SETSCALING:
1912 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1915 error = block_mouse_data(sc, &command_byte);
1918 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1920 unblock_mouse_data(sc, command_byte);
1922 #endif /* MOUSE_SETSCALING */
1924 #if (defined(MOUSE_GETHWID))
1926 error = block_mouse_data(sc, &command_byte);
1929 sc->hw.hwid &= ~0x00ff;
1930 sc->hw.hwid |= get_aux_id(sc->kbdc);
1931 *(int *)addr = sc->hw.hwid & 0x00ff;
1932 unblock_mouse_data(sc, command_byte);
1934 #endif /* MOUSE_GETHWID */
1944 psmtimeout(void *arg)
1946 struct psm_softc *sc;
1949 sc = (struct psm_softc *)arg;
1951 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1953 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1955 kbdc_lock(sc->kbdc, FALSE);
1957 sc->watchdog = TRUE;
1959 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
1966 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
1967 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
1969 static int butmap[8] = {
1973 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1975 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1976 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1977 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1979 static int butmap_versapad[8] = {
1985 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1987 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
1989 struct psm_softc *sc = arg;
1997 /* read until there is nothing to read */
1998 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2000 /* discard the byte if the device is not open */
2001 if ((sc->state & PSM_OPEN) == 0)
2004 getmicrouptime(&tv);
2005 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
2006 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2010 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2011 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2012 timevaladd(&sc->inputtimeout, &tv);
2014 sc->ipacket[sc->inputbytes++] = c;
2015 if (sc->inputbytes < sc->mode.packetsize)
2019 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2020 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
2021 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
2026 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2027 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
2028 c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
2030 if (sc->syncerrors < sc->mode.packetsize) {
2031 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2033 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2034 } else if (sc->syncerrors == sc->mode.packetsize) {
2035 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2037 disable_aux_dev(sc->kbdc);
2038 enable_aux_dev(sc->kbdc);
2039 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2040 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2042 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2043 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2044 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2045 reinitialize(sc, TRUE);
2051 * A kludge for Kensington device!
2052 * The MSB of the horizontal count appears to be stored in
2055 if (sc->hw.model == MOUSE_MODEL_THINK)
2056 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2058 /* ignore the overflow bits... */
2059 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2060 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2062 ms.obutton = sc->button; /* previous button state */
2063 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2064 /* `tapping' action */
2065 if (sc->config & PSM_CONFIG_FORCETAP)
2066 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2068 switch (sc->hw.model) {
2070 case MOUSE_MODEL_EXPLORER:
2072 * b7 b6 b5 b4 b3 b2 b1 b0
2073 * byte 1: oy ox sy sx 1 M R L
2074 * byte 2: x x x x x x x x
2075 * byte 3: y y y y y y y y
2076 * byte 4: * * S2 S1 s d2 d1 d0
2078 * L, M, R, S1, S2: left, middle, right and side buttons
2079 * s: wheel data sign bit
2082 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2083 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
2084 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2085 ? MOUSE_BUTTON4DOWN : 0;
2086 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2087 ? MOUSE_BUTTON5DOWN : 0;
2090 case MOUSE_MODEL_INTELLI:
2091 case MOUSE_MODEL_NET:
2092 /* wheel data is in the fourth byte */
2093 z = (char)sc->ipacket[3];
2094 /* some mice may send 7 when there is no Z movement?! XXX */
2095 if ((z >= 7) || (z <= -7))
2097 /* some compatible mice have additional buttons */
2098 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2099 ? MOUSE_BUTTON4DOWN : 0;
2100 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2101 ? MOUSE_BUTTON5DOWN : 0;
2104 case MOUSE_MODEL_MOUSEMANPLUS:
2106 * PS2++ protocl packet
2108 * b7 b6 b5 b4 b3 b2 b1 b0
2109 * byte 1: * 1 p3 p2 1 * * *
2110 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2112 * p3-p0: packet type
2113 * c1, c2: c1 & c2 == 1, if p2 == 0
2114 * c1 & c2 == 0, if p2 == 1
2116 * packet type: 0 (device type)
2117 * See comments in enable_mmanplus() below.
2119 * packet type: 1 (wheel data)
2121 * b7 b6 b5 b4 b3 b2 b1 b0
2122 * byte 3: h * B5 B4 s d2 d1 d0
2124 * h: 1, if horizontal roller data
2125 * 0, if vertical roller data
2126 * B4, B5: button 4 and 5
2128 * d2-d0: roller data
2130 * packet type: 2 (reserved)
2132 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2134 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
2135 /* the extended data packet encodes button and wheel events */
2136 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
2138 /* wheel data packet */
2140 if (sc->ipacket[2] & 0x80) {
2141 /* horizontal roller count - ignore it XXX*/
2143 /* vertical roller count */
2144 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2145 ? (sc->ipacket[2] & 0x0f) - 16
2146 : (sc->ipacket[2] & 0x0f);
2148 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2149 ? MOUSE_BUTTON4DOWN : 0;
2150 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2151 ? MOUSE_BUTTON5DOWN : 0;
2154 /* this packet type is reserved by Logitech... */
2156 * IBM ScrollPoint Mouse uses this packet type to
2157 * encode both vertical and horizontal scroll movement.
2160 /* horizontal count */
2161 if (sc->ipacket[2] & 0x0f)
2162 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2163 /* vertical count */
2164 if (sc->ipacket[2] & 0xf0)
2165 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2167 /* vertical count */
2168 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
2169 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16
2170 : ((sc->ipacket[2] >> 4) & 0x0f);
2171 /* horizontal count */
2172 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
2173 ? (sc->ipacket[2] & 0x0f) - 16
2174 : (sc->ipacket[2] & 0x0f);
2178 /* device type packet - shouldn't happen */
2182 ms.button = ms.obutton;
2184 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2185 "0x%02x 0x%02x 0x%02x\n",
2186 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
2187 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
2191 /* preserve button states */
2192 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2196 case MOUSE_MODEL_GLIDEPOINT:
2197 /* `tapping' action */
2198 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2201 case MOUSE_MODEL_NETSCROLL:
2202 /* three addtional bytes encode buttons and wheel events */
2203 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2204 ? MOUSE_BUTTON4DOWN : 0;
2205 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2206 ? MOUSE_BUTTON5DOWN : 0;
2207 z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
2208 ? sc->ipacket[4] - 256 : sc->ipacket[4];
2211 case MOUSE_MODEL_THINK:
2212 /* the fourth button state in the first byte */
2213 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2216 case MOUSE_MODEL_VERSAPAD:
2217 /* VersaPad PS/2 absolute mode message format
2219 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2220 * ipacket[0]: 1 1 0 A 1 L T R
2221 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2222 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2223 * ipacket[3]: 1 1 1 A 1 L T R
2224 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2225 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2228 * R: right physical mouse button (1=on)
2229 * T: touch pad virtual button (1=tapping)
2230 * L: left physical mouse button (1=on)
2231 * A: position data is valid (1=valid)
2232 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2233 * V: vertical data (12bit signed integer. V11 is sign bit.)
2236 * Tapping is mapped to MOUSE_BUTTON4.
2238 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2239 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2241 if (c & MOUSE_PS2VERSA_IN_USE) {
2242 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
2243 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
2248 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2251 if (x < 0) /* XXX */
2260 sc->flags |= PSM_FLAGS_FINGERDOWN;
2265 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2267 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2268 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2271 case MOUSE_MODEL_4D:
2273 * b7 b6 b5 b4 b3 b2 b1 b0
2274 * byte 1: s2 d2 s1 d1 1 M R L
2275 * byte 2: sx x x x x x x x
2276 * byte 3: sy y y y y y y y
2278 * s1: wheel 1 direction
2280 * s2: wheel 2 direction
2283 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2284 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2285 switch (c & MOUSE_4D_WHEELBITS) {
2292 case 0x40: /* 2nd wheel turning right XXX */
2295 case 0xc0: /* 2nd wheel turning left XXX */
2301 case MOUSE_MODEL_4DPLUS:
2302 if ((x < 16 - 256) && (y < 16 - 256)) {
2304 * b7 b6 b5 b4 b3 b2 b1 b0
2305 * byte 1: 0 0 1 1 1 M R L
2306 * byte 2: 0 0 0 0 1 0 0 0
2307 * byte 3: 0 0 0 0 S s d1 d0
2309 * L, M, R, S: left, middle, right and side buttons
2310 * s: wheel data sign bit
2314 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2315 ms.button |= MOUSE_BUTTON4DOWN;
2316 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2317 ? ((sc->ipacket[2] & 0x07) - 8)
2318 : (sc->ipacket[2] & 0x07) ;
2320 /* preserve previous button states */
2321 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2325 case MOUSE_MODEL_GENERIC:
2331 if (sc->mode.accelfactor >= 1) {
2333 x = x * x / sc->mode.accelfactor;
2336 if (c & MOUSE_PS2_XNEG)
2340 y = y * y / sc->mode.accelfactor;
2343 if (c & MOUSE_PS2_YNEG)
2351 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2352 | (ms.obutton ^ ms.button);
2354 if (sc->mode.level < PSM_LEVEL_NATIVE)
2355 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
2357 sc->status.flags |= ms.flags;
2358 sc->status.dx += ms.dx;
2359 sc->status.dy += ms.dy;
2360 sc->status.dz += ms.dz;
2361 sc->status.button = ms.button;
2362 sc->button = ms.button;
2364 sc->watchdog = FALSE;
2367 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2368 l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2369 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2370 if (sc->inputbytes > l)
2371 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
2373 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
2374 sc->queue.count += sc->inputbytes;
2378 if (sc->state & PSM_ASLP) {
2379 sc->state &= ~PSM_ASLP;
2380 wakeup((caddr_t) sc);
2382 selwakeup(&sc->rsel);
2387 psmpoll(dev_t dev, int events, struct thread *td)
2389 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2393 /* Return true if a mouse event available */
2395 if (events & (POLLIN | POLLRDNORM)) {
2396 if (sc->queue.count > 0)
2397 revents |= events & (POLLIN | POLLRDNORM);
2399 selrecord(td, &sc->rsel);
2406 /* vendor/model specific routines */
2408 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2410 if (set_mouse_resolution(kbdc, res) != res)
2412 if (set_mouse_scaling(kbdc, scale)
2413 && set_mouse_scaling(kbdc, scale)
2414 && set_mouse_scaling(kbdc, scale)
2415 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2421 mouse_ext_command(KBDC kbdc, int command)
2425 c = (command >> 6) & 0x03;
2426 if (set_mouse_resolution(kbdc, c) != c)
2428 c = (command >> 4) & 0x03;
2429 if (set_mouse_resolution(kbdc, c) != c)
2431 c = (command >> 2) & 0x03;
2432 if (set_mouse_resolution(kbdc, c) != c)
2434 c = (command >> 0) & 0x03;
2435 if (set_mouse_resolution(kbdc, c) != c)
2441 /* Logitech MouseMan Cordless II */
2443 enable_lcordless(struct psm_softc *sc)
2448 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2450 if (status[1] == PSMD_RES_HIGH)
2452 ch = (status[0] & 0x07) - 1; /* channel # */
2453 if ((ch <= 0) || (ch > 4))
2456 * status[1]: always one?
2457 * status[2]: battery status? (0-100)
2463 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2465 enable_groller(struct psm_softc *sc)
2470 * The special sequence to enable the fourth button and the
2471 * roller. Immediately after this sequence check status bytes.
2472 * if the mouse is NetScroll, the second and the third bytes are
2477 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2478 * look like the following.
2480 * byte 1 bit 7 always 0
2481 * bit 6 stream mode (0)
2482 * bit 5 disabled (0)
2483 * bit 4 1:1 scaling (0)
2485 * bit 0-2 button status
2486 * byte 2 resolution (PSMD_RES_HIGH)
2487 * byte 3 report rate (?)
2490 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2492 if ((status[1] != '3') || (status[2] != 'D'))
2494 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2499 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2501 enable_gmouse(struct psm_softc *sc)
2506 * The special sequence to enable the middle, "rubber" button.
2507 * Immediately after this sequence check status bytes.
2508 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2509 * the second and the third bytes are '3' and 'U'.
2510 * NOTE: NetMouse reports that it has three buttons although it has
2511 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2512 * say they have three buttons too and they do have a button on the
2515 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2517 if ((status[1] != '3') || (status[2] != 'U'))
2522 /* ALPS GlidePoint */
2524 enable_aglide(struct psm_softc *sc)
2529 * The special sequence to obtain ALPS GlidePoint specific
2530 * information. Immediately after this sequence, status bytes will
2531 * contain something interesting.
2532 * NOTE: ALPS produces several models of GlidePoint. Some of those
2533 * do not respond to this sequence, thus, cannot be detected this way.
2535 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2537 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2539 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2544 /* Kensington ThinkingMouse/Trackball */
2546 enable_kmouse(struct psm_softc *sc)
2548 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2549 KBDC kbdc = sc->kbdc;
2555 id1 = get_aux_id(kbdc);
2556 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2559 * The device is now in the native mode? It returns a different
2562 id2 = get_aux_id(kbdc);
2563 if ((id1 == id2) || (id2 != 2))
2566 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2569 /* at this point, resolution is LOW, sampling rate is 10/sec */
2570 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2575 * The special sequence to enable the third and fourth buttons.
2576 * Otherwise they behave like the first and second buttons.
2578 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2579 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2584 * At this point, the device is using default resolution and
2585 * sampling rate for the native mode.
2587 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2589 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2592 /* the device appears be enabled by this sequence, diable it for now */
2593 disable_aux_dev(kbdc);
2594 empty_aux_buffer(kbdc, 5);
2599 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2601 enable_mmanplus(struct psm_softc *sc)
2603 KBDC kbdc = sc->kbdc;
2606 /* the special sequence to enable the fourth button and the roller. */
2608 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2609 * must be called exactly three times since the last RESET command
2610 * before this sequence. XXX
2612 if (!set_mouse_scaling(kbdc, 1))
2614 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2616 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2620 * PS2++ protocl, packet type 0
2622 * b7 b6 b5 b4 b3 b2 b1 b0
2623 * byte 1: * 1 p3 p2 1 * * *
2624 * byte 2: 1 1 p1 p0 m1 m0 1 0
2625 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2627 * p3-p0: packet type: 0
2628 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2630 /* check constant bits */
2631 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2633 if ((data[1] & 0xc3) != 0xc2)
2635 /* check d3-d0 in byte 2 */
2636 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2639 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2642 sc->hw.hwid &= 0x00ff;
2643 sc->hw.hwid |= data[2] << 8; /* save model ID */
2646 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2647 * the wheel and the fourth button events are encoded in the
2648 * special data packet. The mouse may be put in the IntelliMouse mode
2649 * if it is initialized by the IntelliMouse's method.
2654 /* MS IntelliMouse Explorer */
2656 enable_msexplorer(struct psm_softc *sc)
2658 static unsigned char rate0[] = { 200, 100, 80, };
2659 static unsigned char rate1[] = { 200, 200, 80, };
2660 KBDC kbdc = sc->kbdc;
2664 /* the special sequence to enable the extra buttons and the roller. */
2665 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2666 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2669 /* the device will give the genuine ID only after the above sequence */
2670 id = get_aux_id(kbdc);
2671 if (id != PSM_EXPLORER_ID)
2675 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2678 * XXX: this is a kludge to fool some KVM switch products
2679 * which think they are clever enough to know the 4-byte IntelliMouse
2680 * protocol, and assume any other protocols use 3-byte packets.
2681 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2682 * correctly to the host computer because of this!
2683 * The following sequence is actually IntelliMouse's "wake up"
2684 * sequence; it will make the KVM think the mouse is IntelliMouse
2685 * when it is in fact IntelliMouse Explorer.
2687 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2688 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2691 id = get_aux_id(kbdc);
2696 /* MS IntelliMouse */
2698 enable_msintelli(struct psm_softc *sc)
2701 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2702 * probe routine and act like IntelliMouse.
2705 static unsigned char rate[] = { 200, 100, 80, };
2706 KBDC kbdc = sc->kbdc;
2710 /* the special sequence to enable the third button and the roller. */
2711 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2712 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2715 /* the device will give the genuine ID only after the above sequence */
2716 id = get_aux_id(kbdc);
2717 if (id != PSM_INTELLI_ID)
2726 /* A4 Tech 4D Mouse */
2728 enable_4dmouse(struct psm_softc *sc)
2731 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2734 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2735 KBDC kbdc = sc->kbdc;
2739 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2740 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2743 id = get_aux_id(kbdc);
2745 * WinEasy 4D, 4 Way Scroll 4D: 6
2746 * Cable-Free 4D: 8 (4DPLUS)
2747 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2749 if (id != PSM_4DMOUSE_ID)
2753 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2758 /* A4 Tech 4D+ Mouse */
2760 enable_4dplus(struct psm_softc *sc)
2763 * Newer wheel mice from A4 Tech seem to use this protocol.
2764 * Older models are recognized as either 4D Mouse or IntelliMouse.
2766 KBDC kbdc = sc->kbdc;
2770 * enable_4dmouse() already issued the following ID sequence...
2771 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2774 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2775 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2780 id = get_aux_id(kbdc);
2781 if (id != PSM_4DPLUS_ID)
2785 sc->hw.buttons = 4; /* XXX */
2790 /* Interlink electronics VersaPad */
2792 enable_versapad(struct psm_softc *sc)
2794 KBDC kbdc = sc->kbdc;
2797 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2798 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2799 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2800 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2801 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2802 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2803 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2805 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2807 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2809 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2811 return TRUE; /* PS/2 absolute mode */
2815 psmresume(device_t dev)
2817 struct psm_softc *sc = device_get_softc(dev);
2818 int unit = device_get_unit(dev);
2822 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2824 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2827 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2829 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2831 * Release the blocked process; it must be notified that the device
2832 * cannot be accessed anymore.
2834 sc->state &= ~PSM_ASLP;
2835 wakeup((caddr_t)sc);
2839 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2844 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);