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 synapticshw_t synhw; /* Synaptics-specific 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 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
168 int pqueue_start; /* start of data in queue */
169 int pqueue_end; /* end of data in queue */
170 int button; /* the latest button state */
171 int xold; /* previous absolute X position */
172 int yold; /* previous absolute Y position */
173 int syncerrors; /* XXX: KILL ME! */
174 struct timeval inputtimeout;
175 struct timeval lastsoftintr; /* time of last soft interrupt */
176 struct timeval lastinputerr; /* time last sync error happened */
177 int watchdog; /* watchdog timer flag */
178 struct callout_handle callout; /* watchdog timer call out */
179 struct callout_handle softcallout; /* buffer timer call out */
185 static devclass_t psm_devclass;
186 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
188 /* driver state flags (state) */
189 #define PSM_VALID 0x80
190 #define PSM_OPEN 1 /* Device is open */
191 #define PSM_ASLP 2 /* Waiting for mouse data */
192 #define PSM_SOFTARMED 4 /* Software interrupt armed */
194 /* driver configuration flags (config) */
195 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
196 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
197 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
198 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
199 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
200 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
201 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
202 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
203 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
204 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
206 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \
208 | PSM_CONFIG_NOCHECKSYNC \
209 | PSM_CONFIG_SYNCHACK \
210 | PSM_CONFIG_NOIDPROBE \
211 | PSM_CONFIG_NORESET \
212 | PSM_CONFIG_FORCETAP \
213 | PSM_CONFIG_IGNPORTERROR \
214 | PSM_CONFIG_HOOKRESUME \
215 | PSM_CONFIG_INITAFTERSUSPEND)
217 /* other flags (flags) */
218 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
220 /* for backward compatibility */
221 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
222 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
223 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
225 typedef struct old_mousehw {
232 typedef struct old_mousemode {
239 /* packet formatting function */
240 typedef int packetfunc_t(struct psm_softc *, unsigned char *,
241 int *, int, mousestatus_t *);
243 /* function prototypes */
244 static void psmidentify(driver_t *, device_t);
245 static int psmprobe(device_t);
246 static int psmattach(device_t);
247 static int psmdetach(device_t);
248 static int psmresume(device_t);
250 static d_open_t psmopen;
251 static d_close_t psmclose;
252 static d_read_t psmread;
253 static d_ioctl_t psmioctl;
254 static d_poll_t psmpoll;
256 static int enable_aux_dev(KBDC);
257 static int disable_aux_dev(KBDC);
258 static int get_mouse_status(KBDC, int *, int, int);
259 static int get_aux_id(KBDC);
260 static int set_mouse_sampling_rate(KBDC, int);
261 static int set_mouse_scaling(KBDC, int);
262 static int set_mouse_resolution(KBDC, int);
263 static int set_mouse_mode(KBDC);
264 static int get_mouse_buttons(KBDC);
265 static int is_a_mouse(int);
266 static void recover_from_error(KBDC);
267 static int restore_controller(KBDC, int);
268 static int doinitialize(struct psm_softc *, mousemode_t *);
269 static int doopen(struct psm_softc *, int);
270 static int reinitialize(struct psm_softc *, int);
271 static char *model_name(int);
272 static void psmsoftintr(void *);
273 static void psmintr(void *);
274 static void psmtimeout(void *);
275 static int timeelapsed(const struct timeval *,
276 int, int, const struct timeval *);
277 static void dropqueue(struct psm_softc *);
278 static void flushpackets(struct psm_softc *);
280 /* vendor specific features */
281 typedef int probefunc_t(struct psm_softc *);
283 static int mouse_id_proc1(KBDC, int, int, int *);
284 static int mouse_ext_command(KBDC, int);
285 static probefunc_t enable_groller;
286 static probefunc_t enable_gmouse;
287 static probefunc_t enable_aglide;
288 static probefunc_t enable_kmouse;
289 static probefunc_t enable_msexplorer;
290 static probefunc_t enable_msintelli;
291 static probefunc_t enable_4dmouse;
292 static probefunc_t enable_4dplus;
293 static probefunc_t enable_mmanplus;
294 static probefunc_t enable_synaptics;
295 static probefunc_t enable_versapad;
296 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, unsigned char *);
300 unsigned char syncmask;
302 probefunc_t *probefunc;
305 * WARNING: the order of probe is very important. Don't mess it
306 * unless you know what you are doing.
308 { MOUSE_MODEL_NET, /* Genius NetMouse */
309 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
310 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
311 0xc8, 6, enable_groller, },
312 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
313 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
314 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
315 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
316 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
317 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
318 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
319 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
320 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
321 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
322 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
323 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
324 { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */
325 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
326 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
327 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
328 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
329 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics, },
330 { MOUSE_MODEL_GENERIC,
331 0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
333 #define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
335 /* device driver declarateion */
336 static device_method_t psm_methods[] = {
337 /* Device interface */
338 DEVMETHOD(device_identify, psmidentify),
339 DEVMETHOD(device_probe, psmprobe),
340 DEVMETHOD(device_attach, psmattach),
341 DEVMETHOD(device_detach, psmdetach),
342 DEVMETHOD(device_resume, psmresume),
347 static driver_t psm_driver = {
350 sizeof(struct psm_softc),
354 static struct cdevsw psm_cdevsw = {
355 .d_version = D_VERSION,
356 .d_flags = D_NEEDGIANT,
362 .d_name = PSM_DRIVER_NAME,
365 /* debug message level */
366 static int verbose = PSM_DEBUG;
368 /* device I/O routines */
370 enable_aux_dev(KBDC kbdc)
374 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
376 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
378 return (res == PSM_ACK);
382 disable_aux_dev(KBDC kbdc)
386 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
388 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
390 return (res == PSM_ACK);
394 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
403 cmd = PSMC_SEND_DEV_STATUS;
406 cmd = PSMC_SEND_DEV_DATA;
409 empty_aux_buffer(kbdc, 5);
410 res = send_aux_command(kbdc, cmd);
412 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
413 (flag == 1) ? "DATA" : "STATUS", res);
417 for (i = 0; i < len; ++i) {
418 status[i] = read_aux_data(kbdc);
424 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
425 (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
432 get_aux_id(KBDC kbdc)
437 empty_aux_buffer(kbdc, 5);
438 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
440 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
447 id = read_aux_data(kbdc);
449 log(LOG_DEBUG, "psm: device ID: %04x\n", id);
455 set_mouse_sampling_rate(KBDC kbdc, int rate)
459 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
461 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
463 return ((res == PSM_ACK) ? rate : -1);
467 set_mouse_scaling(KBDC kbdc, int scale)
474 scale = PSMC_SET_SCALING11;
477 scale = PSMC_SET_SCALING21;
480 res = send_aux_command(kbdc, scale);
482 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
483 (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
485 return (res == PSM_ACK);
488 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
490 set_mouse_resolution(KBDC kbdc, int val)
494 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
496 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
498 return ((res == PSM_ACK) ? val : -1);
502 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
503 * re-enabled by calling `enable_aux_dev()'
506 set_mouse_mode(KBDC kbdc)
510 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
512 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
514 return (res == PSM_ACK);
518 get_mouse_buttons(KBDC kbdc)
520 int c = 2; /* assume two buttons by default */
524 * NOTE: a special sequence to obtain Logitech Mouse specific
525 * information: set resolution to 25 ppi, set scaling to 1:1, set
526 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
527 * mouse status bytes is the number of available buttons.
528 * Some manufactures also support this sequence.
530 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
532 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
533 && set_mouse_scaling(kbdc, 1)
534 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
541 /* misc subroutines */
543 * Someday, I will get the complete list of valid pointing devices and
550 static int valid_ids[] = {
551 PSM_MOUSE_ID, /* mouse */
552 PSM_BALLPOINT_ID, /* ballpoint device */
553 PSM_INTELLI_ID, /* Intellimouse */
554 PSM_EXPLORER_ID, /* Intellimouse Explorer */
555 -1 /* end of table */
559 for (i = 0; valid_ids[i] >= 0; ++i)
560 if (valid_ids[i] == id)
569 model_name(int model)
575 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
576 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
577 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
578 { MOUSE_MODEL_THINK, "ThinkingMouse" },
579 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
580 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
581 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
582 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
583 { MOUSE_MODEL_4D, "4D Mouse" },
584 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
585 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
586 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
587 { MOUSE_MODEL_UNKNOWN, NULL },
591 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
592 if (models[i].model_code == model)
593 return models[i].model_name;
599 recover_from_error(KBDC kbdc)
601 /* discard anything left in the output buffer */
602 empty_both_buffers(kbdc, 10);
606 * NOTE: KBDC_RESET_KBD may not restore the communication between the
607 * keyboard and the controller.
612 * NOTE: somehow diagnostic and keyboard port test commands bring the
615 if (!test_controller(kbdc))
616 log(LOG_ERR, "psm: keyboard controller failed.\n");
617 /* if there isn't a keyboard in the system, the following error is OK */
618 if (test_kbd_port(kbdc) != 0) {
620 log(LOG_ERR, "psm: keyboard port failed.\n");
626 restore_controller(KBDC kbdc, int command_byte)
628 empty_both_buffers(kbdc, 10);
630 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
631 log(LOG_ERR, "psm: failed to restore the keyboard controller "
633 empty_both_buffers(kbdc, 10);
636 empty_both_buffers(kbdc, 10);
642 * Re-initialize the aux port and device. The aux port must be enabled
643 * and its interrupt must be disabled before calling this routine.
644 * The aux device will be disabled before returning.
645 * The keyboard controller must be locked via `kbdc_lock()' before
646 * calling this routine.
649 doinitialize(struct psm_softc *sc, mousemode_t *mode)
651 KBDC kbdc = sc->kbdc;
655 switch((i = test_aux_port(kbdc))) {
656 case 1: /* ignore these errors */
661 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
664 case 0: /* no error */
666 case -1: /* time out */
668 recover_from_error(kbdc);
669 if (sc->config & PSM_CONFIG_IGNPORTERROR)
671 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
676 if (sc->config & PSM_CONFIG_NORESET) {
678 * Don't try to reset the pointing device. It may possibly be
679 * left in the unknown state, though...
683 * NOTE: some controllers appears to hang the `keyboard' when
684 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
686 if (!reset_aux_dev(kbdc)) {
687 recover_from_error(kbdc);
688 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
694 * both the aux port and the aux device is functioning, see
695 * if the device can be enabled.
697 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
698 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
701 empty_both_buffers(kbdc, 10); /* remove stray data if any */
703 if (sc->config & PSM_CONFIG_NOIDPROBE) {
704 i = GENERIC_MOUSE_ENTRY;
706 /* FIXME: hardware ID, mouse buttons? */
708 /* other parameters */
709 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
710 if ((*vendortype[i].probefunc)(sc)) {
712 log(LOG_ERR, "psm%d: found %s\n",
713 sc->unit, model_name(vendortype[i].model));
719 sc->hw.model = vendortype[i].model;
720 sc->mode.packetsize = vendortype[i].packetsize;
722 /* set mouse parameters */
723 if (mode != (mousemode_t *)NULL) {
725 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
726 if (mode->resolution >= 0)
727 mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
728 set_mouse_scaling(kbdc, 1);
729 set_mouse_mode(kbdc);
732 /* request a data packet and extract sync. bits */
733 if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
734 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
736 sc->mode.syncmask[0] = 0;
738 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
739 /* the NetScroll Mouse will send three more bytes... Ignore them */
740 empty_aux_buffer(kbdc, 5);
743 /* just check the status of the mouse */
744 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
745 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
752 doopen(struct psm_softc *sc, int command_byte)
756 /* enable the mouse device */
757 if (!enable_aux_dev(sc->kbdc)) {
758 /* MOUSE ERROR: failed to enable the mouse because:
759 * 1) the mouse is faulty,
760 * 2) the mouse has been removed(!?)
761 * In the latter case, the keyboard may have hung, and need
762 * recovery procedure...
764 recover_from_error(sc->kbdc);
766 /* FIXME: we could reset the mouse here and try to enable
767 * it again. But it will take long time and it's not a good
768 * idea to disable the keyboard that long...
770 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
771 recover_from_error(sc->kbdc);
775 restore_controller(sc->kbdc, command_byte);
776 /* mark this device is no longer available */
777 sc->state &= ~PSM_VALID;
778 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
784 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
785 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
787 /* enable the aux port and interrupt */
788 if (!set_controller_command_byte(sc->kbdc,
789 kbdc_get_device_mask(sc->kbdc),
790 (command_byte & KBD_KBD_CONTROL_BITS)
791 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
792 /* CONTROLLER ERROR */
793 disable_aux_dev(sc->kbdc);
794 restore_controller(sc->kbdc, command_byte);
795 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
800 /* start the watchdog timer */
801 sc->watchdog = FALSE;
802 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
808 reinitialize(struct psm_softc *sc, int doinit)
814 /* don't let anybody mess with the aux device */
815 if (!kbdc_lock(sc->kbdc, TRUE))
819 /* block our watchdog timer */
820 sc->watchdog = FALSE;
821 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
822 callout_handle_init(&sc->callout);
824 /* save the current controller command byte */
825 empty_both_buffers(sc->kbdc, 10);
826 c = get_controller_command_byte(sc->kbdc);
828 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
831 /* enable the aux port but disable the aux interrupt and the keyboard */
832 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
833 kbdc_get_device_mask(sc->kbdc),
834 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
835 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
836 /* CONTROLLER ERROR */
838 kbdc_lock(sc->kbdc, FALSE);
839 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
845 if (sc->state & PSM_VALID) {
846 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */
847 empty_aux_buffer(sc->kbdc, 10);
852 /* try to detect the aux device; are you still there? */
855 if (doinitialize(sc, &sc->mode)) {
857 sc->state |= PSM_VALID;
859 /* the device has gone! */
860 restore_controller(sc->kbdc, c);
861 sc->state &= ~PSM_VALID;
862 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
869 /* restore the driver state */
870 if ((sc->state & PSM_OPEN) && (err == 0)) {
871 /* enable the aux device and the port again */
874 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
877 /* restore the keyboard port and disable the aux port */
878 if (!set_controller_command_byte(sc->kbdc,
879 kbdc_get_device_mask(sc->kbdc),
880 (c & KBD_KBD_CONTROL_BITS)
881 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
882 /* CONTROLLER ERROR */
883 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
889 kbdc_lock(sc->kbdc, FALSE);
893 /* psm driver entry points */
896 psmidentify(driver_t *driver, device_t parent)
903 unit = device_get_unit(parent);
905 /* always add at least one child */
906 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
910 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
915 * If the PS/2 mouse device has already been reported by ACPI or
916 * PnP BIOS, obtain the IRQ resource from it.
917 * (See psmcpnp_attach() below.)
919 psmc = device_find_child(device_get_parent(parent),
920 PSMCPNP_DRIVER_NAME, unit);
923 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
926 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
929 #define endprobe(v) do { if (bootverbose) \
931 kbdc_set_device_mask(sc->kbdc, mask); \
932 kbdc_lock(sc->kbdc, FALSE); \
937 psmprobe(device_t dev)
939 int unit = device_get_unit(dev);
940 struct psm_softc *sc = device_get_softc(dev);
951 /* see if IRQ is available */
953 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
954 RF_SHAREABLE | RF_ACTIVE);
955 if (sc->intr == NULL) {
957 device_printf(dev, "unable to allocate IRQ\n");
960 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
963 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
964 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
965 /* XXX: for backward compatibility */
966 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
968 #ifdef PSM_RESETAFTERSUSPEND
969 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
971 PSM_CONFIG_HOOKRESUME;
973 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
978 device_set_desc(dev, "PS/2 Mouse");
980 if (!kbdc_lock(sc->kbdc, TRUE)) {
981 printf("psm%d: unable to lock the controller.\n", unit);
988 * NOTE: two bits in the command byte controls the operation of the
989 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
990 * port interrupt (IRQ 12) enable bit (bit 2).
993 /* discard anything left after the keyboard initialization */
994 empty_both_buffers(sc->kbdc, 10);
996 /* save the current command byte; it will be used later */
997 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
998 command_byte = get_controller_command_byte(sc->kbdc);
1000 printf("psm%d: current command byte:%04x\n", unit, command_byte);
1001 if (command_byte == -1) {
1002 /* CONTROLLER ERROR */
1003 printf("psm%d: unable to get the current command byte value.\n",
1009 * disable the keyboard port while probing the aux port, which must be
1010 * enabled during this routine
1012 if (!set_controller_command_byte(sc->kbdc,
1013 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1014 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1015 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1017 * this is CONTROLLER ERROR; I don't know how to recover
1018 * from this error...
1020 restore_controller(sc->kbdc, command_byte);
1021 printf("psm%d: unable to set the command byte.\n", unit);
1024 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1027 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1028 * port exists and is functioning. However, some controllers appears
1029 * to respond with zero even when the aux port doesn't exist. (It may
1030 * be that this is only the case when the controller DOES have the aux
1031 * port but the port is not wired on the motherboard.) The keyboard
1032 * controllers without the port, such as the original AT, are
1033 * supporsed to return with an error code or simply time out. In any
1034 * case, we have to continue probing the port even when the controller
1037 * XXX: some controllers erroneously return the error code 1, 2 or 3
1038 * when it has the perfectly functional aux port. We have to ignore
1039 * this error code. Even if the controller HAS error with the aux
1040 * port, it will be detected later...
1041 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1043 switch ((i = test_aux_port(sc->kbdc))) {
1044 case 1: /* ignore these errors */
1049 printf("psm%d: strange result for test aux port (%d).\n",
1052 case 0: /* no error */
1054 case -1: /* time out */
1055 default: /* error */
1056 recover_from_error(sc->kbdc);
1057 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1059 restore_controller(sc->kbdc, command_byte);
1061 printf("psm%d: the aux port is not functioning (%d).\n",
1066 if (sc->config & PSM_CONFIG_NORESET) {
1068 * Don't try to reset the pointing device. It may possibly be
1069 * left in the unknown state, though...
1073 * NOTE: some controllers appears to hang the `keyboard' when the aux
1074 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1076 * Attempt to reset the controller twice -- this helps
1077 * pierce through some KVM switches. The second reset
1080 if (!reset_aux_dev(sc->kbdc)) {
1081 recover_from_error(sc->kbdc);
1082 restore_controller(sc->kbdc, command_byte);
1084 printf("psm%d: failed to reset the aux device.\n", unit);
1086 } else if (!reset_aux_dev(sc->kbdc)) {
1087 recover_from_error(sc->kbdc);
1089 printf("psm%d: failed to reset the aux device (2).\n",
1095 * both the aux port and the aux device is functioning, see if the
1096 * device can be enabled. NOTE: when enabled, the device will start
1097 * sending data; we shall immediately disable the device once we know
1098 * the device can be enabled.
1100 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1102 recover_from_error(sc->kbdc);
1103 restore_controller(sc->kbdc, command_byte);
1105 printf("psm%d: failed to enable the aux device.\n", unit);
1109 /* save the default values after reset */
1110 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1111 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1112 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1114 sc->dflt_mode.rate = sc->mode.rate = -1;
1115 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1118 /* hardware information */
1119 sc->hw.iftype = MOUSE_IF_PS2;
1121 /* verify the device is a mouse */
1122 sc->hw.hwid = get_aux_id(sc->kbdc);
1123 if (!is_a_mouse(sc->hw.hwid)) {
1124 restore_controller(sc->kbdc, command_byte);
1126 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1129 switch (sc->hw.hwid) {
1130 case PSM_BALLPOINT_ID:
1131 sc->hw.type = MOUSE_TRACKBALL;
1134 case PSM_INTELLI_ID:
1135 case PSM_EXPLORER_ID:
1136 case PSM_4DMOUSE_ID:
1138 sc->hw.type = MOUSE_MOUSE;
1141 sc->hw.type = MOUSE_UNKNOWN;
1145 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1147 i = GENERIC_MOUSE_ENTRY;
1150 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1152 /* other parameters */
1153 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1154 if ((*vendortype[i].probefunc)(sc)) {
1156 printf("psm%d: found %s\n",
1157 unit, model_name(vendortype[i].model));
1163 sc->hw.model = vendortype[i].model;
1165 sc->dflt_mode.level = PSM_LEVEL_BASE;
1166 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1167 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1168 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1169 sc->dflt_mode.syncmask[0] = 0;
1171 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1172 if (sc->config & PSM_CONFIG_FORCETAP)
1173 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1174 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1175 sc->mode = sc->dflt_mode;
1176 sc->mode.packetsize = vendortype[i].packetsize;
1178 /* set mouse parameters */
1181 * A version of Logitech FirstMouse+ won't report wheel movement,
1182 * if SET_DEFAULTS is sent... Don't use this command.
1183 * This fix was found by Takashi Nishida.
1185 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1187 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1189 if (sc->config & PSM_CONFIG_RESOLUTION) {
1191 = set_mouse_resolution(sc->kbdc,
1192 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1193 } else if (sc->mode.resolution >= 0) {
1195 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1197 if (sc->mode.rate > 0) {
1198 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1200 set_mouse_scaling(sc->kbdc, 1);
1202 /* request a data packet and extract sync. bits */
1203 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1204 printf("psm%d: failed to get data.\n", unit);
1205 sc->mode.syncmask[0] = 0;
1207 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1208 /* the NetScroll Mouse will send three more bytes... Ignore them */
1209 empty_aux_buffer(sc->kbdc, 5);
1212 /* just check the status of the mouse */
1214 * NOTE: XXX there are some arcane controller/mouse combinations out
1215 * there, which hung the controller unless there is data transmission
1216 * after ACK from the mouse.
1218 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1219 printf("psm%d: failed to get status.\n", unit);
1222 * When in its native mode, some mice operate with different
1223 * default parameters than in the PS/2 compatible mode.
1225 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1226 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1229 /* disable the aux port for now... */
1230 if (!set_controller_command_byte(sc->kbdc,
1231 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1232 (command_byte & KBD_KBD_CONTROL_BITS)
1233 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1235 * this is CONTROLLER ERROR; I don't know the proper way to
1236 * recover from this error...
1238 restore_controller(sc->kbdc, command_byte);
1239 printf("psm%d: unable to set the command byte.\n", unit);
1244 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1245 kbdc_lock(sc->kbdc, FALSE);
1250 psmattach(device_t dev)
1252 int unit = device_get_unit(dev);
1253 struct psm_softc *sc = device_get_softc(dev);
1257 /* Setup initial state */
1258 sc->state = PSM_VALID;
1259 callout_handle_init(&sc->callout);
1261 /* Setup our interrupt handler */
1263 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1264 RF_SHAREABLE | RF_ACTIVE);
1265 if (sc->intr == NULL)
1267 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1269 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1274 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1276 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1280 printf("psm%d: model %s, device ID %d\n",
1281 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1283 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1284 unit, model_name(sc->hw.model),
1285 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1286 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1287 unit, sc->config, sc->flags, sc->mode.packetsize);
1288 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1289 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1299 psmdetach(device_t dev)
1301 struct psm_softc *sc;
1304 sc = device_get_softc(dev);
1305 if (sc->state & PSM_OPEN)
1309 bus_teardown_intr(dev, sc->intr, sc->ih);
1310 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1312 destroy_dev(sc->dev);
1313 destroy_dev(sc->bdev);
1319 psmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
1321 int unit = PSM_UNIT(dev);
1322 struct psm_softc *sc;
1327 /* Get device data */
1328 sc = PSM_SOFTC(unit);
1329 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1330 /* the device is no longer valid/functioning */
1333 /* Disallow multiple opens */
1334 if (sc->state & PSM_OPEN)
1337 device_busy(devclass_get_device(psm_devclass, unit));
1339 /* Initialize state */
1340 sc->mode.level = sc->dflt_mode.level;
1341 sc->mode.protocol = sc->dflt_mode.protocol;
1342 sc->watchdog = FALSE;
1344 /* flush the event queue */
1345 sc->queue.count = 0;
1348 sc->status.flags = 0;
1349 sc->status.button = 0;
1350 sc->status.obutton = 0;
1355 sc->pqueue_start = 0;
1358 /* empty input buffer */
1362 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1363 if (!kbdc_lock(sc->kbdc, TRUE))
1366 /* save the current controller command byte */
1368 command_byte = get_controller_command_byte(sc->kbdc);
1370 /* enable the aux port and temporalily disable the keyboard */
1371 if ((command_byte == -1)
1372 || !set_controller_command_byte(sc->kbdc,
1373 kbdc_get_device_mask(sc->kbdc),
1374 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1375 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1376 /* CONTROLLER ERROR; do you know how to get out of this? */
1377 kbdc_lock(sc->kbdc, FALSE);
1379 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1384 * Now that the keyboard controller is told not to generate
1385 * the keyboard and mouse interrupts, call `splx()' to allow
1386 * the other tty interrupts. The clock interrupt may also occur,
1387 * but timeout routines will be blocked by the poll flag set
1392 /* enable the mouse device */
1393 err = doopen(sc, command_byte);
1397 sc->state |= PSM_OPEN;
1398 kbdc_lock(sc->kbdc, FALSE);
1403 psmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1405 int unit = PSM_UNIT(dev);
1406 struct psm_softc *sc = PSM_SOFTC(unit);
1411 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1412 if (!kbdc_lock(sc->kbdc, TRUE))
1415 /* save the current controller command byte */
1417 command_byte = get_controller_command_byte(sc->kbdc);
1418 if (command_byte == -1) {
1419 kbdc_lock(sc->kbdc, FALSE);
1424 /* disable the aux interrupt and temporalily disable the keyboard */
1425 if (!set_controller_command_byte(sc->kbdc,
1426 kbdc_get_device_mask(sc->kbdc),
1427 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1428 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1429 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1431 /* CONTROLLER ERROR;
1432 * NOTE: we shall force our way through. Because the only
1433 * ill effect we shall see is that we may not be able
1434 * to read ACK from the mouse, and it doesn't matter much
1435 * so long as the mouse will accept the DISABLE command.
1440 /* stop the watchdog timer */
1441 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1442 callout_handle_init(&sc->callout);
1444 /* remove anything left in the output buffer */
1445 empty_aux_buffer(sc->kbdc, 10);
1447 /* disable the aux device, port and interrupt */
1448 if (sc->state & PSM_VALID) {
1449 if (!disable_aux_dev(sc->kbdc)) {
1451 * NOTE: we don't return error and continue, pretending
1452 * we have successfully disabled the device. It's OK because
1453 * the interrupt routine will discard any data from the mouse
1456 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1460 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1461 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1465 if (!set_controller_command_byte(sc->kbdc,
1466 kbdc_get_device_mask(sc->kbdc),
1467 (command_byte & KBD_KBD_CONTROL_BITS)
1468 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1469 /* CONTROLLER ERROR;
1470 * we shall ignore this error; see the above comment.
1472 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1476 /* remove anything left in the output buffer */
1477 empty_aux_buffer(sc->kbdc, 10);
1479 /* close is almost always successful */
1480 sc->state &= ~PSM_OPEN;
1481 kbdc_lock(sc->kbdc, FALSE);
1482 device_unbusy(devclass_get_device(psm_devclass, unit));
1487 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, unsigned char *buf)
1489 static unsigned char butmapps2[8] = {
1491 MOUSE_PS2_BUTTON1DOWN,
1492 MOUSE_PS2_BUTTON2DOWN,
1493 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1494 MOUSE_PS2_BUTTON3DOWN,
1495 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1496 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1497 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1499 static unsigned char butmapmsc[8] = {
1500 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1501 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1502 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1503 MOUSE_MSC_BUTTON3UP,
1504 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1505 MOUSE_MSC_BUTTON2UP,
1506 MOUSE_MSC_BUTTON1UP,
1512 if (sc->mode.level == PSM_LEVEL_BASE) {
1513 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1514 if (status->button & MOUSE_BUTTON4DOWN)
1515 mapped |= MOUSE_BUTTON1DOWN;
1516 status->button = mapped;
1517 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1518 i = imax(imin(status->dx, 255), -256);
1520 buf[0] |= MOUSE_PS2_XNEG;
1522 i = imax(imin(status->dy, 255), -256);
1524 buf[0] |= MOUSE_PS2_YNEG;
1526 return MOUSE_PS2_PACKETSIZE;
1527 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1528 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1529 i = imax(imin(status->dx, 255), -256);
1531 buf[3] = i - buf[1];
1532 i = imax(imin(status->dy, 255), -256);
1534 buf[4] = i - buf[2];
1535 i = imax(imin(status->dz, 127), -128);
1536 buf[5] = (i >> 1) & 0x7f;
1537 buf[6] = (i - (i >> 1)) & 0x7f;
1538 buf[7] = (~status->button >> 3) & 0x7f;
1539 return MOUSE_SYS_PACKETSIZE;
1541 return pb->inputbytes;
1545 psmread(struct cdev *dev, struct uio *uio, int flag)
1547 register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1548 unsigned char buf[PSM_SMALLBUFSIZE];
1553 if ((sc->state & PSM_VALID) == 0)
1556 /* block until mouse activity occured */
1558 while (sc->queue.count <= 0) {
1559 if (PSM_NBLOCKIO(dev)) {
1563 sc->state |= PSM_ASLP;
1564 error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
1565 sc->state &= ~PSM_ASLP;
1569 } else if ((sc->state & PSM_VALID) == 0) {
1570 /* the device disappeared! */
1577 /* copy data to the user land */
1578 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1580 l = imin(sc->queue.count, uio->uio_resid);
1581 if (l > sizeof(buf))
1583 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1584 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1585 sizeof(sc->queue.buf) - sc->queue.head);
1586 bcopy(&sc->queue.buf[0],
1587 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1588 l - (sizeof(sc->queue.buf) - sc->queue.head));
1590 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1592 sc->queue.count -= l;
1593 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1595 error = uiomove(buf, l, uio);
1604 block_mouse_data(struct psm_softc *sc, int *c)
1608 if (!kbdc_lock(sc->kbdc, TRUE))
1612 *c = get_controller_command_byte(sc->kbdc);
1614 || !set_controller_command_byte(sc->kbdc,
1615 kbdc_get_device_mask(sc->kbdc),
1616 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1617 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1618 /* this is CONTROLLER ERROR */
1620 kbdc_lock(sc->kbdc, FALSE);
1625 * The device may be in the middle of status data transmission.
1626 * The transmission will be interrupted, thus, incomplete status
1627 * data must be discarded. Although the aux interrupt is disabled
1628 * at the keyboard controller level, at most one aux interrupt
1629 * may have already been pending and a data byte is in the
1630 * output buffer; throw it away. Note that the second argument
1631 * to `empty_aux_buffer()' is zero, so that the call will just
1632 * flush the internal queue.
1633 * `psmintr()' will be invoked after `splx()' if an interrupt is
1634 * pending; it will see no data and returns immediately.
1636 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1637 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1645 dropqueue(struct psm_softc *sc)
1648 sc->queue.count = 0;
1651 if ((sc->state & PSM_SOFTARMED) != 0) {
1652 sc->state &= ~PSM_SOFTARMED;
1653 untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout);
1655 sc->pqueue_start = sc->pqueue_end;
1659 flushpackets(struct psm_softc *sc)
1663 bzero(&sc->pqueue, sizeof(sc->pqueue));
1667 unblock_mouse_data(struct psm_softc *sc, int c)
1672 * We may have seen a part of status data during `set_mouse_XXX()'.
1673 * they have been queued; flush it.
1675 empty_aux_buffer(sc->kbdc, 0);
1677 /* restore ports and interrupt */
1678 if (!set_controller_command_byte(sc->kbdc,
1679 kbdc_get_device_mask(sc->kbdc),
1680 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1681 /* CONTROLLER ERROR; this is serious, we may have
1682 * been left with the inaccessible keyboard and
1683 * the disabled mouse interrupt.
1688 kbdc_lock(sc->kbdc, FALSE);
1693 psmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1695 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1697 mousestatus_t status;
1698 #if (defined(MOUSE_GETVARS))
1707 /* Perform IOCTL command */
1710 case OLD_MOUSE_GETHWINFO:
1712 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1713 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1714 ((old_mousehw_t *)addr)->type = sc->hw.type;
1715 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1719 case MOUSE_GETHWINFO:
1721 *(mousehw_t *)addr = sc->hw;
1722 if (sc->mode.level == PSM_LEVEL_BASE)
1723 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1727 case MOUSE_SYN_GETHWINFO:
1729 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS)
1730 *(synapticshw_t *)addr = sc->synhw;
1736 case OLD_MOUSE_GETMODE:
1738 switch (sc->mode.level) {
1739 case PSM_LEVEL_BASE:
1740 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1742 case PSM_LEVEL_STANDARD:
1743 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1745 case PSM_LEVEL_NATIVE:
1746 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1749 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1750 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1751 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1757 *(mousemode_t *)addr = sc->mode;
1758 ((mousemode_t *)addr)->resolution =
1759 MOUSE_RES_LOW - sc->mode.resolution;
1760 switch (sc->mode.level) {
1761 case PSM_LEVEL_BASE:
1762 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1763 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1765 case PSM_LEVEL_STANDARD:
1766 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1767 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1768 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1769 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1771 case PSM_LEVEL_NATIVE:
1772 /* FIXME: this isn't quite correct... XXX */
1773 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1779 case OLD_MOUSE_SETMODE:
1781 if (cmd == OLD_MOUSE_SETMODE) {
1782 mode.rate = ((old_mousemode_t *)addr)->rate;
1784 * resolution old I/F new I/F
1791 if (((old_mousemode_t *)addr)->resolution > 0)
1792 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1793 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1796 mode = *(mousemode_t *)addr;
1799 /* adjust and validate parameters. */
1800 if (mode.rate > UCHAR_MAX)
1803 mode.rate = sc->dflt_mode.rate;
1804 else if (mode.rate == -1)
1805 /* don't change the current setting */
1807 else if (mode.rate < 0)
1809 if (mode.resolution >= UCHAR_MAX)
1811 if (mode.resolution >= 200)
1812 mode.resolution = MOUSE_RES_HIGH;
1813 else if (mode.resolution >= 100)
1814 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1815 else if (mode.resolution >= 50)
1816 mode.resolution = MOUSE_RES_MEDIUMLOW;
1817 else if (mode.resolution > 0)
1818 mode.resolution = MOUSE_RES_LOW;
1819 if (mode.resolution == MOUSE_RES_DEFAULT)
1820 mode.resolution = sc->dflt_mode.resolution;
1821 else if (mode.resolution == -1)
1822 /* don't change the current setting */
1824 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1825 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1826 if (mode.level == -1)
1827 /* don't change the current setting */
1828 mode.level = sc->mode.level;
1829 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1831 if (mode.accelfactor == -1)
1832 /* don't change the current setting */
1833 mode.accelfactor = sc->mode.accelfactor;
1834 else if (mode.accelfactor < 0)
1837 /* don't allow anybody to poll the keyboard controller */
1838 error = block_mouse_data(sc, &command_byte);
1842 /* set mouse parameters */
1844 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1845 if (mode.resolution >= 0)
1846 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1847 set_mouse_scaling(sc->kbdc, 1);
1848 get_mouse_status(sc->kbdc, stat, 0, 3);
1851 sc->mode.rate = mode.rate;
1852 sc->mode.resolution = mode.resolution;
1853 sc->mode.accelfactor = mode.accelfactor;
1854 sc->mode.level = mode.level;
1857 unblock_mouse_data(sc, command_byte);
1860 case MOUSE_GETLEVEL:
1861 *(int *)addr = sc->mode.level;
1864 case MOUSE_SETLEVEL:
1865 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1867 sc->mode.level = *(int *)addr;
1870 case MOUSE_GETSTATUS:
1872 status = sc->status;
1873 sc->status.flags = 0;
1874 sc->status.obutton = sc->status.button;
1875 sc->status.button = 0;
1880 *(mousestatus_t *)addr = status;
1883 #if (defined(MOUSE_GETVARS))
1885 var = (mousevar_t *)addr;
1886 bzero(var, sizeof(*var));
1888 var->var[0] = MOUSE_VARS_PS2_SIG;
1889 var->var[1] = sc->config;
1890 var->var[2] = sc->flags;
1896 #endif /* MOUSE_GETVARS */
1898 case MOUSE_READSTATE:
1899 case MOUSE_READDATA:
1900 data = (mousedata_t *)addr;
1901 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1904 error = block_mouse_data(sc, &command_byte);
1907 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1908 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1910 unblock_mouse_data(sc, command_byte);
1913 #if (defined(MOUSE_SETRESOLUTION))
1914 case MOUSE_SETRESOLUTION:
1915 mode.resolution = *(int *)addr;
1916 if (mode.resolution >= UCHAR_MAX)
1918 else if (mode.resolution >= 200)
1919 mode.resolution = MOUSE_RES_HIGH;
1920 else if (mode.resolution >= 100)
1921 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1922 else if (mode.resolution >= 50)
1923 mode.resolution = MOUSE_RES_MEDIUMLOW;
1924 else if (mode.resolution > 0)
1925 mode.resolution = MOUSE_RES_LOW;
1926 if (mode.resolution == MOUSE_RES_DEFAULT)
1927 mode.resolution = sc->dflt_mode.resolution;
1928 else if (mode.resolution == -1)
1929 mode.resolution = sc->mode.resolution;
1930 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1931 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1933 error = block_mouse_data(sc, &command_byte);
1936 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1937 if (sc->mode.resolution != mode.resolution)
1939 unblock_mouse_data(sc, command_byte);
1941 #endif /* MOUSE_SETRESOLUTION */
1943 #if (defined(MOUSE_SETRATE))
1945 mode.rate = *(int *)addr;
1946 if (mode.rate > UCHAR_MAX)
1949 mode.rate = sc->dflt_mode.rate;
1950 else if (mode.rate < 0)
1951 mode.rate = sc->mode.rate;
1953 error = block_mouse_data(sc, &command_byte);
1956 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1957 if (sc->mode.rate != mode.rate)
1959 unblock_mouse_data(sc, command_byte);
1961 #endif /* MOUSE_SETRATE */
1963 #if (defined(MOUSE_SETSCALING))
1964 case MOUSE_SETSCALING:
1965 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1968 error = block_mouse_data(sc, &command_byte);
1971 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1973 unblock_mouse_data(sc, command_byte);
1975 #endif /* MOUSE_SETSCALING */
1977 #if (defined(MOUSE_GETHWID))
1979 error = block_mouse_data(sc, &command_byte);
1982 sc->hw.hwid &= ~0x00ff;
1983 sc->hw.hwid |= get_aux_id(sc->kbdc);
1984 *(int *)addr = sc->hw.hwid & 0x00ff;
1985 unblock_mouse_data(sc, command_byte);
1987 #endif /* MOUSE_GETHWID */
1997 psmtimeout(void *arg)
1999 struct psm_softc *sc;
2002 sc = (struct psm_softc *)arg;
2004 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2006 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
2008 kbdc_lock(sc->kbdc, FALSE);
2010 sc->watchdog = TRUE;
2012 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
2015 static int psmhz = 20;
2016 SYSCTL_INT(_debug, OID_AUTO, psmhz, CTLFLAG_RW, &psmhz, 0, "");
2018 static int psm_soft_timeout = 500000; /* 0.5 sec */
2019 SYSCTL_INT(_debug, OID_AUTO, psm_soft_timeout, CTLFLAG_RW,
2020 &psm_soft_timeout, 0, "");
2022 static int psmerrsecs = 2;
2023 SYSCTL_INT(_debug, OID_AUTO, psmerrsecs, CTLFLAG_RW, &psmerrsecs, 0, "");
2024 static int psmerrusecs = 0;
2025 SYSCTL_INT(_debug, OID_AUTO, psmerrusecs, CTLFLAG_RW, &psmerrusecs, 0, "");
2026 static int psmsecs = 0;
2027 SYSCTL_INT(_debug, OID_AUTO, psmsecs, CTLFLAG_RW, &psmsecs, 0, "");
2028 static int psmusecs = 500000;
2029 SYSCTL_INT(_debug, OID_AUTO, psmusecs, CTLFLAG_RW, &psmusecs, 0, "");
2034 struct psm_softc *sc = arg;
2041 /* read until there is nothing to read */
2042 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2044 pb = &sc->pqueue[sc->pqueue_end];
2045 /* discard the byte if the device is not open */
2046 if ((sc->state & PSM_OPEN) == 0)
2049 getmicrouptime(&now);
2050 if ((pb->inputbytes > 0) && timevalcmp(&now, &sc->inputtimeout, >)) {
2052 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2057 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2058 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2059 timevaladd(&sc->inputtimeout, &now);
2061 pb->ipacket[pb->inputbytes++] = c;
2062 if (pb->inputbytes < sc->mode.packetsize)
2066 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2067 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2068 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]);
2073 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2075 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x) %d"
2076 " cmds since last error.\n",
2077 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2078 sc->cmdcount - sc->lasterr);
2081 sc->lasterr = sc->cmdcount;
2084 sc->lastinputerr = now;
2085 if (sc->syncerrors < sc->mode.packetsize) {
2087 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2090 bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
2091 } else if (sc->syncerrors == sc->mode.packetsize) {
2093 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2096 disable_aux_dev(sc->kbdc);
2097 enable_aux_dev(sc->kbdc);
2098 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2100 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2103 bcopy(&pb->ipacket[1], &pb->ipacket[0], pb->inputbytes);
2104 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2106 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2108 reinitialize(sc, TRUE);
2112 /* if this packet is at all bogus then drop the packet. */
2114 !timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, &now)) {
2121 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2124 * If we've filled the queue then call the softintr ourselves,
2125 * otherwise schedule the interrupt for later.
2127 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2128 (sc->pqueue_end == sc->pqueue_start)) {
2129 if ((sc->state & PSM_SOFTARMED) != 0) {
2130 sc->state &= ~PSM_SOFTARMED;
2131 untimeout(psmsoftintr, arg, sc->softcallout);
2134 } else if ((sc->state & PSM_SOFTARMED) == 0) {
2135 sc->state |= PSM_SOFTARMED;
2136 sc->softcallout = timeout(psmsoftintr, arg,
2137 psmhz < 1 ? 1 : (hz/psmhz));
2143 psmsoftintr(void *arg)
2146 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
2147 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
2149 static int butmap[8] = {
2153 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2155 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
2156 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
2157 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
2159 static int butmap_versapad[8] = {
2165 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
2167 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
2169 register struct psm_softc *sc = arg;
2178 getmicrouptime(&sc->lastsoftintr);
2184 pb = &sc->pqueue[sc->pqueue_start];
2187 * A kludge for Kensington device!
2188 * The MSB of the horizontal count appears to be stored in
2191 if (sc->hw.model == MOUSE_MODEL_THINK)
2192 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2194 /* ignore the overflow bits... */
2195 x = (c & MOUSE_PS2_XNEG) ? pb->ipacket[1] - 256 : pb->ipacket[1];
2196 y = (c & MOUSE_PS2_YNEG) ? pb->ipacket[2] - 256 : pb->ipacket[2];
2198 ms.obutton = sc->button; /* previous button state */
2199 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2200 /* `tapping' action */
2201 if (sc->config & PSM_CONFIG_FORCETAP)
2202 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2204 switch (sc->hw.model) {
2206 case MOUSE_MODEL_EXPLORER:
2208 * b7 b6 b5 b4 b3 b2 b1 b0
2209 * byte 1: oy ox sy sx 1 M R L
2210 * byte 2: x x x x x x x x
2211 * byte 3: y y y y y y y y
2212 * byte 4: * * S2 S1 s d2 d1 d0
2214 * L, M, R, S1, S2: left, middle, right and side buttons
2215 * s: wheel data sign bit
2218 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2219 ? (pb->ipacket[3] & 0x0f) - 16 : (pb->ipacket[3] & 0x0f);
2220 ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2221 ? MOUSE_BUTTON4DOWN : 0;
2222 ms.button |= (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2223 ? MOUSE_BUTTON5DOWN : 0;
2226 case MOUSE_MODEL_INTELLI:
2227 case MOUSE_MODEL_NET:
2228 /* wheel data is in the fourth byte */
2229 z = (char)pb->ipacket[3];
2230 /* some mice may send 7 when there is no Z movement?! XXX */
2231 if ((z >= 7) || (z <= -7))
2233 /* some compatible mice have additional buttons */
2234 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2235 ? MOUSE_BUTTON4DOWN : 0;
2236 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2237 ? MOUSE_BUTTON5DOWN : 0;
2240 case MOUSE_MODEL_MOUSEMANPLUS:
2242 * PS2++ protocl packet
2244 * b7 b6 b5 b4 b3 b2 b1 b0
2245 * byte 1: * 1 p3 p2 1 * * *
2246 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2248 * p3-p0: packet type
2249 * c1, c2: c1 & c2 == 1, if p2 == 0
2250 * c1 & c2 == 0, if p2 == 1
2252 * packet type: 0 (device type)
2253 * See comments in enable_mmanplus() below.
2255 * packet type: 1 (wheel data)
2257 * b7 b6 b5 b4 b3 b2 b1 b0
2258 * byte 3: h * B5 B4 s d2 d1 d0
2260 * h: 1, if horizontal roller data
2261 * 0, if vertical roller data
2262 * B4, B5: button 4 and 5
2264 * d2-d0: roller data
2266 * packet type: 2 (reserved)
2268 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2270 && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2271 /* the extended data packet encodes button and wheel events */
2272 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
2274 /* wheel data packet */
2276 if (pb->ipacket[2] & 0x80) {
2277 /* horizontal roller count - ignore it XXX*/
2279 /* vertical roller count */
2280 z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2281 ? (pb->ipacket[2] & 0x0f) - 16
2282 : (pb->ipacket[2] & 0x0f);
2284 ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2285 ? MOUSE_BUTTON4DOWN : 0;
2286 ms.button |= (pb->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2287 ? MOUSE_BUTTON5DOWN : 0;
2290 /* this packet type is reserved by Logitech... */
2292 * IBM ScrollPoint Mouse uses this packet type to
2293 * encode both vertical and horizontal scroll movement.
2296 /* horizontal count */
2297 if (pb->ipacket[2] & 0x0f)
2298 z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2299 /* vertical count */
2300 if (pb->ipacket[2] & 0xf0)
2301 z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2303 /* vertical count */
2304 z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG)
2305 ? ((pb->ipacket[2] >> 4) & 0x0f) - 16
2306 : ((pb->ipacket[2] >> 4) & 0x0f);
2307 /* horizontal count */
2308 w = (pb->ipacket[2] & MOUSE_SPOINT_WNEG)
2309 ? (pb->ipacket[2] & 0x0f) - 16
2310 : (pb->ipacket[2] & 0x0f);
2314 /* device type packet - shouldn't happen */
2318 ms.button = ms.obutton;
2320 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2321 "0x%02x 0x%02x 0x%02x\n",
2322 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2323 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]);
2327 /* preserve button states */
2328 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2332 case MOUSE_MODEL_GLIDEPOINT:
2333 /* `tapping' action */
2334 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2337 case MOUSE_MODEL_NETSCROLL:
2338 /* three addtional bytes encode buttons and wheel events */
2339 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2340 ? MOUSE_BUTTON4DOWN : 0;
2341 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2342 ? MOUSE_BUTTON5DOWN : 0;
2343 z = (pb->ipacket[3] & MOUSE_PS2_XNEG)
2344 ? pb->ipacket[4] - 256 : pb->ipacket[4];
2347 case MOUSE_MODEL_THINK:
2348 /* the fourth button state in the first byte */
2349 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2352 case MOUSE_MODEL_VERSAPAD:
2353 /* VersaPad PS/2 absolute mode message format
2355 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2356 * ipacket[0]: 1 1 0 A 1 L T R
2357 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2358 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2359 * ipacket[3]: 1 1 1 A 1 L T R
2360 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2361 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2364 * R: right physical mouse button (1=on)
2365 * T: touch pad virtual button (1=tapping)
2366 * L: left physical mouse button (1=on)
2367 * A: position data is valid (1=valid)
2368 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2369 * V: vertical data (12bit signed integer. V11 is sign bit.)
2372 * Tapping is mapped to MOUSE_BUTTON4.
2374 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2375 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2377 if (c & MOUSE_PS2VERSA_IN_USE) {
2378 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
2379 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
2384 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2387 if (x < 0) /* XXX */
2396 sc->flags |= PSM_FLAGS_FINGERDOWN;
2401 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2403 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2404 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2407 case MOUSE_MODEL_4D:
2409 * b7 b6 b5 b4 b3 b2 b1 b0
2410 * byte 1: s2 d2 s1 d1 1 M R L
2411 * byte 2: sx x x x x x x x
2412 * byte 3: sy y y y y y y y
2414 * s1: wheel 1 direction
2416 * s2: wheel 2 direction
2419 x = (pb->ipacket[1] & 0x80) ? pb->ipacket[1] - 256 : pb->ipacket[1];
2420 y = (pb->ipacket[2] & 0x80) ? pb->ipacket[2] - 256 : pb->ipacket[2];
2421 switch (c & MOUSE_4D_WHEELBITS) {
2428 case 0x40: /* 2nd wheel turning right XXX */
2431 case 0xc0: /* 2nd wheel turning left XXX */
2437 case MOUSE_MODEL_4DPLUS:
2438 if ((x < 16 - 256) && (y < 16 - 256)) {
2440 * b7 b6 b5 b4 b3 b2 b1 b0
2441 * byte 1: 0 0 1 1 1 M R L
2442 * byte 2: 0 0 0 0 1 0 0 0
2443 * byte 3: 0 0 0 0 S s d1 d0
2445 * L, M, R, S: left, middle, right and side buttons
2446 * s: wheel data sign bit
2450 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2451 ms.button |= MOUSE_BUTTON4DOWN;
2452 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2453 ? ((pb->ipacket[2] & 0x07) - 8)
2454 : (pb->ipacket[2] & 0x07) ;
2456 /* preserve previous button states */
2457 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2461 case MOUSE_MODEL_SYNAPTICS:
2462 /* TouchPad PS/2 absolute mode message format
2464 * Bits: 7 6 5 4 3 2 1 0 (LSB)
2465 * ------------------------------------------------
2466 * ipacket[0]: 1 0 W3 W2 0 W1 R L
2467 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
2468 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
2469 * ipacket[3]: 1 1 Yc Xc 0 W0 D U
2470 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
2471 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
2474 * L: left physical mouse button
2475 * R: right physical mouse button
2483 * Absolute reportable limits: 0 - 6143.
2484 * Typical bezel limits: 1472 - 5472.
2485 * Typical edge marings: 1632 - 5312.
2487 * w = 3 Passthrough Packet
2489 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2492 /* Sanity check for out of sync packets. */
2493 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2494 (pb->ipacket[3] & 0xc8) != 0xc0)
2497 x = y = x0 = y0 = 0;
2499 /* Pressure value. */
2501 w = ((pb->ipacket[0] & 0x30) >> 2) |
2502 ((pb->ipacket[0] & 0x04) >> 1) |
2503 ((pb->ipacket[3] & 0x04) >> 2);
2505 /* Button presses */
2507 if (pb->ipacket[0] & 0x01)
2508 ms.button |= MOUSE_BUTTON1DOWN;
2509 if (pb->ipacket[0] & 0x02)
2510 ms.button |= MOUSE_BUTTON3DOWN;
2512 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2513 if ((pb->ipacket[3] & 0x01) && (pb->ipacket[0] & 0x01) == 0)
2514 ms.button |= MOUSE_BUTTON4DOWN;
2515 if ((pb->ipacket[3] & 0x02) && (pb->ipacket[0] & 0x02) == 0)
2516 ms.button |= MOUSE_BUTTON5DOWN;
2519 /* There is a finger on the pad. */
2520 if ((w >= 4 && w <= 7) && (z >= 16 && z < 200)) {
2521 x0 = ((pb->ipacket[3] & 0x10) << 8) |
2522 ((pb->ipacket[1] & 0x0f) << 8) |
2524 y0 = ((pb->ipacket[3] & 0x20) << 7) |
2525 ((pb->ipacket[1] & 0xf0) << 4) |
2528 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2529 x0 = (x0 + sc->xold * 3) / 4;
2530 y0 = (y0 + sc->yold * 3) / 4;
2532 x = (x0 - sc->xold) / 4;
2533 y = (y0 - sc->yold) / 4;
2535 sc->flags |= PSM_FLAGS_FINGERDOWN;
2541 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2544 /* Use the extra buttons as a scrollwheel */
2545 if (ms.button & MOUSE_BUTTON4DOWN)
2547 else if (ms.button & MOUSE_BUTTON5DOWN)
2554 case MOUSE_MODEL_GENERIC:
2560 if (sc->mode.accelfactor >= 1) {
2562 x = x * x / sc->mode.accelfactor;
2565 if (c & MOUSE_PS2_XNEG)
2569 y = y * y / sc->mode.accelfactor;
2572 if (c & MOUSE_PS2_YNEG)
2580 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2581 | (ms.obutton ^ ms.button);
2583 if (sc->mode.level < PSM_LEVEL_NATIVE)
2584 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
2586 sc->status.flags |= ms.flags;
2587 sc->status.dx += ms.dx;
2588 sc->status.dy += ms.dy;
2589 sc->status.dz += ms.dz;
2590 sc->status.button = ms.button;
2591 sc->button = ms.button;
2593 sc->watchdog = FALSE;
2596 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
2597 l = imin(pb->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2598 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2599 if (pb->inputbytes > l)
2600 bcopy(&pb->ipacket[l], &sc->queue.buf[0], pb->inputbytes - l);
2602 (sc->queue.tail + pb->inputbytes) % sizeof(sc->queue.buf);
2603 sc->queue.count += pb->inputbytes;
2607 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
2608 sc->pqueue_start = 0;
2609 } while (sc->pqueue_start != sc->pqueue_end);
2610 if (sc->state & PSM_ASLP) {
2611 sc->state &= ~PSM_ASLP;
2614 selwakeuppri(&sc->rsel, PZERO);
2615 sc->state &= ~PSM_SOFTARMED;
2620 psmpoll(struct cdev *dev, int events, struct thread *td)
2622 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2626 /* Return true if a mouse event available */
2628 if (events & (POLLIN | POLLRDNORM)) {
2629 if (sc->queue.count > 0)
2630 revents |= events & (POLLIN | POLLRDNORM);
2632 selrecord(td, &sc->rsel);
2639 /* vendor/model specific routines */
2641 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2643 if (set_mouse_resolution(kbdc, res) != res)
2645 if (set_mouse_scaling(kbdc, scale)
2646 && set_mouse_scaling(kbdc, scale)
2647 && set_mouse_scaling(kbdc, scale)
2648 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2654 mouse_ext_command(KBDC kbdc, int command)
2658 c = (command >> 6) & 0x03;
2659 if (set_mouse_resolution(kbdc, c) != c)
2661 c = (command >> 4) & 0x03;
2662 if (set_mouse_resolution(kbdc, c) != c)
2664 c = (command >> 2) & 0x03;
2665 if (set_mouse_resolution(kbdc, c) != c)
2667 c = (command >> 0) & 0x03;
2668 if (set_mouse_resolution(kbdc, c) != c)
2674 /* Logitech MouseMan Cordless II */
2676 enable_lcordless(struct psm_softc *sc)
2681 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2683 if (status[1] == PSMD_RES_HIGH)
2685 ch = (status[0] & 0x07) - 1; /* channel # */
2686 if ((ch <= 0) || (ch > 4))
2689 * status[1]: always one?
2690 * status[2]: battery status? (0-100)
2696 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2698 enable_groller(struct psm_softc *sc)
2703 * The special sequence to enable the fourth button and the
2704 * roller. Immediately after this sequence check status bytes.
2705 * if the mouse is NetScroll, the second and the third bytes are
2710 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2711 * look like the following.
2713 * byte 1 bit 7 always 0
2714 * bit 6 stream mode (0)
2715 * bit 5 disabled (0)
2716 * bit 4 1:1 scaling (0)
2718 * bit 0-2 button status
2719 * byte 2 resolution (PSMD_RES_HIGH)
2720 * byte 3 report rate (?)
2723 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2725 if ((status[1] != '3') || (status[2] != 'D'))
2727 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2732 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2734 enable_gmouse(struct psm_softc *sc)
2739 * The special sequence to enable the middle, "rubber" button.
2740 * Immediately after this sequence check status bytes.
2741 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2742 * the second and the third bytes are '3' and 'U'.
2743 * NOTE: NetMouse reports that it has three buttons although it has
2744 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2745 * say they have three buttons too and they do have a button on the
2748 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2750 if ((status[1] != '3') || (status[2] != 'U'))
2755 /* ALPS GlidePoint */
2757 enable_aglide(struct psm_softc *sc)
2762 * The special sequence to obtain ALPS GlidePoint specific
2763 * information. Immediately after this sequence, status bytes will
2764 * contain something interesting.
2765 * NOTE: ALPS produces several models of GlidePoint. Some of those
2766 * do not respond to this sequence, thus, cannot be detected this way.
2768 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2770 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2772 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2777 /* Kensington ThinkingMouse/Trackball */
2779 enable_kmouse(struct psm_softc *sc)
2781 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2782 KBDC kbdc = sc->kbdc;
2788 id1 = get_aux_id(kbdc);
2789 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2792 * The device is now in the native mode? It returns a different
2795 id2 = get_aux_id(kbdc);
2796 if ((id1 == id2) || (id2 != 2))
2799 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2802 /* at this point, resolution is LOW, sampling rate is 10/sec */
2803 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2808 * The special sequence to enable the third and fourth buttons.
2809 * Otherwise they behave like the first and second buttons.
2811 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2812 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2817 * At this point, the device is using default resolution and
2818 * sampling rate for the native mode.
2820 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2822 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2825 /* the device appears be enabled by this sequence, diable it for now */
2826 disable_aux_dev(kbdc);
2827 empty_aux_buffer(kbdc, 5);
2832 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2834 enable_mmanplus(struct psm_softc *sc)
2836 KBDC kbdc = sc->kbdc;
2839 /* the special sequence to enable the fourth button and the roller. */
2841 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2842 * must be called exactly three times since the last RESET command
2843 * before this sequence. XXX
2845 if (!set_mouse_scaling(kbdc, 1))
2847 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2849 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2853 * PS2++ protocl, packet type 0
2855 * b7 b6 b5 b4 b3 b2 b1 b0
2856 * byte 1: * 1 p3 p2 1 * * *
2857 * byte 2: 1 1 p1 p0 m1 m0 1 0
2858 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2860 * p3-p0: packet type: 0
2861 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2863 /* check constant bits */
2864 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2866 if ((data[1] & 0xc3) != 0xc2)
2868 /* check d3-d0 in byte 2 */
2869 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2872 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2875 sc->hw.hwid &= 0x00ff;
2876 sc->hw.hwid |= data[2] << 8; /* save model ID */
2879 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2880 * the wheel and the fourth button events are encoded in the
2881 * special data packet. The mouse may be put in the IntelliMouse mode
2882 * if it is initialized by the IntelliMouse's method.
2887 /* MS IntelliMouse Explorer */
2889 enable_msexplorer(struct psm_softc *sc)
2891 static unsigned char rate0[] = { 200, 100, 80, };
2892 static unsigned char rate1[] = { 200, 200, 80, };
2893 KBDC kbdc = sc->kbdc;
2897 /* the special sequence to enable the extra buttons and the roller. */
2898 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2899 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2902 /* the device will give the genuine ID only after the above sequence */
2903 id = get_aux_id(kbdc);
2904 if (id != PSM_EXPLORER_ID)
2908 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2911 * XXX: this is a kludge to fool some KVM switch products
2912 * which think they are clever enough to know the 4-byte IntelliMouse
2913 * protocol, and assume any other protocols use 3-byte packets.
2914 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2915 * correctly to the host computer because of this!
2916 * The following sequence is actually IntelliMouse's "wake up"
2917 * sequence; it will make the KVM think the mouse is IntelliMouse
2918 * when it is in fact IntelliMouse Explorer.
2920 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2921 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2924 id = get_aux_id(kbdc);
2929 /* MS IntelliMouse */
2931 enable_msintelli(struct psm_softc *sc)
2934 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2935 * probe routine and act like IntelliMouse.
2938 static unsigned char rate[] = { 200, 100, 80, };
2939 KBDC kbdc = sc->kbdc;
2943 /* the special sequence to enable the third button and the roller. */
2944 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2945 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2948 /* the device will give the genuine ID only after the above sequence */
2949 id = get_aux_id(kbdc);
2950 if (id != PSM_INTELLI_ID)
2959 /* A4 Tech 4D Mouse */
2961 enable_4dmouse(struct psm_softc *sc)
2964 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2967 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2968 KBDC kbdc = sc->kbdc;
2972 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2973 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2976 id = get_aux_id(kbdc);
2978 * WinEasy 4D, 4 Way Scroll 4D: 6
2979 * Cable-Free 4D: 8 (4DPLUS)
2980 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2982 if (id != PSM_4DMOUSE_ID)
2986 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2991 /* A4 Tech 4D+ Mouse */
2993 enable_4dplus(struct psm_softc *sc)
2996 * Newer wheel mice from A4 Tech seem to use this protocol.
2997 * Older models are recognized as either 4D Mouse or IntelliMouse.
2999 KBDC kbdc = sc->kbdc;
3003 * enable_4dmouse() already issued the following ID sequence...
3004 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
3007 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
3008 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3013 id = get_aux_id(kbdc);
3018 case PSM_4DPLUS_RFSW35_ID:
3030 /* Synaptics Touchpad */
3032 enable_synaptics(struct psm_softc *sc)
3038 disable_aux_dev(kbdc);
3040 /* Just to be on the safe side */
3041 set_mouse_scaling(kbdc, 1);
3043 /* Identify the Touchpad version */
3044 if (mouse_ext_command(kbdc, 0) == 0)
3046 if (get_mouse_status(kbdc, status, 0, 3) != 3)
3048 if (status[1] != 0x47)
3051 sc->synhw.infoMinor = status[0];
3052 sc->synhw.infoMajor = status[2] & 0x0f;
3055 printf("Synaptics Touchpad v%d.%d\n",
3056 sc->synhw.infoMajor, sc->synhw.infoMinor);
3058 if (sc->synhw.infoMajor < 4) {
3059 printf(" Unsupported (pre-v4) Touchpad detected\n");
3063 /* Get the Touchpad model information */
3064 if (mouse_ext_command(kbdc, 3) == 0)
3066 if (get_mouse_status(kbdc, status, 0, 3) != 3)
3068 if ((status[1] & 0x01) != 0) {
3069 printf(" Failed to read model information\n");
3073 sc->synhw.infoRot180 = (status[0] & 0x80) >> 7;
3074 sc->synhw.infoPortrait = (status[0] & 0x40) >> 6;
3075 sc->synhw.infoSensor = status[0] & 0x3f;
3076 sc->synhw.infoHardware = (status[1] & 0xfe) >> 1;
3077 sc->synhw.infoNewAbs = (status[2] & 0x80) >> 7;
3078 sc->synhw.capPen = (status[2] & 0x40) >> 6;
3079 sc->synhw.infoSimplC = (status[2] & 0x20) >> 5;
3080 sc->synhw.infoGeometry = status[2] & 0x0f;
3083 printf(" Model information:\n");
3084 printf(" infoRot180: %d\n", sc->synhw.infoRot180);
3085 printf(" infoPortrait: %d\n", sc->synhw.infoPortrait);
3086 printf(" infoSensor: %d\n", sc->synhw.infoSensor);
3087 printf(" infoHardware: %d\n", sc->synhw.infoHardware);
3088 printf(" infoNewAbs: %d\n", sc->synhw.infoNewAbs);
3089 printf(" capPen: %d\n", sc->synhw.capPen);
3090 printf(" infoSimplC: %d\n", sc->synhw.infoSimplC);
3091 printf(" infoGeometry: %d\n", sc->synhw.infoGeometry);
3094 /* Read the extended capability bits */
3095 if (mouse_ext_command(kbdc, 2) == 0)
3097 if (get_mouse_status(kbdc, status, 0, 3) != 3)
3099 if (status[1] != 0x47) {
3100 printf(" Failed to read extended capability bits\n");
3104 /* Set the different capabilities when they exist */
3105 if ((status[0] & 0x80) >> 7) {
3106 sc->synhw.capExtended = (status[0] & 0x80) >> 7;
3107 sc->synhw.capPassthrough = (status[2] & 0x80) >> 7;
3108 sc->synhw.capSleep = (status[2] & 0x10) >> 4;
3109 sc->synhw.capFourButtons = (status[2] & 0x08) >> 3;
3110 sc->synhw.capMultiFinger = (status[2] & 0x02) >> 1;
3111 sc->synhw.capPalmDetect = (status[2] & 0x01);
3114 printf(" Extended capabilities:\n");
3115 printf(" capExtended: %d\n", sc->synhw.capExtended);
3116 printf(" capPassthrough: %d\n", sc->synhw.capPassthrough);
3117 printf(" capSleep: %d\n", sc->synhw.capSleep);
3118 printf(" capFourButtons: %d\n", sc->synhw.capFourButtons);
3119 printf(" capMultiFinger: %d\n", sc->synhw.capMultiFinger);
3120 printf(" capPalmDetect: %d\n", sc->synhw.capPalmDetect);
3123 sc->synhw.capExtended = 0;
3126 printf(" No extended capabilities\n");
3130 * Read the mode byte
3132 * XXX: Note the Synaptics documentation also defines the first
3133 * byte of the response to this query to be a constant 0x3b, this
3134 * does not appear to be true for Touchpads with guest devices.
3136 if (mouse_ext_command(kbdc, 1) == 0)
3138 if (get_mouse_status(kbdc, status, 0, 3) != 3)
3140 if (status[1] != 0x47) {
3141 printf(" Failed to read mode byte\n");
3145 /* Set the mode byte -- request wmode where available */
3146 if (sc->synhw.capExtended)
3147 mouse_ext_command(kbdc, 0xc1);
3149 mouse_ext_command(kbdc, 0xc0);
3151 /* Reset the sampling rate */
3152 set_mouse_sampling_rate(kbdc, 20);
3155 * Report the correct number of buttons
3157 * XXX: I'm not sure this is used anywhere.
3159 if (sc->synhw.capExtended && sc->synhw.capFourButtons)
3167 /* Interlink electronics VersaPad */
3169 enable_versapad(struct psm_softc *sc)
3171 KBDC kbdc = sc->kbdc;
3174 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
3175 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
3176 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
3177 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
3178 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
3179 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
3180 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
3182 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
3184 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
3186 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
3188 return TRUE; /* PS/2 absolute mode */
3192 psmresume(device_t dev)
3194 struct psm_softc *sc = device_get_softc(dev);
3195 int unit = device_get_unit(dev);
3199 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
3201 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
3204 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
3206 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
3208 * Release the blocked process; it must be notified that the device
3209 * cannot be accessed anymore.
3211 sc->state &= ~PSM_ASLP;
3216 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
3221 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
3224 * This sucks up assignments from PNPBIOS and ACPI.
3228 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
3229 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
3230 * can be probed and attached only after the AT keyboard controller is
3231 * attached, we shall quietly reserve the IRQ resource for later use.
3232 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
3233 * copy the IRQ resource to the PS/2 mouse device instance hanging
3234 * under the keyboard controller, then probe and attach it.
3237 static devclass_t psmcpnp_devclass;
3239 static device_probe_t psmcpnp_probe;
3240 static device_attach_t psmcpnp_attach;
3242 static device_method_t psmcpnp_methods[] = {
3243 DEVMETHOD(device_probe, psmcpnp_probe),
3244 DEVMETHOD(device_attach, psmcpnp_attach),
3249 static driver_t psmcpnp_driver = {
3250 PSMCPNP_DRIVER_NAME,
3255 static struct isa_pnp_id psmcpnp_ids[] = {
3256 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
3257 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
3258 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
3259 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
3260 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
3261 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
3262 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
3263 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
3264 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
3265 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
3270 create_a_copy(device_t atkbdc, device_t me)
3275 /* find the PS/2 mouse device instance under the keyboard controller */
3276 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
3277 device_get_unit(atkbdc));
3280 if (device_get_state(psm) != DS_NOTPRESENT)
3283 /* move our resource to the found device */
3284 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
3285 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
3287 /* ...then probe and attach it */
3288 return device_probe_and_attach(psm);
3292 psmcpnp_probe(device_t dev)
3294 struct resource *res;
3298 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
3302 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
3303 * to the PS/2 mouse device node. But, some buggy PnP BIOS
3304 * declares the PS/2 mouse device node without an IRQ resource!
3305 * If this happens, we shall refer to device hints.
3306 * If we still don't find it there, use a hardcoded value... XXX
3309 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
3311 if (resource_long_value(PSM_DRIVER_NAME,
3312 device_get_unit(dev), "irq", &irq) != 0)
3314 device_printf(dev, "irq resource info is missing; "
3315 "assuming irq %ld\n", irq);
3316 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
3318 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3320 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
3326 return ((res == NULL) ? ENXIO : 0);
3330 psmcpnp_attach(device_t dev)
3335 /* find the keyboard controller, which may be on acpi* or isa* bus */
3336 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
3337 device_get_unit(dev));
3338 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
3339 create_a_copy(atkbdc, dev);
3342 * If we don't have the AT keyboard controller yet,
3343 * just reserve the IRQ for later use...
3344 * (See psmidentify() above.)
3347 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
3354 * Return true if 'now' is earlier than (start + (secs.usecs)).
3355 * Now may be NULL and the function will fetch the current time from
3356 * getmicrouptime(), or a cached 'now' can be passed in.
3357 * All values should be numbers derived from getmicrouptime().
3360 timeelapsed(start, secs, usecs, now)
3361 const struct timeval *start, *now;
3364 struct timeval snow, tv;
3366 /* if there is no 'now' passed in, the get it as a convience. */
3368 getmicrouptime(&snow);
3374 timevaladd(&tv, start);
3375 return (timevalcmp(&tv, now, <));
3378 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
3379 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);