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>
84 #include <sys/libkern.h>
86 #include <sys/limits.h>
87 #include <sys/mouse.h>
88 #include <machine/resource.h>
91 #include <isa/isavar.h>
94 #include <dev/atkbdc/atkbdcreg.h>
95 #include <dev/atkbdc/psm.h>
98 * Driver specific options: the following options may be set by
99 * `options' statements in the kernel configuration file.
104 #define PSM_DEBUG 0 /*
105 * logging: 0: none, 1: brief, 2: verbose
106 * 3: sync errors, 4: all packets
109 #define VLOG(level, args) do { \
110 if (verbose >= level) \
114 #ifndef PSM_INPUT_TIMEOUT
115 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
118 #ifndef PSM_TAP_TIMEOUT
119 #define PSM_TAP_TIMEOUT 125000
122 #ifndef PSM_TAP_THRESHOLD
123 #define PSM_TAP_THRESHOLD 25
126 /* end of driver specific options */
128 #define PSMCPNP_DRIVER_NAME "psmcpnp"
131 #define PSM_BUFSIZE 960
132 #define PSM_SMALLBUFSIZE 240
134 /* operation levels */
135 #define PSM_LEVEL_BASE 0
136 #define PSM_LEVEL_STANDARD 1
137 #define PSM_LEVEL_NATIVE 2
138 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
139 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
141 /* Logitech PS2++ protocol */
142 #define MOUSE_PS2PLUS_CHECKBITS(b) \
143 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
144 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
145 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
148 typedef struct ringbuf {
149 int count; /* # of valid elements in the buffer */
150 int head; /* head pointer */
151 int tail; /* tail poiner */
152 u_char buf[PSM_BUFSIZE];
156 typedef struct packetbuf {
157 u_char ipacket[16]; /* interim input buffer */
158 int inputbytes; /* # of bytes in the input buffer */
161 #ifndef PSM_PACKETQUEUE
162 #define PSM_PACKETQUEUE 128
165 typedef struct synapticsinfo {
166 struct sysctl_ctx_list sysctl_ctx;
167 struct sysctl_oid *sysctl_tree;
168 int directional_scrolls;
169 int two_finger_scroll;
186 int weight_previous_na;
187 int weight_len_squared;
195 int vscroll_ver_area;
196 int vscroll_hor_area;
197 int vscroll_min_delta;
208 typedef struct synapticspacket {
213 #define SYNAPTICS_PACKETQUEUE 10
214 #define SYNAPTICS_QUEUE_CURSOR(x) \
215 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
217 #define SYNAPTICS_VERSION_GE(synhw, major, minor) \
218 ((synhw).infoMajor > (major) || \
219 ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
221 typedef struct smoother {
222 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
235 typedef struct gesture {
241 int zmax; /* maximum pressure value */
242 struct timeval taptimeout; /* tap timeout for touchpads */
246 TRACKPOINT_SYSCTL_SENSITIVITY,
247 TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
248 TRACKPOINT_SYSCTL_UPPER_PLATEAU,
249 TRACKPOINT_SYSCTL_BACKUP_RANGE,
250 TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
251 TRACKPOINT_SYSCTL_MINIMUM_DRAG,
252 TRACKPOINT_SYSCTL_UP_THRESHOLD,
253 TRACKPOINT_SYSCTL_THRESHOLD,
254 TRACKPOINT_SYSCTL_JENKS_CURVATURE,
255 TRACKPOINT_SYSCTL_Z_TIME,
256 TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
257 TRACKPOINT_SYSCTL_SKIP_BACKUPS
260 typedef struct trackpointinfo {
261 struct sysctl_ctx_list sysctl_ctx;
262 struct sysctl_oid *sysctl_tree;
277 typedef struct finger {
284 #define PSM_FINGERS 2 /* # of processed fingers */
285 #define PSM_FINGER_IS_PEN (1<<0)
286 #define PSM_FINGER_FUZZY (1<<1)
287 #define PSM_FINGER_DEFAULT_P tap_threshold
288 #define PSM_FINGER_DEFAULT_W 1
289 #define PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0)
290 #define PSM_FINGER_RESET(f) do { \
291 (f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \
294 typedef struct elantechhw {
310 /* minimum versions supported by this driver */
311 #define ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600)
313 #define ELANTECH_MAGIC(magic) \
314 ((magic)[0] == 0x3c && (magic)[1] == 0x03 && \
315 ((magic)[2] == 0xc8 || (magic)[2] == 0x00))
317 #define ELANTECH_FW_ID 0x00
318 #define ELANTECH_FW_VERSION 0x01
319 #define ELANTECH_CAPABILITIES 0x02
320 #define ELANTECH_SAMPLE 0x03
321 #define ELANTECH_RESOLUTION 0x04
322 #define ELANTECH_REG_READ 0x10
323 #define ELANTECH_REG_WRITE 0x11
324 #define ELANTECH_REG_RDWR 0x00
325 #define ELANTECH_CUSTOM_CMD 0xf8
327 #define ELANTECH_MAX_FINGERS PSM_FINGERS
329 #define ELANTECH_FINGER_SET_XYP(pb) (finger_t) { \
330 .x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2], \
331 .y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5], \
332 .p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f), \
333 .w = PSM_FINGER_DEFAULT_W, \
339 ELANTECH_PKT_TRACKPOINT,
340 ELANTECH_PKT_V2_COMMON,
341 ELANTECH_PKT_V2_2FINGER,
343 ELANTECH_PKT_V4_STATUS,
344 ELANTECH_PKT_V4_HEAD,
345 ELANTECH_PKT_V4_MOTION
348 #define ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06)
349 #define ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 : \
350 (pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) && \
351 (pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff && \
352 (pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff && \
353 (pb)->ipacket[5] == 0xff)
354 #define ELANTECH_PKT_IS_V2(pb) \
355 (((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02)
356 #define ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? \
357 ((pb)->ipacket[3] & 0x09) == 0x08 : \
358 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02)
359 #define ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? \
360 ((pb)->ipacket[3] & 0x09) == 0x09 : \
361 ((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c)
362 #define ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? \
363 ((pb)->ipacket[3] & 0x08) == 0x00 : \
364 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10)
366 typedef struct elantechaction {
367 finger_t fingers[ELANTECH_MAX_FINGERS];
371 /* driver control block */
372 struct psm_softc { /* Driver status information */
374 struct selinfo rsel; /* Process selecting for Input */
375 u_char state; /* Mouse driver state */
376 int config; /* driver configuration flags */
377 int flags; /* other flags */
378 KBDC kbdc; /* handle to access kbd controller */
379 struct resource *intr; /* IRQ resource */
380 void *ih; /* interrupt handle */
381 mousehw_t hw; /* hardware information */
382 synapticshw_t synhw; /* Synaptics hardware information */
383 synapticsinfo_t syninfo; /* Synaptics configuration */
384 smoother_t smoother[PSM_FINGERS]; /* Motion smoothing */
385 gesture_t gesture; /* Gesture context */
386 elantechhw_t elanhw; /* Elantech hardware information */
387 elantechaction_t elanaction; /* Elantech action context */
388 int tphw; /* TrackPoint hardware information */
389 trackpointinfo_t tpinfo; /* TrackPoint configuration */
390 mousemode_t mode; /* operation mode */
391 mousemode_t dflt_mode; /* default operation mode */
392 mousestatus_t status; /* accumulated mouse movement */
393 ringbuf_t queue; /* mouse status queue */
394 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
395 int pqueue_start; /* start of data in queue */
396 int pqueue_end; /* end of data in queue */
397 int button; /* the latest button state */
398 int xold; /* previous absolute X position */
399 int yold; /* previous absolute Y position */
400 int xaverage; /* average X position */
401 int yaverage; /* average Y position */
402 int squelch; /* level to filter movement at low speed */
403 int syncerrors; /* # of bytes discarded to synchronize */
404 int pkterrors; /* # of packets failed during quaranteen. */
405 struct timeval inputtimeout;
406 struct timeval lastsoftintr; /* time of last soft interrupt */
407 struct timeval lastinputerr; /* time last sync error happened */
408 struct timeval idletimeout;
409 packetbuf_t idlepacket; /* packet to send after idle timeout */
410 int watchdog; /* watchdog timer flag */
411 struct callout callout; /* watchdog timer call out */
412 struct callout softcallout; /* buffer timer call out */
417 struct sigio *async; /* Processes waiting for SIGIO */
418 int extended_buttons;
420 static devclass_t psm_devclass;
422 /* driver state flags (state) */
423 #define PSM_VALID 0x80
424 #define PSM_OPEN 1 /* Device is open */
425 #define PSM_ASLP 2 /* Waiting for mouse data */
426 #define PSM_SOFTARMED 4 /* Software interrupt armed */
427 #define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
429 /* driver configuration flags (config) */
430 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
431 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
432 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
433 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
434 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
435 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
436 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
437 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
438 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
440 #define PSM_CONFIG_FLAGS \
441 (PSM_CONFIG_RESOLUTION | \
443 PSM_CONFIG_NOCHECKSYNC | \
444 PSM_CONFIG_NOIDPROBE | \
445 PSM_CONFIG_NORESET | \
446 PSM_CONFIG_FORCETAP | \
447 PSM_CONFIG_IGNPORTERROR | \
448 PSM_CONFIG_HOOKRESUME | \
449 PSM_CONFIG_INITAFTERSUSPEND)
451 /* other flags (flags) */
452 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
454 #define kbdcp(p) ((atkbdc_softc_t *)(p))
455 #define ALWAYS_RESTORE_CONTROLLER(kbdc) !(kbdcp(kbdc)->quirks \
456 & KBDC_QUIRK_KEEP_ACTIVATED)
459 static int tap_enabled = -1;
460 static int verbose = PSM_DEBUG;
461 static int synaptics_support = 0;
462 static int trackpoint_support = 0;
463 static int elantech_support = 0;
465 /* for backward compatibility */
466 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
467 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
468 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
470 typedef struct old_mousehw {
477 typedef struct old_mousemode {
484 #define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field)
486 SYNAPTICS_SYSCTL_MIN_PRESSURE = SYN_OFFSET(min_pressure),
487 SYNAPTICS_SYSCTL_MAX_PRESSURE = SYN_OFFSET(max_pressure),
488 SYNAPTICS_SYSCTL_MAX_WIDTH = SYN_OFFSET(max_width),
489 SYNAPTICS_SYSCTL_MARGIN_TOP = SYN_OFFSET(margin_top),
490 SYNAPTICS_SYSCTL_MARGIN_RIGHT = SYN_OFFSET(margin_right),
491 SYNAPTICS_SYSCTL_MARGIN_BOTTOM = SYN_OFFSET(margin_bottom),
492 SYNAPTICS_SYSCTL_MARGIN_LEFT = SYN_OFFSET(margin_left),
493 SYNAPTICS_SYSCTL_NA_TOP = SYN_OFFSET(na_top),
494 SYNAPTICS_SYSCTL_NA_RIGHT = SYN_OFFSET(na_right),
495 SYNAPTICS_SYSCTL_NA_BOTTOM = SYN_OFFSET(na_bottom),
496 SYNAPTICS_SYSCTL_NA_LEFT = SYN_OFFSET(na_left),
497 SYNAPTICS_SYSCTL_WINDOW_MIN = SYN_OFFSET(window_min),
498 SYNAPTICS_SYSCTL_WINDOW_MAX = SYN_OFFSET(window_max),
499 SYNAPTICS_SYSCTL_MULTIPLICATOR = SYN_OFFSET(multiplicator),
500 SYNAPTICS_SYSCTL_WEIGHT_CURRENT = SYN_OFFSET(weight_current),
501 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS = SYN_OFFSET(weight_previous),
502 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA = SYN_OFFSET(weight_previous_na),
503 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED = SYN_OFFSET(weight_len_squared),
504 SYNAPTICS_SYSCTL_DIV_MIN = SYN_OFFSET(div_min),
505 SYNAPTICS_SYSCTL_DIV_MAX = SYN_OFFSET(div_max),
506 SYNAPTICS_SYSCTL_DIV_MAX_NA = SYN_OFFSET(div_max_na),
507 SYNAPTICS_SYSCTL_DIV_LEN = SYN_OFFSET(div_len),
508 SYNAPTICS_SYSCTL_TAP_MAX_DELTA = SYN_OFFSET(tap_max_delta),
509 SYNAPTICS_SYSCTL_TAP_MIN_QUEUE = SYN_OFFSET(tap_min_queue),
510 SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT = SYN_OFFSET(taphold_timeout),
511 SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA = SYN_OFFSET(vscroll_hor_area),
512 SYNAPTICS_SYSCTL_VSCROLL_VER_AREA = SYN_OFFSET(vscroll_ver_area),
513 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA = SYN_OFFSET(vscroll_min_delta),
514 SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN = SYN_OFFSET(vscroll_div_min),
515 SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX = SYN_OFFSET(vscroll_div_max),
516 SYNAPTICS_SYSCTL_TOUCHPAD_OFF = SYN_OFFSET(touchpad_off),
517 SYNAPTICS_SYSCTL_SOFTBUTTONS_Y = SYN_OFFSET(softbuttons_y),
518 SYNAPTICS_SYSCTL_SOFTBUTTON2_X = SYN_OFFSET(softbutton2_x),
519 SYNAPTICS_SYSCTL_SOFTBUTTON3_X = SYN_OFFSET(softbutton3_x),
522 /* packet formatting function */
523 typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
526 /* function prototypes */
527 static void psmidentify(driver_t *, device_t);
528 static int psmprobe(device_t);
529 static int psmattach(device_t);
530 static int psmdetach(device_t);
531 static int psmresume(device_t);
533 static d_open_t psmopen;
534 static d_close_t psmclose;
535 static d_read_t psmread;
536 static d_write_t psmwrite;
537 static d_ioctl_t psmioctl;
538 static d_poll_t psmpoll;
540 static int enable_aux_dev(KBDC);
541 static int disable_aux_dev(KBDC);
542 static int get_mouse_status(KBDC, int *, int, int);
543 static int get_aux_id(KBDC);
544 static int set_mouse_sampling_rate(KBDC, int);
545 static int set_mouse_scaling(KBDC, int);
546 static int set_mouse_resolution(KBDC, int);
547 static int set_mouse_mode(KBDC);
548 static int get_mouse_buttons(KBDC);
549 static int is_a_mouse(int);
550 static void recover_from_error(KBDC);
551 static int restore_controller(KBDC, int);
552 static int doinitialize(struct psm_softc *, mousemode_t *);
553 static int doopen(struct psm_softc *, int);
554 static int reinitialize(struct psm_softc *, int);
555 static char *model_name(int);
556 static void psmsoftintr(void *);
557 static void psmsoftintridle(void *);
558 static void psmintr(void *);
559 static void psmtimeout(void *);
560 static int timeelapsed(const struct timeval *, int, int,
561 const struct timeval *);
562 static void dropqueue(struct psm_softc *);
563 static void flushpackets(struct psm_softc *);
564 static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
565 mousestatus_t *, int *, int *, int *);
566 static int proc_synaptics(struct psm_softc *, packetbuf_t *,
567 mousestatus_t *, int *, int *, int *);
568 static void proc_versapad(struct psm_softc *, packetbuf_t *,
569 mousestatus_t *, int *, int *, int *);
570 static int proc_elantech(struct psm_softc *, packetbuf_t *,
571 mousestatus_t *, int *, int *, int *);
572 static int psmpalmdetect(struct psm_softc *, finger_t *, int);
573 static void psmgestures(struct psm_softc *, finger_t *, int,
575 static void psmsmoother(struct psm_softc *, finger_t *, int,
576 mousestatus_t *, int *, int *);
577 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
580 /* vendor specific features */
581 enum probearg { PROBE, REINIT };
582 typedef int probefunc_t(struct psm_softc *, enum probearg);
584 static int mouse_id_proc1(KBDC, int, int, int *);
585 static int mouse_ext_command(KBDC, int);
587 static probefunc_t enable_groller;
588 static probefunc_t enable_gmouse;
589 static probefunc_t enable_aglide;
590 static probefunc_t enable_kmouse;
591 static probefunc_t enable_msexplorer;
592 static probefunc_t enable_msintelli;
593 static probefunc_t enable_4dmouse;
594 static probefunc_t enable_4dplus;
595 static probefunc_t enable_mmanplus;
596 static probefunc_t enable_synaptics;
597 static probefunc_t enable_trackpoint;
598 static probefunc_t enable_versapad;
599 static probefunc_t enable_elantech;
601 static void set_trackpoint_parameters(struct psm_softc *sc);
602 static void synaptics_passthrough_on(struct psm_softc *sc);
603 static void synaptics_passthrough_off(struct psm_softc *sc);
604 static int synaptics_preferred_mode(struct psm_softc *sc);
605 static void synaptics_set_mode(struct psm_softc *sc, int mode_byte);
611 probefunc_t *probefunc;
614 * WARNING: the order of probe is very important. Don't mess it
615 * unless you know what you are doing.
617 { MOUSE_MODEL_NET, /* Genius NetMouse */
618 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
619 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
620 0xc8, 6, enable_groller },
621 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
622 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
623 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
624 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
625 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
626 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
627 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
628 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
629 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
630 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
631 { MOUSE_MODEL_ELANTECH, /* Elantech Touchpad */
632 0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech },
633 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
634 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
635 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
636 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
637 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
638 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
639 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
640 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
641 { MOUSE_MODEL_TRACKPOINT, /* IBM/Lenovo TrackPoint */
642 0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
643 { MOUSE_MODEL_GENERIC,
644 0xc0, MOUSE_PS2_PACKETSIZE, NULL },
646 #define GENERIC_MOUSE_ENTRY (nitems(vendortype) - 1)
648 /* device driver declarateion */
649 static device_method_t psm_methods[] = {
650 /* Device interface */
651 DEVMETHOD(device_identify, psmidentify),
652 DEVMETHOD(device_probe, psmprobe),
653 DEVMETHOD(device_attach, psmattach),
654 DEVMETHOD(device_detach, psmdetach),
655 DEVMETHOD(device_resume, psmresume),
660 static driver_t psm_driver = {
663 sizeof(struct psm_softc),
666 static struct cdevsw psm_cdevsw = {
667 .d_version = D_VERSION,
668 .d_flags = D_NEEDGIANT,
675 .d_name = PSM_DRIVER_NAME,
678 /* device I/O routines */
680 enable_aux_dev(KBDC kbdc)
684 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
685 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
687 return (res == PSM_ACK);
691 disable_aux_dev(KBDC kbdc)
695 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
696 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
698 return (res == PSM_ACK);
702 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
711 cmd = PSMC_SEND_DEV_STATUS;
714 cmd = PSMC_SEND_DEV_DATA;
717 empty_aux_buffer(kbdc, 5);
718 res = send_aux_command(kbdc, cmd);
719 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
720 (flag == 1) ? "DATA" : "STATUS", res));
724 for (i = 0; i < len; ++i) {
725 status[i] = read_aux_data(kbdc);
730 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
731 (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
737 get_aux_id(KBDC kbdc)
742 empty_aux_buffer(kbdc, 5);
743 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
744 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
751 id = read_aux_data(kbdc);
752 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
758 set_mouse_sampling_rate(KBDC kbdc, int rate)
762 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
763 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
765 return ((res == PSM_ACK) ? rate : -1);
769 set_mouse_scaling(KBDC kbdc, int scale)
776 scale = PSMC_SET_SCALING11;
779 scale = PSMC_SET_SCALING21;
782 res = send_aux_command(kbdc, scale);
783 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
784 (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
786 return (res == PSM_ACK);
789 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
791 set_mouse_resolution(KBDC kbdc, int val)
795 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
796 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
798 return ((res == PSM_ACK) ? val : -1);
802 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
803 * re-enabled by calling `enable_aux_dev()'
806 set_mouse_mode(KBDC kbdc)
810 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
811 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
813 return (res == PSM_ACK);
817 get_mouse_buttons(KBDC kbdc)
819 int c = 2; /* assume two buttons by default */
823 * NOTE: a special sequence to obtain Logitech Mouse specific
824 * information: set resolution to 25 ppi, set scaling to 1:1, set
825 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
826 * mouse status bytes is the number of available buttons.
827 * Some manufactures also support this sequence.
829 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
831 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
832 set_mouse_scaling(kbdc, 1) &&
833 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
838 /* misc subroutines */
840 * Someday, I will get the complete list of valid pointing devices and
847 static int valid_ids[] = {
848 PSM_MOUSE_ID, /* mouse */
849 PSM_BALLPOINT_ID, /* ballpoint device */
850 PSM_INTELLI_ID, /* Intellimouse */
851 PSM_EXPLORER_ID, /* Intellimouse Explorer */
852 -1 /* end of table */
856 for (i = 0; valid_ids[i] >= 0; ++i)
857 if (valid_ids[i] == id)
866 model_name(int model)
872 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
873 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
874 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
875 { MOUSE_MODEL_THINK, "ThinkingMouse" },
876 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
877 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
878 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
879 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
880 { MOUSE_MODEL_4D, "4D Mouse" },
881 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
882 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
883 { MOUSE_MODEL_TRACKPOINT, "IBM/Lenovo TrackPoint" },
884 { MOUSE_MODEL_ELANTECH, "Elantech Touchpad" },
885 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
886 { MOUSE_MODEL_UNKNOWN, "Unknown" },
890 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
891 if (models[i].model_code == model)
893 return (models[i].model_name);
897 recover_from_error(KBDC kbdc)
899 /* discard anything left in the output buffer */
900 empty_both_buffers(kbdc, 10);
904 * NOTE: KBDC_RESET_KBD may not restore the communication between the
905 * keyboard and the controller.
910 * NOTE: somehow diagnostic and keyboard port test commands bring the
913 if (!test_controller(kbdc))
914 log(LOG_ERR, "psm: keyboard controller failed.\n");
915 /* if there isn't a keyboard in the system, the following error is OK */
916 if (test_kbd_port(kbdc) != 0)
917 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
922 restore_controller(KBDC kbdc, int command_byte)
924 empty_both_buffers(kbdc, 10);
926 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
927 log(LOG_ERR, "psm: failed to restore the keyboard controller "
929 empty_both_buffers(kbdc, 10);
932 empty_both_buffers(kbdc, 10);
938 * Re-initialize the aux port and device. The aux port must be enabled
939 * and its interrupt must be disabled before calling this routine.
940 * The aux device will be disabled before returning.
941 * The keyboard controller must be locked via `kbdc_lock()' before
942 * calling this routine.
945 doinitialize(struct psm_softc *sc, mousemode_t *mode)
947 KBDC kbdc = sc->kbdc;
951 switch((i = test_aux_port(kbdc))) {
952 case 1: /* ignore these errors */
958 "psm%d: strange result for test aux port (%d).\n",
961 case 0: /* no error */
963 case -1: /* time out */
965 recover_from_error(kbdc);
966 if (sc->config & PSM_CONFIG_IGNPORTERROR)
968 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
973 if (sc->config & PSM_CONFIG_NORESET) {
975 * Don't try to reset the pointing device. It may possibly
976 * be left in the unknown state, though...
980 * NOTE: some controllers appears to hang the `keyboard' when
981 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
983 if (!reset_aux_dev(kbdc)) {
984 recover_from_error(kbdc);
985 log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
992 * both the aux port and the aux device is functioning, see
993 * if the device can be enabled.
995 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
996 log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
1000 empty_both_buffers(kbdc, 10); /* remove stray data if any */
1002 /* Re-enable the mouse. */
1003 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1004 if (vendortype[i].model == sc->hw.model)
1005 (*vendortype[i].probefunc)(sc, REINIT);
1007 /* set mouse parameters */
1008 if (mode != (mousemode_t *)NULL) {
1010 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
1011 if (mode->resolution >= 0)
1013 set_mouse_resolution(kbdc, mode->resolution);
1014 set_mouse_scaling(kbdc, 1);
1015 set_mouse_mode(kbdc);
1018 /* Record sync on the next data packet we see. */
1019 sc->flags |= PSM_NEED_SYNCBITS;
1021 /* just check the status of the mouse */
1022 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
1023 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
1030 doopen(struct psm_softc *sc, int command_byte)
1035 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
1036 * no obvious reason. Thus we check the current mode and restore the
1037 * Absolute Mode if it was cleared.
1039 * The previous hack at the end of psmprobe() wasn't efficient when
1040 * moused(8) was restarted.
1042 * A Reset (FF) or Set Defaults (F6) command would clear the
1043 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
1044 * doesn't show any evidence of such a command.
1046 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
1047 mouse_ext_command(sc->kbdc, 1);
1048 get_mouse_status(sc->kbdc, stat, 0, 3);
1049 if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
1052 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1053 VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
1054 "hopefully restored\n",
1060 * A user may want to disable tap and drag gestures on a Synaptics
1061 * TouchPad when it operates in Relative Mode.
1063 if (sc->hw.model == MOUSE_MODEL_GENERIC) {
1064 if (tap_enabled > 0) {
1066 "psm%d: enable tap and drag gestures\n",
1068 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1069 } else if (tap_enabled == 0) {
1071 "psm%d: disable tap and drag gestures\n",
1073 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1077 /* enable the mouse device */
1078 if (!enable_aux_dev(sc->kbdc)) {
1079 /* MOUSE ERROR: failed to enable the mouse because:
1080 * 1) the mouse is faulty,
1081 * 2) the mouse has been removed(!?)
1082 * In the latter case, the keyboard may have hung, and need
1083 * recovery procedure...
1085 recover_from_error(sc->kbdc);
1087 /* FIXME: we could reset the mouse here and try to enable
1088 * it again. But it will take long time and it's not a good
1089 * idea to disable the keyboard that long...
1091 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
1092 recover_from_error(sc->kbdc);
1096 restore_controller(sc->kbdc, command_byte);
1097 /* mark this device is no longer available */
1098 sc->state &= ~PSM_VALID;
1100 "psm%d: failed to enable the device (doopen).\n",
1106 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1107 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
1110 /* enable the aux port and interrupt */
1111 if (!set_controller_command_byte(sc->kbdc,
1112 kbdc_get_device_mask(sc->kbdc),
1113 (command_byte & KBD_KBD_CONTROL_BITS) |
1114 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
1115 /* CONTROLLER ERROR */
1116 disable_aux_dev(sc->kbdc);
1117 restore_controller(sc->kbdc, command_byte);
1119 "psm%d: failed to enable the aux interrupt (doopen).\n",
1124 /* start the watchdog timer */
1125 sc->watchdog = FALSE;
1126 callout_reset(&sc->callout, hz * 2, psmtimeout, sc);
1132 reinitialize(struct psm_softc *sc, int doinit)
1138 /* don't let anybody mess with the aux device */
1139 if (!kbdc_lock(sc->kbdc, TRUE))
1143 /* block our watchdog timer */
1144 sc->watchdog = FALSE;
1145 callout_stop(&sc->callout);
1147 /* save the current controller command byte */
1148 empty_both_buffers(sc->kbdc, 10);
1149 c = get_controller_command_byte(sc->kbdc);
1151 "psm%d: current command byte: %04x (reinitialize).\n",
1154 /* enable the aux port but disable the aux interrupt and the keyboard */
1155 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
1156 kbdc_get_device_mask(sc->kbdc),
1157 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1158 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1159 /* CONTROLLER ERROR */
1161 kbdc_lock(sc->kbdc, FALSE);
1163 "psm%d: unable to set the command byte (reinitialize).\n",
1168 /* flush any data */
1169 if (sc->state & PSM_VALID) {
1170 /* this may fail; but never mind... */
1171 disable_aux_dev(sc->kbdc);
1172 empty_aux_buffer(sc->kbdc, 10);
1177 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1179 /* try to detect the aux device; are you still there? */
1182 if (doinitialize(sc, &sc->mode)) {
1184 sc->state |= PSM_VALID;
1186 /* the device has gone! */
1187 restore_controller(sc->kbdc, c);
1188 sc->state &= ~PSM_VALID;
1190 "psm%d: the aux device has gone! (reinitialize).\n",
1197 /* restore the driver state */
1198 if ((sc->state & PSM_OPEN) && (err == 0)) {
1199 /* enable the aux device and the port again */
1200 err = doopen(sc, c);
1202 log(LOG_ERR, "psm%d: failed to enable the device "
1203 "(reinitialize).\n", sc->unit);
1205 /* restore the keyboard port and disable the aux port */
1206 if (!set_controller_command_byte(sc->kbdc,
1207 kbdc_get_device_mask(sc->kbdc),
1208 (c & KBD_KBD_CONTROL_BITS) |
1209 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1210 /* CONTROLLER ERROR */
1211 log(LOG_ERR, "psm%d: failed to disable the aux port "
1212 "(reinitialize).\n", sc->unit);
1217 kbdc_lock(sc->kbdc, FALSE);
1221 /* psm driver entry points */
1224 psmidentify(driver_t *driver, device_t parent)
1231 unit = device_get_unit(parent);
1233 /* always add at least one child */
1234 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1238 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1243 * If the PS/2 mouse device has already been reported by ACPI or
1244 * PnP BIOS, obtain the IRQ resource from it.
1245 * (See psmcpnp_attach() below.)
1247 psmc = device_find_child(device_get_parent(parent),
1248 PSMCPNP_DRIVER_NAME, unit);
1251 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1254 bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1255 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
1258 #define endprobe(v) do { \
1261 kbdc_set_device_mask(sc->kbdc, mask); \
1262 kbdc_lock(sc->kbdc, FALSE); \
1267 psmprobe(device_t dev)
1269 int unit = device_get_unit(dev);
1270 struct psm_softc *sc = device_get_softc(dev);
1281 /* see if IRQ is available */
1283 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1284 if (sc->intr == NULL) {
1286 device_printf(dev, "unable to allocate IRQ\n");
1289 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1292 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1293 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1294 /* XXX: for backward compatibility */
1295 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1297 #ifdef PSM_RESETAFTERSUSPEND
1298 PSM_CONFIG_INITAFTERSUSPEND;
1300 PSM_CONFIG_HOOKRESUME;
1302 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1307 device_set_desc(dev, "PS/2 Mouse");
1309 if (!kbdc_lock(sc->kbdc, TRUE)) {
1310 printf("psm%d: unable to lock the controller.\n", unit);
1317 * NOTE: two bits in the command byte controls the operation of the
1318 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1319 * port interrupt (IRQ 12) enable bit (bit 2).
1322 /* discard anything left after the keyboard initialization */
1323 empty_both_buffers(sc->kbdc, 10);
1325 /* save the current command byte; it will be used later */
1326 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1327 command_byte = get_controller_command_byte(sc->kbdc);
1329 printf("psm%d: current command byte:%04x\n", unit,
1331 if (command_byte == -1) {
1332 /* CONTROLLER ERROR */
1333 printf("psm%d: unable to get the current command byte value.\n",
1339 * disable the keyboard port while probing the aux port, which must be
1340 * enabled during this routine
1342 if (!set_controller_command_byte(sc->kbdc,
1343 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1344 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1345 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1347 * this is CONTROLLER ERROR; I don't know how to recover
1348 * from this error...
1350 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1351 restore_controller(sc->kbdc, command_byte);
1352 printf("psm%d: unable to set the command byte.\n", unit);
1355 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1358 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1359 * port exists and is functioning. However, some controllers appears
1360 * to respond with zero even when the aux port doesn't exist. (It may
1361 * be that this is only the case when the controller DOES have the aux
1362 * port but the port is not wired on the motherboard.) The keyboard
1363 * controllers without the port, such as the original AT, are
1364 * supposed to return with an error code or simply time out. In any
1365 * case, we have to continue probing the port even when the controller
1368 * XXX: some controllers erroneously return the error code 1, 2 or 3
1369 * when it has a perfectly functional aux port. We have to ignore
1370 * this error code. Even if the controller HAS error with the aux
1371 * port, it will be detected later...
1372 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1374 switch ((i = test_aux_port(sc->kbdc))) {
1375 case 1: /* ignore these errors */
1380 printf("psm%d: strange result for test aux port "
1381 "(%d).\n", unit, i);
1383 case 0: /* no error */
1385 case -1: /* time out */
1386 default: /* error */
1387 recover_from_error(sc->kbdc);
1388 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1390 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1391 restore_controller(sc->kbdc, command_byte);
1393 printf("psm%d: the aux port is not functioning (%d).\n",
1398 if (sc->config & PSM_CONFIG_NORESET) {
1400 * Don't try to reset the pointing device. It may possibly be
1401 * left in an unknown state, though...
1405 * NOTE: some controllers appears to hang the `keyboard' when
1406 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1408 * Attempt to reset the controller twice -- this helps
1409 * pierce through some KVM switches. The second reset
1412 if (!reset_aux_dev(sc->kbdc)) {
1413 recover_from_error(sc->kbdc);
1414 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1415 restore_controller(sc->kbdc, command_byte);
1417 printf("psm%d: failed to reset the aux "
1420 } else if (!reset_aux_dev(sc->kbdc)) {
1421 recover_from_error(sc->kbdc);
1423 printf("psm%d: failed to reset the aux device "
1429 * both the aux port and the aux device are functioning, see if the
1430 * device can be enabled. NOTE: when enabled, the device will start
1431 * sending data; we shall immediately disable the device once we know
1432 * the device can be enabled.
1434 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1436 recover_from_error(sc->kbdc);
1437 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1438 restore_controller(sc->kbdc, command_byte);
1440 printf("psm%d: failed to enable the aux device.\n",
1445 /* save the default values after reset */
1446 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1447 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1448 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1450 sc->dflt_mode.rate = sc->mode.rate = -1;
1451 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1454 /* hardware information */
1455 sc->hw.iftype = MOUSE_IF_PS2;
1457 /* verify the device is a mouse */
1458 sc->hw.hwid = get_aux_id(sc->kbdc);
1459 if (!is_a_mouse(sc->hw.hwid)) {
1460 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1461 restore_controller(sc->kbdc, command_byte);
1463 printf("psm%d: unknown device type (%d).\n", unit,
1467 switch (sc->hw.hwid) {
1468 case PSM_BALLPOINT_ID:
1469 sc->hw.type = MOUSE_TRACKBALL;
1472 case PSM_INTELLI_ID:
1473 case PSM_EXPLORER_ID:
1474 case PSM_4DMOUSE_ID:
1476 sc->hw.type = MOUSE_MOUSE;
1479 sc->hw.type = MOUSE_UNKNOWN;
1483 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1485 i = GENERIC_MOUSE_ENTRY;
1488 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1490 /* other parameters */
1491 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1492 if ((*vendortype[i].probefunc)(sc, PROBE)) {
1494 printf("psm%d: found %s\n", unit,
1495 model_name(vendortype[i].model));
1500 sc->hw.model = vendortype[i].model;
1502 sc->dflt_mode.level = PSM_LEVEL_BASE;
1503 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1504 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1505 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1506 sc->dflt_mode.syncmask[0] = 0;
1508 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1509 if (sc->config & PSM_CONFIG_FORCETAP)
1510 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1511 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1512 sc->mode = sc->dflt_mode;
1513 sc->mode.packetsize = vendortype[i].packetsize;
1515 /* set mouse parameters */
1518 * A version of Logitech FirstMouse+ won't report wheel movement,
1519 * if SET_DEFAULTS is sent... Don't use this command.
1520 * This fix was found by Takashi Nishida.
1522 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1524 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1526 if (sc->config & PSM_CONFIG_RESOLUTION)
1527 sc->mode.resolution =
1528 set_mouse_resolution(sc->kbdc,
1529 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1530 else if (sc->mode.resolution >= 0)
1531 sc->mode.resolution =
1532 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1533 if (sc->mode.rate > 0)
1535 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1536 set_mouse_scaling(sc->kbdc, 1);
1538 /* Record sync on the next data packet we see. */
1539 sc->flags |= PSM_NEED_SYNCBITS;
1541 /* just check the status of the mouse */
1543 * NOTE: XXX there are some arcane controller/mouse combinations out
1544 * there, which hung the controller unless there is data transmission
1545 * after ACK from the mouse.
1547 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1548 printf("psm%d: failed to get status.\n", unit);
1551 * When in its native mode, some mice operate with different
1552 * default parameters than in the PS/2 compatible mode.
1554 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1555 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1558 /* disable the aux port for now... */
1559 if (!set_controller_command_byte(sc->kbdc,
1560 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1561 (command_byte & KBD_KBD_CONTROL_BITS) |
1562 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1564 * this is CONTROLLER ERROR; I don't know the proper way to
1565 * recover from this error...
1567 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1568 restore_controller(sc->kbdc, command_byte);
1569 printf("psm%d: unable to set the command byte.\n", unit);
1574 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1575 kbdc_lock(sc->kbdc, FALSE);
1580 psmattach(device_t dev)
1582 int unit = device_get_unit(dev);
1583 struct psm_softc *sc = device_get_softc(dev);
1587 /* Setup initial state */
1588 sc->state = PSM_VALID;
1589 callout_init(&sc->callout, 0);
1590 callout_init(&sc->softcallout, 0);
1592 /* Setup our interrupt handler */
1594 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1595 if (sc->intr == NULL)
1597 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1600 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1605 sc->dev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "psm%d", unit);
1606 sc->dev->si_drv1 = sc;
1607 sc->bdev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "bpsm%d", unit);
1608 sc->bdev->si_drv1 = sc;
1610 /* Some touchpad devices need full reinitialization after suspend. */
1611 switch (sc->hw.model) {
1612 case MOUSE_MODEL_SYNAPTICS:
1613 case MOUSE_MODEL_GLIDEPOINT:
1614 case MOUSE_MODEL_VERSAPAD:
1615 case MOUSE_MODEL_ELANTECH:
1616 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1619 if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
1620 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1624 /* Elantech trackpad`s sync bit differs from touchpad`s one */
1625 if (sc->hw.model == MOUSE_MODEL_ELANTECH &&
1626 (sc->elanhw.hascrc || sc->elanhw.hastrackpoint))
1627 sc->config |= PSM_CONFIG_NOCHECKSYNC;
1630 printf("psm%d: model %s, device ID %d\n",
1631 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1633 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1634 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1635 sc->hw.hwid >> 8, sc->hw.buttons);
1636 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1637 unit, sc->config, sc->flags, sc->mode.packetsize);
1638 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1639 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1649 psmdetach(device_t dev)
1651 struct psm_softc *sc;
1654 sc = device_get_softc(dev);
1655 if (sc->state & PSM_OPEN)
1659 bus_teardown_intr(dev, sc->intr, sc->ih);
1660 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1662 destroy_dev(sc->dev);
1663 destroy_dev(sc->bdev);
1665 callout_drain(&sc->callout);
1666 callout_drain(&sc->softcallout);
1672 psmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
1674 struct psm_softc *sc;
1679 /* Get device data */
1681 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
1682 /* the device is no longer valid/functioning */
1686 /* Disallow multiple opens */
1687 if (sc->state & PSM_OPEN)
1690 device_busy(devclass_get_device(psm_devclass, sc->unit));
1692 /* Initialize state */
1693 sc->mode.level = sc->dflt_mode.level;
1694 sc->mode.protocol = sc->dflt_mode.protocol;
1695 sc->watchdog = FALSE;
1698 /* flush the event queue */
1699 sc->queue.count = 0;
1702 sc->status.flags = 0;
1703 sc->status.button = 0;
1704 sc->status.obutton = 0;
1709 sc->pqueue_start = 0;
1712 /* empty input buffer */
1717 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1718 if (!kbdc_lock(sc->kbdc, TRUE))
1721 /* save the current controller command byte */
1723 command_byte = get_controller_command_byte(sc->kbdc);
1725 /* enable the aux port and temporalily disable the keyboard */
1726 if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
1727 kbdc_get_device_mask(sc->kbdc),
1728 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1729 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1730 /* CONTROLLER ERROR; do you know how to get out of this? */
1731 kbdc_lock(sc->kbdc, FALSE);
1734 "psm%d: unable to set the command byte (psmopen).\n",
1739 * Now that the keyboard controller is told not to generate
1740 * the keyboard and mouse interrupts, call `splx()' to allow
1741 * the other tty interrupts. The clock interrupt may also occur,
1742 * but timeout routines will be blocked by the poll flag set
1747 /* enable the mouse device */
1748 err = doopen(sc, command_byte);
1752 sc->state |= PSM_OPEN;
1753 kbdc_lock(sc->kbdc, FALSE);
1758 psmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1760 struct psm_softc *sc = dev->si_drv1;
1765 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1766 if (!kbdc_lock(sc->kbdc, TRUE))
1769 /* save the current controller command byte */
1771 command_byte = get_controller_command_byte(sc->kbdc);
1772 if (command_byte == -1) {
1773 kbdc_lock(sc->kbdc, FALSE);
1778 /* disable the aux interrupt and temporalily disable the keyboard */
1779 if (!set_controller_command_byte(sc->kbdc,
1780 kbdc_get_device_mask(sc->kbdc),
1781 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1782 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1784 "psm%d: failed to disable the aux int (psmclose).\n",
1786 /* CONTROLLER ERROR;
1787 * NOTE: we shall force our way through. Because the only
1788 * ill effect we shall see is that we may not be able
1789 * to read ACK from the mouse, and it doesn't matter much
1790 * so long as the mouse will accept the DISABLE command.
1795 /* stop the watchdog timer */
1796 callout_stop(&sc->callout);
1798 /* remove anything left in the output buffer */
1799 empty_aux_buffer(sc->kbdc, 10);
1801 /* disable the aux device, port and interrupt */
1802 if (sc->state & PSM_VALID) {
1803 if (!disable_aux_dev(sc->kbdc)) {
1805 * NOTE: we don't return (error) and continue,
1806 * pretending we have successfully disabled the device.
1807 * It's OK because the interrupt routine will discard
1808 * any data from the mouse hereafter.
1811 "psm%d: failed to disable the device (psmclose).\n",
1815 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1817 "psm%d: failed to get status (psmclose).\n",
1821 if (!set_controller_command_byte(sc->kbdc,
1822 kbdc_get_device_mask(sc->kbdc),
1823 (command_byte & KBD_KBD_CONTROL_BITS) |
1824 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1827 * we shall ignore this error; see the above comment.
1830 "psm%d: failed to disable the aux port (psmclose).\n",
1834 /* remove anything left in the output buffer */
1835 empty_aux_buffer(sc->kbdc, 10);
1837 /* clean up and sigio requests */
1838 if (sc->async != NULL) {
1839 funsetown(&sc->async);
1843 /* close is almost always successful */
1844 sc->state &= ~PSM_OPEN;
1845 kbdc_lock(sc->kbdc, FALSE);
1846 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
1851 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
1854 static u_char butmapps2[8] = {
1856 MOUSE_PS2_BUTTON1DOWN,
1857 MOUSE_PS2_BUTTON2DOWN,
1858 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1859 MOUSE_PS2_BUTTON3DOWN,
1860 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1861 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1862 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
1863 MOUSE_PS2_BUTTON3DOWN,
1865 static u_char butmapmsc[8] = {
1866 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
1867 MOUSE_MSC_BUTTON3UP,
1868 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1869 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1870 MOUSE_MSC_BUTTON3UP,
1871 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1872 MOUSE_MSC_BUTTON2UP,
1873 MOUSE_MSC_BUTTON1UP,
1879 if (sc->mode.level == PSM_LEVEL_BASE) {
1880 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1881 if (status->button & MOUSE_BUTTON4DOWN)
1882 mapped |= MOUSE_BUTTON1DOWN;
1883 status->button = mapped;
1884 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1885 i = imax(imin(status->dx, 255), -256);
1887 buf[0] |= MOUSE_PS2_XNEG;
1889 i = imax(imin(status->dy, 255), -256);
1891 buf[0] |= MOUSE_PS2_YNEG;
1893 return (MOUSE_PS2_PACKETSIZE);
1894 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1895 buf[0] = MOUSE_MSC_SYNC |
1896 butmapmsc[status->button & MOUSE_STDBUTTONS];
1897 i = imax(imin(status->dx, 255), -256);
1899 buf[3] = i - buf[1];
1900 i = imax(imin(status->dy, 255), -256);
1902 buf[4] = i - buf[2];
1903 i = imax(imin(status->dz, 127), -128);
1904 buf[5] = (i >> 1) & 0x7f;
1905 buf[6] = (i - (i >> 1)) & 0x7f;
1906 buf[7] = (~status->button >> 3) & 0x7f;
1907 return (MOUSE_SYS_PACKETSIZE);
1909 return (pb->inputbytes);
1913 psmread(struct cdev *dev, struct uio *uio, int flag)
1915 struct psm_softc *sc = dev->si_drv1;
1916 u_char buf[PSM_SMALLBUFSIZE];
1921 if ((sc->state & PSM_VALID) == 0)
1924 /* block until mouse activity occurred */
1926 while (sc->queue.count <= 0) {
1927 if (dev != sc->bdev) {
1929 return (EWOULDBLOCK);
1931 sc->state |= PSM_ASLP;
1932 error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
1933 sc->state &= ~PSM_ASLP;
1937 } else if ((sc->state & PSM_VALID) == 0) {
1938 /* the device disappeared! */
1945 /* copy data to the user land */
1946 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1948 l = imin(sc->queue.count, uio->uio_resid);
1949 if (l > sizeof(buf))
1951 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1952 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1953 sizeof(sc->queue.buf) - sc->queue.head);
1954 bcopy(&sc->queue.buf[0],
1955 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1956 l - (sizeof(sc->queue.buf) - sc->queue.head));
1958 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1959 sc->queue.count -= l;
1960 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1962 error = uiomove(buf, l, uio);
1971 block_mouse_data(struct psm_softc *sc, int *c)
1975 if (!kbdc_lock(sc->kbdc, TRUE))
1979 *c = get_controller_command_byte(sc->kbdc);
1980 if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
1981 kbdc_get_device_mask(sc->kbdc),
1982 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1983 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1984 /* this is CONTROLLER ERROR */
1986 kbdc_lock(sc->kbdc, FALSE);
1991 * The device may be in the middle of status data transmission.
1992 * The transmission will be interrupted, thus, incomplete status
1993 * data must be discarded. Although the aux interrupt is disabled
1994 * at the keyboard controller level, at most one aux interrupt
1995 * may have already been pending and a data byte is in the
1996 * output buffer; throw it away. Note that the second argument
1997 * to `empty_aux_buffer()' is zero, so that the call will just
1998 * flush the internal queue.
1999 * `psmintr()' will be invoked after `splx()' if an interrupt is
2000 * pending; it will see no data and returns immediately.
2002 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
2003 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
2011 dropqueue(struct psm_softc *sc)
2014 sc->queue.count = 0;
2017 if ((sc->state & PSM_SOFTARMED) != 0) {
2018 sc->state &= ~PSM_SOFTARMED;
2019 callout_stop(&sc->softcallout);
2021 sc->pqueue_start = sc->pqueue_end;
2025 flushpackets(struct psm_softc *sc)
2029 bzero(&sc->pqueue, sizeof(sc->pqueue));
2033 unblock_mouse_data(struct psm_softc *sc, int c)
2038 * We may have seen a part of status data during `set_mouse_XXX()'.
2039 * they have been queued; flush it.
2041 empty_aux_buffer(sc->kbdc, 0);
2043 /* restore ports and interrupt */
2044 if (!set_controller_command_byte(sc->kbdc,
2045 kbdc_get_device_mask(sc->kbdc),
2046 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
2048 * CONTROLLER ERROR; this is serious, we may have
2049 * been left with the inaccessible keyboard and
2050 * the disabled mouse interrupt.
2055 kbdc_lock(sc->kbdc, FALSE);
2060 psmwrite(struct cdev *dev, struct uio *uio, int flag)
2062 struct psm_softc *sc = dev->si_drv1;
2063 u_char buf[PSM_SMALLBUFSIZE];
2064 int error = 0, i, l;
2066 if ((sc->state & PSM_VALID) == 0)
2069 if (sc->mode.level < PSM_LEVEL_NATIVE)
2072 /* copy data from the user land */
2073 while (uio->uio_resid > 0) {
2074 l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
2075 error = uiomove(buf, l, uio);
2078 for (i = 0; i < l; i++) {
2079 VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
2080 if (!write_aux_command(sc->kbdc, buf[i])) {
2082 "psm: cmd 0x%x failed.\n", buf[i]));
2083 return (reinitialize(sc, FALSE));
2092 psmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2095 struct psm_softc *sc = dev->si_drv1;
2097 mousestatus_t status;
2098 #if (defined(MOUSE_GETVARS))
2107 /* Perform IOCTL command */
2110 case OLD_MOUSE_GETHWINFO:
2112 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
2113 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
2114 ((old_mousehw_t *)addr)->type = sc->hw.type;
2115 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
2119 case MOUSE_GETHWINFO:
2121 *(mousehw_t *)addr = sc->hw;
2122 if (sc->mode.level == PSM_LEVEL_BASE)
2123 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
2127 case MOUSE_SYN_GETHWINFO:
2129 if (sc->synhw.infoMajor >= 4)
2130 *(synapticshw_t *)addr = sc->synhw;
2136 case OLD_MOUSE_GETMODE:
2138 switch (sc->mode.level) {
2139 case PSM_LEVEL_BASE:
2140 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2142 case PSM_LEVEL_STANDARD:
2143 ((old_mousemode_t *)addr)->protocol =
2144 MOUSE_PROTO_SYSMOUSE;
2146 case PSM_LEVEL_NATIVE:
2147 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2150 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
2151 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2152 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2158 *(mousemode_t *)addr = sc->mode;
2159 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2160 ((mousemode_t *)addr)->syncmask[0] = 0;
2161 ((mousemode_t *)addr)->syncmask[1] = 0;
2163 ((mousemode_t *)addr)->resolution =
2164 MOUSE_RES_LOW - sc->mode.resolution;
2165 switch (sc->mode.level) {
2166 case PSM_LEVEL_BASE:
2167 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2168 ((mousemode_t *)addr)->packetsize =
2169 MOUSE_PS2_PACKETSIZE;
2171 case PSM_LEVEL_STANDARD:
2172 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2173 ((mousemode_t *)addr)->packetsize =
2174 MOUSE_SYS_PACKETSIZE;
2175 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2176 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2178 case PSM_LEVEL_NATIVE:
2179 /* FIXME: this isn't quite correct... XXX */
2180 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2186 case OLD_MOUSE_SETMODE:
2188 if (cmd == OLD_MOUSE_SETMODE) {
2189 mode.rate = ((old_mousemode_t *)addr)->rate;
2191 * resolution old I/F new I/F
2198 if (((old_mousemode_t *)addr)->resolution > 0)
2200 -((old_mousemode_t *)addr)->resolution - 1;
2202 mode.resolution = 0;
2204 ((old_mousemode_t *)addr)->accelfactor;
2207 mode = *(mousemode_t *)addr;
2209 /* adjust and validate parameters. */
2210 if (mode.rate > UCHAR_MAX)
2213 mode.rate = sc->dflt_mode.rate;
2214 else if (mode.rate == -1)
2215 /* don't change the current setting */
2217 else if (mode.rate < 0)
2219 if (mode.resolution >= UCHAR_MAX)
2221 if (mode.resolution >= 200)
2222 mode.resolution = MOUSE_RES_HIGH;
2223 else if (mode.resolution >= 100)
2224 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2225 else if (mode.resolution >= 50)
2226 mode.resolution = MOUSE_RES_MEDIUMLOW;
2227 else if (mode.resolution > 0)
2228 mode.resolution = MOUSE_RES_LOW;
2229 if (mode.resolution == MOUSE_RES_DEFAULT)
2230 mode.resolution = sc->dflt_mode.resolution;
2231 else if (mode.resolution == -1)
2232 /* don't change the current setting */
2234 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2235 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2236 if (mode.level == -1)
2237 /* don't change the current setting */
2238 mode.level = sc->mode.level;
2239 else if ((mode.level < PSM_LEVEL_MIN) ||
2240 (mode.level > PSM_LEVEL_MAX))
2242 if (mode.accelfactor == -1)
2243 /* don't change the current setting */
2244 mode.accelfactor = sc->mode.accelfactor;
2245 else if (mode.accelfactor < 0)
2248 /* don't allow anybody to poll the keyboard controller */
2249 error = block_mouse_data(sc, &command_byte);
2253 /* set mouse parameters */
2255 mode.rate = set_mouse_sampling_rate(sc->kbdc,
2257 if (mode.resolution >= 0)
2259 set_mouse_resolution(sc->kbdc, mode.resolution);
2260 set_mouse_scaling(sc->kbdc, 1);
2261 get_mouse_status(sc->kbdc, stat, 0, 3);
2264 sc->mode.rate = mode.rate;
2265 sc->mode.resolution = mode.resolution;
2266 sc->mode.accelfactor = mode.accelfactor;
2267 sc->mode.level = mode.level;
2270 unblock_mouse_data(sc, command_byte);
2273 case MOUSE_GETLEVEL:
2274 *(int *)addr = sc->mode.level;
2277 case MOUSE_SETLEVEL:
2278 if ((*(int *)addr < PSM_LEVEL_MIN) ||
2279 (*(int *)addr > PSM_LEVEL_MAX))
2281 sc->mode.level = *(int *)addr;
2284 case MOUSE_GETSTATUS:
2286 status = sc->status;
2287 sc->status.flags = 0;
2288 sc->status.obutton = sc->status.button;
2289 sc->status.button = 0;
2294 *(mousestatus_t *)addr = status;
2297 #if (defined(MOUSE_GETVARS))
2299 var = (mousevar_t *)addr;
2300 bzero(var, sizeof(*var));
2302 var->var[0] = MOUSE_VARS_PS2_SIG;
2303 var->var[1] = sc->config;
2304 var->var[2] = sc->flags;
2310 #endif /* MOUSE_GETVARS */
2312 case MOUSE_READSTATE:
2313 case MOUSE_READDATA:
2314 data = (mousedata_t *)addr;
2315 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2318 error = block_mouse_data(sc, &command_byte);
2321 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2322 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2324 unblock_mouse_data(sc, command_byte);
2327 #if (defined(MOUSE_SETRESOLUTION))
2328 case MOUSE_SETRESOLUTION:
2329 mode.resolution = *(int *)addr;
2330 if (mode.resolution >= UCHAR_MAX)
2332 else if (mode.resolution >= 200)
2333 mode.resolution = MOUSE_RES_HIGH;
2334 else if (mode.resolution >= 100)
2335 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2336 else if (mode.resolution >= 50)
2337 mode.resolution = MOUSE_RES_MEDIUMLOW;
2338 else if (mode.resolution > 0)
2339 mode.resolution = MOUSE_RES_LOW;
2340 if (mode.resolution == MOUSE_RES_DEFAULT)
2341 mode.resolution = sc->dflt_mode.resolution;
2342 else if (mode.resolution == -1)
2343 mode.resolution = sc->mode.resolution;
2344 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2345 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2347 error = block_mouse_data(sc, &command_byte);
2350 sc->mode.resolution =
2351 set_mouse_resolution(sc->kbdc, mode.resolution);
2352 if (sc->mode.resolution != mode.resolution)
2354 unblock_mouse_data(sc, command_byte);
2356 #endif /* MOUSE_SETRESOLUTION */
2358 #if (defined(MOUSE_SETRATE))
2360 mode.rate = *(int *)addr;
2361 if (mode.rate > UCHAR_MAX)
2364 mode.rate = sc->dflt_mode.rate;
2365 else if (mode.rate < 0)
2366 mode.rate = sc->mode.rate;
2368 error = block_mouse_data(sc, &command_byte);
2371 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2372 if (sc->mode.rate != mode.rate)
2374 unblock_mouse_data(sc, command_byte);
2376 #endif /* MOUSE_SETRATE */
2378 #if (defined(MOUSE_SETSCALING))
2379 case MOUSE_SETSCALING:
2380 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2383 error = block_mouse_data(sc, &command_byte);
2386 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2388 unblock_mouse_data(sc, command_byte);
2390 #endif /* MOUSE_SETSCALING */
2392 #if (defined(MOUSE_GETHWID))
2394 error = block_mouse_data(sc, &command_byte);
2397 sc->hw.hwid &= ~0x00ff;
2398 sc->hw.hwid |= get_aux_id(sc->kbdc);
2399 *(int *)addr = sc->hw.hwid & 0x00ff;
2400 unblock_mouse_data(sc, command_byte);
2402 #endif /* MOUSE_GETHWID */
2408 error = fsetown(*(int *)addr, &sc->async);
2411 *(int *) addr = fgetown(&sc->async);
2421 psmtimeout(void *arg)
2423 struct psm_softc *sc;
2426 sc = (struct psm_softc *)arg;
2428 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2429 VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2431 kbdc_lock(sc->kbdc, FALSE);
2433 sc->watchdog = TRUE;
2435 callout_reset(&sc->callout, hz, psmtimeout, sc);
2438 /* Add all sysctls under the debug.psm and hw.psm nodes */
2439 static SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2440 static SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2442 SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RWTUN, &verbose, 0,
2445 static int psmhz = 20;
2446 SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2447 "Frequency of the softcallout (in hz)");
2448 static int psmerrsecs = 2;
2449 SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2450 "Number of seconds during which packets will dropped after a sync error");
2451 static int psmerrusecs = 0;
2452 SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2453 "Microseconds to add to psmerrsecs");
2454 static int psmsecs = 0;
2455 SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2456 "Max number of seconds between soft interrupts");
2457 static int psmusecs = 500000;
2458 SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2459 "Microseconds to add to psmsecs");
2460 static int pkterrthresh = 2;
2461 SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2462 "Number of error packets allowed before reinitializing the mouse");
2464 SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RWTUN, &tap_enabled, 0,
2465 "Enable tap and drag gestures");
2466 static int tap_threshold = PSM_TAP_THRESHOLD;
2467 SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2468 "Button tap threshold");
2469 static int tap_timeout = PSM_TAP_TIMEOUT;
2470 SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2471 "Tap timeout for touchpads");
2474 SYSCTL_INT(_hw_psm, OID_AUTO, synaptics_support, CTLFLAG_RDTUN,
2475 &synaptics_support, 0, "Enable support for Synaptics touchpads");
2477 SYSCTL_INT(_hw_psm, OID_AUTO, trackpoint_support, CTLFLAG_RDTUN,
2478 &trackpoint_support, 0, "Enable support for IBM/Lenovo TrackPoint");
2480 SYSCTL_INT(_hw_psm, OID_AUTO, elantech_support, CTLFLAG_RDTUN,
2481 &elantech_support, 0, "Enable support for Elantech touchpads");
2486 struct psm_softc *sc = arg;
2492 /* read until there is nothing to read */
2493 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2494 pb = &sc->pqueue[sc->pqueue_end];
2496 /* discard the byte if the device is not open */
2497 if ((sc->state & PSM_OPEN) == 0)
2500 getmicrouptime(&now);
2501 if ((pb->inputbytes > 0) &&
2502 timevalcmp(&now, &sc->inputtimeout, >)) {
2503 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2504 "resetting byte count\n"));
2509 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2510 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2511 timevaladd(&sc->inputtimeout, &now);
2513 pb->ipacket[pb->inputbytes++] = c;
2515 if (sc->mode.level == PSM_LEVEL_NATIVE) {
2516 VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2521 if (pb->inputbytes < sc->mode.packetsize)
2525 "psmintr: %02x %02x %02x %02x %02x %02x\n",
2526 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2527 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2532 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2533 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2534 sc->flags &= ~PSM_NEED_SYNCBITS;
2536 "psmintr: Sync bytes now %04x,%04x\n",
2537 sc->mode.syncmask[0], sc->mode.syncmask[0]));
2538 } else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2539 VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
2540 "(%04x != %04x) %d cmds since last error.\n",
2541 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2542 sc->cmdcount - sc->lasterr));
2543 sc->lasterr = sc->cmdcount;
2545 * The sync byte test is a weak measure of packet
2546 * validity. Conservatively discard any input yet
2547 * to be seen by userland when we detect a sync
2548 * error since there is a good chance some of
2549 * the queued packets have undetected errors.
2552 if (sc->syncerrors == 0)
2555 sc->lastinputerr = now;
2556 if (sc->syncerrors >= sc->mode.packetsize * 2 ||
2557 sc->pkterrors >= pkterrthresh) {
2559 * If we've failed to find a single sync byte
2560 * in 2 packets worth of data, or we've seen
2561 * persistent packet errors during the
2562 * validation period, reinitialize the mouse
2563 * in hopes of returning it to the expected
2567 "psmintr: reset the mouse.\n"));
2568 reinitialize(sc, TRUE);
2569 } else if (sc->syncerrors == sc->mode.packetsize) {
2571 * Try a soft reset after searching for a sync
2572 * byte through a packet length of bytes.
2575 "psmintr: re-enable the mouse.\n"));
2577 disable_aux_dev(sc->kbdc);
2578 enable_aux_dev(sc->kbdc);
2581 "psmintr: discard a byte (%d)\n",
2584 bcopy(&pb->ipacket[1], &pb->ipacket[0],
2591 * We have what appears to be a valid packet.
2592 * Reset the error counters.
2597 * Drop even good packets if they occur within a timeout
2598 * period of a sync error. This allows the detection of
2599 * a change in the mouse's packet mode without exposing
2600 * erratic mouse behavior to the user. Some KVMs forget
2601 * enhanced mouse modes during switch events.
2603 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
2610 * Now that we're out of the validation period, reset
2611 * the packet error count.
2617 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2620 * If we've filled the queue then call the softintr ourselves,
2621 * otherwise schedule the interrupt for later.
2623 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2624 (sc->pqueue_end == sc->pqueue_start)) {
2625 if ((sc->state & PSM_SOFTARMED) != 0) {
2626 sc->state &= ~PSM_SOFTARMED;
2627 callout_stop(&sc->softcallout);
2630 } else if ((sc->state & PSM_SOFTARMED) == 0) {
2631 sc->state |= PSM_SOFTARMED;
2632 callout_reset(&sc->softcallout,
2633 psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg);
2639 proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2640 int *x, int *y, int *z)
2644 * PS2++ protocol packet
2646 * b7 b6 b5 b4 b3 b2 b1 b0
2647 * byte 1: * 1 p3 p2 1 * * *
2648 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2650 * p3-p0: packet type
2651 * c1, c2: c1 & c2 == 1, if p2 == 0
2652 * c1 & c2 == 0, if p2 == 1
2654 * packet type: 0 (device type)
2655 * See comments in enable_mmanplus() below.
2657 * packet type: 1 (wheel data)
2659 * b7 b6 b5 b4 b3 b2 b1 b0
2660 * byte 3: h * B5 B4 s d2 d1 d0
2662 * h: 1, if horizontal roller data
2663 * 0, if vertical roller data
2664 * B4, B5: button 4 and 5
2666 * d2-d0: roller data
2668 * packet type: 2 (reserved)
2670 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
2671 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2673 * the extended data packet encodes button
2676 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
2678 /* wheel data packet */
2680 if (pb->ipacket[2] & 0x80) {
2681 /* XXX horizontal roller count - ignore it */
2684 /* vertical roller count */
2685 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
2686 (pb->ipacket[2] & 0x0f) - 16 :
2687 (pb->ipacket[2] & 0x0f);
2689 ms->button |= (pb->ipacket[2] &
2690 MOUSE_PS2PLUS_BUTTON4DOWN) ?
2691 MOUSE_BUTTON4DOWN : 0;
2692 ms->button |= (pb->ipacket[2] &
2693 MOUSE_PS2PLUS_BUTTON5DOWN) ?
2694 MOUSE_BUTTON5DOWN : 0;
2698 * this packet type is reserved by
2702 * IBM ScrollPoint Mouse uses this
2703 * packet type to encode both vertical
2704 * and horizontal scroll movement.
2707 /* horizontal count */
2708 if (pb->ipacket[2] & 0x0f)
2709 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
2711 /* vertical count */
2712 if (pb->ipacket[2] & 0xf0)
2713 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
2717 /* device type packet - shouldn't happen */
2721 ms->button = ms->obutton;
2722 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
2723 "type %d: 0x%02x 0x%02x 0x%02x\n",
2724 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2725 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
2729 /* preserve button states */
2730 ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
2735 proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2736 int *x, int *y, int *z)
2738 static int touchpad_buttons;
2739 static int guest_buttons;
2740 static finger_t f[PSM_FINGERS];
2741 int w, id, nfingers, ewcode, extended_buttons;
2743 extended_buttons = 0;
2745 /* TouchPad PS/2 absolute mode message format with capFourButtons:
2747 * Bits: 7 6 5 4 3 2 1 0 (LSB)
2748 * ------------------------------------------------
2749 * ipacket[0]: 1 0 W3 W2 0 W1 R L
2750 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
2751 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
2752 * ipacket[3]: 1 1 Yc Xc 0 W0 D^R U^L
2753 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
2754 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
2757 * L: left physical mouse button
2758 * R: right physical mouse button
2766 * Without capFourButtons but with nExtendeButtons and/or capMiddle
2768 * Bits: 7 6 5 4 3 2 1 0 (LSB)
2769 * ------------------------------------------------------
2770 * ipacket[3]: 1 1 Yc Xc 0 W0 E^R M^L
2771 * ipacket[4]: X7 X6 X5 X4 X3|b7 X2|b5 X1|b3 X0|b1
2772 * ipacket[5]: Y7 Y6 Y5 Y4 Y3|b8 Y2|b6 Y1|b4 Y0|b2
2775 * M: Middle physical mouse button
2776 * E: Extended mouse buttons reported instead of low bits of X and Y
2777 * b1-b8: Extended mouse buttons
2778 * Only ((nExtendedButtons + 1) >> 1) bits are used in packet
2779 * 4 and 5, for reading X and Y value they should be zeroed.
2781 * Absolute reportable limits: 0 - 6143.
2782 * Typical bezel limits: 1472 - 5472.
2783 * Typical edge marings: 1632 - 5312.
2785 * w = 3 Passthrough Packet
2787 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2790 if (!synaptics_support)
2793 /* Sanity check for out of sync packets. */
2794 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2795 (pb->ipacket[3] & 0xc8) != 0xc0)
2799 ms->button = ms->obutton;
2804 * z = 0 No finger contact
2805 * z = 10 Finger hovering near the pad
2806 * z = 30 Very light finger contact
2807 * z = 80 Normal finger contact
2808 * z = 110 Very heavy finger contact
2809 * z = 200 Finger lying flat on pad surface
2810 * z = 255 Maximum reportable Z
2812 *z = pb->ipacket[2];
2815 * Finger width value
2817 * w = 0 Two finger on the pad (capMultiFinger needed)
2818 * w = 1 Three or more fingers (capMultiFinger needed)
2819 * w = 2 Pen (instead of finger) (capPen needed)
2820 * w = 3 Reserved (passthrough?)
2821 * w = 4-7 Finger of normal width (capPalmDetect needed)
2822 * w = 8-14 Very wide finger or palm (capPalmDetect needed)
2823 * w = 15 Maximum reportable width (capPalmDetect needed)
2825 /* XXX Is checking capExtended enough? */
2826 if (sc->synhw.capExtended)
2827 w = ((pb->ipacket[0] & 0x30) >> 2) |
2828 ((pb->ipacket[0] & 0x04) >> 1) |
2829 ((pb->ipacket[3] & 0x04) >> 2);
2831 /* Assume a finger of regular width. */
2838 * Handle packets from the guest device. See:
2839 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
2841 if (sc->synhw.capPassthrough) {
2842 *x = ((pb->ipacket[1] & 0x10) ?
2843 pb->ipacket[4] - 256 : pb->ipacket[4]);
2844 *y = ((pb->ipacket[1] & 0x20) ?
2845 pb->ipacket[5] - 256 : pb->ipacket[5]);
2849 if (pb->ipacket[1] & 0x01)
2850 guest_buttons |= MOUSE_BUTTON1DOWN;
2851 if (pb->ipacket[1] & 0x04)
2852 guest_buttons |= MOUSE_BUTTON2DOWN;
2853 if (pb->ipacket[1] & 0x02)
2854 guest_buttons |= MOUSE_BUTTON3DOWN;
2856 ms->button = touchpad_buttons | guest_buttons |
2857 sc->extended_buttons;
2862 /* Handle Extended W mode packets */
2863 ewcode = (pb->ipacket[5] & 0xf0) >> 4;
2867 /* Secondary finger */
2868 if (sc->synhw.capAdvancedGestures)
2870 .x = (((pb->ipacket[4] & 0x0f) << 8) |
2871 pb->ipacket[1]) << 1,
2872 .y = (((pb->ipacket[4] & 0xf0) << 4) |
2873 pb->ipacket[2]) << 1,
2874 .p = ((pb->ipacket[3] & 0x30) |
2875 (pb->ipacket[5] & 0x0f)) << 1,
2876 .w = PSM_FINGER_DEFAULT_W,
2877 .flags = PSM_FINGER_FUZZY,
2879 else if (sc->synhw.capReportsV)
2881 .x = (((pb->ipacket[4] & 0x0f) << 8) |
2882 (pb->ipacket[1] & 0xfe)) << 1,
2883 .y = (((pb->ipacket[4] & 0xf0) << 4) |
2884 (pb->ipacket[2] & 0xfe)) << 1,
2885 .p = ((pb->ipacket[3] & 0x30) |
2886 (pb->ipacket[5] & 0x0e)) << 1,
2887 .w = (((pb->ipacket[5] & 0x01) << 2) |
2888 ((pb->ipacket[2] & 0x01) << 1) |
2889 (pb->ipacket[1] & 0x01)) + 8,
2890 .flags = PSM_FINGER_FUZZY,
2907 if (sc->syninfo.touchpad_off)
2910 /* Button presses */
2911 touchpad_buttons = 0;
2912 if (pb->ipacket[0] & 0x01)
2913 touchpad_buttons |= MOUSE_BUTTON1DOWN;
2914 if (pb->ipacket[0] & 0x02)
2915 touchpad_buttons |= MOUSE_BUTTON3DOWN;
2917 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2918 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
2919 touchpad_buttons |= MOUSE_BUTTON4DOWN;
2920 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
2921 touchpad_buttons |= MOUSE_BUTTON5DOWN;
2922 } else if (sc->synhw.capExtended && sc->synhw.capMiddle &&
2923 !sc->synhw.capClickPad) {
2925 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
2926 touchpad_buttons |= MOUSE_BUTTON2DOWN;
2927 } else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
2928 /* Extended Buttons */
2929 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
2930 if (sc->syninfo.directional_scrolls) {
2931 if (pb->ipacket[4] & 0x01)
2932 extended_buttons |= MOUSE_BUTTON4DOWN;
2933 if (pb->ipacket[5] & 0x01)
2934 extended_buttons |= MOUSE_BUTTON5DOWN;
2935 if (pb->ipacket[4] & 0x02)
2936 extended_buttons |= MOUSE_BUTTON6DOWN;
2937 if (pb->ipacket[5] & 0x02)
2938 extended_buttons |= MOUSE_BUTTON7DOWN;
2940 if (pb->ipacket[4] & 0x01)
2941 extended_buttons |= MOUSE_BUTTON1DOWN;
2942 if (pb->ipacket[5] & 0x01)
2943 extended_buttons |= MOUSE_BUTTON3DOWN;
2944 if (pb->ipacket[4] & 0x02)
2945 extended_buttons |= MOUSE_BUTTON2DOWN;
2946 sc->extended_buttons = extended_buttons;
2950 * Zero out bits used by extended buttons to avoid
2951 * misinterpretation of the data absolute position.
2953 * The bits represented by
2955 * (nExtendedButtons + 1) >> 1
2957 * will be masked out in both bytes.
2958 * The mask for n bits is computed with the formula
2964 maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
2965 mask = (1 << maskedbits) - 1;
2966 pb->ipacket[4] &= ~(mask);
2967 pb->ipacket[5] &= ~(mask);
2968 } else if (!sc->syninfo.directional_scrolls &&
2969 !sc->gesture.in_vscroll) {
2971 * Keep reporting MOUSE DOWN until we get a new packet
2972 * indicating otherwise.
2974 extended_buttons |= sc->extended_buttons;
2977 /* Handle ClickPad */
2978 if (sc->synhw.capClickPad &&
2979 ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01))
2980 touchpad_buttons |= MOUSE_BUTTON1DOWN;
2982 if (sc->synhw.capReportsV && nfingers > 1)
2984 .x = ((pb->ipacket[3] & 0x10) << 8) |
2985 ((pb->ipacket[1] & 0x0f) << 8) |
2986 (pb->ipacket[4] & 0xfd),
2987 .y = ((pb->ipacket[3] & 0x20) << 7) |
2988 ((pb->ipacket[1] & 0xf0) << 4) |
2989 (pb->ipacket[5] & 0xfd),
2991 .w = (((pb->ipacket[2] & 0x01) << 2) |
2992 (pb->ipacket[5] & 0x02) |
2993 ((pb->ipacket[4] & 0x02) >> 1)) + 8,
2994 .flags = PSM_FINGER_FUZZY,
2998 .x = ((pb->ipacket[3] & 0x10) << 8) |
2999 ((pb->ipacket[1] & 0x0f) << 8) |
3001 .y = ((pb->ipacket[3] & 0x20) << 7) |
3002 ((pb->ipacket[1] & 0xf0) << 4) |
3006 .flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0,
3009 /* Ignore hovering and unmeasurable touches */
3010 if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2)
3013 for (id = 0; id < PSM_FINGERS; id++)
3015 PSM_FINGER_RESET(f[id]);
3017 ms->button = touchpad_buttons;
3019 /* Palm detection doesn't terminate the current action. */
3020 if (!psmpalmdetect(sc, &f[0], nfingers)) {
3021 psmgestures(sc, &f[0], nfingers, ms);
3022 for (id = 0; id < PSM_FINGERS; id++)
3023 psmsmoother(sc, &f[id], id, ms, x, y);
3025 VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f[0].w));
3028 ms->button |= extended_buttons | guest_buttons;
3032 * Use the extra buttons as a scrollwheel
3034 * XXX X.Org uses the Z axis for vertical wheel only,
3035 * whereas moused(8) understands special values to differ
3036 * vertical and horizontal wheels.
3038 * xf86-input-mouse needs therefore a small patch to
3039 * understand these special values. Without it, the
3040 * horizontal wheel acts as a vertical wheel in X.Org.
3042 * That's why the horizontal wheel is disabled by
3045 if (ms->button & MOUSE_BUTTON4DOWN)
3047 else if (ms->button & MOUSE_BUTTON5DOWN)
3049 else if (ms->button & MOUSE_BUTTON6DOWN)
3051 else if (ms->button & MOUSE_BUTTON7DOWN)
3055 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
3056 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
3062 psmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers)
3065 ((sc->synhw.capMultiFinger ||
3066 sc->synhw.capAdvancedGestures) && nfingers > 1) ||
3067 (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) ||
3068 (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) ||
3069 (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) {
3071 * We consider the packet irrelevant for the current
3073 * - the width isn't comprised in:
3075 * - the pressure isn't comprised in:
3076 * [min_pressure; max_pressure]
3077 * - pen aren't supported but PSM_FINGER_IS_PEN is set
3085 psmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers,
3088 smoother_t *smoother;
3091 int y_ok, center_button, center_x, right_button, right_x, i;
3094 smoother = &sc->smoother[0];
3095 gest = &sc->gesture;
3097 /* Find first active finger. */
3099 for (i = 0; i < PSM_FINGERS; i++) {
3100 if (PSM_FINGER_IS_SET(fingers[i])) {
3102 smoother = &sc->smoother[i];
3109 * Check pressure to detect a real wanted action on the
3112 if (f->p >= sc->syninfo.min_pressure) {
3115 int start_x, start_y;
3117 int margin_top, margin_right, margin_bottom, margin_left;
3118 int window_min, window_max;
3119 int vscroll_hor_area, vscroll_ver_area;
3120 int two_finger_scroll;
3124 /* XXX Verify values? */
3125 margin_top = sc->syninfo.margin_top;
3126 margin_right = sc->syninfo.margin_right;
3127 margin_bottom = sc->syninfo.margin_bottom;
3128 margin_left = sc->syninfo.margin_left;
3129 window_min = sc->syninfo.window_min;
3130 window_max = sc->syninfo.window_max;
3131 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
3132 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
3133 two_finger_scroll = sc->syninfo.two_finger_scroll;
3134 max_x = sc->syninfo.max_x;
3135 max_y = sc->syninfo.max_y;
3137 /* Read current absolute position. */
3142 * Limit the coordinates to the specified margins because
3143 * this area isn't very reliable.
3145 if (x0 <= margin_left)
3147 else if (x0 >= max_x - margin_right)
3148 x0 = max_x - margin_right;
3149 if (y0 <= margin_bottom)
3151 else if (y0 >= max_y - margin_top)
3152 y0 = max_y - margin_top;
3154 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
3155 x0, y0, f->p, f->w));
3158 * If the action is just beginning, init the structure and
3159 * compute tap timeout.
3161 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
3162 VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
3164 /* Initialize queue. */
3165 gest->window_min = window_min;
3167 /* Reset pressure peak. */
3170 /* Reset fingers count. */
3171 gest->fingers_nb = 0;
3173 /* Reset virtual scrolling state. */
3174 gest->in_vscroll = 0;
3176 /* Compute tap timeout. */
3177 gest->taptimeout.tv_sec = tap_timeout / 1000000;
3178 gest->taptimeout.tv_usec = tap_timeout % 1000000;
3179 timevaladd(&gest->taptimeout, &sc->lastsoftintr);
3181 sc->flags |= PSM_FLAGS_FINGERDOWN;
3183 /* Smoother has not been reset yet */
3188 queue_len = smoother->queue_len + 1;
3189 start_x = smoother->start_x;
3190 start_y = smoother->start_y;
3193 /* Process ClickPad softbuttons */
3194 if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) {
3195 y_ok = sc->syninfo.softbuttons_y >= 0 ?
3196 start_y < sc->syninfo.softbuttons_y :
3197 start_y > max_y + sc->syninfo.softbuttons_y;
3199 center_button = MOUSE_BUTTON2DOWN;
3200 center_x = sc->syninfo.softbutton2_x;
3201 right_button = MOUSE_BUTTON3DOWN;
3202 right_x = sc->syninfo.softbutton3_x;
3204 if (center_x > 0 && right_x > 0 && center_x > right_x) {
3205 center_button = MOUSE_BUTTON3DOWN;
3206 center_x = sc->syninfo.softbutton3_x;
3207 right_button = MOUSE_BUTTON2DOWN;
3208 right_x = sc->syninfo.softbutton2_x;
3211 if (right_x > 0 && start_x > right_x && y_ok)
3212 ms->button = (ms->button &
3213 ~MOUSE_BUTTON1DOWN) | right_button;
3214 else if (center_x > 0 && start_x > center_x && y_ok)
3215 ms->button = (ms->button &
3216 ~MOUSE_BUTTON1DOWN) | center_button;
3219 /* If in tap-hold, add the recorded button. */
3220 if (gest->in_taphold)
3221 ms->button |= gest->tap_button;
3224 * For tap, we keep the maximum number of fingers and the
3225 * pressure peak. Also with multiple fingers, we increase
3226 * the minimum window.
3229 gest->window_min = window_max;
3230 gest->fingers_nb = imax(nfingers, gest->fingers_nb);
3231 gest->zmax = imax(f->p, gest->zmax);
3233 /* Do we have enough packets to consider this a gesture? */
3234 if (queue_len < gest->window_min)
3237 /* Is a scrolling action occurring? */
3238 if (!gest->in_taphold && !ms->button &&
3239 (!gest->in_vscroll || two_finger_scroll)) {
3241 * A scrolling action must not conflict with a tap
3242 * action. Here are the conditions to consider a
3244 * - the action in a configurable area
3245 * - one of the following:
3246 * . the distance between the last packet and the
3247 * first should be above a configurable minimum
3250 dxp = abs(x0 - start_x);
3251 dyp = abs(y0 - start_y);
3253 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) ||
3254 dxp >= sc->syninfo.vscroll_min_delta ||
3255 dyp >= sc->syninfo.vscroll_min_delta) {
3257 * Handle two finger scrolling.
3258 * Note that we don't rely on fingers_nb
3259 * as that keeps the maximum number of fingers.
3261 if (two_finger_scroll) {
3262 if (nfingers == 2) {
3269 /* Check for horizontal scrolling. */
3270 if ((vscroll_hor_area > 0 &&
3271 start_y <= vscroll_hor_area) ||
3272 (vscroll_hor_area < 0 &&
3274 max_y + vscroll_hor_area))
3275 gest->in_vscroll += 2;
3277 /* Check for vertical scrolling. */
3278 if ((vscroll_ver_area > 0 &&
3279 start_x <= vscroll_ver_area) ||
3280 (vscroll_ver_area < 0 &&
3282 max_x + vscroll_ver_area))
3283 gest->in_vscroll += 1;
3286 /* Avoid conflicts if area overlaps. */
3287 if (gest->in_vscroll >= 3)
3289 (dxp > dyp) ? 2 : 1;
3293 * Reset two finger scrolling when the number of fingers
3294 * is different from two or any button is pressed.
3296 if (two_finger_scroll && gest->in_vscroll != 0 &&
3297 (nfingers != 2 || ms->button))
3298 gest->in_vscroll = 0;
3301 "synaptics: virtual scrolling: %s "
3302 "(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n",
3303 gest->in_vscroll ? "YES" : "NO",
3304 gest->in_vscroll, dxp, dyp,
3307 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3309 * An action is currently taking place but the pressure
3310 * dropped under the minimum, putting an end to it.
3312 int taphold_timeout, dx, dy, tap_max_delta;
3314 dx = abs(smoother->queue[smoother->queue_cursor].x -
3316 dy = abs(smoother->queue[smoother->queue_cursor].y -
3319 /* Max delta is disabled for multi-fingers tap. */
3320 if (gest->fingers_nb > 1)
3321 tap_max_delta = imax(dx, dy);
3323 tap_max_delta = sc->syninfo.tap_max_delta;
3325 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3327 /* Check for tap. */
3329 "synaptics: zmax=%d, dx=%d, dy=%d, "
3330 "delta=%d, fingers=%d, queue=%d\n",
3331 gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb,
3332 smoother->queue_len));
3333 if (!gest->in_vscroll && gest->zmax >= tap_threshold &&
3334 timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) &&
3335 dx <= tap_max_delta && dy <= tap_max_delta &&
3336 smoother->queue_len >= sc->syninfo.tap_min_queue) {
3339 * - the maximum pressure went over tap_threshold
3340 * - the action ended before tap_timeout
3342 * To handle tap-hold, we must delay any button push to
3345 if (gest->in_taphold) {
3347 * This is the second and last tap of a
3348 * double tap action, not a tap-hold.
3350 gest->in_taphold = 0;
3353 * For double-tap to work:
3354 * - no button press is emitted (to
3355 * simulate a button release)
3356 * - PSM_FLAGS_FINGERDOWN is set to
3357 * force the next packet to emit a
3361 "synaptics: button RELEASE: %d\n",
3363 sc->flags |= PSM_FLAGS_FINGERDOWN;
3365 /* Schedule button press on next interrupt */
3366 sc->idletimeout.tv_sec = psmhz > 1 ?
3368 sc->idletimeout.tv_usec = psmhz > 1 ?
3369 1000000 / psmhz : 0;
3372 * This is the first tap: we set the
3373 * tap-hold state and notify the button
3376 gest->in_taphold = 1;
3377 taphold_timeout = sc->syninfo.taphold_timeout;
3378 gest->taptimeout.tv_sec = taphold_timeout /
3380 gest->taptimeout.tv_usec = taphold_timeout %
3382 sc->idletimeout = gest->taptimeout;
3383 timevaladd(&gest->taptimeout,
3386 switch (gest->fingers_nb) {
3400 "synaptics: button PRESS: %d\n",
3402 ms->button |= gest->tap_button;
3406 * Not enough pressure or timeout: reset
3409 if (gest->in_taphold) {
3411 "synaptics: button RELEASE: %d\n",
3413 gest->in_taphold = 0;
3416 "synaptics: not a tap-hold\n"));
3419 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) {
3421 * For a tap-hold to work, the button must remain down at
3422 * least until timeout (where the in_taphold flags will be
3423 * cleared) or during the next action.
3425 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) {
3426 ms->button |= gest->tap_button;
3428 VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n",
3430 gest->in_taphold = 0;
3438 psmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id,
3439 mousestatus_t *ms, int *x, int *y)
3441 smoother_t *smoother = &sc->smoother[smoother_id];
3442 gesture_t *gest = &(sc->gesture);
3445 * Check pressure to detect a real wanted action on the
3448 if (f->p >= sc->syninfo.min_pressure) {
3450 int cursor, peer, window;
3451 int dx, dy, dxp, dyp;
3452 int max_width, max_pressure;
3453 int margin_top, margin_right, margin_bottom, margin_left;
3454 int na_top, na_right, na_bottom, na_left;
3455 int window_min, window_max;
3457 int weight_current, weight_previous, weight_len_squared;
3458 int div_min, div_max, div_len;
3459 int vscroll_hor_area, vscroll_ver_area;
3460 int two_finger_scroll;
3462 int len, weight_prev_x, weight_prev_y;
3463 int div_max_x, div_max_y, div_x, div_y;
3467 /* XXX Verify values? */
3468 max_width = sc->syninfo.max_width;
3469 max_pressure = sc->syninfo.max_pressure;
3470 margin_top = sc->syninfo.margin_top;
3471 margin_right = sc->syninfo.margin_right;
3472 margin_bottom = sc->syninfo.margin_bottom;
3473 margin_left = sc->syninfo.margin_left;
3474 na_top = sc->syninfo.na_top;
3475 na_right = sc->syninfo.na_right;
3476 na_bottom = sc->syninfo.na_bottom;
3477 na_left = sc->syninfo.na_left;
3478 window_min = sc->syninfo.window_min;
3479 window_max = sc->syninfo.window_max;
3480 multiplicator = sc->syninfo.multiplicator;
3481 weight_current = sc->syninfo.weight_current;
3482 weight_previous = sc->syninfo.weight_previous;
3483 weight_len_squared = sc->syninfo.weight_len_squared;
3484 div_min = sc->syninfo.div_min;
3485 div_max = sc->syninfo.div_max;
3486 div_len = sc->syninfo.div_len;
3487 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
3488 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
3489 two_finger_scroll = sc->syninfo.two_finger_scroll;
3490 max_x = sc->syninfo.max_x;
3491 max_y = sc->syninfo.max_y;
3493 is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0;
3495 /* Read current absolute position. */
3500 * Limit the coordinates to the specified margins because
3501 * this area isn't very reliable.
3503 if (x0 <= margin_left)
3505 else if (x0 >= max_x - margin_right)
3506 x0 = max_x - margin_right;
3507 if (y0 <= margin_bottom)
3509 else if (y0 >= max_y - margin_top)
3510 y0 = max_y - margin_top;
3512 /* If the action is just beginning, init the structure. */
3513 if (smoother->active == 0) {
3514 VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id));
3516 /* Store the first point of this action. */
3517 smoother->start_x = x0;
3518 smoother->start_y = y0;
3521 /* Initialize queue. */
3522 smoother->queue_cursor = SYNAPTICS_PACKETQUEUE;
3523 smoother->queue_len = 0;
3525 /* Reset average. */
3526 smoother->avg_dx = 0;
3527 smoother->avg_dy = 0;
3529 /* Reset squelch. */
3530 smoother->squelch_x = 0;
3531 smoother->squelch_y = 0;
3533 /* Activate queue */
3534 smoother->active = 1;
3536 /* Calculate the current delta. */
3537 cursor = smoother->queue_cursor;
3538 dx = x0 - smoother->queue[cursor].x;
3539 dy = y0 - smoother->queue[cursor].y;
3542 VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n",
3543 smoother_id, x0, y0, f->p, f->w));
3545 /* Queue this new packet. */
3546 cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1);
3547 smoother->queue[cursor].x = x0;
3548 smoother->queue[cursor].y = y0;
3549 smoother->queue_cursor = cursor;
3550 if (smoother->queue_len < SYNAPTICS_PACKETQUEUE)
3551 smoother->queue_len++;
3553 "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
3554 smoother_id, cursor, x0, y0, dx, dy));
3556 /* Do we have enough packets to consider this a movement? */
3557 if (smoother->queue_len < gest->window_min)
3560 weight_prev_x = weight_prev_y = weight_previous;
3561 div_max_x = div_max_y = div_max;
3563 if (gest->in_vscroll) {
3564 /* Dividers are different with virtual scrolling. */
3565 div_min = sc->syninfo.vscroll_div_min;
3566 div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
3569 * There's a lot of noise in coordinates when
3570 * the finger is on the touchpad's borders. When
3571 * using this area, we apply a special weight and
3574 if (x0 <= na_left || x0 >= max_x - na_right) {
3575 weight_prev_x = sc->syninfo.weight_previous_na;
3576 div_max_x = sc->syninfo.div_max_na;
3579 if (y0 <= na_bottom || y0 >= max_y - na_top) {
3580 weight_prev_y = sc->syninfo.weight_previous_na;
3581 div_max_y = sc->syninfo.div_max_na;
3586 * Calculate weights for the average operands and
3587 * the divisor. Both depend on the distance between
3588 * the current packet and a previous one (based on the
3591 window = imin(smoother->queue_len, window_max);
3592 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
3593 dxp = abs(x0 - smoother->queue[peer].x) + 1;
3594 dyp = abs(y0 - smoother->queue[peer].y) + 1;
3595 len = (dxp * dxp) + (dyp * dyp);
3596 weight_prev_x = imin(weight_prev_x,
3597 weight_len_squared * weight_prev_x / len);
3598 weight_prev_y = imin(weight_prev_y,
3599 weight_len_squared * weight_prev_y / len);
3601 len = (dxp + dyp) / 2;
3602 div_x = div_len * div_max_x / len;
3603 div_x = imin(div_max_x, div_x);
3604 div_x = imax(div_min, div_x);
3605 div_y = div_len * div_max_y / len;
3606 div_y = imin(div_max_y, div_y);
3607 div_y = imax(div_min, div_y);
3610 "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
3611 smoother_id, peer, len, weight_prev_x, weight_prev_y,
3614 /* Compute averages. */
3616 (weight_current * dx * multiplicator +
3617 weight_prev_x * smoother->avg_dx) /
3618 (weight_current + weight_prev_x);
3621 (weight_current * dy * multiplicator +
3622 weight_prev_y * smoother->avg_dy) /
3623 (weight_current + weight_prev_y);
3626 "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id,
3627 smoother->avg_dx / multiplicator,
3628 smoother->avg_dy / multiplicator));
3630 /* Use these averages to calculate x & y. */
3631 smoother->squelch_x += smoother->avg_dx;
3632 dxp = smoother->squelch_x / (div_x * multiplicator);
3633 smoother->squelch_x = smoother->squelch_x %
3634 (div_x * multiplicator);
3636 smoother->squelch_y += smoother->avg_dy;
3637 dyp = smoother->squelch_y / (div_y * multiplicator);
3638 smoother->squelch_y = smoother->squelch_y %
3639 (div_y * multiplicator);
3641 switch(gest->in_vscroll) {
3642 case 0: /* Pointer movement. */
3643 /* On real<->fuzzy finger switch the x/y pos jumps */
3644 if (is_fuzzy == smoother->is_fuzzy) {
3649 VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n",
3650 smoother_id, dx, dy, dxp, dyp));
3652 case 1: /* Vertical scrolling. */
3654 ms->button |= (dyp > 0) ?
3655 MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN;
3657 case 2: /* Horizontal scrolling. */
3659 ms->button |= (dxp > 0) ?
3660 MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN;
3664 smoother->is_fuzzy = is_fuzzy;
3668 * Deactivate queue. Note: We can not just reset queue here
3669 * as these values are still used by gesture processor.
3670 * So postpone reset till next touch.
3672 smoother->active = 0;
3677 proc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3678 int *x, int *y, int *z)
3680 static int touchpad_button, trackpoint_button;
3681 finger_t fn, f[ELANTECH_MAX_FINGERS];
3682 int pkt, id, scale, i, nfingers, mask;
3684 if (!elantech_support)
3687 /* Determine packet format and do a sanity check for out of sync packets. */
3688 if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion))
3689 pkt = ELANTECH_PKT_NOP;
3690 else if (ELANTECH_PKT_IS_TRACKPOINT(pb))
3691 pkt = ELANTECH_PKT_TRACKPOINT;
3693 switch (sc->elanhw.hwversion) {
3695 if (!ELANTECH_PKT_IS_V2(pb))
3698 pkt = (pb->ipacket[0] & 0xc0) == 0x80 ?
3699 ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON;
3702 if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) &&
3703 !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc))
3706 pkt = ELANTECH_PKT_V3;
3709 if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc))
3712 switch (pb->ipacket[3] & 0x03) {
3714 pkt = ELANTECH_PKT_V4_STATUS;
3717 pkt = ELANTECH_PKT_V4_HEAD;
3720 pkt = ELANTECH_PKT_V4_MOTION;
3730 VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt));
3732 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
3733 PSM_FINGER_RESET(f[id]);
3736 ms->button = ms->obutton;
3738 if (sc->syninfo.touchpad_off)
3742 * L: Left mouse button pressed
3743 * R: Right mouse button pressed
3744 * N: number of fingers on touchpad
3745 * X: absolute x value (horizontal)
3746 * Y: absolute y value (vertical)
3747 * W; width of the finger touch
3751 case ELANTECH_PKT_V2_COMMON: /* HW V2. One/Three finger touch */
3752 /* 7 6 5 4 3 2 1 0 (LSB)
3753 * -------------------------------------------
3754 * ipacket[0]: N1 N0 W3 W2 . . R L
3755 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
3756 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
3757 * ipacket[3]: N4 VF W1 W0 . . . B2
3758 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
3759 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3760 * -------------------------------------------
3761 * N4: set if more than 3 fingers (only in 3 fingers mode)
3762 * VF: a kind of flag? (only on EF123, 0 when finger
3763 * is over one of the buttons, 1 otherwise)
3764 * B2: (on EF113 only, 0 otherwise), one button pressed
3765 * P & W is not reported on EF113 touchpads
3767 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
3768 if (nfingers == 3 && (pb->ipacket[3] & 0x80))
3770 mask = (1 << nfingers) - 1;
3772 fn = ELANTECH_FINGER_SET_XYP(pb);
3773 if (sc->elanhw.haspressure) {
3774 fn.w = ((pb->ipacket[0] & 0x30) >> 2) |
3775 ((pb->ipacket[3] & 0x30) >> 4);
3777 fn.p = PSM_FINGER_DEFAULT_P;
3778 fn.w = PSM_FINGER_DEFAULT_W;
3782 * HW v2 dont report exact finger positions when 3 or more
3783 * fingers are on touchpad. Use reported value as fingers
3784 * position as it is required for tap detection
3787 fn.flags = PSM_FINGER_FUZZY;
3789 for (id = 0; id < imin(nfingers, ELANTECH_MAX_FINGERS); id++)
3793 case ELANTECH_PKT_V2_2FINGER: /*HW V2. Two finger touch */
3794 /* 7 6 5 4 3 2 1 0 (LSB)
3795 * -------------------------------------------
3796 * ipacket[0]: N1 N0 AY8 AX8 . . R L
3797 * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0
3798 * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0
3799 * ipacket[3]: . . BY8 BX8 . . . .
3800 * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0
3801 * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0
3802 * -------------------------------------------
3803 * AX: lower-left finger absolute x value
3804 * AY: lower-left finger absolute y value
3805 * BX: upper-right finger absolute x value
3806 * BY: upper-right finger absolute y value
3809 mask = (1 << nfingers) - 1;
3811 for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++)
3812 f[id] = (finger_t) {
3813 .x = (((pb->ipacket[id * 3] & 0x10) << 4) |
3814 pb->ipacket[id * 3 + 1]) << 2,
3815 .y = (((pb->ipacket[id * 3] & 0x20) << 3) |
3816 pb->ipacket[id * 3 + 2]) << 2,
3817 .p = PSM_FINGER_DEFAULT_P,
3818 .w = PSM_FINGER_DEFAULT_W,
3819 /* HW ver.2 sends bounding box */
3820 .flags = PSM_FINGER_FUZZY
3824 case ELANTECH_PKT_V3: /* HW Version 3 */
3825 /* 7 6 5 4 3 2 1 0 (LSB)
3826 * -------------------------------------------
3827 * ipacket[0]: N1 N0 W3 W2 0 1 R L
3828 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
3829 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
3830 * ipacket[3]: 0 0 W1 W0 0 0 1 0
3831 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
3832 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3833 * -------------------------------------------
3835 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
3836 mask = (1 << nfingers) - 1;
3839 fn = ELANTECH_FINGER_SET_XYP(pb);
3840 fn.w = ((pb->ipacket[0] & 0x30) >> 2) |
3841 ((pb->ipacket[3] & 0x30) >> 4);
3844 * HW v3 dont report exact finger positions when 3 or more
3845 * fingers are on touchpad. Use reported value as fingers
3846 * position as it is required for tap detection
3849 fn.flags = PSM_FINGER_FUZZY;
3851 for (id = 0; id < imin(nfingers, ELANTECH_MAX_FINGERS); id++)
3854 if (nfingers == 2) {
3855 if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) {
3856 sc->elanaction.fingers[0] = fn;
3859 f[0] = sc->elanaction.fingers[0];
3863 case ELANTECH_PKT_V4_STATUS: /* HW Version 4. Status packet */
3864 /* 7 6 5 4 3 2 1 0 (LSB)
3865 * -------------------------------------------
3866 * ipacket[0]: . . . . 0 1 R L
3867 * ipacket[1]: . . . F4 F3 F2 F1 F0
3868 * ipacket[2]: . . . . . . . .
3869 * ipacket[3]: . . . 1 0 0 0 0
3870 * ipacket[4]: PL . . . . . . .
3871 * ipacket[5]: . . . . . . . .
3872 * -------------------------------------------
3873 * Fn: finger n is on touchpad
3875 * HV ver4 sends a status packet to indicate that the numbers
3876 * or identities of the fingers has been changed
3879 mask = pb->ipacket[1] & 0x1f;
3880 nfingers = bitcount(mask);
3882 /* Skip "new finger is on touchpad" packets */
3883 if ((sc->elanaction.mask & mask) == sc->elanaction.mask &&
3884 (mask & ~sc->elanaction.mask)) {
3885 sc->elanaction.mask = mask;
3891 case ELANTECH_PKT_V4_HEAD: /* HW Version 4. Head packet */
3892 /* 7 6 5 4 3 2 1 0 (LSB)
3893 * -------------------------------------------
3894 * ipacket[0]: W3 W2 W1 W0 0 1 R L
3895 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
3896 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
3897 * ipacket[3]: ID2 ID1 ID0 1 0 0 0 1
3898 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
3899 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3900 * -------------------------------------------
3902 * HW ver 4 sends head packets in two cases:
3903 * 1. One finger touch and movement.
3904 * 2. Next after status packet to tell new finger positions.
3906 mask = sc->elanaction.mask;
3907 nfingers = bitcount(mask);
3908 id = ((pb->ipacket[3] & 0xe0) >> 5) - 1;
3910 if (id >= 0 && id < ELANTECH_MAX_FINGERS) {
3911 f[id] = ELANTECH_FINGER_SET_XYP(pb);
3912 f[id].w = (pb->ipacket[0] & 0xf0) >> 4;
3916 case ELANTECH_PKT_V4_MOTION: /* HW Version 4. Motion packet */
3917 /* 7 6 5 4 3 2 1 0 (LSB)
3918 * -------------------------------------------
3919 * ipacket[0]: ID2 ID1 ID0 OF 0 1 R L
3920 * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
3921 * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
3922 * ipacket[3]: ID2 ID1 ID0 1 0 0 1 0
3923 * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
3924 * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
3925 * -------------------------------------------
3926 * OF: delta overflows (> 127 or < -128), in this case
3927 * firmware sends us (delta x / 5) and (delta y / 5)
3929 * DX: delta x (two's complement)
3930 * XY: delta y (two's complement)
3931 * byte 0 ~ 2 for one finger
3932 * byte 3 ~ 5 for another finger
3934 mask = sc->elanaction.mask;
3935 nfingers = bitcount(mask);
3937 scale = (pb->ipacket[0] & 0x10) ? 5 : 1;
3938 for (i = 0; i <= 3; i += 3) {
3939 id = ((pb->ipacket[i] & 0xe0) >> 5) - 1;
3940 if (id < 0 || id >= ELANTECH_MAX_FINGERS)
3943 if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) {
3944 f[id] = sc->elanaction.fingers[id];
3945 f[id].x += imax(-f[id].x,
3946 (signed char)pb->ipacket[i+1] * scale);
3947 f[id].y += imax(-f[id].y,
3948 (signed char)pb->ipacket[i+2] * scale);
3950 VLOG(3, (LOG_DEBUG, "elantech: "
3951 "HW v4 motion packet skipped\n"));
3957 case ELANTECH_PKT_TRACKPOINT:
3958 /* 7 6 5 4 3 2 1 0 (LSB)
3959 * -------------------------------------------
3960 * ipacket[0]: 0 0 SX SY 0 M R L
3961 * ipacket[1]: ~SX 0 0 0 0 0 0 0
3962 * ipacket[2]: ~SY 0 0 0 0 0 0 0
3963 * ipacket[3]: 0 0 ~SY ~SX 0 1 1 0
3964 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
3965 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3966 * -------------------------------------------
3967 * X and Y are written in two's complement spread
3968 * over 9 bits with SX/SY the relative top bit and
3969 * X7..X0 and Y7..Y0 the lower bits.
3971 *x = (pb->ipacket[0] & 0x20) ?
3972 pb->ipacket[4] - 256 : pb->ipacket[4];
3973 *y = (pb->ipacket[0] & 0x10) ?
3974 pb->ipacket[5] - 256 : pb->ipacket[5];
3977 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
3978 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) |
3979 ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0);
3981 ms->button = touchpad_button | trackpoint_button;
3984 case ELANTECH_PKT_NOP:
3991 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
3992 if (PSM_FINGER_IS_SET(f[id]))
3993 VLOG(2, (LOG_DEBUG, "elantech: "
3994 "finger %d: down [%d, %d], %d, %d, %d\n", id + 1,
3995 f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags));
3997 /* Touchpad button presses */
3998 if (sc->elanhw.isclickpad) {
4000 ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0);
4003 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4004 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0);
4007 ms->button = touchpad_button | trackpoint_button;
4009 /* Palm detection doesn't terminate the current action. */
4010 if (!psmpalmdetect(sc, &f[0], nfingers)) {
4011 /* Send finger 1 position to gesture processor */
4012 if (PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) ||
4014 psmgestures(sc, &f[0], imin(nfingers, 3), ms);
4015 /* Send fingers positions to movement smoothers */
4016 for (id = 0; id < PSM_FINGERS; id++)
4017 if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id)))
4018 psmsmoother(sc, &f[id], id, ms, x, y);
4020 VLOG(2, (LOG_DEBUG, "elantech: palm detected! (%d)\n",
4024 /* Store current finger positions in action context */
4025 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4026 if (PSM_FINGER_IS_SET(f[id]))
4027 sc->elanaction.fingers[id] = f[id];
4028 if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id)))
4029 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
4031 sc->elanaction.mask = mask;
4033 /* Use the extra buttons as a scrollwheel */
4034 if (ms->button & MOUSE_BUTTON4DOWN)
4036 else if (ms->button & MOUSE_BUTTON5DOWN)
4038 else if (ms->button & MOUSE_BUTTON6DOWN)
4040 else if (ms->button & MOUSE_BUTTON7DOWN)
4044 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
4045 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
4051 proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4052 int *x, int *y, int *z)
4054 static int butmap_versapad[8] = {
4060 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4062 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
4066 /* VersaPad PS/2 absolute mode message format
4068 * [packet1] 7 6 5 4 3 2 1 0(LSB)
4069 * ipacket[0]: 1 1 0 A 1 L T R
4070 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
4071 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
4072 * ipacket[3]: 1 1 1 A 1 L T R
4073 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
4074 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
4077 * R: right physical mouse button (1=on)
4078 * T: touch pad virtual button (1=tapping)
4079 * L: left physical mouse button (1=on)
4080 * A: position data is valid (1=valid)
4081 * H: horizontal data (12bit signed integer. H11 is sign bit.)
4082 * V: vertical data (12bit signed integer. V11 is sign bit.)
4085 * Tapping is mapped to MOUSE_BUTTON4.
4089 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
4090 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
4091 if (c & MOUSE_PS2VERSA_IN_USE) {
4092 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
4093 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
4098 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
4101 if (*x < 0) /* XXX */
4110 sc->flags |= PSM_FLAGS_FINGERDOWN;
4114 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
4118 psmsoftintridle(void *arg)
4120 struct psm_softc *sc = arg;
4123 /* Invoke soft handler only when pqueue is empty. Otherwise it will be
4124 * invoked from psmintr soon with pqueue filled with real data */
4125 if (sc->pqueue_start == sc->pqueue_end &&
4126 sc->idlepacket.inputbytes > 0) {
4127 /* Grow circular queue backwards to avoid race with psmintr */
4128 if (--sc->pqueue_start < 0)
4129 sc->pqueue_start = PSM_PACKETQUEUE - 1;
4131 pb = &sc->pqueue[sc->pqueue_start];
4132 memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t));
4134 "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n",
4135 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
4136 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
4143 psmsoftintr(void *arg)
4146 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
4147 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
4149 static int butmap[8] = {
4153 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4155 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
4156 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
4157 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
4159 struct psm_softc *sc = arg;
4162 int x, y, z, c, l, s;
4164 getmicrouptime(&sc->lastsoftintr);
4169 pb = &sc->pqueue[sc->pqueue_start];
4171 if (sc->mode.level == PSM_LEVEL_NATIVE)
4176 * A kludge for Kensington device!
4177 * The MSB of the horizontal count appears to be stored in
4180 if (sc->hw.model == MOUSE_MODEL_THINK)
4181 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
4183 /* ignore the overflow bits... */
4184 x = (c & MOUSE_PS2_XNEG) ?
4185 pb->ipacket[1] - 256 : pb->ipacket[1];
4186 y = (c & MOUSE_PS2_YNEG) ?
4187 pb->ipacket[2] - 256 : pb->ipacket[2];
4189 ms.obutton = sc->button; /* previous button state */
4190 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
4191 /* `tapping' action */
4192 if (sc->config & PSM_CONFIG_FORCETAP)
4193 ms.button |= ((c & MOUSE_PS2_TAP)) ?
4194 0 : MOUSE_BUTTON4DOWN;
4195 timevalclear(&sc->idletimeout);
4196 sc->idlepacket.inputbytes = 0;
4198 switch (sc->hw.model) {
4200 case MOUSE_MODEL_EXPLORER:
4202 * b7 b6 b5 b4 b3 b2 b1 b0
4203 * byte 1: oy ox sy sx 1 M R L
4204 * byte 2: x x x x x x x x
4205 * byte 3: y y y y y y y y
4206 * byte 4: * * S2 S1 s d2 d1 d0
4208 * L, M, R, S1, S2: left, middle, right and side buttons
4209 * s: wheel data sign bit
4212 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
4213 (pb->ipacket[3] & 0x0f) - 16 :
4214 (pb->ipacket[3] & 0x0f);
4216 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
4217 MOUSE_BUTTON4DOWN : 0;
4219 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
4220 MOUSE_BUTTON5DOWN : 0;
4223 case MOUSE_MODEL_INTELLI:
4224 case MOUSE_MODEL_NET:
4225 /* wheel data is in the fourth byte */
4226 z = (char)pb->ipacket[3];
4228 * XXX some mice may send 7 when there is no Z movement? */
4229 if ((z >= 7) || (z <= -7))
4231 /* some compatible mice have additional buttons */
4232 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
4233 MOUSE_BUTTON4DOWN : 0;
4234 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
4235 MOUSE_BUTTON5DOWN : 0;
4238 case MOUSE_MODEL_MOUSEMANPLUS:
4239 proc_mmanplus(sc, pb, &ms, &x, &y, &z);
4242 case MOUSE_MODEL_GLIDEPOINT:
4243 /* `tapping' action */
4244 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
4248 case MOUSE_MODEL_NETSCROLL:
4250 * three additional bytes encode buttons and
4253 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
4254 MOUSE_BUTTON4DOWN : 0;
4255 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
4256 MOUSE_BUTTON5DOWN : 0;
4257 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
4258 pb->ipacket[4] - 256 : pb->ipacket[4];
4261 case MOUSE_MODEL_THINK:
4262 /* the fourth button state in the first byte */
4263 ms.button |= (c & MOUSE_PS2_TAP) ?
4264 MOUSE_BUTTON4DOWN : 0;
4267 case MOUSE_MODEL_VERSAPAD:
4268 proc_versapad(sc, pb, &ms, &x, &y, &z);
4269 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
4270 ((y < 0) ? MOUSE_PS2_YNEG : 0);
4273 case MOUSE_MODEL_4D:
4275 * b7 b6 b5 b4 b3 b2 b1 b0
4276 * byte 1: s2 d2 s1 d1 1 M R L
4277 * byte 2: sx x x x x x x x
4278 * byte 3: sy y y y y y y y
4280 * s1: wheel 1 direction
4282 * s2: wheel 2 direction
4285 x = (pb->ipacket[1] & 0x80) ?
4286 pb->ipacket[1] - 256 : pb->ipacket[1];
4287 y = (pb->ipacket[2] & 0x80) ?
4288 pb->ipacket[2] - 256 : pb->ipacket[2];
4289 switch (c & MOUSE_4D_WHEELBITS) {
4296 case 0x40: /* XXX 2nd wheel turning right */
4299 case 0xc0: /* XXX 2nd wheel turning left */
4305 case MOUSE_MODEL_4DPLUS:
4306 if ((x < 16 - 256) && (y < 16 - 256)) {
4308 * b7 b6 b5 b4 b3 b2 b1 b0
4309 * byte 1: 0 0 1 1 1 M R L
4310 * byte 2: 0 0 0 0 1 0 0 0
4311 * byte 3: 0 0 0 0 S s d1 d0
4313 * L, M, R, S: left, middle, right,
4315 * s: wheel data sign bit
4319 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
4320 ms.button |= MOUSE_BUTTON4DOWN;
4321 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
4322 ((pb->ipacket[2] & 0x07) - 8) :
4323 (pb->ipacket[2] & 0x07) ;
4325 /* preserve previous button states */
4326 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
4330 case MOUSE_MODEL_SYNAPTICS:
4331 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0)
4335 case MOUSE_MODEL_ELANTECH:
4336 if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0)
4340 case MOUSE_MODEL_TRACKPOINT:
4341 case MOUSE_MODEL_GENERIC:
4347 if (sc->mode.accelfactor >= 1) {
4349 x = x * x / sc->mode.accelfactor;
4352 if (c & MOUSE_PS2_XNEG)
4356 y = y * y / sc->mode.accelfactor;
4359 if (c & MOUSE_PS2_YNEG)
4364 /* Store last packet for reinjection if it has not been set already */
4365 if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0)
4366 sc->idlepacket = *pb;
4371 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
4372 (ms.obutton ^ ms.button);
4374 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
4376 sc->status.flags |= ms.flags;
4377 sc->status.dx += ms.dx;
4378 sc->status.dy += ms.dy;
4379 sc->status.dz += ms.dz;
4380 sc->status.button = ms.button;
4381 sc->button = ms.button;
4384 sc->watchdog = FALSE;
4387 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
4388 l = imin(pb->inputbytes,
4389 sizeof(sc->queue.buf) - sc->queue.tail);
4390 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
4391 if (pb->inputbytes > l)
4392 bcopy(&pb->ipacket[l], &sc->queue.buf[0],
4393 pb->inputbytes - l);
4394 sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
4395 sizeof(sc->queue.buf);
4396 sc->queue.count += pb->inputbytes;
4401 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
4402 sc->pqueue_start = 0;
4403 } while (sc->pqueue_start != sc->pqueue_end);
4405 if (sc->state & PSM_ASLP) {
4406 sc->state &= ~PSM_ASLP;
4409 selwakeuppri(&sc->rsel, PZERO);
4410 if (sc->async != NULL) {
4411 pgsigio(&sc->async, SIGIO, 0);
4413 sc->state &= ~PSM_SOFTARMED;
4415 /* schedule injection of predefined packet after idletimeout
4416 * if no data packets have been received from psmintr */
4417 if (timevalisset(&sc->idletimeout)) {
4418 sc->state |= PSM_SOFTARMED;
4419 callout_reset(&sc->softcallout, tvtohz(&sc->idletimeout),
4420 psmsoftintridle, sc);
4421 VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n",
4422 tvtohz(&sc->idletimeout)));
4428 psmpoll(struct cdev *dev, int events, struct thread *td)
4430 struct psm_softc *sc = dev->si_drv1;
4434 /* Return true if a mouse event available */
4436 if (events & (POLLIN | POLLRDNORM)) {
4437 if (sc->queue.count > 0)
4438 revents |= events & (POLLIN | POLLRDNORM);
4440 selrecord(td, &sc->rsel);
4447 /* vendor/model specific routines */
4449 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
4451 if (set_mouse_resolution(kbdc, res) != res)
4453 if (set_mouse_scaling(kbdc, scale) &&
4454 set_mouse_scaling(kbdc, scale) &&
4455 set_mouse_scaling(kbdc, scale) &&
4456 (get_mouse_status(kbdc, status, 0, 3) >= 3))
4462 mouse_ext_command(KBDC kbdc, int command)
4466 c = (command >> 6) & 0x03;
4467 if (set_mouse_resolution(kbdc, c) != c)
4469 c = (command >> 4) & 0x03;
4470 if (set_mouse_resolution(kbdc, c) != c)
4472 c = (command >> 2) & 0x03;
4473 if (set_mouse_resolution(kbdc, c) != c)
4475 c = (command >> 0) & 0x03;
4476 if (set_mouse_resolution(kbdc, c) != c)
4482 /* Logitech MouseMan Cordless II */
4484 enable_lcordless(struct psm_softc *sc, enum probearg arg)
4486 KBDC kbdc = sc->kbdc;
4490 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status))
4492 if (status[1] == PSMD_RES_HIGH)
4494 ch = (status[0] & 0x07) - 1; /* channel # */
4495 if ((ch <= 0) || (ch > 4))
4498 * status[1]: always one?
4499 * status[2]: battery status? (0-100)
4505 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
4507 enable_groller(struct psm_softc *sc, enum probearg arg)
4509 KBDC kbdc = sc->kbdc;
4513 * The special sequence to enable the fourth button and the
4514 * roller. Immediately after this sequence check status bytes.
4515 * if the mouse is NetScroll, the second and the third bytes are
4520 * If the mouse is an ordinary PS/2 mouse, the status bytes should
4521 * look like the following.
4523 * byte 1 bit 7 always 0
4524 * bit 6 stream mode (0)
4525 * bit 5 disabled (0)
4526 * bit 4 1:1 scaling (0)
4528 * bit 0-2 button status
4529 * byte 2 resolution (PSMD_RES_HIGH)
4530 * byte 3 report rate (?)
4533 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
4535 if ((status[1] != '3') || (status[2] != 'D'))
4537 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
4543 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
4545 enable_gmouse(struct psm_softc *sc, enum probearg arg)
4547 KBDC kbdc = sc->kbdc;
4551 * The special sequence to enable the middle, "rubber" button.
4552 * Immediately after this sequence check status bytes.
4553 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
4554 * the second and the third bytes are '3' and 'U'.
4555 * NOTE: NetMouse reports that it has three buttons although it has
4556 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
4557 * say they have three buttons too and they do have a button on the
4560 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
4562 if ((status[1] != '3') || (status[2] != 'U'))
4567 /* ALPS GlidePoint */
4569 enable_aglide(struct psm_softc *sc, enum probearg arg)
4571 KBDC kbdc = sc->kbdc;
4575 * The special sequence to obtain ALPS GlidePoint specific
4576 * information. Immediately after this sequence, status bytes will
4577 * contain something interesting.
4578 * NOTE: ALPS produces several models of GlidePoint. Some of those
4579 * do not respond to this sequence, thus, cannot be detected this way.
4581 if (set_mouse_sampling_rate(kbdc, 100) != 100)
4583 if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
4585 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
4590 /* Kensington ThinkingMouse/Trackball */
4592 enable_kmouse(struct psm_softc *sc, enum probearg arg)
4594 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
4595 KBDC kbdc = sc->kbdc;
4601 id1 = get_aux_id(kbdc);
4602 if (set_mouse_sampling_rate(kbdc, 10) != 10)
4605 * The device is now in the native mode? It returns a different
4608 id2 = get_aux_id(kbdc);
4609 if ((id1 == id2) || (id2 != 2))
4612 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
4615 /* at this point, resolution is LOW, sampling rate is 10/sec */
4616 if (get_mouse_status(kbdc, status, 0, 3) < 3)
4621 * The special sequence to enable the third and fourth buttons.
4622 * Otherwise they behave like the first and second buttons.
4624 for (i = 0; i < nitems(rate); ++i)
4625 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4629 * At this point, the device is using default resolution and
4630 * sampling rate for the native mode.
4632 if (get_mouse_status(kbdc, status, 0, 3) < 3)
4634 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
4637 /* the device appears be enabled by this sequence, diable it for now */
4638 disable_aux_dev(kbdc);
4639 empty_aux_buffer(kbdc, 5);
4644 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
4646 enable_mmanplus(struct psm_softc *sc, enum probearg arg)
4648 KBDC kbdc = sc->kbdc;
4651 /* the special sequence to enable the fourth button and the roller. */
4653 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
4654 * must be called exactly three times since the last RESET command
4655 * before this sequence. XXX
4657 if (!set_mouse_scaling(kbdc, 1))
4659 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
4661 if (get_mouse_status(kbdc, data, 1, 3) < 3)
4665 * PS2++ protocol, packet type 0
4667 * b7 b6 b5 b4 b3 b2 b1 b0
4668 * byte 1: * 1 p3 p2 1 * * *
4669 * byte 2: 1 1 p1 p0 m1 m0 1 0
4670 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
4672 * p3-p0: packet type: 0
4673 * m7-m0: model ID: MouseMan+:0x50,
4675 * ScrollPoint:0x58...
4677 /* check constant bits */
4678 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
4680 if ((data[1] & 0xc3) != 0xc2)
4682 /* check d3-d0 in byte 2 */
4683 if (!MOUSE_PS2PLUS_CHECKBITS(data))
4686 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
4690 sc->hw.hwid &= 0x00ff;
4691 sc->hw.hwid |= data[2] << 8; /* save model ID */
4695 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
4696 * the wheel and the fourth button events are encoded in the
4697 * special data packet. The mouse may be put in the IntelliMouse mode
4698 * if it is initialized by the IntelliMouse's method.
4703 /* MS IntelliMouse Explorer */
4705 enable_msexplorer(struct psm_softc *sc, enum probearg arg)
4707 KBDC kbdc = sc->kbdc;
4708 static u_char rate0[] = { 200, 100, 80, };
4709 static u_char rate1[] = { 200, 200, 80, };
4714 * This is needed for at least A4Tech X-7xx mice - they do not go
4715 * straight to Explorer mode, but need to be set to Intelli mode
4718 enable_msintelli(sc, arg);
4720 /* the special sequence to enable the extra buttons and the roller. */
4721 for (i = 0; i < nitems(rate1); ++i)
4722 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
4724 /* the device will give the genuine ID only after the above sequence */
4725 id = get_aux_id(kbdc);
4726 if (id != PSM_EXPLORER_ID)
4730 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
4735 * XXX: this is a kludge to fool some KVM switch products
4736 * which think they are clever enough to know the 4-byte IntelliMouse
4737 * protocol, and assume any other protocols use 3-byte packets.
4738 * They don't convey 4-byte data packets from the IntelliMouse Explorer
4739 * correctly to the host computer because of this!
4740 * The following sequence is actually IntelliMouse's "wake up"
4741 * sequence; it will make the KVM think the mouse is IntelliMouse
4742 * when it is in fact IntelliMouse Explorer.
4744 for (i = 0; i < nitems(rate0); ++i)
4745 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
4754 * Logitech MouseMan+ and FirstMouse+ will also respond to this
4755 * probe routine and act like IntelliMouse.
4758 enable_msintelli(struct psm_softc *sc, enum probearg arg)
4760 KBDC kbdc = sc->kbdc;
4761 static u_char rate[] = { 200, 100, 80, };
4765 /* the special sequence to enable the third button and the roller. */
4766 for (i = 0; i < nitems(rate); ++i)
4767 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4769 /* the device will give the genuine ID only after the above sequence */
4770 id = get_aux_id(kbdc);
4771 if (id != PSM_INTELLI_ID)
4784 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
4787 enable_4dmouse(struct psm_softc *sc, enum probearg arg)
4789 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
4790 KBDC kbdc = sc->kbdc;
4794 for (i = 0; i < nitems(rate); ++i)
4795 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4797 id = get_aux_id(kbdc);
4799 * WinEasy 4D, 4 Way Scroll 4D: 6
4800 * Cable-Free 4D: 8 (4DPLUS)
4801 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
4803 if (id != PSM_4DMOUSE_ID)
4807 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
4816 * Newer wheel mice from A4 Tech seem to use this protocol.
4817 * Older models are recognized as either 4D Mouse or IntelliMouse.
4820 enable_4dplus(struct psm_softc *sc, enum probearg arg)
4822 KBDC kbdc = sc->kbdc;
4826 * enable_4dmouse() already issued the following ID sequence...
4827 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
4830 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
4831 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4835 id = get_aux_id(kbdc);
4839 case PSM_4DPLUS_RFSW35_ID:
4846 sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
4853 /* Synaptics Touchpad */
4855 synaptics_sysctl(SYSCTL_HANDLER_ARGS)
4857 struct psm_softc *sc;
4860 if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 ||
4861 oidp->oid_arg2 > SYNAPTICS_SYSCTL_SOFTBUTTON3_X)
4864 sc = oidp->oid_arg1;
4866 /* Read the current value. */
4867 arg = *(int *)((char *)sc + oidp->oid_arg2);
4868 error = sysctl_handle_int(oidp, &arg, 0, req);
4871 if (error || !req->newptr)
4875 * Check that the new value is in the concerned node's range
4878 switch (oidp->oid_arg2) {
4879 case SYNAPTICS_SYSCTL_MIN_PRESSURE:
4880 case SYNAPTICS_SYSCTL_MAX_PRESSURE:
4881 if (arg < 0 || arg > 255)
4884 case SYNAPTICS_SYSCTL_MAX_WIDTH:
4885 if (arg < 4 || arg > 15)
4888 case SYNAPTICS_SYSCTL_MARGIN_TOP:
4889 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
4890 case SYNAPTICS_SYSCTL_NA_TOP:
4891 case SYNAPTICS_SYSCTL_NA_BOTTOM:
4892 if (arg < 0 || arg > sc->synhw.maximumYCoord)
4895 case SYNAPTICS_SYSCTL_SOFTBUTTON2_X:
4896 case SYNAPTICS_SYSCTL_SOFTBUTTON3_X:
4897 /* Softbuttons is clickpad only feature */
4898 if (!sc->synhw.capClickPad && arg != 0)
4901 case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
4902 case SYNAPTICS_SYSCTL_MARGIN_LEFT:
4903 case SYNAPTICS_SYSCTL_NA_RIGHT:
4904 case SYNAPTICS_SYSCTL_NA_LEFT:
4905 if (arg < 0 || arg > sc->synhw.maximumXCoord)
4908 case SYNAPTICS_SYSCTL_WINDOW_MIN:
4909 case SYNAPTICS_SYSCTL_WINDOW_MAX:
4910 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
4911 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
4914 case SYNAPTICS_SYSCTL_MULTIPLICATOR:
4915 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
4916 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
4917 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
4918 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
4919 case SYNAPTICS_SYSCTL_DIV_MIN:
4920 case SYNAPTICS_SYSCTL_DIV_MAX:
4921 case SYNAPTICS_SYSCTL_DIV_MAX_NA:
4922 case SYNAPTICS_SYSCTL_DIV_LEN:
4923 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
4924 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
4928 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
4929 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
4930 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
4934 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
4935 if (arg < -sc->synhw.maximumXCoord ||
4936 arg > sc->synhw.maximumXCoord)
4939 case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y:
4940 /* Softbuttons is clickpad only feature */
4941 if (!sc->synhw.capClickPad && arg != 0)
4944 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
4945 if (arg < -sc->synhw.maximumYCoord ||
4946 arg > sc->synhw.maximumYCoord)
4949 case SYNAPTICS_SYSCTL_TOUCHPAD_OFF:
4950 if (arg < 0 || arg > 1)
4958 *(int *)((char *)sc + oidp->oid_arg2) = arg;
4964 synaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc)
4967 * Set predefined sizes for softbuttons.
4968 * Values are taken to match HP Pavilion dv6 clickpad drawings
4969 * with thin middle softbutton placed on separator
4972 /* hw.psm.synaptics.softbuttons_y */
4973 sc->syninfo.softbuttons_y = 1700;
4974 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4975 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4976 "softbuttons_y", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4977 sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y,
4978 synaptics_sysctl, "I",
4979 "Vertical size of softbuttons area");
4981 /* hw.psm.synaptics.softbutton2_x */
4982 sc->syninfo.softbutton2_x = 3100;
4983 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4984 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4985 "softbutton2_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4986 sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X,
4987 synaptics_sysctl, "I",
4988 "Horisontal position of 2-nd softbutton left edge (0-disable)");
4990 /* hw.psm.synaptics.softbutton3_x */
4991 sc->syninfo.softbutton3_x = 3900;
4992 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4993 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4994 "softbutton3_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4995 sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X,
4996 synaptics_sysctl, "I",
4997 "Horisontal position of 3-rd softbutton left edge (0-disable)");
5001 synaptics_sysctl_create_tree(struct psm_softc *sc, const char *name,
5005 if (sc->syninfo.sysctl_tree != NULL)
5008 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
5009 sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
5010 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
5011 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name, CTLFLAG_RD,
5014 /* hw.psm.synaptics.directional_scrolls. */
5015 sc->syninfo.directional_scrolls = 0;
5016 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5017 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5018 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
5019 &sc->syninfo.directional_scrolls, 0,
5020 "Enable hardware scrolling pad (if non-zero) or register it as "
5021 "extended buttons (if 0)");
5023 /* hw.psm.synaptics.max_x. */
5024 sc->syninfo.max_x = 6143;
5025 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5026 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5027 "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY,
5028 &sc->syninfo.max_x, 0,
5029 "Horizontal reporting range");
5031 /* hw.psm.synaptics.max_y. */
5032 sc->syninfo.max_y = 6143;
5033 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5034 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5035 "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY,
5036 &sc->syninfo.max_y, 0,
5037 "Vertical reporting range");
5040 * Turn off two finger scroll if we have a
5041 * physical area reserved for scrolling or when
5042 * there's no multi finger support.
5044 if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 &&
5045 sc->synhw.capAdvancedGestures == 0))
5046 sc->syninfo.two_finger_scroll = 0;
5048 sc->syninfo.two_finger_scroll = 1;
5049 /* hw.psm.synaptics.two_finger_scroll. */
5050 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5051 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5052 "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY,
5053 &sc->syninfo.two_finger_scroll, 0,
5054 "Enable two finger scrolling");
5056 /* hw.psm.synaptics.min_pressure. */
5057 sc->syninfo.min_pressure = 16;
5058 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5059 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5060 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5061 sc, SYNAPTICS_SYSCTL_MIN_PRESSURE,
5062 synaptics_sysctl, "I",
5063 "Minimum pressure required to start an action");
5065 /* hw.psm.synaptics.max_pressure. */
5066 sc->syninfo.max_pressure = 220;
5067 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5068 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5069 "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5070 sc, SYNAPTICS_SYSCTL_MAX_PRESSURE,
5071 synaptics_sysctl, "I",
5072 "Maximum pressure to detect palm");
5074 /* hw.psm.synaptics.max_width. */
5075 sc->syninfo.max_width = 10;
5076 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5077 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5078 "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5079 sc, SYNAPTICS_SYSCTL_MAX_WIDTH,
5080 synaptics_sysctl, "I",
5081 "Maximum finger width to detect palm");
5083 /* hw.psm.synaptics.top_margin. */
5084 sc->syninfo.margin_top = 200;
5085 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5086 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5087 "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5088 sc, SYNAPTICS_SYSCTL_MARGIN_TOP,
5089 synaptics_sysctl, "I",
5092 /* hw.psm.synaptics.right_margin. */
5093 sc->syninfo.margin_right = 200;
5094 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5095 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5096 "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5097 sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
5098 synaptics_sysctl, "I",
5101 /* hw.psm.synaptics.bottom_margin. */
5102 sc->syninfo.margin_bottom = 200;
5103 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5104 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5105 "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5106 sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
5107 synaptics_sysctl, "I",
5110 /* hw.psm.synaptics.left_margin. */
5111 sc->syninfo.margin_left = 200;
5112 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5113 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5114 "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5115 sc, SYNAPTICS_SYSCTL_MARGIN_LEFT,
5116 synaptics_sysctl, "I",
5119 /* hw.psm.synaptics.na_top. */
5120 sc->syninfo.na_top = 1783;
5121 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5122 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5123 "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5124 sc, SYNAPTICS_SYSCTL_NA_TOP,
5125 synaptics_sysctl, "I",
5126 "Top noisy area, where weight_previous_na is used instead "
5127 "of weight_previous");
5129 /* hw.psm.synaptics.na_right. */
5130 sc->syninfo.na_right = 563;
5131 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5132 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5133 "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5134 sc, SYNAPTICS_SYSCTL_NA_RIGHT,
5135 synaptics_sysctl, "I",
5136 "Right noisy area, where weight_previous_na is used instead "
5137 "of weight_previous");
5139 /* hw.psm.synaptics.na_bottom. */
5140 sc->syninfo.na_bottom = 1408;
5141 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5142 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5143 "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5144 sc, SYNAPTICS_SYSCTL_NA_BOTTOM,
5145 synaptics_sysctl, "I",
5146 "Bottom noisy area, where weight_previous_na is used instead "
5147 "of weight_previous");
5149 /* hw.psm.synaptics.na_left. */
5150 sc->syninfo.na_left = 1600;
5151 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5152 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5153 "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5154 sc, SYNAPTICS_SYSCTL_NA_LEFT,
5155 synaptics_sysctl, "I",
5156 "Left noisy area, where weight_previous_na is used instead "
5157 "of weight_previous");
5159 /* hw.psm.synaptics.window_min. */
5160 sc->syninfo.window_min = 4;
5161 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5162 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5163 "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5164 sc, SYNAPTICS_SYSCTL_WINDOW_MIN,
5165 synaptics_sysctl, "I",
5166 "Minimum window size to start an action");
5168 /* hw.psm.synaptics.window_max. */
5169 sc->syninfo.window_max = 10;
5170 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5171 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5172 "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5173 sc, SYNAPTICS_SYSCTL_WINDOW_MAX,
5174 synaptics_sysctl, "I",
5175 "Maximum window size");
5177 /* hw.psm.synaptics.multiplicator. */
5178 sc->syninfo.multiplicator = 10000;
5179 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5180 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5181 "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5182 sc, SYNAPTICS_SYSCTL_MULTIPLICATOR,
5183 synaptics_sysctl, "I",
5184 "Multiplicator to increase precision in averages and divisions");
5186 /* hw.psm.synaptics.weight_current. */
5187 sc->syninfo.weight_current = 3;
5188 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5189 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5190 "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5191 sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
5192 synaptics_sysctl, "I",
5193 "Weight of the current movement in the new average");
5195 /* hw.psm.synaptics.weight_previous. */
5196 sc->syninfo.weight_previous = 6;
5197 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5198 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5199 "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5200 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
5201 synaptics_sysctl, "I",
5202 "Weight of the previous average");
5204 /* hw.psm.synaptics.weight_previous_na. */
5205 sc->syninfo.weight_previous_na = 20;
5206 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5207 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5208 "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5209 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
5210 synaptics_sysctl, "I",
5211 "Weight of the previous average (inside the noisy area)");
5213 /* hw.psm.synaptics.weight_len_squared. */
5214 sc->syninfo.weight_len_squared = 2000;
5215 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5216 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5217 "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5218 sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
5219 synaptics_sysctl, "I",
5220 "Length (squared) of segments where weight_previous "
5221 "starts to decrease");
5223 /* hw.psm.synaptics.div_min. */
5224 sc->syninfo.div_min = 9;
5225 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5226 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5227 "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5228 sc, SYNAPTICS_SYSCTL_DIV_MIN,
5229 synaptics_sysctl, "I",
5230 "Divisor for fast movements");
5232 /* hw.psm.synaptics.div_max. */
5233 sc->syninfo.div_max = 17;
5234 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5235 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5236 "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5237 sc, SYNAPTICS_SYSCTL_DIV_MAX,
5238 synaptics_sysctl, "I",
5239 "Divisor for slow movements");
5241 /* hw.psm.synaptics.div_max_na. */
5242 sc->syninfo.div_max_na = 30;
5243 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5244 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5245 "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5246 sc, SYNAPTICS_SYSCTL_DIV_MAX_NA,
5247 synaptics_sysctl, "I",
5248 "Divisor with slow movements (inside the noisy area)");
5250 /* hw.psm.synaptics.div_len. */
5251 sc->syninfo.div_len = 100;
5252 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5253 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5254 "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5255 sc, SYNAPTICS_SYSCTL_DIV_LEN,
5256 synaptics_sysctl, "I",
5257 "Length of segments where div_max starts to decrease");
5259 /* hw.psm.synaptics.tap_max_delta. */
5260 sc->syninfo.tap_max_delta = 80;
5261 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5262 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5263 "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5264 sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
5265 synaptics_sysctl, "I",
5266 "Length of segments above which a tap is ignored");
5268 /* hw.psm.synaptics.tap_min_queue. */
5269 sc->syninfo.tap_min_queue = 2;
5270 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5271 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5272 "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5273 sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
5274 synaptics_sysctl, "I",
5275 "Number of packets required to consider a tap");
5277 /* hw.psm.synaptics.taphold_timeout. */
5278 sc->gesture.in_taphold = 0;
5279 sc->syninfo.taphold_timeout = tap_timeout;
5280 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5281 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5282 "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5283 sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
5284 synaptics_sysctl, "I",
5285 "Maximum elapsed time between two taps to consider a tap-hold "
5288 /* hw.psm.synaptics.vscroll_hor_area. */
5289 sc->syninfo.vscroll_hor_area = 0; /* 1300 */
5290 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5291 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5292 "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5293 sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
5294 synaptics_sysctl, "I",
5295 "Area reserved for horizontal virtual scrolling");
5297 /* hw.psm.synaptics.vscroll_ver_area. */
5298 sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right;
5299 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5300 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5301 "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5302 sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
5303 synaptics_sysctl, "I",
5304 "Area reserved for vertical virtual scrolling");
5306 /* hw.psm.synaptics.vscroll_min_delta. */
5307 sc->syninfo.vscroll_min_delta = 50;
5308 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5309 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5310 "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5311 sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
5312 synaptics_sysctl, "I",
5313 "Minimum movement to consider virtual scrolling");
5315 /* hw.psm.synaptics.vscroll_div_min. */
5316 sc->syninfo.vscroll_div_min = 100;
5317 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5318 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5319 "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5320 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
5321 synaptics_sysctl, "I",
5322 "Divisor for fast scrolling");
5324 /* hw.psm.synaptics.vscroll_div_min. */
5325 sc->syninfo.vscroll_div_max = 150;
5326 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5327 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5328 "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5329 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
5330 synaptics_sysctl, "I",
5331 "Divisor for slow scrolling");
5333 /* hw.psm.synaptics.touchpad_off. */
5334 sc->syninfo.touchpad_off = 0;
5335 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5336 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5337 "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5338 sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF,
5339 synaptics_sysctl, "I",
5340 "Turn off touchpad");
5342 sc->syninfo.softbuttons_y = 0;
5343 sc->syninfo.softbutton2_x = 0;
5344 sc->syninfo.softbutton3_x = 0;
5346 /* skip softbuttons sysctl on not clickpads */
5347 if (sc->synhw.capClickPad)
5348 synaptics_sysctl_create_softbuttons_tree(sc);
5352 synaptics_preferred_mode(struct psm_softc *sc) {
5355 /* Check if we are in relative mode */
5356 if (sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
5357 if (tap_enabled == 0)
5359 * Disable tap & drag gestures. We use a Mode Byte
5360 * and set the DisGest bit (see §2.5 of Synaptics
5361 * TouchPad Interfacing Guide).
5366 * Enable tap & drag gestures. We use a Mode Byte
5367 * and clear the DisGest bit (see §2.5 of Synaptics
5368 * TouchPad Interfacing Guide).
5375 /* request wmode where available */
5376 if (sc->synhw.capExtended)
5383 synaptics_set_mode(struct psm_softc *sc, int mode_byte) {
5384 mouse_ext_command(sc->kbdc, mode_byte);
5386 /* "Commit" the Set Mode Byte command sent above. */
5387 set_mouse_sampling_rate(sc->kbdc, 20);
5390 * Enable advanced gestures mode if supported and we are not entering
5391 * passthrough or relative mode.
5393 if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) &&
5394 sc->hw.model == MOUSE_MODEL_SYNAPTICS && !(mode_byte & (1 << 5))) {
5395 mouse_ext_command(sc->kbdc, 3);
5396 set_mouse_sampling_rate(sc->kbdc, 0xc8);
5401 enable_synaptics(struct psm_softc *sc, enum probearg arg)
5403 KBDC kbdc = sc->kbdc;
5404 synapticshw_t synhw;
5408 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
5411 * Just to be on the safe side: this avoids troubles with
5412 * following mouse_ext_command() when the previous command
5413 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
5414 * Synaptics Touchpad behaviour.
5416 set_mouse_scaling(kbdc, 1);
5418 /* Identify the Touchpad version. */
5419 if (mouse_ext_command(kbdc, 0) == 0)
5421 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5423 if (status[1] != 0x47)
5426 bzero(&synhw, sizeof(synhw));
5427 synhw.infoMinor = status[0];
5428 synhw.infoMajor = status[2] & 0x0f;
5431 printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
5434 if (synhw.infoMajor < 4) {
5435 printf(" Unsupported (pre-v4) Touchpad detected\n");
5439 /* Get the Touchpad model information. */
5440 if (mouse_ext_command(kbdc, 3) == 0)
5442 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5444 if ((status[1] & 0x01) != 0) {
5445 printf(" Failed to read model information\n");
5449 synhw.infoRot180 = (status[0] & 0x80) != 0;
5450 synhw.infoPortrait = (status[0] & 0x40) != 0;
5451 synhw.infoSensor = status[0] & 0x3f;
5452 synhw.infoHardware = (status[1] & 0xfe) >> 1;
5453 synhw.infoNewAbs = (status[2] & 0x80) != 0;
5454 synhw.capPen = (status[2] & 0x40) != 0;
5455 synhw.infoSimplC = (status[2] & 0x20) != 0;
5456 synhw.infoGeometry = status[2] & 0x0f;
5459 printf(" Model information:\n");
5460 printf(" infoRot180: %d\n", synhw.infoRot180);
5461 printf(" infoPortrait: %d\n", synhw.infoPortrait);
5462 printf(" infoSensor: %d\n", synhw.infoSensor);
5463 printf(" infoHardware: %d\n", synhw.infoHardware);
5464 printf(" infoNewAbs: %d\n", synhw.infoNewAbs);
5465 printf(" capPen: %d\n", synhw.capPen);
5466 printf(" infoSimplC: %d\n", synhw.infoSimplC);
5467 printf(" infoGeometry: %d\n", synhw.infoGeometry);
5470 /* Read the extended capability bits. */
5471 if (mouse_ext_command(kbdc, 2) == 0)
5473 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5475 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
5476 printf(" Failed to read extended capability bits\n");
5480 /* Set the different capabilities when they exist. */
5482 synhw.capExtended = (status[0] & 0x80) != 0;
5483 if (synhw.capExtended) {
5484 synhw.nExtendedQueries = (status[0] & 0x70) >> 4;
5485 synhw.capMiddle = (status[0] & 0x04) != 0;
5486 synhw.capPassthrough = (status[2] & 0x80) != 0;
5487 synhw.capLowPower = (status[2] & 0x40) != 0;
5488 synhw.capMultiFingerReport =
5489 (status[2] & 0x20) != 0;
5490 synhw.capSleep = (status[2] & 0x10) != 0;
5491 synhw.capFourButtons = (status[2] & 0x08) != 0;
5492 synhw.capBallistics = (status[2] & 0x04) != 0;
5493 synhw.capMultiFinger = (status[2] & 0x02) != 0;
5494 synhw.capPalmDetect = (status[2] & 0x01) != 0;
5496 if (!set_mouse_scaling(kbdc, 1))
5498 if (mouse_ext_command(kbdc, 0x08) == 0)
5500 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5503 synhw.infoXupmm = status[0];
5504 synhw.infoYupmm = status[2];
5507 printf(" Extended capabilities:\n");
5508 printf(" capExtended: %d\n", synhw.capExtended);
5509 printf(" capMiddle: %d\n", synhw.capMiddle);
5510 printf(" nExtendedQueries: %d\n",
5511 synhw.nExtendedQueries);
5512 printf(" capPassthrough: %d\n", synhw.capPassthrough);
5513 printf(" capLowPower: %d\n", synhw.capLowPower);
5514 printf(" capMultiFingerReport: %d\n",
5515 synhw.capMultiFingerReport);
5516 printf(" capSleep: %d\n", synhw.capSleep);
5517 printf(" capFourButtons: %d\n", synhw.capFourButtons);
5518 printf(" capBallistics: %d\n", synhw.capBallistics);
5519 printf(" capMultiFinger: %d\n", synhw.capMultiFinger);
5520 printf(" capPalmDetect: %d\n", synhw.capPalmDetect);
5521 printf(" infoXupmm: %d\n", synhw.infoXupmm);
5522 printf(" infoYupmm: %d\n", synhw.infoYupmm);
5526 * If nExtendedQueries is 1 or greater, then the TouchPad
5527 * supports this number of extended queries. We can load
5528 * more information about buttons using query 0x09.
5530 if (synhw.nExtendedQueries >= 1) {
5531 if (!set_mouse_scaling(kbdc, 1))
5533 if (mouse_ext_command(kbdc, 0x09) == 0)
5535 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5537 synhw.verticalScroll = (status[0] & 0x01) != 0;
5538 synhw.horizontalScroll = (status[0] & 0x02) != 0;
5539 synhw.verticalWheel = (status[0] & 0x08) != 0;
5540 synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
5541 synhw.capEWmode = (status[0] & 0x04) != 0;
5543 printf(" Extended model ID:\n");
5544 printf(" verticalScroll: %d\n",
5545 synhw.verticalScroll);
5546 printf(" horizontalScroll: %d\n",
5547 synhw.horizontalScroll);
5548 printf(" verticalWheel: %d\n",
5549 synhw.verticalWheel);
5550 printf(" nExtendedButtons: %d\n",
5551 synhw.nExtendedButtons);
5552 printf(" capEWmode: %d\n",
5556 * Add the number of extended buttons to the total
5557 * button support count, including the middle button
5558 * if capMiddle support bit is set.
5560 buttons = synhw.nExtendedButtons + synhw.capMiddle;
5563 * If the capFourButtons support bit is set,
5564 * add a fourth button to the total button count.
5566 buttons = synhw.capFourButtons ? 1 : 0;
5568 /* Read the continued capabilities bits. */
5569 if (synhw.nExtendedQueries >= 4) {
5570 if (!set_mouse_scaling(kbdc, 1))
5572 if (mouse_ext_command(kbdc, 0x0c) == 0)
5574 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5577 synhw.capClickPad = (status[1] & 0x01) << 1;
5578 synhw.capClickPad |= (status[0] & 0x10) != 0;
5579 synhw.capDeluxeLEDs = (status[1] & 0x02) != 0;
5580 synhw.noAbsoluteFilter = (status[1] & 0x04) != 0;
5581 synhw.capReportsV = (status[1] & 0x08) != 0;
5582 synhw.capUniformClickPad = (status[1] & 0x10) != 0;
5583 synhw.capReportsMin = (status[1] & 0x20) != 0;
5584 synhw.capInterTouch = (status[1] & 0x40) != 0;
5585 synhw.capReportsMax = (status[0] & 0x02) != 0;
5586 synhw.capClearPad = (status[0] & 0x04) != 0;
5587 synhw.capAdvancedGestures = (status[0] & 0x08) != 0;
5588 synhw.capCoveredPad = (status[0] & 0x80) != 0;
5590 if (synhw.capReportsMax) {
5591 if (!set_mouse_scaling(kbdc, 1))
5593 if (mouse_ext_command(kbdc, 0x0d) == 0)
5595 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5598 synhw.maximumXCoord = (status[0] << 5) |
5599 ((status[1] & 0x0f) << 1);
5600 synhw.maximumYCoord = (status[2] << 5) |
5601 ((status[1] & 0xf0) >> 3);
5604 * Typical bezel limits. Taken from 'Synaptics
5605 * PS/2 * TouchPad Interfacing Guide' p.3.2.3.
5607 synhw.maximumXCoord = 5472;
5608 synhw.maximumYCoord = 4448;
5611 if (synhw.capReportsMin) {
5612 if (!set_mouse_scaling(kbdc, 1))
5614 if (mouse_ext_command(kbdc, 0x0f) == 0)
5616 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5619 synhw.minimumXCoord = (status[0] << 5) |
5620 ((status[1] & 0x0f) << 1);
5621 synhw.minimumYCoord = (status[2] << 5) |
5622 ((status[1] & 0xf0) >> 3);
5625 * Typical bezel limits. Taken from 'Synaptics
5626 * PS/2 * TouchPad Interfacing Guide' p.3.2.3.
5628 synhw.minimumXCoord = 1472;
5629 synhw.minimumYCoord = 1408;
5633 printf(" Continued capabilities:\n");
5634 printf(" capClickPad: %d\n",
5636 printf(" capDeluxeLEDs: %d\n",
5637 synhw.capDeluxeLEDs);
5638 printf(" noAbsoluteFilter: %d\n",
5639 synhw.noAbsoluteFilter);
5640 printf(" capReportsV: %d\n",
5642 printf(" capUniformClickPad: %d\n",
5643 synhw.capUniformClickPad);
5644 printf(" capReportsMin: %d\n",
5645 synhw.capReportsMin);
5646 printf(" capInterTouch: %d\n",
5647 synhw.capInterTouch);
5648 printf(" capReportsMax: %d\n",
5649 synhw.capReportsMax);
5650 printf(" capClearPad: %d\n",
5652 printf(" capAdvancedGestures: %d\n",
5653 synhw.capAdvancedGestures);
5654 printf(" capCoveredPad: %d\n",
5655 synhw.capCoveredPad);
5656 if (synhw.capReportsMax) {
5657 printf(" maximumXCoord: %d\n",
5658 synhw.maximumXCoord);
5659 printf(" maximumYCoord: %d\n",
5660 synhw.maximumYCoord);
5662 if (synhw.capReportsMin) {
5663 printf(" minimumXCoord: %d\n",
5664 synhw.minimumXCoord);
5665 printf(" minimumYCoord: %d\n",
5666 synhw.minimumYCoord);
5669 buttons += synhw.capClickPad;
5674 if (synhw.capExtended)
5675 printf(" Additional Buttons: %d\n", buttons);
5677 printf(" No extended capabilities\n");
5681 * Add the default number of 3 buttons to the total
5682 * count of supported buttons reported above.
5687 * Read the mode byte.
5689 * XXX: Note the Synaptics documentation also defines the first
5690 * byte of the response to this query to be a constant 0x3b, this
5691 * does not appear to be true for Touchpads with guest devices.
5693 if (mouse_ext_command(kbdc, 1) == 0)
5695 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5697 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
5698 printf(" Failed to read mode byte\n");
5704 if (!synaptics_support)
5707 /* Set mouse type just now for synaptics_set_mode() */
5708 sc->hw.model = MOUSE_MODEL_SYNAPTICS;
5710 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
5712 if (trackpoint_support && synhw.capPassthrough) {
5713 enable_trackpoint(sc, arg);
5716 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
5719 /* Create sysctl tree. */
5720 synaptics_sysctl_create_tree(sc, "synaptics",
5721 "Synaptics TouchPad");
5722 sc->hw.buttons = buttons;
5729 synaptics_passthrough_on(struct psm_softc *sc)
5731 VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n"));
5732 synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5));
5736 synaptics_passthrough_off(struct psm_softc *sc)
5738 VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n"));
5739 set_mouse_scaling(sc->kbdc, 2);
5740 set_mouse_scaling(sc->kbdc, 1);
5741 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
5744 /* IBM/Lenovo TrackPoint */
5746 trackpoint_command(struct psm_softc *sc, int cmd, int loc, int val)
5748 const int seq[] = { 0xe2, cmd, loc, val };
5751 if (sc->synhw.capPassthrough)
5752 synaptics_passthrough_on(sc);
5754 for (i = 0; i < nitems(seq); i++) {
5755 if (sc->synhw.capPassthrough &&
5756 (seq[i] == 0xff || seq[i] == 0xe7))
5757 if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) {
5758 synaptics_passthrough_off(sc);
5761 if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) {
5762 if (sc->synhw.capPassthrough)
5763 synaptics_passthrough_off(sc);
5768 if (sc->synhw.capPassthrough)
5769 synaptics_passthrough_off(sc);
5774 #define PSM_TPINFO(x) offsetof(struct psm_softc, tpinfo.x)
5780 trackpoint_sysctl(SYSCTL_HANDLER_ARGS)
5782 static const int data[][3] = {
5783 { 0x00, 0x4a, PSM_TPINFO(sensitivity) },
5784 { 0x00, 0x4d, PSM_TPINFO(inertia) },
5785 { 0x00, 0x60, PSM_TPINFO(uplateau) },
5786 { 0x00, 0x57, PSM_TPINFO(reach) },
5787 { 0x00, 0x58, PSM_TPINFO(draghys) },
5788 { 0x00, 0x59, PSM_TPINFO(mindrag) },
5789 { 0x00, 0x5a, PSM_TPINFO(upthresh) },
5790 { 0x00, 0x5c, PSM_TPINFO(threshold) },
5791 { 0x00, 0x5d, PSM_TPINFO(jenks) },
5792 { 0x00, 0x5e, PSM_TPINFO(ztime) },
5793 { 0x01, 0x2c, PSM_TPINFO(pts) },
5794 { 0x08, 0x2d, PSM_TPINFO(skipback) }
5796 struct psm_softc *sc;
5797 int error, newval, *oldvalp;
5800 if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
5804 oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
5806 error = sysctl_handle_int(oidp, &newval, 0, req);
5809 if (newval == *oldvalp)
5811 if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
5813 error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47,
5814 tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
5823 trackpoint_sysctl_create_tree(struct psm_softc *sc)
5826 if (sc->tpinfo.sysctl_tree != NULL)
5829 /* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
5830 sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
5831 sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
5832 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD,
5833 0, "IBM/Lenovo TrackPoint");
5835 /* hw.psm.trackpoint.sensitivity */
5836 sc->tpinfo.sensitivity = 0x80;
5837 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5838 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5839 "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5840 sc, TRACKPOINT_SYSCTL_SENSITIVITY,
5841 trackpoint_sysctl, "I",
5844 /* hw.psm.trackpoint.negative_inertia */
5845 sc->tpinfo.inertia = 0x06;
5846 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5847 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5848 "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5849 sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
5850 trackpoint_sysctl, "I",
5851 "Negative inertia factor");
5853 /* hw.psm.trackpoint.upper_plateau */
5854 sc->tpinfo.uplateau = 0x61;
5855 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5856 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5857 "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5858 sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
5859 trackpoint_sysctl, "I",
5860 "Transfer function upper plateau speed");
5862 /* hw.psm.trackpoint.backup_range */
5863 sc->tpinfo.reach = 0x0a;
5864 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5865 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5866 "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5867 sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
5868 trackpoint_sysctl, "I",
5871 /* hw.psm.trackpoint.drag_hysteresis */
5872 sc->tpinfo.draghys = 0xff;
5873 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5874 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5875 "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5876 sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
5877 trackpoint_sysctl, "I",
5880 /* hw.psm.trackpoint.minimum_drag */
5881 sc->tpinfo.mindrag = 0x14;
5882 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5883 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5884 "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5885 sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
5886 trackpoint_sysctl, "I",
5889 /* hw.psm.trackpoint.up_threshold */
5890 sc->tpinfo.upthresh = 0xff;
5891 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5892 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5893 "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5894 sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
5895 trackpoint_sysctl, "I",
5896 "Up threshold for release");
5898 /* hw.psm.trackpoint.threshold */
5899 sc->tpinfo.threshold = 0x08;
5900 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5901 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5902 "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5903 sc, TRACKPOINT_SYSCTL_THRESHOLD,
5904 trackpoint_sysctl, "I",
5907 /* hw.psm.trackpoint.jenks_curvature */
5908 sc->tpinfo.jenks = 0x87;
5909 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5910 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5911 "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5912 sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
5913 trackpoint_sysctl, "I",
5916 /* hw.psm.trackpoint.z_time */
5917 sc->tpinfo.ztime = 0x26;
5918 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5919 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5920 "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5921 sc, TRACKPOINT_SYSCTL_Z_TIME,
5922 trackpoint_sysctl, "I",
5925 /* hw.psm.trackpoint.press_to_select */
5926 sc->tpinfo.pts = 0x00;
5927 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5928 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5929 "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5930 sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
5931 trackpoint_sysctl, "I",
5934 /* hw.psm.trackpoint.skip_backups */
5935 sc->tpinfo.skipback = 0x00;
5936 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5937 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5938 "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5939 sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
5940 trackpoint_sysctl, "I",
5941 "Skip backups from drags");
5945 set_trackpoint_parameters(struct psm_softc *sc)
5947 trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity);
5948 trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau);
5949 trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia);
5950 trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach);
5951 trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys);
5952 trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag);
5953 trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh);
5954 trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold);
5955 trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks);
5956 trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime);
5957 if (sc->tpinfo.pts == 0x01)
5958 trackpoint_command(sc, 0x47, 0x2c, 0x01);
5959 if (sc->tpinfo.skipback == 0x01)
5960 trackpoint_command(sc, 0x47, 0x2d, 0x08);
5964 enable_trackpoint(struct psm_softc *sc, enum probearg arg)
5966 KBDC kbdc = sc->kbdc;
5970 * If called from enable_synaptics(), make sure that passthrough
5971 * mode is enabled so we can reach the trackpoint.
5972 * However, passthrough mode must be disabled before setting the
5973 * trackpoint parameters, as rackpoint_command() enables and disables
5974 * passthrough mode on its own.
5976 if (sc->synhw.capPassthrough)
5977 synaptics_passthrough_on(sc);
5979 if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
5980 read_aux_data(kbdc) != 0x01)
5982 id = read_aux_data(kbdc);
5987 if (!trackpoint_support)
5990 if (sc->synhw.capPassthrough)
5991 synaptics_passthrough_off(sc);
5994 trackpoint_sysctl_create_tree(sc);
5996 * Don't overwrite hwid and buttons when we are
5999 if (!sc->synhw.capPassthrough) {
6005 set_trackpoint_parameters(sc);
6010 if (sc->synhw.capPassthrough)
6011 synaptics_passthrough_off(sc);
6016 /* Interlink electronics VersaPad */
6018 enable_versapad(struct psm_softc *sc, enum probearg arg)
6020 KBDC kbdc = sc->kbdc;
6023 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
6024 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
6025 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6026 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6027 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6028 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6029 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
6031 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
6033 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6035 return (TRUE); /* PS/2 absolute mode */
6038 /* Elantech Touchpad */
6040 elantech_read_1(KBDC kbdc, int hwversion, int reg, int *val)
6042 int res, readcmd, retidx;
6045 readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR;
6046 retidx = hwversion == 4 ? 1 : 0;
6048 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6049 res |= send_aux_command(kbdc, readcmd) != PSM_ACK;
6050 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6051 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6052 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6055 *val = resp[retidx];
6061 elantech_write_1(KBDC kbdc, int hwversion, int reg, int val)
6065 writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR;
6067 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6068 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6069 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6070 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6071 if (hwversion == 4) {
6072 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6073 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6075 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6076 res |= send_aux_command(kbdc, val) != PSM_ACK;
6077 res |= set_mouse_scaling(kbdc, 1) == 0;
6083 elantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp)
6087 if (hwversion == 2) {
6088 res = set_mouse_scaling(kbdc, 1) == 0;
6089 res |= mouse_ext_command(kbdc, cmd) == 0;
6091 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6092 res |= send_aux_command(kbdc, cmd) != PSM_ACK;
6094 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6100 elantech_init(KBDC kbdc, elantechhw_t *elanhw)
6102 int i, val, res, hwversion, reg10;
6104 /* set absolute mode */
6105 hwversion = elanhw->hwversion;
6107 switch (hwversion) {
6109 reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4;
6110 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6113 res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A);
6117 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6120 res = elantech_write_1(kbdc, hwversion, 0x07, 0x01);
6126 /* Read back reg 0x10 to ensure hardware is ready. */
6127 if (res == 0 && reg10 >= 0) {
6128 for (i = 0; i < 5; i++) {
6129 if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0)
6138 printf("couldn't set absolute mode\n");
6144 elantech_init_synaptics(struct psm_softc *sc)
6147 /* Set capabilites required by movement smother */
6148 sc->synhw.infoMajor = sc->elanhw.hwversion;
6149 sc->synhw.infoMinor = sc->elanhw.fwversion;
6150 sc->synhw.infoXupmm = sc->elanhw.dpmmx;
6151 sc->synhw.infoYupmm = sc->elanhw.dpmmy;
6152 sc->synhw.verticalScroll = 0;
6153 sc->synhw.nExtendedQueries = 4;
6154 sc->synhw.capExtended = 1;
6155 sc->synhw.capPassthrough = sc->elanhw.hastrackpoint;
6156 sc->synhw.capClickPad = sc->elanhw.isclickpad;
6157 sc->synhw.capMultiFinger = 1;
6158 sc->synhw.capAdvancedGestures = 1;
6159 sc->synhw.capPalmDetect = 1;
6160 sc->synhw.capPen = 0;
6161 sc->synhw.capReportsMax = 1;
6162 sc->synhw.maximumXCoord = sc->elanhw.sizex;
6163 sc->synhw.maximumYCoord = sc->elanhw.sizey;
6164 sc->synhw.capReportsMin = 1;
6165 sc->synhw.minimumXCoord = 0;
6166 sc->synhw.minimumYCoord = 0;
6168 if (sc->syninfo.sysctl_tree == NULL) {
6169 synaptics_sysctl_create_tree(sc, "elantech",
6170 "Elantech Touchpad");
6173 * Adjust synaptic smoother tunables
6174 * 1. Disable finger detection pressure threshold. Unlike
6175 * synaptics we assume the finger is acting when packet with
6176 * its X&Y arrives not when pressure exceedes some threshold
6177 * 2. Disable unrelated features like margins and noisy areas
6178 * 3. Disable virtual scroll areas as 2nd finger is preferable
6179 * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized
6181 * 5. Scale down divisors and movement lengths by a factor of 3
6182 * where 3 is Synaptics to Elantech (~2200/800) dpi ratio
6185 /* Set reporting range to be equal touchpad size */
6186 sc->syninfo.max_x = sc->elanhw.sizex;
6187 sc->syninfo.max_y = sc->elanhw.sizey;
6189 /* Disable finger detection pressure threshold */
6190 sc->syninfo.min_pressure = 1;
6192 /* Use full area of touchpad */
6193 sc->syninfo.margin_top = 0;
6194 sc->syninfo.margin_right = 0;
6195 sc->syninfo.margin_bottom = 0;
6196 sc->syninfo.margin_left = 0;
6198 /* Disable noisy area */
6199 sc->syninfo.na_top = 0;
6200 sc->syninfo.na_right = 0;
6201 sc->syninfo.na_bottom = 0;
6202 sc->syninfo.na_left = 0;
6204 /* Tune divisors and movement lengths */
6205 sc->syninfo.weight_len_squared = 200;
6206 sc->syninfo.div_min = 3;
6207 sc->syninfo.div_max = 6;
6208 sc->syninfo.div_max_na = 10;
6209 sc->syninfo.div_len = 30;
6210 sc->syninfo.tap_max_delta = 25;
6212 /* Disable virtual scrolling areas and tune its divisors */
6213 sc->syninfo.vscroll_hor_area = 0;
6214 sc->syninfo.vscroll_ver_area = 0;
6215 sc->syninfo.vscroll_min_delta = 15;
6216 sc->syninfo.vscroll_div_min = 30;
6217 sc->syninfo.vscroll_div_max = 50;
6219 /* Set bottom quarter as 42% - 16% - 42% sized softbuttons */
6220 if (sc->elanhw.isclickpad) {
6221 sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4;
6222 sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25;
6223 sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25;
6231 enable_elantech(struct psm_softc *sc, enum probearg arg)
6233 static const int ic2hw[] =
6234 /*IC: 0 1 2 3 4 5 6 7 8 9 a b c d e f */
6235 { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0 };
6236 elantechhw_t elanhw;
6237 int icversion, hwversion, dptracex, dptracey, id, resp[3], dpix, dpiy;
6238 KBDC kbdc = sc->kbdc;
6240 VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n"));
6242 set_mouse_scaling(kbdc, 1);
6243 set_mouse_scaling(kbdc, 1);
6244 set_mouse_scaling(kbdc, 1);
6245 if (get_mouse_status(kbdc, resp, 0, 3) != 3)
6248 if (!ELANTECH_MAGIC(resp))
6251 /* Identify the Touchpad version. */
6252 if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp))
6255 bzero(&elanhw, sizeof(elanhw));
6257 elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2];
6258 icversion = resp[0] & 0x0f;
6259 hwversion = ic2hw[icversion];
6262 printf("Elantech touchpad hardware v.%d firmware v.0x%06x\n",
6263 hwversion, elanhw.fwversion);
6265 if (ELANTECH_HW_IS_V1(elanhw.fwversion)) {
6266 printf (" Unsupported touchpad hardware (v1)\n");
6269 if (hwversion == 0) {
6270 printf (" Unknown touchpad hardware (firmware v.0x%06x)\n",
6275 /* Get the Touchpad model information. */
6276 elanhw.hwversion = hwversion;
6277 elanhw.issemimt = hwversion == 2;
6278 elanhw.isclickpad = (resp[1] & 0x10) != 0;
6279 elanhw.hascrc = (resp[1] & 0x40) != 0;
6280 elanhw.haspressure = elanhw.fwversion >= 0x020800;
6282 /* Read the capability bits. */
6283 if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) {
6284 printf(" Failed to read capability bits\n");
6288 elanhw.ntracesx = resp[1] - 1;
6289 elanhw.ntracesy = resp[2] - 1;
6290 elanhw.hastrackpoint = (resp[0] & 0x80) != 0;
6292 /* Get the touchpad resolution */
6293 switch (hwversion) {
6295 if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp)
6297 dpix = (resp[1] & 0x0f) * 10 + 790;
6298 dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790;
6299 elanhw.dpmmx = (dpix * 10 + 5) / 254;
6300 elanhw.dpmmy = (dpiy * 10 + 5) / 254;
6306 elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */
6310 if (!elantech_support)
6313 if (elantech_init(kbdc, &elanhw)) {
6314 printf("couldn't initialize elantech touchpad\n");
6319 * Get the touchpad reporting range.
6320 * On HW v.3 touchpads it should be done after switching hardware
6321 * to real resolution mode (by setting bit 3 of reg10)
6323 if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) {
6324 printf(" Failed to read touchpad size\n");
6325 elanhw.sizex = 10000; /* Arbitrary high values to */
6326 elanhw.sizey = 10000; /* prevent clipping in smoother */
6327 } else if (hwversion == 2) {
6328 dptracex = dptracey = 64;
6329 if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) &&
6330 !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) {
6331 dptracex = resp[1] / 2;
6332 dptracey = resp[2] / 2;
6334 elanhw.sizex = (elanhw.ntracesx - 1) * dptracex;
6335 elanhw.sizey = (elanhw.ntracesy - 1) * dptracey;
6337 elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1];
6338 elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2];
6342 printf(" Model information:\n");
6343 printf(" MaxX: %d\n", elanhw.sizex);
6344 printf(" MaxY: %d\n", elanhw.sizey);
6345 printf(" DpmmX: %d\n", elanhw.dpmmx);
6346 printf(" DpmmY: %d\n", elanhw.dpmmy);
6347 printf(" TracesX: %d\n", elanhw.ntracesx);
6348 printf(" TracesY: %d\n", elanhw.ntracesy);
6349 printf(" SemiMT: %d\n", elanhw.issemimt);
6350 printf(" Clickpad: %d\n", elanhw.isclickpad);
6351 printf(" Trackpoint: %d\n", elanhw.hastrackpoint);
6352 printf(" CRC: %d\n", elanhw.hascrc);
6353 printf(" Pressure: %d\n", elanhw.haspressure);
6356 VLOG(3, (LOG_DEBUG, "elantech: END init\n"));
6359 sc->elanhw = elanhw;
6362 /* Initialize synaptics movement smoother */
6363 elantech_init_synaptics(sc);
6365 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
6366 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
6373 * Return true if 'now' is earlier than (start + (secs.usecs)).
6374 * Now may be NULL and the function will fetch the current time from
6375 * getmicrouptime(), or a cached 'now' can be passed in.
6376 * All values should be numbers derived from getmicrouptime().
6379 timeelapsed(start, secs, usecs, now)
6380 const struct timeval *start, *now;
6383 struct timeval snow, tv;
6385 /* if there is no 'now' passed in, the get it as a convience. */
6387 getmicrouptime(&snow);
6393 timevaladd(&tv, start);
6394 return (timevalcmp(&tv, now, <));
6398 psmresume(device_t dev)
6400 struct psm_softc *sc = device_get_softc(dev);
6401 int unit = device_get_unit(dev);
6404 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
6407 (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
6410 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
6412 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
6414 * Release the blocked process; it must be notified that
6415 * the device cannot be accessed anymore.
6417 sc->state &= ~PSM_ASLP;
6421 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
6426 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
6431 * This sucks up assignments from PNPBIOS and ACPI.
6435 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
6436 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
6437 * can be probed and attached only after the AT keyboard controller is
6438 * attached, we shall quietly reserve the IRQ resource for later use.
6439 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
6440 * copy the IRQ resource to the PS/2 mouse device instance hanging
6441 * under the keyboard controller, then probe and attach it.
6444 static devclass_t psmcpnp_devclass;
6446 static device_probe_t psmcpnp_probe;
6447 static device_attach_t psmcpnp_attach;
6449 static device_method_t psmcpnp_methods[] = {
6450 DEVMETHOD(device_probe, psmcpnp_probe),
6451 DEVMETHOD(device_attach, psmcpnp_attach),
6456 static driver_t psmcpnp_driver = {
6457 PSMCPNP_DRIVER_NAME,
6462 static struct isa_pnp_id psmcpnp_ids[] = {
6463 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
6464 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */
6465 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */
6466 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
6467 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
6468 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
6469 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */
6470 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
6471 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
6472 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
6473 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
6474 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
6475 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
6480 create_a_copy(device_t atkbdc, device_t me)
6485 /* find the PS/2 mouse device instance under the keyboard controller */
6486 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
6487 device_get_unit(atkbdc));
6490 if (device_get_state(psm) != DS_NOTPRESENT)
6493 /* move our resource to the found device */
6494 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
6495 bus_delete_resource(me, SYS_RES_IRQ, 0);
6496 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
6498 /* ...then probe and attach it */
6499 return (device_probe_and_attach(psm));
6503 psmcpnp_probe(device_t dev)
6505 struct resource *res;
6509 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
6513 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
6514 * to the PS/2 mouse device node. But, some buggy PnP BIOS
6515 * declares the PS/2 mouse device node without an IRQ resource!
6516 * If this happens, we shall refer to device hints.
6517 * If we still don't find it there, use a hardcoded value... XXX
6520 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
6522 if (resource_long_value(PSM_DRIVER_NAME,
6523 device_get_unit(dev),"irq", &irq) != 0)
6525 device_printf(dev, "irq resource info is missing; "
6526 "assuming irq %ld\n", irq);
6527 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
6529 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0);
6530 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
6536 return ((res == NULL) ? ENXIO : 0);
6540 psmcpnp_attach(device_t dev)
6544 /* find the keyboard controller, which may be on acpi* or isa* bus */
6545 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
6546 device_get_unit(dev));
6547 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
6548 create_a_copy(atkbdc, dev);
6553 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
6554 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
6556 #endif /* DEV_ISA */