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$");
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/module.h>
73 #include <sys/filio.h>
75 #include <sys/sigio.h>
76 #include <sys/signalvar.h>
77 #include <sys/syslog.h>
78 #include <machine/bus.h>
80 #include <sys/selinfo.h>
81 #include <sys/sysctl.h>
85 #include <sys/limits.h>
86 #include <sys/mouse.h>
87 #include <machine/resource.h>
90 #include <isa/isavar.h>
93 #include <dev/atkbdc/atkbdcreg.h>
94 #include <dev/atkbdc/psm.h>
97 * Driver specific options: the following options may be set by
98 * `options' statements in the kernel configuration file.
103 #define PSM_DEBUG 0 /*
104 * logging: 0: none, 1: brief, 2: verbose
105 * 3: sync errors, 4: all packets
108 #define VLOG(level, args) do { \
109 if (verbose >= level) \
113 #ifndef PSM_INPUT_TIMEOUT
114 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
117 #ifndef PSM_TAP_TIMEOUT
118 #define PSM_TAP_TIMEOUT 125000
121 #ifndef PSM_TAP_THRESHOLD
122 #define PSM_TAP_THRESHOLD 25
125 /* end of driver specific options */
127 #define PSMCPNP_DRIVER_NAME "psmcpnp"
130 #define PSM_BUFSIZE 960
131 #define PSM_SMALLBUFSIZE 240
133 /* operation levels */
134 #define PSM_LEVEL_BASE 0
135 #define PSM_LEVEL_STANDARD 1
136 #define PSM_LEVEL_NATIVE 2
137 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
138 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
140 /* Logitech PS2++ protocol */
141 #define MOUSE_PS2PLUS_CHECKBITS(b) \
142 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
143 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
144 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
147 #define PSM_UNIT(dev) (dev2unit(dev) >> 1)
148 #define PSM_NBLOCKIO(dev) (dev2unit(dev) & 1)
149 #define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1))
152 typedef struct ringbuf {
153 int count; /* # of valid elements in the buffer */
154 int head; /* head pointer */
155 int tail; /* tail poiner */
156 u_char buf[PSM_BUFSIZE];
160 typedef struct packetbuf {
161 u_char ipacket[16]; /* interim input buffer */
162 int inputbytes; /* # of bytes in the input buffer */
165 #ifndef PSM_PACKETQUEUE
166 #define PSM_PACKETQUEUE 128
170 SYNAPTICS_SYSCTL_MIN_PRESSURE,
171 SYNAPTICS_SYSCTL_MAX_PRESSURE,
172 SYNAPTICS_SYSCTL_MAX_WIDTH,
173 SYNAPTICS_SYSCTL_MARGIN_TOP,
174 SYNAPTICS_SYSCTL_MARGIN_RIGHT,
175 SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
176 SYNAPTICS_SYSCTL_MARGIN_LEFT,
177 SYNAPTICS_SYSCTL_NA_TOP,
178 SYNAPTICS_SYSCTL_NA_RIGHT,
179 SYNAPTICS_SYSCTL_NA_BOTTOM,
180 SYNAPTICS_SYSCTL_NA_LEFT,
181 SYNAPTICS_SYSCTL_WINDOW_MIN,
182 SYNAPTICS_SYSCTL_WINDOW_MAX,
183 SYNAPTICS_SYSCTL_MULTIPLICATOR,
184 SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
185 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
186 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
187 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
188 SYNAPTICS_SYSCTL_DIV_MIN,
189 SYNAPTICS_SYSCTL_DIV_MAX,
190 SYNAPTICS_SYSCTL_DIV_MAX_NA,
191 SYNAPTICS_SYSCTL_DIV_LEN,
192 SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
193 SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
194 SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
195 SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
196 SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
197 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
198 SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
199 SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX
202 typedef struct synapticsinfo {
203 struct sysctl_ctx_list sysctl_ctx;
204 struct sysctl_oid *sysctl_tree;
205 int directional_scrolls;
222 int weight_previous_na;
223 int weight_len_squared;
231 int vscroll_ver_area;
232 int vscroll_hor_area;
233 int vscroll_min_delta;
238 typedef struct synapticspacket {
243 #define SYNAPTICS_PACKETQUEUE 10
244 #define SYNAPTICS_QUEUE_CURSOR(x) \
245 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
247 typedef struct synapticsaction {
248 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
264 /* driver control block */
265 struct psm_softc { /* Driver status information */
267 struct selinfo rsel; /* Process selecting for Input */
268 u_char state; /* Mouse driver state */
269 int config; /* driver configuration flags */
270 int flags; /* other flags */
271 KBDC kbdc; /* handle to access kbd controller */
272 struct resource *intr; /* IRQ resource */
273 void *ih; /* interrupt handle */
274 mousehw_t hw; /* hardware information */
275 synapticshw_t synhw; /* Synaptics hardware information */
276 synapticsinfo_t syninfo; /* Synaptics configuration */
277 synapticsaction_t synaction; /* Synaptics action context */
278 mousemode_t mode; /* operation mode */
279 mousemode_t dflt_mode; /* default operation mode */
280 mousestatus_t status; /* accumulated mouse movement */
281 ringbuf_t queue; /* mouse status queue */
282 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
283 int pqueue_start; /* start of data in queue */
284 int pqueue_end; /* end of data in queue */
285 int button; /* the latest button state */
286 int xold; /* previous absolute X position */
287 int yold; /* previous absolute Y position */
288 int xaverage; /* average X position */
289 int yaverage; /* average Y position */
290 int squelch; /* level to filter movement at low speed */
291 int zmax; /* maximum pressure value for touchpads */
292 int syncerrors; /* # of bytes discarded to synchronize */
293 int pkterrors; /* # of packets failed during quaranteen. */
294 struct timeval inputtimeout;
295 struct timeval lastsoftintr; /* time of last soft interrupt */
296 struct timeval lastinputerr; /* time last sync error happened */
297 struct timeval taptimeout; /* tap timeout for touchpads */
298 int watchdog; /* watchdog timer flag */
299 struct callout_handle callout; /* watchdog timer call out */
300 struct callout_handle softcallout; /* buffer timer call out */
305 struct sigio *async; /* Processes waiting for SIGIO */
307 static devclass_t psm_devclass;
308 #define PSM_SOFTC(unit) \
309 ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
311 /* driver state flags (state) */
312 #define PSM_VALID 0x80
313 #define PSM_OPEN 1 /* Device is open */
314 #define PSM_ASLP 2 /* Waiting for mouse data */
315 #define PSM_SOFTARMED 4 /* Software interrupt armed */
316 #define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
318 /* driver configuration flags (config) */
319 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
320 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
321 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
322 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
323 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
324 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
325 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
326 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
327 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
328 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
330 #define PSM_CONFIG_FLAGS \
331 (PSM_CONFIG_RESOLUTION | \
333 PSM_CONFIG_NOCHECKSYNC | \
334 PSM_CONFIG_SYNCHACK | \
335 PSM_CONFIG_NOIDPROBE | \
336 PSM_CONFIG_NORESET | \
337 PSM_CONFIG_FORCETAP | \
338 PSM_CONFIG_IGNPORTERROR | \
339 PSM_CONFIG_HOOKRESUME | \
340 PSM_CONFIG_INITAFTERSUSPEND)
342 /* other flags (flags) */
343 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
346 static int synaptics_support = 0;
347 TUNABLE_INT("hw.psm.synaptics_support", &synaptics_support);
349 static int verbose = PSM_DEBUG;
350 TUNABLE_INT("debug.psm.loglevel", &verbose);
352 /* for backward compatibility */
353 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
354 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
355 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
357 typedef struct old_mousehw {
364 typedef struct old_mousemode {
371 /* packet formatting function */
372 typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
375 /* function prototypes */
376 static void psmidentify(driver_t *, device_t);
377 static int psmprobe(device_t);
378 static int psmattach(device_t);
379 static int psmdetach(device_t);
380 static int psmresume(device_t);
382 static d_open_t psmopen;
383 static d_close_t psmclose;
384 static d_read_t psmread;
385 static d_write_t psmwrite;
386 static d_ioctl_t psmioctl;
387 static d_poll_t psmpoll;
389 static int enable_aux_dev(KBDC);
390 static int disable_aux_dev(KBDC);
391 static int get_mouse_status(KBDC, int *, int, int);
392 static int get_aux_id(KBDC);
393 static int set_mouse_sampling_rate(KBDC, int);
394 static int set_mouse_scaling(KBDC, int);
395 static int set_mouse_resolution(KBDC, int);
396 static int set_mouse_mode(KBDC);
397 static int get_mouse_buttons(KBDC);
398 static int is_a_mouse(int);
399 static void recover_from_error(KBDC);
400 static int restore_controller(KBDC, int);
401 static int doinitialize(struct psm_softc *, mousemode_t *);
402 static int doopen(struct psm_softc *, int);
403 static int reinitialize(struct psm_softc *, int);
404 static char *model_name(int);
405 static void psmsoftintr(void *);
406 static void psmintr(void *);
407 static void psmtimeout(void *);
408 static int timeelapsed(const struct timeval *, int, int,
409 const struct timeval *);
410 static void dropqueue(struct psm_softc *);
411 static void flushpackets(struct psm_softc *);
412 static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
413 mousestatus_t *, int *, int *, int *);
414 static int proc_synaptics(struct psm_softc *, packetbuf_t *,
415 mousestatus_t *, int *, int *, int *);
416 static void proc_versapad(struct psm_softc *, packetbuf_t *,
417 mousestatus_t *, int *, int *, int *);
418 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
421 /* vendor specific features */
422 typedef int probefunc_t(struct psm_softc *);
424 static int mouse_id_proc1(KBDC, int, int, int *);
425 static int mouse_ext_command(KBDC, int);
427 static probefunc_t enable_groller;
428 static probefunc_t enable_gmouse;
429 static probefunc_t enable_aglide;
430 static probefunc_t enable_kmouse;
431 static probefunc_t enable_msexplorer;
432 static probefunc_t enable_msintelli;
433 static probefunc_t enable_4dmouse;
434 static probefunc_t enable_4dplus;
435 static probefunc_t enable_mmanplus;
436 static probefunc_t enable_synaptics;
437 static probefunc_t enable_versapad;
443 probefunc_t *probefunc;
446 * WARNING: the order of probe is very important. Don't mess it
447 * unless you know what you are doing.
449 { MOUSE_MODEL_NET, /* Genius NetMouse */
450 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
451 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
452 0xc8, 6, enable_groller },
453 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
454 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
455 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
456 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
457 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
458 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
459 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
460 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
461 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
462 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
463 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
464 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
465 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
466 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
467 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
468 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
469 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
470 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
471 { MOUSE_MODEL_GENERIC,
472 0xc0, MOUSE_PS2_PACKETSIZE, NULL },
474 #define GENERIC_MOUSE_ENTRY \
475 ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
477 /* device driver declarateion */
478 static device_method_t psm_methods[] = {
479 /* Device interface */
480 DEVMETHOD(device_identify, psmidentify),
481 DEVMETHOD(device_probe, psmprobe),
482 DEVMETHOD(device_attach, psmattach),
483 DEVMETHOD(device_detach, psmdetach),
484 DEVMETHOD(device_resume, psmresume),
489 static driver_t psm_driver = {
492 sizeof(struct psm_softc),
495 static struct cdevsw psm_cdevsw = {
496 .d_version = D_VERSION,
497 .d_flags = D_NEEDGIANT,
504 .d_name = PSM_DRIVER_NAME,
507 /* device I/O routines */
509 enable_aux_dev(KBDC kbdc)
513 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
514 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
516 return (res == PSM_ACK);
520 disable_aux_dev(KBDC kbdc)
524 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
525 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
527 return (res == PSM_ACK);
531 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
540 cmd = PSMC_SEND_DEV_STATUS;
543 cmd = PSMC_SEND_DEV_DATA;
546 empty_aux_buffer(kbdc, 5);
547 res = send_aux_command(kbdc, cmd);
548 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
549 (flag == 1) ? "DATA" : "STATUS", res));
553 for (i = 0; i < len; ++i) {
554 status[i] = read_aux_data(kbdc);
559 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
560 (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
566 get_aux_id(KBDC kbdc)
571 empty_aux_buffer(kbdc, 5);
572 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
573 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
580 id = read_aux_data(kbdc);
581 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
587 set_mouse_sampling_rate(KBDC kbdc, int rate)
591 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
592 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
594 return ((res == PSM_ACK) ? rate : -1);
598 set_mouse_scaling(KBDC kbdc, int scale)
605 scale = PSMC_SET_SCALING11;
608 scale = PSMC_SET_SCALING21;
611 res = send_aux_command(kbdc, scale);
612 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
613 (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
615 return (res == PSM_ACK);
618 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
620 set_mouse_resolution(KBDC kbdc, int val)
624 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
625 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
627 return ((res == PSM_ACK) ? val : -1);
631 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
632 * re-enabled by calling `enable_aux_dev()'
635 set_mouse_mode(KBDC kbdc)
639 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
640 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
642 return (res == PSM_ACK);
646 get_mouse_buttons(KBDC kbdc)
648 int c = 2; /* assume two buttons by default */
652 * NOTE: a special sequence to obtain Logitech Mouse specific
653 * information: set resolution to 25 ppi, set scaling to 1:1, set
654 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
655 * mouse status bytes is the number of available buttons.
656 * Some manufactures also support this sequence.
658 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
660 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
661 set_mouse_scaling(kbdc, 1) &&
662 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
667 /* misc subroutines */
669 * Someday, I will get the complete list of valid pointing devices and
676 static int valid_ids[] = {
677 PSM_MOUSE_ID, /* mouse */
678 PSM_BALLPOINT_ID, /* ballpoint device */
679 PSM_INTELLI_ID, /* Intellimouse */
680 PSM_EXPLORER_ID, /* Intellimouse Explorer */
681 -1 /* end of table */
685 for (i = 0; valid_ids[i] >= 0; ++i)
686 if (valid_ids[i] == id)
695 model_name(int model)
701 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
702 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
703 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
704 { MOUSE_MODEL_THINK, "ThinkingMouse" },
705 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
706 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
707 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
708 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
709 { MOUSE_MODEL_4D, "4D Mouse" },
710 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
711 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
712 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
713 { MOUSE_MODEL_UNKNOWN, "Unknown" },
717 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
718 if (models[i].model_code == model)
720 return (models[i].model_name);
724 recover_from_error(KBDC kbdc)
726 /* discard anything left in the output buffer */
727 empty_both_buffers(kbdc, 10);
731 * NOTE: KBDC_RESET_KBD may not restore the communication between the
732 * keyboard and the controller.
737 * NOTE: somehow diagnostic and keyboard port test commands bring the
740 if (!test_controller(kbdc))
741 log(LOG_ERR, "psm: keyboard controller failed.\n");
742 /* if there isn't a keyboard in the system, the following error is OK */
743 if (test_kbd_port(kbdc) != 0)
744 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
749 restore_controller(KBDC kbdc, int command_byte)
751 empty_both_buffers(kbdc, 10);
753 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
754 log(LOG_ERR, "psm: failed to restore the keyboard controller "
756 empty_both_buffers(kbdc, 10);
759 empty_both_buffers(kbdc, 10);
765 * Re-initialize the aux port and device. The aux port must be enabled
766 * and its interrupt must be disabled before calling this routine.
767 * The aux device will be disabled before returning.
768 * The keyboard controller must be locked via `kbdc_lock()' before
769 * calling this routine.
772 doinitialize(struct psm_softc *sc, mousemode_t *mode)
774 KBDC kbdc = sc->kbdc;
778 switch((i = test_aux_port(kbdc))) {
779 case 1: /* ignore these errors */
785 "psm%d: strange result for test aux port (%d).\n",
788 case 0: /* no error */
790 case -1: /* time out */
792 recover_from_error(kbdc);
793 if (sc->config & PSM_CONFIG_IGNPORTERROR)
795 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
800 if (sc->config & PSM_CONFIG_NORESET) {
802 * Don't try to reset the pointing device. It may possibly
803 * be left in the unknown state, though...
807 * NOTE: some controllers appears to hang the `keyboard' when
808 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
810 if (!reset_aux_dev(kbdc)) {
811 recover_from_error(kbdc);
812 log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
819 * both the aux port and the aux device is functioning, see
820 * if the device can be enabled.
822 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
823 log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
827 empty_both_buffers(kbdc, 10); /* remove stray data if any */
829 if (sc->config & PSM_CONFIG_NOIDPROBE)
830 i = GENERIC_MOUSE_ENTRY;
832 /* FIXME: hardware ID, mouse buttons? */
834 /* other parameters */
835 for (i = 0; vendortype[i].probefunc != NULL; ++i)
836 if ((*vendortype[i].probefunc)(sc)) {
838 log(LOG_ERR, "psm%d: found %s\n",
840 model_name(vendortype[i].model));
845 sc->hw.model = vendortype[i].model;
846 sc->mode.packetsize = vendortype[i].packetsize;
848 /* set mouse parameters */
849 if (mode != (mousemode_t *)NULL) {
851 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
852 if (mode->resolution >= 0)
854 set_mouse_resolution(kbdc, mode->resolution);
855 set_mouse_scaling(kbdc, 1);
856 set_mouse_mode(kbdc);
859 /* Record sync on the next data packet we see. */
860 sc->flags |= PSM_NEED_SYNCBITS;
862 /* just check the status of the mouse */
863 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
864 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
871 doopen(struct psm_softc *sc, int command_byte)
876 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
877 * no obvious reason. Thus we check the current mode and restore the
878 * Absolute Mode if it was cleared.
880 * The previous hack at the end of psmprobe() wasn't efficient when
881 * moused(8) was restarted.
883 * A Reset (FF) or Set Defaults (F6) command would clear the
884 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
885 * doesn't show any evidence of such a command.
887 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
888 mouse_ext_command(sc->kbdc, 1);
889 get_mouse_status(sc->kbdc, stat, 0, 3);
890 if (stat[1] == 0x47 && stat[2] == 0x40) {
891 /* Set the mode byte -- request wmode where
893 if (sc->synhw.capExtended)
894 mouse_ext_command(sc->kbdc, 0xc1);
896 mouse_ext_command(sc->kbdc, 0xc0);
897 set_mouse_sampling_rate(sc->kbdc, 20);
898 VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
899 "hopefully restored\n",
904 /* enable the mouse device */
905 if (!enable_aux_dev(sc->kbdc)) {
906 /* MOUSE ERROR: failed to enable the mouse because:
907 * 1) the mouse is faulty,
908 * 2) the mouse has been removed(!?)
909 * In the latter case, the keyboard may have hung, and need
910 * recovery procedure...
912 recover_from_error(sc->kbdc);
914 /* FIXME: we could reset the mouse here and try to enable
915 * it again. But it will take long time and it's not a good
916 * idea to disable the keyboard that long...
918 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
919 recover_from_error(sc->kbdc);
923 restore_controller(sc->kbdc, command_byte);
924 /* mark this device is no longer available */
925 sc->state &= ~PSM_VALID;
927 "psm%d: failed to enable the device (doopen).\n",
933 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
934 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
937 /* enable the aux port and interrupt */
938 if (!set_controller_command_byte(sc->kbdc,
939 kbdc_get_device_mask(sc->kbdc),
940 (command_byte & KBD_KBD_CONTROL_BITS) |
941 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
942 /* CONTROLLER ERROR */
943 disable_aux_dev(sc->kbdc);
944 restore_controller(sc->kbdc, command_byte);
946 "psm%d: failed to enable the aux interrupt (doopen).\n",
951 /* start the watchdog timer */
952 sc->watchdog = FALSE;
953 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
959 reinitialize(struct psm_softc *sc, int doinit)
965 /* don't let anybody mess with the aux device */
966 if (!kbdc_lock(sc->kbdc, TRUE))
970 /* block our watchdog timer */
971 sc->watchdog = FALSE;
972 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
973 callout_handle_init(&sc->callout);
975 /* save the current controller command byte */
976 empty_both_buffers(sc->kbdc, 10);
977 c = get_controller_command_byte(sc->kbdc);
979 "psm%d: current command byte: %04x (reinitialize).\n",
982 /* enable the aux port but disable the aux interrupt and the keyboard */
983 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
984 kbdc_get_device_mask(sc->kbdc),
985 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
986 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
987 /* CONTROLLER ERROR */
989 kbdc_lock(sc->kbdc, FALSE);
991 "psm%d: unable to set the command byte (reinitialize).\n",
997 if (sc->state & PSM_VALID) {
998 /* this may fail; but never mind... */
999 disable_aux_dev(sc->kbdc);
1000 empty_aux_buffer(sc->kbdc, 10);
1005 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1007 /* try to detect the aux device; are you still there? */
1010 if (doinitialize(sc, &sc->mode)) {
1012 sc->state |= PSM_VALID;
1014 /* the device has gone! */
1015 restore_controller(sc->kbdc, c);
1016 sc->state &= ~PSM_VALID;
1018 "psm%d: the aux device has gone! (reinitialize).\n",
1025 /* restore the driver state */
1026 if ((sc->state & PSM_OPEN) && (err == 0)) {
1027 /* enable the aux device and the port again */
1028 err = doopen(sc, c);
1030 log(LOG_ERR, "psm%d: failed to enable the device "
1031 "(reinitialize).\n", sc->unit);
1033 /* restore the keyboard port and disable the aux port */
1034 if (!set_controller_command_byte(sc->kbdc,
1035 kbdc_get_device_mask(sc->kbdc),
1036 (c & KBD_KBD_CONTROL_BITS) |
1037 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1038 /* CONTROLLER ERROR */
1039 log(LOG_ERR, "psm%d: failed to disable the aux port "
1040 "(reinitialize).\n", sc->unit);
1045 kbdc_lock(sc->kbdc, FALSE);
1049 /* psm driver entry points */
1052 psmidentify(driver_t *driver, device_t parent)
1059 unit = device_get_unit(parent);
1061 /* always add at least one child */
1062 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1066 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1071 * If the PS/2 mouse device has already been reported by ACPI or
1072 * PnP BIOS, obtain the IRQ resource from it.
1073 * (See psmcpnp_attach() below.)
1075 psmc = device_find_child(device_get_parent(parent),
1076 PSMCPNP_DRIVER_NAME, unit);
1079 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1082 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
1085 #define endprobe(v) do { \
1088 kbdc_set_device_mask(sc->kbdc, mask); \
1089 kbdc_lock(sc->kbdc, FALSE); \
1094 psmprobe(device_t dev)
1096 int unit = device_get_unit(dev);
1097 struct psm_softc *sc = device_get_softc(dev);
1108 /* see if IRQ is available */
1110 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1111 RF_SHAREABLE | RF_ACTIVE);
1112 if (sc->intr == NULL) {
1114 device_printf(dev, "unable to allocate IRQ\n");
1117 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1120 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1121 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1122 /* XXX: for backward compatibility */
1123 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1125 #ifdef PSM_RESETAFTERSUSPEND
1126 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
1128 PSM_CONFIG_HOOKRESUME;
1130 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1135 device_set_desc(dev, "PS/2 Mouse");
1137 if (!kbdc_lock(sc->kbdc, TRUE)) {
1138 printf("psm%d: unable to lock the controller.\n", unit);
1145 * NOTE: two bits in the command byte controls the operation of the
1146 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1147 * port interrupt (IRQ 12) enable bit (bit 2).
1150 /* discard anything left after the keyboard initialization */
1151 empty_both_buffers(sc->kbdc, 10);
1153 /* save the current command byte; it will be used later */
1154 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1155 command_byte = get_controller_command_byte(sc->kbdc);
1157 printf("psm%d: current command byte:%04x\n", unit,
1159 if (command_byte == -1) {
1160 /* CONTROLLER ERROR */
1161 printf("psm%d: unable to get the current command byte value.\n",
1167 * disable the keyboard port while probing the aux port, which must be
1168 * enabled during this routine
1170 if (!set_controller_command_byte(sc->kbdc,
1171 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1172 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1173 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1175 * this is CONTROLLER ERROR; I don't know how to recover
1176 * from this error...
1178 restore_controller(sc->kbdc, command_byte);
1179 printf("psm%d: unable to set the command byte.\n", unit);
1182 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1185 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1186 * port exists and is functioning. However, some controllers appears
1187 * to respond with zero even when the aux port doesn't exist. (It may
1188 * be that this is only the case when the controller DOES have the aux
1189 * port but the port is not wired on the motherboard.) The keyboard
1190 * controllers without the port, such as the original AT, are
1191 * supporsed to return with an error code or simply time out. In any
1192 * case, we have to continue probing the port even when the controller
1195 * XXX: some controllers erroneously return the error code 1, 2 or 3
1196 * when it has the perfectly functional aux port. We have to ignore
1197 * this error code. Even if the controller HAS error with the aux
1198 * port, it will be detected later...
1199 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1201 switch ((i = test_aux_port(sc->kbdc))) {
1202 case 1: /* ignore these errors */
1207 printf("psm%d: strange result for test aux port "
1208 "(%d).\n", unit, i);
1210 case 0: /* no error */
1212 case -1: /* time out */
1213 default: /* error */
1214 recover_from_error(sc->kbdc);
1215 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1217 restore_controller(sc->kbdc, command_byte);
1219 printf("psm%d: the aux port is not functioning (%d).\n",
1224 if (sc->config & PSM_CONFIG_NORESET) {
1226 * Don't try to reset the pointing device. It may possibly be
1227 * left in the unknown state, though...
1231 * NOTE: some controllers appears to hang the `keyboard' when
1232 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1234 * Attempt to reset the controller twice -- this helps
1235 * pierce through some KVM switches. The second reset
1238 if (!reset_aux_dev(sc->kbdc)) {
1239 recover_from_error(sc->kbdc);
1240 restore_controller(sc->kbdc, command_byte);
1242 printf("psm%d: failed to reset the aux "
1245 } else if (!reset_aux_dev(sc->kbdc)) {
1246 recover_from_error(sc->kbdc);
1248 printf("psm%d: failed to reset the aux device "
1254 * both the aux port and the aux device is functioning, see if the
1255 * device can be enabled. NOTE: when enabled, the device will start
1256 * sending data; we shall immediately disable the device once we know
1257 * the device can be enabled.
1259 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1261 recover_from_error(sc->kbdc);
1262 restore_controller(sc->kbdc, command_byte);
1264 printf("psm%d: failed to enable the aux device.\n",
1269 /* save the default values after reset */
1270 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1271 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1272 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1274 sc->dflt_mode.rate = sc->mode.rate = -1;
1275 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1278 /* hardware information */
1279 sc->hw.iftype = MOUSE_IF_PS2;
1281 /* verify the device is a mouse */
1282 sc->hw.hwid = get_aux_id(sc->kbdc);
1283 if (!is_a_mouse(sc->hw.hwid)) {
1284 restore_controller(sc->kbdc, command_byte);
1286 printf("psm%d: unknown device type (%d).\n", unit,
1290 switch (sc->hw.hwid) {
1291 case PSM_BALLPOINT_ID:
1292 sc->hw.type = MOUSE_TRACKBALL;
1295 case PSM_INTELLI_ID:
1296 case PSM_EXPLORER_ID:
1297 case PSM_4DMOUSE_ID:
1299 sc->hw.type = MOUSE_MOUSE;
1302 sc->hw.type = MOUSE_UNKNOWN;
1306 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1308 i = GENERIC_MOUSE_ENTRY;
1311 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1313 /* other parameters */
1314 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1315 if ((*vendortype[i].probefunc)(sc)) {
1317 printf("psm%d: found %s\n", unit,
1318 model_name(vendortype[i].model));
1323 sc->hw.model = vendortype[i].model;
1325 sc->dflt_mode.level = PSM_LEVEL_BASE;
1326 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1327 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1328 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1329 sc->dflt_mode.syncmask[0] = 0;
1331 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1332 if (sc->config & PSM_CONFIG_FORCETAP)
1333 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1334 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1335 sc->mode = sc->dflt_mode;
1336 sc->mode.packetsize = vendortype[i].packetsize;
1338 /* set mouse parameters */
1341 * A version of Logitech FirstMouse+ won't report wheel movement,
1342 * if SET_DEFAULTS is sent... Don't use this command.
1343 * This fix was found by Takashi Nishida.
1345 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1347 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1349 if (sc->config & PSM_CONFIG_RESOLUTION)
1350 sc->mode.resolution =
1351 set_mouse_resolution(sc->kbdc,
1352 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1353 else if (sc->mode.resolution >= 0)
1354 sc->mode.resolution =
1355 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1356 if (sc->mode.rate > 0)
1358 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1359 set_mouse_scaling(sc->kbdc, 1);
1361 /* Record sync on the next data packet we see. */
1362 sc->flags |= PSM_NEED_SYNCBITS;
1364 /* just check the status of the mouse */
1366 * NOTE: XXX there are some arcane controller/mouse combinations out
1367 * there, which hung the controller unless there is data transmission
1368 * after ACK from the mouse.
1370 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1371 printf("psm%d: failed to get status.\n", unit);
1374 * When in its native mode, some mice operate with different
1375 * default parameters than in the PS/2 compatible mode.
1377 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1378 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1381 /* disable the aux port for now... */
1382 if (!set_controller_command_byte(sc->kbdc,
1383 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1384 (command_byte & KBD_KBD_CONTROL_BITS) |
1385 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1387 * this is CONTROLLER ERROR; I don't know the proper way to
1388 * recover from this error...
1390 restore_controller(sc->kbdc, command_byte);
1391 printf("psm%d: unable to set the command byte.\n", unit);
1396 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1397 kbdc_lock(sc->kbdc, FALSE);
1402 psmattach(device_t dev)
1404 int unit = device_get_unit(dev);
1405 struct psm_softc *sc = device_get_softc(dev);
1409 /* Setup initial state */
1410 sc->state = PSM_VALID;
1411 callout_handle_init(&sc->callout);
1413 /* Setup our interrupt handler */
1415 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1416 RF_SHAREABLE | RF_ACTIVE);
1417 if (sc->intr == NULL)
1419 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1422 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1427 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1429 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1433 printf("psm%d: model %s, device ID %d\n",
1434 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1436 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1437 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1438 sc->hw.hwid >> 8, sc->hw.buttons);
1439 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1440 unit, sc->config, sc->flags, sc->mode.packetsize);
1441 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1442 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1452 psmdetach(device_t dev)
1454 struct psm_softc *sc;
1457 sc = device_get_softc(dev);
1458 if (sc->state & PSM_OPEN)
1462 bus_teardown_intr(dev, sc->intr, sc->ih);
1463 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1465 destroy_dev(sc->dev);
1466 destroy_dev(sc->bdev);
1472 psmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
1474 int unit = PSM_UNIT(dev);
1475 struct psm_softc *sc;
1480 /* Get device data */
1481 sc = PSM_SOFTC(unit);
1482 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
1483 /* the device is no longer valid/functioning */
1487 /* Disallow multiple opens */
1488 if (sc->state & PSM_OPEN)
1491 device_busy(devclass_get_device(psm_devclass, unit));
1493 /* Initialize state */
1494 sc->mode.level = sc->dflt_mode.level;
1495 sc->mode.protocol = sc->dflt_mode.protocol;
1496 sc->watchdog = FALSE;
1499 /* flush the event queue */
1500 sc->queue.count = 0;
1503 sc->status.flags = 0;
1504 sc->status.button = 0;
1505 sc->status.obutton = 0;
1510 sc->pqueue_start = 0;
1513 /* empty input buffer */
1518 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1519 if (!kbdc_lock(sc->kbdc, TRUE))
1522 /* save the current controller command byte */
1524 command_byte = get_controller_command_byte(sc->kbdc);
1526 /* enable the aux port and temporalily disable the keyboard */
1527 if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
1528 kbdc_get_device_mask(sc->kbdc),
1529 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1530 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1531 /* CONTROLLER ERROR; do you know how to get out of this? */
1532 kbdc_lock(sc->kbdc, FALSE);
1535 "psm%d: unable to set the command byte (psmopen).\n", unit);
1539 * Now that the keyboard controller is told not to generate
1540 * the keyboard and mouse interrupts, call `splx()' to allow
1541 * the other tty interrupts. The clock interrupt may also occur,
1542 * but timeout routines will be blocked by the poll flag set
1547 /* enable the mouse device */
1548 err = doopen(sc, command_byte);
1552 sc->state |= PSM_OPEN;
1553 kbdc_lock(sc->kbdc, FALSE);
1558 psmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1560 int unit = PSM_UNIT(dev);
1561 struct psm_softc *sc = PSM_SOFTC(unit);
1566 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1567 if (!kbdc_lock(sc->kbdc, TRUE))
1570 /* save the current controller command byte */
1572 command_byte = get_controller_command_byte(sc->kbdc);
1573 if (command_byte == -1) {
1574 kbdc_lock(sc->kbdc, FALSE);
1579 /* disable the aux interrupt and temporalily disable the keyboard */
1580 if (!set_controller_command_byte(sc->kbdc,
1581 kbdc_get_device_mask(sc->kbdc),
1582 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1583 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1585 "psm%d: failed to disable the aux int (psmclose).\n", unit);
1586 /* CONTROLLER ERROR;
1587 * NOTE: we shall force our way through. Because the only
1588 * ill effect we shall see is that we may not be able
1589 * to read ACK from the mouse, and it doesn't matter much
1590 * so long as the mouse will accept the DISABLE command.
1595 /* stop the watchdog timer */
1596 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1597 callout_handle_init(&sc->callout);
1599 /* remove anything left in the output buffer */
1600 empty_aux_buffer(sc->kbdc, 10);
1602 /* disable the aux device, port and interrupt */
1603 if (sc->state & PSM_VALID) {
1604 if (!disable_aux_dev(sc->kbdc)) {
1606 * NOTE: we don't return (error) and continue,
1607 * pretending we have successfully disabled the device.
1608 * It's OK because the interrupt routine will discard
1609 * any data from the mouse hereafter.
1612 "psm%d: failed to disable the device (psmclose).\n",
1616 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1618 "psm%d: failed to get status (psmclose).\n", unit);
1621 if (!set_controller_command_byte(sc->kbdc,
1622 kbdc_get_device_mask(sc->kbdc),
1623 (command_byte & KBD_KBD_CONTROL_BITS) |
1624 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1627 * we shall ignore this error; see the above comment.
1630 "psm%d: failed to disable the aux port (psmclose).\n",
1634 /* remove anything left in the output buffer */
1635 empty_aux_buffer(sc->kbdc, 10);
1637 /* clean up and sigio requests */
1638 if (sc->async != NULL) {
1639 funsetown(&sc->async);
1643 /* close is almost always successful */
1644 sc->state &= ~PSM_OPEN;
1645 kbdc_lock(sc->kbdc, FALSE);
1646 device_unbusy(devclass_get_device(psm_devclass, unit));
1651 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
1654 static u_char butmapps2[8] = {
1656 MOUSE_PS2_BUTTON1DOWN,
1657 MOUSE_PS2_BUTTON2DOWN,
1658 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1659 MOUSE_PS2_BUTTON3DOWN,
1660 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1661 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1662 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
1663 MOUSE_PS2_BUTTON3DOWN,
1665 static u_char butmapmsc[8] = {
1666 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
1667 MOUSE_MSC_BUTTON3UP,
1668 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1669 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1670 MOUSE_MSC_BUTTON3UP,
1671 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1672 MOUSE_MSC_BUTTON2UP,
1673 MOUSE_MSC_BUTTON1UP,
1679 if (sc->mode.level == PSM_LEVEL_BASE) {
1680 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1681 if (status->button & MOUSE_BUTTON4DOWN)
1682 mapped |= MOUSE_BUTTON1DOWN;
1683 status->button = mapped;
1684 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1685 i = imax(imin(status->dx, 255), -256);
1687 buf[0] |= MOUSE_PS2_XNEG;
1689 i = imax(imin(status->dy, 255), -256);
1691 buf[0] |= MOUSE_PS2_YNEG;
1693 return (MOUSE_PS2_PACKETSIZE);
1694 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1695 buf[0] = MOUSE_MSC_SYNC |
1696 butmapmsc[status->button & MOUSE_STDBUTTONS];
1697 i = imax(imin(status->dx, 255), -256);
1699 buf[3] = i - buf[1];
1700 i = imax(imin(status->dy, 255), -256);
1702 buf[4] = i - buf[2];
1703 i = imax(imin(status->dz, 127), -128);
1704 buf[5] = (i >> 1) & 0x7f;
1705 buf[6] = (i - (i >> 1)) & 0x7f;
1706 buf[7] = (~status->button >> 3) & 0x7f;
1707 return (MOUSE_SYS_PACKETSIZE);
1709 return (pb->inputbytes);
1713 psmread(struct cdev *dev, struct uio *uio, int flag)
1715 register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1716 u_char buf[PSM_SMALLBUFSIZE];
1721 if ((sc->state & PSM_VALID) == 0)
1724 /* block until mouse activity occured */
1726 while (sc->queue.count <= 0) {
1727 if (PSM_NBLOCKIO(dev)) {
1729 return (EWOULDBLOCK);
1731 sc->state |= PSM_ASLP;
1732 error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
1733 sc->state &= ~PSM_ASLP;
1737 } else if ((sc->state & PSM_VALID) == 0) {
1738 /* the device disappeared! */
1745 /* copy data to the user land */
1746 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1748 l = imin(sc->queue.count, uio->uio_resid);
1749 if (l > sizeof(buf))
1751 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1752 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1753 sizeof(sc->queue.buf) - sc->queue.head);
1754 bcopy(&sc->queue.buf[0],
1755 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1756 l - (sizeof(sc->queue.buf) - sc->queue.head));
1758 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1759 sc->queue.count -= l;
1760 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1762 error = uiomove(buf, l, uio);
1771 block_mouse_data(struct psm_softc *sc, int *c)
1775 if (!kbdc_lock(sc->kbdc, TRUE))
1779 *c = get_controller_command_byte(sc->kbdc);
1780 if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
1781 kbdc_get_device_mask(sc->kbdc),
1782 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1783 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1784 /* this is CONTROLLER ERROR */
1786 kbdc_lock(sc->kbdc, FALSE);
1791 * The device may be in the middle of status data transmission.
1792 * The transmission will be interrupted, thus, incomplete status
1793 * data must be discarded. Although the aux interrupt is disabled
1794 * at the keyboard controller level, at most one aux interrupt
1795 * may have already been pending and a data byte is in the
1796 * output buffer; throw it away. Note that the second argument
1797 * to `empty_aux_buffer()' is zero, so that the call will just
1798 * flush the internal queue.
1799 * `psmintr()' will be invoked after `splx()' if an interrupt is
1800 * pending; it will see no data and returns immediately.
1802 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1803 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1811 dropqueue(struct psm_softc *sc)
1814 sc->queue.count = 0;
1817 if ((sc->state & PSM_SOFTARMED) != 0) {
1818 sc->state &= ~PSM_SOFTARMED;
1819 untimeout(psmsoftintr, (void *)(uintptr_t)sc, sc->softcallout);
1821 sc->pqueue_start = sc->pqueue_end;
1825 flushpackets(struct psm_softc *sc)
1829 bzero(&sc->pqueue, sizeof(sc->pqueue));
1833 unblock_mouse_data(struct psm_softc *sc, int c)
1838 * We may have seen a part of status data during `set_mouse_XXX()'.
1839 * they have been queued; flush it.
1841 empty_aux_buffer(sc->kbdc, 0);
1843 /* restore ports and interrupt */
1844 if (!set_controller_command_byte(sc->kbdc,
1845 kbdc_get_device_mask(sc->kbdc),
1846 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1848 * CONTROLLER ERROR; this is serious, we may have
1849 * been left with the inaccessible keyboard and
1850 * the disabled mouse interrupt.
1855 kbdc_lock(sc->kbdc, FALSE);
1860 psmwrite(struct cdev *dev, struct uio *uio, int flag)
1862 register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1863 u_char buf[PSM_SMALLBUFSIZE];
1864 int error = 0, i, l;
1866 if ((sc->state & PSM_VALID) == 0)
1869 if (sc->mode.level < PSM_LEVEL_NATIVE)
1872 /* copy data from the user land */
1873 while (uio->uio_resid > 0) {
1874 l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
1875 error = uiomove(buf, l, uio);
1878 for (i = 0; i < l; i++) {
1879 VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
1880 if (!write_aux_command(sc->kbdc, buf[i])) {
1882 "psm: cmd 0x%x failed.\n", buf[i]));
1883 return (reinitialize(sc, FALSE));
1892 psmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
1895 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1897 mousestatus_t status;
1898 #if (defined(MOUSE_GETVARS))
1907 /* Perform IOCTL command */
1910 case OLD_MOUSE_GETHWINFO:
1912 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1913 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1914 ((old_mousehw_t *)addr)->type = sc->hw.type;
1915 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1919 case MOUSE_GETHWINFO:
1921 *(mousehw_t *)addr = sc->hw;
1922 if (sc->mode.level == PSM_LEVEL_BASE)
1923 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1927 case MOUSE_SYN_GETHWINFO:
1929 if (synaptics_support && sc->hw.model == MOUSE_MODEL_SYNAPTICS)
1930 *(synapticshw_t *)addr = sc->synhw;
1936 case OLD_MOUSE_GETMODE:
1938 switch (sc->mode.level) {
1939 case PSM_LEVEL_BASE:
1940 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1942 case PSM_LEVEL_STANDARD:
1943 ((old_mousemode_t *)addr)->protocol =
1944 MOUSE_PROTO_SYSMOUSE;
1946 case PSM_LEVEL_NATIVE:
1947 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1950 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1951 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1952 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1958 *(mousemode_t *)addr = sc->mode;
1959 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
1960 ((mousemode_t *)addr)->syncmask[0] = 0;
1961 ((mousemode_t *)addr)->syncmask[1] = 0;
1963 ((mousemode_t *)addr)->resolution =
1964 MOUSE_RES_LOW - sc->mode.resolution;
1965 switch (sc->mode.level) {
1966 case PSM_LEVEL_BASE:
1967 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1968 ((mousemode_t *)addr)->packetsize =
1969 MOUSE_PS2_PACKETSIZE;
1971 case PSM_LEVEL_STANDARD:
1972 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1973 ((mousemode_t *)addr)->packetsize =
1974 MOUSE_SYS_PACKETSIZE;
1975 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1976 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1978 case PSM_LEVEL_NATIVE:
1979 /* FIXME: this isn't quite correct... XXX */
1980 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1986 case OLD_MOUSE_SETMODE:
1988 if (cmd == OLD_MOUSE_SETMODE) {
1989 mode.rate = ((old_mousemode_t *)addr)->rate;
1991 * resolution old I/F new I/F
1998 if (((old_mousemode_t *)addr)->resolution > 0)
2000 -((old_mousemode_t *)addr)->resolution - 1;
2002 mode.resolution = 0;
2004 ((old_mousemode_t *)addr)->accelfactor;
2007 mode = *(mousemode_t *)addr;
2009 /* adjust and validate parameters. */
2010 if (mode.rate > UCHAR_MAX)
2013 mode.rate = sc->dflt_mode.rate;
2014 else if (mode.rate == -1)
2015 /* don't change the current setting */
2017 else if (mode.rate < 0)
2019 if (mode.resolution >= UCHAR_MAX)
2021 if (mode.resolution >= 200)
2022 mode.resolution = MOUSE_RES_HIGH;
2023 else if (mode.resolution >= 100)
2024 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2025 else if (mode.resolution >= 50)
2026 mode.resolution = MOUSE_RES_MEDIUMLOW;
2027 else if (mode.resolution > 0)
2028 mode.resolution = MOUSE_RES_LOW;
2029 if (mode.resolution == MOUSE_RES_DEFAULT)
2030 mode.resolution = sc->dflt_mode.resolution;
2031 else if (mode.resolution == -1)
2032 /* don't change the current setting */
2034 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2035 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2036 if (mode.level == -1)
2037 /* don't change the current setting */
2038 mode.level = sc->mode.level;
2039 else if ((mode.level < PSM_LEVEL_MIN) ||
2040 (mode.level > PSM_LEVEL_MAX))
2042 if (mode.accelfactor == -1)
2043 /* don't change the current setting */
2044 mode.accelfactor = sc->mode.accelfactor;
2045 else if (mode.accelfactor < 0)
2048 /* don't allow anybody to poll the keyboard controller */
2049 error = block_mouse_data(sc, &command_byte);
2053 /* set mouse parameters */
2055 mode.rate = set_mouse_sampling_rate(sc->kbdc,
2057 if (mode.resolution >= 0)
2059 set_mouse_resolution(sc->kbdc, mode.resolution);
2060 set_mouse_scaling(sc->kbdc, 1);
2061 get_mouse_status(sc->kbdc, stat, 0, 3);
2064 sc->mode.rate = mode.rate;
2065 sc->mode.resolution = mode.resolution;
2066 sc->mode.accelfactor = mode.accelfactor;
2067 sc->mode.level = mode.level;
2070 unblock_mouse_data(sc, command_byte);
2073 case MOUSE_GETLEVEL:
2074 *(int *)addr = sc->mode.level;
2077 case MOUSE_SETLEVEL:
2078 if ((*(int *)addr < PSM_LEVEL_MIN) ||
2079 (*(int *)addr > PSM_LEVEL_MAX))
2081 sc->mode.level = *(int *)addr;
2084 case MOUSE_GETSTATUS:
2086 status = sc->status;
2087 sc->status.flags = 0;
2088 sc->status.obutton = sc->status.button;
2089 sc->status.button = 0;
2094 *(mousestatus_t *)addr = status;
2097 #if (defined(MOUSE_GETVARS))
2099 var = (mousevar_t *)addr;
2100 bzero(var, sizeof(*var));
2102 var->var[0] = MOUSE_VARS_PS2_SIG;
2103 var->var[1] = sc->config;
2104 var->var[2] = sc->flags;
2110 #endif /* MOUSE_GETVARS */
2112 case MOUSE_READSTATE:
2113 case MOUSE_READDATA:
2114 data = (mousedata_t *)addr;
2115 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2118 error = block_mouse_data(sc, &command_byte);
2121 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2122 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2124 unblock_mouse_data(sc, command_byte);
2127 #if (defined(MOUSE_SETRESOLUTION))
2128 case MOUSE_SETRESOLUTION:
2129 mode.resolution = *(int *)addr;
2130 if (mode.resolution >= UCHAR_MAX)
2132 else if (mode.resolution >= 200)
2133 mode.resolution = MOUSE_RES_HIGH;
2134 else if (mode.resolution >= 100)
2135 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2136 else if (mode.resolution >= 50)
2137 mode.resolution = MOUSE_RES_MEDIUMLOW;
2138 else if (mode.resolution > 0)
2139 mode.resolution = MOUSE_RES_LOW;
2140 if (mode.resolution == MOUSE_RES_DEFAULT)
2141 mode.resolution = sc->dflt_mode.resolution;
2142 else if (mode.resolution == -1)
2143 mode.resolution = sc->mode.resolution;
2144 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2145 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2147 error = block_mouse_data(sc, &command_byte);
2150 sc->mode.resolution =
2151 set_mouse_resolution(sc->kbdc, mode.resolution);
2152 if (sc->mode.resolution != mode.resolution)
2154 unblock_mouse_data(sc, command_byte);
2156 #endif /* MOUSE_SETRESOLUTION */
2158 #if (defined(MOUSE_SETRATE))
2160 mode.rate = *(int *)addr;
2161 if (mode.rate > UCHAR_MAX)
2164 mode.rate = sc->dflt_mode.rate;
2165 else if (mode.rate < 0)
2166 mode.rate = sc->mode.rate;
2168 error = block_mouse_data(sc, &command_byte);
2171 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2172 if (sc->mode.rate != mode.rate)
2174 unblock_mouse_data(sc, command_byte);
2176 #endif /* MOUSE_SETRATE */
2178 #if (defined(MOUSE_SETSCALING))
2179 case MOUSE_SETSCALING:
2180 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2183 error = block_mouse_data(sc, &command_byte);
2186 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2188 unblock_mouse_data(sc, command_byte);
2190 #endif /* MOUSE_SETSCALING */
2192 #if (defined(MOUSE_GETHWID))
2194 error = block_mouse_data(sc, &command_byte);
2197 sc->hw.hwid &= ~0x00ff;
2198 sc->hw.hwid |= get_aux_id(sc->kbdc);
2199 *(int *)addr = sc->hw.hwid & 0x00ff;
2200 unblock_mouse_data(sc, command_byte);
2202 #endif /* MOUSE_GETHWID */
2208 error = fsetown(*(int *)addr, &sc->async);
2211 *(int *) addr = fgetown(&sc->async);
2221 psmtimeout(void *arg)
2223 struct psm_softc *sc;
2226 sc = (struct psm_softc *)arg;
2228 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2229 VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2231 kbdc_lock(sc->kbdc, FALSE);
2233 sc->watchdog = TRUE;
2235 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
2238 /* Add all sysctls under the debug.psm and hw.psm nodes */
2239 SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2240 SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2242 SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RW, &verbose, 0,
2245 static int psmhz = 20;
2246 SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2247 "Frequency of the softcallout (in hz)");
2248 static int psmerrsecs = 2;
2249 SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2250 "Number of seconds during which packets will dropped after a sync error");
2251 static int psmerrusecs = 0;
2252 SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2253 "Microseconds to add to psmerrsecs");
2254 static int psmsecs = 0;
2255 SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2256 "Max number of seconds between soft interrupts");
2257 static int psmusecs = 500000;
2258 SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2259 "Microseconds to add to psmsecs");
2260 static int pkterrthresh = 2;
2261 SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2262 "Number of error packets allowed before reinitializing the mouse");
2264 static int tap_threshold = PSM_TAP_THRESHOLD;
2265 SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2266 "Button tap threshold");
2267 static int tap_timeout = PSM_TAP_TIMEOUT;
2268 SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2269 "Tap timeout for touchpads");
2274 struct psm_softc *sc = arg;
2280 /* read until there is nothing to read */
2281 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2282 pb = &sc->pqueue[sc->pqueue_end];
2284 /* discard the byte if the device is not open */
2285 if ((sc->state & PSM_OPEN) == 0)
2288 getmicrouptime(&now);
2289 if ((pb->inputbytes > 0) &&
2290 timevalcmp(&now, &sc->inputtimeout, >)) {
2291 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2292 "resetting byte count\n"));
2297 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2298 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2299 timevaladd(&sc->inputtimeout, &now);
2301 pb->ipacket[pb->inputbytes++] = c;
2303 if (sc->mode.level == PSM_LEVEL_NATIVE) {
2304 VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2309 if (pb->inputbytes < sc->mode.packetsize)
2313 "psmintr: %02x %02x %02x %02x %02x %02x\n",
2314 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2315 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2320 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2321 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2322 sc->flags &= ~PSM_NEED_SYNCBITS;
2324 "psmintr: Sync bytes now %04x,%04x\n",
2325 sc->mode.syncmask[0], sc->mode.syncmask[0]));
2326 } else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2327 VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
2328 "(%04x != %04x) %d cmds since last error.\n",
2329 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2330 sc->cmdcount - sc->lasterr));
2331 sc->lasterr = sc->cmdcount;
2333 * The sync byte test is a weak measure of packet
2334 * validity. Conservatively discard any input yet
2335 * to be seen by userland when we detect a sync
2336 * error since there is a good chance some of
2337 * the queued packets have undetected errors.
2340 if (sc->syncerrors == 0)
2343 sc->lastinputerr = now;
2344 if (sc->syncerrors >= sc->mode.packetsize * 2 ||
2345 sc->pkterrors >= pkterrthresh) {
2347 * If we've failed to find a single sync byte
2348 * in 2 packets worth of data, or we've seen
2349 * persistent packet errors during the
2350 * validation period, reinitialize the mouse
2351 * in hopes of returning it to the expected
2355 "psmintr: reset the mouse.\n"));
2356 reinitialize(sc, TRUE);
2357 } else if (sc->syncerrors == sc->mode.packetsize) {
2359 * Try a soft reset after searching for a sync
2360 * byte through a packet length of bytes.
2363 "psmintr: re-enable the mouse.\n"));
2365 disable_aux_dev(sc->kbdc);
2366 enable_aux_dev(sc->kbdc);
2369 "psmintr: discard a byte (%d)\n",
2372 bcopy(&pb->ipacket[1], &pb->ipacket[0],
2379 * We have what appears to be a valid packet.
2380 * Reset the error counters.
2385 * Drop even good packets if they occur within a timeout
2386 * period of a sync error. This allows the detection of
2387 * a change in the mouse's packet mode without exposing
2388 * erratic mouse behavior to the user. Some KVMs forget
2389 * enhanced mouse modes during switch events.
2391 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
2398 * Now that we're out of the validation period, reset
2399 * the packet error count.
2405 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2408 * If we've filled the queue then call the softintr ourselves,
2409 * otherwise schedule the interrupt for later.
2411 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2412 (sc->pqueue_end == sc->pqueue_start)) {
2413 if ((sc->state & PSM_SOFTARMED) != 0) {
2414 sc->state &= ~PSM_SOFTARMED;
2415 untimeout(psmsoftintr, arg, sc->softcallout);
2418 } else if ((sc->state & PSM_SOFTARMED) == 0) {
2419 sc->state |= PSM_SOFTARMED;
2420 sc->softcallout = timeout(psmsoftintr, arg,
2421 psmhz < 1 ? 1 : (hz/psmhz));
2427 proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2428 int *x, int *y, int *z)
2432 * PS2++ protocl packet
2434 * b7 b6 b5 b4 b3 b2 b1 b0
2435 * byte 1: * 1 p3 p2 1 * * *
2436 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2438 * p3-p0: packet type
2439 * c1, c2: c1 & c2 == 1, if p2 == 0
2440 * c1 & c2 == 0, if p2 == 1
2442 * packet type: 0 (device type)
2443 * See comments in enable_mmanplus() below.
2445 * packet type: 1 (wheel data)
2447 * b7 b6 b5 b4 b3 b2 b1 b0
2448 * byte 3: h * B5 B4 s d2 d1 d0
2450 * h: 1, if horizontal roller data
2451 * 0, if vertical roller data
2452 * B4, B5: button 4 and 5
2454 * d2-d0: roller data
2456 * packet type: 2 (reserved)
2458 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
2459 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2461 * the extended data packet encodes button
2464 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
2466 /* wheel data packet */
2468 if (pb->ipacket[2] & 0x80) {
2469 /* XXX horizontal roller count - ignore it */
2472 /* vertical roller count */
2473 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
2474 (pb->ipacket[2] & 0x0f) - 16 :
2475 (pb->ipacket[2] & 0x0f);
2477 ms->button |= (pb->ipacket[2] &
2478 MOUSE_PS2PLUS_BUTTON4DOWN) ?
2479 MOUSE_BUTTON4DOWN : 0;
2480 ms->button |= (pb->ipacket[2] &
2481 MOUSE_PS2PLUS_BUTTON5DOWN) ?
2482 MOUSE_BUTTON5DOWN : 0;
2486 * this packet type is reserved by
2490 * IBM ScrollPoint Mouse uses this
2491 * packet type to encode both vertical
2492 * and horizontal scroll movement.
2495 /* horizontal count */
2496 if (pb->ipacket[2] & 0x0f)
2497 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
2499 /* vertical count */
2500 if (pb->ipacket[2] & 0xf0)
2501 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
2505 /* device type packet - shouldn't happen */
2509 ms->button = ms->obutton;
2510 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
2511 "type %d: 0x%02x 0x%02x 0x%02x\n",
2512 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2513 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
2517 /* preserve button states */
2518 ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
2523 proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2524 int *x, int *y, int *z)
2526 static int touchpad_buttons;
2527 static int guest_buttons;
2530 /* TouchPad PS/2 absolute mode message format
2532 * Bits: 7 6 5 4 3 2 1 0 (LSB)
2533 * ------------------------------------------------
2534 * ipacket[0]: 1 0 W3 W2 0 W1 R L
2535 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
2536 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
2537 * ipacket[3]: 1 1 Yc Xc 0 W0 D U
2538 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
2539 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
2542 * L: left physical mouse button
2543 * R: right physical mouse button
2551 * Absolute reportable limits: 0 - 6143.
2552 * Typical bezel limits: 1472 - 5472.
2553 * Typical edge marings: 1632 - 5312.
2555 * w = 3 Passthrough Packet
2557 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2560 if (!synaptics_support)
2563 /* Sanity check for out of sync packets. */
2564 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2565 (pb->ipacket[3] & 0xc8) != 0xc0)
2573 * z = 0 No finger contact
2574 * z = 10 Finger hovering near the pad
2575 * z = 30 Very light finger contact
2576 * z = 80 Normal finger contact
2577 * z = 110 Very heavy finger contact
2578 * z = 200 Finger lying flat on pad surface
2579 * z = 255 Maximum reportable Z
2581 *z = pb->ipacket[2];
2584 * Finger width value
2586 * w = 0 Two finger on the pad (capMultiFinger needed)
2587 * w = 1 Three or more fingers (capMultiFinger needed)
2588 * w = 2 Pen (instead of finger) (capPen needed)
2589 * w = 3 Reserved (passthrough?)
2590 * w = 4-7 Finger of normal width (capPalmDetect needed)
2591 * w = 8-14 Very wide finger or palm (capPalmDetect needed)
2592 * w = 15 Maximum reportable width (capPalmDetect needed)
2594 /* XXX Is checking capExtended enough? */
2595 if (sc->synhw.capExtended)
2596 w = ((pb->ipacket[0] & 0x30) >> 2) |
2597 ((pb->ipacket[0] & 0x04) >> 1) |
2598 ((pb->ipacket[3] & 0x04) >> 2);
2600 /* Assume a finger of regular width. */
2604 /* Handle packets from the guest device */
2605 /* XXX Documentation? */
2606 if (w == 3 && sc->synhw.capPassthrough) {
2607 *x = ((pb->ipacket[1] & 0x10) ?
2608 pb->ipacket[4] - 256 : pb->ipacket[4]);
2609 *y = ((pb->ipacket[1] & 0x20) ?
2610 pb->ipacket[5] - 256 : pb->ipacket[5]);
2614 if (pb->ipacket[1] & 0x01)
2615 guest_buttons |= MOUSE_BUTTON1DOWN;
2616 if (pb->ipacket[1] & 0x04)
2617 guest_buttons |= MOUSE_BUTTON2DOWN;
2618 if (pb->ipacket[1] & 0x02)
2619 guest_buttons |= MOUSE_BUTTON3DOWN;
2621 ms->button = touchpad_buttons | guest_buttons;
2625 /* Button presses */
2626 touchpad_buttons = 0;
2627 if (pb->ipacket[0] & 0x01)
2628 touchpad_buttons |= MOUSE_BUTTON1DOWN;
2629 if (pb->ipacket[0] & 0x02)
2630 touchpad_buttons |= MOUSE_BUTTON3DOWN;
2632 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2633 if ((pb->ipacket[3] & 0x01) && (pb->ipacket[0] & 0x01) == 0)
2634 touchpad_buttons |= MOUSE_BUTTON4DOWN;
2635 if ((pb->ipacket[3] & 0x02) && (pb->ipacket[0] & 0x02) == 0)
2636 touchpad_buttons |= MOUSE_BUTTON5DOWN;
2640 * In newer pads - bit 0x02 in the third byte of
2641 * the packet indicates that we have an extended
2644 /* XXX Documentation? */
2645 if (pb->ipacket[3] & 0x02) {
2647 * if directional_scrolls is not 1, we treat any of
2648 * the scrolling directions as middle-click.
2650 if (sc->syninfo.directional_scrolls) {
2651 if (pb->ipacket[4] & 0x01)
2652 touchpad_buttons |= MOUSE_BUTTON4DOWN;
2653 if (pb->ipacket[5] & 0x01)
2654 touchpad_buttons |= MOUSE_BUTTON5DOWN;
2655 if (pb->ipacket[4] & 0x02)
2656 touchpad_buttons |= MOUSE_BUTTON6DOWN;
2657 if (pb->ipacket[5] & 0x02)
2658 touchpad_buttons |= MOUSE_BUTTON7DOWN;
2660 if ((pb->ipacket[4] & 0x0F) ||
2661 (pb->ipacket[5] & 0x0F))
2662 touchpad_buttons |= MOUSE_BUTTON2DOWN;
2666 ms->button = touchpad_buttons | guest_buttons;
2668 /* Check pressure to detect a real wanted action on the
2670 if (*z >= sc->syninfo.min_pressure) {
2671 synapticsaction_t *synaction;
2672 int cursor, peer, window;
2673 int dx, dy, dxp, dyp;
2674 int max_width, max_pressure;
2675 int margin_top, margin_right, margin_bottom, margin_left;
2676 int na_top, na_right, na_bottom, na_left;
2677 int window_min, window_max;
2679 int weight_current, weight_previous, weight_len_squared;
2680 int div_min, div_max, div_len;
2681 int vscroll_hor_area, vscroll_ver_area;
2683 int len, weight_prev_x, weight_prev_y;
2684 int div_max_x, div_max_y, div_x, div_y;
2687 /* XXX Verify values? */
2688 max_width = sc->syninfo.max_width;
2689 max_pressure = sc->syninfo.max_pressure;
2690 margin_top = sc->syninfo.margin_top;
2691 margin_right = sc->syninfo.margin_right;
2692 margin_bottom = sc->syninfo.margin_bottom;
2693 margin_left = sc->syninfo.margin_left;
2694 na_top = sc->syninfo.na_top;
2695 na_right = sc->syninfo.na_right;
2696 na_bottom = sc->syninfo.na_bottom;
2697 na_left = sc->syninfo.na_left;
2698 window_min = sc->syninfo.window_min;
2699 window_max = sc->syninfo.window_max;
2700 multiplicator = sc->syninfo.multiplicator;
2701 weight_current = sc->syninfo.weight_current;
2702 weight_previous = sc->syninfo.weight_previous;
2703 weight_len_squared = sc->syninfo.weight_len_squared;
2704 div_min = sc->syninfo.div_min;
2705 div_max = sc->syninfo.div_max;
2706 div_len = sc->syninfo.div_len;
2707 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
2708 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
2710 /* Palm detection. */
2712 (sc->synhw.capMultiFinger && (w == 0 || w == 1)) ||
2713 (sc->synhw.capPalmDetect && w >= 4 && w <= max_width) ||
2714 (!sc->synhw.capPalmDetect && *z <= max_pressure) ||
2715 (sc->synhw.capPen && w == 2))) {
2717 * We consider the packet irrelevant for the current
2719 * - the width isn't comprised in:
2721 * - the pressure isn't comprised in:
2722 * [min_pressure; max_pressure]
2723 * - pen aren't supported but w is 2
2725 * Note that this doesn't terminate the current action.
2728 "synaptics: palm detected! (%d)\n", w));
2732 /* Read current absolute position. */
2733 x0 = ((pb->ipacket[3] & 0x10) << 8) |
2734 ((pb->ipacket[1] & 0x0f) << 8) |
2736 y0 = ((pb->ipacket[3] & 0x20) << 7) |
2737 ((pb->ipacket[1] & 0xf0) << 4) |
2740 synaction = &(sc->synaction);
2743 * If the action is just beginning, init the structure and
2744 * compute tap timeout.
2746 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
2747 VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
2749 /* Store the first point of this action. */
2750 synaction->start_x = x0;
2751 synaction->start_y = y0;
2754 /* Initialize queue. */
2755 synaction->queue_cursor = SYNAPTICS_PACKETQUEUE;
2756 synaction->queue_len = 0;
2757 synaction->window_min = window_min;
2759 /* Reset average. */
2760 synaction->avg_dx = 0;
2761 synaction->avg_dy = 0;
2763 /* Reset squelch. */
2764 synaction->squelch_x = 0;
2765 synaction->squelch_y = 0;
2767 /* Reset pressure peak. */
2770 /* Reset fingers count. */
2771 synaction->fingers_nb = 0;
2773 /* Reset virtual scrolling state. */
2774 synaction->in_vscroll = 0;
2776 /* Compute tap timeout. */
2777 sc->taptimeout.tv_sec = tap_timeout / 1000000;
2778 sc->taptimeout.tv_usec = tap_timeout % 1000000;
2779 timevaladd(&sc->taptimeout, &sc->lastsoftintr);
2781 sc->flags |= PSM_FLAGS_FINGERDOWN;
2783 /* Calculate the current delta. */
2784 cursor = synaction->queue_cursor;
2785 dx = x0 - synaction->queue[cursor].x;
2786 dy = y0 - synaction->queue[cursor].y;
2789 /* If in tap-hold, add the recorded button. */
2790 if (synaction->in_taphold)
2791 ms->button |= synaction->tap_button;
2794 * From now on, we can use the SYNAPTICS_END label to skip
2795 * the current packet.
2799 * Limit the coordinates to the specified margins because
2800 * this area isn't very reliable.
2802 if (x0 <= margin_left)
2804 else if (x0 >= 6143 - margin_right)
2805 x0 = 6143 - margin_right;
2806 if (y0 <= margin_bottom)
2808 else if (y0 >= 6143 - margin_top)
2809 y0 = 6143 - margin_top;
2811 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
2814 /* Queue this new packet. */
2815 cursor = SYNAPTICS_QUEUE_CURSOR(synaction->queue_cursor - 1);
2816 synaction->queue[cursor].x = x0;
2817 synaction->queue[cursor].y = y0;
2818 synaction->queue_cursor = cursor;
2819 if (synaction->queue_len < SYNAPTICS_PACKETQUEUE)
2820 synaction->queue_len++;
2822 "synaptics: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
2823 cursor, x0, y0, dx, dy));
2826 * For tap, we keep the maximum number of fingers and the
2827 * pressure peak. Also with multiple fingers, we increase
2828 * the minimum window.
2831 case 1: /* Three or more fingers. */
2832 synaction->fingers_nb = imax(3, synaction->fingers_nb);
2833 synaction->window_min = window_max;
2835 case 0: /* Two fingers. */
2836 synaction->fingers_nb = imax(2, synaction->fingers_nb);
2837 synaction->window_min = window_max;
2839 default: /* One finger or undetectable. */
2840 synaction->fingers_nb = imax(1, synaction->fingers_nb);
2842 sc->zmax = imax(*z, sc->zmax);
2844 /* Do we have enough packets to consider this a movement? */
2845 if (synaction->queue_len < synaction->window_min)
2848 /* Is a scrolling action occuring? */
2849 if (!synaction->in_taphold && !synaction->in_vscroll) {
2851 * A scrolling action must not conflict with a tap
2852 * action. Here are the conditions to consider a
2854 * - the action in a configurable area
2855 * - one of the following:
2856 * . the distance between the last packet and the
2857 * first should be above a configurable minimum
2860 dxp = abs(synaction->queue[synaction->queue_cursor].x -
2861 synaction->start_x);
2862 dyp = abs(synaction->queue[synaction->queue_cursor].y -
2863 synaction->start_y);
2865 if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, >) ||
2866 dxp >= sc->syninfo.vscroll_min_delta ||
2867 dyp >= sc->syninfo.vscroll_min_delta) {
2868 /* Check for horizontal scrolling. */
2869 if ((vscroll_hor_area > 0 &&
2870 synaction->start_y <= vscroll_hor_area) ||
2871 (vscroll_hor_area < 0 &&
2872 synaction->start_y >=
2873 6143 + vscroll_hor_area))
2874 synaction->in_vscroll += 2;
2876 /* Check for vertical scrolling. */
2877 if ((vscroll_ver_area > 0 &&
2878 synaction->start_x <= vscroll_ver_area) ||
2879 (vscroll_ver_area < 0 &&
2880 synaction->start_x >=
2881 6143 + vscroll_ver_area))
2882 synaction->in_vscroll += 1;
2884 /* Avoid conflicts if area overlaps. */
2885 if (synaction->in_vscroll == 3)
2886 synaction->in_vscroll =
2887 (dxp > dyp) ? 2 : 1;
2890 "synaptics: virtual scrolling: %s "
2891 "(direction=%d, dxp=%d, dyp=%d)\n",
2892 synaction->in_vscroll ? "YES" : "NO",
2893 synaction->in_vscroll, dxp, dyp));
2896 weight_prev_x = weight_prev_y = weight_previous;
2897 div_max_x = div_max_y = div_max;
2899 if (synaction->in_vscroll) {
2900 /* Dividers are different with virtual scrolling. */
2901 div_min = sc->syninfo.vscroll_div_min;
2902 div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
2905 * There's a lot of noise in coordinates when
2906 * the finger is on the touchpad's borders. When
2907 * using this area, we apply a special weight and
2910 if (x0 <= na_left || x0 >= 6143 - na_right) {
2911 weight_prev_x = sc->syninfo.weight_previous_na;
2912 div_max_x = sc->syninfo.div_max_na;
2915 if (y0 <= na_bottom || y0 >= 6143 - na_top) {
2916 weight_prev_y = sc->syninfo.weight_previous_na;
2917 div_max_y = sc->syninfo.div_max_na;
2922 * Calculate weights for the average operands and
2923 * the divisor. Both depend on the distance between
2924 * the current packet and a previous one (based on the
2927 window = imin(synaction->queue_len, window_max);
2928 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
2929 dxp = abs(x0 - synaction->queue[peer].x) + 1;
2930 dyp = abs(y0 - synaction->queue[peer].y) + 1;
2931 len = (dxp * dxp) + (dyp * dyp);
2932 weight_prev_x = imin(weight_prev_x,
2933 weight_len_squared * weight_prev_x / len);
2934 weight_prev_y = imin(weight_prev_y,
2935 weight_len_squared * weight_prev_y / len);
2937 len = (dxp + dyp) / 2;
2938 div_x = div_len * div_max_x / len;
2939 div_x = imin(div_max_x, div_x);
2940 div_x = imax(div_min, div_x);
2941 div_y = div_len * div_max_y / len;
2942 div_y = imin(div_max_y, div_y);
2943 div_y = imax(div_min, div_y);
2946 "synaptics: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
2947 peer, len, weight_prev_x, weight_prev_y, div_x, div_y));
2949 /* Compute averages. */
2951 (weight_current * dx * multiplicator +
2952 weight_prev_x * synaction->avg_dx) /
2953 (weight_current + weight_prev_x);
2956 (weight_current * dy * multiplicator +
2957 weight_prev_y * synaction->avg_dy) /
2958 (weight_current + weight_prev_y);
2961 "synaptics: avg_dx~=%d, avg_dy~=%d\n",
2962 synaction->avg_dx / multiplicator,
2963 synaction->avg_dy / multiplicator));
2965 /* Use these averages to calculate x & y. */
2966 synaction->squelch_x += synaction->avg_dx;
2967 *x = synaction->squelch_x / (div_x * multiplicator);
2968 synaction->squelch_x = synaction->squelch_x %
2969 (div_x * multiplicator);
2971 synaction->squelch_y += synaction->avg_dy;
2972 *y = synaction->squelch_y / (div_y * multiplicator);
2973 synaction->squelch_y = synaction->squelch_y %
2974 (div_y * multiplicator);
2976 if (synaction->in_vscroll) {
2977 switch(synaction->in_vscroll) {
2978 case 1: /* Vertical scrolling. */
2980 ms->button |= (*y > 0) ?
2984 case 2: /* Horizontal scrolling. */
2986 ms->button |= (*x > 0) ?
2992 /* The pointer is not moved. */
2995 VLOG(3, (LOG_DEBUG, "synaptics: [%d, %d] -> [%d, %d]\n",
2998 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3000 * An action is currently taking place but the pressure
3001 * dropped under the minimum, putting an end to it.
3003 synapticsaction_t *synaction;
3004 int taphold_timeout, dx, dy, tap_max_delta;
3006 synaction = &(sc->synaction);
3007 dx = abs(synaction->queue[synaction->queue_cursor].x -
3008 synaction->start_x);
3009 dy = abs(synaction->queue[synaction->queue_cursor].y -
3010 synaction->start_y);
3012 /* Max delta is disabled for multi-fingers tap. */
3013 if (synaction->fingers_nb > 1)
3014 tap_max_delta = imax(dx, dy);
3016 tap_max_delta = sc->syninfo.tap_max_delta;
3018 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3020 /* Check for tap. */
3022 "synaptics: zmax=%d, dx=%d, dy=%d, "
3023 "delta=%d, fingers=%d, queue=%d\n",
3024 sc->zmax, dx, dy, tap_max_delta, synaction->fingers_nb,
3025 synaction->queue_len));
3026 if (!synaction->in_vscroll && sc->zmax >= tap_threshold &&
3027 timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=) &&
3028 dx <= tap_max_delta && dy <= tap_max_delta &&
3029 synaction->queue_len >= sc->syninfo.tap_min_queue) {
3032 * - the maximum pressure went over tap_threshold
3033 * - the action ended before tap_timeout
3035 * To handle tap-hold, we must delay any button push to
3038 if (synaction->in_taphold) {
3040 * This is the second and last tap of a
3041 * double tap action, not a tap-hold.
3043 synaction->in_taphold = 0;
3046 * For double-tap to work:
3047 * - no button press is emitted (to
3048 * simulate a button release)
3049 * - PSM_FLAGS_FINGERDOWN is set to
3050 * force the next packet to emit a
3054 "synaptics: button RELEASE: %d\n",
3055 synaction->tap_button));
3056 sc->flags |= PSM_FLAGS_FINGERDOWN;
3059 * This is the first tap: we set the
3060 * tap-hold state and notify the button
3063 synaction->in_taphold = 1;
3064 taphold_timeout = sc->syninfo.taphold_timeout;
3065 sc->taptimeout.tv_sec = taphold_timeout /
3067 sc->taptimeout.tv_usec = taphold_timeout %
3069 timevaladd(&sc->taptimeout, &sc->lastsoftintr);
3071 switch (synaction->fingers_nb) {
3073 synaction->tap_button =
3077 synaction->tap_button =
3081 synaction->tap_button =
3085 "synaptics: button PRESS: %d\n",
3086 synaction->tap_button));
3087 ms->button |= synaction->tap_button;
3091 * Not enough pressure or timeout: reset
3094 if (synaction->in_taphold) {
3096 "synaptics: button RELEASE: %d\n",
3097 synaction->tap_button));
3098 synaction->in_taphold = 0;
3101 "synaptics: not a tap-hold\n"));
3104 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) &&
3105 sc->synaction.in_taphold) {
3107 * For a tap-hold to work, the button must remain down at
3108 * least until timeout (where the in_taphold flags will be
3109 * cleared) or during the next action.
3111 if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) {
3112 ms->button |= sc->synaction.tap_button;
3115 "synaptics: button RELEASE: %d\n",
3116 sc->synaction.tap_button));
3117 sc->synaction.in_taphold = 0;
3123 * Use the extra buttons as a scrollwheel
3125 * XXX X.Org uses the Z axis for vertical wheel only,
3126 * whereas moused(8) understands special values to differ
3127 * vertical and horizontal wheels.
3129 * xf86-input-mouse needs therefore a small patch to
3130 * understand these special values. Without it, the
3131 * horizontal wheel acts as a vertical wheel in X.Org.
3133 * That's why the horizontal wheel is disabled by
3136 if (ms->button & MOUSE_BUTTON4DOWN) {
3138 ms->button &= ~MOUSE_BUTTON4DOWN;
3139 } else if (ms->button & MOUSE_BUTTON5DOWN) {
3141 ms->button &= ~MOUSE_BUTTON5DOWN;
3142 } else if (ms->button & MOUSE_BUTTON6DOWN) {
3144 ms->button &= ~MOUSE_BUTTON6DOWN;
3145 } else if (ms->button & MOUSE_BUTTON7DOWN) {
3147 ms->button &= ~MOUSE_BUTTON7DOWN;
3155 proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3156 int *x, int *y, int *z)
3158 static int butmap_versapad[8] = {
3164 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3166 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
3170 /* VersaPad PS/2 absolute mode message format
3172 * [packet1] 7 6 5 4 3 2 1 0(LSB)
3173 * ipacket[0]: 1 1 0 A 1 L T R
3174 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
3175 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
3176 * ipacket[3]: 1 1 1 A 1 L T R
3177 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
3178 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
3181 * R: right physical mouse button (1=on)
3182 * T: touch pad virtual button (1=tapping)
3183 * L: left physical mouse button (1=on)
3184 * A: position data is valid (1=valid)
3185 * H: horizontal data (12bit signed integer. H11 is sign bit.)
3186 * V: vertical data (12bit signed integer. V11 is sign bit.)
3189 * Tapping is mapped to MOUSE_BUTTON4.
3193 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
3194 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
3195 if (c & MOUSE_PS2VERSA_IN_USE) {
3196 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
3197 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
3202 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3205 if (*x < 0) /* XXX */
3214 sc->flags |= PSM_FLAGS_FINGERDOWN;
3218 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3222 psmsoftintr(void *arg)
3225 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
3226 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
3228 static int butmap[8] = {
3232 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3234 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
3235 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
3236 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
3238 register struct psm_softc *sc = arg;
3241 int x, y, z, c, l, s;
3243 getmicrouptime(&sc->lastsoftintr);
3248 pb = &sc->pqueue[sc->pqueue_start];
3250 if (sc->mode.level == PSM_LEVEL_NATIVE)
3255 * A kludge for Kensington device!
3256 * The MSB of the horizontal count appears to be stored in
3259 if (sc->hw.model == MOUSE_MODEL_THINK)
3260 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
3262 /* ignore the overflow bits... */
3263 x = (c & MOUSE_PS2_XNEG) ?
3264 pb->ipacket[1] - 256 : pb->ipacket[1];
3265 y = (c & MOUSE_PS2_YNEG) ?
3266 pb->ipacket[2] - 256 : pb->ipacket[2];
3268 ms.obutton = sc->button; /* previous button state */
3269 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
3270 /* `tapping' action */
3271 if (sc->config & PSM_CONFIG_FORCETAP)
3272 ms.button |= ((c & MOUSE_PS2_TAP)) ?
3273 0 : MOUSE_BUTTON4DOWN;
3275 switch (sc->hw.model) {
3277 case MOUSE_MODEL_EXPLORER:
3279 * b7 b6 b5 b4 b3 b2 b1 b0
3280 * byte 1: oy ox sy sx 1 M R L
3281 * byte 2: x x x x x x x x
3282 * byte 3: y y y y y y y y
3283 * byte 4: * * S2 S1 s d2 d1 d0
3285 * L, M, R, S1, S2: left, middle, right and side buttons
3286 * s: wheel data sign bit
3289 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
3290 (pb->ipacket[3] & 0x0f) - 16 :
3291 (pb->ipacket[3] & 0x0f);
3293 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
3294 MOUSE_BUTTON4DOWN : 0;
3296 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
3297 MOUSE_BUTTON5DOWN : 0;
3300 case MOUSE_MODEL_INTELLI:
3301 case MOUSE_MODEL_NET:
3302 /* wheel data is in the fourth byte */
3303 z = (char)pb->ipacket[3];
3305 * XXX some mice may send 7 when there is no Z movement? */
3306 if ((z >= 7) || (z <= -7))
3308 /* some compatible mice have additional buttons */
3309 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
3310 MOUSE_BUTTON4DOWN : 0;
3311 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
3312 MOUSE_BUTTON5DOWN : 0;
3315 case MOUSE_MODEL_MOUSEMANPLUS:
3316 proc_mmanplus(sc, pb, &ms, &x, &y, &z);
3319 case MOUSE_MODEL_GLIDEPOINT:
3320 /* `tapping' action */
3321 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
3325 case MOUSE_MODEL_NETSCROLL:
3327 * three addtional bytes encode buttons and
3330 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
3331 MOUSE_BUTTON4DOWN : 0;
3332 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
3333 MOUSE_BUTTON5DOWN : 0;
3334 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
3335 pb->ipacket[4] - 256 : pb->ipacket[4];
3338 case MOUSE_MODEL_THINK:
3339 /* the fourth button state in the first byte */
3340 ms.button |= (c & MOUSE_PS2_TAP) ?
3341 MOUSE_BUTTON4DOWN : 0;
3344 case MOUSE_MODEL_VERSAPAD:
3345 proc_versapad(sc, pb, &ms, &x, &y, &z);
3346 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
3347 ((y < 0) ? MOUSE_PS2_YNEG : 0);
3350 case MOUSE_MODEL_4D:
3352 * b7 b6 b5 b4 b3 b2 b1 b0
3353 * byte 1: s2 d2 s1 d1 1 M R L
3354 * byte 2: sx x x x x x x x
3355 * byte 3: sy y y y y y y y
3357 * s1: wheel 1 direction
3359 * s2: wheel 2 direction
3362 x = (pb->ipacket[1] & 0x80) ?
3363 pb->ipacket[1] - 256 : pb->ipacket[1];
3364 y = (pb->ipacket[2] & 0x80) ?
3365 pb->ipacket[2] - 256 : pb->ipacket[2];
3366 switch (c & MOUSE_4D_WHEELBITS) {
3373 case 0x40: /* XXX 2nd wheel turning right */
3376 case 0xc0: /* XXX 2nd wheel turning left */
3382 case MOUSE_MODEL_4DPLUS:
3383 if ((x < 16 - 256) && (y < 16 - 256)) {
3385 * b7 b6 b5 b4 b3 b2 b1 b0
3386 * byte 1: 0 0 1 1 1 M R L
3387 * byte 2: 0 0 0 0 1 0 0 0
3388 * byte 3: 0 0 0 0 S s d1 d0
3390 * L, M, R, S: left, middle, right,
3392 * s: wheel data sign bit
3396 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
3397 ms.button |= MOUSE_BUTTON4DOWN;
3398 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
3399 ((pb->ipacket[2] & 0x07) - 8) :
3400 (pb->ipacket[2] & 0x07) ;
3402 /* preserve previous button states */
3403 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
3407 case MOUSE_MODEL_SYNAPTICS:
3408 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0)
3412 case MOUSE_MODEL_GENERIC:
3418 if (sc->mode.accelfactor >= 1) {
3420 x = x * x / sc->mode.accelfactor;
3423 if (c & MOUSE_PS2_XNEG)
3427 y = y * y / sc->mode.accelfactor;
3430 if (c & MOUSE_PS2_YNEG)
3438 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
3439 (ms.obutton ^ ms.button);
3441 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
3443 sc->status.flags |= ms.flags;
3444 sc->status.dx += ms.dx;
3445 sc->status.dy += ms.dy;
3446 sc->status.dz += ms.dz;
3447 sc->status.button = ms.button;
3448 sc->button = ms.button;
3451 sc->watchdog = FALSE;
3454 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
3455 l = imin(pb->inputbytes,
3456 sizeof(sc->queue.buf) - sc->queue.tail);
3457 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
3458 if (pb->inputbytes > l)
3459 bcopy(&pb->ipacket[l], &sc->queue.buf[0],
3460 pb->inputbytes - l);
3461 sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
3462 sizeof(sc->queue.buf);
3463 sc->queue.count += pb->inputbytes;
3468 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
3469 sc->pqueue_start = 0;
3470 } while (sc->pqueue_start != sc->pqueue_end);
3472 if (sc->state & PSM_ASLP) {
3473 sc->state &= ~PSM_ASLP;
3476 selwakeuppri(&sc->rsel, PZERO);
3477 if (sc->async != NULL) {
3478 pgsigio(&sc->async, SIGIO, 0);
3480 sc->state &= ~PSM_SOFTARMED;
3485 psmpoll(struct cdev *dev, int events, struct thread *td)
3487 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
3491 /* Return true if a mouse event available */
3493 if (events & (POLLIN | POLLRDNORM)) {
3494 if (sc->queue.count > 0)
3495 revents |= events & (POLLIN | POLLRDNORM);
3497 selrecord(td, &sc->rsel);
3504 /* vendor/model specific routines */
3506 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
3508 if (set_mouse_resolution(kbdc, res) != res)
3510 if (set_mouse_scaling(kbdc, scale) &&
3511 set_mouse_scaling(kbdc, scale) &&
3512 set_mouse_scaling(kbdc, scale) &&
3513 (get_mouse_status(kbdc, status, 0, 3) >= 3))
3519 mouse_ext_command(KBDC kbdc, int command)
3523 c = (command >> 6) & 0x03;
3524 if (set_mouse_resolution(kbdc, c) != c)
3526 c = (command >> 4) & 0x03;
3527 if (set_mouse_resolution(kbdc, c) != c)
3529 c = (command >> 2) & 0x03;
3530 if (set_mouse_resolution(kbdc, c) != c)
3532 c = (command >> 0) & 0x03;
3533 if (set_mouse_resolution(kbdc, c) != c)
3539 /* Logitech MouseMan Cordless II */
3541 enable_lcordless(struct psm_softc *sc)
3546 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
3548 if (status[1] == PSMD_RES_HIGH)
3550 ch = (status[0] & 0x07) - 1; /* channel # */
3551 if ((ch <= 0) || (ch > 4))
3554 * status[1]: always one?
3555 * status[2]: battery status? (0-100)
3561 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
3563 enable_groller(struct psm_softc *sc)
3568 * The special sequence to enable the fourth button and the
3569 * roller. Immediately after this sequence check status bytes.
3570 * if the mouse is NetScroll, the second and the third bytes are
3575 * If the mouse is an ordinary PS/2 mouse, the status bytes should
3576 * look like the following.
3578 * byte 1 bit 7 always 0
3579 * bit 6 stream mode (0)
3580 * bit 5 disabled (0)
3581 * bit 4 1:1 scaling (0)
3583 * bit 0-2 button status
3584 * byte 2 resolution (PSMD_RES_HIGH)
3585 * byte 3 report rate (?)
3588 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
3590 if ((status[1] != '3') || (status[2] != 'D'))
3592 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
3597 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
3599 enable_gmouse(struct psm_softc *sc)
3604 * The special sequence to enable the middle, "rubber" button.
3605 * Immediately after this sequence check status bytes.
3606 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
3607 * the second and the third bytes are '3' and 'U'.
3608 * NOTE: NetMouse reports that it has three buttons although it has
3609 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
3610 * say they have three buttons too and they do have a button on the
3613 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
3615 if ((status[1] != '3') || (status[2] != 'U'))
3620 /* ALPS GlidePoint */
3622 enable_aglide(struct psm_softc *sc)
3627 * The special sequence to obtain ALPS GlidePoint specific
3628 * information. Immediately after this sequence, status bytes will
3629 * contain something interesting.
3630 * NOTE: ALPS produces several models of GlidePoint. Some of those
3631 * do not respond to this sequence, thus, cannot be detected this way.
3633 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
3635 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
3637 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
3642 /* Kensington ThinkingMouse/Trackball */
3644 enable_kmouse(struct psm_softc *sc)
3646 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
3647 KBDC kbdc = sc->kbdc;
3653 id1 = get_aux_id(kbdc);
3654 if (set_mouse_sampling_rate(kbdc, 10) != 10)
3657 * The device is now in the native mode? It returns a different
3660 id2 = get_aux_id(kbdc);
3661 if ((id1 == id2) || (id2 != 2))
3664 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
3667 /* at this point, resolution is LOW, sampling rate is 10/sec */
3668 if (get_mouse_status(kbdc, status, 0, 3) < 3)
3673 * The special sequence to enable the third and fourth buttons.
3674 * Otherwise they behave like the first and second buttons.
3676 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3677 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3681 * At this point, the device is using default resolution and
3682 * sampling rate for the native mode.
3684 if (get_mouse_status(kbdc, status, 0, 3) < 3)
3686 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
3689 /* the device appears be enabled by this sequence, diable it for now */
3690 disable_aux_dev(kbdc);
3691 empty_aux_buffer(kbdc, 5);
3696 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
3698 enable_mmanplus(struct psm_softc *sc)
3700 KBDC kbdc = sc->kbdc;
3703 /* the special sequence to enable the fourth button and the roller. */
3705 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
3706 * must be called exactly three times since the last RESET command
3707 * before this sequence. XXX
3709 if (!set_mouse_scaling(kbdc, 1))
3711 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
3713 if (get_mouse_status(kbdc, data, 1, 3) < 3)
3717 * PS2++ protocl, packet type 0
3719 * b7 b6 b5 b4 b3 b2 b1 b0
3720 * byte 1: * 1 p3 p2 1 * * *
3721 * byte 2: 1 1 p1 p0 m1 m0 1 0
3722 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
3724 * p3-p0: packet type: 0
3725 * m7-m0: model ID: MouseMan+:0x50,
3727 * ScrollPoint:0x58...
3729 /* check constant bits */
3730 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
3732 if ((data[1] & 0xc3) != 0xc2)
3734 /* check d3-d0 in byte 2 */
3735 if (!MOUSE_PS2PLUS_CHECKBITS(data))
3738 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
3741 sc->hw.hwid &= 0x00ff;
3742 sc->hw.hwid |= data[2] << 8; /* save model ID */
3745 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
3746 * the wheel and the fourth button events are encoded in the
3747 * special data packet. The mouse may be put in the IntelliMouse mode
3748 * if it is initialized by the IntelliMouse's method.
3753 /* MS IntelliMouse Explorer */
3755 enable_msexplorer(struct psm_softc *sc)
3757 static u_char rate0[] = { 200, 100, 80, };
3758 static u_char rate1[] = { 200, 200, 80, };
3759 KBDC kbdc = sc->kbdc;
3764 * This is needed for at least A4Tech X-7xx mice - they do not go
3765 * straight to Explorer mode, but need to be set to Intelli mode
3768 enable_msintelli(sc);
3770 /* the special sequence to enable the extra buttons and the roller. */
3771 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i)
3772 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
3774 /* the device will give the genuine ID only after the above sequence */
3775 id = get_aux_id(kbdc);
3776 if (id != PSM_EXPLORER_ID)
3780 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
3783 * XXX: this is a kludge to fool some KVM switch products
3784 * which think they are clever enough to know the 4-byte IntelliMouse
3785 * protocol, and assume any other protocols use 3-byte packets.
3786 * They don't convey 4-byte data packets from the IntelliMouse Explorer
3787 * correctly to the host computer because of this!
3788 * The following sequence is actually IntelliMouse's "wake up"
3789 * sequence; it will make the KVM think the mouse is IntelliMouse
3790 * when it is in fact IntelliMouse Explorer.
3792 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i)
3793 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
3795 id = get_aux_id(kbdc);
3800 /* MS IntelliMouse */
3802 enable_msintelli(struct psm_softc *sc)
3805 * Logitech MouseMan+ and FirstMouse+ will also respond to this
3806 * probe routine and act like IntelliMouse.
3809 static u_char rate[] = { 200, 100, 80, };
3810 KBDC kbdc = sc->kbdc;
3814 /* the special sequence to enable the third button and the roller. */
3815 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3816 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3818 /* the device will give the genuine ID only after the above sequence */
3819 id = get_aux_id(kbdc);
3820 if (id != PSM_INTELLI_ID)
3829 /* A4 Tech 4D Mouse */
3831 enable_4dmouse(struct psm_softc *sc)
3834 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
3837 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
3838 KBDC kbdc = sc->kbdc;
3842 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3843 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3845 id = get_aux_id(kbdc);
3847 * WinEasy 4D, 4 Way Scroll 4D: 6
3848 * Cable-Free 4D: 8 (4DPLUS)
3849 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
3851 if (id != PSM_4DMOUSE_ID)
3855 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
3860 /* A4 Tech 4D+ Mouse */
3862 enable_4dplus(struct psm_softc *sc)
3865 * Newer wheel mice from A4 Tech seem to use this protocol.
3866 * Older models are recognized as either 4D Mouse or IntelliMouse.
3868 KBDC kbdc = sc->kbdc;
3872 * enable_4dmouse() already issued the following ID sequence...
3873 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
3876 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
3877 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3881 id = get_aux_id(kbdc);
3886 case PSM_4DPLUS_RFSW35_ID:
3898 /* Synaptics Touchpad */
3900 synaptics_sysctl(SYSCTL_HANDLER_ARGS)
3904 /* Read the current value. */
3905 arg = *(int *)oidp->oid_arg1;
3906 error = sysctl_handle_int(oidp, &arg, 0, req);
3909 if (error || !req->newptr)
3913 * Check that the new value is in the concerned node's range
3916 switch (oidp->oid_arg2) {
3917 case SYNAPTICS_SYSCTL_MIN_PRESSURE:
3918 case SYNAPTICS_SYSCTL_MAX_PRESSURE:
3919 if (arg < 0 || arg > 255)
3922 case SYNAPTICS_SYSCTL_MAX_WIDTH:
3923 if (arg < 4 || arg > 15)
3926 case SYNAPTICS_SYSCTL_MARGIN_TOP:
3927 case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
3928 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
3929 case SYNAPTICS_SYSCTL_MARGIN_LEFT:
3930 case SYNAPTICS_SYSCTL_NA_TOP:
3931 case SYNAPTICS_SYSCTL_NA_RIGHT:
3932 case SYNAPTICS_SYSCTL_NA_BOTTOM:
3933 case SYNAPTICS_SYSCTL_NA_LEFT:
3934 if (arg < 0 || arg > 6143)
3937 case SYNAPTICS_SYSCTL_WINDOW_MIN:
3938 case SYNAPTICS_SYSCTL_WINDOW_MAX:
3939 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
3940 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
3943 case SYNAPTICS_SYSCTL_MULTIPLICATOR:
3944 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
3945 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
3946 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
3947 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
3948 case SYNAPTICS_SYSCTL_DIV_MIN:
3949 case SYNAPTICS_SYSCTL_DIV_MAX:
3950 case SYNAPTICS_SYSCTL_DIV_MAX_NA:
3951 case SYNAPTICS_SYSCTL_DIV_LEN:
3952 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
3953 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
3957 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
3958 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
3959 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
3963 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
3964 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
3965 if (arg < -6143 || arg > 6143)
3973 *(int *)oidp->oid_arg1 = arg;
3979 synaptics_sysctl_create_tree(struct psm_softc *sc)
3982 if (sc->syninfo.sysctl_tree != NULL)
3985 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
3986 sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
3987 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
3988 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "synaptics", CTLFLAG_RD,
3989 0, "Synaptics TouchPad");
3991 /* hw.psm.synaptics.directional_scrolls. */
3992 sc->syninfo.directional_scrolls = 1;
3993 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
3994 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
3995 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
3996 &sc->syninfo.directional_scrolls, 0,
3997 "Enable hardware scrolling pad (if non-zero) or register it as "
3998 "a middle-click (if 0)");
4000 /* hw.psm.synaptics.min_pressure. */
4001 sc->syninfo.min_pressure = 16;
4002 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4003 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4004 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4005 &sc->syninfo.min_pressure, SYNAPTICS_SYSCTL_MIN_PRESSURE,
4006 synaptics_sysctl, "I",
4007 "Minimum pressure required to start an action");
4009 /* hw.psm.synaptics.max_pressure. */
4010 sc->syninfo.max_pressure = 220;
4011 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4012 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4013 "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4014 &sc->syninfo.max_pressure, SYNAPTICS_SYSCTL_MAX_PRESSURE,
4015 synaptics_sysctl, "I",
4016 "Maximum pressure to detect palm");
4018 /* hw.psm.synaptics.max_width. */
4019 sc->syninfo.max_width = 10;
4020 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4021 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4022 "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4023 &sc->syninfo.max_width, SYNAPTICS_SYSCTL_MAX_WIDTH,
4024 synaptics_sysctl, "I",
4025 "Maximum finger width to detect palm");
4027 /* hw.psm.synaptics.top_margin. */
4028 sc->syninfo.margin_top = 200;
4029 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4030 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4031 "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4032 &sc->syninfo.margin_top, SYNAPTICS_SYSCTL_MARGIN_TOP,
4033 synaptics_sysctl, "I",
4036 /* hw.psm.synaptics.right_margin. */
4037 sc->syninfo.margin_right = 200;
4038 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4039 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4040 "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4041 &sc->syninfo.margin_right, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
4042 synaptics_sysctl, "I",
4045 /* hw.psm.synaptics.bottom_margin. */
4046 sc->syninfo.margin_bottom = 200;
4047 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4048 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4049 "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4050 &sc->syninfo.margin_bottom, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
4051 synaptics_sysctl, "I",
4054 /* hw.psm.synaptics.left_margin. */
4055 sc->syninfo.margin_left = 200;
4056 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4057 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4058 "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4059 &sc->syninfo.margin_left, SYNAPTICS_SYSCTL_MARGIN_LEFT,
4060 synaptics_sysctl, "I",
4063 /* hw.psm.synaptics.na_top. */
4064 sc->syninfo.na_top = 1783;
4065 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4066 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4067 "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4068 &sc->syninfo.na_top, SYNAPTICS_SYSCTL_NA_TOP,
4069 synaptics_sysctl, "I",
4070 "Top noisy area, where weight_previous_na is used instead "
4071 "of weight_previous");
4073 /* hw.psm.synaptics.na_right. */
4074 sc->syninfo.na_right = 563;
4075 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4076 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4077 "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4078 &sc->syninfo.na_right, SYNAPTICS_SYSCTL_NA_RIGHT,
4079 synaptics_sysctl, "I",
4080 "Right noisy area, where weight_previous_na is used instead "
4081 "of weight_previous");
4083 /* hw.psm.synaptics.na_bottom. */
4084 sc->syninfo.na_bottom = 1408;
4085 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4086 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4087 "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4088 &sc->syninfo.na_bottom, SYNAPTICS_SYSCTL_NA_BOTTOM,
4089 synaptics_sysctl, "I",
4090 "Bottom noisy area, where weight_previous_na is used instead "
4091 "of weight_previous");
4093 /* hw.psm.synaptics.na_left. */
4094 sc->syninfo.na_left = 1600;
4095 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4096 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4097 "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4098 &sc->syninfo.na_left, SYNAPTICS_SYSCTL_NA_LEFT,
4099 synaptics_sysctl, "I",
4100 "Left noisy area, where weight_previous_na is used instead "
4101 "of weight_previous");
4103 /* hw.psm.synaptics.window_min. */
4104 sc->syninfo.window_min = 4;
4105 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4106 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4107 "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4108 &sc->syninfo.window_min, SYNAPTICS_SYSCTL_WINDOW_MIN,
4109 synaptics_sysctl, "I",
4110 "Minimum window size to start an action");
4112 /* hw.psm.synaptics.window_max. */
4113 sc->syninfo.window_max = 10;
4114 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4115 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4116 "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4117 &sc->syninfo.window_max, SYNAPTICS_SYSCTL_WINDOW_MAX,
4118 synaptics_sysctl, "I",
4119 "Maximum window size");
4121 /* hw.psm.synaptics.multiplicator. */
4122 sc->syninfo.multiplicator = 10000;
4123 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4124 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4125 "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4126 &sc->syninfo.multiplicator, SYNAPTICS_SYSCTL_MULTIPLICATOR,
4127 synaptics_sysctl, "I",
4128 "Multiplicator to increase precision in averages and divisions");
4130 /* hw.psm.synaptics.weight_current. */
4131 sc->syninfo.weight_current = 3;
4132 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4133 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4134 "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4135 &sc->syninfo.weight_current, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
4136 synaptics_sysctl, "I",
4137 "Weight of the current movement in the new average");
4139 /* hw.psm.synaptics.weight_previous. */
4140 sc->syninfo.weight_previous = 6;
4141 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4142 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4143 "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4144 &sc->syninfo.weight_previous, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
4145 synaptics_sysctl, "I",
4146 "Weight of the previous average");
4148 /* hw.psm.synaptics.weight_previous_na. */
4149 sc->syninfo.weight_previous_na = 20;
4150 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4151 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4152 "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4153 &sc->syninfo.weight_previous_na,
4154 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
4155 synaptics_sysctl, "I",
4156 "Weight of the previous average (inside the noisy area)");
4158 /* hw.psm.synaptics.weight_len_squared. */
4159 sc->syninfo.weight_len_squared = 2000;
4160 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4161 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4162 "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4163 &sc->syninfo.weight_len_squared,
4164 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
4165 synaptics_sysctl, "I",
4166 "Length (squared) of segments where weight_previous "
4167 "starts to decrease");
4169 /* hw.psm.synaptics.div_min. */
4170 sc->syninfo.div_min = 9;
4171 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4172 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4173 "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4174 &sc->syninfo.div_min, SYNAPTICS_SYSCTL_DIV_MIN,
4175 synaptics_sysctl, "I",
4176 "Divisor for fast movements");
4178 /* hw.psm.synaptics.div_max. */
4179 sc->syninfo.div_max = 17;
4180 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4181 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4182 "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4183 &sc->syninfo.div_max, SYNAPTICS_SYSCTL_DIV_MAX,
4184 synaptics_sysctl, "I",
4185 "Divisor for slow movements");
4187 /* hw.psm.synaptics.div_max_na. */
4188 sc->syninfo.div_max_na = 30;
4189 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4190 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4191 "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4192 &sc->syninfo.div_max_na, SYNAPTICS_SYSCTL_DIV_MAX_NA,
4193 synaptics_sysctl, "I",
4194 "Divisor with slow movements (inside the noisy area)");
4196 /* hw.psm.synaptics.div_len. */
4197 sc->syninfo.div_len = 100;
4198 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4199 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4200 "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4201 &sc->syninfo.div_len, SYNAPTICS_SYSCTL_DIV_LEN,
4202 synaptics_sysctl, "I",
4203 "Length of segments where div_max starts to decrease");
4205 /* hw.psm.synaptics.tap_max_delta. */
4206 sc->syninfo.tap_max_delta = 80;
4207 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4208 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4209 "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4210 &sc->syninfo.tap_max_delta, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
4211 synaptics_sysctl, "I",
4212 "Length of segments above which a tap is ignored");
4214 /* hw.psm.synaptics.tap_min_queue. */
4215 sc->syninfo.tap_min_queue = 2;
4216 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4217 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4218 "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4219 &sc->syninfo.tap_min_queue, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
4220 synaptics_sysctl, "I",
4221 "Number of packets required to consider a tap");
4223 /* hw.psm.synaptics.taphold_timeout. */
4224 sc->synaction.in_taphold = 0;
4225 sc->syninfo.taphold_timeout = tap_timeout;
4226 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4227 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4228 "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4229 &sc->syninfo.taphold_timeout, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
4230 synaptics_sysctl, "I",
4231 "Maximum elapsed time between two taps to consider a tap-hold "
4234 /* hw.psm.synaptics.vscroll_hor_area. */
4235 sc->syninfo.vscroll_hor_area = 0; /* 1300 */
4236 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4237 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4238 "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4239 &sc->syninfo.vscroll_hor_area, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
4240 synaptics_sysctl, "I",
4241 "Area reserved for horizontal virtual scrolling");
4243 /* hw.psm.synaptics.vscroll_ver_area. */
4244 sc->syninfo.vscroll_ver_area = -600;
4245 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4246 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4247 "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4248 &sc->syninfo.vscroll_ver_area, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
4249 synaptics_sysctl, "I",
4250 "Area reserved for vertical virtual scrolling");
4252 /* hw.psm.synaptics.vscroll_min_delta. */
4253 sc->syninfo.vscroll_min_delta = 50;
4254 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4255 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4256 "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4257 &sc->syninfo.vscroll_min_delta,
4258 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
4259 synaptics_sysctl, "I",
4260 "Minimum movement to consider virtual scrolling");
4262 /* hw.psm.synaptics.vscroll_div_min. */
4263 sc->syninfo.vscroll_div_min = 100;
4264 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4265 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4266 "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4267 &sc->syninfo.vscroll_div_min, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
4268 synaptics_sysctl, "I",
4269 "Divisor for fast scrolling");
4271 /* hw.psm.synaptics.vscroll_div_min. */
4272 sc->syninfo.vscroll_div_max = 150;
4273 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4274 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4275 "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4276 &sc->syninfo.vscroll_div_max, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
4277 synaptics_sysctl, "I",
4278 "Divisor for slow scrolling");
4282 enable_synaptics(struct psm_softc *sc)
4287 if (!synaptics_support)
4291 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
4296 * Just to be on the safe side: this avoids troubles with
4297 * following mouse_ext_command() when the previous command
4298 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
4299 * Synaptics Touchpad behaviour.
4301 set_mouse_scaling(kbdc, 1);
4303 /* Identify the Touchpad version. */
4304 if (mouse_ext_command(kbdc, 0) == 0)
4306 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4308 if (status[1] != 0x47)
4311 sc->synhw.infoMinor = status[0];
4312 sc->synhw.infoMajor = status[2] & 0x0f;
4315 printf("Synaptics Touchpad v%d.%d\n", sc->synhw.infoMajor,
4316 sc->synhw.infoMinor);
4318 if (sc->synhw.infoMajor < 4) {
4319 printf(" Unsupported (pre-v4) Touchpad detected\n");
4323 /* Get the Touchpad model information. */
4324 if (mouse_ext_command(kbdc, 3) == 0)
4326 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4328 if ((status[1] & 0x01) != 0) {
4329 printf(" Failed to read model information\n");
4333 sc->synhw.infoRot180 = (status[0] & 0x80) >> 7;
4334 sc->synhw.infoPortrait = (status[0] & 0x40) >> 6;
4335 sc->synhw.infoSensor = status[0] & 0x3f;
4336 sc->synhw.infoHardware = (status[1] & 0xfe) >> 1;
4337 sc->synhw.infoNewAbs = (status[2] & 0x80) >> 7;
4338 sc->synhw.capPen = (status[2] & 0x40) >> 6;
4339 sc->synhw.infoSimplC = (status[2] & 0x20) >> 5;
4340 sc->synhw.infoGeometry = status[2] & 0x0f;
4343 printf(" Model information:\n");
4344 printf(" infoRot180: %d\n", sc->synhw.infoRot180);
4345 printf(" infoPortrait: %d\n", sc->synhw.infoPortrait);
4346 printf(" infoSensor: %d\n", sc->synhw.infoSensor);
4347 printf(" infoHardware: %d\n", sc->synhw.infoHardware);
4348 printf(" infoNewAbs: %d\n", sc->synhw.infoNewAbs);
4349 printf(" capPen: %d\n", sc->synhw.capPen);
4350 printf(" infoSimplC: %d\n", sc->synhw.infoSimplC);
4351 printf(" infoGeometry: %d\n", sc->synhw.infoGeometry);
4354 /* Read the extended capability bits. */
4355 if (mouse_ext_command(kbdc, 2) == 0)
4357 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4359 if (status[1] != 0x47) {
4360 printf(" Failed to read extended capability bits\n");
4364 /* Set the different capabilities when they exist. */
4365 if ((status[0] & 0x80) >> 7) {
4366 sc->synhw.capExtended = (status[0] & 0x80) >> 7;
4367 sc->synhw.capPassthrough = (status[2] & 0x80) >> 7;
4368 sc->synhw.capSleep = (status[2] & 0x10) >> 4;
4369 sc->synhw.capFourButtons = (status[2] & 0x08) >> 3;
4370 sc->synhw.capMultiFinger = (status[2] & 0x02) >> 1;
4371 sc->synhw.capPalmDetect = (status[2] & 0x01);
4374 printf(" Extended capabilities:\n");
4375 printf(" capExtended: %d\n", sc->synhw.capExtended);
4376 printf(" capPassthrough: %d\n",
4377 sc->synhw.capPassthrough);
4378 printf(" capSleep: %d\n", sc->synhw.capSleep);
4379 printf(" capFourButtons: %d\n",
4380 sc->synhw.capFourButtons);
4381 printf(" capMultiFinger: %d\n",
4382 sc->synhw.capMultiFinger);
4383 printf(" capPalmDetect: %d\n",
4384 sc->synhw.capPalmDetect);
4388 * If we have bits set in status[0] & 0x70, then we can load
4389 * more information about buttons using query 0x09.
4391 if (status[0] & 0x70) {
4392 if (mouse_ext_command(kbdc, 0x09) == 0)
4394 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4396 sc->hw.buttons = ((status[1] & 0xf0) >> 4) + 3;
4398 printf(" Additional Buttons: %d\n",
4402 sc->synhw.capExtended = 0;
4405 printf(" No extended capabilities\n");
4409 * Read the mode byte.
4411 * XXX: Note the Synaptics documentation also defines the first
4412 * byte of the response to this query to be a constant 0x3b, this
4413 * does not appear to be true for Touchpads with guest devices.
4415 if (mouse_ext_command(kbdc, 1) == 0)
4417 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4419 if (status[1] != 0x47) {
4420 printf(" Failed to read mode byte\n");
4424 /* Set the mode byte; request wmode where available. */
4425 if (sc->synhw.capExtended)
4426 mouse_ext_command(kbdc, 0xc1);
4428 mouse_ext_command(kbdc, 0xc0);
4430 /* "Commit" the Set Mode Byte command sent above. */
4431 set_mouse_sampling_rate(kbdc, 20);
4434 * Report the correct number of buttons
4436 * XXX: I'm not sure this is used anywhere.
4438 if (sc->synhw.capExtended && sc->synhw.capFourButtons)
4441 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n",
4444 /* Create sysctl tree. */
4445 synaptics_sysctl_create_tree(sc);
4448 * The touchpad will have to be reinitialized after
4451 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
4456 /* Interlink electronics VersaPad */
4458 enable_versapad(struct psm_softc *sc)
4460 KBDC kbdc = sc->kbdc;
4463 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
4464 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
4465 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4466 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4467 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4468 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4469 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
4471 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
4473 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4475 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
4477 return (TRUE); /* PS/2 absolute mode */
4481 * Return true if 'now' is earlier than (start + (secs.usecs)).
4482 * Now may be NULL and the function will fetch the current time from
4483 * getmicrouptime(), or a cached 'now' can be passed in.
4484 * All values should be numbers derived from getmicrouptime().
4487 timeelapsed(start, secs, usecs, now)
4488 const struct timeval *start, *now;
4491 struct timeval snow, tv;
4493 /* if there is no 'now' passed in, the get it as a convience. */
4495 getmicrouptime(&snow);
4501 timevaladd(&tv, start);
4502 return (timevalcmp(&tv, now, <));
4506 psmresume(device_t dev)
4508 struct psm_softc *sc = device_get_softc(dev);
4509 int unit = device_get_unit(dev);
4512 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
4514 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
4517 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
4519 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
4521 * Release the blocked process; it must be notified that
4522 * the device cannot be accessed anymore.
4524 sc->state &= ~PSM_ASLP;
4528 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
4533 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
4538 * This sucks up assignments from PNPBIOS and ACPI.
4542 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
4543 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
4544 * can be probed and attached only after the AT keyboard controller is
4545 * attached, we shall quietly reserve the IRQ resource for later use.
4546 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
4547 * copy the IRQ resource to the PS/2 mouse device instance hanging
4548 * under the keyboard controller, then probe and attach it.
4551 static devclass_t psmcpnp_devclass;
4553 static device_probe_t psmcpnp_probe;
4554 static device_attach_t psmcpnp_attach;
4556 static device_method_t psmcpnp_methods[] = {
4557 DEVMETHOD(device_probe, psmcpnp_probe),
4558 DEVMETHOD(device_attach, psmcpnp_attach),
4563 static driver_t psmcpnp_driver = {
4564 PSMCPNP_DRIVER_NAME,
4569 static struct isa_pnp_id psmcpnp_ids[] = {
4570 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
4571 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */
4572 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */
4573 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
4574 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
4575 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
4576 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */
4577 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
4578 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
4579 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
4580 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
4581 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
4582 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
4587 create_a_copy(device_t atkbdc, device_t me)
4592 /* find the PS/2 mouse device instance under the keyboard controller */
4593 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
4594 device_get_unit(atkbdc));
4597 if (device_get_state(psm) != DS_NOTPRESENT)
4600 /* move our resource to the found device */
4601 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
4602 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
4604 /* ...then probe and attach it */
4605 return (device_probe_and_attach(psm));
4609 psmcpnp_probe(device_t dev)
4611 struct resource *res;
4615 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
4619 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
4620 * to the PS/2 mouse device node. But, some buggy PnP BIOS
4621 * declares the PS/2 mouse device node without an IRQ resource!
4622 * If this happens, we shall refer to device hints.
4623 * If we still don't find it there, use a hardcoded value... XXX
4626 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
4628 if (resource_long_value(PSM_DRIVER_NAME,
4629 device_get_unit(dev),"irq", &irq) != 0)
4631 device_printf(dev, "irq resource info is missing; "
4632 "assuming irq %ld\n", irq);
4633 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
4635 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
4636 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
4642 return ((res == NULL) ? ENXIO : 0);
4646 psmcpnp_attach(device_t dev)
4651 /* find the keyboard controller, which may be on acpi* or isa* bus */
4652 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
4653 device_get_unit(dev));
4654 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
4655 create_a_copy(atkbdc, dev);
4658 * If we don't have the AT keyboard controller yet,
4659 * just reserve the IRQ for later use...
4660 * (See psmidentify() above.)
4663 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
4669 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
4670 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
4672 #endif /* DEV_ISA */