2 * Copyright (c) 1992, 1993 Erik Forsberg.
3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
15 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 * $FreeBSD: src/sys/isa/psm.c,v 1.23.2.6 2002/03/27 16:53:35 pb Exp $
24 * $DragonFly: src/sys/dev/misc/psm/psm.c,v 1.3 2003/06/23 17:55:40 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 <isa/isavar.h>
88 #include <dev/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) << 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 */
179 devclass_t psm_devclass;
180 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
182 /* driver state flags (state) */
183 #define PSM_VALID 0x80
184 #define PSM_OPEN 1 /* Device is open */
185 #define PSM_ASLP 2 /* Waiting for mouse data */
187 /* driver configuration flags (config) */
188 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
189 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
190 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
191 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
192 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
193 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
194 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
195 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
196 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
197 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
199 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \
201 | PSM_CONFIG_NOCHECKSYNC \
202 | PSM_CONFIG_SYNCHACK \
203 | PSM_CONFIG_NOIDPROBE \
204 | PSM_CONFIG_NORESET \
205 | PSM_CONFIG_FORCETAP \
206 | PSM_CONFIG_IGNPORTERROR \
207 | PSM_CONFIG_HOOKRESUME \
208 | PSM_CONFIG_INITAFTERSUSPEND)
210 /* other flags (flags) */
211 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
213 /* for backward compatibility */
214 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
215 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
216 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
218 typedef struct old_mousehw {
225 typedef struct old_mousemode {
232 /* packet formatting function */
233 typedef int packetfunc_t __P((struct psm_softc *, unsigned char *,
234 int *, int, mousestatus_t *));
236 /* function prototypes */
237 static int psmprobe __P((device_t));
238 static int psmattach __P((device_t));
239 static int psmdetach __P((device_t));
240 static int psmresume __P((device_t));
242 static d_open_t psmopen;
243 static d_close_t psmclose;
244 static d_read_t psmread;
245 static d_ioctl_t psmioctl;
246 static d_poll_t psmpoll;
248 static int enable_aux_dev __P((KBDC));
249 static int disable_aux_dev __P((KBDC));
250 static int get_mouse_status __P((KBDC, int *, int, int));
251 static int get_aux_id __P((KBDC));
252 static int set_mouse_sampling_rate __P((KBDC, int));
253 static int set_mouse_scaling __P((KBDC, int));
254 static int set_mouse_resolution __P((KBDC, int));
255 static int set_mouse_mode __P((KBDC));
256 static int get_mouse_buttons __P((KBDC));
257 static int is_a_mouse __P((int));
258 static void recover_from_error __P((KBDC));
259 static int restore_controller __P((KBDC, int));
260 static int doinitialize __P((struct psm_softc *, mousemode_t *));
261 static int doopen __P((struct psm_softc *, int));
262 static int reinitialize __P((struct psm_softc *, int));
263 static char *model_name __P((int));
264 static void psmintr __P((void *));
265 static void psmtimeout __P((void *));
267 /* vendor specific features */
268 typedef int probefunc_t __P((struct psm_softc *));
270 static int mouse_id_proc1 __P((KBDC, int, int, int *));
271 static int mouse_ext_command __P((KBDC, int));
272 static probefunc_t enable_groller;
273 static probefunc_t enable_gmouse;
274 static probefunc_t enable_aglide;
275 static probefunc_t enable_kmouse;
276 static probefunc_t enable_msexplorer;
277 static probefunc_t enable_msintelli;
278 static probefunc_t enable_4dmouse;
279 static probefunc_t enable_4dplus;
280 static probefunc_t enable_mmanplus;
281 static probefunc_t enable_versapad;
282 static int tame_mouse __P((struct psm_softc *, mousestatus_t *, unsigned char *));
286 unsigned char syncmask;
288 probefunc_t *probefunc;
291 * WARNING: the order of probe is very important. Don't mess it
292 * unless you know what you are doing.
294 { MOUSE_MODEL_NET, /* Genius NetMouse */
295 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
296 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
297 0xc8, 6, enable_groller, },
298 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
299 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
300 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
301 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
302 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
303 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
304 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
305 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
306 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
307 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
308 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
309 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
310 { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */
311 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
312 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
313 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
314 { MOUSE_MODEL_GENERIC,
315 0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
317 #define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
319 /* device driver declarateion */
320 static device_method_t psm_methods[] = {
321 /* Device interface */
322 DEVMETHOD(device_probe, psmprobe),
323 DEVMETHOD(device_attach, psmattach),
324 DEVMETHOD(device_detach, psmdetach),
325 DEVMETHOD(device_resume, psmresume),
330 static driver_t psm_driver = {
333 sizeof(struct psm_softc),
337 static struct isa_pnp_id psm_ids[] = {
338 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
339 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
344 #define CDEV_MAJOR 21
346 static struct cdevsw psm_cdevsw = {
348 /* close */ psmclose,
351 /* ioctl */ psmioctl,
354 /* strategy */ nostrategy,
355 /* name */ PSM_DRIVER_NAME,
356 /* maj */ CDEV_MAJOR,
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 if (!reset_aux_dev(sc->kbdc)) {
1036 recover_from_error(sc->kbdc);
1037 restore_controller(sc->kbdc, command_byte);
1039 printf("psm%d: failed to reset the aux device.\n", unit);
1045 * both the aux port and the aux device is functioning, see if the
1046 * device can be enabled. NOTE: when enabled, the device will start
1047 * sending data; we shall immediately disable the device once we know
1048 * the device can be enabled.
1050 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1052 recover_from_error(sc->kbdc);
1053 restore_controller(sc->kbdc, command_byte);
1055 printf("psm%d: failed to enable the aux device.\n", unit);
1059 /* save the default values after reset */
1060 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1061 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1062 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1064 sc->dflt_mode.rate = sc->mode.rate = -1;
1065 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1068 /* hardware information */
1069 sc->hw.iftype = MOUSE_IF_PS2;
1071 /* verify the device is a mouse */
1072 sc->hw.hwid = get_aux_id(sc->kbdc);
1073 if (!is_a_mouse(sc->hw.hwid)) {
1074 restore_controller(sc->kbdc, command_byte);
1076 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1079 switch (sc->hw.hwid) {
1080 case PSM_BALLPOINT_ID:
1081 sc->hw.type = MOUSE_TRACKBALL;
1084 case PSM_INTELLI_ID:
1085 case PSM_EXPLORER_ID:
1086 case PSM_4DMOUSE_ID:
1088 sc->hw.type = MOUSE_MOUSE;
1091 sc->hw.type = MOUSE_UNKNOWN;
1095 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1097 i = GENERIC_MOUSE_ENTRY;
1100 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1102 /* other parameters */
1103 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1104 if ((*vendortype[i].probefunc)(sc)) {
1106 printf("psm%d: found %s\n",
1107 unit, model_name(vendortype[i].model));
1113 sc->hw.model = vendortype[i].model;
1115 sc->dflt_mode.level = PSM_LEVEL_BASE;
1116 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1117 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1118 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1119 sc->dflt_mode.syncmask[0] = 0;
1121 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1122 if (sc->config & PSM_CONFIG_FORCETAP)
1123 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1124 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1125 sc->mode = sc->dflt_mode;
1126 sc->mode.packetsize = vendortype[i].packetsize;
1128 /* set mouse parameters */
1131 * A version of Logitech FirstMouse+ won't report wheel movement,
1132 * if SET_DEFAULTS is sent... Don't use this command.
1133 * This fix was found by Takashi Nishida.
1135 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1137 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1139 if (sc->config & PSM_CONFIG_RESOLUTION) {
1141 = set_mouse_resolution(sc->kbdc,
1142 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1143 } else if (sc->mode.resolution >= 0) {
1145 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1147 if (sc->mode.rate > 0) {
1148 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1150 set_mouse_scaling(sc->kbdc, 1);
1152 /* request a data packet and extract sync. bits */
1153 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1154 printf("psm%d: failed to get data.\n", unit);
1155 sc->mode.syncmask[0] = 0;
1157 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1158 /* the NetScroll Mouse will send three more bytes... Ignore them */
1159 empty_aux_buffer(sc->kbdc, 5);
1162 /* just check the status of the mouse */
1164 * NOTE: XXX there are some arcane controller/mouse combinations out
1165 * there, which hung the controller unless there is data transmission
1166 * after ACK from the mouse.
1168 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1169 printf("psm%d: failed to get status.\n", unit);
1172 * When in its native mode, some mice operate with different
1173 * default parameters than in the PS/2 compatible mode.
1175 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1176 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1179 /* disable the aux port for now... */
1180 if (!set_controller_command_byte(sc->kbdc,
1181 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1182 (command_byte & KBD_KBD_CONTROL_BITS)
1183 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1185 * this is CONTROLLER ERROR; I don't know the proper way to
1186 * recover from this error...
1188 restore_controller(sc->kbdc, command_byte);
1189 printf("psm%d: unable to set the command byte.\n", unit);
1193 /* see if IRQ is available */
1195 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1197 if (sc->intr == NULL) {
1198 printf("psm%d: unable to allocate the IRQ resource (%d).\n",
1202 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1206 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1207 kbdc_lock(sc->kbdc, FALSE);
1212 psmattach(device_t dev)
1214 int unit = device_get_unit(dev);
1215 struct psm_softc *sc = device_get_softc(dev);
1220 if (sc == NULL) /* shouldn't happen */
1223 /* Setup initial state */
1224 sc->state = PSM_VALID;
1225 callout_handle_init(&sc->callout);
1227 /* Setup our interrupt handler */
1229 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1230 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1232 if (sc->intr == NULL)
1234 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
1235 INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1237 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1242 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1244 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1248 printf("psm%d: model %s, device ID %d\n",
1249 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1251 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1252 unit, model_name(sc->hw.model),
1253 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1254 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1255 unit, sc->config, sc->flags, sc->mode.packetsize);
1256 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1257 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1267 psmdetach(device_t dev)
1269 struct psm_softc *sc;
1272 sc = device_get_softc(dev);
1273 if (sc->state & PSM_OPEN)
1277 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih);
1278 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1280 destroy_dev(sc->dev);
1281 destroy_dev(sc->bdev);
1287 psmopen(dev_t dev, int flag, int fmt, struct thread *td)
1289 int unit = PSM_UNIT(dev);
1290 struct psm_softc *sc;
1295 /* Get device data */
1296 sc = PSM_SOFTC(unit);
1297 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1298 /* the device is no longer valid/functioning */
1301 /* Disallow multiple opens */
1302 if (sc->state & PSM_OPEN)
1305 device_busy(devclass_get_device(psm_devclass, unit));
1307 /* Initialize state */
1308 sc->rsel.si_flags = 0;
1309 sc->rsel.si_pid = 0;
1310 sc->mode.level = sc->dflt_mode.level;
1311 sc->mode.protocol = sc->dflt_mode.protocol;
1312 sc->watchdog = FALSE;
1314 /* flush the event queue */
1315 sc->queue.count = 0;
1318 sc->status.flags = 0;
1319 sc->status.button = 0;
1320 sc->status.obutton = 0;
1326 /* empty input buffer */
1327 bzero(sc->ipacket, sizeof(sc->ipacket));
1331 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1332 if (!kbdc_lock(sc->kbdc, TRUE))
1335 /* save the current controller command byte */
1337 command_byte = get_controller_command_byte(sc->kbdc);
1339 /* enable the aux port and temporalily disable the keyboard */
1340 if ((command_byte == -1)
1341 || !set_controller_command_byte(sc->kbdc,
1342 kbdc_get_device_mask(sc->kbdc),
1343 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1344 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1345 /* CONTROLLER ERROR; do you know how to get out of this? */
1346 kbdc_lock(sc->kbdc, FALSE);
1348 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1353 * Now that the keyboard controller is told not to generate
1354 * the keyboard and mouse interrupts, call `splx()' to allow
1355 * the other tty interrupts. The clock interrupt may also occur,
1356 * but timeout routines will be blocked by the poll flag set
1361 /* enable the mouse device */
1362 err = doopen(sc, command_byte);
1366 sc->state |= PSM_OPEN;
1367 kbdc_lock(sc->kbdc, FALSE);
1372 psmclose(dev_t dev, int flag, int fmt, struct thread *td)
1374 int unit = PSM_UNIT(dev);
1375 struct psm_softc *sc = PSM_SOFTC(unit);
1380 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1381 if (!kbdc_lock(sc->kbdc, TRUE))
1384 /* save the current controller command byte */
1386 command_byte = get_controller_command_byte(sc->kbdc);
1387 if (command_byte == -1) {
1388 kbdc_lock(sc->kbdc, FALSE);
1393 /* disable the aux interrupt and temporalily disable the keyboard */
1394 if (!set_controller_command_byte(sc->kbdc,
1395 kbdc_get_device_mask(sc->kbdc),
1396 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1397 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1398 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1400 /* CONTROLLER ERROR;
1401 * NOTE: we shall force our way through. Because the only
1402 * ill effect we shall see is that we may not be able
1403 * to read ACK from the mouse, and it doesn't matter much
1404 * so long as the mouse will accept the DISABLE command.
1409 /* stop the watchdog timer */
1410 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1411 callout_handle_init(&sc->callout);
1413 /* remove anything left in the output buffer */
1414 empty_aux_buffer(sc->kbdc, 10);
1416 /* disable the aux device, port and interrupt */
1417 if (sc->state & PSM_VALID) {
1418 if (!disable_aux_dev(sc->kbdc)) {
1420 * NOTE: we don't return error and continue, pretending
1421 * we have successfully disabled the device. It's OK because
1422 * the interrupt routine will discard any data from the mouse
1425 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1429 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1430 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1434 if (!set_controller_command_byte(sc->kbdc,
1435 kbdc_get_device_mask(sc->kbdc),
1436 (command_byte & KBD_KBD_CONTROL_BITS)
1437 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1438 /* CONTROLLER ERROR;
1439 * we shall ignore this error; see the above comment.
1441 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1445 /* remove anything left in the output buffer */
1446 empty_aux_buffer(sc->kbdc, 10);
1448 /* close is almost always successful */
1449 sc->state &= ~PSM_OPEN;
1450 kbdc_lock(sc->kbdc, FALSE);
1451 device_unbusy(devclass_get_device(psm_devclass, unit));
1456 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
1458 static unsigned char butmapps2[8] = {
1460 MOUSE_PS2_BUTTON1DOWN,
1461 MOUSE_PS2_BUTTON2DOWN,
1462 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1463 MOUSE_PS2_BUTTON3DOWN,
1464 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1465 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1466 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1468 static unsigned char butmapmsc[8] = {
1469 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1470 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1471 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1472 MOUSE_MSC_BUTTON3UP,
1473 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1474 MOUSE_MSC_BUTTON2UP,
1475 MOUSE_MSC_BUTTON1UP,
1481 if (sc->mode.level == PSM_LEVEL_BASE) {
1482 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1483 if (status->button & MOUSE_BUTTON4DOWN)
1484 mapped |= MOUSE_BUTTON1DOWN;
1485 status->button = mapped;
1486 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1487 i = max(min(status->dx, 255), -256);
1489 buf[0] |= MOUSE_PS2_XNEG;
1491 i = max(min(status->dy, 255), -256);
1493 buf[0] |= MOUSE_PS2_YNEG;
1495 return MOUSE_PS2_PACKETSIZE;
1496 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1497 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1498 i = max(min(status->dx, 255), -256);
1500 buf[3] = i - buf[1];
1501 i = max(min(status->dy, 255), -256);
1503 buf[4] = i - buf[2];
1504 i = max(min(status->dz, 127), -128);
1505 buf[5] = (i >> 1) & 0x7f;
1506 buf[6] = (i - (i >> 1)) & 0x7f;
1507 buf[7] = (~status->button >> 3) & 0x7f;
1508 return MOUSE_SYS_PACKETSIZE;
1510 return sc->inputbytes;;
1514 psmread(dev_t dev, struct uio *uio, int flag)
1516 register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1517 unsigned char buf[PSM_SMALLBUFSIZE];
1522 if ((sc->state & PSM_VALID) == 0)
1525 /* block until mouse activity occured */
1527 while (sc->queue.count <= 0) {
1528 if (PSM_NBLOCKIO(dev)) {
1532 sc->state |= PSM_ASLP;
1533 error = tsleep((caddr_t) sc, PZERO | PCATCH, "psmrea", 0);
1534 sc->state &= ~PSM_ASLP;
1538 } else if ((sc->state & PSM_VALID) == 0) {
1539 /* the device disappeared! */
1546 /* copy data to the user land */
1547 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1549 l = min(sc->queue.count, uio->uio_resid);
1550 if (l > sizeof(buf))
1552 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1553 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1554 sizeof(sc->queue.buf) - sc->queue.head);
1555 bcopy(&sc->queue.buf[0],
1556 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1557 l - (sizeof(sc->queue.buf) - sc->queue.head));
1559 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1561 sc->queue.count -= l;
1562 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1564 error = uiomove(buf, l, uio);
1573 block_mouse_data(struct psm_softc *sc, int *c)
1577 if (!kbdc_lock(sc->kbdc, TRUE))
1581 *c = get_controller_command_byte(sc->kbdc);
1583 || !set_controller_command_byte(sc->kbdc,
1584 kbdc_get_device_mask(sc->kbdc),
1585 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1586 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1587 /* this is CONTROLLER ERROR */
1589 kbdc_lock(sc->kbdc, FALSE);
1594 * The device may be in the middle of status data transmission.
1595 * The transmission will be interrupted, thus, incomplete status
1596 * data must be discarded. Although the aux interrupt is disabled
1597 * at the keyboard controller level, at most one aux interrupt
1598 * may have already been pending and a data byte is in the
1599 * output buffer; throw it away. Note that the second argument
1600 * to `empty_aux_buffer()' is zero, so that the call will just
1601 * flush the internal queue.
1602 * `psmintr()' will be invoked after `splx()' if an interrupt is
1603 * pending; it will see no data and returns immediately.
1605 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1606 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1614 unblock_mouse_data(struct psm_softc *sc, int c)
1619 * We may have seen a part of status data during `set_mouse_XXX()'.
1620 * they have been queued; flush it.
1622 empty_aux_buffer(sc->kbdc, 0);
1624 /* restore ports and interrupt */
1625 if (!set_controller_command_byte(sc->kbdc,
1626 kbdc_get_device_mask(sc->kbdc),
1627 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1628 /* CONTROLLER ERROR; this is serious, we may have
1629 * been left with the inaccessible keyboard and
1630 * the disabled mouse interrupt.
1635 kbdc_lock(sc->kbdc, FALSE);
1640 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1642 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1644 mousestatus_t status;
1645 #if (defined(MOUSE_GETVARS))
1654 /* Perform IOCTL command */
1657 case OLD_MOUSE_GETHWINFO:
1659 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1660 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1661 ((old_mousehw_t *)addr)->type = sc->hw.type;
1662 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1666 case MOUSE_GETHWINFO:
1668 *(mousehw_t *)addr = sc->hw;
1669 if (sc->mode.level == PSM_LEVEL_BASE)
1670 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1674 case OLD_MOUSE_GETMODE:
1676 switch (sc->mode.level) {
1677 case PSM_LEVEL_BASE:
1678 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1680 case PSM_LEVEL_STANDARD:
1681 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1683 case PSM_LEVEL_NATIVE:
1684 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1687 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1688 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1689 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1695 *(mousemode_t *)addr = sc->mode;
1696 ((mousemode_t *)addr)->resolution =
1697 MOUSE_RES_LOW - sc->mode.resolution;
1698 switch (sc->mode.level) {
1699 case PSM_LEVEL_BASE:
1700 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1701 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1703 case PSM_LEVEL_STANDARD:
1704 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1705 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1706 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1707 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1709 case PSM_LEVEL_NATIVE:
1710 /* FIXME: this isn't quite correct... XXX */
1711 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1717 case OLD_MOUSE_SETMODE:
1719 if (cmd == OLD_MOUSE_SETMODE) {
1720 mode.rate = ((old_mousemode_t *)addr)->rate;
1722 * resolution old I/F new I/F
1729 if (((old_mousemode_t *)addr)->resolution > 0)
1730 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1731 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1734 mode = *(mousemode_t *)addr;
1737 /* adjust and validate parameters. */
1738 if (mode.rate > UCHAR_MAX)
1741 mode.rate = sc->dflt_mode.rate;
1742 else if (mode.rate == -1)
1743 /* don't change the current setting */
1745 else if (mode.rate < 0)
1747 if (mode.resolution >= UCHAR_MAX)
1749 if (mode.resolution >= 200)
1750 mode.resolution = MOUSE_RES_HIGH;
1751 else if (mode.resolution >= 100)
1752 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1753 else if (mode.resolution >= 50)
1754 mode.resolution = MOUSE_RES_MEDIUMLOW;
1755 else if (mode.resolution > 0)
1756 mode.resolution = MOUSE_RES_LOW;
1757 if (mode.resolution == MOUSE_RES_DEFAULT)
1758 mode.resolution = sc->dflt_mode.resolution;
1759 else if (mode.resolution == -1)
1760 /* don't change the current setting */
1762 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1763 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1764 if (mode.level == -1)
1765 /* don't change the current setting */
1766 mode.level = sc->mode.level;
1767 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1769 if (mode.accelfactor == -1)
1770 /* don't change the current setting */
1771 mode.accelfactor = sc->mode.accelfactor;
1772 else if (mode.accelfactor < 0)
1775 /* don't allow anybody to poll the keyboard controller */
1776 error = block_mouse_data(sc, &command_byte);
1780 /* set mouse parameters */
1782 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1783 if (mode.resolution >= 0)
1784 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1785 set_mouse_scaling(sc->kbdc, 1);
1786 get_mouse_status(sc->kbdc, stat, 0, 3);
1789 sc->mode.rate = mode.rate;
1790 sc->mode.resolution = mode.resolution;
1791 sc->mode.accelfactor = mode.accelfactor;
1792 sc->mode.level = mode.level;
1795 unblock_mouse_data(sc, command_byte);
1798 case MOUSE_GETLEVEL:
1799 *(int *)addr = sc->mode.level;
1802 case MOUSE_SETLEVEL:
1803 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1805 sc->mode.level = *(int *)addr;
1808 case MOUSE_GETSTATUS:
1810 status = sc->status;
1811 sc->status.flags = 0;
1812 sc->status.obutton = sc->status.button;
1813 sc->status.button = 0;
1818 *(mousestatus_t *)addr = status;
1821 #if (defined(MOUSE_GETVARS))
1823 var = (mousevar_t *)addr;
1824 bzero(var, sizeof(*var));
1826 var->var[0] = MOUSE_VARS_PS2_SIG;
1827 var->var[1] = sc->config;
1828 var->var[2] = sc->flags;
1834 #endif /* MOUSE_GETVARS */
1836 case MOUSE_READSTATE:
1837 case MOUSE_READDATA:
1838 data = (mousedata_t *)addr;
1839 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1842 error = block_mouse_data(sc, &command_byte);
1845 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1846 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1848 unblock_mouse_data(sc, command_byte);
1851 #if (defined(MOUSE_SETRESOLUTION))
1852 case MOUSE_SETRESOLUTION:
1853 mode.resolution = *(int *)addr;
1854 if (mode.resolution >= UCHAR_MAX)
1856 else if (mode.resolution >= 200)
1857 mode.resolution = MOUSE_RES_HIGH;
1858 else if (mode.resolution >= 100)
1859 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1860 else if (mode.resolution >= 50)
1861 mode.resolution = MOUSE_RES_MEDIUMLOW;
1862 else if (mode.resolution > 0)
1863 mode.resolution = MOUSE_RES_LOW;
1864 if (mode.resolution == MOUSE_RES_DEFAULT)
1865 mode.resolution = sc->dflt_mode.resolution;
1866 else if (mode.resolution == -1)
1867 mode.resolution = sc->mode.resolution;
1868 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1869 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1871 error = block_mouse_data(sc, &command_byte);
1874 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1875 if (sc->mode.resolution != mode.resolution)
1877 unblock_mouse_data(sc, command_byte);
1879 #endif /* MOUSE_SETRESOLUTION */
1881 #if (defined(MOUSE_SETRATE))
1883 mode.rate = *(int *)addr;
1884 if (mode.rate > UCHAR_MAX)
1887 mode.rate = sc->dflt_mode.rate;
1888 else if (mode.rate < 0)
1889 mode.rate = sc->mode.rate;
1891 error = block_mouse_data(sc, &command_byte);
1894 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1895 if (sc->mode.rate != mode.rate)
1897 unblock_mouse_data(sc, command_byte);
1899 #endif /* MOUSE_SETRATE */
1901 #if (defined(MOUSE_SETSCALING))
1902 case MOUSE_SETSCALING:
1903 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1906 error = block_mouse_data(sc, &command_byte);
1909 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1911 unblock_mouse_data(sc, command_byte);
1913 #endif /* MOUSE_SETSCALING */
1915 #if (defined(MOUSE_GETHWID))
1917 error = block_mouse_data(sc, &command_byte);
1920 sc->hw.hwid &= ~0x00ff;
1921 sc->hw.hwid |= get_aux_id(sc->kbdc);
1922 *(int *)addr = sc->hw.hwid & 0x00ff;
1923 unblock_mouse_data(sc, command_byte);
1925 #endif /* MOUSE_GETHWID */
1935 psmtimeout(void *arg)
1937 struct psm_softc *sc;
1940 sc = (struct psm_softc *)arg;
1942 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1944 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1946 kbdc_lock(sc->kbdc, FALSE);
1948 sc->watchdog = TRUE;
1950 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
1957 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
1958 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
1960 static int butmap[8] = {
1964 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1966 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1967 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1968 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1970 static int butmap_versapad[8] = {
1976 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1978 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
1980 register struct psm_softc *sc = arg;
1988 /* read until there is nothing to read */
1989 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
1991 /* discard the byte if the device is not open */
1992 if ((sc->state & PSM_OPEN) == 0)
1995 getmicrouptime(&tv);
1996 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
1997 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2001 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2002 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2003 timevaladd(&sc->inputtimeout, &tv);
2005 sc->ipacket[sc->inputbytes++] = c;
2006 if (sc->inputbytes < sc->mode.packetsize)
2010 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2011 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
2012 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
2017 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2018 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
2019 c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
2021 if (sc->syncerrors < sc->mode.packetsize) {
2022 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2024 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2025 } else if (sc->syncerrors == sc->mode.packetsize) {
2026 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2028 disable_aux_dev(sc->kbdc);
2029 enable_aux_dev(sc->kbdc);
2030 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
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 >= PSM_SYNCERR_THRESHOLD1) {
2035 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2036 reinitialize(sc, TRUE);
2042 * A kludge for Kensington device!
2043 * The MSB of the horizontal count appears to be stored in
2046 if (sc->hw.model == MOUSE_MODEL_THINK)
2047 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2049 /* ignore the overflow bits... */
2050 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2051 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2053 ms.obutton = sc->button; /* previous button state */
2054 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2055 /* `tapping' action */
2056 if (sc->config & PSM_CONFIG_FORCETAP)
2057 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2059 switch (sc->hw.model) {
2061 case MOUSE_MODEL_EXPLORER:
2063 * b7 b6 b5 b4 b3 b2 b1 b0
2064 * byte 1: oy ox sy sx 1 M R L
2065 * byte 2: x x x x x x x x
2066 * byte 3: y y y y y y y y
2067 * byte 4: * * S2 S1 s d2 d1 d0
2069 * L, M, R, S1, S2: left, middle, right and side buttons
2070 * s: wheel data sign bit
2073 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2074 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
2075 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2076 ? MOUSE_BUTTON4DOWN : 0;
2077 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2078 ? MOUSE_BUTTON5DOWN : 0;
2081 case MOUSE_MODEL_INTELLI:
2082 case MOUSE_MODEL_NET:
2083 /* wheel data is in the fourth byte */
2084 z = (char)sc->ipacket[3];
2085 /* some mice may send 7 when there is no Z movement?! XXX */
2086 if ((z >= 7) || (z <= -7))
2088 /* some compatible mice have additional buttons */
2089 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2090 ? MOUSE_BUTTON4DOWN : 0;
2091 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2092 ? MOUSE_BUTTON5DOWN : 0;
2095 case MOUSE_MODEL_MOUSEMANPLUS:
2097 * PS2++ protocl packet
2099 * b7 b6 b5 b4 b3 b2 b1 b0
2100 * byte 1: * 1 p3 p2 1 * * *
2101 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2103 * p3-p0: packet type
2104 * c1, c2: c1 & c2 == 1, if p2 == 0
2105 * c1 & c2 == 0, if p2 == 1
2107 * packet type: 0 (device type)
2108 * See comments in enable_mmanplus() below.
2110 * packet type: 1 (wheel data)
2112 * b7 b6 b5 b4 b3 b2 b1 b0
2113 * byte 3: h * B5 B4 s d2 d1 d0
2115 * h: 1, if horizontal roller data
2116 * 0, if vertical roller data
2117 * B4, B5: button 4 and 5
2119 * d2-d0: roller data
2121 * packet type: 2 (reserved)
2123 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2125 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
2126 /* the extended data packet encodes button and wheel events */
2127 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
2129 /* wheel data packet */
2131 if (sc->ipacket[2] & 0x80) {
2132 /* horizontal roller count - ignore it XXX*/
2134 /* vertical roller count */
2135 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2136 ? (sc->ipacket[2] & 0x0f) - 16
2137 : (sc->ipacket[2] & 0x0f);
2139 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2140 ? MOUSE_BUTTON4DOWN : 0;
2141 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2142 ? MOUSE_BUTTON5DOWN : 0;
2145 /* this packet type is reserved by Logitech... */
2147 * IBM ScrollPoint Mouse uses this packet type to
2148 * encode both vertical and horizontal scroll movement.
2151 /* horizontal count */
2152 if (sc->ipacket[2] & 0x0f)
2153 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2154 /* vertical count */
2155 if (sc->ipacket[2] & 0xf0)
2156 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2158 /* vertical count */
2159 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
2160 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16
2161 : ((sc->ipacket[2] >> 4) & 0x0f);
2162 /* horizontal count */
2163 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
2164 ? (sc->ipacket[2] & 0x0f) - 16
2165 : (sc->ipacket[2] & 0x0f);
2169 /* device type packet - shouldn't happen */
2173 ms.button = ms.obutton;
2175 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2176 "0x%02x 0x%02x 0x%02x\n",
2177 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
2178 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
2182 /* preserve button states */
2183 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2187 case MOUSE_MODEL_GLIDEPOINT:
2188 /* `tapping' action */
2189 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2192 case MOUSE_MODEL_NETSCROLL:
2193 /* three addtional bytes encode buttons and wheel events */
2194 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2195 ? MOUSE_BUTTON4DOWN : 0;
2196 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2197 ? MOUSE_BUTTON5DOWN : 0;
2198 z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
2199 ? sc->ipacket[4] - 256 : sc->ipacket[4];
2202 case MOUSE_MODEL_THINK:
2203 /* the fourth button state in the first byte */
2204 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2207 case MOUSE_MODEL_VERSAPAD:
2208 /* VersaPad PS/2 absolute mode message format
2210 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2211 * ipacket[0]: 1 1 0 A 1 L T R
2212 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2213 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2214 * ipacket[3]: 1 1 1 A 1 L T R
2215 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2216 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2219 * R: right physical mouse button (1=on)
2220 * T: touch pad virtual button (1=tapping)
2221 * L: left physical mouse button (1=on)
2222 * A: position data is valid (1=valid)
2223 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2224 * V: vertical data (12bit signed integer. V11 is sign bit.)
2227 * Tapping is mapped to MOUSE_BUTTON4.
2229 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2230 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2232 if (c & MOUSE_PS2VERSA_IN_USE) {
2233 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
2234 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
2239 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2242 if (x < 0) /* XXX */
2251 sc->flags |= PSM_FLAGS_FINGERDOWN;
2256 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2258 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2259 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2262 case MOUSE_MODEL_4D:
2264 * b7 b6 b5 b4 b3 b2 b1 b0
2265 * byte 1: s2 d2 s1 d1 1 M R L
2266 * byte 2: sx x x x x x x x
2267 * byte 3: sy y y y y y y y
2269 * s1: wheel 1 direction
2271 * s2: wheel 2 direction
2274 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2275 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2276 switch (c & MOUSE_4D_WHEELBITS) {
2283 case 0x40: /* 2nd wheel turning right XXX */
2286 case 0xc0: /* 2nd wheel turning left XXX */
2292 case MOUSE_MODEL_4DPLUS:
2293 if ((x < 16 - 256) && (y < 16 - 256)) {
2295 * b7 b6 b5 b4 b3 b2 b1 b0
2296 * byte 1: 0 0 1 1 1 M R L
2297 * byte 2: 0 0 0 0 1 0 0 0
2298 * byte 3: 0 0 0 0 S s d1 d0
2300 * L, M, R, S: left, middle, right and side buttons
2301 * s: wheel data sign bit
2305 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2306 ms.button |= MOUSE_BUTTON4DOWN;
2307 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2308 ? ((sc->ipacket[2] & 0x07) - 8)
2309 : (sc->ipacket[2] & 0x07) ;
2311 /* preserve previous button states */
2312 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2316 case MOUSE_MODEL_GENERIC:
2322 if (sc->mode.accelfactor >= 1) {
2324 x = x * x / sc->mode.accelfactor;
2327 if (c & MOUSE_PS2_XNEG)
2331 y = y * y / sc->mode.accelfactor;
2334 if (c & MOUSE_PS2_YNEG)
2342 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2343 | (ms.obutton ^ ms.button);
2345 if (sc->mode.level < PSM_LEVEL_NATIVE)
2346 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
2348 sc->status.flags |= ms.flags;
2349 sc->status.dx += ms.dx;
2350 sc->status.dy += ms.dy;
2351 sc->status.dz += ms.dz;
2352 sc->status.button = ms.button;
2353 sc->button = ms.button;
2355 sc->watchdog = FALSE;
2358 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2359 l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2360 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2361 if (sc->inputbytes > l)
2362 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
2364 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
2365 sc->queue.count += sc->inputbytes;
2369 if (sc->state & PSM_ASLP) {
2370 sc->state &= ~PSM_ASLP;
2371 wakeup((caddr_t) sc);
2373 selwakeup(&sc->rsel);
2378 psmpoll(dev_t dev, int events, struct thread *td)
2380 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2384 /* Return true if a mouse event available */
2386 if (events & (POLLIN | POLLRDNORM)) {
2387 if (sc->queue.count > 0)
2388 revents |= events & (POLLIN | POLLRDNORM);
2390 selrecord(td, &sc->rsel);
2397 /* vendor/model specific routines */
2399 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2401 if (set_mouse_resolution(kbdc, res) != res)
2403 if (set_mouse_scaling(kbdc, scale)
2404 && set_mouse_scaling(kbdc, scale)
2405 && set_mouse_scaling(kbdc, scale)
2406 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2412 mouse_ext_command(KBDC kbdc, int command)
2416 c = (command >> 6) & 0x03;
2417 if (set_mouse_resolution(kbdc, c) != c)
2419 c = (command >> 4) & 0x03;
2420 if (set_mouse_resolution(kbdc, c) != c)
2422 c = (command >> 2) & 0x03;
2423 if (set_mouse_resolution(kbdc, c) != c)
2425 c = (command >> 0) & 0x03;
2426 if (set_mouse_resolution(kbdc, c) != c)
2432 /* Logitech MouseMan Cordless II */
2434 enable_lcordless(struct psm_softc *sc)
2439 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2441 if (status[1] == PSMD_RES_HIGH)
2443 ch = (status[0] & 0x07) - 1; /* channel # */
2444 if ((ch <= 0) || (ch > 4))
2447 * status[1]: always one?
2448 * status[2]: battery status? (0-100)
2454 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2456 enable_groller(struct psm_softc *sc)
2461 * The special sequence to enable the fourth button and the
2462 * roller. Immediately after this sequence check status bytes.
2463 * if the mouse is NetScroll, the second and the third bytes are
2468 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2469 * look like the following.
2471 * byte 1 bit 7 always 0
2472 * bit 6 stream mode (0)
2473 * bit 5 disabled (0)
2474 * bit 4 1:1 scaling (0)
2476 * bit 0-2 button status
2477 * byte 2 resolution (PSMD_RES_HIGH)
2478 * byte 3 report rate (?)
2481 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2483 if ((status[1] != '3') || (status[2] != 'D'))
2485 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2490 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2492 enable_gmouse(struct psm_softc *sc)
2497 * The special sequence to enable the middle, "rubber" button.
2498 * Immediately after this sequence check status bytes.
2499 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2500 * the second and the third bytes are '3' and 'U'.
2501 * NOTE: NetMouse reports that it has three buttons although it has
2502 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2503 * say they have three buttons too and they do have a button on the
2506 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2508 if ((status[1] != '3') || (status[2] != 'U'))
2513 /* ALPS GlidePoint */
2515 enable_aglide(struct psm_softc *sc)
2520 * The special sequence to obtain ALPS GlidePoint specific
2521 * information. Immediately after this sequence, status bytes will
2522 * contain something interesting.
2523 * NOTE: ALPS produces several models of GlidePoint. Some of those
2524 * do not respond to this sequence, thus, cannot be detected this way.
2526 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2528 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2530 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2535 /* Kensington ThinkingMouse/Trackball */
2537 enable_kmouse(struct psm_softc *sc)
2539 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2540 KBDC kbdc = sc->kbdc;
2546 id1 = get_aux_id(kbdc);
2547 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2550 * The device is now in the native mode? It returns a different
2553 id2 = get_aux_id(kbdc);
2554 if ((id1 == id2) || (id2 != 2))
2557 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2560 /* at this point, resolution is LOW, sampling rate is 10/sec */
2561 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2566 * The special sequence to enable the third and fourth buttons.
2567 * Otherwise they behave like the first and second buttons.
2569 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2570 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2575 * At this point, the device is using default resolution and
2576 * sampling rate for the native mode.
2578 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2580 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2583 /* the device appears be enabled by this sequence, diable it for now */
2584 disable_aux_dev(kbdc);
2585 empty_aux_buffer(kbdc, 5);
2590 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2592 enable_mmanplus(struct psm_softc *sc)
2594 KBDC kbdc = sc->kbdc;
2597 /* the special sequence to enable the fourth button and the roller. */
2599 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2600 * must be called exactly three times since the last RESET command
2601 * before this sequence. XXX
2603 if (!set_mouse_scaling(kbdc, 1))
2605 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2607 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2611 * PS2++ protocl, packet type 0
2613 * b7 b6 b5 b4 b3 b2 b1 b0
2614 * byte 1: * 1 p3 p2 1 * * *
2615 * byte 2: 1 1 p1 p0 m1 m0 1 0
2616 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2618 * p3-p0: packet type: 0
2619 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2621 /* check constant bits */
2622 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2624 if ((data[1] & 0xc3) != 0xc2)
2626 /* check d3-d0 in byte 2 */
2627 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2630 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2633 sc->hw.hwid &= 0x00ff;
2634 sc->hw.hwid |= data[2] << 8; /* save model ID */
2637 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2638 * the wheel and the fourth button events are encoded in the
2639 * special data packet. The mouse may be put in the IntelliMouse mode
2640 * if it is initialized by the IntelliMouse's method.
2645 /* MS IntelliMouse Explorer */
2647 enable_msexplorer(struct psm_softc *sc)
2649 static unsigned char rate0[] = { 200, 100, 80, };
2650 static unsigned char rate1[] = { 200, 200, 80, };
2651 KBDC kbdc = sc->kbdc;
2655 /* the special sequence to enable the extra buttons and the roller. */
2656 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2657 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2660 /* the device will give the genuine ID only after the above sequence */
2661 id = get_aux_id(kbdc);
2662 if (id != PSM_EXPLORER_ID)
2666 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2669 * XXX: this is a kludge to fool some KVM switch products
2670 * which think they are clever enough to know the 4-byte IntelliMouse
2671 * protocol, and assume any other protocols use 3-byte packets.
2672 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2673 * correctly to the host computer because of this!
2674 * The following sequence is actually IntelliMouse's "wake up"
2675 * sequence; it will make the KVM think the mouse is IntelliMouse
2676 * when it is in fact IntelliMouse Explorer.
2678 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2679 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2682 id = get_aux_id(kbdc);
2687 /* MS IntelliMouse */
2689 enable_msintelli(struct psm_softc *sc)
2692 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2693 * probe routine and act like IntelliMouse.
2696 static unsigned char rate[] = { 200, 100, 80, };
2697 KBDC kbdc = sc->kbdc;
2701 /* the special sequence to enable the third button and the roller. */
2702 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2703 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2706 /* the device will give the genuine ID only after the above sequence */
2707 id = get_aux_id(kbdc);
2708 if (id != PSM_INTELLI_ID)
2717 /* A4 Tech 4D Mouse */
2719 enable_4dmouse(struct psm_softc *sc)
2722 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2725 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2726 KBDC kbdc = sc->kbdc;
2730 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2731 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2734 id = get_aux_id(kbdc);
2736 * WinEasy 4D, 4 Way Scroll 4D: 6
2737 * Cable-Free 4D: 8 (4DPLUS)
2738 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2740 if (id != PSM_4DMOUSE_ID)
2744 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2749 /* A4 Tech 4D+ Mouse */
2751 enable_4dplus(struct psm_softc *sc)
2754 * Newer wheel mice from A4 Tech seem to use this protocol.
2755 * Older models are recognized as either 4D Mouse or IntelliMouse.
2757 KBDC kbdc = sc->kbdc;
2761 * enable_4dmouse() already issued the following ID sequence...
2762 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2765 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2766 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2771 id = get_aux_id(kbdc);
2772 if (id != PSM_4DPLUS_ID)
2776 sc->hw.buttons = 4; /* XXX */
2781 /* Interlink electronics VersaPad */
2783 enable_versapad(struct psm_softc *sc)
2785 KBDC kbdc = sc->kbdc;
2788 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2789 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2790 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2791 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2792 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2793 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2794 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2796 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2798 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2800 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2802 return TRUE; /* PS/2 absolute mode */
2806 psmresume(device_t dev)
2808 struct psm_softc *sc = device_get_softc(dev);
2809 int unit = device_get_unit(dev);
2813 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2815 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2818 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2820 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2822 * Release the blocked process; it must be notified that the device
2823 * cannot be accessed anymore.
2825 sc->state &= ~PSM_ASLP;
2826 wakeup((caddr_t)sc);
2830 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2835 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);