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.7 2003/08/07 21:16:58 dillon Exp $
28 * Ported to 386bsd Oct 17, 1992
29 * Sandi Donno, Computer Science, University of Cape Town, South Africa
30 * Please send bug reports to sandi@cs.uct.ac.za
32 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
33 * although I was only partially successful in getting the alpha release
34 * of his "driver for the Logitech and ATI Inport Bus mice for use with
35 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
36 * found his code to be an invaluable reference when porting this driver
39 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
40 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
42 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
43 * Andrew Herbert - 12 June 1993
45 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
48 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
51 * Hardware access routines and probe logic rewritten by
52 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
53 * - 3, 14, 22 October 1996.
54 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
55 * - 14, 30 November 1996. Uses `kbdio.c'.
56 * - 13 December 1996. Uses queuing version of `kbdio.c'.
57 * - January/February 1997. Tweaked probe logic for
58 * HiNote UltraII/Latitude/Armada laptops.
59 * - 30 July 1997. Added APM support.
60 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
61 * Improved sync check logic.
62 * Vendor specific support routines.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/module.h>
74 #include <sys/syslog.h>
75 #include <sys/malloc.h>
76 #include <machine/bus.h>
78 #include <sys/select.h>
82 #include <machine/clock.h>
83 #include <machine/limits.h>
84 #include <machine/mouse.h>
85 #include <machine/resource.h>
87 #include <bus/isa/isavar.h>
88 #include <dev/misc/kbd/atkbdcreg.h>
91 * Driver specific options: the following options may be set by
92 * `options' statements in the kernel configuration file.
97 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */
100 #ifndef PSM_SYNCERR_THRESHOLD1
101 #define PSM_SYNCERR_THRESHOLD1 20
104 #ifndef PSM_INPUT_TIMEOUT
105 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
108 /* end of driver specific options */
110 #define PSM_DRIVER_NAME "psm"
111 #define PSMCPNP_DRIVER_NAME "psmcpnp"
114 #define PSM_BUFSIZE 960
115 #define PSM_SMALLBUFSIZE 240
117 /* operation levels */
118 #define PSM_LEVEL_BASE 0
119 #define PSM_LEVEL_STANDARD 1
120 #define PSM_LEVEL_NATIVE 2
121 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
122 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
124 /* Logitech PS2++ protocol */
125 #define MOUSE_PS2PLUS_CHECKBITS(b) \
126 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
127 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
128 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
131 #define PSM_UNIT(dev) (minor(dev) >> 1)
132 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
133 #define PSM_MKMINOR(unit,block) (((unit) << 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 = {
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 if (!reset_aux_dev(sc->kbdc)) {
1038 recover_from_error(sc->kbdc);
1039 restore_controller(sc->kbdc, command_byte);
1041 printf("psm%d: failed to reset the aux device.\n", unit);
1047 * both the aux port and the aux device is functioning, see if the
1048 * device can be enabled. NOTE: when enabled, the device will start
1049 * sending data; we shall immediately disable the device once we know
1050 * the device can be enabled.
1052 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1054 recover_from_error(sc->kbdc);
1055 restore_controller(sc->kbdc, command_byte);
1057 printf("psm%d: failed to enable the aux device.\n", unit);
1061 /* save the default values after reset */
1062 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1063 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1064 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1066 sc->dflt_mode.rate = sc->mode.rate = -1;
1067 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1070 /* hardware information */
1071 sc->hw.iftype = MOUSE_IF_PS2;
1073 /* verify the device is a mouse */
1074 sc->hw.hwid = get_aux_id(sc->kbdc);
1075 if (!is_a_mouse(sc->hw.hwid)) {
1076 restore_controller(sc->kbdc, command_byte);
1078 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1081 switch (sc->hw.hwid) {
1082 case PSM_BALLPOINT_ID:
1083 sc->hw.type = MOUSE_TRACKBALL;
1086 case PSM_INTELLI_ID:
1087 case PSM_EXPLORER_ID:
1088 case PSM_4DMOUSE_ID:
1090 sc->hw.type = MOUSE_MOUSE;
1093 sc->hw.type = MOUSE_UNKNOWN;
1097 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1099 i = GENERIC_MOUSE_ENTRY;
1102 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1104 /* other parameters */
1105 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1106 if ((*vendortype[i].probefunc)(sc)) {
1108 printf("psm%d: found %s\n",
1109 unit, model_name(vendortype[i].model));
1115 sc->hw.model = vendortype[i].model;
1117 sc->dflt_mode.level = PSM_LEVEL_BASE;
1118 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1119 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1120 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1121 sc->dflt_mode.syncmask[0] = 0;
1123 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1124 if (sc->config & PSM_CONFIG_FORCETAP)
1125 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1126 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1127 sc->mode = sc->dflt_mode;
1128 sc->mode.packetsize = vendortype[i].packetsize;
1130 /* set mouse parameters */
1133 * A version of Logitech FirstMouse+ won't report wheel movement,
1134 * if SET_DEFAULTS is sent... Don't use this command.
1135 * This fix was found by Takashi Nishida.
1137 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1139 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1141 if (sc->config & PSM_CONFIG_RESOLUTION) {
1143 = set_mouse_resolution(sc->kbdc,
1144 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1145 } else if (sc->mode.resolution >= 0) {
1147 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1149 if (sc->mode.rate > 0) {
1150 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1152 set_mouse_scaling(sc->kbdc, 1);
1154 /* request a data packet and extract sync. bits */
1155 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1156 printf("psm%d: failed to get data.\n", unit);
1157 sc->mode.syncmask[0] = 0;
1159 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1160 /* the NetScroll Mouse will send three more bytes... Ignore them */
1161 empty_aux_buffer(sc->kbdc, 5);
1164 /* just check the status of the mouse */
1166 * NOTE: XXX there are some arcane controller/mouse combinations out
1167 * there, which hung the controller unless there is data transmission
1168 * after ACK from the mouse.
1170 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1171 printf("psm%d: failed to get status.\n", unit);
1174 * When in its native mode, some mice operate with different
1175 * default parameters than in the PS/2 compatible mode.
1177 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1178 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1181 /* disable the aux port for now... */
1182 if (!set_controller_command_byte(sc->kbdc,
1183 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1184 (command_byte & KBD_KBD_CONTROL_BITS)
1185 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1187 * this is CONTROLLER ERROR; I don't know the proper way to
1188 * recover from this error...
1190 restore_controller(sc->kbdc, command_byte);
1191 printf("psm%d: unable to set the command byte.\n", unit);
1195 /* see if IRQ is available */
1197 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1199 if (sc->intr == NULL) {
1200 printf("psm%d: unable to allocate the IRQ resource (%d).\n",
1204 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1208 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1209 kbdc_lock(sc->kbdc, FALSE);
1214 psmattach(device_t dev)
1216 int unit = device_get_unit(dev);
1217 struct psm_softc *sc = device_get_softc(dev);
1222 if (sc == NULL) /* shouldn't happen */
1225 /* Setup initial state */
1226 sc->state = PSM_VALID;
1227 callout_handle_init(&sc->callout);
1229 /* Setup our interrupt handler */
1231 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1232 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1234 if (sc->intr == NULL)
1236 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
1237 INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1239 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1244 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1246 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1250 printf("psm%d: model %s, device ID %d\n",
1251 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1253 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1254 unit, model_name(sc->hw.model),
1255 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1256 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1257 unit, sc->config, sc->flags, sc->mode.packetsize);
1258 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1259 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1269 psmdetach(device_t dev)
1271 struct psm_softc *sc;
1274 sc = device_get_softc(dev);
1275 if (sc->state & PSM_OPEN)
1279 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih);
1280 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1282 destroy_dev(sc->dev);
1283 destroy_dev(sc->bdev);
1289 psmopen(dev_t dev, int flag, int fmt, struct thread *td)
1291 int unit = PSM_UNIT(dev);
1292 struct psm_softc *sc;
1297 /* Get device data */
1298 sc = PSM_SOFTC(unit);
1299 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1300 /* the device is no longer valid/functioning */
1303 /* Disallow multiple opens */
1304 if (sc->state & PSM_OPEN)
1307 device_busy(devclass_get_device(psm_devclass, unit));
1309 /* Initialize state */
1310 sc->rsel.si_flags = 0;
1311 sc->rsel.si_pid = 0;
1312 sc->mode.level = sc->dflt_mode.level;
1313 sc->mode.protocol = sc->dflt_mode.protocol;
1314 sc->watchdog = FALSE;
1316 /* flush the event queue */
1317 sc->queue.count = 0;
1320 sc->status.flags = 0;
1321 sc->status.button = 0;
1322 sc->status.obutton = 0;
1328 /* empty input buffer */
1329 bzero(sc->ipacket, sizeof(sc->ipacket));
1333 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1334 if (!kbdc_lock(sc->kbdc, TRUE))
1337 /* save the current controller command byte */
1339 command_byte = get_controller_command_byte(sc->kbdc);
1341 /* enable the aux port and temporalily disable the keyboard */
1342 if ((command_byte == -1)
1343 || !set_controller_command_byte(sc->kbdc,
1344 kbdc_get_device_mask(sc->kbdc),
1345 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1346 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1347 /* CONTROLLER ERROR; do you know how to get out of this? */
1348 kbdc_lock(sc->kbdc, FALSE);
1350 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1355 * Now that the keyboard controller is told not to generate
1356 * the keyboard and mouse interrupts, call `splx()' to allow
1357 * the other tty interrupts. The clock interrupt may also occur,
1358 * but timeout routines will be blocked by the poll flag set
1363 /* enable the mouse device */
1364 err = doopen(sc, command_byte);
1368 sc->state |= PSM_OPEN;
1369 kbdc_lock(sc->kbdc, FALSE);
1374 psmclose(dev_t dev, int flag, int fmt, struct thread *td)
1376 int unit = PSM_UNIT(dev);
1377 struct psm_softc *sc = PSM_SOFTC(unit);
1382 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1383 if (!kbdc_lock(sc->kbdc, TRUE))
1386 /* save the current controller command byte */
1388 command_byte = get_controller_command_byte(sc->kbdc);
1389 if (command_byte == -1) {
1390 kbdc_lock(sc->kbdc, FALSE);
1395 /* disable the aux interrupt and temporalily disable the keyboard */
1396 if (!set_controller_command_byte(sc->kbdc,
1397 kbdc_get_device_mask(sc->kbdc),
1398 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1399 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1400 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1402 /* CONTROLLER ERROR;
1403 * NOTE: we shall force our way through. Because the only
1404 * ill effect we shall see is that we may not be able
1405 * to read ACK from the mouse, and it doesn't matter much
1406 * so long as the mouse will accept the DISABLE command.
1411 /* stop the watchdog timer */
1412 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1413 callout_handle_init(&sc->callout);
1415 /* remove anything left in the output buffer */
1416 empty_aux_buffer(sc->kbdc, 10);
1418 /* disable the aux device, port and interrupt */
1419 if (sc->state & PSM_VALID) {
1420 if (!disable_aux_dev(sc->kbdc)) {
1422 * NOTE: we don't return error and continue, pretending
1423 * we have successfully disabled the device. It's OK because
1424 * the interrupt routine will discard any data from the mouse
1427 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1431 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1432 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1436 if (!set_controller_command_byte(sc->kbdc,
1437 kbdc_get_device_mask(sc->kbdc),
1438 (command_byte & KBD_KBD_CONTROL_BITS)
1439 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1440 /* CONTROLLER ERROR;
1441 * we shall ignore this error; see the above comment.
1443 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1447 /* remove anything left in the output buffer */
1448 empty_aux_buffer(sc->kbdc, 10);
1450 /* close is almost always successful */
1451 sc->state &= ~PSM_OPEN;
1452 kbdc_lock(sc->kbdc, FALSE);
1453 device_unbusy(devclass_get_device(psm_devclass, unit));
1458 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
1460 static unsigned char butmapps2[8] = {
1462 MOUSE_PS2_BUTTON1DOWN,
1463 MOUSE_PS2_BUTTON2DOWN,
1464 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1465 MOUSE_PS2_BUTTON3DOWN,
1466 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1467 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1468 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1470 static unsigned char butmapmsc[8] = {
1471 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1472 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1473 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1474 MOUSE_MSC_BUTTON3UP,
1475 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1476 MOUSE_MSC_BUTTON2UP,
1477 MOUSE_MSC_BUTTON1UP,
1483 if (sc->mode.level == PSM_LEVEL_BASE) {
1484 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1485 if (status->button & MOUSE_BUTTON4DOWN)
1486 mapped |= MOUSE_BUTTON1DOWN;
1487 status->button = mapped;
1488 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1489 i = max(min(status->dx, 255), -256);
1491 buf[0] |= MOUSE_PS2_XNEG;
1493 i = max(min(status->dy, 255), -256);
1495 buf[0] |= MOUSE_PS2_YNEG;
1497 return MOUSE_PS2_PACKETSIZE;
1498 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1499 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1500 i = max(min(status->dx, 255), -256);
1502 buf[3] = i - buf[1];
1503 i = max(min(status->dy, 255), -256);
1505 buf[4] = i - buf[2];
1506 i = max(min(status->dz, 127), -128);
1507 buf[5] = (i >> 1) & 0x7f;
1508 buf[6] = (i - (i >> 1)) & 0x7f;
1509 buf[7] = (~status->button >> 3) & 0x7f;
1510 return MOUSE_SYS_PACKETSIZE;
1512 return sc->inputbytes;;
1516 psmread(dev_t dev, struct uio *uio, int flag)
1518 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1519 unsigned char buf[PSM_SMALLBUFSIZE];
1524 if ((sc->state & PSM_VALID) == 0)
1527 /* block until mouse activity occured */
1529 while (sc->queue.count <= 0) {
1530 if (PSM_NBLOCKIO(dev)) {
1534 sc->state |= PSM_ASLP;
1535 error = tsleep((caddr_t) sc, PCATCH, "psmrea", 0);
1536 sc->state &= ~PSM_ASLP;
1540 } else if ((sc->state & PSM_VALID) == 0) {
1541 /* the device disappeared! */
1548 /* copy data to the user land */
1549 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1551 l = min(sc->queue.count, uio->uio_resid);
1552 if (l > sizeof(buf))
1554 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1555 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1556 sizeof(sc->queue.buf) - sc->queue.head);
1557 bcopy(&sc->queue.buf[0],
1558 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1559 l - (sizeof(sc->queue.buf) - sc->queue.head));
1561 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1563 sc->queue.count -= l;
1564 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1566 error = uiomove(buf, l, uio);
1575 block_mouse_data(struct psm_softc *sc, int *c)
1579 if (!kbdc_lock(sc->kbdc, TRUE))
1583 *c = get_controller_command_byte(sc->kbdc);
1585 || !set_controller_command_byte(sc->kbdc,
1586 kbdc_get_device_mask(sc->kbdc),
1587 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1588 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1589 /* this is CONTROLLER ERROR */
1591 kbdc_lock(sc->kbdc, FALSE);
1596 * The device may be in the middle of status data transmission.
1597 * The transmission will be interrupted, thus, incomplete status
1598 * data must be discarded. Although the aux interrupt is disabled
1599 * at the keyboard controller level, at most one aux interrupt
1600 * may have already been pending and a data byte is in the
1601 * output buffer; throw it away. Note that the second argument
1602 * to `empty_aux_buffer()' is zero, so that the call will just
1603 * flush the internal queue.
1604 * `psmintr()' will be invoked after `splx()' if an interrupt is
1605 * pending; it will see no data and returns immediately.
1607 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1608 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1616 unblock_mouse_data(struct psm_softc *sc, int c)
1621 * We may have seen a part of status data during `set_mouse_XXX()'.
1622 * they have been queued; flush it.
1624 empty_aux_buffer(sc->kbdc, 0);
1626 /* restore ports and interrupt */
1627 if (!set_controller_command_byte(sc->kbdc,
1628 kbdc_get_device_mask(sc->kbdc),
1629 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1630 /* CONTROLLER ERROR; this is serious, we may have
1631 * been left with the inaccessible keyboard and
1632 * the disabled mouse interrupt.
1637 kbdc_lock(sc->kbdc, FALSE);
1642 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1644 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1646 mousestatus_t status;
1647 #if (defined(MOUSE_GETVARS))
1656 /* Perform IOCTL command */
1659 case OLD_MOUSE_GETHWINFO:
1661 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1662 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1663 ((old_mousehw_t *)addr)->type = sc->hw.type;
1664 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1668 case MOUSE_GETHWINFO:
1670 *(mousehw_t *)addr = sc->hw;
1671 if (sc->mode.level == PSM_LEVEL_BASE)
1672 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1676 case OLD_MOUSE_GETMODE:
1678 switch (sc->mode.level) {
1679 case PSM_LEVEL_BASE:
1680 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1682 case PSM_LEVEL_STANDARD:
1683 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1685 case PSM_LEVEL_NATIVE:
1686 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1689 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1690 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1691 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1697 *(mousemode_t *)addr = sc->mode;
1698 ((mousemode_t *)addr)->resolution =
1699 MOUSE_RES_LOW - sc->mode.resolution;
1700 switch (sc->mode.level) {
1701 case PSM_LEVEL_BASE:
1702 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1703 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1705 case PSM_LEVEL_STANDARD:
1706 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1707 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1708 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1709 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1711 case PSM_LEVEL_NATIVE:
1712 /* FIXME: this isn't quite correct... XXX */
1713 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1719 case OLD_MOUSE_SETMODE:
1721 if (cmd == OLD_MOUSE_SETMODE) {
1722 mode.rate = ((old_mousemode_t *)addr)->rate;
1724 * resolution old I/F new I/F
1731 if (((old_mousemode_t *)addr)->resolution > 0)
1732 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1733 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1736 mode = *(mousemode_t *)addr;
1739 /* adjust and validate parameters. */
1740 if (mode.rate > UCHAR_MAX)
1743 mode.rate = sc->dflt_mode.rate;
1744 else if (mode.rate == -1)
1745 /* don't change the current setting */
1747 else if (mode.rate < 0)
1749 if (mode.resolution >= UCHAR_MAX)
1751 if (mode.resolution >= 200)
1752 mode.resolution = MOUSE_RES_HIGH;
1753 else if (mode.resolution >= 100)
1754 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1755 else if (mode.resolution >= 50)
1756 mode.resolution = MOUSE_RES_MEDIUMLOW;
1757 else if (mode.resolution > 0)
1758 mode.resolution = MOUSE_RES_LOW;
1759 if (mode.resolution == MOUSE_RES_DEFAULT)
1760 mode.resolution = sc->dflt_mode.resolution;
1761 else if (mode.resolution == -1)
1762 /* don't change the current setting */
1764 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1765 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1766 if (mode.level == -1)
1767 /* don't change the current setting */
1768 mode.level = sc->mode.level;
1769 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1771 if (mode.accelfactor == -1)
1772 /* don't change the current setting */
1773 mode.accelfactor = sc->mode.accelfactor;
1774 else if (mode.accelfactor < 0)
1777 /* don't allow anybody to poll the keyboard controller */
1778 error = block_mouse_data(sc, &command_byte);
1782 /* set mouse parameters */
1784 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1785 if (mode.resolution >= 0)
1786 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1787 set_mouse_scaling(sc->kbdc, 1);
1788 get_mouse_status(sc->kbdc, stat, 0, 3);
1791 sc->mode.rate = mode.rate;
1792 sc->mode.resolution = mode.resolution;
1793 sc->mode.accelfactor = mode.accelfactor;
1794 sc->mode.level = mode.level;
1797 unblock_mouse_data(sc, command_byte);
1800 case MOUSE_GETLEVEL:
1801 *(int *)addr = sc->mode.level;
1804 case MOUSE_SETLEVEL:
1805 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1807 sc->mode.level = *(int *)addr;
1810 case MOUSE_GETSTATUS:
1812 status = sc->status;
1813 sc->status.flags = 0;
1814 sc->status.obutton = sc->status.button;
1815 sc->status.button = 0;
1820 *(mousestatus_t *)addr = status;
1823 #if (defined(MOUSE_GETVARS))
1825 var = (mousevar_t *)addr;
1826 bzero(var, sizeof(*var));
1828 var->var[0] = MOUSE_VARS_PS2_SIG;
1829 var->var[1] = sc->config;
1830 var->var[2] = sc->flags;
1836 #endif /* MOUSE_GETVARS */
1838 case MOUSE_READSTATE:
1839 case MOUSE_READDATA:
1840 data = (mousedata_t *)addr;
1841 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1844 error = block_mouse_data(sc, &command_byte);
1847 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1848 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1850 unblock_mouse_data(sc, command_byte);
1853 #if (defined(MOUSE_SETRESOLUTION))
1854 case MOUSE_SETRESOLUTION:
1855 mode.resolution = *(int *)addr;
1856 if (mode.resolution >= UCHAR_MAX)
1858 else if (mode.resolution >= 200)
1859 mode.resolution = MOUSE_RES_HIGH;
1860 else if (mode.resolution >= 100)
1861 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1862 else if (mode.resolution >= 50)
1863 mode.resolution = MOUSE_RES_MEDIUMLOW;
1864 else if (mode.resolution > 0)
1865 mode.resolution = MOUSE_RES_LOW;
1866 if (mode.resolution == MOUSE_RES_DEFAULT)
1867 mode.resolution = sc->dflt_mode.resolution;
1868 else if (mode.resolution == -1)
1869 mode.resolution = sc->mode.resolution;
1870 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1871 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1873 error = block_mouse_data(sc, &command_byte);
1876 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1877 if (sc->mode.resolution != mode.resolution)
1879 unblock_mouse_data(sc, command_byte);
1881 #endif /* MOUSE_SETRESOLUTION */
1883 #if (defined(MOUSE_SETRATE))
1885 mode.rate = *(int *)addr;
1886 if (mode.rate > UCHAR_MAX)
1889 mode.rate = sc->dflt_mode.rate;
1890 else if (mode.rate < 0)
1891 mode.rate = sc->mode.rate;
1893 error = block_mouse_data(sc, &command_byte);
1896 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1897 if (sc->mode.rate != mode.rate)
1899 unblock_mouse_data(sc, command_byte);
1901 #endif /* MOUSE_SETRATE */
1903 #if (defined(MOUSE_SETSCALING))
1904 case MOUSE_SETSCALING:
1905 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1908 error = block_mouse_data(sc, &command_byte);
1911 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1913 unblock_mouse_data(sc, command_byte);
1915 #endif /* MOUSE_SETSCALING */
1917 #if (defined(MOUSE_GETHWID))
1919 error = block_mouse_data(sc, &command_byte);
1922 sc->hw.hwid &= ~0x00ff;
1923 sc->hw.hwid |= get_aux_id(sc->kbdc);
1924 *(int *)addr = sc->hw.hwid & 0x00ff;
1925 unblock_mouse_data(sc, command_byte);
1927 #endif /* MOUSE_GETHWID */
1937 psmtimeout(void *arg)
1939 struct psm_softc *sc;
1942 sc = (struct psm_softc *)arg;
1944 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1946 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1948 kbdc_lock(sc->kbdc, FALSE);
1950 sc->watchdog = TRUE;
1952 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
1959 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
1960 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
1962 static int butmap[8] = {
1966 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1968 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1969 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1970 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1972 static int butmap_versapad[8] = {
1978 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1980 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
1982 struct psm_softc *sc = arg;
1990 /* read until there is nothing to read */
1991 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
1993 /* discard the byte if the device is not open */
1994 if ((sc->state & PSM_OPEN) == 0)
1997 getmicrouptime(&tv);
1998 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
1999 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2003 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2004 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2005 timevaladd(&sc->inputtimeout, &tv);
2007 sc->ipacket[sc->inputbytes++] = c;
2008 if (sc->inputbytes < sc->mode.packetsize)
2012 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2013 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
2014 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
2019 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2020 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
2021 c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
2023 if (sc->syncerrors < sc->mode.packetsize) {
2024 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2026 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2027 } else if (sc->syncerrors == sc->mode.packetsize) {
2028 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2030 disable_aux_dev(sc->kbdc);
2031 enable_aux_dev(sc->kbdc);
2032 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
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 >= PSM_SYNCERR_THRESHOLD1) {
2037 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2038 reinitialize(sc, TRUE);
2044 * A kludge for Kensington device!
2045 * The MSB of the horizontal count appears to be stored in
2048 if (sc->hw.model == MOUSE_MODEL_THINK)
2049 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2051 /* ignore the overflow bits... */
2052 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2053 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2055 ms.obutton = sc->button; /* previous button state */
2056 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2057 /* `tapping' action */
2058 if (sc->config & PSM_CONFIG_FORCETAP)
2059 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2061 switch (sc->hw.model) {
2063 case MOUSE_MODEL_EXPLORER:
2065 * b7 b6 b5 b4 b3 b2 b1 b0
2066 * byte 1: oy ox sy sx 1 M R L
2067 * byte 2: x x x x x x x x
2068 * byte 3: y y y y y y y y
2069 * byte 4: * * S2 S1 s d2 d1 d0
2071 * L, M, R, S1, S2: left, middle, right and side buttons
2072 * s: wheel data sign bit
2075 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2076 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
2077 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2078 ? MOUSE_BUTTON4DOWN : 0;
2079 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2080 ? MOUSE_BUTTON5DOWN : 0;
2083 case MOUSE_MODEL_INTELLI:
2084 case MOUSE_MODEL_NET:
2085 /* wheel data is in the fourth byte */
2086 z = (char)sc->ipacket[3];
2087 /* some mice may send 7 when there is no Z movement?! XXX */
2088 if ((z >= 7) || (z <= -7))
2090 /* some compatible mice have additional buttons */
2091 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2092 ? MOUSE_BUTTON4DOWN : 0;
2093 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2094 ? MOUSE_BUTTON5DOWN : 0;
2097 case MOUSE_MODEL_MOUSEMANPLUS:
2099 * PS2++ protocl packet
2101 * b7 b6 b5 b4 b3 b2 b1 b0
2102 * byte 1: * 1 p3 p2 1 * * *
2103 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2105 * p3-p0: packet type
2106 * c1, c2: c1 & c2 == 1, if p2 == 0
2107 * c1 & c2 == 0, if p2 == 1
2109 * packet type: 0 (device type)
2110 * See comments in enable_mmanplus() below.
2112 * packet type: 1 (wheel data)
2114 * b7 b6 b5 b4 b3 b2 b1 b0
2115 * byte 3: h * B5 B4 s d2 d1 d0
2117 * h: 1, if horizontal roller data
2118 * 0, if vertical roller data
2119 * B4, B5: button 4 and 5
2121 * d2-d0: roller data
2123 * packet type: 2 (reserved)
2125 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2127 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
2128 /* the extended data packet encodes button and wheel events */
2129 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
2131 /* wheel data packet */
2133 if (sc->ipacket[2] & 0x80) {
2134 /* horizontal roller count - ignore it XXX*/
2136 /* vertical roller count */
2137 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2138 ? (sc->ipacket[2] & 0x0f) - 16
2139 : (sc->ipacket[2] & 0x0f);
2141 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2142 ? MOUSE_BUTTON4DOWN : 0;
2143 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2144 ? MOUSE_BUTTON5DOWN : 0;
2147 /* this packet type is reserved by Logitech... */
2149 * IBM ScrollPoint Mouse uses this packet type to
2150 * encode both vertical and horizontal scroll movement.
2153 /* horizontal count */
2154 if (sc->ipacket[2] & 0x0f)
2155 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2156 /* vertical count */
2157 if (sc->ipacket[2] & 0xf0)
2158 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2160 /* vertical count */
2161 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
2162 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16
2163 : ((sc->ipacket[2] >> 4) & 0x0f);
2164 /* horizontal count */
2165 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
2166 ? (sc->ipacket[2] & 0x0f) - 16
2167 : (sc->ipacket[2] & 0x0f);
2171 /* device type packet - shouldn't happen */
2175 ms.button = ms.obutton;
2177 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2178 "0x%02x 0x%02x 0x%02x\n",
2179 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
2180 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
2184 /* preserve button states */
2185 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2189 case MOUSE_MODEL_GLIDEPOINT:
2190 /* `tapping' action */
2191 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2194 case MOUSE_MODEL_NETSCROLL:
2195 /* three addtional bytes encode buttons and wheel events */
2196 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2197 ? MOUSE_BUTTON4DOWN : 0;
2198 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2199 ? MOUSE_BUTTON5DOWN : 0;
2200 z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
2201 ? sc->ipacket[4] - 256 : sc->ipacket[4];
2204 case MOUSE_MODEL_THINK:
2205 /* the fourth button state in the first byte */
2206 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2209 case MOUSE_MODEL_VERSAPAD:
2210 /* VersaPad PS/2 absolute mode message format
2212 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2213 * ipacket[0]: 1 1 0 A 1 L T R
2214 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2215 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2216 * ipacket[3]: 1 1 1 A 1 L T R
2217 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2218 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2221 * R: right physical mouse button (1=on)
2222 * T: touch pad virtual button (1=tapping)
2223 * L: left physical mouse button (1=on)
2224 * A: position data is valid (1=valid)
2225 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2226 * V: vertical data (12bit signed integer. V11 is sign bit.)
2229 * Tapping is mapped to MOUSE_BUTTON4.
2231 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2232 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2234 if (c & MOUSE_PS2VERSA_IN_USE) {
2235 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
2236 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
2241 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2244 if (x < 0) /* XXX */
2253 sc->flags |= PSM_FLAGS_FINGERDOWN;
2258 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2260 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2261 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2264 case MOUSE_MODEL_4D:
2266 * b7 b6 b5 b4 b3 b2 b1 b0
2267 * byte 1: s2 d2 s1 d1 1 M R L
2268 * byte 2: sx x x x x x x x
2269 * byte 3: sy y y y y y y y
2271 * s1: wheel 1 direction
2273 * s2: wheel 2 direction
2276 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2277 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2278 switch (c & MOUSE_4D_WHEELBITS) {
2285 case 0x40: /* 2nd wheel turning right XXX */
2288 case 0xc0: /* 2nd wheel turning left XXX */
2294 case MOUSE_MODEL_4DPLUS:
2295 if ((x < 16 - 256) && (y < 16 - 256)) {
2297 * b7 b6 b5 b4 b3 b2 b1 b0
2298 * byte 1: 0 0 1 1 1 M R L
2299 * byte 2: 0 0 0 0 1 0 0 0
2300 * byte 3: 0 0 0 0 S s d1 d0
2302 * L, M, R, S: left, middle, right and side buttons
2303 * s: wheel data sign bit
2307 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2308 ms.button |= MOUSE_BUTTON4DOWN;
2309 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2310 ? ((sc->ipacket[2] & 0x07) - 8)
2311 : (sc->ipacket[2] & 0x07) ;
2313 /* preserve previous button states */
2314 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2318 case MOUSE_MODEL_GENERIC:
2324 if (sc->mode.accelfactor >= 1) {
2326 x = x * x / sc->mode.accelfactor;
2329 if (c & MOUSE_PS2_XNEG)
2333 y = y * y / sc->mode.accelfactor;
2336 if (c & MOUSE_PS2_YNEG)
2344 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2345 | (ms.obutton ^ ms.button);
2347 if (sc->mode.level < PSM_LEVEL_NATIVE)
2348 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
2350 sc->status.flags |= ms.flags;
2351 sc->status.dx += ms.dx;
2352 sc->status.dy += ms.dy;
2353 sc->status.dz += ms.dz;
2354 sc->status.button = ms.button;
2355 sc->button = ms.button;
2357 sc->watchdog = FALSE;
2360 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2361 l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2362 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2363 if (sc->inputbytes > l)
2364 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
2366 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
2367 sc->queue.count += sc->inputbytes;
2371 if (sc->state & PSM_ASLP) {
2372 sc->state &= ~PSM_ASLP;
2373 wakeup((caddr_t) sc);
2375 selwakeup(&sc->rsel);
2380 psmpoll(dev_t dev, int events, struct thread *td)
2382 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2386 /* Return true if a mouse event available */
2388 if (events & (POLLIN | POLLRDNORM)) {
2389 if (sc->queue.count > 0)
2390 revents |= events & (POLLIN | POLLRDNORM);
2392 selrecord(td, &sc->rsel);
2399 /* vendor/model specific routines */
2401 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2403 if (set_mouse_resolution(kbdc, res) != res)
2405 if (set_mouse_scaling(kbdc, scale)
2406 && set_mouse_scaling(kbdc, scale)
2407 && set_mouse_scaling(kbdc, scale)
2408 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2414 mouse_ext_command(KBDC kbdc, int command)
2418 c = (command >> 6) & 0x03;
2419 if (set_mouse_resolution(kbdc, c) != c)
2421 c = (command >> 4) & 0x03;
2422 if (set_mouse_resolution(kbdc, c) != c)
2424 c = (command >> 2) & 0x03;
2425 if (set_mouse_resolution(kbdc, c) != c)
2427 c = (command >> 0) & 0x03;
2428 if (set_mouse_resolution(kbdc, c) != c)
2434 /* Logitech MouseMan Cordless II */
2436 enable_lcordless(struct psm_softc *sc)
2441 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2443 if (status[1] == PSMD_RES_HIGH)
2445 ch = (status[0] & 0x07) - 1; /* channel # */
2446 if ((ch <= 0) || (ch > 4))
2449 * status[1]: always one?
2450 * status[2]: battery status? (0-100)
2456 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2458 enable_groller(struct psm_softc *sc)
2463 * The special sequence to enable the fourth button and the
2464 * roller. Immediately after this sequence check status bytes.
2465 * if the mouse is NetScroll, the second and the third bytes are
2470 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2471 * look like the following.
2473 * byte 1 bit 7 always 0
2474 * bit 6 stream mode (0)
2475 * bit 5 disabled (0)
2476 * bit 4 1:1 scaling (0)
2478 * bit 0-2 button status
2479 * byte 2 resolution (PSMD_RES_HIGH)
2480 * byte 3 report rate (?)
2483 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2485 if ((status[1] != '3') || (status[2] != 'D'))
2487 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2492 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2494 enable_gmouse(struct psm_softc *sc)
2499 * The special sequence to enable the middle, "rubber" button.
2500 * Immediately after this sequence check status bytes.
2501 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2502 * the second and the third bytes are '3' and 'U'.
2503 * NOTE: NetMouse reports that it has three buttons although it has
2504 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2505 * say they have three buttons too and they do have a button on the
2508 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2510 if ((status[1] != '3') || (status[2] != 'U'))
2515 /* ALPS GlidePoint */
2517 enable_aglide(struct psm_softc *sc)
2522 * The special sequence to obtain ALPS GlidePoint specific
2523 * information. Immediately after this sequence, status bytes will
2524 * contain something interesting.
2525 * NOTE: ALPS produces several models of GlidePoint. Some of those
2526 * do not respond to this sequence, thus, cannot be detected this way.
2528 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2530 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2532 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2537 /* Kensington ThinkingMouse/Trackball */
2539 enable_kmouse(struct psm_softc *sc)
2541 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2542 KBDC kbdc = sc->kbdc;
2548 id1 = get_aux_id(kbdc);
2549 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2552 * The device is now in the native mode? It returns a different
2555 id2 = get_aux_id(kbdc);
2556 if ((id1 == id2) || (id2 != 2))
2559 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2562 /* at this point, resolution is LOW, sampling rate is 10/sec */
2563 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2568 * The special sequence to enable the third and fourth buttons.
2569 * Otherwise they behave like the first and second buttons.
2571 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2572 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2577 * At this point, the device is using default resolution and
2578 * sampling rate for the native mode.
2580 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2582 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2585 /* the device appears be enabled by this sequence, diable it for now */
2586 disable_aux_dev(kbdc);
2587 empty_aux_buffer(kbdc, 5);
2592 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2594 enable_mmanplus(struct psm_softc *sc)
2596 KBDC kbdc = sc->kbdc;
2599 /* the special sequence to enable the fourth button and the roller. */
2601 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2602 * must be called exactly three times since the last RESET command
2603 * before this sequence. XXX
2605 if (!set_mouse_scaling(kbdc, 1))
2607 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2609 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2613 * PS2++ protocl, packet type 0
2615 * b7 b6 b5 b4 b3 b2 b1 b0
2616 * byte 1: * 1 p3 p2 1 * * *
2617 * byte 2: 1 1 p1 p0 m1 m0 1 0
2618 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2620 * p3-p0: packet type: 0
2621 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2623 /* check constant bits */
2624 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2626 if ((data[1] & 0xc3) != 0xc2)
2628 /* check d3-d0 in byte 2 */
2629 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2632 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2635 sc->hw.hwid &= 0x00ff;
2636 sc->hw.hwid |= data[2] << 8; /* save model ID */
2639 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2640 * the wheel and the fourth button events are encoded in the
2641 * special data packet. The mouse may be put in the IntelliMouse mode
2642 * if it is initialized by the IntelliMouse's method.
2647 /* MS IntelliMouse Explorer */
2649 enable_msexplorer(struct psm_softc *sc)
2651 static unsigned char rate0[] = { 200, 100, 80, };
2652 static unsigned char rate1[] = { 200, 200, 80, };
2653 KBDC kbdc = sc->kbdc;
2657 /* the special sequence to enable the extra buttons and the roller. */
2658 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2659 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2662 /* the device will give the genuine ID only after the above sequence */
2663 id = get_aux_id(kbdc);
2664 if (id != PSM_EXPLORER_ID)
2668 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2671 * XXX: this is a kludge to fool some KVM switch products
2672 * which think they are clever enough to know the 4-byte IntelliMouse
2673 * protocol, and assume any other protocols use 3-byte packets.
2674 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2675 * correctly to the host computer because of this!
2676 * The following sequence is actually IntelliMouse's "wake up"
2677 * sequence; it will make the KVM think the mouse is IntelliMouse
2678 * when it is in fact IntelliMouse Explorer.
2680 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2681 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2684 id = get_aux_id(kbdc);
2689 /* MS IntelliMouse */
2691 enable_msintelli(struct psm_softc *sc)
2694 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2695 * probe routine and act like IntelliMouse.
2698 static unsigned char rate[] = { 200, 100, 80, };
2699 KBDC kbdc = sc->kbdc;
2703 /* the special sequence to enable the third button and the roller. */
2704 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2705 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2708 /* the device will give the genuine ID only after the above sequence */
2709 id = get_aux_id(kbdc);
2710 if (id != PSM_INTELLI_ID)
2719 /* A4 Tech 4D Mouse */
2721 enable_4dmouse(struct psm_softc *sc)
2724 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2727 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2728 KBDC kbdc = sc->kbdc;
2732 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2733 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2736 id = get_aux_id(kbdc);
2738 * WinEasy 4D, 4 Way Scroll 4D: 6
2739 * Cable-Free 4D: 8 (4DPLUS)
2740 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2742 if (id != PSM_4DMOUSE_ID)
2746 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2751 /* A4 Tech 4D+ Mouse */
2753 enable_4dplus(struct psm_softc *sc)
2756 * Newer wheel mice from A4 Tech seem to use this protocol.
2757 * Older models are recognized as either 4D Mouse or IntelliMouse.
2759 KBDC kbdc = sc->kbdc;
2763 * enable_4dmouse() already issued the following ID sequence...
2764 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2767 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2768 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2773 id = get_aux_id(kbdc);
2774 if (id != PSM_4DPLUS_ID)
2778 sc->hw.buttons = 4; /* XXX */
2783 /* Interlink electronics VersaPad */
2785 enable_versapad(struct psm_softc *sc)
2787 KBDC kbdc = sc->kbdc;
2790 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2791 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2792 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2793 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2794 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2795 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2796 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2798 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2800 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2802 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2804 return TRUE; /* PS/2 absolute mode */
2808 psmresume(device_t dev)
2810 struct psm_softc *sc = device_get_softc(dev);
2811 int unit = device_get_unit(dev);
2815 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2817 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2820 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2822 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2824 * Release the blocked process; it must be notified that the device
2825 * cannot be accessed anymore.
2827 sc->state &= ~PSM_ASLP;
2828 wakeup((caddr_t)sc);
2832 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2837 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);