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.
24 * Ported to 386bsd Oct 17, 1992
25 * Sandi Donno, Computer Science, University of Cape Town, South Africa
26 * Please send bug reports to sandi@cs.uct.ac.za
28 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
29 * although I was only partially successful in getting the alpha release
30 * of his "driver for the Logitech and ATI Inport Bus mice for use with
31 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
32 * found his code to be an invaluable reference when porting this driver
35 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
36 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
38 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
39 * Andrew Herbert - 12 June 1993
41 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
44 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
47 * Hardware access routines and probe logic rewritten by
48 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
49 * - 3, 14, 22 October 1996.
50 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
51 * - 14, 30 November 1996. Uses `kbdio.c'.
52 * - 13 December 1996. Uses queuing version of `kbdio.c'.
53 * - January/February 1997. Tweaked probe logic for
54 * HiNote UltraII/Latitude/Armada laptops.
55 * - 30 July 1997. Added APM support.
56 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
57 * Improved sync check logic.
58 * Vendor specific support routines.
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
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 <machine/bus.h>
76 #include <sys/selinfo.h>
80 #include <sys/limits.h>
81 #include <sys/mouse.h>
82 #include <machine/resource.h>
84 #include <isa/isavar.h>
85 #include <dev/kbd/atkbdcreg.h>
88 * Driver specific options: the following options may be set by
89 * `options' statements in the kernel configuration file.
94 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */
97 #ifndef PSM_SYNCERR_THRESHOLD1
98 #define PSM_SYNCERR_THRESHOLD1 20
101 #ifndef PSM_INPUT_TIMEOUT
102 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
105 /* end of driver specific options */
107 #define PSM_DRIVER_NAME "psm"
108 #define PSMCPNP_DRIVER_NAME "psmcpnp"
111 #define PSM_BUFSIZE 960
112 #define PSM_SMALLBUFSIZE 240
114 /* operation levels */
115 #define PSM_LEVEL_BASE 0
116 #define PSM_LEVEL_STANDARD 1
117 #define PSM_LEVEL_NATIVE 2
118 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
119 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
121 /* Logitech PS2++ protocol */
122 #define MOUSE_PS2PLUS_CHECKBITS(b) \
123 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
124 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
125 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
128 #define PSM_UNIT(dev) (minor(dev) >> 1)
129 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
130 #define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1))
133 typedef struct ringbuf {
134 int count; /* # of valid elements in the buffer */
135 int head; /* head pointer */
136 int tail; /* tail poiner */
137 unsigned char buf[PSM_BUFSIZE];
140 /* driver control block */
141 struct psm_softc { /* Driver status information */
143 struct selinfo rsel; /* Process selecting for Input */
144 unsigned char state; /* Mouse driver state */
145 int config; /* driver configuration flags */
146 int flags; /* other flags */
147 KBDC kbdc; /* handle to access the keyboard controller */
148 struct resource *intr; /* IRQ resource */
149 void *ih; /* interrupt handle */
150 mousehw_t hw; /* hardware information */
151 mousemode_t mode; /* operation mode */
152 mousemode_t dflt_mode; /* default operation mode */
153 mousestatus_t status; /* accumulated mouse movement */
154 ringbuf_t queue; /* mouse status queue */
155 unsigned char ipacket[16]; /* interim input buffer */
156 int inputbytes; /* # of bytes in the input buffer */
157 int button; /* the latest button state */
158 int xold; /* previous absolute X position */
159 int yold; /* previous absolute Y position */
161 struct timeval inputtimeout;
162 int watchdog; /* watchdog timer flag */
163 struct callout_handle callout; /* watchdog timer call out */
167 static devclass_t psm_devclass;
168 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
170 /* driver state flags (state) */
171 #define PSM_VALID 0x80
172 #define PSM_OPEN 1 /* Device is open */
173 #define PSM_ASLP 2 /* Waiting for mouse data */
175 /* driver configuration flags (config) */
176 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
177 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
178 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
179 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
180 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
181 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
182 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
183 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
184 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
185 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
187 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \
189 | PSM_CONFIG_NOCHECKSYNC \
190 | PSM_CONFIG_SYNCHACK \
191 | PSM_CONFIG_NOIDPROBE \
192 | PSM_CONFIG_NORESET \
193 | PSM_CONFIG_FORCETAP \
194 | PSM_CONFIG_IGNPORTERROR \
195 | PSM_CONFIG_HOOKRESUME \
196 | PSM_CONFIG_INITAFTERSUSPEND)
198 /* other flags (flags) */
199 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
201 /* for backward compatibility */
202 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
203 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
204 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
206 typedef struct old_mousehw {
213 typedef struct old_mousemode {
220 /* packet formatting function */
221 typedef int packetfunc_t(struct psm_softc *, unsigned char *,
222 int *, int, mousestatus_t *);
224 /* function prototypes */
225 static void psmidentify(driver_t *, device_t);
226 static int psmprobe(device_t);
227 static int psmattach(device_t);
228 static int psmdetach(device_t);
229 static int psmresume(device_t);
231 static d_open_t psmopen;
232 static d_close_t psmclose;
233 static d_read_t psmread;
234 static d_ioctl_t psmioctl;
235 static d_poll_t psmpoll;
237 static int enable_aux_dev(KBDC);
238 static int disable_aux_dev(KBDC);
239 static int get_mouse_status(KBDC, int *, int, int);
240 static int get_aux_id(KBDC);
241 static int set_mouse_sampling_rate(KBDC, int);
242 static int set_mouse_scaling(KBDC, int);
243 static int set_mouse_resolution(KBDC, int);
244 static int set_mouse_mode(KBDC);
245 static int get_mouse_buttons(KBDC);
246 static int is_a_mouse(int);
247 static void recover_from_error(KBDC);
248 static int restore_controller(KBDC, int);
249 static int doinitialize(struct psm_softc *, mousemode_t *);
250 static int doopen(struct psm_softc *, int);
251 static int reinitialize(struct psm_softc *, int);
252 static char *model_name(int);
253 static void psmintr(void *);
254 static void psmtimeout(void *);
256 /* vendor specific features */
257 typedef int probefunc_t(struct psm_softc *);
259 static int mouse_id_proc1(KBDC, int, int, int *);
260 static int mouse_ext_command(KBDC, int);
261 static probefunc_t enable_groller;
262 static probefunc_t enable_gmouse;
263 static probefunc_t enable_aglide;
264 static probefunc_t enable_kmouse;
265 static probefunc_t enable_msexplorer;
266 static probefunc_t enable_msintelli;
267 static probefunc_t enable_4dmouse;
268 static probefunc_t enable_4dplus;
269 static probefunc_t enable_mmanplus;
270 static probefunc_t enable_versapad;
271 static int tame_mouse(struct psm_softc *, mousestatus_t *, unsigned char *);
275 unsigned char syncmask;
277 probefunc_t *probefunc;
280 * WARNING: the order of probe is very important. Don't mess it
281 * unless you know what you are doing.
283 { MOUSE_MODEL_NET, /* Genius NetMouse */
284 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
285 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
286 0xc8, 6, enable_groller, },
287 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
288 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
289 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
290 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
291 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
292 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
293 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
294 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
295 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
296 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
297 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
298 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
299 { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */
300 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
301 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
302 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
303 { MOUSE_MODEL_GENERIC,
304 0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
306 #define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
308 /* device driver declarateion */
309 static device_method_t psm_methods[] = {
310 /* Device interface */
311 DEVMETHOD(device_identify, psmidentify),
312 DEVMETHOD(device_probe, psmprobe),
313 DEVMETHOD(device_attach, psmattach),
314 DEVMETHOD(device_detach, psmdetach),
315 DEVMETHOD(device_resume, psmresume),
320 static driver_t psm_driver = {
323 sizeof(struct psm_softc),
326 #define CDEV_MAJOR 21
328 static struct cdevsw psm_cdevsw = {
334 .d_name = PSM_DRIVER_NAME,
338 /* debug message level */
339 static int verbose = PSM_DEBUG;
341 /* device I/O routines */
343 enable_aux_dev(KBDC kbdc)
347 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
349 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
351 return (res == PSM_ACK);
355 disable_aux_dev(KBDC kbdc)
359 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
361 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
363 return (res == PSM_ACK);
367 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
376 cmd = PSMC_SEND_DEV_STATUS;
379 cmd = PSMC_SEND_DEV_DATA;
382 empty_aux_buffer(kbdc, 5);
383 res = send_aux_command(kbdc, cmd);
385 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
386 (flag == 1) ? "DATA" : "STATUS", res);
390 for (i = 0; i < len; ++i) {
391 status[i] = read_aux_data(kbdc);
397 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
398 (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
405 get_aux_id(KBDC kbdc)
410 empty_aux_buffer(kbdc, 5);
411 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
413 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
420 id = read_aux_data(kbdc);
422 log(LOG_DEBUG, "psm: device ID: %04x\n", id);
428 set_mouse_sampling_rate(KBDC kbdc, int rate)
432 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
434 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
436 return ((res == PSM_ACK) ? rate : -1);
440 set_mouse_scaling(KBDC kbdc, int scale)
447 scale = PSMC_SET_SCALING11;
450 scale = PSMC_SET_SCALING21;
453 res = send_aux_command(kbdc, scale);
455 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
456 (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
458 return (res == PSM_ACK);
461 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
463 set_mouse_resolution(KBDC kbdc, int val)
467 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
469 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
471 return ((res == PSM_ACK) ? val : -1);
475 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
476 * re-enabled by calling `enable_aux_dev()'
479 set_mouse_mode(KBDC kbdc)
483 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
485 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
487 return (res == PSM_ACK);
491 get_mouse_buttons(KBDC kbdc)
493 int c = 2; /* assume two buttons by default */
497 * NOTE: a special sequence to obtain Logitech Mouse specific
498 * information: set resolution to 25 ppi, set scaling to 1:1, set
499 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
500 * mouse status bytes is the number of available buttons.
501 * Some manufactures also support this sequence.
503 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
505 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
506 && set_mouse_scaling(kbdc, 1)
507 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
514 /* misc subroutines */
516 * Someday, I will get the complete list of valid pointing devices and
523 static int valid_ids[] = {
524 PSM_MOUSE_ID, /* mouse */
525 PSM_BALLPOINT_ID, /* ballpoint device */
526 PSM_INTELLI_ID, /* Intellimouse */
527 PSM_EXPLORER_ID, /* Intellimouse Explorer */
528 -1 /* end of table */
532 for (i = 0; valid_ids[i] >= 0; ++i)
533 if (valid_ids[i] == id)
542 model_name(int model)
548 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
549 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
550 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
551 { MOUSE_MODEL_THINK, "ThinkingMouse" },
552 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
553 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
554 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
555 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
556 { MOUSE_MODEL_4D, "4D Mouse" },
557 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
558 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
559 { MOUSE_MODEL_UNKNOWN, NULL },
563 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
564 if (models[i].model_code == model)
565 return models[i].model_name;
571 recover_from_error(KBDC kbdc)
573 /* discard anything left in the output buffer */
574 empty_both_buffers(kbdc, 10);
578 * NOTE: KBDC_RESET_KBD may not restore the communication between the
579 * keyboard and the controller.
584 * NOTE: somehow diagnostic and keyboard port test commands bring the
587 if (!test_controller(kbdc))
588 log(LOG_ERR, "psm: keyboard controller failed.\n");
589 /* if there isn't a keyboard in the system, the following error is OK */
590 if (test_kbd_port(kbdc) != 0) {
592 log(LOG_ERR, "psm: keyboard port failed.\n");
598 restore_controller(KBDC kbdc, int command_byte)
600 empty_both_buffers(kbdc, 10);
602 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
603 log(LOG_ERR, "psm: failed to restore the keyboard controller "
605 empty_both_buffers(kbdc, 10);
608 empty_both_buffers(kbdc, 10);
614 * Re-initialize the aux port and device. The aux port must be enabled
615 * and its interrupt must be disabled before calling this routine.
616 * The aux device will be disabled before returning.
617 * The keyboard controller must be locked via `kbdc_lock()' before
618 * calling this routine.
621 doinitialize(struct psm_softc *sc, mousemode_t *mode)
623 KBDC kbdc = sc->kbdc;
627 switch((i = test_aux_port(kbdc))) {
628 case 1: /* ignore this error */
631 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
634 case 0: /* no error */
636 case -1: /* time out */
638 recover_from_error(kbdc);
639 if (sc->config & PSM_CONFIG_IGNPORTERROR)
641 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
646 if (sc->config & PSM_CONFIG_NORESET) {
648 * Don't try to reset the pointing device. It may possibly be
649 * left in the unknown state, though...
653 * NOTE: some controllers appears to hang the `keyboard' when
654 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
656 if (!reset_aux_dev(kbdc)) {
657 recover_from_error(kbdc);
658 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
664 * both the aux port and the aux device is functioning, see
665 * if the device can be enabled.
667 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
668 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
671 empty_both_buffers(kbdc, 10); /* remove stray data if any */
673 if (sc->config & PSM_CONFIG_NOIDPROBE) {
674 i = GENERIC_MOUSE_ENTRY;
676 /* FIXME: hardware ID, mouse buttons? */
678 /* other parameters */
679 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
680 if ((*vendortype[i].probefunc)(sc)) {
682 log(LOG_ERR, "psm%d: found %s\n",
683 sc->unit, model_name(vendortype[i].model));
689 sc->hw.model = vendortype[i].model;
690 sc->mode.packetsize = vendortype[i].packetsize;
692 /* set mouse parameters */
693 if (mode != (mousemode_t *)NULL) {
695 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
696 if (mode->resolution >= 0)
697 mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
698 set_mouse_scaling(kbdc, 1);
699 set_mouse_mode(kbdc);
702 /* request a data packet and extract sync. bits */
703 if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
704 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
706 sc->mode.syncmask[0] = 0;
708 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
709 /* the NetScroll Mouse will send three more bytes... Ignore them */
710 empty_aux_buffer(kbdc, 5);
713 /* just check the status of the mouse */
714 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
715 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
722 doopen(struct psm_softc *sc, int command_byte)
726 /* enable the mouse device */
727 if (!enable_aux_dev(sc->kbdc)) {
728 /* MOUSE ERROR: failed to enable the mouse because:
729 * 1) the mouse is faulty,
730 * 2) the mouse has been removed(!?)
731 * In the latter case, the keyboard may have hung, and need
732 * recovery procedure...
734 recover_from_error(sc->kbdc);
736 /* FIXME: we could reset the mouse here and try to enable
737 * it again. But it will take long time and it's not a good
738 * idea to disable the keyboard that long...
740 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
741 recover_from_error(sc->kbdc);
745 restore_controller(sc->kbdc, command_byte);
746 /* mark this device is no longer available */
747 sc->state &= ~PSM_VALID;
748 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
754 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
755 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
757 /* enable the aux port and interrupt */
758 if (!set_controller_command_byte(sc->kbdc,
759 kbdc_get_device_mask(sc->kbdc),
760 (command_byte & KBD_KBD_CONTROL_BITS)
761 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
762 /* CONTROLLER ERROR */
763 disable_aux_dev(sc->kbdc);
764 restore_controller(sc->kbdc, command_byte);
765 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
770 /* start the watchdog timer */
771 sc->watchdog = FALSE;
772 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
778 reinitialize(struct psm_softc *sc, int doinit)
784 /* don't let anybody mess with the aux device */
785 if (!kbdc_lock(sc->kbdc, TRUE))
789 /* block our watchdog timer */
790 sc->watchdog = FALSE;
791 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
792 callout_handle_init(&sc->callout);
794 /* save the current controller command byte */
795 empty_both_buffers(sc->kbdc, 10);
796 c = get_controller_command_byte(sc->kbdc);
798 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
801 /* enable the aux port but disable the aux interrupt and the keyboard */
802 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
803 kbdc_get_device_mask(sc->kbdc),
804 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
805 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
806 /* CONTROLLER ERROR */
808 kbdc_lock(sc->kbdc, FALSE);
809 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
815 if (sc->state & PSM_VALID) {
816 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */
817 empty_aux_buffer(sc->kbdc, 10);
822 /* try to detect the aux device; are you still there? */
825 if (doinitialize(sc, &sc->mode)) {
827 sc->state |= PSM_VALID;
829 /* the device has gone! */
830 restore_controller(sc->kbdc, c);
831 sc->state &= ~PSM_VALID;
832 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
839 /* restore the driver state */
840 if ((sc->state & PSM_OPEN) && (err == 0)) {
841 /* enable the aux device and the port again */
844 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
847 /* restore the keyboard port and disable the aux port */
848 if (!set_controller_command_byte(sc->kbdc,
849 kbdc_get_device_mask(sc->kbdc),
850 (c & KBD_KBD_CONTROL_BITS)
851 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
852 /* CONTROLLER ERROR */
853 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
859 kbdc_lock(sc->kbdc, FALSE);
863 /* psm driver entry points */
866 psmidentify(driver_t *driver, device_t parent)
873 unit = device_get_unit(parent);
875 /* always add at least one child */
876 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
880 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
885 * If the PS/2 mouse device has already been reported by ACPI or
886 * PnP BIOS, obtain the IRQ resource from it.
887 * (See psmcpnp_attach() below.)
889 psmc = device_find_child(device_get_parent(parent),
890 PSMCPNP_DRIVER_NAME, unit);
893 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
896 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
899 #define endprobe(v) do { if (bootverbose) \
901 kbdc_set_device_mask(sc->kbdc, mask); \
902 kbdc_lock(sc->kbdc, FALSE); \
907 psmprobe(device_t dev)
909 int unit = device_get_unit(dev);
910 struct psm_softc *sc = device_get_softc(dev);
921 /* see if IRQ is available */
923 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
924 RF_SHAREABLE | RF_ACTIVE);
925 if (sc->intr == NULL) {
927 device_printf(dev, "unable to allocate IRQ\n");
930 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
933 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
934 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
935 /* XXX: for backward compatibility */
936 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
938 #ifdef PSM_RESETAFTERSUSPEND
939 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
941 PSM_CONFIG_HOOKRESUME;
943 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
948 device_set_desc(dev, "PS/2 Mouse");
950 if (!kbdc_lock(sc->kbdc, TRUE)) {
951 printf("psm%d: unable to lock the controller.\n", unit);
958 * NOTE: two bits in the command byte controls the operation of the
959 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
960 * port interrupt (IRQ 12) enable bit (bit 2).
963 /* discard anything left after the keyboard initialization */
964 empty_both_buffers(sc->kbdc, 10);
966 /* save the current command byte; it will be used later */
967 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
968 command_byte = get_controller_command_byte(sc->kbdc);
970 printf("psm%d: current command byte:%04x\n", unit, command_byte);
971 if (command_byte == -1) {
972 /* CONTROLLER ERROR */
973 printf("psm%d: unable to get the current command byte value.\n",
979 * disable the keyboard port while probing the aux port, which must be
980 * enabled during this routine
982 if (!set_controller_command_byte(sc->kbdc,
983 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
984 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
985 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
987 * this is CONTROLLER ERROR; I don't know how to recover
990 restore_controller(sc->kbdc, command_byte);
991 printf("psm%d: unable to set the command byte.\n", unit);
994 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
997 * NOTE: `test_aux_port()' is designed to return with zero if the aux
998 * port exists and is functioning. However, some controllers appears
999 * to respond with zero even when the aux port doesn't exist. (It may
1000 * be that this is only the case when the controller DOES have the aux
1001 * port but the port is not wired on the motherboard.) The keyboard
1002 * controllers without the port, such as the original AT, are
1003 * supporsed to return with an error code or simply time out. In any
1004 * case, we have to continue probing the port even when the controller
1007 * XXX: some controllers erroneously return the error code 1 when
1008 * it has the perfectly functional aux port. We have to ignore this
1009 * error code. Even if the controller HAS error with the aux port,
1010 * it will be detected later...
1011 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1013 switch ((i = test_aux_port(sc->kbdc))) {
1014 case 1: /* ignore this error */
1017 printf("psm%d: strange result for test aux port (%d).\n",
1020 case 0: /* no error */
1022 case -1: /* time out */
1023 default: /* error */
1024 recover_from_error(sc->kbdc);
1025 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1027 restore_controller(sc->kbdc, command_byte);
1029 printf("psm%d: the aux port is not functioning (%d).\n",
1034 if (sc->config & PSM_CONFIG_NORESET) {
1036 * Don't try to reset the pointing device. It may possibly be
1037 * left in the unknown state, though...
1041 * NOTE: some controllers appears to hang the `keyboard' when the aux
1042 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1044 * Attempt to reset the controller twice -- this helps
1045 * pierce through some KVM switches. The second reset
1048 if (!reset_aux_dev(sc->kbdc)) {
1049 recover_from_error(sc->kbdc);
1050 restore_controller(sc->kbdc, command_byte);
1052 printf("psm%d: failed to reset the aux device.\n", unit);
1054 } else if (!reset_aux_dev(sc->kbdc)) {
1055 recover_from_error(sc->kbdc);
1057 printf("psm%d: failed to reset the aux device (2).\n",
1063 * both the aux port and the aux device is functioning, see if the
1064 * device can be enabled. NOTE: when enabled, the device will start
1065 * sending data; we shall immediately disable the device once we know
1066 * the device can be enabled.
1068 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1070 recover_from_error(sc->kbdc);
1071 restore_controller(sc->kbdc, command_byte);
1073 printf("psm%d: failed to enable the aux device.\n", unit);
1077 /* save the default values after reset */
1078 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1079 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1080 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1082 sc->dflt_mode.rate = sc->mode.rate = -1;
1083 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1086 /* hardware information */
1087 sc->hw.iftype = MOUSE_IF_PS2;
1089 /* verify the device is a mouse */
1090 sc->hw.hwid = get_aux_id(sc->kbdc);
1091 if (!is_a_mouse(sc->hw.hwid)) {
1092 restore_controller(sc->kbdc, command_byte);
1094 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1097 switch (sc->hw.hwid) {
1098 case PSM_BALLPOINT_ID:
1099 sc->hw.type = MOUSE_TRACKBALL;
1102 case PSM_INTELLI_ID:
1103 case PSM_EXPLORER_ID:
1104 case PSM_4DMOUSE_ID:
1106 sc->hw.type = MOUSE_MOUSE;
1109 sc->hw.type = MOUSE_UNKNOWN;
1113 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1115 i = GENERIC_MOUSE_ENTRY;
1118 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1120 /* other parameters */
1121 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1122 if ((*vendortype[i].probefunc)(sc)) {
1124 printf("psm%d: found %s\n",
1125 unit, model_name(vendortype[i].model));
1131 sc->hw.model = vendortype[i].model;
1133 sc->dflt_mode.level = PSM_LEVEL_BASE;
1134 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1135 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1136 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1137 sc->dflt_mode.syncmask[0] = 0;
1139 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1140 if (sc->config & PSM_CONFIG_FORCETAP)
1141 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1142 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1143 sc->mode = sc->dflt_mode;
1144 sc->mode.packetsize = vendortype[i].packetsize;
1146 /* set mouse parameters */
1149 * A version of Logitech FirstMouse+ won't report wheel movement,
1150 * if SET_DEFAULTS is sent... Don't use this command.
1151 * This fix was found by Takashi Nishida.
1153 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1155 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1157 if (sc->config & PSM_CONFIG_RESOLUTION) {
1159 = set_mouse_resolution(sc->kbdc,
1160 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1161 } else if (sc->mode.resolution >= 0) {
1163 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1165 if (sc->mode.rate > 0) {
1166 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1168 set_mouse_scaling(sc->kbdc, 1);
1170 /* request a data packet and extract sync. bits */
1171 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1172 printf("psm%d: failed to get data.\n", unit);
1173 sc->mode.syncmask[0] = 0;
1175 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1176 /* the NetScroll Mouse will send three more bytes... Ignore them */
1177 empty_aux_buffer(sc->kbdc, 5);
1180 /* just check the status of the mouse */
1182 * NOTE: XXX there are some arcane controller/mouse combinations out
1183 * there, which hung the controller unless there is data transmission
1184 * after ACK from the mouse.
1186 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1187 printf("psm%d: failed to get status.\n", unit);
1190 * When in its native mode, some mice operate with different
1191 * default parameters than in the PS/2 compatible mode.
1193 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1194 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1197 /* disable the aux port for now... */
1198 if (!set_controller_command_byte(sc->kbdc,
1199 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1200 (command_byte & KBD_KBD_CONTROL_BITS)
1201 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1203 * this is CONTROLLER ERROR; I don't know the proper way to
1204 * recover from this error...
1206 restore_controller(sc->kbdc, command_byte);
1207 printf("psm%d: unable to set the command byte.\n", unit);
1212 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1213 kbdc_lock(sc->kbdc, FALSE);
1218 psmattach(device_t dev)
1220 int unit = device_get_unit(dev);
1221 struct psm_softc *sc = device_get_softc(dev);
1225 if (sc == NULL) /* shouldn't happen */
1228 /* Setup initial state */
1229 sc->state = PSM_VALID;
1230 callout_handle_init(&sc->callout);
1232 /* Setup our interrupt handler */
1234 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1235 RF_SHAREABLE | RF_ACTIVE);
1236 if (sc->intr == NULL)
1238 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1240 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1245 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1247 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1251 printf("psm%d: model %s, device ID %d\n",
1252 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1254 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1255 unit, model_name(sc->hw.model),
1256 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1257 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1258 unit, sc->config, sc->flags, sc->mode.packetsize);
1259 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1260 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1270 psmdetach(device_t dev)
1272 struct psm_softc *sc;
1275 sc = device_get_softc(dev);
1276 if (sc->state & PSM_OPEN)
1280 bus_teardown_intr(dev, sc->intr, sc->ih);
1281 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1283 destroy_dev(sc->dev);
1284 destroy_dev(sc->bdev);
1290 psmopen(dev_t dev, int flag, int fmt, struct thread *td)
1292 int unit = PSM_UNIT(dev);
1293 struct psm_softc *sc;
1298 /* Get device data */
1299 sc = PSM_SOFTC(unit);
1300 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1301 /* the device is no longer valid/functioning */
1304 /* Disallow multiple opens */
1305 if (sc->state & PSM_OPEN)
1308 device_busy(devclass_get_device(psm_devclass, unit));
1310 /* Initialize state */
1311 sc->mode.level = sc->dflt_mode.level;
1312 sc->mode.protocol = sc->dflt_mode.protocol;
1313 sc->watchdog = FALSE;
1315 /* flush the event queue */
1316 sc->queue.count = 0;
1319 sc->status.flags = 0;
1320 sc->status.button = 0;
1321 sc->status.obutton = 0;
1327 /* empty input buffer */
1328 bzero(sc->ipacket, sizeof(sc->ipacket));
1332 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1333 if (!kbdc_lock(sc->kbdc, TRUE))
1336 /* save the current controller command byte */
1338 command_byte = get_controller_command_byte(sc->kbdc);
1340 /* enable the aux port and temporalily disable the keyboard */
1341 if ((command_byte == -1)
1342 || !set_controller_command_byte(sc->kbdc,
1343 kbdc_get_device_mask(sc->kbdc),
1344 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1345 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1346 /* CONTROLLER ERROR; do you know how to get out of this? */
1347 kbdc_lock(sc->kbdc, FALSE);
1349 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1354 * Now that the keyboard controller is told not to generate
1355 * the keyboard and mouse interrupts, call `splx()' to allow
1356 * the other tty interrupts. The clock interrupt may also occur,
1357 * but timeout routines will be blocked by the poll flag set
1362 /* enable the mouse device */
1363 err = doopen(sc, command_byte);
1367 sc->state |= PSM_OPEN;
1368 kbdc_lock(sc->kbdc, FALSE);
1373 psmclose(dev_t dev, int flag, int fmt, struct thread *td)
1375 int unit = PSM_UNIT(dev);
1376 struct psm_softc *sc = PSM_SOFTC(unit);
1381 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1382 if (!kbdc_lock(sc->kbdc, TRUE))
1385 /* save the current controller command byte */
1387 command_byte = get_controller_command_byte(sc->kbdc);
1388 if (command_byte == -1) {
1389 kbdc_lock(sc->kbdc, FALSE);
1394 /* disable the aux interrupt and temporalily disable the keyboard */
1395 if (!set_controller_command_byte(sc->kbdc,
1396 kbdc_get_device_mask(sc->kbdc),
1397 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1398 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1399 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1401 /* CONTROLLER ERROR;
1402 * NOTE: we shall force our way through. Because the only
1403 * ill effect we shall see is that we may not be able
1404 * to read ACK from the mouse, and it doesn't matter much
1405 * so long as the mouse will accept the DISABLE command.
1410 /* stop the watchdog timer */
1411 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1412 callout_handle_init(&sc->callout);
1414 /* remove anything left in the output buffer */
1415 empty_aux_buffer(sc->kbdc, 10);
1417 /* disable the aux device, port and interrupt */
1418 if (sc->state & PSM_VALID) {
1419 if (!disable_aux_dev(sc->kbdc)) {
1421 * NOTE: we don't return error and continue, pretending
1422 * we have successfully disabled the device. It's OK because
1423 * the interrupt routine will discard any data from the mouse
1426 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1430 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1431 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1435 if (!set_controller_command_byte(sc->kbdc,
1436 kbdc_get_device_mask(sc->kbdc),
1437 (command_byte & KBD_KBD_CONTROL_BITS)
1438 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1439 /* CONTROLLER ERROR;
1440 * we shall ignore this error; see the above comment.
1442 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1446 /* remove anything left in the output buffer */
1447 empty_aux_buffer(sc->kbdc, 10);
1449 /* close is almost always successful */
1450 sc->state &= ~PSM_OPEN;
1451 kbdc_lock(sc->kbdc, FALSE);
1452 device_unbusy(devclass_get_device(psm_devclass, unit));
1457 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
1459 static unsigned char butmapps2[8] = {
1461 MOUSE_PS2_BUTTON1DOWN,
1462 MOUSE_PS2_BUTTON2DOWN,
1463 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1464 MOUSE_PS2_BUTTON3DOWN,
1465 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1466 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1467 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1469 static unsigned char butmapmsc[8] = {
1470 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1471 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1472 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1473 MOUSE_MSC_BUTTON3UP,
1474 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1475 MOUSE_MSC_BUTTON2UP,
1476 MOUSE_MSC_BUTTON1UP,
1482 if (sc->mode.level == PSM_LEVEL_BASE) {
1483 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1484 if (status->button & MOUSE_BUTTON4DOWN)
1485 mapped |= MOUSE_BUTTON1DOWN;
1486 status->button = mapped;
1487 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1488 i = imax(imin(status->dx, 255), -256);
1490 buf[0] |= MOUSE_PS2_XNEG;
1492 i = imax(imin(status->dy, 255), -256);
1494 buf[0] |= MOUSE_PS2_YNEG;
1496 return MOUSE_PS2_PACKETSIZE;
1497 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1498 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1499 i = imax(imin(status->dx, 255), -256);
1501 buf[3] = i - buf[1];
1502 i = imax(imin(status->dy, 255), -256);
1504 buf[4] = i - buf[2];
1505 i = imax(imin(status->dz, 127), -128);
1506 buf[5] = (i >> 1) & 0x7f;
1507 buf[6] = (i - (i >> 1)) & 0x7f;
1508 buf[7] = (~status->button >> 3) & 0x7f;
1509 return MOUSE_SYS_PACKETSIZE;
1511 return sc->inputbytes;
1515 psmread(dev_t dev, struct uio *uio, int flag)
1517 register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1518 unsigned char buf[PSM_SMALLBUFSIZE];
1523 if ((sc->state & PSM_VALID) == 0)
1526 /* block until mouse activity occured */
1528 while (sc->queue.count <= 0) {
1529 if (PSM_NBLOCKIO(dev)) {
1533 sc->state |= PSM_ASLP;
1534 error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
1535 sc->state &= ~PSM_ASLP;
1539 } else if ((sc->state & PSM_VALID) == 0) {
1540 /* the device disappeared! */
1547 /* copy data to the user land */
1548 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1550 l = imin(sc->queue.count, uio->uio_resid);
1551 if (l > sizeof(buf))
1553 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1554 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1555 sizeof(sc->queue.buf) - sc->queue.head);
1556 bcopy(&sc->queue.buf[0],
1557 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1558 l - (sizeof(sc->queue.buf) - sc->queue.head));
1560 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1562 sc->queue.count -= l;
1563 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1565 error = uiomove(buf, l, uio);
1574 block_mouse_data(struct psm_softc *sc, int *c)
1578 if (!kbdc_lock(sc->kbdc, TRUE))
1582 *c = get_controller_command_byte(sc->kbdc);
1584 || !set_controller_command_byte(sc->kbdc,
1585 kbdc_get_device_mask(sc->kbdc),
1586 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1587 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1588 /* this is CONTROLLER ERROR */
1590 kbdc_lock(sc->kbdc, FALSE);
1595 * The device may be in the middle of status data transmission.
1596 * The transmission will be interrupted, thus, incomplete status
1597 * data must be discarded. Although the aux interrupt is disabled
1598 * at the keyboard controller level, at most one aux interrupt
1599 * may have already been pending and a data byte is in the
1600 * output buffer; throw it away. Note that the second argument
1601 * to `empty_aux_buffer()' is zero, so that the call will just
1602 * flush the internal queue.
1603 * `psmintr()' will be invoked after `splx()' if an interrupt is
1604 * pending; it will see no data and returns immediately.
1606 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1607 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1615 unblock_mouse_data(struct psm_softc *sc, int c)
1620 * We may have seen a part of status data during `set_mouse_XXX()'.
1621 * they have been queued; flush it.
1623 empty_aux_buffer(sc->kbdc, 0);
1625 /* restore ports and interrupt */
1626 if (!set_controller_command_byte(sc->kbdc,
1627 kbdc_get_device_mask(sc->kbdc),
1628 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1629 /* CONTROLLER ERROR; this is serious, we may have
1630 * been left with the inaccessible keyboard and
1631 * the disabled mouse interrupt.
1636 kbdc_lock(sc->kbdc, FALSE);
1641 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1643 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1645 mousestatus_t status;
1646 #if (defined(MOUSE_GETVARS))
1655 /* Perform IOCTL command */
1658 case OLD_MOUSE_GETHWINFO:
1660 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1661 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1662 ((old_mousehw_t *)addr)->type = sc->hw.type;
1663 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1667 case MOUSE_GETHWINFO:
1669 *(mousehw_t *)addr = sc->hw;
1670 if (sc->mode.level == PSM_LEVEL_BASE)
1671 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1675 case OLD_MOUSE_GETMODE:
1677 switch (sc->mode.level) {
1678 case PSM_LEVEL_BASE:
1679 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1681 case PSM_LEVEL_STANDARD:
1682 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1684 case PSM_LEVEL_NATIVE:
1685 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1688 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1689 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1690 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1696 *(mousemode_t *)addr = sc->mode;
1697 ((mousemode_t *)addr)->resolution =
1698 MOUSE_RES_LOW - sc->mode.resolution;
1699 switch (sc->mode.level) {
1700 case PSM_LEVEL_BASE:
1701 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1702 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1704 case PSM_LEVEL_STANDARD:
1705 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1706 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1707 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1708 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1710 case PSM_LEVEL_NATIVE:
1711 /* FIXME: this isn't quite correct... XXX */
1712 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1718 case OLD_MOUSE_SETMODE:
1720 if (cmd == OLD_MOUSE_SETMODE) {
1721 mode.rate = ((old_mousemode_t *)addr)->rate;
1723 * resolution old I/F new I/F
1730 if (((old_mousemode_t *)addr)->resolution > 0)
1731 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1732 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1735 mode = *(mousemode_t *)addr;
1738 /* adjust and validate parameters. */
1739 if (mode.rate > UCHAR_MAX)
1742 mode.rate = sc->dflt_mode.rate;
1743 else if (mode.rate == -1)
1744 /* don't change the current setting */
1746 else if (mode.rate < 0)
1748 if (mode.resolution >= UCHAR_MAX)
1750 if (mode.resolution >= 200)
1751 mode.resolution = MOUSE_RES_HIGH;
1752 else if (mode.resolution >= 100)
1753 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1754 else if (mode.resolution >= 50)
1755 mode.resolution = MOUSE_RES_MEDIUMLOW;
1756 else if (mode.resolution > 0)
1757 mode.resolution = MOUSE_RES_LOW;
1758 if (mode.resolution == MOUSE_RES_DEFAULT)
1759 mode.resolution = sc->dflt_mode.resolution;
1760 else if (mode.resolution == -1)
1761 /* don't change the current setting */
1763 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1764 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1765 if (mode.level == -1)
1766 /* don't change the current setting */
1767 mode.level = sc->mode.level;
1768 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1770 if (mode.accelfactor == -1)
1771 /* don't change the current setting */
1772 mode.accelfactor = sc->mode.accelfactor;
1773 else if (mode.accelfactor < 0)
1776 /* don't allow anybody to poll the keyboard controller */
1777 error = block_mouse_data(sc, &command_byte);
1781 /* set mouse parameters */
1783 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1784 if (mode.resolution >= 0)
1785 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1786 set_mouse_scaling(sc->kbdc, 1);
1787 get_mouse_status(sc->kbdc, stat, 0, 3);
1790 sc->mode.rate = mode.rate;
1791 sc->mode.resolution = mode.resolution;
1792 sc->mode.accelfactor = mode.accelfactor;
1793 sc->mode.level = mode.level;
1796 unblock_mouse_data(sc, command_byte);
1799 case MOUSE_GETLEVEL:
1800 *(int *)addr = sc->mode.level;
1803 case MOUSE_SETLEVEL:
1804 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1806 sc->mode.level = *(int *)addr;
1809 case MOUSE_GETSTATUS:
1811 status = sc->status;
1812 sc->status.flags = 0;
1813 sc->status.obutton = sc->status.button;
1814 sc->status.button = 0;
1819 *(mousestatus_t *)addr = status;
1822 #if (defined(MOUSE_GETVARS))
1824 var = (mousevar_t *)addr;
1825 bzero(var, sizeof(*var));
1827 var->var[0] = MOUSE_VARS_PS2_SIG;
1828 var->var[1] = sc->config;
1829 var->var[2] = sc->flags;
1835 #endif /* MOUSE_GETVARS */
1837 case MOUSE_READSTATE:
1838 case MOUSE_READDATA:
1839 data = (mousedata_t *)addr;
1840 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1843 error = block_mouse_data(sc, &command_byte);
1846 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1847 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1849 unblock_mouse_data(sc, command_byte);
1852 #if (defined(MOUSE_SETRESOLUTION))
1853 case MOUSE_SETRESOLUTION:
1854 mode.resolution = *(int *)addr;
1855 if (mode.resolution >= UCHAR_MAX)
1857 else if (mode.resolution >= 200)
1858 mode.resolution = MOUSE_RES_HIGH;
1859 else if (mode.resolution >= 100)
1860 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1861 else if (mode.resolution >= 50)
1862 mode.resolution = MOUSE_RES_MEDIUMLOW;
1863 else if (mode.resolution > 0)
1864 mode.resolution = MOUSE_RES_LOW;
1865 if (mode.resolution == MOUSE_RES_DEFAULT)
1866 mode.resolution = sc->dflt_mode.resolution;
1867 else if (mode.resolution == -1)
1868 mode.resolution = sc->mode.resolution;
1869 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1870 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1872 error = block_mouse_data(sc, &command_byte);
1875 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1876 if (sc->mode.resolution != mode.resolution)
1878 unblock_mouse_data(sc, command_byte);
1880 #endif /* MOUSE_SETRESOLUTION */
1882 #if (defined(MOUSE_SETRATE))
1884 mode.rate = *(int *)addr;
1885 if (mode.rate > UCHAR_MAX)
1888 mode.rate = sc->dflt_mode.rate;
1889 else if (mode.rate < 0)
1890 mode.rate = sc->mode.rate;
1892 error = block_mouse_data(sc, &command_byte);
1895 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1896 if (sc->mode.rate != mode.rate)
1898 unblock_mouse_data(sc, command_byte);
1900 #endif /* MOUSE_SETRATE */
1902 #if (defined(MOUSE_SETSCALING))
1903 case MOUSE_SETSCALING:
1904 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1907 error = block_mouse_data(sc, &command_byte);
1910 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1912 unblock_mouse_data(sc, command_byte);
1914 #endif /* MOUSE_SETSCALING */
1916 #if (defined(MOUSE_GETHWID))
1918 error = block_mouse_data(sc, &command_byte);
1921 sc->hw.hwid &= ~0x00ff;
1922 sc->hw.hwid |= get_aux_id(sc->kbdc);
1923 *(int *)addr = sc->hw.hwid & 0x00ff;
1924 unblock_mouse_data(sc, command_byte);
1926 #endif /* MOUSE_GETHWID */
1936 psmtimeout(void *arg)
1938 struct psm_softc *sc;
1941 sc = (struct psm_softc *)arg;
1943 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1945 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1947 kbdc_lock(sc->kbdc, FALSE);
1949 sc->watchdog = TRUE;
1951 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
1958 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
1959 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
1961 static int butmap[8] = {
1965 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1967 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1968 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1969 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1971 static int butmap_versapad[8] = {
1977 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1979 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
1981 register struct psm_softc *sc = arg;
1989 /* read until there is nothing to read */
1990 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
1992 /* discard the byte if the device is not open */
1993 if ((sc->state & PSM_OPEN) == 0)
1996 getmicrouptime(&tv);
1997 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
1998 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2002 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2003 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2004 timevaladd(&sc->inputtimeout, &tv);
2006 sc->ipacket[sc->inputbytes++] = c;
2007 if (sc->inputbytes < sc->mode.packetsize)
2011 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2012 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
2013 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
2018 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2019 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
2020 c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
2022 if (sc->syncerrors < sc->mode.packetsize) {
2023 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2025 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2026 } else if (sc->syncerrors == sc->mode.packetsize) {
2027 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2029 disable_aux_dev(sc->kbdc);
2030 enable_aux_dev(sc->kbdc);
2031 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2032 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2034 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2035 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2036 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2037 reinitialize(sc, TRUE);
2043 * A kludge for Kensington device!
2044 * The MSB of the horizontal count appears to be stored in
2047 if (sc->hw.model == MOUSE_MODEL_THINK)
2048 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2050 /* ignore the overflow bits... */
2051 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2052 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2054 ms.obutton = sc->button; /* previous button state */
2055 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2056 /* `tapping' action */
2057 if (sc->config & PSM_CONFIG_FORCETAP)
2058 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2060 switch (sc->hw.model) {
2062 case MOUSE_MODEL_EXPLORER:
2064 * b7 b6 b5 b4 b3 b2 b1 b0
2065 * byte 1: oy ox sy sx 1 M R L
2066 * byte 2: x x x x x x x x
2067 * byte 3: y y y y y y y y
2068 * byte 4: * * S2 S1 s d2 d1 d0
2070 * L, M, R, S1, S2: left, middle, right and side buttons
2071 * s: wheel data sign bit
2074 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2075 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
2076 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2077 ? MOUSE_BUTTON4DOWN : 0;
2078 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2079 ? MOUSE_BUTTON5DOWN : 0;
2082 case MOUSE_MODEL_INTELLI:
2083 case MOUSE_MODEL_NET:
2084 /* wheel data is in the fourth byte */
2085 z = (char)sc->ipacket[3];
2086 /* some mice may send 7 when there is no Z movement?! XXX */
2087 if ((z >= 7) || (z <= -7))
2089 /* some compatible mice have additional buttons */
2090 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2091 ? MOUSE_BUTTON4DOWN : 0;
2092 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2093 ? MOUSE_BUTTON5DOWN : 0;
2096 case MOUSE_MODEL_MOUSEMANPLUS:
2098 * PS2++ protocl packet
2100 * b7 b6 b5 b4 b3 b2 b1 b0
2101 * byte 1: * 1 p3 p2 1 * * *
2102 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2104 * p3-p0: packet type
2105 * c1, c2: c1 & c2 == 1, if p2 == 0
2106 * c1 & c2 == 0, if p2 == 1
2108 * packet type: 0 (device type)
2109 * See comments in enable_mmanplus() below.
2111 * packet type: 1 (wheel data)
2113 * b7 b6 b5 b4 b3 b2 b1 b0
2114 * byte 3: h * B5 B4 s d2 d1 d0
2116 * h: 1, if horizontal roller data
2117 * 0, if vertical roller data
2118 * B4, B5: button 4 and 5
2120 * d2-d0: roller data
2122 * packet type: 2 (reserved)
2124 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2126 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
2127 /* the extended data packet encodes button and wheel events */
2128 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
2130 /* wheel data packet */
2132 if (sc->ipacket[2] & 0x80) {
2133 /* horizontal roller count - ignore it XXX*/
2135 /* vertical roller count */
2136 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2137 ? (sc->ipacket[2] & 0x0f) - 16
2138 : (sc->ipacket[2] & 0x0f);
2140 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2141 ? MOUSE_BUTTON4DOWN : 0;
2142 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2143 ? MOUSE_BUTTON5DOWN : 0;
2146 /* this packet type is reserved by Logitech... */
2148 * IBM ScrollPoint Mouse uses this packet type to
2149 * encode both vertical and horizontal scroll movement.
2152 /* horizontal count */
2153 if (sc->ipacket[2] & 0x0f)
2154 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2155 /* vertical count */
2156 if (sc->ipacket[2] & 0xf0)
2157 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2159 /* vertical count */
2160 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
2161 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16
2162 : ((sc->ipacket[2] >> 4) & 0x0f);
2163 /* horizontal count */
2164 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
2165 ? (sc->ipacket[2] & 0x0f) - 16
2166 : (sc->ipacket[2] & 0x0f);
2170 /* device type packet - shouldn't happen */
2174 ms.button = ms.obutton;
2176 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2177 "0x%02x 0x%02x 0x%02x\n",
2178 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
2179 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
2183 /* preserve button states */
2184 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2188 case MOUSE_MODEL_GLIDEPOINT:
2189 /* `tapping' action */
2190 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2193 case MOUSE_MODEL_NETSCROLL:
2194 /* three addtional bytes encode buttons and wheel events */
2195 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2196 ? MOUSE_BUTTON4DOWN : 0;
2197 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2198 ? MOUSE_BUTTON5DOWN : 0;
2199 z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
2200 ? sc->ipacket[4] - 256 : sc->ipacket[4];
2203 case MOUSE_MODEL_THINK:
2204 /* the fourth button state in the first byte */
2205 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2208 case MOUSE_MODEL_VERSAPAD:
2209 /* VersaPad PS/2 absolute mode message format
2211 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2212 * ipacket[0]: 1 1 0 A 1 L T R
2213 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2214 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2215 * ipacket[3]: 1 1 1 A 1 L T R
2216 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2217 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2220 * R: right physical mouse button (1=on)
2221 * T: touch pad virtual button (1=tapping)
2222 * L: left physical mouse button (1=on)
2223 * A: position data is valid (1=valid)
2224 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2225 * V: vertical data (12bit signed integer. V11 is sign bit.)
2228 * Tapping is mapped to MOUSE_BUTTON4.
2230 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2231 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2233 if (c & MOUSE_PS2VERSA_IN_USE) {
2234 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
2235 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
2240 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2243 if (x < 0) /* XXX */
2252 sc->flags |= PSM_FLAGS_FINGERDOWN;
2257 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2259 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2260 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2263 case MOUSE_MODEL_4D:
2265 * b7 b6 b5 b4 b3 b2 b1 b0
2266 * byte 1: s2 d2 s1 d1 1 M R L
2267 * byte 2: sx x x x x x x x
2268 * byte 3: sy y y y y y y y
2270 * s1: wheel 1 direction
2272 * s2: wheel 2 direction
2275 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2276 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2277 switch (c & MOUSE_4D_WHEELBITS) {
2284 case 0x40: /* 2nd wheel turning right XXX */
2287 case 0xc0: /* 2nd wheel turning left XXX */
2293 case MOUSE_MODEL_4DPLUS:
2294 if ((x < 16 - 256) && (y < 16 - 256)) {
2296 * b7 b6 b5 b4 b3 b2 b1 b0
2297 * byte 1: 0 0 1 1 1 M R L
2298 * byte 2: 0 0 0 0 1 0 0 0
2299 * byte 3: 0 0 0 0 S s d1 d0
2301 * L, M, R, S: left, middle, right and side buttons
2302 * s: wheel data sign bit
2306 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2307 ms.button |= MOUSE_BUTTON4DOWN;
2308 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2309 ? ((sc->ipacket[2] & 0x07) - 8)
2310 : (sc->ipacket[2] & 0x07) ;
2312 /* preserve previous button states */
2313 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2317 case MOUSE_MODEL_GENERIC:
2323 if (sc->mode.accelfactor >= 1) {
2325 x = x * x / sc->mode.accelfactor;
2328 if (c & MOUSE_PS2_XNEG)
2332 y = y * y / sc->mode.accelfactor;
2335 if (c & MOUSE_PS2_YNEG)
2343 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2344 | (ms.obutton ^ ms.button);
2346 if (sc->mode.level < PSM_LEVEL_NATIVE)
2347 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
2349 sc->status.flags |= ms.flags;
2350 sc->status.dx += ms.dx;
2351 sc->status.dy += ms.dy;
2352 sc->status.dz += ms.dz;
2353 sc->status.button = ms.button;
2354 sc->button = ms.button;
2356 sc->watchdog = FALSE;
2359 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2360 l = imin(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2361 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2362 if (sc->inputbytes > l)
2363 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
2365 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
2366 sc->queue.count += sc->inputbytes;
2370 if (sc->state & PSM_ASLP) {
2371 sc->state &= ~PSM_ASLP;
2374 selwakeup(&sc->rsel);
2379 psmpoll(dev_t dev, int events, struct thread *td)
2381 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2385 /* Return true if a mouse event available */
2387 if (events & (POLLIN | POLLRDNORM)) {
2388 if (sc->queue.count > 0)
2389 revents |= events & (POLLIN | POLLRDNORM);
2391 selrecord(td, &sc->rsel);
2398 /* vendor/model specific routines */
2400 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2402 if (set_mouse_resolution(kbdc, res) != res)
2404 if (set_mouse_scaling(kbdc, scale)
2405 && set_mouse_scaling(kbdc, scale)
2406 && set_mouse_scaling(kbdc, scale)
2407 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2413 mouse_ext_command(KBDC kbdc, int command)
2417 c = (command >> 6) & 0x03;
2418 if (set_mouse_resolution(kbdc, c) != c)
2420 c = (command >> 4) & 0x03;
2421 if (set_mouse_resolution(kbdc, c) != c)
2423 c = (command >> 2) & 0x03;
2424 if (set_mouse_resolution(kbdc, c) != c)
2426 c = (command >> 0) & 0x03;
2427 if (set_mouse_resolution(kbdc, c) != c)
2433 /* Logitech MouseMan Cordless II */
2435 enable_lcordless(struct psm_softc *sc)
2440 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2442 if (status[1] == PSMD_RES_HIGH)
2444 ch = (status[0] & 0x07) - 1; /* channel # */
2445 if ((ch <= 0) || (ch > 4))
2448 * status[1]: always one?
2449 * status[2]: battery status? (0-100)
2455 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2457 enable_groller(struct psm_softc *sc)
2462 * The special sequence to enable the fourth button and the
2463 * roller. Immediately after this sequence check status bytes.
2464 * if the mouse is NetScroll, the second and the third bytes are
2469 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2470 * look like the following.
2472 * byte 1 bit 7 always 0
2473 * bit 6 stream mode (0)
2474 * bit 5 disabled (0)
2475 * bit 4 1:1 scaling (0)
2477 * bit 0-2 button status
2478 * byte 2 resolution (PSMD_RES_HIGH)
2479 * byte 3 report rate (?)
2482 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2484 if ((status[1] != '3') || (status[2] != 'D'))
2486 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2491 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2493 enable_gmouse(struct psm_softc *sc)
2498 * The special sequence to enable the middle, "rubber" button.
2499 * Immediately after this sequence check status bytes.
2500 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2501 * the second and the third bytes are '3' and 'U'.
2502 * NOTE: NetMouse reports that it has three buttons although it has
2503 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2504 * say they have three buttons too and they do have a button on the
2507 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2509 if ((status[1] != '3') || (status[2] != 'U'))
2514 /* ALPS GlidePoint */
2516 enable_aglide(struct psm_softc *sc)
2521 * The special sequence to obtain ALPS GlidePoint specific
2522 * information. Immediately after this sequence, status bytes will
2523 * contain something interesting.
2524 * NOTE: ALPS produces several models of GlidePoint. Some of those
2525 * do not respond to this sequence, thus, cannot be detected this way.
2527 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2529 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2531 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2536 /* Kensington ThinkingMouse/Trackball */
2538 enable_kmouse(struct psm_softc *sc)
2540 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2541 KBDC kbdc = sc->kbdc;
2547 id1 = get_aux_id(kbdc);
2548 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2551 * The device is now in the native mode? It returns a different
2554 id2 = get_aux_id(kbdc);
2555 if ((id1 == id2) || (id2 != 2))
2558 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2561 /* at this point, resolution is LOW, sampling rate is 10/sec */
2562 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2567 * The special sequence to enable the third and fourth buttons.
2568 * Otherwise they behave like the first and second buttons.
2570 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2571 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2576 * At this point, the device is using default resolution and
2577 * sampling rate for the native mode.
2579 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2581 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2584 /* the device appears be enabled by this sequence, diable it for now */
2585 disable_aux_dev(kbdc);
2586 empty_aux_buffer(kbdc, 5);
2591 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2593 enable_mmanplus(struct psm_softc *sc)
2595 KBDC kbdc = sc->kbdc;
2598 /* the special sequence to enable the fourth button and the roller. */
2600 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2601 * must be called exactly three times since the last RESET command
2602 * before this sequence. XXX
2604 if (!set_mouse_scaling(kbdc, 1))
2606 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2608 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2612 * PS2++ protocl, packet type 0
2614 * b7 b6 b5 b4 b3 b2 b1 b0
2615 * byte 1: * 1 p3 p2 1 * * *
2616 * byte 2: 1 1 p1 p0 m1 m0 1 0
2617 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2619 * p3-p0: packet type: 0
2620 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2622 /* check constant bits */
2623 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2625 if ((data[1] & 0xc3) != 0xc2)
2627 /* check d3-d0 in byte 2 */
2628 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2631 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2634 sc->hw.hwid &= 0x00ff;
2635 sc->hw.hwid |= data[2] << 8; /* save model ID */
2638 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2639 * the wheel and the fourth button events are encoded in the
2640 * special data packet. The mouse may be put in the IntelliMouse mode
2641 * if it is initialized by the IntelliMouse's method.
2646 /* MS IntelliMouse Explorer */
2648 enable_msexplorer(struct psm_softc *sc)
2650 static unsigned char rate0[] = { 200, 100, 80, };
2651 static unsigned char rate1[] = { 200, 200, 80, };
2652 KBDC kbdc = sc->kbdc;
2656 /* the special sequence to enable the extra buttons and the roller. */
2657 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2658 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2661 /* the device will give the genuine ID only after the above sequence */
2662 id = get_aux_id(kbdc);
2663 if (id != PSM_EXPLORER_ID)
2667 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2670 * XXX: this is a kludge to fool some KVM switch products
2671 * which think they are clever enough to know the 4-byte IntelliMouse
2672 * protocol, and assume any other protocols use 3-byte packets.
2673 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2674 * correctly to the host computer because of this!
2675 * The following sequence is actually IntelliMouse's "wake up"
2676 * sequence; it will make the KVM think the mouse is IntelliMouse
2677 * when it is in fact IntelliMouse Explorer.
2679 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2680 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2683 id = get_aux_id(kbdc);
2688 /* MS IntelliMouse */
2690 enable_msintelli(struct psm_softc *sc)
2693 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2694 * probe routine and act like IntelliMouse.
2697 static unsigned char rate[] = { 200, 100, 80, };
2698 KBDC kbdc = sc->kbdc;
2702 /* the special sequence to enable the third button and the roller. */
2703 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2704 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2707 /* the device will give the genuine ID only after the above sequence */
2708 id = get_aux_id(kbdc);
2709 if (id != PSM_INTELLI_ID)
2718 /* A4 Tech 4D Mouse */
2720 enable_4dmouse(struct psm_softc *sc)
2723 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2726 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2727 KBDC kbdc = sc->kbdc;
2731 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2732 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2735 id = get_aux_id(kbdc);
2737 * WinEasy 4D, 4 Way Scroll 4D: 6
2738 * Cable-Free 4D: 8 (4DPLUS)
2739 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2741 if (id != PSM_4DMOUSE_ID)
2745 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2750 /* A4 Tech 4D+ Mouse */
2752 enable_4dplus(struct psm_softc *sc)
2755 * Newer wheel mice from A4 Tech seem to use this protocol.
2756 * Older models are recognized as either 4D Mouse or IntelliMouse.
2758 KBDC kbdc = sc->kbdc;
2762 * enable_4dmouse() already issued the following ID sequence...
2763 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2766 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2767 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2772 id = get_aux_id(kbdc);
2777 case PSM_4DPLUS_RFSW35_ID:
2789 /* Interlink electronics VersaPad */
2791 enable_versapad(struct psm_softc *sc)
2793 KBDC kbdc = sc->kbdc;
2796 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2797 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2798 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
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 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2804 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2806 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2808 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2810 return TRUE; /* PS/2 absolute mode */
2814 psmresume(device_t dev)
2816 struct psm_softc *sc = device_get_softc(dev);
2817 int unit = device_get_unit(dev);
2821 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2823 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2826 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2828 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2830 * Release the blocked process; it must be notified that the device
2831 * cannot be accessed anymore.
2833 sc->state &= ~PSM_ASLP;
2838 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2843 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
2846 * This sucks up assignments from PNPBIOS and ACPI.
2850 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
2851 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
2852 * can be probed and attached only after the AT keyboard controller is
2853 * attached, we shall quietly reserve the IRQ resource for later use.
2854 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
2855 * copy the IRQ resource to the PS/2 mouse device instance hanging
2856 * under the keyboard controller, then probe and attach it.
2859 static devclass_t psmcpnp_devclass;
2861 static device_probe_t psmcpnp_probe;
2862 static device_attach_t psmcpnp_attach;
2864 static device_method_t psmcpnp_methods[] = {
2865 DEVMETHOD(device_probe, psmcpnp_probe),
2866 DEVMETHOD(device_attach, psmcpnp_attach),
2871 static driver_t psmcpnp_driver = {
2872 PSMCPNP_DRIVER_NAME,
2877 static struct isa_pnp_id psmcpnp_ids[] = {
2878 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
2879 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
2880 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
2881 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
2882 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
2883 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
2884 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
2885 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
2886 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
2887 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
2892 create_a_copy(device_t atkbdc, device_t me)
2897 /* find the PS/2 mouse device instance under the keyboard controller */
2898 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
2899 device_get_unit(atkbdc));
2902 if (device_get_state(psm) != DS_NOTPRESENT)
2905 /* move our resource to the found device */
2906 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
2907 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
2909 /* ...then probe and attach it */
2910 return device_probe_and_attach(psm);
2914 psmcpnp_probe(device_t dev)
2916 struct resource *res;
2920 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
2924 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
2925 * to the PS/2 mouse device node. But, some buggy PnP BIOS
2926 * declares the PS/2 mouse device node without an IRQ resource!
2927 * If this happens, we shall refer to device hints.
2928 * If we still don't find it there, use a hardcoded value... XXX
2931 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
2933 if (resource_long_value(PSM_DRIVER_NAME,
2934 device_get_unit(dev), "irq", &irq) != 0)
2936 device_printf(dev, "irq resource info is missing; "
2937 "assuming irq %ld\n", irq);
2938 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
2940 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
2942 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
2948 return ((res == NULL) ? ENXIO : 0);
2952 psmcpnp_attach(device_t dev)
2957 /* find the keyboard controller, which may be on acpi* or isa* bus */
2958 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
2959 device_get_unit(dev));
2960 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
2961 create_a_copy(atkbdc, dev);
2964 * If we don't have the AT keyboard controller yet,
2965 * just reserve the IRQ for later use...
2966 * (See psmidentify() above.)
2969 bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
2976 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
2977 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);