2 * Copyright (c) 1992, 1993 Erik Forsberg.
3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
15 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 * Ported to 386bsd Oct 17, 1992
25 * Sandi Donno, Computer Science, University of Cape Town, South Africa
26 * Please send bug reports to sandi@cs.uct.ac.za
28 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
29 * although I was only partially successful in getting the alpha release
30 * of his "driver for the Logitech and ATI Inport Bus mice for use with
31 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
32 * found his code to be an invaluable reference when porting this driver
35 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
36 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
38 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
39 * Andrew Herbert - 12 June 1993
41 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
44 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
47 * Hardware access routines and probe logic rewritten by
48 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
49 * - 3, 14, 22 October 1996.
50 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
51 * - 14, 30 November 1996. Uses `kbdio.c'.
52 * - 13 December 1996. Uses queuing version of `kbdio.c'.
53 * - January/February 1997. Tweaked probe logic for
54 * HiNote UltraII/Latitude/Armada laptops.
55 * - 30 July 1997. Added APM support.
56 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
57 * Improved sync check logic.
58 * Vendor specific support routines.
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/module.h>
73 #include <sys/syslog.h>
74 #include <machine/bus.h>
76 #include <sys/selinfo.h>
77 #include <sys/sysctl.h>
81 #include <sys/limits.h>
82 #include <sys/mouse.h>
83 #include <machine/resource.h>
85 #include <isa/isavar.h>
86 #include <dev/kbd/atkbdcreg.h>
89 * Driver specific options: the following options may be set by
90 * `options' statements in the kernel configuration file.
95 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */
98 #ifndef PSM_SYNCERR_THRESHOLD1
99 #define PSM_SYNCERR_THRESHOLD1 20
102 #ifndef PSM_INPUT_TIMEOUT
103 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
106 /* end of driver specific options */
108 #define PSM_DRIVER_NAME "psm"
109 #define PSMCPNP_DRIVER_NAME "psmcpnp"
112 #define PSM_BUFSIZE 960
113 #define PSM_SMALLBUFSIZE 240
115 /* operation levels */
116 #define PSM_LEVEL_BASE 0
117 #define PSM_LEVEL_STANDARD 1
118 #define PSM_LEVEL_NATIVE 2
119 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
120 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
122 /* Logitech PS2++ protocol */
123 #define MOUSE_PS2PLUS_CHECKBITS(b) \
124 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
125 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
126 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
129 #define PSM_UNIT(dev) (minor(dev) >> 1)
130 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
131 #define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1))
134 typedef struct ringbuf {
135 int count; /* # of valid elements in the buffer */
136 int head; /* head pointer */
137 int tail; /* tail poiner */
138 unsigned char buf[PSM_BUFSIZE];
142 typedef struct packetbuf {
143 unsigned char ipacket[16]; /* interim input buffer */
144 int inputbytes; /* # of bytes in the input buffer */
147 #ifndef PSM_PACKETQUEUE
148 #define PSM_PACKETQUEUE 128
151 /* driver control block */
152 struct psm_softc { /* Driver status information */
154 struct selinfo rsel; /* Process selecting for Input */
155 unsigned char state; /* Mouse driver state */
156 int config; /* driver configuration flags */
157 int flags; /* other flags */
158 KBDC kbdc; /* handle to access the keyboard controller */
159 struct resource *intr; /* IRQ resource */
160 void *ih; /* interrupt handle */
161 mousehw_t hw; /* hardware information */
162 mousemode_t mode; /* operation mode */
163 mousemode_t dflt_mode; /* default operation mode */
164 mousestatus_t status; /* accumulated mouse movement */
165 ringbuf_t queue; /* mouse status queue */
166 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
167 int pqueue_start; /* start of data in queue */
168 int pqueue_end; /* end of data in queue */
169 int button; /* the latest button state */
170 int xold; /* previous absolute X position */
171 int yold; /* previous absolute Y position */
172 int syncerrors; /* XXX: KILL ME! */
173 struct timeval inputtimeout;
174 struct timeval lastsoftintr; /* time of last soft interrupt */
175 struct timeval lastinputerr; /* time last sync error happened */
176 int watchdog; /* watchdog timer flag */
177 struct callout_handle callout; /* watchdog timer call out */
178 struct callout_handle softcallout; /* buffer timer call out */
184 static devclass_t psm_devclass;
185 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
187 /* driver state flags (state) */
188 #define PSM_VALID 0x80
189 #define PSM_OPEN 1 /* Device is open */
190 #define PSM_ASLP 2 /* Waiting for mouse data */
191 #define PSM_SOFTARMED 4 /* Software interrupt armed */
193 /* driver configuration flags (config) */
194 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
195 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
196 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
197 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
198 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
199 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
200 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
201 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
202 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
203 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
205 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \
207 | PSM_CONFIG_NOCHECKSYNC \
208 | PSM_CONFIG_SYNCHACK \
209 | PSM_CONFIG_NOIDPROBE \
210 | PSM_CONFIG_NORESET \
211 | PSM_CONFIG_FORCETAP \
212 | PSM_CONFIG_IGNPORTERROR \
213 | PSM_CONFIG_HOOKRESUME \
214 | PSM_CONFIG_INITAFTERSUSPEND)
216 /* other flags (flags) */
217 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
219 /* for backward compatibility */
220 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
221 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
222 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
224 typedef struct old_mousehw {
231 typedef struct old_mousemode {
238 /* packet formatting function */
239 typedef int packetfunc_t(struct psm_softc *, unsigned char *,
240 int *, int, mousestatus_t *);
242 /* function prototypes */
243 static void psmidentify(driver_t *, device_t);
244 static int psmprobe(device_t);
245 static int psmattach(device_t);
246 static int psmdetach(device_t);
247 static int psmresume(device_t);
249 static d_open_t psmopen;
250 static d_close_t psmclose;
251 static d_read_t psmread;
252 static d_ioctl_t psmioctl;
253 static d_poll_t psmpoll;
255 static int enable_aux_dev(KBDC);
256 static int disable_aux_dev(KBDC);
257 static int get_mouse_status(KBDC, int *, int, int);
258 static int get_aux_id(KBDC);
259 static int set_mouse_sampling_rate(KBDC, int);
260 static int set_mouse_scaling(KBDC, int);
261 static int set_mouse_resolution(KBDC, int);
262 static int set_mouse_mode(KBDC);
263 static int get_mouse_buttons(KBDC);
264 static int is_a_mouse(int);
265 static void recover_from_error(KBDC);
266 static int restore_controller(KBDC, int);
267 static int doinitialize(struct psm_softc *, mousemode_t *);
268 static int doopen(struct psm_softc *, int);
269 static int reinitialize(struct psm_softc *, int);
270 static char *model_name(int);
271 static void psmsoftintr(void *);
272 static void psmintr(void *);
273 static void psmtimeout(void *);
274 static int timeelapsed(const struct timeval *,
275 int, int, const struct timeval *);
276 static void dropqueue(struct psm_softc *);
277 static void flushpackets(struct psm_softc *);
279 /* vendor specific features */
280 typedef int probefunc_t(struct psm_softc *);
282 static int mouse_id_proc1(KBDC, int, int, int *);
283 static int mouse_ext_command(KBDC, int);
284 static probefunc_t enable_groller;
285 static probefunc_t enable_gmouse;
286 static probefunc_t enable_aglide;
287 static probefunc_t enable_kmouse;
288 static probefunc_t enable_msexplorer;
289 static probefunc_t enable_msintelli;
290 static probefunc_t enable_4dmouse;
291 static probefunc_t enable_4dplus;
292 static probefunc_t enable_mmanplus;
293 static probefunc_t enable_versapad;
294 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, unsigned char *);
298 unsigned char syncmask;
300 probefunc_t *probefunc;
303 * WARNING: the order of probe is very important. Don't mess it
304 * unless you know what you are doing.
306 { MOUSE_MODEL_NET, /* Genius NetMouse */
307 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
308 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
309 0xc8, 6, enable_groller, },
310 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
311 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
312 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
313 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
314 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
315 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
316 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
317 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
318 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
319 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
320 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
321 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
322 { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */
323 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
324 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
325 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
326 { MOUSE_MODEL_GENERIC,
327 0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
329 #define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
331 /* device driver declarateion */
332 static device_method_t psm_methods[] = {
333 /* Device interface */
334 DEVMETHOD(device_identify, psmidentify),
335 DEVMETHOD(device_probe, psmprobe),
336 DEVMETHOD(device_attach, psmattach),
337 DEVMETHOD(device_detach, psmdetach),
338 DEVMETHOD(device_resume, psmresume),
343 static driver_t psm_driver = {
346 sizeof(struct psm_softc),
350 static struct cdevsw psm_cdevsw = {
351 .d_version = D_VERSION,
352 .d_flags = D_NEEDGIANT,
358 .d_name = PSM_DRIVER_NAME,
361 /* debug message level */
362 static int verbose = PSM_DEBUG;
364 /* device I/O routines */
366 enable_aux_dev(KBDC kbdc)
370 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
372 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
374 return (res == PSM_ACK);
378 disable_aux_dev(KBDC kbdc)
382 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
384 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
386 return (res == PSM_ACK);
390 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
399 cmd = PSMC_SEND_DEV_STATUS;
402 cmd = PSMC_SEND_DEV_DATA;
405 empty_aux_buffer(kbdc, 5);
406 res = send_aux_command(kbdc, cmd);
408 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
409 (flag == 1) ? "DATA" : "STATUS", res);
413 for (i = 0; i < len; ++i) {
414 status[i] = read_aux_data(kbdc);
420 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
421 (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
428 get_aux_id(KBDC kbdc)
433 empty_aux_buffer(kbdc, 5);
434 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
436 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
443 id = read_aux_data(kbdc);
445 log(LOG_DEBUG, "psm: device ID: %04x\n", id);
451 set_mouse_sampling_rate(KBDC kbdc, int rate)
455 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
457 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
459 return ((res == PSM_ACK) ? rate : -1);
463 set_mouse_scaling(KBDC kbdc, int scale)
470 scale = PSMC_SET_SCALING11;
473 scale = PSMC_SET_SCALING21;
476 res = send_aux_command(kbdc, scale);
478 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
479 (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
481 return (res == PSM_ACK);
484 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
486 set_mouse_resolution(KBDC kbdc, int val)
490 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
492 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
494 return ((res == PSM_ACK) ? val : -1);
498 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
499 * re-enabled by calling `enable_aux_dev()'
502 set_mouse_mode(KBDC kbdc)
506 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
508 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
510 return (res == PSM_ACK);
514 get_mouse_buttons(KBDC kbdc)
516 int c = 2; /* assume two buttons by default */
520 * NOTE: a special sequence to obtain Logitech Mouse specific
521 * information: set resolution to 25 ppi, set scaling to 1:1, set
522 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
523 * mouse status bytes is the number of available buttons.
524 * Some manufactures also support this sequence.
526 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
528 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
529 && set_mouse_scaling(kbdc, 1)
530 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
537 /* misc subroutines */
539 * Someday, I will get the complete list of valid pointing devices and
546 static int valid_ids[] = {
547 PSM_MOUSE_ID, /* mouse */
548 PSM_BALLPOINT_ID, /* ballpoint device */
549 PSM_INTELLI_ID, /* Intellimouse */
550 PSM_EXPLORER_ID, /* Intellimouse Explorer */
551 -1 /* end of table */
555 for (i = 0; valid_ids[i] >= 0; ++i)
556 if (valid_ids[i] == id)
565 model_name(int model)
571 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
572 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
573 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
574 { MOUSE_MODEL_THINK, "ThinkingMouse" },
575 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
576 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
577 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
578 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
579 { MOUSE_MODEL_4D, "4D Mouse" },
580 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
581 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
582 { MOUSE_MODEL_UNKNOWN, NULL },
586 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
587 if (models[i].model_code == model)
588 return models[i].model_name;
594 recover_from_error(KBDC kbdc)
596 /* discard anything left in the output buffer */
597 empty_both_buffers(kbdc, 10);
601 * NOTE: KBDC_RESET_KBD may not restore the communication between the
602 * keyboard and the controller.
607 * NOTE: somehow diagnostic and keyboard port test commands bring the
610 if (!test_controller(kbdc))
611 log(LOG_ERR, "psm: keyboard controller failed.\n");
612 /* if there isn't a keyboard in the system, the following error is OK */
613 if (test_kbd_port(kbdc) != 0) {
615 log(LOG_ERR, "psm: keyboard port failed.\n");
621 restore_controller(KBDC kbdc, int command_byte)
623 empty_both_buffers(kbdc, 10);
625 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
626 log(LOG_ERR, "psm: failed to restore the keyboard controller "
628 empty_both_buffers(kbdc, 10);
631 empty_both_buffers(kbdc, 10);
637 * Re-initialize the aux port and device. The aux port must be enabled
638 * and its interrupt must be disabled before calling this routine.
639 * The aux device will be disabled before returning.
640 * The keyboard controller must be locked via `kbdc_lock()' before
641 * calling this routine.
644 doinitialize(struct psm_softc *sc, mousemode_t *mode)
646 KBDC kbdc = sc->kbdc;
650 switch((i = test_aux_port(kbdc))) {
651 case 1: /* ignore this error */
654 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
657 case 0: /* no error */
659 case -1: /* time out */
661 recover_from_error(kbdc);
662 if (sc->config & PSM_CONFIG_IGNPORTERROR)
664 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
669 if (sc->config & PSM_CONFIG_NORESET) {
671 * Don't try to reset the pointing device. It may possibly be
672 * left in the unknown state, though...
676 * NOTE: some controllers appears to hang the `keyboard' when
677 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
679 if (!reset_aux_dev(kbdc)) {
680 recover_from_error(kbdc);
681 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
687 * both the aux port and the aux device is functioning, see
688 * if the device can be enabled.
690 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
691 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
694 empty_both_buffers(kbdc, 10); /* remove stray data if any */
696 if (sc->config & PSM_CONFIG_NOIDPROBE) {
697 i = GENERIC_MOUSE_ENTRY;
699 /* FIXME: hardware ID, mouse buttons? */
701 /* other parameters */
702 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
703 if ((*vendortype[i].probefunc)(sc)) {
705 log(LOG_ERR, "psm%d: found %s\n",
706 sc->unit, model_name(vendortype[i].model));
712 sc->hw.model = vendortype[i].model;
713 sc->mode.packetsize = vendortype[i].packetsize;
715 /* set mouse parameters */
716 if (mode != (mousemode_t *)NULL) {
718 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
719 if (mode->resolution >= 0)
720 mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
721 set_mouse_scaling(kbdc, 1);
722 set_mouse_mode(kbdc);
725 /* request a data packet and extract sync. bits */
726 if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
727 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
729 sc->mode.syncmask[0] = 0;
731 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
732 /* the NetScroll Mouse will send three more bytes... Ignore them */
733 empty_aux_buffer(kbdc, 5);
736 /* just check the status of the mouse */
737 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
738 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
745 doopen(struct psm_softc *sc, int command_byte)
749 /* enable the mouse device */
750 if (!enable_aux_dev(sc->kbdc)) {
751 /* MOUSE ERROR: failed to enable the mouse because:
752 * 1) the mouse is faulty,
753 * 2) the mouse has been removed(!?)
754 * In the latter case, the keyboard may have hung, and need
755 * recovery procedure...
757 recover_from_error(sc->kbdc);
759 /* FIXME: we could reset the mouse here and try to enable
760 * it again. But it will take long time and it's not a good
761 * idea to disable the keyboard that long...
763 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
764 recover_from_error(sc->kbdc);
768 restore_controller(sc->kbdc, command_byte);
769 /* mark this device is no longer available */
770 sc->state &= ~PSM_VALID;
771 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
777 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
778 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
780 /* enable the aux port and interrupt */
781 if (!set_controller_command_byte(sc->kbdc,
782 kbdc_get_device_mask(sc->kbdc),
783 (command_byte & KBD_KBD_CONTROL_BITS)
784 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
785 /* CONTROLLER ERROR */
786 disable_aux_dev(sc->kbdc);
787 restore_controller(sc->kbdc, command_byte);
788 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
793 /* start the watchdog timer */
794 sc->watchdog = FALSE;
795 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
801 reinitialize(struct psm_softc *sc, int doinit)
807 /* don't let anybody mess with the aux device */
808 if (!kbdc_lock(sc->kbdc, TRUE))
812 /* block our watchdog timer */
813 sc->watchdog = FALSE;
814 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
815 callout_handle_init(&sc->callout);
817 /* save the current controller command byte */
818 empty_both_buffers(sc->kbdc, 10);
819 c = get_controller_command_byte(sc->kbdc);
821 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
824 /* enable the aux port but disable the aux interrupt and the keyboard */
825 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
826 kbdc_get_device_mask(sc->kbdc),
827 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
828 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
829 /* CONTROLLER ERROR */
831 kbdc_lock(sc->kbdc, FALSE);
832 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
838 if (sc->state & PSM_VALID) {
839 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */
840 empty_aux_buffer(sc->kbdc, 10);
845 /* try to detect the aux device; are you still there? */
848 if (doinitialize(sc, &sc->mode)) {
850 sc->state |= PSM_VALID;
852 /* the device has gone! */
853 restore_controller(sc->kbdc, c);
854 sc->state &= ~PSM_VALID;
855 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
862 /* restore the driver state */
863 if ((sc->state & PSM_OPEN) && (err == 0)) {
864 /* enable the aux device and the port again */
867 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
870 /* restore the keyboard port and disable the aux port */
871 if (!set_controller_command_byte(sc->kbdc,
872 kbdc_get_device_mask(sc->kbdc),
873 (c & KBD_KBD_CONTROL_BITS)
874 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
875 /* CONTROLLER ERROR */
876 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
882 kbdc_lock(sc->kbdc, FALSE);
886 /* psm driver entry points */
889 psmidentify(driver_t *driver, device_t parent)
896 unit = device_get_unit(parent);
898 /* always add at least one child */
899 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
903 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
908 * If the PS/2 mouse device has already been reported by ACPI or
909 * PnP BIOS, obtain the IRQ resource from it.
910 * (See psmcpnp_attach() below.)
912 psmc = device_find_child(device_get_parent(parent),
913 PSMCPNP_DRIVER_NAME, unit);
916 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
919 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
922 #define endprobe(v) do { if (bootverbose) \
924 kbdc_set_device_mask(sc->kbdc, mask); \
925 kbdc_lock(sc->kbdc, FALSE); \
930 psmprobe(device_t dev)
932 int unit = device_get_unit(dev);
933 struct psm_softc *sc = device_get_softc(dev);
944 /* see if IRQ is available */
946 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
947 RF_SHAREABLE | RF_ACTIVE);
948 if (sc->intr == NULL) {
950 device_printf(dev, "unable to allocate IRQ\n");
953 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
956 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
957 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
958 /* XXX: for backward compatibility */
959 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
961 #ifdef PSM_RESETAFTERSUSPEND
962 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
964 PSM_CONFIG_HOOKRESUME;
966 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
971 device_set_desc(dev, "PS/2 Mouse");
973 if (!kbdc_lock(sc->kbdc, TRUE)) {
974 printf("psm%d: unable to lock the controller.\n", unit);
981 * NOTE: two bits in the command byte controls the operation of the
982 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
983 * port interrupt (IRQ 12) enable bit (bit 2).
986 /* discard anything left after the keyboard initialization */
987 empty_both_buffers(sc->kbdc, 10);
989 /* save the current command byte; it will be used later */
990 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
991 command_byte = get_controller_command_byte(sc->kbdc);
993 printf("psm%d: current command byte:%04x\n", unit, command_byte);
994 if (command_byte == -1) {
995 /* CONTROLLER ERROR */
996 printf("psm%d: unable to get the current command byte value.\n",
1002 * disable the keyboard port while probing the aux port, which must be
1003 * enabled during this routine
1005 if (!set_controller_command_byte(sc->kbdc,
1006 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1007 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1008 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1010 * this is CONTROLLER ERROR; I don't know how to recover
1011 * from this error...
1013 restore_controller(sc->kbdc, command_byte);
1014 printf("psm%d: unable to set the command byte.\n", unit);
1017 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1020 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1021 * port exists and is functioning. However, some controllers appears
1022 * to respond with zero even when the aux port doesn't exist. (It may
1023 * be that this is only the case when the controller DOES have the aux
1024 * port but the port is not wired on the motherboard.) The keyboard
1025 * controllers without the port, such as the original AT, are
1026 * supporsed to return with an error code or simply time out. In any
1027 * case, we have to continue probing the port even when the controller
1030 * XXX: some controllers erroneously return the error code 1 when
1031 * it has the perfectly functional aux port. We have to ignore this
1032 * error code. Even if the controller HAS error with the aux port,
1033 * it will be detected later...
1034 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1036 switch ((i = test_aux_port(sc->kbdc))) {
1037 case 1: /* ignore this error */
1040 printf("psm%d: strange result for test aux port (%d).\n",
1043 case 0: /* no error */
1045 case -1: /* time out */
1046 default: /* error */
1047 recover_from_error(sc->kbdc);
1048 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1050 restore_controller(sc->kbdc, command_byte);
1052 printf("psm%d: the aux port is not functioning (%d).\n",
1057 if (sc->config & PSM_CONFIG_NORESET) {
1059 * Don't try to reset the pointing device. It may possibly be
1060 * left in the unknown state, though...
1064 * NOTE: some controllers appears to hang the `keyboard' when the aux
1065 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1067 * Attempt to reset the controller twice -- this helps
1068 * pierce through some KVM switches. The second reset
1071 if (!reset_aux_dev(sc->kbdc)) {
1072 recover_from_error(sc->kbdc);
1073 restore_controller(sc->kbdc, command_byte);
1075 printf("psm%d: failed to reset the aux device.\n", unit);
1077 } else if (!reset_aux_dev(sc->kbdc)) {
1078 recover_from_error(sc->kbdc);
1080 printf("psm%d: failed to reset the aux device (2).\n",
1086 * both the aux port and the aux device is functioning, see if the
1087 * device can be enabled. NOTE: when enabled, the device will start
1088 * sending data; we shall immediately disable the device once we know
1089 * the device can be enabled.
1091 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1093 recover_from_error(sc->kbdc);
1094 restore_controller(sc->kbdc, command_byte);
1096 printf("psm%d: failed to enable the aux device.\n", unit);
1100 /* save the default values after reset */
1101 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1102 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1103 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1105 sc->dflt_mode.rate = sc->mode.rate = -1;
1106 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1109 /* hardware information */
1110 sc->hw.iftype = MOUSE_IF_PS2;
1112 /* verify the device is a mouse */
1113 sc->hw.hwid = get_aux_id(sc->kbdc);
1114 if (!is_a_mouse(sc->hw.hwid)) {
1115 restore_controller(sc->kbdc, command_byte);
1117 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1120 switch (sc->hw.hwid) {
1121 case PSM_BALLPOINT_ID:
1122 sc->hw.type = MOUSE_TRACKBALL;
1125 case PSM_INTELLI_ID:
1126 case PSM_EXPLORER_ID:
1127 case PSM_4DMOUSE_ID:
1129 sc->hw.type = MOUSE_MOUSE;
1132 sc->hw.type = MOUSE_UNKNOWN;
1136 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1138 i = GENERIC_MOUSE_ENTRY;
1141 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1143 /* other parameters */
1144 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1145 if ((*vendortype[i].probefunc)(sc)) {
1147 printf("psm%d: found %s\n",
1148 unit, model_name(vendortype[i].model));
1154 sc->hw.model = vendortype[i].model;
1156 sc->dflt_mode.level = PSM_LEVEL_BASE;
1157 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1158 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1159 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1160 sc->dflt_mode.syncmask[0] = 0;
1162 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1163 if (sc->config & PSM_CONFIG_FORCETAP)
1164 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1165 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1166 sc->mode = sc->dflt_mode;
1167 sc->mode.packetsize = vendortype[i].packetsize;
1169 /* set mouse parameters */
1172 * A version of Logitech FirstMouse+ won't report wheel movement,
1173 * if SET_DEFAULTS is sent... Don't use this command.
1174 * This fix was found by Takashi Nishida.
1176 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1178 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1180 if (sc->config & PSM_CONFIG_RESOLUTION) {
1182 = set_mouse_resolution(sc->kbdc,
1183 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1184 } else if (sc->mode.resolution >= 0) {
1186 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1188 if (sc->mode.rate > 0) {
1189 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1191 set_mouse_scaling(sc->kbdc, 1);
1193 /* request a data packet and extract sync. bits */
1194 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1195 printf("psm%d: failed to get data.\n", unit);
1196 sc->mode.syncmask[0] = 0;
1198 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1199 /* the NetScroll Mouse will send three more bytes... Ignore them */
1200 empty_aux_buffer(sc->kbdc, 5);
1203 /* just check the status of the mouse */
1205 * NOTE: XXX there are some arcane controller/mouse combinations out
1206 * there, which hung the controller unless there is data transmission
1207 * after ACK from the mouse.
1209 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1210 printf("psm%d: failed to get status.\n", unit);
1213 * When in its native mode, some mice operate with different
1214 * default parameters than in the PS/2 compatible mode.
1216 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1217 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1220 /* disable the aux port for now... */
1221 if (!set_controller_command_byte(sc->kbdc,
1222 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1223 (command_byte & KBD_KBD_CONTROL_BITS)
1224 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1226 * this is CONTROLLER ERROR; I don't know the proper way to
1227 * recover from this error...
1229 restore_controller(sc->kbdc, command_byte);
1230 printf("psm%d: unable to set the command byte.\n", unit);
1235 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1236 kbdc_lock(sc->kbdc, FALSE);
1241 psmattach(device_t dev)
1243 int unit = device_get_unit(dev);
1244 struct psm_softc *sc = device_get_softc(dev);
1248 if (sc == NULL) /* shouldn't happen */
1251 /* Setup initial state */
1252 sc->state = PSM_VALID;
1253 callout_handle_init(&sc->callout);
1255 /* Setup our interrupt handler */
1257 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1258 RF_SHAREABLE | RF_ACTIVE);
1259 if (sc->intr == NULL)
1261 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1263 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1268 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1270 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1274 printf("psm%d: model %s, device ID %d\n",
1275 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1277 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1278 unit, model_name(sc->hw.model),
1279 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1280 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1281 unit, sc->config, sc->flags, sc->mode.packetsize);
1282 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1283 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1293 psmdetach(device_t dev)
1295 struct psm_softc *sc;
1298 sc = device_get_softc(dev);
1299 if (sc->state & PSM_OPEN)
1303 bus_teardown_intr(dev, sc->intr, sc->ih);
1304 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1306 destroy_dev(sc->dev);
1307 destroy_dev(sc->bdev);
1313 psmopen(dev_t dev, int flag, int fmt, struct thread *td)
1315 int unit = PSM_UNIT(dev);
1316 struct psm_softc *sc;
1321 /* Get device data */
1322 sc = PSM_SOFTC(unit);
1323 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1324 /* the device is no longer valid/functioning */
1327 /* Disallow multiple opens */
1328 if (sc->state & PSM_OPEN)
1331 device_busy(devclass_get_device(psm_devclass, unit));
1333 /* Initialize state */
1334 sc->mode.level = sc->dflt_mode.level;
1335 sc->mode.protocol = sc->dflt_mode.protocol;
1336 sc->watchdog = FALSE;
1338 /* flush the event queue */
1339 sc->queue.count = 0;
1342 sc->status.flags = 0;
1343 sc->status.button = 0;
1344 sc->status.obutton = 0;
1349 sc->pqueue_start = 0;
1352 /* empty input buffer */
1356 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1357 if (!kbdc_lock(sc->kbdc, TRUE))
1360 /* save the current controller command byte */
1362 command_byte = get_controller_command_byte(sc->kbdc);
1364 /* enable the aux port and temporalily disable the keyboard */
1365 if ((command_byte == -1)
1366 || !set_controller_command_byte(sc->kbdc,
1367 kbdc_get_device_mask(sc->kbdc),
1368 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1369 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1370 /* CONTROLLER ERROR; do you know how to get out of this? */
1371 kbdc_lock(sc->kbdc, FALSE);
1373 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1378 * Now that the keyboard controller is told not to generate
1379 * the keyboard and mouse interrupts, call `splx()' to allow
1380 * the other tty interrupts. The clock interrupt may also occur,
1381 * but timeout routines will be blocked by the poll flag set
1386 /* enable the mouse device */
1387 err = doopen(sc, command_byte);
1391 sc->state |= PSM_OPEN;
1392 kbdc_lock(sc->kbdc, FALSE);
1397 psmclose(dev_t dev, int flag, int fmt, struct thread *td)
1399 int unit = PSM_UNIT(dev);
1400 struct psm_softc *sc = PSM_SOFTC(unit);
1405 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1406 if (!kbdc_lock(sc->kbdc, TRUE))
1409 /* save the current controller command byte */
1411 command_byte = get_controller_command_byte(sc->kbdc);
1412 if (command_byte == -1) {
1413 kbdc_lock(sc->kbdc, FALSE);
1418 /* disable the aux interrupt and temporalily disable the keyboard */
1419 if (!set_controller_command_byte(sc->kbdc,
1420 kbdc_get_device_mask(sc->kbdc),
1421 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1422 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1423 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1425 /* CONTROLLER ERROR;
1426 * NOTE: we shall force our way through. Because the only
1427 * ill effect we shall see is that we may not be able
1428 * to read ACK from the mouse, and it doesn't matter much
1429 * so long as the mouse will accept the DISABLE command.
1434 /* stop the watchdog timer */
1435 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1436 callout_handle_init(&sc->callout);
1438 /* remove anything left in the output buffer */
1439 empty_aux_buffer(sc->kbdc, 10);
1441 /* disable the aux device, port and interrupt */
1442 if (sc->state & PSM_VALID) {
1443 if (!disable_aux_dev(sc->kbdc)) {
1445 * NOTE: we don't return error and continue, pretending
1446 * we have successfully disabled the device. It's OK because
1447 * the interrupt routine will discard any data from the mouse
1450 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1454 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1455 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1459 if (!set_controller_command_byte(sc->kbdc,
1460 kbdc_get_device_mask(sc->kbdc),
1461 (command_byte & KBD_KBD_CONTROL_BITS)
1462 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1463 /* CONTROLLER ERROR;
1464 * we shall ignore this error; see the above comment.
1466 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1470 /* remove anything left in the output buffer */
1471 empty_aux_buffer(sc->kbdc, 10);
1473 /* close is almost always successful */
1474 sc->state &= ~PSM_OPEN;
1475 kbdc_lock(sc->kbdc, FALSE);
1476 device_unbusy(devclass_get_device(psm_devclass, unit));
1481 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, unsigned char *buf)
1483 static unsigned char butmapps2[8] = {
1485 MOUSE_PS2_BUTTON1DOWN,
1486 MOUSE_PS2_BUTTON2DOWN,
1487 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1488 MOUSE_PS2_BUTTON3DOWN,
1489 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1490 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1491 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1493 static unsigned char butmapmsc[8] = {
1494 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1495 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1496 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1497 MOUSE_MSC_BUTTON3UP,
1498 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1499 MOUSE_MSC_BUTTON2UP,
1500 MOUSE_MSC_BUTTON1UP,
1506 if (sc->mode.level == PSM_LEVEL_BASE) {
1507 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1508 if (status->button & MOUSE_BUTTON4DOWN)
1509 mapped |= MOUSE_BUTTON1DOWN;
1510 status->button = mapped;
1511 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1512 i = imax(imin(status->dx, 255), -256);
1514 buf[0] |= MOUSE_PS2_XNEG;
1516 i = imax(imin(status->dy, 255), -256);
1518 buf[0] |= MOUSE_PS2_YNEG;
1520 return MOUSE_PS2_PACKETSIZE;
1521 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1522 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1523 i = imax(imin(status->dx, 255), -256);
1525 buf[3] = i - buf[1];
1526 i = imax(imin(status->dy, 255), -256);
1528 buf[4] = i - buf[2];
1529 i = imax(imin(status->dz, 127), -128);
1530 buf[5] = (i >> 1) & 0x7f;
1531 buf[6] = (i - (i >> 1)) & 0x7f;
1532 buf[7] = (~status->button >> 3) & 0x7f;
1533 return MOUSE_SYS_PACKETSIZE;
1535 return pb->inputbytes;
1539 psmread(dev_t dev, struct uio *uio, int flag)
1541 register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1542 unsigned char buf[PSM_SMALLBUFSIZE];
1547 if ((sc->state & PSM_VALID) == 0)
1550 /* block until mouse activity occured */
1552 while (sc->queue.count <= 0) {
1553 if (PSM_NBLOCKIO(dev)) {
1557 sc->state |= PSM_ASLP;
1558 error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
1559 sc->state &= ~PSM_ASLP;
1563 } else if ((sc->state & PSM_VALID) == 0) {
1564 /* the device disappeared! */
1571 /* copy data to the user land */
1572 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1574 l = imin(sc->queue.count, uio->uio_resid);
1575 if (l > sizeof(buf))
1577 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1578 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1579 sizeof(sc->queue.buf) - sc->queue.head);
1580 bcopy(&sc->queue.buf[0],
1581 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1582 l - (sizeof(sc->queue.buf) - sc->queue.head));
1584 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1586 sc->queue.count -= l;
1587 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1589 error = uiomove(buf, l, uio);
1598 block_mouse_data(struct psm_softc *sc, int *c)
1602 if (!kbdc_lock(sc->kbdc, TRUE))
1606 *c = get_controller_command_byte(sc->kbdc);
1608 || !set_controller_command_byte(sc->kbdc,
1609 kbdc_get_device_mask(sc->kbdc),
1610 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1611 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1612 /* this is CONTROLLER ERROR */
1614 kbdc_lock(sc->kbdc, FALSE);
1619 * The device may be in the middle of status data transmission.
1620 * The transmission will be interrupted, thus, incomplete status
1621 * data must be discarded. Although the aux interrupt is disabled
1622 * at the keyboard controller level, at most one aux interrupt
1623 * may have already been pending and a data byte is in the
1624 * output buffer; throw it away. Note that the second argument
1625 * to `empty_aux_buffer()' is zero, so that the call will just
1626 * flush the internal queue.
1627 * `psmintr()' will be invoked after `splx()' if an interrupt is
1628 * pending; it will see no data and returns immediately.
1630 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1631 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1639 dropqueue(struct psm_softc *sc)
1642 sc->queue.count = 0;
1645 if ((sc->state & PSM_SOFTARMED) != 0) {
1646 sc->state &= ~PSM_SOFTARMED;
1647 untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout);
1649 sc->pqueue_start = sc->pqueue_end;
1653 flushpackets(struct psm_softc *sc)
1657 bzero(&sc->pqueue, sizeof(sc->pqueue));
1661 unblock_mouse_data(struct psm_softc *sc, int c)
1666 * We may have seen a part of status data during `set_mouse_XXX()'.
1667 * they have been queued; flush it.
1669 empty_aux_buffer(sc->kbdc, 0);
1671 /* restore ports and interrupt */
1672 if (!set_controller_command_byte(sc->kbdc,
1673 kbdc_get_device_mask(sc->kbdc),
1674 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1675 /* CONTROLLER ERROR; this is serious, we may have
1676 * been left with the inaccessible keyboard and
1677 * the disabled mouse interrupt.
1682 kbdc_lock(sc->kbdc, FALSE);
1687 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1689 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1691 mousestatus_t status;
1692 #if (defined(MOUSE_GETVARS))
1701 /* Perform IOCTL command */
1704 case OLD_MOUSE_GETHWINFO:
1706 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1707 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1708 ((old_mousehw_t *)addr)->type = sc->hw.type;
1709 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1713 case MOUSE_GETHWINFO:
1715 *(mousehw_t *)addr = sc->hw;
1716 if (sc->mode.level == PSM_LEVEL_BASE)
1717 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1721 case OLD_MOUSE_GETMODE:
1723 switch (sc->mode.level) {
1724 case PSM_LEVEL_BASE:
1725 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1727 case PSM_LEVEL_STANDARD:
1728 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1730 case PSM_LEVEL_NATIVE:
1731 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1734 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1735 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1736 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1742 *(mousemode_t *)addr = sc->mode;
1743 ((mousemode_t *)addr)->resolution =
1744 MOUSE_RES_LOW - sc->mode.resolution;
1745 switch (sc->mode.level) {
1746 case PSM_LEVEL_BASE:
1747 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1748 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1750 case PSM_LEVEL_STANDARD:
1751 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1752 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1753 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1754 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1756 case PSM_LEVEL_NATIVE:
1757 /* FIXME: this isn't quite correct... XXX */
1758 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1764 case OLD_MOUSE_SETMODE:
1766 if (cmd == OLD_MOUSE_SETMODE) {
1767 mode.rate = ((old_mousemode_t *)addr)->rate;
1769 * resolution old I/F new I/F
1776 if (((old_mousemode_t *)addr)->resolution > 0)
1777 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1778 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1781 mode = *(mousemode_t *)addr;
1784 /* adjust and validate parameters. */
1785 if (mode.rate > UCHAR_MAX)
1788 mode.rate = sc->dflt_mode.rate;
1789 else if (mode.rate == -1)
1790 /* don't change the current setting */
1792 else if (mode.rate < 0)
1794 if (mode.resolution >= UCHAR_MAX)
1796 if (mode.resolution >= 200)
1797 mode.resolution = MOUSE_RES_HIGH;
1798 else if (mode.resolution >= 100)
1799 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1800 else if (mode.resolution >= 50)
1801 mode.resolution = MOUSE_RES_MEDIUMLOW;
1802 else if (mode.resolution > 0)
1803 mode.resolution = MOUSE_RES_LOW;
1804 if (mode.resolution == MOUSE_RES_DEFAULT)
1805 mode.resolution = sc->dflt_mode.resolution;
1806 else if (mode.resolution == -1)
1807 /* don't change the current setting */
1809 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1810 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1811 if (mode.level == -1)
1812 /* don't change the current setting */
1813 mode.level = sc->mode.level;
1814 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1816 if (mode.accelfactor == -1)
1817 /* don't change the current setting */
1818 mode.accelfactor = sc->mode.accelfactor;
1819 else if (mode.accelfactor < 0)
1822 /* don't allow anybody to poll the keyboard controller */
1823 error = block_mouse_data(sc, &command_byte);
1827 /* set mouse parameters */
1829 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1830 if (mode.resolution >= 0)
1831 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1832 set_mouse_scaling(sc->kbdc, 1);
1833 get_mouse_status(sc->kbdc, stat, 0, 3);
1836 sc->mode.rate = mode.rate;
1837 sc->mode.resolution = mode.resolution;
1838 sc->mode.accelfactor = mode.accelfactor;
1839 sc->mode.level = mode.level;
1842 unblock_mouse_data(sc, command_byte);
1845 case MOUSE_GETLEVEL:
1846 *(int *)addr = sc->mode.level;
1849 case MOUSE_SETLEVEL:
1850 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1852 sc->mode.level = *(int *)addr;
1855 case MOUSE_GETSTATUS:
1857 status = sc->status;
1858 sc->status.flags = 0;
1859 sc->status.obutton = sc->status.button;
1860 sc->status.button = 0;
1865 *(mousestatus_t *)addr = status;
1868 #if (defined(MOUSE_GETVARS))
1870 var = (mousevar_t *)addr;
1871 bzero(var, sizeof(*var));
1873 var->var[0] = MOUSE_VARS_PS2_SIG;
1874 var->var[1] = sc->config;
1875 var->var[2] = sc->flags;
1881 #endif /* MOUSE_GETVARS */
1883 case MOUSE_READSTATE:
1884 case MOUSE_READDATA:
1885 data = (mousedata_t *)addr;
1886 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1889 error = block_mouse_data(sc, &command_byte);
1892 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1893 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1895 unblock_mouse_data(sc, command_byte);
1898 #if (defined(MOUSE_SETRESOLUTION))
1899 case MOUSE_SETRESOLUTION:
1900 mode.resolution = *(int *)addr;
1901 if (mode.resolution >= UCHAR_MAX)
1903 else if (mode.resolution >= 200)
1904 mode.resolution = MOUSE_RES_HIGH;
1905 else if (mode.resolution >= 100)
1906 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1907 else if (mode.resolution >= 50)
1908 mode.resolution = MOUSE_RES_MEDIUMLOW;
1909 else if (mode.resolution > 0)
1910 mode.resolution = MOUSE_RES_LOW;
1911 if (mode.resolution == MOUSE_RES_DEFAULT)
1912 mode.resolution = sc->dflt_mode.resolution;
1913 else if (mode.resolution == -1)
1914 mode.resolution = sc->mode.resolution;
1915 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1916 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1918 error = block_mouse_data(sc, &command_byte);
1921 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1922 if (sc->mode.resolution != mode.resolution)
1924 unblock_mouse_data(sc, command_byte);
1926 #endif /* MOUSE_SETRESOLUTION */
1928 #if (defined(MOUSE_SETRATE))
1930 mode.rate = *(int *)addr;
1931 if (mode.rate > UCHAR_MAX)
1934 mode.rate = sc->dflt_mode.rate;
1935 else if (mode.rate < 0)
1936 mode.rate = sc->mode.rate;
1938 error = block_mouse_data(sc, &command_byte);
1941 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1942 if (sc->mode.rate != mode.rate)
1944 unblock_mouse_data(sc, command_byte);
1946 #endif /* MOUSE_SETRATE */
1948 #if (defined(MOUSE_SETSCALING))
1949 case MOUSE_SETSCALING:
1950 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1953 error = block_mouse_data(sc, &command_byte);
1956 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1958 unblock_mouse_data(sc, command_byte);
1960 #endif /* MOUSE_SETSCALING */
1962 #if (defined(MOUSE_GETHWID))
1964 error = block_mouse_data(sc, &command_byte);
1967 sc->hw.hwid &= ~0x00ff;
1968 sc->hw.hwid |= get_aux_id(sc->kbdc);
1969 *(int *)addr = sc->hw.hwid & 0x00ff;
1970 unblock_mouse_data(sc, command_byte);
1972 #endif /* MOUSE_GETHWID */
1982 psmtimeout(void *arg)
1984 struct psm_softc *sc;
1987 sc = (struct psm_softc *)arg;
1989 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1991 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1993 kbdc_lock(sc->kbdc, FALSE);
1995 sc->watchdog = TRUE;
1997 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
2000 static int psmhz = 20;
2001 SYSCTL_INT(_debug, OID_AUTO, psmhz, CTLFLAG_RW, &psmhz, 0, "");
2003 static int psm_soft_timeout = 500000; /* 0.5 sec */
2004 SYSCTL_INT(_debug, OID_AUTO, psm_soft_timeout, CTLFLAG_RW,
2005 &psm_soft_timeout, 0, "");
2007 static int psmerrsecs = 2;
2008 SYSCTL_INT(_debug, OID_AUTO, psmerrsecs, CTLFLAG_RW, &psmerrsecs, 0, "");
2009 static int psmerrusecs = 0;
2010 SYSCTL_INT(_debug, OID_AUTO, psmerrusecs, CTLFLAG_RW, &psmerrusecs, 0, "");
2011 static int psmsecs = 0;
2012 SYSCTL_INT(_debug, OID_AUTO, psmsecs, CTLFLAG_RW, &psmsecs, 0, "");
2013 static int psmusecs = 500000;
2014 SYSCTL_INT(_debug, OID_AUTO, psmusecs, CTLFLAG_RW, &psmusecs, 0, "");
2019 struct psm_softc *sc = arg;
2026 /* read until there is nothing to read */
2027 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2029 pb = &sc->pqueue[sc->pqueue_end];
2030 /* discard the byte if the device is not open */
2031 if ((sc->state & PSM_OPEN) == 0)
2034 getmicrouptime(&now);
2035 if ((pb->inputbytes > 0) && timevalcmp(&now, &sc->inputtimeout, >)) {
2037 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2042 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2043 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2044 timevaladd(&sc->inputtimeout, &now);
2046 pb->ipacket[pb->inputbytes++] = c;
2047 if (pb->inputbytes < sc->mode.packetsize)
2051 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2052 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2053 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]);
2058 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2060 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x) %d"
2061 " cmds since last error.\n",
2062 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2063 sc->cmdcount - sc->lasterr);
2066 sc->lasterr = sc->cmdcount;
2069 sc->lastinputerr = now;
2070 if (sc->syncerrors < sc->mode.packetsize) {
2072 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2075 bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
2076 } else if (sc->syncerrors == sc->mode.packetsize) {
2078 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2081 disable_aux_dev(sc->kbdc);
2082 enable_aux_dev(sc->kbdc);
2083 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2085 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2088 bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
2089 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2091 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2093 reinitialize(sc, TRUE);
2097 /* if this packet is at all bogus then drop the packet. */
2099 !timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, &now)) {
2106 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2109 * If we've filled the queue then call the softintr ourselves,
2110 * otherwise schedule the interrupt for later.
2112 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2113 (sc->pqueue_end == sc->pqueue_start)) {
2114 if ((sc->state & PSM_SOFTARMED) != 0) {
2115 sc->state &= ~PSM_SOFTARMED;
2116 untimeout(psmsoftintr, arg, sc->softcallout);
2119 } else if ((sc->state & PSM_SOFTARMED) == 0) {
2120 sc->state |= PSM_SOFTARMED;
2121 sc->softcallout = timeout(psmsoftintr, arg,
2122 psmhz < 1 ? 1 : (hz/psmhz));
2128 psmsoftintr(void *arg)
2131 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
2132 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
2134 static int butmap[8] = {
2138 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2140 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
2141 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
2142 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
2144 static int butmap_versapad[8] = {
2150 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2152 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
2154 register struct psm_softc *sc = arg;
2163 getmicrouptime(&sc->lastsoftintr);
2169 pb = &sc->pqueue[sc->pqueue_start];
2172 * A kludge for Kensington device!
2173 * The MSB of the horizontal count appears to be stored in
2176 if (sc->hw.model == MOUSE_MODEL_THINK)
2177 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2179 /* ignore the overflow bits... */
2180 x = (c & MOUSE_PS2_XNEG) ? pb->ipacket[1] - 256 : pb->ipacket[1];
2181 y = (c & MOUSE_PS2_YNEG) ? pb->ipacket[2] - 256 : pb->ipacket[2];
2183 ms.obutton = sc->button; /* previous button state */
2184 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2185 /* `tapping' action */
2186 if (sc->config & PSM_CONFIG_FORCETAP)
2187 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2189 switch (sc->hw.model) {
2191 case MOUSE_MODEL_EXPLORER:
2193 * b7 b6 b5 b4 b3 b2 b1 b0
2194 * byte 1: oy ox sy sx 1 M R L
2195 * byte 2: x x x x x x x x
2196 * byte 3: y y y y y y y y
2197 * byte 4: * * S2 S1 s d2 d1 d0
2199 * L, M, R, S1, S2: left, middle, right and side buttons
2200 * s: wheel data sign bit
2203 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2204 ? (pb->ipacket[3] & 0x0f) - 16 : (pb->ipacket[3] & 0x0f);
2205 ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2206 ? MOUSE_BUTTON4DOWN : 0;
2207 ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2208 ? MOUSE_BUTTON5DOWN : 0;
2211 case MOUSE_MODEL_INTELLI:
2212 case MOUSE_MODEL_NET:
2213 /* wheel data is in the fourth byte */
2214 z = (char)pb->ipacket[3];
2215 /* some mice may send 7 when there is no Z movement?! XXX */
2216 if ((z >= 7) || (z <= -7))
2218 /* some compatible mice have additional buttons */
2219 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2220 ? MOUSE_BUTTON4DOWN : 0;
2221 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2222 ? MOUSE_BUTTON5DOWN : 0;
2225 case MOUSE_MODEL_MOUSEMANPLUS:
2227 * PS2++ protocl packet
2229 * b7 b6 b5 b4 b3 b2 b1 b0
2230 * byte 1: * 1 p3 p2 1 * * *
2231 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2233 * p3-p0: packet type
2234 * c1, c2: c1 & c2 == 1, if p2 == 0
2235 * c1 & c2 == 0, if p2 == 1
2237 * packet type: 0 (device type)
2238 * See comments in enable_mmanplus() below.
2240 * packet type: 1 (wheel data)
2242 * b7 b6 b5 b4 b3 b2 b1 b0
2243 * byte 3: h * B5 B4 s d2 d1 d0
2245 * h: 1, if horizontal roller data
2246 * 0, if vertical roller data
2247 * B4, B5: button 4 and 5
2249 * d2-d0: roller data
2251 * packet type: 2 (reserved)
2253 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2255 && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2256 /* the extended data packet encodes button and wheel events */
2257 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
2259 /* wheel data packet */
2261 if (pb->ipacket[2] & 0x80) {
2262 /* horizontal roller count - ignore it XXX*/
2264 /* vertical roller count */
2265 z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2266 ? (pb->ipacket[2] & 0x0f) - 16
2267 : (pb->ipacket[2] & 0x0f);
2269 ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2270 ? MOUSE_BUTTON4DOWN : 0;
2271 ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2272 ? MOUSE_BUTTON5DOWN : 0;
2275 /* this packet type is reserved by Logitech... */
2277 * IBM ScrollPoint Mouse uses this packet type to
2278 * encode both vertical and horizontal scroll movement.
2281 /* horizontal count */
2282 if (pb->ipacket[2] & 0x0f)
2283 z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2284 /* vertical count */
2285 if (pb->ipacket[2] & 0xf0)
2286 z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2288 /* vertical count */
2289 z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG)
2290 ? ((pb->ipacket[2] >> 4) & 0x0f) - 16
2291 : ((pb->ipacket[2] >> 4) & 0x0f);
2292 /* horizontal count */
2293 w = (pb->ipacket[2] & MOUSE_SPOINT_WNEG)
2294 ? (pb->ipacket[2] & 0x0f) - 16
2295 : (pb->ipacket[2] & 0x0f);
2299 /* device type packet - shouldn't happen */
2303 ms.button = ms.obutton;
2305 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2306 "0x%02x 0x%02x 0x%02x\n",
2307 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2308 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]);
2312 /* preserve button states */
2313 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2317 case MOUSE_MODEL_GLIDEPOINT:
2318 /* `tapping' action */
2319 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2322 case MOUSE_MODEL_NETSCROLL:
2323 /* three addtional bytes encode buttons and wheel events */
2324 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2325 ? MOUSE_BUTTON4DOWN : 0;
2326 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2327 ? MOUSE_BUTTON5DOWN : 0;
2328 z = (pb->ipacket[3] & MOUSE_PS2_XNEG)
2329 ? pb->ipacket[4] - 256 : pb->ipacket[4];
2332 case MOUSE_MODEL_THINK:
2333 /* the fourth button state in the first byte */
2334 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2337 case MOUSE_MODEL_VERSAPAD:
2338 /* VersaPad PS/2 absolute mode message format
2340 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2341 * ipacket[0]: 1 1 0 A 1 L T R
2342 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2343 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2344 * ipacket[3]: 1 1 1 A 1 L T R
2345 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2346 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2349 * R: right physical mouse button (1=on)
2350 * T: touch pad virtual button (1=tapping)
2351 * L: left physical mouse button (1=on)
2352 * A: position data is valid (1=valid)
2353 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2354 * V: vertical data (12bit signed integer. V11 is sign bit.)
2357 * Tapping is mapped to MOUSE_BUTTON4.
2359 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2360 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2362 if (c & MOUSE_PS2VERSA_IN_USE) {
2363 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
2364 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
2369 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2372 if (x < 0) /* XXX */
2381 sc->flags |= PSM_FLAGS_FINGERDOWN;
2386 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2388 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2389 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2392 case MOUSE_MODEL_4D:
2394 * b7 b6 b5 b4 b3 b2 b1 b0
2395 * byte 1: s2 d2 s1 d1 1 M R L
2396 * byte 2: sx x x x x x x x
2397 * byte 3: sy y y y y y y y
2399 * s1: wheel 1 direction
2401 * s2: wheel 2 direction
2404 x = (pb->ipacket[1] & 0x80) ? pb->ipacket[1] - 256 : pb->ipacket[1];
2405 y = (pb->ipacket[2] & 0x80) ? pb->ipacket[2] - 256 : pb->ipacket[2];
2406 switch (c & MOUSE_4D_WHEELBITS) {
2413 case 0x40: /* 2nd wheel turning right XXX */
2416 case 0xc0: /* 2nd wheel turning left XXX */
2422 case MOUSE_MODEL_4DPLUS:
2423 if ((x < 16 - 256) && (y < 16 - 256)) {
2425 * b7 b6 b5 b4 b3 b2 b1 b0
2426 * byte 1: 0 0 1 1 1 M R L
2427 * byte 2: 0 0 0 0 1 0 0 0
2428 * byte 3: 0 0 0 0 S s d1 d0
2430 * L, M, R, S: left, middle, right and side buttons
2431 * s: wheel data sign bit
2435 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2436 ms.button |= MOUSE_BUTTON4DOWN;
2437 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2438 ? ((pb->ipacket[2] & 0x07) - 8)
2439 : (pb->ipacket[2] & 0x07) ;
2441 /* preserve previous button states */
2442 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2446 case MOUSE_MODEL_GENERIC:
2452 if (sc->mode.accelfactor >= 1) {
2454 x = x * x / sc->mode.accelfactor;
2457 if (c & MOUSE_PS2_XNEG)
2461 y = y * y / sc->mode.accelfactor;
2464 if (c & MOUSE_PS2_YNEG)
2472 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2473 | (ms.obutton ^ ms.button);
2475 if (sc->mode.level < PSM_LEVEL_NATIVE)
2476 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
2478 sc->status.flags |= ms.flags;
2479 sc->status.dx += ms.dx;
2480 sc->status.dy += ms.dy;
2481 sc->status.dz += ms.dz;
2482 sc->status.button = ms.button;
2483 sc->button = ms.button;
2485 sc->watchdog = FALSE;
2488 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
2489 l = imin(pb->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2490 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2491 if (pb->inputbytes > l)
2492 bcopy(&pb->ipacket[l], &sc->queue.buf[0], pb->inputbytes - l);
2494 (sc->queue.tail + pb->inputbytes) % sizeof(sc->queue.buf);
2495 sc->queue.count += pb->inputbytes;
2499 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
2500 sc->pqueue_start = 0;
2501 } while (sc->pqueue_start != sc->pqueue_end);
2502 if (sc->state & PSM_ASLP) {
2503 sc->state &= ~PSM_ASLP;
2506 selwakeuppri(&sc->rsel, PZERO);
2507 sc->state &= ~PSM_SOFTARMED;
2512 psmpoll(dev_t dev, int events, struct thread *td)
2514 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2518 /* Return true if a mouse event available */
2520 if (events & (POLLIN | POLLRDNORM)) {
2521 if (sc->queue.count > 0)
2522 revents |= events & (POLLIN | POLLRDNORM);
2524 selrecord(td, &sc->rsel);
2531 /* vendor/model specific routines */
2533 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2535 if (set_mouse_resolution(kbdc, res) != res)
2537 if (set_mouse_scaling(kbdc, scale)
2538 && set_mouse_scaling(kbdc, scale)
2539 && set_mouse_scaling(kbdc, scale)
2540 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2546 mouse_ext_command(KBDC kbdc, int command)
2550 c = (command >> 6) & 0x03;
2551 if (set_mouse_resolution(kbdc, c) != c)
2553 c = (command >> 4) & 0x03;
2554 if (set_mouse_resolution(kbdc, c) != c)
2556 c = (command >> 2) & 0x03;
2557 if (set_mouse_resolution(kbdc, c) != c)
2559 c = (command >> 0) & 0x03;
2560 if (set_mouse_resolution(kbdc, c) != c)
2566 /* Logitech MouseMan Cordless II */
2568 enable_lcordless(struct psm_softc *sc)
2573 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2575 if (status[1] == PSMD_RES_HIGH)
2577 ch = (status[0] & 0x07) - 1; /* channel # */
2578 if ((ch <= 0) || (ch > 4))
2581 * status[1]: always one?
2582 * status[2]: battery status? (0-100)
2588 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2590 enable_groller(struct psm_softc *sc)
2595 * The special sequence to enable the fourth button and the
2596 * roller. Immediately after this sequence check status bytes.
2597 * if the mouse is NetScroll, the second and the third bytes are
2602 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2603 * look like the following.
2605 * byte 1 bit 7 always 0
2606 * bit 6 stream mode (0)
2607 * bit 5 disabled (0)
2608 * bit 4 1:1 scaling (0)
2610 * bit 0-2 button status
2611 * byte 2 resolution (PSMD_RES_HIGH)
2612 * byte 3 report rate (?)
2615 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2617 if ((status[1] != '3') || (status[2] != 'D'))
2619 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2624 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2626 enable_gmouse(struct psm_softc *sc)
2631 * The special sequence to enable the middle, "rubber" button.
2632 * Immediately after this sequence check status bytes.
2633 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2634 * the second and the third bytes are '3' and 'U'.
2635 * NOTE: NetMouse reports that it has three buttons although it has
2636 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2637 * say they have three buttons too and they do have a button on the
2640 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2642 if ((status[1] != '3') || (status[2] != 'U'))
2647 /* ALPS GlidePoint */
2649 enable_aglide(struct psm_softc *sc)
2654 * The special sequence to obtain ALPS GlidePoint specific
2655 * information. Immediately after this sequence, status bytes will
2656 * contain something interesting.
2657 * NOTE: ALPS produces several models of GlidePoint. Some of those
2658 * do not respond to this sequence, thus, cannot be detected this way.
2660 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2662 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2664 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2669 /* Kensington ThinkingMouse/Trackball */
2671 enable_kmouse(struct psm_softc *sc)
2673 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2674 KBDC kbdc = sc->kbdc;
2680 id1 = get_aux_id(kbdc);
2681 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2684 * The device is now in the native mode? It returns a different
2687 id2 = get_aux_id(kbdc);
2688 if ((id1 == id2) || (id2 != 2))
2691 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2694 /* at this point, resolution is LOW, sampling rate is 10/sec */
2695 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2700 * The special sequence to enable the third and fourth buttons.
2701 * Otherwise they behave like the first and second buttons.
2703 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2704 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2709 * At this point, the device is using default resolution and
2710 * sampling rate for the native mode.
2712 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2714 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2717 /* the device appears be enabled by this sequence, diable it for now */
2718 disable_aux_dev(kbdc);
2719 empty_aux_buffer(kbdc, 5);
2724 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2726 enable_mmanplus(struct psm_softc *sc)
2728 KBDC kbdc = sc->kbdc;
2731 /* the special sequence to enable the fourth button and the roller. */
2733 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2734 * must be called exactly three times since the last RESET command
2735 * before this sequence. XXX
2737 if (!set_mouse_scaling(kbdc, 1))
2739 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2741 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2745 * PS2++ protocl, packet type 0
2747 * b7 b6 b5 b4 b3 b2 b1 b0
2748 * byte 1: * 1 p3 p2 1 * * *
2749 * byte 2: 1 1 p1 p0 m1 m0 1 0
2750 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2752 * p3-p0: packet type: 0
2753 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2755 /* check constant bits */
2756 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2758 if ((data[1] & 0xc3) != 0xc2)
2760 /* check d3-d0 in byte 2 */
2761 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2764 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2767 sc->hw.hwid &= 0x00ff;
2768 sc->hw.hwid |= data[2] << 8; /* save model ID */
2771 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2772 * the wheel and the fourth button events are encoded in the
2773 * special data packet. The mouse may be put in the IntelliMouse mode
2774 * if it is initialized by the IntelliMouse's method.
2779 /* MS IntelliMouse Explorer */
2781 enable_msexplorer(struct psm_softc *sc)
2783 static unsigned char rate0[] = { 200, 100, 80, };
2784 static unsigned char rate1[] = { 200, 200, 80, };
2785 KBDC kbdc = sc->kbdc;
2789 /* the special sequence to enable the extra buttons and the roller. */
2790 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2791 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2794 /* the device will give the genuine ID only after the above sequence */
2795 id = get_aux_id(kbdc);
2796 if (id != PSM_EXPLORER_ID)
2800 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2803 * XXX: this is a kludge to fool some KVM switch products
2804 * which think they are clever enough to know the 4-byte IntelliMouse
2805 * protocol, and assume any other protocols use 3-byte packets.
2806 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2807 * correctly to the host computer because of this!
2808 * The following sequence is actually IntelliMouse's "wake up"
2809 * sequence; it will make the KVM think the mouse is IntelliMouse
2810 * when it is in fact IntelliMouse Explorer.
2812 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2813 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2816 id = get_aux_id(kbdc);
2821 /* MS IntelliMouse */
2823 enable_msintelli(struct psm_softc *sc)
2826 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2827 * probe routine and act like IntelliMouse.
2830 static unsigned char rate[] = { 200, 100, 80, };
2831 KBDC kbdc = sc->kbdc;
2835 /* the special sequence to enable the third button and the roller. */
2836 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2837 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2840 /* the device will give the genuine ID only after the above sequence */
2841 id = get_aux_id(kbdc);
2842 if (id != PSM_INTELLI_ID)
2851 /* A4 Tech 4D Mouse */
2853 enable_4dmouse(struct psm_softc *sc)
2856 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2859 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2860 KBDC kbdc = sc->kbdc;
2864 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2865 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2868 id = get_aux_id(kbdc);
2870 * WinEasy 4D, 4 Way Scroll 4D: 6
2871 * Cable-Free 4D: 8 (4DPLUS)
2872 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2874 if (id != PSM_4DMOUSE_ID)
2878 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2883 /* A4 Tech 4D+ Mouse */
2885 enable_4dplus(struct psm_softc *sc)
2888 * Newer wheel mice from A4 Tech seem to use this protocol.
2889 * Older models are recognized as either 4D Mouse or IntelliMouse.
2891 KBDC kbdc = sc->kbdc;
2895 * enable_4dmouse() already issued the following ID sequence...
2896 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2899 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2900 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2905 id = get_aux_id(kbdc);
2910 case PSM_4DPLUS_RFSW35_ID:
2922 /* Interlink electronics VersaPad */
2924 enable_versapad(struct psm_softc *sc)
2926 KBDC kbdc = sc->kbdc;
2929 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2930 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2931 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2932 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2933 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2934 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2935 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2937 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2939 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2941 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2943 return TRUE; /* PS/2 absolute mode */
2947 psmresume(device_t dev)
2949 struct psm_softc *sc = device_get_softc(dev);
2950 int unit = device_get_unit(dev);
2954 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2956 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2959 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2961 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2963 * Release the blocked process; it must be notified that the device
2964 * cannot be accessed anymore.
2966 sc->state &= ~PSM_ASLP;
2971 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2976 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
2979 * This sucks up assignments from PNPBIOS and ACPI.
2983 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
2984 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
2985 * can be probed and attached only after the AT keyboard controller is
2986 * attached, we shall quietly reserve the IRQ resource for later use.
2987 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
2988 * copy the IRQ resource to the PS/2 mouse device instance hanging
2989 * under the keyboard controller, then probe and attach it.
2992 static devclass_t psmcpnp_devclass;
2994 static device_probe_t psmcpnp_probe;
2995 static device_attach_t psmcpnp_attach;
2997 static device_method_t psmcpnp_methods[] = {
2998 DEVMETHOD(device_probe, psmcpnp_probe),
2999 DEVMETHOD(device_attach, psmcpnp_attach),
3004 static driver_t psmcpnp_driver = {
3005 PSMCPNP_DRIVER_NAME,
3010 static struct isa_pnp_id psmcpnp_ids[] = {
3011 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
3012 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
3013 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
3014 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
3015 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
3016 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
3017 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
3018 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
3019 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
3020 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
3025 create_a_copy(device_t atkbdc, device_t me)
3030 /* find the PS/2 mouse device instance under the keyboard controller */
3031 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
3032 device_get_unit(atkbdc));
3035 if (device_get_state(psm) != DS_NOTPRESENT)
3038 /* move our resource to the found device */
3039 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
3040 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
3042 /* ...then probe and attach it */
3043 return device_probe_and_attach(psm);
3047 psmcpnp_probe(device_t dev)
3049 struct resource *res;
3053 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
3057 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
3058 * to the PS/2 mouse device node. But, some buggy PnP BIOS
3059 * declares the PS/2 mouse device node without an IRQ resource!
3060 * If this happens, we shall refer to device hints.
3061 * If we still don't find it there, use a hardcoded value... XXX
3064 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
3066 if (resource_long_value(PSM_DRIVER_NAME,
3067 device_get_unit(dev), "irq", &irq) != 0)
3069 device_printf(dev, "irq resource info is missing; "
3070 "assuming irq %ld\n", irq);
3071 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
3073 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3075 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
3081 return ((res == NULL) ? ENXIO : 0);
3085 psmcpnp_attach(device_t dev)
3090 /* find the keyboard controller, which may be on acpi* or isa* bus */
3091 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
3092 device_get_unit(dev));
3093 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
3094 create_a_copy(atkbdc, dev);
3097 * If we don't have the AT keyboard controller yet,
3098 * just reserve the IRQ for later use...
3099 * (See psmidentify() above.)
3102 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
3109 * Return true if 'now' is earlier than (start + (secs.usecs)).
3110 * Now may be NULL and the function will fetch the current time from
3111 * getmicrouptime(), or a cached 'now' can be passed in.
3112 * All values should be numbers derived from getmicrouptime().
3115 timeelapsed(start, secs, usecs, now)
3116 const struct timeval *start, *now;
3119 struct timeval snow, tv;
3121 /* if there is no 'now' passed in, the get it as a convience. */
3123 getmicrouptime(&snow);
3129 timevaladd(&tv, start);
3130 return (timevalcmp(&tv, now, <));
3133 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
3134 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);