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.
27 * Ported to 386bsd Oct 17, 1992
28 * Sandi Donno, Computer Science, University of Cape Town, South Africa
29 * Please send bug reports to sandi@cs.uct.ac.za
31 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
32 * although I was only partially successful in getting the alpha release
33 * of his "driver for the Logitech and ATI Inport Bus mice for use with
34 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
35 * found his code to be an invaluable reference when porting this driver
38 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
39 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
41 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
42 * Andrew Herbert - 12 June 1993
44 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
47 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
50 * Hardware access routines and probe logic rewritten by
51 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
52 * - 3, 14, 22 October 1996.
53 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
54 * - 14, 30 November 1996. Uses `kbdio.c'.
55 * - 13 December 1996. Uses queuing version of `kbdio.c'.
56 * - January/February 1997. Tweaked probe logic for
57 * HiNote UltraII/Latitude/Armada laptops.
58 * - 30 July 1997. Added APM support.
59 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
60 * Improved sync check logic.
61 * Vendor specific support routines.
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>
80 #include <machine/limits.h>
81 #include <sys/mouse.h>
82 #include <machine/resource.h>
84 #include <isa/isavar.h>
85 #include <dev/kbd/atkbdcreg.h>
88 * Driver specific options: the following options may be set by
89 * `options' statements in the kernel configuration file.
94 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */
97 #ifndef PSM_SYNCERR_THRESHOLD1
98 #define PSM_SYNCERR_THRESHOLD1 20
101 #ifndef PSM_INPUT_TIMEOUT
102 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
105 /* end of driver specific options */
107 #define PSM_DRIVER_NAME "psm"
108 #define PSMCPNP_DRIVER_NAME "psmcpnp"
111 #define PSM_BUFSIZE 960
112 #define PSM_SMALLBUFSIZE 240
114 /* operation levels */
115 #define PSM_LEVEL_BASE 0
116 #define PSM_LEVEL_STANDARD 1
117 #define PSM_LEVEL_NATIVE 2
118 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
119 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
121 /* Logitech PS2++ protocol */
122 #define MOUSE_PS2PLUS_CHECKBITS(b) \
123 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
124 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
125 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
128 #define PSM_UNIT(dev) (minor(dev) >> 1)
129 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
130 #define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1))
133 #define max(x,y) ((x) > (y) ? (x) : (y))
136 #define min(x,y) ((x) < (y) ? (x) : (y))
139 #define abs(x) (((x) < 0) ? -(x) : (x))
142 typedef struct ringbuf {
143 int count; /* # of valid elements in the buffer */
144 int head; /* head pointer */
145 int tail; /* tail poiner */
146 unsigned char buf[PSM_BUFSIZE];
149 /* driver control block */
150 struct psm_softc { /* Driver status information */
152 struct selinfo rsel; /* Process selecting for Input */
153 unsigned char state; /* Mouse driver state */
154 int config; /* driver configuration flags */
155 int flags; /* other flags */
156 KBDC kbdc; /* handle to access the keyboard controller */
157 struct resource *intr; /* IRQ resource */
158 void *ih; /* interrupt handle */
159 mousehw_t hw; /* hardware information */
160 mousemode_t mode; /* operation mode */
161 mousemode_t dflt_mode; /* default operation mode */
162 mousestatus_t status; /* accumulated mouse movement */
163 ringbuf_t queue; /* mouse status queue */
164 unsigned char ipacket[16]; /* interim input buffer */
165 int inputbytes; /* # of bytes in the input buffer */
166 int button; /* the latest button state */
167 int xold; /* previous absolute X position */
168 int yold; /* previous absolute Y position */
170 struct timeval inputtimeout;
171 int watchdog; /* watchdog timer flag */
172 struct callout_handle callout; /* watchdog timer call out */
176 devclass_t psm_devclass;
177 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
179 /* driver state flags (state) */
180 #define PSM_VALID 0x80
181 #define PSM_OPEN 1 /* Device is open */
182 #define PSM_ASLP 2 /* Waiting for mouse data */
184 /* driver configuration flags (config) */
185 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
186 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
187 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
188 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
189 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
190 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
191 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
192 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
193 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
194 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
196 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \
198 | PSM_CONFIG_NOCHECKSYNC \
199 | PSM_CONFIG_SYNCHACK \
200 | PSM_CONFIG_NOIDPROBE \
201 | PSM_CONFIG_NORESET \
202 | PSM_CONFIG_FORCETAP \
203 | PSM_CONFIG_IGNPORTERROR \
204 | PSM_CONFIG_HOOKRESUME \
205 | PSM_CONFIG_INITAFTERSUSPEND)
207 /* other flags (flags) */
208 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
210 /* for backward compatibility */
211 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
212 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
213 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
215 typedef struct old_mousehw {
222 typedef struct old_mousemode {
229 /* packet formatting function */
230 typedef int packetfunc_t(struct psm_softc *, unsigned char *,
231 int *, int, mousestatus_t *);
233 /* function prototypes */
234 static void psmidentify(driver_t *, device_t);
235 static int psmprobe(device_t);
236 static int psmattach(device_t);
237 static int psmdetach(device_t);
238 static int psmresume(device_t);
240 static d_open_t psmopen;
241 static d_close_t psmclose;
242 static d_read_t psmread;
243 static d_ioctl_t psmioctl;
244 static d_poll_t psmpoll;
246 static int enable_aux_dev(KBDC);
247 static int disable_aux_dev(KBDC);
248 static int get_mouse_status(KBDC, int *, int, int);
249 static int get_aux_id(KBDC);
250 static int set_mouse_sampling_rate(KBDC, int);
251 static int set_mouse_scaling(KBDC, int);
252 static int set_mouse_resolution(KBDC, int);
253 static int set_mouse_mode(KBDC);
254 static int get_mouse_buttons(KBDC);
255 static int is_a_mouse(int);
256 static void recover_from_error(KBDC);
257 static int restore_controller(KBDC, int);
258 static int doinitialize(struct psm_softc *, mousemode_t *);
259 static int doopen(struct psm_softc *, int);
260 static int reinitialize(struct psm_softc *, int);
261 static char *model_name(int);
262 static void psmintr(void *);
263 static void psmtimeout(void *);
265 /* vendor specific features */
266 typedef int probefunc_t(struct psm_softc *);
268 static int mouse_id_proc1(KBDC, int, int, int *);
269 static int mouse_ext_command(KBDC, int);
270 static probefunc_t enable_groller;
271 static probefunc_t enable_gmouse;
272 static probefunc_t enable_aglide;
273 static probefunc_t enable_kmouse;
274 static probefunc_t enable_msexplorer;
275 static probefunc_t enable_msintelli;
276 static probefunc_t enable_4dmouse;
277 static probefunc_t enable_4dplus;
278 static probefunc_t enable_mmanplus;
279 static probefunc_t enable_versapad;
280 static int tame_mouse(struct psm_softc *, mousestatus_t *, unsigned char *);
284 unsigned char syncmask;
286 probefunc_t *probefunc;
289 * WARNING: the order of probe is very important. Don't mess it
290 * unless you know what you are doing.
292 { MOUSE_MODEL_NET, /* Genius NetMouse */
293 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
294 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
295 0xc8, 6, enable_groller, },
296 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
297 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
298 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
299 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
300 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
301 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
302 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
303 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
304 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
305 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
306 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
307 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
308 { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */
309 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
310 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
311 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
312 { MOUSE_MODEL_GENERIC,
313 0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
315 #define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
317 /* device driver declarateion */
318 static device_method_t psm_methods[] = {
319 /* Device interface */
320 DEVMETHOD(device_identify, psmidentify),
321 DEVMETHOD(device_probe, psmprobe),
322 DEVMETHOD(device_attach, psmattach),
323 DEVMETHOD(device_detach, psmdetach),
324 DEVMETHOD(device_resume, psmresume),
329 static driver_t psm_driver = {
332 sizeof(struct psm_softc),
335 #define CDEV_MAJOR 21
337 static struct cdevsw psm_cdevsw = {
339 /* close */ psmclose,
342 /* ioctl */ psmioctl,
345 /* strategy */ nostrategy,
346 /* name */ PSM_DRIVER_NAME,
347 /* maj */ CDEV_MAJOR,
353 /* debug message level */
354 static int verbose = PSM_DEBUG;
356 /* device I/O routines */
358 enable_aux_dev(KBDC kbdc)
362 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
364 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
366 return (res == PSM_ACK);
370 disable_aux_dev(KBDC kbdc)
374 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
376 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
378 return (res == PSM_ACK);
382 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
391 cmd = PSMC_SEND_DEV_STATUS;
394 cmd = PSMC_SEND_DEV_DATA;
397 empty_aux_buffer(kbdc, 5);
398 res = send_aux_command(kbdc, cmd);
400 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
401 (flag == 1) ? "DATA" : "STATUS", res);
405 for (i = 0; i < len; ++i) {
406 status[i] = read_aux_data(kbdc);
412 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
413 (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
420 get_aux_id(KBDC kbdc)
425 empty_aux_buffer(kbdc, 5);
426 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
428 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
435 id = read_aux_data(kbdc);
437 log(LOG_DEBUG, "psm: device ID: %04x\n", id);
443 set_mouse_sampling_rate(KBDC kbdc, int rate)
447 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
449 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
451 return ((res == PSM_ACK) ? rate : -1);
455 set_mouse_scaling(KBDC kbdc, int scale)
462 scale = PSMC_SET_SCALING11;
465 scale = PSMC_SET_SCALING21;
468 res = send_aux_command(kbdc, scale);
470 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
471 (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
473 return (res == PSM_ACK);
476 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
478 set_mouse_resolution(KBDC kbdc, int val)
482 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
484 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
486 return ((res == PSM_ACK) ? val : -1);
490 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
491 * re-enabled by calling `enable_aux_dev()'
494 set_mouse_mode(KBDC kbdc)
498 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
500 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
502 return (res == PSM_ACK);
506 get_mouse_buttons(KBDC kbdc)
508 int c = 2; /* assume two buttons by default */
512 * NOTE: a special sequence to obtain Logitech Mouse specific
513 * information: set resolution to 25 ppi, set scaling to 1:1, set
514 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
515 * mouse status bytes is the number of available buttons.
516 * Some manufactures also support this sequence.
518 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
520 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
521 && set_mouse_scaling(kbdc, 1)
522 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
529 /* misc subroutines */
531 * Someday, I will get the complete list of valid pointing devices and
538 static int valid_ids[] = {
539 PSM_MOUSE_ID, /* mouse */
540 PSM_BALLPOINT_ID, /* ballpoint device */
541 PSM_INTELLI_ID, /* Intellimouse */
542 PSM_EXPLORER_ID, /* Intellimouse Explorer */
543 -1 /* end of table */
547 for (i = 0; valid_ids[i] >= 0; ++i)
548 if (valid_ids[i] == id)
557 model_name(int model)
563 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
564 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
565 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
566 { MOUSE_MODEL_THINK, "ThinkingMouse" },
567 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
568 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
569 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
570 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
571 { MOUSE_MODEL_4D, "4D Mouse" },
572 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
573 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
574 { MOUSE_MODEL_UNKNOWN, NULL },
578 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
579 if (models[i].model_code == model)
580 return models[i].model_name;
586 recover_from_error(KBDC kbdc)
588 /* discard anything left in the output buffer */
589 empty_both_buffers(kbdc, 10);
593 * NOTE: KBDC_RESET_KBD may not restore the communication between the
594 * keyboard and the controller.
599 * NOTE: somehow diagnostic and keyboard port test commands bring the
602 if (!test_controller(kbdc))
603 log(LOG_ERR, "psm: keyboard controller failed.\n");
604 /* if there isn't a keyboard in the system, the following error is OK */
605 if (test_kbd_port(kbdc) != 0) {
607 log(LOG_ERR, "psm: keyboard port failed.\n");
613 restore_controller(KBDC kbdc, int command_byte)
615 empty_both_buffers(kbdc, 10);
617 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
618 log(LOG_ERR, "psm: failed to restore the keyboard controller "
620 empty_both_buffers(kbdc, 10);
623 empty_both_buffers(kbdc, 10);
629 * Re-initialize the aux port and device. The aux port must be enabled
630 * and its interrupt must be disabled before calling this routine.
631 * The aux device will be disabled before returning.
632 * The keyboard controller must be locked via `kbdc_lock()' before
633 * calling this routine.
636 doinitialize(struct psm_softc *sc, mousemode_t *mode)
638 KBDC kbdc = sc->kbdc;
642 switch((i = test_aux_port(kbdc))) {
643 case 1: /* ignore this error */
646 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
649 case 0: /* no error */
651 case -1: /* time out */
653 recover_from_error(kbdc);
654 if (sc->config & PSM_CONFIG_IGNPORTERROR)
656 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
661 if (sc->config & PSM_CONFIG_NORESET) {
663 * Don't try to reset the pointing device. It may possibly be
664 * left in the unknown state, though...
668 * NOTE: some controllers appears to hang the `keyboard' when
669 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
671 if (!reset_aux_dev(kbdc)) {
672 recover_from_error(kbdc);
673 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
679 * both the aux port and the aux device is functioning, see
680 * if the device can be enabled.
682 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
683 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
686 empty_both_buffers(kbdc, 10); /* remove stray data if any */
688 if (sc->config & PSM_CONFIG_NOIDPROBE) {
689 i = GENERIC_MOUSE_ENTRY;
691 /* FIXME: hardware ID, mouse buttons? */
693 /* other parameters */
694 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
695 if ((*vendortype[i].probefunc)(sc)) {
697 log(LOG_ERR, "psm%d: found %s\n",
698 sc->unit, model_name(vendortype[i].model));
704 sc->hw.model = vendortype[i].model;
705 sc->mode.packetsize = vendortype[i].packetsize;
707 /* set mouse parameters */
708 if (mode != (mousemode_t *)NULL) {
710 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
711 if (mode->resolution >= 0)
712 mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
713 set_mouse_scaling(kbdc, 1);
714 set_mouse_mode(kbdc);
717 /* request a data packet and extract sync. bits */
718 if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
719 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
721 sc->mode.syncmask[0] = 0;
723 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
724 /* the NetScroll Mouse will send three more bytes... Ignore them */
725 empty_aux_buffer(kbdc, 5);
728 /* just check the status of the mouse */
729 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
730 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
737 doopen(struct psm_softc *sc, int command_byte)
741 /* enable the mouse device */
742 if (!enable_aux_dev(sc->kbdc)) {
743 /* MOUSE ERROR: failed to enable the mouse because:
744 * 1) the mouse is faulty,
745 * 2) the mouse has been removed(!?)
746 * In the latter case, the keyboard may have hung, and need
747 * recovery procedure...
749 recover_from_error(sc->kbdc);
751 /* FIXME: we could reset the mouse here and try to enable
752 * it again. But it will take long time and it's not a good
753 * idea to disable the keyboard that long...
755 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
756 recover_from_error(sc->kbdc);
760 restore_controller(sc->kbdc, command_byte);
761 /* mark this device is no longer available */
762 sc->state &= ~PSM_VALID;
763 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
769 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
770 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
772 /* enable the aux port and interrupt */
773 if (!set_controller_command_byte(sc->kbdc,
774 kbdc_get_device_mask(sc->kbdc),
775 (command_byte & KBD_KBD_CONTROL_BITS)
776 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
777 /* CONTROLLER ERROR */
778 disable_aux_dev(sc->kbdc);
779 restore_controller(sc->kbdc, command_byte);
780 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
785 /* start the watchdog timer */
786 sc->watchdog = FALSE;
787 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
793 reinitialize(struct psm_softc *sc, int doinit)
799 /* don't let anybody mess with the aux device */
800 if (!kbdc_lock(sc->kbdc, TRUE))
804 /* block our watchdog timer */
805 sc->watchdog = FALSE;
806 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
807 callout_handle_init(&sc->callout);
809 /* save the current controller command byte */
810 empty_both_buffers(sc->kbdc, 10);
811 c = get_controller_command_byte(sc->kbdc);
813 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
816 /* enable the aux port but disable the aux interrupt and the keyboard */
817 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
818 kbdc_get_device_mask(sc->kbdc),
819 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
820 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
821 /* CONTROLLER ERROR */
823 kbdc_lock(sc->kbdc, FALSE);
824 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
830 if (sc->state & PSM_VALID) {
831 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */
832 empty_aux_buffer(sc->kbdc, 10);
837 /* try to detect the aux device; are you still there? */
840 if (doinitialize(sc, &sc->mode)) {
842 sc->state |= PSM_VALID;
844 /* the device has gone! */
845 restore_controller(sc->kbdc, c);
846 sc->state &= ~PSM_VALID;
847 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
854 /* restore the driver state */
855 if ((sc->state & PSM_OPEN) && (err == 0)) {
856 /* enable the aux device and the port again */
859 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
862 /* restore the keyboard port and disable the aux port */
863 if (!set_controller_command_byte(sc->kbdc,
864 kbdc_get_device_mask(sc->kbdc),
865 (c & KBD_KBD_CONTROL_BITS)
866 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
867 /* CONTROLLER ERROR */
868 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
874 kbdc_lock(sc->kbdc, FALSE);
878 /* psm driver entry points */
881 psmidentify(driver_t *driver, device_t parent)
888 unit = device_get_unit(parent);
890 /* always add at least one child */
891 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
895 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
900 * If the PS/2 mouse device has already been reported by ACPI or
901 * PnP BIOS, obtain the IRQ resource from it.
902 * (See psmcpnp_attach() below.)
904 psmc = device_find_child(device_get_parent(parent),
905 PSMCPNP_DRIVER_NAME, unit);
908 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
911 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
914 #define endprobe(v) { if (bootverbose) \
916 kbdc_set_device_mask(sc->kbdc, mask); \
917 kbdc_lock(sc->kbdc, FALSE); \
922 psmprobe(device_t dev)
924 int unit = device_get_unit(dev);
925 struct psm_softc *sc = device_get_softc(dev);
936 /* see if IRQ is available */
938 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
939 RF_SHAREABLE | RF_ACTIVE);
940 if (sc->intr == NULL) {
942 device_printf(dev, "unable to allocate IRQ\n");
945 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
948 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
949 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
950 /* XXX: for backward compatibility */
951 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
953 #ifdef PSM_RESETAFTERSUSPEND
954 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
956 PSM_CONFIG_HOOKRESUME;
958 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
963 device_set_desc(dev, "PS/2 Mouse");
965 if (!kbdc_lock(sc->kbdc, TRUE)) {
966 printf("psm%d: unable to lock the controller.\n", unit);
973 * NOTE: two bits in the command byte controls the operation of the
974 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
975 * port interrupt (IRQ 12) enable bit (bit 2).
978 /* discard anything left after the keyboard initialization */
979 empty_both_buffers(sc->kbdc, 10);
981 /* save the current command byte; it will be used later */
982 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
983 command_byte = get_controller_command_byte(sc->kbdc);
985 printf("psm%d: current command byte:%04x\n", unit, command_byte);
986 if (command_byte == -1) {
987 /* CONTROLLER ERROR */
988 printf("psm%d: unable to get the current command byte value.\n",
994 * disable the keyboard port while probing the aux port, which must be
995 * enabled during this routine
997 if (!set_controller_command_byte(sc->kbdc,
998 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
999 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1000 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1002 * this is CONTROLLER ERROR; I don't know how to recover
1003 * from this error...
1005 restore_controller(sc->kbdc, command_byte);
1006 printf("psm%d: unable to set the command byte.\n", unit);
1009 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1012 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1013 * port exists and is functioning. However, some controllers appears
1014 * to respond with zero even when the aux port doesn't exist. (It may
1015 * be that this is only the case when the controller DOES have the aux
1016 * port but the port is not wired on the motherboard.) The keyboard
1017 * controllers without the port, such as the original AT, are
1018 * supporsed to return with an error code or simply time out. In any
1019 * case, we have to continue probing the port even when the controller
1022 * XXX: some controllers erroneously return the error code 1 when
1023 * it has the perfectly functional aux port. We have to ignore this
1024 * error code. Even if the controller HAS error with the aux port,
1025 * it will be detected later...
1026 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1028 switch ((i = test_aux_port(sc->kbdc))) {
1029 case 1: /* ignore this error */
1032 printf("psm%d: strange result for test aux port (%d).\n",
1035 case 0: /* no error */
1037 case -1: /* time out */
1038 default: /* error */
1039 recover_from_error(sc->kbdc);
1040 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1042 restore_controller(sc->kbdc, command_byte);
1044 printf("psm%d: the aux port is not functioning (%d).\n",
1049 if (sc->config & PSM_CONFIG_NORESET) {
1051 * Don't try to reset the pointing device. It may possibly be
1052 * left in the unknown state, though...
1056 * NOTE: some controllers appears to hang the `keyboard' when the aux
1057 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1059 if (!reset_aux_dev(sc->kbdc)) {
1060 recover_from_error(sc->kbdc);
1061 restore_controller(sc->kbdc, command_byte);
1063 printf("psm%d: failed to reset the aux device.\n", unit);
1069 * both the aux port and the aux device is functioning, see if the
1070 * device can be enabled. NOTE: when enabled, the device will start
1071 * sending data; we shall immediately disable the device once we know
1072 * the device can be enabled.
1074 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1076 recover_from_error(sc->kbdc);
1077 restore_controller(sc->kbdc, command_byte);
1079 printf("psm%d: failed to enable the aux device.\n", unit);
1083 /* save the default values after reset */
1084 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1085 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1086 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1088 sc->dflt_mode.rate = sc->mode.rate = -1;
1089 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1092 /* hardware information */
1093 sc->hw.iftype = MOUSE_IF_PS2;
1095 /* verify the device is a mouse */
1096 sc->hw.hwid = get_aux_id(sc->kbdc);
1097 if (!is_a_mouse(sc->hw.hwid)) {
1098 restore_controller(sc->kbdc, command_byte);
1100 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1103 switch (sc->hw.hwid) {
1104 case PSM_BALLPOINT_ID:
1105 sc->hw.type = MOUSE_TRACKBALL;
1108 case PSM_INTELLI_ID:
1109 case PSM_EXPLORER_ID:
1110 case PSM_4DMOUSE_ID:
1112 sc->hw.type = MOUSE_MOUSE;
1115 sc->hw.type = MOUSE_UNKNOWN;
1119 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1121 i = GENERIC_MOUSE_ENTRY;
1124 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1126 /* other parameters */
1127 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1128 if ((*vendortype[i].probefunc)(sc)) {
1130 printf("psm%d: found %s\n",
1131 unit, model_name(vendortype[i].model));
1137 sc->hw.model = vendortype[i].model;
1139 sc->dflt_mode.level = PSM_LEVEL_BASE;
1140 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1141 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1142 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1143 sc->dflt_mode.syncmask[0] = 0;
1145 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1146 if (sc->config & PSM_CONFIG_FORCETAP)
1147 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1148 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1149 sc->mode = sc->dflt_mode;
1150 sc->mode.packetsize = vendortype[i].packetsize;
1152 /* set mouse parameters */
1155 * A version of Logitech FirstMouse+ won't report wheel movement,
1156 * if SET_DEFAULTS is sent... Don't use this command.
1157 * This fix was found by Takashi Nishida.
1159 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1161 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1163 if (sc->config & PSM_CONFIG_RESOLUTION) {
1165 = set_mouse_resolution(sc->kbdc,
1166 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1167 } else if (sc->mode.resolution >= 0) {
1169 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1171 if (sc->mode.rate > 0) {
1172 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1174 set_mouse_scaling(sc->kbdc, 1);
1176 /* request a data packet and extract sync. bits */
1177 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1178 printf("psm%d: failed to get data.\n", unit);
1179 sc->mode.syncmask[0] = 0;
1181 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1182 /* the NetScroll Mouse will send three more bytes... Ignore them */
1183 empty_aux_buffer(sc->kbdc, 5);
1186 /* just check the status of the mouse */
1188 * NOTE: XXX there are some arcane controller/mouse combinations out
1189 * there, which hung the controller unless there is data transmission
1190 * after ACK from the mouse.
1192 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1193 printf("psm%d: failed to get status.\n", unit);
1196 * When in its native mode, some mice operate with different
1197 * default parameters than in the PS/2 compatible mode.
1199 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1200 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1203 /* disable the aux port for now... */
1204 if (!set_controller_command_byte(sc->kbdc,
1205 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1206 (command_byte & KBD_KBD_CONTROL_BITS)
1207 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1209 * this is CONTROLLER ERROR; I don't know the proper way to
1210 * recover from this error...
1212 restore_controller(sc->kbdc, command_byte);
1213 printf("psm%d: unable to set the command byte.\n", unit);
1218 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1219 kbdc_lock(sc->kbdc, FALSE);
1224 psmattach(device_t dev)
1226 int unit = device_get_unit(dev);
1227 struct psm_softc *sc = device_get_softc(dev);
1231 if (sc == NULL) /* shouldn't happen */
1234 /* Setup initial state */
1235 sc->state = PSM_VALID;
1236 callout_handle_init(&sc->callout);
1238 /* Setup our interrupt handler */
1240 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1241 RF_SHAREABLE | RF_ACTIVE);
1242 if (sc->intr == NULL)
1244 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1246 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1251 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1253 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1257 printf("psm%d: model %s, device ID %d\n",
1258 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1260 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1261 unit, model_name(sc->hw.model),
1262 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1263 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1264 unit, sc->config, sc->flags, sc->mode.packetsize);
1265 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1266 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1276 psmdetach(device_t dev)
1278 struct psm_softc *sc;
1281 sc = device_get_softc(dev);
1282 if (sc->state & PSM_OPEN)
1286 bus_teardown_intr(dev, sc->intr, sc->ih);
1287 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1289 destroy_dev(sc->dev);
1290 destroy_dev(sc->bdev);
1296 psmopen(dev_t dev, int flag, int fmt, struct thread *td)
1298 int unit = PSM_UNIT(dev);
1299 struct psm_softc *sc;
1304 /* Get device data */
1305 sc = PSM_SOFTC(unit);
1306 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1307 /* the device is no longer valid/functioning */
1310 /* Disallow multiple opens */
1311 if (sc->state & PSM_OPEN)
1314 device_busy(devclass_get_device(psm_devclass, unit));
1316 /* Initialize state */
1317 sc->mode.level = sc->dflt_mode.level;
1318 sc->mode.protocol = sc->dflt_mode.protocol;
1319 sc->watchdog = FALSE;
1321 /* flush the event queue */
1322 sc->queue.count = 0;
1325 sc->status.flags = 0;
1326 sc->status.button = 0;
1327 sc->status.obutton = 0;
1333 /* empty input buffer */
1334 bzero(sc->ipacket, sizeof(sc->ipacket));
1338 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1339 if (!kbdc_lock(sc->kbdc, TRUE))
1342 /* save the current controller command byte */
1344 command_byte = get_controller_command_byte(sc->kbdc);
1346 /* enable the aux port and temporalily disable the keyboard */
1347 if ((command_byte == -1)
1348 || !set_controller_command_byte(sc->kbdc,
1349 kbdc_get_device_mask(sc->kbdc),
1350 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1351 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1352 /* CONTROLLER ERROR; do you know how to get out of this? */
1353 kbdc_lock(sc->kbdc, FALSE);
1355 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1360 * Now that the keyboard controller is told not to generate
1361 * the keyboard and mouse interrupts, call `splx()' to allow
1362 * the other tty interrupts. The clock interrupt may also occur,
1363 * but timeout routines will be blocked by the poll flag set
1368 /* enable the mouse device */
1369 err = doopen(sc, command_byte);
1373 sc->state |= PSM_OPEN;
1374 kbdc_lock(sc->kbdc, FALSE);
1379 psmclose(dev_t dev, int flag, int fmt, struct thread *td)
1381 int unit = PSM_UNIT(dev);
1382 struct psm_softc *sc = PSM_SOFTC(unit);
1387 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1388 if (!kbdc_lock(sc->kbdc, TRUE))
1391 /* save the current controller command byte */
1393 command_byte = get_controller_command_byte(sc->kbdc);
1394 if (command_byte == -1) {
1395 kbdc_lock(sc->kbdc, FALSE);
1400 /* disable the aux interrupt and temporalily disable the keyboard */
1401 if (!set_controller_command_byte(sc->kbdc,
1402 kbdc_get_device_mask(sc->kbdc),
1403 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1404 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1405 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1407 /* CONTROLLER ERROR;
1408 * NOTE: we shall force our way through. Because the only
1409 * ill effect we shall see is that we may not be able
1410 * to read ACK from the mouse, and it doesn't matter much
1411 * so long as the mouse will accept the DISABLE command.
1416 /* stop the watchdog timer */
1417 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1418 callout_handle_init(&sc->callout);
1420 /* remove anything left in the output buffer */
1421 empty_aux_buffer(sc->kbdc, 10);
1423 /* disable the aux device, port and interrupt */
1424 if (sc->state & PSM_VALID) {
1425 if (!disable_aux_dev(sc->kbdc)) {
1427 * NOTE: we don't return error and continue, pretending
1428 * we have successfully disabled the device. It's OK because
1429 * the interrupt routine will discard any data from the mouse
1432 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1436 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1437 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1441 if (!set_controller_command_byte(sc->kbdc,
1442 kbdc_get_device_mask(sc->kbdc),
1443 (command_byte & KBD_KBD_CONTROL_BITS)
1444 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1445 /* CONTROLLER ERROR;
1446 * we shall ignore this error; see the above comment.
1448 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1452 /* remove anything left in the output buffer */
1453 empty_aux_buffer(sc->kbdc, 10);
1455 /* close is almost always successful */
1456 sc->state &= ~PSM_OPEN;
1457 kbdc_lock(sc->kbdc, FALSE);
1458 device_unbusy(devclass_get_device(psm_devclass, unit));
1463 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
1465 static unsigned char butmapps2[8] = {
1467 MOUSE_PS2_BUTTON1DOWN,
1468 MOUSE_PS2_BUTTON2DOWN,
1469 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1470 MOUSE_PS2_BUTTON3DOWN,
1471 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1472 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1473 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1475 static unsigned char butmapmsc[8] = {
1476 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1477 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1478 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1479 MOUSE_MSC_BUTTON3UP,
1480 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1481 MOUSE_MSC_BUTTON2UP,
1482 MOUSE_MSC_BUTTON1UP,
1488 if (sc->mode.level == PSM_LEVEL_BASE) {
1489 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1490 if (status->button & MOUSE_BUTTON4DOWN)
1491 mapped |= MOUSE_BUTTON1DOWN;
1492 status->button = mapped;
1493 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1494 i = max(min(status->dx, 255), -256);
1496 buf[0] |= MOUSE_PS2_XNEG;
1498 i = max(min(status->dy, 255), -256);
1500 buf[0] |= MOUSE_PS2_YNEG;
1502 return MOUSE_PS2_PACKETSIZE;
1503 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1504 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1505 i = max(min(status->dx, 255), -256);
1507 buf[3] = i - buf[1];
1508 i = max(min(status->dy, 255), -256);
1510 buf[4] = i - buf[2];
1511 i = max(min(status->dz, 127), -128);
1512 buf[5] = (i >> 1) & 0x7f;
1513 buf[6] = (i - (i >> 1)) & 0x7f;
1514 buf[7] = (~status->button >> 3) & 0x7f;
1515 return MOUSE_SYS_PACKETSIZE;
1517 return sc->inputbytes;;
1521 psmread(dev_t dev, struct uio *uio, int flag)
1523 register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1524 unsigned char buf[PSM_SMALLBUFSIZE];
1529 if ((sc->state & PSM_VALID) == 0)
1532 /* block until mouse activity occured */
1534 while (sc->queue.count <= 0) {
1535 if (PSM_NBLOCKIO(dev)) {
1539 sc->state |= PSM_ASLP;
1540 error = tsleep((caddr_t) sc, PZERO | PCATCH, "psmrea", 0);
1541 sc->state &= ~PSM_ASLP;
1545 } else if ((sc->state & PSM_VALID) == 0) {
1546 /* the device disappeared! */
1553 /* copy data to the user land */
1554 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1556 l = min(sc->queue.count, uio->uio_resid);
1557 if (l > sizeof(buf))
1559 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1560 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1561 sizeof(sc->queue.buf) - sc->queue.head);
1562 bcopy(&sc->queue.buf[0],
1563 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1564 l - (sizeof(sc->queue.buf) - sc->queue.head));
1566 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1568 sc->queue.count -= l;
1569 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1571 error = uiomove(buf, l, uio);
1580 block_mouse_data(struct psm_softc *sc, int *c)
1584 if (!kbdc_lock(sc->kbdc, TRUE))
1588 *c = get_controller_command_byte(sc->kbdc);
1590 || !set_controller_command_byte(sc->kbdc,
1591 kbdc_get_device_mask(sc->kbdc),
1592 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1593 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1594 /* this is CONTROLLER ERROR */
1596 kbdc_lock(sc->kbdc, FALSE);
1601 * The device may be in the middle of status data transmission.
1602 * The transmission will be interrupted, thus, incomplete status
1603 * data must be discarded. Although the aux interrupt is disabled
1604 * at the keyboard controller level, at most one aux interrupt
1605 * may have already been pending and a data byte is in the
1606 * output buffer; throw it away. Note that the second argument
1607 * to `empty_aux_buffer()' is zero, so that the call will just
1608 * flush the internal queue.
1609 * `psmintr()' will be invoked after `splx()' if an interrupt is
1610 * pending; it will see no data and returns immediately.
1612 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1613 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1621 unblock_mouse_data(struct psm_softc *sc, int c)
1626 * We may have seen a part of status data during `set_mouse_XXX()'.
1627 * they have been queued; flush it.
1629 empty_aux_buffer(sc->kbdc, 0);
1631 /* restore ports and interrupt */
1632 if (!set_controller_command_byte(sc->kbdc,
1633 kbdc_get_device_mask(sc->kbdc),
1634 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1635 /* CONTROLLER ERROR; this is serious, we may have
1636 * been left with the inaccessible keyboard and
1637 * the disabled mouse interrupt.
1642 kbdc_lock(sc->kbdc, FALSE);
1647 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1649 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1651 mousestatus_t status;
1652 #if (defined(MOUSE_GETVARS))
1661 /* Perform IOCTL command */
1664 case OLD_MOUSE_GETHWINFO:
1666 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1667 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1668 ((old_mousehw_t *)addr)->type = sc->hw.type;
1669 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1673 case MOUSE_GETHWINFO:
1675 *(mousehw_t *)addr = sc->hw;
1676 if (sc->mode.level == PSM_LEVEL_BASE)
1677 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1681 case OLD_MOUSE_GETMODE:
1683 switch (sc->mode.level) {
1684 case PSM_LEVEL_BASE:
1685 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1687 case PSM_LEVEL_STANDARD:
1688 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1690 case PSM_LEVEL_NATIVE:
1691 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1694 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1695 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1696 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1702 *(mousemode_t *)addr = sc->mode;
1703 ((mousemode_t *)addr)->resolution =
1704 MOUSE_RES_LOW - sc->mode.resolution;
1705 switch (sc->mode.level) {
1706 case PSM_LEVEL_BASE:
1707 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1708 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1710 case PSM_LEVEL_STANDARD:
1711 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1712 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1713 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1714 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1716 case PSM_LEVEL_NATIVE:
1717 /* FIXME: this isn't quite correct... XXX */
1718 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1724 case OLD_MOUSE_SETMODE:
1726 if (cmd == OLD_MOUSE_SETMODE) {
1727 mode.rate = ((old_mousemode_t *)addr)->rate;
1729 * resolution old I/F new I/F
1736 if (((old_mousemode_t *)addr)->resolution > 0)
1737 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1738 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1741 mode = *(mousemode_t *)addr;
1744 /* adjust and validate parameters. */
1745 if (mode.rate > UCHAR_MAX)
1748 mode.rate = sc->dflt_mode.rate;
1749 else if (mode.rate == -1)
1750 /* don't change the current setting */
1752 else if (mode.rate < 0)
1754 if (mode.resolution >= UCHAR_MAX)
1756 if (mode.resolution >= 200)
1757 mode.resolution = MOUSE_RES_HIGH;
1758 else if (mode.resolution >= 100)
1759 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1760 else if (mode.resolution >= 50)
1761 mode.resolution = MOUSE_RES_MEDIUMLOW;
1762 else if (mode.resolution > 0)
1763 mode.resolution = MOUSE_RES_LOW;
1764 if (mode.resolution == MOUSE_RES_DEFAULT)
1765 mode.resolution = sc->dflt_mode.resolution;
1766 else if (mode.resolution == -1)
1767 /* don't change the current setting */
1769 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1770 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1771 if (mode.level == -1)
1772 /* don't change the current setting */
1773 mode.level = sc->mode.level;
1774 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1776 if (mode.accelfactor == -1)
1777 /* don't change the current setting */
1778 mode.accelfactor = sc->mode.accelfactor;
1779 else if (mode.accelfactor < 0)
1782 /* don't allow anybody to poll the keyboard controller */
1783 error = block_mouse_data(sc, &command_byte);
1787 /* set mouse parameters */
1789 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1790 if (mode.resolution >= 0)
1791 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1792 set_mouse_scaling(sc->kbdc, 1);
1793 get_mouse_status(sc->kbdc, stat, 0, 3);
1796 sc->mode.rate = mode.rate;
1797 sc->mode.resolution = mode.resolution;
1798 sc->mode.accelfactor = mode.accelfactor;
1799 sc->mode.level = mode.level;
1802 unblock_mouse_data(sc, command_byte);
1805 case MOUSE_GETLEVEL:
1806 *(int *)addr = sc->mode.level;
1809 case MOUSE_SETLEVEL:
1810 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1812 sc->mode.level = *(int *)addr;
1815 case MOUSE_GETSTATUS:
1817 status = sc->status;
1818 sc->status.flags = 0;
1819 sc->status.obutton = sc->status.button;
1820 sc->status.button = 0;
1825 *(mousestatus_t *)addr = status;
1828 #if (defined(MOUSE_GETVARS))
1830 var = (mousevar_t *)addr;
1831 bzero(var, sizeof(*var));
1833 var->var[0] = MOUSE_VARS_PS2_SIG;
1834 var->var[1] = sc->config;
1835 var->var[2] = sc->flags;
1841 #endif /* MOUSE_GETVARS */
1843 case MOUSE_READSTATE:
1844 case MOUSE_READDATA:
1845 data = (mousedata_t *)addr;
1846 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1849 error = block_mouse_data(sc, &command_byte);
1852 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1853 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1855 unblock_mouse_data(sc, command_byte);
1858 #if (defined(MOUSE_SETRESOLUTION))
1859 case MOUSE_SETRESOLUTION:
1860 mode.resolution = *(int *)addr;
1861 if (mode.resolution >= UCHAR_MAX)
1863 else if (mode.resolution >= 200)
1864 mode.resolution = MOUSE_RES_HIGH;
1865 else if (mode.resolution >= 100)
1866 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1867 else if (mode.resolution >= 50)
1868 mode.resolution = MOUSE_RES_MEDIUMLOW;
1869 else if (mode.resolution > 0)
1870 mode.resolution = MOUSE_RES_LOW;
1871 if (mode.resolution == MOUSE_RES_DEFAULT)
1872 mode.resolution = sc->dflt_mode.resolution;
1873 else if (mode.resolution == -1)
1874 mode.resolution = sc->mode.resolution;
1875 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1876 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1878 error = block_mouse_data(sc, &command_byte);
1881 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1882 if (sc->mode.resolution != mode.resolution)
1884 unblock_mouse_data(sc, command_byte);
1886 #endif /* MOUSE_SETRESOLUTION */
1888 #if (defined(MOUSE_SETRATE))
1890 mode.rate = *(int *)addr;
1891 if (mode.rate > UCHAR_MAX)
1894 mode.rate = sc->dflt_mode.rate;
1895 else if (mode.rate < 0)
1896 mode.rate = sc->mode.rate;
1898 error = block_mouse_data(sc, &command_byte);
1901 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1902 if (sc->mode.rate != mode.rate)
1904 unblock_mouse_data(sc, command_byte);
1906 #endif /* MOUSE_SETRATE */
1908 #if (defined(MOUSE_SETSCALING))
1909 case MOUSE_SETSCALING:
1910 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1913 error = block_mouse_data(sc, &command_byte);
1916 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1918 unblock_mouse_data(sc, command_byte);
1920 #endif /* MOUSE_SETSCALING */
1922 #if (defined(MOUSE_GETHWID))
1924 error = block_mouse_data(sc, &command_byte);
1927 sc->hw.hwid &= ~0x00ff;
1928 sc->hw.hwid |= get_aux_id(sc->kbdc);
1929 *(int *)addr = sc->hw.hwid & 0x00ff;
1930 unblock_mouse_data(sc, command_byte);
1932 #endif /* MOUSE_GETHWID */
1942 psmtimeout(void *arg)
1944 struct psm_softc *sc;
1947 sc = (struct psm_softc *)arg;
1949 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1951 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1953 kbdc_lock(sc->kbdc, FALSE);
1955 sc->watchdog = TRUE;
1957 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
1964 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
1965 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
1967 static int butmap[8] = {
1971 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1973 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1974 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1975 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1977 static int butmap_versapad[8] = {
1983 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1985 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
1987 register struct psm_softc *sc = arg;
1995 /* read until there is nothing to read */
1996 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
1998 /* discard the byte if the device is not open */
1999 if ((sc->state & PSM_OPEN) == 0)
2002 getmicrouptime(&tv);
2003 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
2004 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
2008 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2009 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2010 timevaladd(&sc->inputtimeout, &tv);
2012 sc->ipacket[sc->inputbytes++] = c;
2013 if (sc->inputbytes < sc->mode.packetsize)
2017 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2018 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
2019 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
2024 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2025 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
2026 c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
2028 if (sc->syncerrors < sc->mode.packetsize) {
2029 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2031 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2032 } else if (sc->syncerrors == sc->mode.packetsize) {
2033 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2035 disable_aux_dev(sc->kbdc);
2036 enable_aux_dev(sc->kbdc);
2037 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2038 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2040 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2041 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2042 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2043 reinitialize(sc, TRUE);
2049 * A kludge for Kensington device!
2050 * The MSB of the horizontal count appears to be stored in
2053 if (sc->hw.model == MOUSE_MODEL_THINK)
2054 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2056 /* ignore the overflow bits... */
2057 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2058 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2060 ms.obutton = sc->button; /* previous button state */
2061 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2062 /* `tapping' action */
2063 if (sc->config & PSM_CONFIG_FORCETAP)
2064 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2066 switch (sc->hw.model) {
2068 case MOUSE_MODEL_EXPLORER:
2070 * b7 b6 b5 b4 b3 b2 b1 b0
2071 * byte 1: oy ox sy sx 1 M R L
2072 * byte 2: x x x x x x x x
2073 * byte 3: y y y y y y y y
2074 * byte 4: * * S2 S1 s d2 d1 d0
2076 * L, M, R, S1, S2: left, middle, right and side buttons
2077 * s: wheel data sign bit
2080 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2081 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
2082 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2083 ? MOUSE_BUTTON4DOWN : 0;
2084 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2085 ? MOUSE_BUTTON5DOWN : 0;
2088 case MOUSE_MODEL_INTELLI:
2089 case MOUSE_MODEL_NET:
2090 /* wheel data is in the fourth byte */
2091 z = (char)sc->ipacket[3];
2092 /* some mice may send 7 when there is no Z movement?! XXX */
2093 if ((z >= 7) || (z <= -7))
2095 /* some compatible mice have additional buttons */
2096 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2097 ? MOUSE_BUTTON4DOWN : 0;
2098 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2099 ? MOUSE_BUTTON5DOWN : 0;
2102 case MOUSE_MODEL_MOUSEMANPLUS:
2104 * PS2++ protocl packet
2106 * b7 b6 b5 b4 b3 b2 b1 b0
2107 * byte 1: * 1 p3 p2 1 * * *
2108 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2110 * p3-p0: packet type
2111 * c1, c2: c1 & c2 == 1, if p2 == 0
2112 * c1 & c2 == 0, if p2 == 1
2114 * packet type: 0 (device type)
2115 * See comments in enable_mmanplus() below.
2117 * packet type: 1 (wheel data)
2119 * b7 b6 b5 b4 b3 b2 b1 b0
2120 * byte 3: h * B5 B4 s d2 d1 d0
2122 * h: 1, if horizontal roller data
2123 * 0, if vertical roller data
2124 * B4, B5: button 4 and 5
2126 * d2-d0: roller data
2128 * packet type: 2 (reserved)
2130 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2132 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
2133 /* the extended data packet encodes button and wheel events */
2134 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
2136 /* wheel data packet */
2138 if (sc->ipacket[2] & 0x80) {
2139 /* horizontal roller count - ignore it XXX*/
2141 /* vertical roller count */
2142 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2143 ? (sc->ipacket[2] & 0x0f) - 16
2144 : (sc->ipacket[2] & 0x0f);
2146 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2147 ? MOUSE_BUTTON4DOWN : 0;
2148 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2149 ? MOUSE_BUTTON5DOWN : 0;
2152 /* this packet type is reserved by Logitech... */
2154 * IBM ScrollPoint Mouse uses this packet type to
2155 * encode both vertical and horizontal scroll movement.
2158 /* horizontal count */
2159 if (sc->ipacket[2] & 0x0f)
2160 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2161 /* vertical count */
2162 if (sc->ipacket[2] & 0xf0)
2163 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2165 /* vertical count */
2166 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
2167 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16
2168 : ((sc->ipacket[2] >> 4) & 0x0f);
2169 /* horizontal count */
2170 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
2171 ? (sc->ipacket[2] & 0x0f) - 16
2172 : (sc->ipacket[2] & 0x0f);
2176 /* device type packet - shouldn't happen */
2180 ms.button = ms.obutton;
2182 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2183 "0x%02x 0x%02x 0x%02x\n",
2184 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
2185 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
2189 /* preserve button states */
2190 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2194 case MOUSE_MODEL_GLIDEPOINT:
2195 /* `tapping' action */
2196 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2199 case MOUSE_MODEL_NETSCROLL:
2200 /* three addtional bytes encode buttons and wheel events */
2201 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2202 ? MOUSE_BUTTON4DOWN : 0;
2203 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2204 ? MOUSE_BUTTON5DOWN : 0;
2205 z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
2206 ? sc->ipacket[4] - 256 : sc->ipacket[4];
2209 case MOUSE_MODEL_THINK:
2210 /* the fourth button state in the first byte */
2211 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2214 case MOUSE_MODEL_VERSAPAD:
2215 /* VersaPad PS/2 absolute mode message format
2217 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2218 * ipacket[0]: 1 1 0 A 1 L T R
2219 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2220 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2221 * ipacket[3]: 1 1 1 A 1 L T R
2222 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2223 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2226 * R: right physical mouse button (1=on)
2227 * T: touch pad virtual button (1=tapping)
2228 * L: left physical mouse button (1=on)
2229 * A: position data is valid (1=valid)
2230 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2231 * V: vertical data (12bit signed integer. V11 is sign bit.)
2234 * Tapping is mapped to MOUSE_BUTTON4.
2236 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2237 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2239 if (c & MOUSE_PS2VERSA_IN_USE) {
2240 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
2241 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
2246 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2249 if (x < 0) /* XXX */
2258 sc->flags |= PSM_FLAGS_FINGERDOWN;
2263 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2265 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2266 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2269 case MOUSE_MODEL_4D:
2271 * b7 b6 b5 b4 b3 b2 b1 b0
2272 * byte 1: s2 d2 s1 d1 1 M R L
2273 * byte 2: sx x x x x x x x
2274 * byte 3: sy y y y y y y y
2276 * s1: wheel 1 direction
2278 * s2: wheel 2 direction
2281 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2282 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2283 switch (c & MOUSE_4D_WHEELBITS) {
2290 case 0x40: /* 2nd wheel turning right XXX */
2293 case 0xc0: /* 2nd wheel turning left XXX */
2299 case MOUSE_MODEL_4DPLUS:
2300 if ((x < 16 - 256) && (y < 16 - 256)) {
2302 * b7 b6 b5 b4 b3 b2 b1 b0
2303 * byte 1: 0 0 1 1 1 M R L
2304 * byte 2: 0 0 0 0 1 0 0 0
2305 * byte 3: 0 0 0 0 S s d1 d0
2307 * L, M, R, S: left, middle, right and side buttons
2308 * s: wheel data sign bit
2312 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2313 ms.button |= MOUSE_BUTTON4DOWN;
2314 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2315 ? ((sc->ipacket[2] & 0x07) - 8)
2316 : (sc->ipacket[2] & 0x07) ;
2318 /* preserve previous button states */
2319 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2323 case MOUSE_MODEL_GENERIC:
2329 if (sc->mode.accelfactor >= 1) {
2331 x = x * x / sc->mode.accelfactor;
2334 if (c & MOUSE_PS2_XNEG)
2338 y = y * y / sc->mode.accelfactor;
2341 if (c & MOUSE_PS2_YNEG)
2349 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2350 | (ms.obutton ^ ms.button);
2352 if (sc->mode.level < PSM_LEVEL_NATIVE)
2353 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
2355 sc->status.flags |= ms.flags;
2356 sc->status.dx += ms.dx;
2357 sc->status.dy += ms.dy;
2358 sc->status.dz += ms.dz;
2359 sc->status.button = ms.button;
2360 sc->button = ms.button;
2362 sc->watchdog = FALSE;
2365 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2366 l = min(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2367 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2368 if (sc->inputbytes > l)
2369 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
2371 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
2372 sc->queue.count += sc->inputbytes;
2376 if (sc->state & PSM_ASLP) {
2377 sc->state &= ~PSM_ASLP;
2378 wakeup((caddr_t) sc);
2380 selwakeup(&sc->rsel);
2385 psmpoll(dev_t dev, int events, struct thread *td)
2387 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2391 /* Return true if a mouse event available */
2393 if (events & (POLLIN | POLLRDNORM)) {
2394 if (sc->queue.count > 0)
2395 revents |= events & (POLLIN | POLLRDNORM);
2397 selrecord(td, &sc->rsel);
2404 /* vendor/model specific routines */
2406 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2408 if (set_mouse_resolution(kbdc, res) != res)
2410 if (set_mouse_scaling(kbdc, scale)
2411 && set_mouse_scaling(kbdc, scale)
2412 && set_mouse_scaling(kbdc, scale)
2413 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2419 mouse_ext_command(KBDC kbdc, int command)
2423 c = (command >> 6) & 0x03;
2424 if (set_mouse_resolution(kbdc, c) != c)
2426 c = (command >> 4) & 0x03;
2427 if (set_mouse_resolution(kbdc, c) != c)
2429 c = (command >> 2) & 0x03;
2430 if (set_mouse_resolution(kbdc, c) != c)
2432 c = (command >> 0) & 0x03;
2433 if (set_mouse_resolution(kbdc, c) != c)
2439 /* Logitech MouseMan Cordless II */
2441 enable_lcordless(struct psm_softc *sc)
2446 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2448 if (status[1] == PSMD_RES_HIGH)
2450 ch = (status[0] & 0x07) - 1; /* channel # */
2451 if ((ch <= 0) || (ch > 4))
2454 * status[1]: always one?
2455 * status[2]: battery status? (0-100)
2461 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2463 enable_groller(struct psm_softc *sc)
2468 * The special sequence to enable the fourth button and the
2469 * roller. Immediately after this sequence check status bytes.
2470 * if the mouse is NetScroll, the second and the third bytes are
2475 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2476 * look like the following.
2478 * byte 1 bit 7 always 0
2479 * bit 6 stream mode (0)
2480 * bit 5 disabled (0)
2481 * bit 4 1:1 scaling (0)
2483 * bit 0-2 button status
2484 * byte 2 resolution (PSMD_RES_HIGH)
2485 * byte 3 report rate (?)
2488 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2490 if ((status[1] != '3') || (status[2] != 'D'))
2492 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2497 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2499 enable_gmouse(struct psm_softc *sc)
2504 * The special sequence to enable the middle, "rubber" button.
2505 * Immediately after this sequence check status bytes.
2506 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2507 * the second and the third bytes are '3' and 'U'.
2508 * NOTE: NetMouse reports that it has three buttons although it has
2509 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2510 * say they have three buttons too and they do have a button on the
2513 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2515 if ((status[1] != '3') || (status[2] != 'U'))
2520 /* ALPS GlidePoint */
2522 enable_aglide(struct psm_softc *sc)
2527 * The special sequence to obtain ALPS GlidePoint specific
2528 * information. Immediately after this sequence, status bytes will
2529 * contain something interesting.
2530 * NOTE: ALPS produces several models of GlidePoint. Some of those
2531 * do not respond to this sequence, thus, cannot be detected this way.
2533 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2535 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2537 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2542 /* Kensington ThinkingMouse/Trackball */
2544 enable_kmouse(struct psm_softc *sc)
2546 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2547 KBDC kbdc = sc->kbdc;
2553 id1 = get_aux_id(kbdc);
2554 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2557 * The device is now in the native mode? It returns a different
2560 id2 = get_aux_id(kbdc);
2561 if ((id1 == id2) || (id2 != 2))
2564 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2567 /* at this point, resolution is LOW, sampling rate is 10/sec */
2568 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2573 * The special sequence to enable the third and fourth buttons.
2574 * Otherwise they behave like the first and second buttons.
2576 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2577 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2582 * At this point, the device is using default resolution and
2583 * sampling rate for the native mode.
2585 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2587 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2590 /* the device appears be enabled by this sequence, diable it for now */
2591 disable_aux_dev(kbdc);
2592 empty_aux_buffer(kbdc, 5);
2597 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2599 enable_mmanplus(struct psm_softc *sc)
2601 KBDC kbdc = sc->kbdc;
2604 /* the special sequence to enable the fourth button and the roller. */
2606 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2607 * must be called exactly three times since the last RESET command
2608 * before this sequence. XXX
2610 if (!set_mouse_scaling(kbdc, 1))
2612 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2614 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2618 * PS2++ protocl, packet type 0
2620 * b7 b6 b5 b4 b3 b2 b1 b0
2621 * byte 1: * 1 p3 p2 1 * * *
2622 * byte 2: 1 1 p1 p0 m1 m0 1 0
2623 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2625 * p3-p0: packet type: 0
2626 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2628 /* check constant bits */
2629 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2631 if ((data[1] & 0xc3) != 0xc2)
2633 /* check d3-d0 in byte 2 */
2634 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2637 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2640 sc->hw.hwid &= 0x00ff;
2641 sc->hw.hwid |= data[2] << 8; /* save model ID */
2644 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2645 * the wheel and the fourth button events are encoded in the
2646 * special data packet. The mouse may be put in the IntelliMouse mode
2647 * if it is initialized by the IntelliMouse's method.
2652 /* MS IntelliMouse Explorer */
2654 enable_msexplorer(struct psm_softc *sc)
2656 static unsigned char rate0[] = { 200, 100, 80, };
2657 static unsigned char rate1[] = { 200, 200, 80, };
2658 KBDC kbdc = sc->kbdc;
2662 /* the special sequence to enable the extra buttons and the roller. */
2663 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2664 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2667 /* the device will give the genuine ID only after the above sequence */
2668 id = get_aux_id(kbdc);
2669 if (id != PSM_EXPLORER_ID)
2673 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2676 * XXX: this is a kludge to fool some KVM switch products
2677 * which think they are clever enough to know the 4-byte IntelliMouse
2678 * protocol, and assume any other protocols use 3-byte packets.
2679 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2680 * correctly to the host computer because of this!
2681 * The following sequence is actually IntelliMouse's "wake up"
2682 * sequence; it will make the KVM think the mouse is IntelliMouse
2683 * when it is in fact IntelliMouse Explorer.
2685 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2686 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2689 id = get_aux_id(kbdc);
2694 /* MS IntelliMouse */
2696 enable_msintelli(struct psm_softc *sc)
2699 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2700 * probe routine and act like IntelliMouse.
2703 static unsigned char rate[] = { 200, 100, 80, };
2704 KBDC kbdc = sc->kbdc;
2708 /* the special sequence to enable the third button and the roller. */
2709 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2710 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2713 /* the device will give the genuine ID only after the above sequence */
2714 id = get_aux_id(kbdc);
2715 if (id != PSM_INTELLI_ID)
2724 /* A4 Tech 4D Mouse */
2726 enable_4dmouse(struct psm_softc *sc)
2729 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2732 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2733 KBDC kbdc = sc->kbdc;
2737 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2738 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2741 id = get_aux_id(kbdc);
2743 * WinEasy 4D, 4 Way Scroll 4D: 6
2744 * Cable-Free 4D: 8 (4DPLUS)
2745 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2747 if (id != PSM_4DMOUSE_ID)
2751 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2756 /* A4 Tech 4D+ Mouse */
2758 enable_4dplus(struct psm_softc *sc)
2761 * Newer wheel mice from A4 Tech seem to use this protocol.
2762 * Older models are recognized as either 4D Mouse or IntelliMouse.
2764 KBDC kbdc = sc->kbdc;
2768 * enable_4dmouse() already issued the following ID sequence...
2769 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2772 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2773 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2778 id = get_aux_id(kbdc);
2779 if (id != PSM_4DPLUS_ID)
2783 sc->hw.buttons = 4; /* XXX */
2788 /* Interlink electronics VersaPad */
2790 enable_versapad(struct psm_softc *sc)
2792 KBDC kbdc = sc->kbdc;
2795 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2796 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2797 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2798 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2799 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2800 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2801 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2803 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2805 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2807 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2809 return TRUE; /* PS/2 absolute mode */
2813 psmresume(device_t dev)
2815 struct psm_softc *sc = device_get_softc(dev);
2816 int unit = device_get_unit(dev);
2820 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2822 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2825 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2827 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2829 * Release the blocked process; it must be notified that the device
2830 * cannot be accessed anymore.
2832 sc->state &= ~PSM_ASLP;
2833 wakeup((caddr_t)sc);
2837 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2842 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
2845 * This sucks up assignments from PNPBIOS and ACPI.
2849 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
2850 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
2851 * can be probed and attached only after the AT keyboard controller is
2852 * attached, we shall quietly reserve the IRQ resource for later use.
2853 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
2854 * copy the IRQ resource to the PS/2 mouse device instance hanging
2855 * under the keyboard controller, then probe and attach it.
2858 static devclass_t psmcpnp_devclass;
2860 static device_probe_t psmcpnp_probe;
2861 static device_attach_t psmcpnp_attach;
2863 static device_method_t psmcpnp_methods[] = {
2864 DEVMETHOD(device_probe, psmcpnp_probe),
2865 DEVMETHOD(device_attach, psmcpnp_attach),
2870 static driver_t psmcpnp_driver = {
2871 PSMCPNP_DRIVER_NAME,
2876 static struct isa_pnp_id psmcpnp_ids[] = {
2877 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
2878 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
2879 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
2880 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
2881 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
2882 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio*/
2887 create_a_copy(device_t atkbdc, device_t me)
2892 /* find the PS/2 mouse device instance under the keyboard controller */
2893 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
2894 device_get_unit(atkbdc));
2897 if (device_get_state(psm) != DS_NOTPRESENT)
2900 /* move our resource to the found device */
2901 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
2902 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
2904 /* ...then probe and attach it */
2905 return device_probe_and_attach(psm);
2909 psmcpnp_probe(device_t dev)
2911 struct resource *res;
2915 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
2919 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
2920 * to the PS/2 mouse device node. But, some buggy PnP BIOS
2921 * declares the PS/2 mouse device node without an IRQ resource!
2922 * If this happens, we shall refer to device hints.
2923 * If we still don't find it there, use a hardcoded value... XXX
2926 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
2928 if (resource_long_value(PSM_DRIVER_NAME,
2929 device_get_unit(dev), "irq", &irq) != 0)
2931 device_printf(dev, "irq resource info is missing; "
2932 "assuming irq %ld\n", irq);
2933 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
2935 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
2937 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
2943 return ((res == NULL) ? ENXIO : 0);
2947 psmcpnp_attach(device_t dev)
2952 /* find the keyboard controller, which may be on acpi* or isa* bus */
2953 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
2954 device_get_unit(dev));
2955 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
2956 create_a_copy(atkbdc, dev);
2959 * If we don't have the AT keyboard controller yet,
2960 * just reserve the IRQ for later use...
2961 * (See psmidentify() above.)
2964 bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
2971 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
2972 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);