2 * Copyright (c) 1992, 1993 Erik Forsberg.
3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
15 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 * $FreeBSD: src/sys/isa/psm.c,v 1.23.2.7 2003/11/12 04:26:26 mikeh Exp $
24 * $DragonFly: src/sys/dev/misc/psm/psm.c,v 1.9 2004/02/10 15:56:38 hmp Exp $
28 * Ported to 386bsd Oct 17, 1992
29 * Sandi Donno, Computer Science, University of Cape Town, South Africa
30 * Please send bug reports to sandi@cs.uct.ac.za
32 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
33 * although I was only partially successful in getting the alpha release
34 * of his "driver for the Logitech and ATI Inport Bus mice for use with
35 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
36 * found his code to be an invaluable reference when porting this driver
39 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
40 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
42 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
43 * Andrew Herbert - 12 June 1993
45 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
48 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
51 * Hardware access routines and probe logic rewritten by
52 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
53 * - 3, 14, 22 October 1996.
54 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
55 * - 14, 30 November 1996. Uses `kbdio.c'.
56 * - 13 December 1996. Uses queuing version of `kbdio.c'.
57 * - January/February 1997. Tweaked probe logic for
58 * HiNote UltraII/Latitude/Armada laptops.
59 * - 30 July 1997. Added APM support.
60 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
61 * Improved sync check logic.
62 * Vendor specific support routines.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/module.h>
74 #include <sys/syslog.h>
75 #include <sys/malloc.h>
76 #include <machine/bus.h>
78 #include <sys/select.h>
82 #include <machine/clock.h>
83 #include <machine/limits.h>
84 #include <machine/mouse.h>
85 #include <machine/resource.h>
87 #include <bus/isa/isavar.h>
88 #include <dev/misc/kbd/atkbdcreg.h>
91 * Driver specific options: the following options may be set by
92 * `options' statements in the kernel configuration file.
97 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */
100 #ifndef PSM_SYNCERR_THRESHOLD1
101 #define PSM_SYNCERR_THRESHOLD1 20
104 #ifndef PSM_INPUT_TIMEOUT
105 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
108 /* end of driver specific options */
110 #define PSM_DRIVER_NAME "psm"
111 #define PSMCPNP_DRIVER_NAME "psmcpnp"
114 #define PSM_BUFSIZE 960
115 #define PSM_SMALLBUFSIZE 240
117 /* operation levels */
118 #define PSM_LEVEL_BASE 0
119 #define PSM_LEVEL_STANDARD 1
120 #define PSM_LEVEL_NATIVE 2
121 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
122 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
124 /* Logitech PS2++ protocol */
125 #define MOUSE_PS2PLUS_CHECKBITS(b) \
126 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
127 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
128 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
131 #define PSM_UNIT(dev) (minor(dev) >> 1)
132 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
133 #define PSM_MKMINOR(unit,block) (((unit) << 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 (struct psm_softc *, unsigned char *,
234 int *, int, mousestatus_t *);
236 /* function prototypes */
237 static int psmprobe (device_t);
238 static int psmattach (device_t);
239 static int psmdetach (device_t);
240 static int psmresume (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 (KBDC);
249 static int disable_aux_dev (KBDC);
250 static int get_mouse_status (KBDC, int *, int, int);
251 static int get_aux_id (KBDC);
252 static int set_mouse_sampling_rate (KBDC, int);
253 static int set_mouse_scaling (KBDC, int);
254 static int set_mouse_resolution (KBDC, int);
255 static int set_mouse_mode (KBDC);
256 static int get_mouse_buttons (KBDC);
257 static int is_a_mouse (int);
258 static void recover_from_error (KBDC);
259 static int restore_controller (KBDC, int);
260 static int doinitialize (struct psm_softc *, mousemode_t *);
261 static int doopen (struct psm_softc *, int);
262 static int reinitialize (struct psm_softc *, int);
263 static char *model_name (int);
264 static void psmintr (void *);
265 static void psmtimeout (void *);
267 /* vendor specific features */
268 typedef int probefunc_t (struct psm_softc *);
270 static int mouse_id_proc1 (KBDC, int, int, int *);
271 static int mouse_ext_command (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 (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 = {
347 /* name */ PSM_DRIVER_NAME,
348 /* maj */ CDEV_MAJOR,
354 /* close */ psmclose,
357 /* ioctl */ psmioctl,
360 /* strategy */ nostrategy,
365 /* debug message level */
366 static int verbose = PSM_DEBUG;
368 /* device I/O routines */
370 enable_aux_dev(KBDC kbdc)
374 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
376 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
378 return (res == PSM_ACK);
382 disable_aux_dev(KBDC kbdc)
386 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
388 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
390 return (res == PSM_ACK);
394 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
403 cmd = PSMC_SEND_DEV_STATUS;
406 cmd = PSMC_SEND_DEV_DATA;
409 empty_aux_buffer(kbdc, 5);
410 res = send_aux_command(kbdc, cmd);
412 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
413 (flag == 1) ? "DATA" : "STATUS", res);
417 for (i = 0; i < len; ++i) {
418 status[i] = read_aux_data(kbdc);
424 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
425 (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
432 get_aux_id(KBDC kbdc)
437 empty_aux_buffer(kbdc, 5);
438 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
440 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
447 id = read_aux_data(kbdc);
449 log(LOG_DEBUG, "psm: device ID: %04x\n", id);
455 set_mouse_sampling_rate(KBDC kbdc, int rate)
459 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
461 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
463 return ((res == PSM_ACK) ? rate : -1);
467 set_mouse_scaling(KBDC kbdc, int scale)
474 scale = PSMC_SET_SCALING11;
477 scale = PSMC_SET_SCALING21;
480 res = send_aux_command(kbdc, scale);
482 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
483 (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
485 return (res == PSM_ACK);
488 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
490 set_mouse_resolution(KBDC kbdc, int val)
494 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
496 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
498 return ((res == PSM_ACK) ? val : -1);
502 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
503 * re-enabled by calling `enable_aux_dev()'
506 set_mouse_mode(KBDC kbdc)
510 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
512 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
514 return (res == PSM_ACK);
518 get_mouse_buttons(KBDC kbdc)
520 int c = 2; /* assume two buttons by default */
524 * NOTE: a special sequence to obtain Logitech Mouse specific
525 * information: set resolution to 25 ppi, set scaling to 1:1, set
526 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
527 * mouse status bytes is the number of available buttons.
528 * Some manufactures also support this sequence.
530 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
532 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
533 && set_mouse_scaling(kbdc, 1)
534 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
541 /* misc subroutines */
543 * Someday, I will get the complete list of valid pointing devices and
550 static int valid_ids[] = {
551 PSM_MOUSE_ID, /* mouse */
552 PSM_BALLPOINT_ID, /* ballpoint device */
553 PSM_INTELLI_ID, /* Intellimouse */
554 PSM_EXPLORER_ID, /* Intellimouse Explorer */
555 -1 /* end of table */
559 for (i = 0; valid_ids[i] >= 0; ++i)
560 if (valid_ids[i] == id)
569 model_name(int model)
575 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
576 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
577 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
578 { MOUSE_MODEL_THINK, "ThinkingMouse" },
579 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
580 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
581 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
582 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
583 { MOUSE_MODEL_4D, "4D Mouse" },
584 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
585 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
586 { MOUSE_MODEL_UNKNOWN, NULL },
590 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
591 if (models[i].model_code == model)
592 return models[i].model_name;
598 recover_from_error(KBDC kbdc)
600 /* discard anything left in the output buffer */
601 empty_both_buffers(kbdc, 10);
605 * NOTE: KBDC_RESET_KBD may not restore the communication between the
606 * keyboard and the controller.
611 * NOTE: somehow diagnostic and keyboard port test commands bring the
614 if (!test_controller(kbdc))
615 log(LOG_ERR, "psm: keyboard controller failed.\n");
616 /* if there isn't a keyboard in the system, the following error is OK */
617 if (test_kbd_port(kbdc) != 0) {
619 log(LOG_ERR, "psm: keyboard port failed.\n");
625 restore_controller(KBDC kbdc, int command_byte)
627 empty_both_buffers(kbdc, 10);
629 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
630 log(LOG_ERR, "psm: failed to restore the keyboard controller "
632 empty_both_buffers(kbdc, 10);
635 empty_both_buffers(kbdc, 10);
641 * Re-initialize the aux port and device. The aux port must be enabled
642 * and its interrupt must be disabled before calling this routine.
643 * The aux device will be disabled before returning.
644 * The keyboard controller must be locked via `kbdc_lock()' before
645 * calling this routine.
648 doinitialize(struct psm_softc *sc, mousemode_t *mode)
650 KBDC kbdc = sc->kbdc;
654 switch((i = test_aux_port(kbdc))) {
655 case 1: /* ignore this error */
658 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
661 case 0: /* no error */
663 case -1: /* time out */
665 recover_from_error(kbdc);
666 if (sc->config & PSM_CONFIG_IGNPORTERROR)
668 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
673 if (sc->config & PSM_CONFIG_NORESET) {
675 * Don't try to reset the pointing device. It may possibly be
676 * left in the unknown state, though...
680 * NOTE: some controllers appears to hang the `keyboard' when
681 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
683 if (!reset_aux_dev(kbdc)) {
684 recover_from_error(kbdc);
685 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
691 * both the aux port and the aux device is functioning, see
692 * if the device can be enabled.
694 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
695 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
698 empty_both_buffers(kbdc, 10); /* remove stray data if any */
700 if (sc->config & PSM_CONFIG_NOIDPROBE) {
701 i = GENERIC_MOUSE_ENTRY;
703 /* FIXME: hardware ID, mouse buttons? */
705 /* other parameters */
706 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
707 if ((*vendortype[i].probefunc)(sc)) {
709 log(LOG_ERR, "psm%d: found %s\n",
710 sc->unit, model_name(vendortype[i].model));
716 sc->hw.model = vendortype[i].model;
717 sc->mode.packetsize = vendortype[i].packetsize;
719 /* set mouse parameters */
720 if (mode != (mousemode_t *)NULL) {
722 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
723 if (mode->resolution >= 0)
724 mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
725 set_mouse_scaling(kbdc, 1);
726 set_mouse_mode(kbdc);
729 /* request a data packet and extract sync. bits */
730 if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
731 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
733 sc->mode.syncmask[0] = 0;
735 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
736 /* the NetScroll Mouse will send three more bytes... Ignore them */
737 empty_aux_buffer(kbdc, 5);
740 /* just check the status of the mouse */
741 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
742 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
749 doopen(struct psm_softc *sc, int command_byte)
753 /* enable the mouse device */
754 if (!enable_aux_dev(sc->kbdc)) {
755 /* MOUSE ERROR: failed to enable the mouse because:
756 * 1) the mouse is faulty,
757 * 2) the mouse has been removed(!?)
758 * In the latter case, the keyboard may have hung, and need
759 * recovery procedure...
761 recover_from_error(sc->kbdc);
763 /* FIXME: we could reset the mouse here and try to enable
764 * it again. But it will take long time and it's not a good
765 * idea to disable the keyboard that long...
767 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
768 recover_from_error(sc->kbdc);
772 restore_controller(sc->kbdc, command_byte);
773 /* mark this device is no longer available */
774 sc->state &= ~PSM_VALID;
775 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
781 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
782 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
784 /* enable the aux port and interrupt */
785 if (!set_controller_command_byte(sc->kbdc,
786 kbdc_get_device_mask(sc->kbdc),
787 (command_byte & KBD_KBD_CONTROL_BITS)
788 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
789 /* CONTROLLER ERROR */
790 disable_aux_dev(sc->kbdc);
791 restore_controller(sc->kbdc, command_byte);
792 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
797 /* start the watchdog timer */
798 sc->watchdog = FALSE;
799 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
805 reinitialize(struct psm_softc *sc, int doinit)
811 /* don't let anybody mess with the aux device */
812 if (!kbdc_lock(sc->kbdc, TRUE))
816 /* block our watchdog timer */
817 sc->watchdog = FALSE;
818 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
819 callout_handle_init(&sc->callout);
821 /* save the current controller command byte */
822 empty_both_buffers(sc->kbdc, 10);
823 c = get_controller_command_byte(sc->kbdc);
825 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
828 /* enable the aux port but disable the aux interrupt and the keyboard */
829 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
830 kbdc_get_device_mask(sc->kbdc),
831 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
832 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
833 /* CONTROLLER ERROR */
835 kbdc_lock(sc->kbdc, FALSE);
836 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
842 if (sc->state & PSM_VALID) {
843 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */
844 empty_aux_buffer(sc->kbdc, 10);
849 /* try to detect the aux device; are you still there? */
852 if (doinitialize(sc, &sc->mode)) {
854 sc->state |= PSM_VALID;
856 /* the device has gone! */
857 restore_controller(sc->kbdc, c);
858 sc->state &= ~PSM_VALID;
859 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
866 /* restore the driver state */
867 if ((sc->state & PSM_OPEN) && (err == 0)) {
868 /* enable the aux device and the port again */
871 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
874 /* restore the keyboard port and disable the aux port */
875 if (!set_controller_command_byte(sc->kbdc,
876 kbdc_get_device_mask(sc->kbdc),
877 (c & KBD_KBD_CONTROL_BITS)
878 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
879 /* CONTROLLER ERROR */
880 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
886 kbdc_lock(sc->kbdc, FALSE);
890 /* psm driver entry points */
892 #define endprobe(v) { if (bootverbose) \
894 kbdc_set_device_mask(sc->kbdc, mask); \
895 kbdc_lock(sc->kbdc, FALSE); \
900 psmprobe(device_t dev)
902 int unit = device_get_unit(dev);
903 struct psm_softc *sc = device_get_softc(dev);
918 if (XXX_PNP_PROBE(device_get_parent(dev), dev, psm_ids) == ENXIO)
922 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
923 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
926 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
927 sc->config = flags & PSM_CONFIG_FLAGS;
928 /* XXX: for backward compatibility */
929 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
931 #ifdef PSM_RESETAFTERSUSPEND
932 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
934 PSM_CONFIG_HOOKRESUME;
936 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
941 device_set_desc(dev, "PS/2 Mouse");
943 if (!kbdc_lock(sc->kbdc, TRUE)) {
944 printf("psm%d: unable to lock the controller.\n", unit);
951 * NOTE: two bits in the command byte controls the operation of the
952 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
953 * port interrupt (IRQ 12) enable bit (bit 2).
956 /* discard anything left after the keyboard initialization */
957 empty_both_buffers(sc->kbdc, 10);
959 /* save the current command byte; it will be used later */
960 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
961 command_byte = get_controller_command_byte(sc->kbdc);
963 printf("psm%d: current command byte:%04x\n", unit, command_byte);
964 if (command_byte == -1) {
965 /* CONTROLLER ERROR */
966 printf("psm%d: unable to get the current command byte value.\n",
972 * disable the keyboard port while probing the aux port, which must be
973 * enabled during this routine
975 if (!set_controller_command_byte(sc->kbdc,
976 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
977 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
978 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
980 * this is CONTROLLER ERROR; I don't know how to recover
983 restore_controller(sc->kbdc, command_byte);
984 printf("psm%d: unable to set the command byte.\n", unit);
987 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
990 * NOTE: `test_aux_port()' is designed to return with zero if the aux
991 * port exists and is functioning. However, some controllers appears
992 * to respond with zero even when the aux port doesn't exist. (It may
993 * be that this is only the case when the controller DOES have the aux
994 * port but the port is not wired on the motherboard.) The keyboard
995 * controllers without the port, such as the original AT, are
996 * supporsed to return with an error code or simply time out. In any
997 * case, we have to continue probing the port even when the controller
1000 * XXX: some controllers erroneously return the error code 1 when
1001 * it has the perfectly functional aux port. We have to ignore this
1002 * error code. Even if the controller HAS error with the aux port,
1003 * it will be detected later...
1004 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1006 switch ((i = test_aux_port(sc->kbdc))) {
1007 case 1: /* ignore this error */
1010 printf("psm%d: strange result for test aux port (%d).\n",
1013 case 0: /* no error */
1015 case -1: /* time out */
1016 default: /* error */
1017 recover_from_error(sc->kbdc);
1018 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1020 restore_controller(sc->kbdc, command_byte);
1022 printf("psm%d: the aux port is not functioning (%d).\n",
1027 if (sc->config & PSM_CONFIG_NORESET) {
1029 * Don't try to reset the pointing device. It may possibly be
1030 * left in the unknown state, though...
1034 * NOTE: some controllers appears to hang the `keyboard' when the aux
1035 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1037 * Attempt to reset the controller twice -- this helps
1038 * pierce through some KVM switches. The second reset
1041 if (!reset_aux_dev(sc->kbdc)) {
1042 recover_from_error(sc->kbdc);
1043 restore_controller(sc->kbdc, command_byte);
1045 printf("psm%d: failed to reset the aux device.\n", unit);
1047 } else if (!reset_aux_dev(sc->kbdc)) {
1048 recover_from_error(sc->kbdc);
1050 printf("psm%d: failed to reset the aux device (2).\n",
1056 * both the aux port and the aux device is functioning, see if the
1057 * device can be enabled. NOTE: when enabled, the device will start
1058 * sending data; we shall immediately disable the device once we know
1059 * the device can be enabled.
1061 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1063 recover_from_error(sc->kbdc);
1064 restore_controller(sc->kbdc, command_byte);
1066 printf("psm%d: failed to enable the aux device.\n", unit);
1070 /* save the default values after reset */
1071 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1072 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1073 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1075 sc->dflt_mode.rate = sc->mode.rate = -1;
1076 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1079 /* hardware information */
1080 sc->hw.iftype = MOUSE_IF_PS2;
1082 /* verify the device is a mouse */
1083 sc->hw.hwid = get_aux_id(sc->kbdc);
1084 if (!is_a_mouse(sc->hw.hwid)) {
1085 restore_controller(sc->kbdc, command_byte);
1087 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1090 switch (sc->hw.hwid) {
1091 case PSM_BALLPOINT_ID:
1092 sc->hw.type = MOUSE_TRACKBALL;
1095 case PSM_INTELLI_ID:
1096 case PSM_EXPLORER_ID:
1097 case PSM_4DMOUSE_ID:
1099 sc->hw.type = MOUSE_MOUSE;
1102 sc->hw.type = MOUSE_UNKNOWN;
1106 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1108 i = GENERIC_MOUSE_ENTRY;
1111 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1113 /* other parameters */
1114 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1115 if ((*vendortype[i].probefunc)(sc)) {
1117 printf("psm%d: found %s\n",
1118 unit, model_name(vendortype[i].model));
1124 sc->hw.model = vendortype[i].model;
1126 sc->dflt_mode.level = PSM_LEVEL_BASE;
1127 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1128 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1129 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1130 sc->dflt_mode.syncmask[0] = 0;
1132 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1133 if (sc->config & PSM_CONFIG_FORCETAP)
1134 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1135 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1136 sc->mode = sc->dflt_mode;
1137 sc->mode.packetsize = vendortype[i].packetsize;
1139 /* set mouse parameters */
1142 * A version of Logitech FirstMouse+ won't report wheel movement,
1143 * if SET_DEFAULTS is sent... Don't use this command.
1144 * This fix was found by Takashi Nishida.
1146 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1148 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1150 if (sc->config & PSM_CONFIG_RESOLUTION) {
1152 = set_mouse_resolution(sc->kbdc,
1153 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1154 } else if (sc->mode.resolution >= 0) {
1156 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1158 if (sc->mode.rate > 0) {
1159 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1161 set_mouse_scaling(sc->kbdc, 1);
1163 /* request a data packet and extract sync. bits */
1164 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1165 printf("psm%d: failed to get data.\n", unit);
1166 sc->mode.syncmask[0] = 0;
1168 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1169 /* the NetScroll Mouse will send three more bytes... Ignore them */
1170 empty_aux_buffer(sc->kbdc, 5);
1173 /* just check the status of the mouse */
1175 * NOTE: XXX there are some arcane controller/mouse combinations out
1176 * there, which hung the controller unless there is data transmission
1177 * after ACK from the mouse.
1179 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1180 printf("psm%d: failed to get status.\n", unit);
1183 * When in its native mode, some mice operate with different
1184 * default parameters than in the PS/2 compatible mode.
1186 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1187 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1190 /* disable the aux port for now... */
1191 if (!set_controller_command_byte(sc->kbdc,
1192 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1193 (command_byte & KBD_KBD_CONTROL_BITS)
1194 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1196 * this is CONTROLLER ERROR; I don't know the proper way to
1197 * recover from this error...
1199 restore_controller(sc->kbdc, command_byte);
1200 printf("psm%d: unable to set the command byte.\n", unit);
1204 /* see if IRQ is available */
1206 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1208 if (sc->intr == NULL) {
1209 printf("psm%d: unable to allocate the IRQ resource (%d).\n",
1213 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1217 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1218 kbdc_lock(sc->kbdc, FALSE);
1223 psmattach(device_t dev)
1225 int unit = device_get_unit(dev);
1226 struct psm_softc *sc = device_get_softc(dev);
1231 if (sc == NULL) /* shouldn't happen */
1234 /* Setup initial state */
1235 sc->state = PSM_VALID;
1236 callout_handle_init(&sc->callout);
1238 /* Setup our interrupt handler */
1240 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1241 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1243 if (sc->intr == NULL)
1245 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
1246 INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1248 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1253 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1255 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1259 printf("psm%d: model %s, device ID %d\n",
1260 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1262 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1263 unit, model_name(sc->hw.model),
1264 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1265 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1266 unit, sc->config, sc->flags, sc->mode.packetsize);
1267 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1268 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1278 psmdetach(device_t dev)
1280 struct psm_softc *sc;
1283 sc = device_get_softc(dev);
1284 if (sc->state & PSM_OPEN)
1288 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih);
1289 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1291 destroy_dev(sc->dev);
1292 destroy_dev(sc->bdev);
1298 psmopen(dev_t dev, int flag, int fmt, struct thread *td)
1300 int unit = PSM_UNIT(dev);
1301 struct psm_softc *sc;
1306 /* Get device data */
1307 sc = PSM_SOFTC(unit);
1308 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1309 /* the device is no longer valid/functioning */
1312 /* Disallow multiple opens */
1313 if (sc->state & PSM_OPEN)
1316 device_busy(devclass_get_device(psm_devclass, unit));
1318 /* Initialize state */
1319 sc->rsel.si_flags = 0;
1320 sc->rsel.si_pid = 0;
1321 sc->mode.level = sc->dflt_mode.level;
1322 sc->mode.protocol = sc->dflt_mode.protocol;
1323 sc->watchdog = FALSE;
1325 /* flush the event queue */
1326 sc->queue.count = 0;
1329 sc->status.flags = 0;
1330 sc->status.button = 0;
1331 sc->status.obutton = 0;
1337 /* empty input buffer */
1338 bzero(sc->ipacket, sizeof(sc->ipacket));
1342 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1343 if (!kbdc_lock(sc->kbdc, TRUE))
1346 /* save the current controller command byte */
1348 command_byte = get_controller_command_byte(sc->kbdc);
1350 /* enable the aux port and temporalily disable the keyboard */
1351 if ((command_byte == -1)
1352 || !set_controller_command_byte(sc->kbdc,
1353 kbdc_get_device_mask(sc->kbdc),
1354 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1355 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1356 /* CONTROLLER ERROR; do you know how to get out of this? */
1357 kbdc_lock(sc->kbdc, FALSE);
1359 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1364 * Now that the keyboard controller is told not to generate
1365 * the keyboard and mouse interrupts, call `splx()' to allow
1366 * the other tty interrupts. The clock interrupt may also occur,
1367 * but timeout routines will be blocked by the poll flag set
1372 /* enable the mouse device */
1373 err = doopen(sc, command_byte);
1377 sc->state |= PSM_OPEN;
1378 kbdc_lock(sc->kbdc, FALSE);
1383 psmclose(dev_t dev, int flag, int fmt, struct thread *td)
1385 int unit = PSM_UNIT(dev);
1386 struct psm_softc *sc = PSM_SOFTC(unit);
1391 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1392 if (!kbdc_lock(sc->kbdc, TRUE))
1395 /* save the current controller command byte */
1397 command_byte = get_controller_command_byte(sc->kbdc);
1398 if (command_byte == -1) {
1399 kbdc_lock(sc->kbdc, FALSE);
1404 /* disable the aux interrupt and temporalily disable the keyboard */
1405 if (!set_controller_command_byte(sc->kbdc,
1406 kbdc_get_device_mask(sc->kbdc),
1407 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1408 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1409 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1411 /* CONTROLLER ERROR;
1412 * NOTE: we shall force our way through. Because the only
1413 * ill effect we shall see is that we may not be able
1414 * to read ACK from the mouse, and it doesn't matter much
1415 * so long as the mouse will accept the DISABLE command.
1420 /* stop the watchdog timer */
1421 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1422 callout_handle_init(&sc->callout);
1424 /* remove anything left in the output buffer */
1425 empty_aux_buffer(sc->kbdc, 10);
1427 /* disable the aux device, port and interrupt */
1428 if (sc->state & PSM_VALID) {
1429 if (!disable_aux_dev(sc->kbdc)) {
1431 * NOTE: we don't return error and continue, pretending
1432 * we have successfully disabled the device. It's OK because
1433 * the interrupt routine will discard any data from the mouse
1436 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1440 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1441 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1445 if (!set_controller_command_byte(sc->kbdc,
1446 kbdc_get_device_mask(sc->kbdc),
1447 (command_byte & KBD_KBD_CONTROL_BITS)
1448 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1449 /* CONTROLLER ERROR;
1450 * we shall ignore this error; see the above comment.
1452 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1456 /* remove anything left in the output buffer */
1457 empty_aux_buffer(sc->kbdc, 10);
1459 /* close is almost always successful */
1460 sc->state &= ~PSM_OPEN;
1461 kbdc_lock(sc->kbdc, FALSE);
1462 device_unbusy(devclass_get_device(psm_devclass, unit));
1467 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
1469 static unsigned char butmapps2[8] = {
1471 MOUSE_PS2_BUTTON1DOWN,
1472 MOUSE_PS2_BUTTON2DOWN,
1473 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1474 MOUSE_PS2_BUTTON3DOWN,
1475 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1476 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1477 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1479 static unsigned char butmapmsc[8] = {
1480 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1481 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1482 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1483 MOUSE_MSC_BUTTON3UP,
1484 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1485 MOUSE_MSC_BUTTON2UP,
1486 MOUSE_MSC_BUTTON1UP,
1492 if (sc->mode.level == PSM_LEVEL_BASE) {
1493 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1494 if (status->button & MOUSE_BUTTON4DOWN)
1495 mapped |= MOUSE_BUTTON1DOWN;
1496 status->button = mapped;
1497 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1498 i = max(min(status->dx, 255), -256);
1500 buf[0] |= MOUSE_PS2_XNEG;
1502 i = max(min(status->dy, 255), -256);
1504 buf[0] |= MOUSE_PS2_YNEG;
1506 return MOUSE_PS2_PACKETSIZE;
1507 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1508 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1509 i = max(min(status->dx, 255), -256);
1511 buf[3] = i - buf[1];
1512 i = max(min(status->dy, 255), -256);
1514 buf[4] = i - buf[2];
1515 i = max(min(status->dz, 127), -128);
1516 buf[5] = (i >> 1) & 0x7f;
1517 buf[6] = (i - (i >> 1)) & 0x7f;
1518 buf[7] = (~status->button >> 3) & 0x7f;
1519 return MOUSE_SYS_PACKETSIZE;
1521 return sc->inputbytes;;
1525 psmread(dev_t dev, struct uio *uio, int flag)
1527 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1528 unsigned char buf[PSM_SMALLBUFSIZE];
1533 if ((sc->state & PSM_VALID) == 0)
1536 /* block until mouse activity occured */
1538 while (sc->queue.count <= 0) {
1539 if (PSM_NBLOCKIO(dev)) {
1543 sc->state |= PSM_ASLP;
1544 error = tsleep((caddr_t) sc, PCATCH, "psmrea", 0);
1545 sc->state &= ~PSM_ASLP;
1549 } else if ((sc->state & PSM_VALID) == 0) {
1550 /* the device disappeared! */
1557 /* copy data to the user land */
1558 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1560 l = min(sc->queue.count, uio->uio_resid);
1561 if (l > sizeof(buf))
1563 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1564 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1565 sizeof(sc->queue.buf) - sc->queue.head);
1566 bcopy(&sc->queue.buf[0],
1567 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1568 l - (sizeof(sc->queue.buf) - sc->queue.head));
1570 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1572 sc->queue.count -= l;
1573 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1575 error = uiomove(buf, l, uio);
1584 block_mouse_data(struct psm_softc *sc, int *c)
1588 if (!kbdc_lock(sc->kbdc, TRUE))
1592 *c = get_controller_command_byte(sc->kbdc);
1594 || !set_controller_command_byte(sc->kbdc,
1595 kbdc_get_device_mask(sc->kbdc),
1596 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1597 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1598 /* this is CONTROLLER ERROR */
1600 kbdc_lock(sc->kbdc, FALSE);
1605 * The device may be in the middle of status data transmission.
1606 * The transmission will be interrupted, thus, incomplete status
1607 * data must be discarded. Although the aux interrupt is disabled
1608 * at the keyboard controller level, at most one aux interrupt
1609 * may have already been pending and a data byte is in the
1610 * output buffer; throw it away. Note that the second argument
1611 * to `empty_aux_buffer()' is zero, so that the call will just
1612 * flush the internal queue.
1613 * `psmintr()' will be invoked after `splx()' if an interrupt is
1614 * pending; it will see no data and returns immediately.
1616 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1617 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1625 unblock_mouse_data(struct psm_softc *sc, int c)
1630 * We may have seen a part of status data during `set_mouse_XXX()'.
1631 * they have been queued; flush it.
1633 empty_aux_buffer(sc->kbdc, 0);
1635 /* restore ports and interrupt */
1636 if (!set_controller_command_byte(sc->kbdc,
1637 kbdc_get_device_mask(sc->kbdc),
1638 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1639 /* CONTROLLER ERROR; this is serious, we may have
1640 * been left with the inaccessible keyboard and
1641 * the disabled mouse interrupt.
1646 kbdc_lock(sc->kbdc, FALSE);
1651 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1653 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1655 mousestatus_t status;
1656 #if (defined(MOUSE_GETVARS))
1665 /* Perform IOCTL command */
1668 case OLD_MOUSE_GETHWINFO:
1670 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1671 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1672 ((old_mousehw_t *)addr)->type = sc->hw.type;
1673 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1677 case MOUSE_GETHWINFO:
1679 *(mousehw_t *)addr = sc->hw;
1680 if (sc->mode.level == PSM_LEVEL_BASE)
1681 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1685 case OLD_MOUSE_GETMODE:
1687 switch (sc->mode.level) {
1688 case PSM_LEVEL_BASE:
1689 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1691 case PSM_LEVEL_STANDARD:
1692 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1694 case PSM_LEVEL_NATIVE:
1695 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1698 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1699 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1700 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1706 *(mousemode_t *)addr = sc->mode;
1707 ((mousemode_t *)addr)->resolution =
1708 MOUSE_RES_LOW - sc->mode.resolution;
1709 switch (sc->mode.level) {
1710 case PSM_LEVEL_BASE:
1711 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1712 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1714 case PSM_LEVEL_STANDARD:
1715 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1716 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1717 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1718 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1720 case PSM_LEVEL_NATIVE:
1721 /* FIXME: this isn't quite correct... XXX */
1722 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1728 case OLD_MOUSE_SETMODE:
1730 if (cmd == OLD_MOUSE_SETMODE) {
1731 mode.rate = ((old_mousemode_t *)addr)->rate;
1733 * resolution old I/F new I/F
1740 if (((old_mousemode_t *)addr)->resolution > 0)
1741 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1742 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1745 mode = *(mousemode_t *)addr;
1748 /* adjust and validate parameters. */
1749 if (mode.rate > UCHAR_MAX)
1752 mode.rate = sc->dflt_mode.rate;
1753 else if (mode.rate == -1)
1754 /* don't change the current setting */
1756 else if (mode.rate < 0)
1758 if (mode.resolution >= UCHAR_MAX)
1760 if (mode.resolution >= 200)
1761 mode.resolution = MOUSE_RES_HIGH;
1762 else if (mode.resolution >= 100)
1763 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1764 else if (mode.resolution >= 50)
1765 mode.resolution = MOUSE_RES_MEDIUMLOW;
1766 else if (mode.resolution > 0)
1767 mode.resolution = MOUSE_RES_LOW;
1768 if (mode.resolution == MOUSE_RES_DEFAULT)
1769 mode.resolution = sc->dflt_mode.resolution;
1770 else if (mode.resolution == -1)
1771 /* don't change the current setting */
1773 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1774 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1775 if (mode.level == -1)
1776 /* don't change the current setting */
1777 mode.level = sc->mode.level;
1778 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1780 if (mode.accelfactor == -1)
1781 /* don't change the current setting */
1782 mode.accelfactor = sc->mode.accelfactor;
1783 else if (mode.accelfactor < 0)
1786 /* don't allow anybody to poll the keyboard controller */
1787 error = block_mouse_data(sc, &command_byte);
1791 /* set mouse parameters */
1793 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1794 if (mode.resolution >= 0)
1795 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1796 set_mouse_scaling(sc->kbdc, 1);
1797 get_mouse_status(sc->kbdc, stat, 0, 3);
1800 sc->mode.rate = mode.rate;
1801 sc->mode.resolution = mode.resolution;
1802 sc->mode.accelfactor = mode.accelfactor;
1803 sc->mode.level = mode.level;
1806 unblock_mouse_data(sc, command_byte);
1809 case MOUSE_GETLEVEL:
1810 *(int *)addr = sc->mode.level;
1813 case MOUSE_SETLEVEL:
1814 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1816 sc->mode.level = *(int *)addr;
1819 case MOUSE_GETSTATUS:
1821 status = sc->status;
1822 sc->status.flags = 0;
1823 sc->status.obutton = sc->status.button;
1824 sc->status.button = 0;
1829 *(mousestatus_t *)addr = status;
1832 #if (defined(MOUSE_GETVARS))
1834 var = (mousevar_t *)addr;
1835 bzero(var, sizeof(*var));
1837 var->var[0] = MOUSE_VARS_PS2_SIG;
1838 var->var[1] = sc->config;
1839 var->var[2] = sc->flags;
1845 #endif /* MOUSE_GETVARS */
1847 case MOUSE_READSTATE:
1848 case MOUSE_READDATA:
1849 data = (mousedata_t *)addr;
1850 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1853 error = block_mouse_data(sc, &command_byte);
1856 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1857 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1859 unblock_mouse_data(sc, command_byte);
1862 #if (defined(MOUSE_SETRESOLUTION))
1863 case MOUSE_SETRESOLUTION:
1864 mode.resolution = *(int *)addr;
1865 if (mode.resolution >= UCHAR_MAX)
1867 else if (mode.resolution >= 200)
1868 mode.resolution = MOUSE_RES_HIGH;
1869 else if (mode.resolution >= 100)
1870 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1871 else if (mode.resolution >= 50)
1872 mode.resolution = MOUSE_RES_MEDIUMLOW;
1873 else if (mode.resolution > 0)
1874 mode.resolution = MOUSE_RES_LOW;
1875 if (mode.resolution == MOUSE_RES_DEFAULT)
1876 mode.resolution = sc->dflt_mode.resolution;
1877 else if (mode.resolution == -1)
1878 mode.resolution = sc->mode.resolution;
1879 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1880 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1882 error = block_mouse_data(sc, &command_byte);
1885 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1886 if (sc->mode.resolution != mode.resolution)
1888 unblock_mouse_data(sc, command_byte);
1890 #endif /* MOUSE_SETRESOLUTION */
1892 #if (defined(MOUSE_SETRATE))
1894 mode.rate = *(int *)addr;
1895 if (mode.rate > UCHAR_MAX)
1898 mode.rate = sc->dflt_mode.rate;
1899 else if (mode.rate < 0)
1900 mode.rate = sc->mode.rate;
1902 error = block_mouse_data(sc, &command_byte);
1905 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1906 if (sc->mode.rate != mode.rate)
1908 unblock_mouse_data(sc, command_byte);
1910 #endif /* MOUSE_SETRATE */
1912 #if (defined(MOUSE_SETSCALING))
1913 case MOUSE_SETSCALING:
1914 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1917 error = block_mouse_data(sc, &command_byte);
1920 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1922 unblock_mouse_data(sc, command_byte);
1924 #endif /* MOUSE_SETSCALING */
1926 #if (defined(MOUSE_GETHWID))
1928 error = block_mouse_data(sc, &command_byte);
1931 sc->hw.hwid &= ~0x00ff;
1932 sc->hw.hwid |= get_aux_id(sc->kbdc);
1933 *(int *)addr = sc->hw.hwid & 0x00ff;
1934 unblock_mouse_data(sc, command_byte);
1936 #endif /* MOUSE_GETHWID */
1946 psmtimeout(void *arg)
1948 struct psm_softc *sc;
1951 sc = (struct psm_softc *)arg;
1953 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1955 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1957 kbdc_lock(sc->kbdc, FALSE);
1959 sc->watchdog = TRUE;
1961 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
1968 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
1969 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
1971 static int butmap[8] = {
1975 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1977 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1978 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1979 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1981 static int butmap_versapad[8] = {
1987 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1989 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
1991 struct psm_softc *sc = arg;
1999 /* read until there is nothing to read */
2000 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2002 /* discard the byte if the device is not open */
2003 if ((sc->state & PSM_OPEN) == 0)
2006 getmicrouptime(&tv);
2007 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
2008 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2012 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2013 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2014 timevaladd(&sc->inputtimeout, &tv);
2016 sc->ipacket[sc->inputbytes++] = c;
2017 if (sc->inputbytes < sc->mode.packetsize)
2021 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2022 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
2023 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
2028 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2029 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
2030 c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
2032 if (sc->syncerrors < sc->mode.packetsize) {
2033 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2035 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2036 } else if (sc->syncerrors == sc->mode.packetsize) {
2037 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2039 disable_aux_dev(sc->kbdc);
2040 enable_aux_dev(sc->kbdc);
2041 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2042 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2044 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2045 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2046 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2047 reinitialize(sc, TRUE);
2053 * A kludge for Kensington device!
2054 * The MSB of the horizontal count appears to be stored in
2057 if (sc->hw.model == MOUSE_MODEL_THINK)
2058 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2060 /* ignore the overflow bits... */
2061 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2062 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2064 ms.obutton = sc->button; /* previous button state */
2065 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2066 /* `tapping' action */
2067 if (sc->config & PSM_CONFIG_FORCETAP)
2068 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2070 switch (sc->hw.model) {
2072 case MOUSE_MODEL_EXPLORER:
2074 * b7 b6 b5 b4 b3 b2 b1 b0
2075 * byte 1: oy ox sy sx 1 M R L
2076 * byte 2: x x x x x x x x
2077 * byte 3: y y y y y y y y
2078 * byte 4: * * S2 S1 s d2 d1 d0
2080 * L, M, R, S1, S2: left, middle, right and side buttons
2081 * s: wheel data sign bit
2084 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2085 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
2086 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2087 ? MOUSE_BUTTON4DOWN : 0;
2088 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2089 ? MOUSE_BUTTON5DOWN : 0;
2092 case MOUSE_MODEL_INTELLI:
2093 case MOUSE_MODEL_NET:
2094 /* wheel data is in the fourth byte */
2095 z = (char)sc->ipacket[3];
2096 /* some mice may send 7 when there is no Z movement?! XXX */
2097 if ((z >= 7) || (z <= -7))
2099 /* some compatible mice have additional buttons */
2100 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2101 ? MOUSE_BUTTON4DOWN : 0;
2102 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2103 ? MOUSE_BUTTON5DOWN : 0;
2106 case MOUSE_MODEL_MOUSEMANPLUS:
2108 * PS2++ protocl packet
2110 * b7 b6 b5 b4 b3 b2 b1 b0
2111 * byte 1: * 1 p3 p2 1 * * *
2112 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2114 * p3-p0: packet type
2115 * c1, c2: c1 & c2 == 1, if p2 == 0
2116 * c1 & c2 == 0, if p2 == 1
2118 * packet type: 0 (device type)
2119 * See comments in enable_mmanplus() below.
2121 * packet type: 1 (wheel data)
2123 * b7 b6 b5 b4 b3 b2 b1 b0
2124 * byte 3: h * B5 B4 s d2 d1 d0
2126 * h: 1, if horizontal roller data
2127 * 0, if vertical roller data
2128 * B4, B5: button 4 and 5
2130 * d2-d0: roller data
2132 * packet type: 2 (reserved)
2134 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2136 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
2137 /* the extended data packet encodes button and wheel events */
2138 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
2140 /* wheel data packet */
2142 if (sc->ipacket[2] & 0x80) {
2143 /* horizontal roller count - ignore it XXX*/
2145 /* vertical roller count */
2146 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2147 ? (sc->ipacket[2] & 0x0f) - 16
2148 : (sc->ipacket[2] & 0x0f);
2150 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2151 ? MOUSE_BUTTON4DOWN : 0;
2152 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2153 ? MOUSE_BUTTON5DOWN : 0;
2156 /* this packet type is reserved by Logitech... */
2158 * IBM ScrollPoint Mouse uses this packet type to
2159 * encode both vertical and horizontal scroll movement.
2162 /* horizontal count */
2163 if (sc->ipacket[2] & 0x0f)
2164 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2165 /* vertical count */
2166 if (sc->ipacket[2] & 0xf0)
2167 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2169 /* vertical count */
2170 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
2171 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16
2172 : ((sc->ipacket[2] >> 4) & 0x0f);
2173 /* horizontal count */
2174 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
2175 ? (sc->ipacket[2] & 0x0f) - 16
2176 : (sc->ipacket[2] & 0x0f);
2180 /* device type packet - shouldn't happen */
2184 ms.button = ms.obutton;
2186 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2187 "0x%02x 0x%02x 0x%02x\n",
2188 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
2189 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
2193 /* preserve button states */
2194 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2198 case MOUSE_MODEL_GLIDEPOINT:
2199 /* `tapping' action */
2200 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2203 case MOUSE_MODEL_NETSCROLL:
2204 /* three addtional bytes encode buttons and wheel events */
2205 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2206 ? MOUSE_BUTTON4DOWN : 0;
2207 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2208 ? MOUSE_BUTTON5DOWN : 0;
2209 z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
2210 ? sc->ipacket[4] - 256 : sc->ipacket[4];
2213 case MOUSE_MODEL_THINK:
2214 /* the fourth button state in the first byte */
2215 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2218 case MOUSE_MODEL_VERSAPAD:
2219 /* VersaPad PS/2 absolute mode message format
2221 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2222 * ipacket[0]: 1 1 0 A 1 L T R
2223 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2224 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2225 * ipacket[3]: 1 1 1 A 1 L T R
2226 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2227 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2230 * R: right physical mouse button (1=on)
2231 * T: touch pad virtual button (1=tapping)
2232 * L: left physical mouse button (1=on)
2233 * A: position data is valid (1=valid)
2234 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2235 * V: vertical data (12bit signed integer. V11 is sign bit.)
2238 * Tapping is mapped to MOUSE_BUTTON4.
2240 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2241 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2243 if (c & MOUSE_PS2VERSA_IN_USE) {
2244 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
2245 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
2250 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2253 if (x < 0) /* XXX */
2262 sc->flags |= PSM_FLAGS_FINGERDOWN;
2267 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2269 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2270 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2273 case MOUSE_MODEL_4D:
2275 * b7 b6 b5 b4 b3 b2 b1 b0
2276 * byte 1: s2 d2 s1 d1 1 M R L
2277 * byte 2: sx x x x x x x x
2278 * byte 3: sy y y y y y y y
2280 * s1: wheel 1 direction
2282 * s2: wheel 2 direction
2285 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2286 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2287 switch (c & MOUSE_4D_WHEELBITS) {
2294 case 0x40: /* 2nd wheel turning right XXX */
2297 case 0xc0: /* 2nd wheel turning left XXX */
2303 case MOUSE_MODEL_4DPLUS:
2304 if ((x < 16 - 256) && (y < 16 - 256)) {
2306 * b7 b6 b5 b4 b3 b2 b1 b0
2307 * byte 1: 0 0 1 1 1 M R L
2308 * byte 2: 0 0 0 0 1 0 0 0
2309 * byte 3: 0 0 0 0 S s d1 d0
2311 * L, M, R, S: left, middle, right and side buttons
2312 * s: wheel data sign bit
2316 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2317 ms.button |= MOUSE_BUTTON4DOWN;
2318 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2319 ? ((sc->ipacket[2] & 0x07) - 8)
2320 : (sc->ipacket[2] & 0x07) ;
2322 /* preserve previous button states */
2323 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2327 case MOUSE_MODEL_GENERIC:
2333 if (sc->mode.accelfactor >= 1) {
2335 x = x * x / sc->mode.accelfactor;
2338 if (c & MOUSE_PS2_XNEG)
2342 y = y * y / sc->mode.accelfactor;
2345 if (c & MOUSE_PS2_YNEG)
2353 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2354 | (ms.obutton ^ ms.button);
2356 if (sc->mode.level < PSM_LEVEL_NATIVE)
2357 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
2359 sc->status.flags |= ms.flags;
2360 sc->status.dx += ms.dx;
2361 sc->status.dy += ms.dy;
2362 sc->status.dz += ms.dz;
2363 sc->status.button = ms.button;
2364 sc->button = ms.button;
2366 sc->watchdog = FALSE;
2369 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2370 l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2371 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2372 if (sc->inputbytes > l)
2373 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
2375 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
2376 sc->queue.count += sc->inputbytes;
2380 if (sc->state & PSM_ASLP) {
2381 sc->state &= ~PSM_ASLP;
2382 wakeup((caddr_t) sc);
2384 selwakeup(&sc->rsel);
2389 psmpoll(dev_t dev, int events, struct thread *td)
2391 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2395 /* Return true if a mouse event available */
2397 if (events & (POLLIN | POLLRDNORM)) {
2398 if (sc->queue.count > 0)
2399 revents |= events & (POLLIN | POLLRDNORM);
2401 selrecord(td, &sc->rsel);
2408 /* vendor/model specific routines */
2410 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2412 if (set_mouse_resolution(kbdc, res) != res)
2414 if (set_mouse_scaling(kbdc, scale)
2415 && set_mouse_scaling(kbdc, scale)
2416 && set_mouse_scaling(kbdc, scale)
2417 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2423 mouse_ext_command(KBDC kbdc, int command)
2427 c = (command >> 6) & 0x03;
2428 if (set_mouse_resolution(kbdc, c) != c)
2430 c = (command >> 4) & 0x03;
2431 if (set_mouse_resolution(kbdc, c) != c)
2433 c = (command >> 2) & 0x03;
2434 if (set_mouse_resolution(kbdc, c) != c)
2436 c = (command >> 0) & 0x03;
2437 if (set_mouse_resolution(kbdc, c) != c)
2443 /* Logitech MouseMan Cordless II */
2445 enable_lcordless(struct psm_softc *sc)
2450 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2452 if (status[1] == PSMD_RES_HIGH)
2454 ch = (status[0] & 0x07) - 1; /* channel # */
2455 if ((ch <= 0) || (ch > 4))
2458 * status[1]: always one?
2459 * status[2]: battery status? (0-100)
2465 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2467 enable_groller(struct psm_softc *sc)
2472 * The special sequence to enable the fourth button and the
2473 * roller. Immediately after this sequence check status bytes.
2474 * if the mouse is NetScroll, the second and the third bytes are
2479 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2480 * look like the following.
2482 * byte 1 bit 7 always 0
2483 * bit 6 stream mode (0)
2484 * bit 5 disabled (0)
2485 * bit 4 1:1 scaling (0)
2487 * bit 0-2 button status
2488 * byte 2 resolution (PSMD_RES_HIGH)
2489 * byte 3 report rate (?)
2492 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2494 if ((status[1] != '3') || (status[2] != 'D'))
2496 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2501 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2503 enable_gmouse(struct psm_softc *sc)
2508 * The special sequence to enable the middle, "rubber" button.
2509 * Immediately after this sequence check status bytes.
2510 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2511 * the second and the third bytes are '3' and 'U'.
2512 * NOTE: NetMouse reports that it has three buttons although it has
2513 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2514 * say they have three buttons too and they do have a button on the
2517 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2519 if ((status[1] != '3') || (status[2] != 'U'))
2524 /* ALPS GlidePoint */
2526 enable_aglide(struct psm_softc *sc)
2531 * The special sequence to obtain ALPS GlidePoint specific
2532 * information. Immediately after this sequence, status bytes will
2533 * contain something interesting.
2534 * NOTE: ALPS produces several models of GlidePoint. Some of those
2535 * do not respond to this sequence, thus, cannot be detected this way.
2537 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2539 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2541 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2546 /* Kensington ThinkingMouse/Trackball */
2548 enable_kmouse(struct psm_softc *sc)
2550 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2551 KBDC kbdc = sc->kbdc;
2557 id1 = get_aux_id(kbdc);
2558 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2561 * The device is now in the native mode? It returns a different
2564 id2 = get_aux_id(kbdc);
2565 if ((id1 == id2) || (id2 != 2))
2568 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2571 /* at this point, resolution is LOW, sampling rate is 10/sec */
2572 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2577 * The special sequence to enable the third and fourth buttons.
2578 * Otherwise they behave like the first and second buttons.
2580 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2581 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2586 * At this point, the device is using default resolution and
2587 * sampling rate for the native mode.
2589 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2591 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2594 /* the device appears be enabled by this sequence, diable it for now */
2595 disable_aux_dev(kbdc);
2596 empty_aux_buffer(kbdc, 5);
2601 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2603 enable_mmanplus(struct psm_softc *sc)
2605 KBDC kbdc = sc->kbdc;
2608 /* the special sequence to enable the fourth button and the roller. */
2610 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2611 * must be called exactly three times since the last RESET command
2612 * before this sequence. XXX
2614 if (!set_mouse_scaling(kbdc, 1))
2616 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2618 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2622 * PS2++ protocl, packet type 0
2624 * b7 b6 b5 b4 b3 b2 b1 b0
2625 * byte 1: * 1 p3 p2 1 * * *
2626 * byte 2: 1 1 p1 p0 m1 m0 1 0
2627 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2629 * p3-p0: packet type: 0
2630 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2632 /* check constant bits */
2633 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2635 if ((data[1] & 0xc3) != 0xc2)
2637 /* check d3-d0 in byte 2 */
2638 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2641 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2644 sc->hw.hwid &= 0x00ff;
2645 sc->hw.hwid |= data[2] << 8; /* save model ID */
2648 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2649 * the wheel and the fourth button events are encoded in the
2650 * special data packet. The mouse may be put in the IntelliMouse mode
2651 * if it is initialized by the IntelliMouse's method.
2656 /* MS IntelliMouse Explorer */
2658 enable_msexplorer(struct psm_softc *sc)
2660 static unsigned char rate0[] = { 200, 100, 80, };
2661 static unsigned char rate1[] = { 200, 200, 80, };
2662 KBDC kbdc = sc->kbdc;
2666 /* the special sequence to enable the extra buttons and the roller. */
2667 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2668 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2671 /* the device will give the genuine ID only after the above sequence */
2672 id = get_aux_id(kbdc);
2673 if (id != PSM_EXPLORER_ID)
2677 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2680 * XXX: this is a kludge to fool some KVM switch products
2681 * which think they are clever enough to know the 4-byte IntelliMouse
2682 * protocol, and assume any other protocols use 3-byte packets.
2683 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2684 * correctly to the host computer because of this!
2685 * The following sequence is actually IntelliMouse's "wake up"
2686 * sequence; it will make the KVM think the mouse is IntelliMouse
2687 * when it is in fact IntelliMouse Explorer.
2689 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2690 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2693 id = get_aux_id(kbdc);
2698 /* MS IntelliMouse */
2700 enable_msintelli(struct psm_softc *sc)
2703 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2704 * probe routine and act like IntelliMouse.
2707 static unsigned char rate[] = { 200, 100, 80, };
2708 KBDC kbdc = sc->kbdc;
2712 /* the special sequence to enable the third button and the roller. */
2713 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2714 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2717 /* the device will give the genuine ID only after the above sequence */
2718 id = get_aux_id(kbdc);
2719 if (id != PSM_INTELLI_ID)
2728 /* A4 Tech 4D Mouse */
2730 enable_4dmouse(struct psm_softc *sc)
2733 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2736 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2737 KBDC kbdc = sc->kbdc;
2741 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2742 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2745 id = get_aux_id(kbdc);
2747 * WinEasy 4D, 4 Way Scroll 4D: 6
2748 * Cable-Free 4D: 8 (4DPLUS)
2749 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2751 if (id != PSM_4DMOUSE_ID)
2755 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2760 /* A4 Tech 4D+ Mouse */
2762 enable_4dplus(struct psm_softc *sc)
2765 * Newer wheel mice from A4 Tech seem to use this protocol.
2766 * Older models are recognized as either 4D Mouse or IntelliMouse.
2768 KBDC kbdc = sc->kbdc;
2772 * enable_4dmouse() already issued the following ID sequence...
2773 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2776 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2777 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2782 id = get_aux_id(kbdc);
2783 if (id != PSM_4DPLUS_ID)
2787 sc->hw.buttons = 4; /* XXX */
2792 /* Interlink electronics VersaPad */
2794 enable_versapad(struct psm_softc *sc)
2796 KBDC kbdc = sc->kbdc;
2799 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2800 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2801 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2802 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2803 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2804 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2805 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2807 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2809 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2811 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2813 return TRUE; /* PS/2 absolute mode */
2817 psmresume(device_t dev)
2819 struct psm_softc *sc = device_get_softc(dev);
2820 int unit = device_get_unit(dev);
2824 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2826 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2829 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2831 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2833 * Release the blocked process; it must be notified that the device
2834 * cannot be accessed anymore.
2836 sc->state &= ~PSM_ASLP;
2837 wakeup((caddr_t)sc);
2841 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2846 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);