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.25 2007/06/13 17:15:25 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>
73 #include <sys/device.h>
74 #include <sys/event.h>
75 #include <sys/syslog.h>
76 #include <sys/malloc.h>
78 #include <sys/selinfo.h>
79 #include <sys/thread2.h>
83 #include <machine/clock.h>
84 #include <machine/limits.h>
85 #include <machine/mouse.h>
87 #include <bus/isa/isavar.h>
88 #include <dev/misc/kbd/atkbdcreg.h>
91 * Driver specific options: the following options may be set by
92 * `options' statements in the kernel configuration file.
97 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */
100 #ifndef PSM_SYNCERR_THRESHOLD1
101 #define PSM_SYNCERR_THRESHOLD1 20
104 #ifndef PSM_INPUT_TIMEOUT
105 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
108 /* end of driver specific options */
110 #define PSM_DRIVER_NAME "psm"
111 #define PSMCPNP_DRIVER_NAME "psmcpnp"
114 #define PSM_BUFSIZE 960
115 #define PSM_SMALLBUFSIZE 240
117 /* operation levels */
118 #define PSM_LEVEL_BASE 0
119 #define PSM_LEVEL_STANDARD 1
120 #define PSM_LEVEL_NATIVE 2
121 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
122 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
124 /* Logitech PS2++ protocol */
125 #define MOUSE_PS2PLUS_CHECKBITS(b) \
126 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
127 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
128 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
131 #define PSM_UNIT(dev) (minor(dev) >> 1)
132 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
133 #define PSM_MKMINOR(unit,block) ((((unit) & 0xff) << 1) | ((block) ? 0:1))
136 #define max(x,y) ((x) > (y) ? (x) : (y))
139 #define min(x,y) ((x) < (y) ? (x) : (y))
142 #define abs(x) (((x) < 0) ? -(x) : (x))
145 typedef struct ringbuf {
146 int count; /* # of valid elements in the buffer */
147 int head; /* head pointer */
148 int tail; /* tail poiner */
149 unsigned char buf[PSM_BUFSIZE];
152 /* driver control block */
153 struct psm_softc { /* Driver status information */
155 struct selinfo rsel; /* Process selecting for Input */
156 unsigned char state; /* Mouse driver state */
157 int config; /* driver configuration flags */
158 int flags; /* other flags */
159 KBDC kbdc; /* handle to access the keyboard controller */
160 struct resource *intr; /* IRQ resource */
161 void *ih; /* interrupt handle */
162 mousehw_t hw; /* hardware information */
163 mousemode_t mode; /* operation mode */
164 mousemode_t dflt_mode; /* default operation mode */
165 mousestatus_t status; /* accumulated mouse movement */
166 ringbuf_t queue; /* mouse status queue */
167 unsigned char ipacket[16]; /* interim input buffer */
168 int inputbytes; /* # of bytes in the input buffer */
169 int button; /* the latest button state */
170 int xold; /* previous absolute X position */
171 int yold; /* previous absolute Y position */
173 struct timeval inputtimeout;
174 int watchdog; /* watchdog timer flag */
175 struct callout callout; /* watchdog timer call out */
177 devclass_t psm_devclass;
178 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
180 /* driver state flags (state) */
181 #define PSM_VALID 0x80
182 #define PSM_OPEN 1 /* Device is open */
183 #define PSM_ASLP 2 /* Waiting for mouse data */
185 /* driver configuration flags (config) */
186 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
187 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
188 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
189 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
190 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
191 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
192 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
193 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
194 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
195 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
197 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \
199 | PSM_CONFIG_NOCHECKSYNC \
200 | PSM_CONFIG_SYNCHACK \
201 | PSM_CONFIG_NOIDPROBE \
202 | PSM_CONFIG_NORESET \
203 | PSM_CONFIG_FORCETAP \
204 | PSM_CONFIG_IGNPORTERROR \
205 | PSM_CONFIG_HOOKRESUME \
206 | PSM_CONFIG_INITAFTERSUSPEND)
208 /* other flags (flags) */
209 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
211 /* for backward compatibility */
212 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
213 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
214 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
216 typedef struct old_mousehw {
223 typedef struct old_mousemode {
230 /* packet formatting function */
231 typedef int packetfunc_t (struct psm_softc *, unsigned char *,
232 int *, int, mousestatus_t *);
234 /* function prototypes */
235 static int psmprobe (device_t);
236 static int psmattach (device_t);
237 static int psmdetach (device_t);
238 static int psmresume (device_t);
240 static d_open_t psmopen;
241 static d_close_t psmclose;
242 static d_read_t psmread;
243 static d_ioctl_t psmioctl;
244 static d_kqfilter_t psmkqfilter;
246 static int enable_aux_dev (KBDC);
247 static int disable_aux_dev (KBDC);
248 static int get_mouse_status (KBDC, int *, int, int);
249 static int get_aux_id (KBDC);
250 static int set_mouse_sampling_rate (KBDC, int);
251 static int set_mouse_scaling (KBDC, int);
252 static int set_mouse_resolution (KBDC, int);
253 static int set_mouse_mode (KBDC);
254 static int get_mouse_buttons (KBDC);
255 static int is_a_mouse (int);
256 static void recover_from_error (KBDC);
257 static int restore_controller (KBDC, int);
258 static int doinitialize (struct psm_softc *, mousemode_t *);
259 static int doopen (struct psm_softc *, int);
260 static int reinitialize (struct psm_softc *, int);
261 static char *model_name (int);
262 static void psmintr (void *);
263 static void psmtimeout (void *);
264 static void psmfilter_detach(struct knote *);
265 static int psmfilter(struct knote *, long);
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 dev_ops psm_ops = {
347 { PSM_DRIVER_NAME, CDEV_MAJOR, D_KQFILTER },
352 .d_kqfilter = psmkqfilter
355 /* debug message level */
356 static int verbose = PSM_DEBUG;
358 /* device I/O routines */
360 enable_aux_dev(KBDC kbdc)
364 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
366 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
368 return (res == PSM_ACK);
372 disable_aux_dev(KBDC kbdc)
376 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
378 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
380 return (res == PSM_ACK);
384 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
393 cmd = PSMC_SEND_DEV_STATUS;
396 cmd = PSMC_SEND_DEV_DATA;
399 empty_aux_buffer(kbdc, 5);
400 res = send_aux_command(kbdc, cmd);
402 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
403 (flag == 1) ? "DATA" : "STATUS", res);
407 for (i = 0; i < len; ++i) {
408 status[i] = read_aux_data(kbdc);
414 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
415 (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
422 get_aux_id(KBDC kbdc)
427 empty_aux_buffer(kbdc, 5);
428 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
430 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
437 id = read_aux_data(kbdc);
439 log(LOG_DEBUG, "psm: device ID: %04x\n", id);
445 set_mouse_sampling_rate(KBDC kbdc, int rate)
449 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
451 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
453 return ((res == PSM_ACK) ? rate : -1);
457 set_mouse_scaling(KBDC kbdc, int scale)
464 scale = PSMC_SET_SCALING11;
467 scale = PSMC_SET_SCALING21;
470 res = send_aux_command(kbdc, scale);
472 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
473 (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
475 return (res == PSM_ACK);
478 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
480 set_mouse_resolution(KBDC kbdc, int val)
484 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
486 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
488 return ((res == PSM_ACK) ? val : -1);
492 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
493 * re-enabled by calling `enable_aux_dev()'
496 set_mouse_mode(KBDC kbdc)
500 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
502 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
504 return (res == PSM_ACK);
508 get_mouse_buttons(KBDC kbdc)
510 int c = 2; /* assume two buttons by default */
514 * NOTE: a special sequence to obtain Logitech Mouse specific
515 * information: set resolution to 25 ppi, set scaling to 1:1, set
516 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
517 * mouse status bytes is the number of available buttons.
518 * Some manufactures also support this sequence.
520 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
522 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
523 && set_mouse_scaling(kbdc, 1)
524 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
531 /* misc subroutines */
533 * Someday, I will get the complete list of valid pointing devices and
540 static int valid_ids[] = {
541 PSM_MOUSE_ID, /* mouse */
542 PSM_BALLPOINT_ID, /* ballpoint device */
543 PSM_INTELLI_ID, /* Intellimouse */
544 PSM_EXPLORER_ID, /* Intellimouse Explorer */
545 -1 /* end of table */
549 for (i = 0; valid_ids[i] >= 0; ++i)
550 if (valid_ids[i] == id)
559 model_name(int model)
565 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
566 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
567 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
568 { MOUSE_MODEL_THINK, "ThinkingMouse" },
569 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
570 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
571 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
572 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
573 { MOUSE_MODEL_4D, "4D Mouse" },
574 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
575 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
576 { MOUSE_MODEL_UNKNOWN, NULL },
580 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
581 if (models[i].model_code == model)
582 return models[i].model_name;
588 recover_from_error(KBDC kbdc)
590 /* discard anything left in the output buffer */
591 empty_both_buffers(kbdc, 10);
595 * NOTE: KBDC_RESET_KBD may not restore the communication between the
596 * keyboard and the controller.
601 * NOTE: somehow diagnostic and keyboard port test commands bring the
604 if (!test_controller(kbdc))
605 log(LOG_ERR, "psm: keyboard controller failed.\n");
606 /* if there isn't a keyboard in the system, the following error is OK */
607 if (test_kbd_port(kbdc) != 0) {
609 log(LOG_ERR, "psm: keyboard port failed.\n");
615 restore_controller(KBDC kbdc, int command_byte)
617 empty_both_buffers(kbdc, 10);
619 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
620 log(LOG_ERR, "psm: failed to restore the keyboard controller "
622 empty_both_buffers(kbdc, 10);
625 empty_both_buffers(kbdc, 10);
631 * Re-initialize the aux port and device. The aux port must be enabled
632 * and its interrupt must be disabled before calling this routine.
633 * The aux device will be disabled before returning.
634 * The keyboard controller must be locked via `kbdc_lock()' before
635 * calling this routine.
638 doinitialize(struct psm_softc *sc, mousemode_t *mode)
640 KBDC kbdc = sc->kbdc;
644 switch((i = test_aux_port(kbdc))) {
645 case 1: /* ignore this error */
646 case 2: /* Ignore 2 and 3 for use with some acer and compal laptops */
650 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
653 case 0: /* no error */
655 case -1: /* time out */
657 recover_from_error(kbdc);
658 if (sc->config & PSM_CONFIG_IGNPORTERROR)
660 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
665 if (sc->config & PSM_CONFIG_NORESET) {
667 * Don't try to reset the pointing device. It may possibly be
668 * left in the unknown state, though...
672 * NOTE: some controllers appears to hang the `keyboard' when
673 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
675 if (!reset_aux_dev(kbdc)) {
676 recover_from_error(kbdc);
677 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
683 * both the aux port and the aux device is functioning, see
684 * if the device can be enabled.
686 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
687 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
690 empty_both_buffers(kbdc, 10); /* remove stray data if any */
692 if (sc->config & PSM_CONFIG_NOIDPROBE) {
693 i = GENERIC_MOUSE_ENTRY;
695 /* FIXME: hardware ID, mouse buttons? */
697 /* other parameters */
698 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
699 if ((*vendortype[i].probefunc)(sc)) {
701 log(LOG_ERR, "psm%d: found %s\n",
702 sc->unit, model_name(vendortype[i].model));
708 sc->hw.model = vendortype[i].model;
709 sc->mode.packetsize = vendortype[i].packetsize;
711 /* set mouse parameters */
714 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
715 if (mode->resolution >= 0)
716 mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
717 set_mouse_scaling(kbdc, 1);
718 set_mouse_mode(kbdc);
721 /* request a data packet and extract sync. bits */
722 if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
723 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
725 sc->mode.syncmask[0] = 0;
727 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
728 /* the NetScroll Mouse will send three more bytes... Ignore them */
729 empty_aux_buffer(kbdc, 5);
732 /* just check the status of the mouse */
733 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
734 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
741 doopen(struct psm_softc *sc, int command_byte)
745 /* enable the mouse device */
746 if (!enable_aux_dev(sc->kbdc)) {
747 /* MOUSE ERROR: failed to enable the mouse because:
748 * 1) the mouse is faulty,
749 * 2) the mouse has been removed(!?)
750 * In the latter case, the keyboard may have hung, and need
751 * recovery procedure...
753 recover_from_error(sc->kbdc);
755 /* FIXME: we could reset the mouse here and try to enable
756 * it again. But it will take long time and it's not a good
757 * idea to disable the keyboard that long...
759 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
760 recover_from_error(sc->kbdc);
764 restore_controller(sc->kbdc, command_byte);
765 /* mark this device is no longer available */
766 sc->state &= ~PSM_VALID;
767 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
773 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
774 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
776 /* enable the aux port and interrupt */
777 if (!set_controller_command_byte(sc->kbdc,
778 kbdc_get_device_mask(sc->kbdc),
779 (command_byte & KBD_KBD_CONTROL_BITS)
780 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
781 /* CONTROLLER ERROR */
782 disable_aux_dev(sc->kbdc);
783 restore_controller(sc->kbdc, command_byte);
784 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
789 /* start the watchdog timer */
790 sc->watchdog = FALSE;
791 callout_reset(&sc->callout, hz * 2, psmtimeout, (void *)(uintptr_t)sc);
797 reinitialize(struct psm_softc *sc, int doinit)
802 /* don't let anybody mess with the aux device */
803 if (!kbdc_lock(sc->kbdc, TRUE))
807 /* block our watchdog timer */
808 sc->watchdog = FALSE;
809 callout_stop(&sc->callout);
811 /* save the current controller command byte */
812 empty_both_buffers(sc->kbdc, 10);
813 c = get_controller_command_byte(sc->kbdc);
815 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
818 /* enable the aux port but disable the aux interrupt and the keyboard */
819 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
820 kbdc_get_device_mask(sc->kbdc),
821 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
822 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
823 /* CONTROLLER ERROR */
825 kbdc_lock(sc->kbdc, FALSE);
826 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
832 if (sc->state & PSM_VALID) {
833 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */
834 empty_aux_buffer(sc->kbdc, 10);
839 /* try to detect the aux device; are you still there? */
842 if (doinitialize(sc, &sc->mode)) {
844 sc->state |= PSM_VALID;
846 /* the device has gone! */
847 restore_controller(sc->kbdc, c);
848 sc->state &= ~PSM_VALID;
849 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
856 /* restore the driver state */
857 if ((sc->state & PSM_OPEN) && (err == 0)) {
858 /* enable the aux device and the port again */
861 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
864 /* restore the keyboard port and disable the aux port */
865 if (!set_controller_command_byte(sc->kbdc,
866 kbdc_get_device_mask(sc->kbdc),
867 (c & KBD_KBD_CONTROL_BITS)
868 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
869 /* CONTROLLER ERROR */
870 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
876 kbdc_lock(sc->kbdc, FALSE);
880 /* psm driver entry points */
882 #define endprobe(v) { if (bootverbose) \
884 kbdc_set_device_mask(sc->kbdc, mask); \
885 kbdc_lock(sc->kbdc, FALSE); \
890 psmprobe(device_t dev)
892 int unit = device_get_unit(dev);
893 struct psm_softc *sc = device_get_softc(dev);
908 if (XXX_PNP_PROBE(device_get_parent(dev), dev, psm_ids) == ENXIO)
912 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
913 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
916 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
917 sc->config = flags & PSM_CONFIG_FLAGS;
918 /* XXX: for backward compatibility */
919 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
921 #ifdef PSM_RESETAFTERSUSPEND
922 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
924 PSM_CONFIG_HOOKRESUME;
926 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
931 device_set_desc(dev, "PS/2 Mouse");
933 if (!kbdc_lock(sc->kbdc, TRUE)) {
934 kprintf("psm%d: unable to lock the controller.\n", unit);
941 * NOTE: two bits in the command byte controls the operation of the
942 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
943 * port interrupt (IRQ 12) enable bit (bit 2).
946 /* discard anything left after the keyboard initialization */
947 empty_both_buffers(sc->kbdc, 10);
949 /* save the current command byte; it will be used later */
950 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
951 command_byte = get_controller_command_byte(sc->kbdc);
953 kprintf("psm%d: current command byte:%04x\n", unit, command_byte);
954 if (command_byte == -1) {
955 /* CONTROLLER ERROR */
956 kprintf("psm%d: unable to get the current command byte value.\n",
962 * disable the keyboard port while probing the aux port, which must be
963 * enabled during this routine
965 if (!set_controller_command_byte(sc->kbdc,
966 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
967 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
968 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
970 * this is CONTROLLER ERROR; I don't know how to recover
973 restore_controller(sc->kbdc, command_byte);
974 kprintf("psm%d: unable to set the command byte.\n", unit);
977 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
980 * NOTE: `test_aux_port()' is designed to return with zero if the aux
981 * port exists and is functioning. However, some controllers appears
982 * to respond with zero even when the aux port doesn't exist. (It may
983 * be that this is only the case when the controller DOES have the aux
984 * port but the port is not wired on the motherboard.) The keyboard
985 * controllers without the port, such as the original AT, are
986 * supporsed to return with an error code or simply time out. In any
987 * case, we have to continue probing the port even when the controller
990 * XXX: some controllers erroneously return the error code 1 when
991 * it has the perfectly functional aux port. We have to ignore this
992 * error code. Even if the controller HAS error with the aux port,
993 * it will be detected later...
994 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
996 switch ((i = test_aux_port(sc->kbdc))) {
997 case 1: /* ignore this error */
998 case 2: /* Ignore 2 and 3 for use with some acer and compal laptops */
1002 kprintf("psm%d: strange result for test aux port (%d).\n",
1005 case 0: /* no error */
1007 case -1: /* time out */
1008 default: /* error */
1009 recover_from_error(sc->kbdc);
1010 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1012 restore_controller(sc->kbdc, command_byte);
1014 kprintf("psm%d: the aux port is not functioning (%d).\n",
1019 if (sc->config & PSM_CONFIG_NORESET) {
1021 * Don't try to reset the pointing device. It may possibly be
1022 * left in the unknown state, though...
1026 * NOTE: some controllers appears to hang the `keyboard' when the aux
1027 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1029 * Attempt to reset the controller twice -- this helps
1030 * pierce through some KVM switches. The second reset
1033 if (!reset_aux_dev(sc->kbdc)) {
1034 recover_from_error(sc->kbdc);
1035 restore_controller(sc->kbdc, command_byte);
1037 kprintf("psm%d: failed to reset the aux device.\n", unit);
1039 } else if (!reset_aux_dev(sc->kbdc)) {
1040 recover_from_error(sc->kbdc);
1042 kprintf("psm%d: failed to reset the aux device (2).\n",
1048 * both the aux port and the aux device is functioning, see if the
1049 * device can be enabled. NOTE: when enabled, the device will start
1050 * sending data; we shall immediately disable the device once we know
1051 * the device can be enabled.
1053 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1055 recover_from_error(sc->kbdc);
1056 restore_controller(sc->kbdc, command_byte);
1058 kprintf("psm%d: failed to enable the aux device.\n", unit);
1062 /* save the default values after reset */
1063 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1064 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1065 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1067 sc->dflt_mode.rate = sc->mode.rate = -1;
1068 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1071 /* hardware information */
1072 sc->hw.iftype = MOUSE_IF_PS2;
1074 /* verify the device is a mouse */
1075 sc->hw.hwid = get_aux_id(sc->kbdc);
1076 if (!is_a_mouse(sc->hw.hwid)) {
1077 restore_controller(sc->kbdc, command_byte);
1079 kprintf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1082 switch (sc->hw.hwid) {
1083 case PSM_BALLPOINT_ID:
1084 sc->hw.type = MOUSE_TRACKBALL;
1087 case PSM_INTELLI_ID:
1088 case PSM_EXPLORER_ID:
1089 case PSM_4DMOUSE_ID:
1091 sc->hw.type = MOUSE_MOUSE;
1094 sc->hw.type = MOUSE_UNKNOWN;
1098 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1100 i = GENERIC_MOUSE_ENTRY;
1103 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1105 /* other parameters */
1106 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1107 if ((*vendortype[i].probefunc)(sc)) {
1109 kprintf("psm%d: found %s\n",
1110 unit, model_name(vendortype[i].model));
1116 sc->hw.model = vendortype[i].model;
1118 sc->dflt_mode.level = PSM_LEVEL_BASE;
1119 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1120 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1121 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1122 sc->dflt_mode.syncmask[0] = 0;
1124 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1125 if (sc->config & PSM_CONFIG_FORCETAP)
1126 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1127 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1128 sc->mode = sc->dflt_mode;
1129 sc->mode.packetsize = vendortype[i].packetsize;
1131 /* set mouse parameters */
1134 * A version of Logitech FirstMouse+ won't report wheel movement,
1135 * if SET_DEFAULTS is sent... Don't use this command.
1136 * This fix was found by Takashi Nishida.
1138 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1140 kprintf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1142 if (sc->config & PSM_CONFIG_RESOLUTION) {
1144 = set_mouse_resolution(sc->kbdc,
1145 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1146 } else if (sc->mode.resolution >= 0) {
1148 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1150 if (sc->mode.rate > 0) {
1151 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1153 set_mouse_scaling(sc->kbdc, 1);
1155 /* request a data packet and extract sync. bits */
1156 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1157 kprintf("psm%d: failed to get data.\n", unit);
1158 sc->mode.syncmask[0] = 0;
1160 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1161 /* the NetScroll Mouse will send three more bytes... Ignore them */
1162 empty_aux_buffer(sc->kbdc, 5);
1165 /* just check the status of the mouse */
1167 * NOTE: XXX there are some arcane controller/mouse combinations out
1168 * there, which hung the controller unless there is data transmission
1169 * after ACK from the mouse.
1171 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1172 kprintf("psm%d: failed to get status.\n", unit);
1175 * When in its native mode, some mice operate with different
1176 * default parameters than in the PS/2 compatible mode.
1178 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1179 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1182 /* disable the aux port for now... */
1183 if (!set_controller_command_byte(sc->kbdc,
1184 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1185 (command_byte & KBD_KBD_CONTROL_BITS)
1186 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1188 * this is CONTROLLER ERROR; I don't know the proper way to
1189 * recover from this error...
1191 restore_controller(sc->kbdc, command_byte);
1192 kprintf("psm%d: unable to set the command byte.\n", unit);
1196 /* see if IRQ is available */
1198 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1200 if (sc->intr == NULL) {
1201 kprintf("psm%d: unable to allocate the IRQ resource (%d).\n",
1205 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1209 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1210 kbdc_lock(sc->kbdc, FALSE);
1215 psmattach(device_t dev)
1217 int unit = device_get_unit(dev);
1218 struct psm_softc *sc = device_get_softc(dev);
1223 if (sc == NULL) /* shouldn't happen */
1226 /* Setup initial state */
1227 sc->state = PSM_VALID;
1228 callout_init(&sc->callout);
1230 /* Setup our interrupt handler */
1232 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1233 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
1235 if (sc->intr == NULL)
1237 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
1238 INTR_NOPOLL, psmintr, sc, &sc->ih, NULL);
1240 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1245 make_dev(&psm_ops, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, "psm%d", unit);
1246 make_dev(&psm_ops, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, "bpsm%d", unit);
1249 kprintf("psm%d: model %s, device ID %d\n",
1250 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1252 kprintf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1253 unit, model_name(sc->hw.model),
1254 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1255 kprintf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1256 unit, sc->config, sc->flags, sc->mode.packetsize);
1257 kprintf("psm%d: syncmask:%02x, syncbits:%02x\n",
1258 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1268 psmdetach(device_t dev)
1270 struct psm_softc *sc;
1274 sc = device_get_softc(dev);
1275 if (sc->state & PSM_OPEN)
1278 unit = device_get_unit(dev);
1281 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->intr, sc->ih);
1282 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1283 kprintf("devfs: Please make sure that only the right psm device was removed!!!!\n");
1284 dev_ops_remove_minor(&psm_ops, /*PSM_MKMINOR(-1, 0), */PSM_MKMINOR(unit, 0));
1290 psmopen(struct dev_open_args *ap)
1292 cdev_t dev = ap->a_head.a_dev;
1293 int unit = PSM_UNIT(dev);
1294 struct psm_softc *sc;
1298 /* Get device data */
1299 sc = PSM_SOFTC(unit);
1300 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1301 /* the device is no longer valid/functioning */
1304 /* Disallow multiple opens */
1305 if (sc->state & PSM_OPEN)
1309 device_busy(devclass_get_device(psm_devclass, unit));
1312 /* Initialize state */
1313 sc->rsel.si_flags = 0;
1314 sc->rsel.si_pid = 0;
1315 sc->mode.level = sc->dflt_mode.level;
1316 sc->mode.protocol = sc->dflt_mode.protocol;
1317 sc->watchdog = FALSE;
1319 /* flush the event queue */
1320 sc->queue.count = 0;
1323 sc->status.flags = 0;
1324 sc->status.button = 0;
1325 sc->status.obutton = 0;
1331 /* empty input buffer */
1332 bzero(sc->ipacket, sizeof(sc->ipacket));
1336 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1337 if (!kbdc_lock(sc->kbdc, TRUE))
1340 /* save the current controller command byte */
1342 command_byte = get_controller_command_byte(sc->kbdc);
1344 /* enable the aux port and temporalily disable the keyboard */
1345 if ((command_byte == -1)
1346 || !set_controller_command_byte(sc->kbdc,
1347 kbdc_get_device_mask(sc->kbdc),
1348 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1349 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1350 /* CONTROLLER ERROR; do you know how to get out of this? */
1351 kbdc_lock(sc->kbdc, FALSE);
1353 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1358 * Now that the keyboard controller is told not to generate
1359 * the keyboard and mouse interrupts, call `splx()' to allow
1360 * the other tty interrupts. The clock interrupt may also occur,
1361 * but timeout routines will be blocked by the poll flag set
1366 /* enable the mouse device */
1367 err = doopen(sc, command_byte);
1371 sc->state |= PSM_OPEN;
1372 kbdc_lock(sc->kbdc, FALSE);
1377 psmclose(struct dev_close_args *ap)
1379 cdev_t dev = ap->a_head.a_dev;
1380 int unit = PSM_UNIT(dev);
1381 struct psm_softc *sc = PSM_SOFTC(unit);
1385 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1386 if (!kbdc_lock(sc->kbdc, TRUE))
1389 /* save the current controller command byte */
1391 command_byte = get_controller_command_byte(sc->kbdc);
1392 if (command_byte == -1) {
1393 kbdc_lock(sc->kbdc, FALSE);
1398 /* disable the aux interrupt and temporalily disable the keyboard */
1399 if (!set_controller_command_byte(sc->kbdc,
1400 kbdc_get_device_mask(sc->kbdc),
1401 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1402 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1403 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1405 /* CONTROLLER ERROR;
1406 * NOTE: we shall force our way through. Because the only
1407 * ill effect we shall see is that we may not be able
1408 * to read ACK from the mouse, and it doesn't matter much
1409 * so long as the mouse will accept the DISABLE command.
1414 /* stop the watchdog timer */
1415 callout_stop(&sc->callout);
1417 /* remove anything left in the output buffer */
1418 empty_aux_buffer(sc->kbdc, 10);
1420 /* disable the aux device, port and interrupt */
1421 if (sc->state & PSM_VALID) {
1422 if (!disable_aux_dev(sc->kbdc)) {
1424 * NOTE: we don't return error and continue, pretending
1425 * we have successfully disabled the device. It's OK because
1426 * the interrupt routine will discard any data from the mouse
1429 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1433 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1434 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1438 if (!set_controller_command_byte(sc->kbdc,
1439 kbdc_get_device_mask(sc->kbdc),
1440 (command_byte & KBD_KBD_CONTROL_BITS)
1441 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1442 /* CONTROLLER ERROR;
1443 * we shall ignore this error; see the above comment.
1445 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1449 /* remove anything left in the output buffer */
1450 empty_aux_buffer(sc->kbdc, 10);
1452 /* close is almost always successful */
1453 sc->state &= ~PSM_OPEN;
1454 kbdc_lock(sc->kbdc, FALSE);
1456 device_unbusy(devclass_get_device(psm_devclass, unit));
1462 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
1464 static unsigned char butmapps2[8] = {
1466 MOUSE_PS2_BUTTON1DOWN,
1467 MOUSE_PS2_BUTTON2DOWN,
1468 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1469 MOUSE_PS2_BUTTON3DOWN,
1470 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1471 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1472 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1474 static unsigned char butmapmsc[8] = {
1475 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1476 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1477 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1478 MOUSE_MSC_BUTTON3UP,
1479 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1480 MOUSE_MSC_BUTTON2UP,
1481 MOUSE_MSC_BUTTON1UP,
1487 if (sc->mode.level == PSM_LEVEL_BASE) {
1488 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1489 if (status->button & MOUSE_BUTTON4DOWN)
1490 mapped |= MOUSE_BUTTON1DOWN;
1491 status->button = mapped;
1492 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1493 i = max(min(status->dx, 255), -256);
1495 buf[0] |= MOUSE_PS2_XNEG;
1497 i = max(min(status->dy, 255), -256);
1499 buf[0] |= MOUSE_PS2_YNEG;
1501 return MOUSE_PS2_PACKETSIZE;
1502 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1503 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1504 i = max(min(status->dx, 255), -256);
1506 buf[3] = i - buf[1];
1507 i = max(min(status->dy, 255), -256);
1509 buf[4] = i - buf[2];
1510 i = max(min(status->dz, 127), -128);
1511 buf[5] = (i >> 1) & 0x7f;
1512 buf[6] = (i - (i >> 1)) & 0x7f;
1513 buf[7] = (~status->button >> 3) & 0x7f;
1514 return MOUSE_SYS_PACKETSIZE;
1516 return sc->inputbytes;
1520 psmread(struct dev_read_args *ap)
1522 cdev_t dev = ap->a_head.a_dev;
1523 struct uio *uio = ap->a_uio;
1524 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1525 unsigned char buf[PSM_SMALLBUFSIZE];
1529 if ((sc->state & PSM_VALID) == 0)
1532 /* block until mouse activity occured */
1534 while (sc->queue.count <= 0) {
1535 if (PSM_NBLOCKIO(dev)) {
1539 sc->state |= PSM_ASLP;
1540 error = tsleep((caddr_t) sc, PCATCH, "psmrea", 0);
1541 sc->state &= ~PSM_ASLP;
1545 } else if ((sc->state & PSM_VALID) == 0) {
1546 /* the device disappeared! */
1553 /* copy data to the user land */
1554 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1556 l = (int)szmin(sc->queue.count, uio->uio_resid);
1557 if (l > sizeof(buf))
1559 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1560 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1561 sizeof(sc->queue.buf) - sc->queue.head);
1562 bcopy(&sc->queue.buf[0],
1563 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1564 l - (sizeof(sc->queue.buf) - sc->queue.head));
1566 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1568 sc->queue.count -= l;
1569 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1571 error = uiomove(buf, (size_t)l, uio);
1580 block_mouse_data(struct psm_softc *sc, int *c)
1582 if (!kbdc_lock(sc->kbdc, TRUE))
1586 *c = get_controller_command_byte(sc->kbdc);
1588 || !set_controller_command_byte(sc->kbdc,
1589 kbdc_get_device_mask(sc->kbdc),
1590 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1591 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1592 /* this is CONTROLLER ERROR */
1594 kbdc_lock(sc->kbdc, FALSE);
1599 * The device may be in the middle of status data transmission.
1600 * The transmission will be interrupted, thus, incomplete status
1601 * data must be discarded. Although the aux interrupt is disabled
1602 * at the keyboard controller level, at most one aux interrupt
1603 * may have already been pending and a data byte is in the
1604 * output buffer; throw it away. Note that the second argument
1605 * to `empty_aux_buffer()' is zero, so that the call will just
1606 * flush the internal queue.
1607 * `psmintr()' will be invoked after `splx()' if an interrupt is
1608 * pending; it will see no data and returns immediately.
1610 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1611 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1619 unblock_mouse_data(struct psm_softc *sc, int c)
1624 * We may have seen a part of status data during `set_mouse_XXX()'.
1625 * they have been queued; flush it.
1627 empty_aux_buffer(sc->kbdc, 0);
1629 /* restore ports and interrupt */
1630 if (!set_controller_command_byte(sc->kbdc,
1631 kbdc_get_device_mask(sc->kbdc),
1632 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1633 /* CONTROLLER ERROR; this is serious, we may have
1634 * been left with the inaccessible keyboard and
1635 * the disabled mouse interrupt.
1640 kbdc_lock(sc->kbdc, FALSE);
1645 psmioctl(struct dev_ioctl_args *ap)
1647 cdev_t dev = ap->a_head.a_dev;
1648 caddr_t addr= ap->a_data;
1649 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1651 mousestatus_t status;
1652 #if (defined(MOUSE_GETVARS))
1660 mode.resolution = -1;
1662 /* Perform IOCTL command */
1664 switch (ap->a_cmd) {
1665 case OLD_MOUSE_GETHWINFO:
1667 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1668 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1669 ((old_mousehw_t *)addr)->type = sc->hw.type;
1670 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1674 case MOUSE_GETHWINFO:
1676 *(mousehw_t *)addr = sc->hw;
1677 if (sc->mode.level == PSM_LEVEL_BASE)
1678 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1682 case OLD_MOUSE_GETMODE:
1684 switch (sc->mode.level) {
1685 case PSM_LEVEL_BASE:
1686 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1688 case PSM_LEVEL_STANDARD:
1689 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1691 case PSM_LEVEL_NATIVE:
1692 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1695 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1696 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1697 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1703 *(mousemode_t *)addr = sc->mode;
1704 ((mousemode_t *)addr)->resolution =
1705 MOUSE_RES_LOW - sc->mode.resolution;
1706 switch (sc->mode.level) {
1707 case PSM_LEVEL_BASE:
1708 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1709 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1711 case PSM_LEVEL_STANDARD:
1712 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1713 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1714 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1715 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1717 case PSM_LEVEL_NATIVE:
1718 /* FIXME: this isn't quite correct... XXX */
1719 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1725 case OLD_MOUSE_SETMODE:
1727 if (ap->a_cmd == OLD_MOUSE_SETMODE) {
1728 mode.rate = ((old_mousemode_t *)addr)->rate;
1730 * resolution old I/F new I/F
1737 if (((old_mousemode_t *)addr)->resolution > 0)
1738 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1739 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1742 mode = *(mousemode_t *)addr;
1745 /* adjust and validate parameters. */
1746 if (mode.rate > UCHAR_MAX)
1749 mode.rate = sc->dflt_mode.rate;
1750 else if (mode.rate == -1)
1751 /* don't change the current setting */
1753 else if (mode.rate < 0)
1755 if (mode.resolution >= UCHAR_MAX)
1757 if (mode.resolution >= 200)
1758 mode.resolution = MOUSE_RES_HIGH;
1759 else if (mode.resolution >= 100)
1760 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1761 else if (mode.resolution >= 50)
1762 mode.resolution = MOUSE_RES_MEDIUMLOW;
1763 else if (mode.resolution > 0)
1764 mode.resolution = MOUSE_RES_LOW;
1765 if (mode.resolution == MOUSE_RES_DEFAULT)
1766 mode.resolution = sc->dflt_mode.resolution;
1767 else if (mode.resolution == -1)
1768 /* don't change the current setting */
1770 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1771 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1772 if (mode.level == -1)
1773 /* don't change the current setting */
1774 mode.level = sc->mode.level;
1775 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1777 if (mode.accelfactor == -1)
1778 /* don't change the current setting */
1779 mode.accelfactor = sc->mode.accelfactor;
1780 else if (mode.accelfactor < 0)
1783 /* don't allow anybody to poll the keyboard controller */
1784 error = block_mouse_data(sc, &command_byte);
1788 /* set mouse parameters */
1790 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1791 if (mode.resolution >= 0)
1792 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1793 set_mouse_scaling(sc->kbdc, 1);
1794 get_mouse_status(sc->kbdc, stat, 0, 3);
1797 sc->mode.rate = mode.rate;
1798 sc->mode.resolution = mode.resolution;
1799 sc->mode.accelfactor = mode.accelfactor;
1800 sc->mode.level = mode.level;
1803 unblock_mouse_data(sc, command_byte);
1806 case MOUSE_GETLEVEL:
1807 *(int *)addr = sc->mode.level;
1810 case MOUSE_SETLEVEL:
1811 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1813 sc->mode.level = *(int *)addr;
1816 case MOUSE_GETSTATUS:
1818 status = sc->status;
1819 sc->status.flags = 0;
1820 sc->status.obutton = sc->status.button;
1821 sc->status.button = 0;
1826 *(mousestatus_t *)addr = status;
1829 #if (defined(MOUSE_GETVARS))
1831 var = (mousevar_t *)addr;
1832 bzero(var, sizeof(*var));
1834 var->var[0] = MOUSE_VARS_PS2_SIG;
1835 var->var[1] = sc->config;
1836 var->var[2] = sc->flags;
1842 #endif /* MOUSE_GETVARS */
1844 case MOUSE_READSTATE:
1845 case MOUSE_READDATA:
1846 data = (mousedata_t *)addr;
1847 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1850 error = block_mouse_data(sc, &command_byte);
1853 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1854 (ap->a_cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1856 unblock_mouse_data(sc, command_byte);
1859 #if (defined(MOUSE_SETRESOLUTION))
1860 case MOUSE_SETRESOLUTION:
1861 mode.resolution = *(int *)addr;
1862 if (mode.resolution >= UCHAR_MAX)
1864 else if (mode.resolution >= 200)
1865 mode.resolution = MOUSE_RES_HIGH;
1866 else if (mode.resolution >= 100)
1867 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1868 else if (mode.resolution >= 50)
1869 mode.resolution = MOUSE_RES_MEDIUMLOW;
1870 else if (mode.resolution > 0)
1871 mode.resolution = MOUSE_RES_LOW;
1872 if (mode.resolution == MOUSE_RES_DEFAULT)
1873 mode.resolution = sc->dflt_mode.resolution;
1874 else if (mode.resolution == -1)
1875 mode.resolution = sc->mode.resolution;
1876 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1877 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1879 error = block_mouse_data(sc, &command_byte);
1882 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1883 if (sc->mode.resolution != mode.resolution)
1885 unblock_mouse_data(sc, command_byte);
1887 #endif /* MOUSE_SETRESOLUTION */
1889 #if (defined(MOUSE_SETRATE))
1891 mode.rate = *(int *)addr;
1892 if (mode.rate > UCHAR_MAX)
1895 mode.rate = sc->dflt_mode.rate;
1896 else if (mode.rate < 0)
1897 mode.rate = sc->mode.rate;
1899 error = block_mouse_data(sc, &command_byte);
1902 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1903 if (sc->mode.rate != mode.rate)
1905 unblock_mouse_data(sc, command_byte);
1907 #endif /* MOUSE_SETRATE */
1909 #if (defined(MOUSE_SETSCALING))
1910 case MOUSE_SETSCALING:
1911 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1914 error = block_mouse_data(sc, &command_byte);
1917 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1919 unblock_mouse_data(sc, command_byte);
1921 #endif /* MOUSE_SETSCALING */
1923 #if (defined(MOUSE_GETHWID))
1925 error = block_mouse_data(sc, &command_byte);
1928 sc->hw.hwid &= ~0x00ff;
1929 sc->hw.hwid |= get_aux_id(sc->kbdc);
1930 *(int *)addr = sc->hw.hwid & 0x00ff;
1931 unblock_mouse_data(sc, command_byte);
1933 #endif /* MOUSE_GETHWID */
1943 psmtimeout(void *arg)
1945 struct psm_softc *sc;
1947 sc = (struct psm_softc *)arg;
1949 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1951 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1953 kbdc_lock(sc->kbdc, FALSE);
1955 sc->watchdog = TRUE;
1956 callout_reset(&sc->callout, hz, psmtimeout, (void *)(uintptr_t)sc);
1964 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
1965 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
1967 static int butmap[8] = {
1971 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1973 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1974 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1975 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1977 static int butmap_versapad[8] = {
1983 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1985 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
1987 struct psm_softc *sc = arg;
1995 /* read until there is nothing to read */
1996 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
1998 /* discard the byte if the device is not open */
1999 if ((sc->state & PSM_OPEN) == 0)
2002 getmicrouptime(&tv);
2003 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
2004 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2008 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2009 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2010 timevaladd(&sc->inputtimeout, &tv);
2012 sc->ipacket[sc->inputbytes++] = c;
2013 if (sc->inputbytes < sc->mode.packetsize)
2017 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2018 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
2019 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
2024 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2025 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
2026 c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
2028 if (sc->syncerrors < sc->mode.packetsize) {
2029 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2031 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2032 } else if (sc->syncerrors == sc->mode.packetsize) {
2033 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2035 disable_aux_dev(sc->kbdc);
2036 enable_aux_dev(sc->kbdc);
2037 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2038 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2040 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2041 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2042 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2043 reinitialize(sc, TRUE);
2049 * A kludge for Kensington device!
2050 * The MSB of the horizontal count appears to be stored in
2053 if (sc->hw.model == MOUSE_MODEL_THINK)
2054 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2056 /* ignore the overflow bits... */
2057 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2058 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2060 ms.obutton = sc->button; /* previous button state */
2061 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2062 /* `tapping' action */
2063 if (sc->config & PSM_CONFIG_FORCETAP)
2064 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2066 switch (sc->hw.model) {
2068 case MOUSE_MODEL_EXPLORER:
2070 * b7 b6 b5 b4 b3 b2 b1 b0
2071 * byte 1: oy ox sy sx 1 M R L
2072 * byte 2: x x x x x x x x
2073 * byte 3: y y y y y y y y
2074 * byte 4: * * S2 S1 s d2 d1 d0
2076 * L, M, R, S1, S2: left, middle, right and side buttons
2077 * s: wheel data sign bit
2080 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2081 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
2082 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2083 ? MOUSE_BUTTON4DOWN : 0;
2084 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2085 ? MOUSE_BUTTON5DOWN : 0;
2088 case MOUSE_MODEL_INTELLI:
2089 case MOUSE_MODEL_NET:
2090 /* wheel data is in the fourth byte */
2091 z = (char)sc->ipacket[3];
2092 /* some mice may send 7 when there is no Z movement?! XXX */
2093 if ((z >= 7) || (z <= -7))
2095 /* some compatible mice have additional buttons */
2096 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2097 ? MOUSE_BUTTON4DOWN : 0;
2098 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2099 ? MOUSE_BUTTON5DOWN : 0;
2102 case MOUSE_MODEL_MOUSEMANPLUS:
2104 * PS2++ protocl packet
2106 * b7 b6 b5 b4 b3 b2 b1 b0
2107 * byte 1: * 1 p3 p2 1 * * *
2108 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2110 * p3-p0: packet type
2111 * c1, c2: c1 & c2 == 1, if p2 == 0
2112 * c1 & c2 == 0, if p2 == 1
2114 * packet type: 0 (device type)
2115 * See comments in enable_mmanplus() below.
2117 * packet type: 1 (wheel data)
2119 * b7 b6 b5 b4 b3 b2 b1 b0
2120 * byte 3: h * B5 B4 s d2 d1 d0
2122 * h: 1, if horizontal roller data
2123 * 0, if vertical roller data
2124 * B4, B5: button 4 and 5
2126 * d2-d0: roller data
2128 * packet type: 2 (reserved)
2130 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2132 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
2133 /* the extended data packet encodes button and wheel events */
2134 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
2136 /* wheel data packet */
2138 if (sc->ipacket[2] & 0x80) {
2139 /* horizontal roller count - ignore it XXX*/
2141 /* vertical roller count */
2142 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2143 ? (sc->ipacket[2] & 0x0f) - 16
2144 : (sc->ipacket[2] & 0x0f);
2146 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2147 ? MOUSE_BUTTON4DOWN : 0;
2148 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2149 ? MOUSE_BUTTON5DOWN : 0;
2152 /* this packet type is reserved by Logitech... */
2154 * IBM ScrollPoint Mouse uses this packet type to
2155 * encode both vertical and horizontal scroll movement.
2158 /* horizontal count */
2159 if (sc->ipacket[2] & 0x0f)
2160 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2161 /* vertical count */
2162 if (sc->ipacket[2] & 0xf0)
2163 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2165 /* vertical count */
2166 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
2167 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16
2168 : ((sc->ipacket[2] >> 4) & 0x0f);
2169 /* horizontal count */
2170 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
2171 ? (sc->ipacket[2] & 0x0f) - 16
2172 : (sc->ipacket[2] & 0x0f);
2176 /* device type packet - shouldn't happen */
2180 ms.button = ms.obutton;
2182 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2183 "0x%02x 0x%02x 0x%02x\n",
2184 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
2185 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
2189 /* preserve button states */
2190 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2194 case MOUSE_MODEL_GLIDEPOINT:
2195 /* `tapping' action */
2196 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2199 case MOUSE_MODEL_NETSCROLL:
2200 /* three addtional bytes encode buttons and wheel events */
2201 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2202 ? MOUSE_BUTTON4DOWN : 0;
2203 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2204 ? MOUSE_BUTTON5DOWN : 0;
2205 z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
2206 ? sc->ipacket[4] - 256 : sc->ipacket[4];
2209 case MOUSE_MODEL_THINK:
2210 /* the fourth button state in the first byte */
2211 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2214 case MOUSE_MODEL_VERSAPAD:
2215 /* VersaPad PS/2 absolute mode message format
2217 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2218 * ipacket[0]: 1 1 0 A 1 L T R
2219 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2220 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2221 * ipacket[3]: 1 1 1 A 1 L T R
2222 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2223 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2226 * R: right physical mouse button (1=on)
2227 * T: touch pad virtual button (1=tapping)
2228 * L: left physical mouse button (1=on)
2229 * A: position data is valid (1=valid)
2230 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2231 * V: vertical data (12bit signed integer. V11 is sign bit.)
2234 * Tapping is mapped to MOUSE_BUTTON4.
2236 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2237 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2239 if (c & MOUSE_PS2VERSA_IN_USE) {
2240 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
2241 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
2246 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2249 if (x < 0) /* XXX */
2258 sc->flags |= PSM_FLAGS_FINGERDOWN;
2263 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2265 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2266 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2269 case MOUSE_MODEL_4D:
2271 * b7 b6 b5 b4 b3 b2 b1 b0
2272 * byte 1: s2 d2 s1 d1 1 M R L
2273 * byte 2: sx x x x x x x x
2274 * byte 3: sy y y y y y y y
2276 * s1: wheel 1 direction
2278 * s2: wheel 2 direction
2281 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2282 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2283 switch (c & MOUSE_4D_WHEELBITS) {
2290 case 0x40: /* 2nd wheel turning right XXX */
2293 case 0xc0: /* 2nd wheel turning left XXX */
2299 case MOUSE_MODEL_4DPLUS:
2300 if ((x < 16 - 256) && (y < 16 - 256)) {
2302 * b7 b6 b5 b4 b3 b2 b1 b0
2303 * byte 1: 0 0 1 1 1 M R L
2304 * byte 2: 0 0 0 0 1 0 0 0
2305 * byte 3: 0 0 0 0 S s d1 d0
2307 * L, M, R, S: left, middle, right and side buttons
2308 * s: wheel data sign bit
2312 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2313 ms.button |= MOUSE_BUTTON4DOWN;
2314 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2315 ? ((sc->ipacket[2] & 0x07) - 8)
2316 : (sc->ipacket[2] & 0x07) ;
2318 /* preserve previous button states */
2319 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2323 case MOUSE_MODEL_GENERIC:
2329 if (sc->mode.accelfactor >= 1) {
2331 x = x * x / sc->mode.accelfactor;
2334 if (c & MOUSE_PS2_XNEG)
2338 y = y * y / sc->mode.accelfactor;
2341 if (c & MOUSE_PS2_YNEG)
2349 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2350 | (ms.obutton ^ ms.button);
2352 if (sc->mode.level < PSM_LEVEL_NATIVE)
2353 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
2355 sc->status.flags |= ms.flags;
2356 sc->status.dx += ms.dx;
2357 sc->status.dy += ms.dy;
2358 sc->status.dz += ms.dz;
2359 sc->status.button = ms.button;
2360 sc->button = ms.button;
2362 sc->watchdog = FALSE;
2365 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2366 l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2367 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2368 if (sc->inputbytes > l)
2369 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
2371 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
2372 sc->queue.count += sc->inputbytes;
2376 if (sc->state & PSM_ASLP) {
2377 sc->state &= ~PSM_ASLP;
2378 wakeup((caddr_t) sc);
2380 KNOTE(&sc->rsel.si_note, 0);
2384 static struct filterops psmfiltops =
2385 { 1, NULL, psmfilter_detach, psmfilter };
2388 psmkqfilter(struct dev_kqfilter_args *ap)
2390 cdev_t dev = ap->a_head.a_dev;
2391 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2392 struct knote *kn = ap->a_kn;
2393 struct klist *klist;
2397 switch (kn->kn_filter) {
2399 kn->kn_fop = &psmfiltops;
2400 kn->kn_hook = (caddr_t)sc;
2403 ap->a_result = EOPNOTSUPP;
2408 klist = &sc->rsel.si_note;
2409 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
2416 psmfilter_detach(struct knote *kn)
2418 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook;
2419 struct klist *klist;
2422 klist = &sc->rsel.si_note;
2423 SLIST_REMOVE(klist, kn, knote, kn_selnext);
2428 psmfilter(struct knote *kn, long hint)
2430 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook;
2434 if (sc->queue.count > 0)
2441 /* vendor/model specific routines */
2443 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2445 if (set_mouse_resolution(kbdc, res) != res)
2447 if (set_mouse_scaling(kbdc, scale)
2448 && set_mouse_scaling(kbdc, scale)
2449 && set_mouse_scaling(kbdc, scale)
2450 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2456 mouse_ext_command(KBDC kbdc, int command)
2460 c = (command >> 6) & 0x03;
2461 if (set_mouse_resolution(kbdc, c) != c)
2463 c = (command >> 4) & 0x03;
2464 if (set_mouse_resolution(kbdc, c) != c)
2466 c = (command >> 2) & 0x03;
2467 if (set_mouse_resolution(kbdc, c) != c)
2469 c = (command >> 0) & 0x03;
2470 if (set_mouse_resolution(kbdc, c) != c)
2476 /* Logitech MouseMan Cordless II */
2478 enable_lcordless(struct psm_softc *sc)
2483 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2485 if (status[1] == PSMD_RES_HIGH)
2487 ch = (status[0] & 0x07) - 1; /* channel # */
2488 if ((ch <= 0) || (ch > 4))
2491 * status[1]: always one?
2492 * status[2]: battery status? (0-100)
2498 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2500 enable_groller(struct psm_softc *sc)
2505 * The special sequence to enable the fourth button and the
2506 * roller. Immediately after this sequence check status bytes.
2507 * if the mouse is NetScroll, the second and the third bytes are
2512 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2513 * look like the following.
2515 * byte 1 bit 7 always 0
2516 * bit 6 stream mode (0)
2517 * bit 5 disabled (0)
2518 * bit 4 1:1 scaling (0)
2520 * bit 0-2 button status
2521 * byte 2 resolution (PSMD_RES_HIGH)
2522 * byte 3 report rate (?)
2525 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2527 if ((status[1] != '3') || (status[2] != 'D'))
2529 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2534 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2536 enable_gmouse(struct psm_softc *sc)
2541 * The special sequence to enable the middle, "rubber" button.
2542 * Immediately after this sequence check status bytes.
2543 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2544 * the second and the third bytes are '3' and 'U'.
2545 * NOTE: NetMouse reports that it has three buttons although it has
2546 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2547 * say they have three buttons too and they do have a button on the
2550 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2552 if ((status[1] != '3') || (status[2] != 'U'))
2557 /* ALPS GlidePoint */
2559 enable_aglide(struct psm_softc *sc)
2564 * The special sequence to obtain ALPS GlidePoint specific
2565 * information. Immediately after this sequence, status bytes will
2566 * contain something interesting.
2567 * NOTE: ALPS produces several models of GlidePoint. Some of those
2568 * do not respond to this sequence, thus, cannot be detected this way.
2570 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2572 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2574 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2579 /* Kensington ThinkingMouse/Trackball */
2581 enable_kmouse(struct psm_softc *sc)
2583 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2584 KBDC kbdc = sc->kbdc;
2590 id1 = get_aux_id(kbdc);
2591 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2594 * The device is now in the native mode? It returns a different
2597 id2 = get_aux_id(kbdc);
2598 if ((id1 == id2) || (id2 != 2))
2601 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2604 /* at this point, resolution is LOW, sampling rate is 10/sec */
2605 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2610 * The special sequence to enable the third and fourth buttons.
2611 * Otherwise they behave like the first and second buttons.
2613 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2614 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2619 * At this point, the device is using default resolution and
2620 * sampling rate for the native mode.
2622 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2624 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2627 /* the device appears be enabled by this sequence, diable it for now */
2628 disable_aux_dev(kbdc);
2629 empty_aux_buffer(kbdc, 5);
2634 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2636 enable_mmanplus(struct psm_softc *sc)
2638 KBDC kbdc = sc->kbdc;
2641 /* the special sequence to enable the fourth button and the roller. */
2643 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2644 * must be called exactly three times since the last RESET command
2645 * before this sequence. XXX
2647 if (!set_mouse_scaling(kbdc, 1))
2649 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2651 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2655 * PS2++ protocl, packet type 0
2657 * b7 b6 b5 b4 b3 b2 b1 b0
2658 * byte 1: * 1 p3 p2 1 * * *
2659 * byte 2: 1 1 p1 p0 m1 m0 1 0
2660 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2662 * p3-p0: packet type: 0
2663 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2665 /* check constant bits */
2666 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2668 if ((data[1] & 0xc3) != 0xc2)
2670 /* check d3-d0 in byte 2 */
2671 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2674 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2677 sc->hw.hwid &= 0x00ff;
2678 sc->hw.hwid |= data[2] << 8; /* save model ID */
2681 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2682 * the wheel and the fourth button events are encoded in the
2683 * special data packet. The mouse may be put in the IntelliMouse mode
2684 * if it is initialized by the IntelliMouse's method.
2689 /* MS IntelliMouse Explorer */
2691 enable_msexplorer(struct psm_softc *sc)
2693 static unsigned char rate0[] = { 200, 100, 80, };
2694 static unsigned char rate1[] = { 200, 200, 80, };
2695 KBDC kbdc = sc->kbdc;
2699 /* the special sequence to enable the extra buttons and the roller. */
2700 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2701 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2704 /* the device will give the genuine ID only after the above sequence */
2705 id = get_aux_id(kbdc);
2706 if (id != PSM_EXPLORER_ID)
2710 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2713 * XXX: this is a kludge to fool some KVM switch products
2714 * which think they are clever enough to know the 4-byte IntelliMouse
2715 * protocol, and assume any other protocols use 3-byte packets.
2716 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2717 * correctly to the host computer because of this!
2718 * The following sequence is actually IntelliMouse's "wake up"
2719 * sequence; it will make the KVM think the mouse is IntelliMouse
2720 * when it is in fact IntelliMouse Explorer.
2722 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2723 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2726 id = get_aux_id(kbdc);
2731 /* MS IntelliMouse */
2733 enable_msintelli(struct psm_softc *sc)
2736 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2737 * probe routine and act like IntelliMouse.
2740 static unsigned char rate[] = { 200, 100, 80, };
2741 KBDC kbdc = sc->kbdc;
2745 /* the special sequence to enable the third button and the roller. */
2746 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2747 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2750 /* the device will give the genuine ID only after the above sequence */
2751 id = get_aux_id(kbdc);
2752 if (id != PSM_INTELLI_ID)
2761 /* A4 Tech 4D Mouse */
2763 enable_4dmouse(struct psm_softc *sc)
2766 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2769 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2770 KBDC kbdc = sc->kbdc;
2774 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2775 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2778 id = get_aux_id(kbdc);
2780 * WinEasy 4D, 4 Way Scroll 4D: 6
2781 * Cable-Free 4D: 8 (4DPLUS)
2782 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2784 if (id != PSM_4DMOUSE_ID)
2788 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2793 /* A4 Tech 4D+ Mouse */
2795 enable_4dplus(struct psm_softc *sc)
2798 * Newer wheel mice from A4 Tech seem to use this protocol.
2799 * Older models are recognized as either 4D Mouse or IntelliMouse.
2801 KBDC kbdc = sc->kbdc;
2805 * enable_4dmouse() already issued the following ID sequence...
2806 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2809 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2810 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2815 id = get_aux_id(kbdc);
2816 if (id != PSM_4DPLUS_ID)
2820 sc->hw.buttons = 4; /* XXX */
2825 /* Interlink electronics VersaPad */
2827 enable_versapad(struct psm_softc *sc)
2829 KBDC kbdc = sc->kbdc;
2832 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2833 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2834 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2835 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2836 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2837 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2838 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2840 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2842 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2844 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2846 return TRUE; /* PS/2 absolute mode */
2850 psmresume(device_t dev)
2852 struct psm_softc *sc = device_get_softc(dev);
2853 int unit = device_get_unit(dev);
2857 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2859 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2862 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2864 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2866 * Release the blocked process; it must be notified that the device
2867 * cannot be accessed anymore.
2869 sc->state &= ~PSM_ASLP;
2870 wakeup((caddr_t)sc);
2874 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2879 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);