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.
60 * $FreeBSD: src/sys/dev/atkbdc/psm.c,v 1.107 2010/09/09 07:52:15 ed Exp $
61 * $FreeBSD: stable/11/sys/dev/atkbdc/psm.c 307576 2016-10-18 20:17:57Z gonzo $
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/module.h>
72 #include <sys/device.h>
73 #include <sys/event.h>
74 #include <sys/syslog.h>
75 #include <sys/malloc.h>
77 #include <sys/sysctl.h>
78 #include <sys/thread2.h>
81 #include <sys/machintr.h>
82 #include <sys/vnode.h>
84 #include <machine/clock.h>
85 #include <machine/limits.h>
86 #include <sys/mouse.h>
88 #include <bus/isa/isavar.h>
89 #include <dev/misc/kbd/atkbdcreg.h>
91 #include <sys/signalvar.h>
92 #include <sys/filio.h>
96 * Driver specific options: the following options may be set by
97 * `options' statements in the kernel configuration file.
102 #define PSM_DEBUG 0 /*
103 * logging: 0: none, 1: brief, 2: verbose
104 * 3: sync errors, 4: all packets
107 #define VLOG(level, args) do { \
108 if (verbose >= level) \
112 #ifndef PSM_INPUT_TIMEOUT
113 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
116 #ifndef PSM_TAP_TIMEOUT
117 #define PSM_TAP_TIMEOUT 125000
120 #ifndef PSM_TAP_THRESHOLD
121 #define PSM_TAP_THRESHOLD 25
124 /* end of driver specific options */
126 #define PSM_DRIVER_NAME "psm"
127 #define PSMCPNP_DRIVER_NAME "psmcpnp"
130 #define PSM_BUFSIZE 960
131 #define PSM_SMALLBUFSIZE 240
133 /* operation levels */
134 #define PSM_LEVEL_BASE 0
135 #define PSM_LEVEL_STANDARD 1
136 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
137 #define PSM_LEVEL_MAX PSM_LEVEL_STANDARD
139 /* Logitech PS2++ protocol */
140 #define MOUSE_PS2PLUS_CHECKBITS(b) \
141 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
142 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
143 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
146 typedef struct ringbuf {
147 int count; /* # of valid elements in the buffer */
148 int head; /* head pointer */
149 int tail; /* tail poiner */
150 u_char buf[PSM_BUFSIZE];
154 typedef struct packetbuf {
155 u_char ipacket[16]; /* interim input buffer */
156 int inputbytes; /* # of bytes in the input buffer */
159 #ifndef PSM_PACKETQUEUE
160 #define PSM_PACKETQUEUE 128
164 typedef struct synapticsinfo {
165 struct sysctl_oid *sysctl_tree;
166 struct sysctl_ctx_list sysctl_ctx;
167 int directional_scrolls;
168 int two_finger_scroll;
185 int weight_previous_na;
186 int weight_len_squared;
194 int vscroll_ver_area;
195 int vscroll_hor_area;
196 int vscroll_min_delta;
207 typedef struct synapticspacket {
212 #define SYNAPTICS_PACKETQUEUE 10
213 #define SYNAPTICS_QUEUE_CURSOR(x) \
214 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
216 #define SYNAPTICS_VERSION_GE(synhw, major, minor) \
217 ((synhw).infoMajor > (major) || \
218 ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
222 typedef struct smoother {
223 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
236 typedef struct gesture {
242 int zmax; /* maximum pressure value */
243 struct timeval taptimeout; /* tap timeout for touchpads */
247 TRACKPOINT_SYSCTL_SENSITIVITY,
248 TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
249 TRACKPOINT_SYSCTL_UPPER_PLATEAU,
250 TRACKPOINT_SYSCTL_BACKUP_RANGE,
251 TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
252 TRACKPOINT_SYSCTL_MINIMUM_DRAG,
253 TRACKPOINT_SYSCTL_UP_THRESHOLD,
254 TRACKPOINT_SYSCTL_THRESHOLD,
255 TRACKPOINT_SYSCTL_JENKS_CURVATURE,
256 TRACKPOINT_SYSCTL_Z_TIME,
257 TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
258 TRACKPOINT_SYSCTL_SKIP_BACKUPS
261 typedef struct trackpointinfo {
262 struct sysctl_ctx_list sysctl_ctx;
263 struct sysctl_oid *sysctl_tree;
278 typedef struct finger {
285 #define PSM_FINGERS 2 /* # of processed fingers */
286 #define PSM_FINGER_IS_PEN (1<<0)
287 #define PSM_FINGER_FUZZY (1<<1)
288 #define PSM_FINGER_DEFAULT_P tap_threshold
289 #define PSM_FINGER_DEFAULT_W 1
290 #define PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0)
291 #define PSM_FINGER_RESET(f) do { \
292 (f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \
295 typedef struct elantechhw {
311 /* minimum versions supported by this driver */
312 #define ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600)
314 #define ELANTECH_MAGIC(magic) \
315 ((magic)[0] == 0x3c && (magic)[1] == 0x03 && \
316 ((magic)[2] == 0xc8 || (magic)[2] == 0x00))
318 #define ELANTECH_FW_ID 0x00
319 #define ELANTECH_FW_VERSION 0x01
320 #define ELANTECH_CAPABILITIES 0x02
321 #define ELANTECH_SAMPLE 0x03
322 #define ELANTECH_RESOLUTION 0x04
323 #define ELANTECH_REG_READ 0x10
324 #define ELANTECH_REG_WRITE 0x11
325 #define ELANTECH_REG_RDWR 0x00
326 #define ELANTECH_CUSTOM_CMD 0xf8
328 #define ELANTECH_MAX_FINGERS PSM_FINGERS
330 #define ELANTECH_FINGER_SET_XYP(pb) (finger_t) { \
331 .x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2], \
332 .y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5], \
333 .p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f), \
334 .w = PSM_FINGER_DEFAULT_W, \
340 ELANTECH_PKT_TRACKPOINT,
341 ELANTECH_PKT_V2_COMMON,
342 ELANTECH_PKT_V2_2FINGER,
344 ELANTECH_PKT_V4_STATUS,
345 ELANTECH_PKT_V4_HEAD,
346 ELANTECH_PKT_V4_MOTION
349 #define ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06)
350 #define ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 : \
351 (pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) && \
352 (pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff && \
353 (pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff && \
354 (pb)->ipacket[5] == 0xff)
355 #define ELANTECH_PKT_IS_V2(pb) \
356 (((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02)
357 #define ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? \
358 ((pb)->ipacket[3] & 0x09) == 0x08 : \
359 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02)
360 #define ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? \
361 ((pb)->ipacket[3] & 0x09) == 0x09 : \
362 ((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c)
363 #define ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? \
364 ((pb)->ipacket[3] & 0x08) == 0x00 : \
365 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10)
367 typedef struct elantechaction {
368 finger_t fingers[ELANTECH_MAX_FINGERS];
372 /* driver control block */
373 struct psm_softc { /* Driver status information */
375 struct kqinfo rkq; /* Processes with registered kevents */
376 struct lock lock; /* Control lock */
377 u_char state; /* Mouse driver state */
378 int config; /* driver configuration flags */
379 int flags; /* other flags */
380 KBDC kbdc; /* handle to access kbd controller */
381 struct resource *intr; /* IRQ resource */
382 void *ih; /* interrupt handle */
383 mousehw_t hw; /* hardware information */
384 synapticshw_t synhw; /* Synaptics hardware information */
385 synapticsinfo_t syninfo; /* Synaptics configuration */
386 smoother_t smoother[PSM_FINGERS]; /* Motion smoothing */
387 gesture_t gesture; /* Gesture context */
388 elantechhw_t elanhw; /* Elantech hardware information */
389 elantechaction_t elanaction; /* Elantech action context */
390 int tphw; /* TrackPoint hardware information */
391 trackpointinfo_t tpinfo; /* TrackPoint configuration */
392 mousemode_t mode; /* operation mode */
393 mousemode_t dflt_mode; /* default operation mode */
394 mousestatus_t status; /* accumulated mouse movement */
395 ringbuf_t queue; /* mouse status queue */
396 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
397 int pqueue_start; /* start of data in queue */
398 int pqueue_end; /* end of data in queue */
399 int button; /* the latest button state */
400 int xold; /* previous absolute X position */
401 int yold; /* previous absolute Y position */
402 int xaverage; /* average X position */
403 int yaverage; /* average Y position */
404 int squelch; /* level to filter movement at low speed */
405 int syncerrors; /* # of bytes discarded to synchronize */
406 int pkterrors; /* # of packets failed during quaranteen. */
407 struct timeval inputtimeout;
408 struct timeval lastsoftintr; /* time of last soft interrupt */
409 struct timeval lastinputerr; /* time last sync error happened */
410 struct timeval idletimeout;
411 packetbuf_t idlepacket; /* packet to send after idle timeout */
412 int watchdog; /* watchdog timer flag */
413 struct callout callout; /* watchdog timer call out */
414 struct callout softcallout; /* buffer timer call out */
418 int extended_buttons;
420 static devclass_t psm_devclass;
422 #define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field)
424 SYNAPTICS_SYSCTL_MIN_PRESSURE = SYN_OFFSET(min_pressure),
425 SYNAPTICS_SYSCTL_MAX_PRESSURE = SYN_OFFSET(max_pressure),
426 SYNAPTICS_SYSCTL_MAX_WIDTH = SYN_OFFSET(max_width),
427 SYNAPTICS_SYSCTL_MARGIN_TOP = SYN_OFFSET(margin_top),
428 SYNAPTICS_SYSCTL_MARGIN_RIGHT = SYN_OFFSET(margin_right),
429 SYNAPTICS_SYSCTL_MARGIN_BOTTOM = SYN_OFFSET(margin_bottom),
430 SYNAPTICS_SYSCTL_MARGIN_LEFT = SYN_OFFSET(margin_left),
431 SYNAPTICS_SYSCTL_NA_TOP = SYN_OFFSET(na_top),
432 SYNAPTICS_SYSCTL_NA_RIGHT = SYN_OFFSET(na_right),
433 SYNAPTICS_SYSCTL_NA_BOTTOM = SYN_OFFSET(na_bottom),
434 SYNAPTICS_SYSCTL_NA_LEFT = SYN_OFFSET(na_left),
435 SYNAPTICS_SYSCTL_WINDOW_MIN = SYN_OFFSET(window_min),
436 SYNAPTICS_SYSCTL_WINDOW_MAX = SYN_OFFSET(window_max),
437 SYNAPTICS_SYSCTL_MULTIPLICATOR = SYN_OFFSET(multiplicator),
438 SYNAPTICS_SYSCTL_WEIGHT_CURRENT = SYN_OFFSET(weight_current),
439 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS = SYN_OFFSET(weight_previous),
440 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA = SYN_OFFSET(weight_previous_na),
441 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED = SYN_OFFSET(weight_len_squared),
442 SYNAPTICS_SYSCTL_DIV_MIN = SYN_OFFSET(div_min),
443 SYNAPTICS_SYSCTL_DIV_MAX = SYN_OFFSET(div_max),
444 SYNAPTICS_SYSCTL_DIV_MAX_NA = SYN_OFFSET(div_max_na),
445 SYNAPTICS_SYSCTL_DIV_LEN = SYN_OFFSET(div_len),
446 SYNAPTICS_SYSCTL_TAP_MAX_DELTA = SYN_OFFSET(tap_max_delta),
447 SYNAPTICS_SYSCTL_TAP_MIN_QUEUE = SYN_OFFSET(tap_min_queue),
448 SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT = SYN_OFFSET(taphold_timeout),
449 SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA = SYN_OFFSET(vscroll_hor_area),
450 SYNAPTICS_SYSCTL_VSCROLL_VER_AREA = SYN_OFFSET(vscroll_ver_area),
451 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA = SYN_OFFSET(vscroll_min_delta),
452 SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN = SYN_OFFSET(vscroll_div_min),
453 SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX = SYN_OFFSET(vscroll_div_max),
454 SYNAPTICS_SYSCTL_TOUCHPAD_OFF = SYN_OFFSET(touchpad_off),
455 SYNAPTICS_SYSCTL_SOFTBUTTONS_Y = SYN_OFFSET(softbuttons_y),
456 SYNAPTICS_SYSCTL_SOFTBUTTON2_X = SYN_OFFSET(softbutton2_x),
457 SYNAPTICS_SYSCTL_SOFTBUTTON3_X = SYN_OFFSET(softbutton3_x),
460 /* driver state flags (state) */
461 #define PSM_VALID 0x80
462 #define PSM_OPEN 1 /* Device is open */
463 #define PSM_ASLP 2 /* Waiting for mouse data */
464 #define PSM_SOFTARMED 4 /* Software interrupt armed */
465 #define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
467 /* driver configuration flags (config) */
468 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
469 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
470 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
471 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
472 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
473 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
474 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
475 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
476 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
478 #define PSM_CONFIG_FLAGS \
479 (PSM_CONFIG_RESOLUTION | \
481 PSM_CONFIG_NOCHECKSYNC | \
482 PSM_CONFIG_NOIDPROBE | \
483 PSM_CONFIG_NORESET | \
484 PSM_CONFIG_FORCETAP | \
485 PSM_CONFIG_IGNPORTERROR | \
486 PSM_CONFIG_HOOKRESUME | \
487 PSM_CONFIG_INITAFTERSUSPEND)
489 /* other flags (flags) */
490 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
492 #define kbdcp(p) ((atkbdc_softc_t *)(p))
495 static int tap_enabled = -1;
496 TUNABLE_INT("hw.psm.tap_enabled", &tap_enabled);
498 static int synaptics_support = 0;
499 TUNABLE_INT("hw.psm.synaptics_support", &synaptics_support);
501 static int verbose = PSM_DEBUG;
502 TUNABLE_INT("debug.psm.loglevel", &verbose);
504 static int trackpoint_support = 0;
505 TUNABLE_INT("hw.psm.trackpoint_support",&trackpoint_support);
507 static int elantech_support = 0;
508 TUNABLE_INT("hw.psm.elantech_support",&elantech_support);
511 /* for backward compatibility */
512 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
513 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
514 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
516 typedef struct old_mousehw {
523 typedef struct old_mousemode {
529 /* packet formatting function */
530 typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
533 /* function prototypes */
534 static void psmidentify(driver_t *, device_t);
535 static int psmprobe(device_t);
536 static int psmattach(device_t);
537 static int psmdetach(device_t);
538 static int psmresume(device_t);
540 static d_open_t psmopen;
541 static d_close_t psmclose;
542 static d_read_t psmread;
543 static d_ioctl_t psmioctl;
544 static d_kqfilter_t psmkqfilter;
546 static int enable_aux_dev(KBDC);
547 static int disable_aux_dev(KBDC);
548 static int get_mouse_status(KBDC, int *, int, int);
549 static int get_aux_id(KBDC);
550 static int set_mouse_sampling_rate(KBDC, int);
551 static int set_mouse_scaling(KBDC, int);
552 static int set_mouse_resolution(KBDC, int);
553 static int set_mouse_mode(KBDC);
554 static int get_mouse_buttons(KBDC);
555 static void recover_from_error(KBDC);
556 static int restore_controller(KBDC, int);
557 static int doinitialize(struct psm_softc *, mousemode_t *);
558 static int doopen(struct psm_softc *, int);
559 static int reinitialize(struct psm_softc *, int);
560 static char *model_name(int);
561 static void psmsoftintr(void *);
562 static void psmsoftintridle(void *);
563 static void psmintr(void *);
564 static void psmtimeout(void *);
565 static void psmfilter_detach(struct knote *);
566 static int psmfilter(struct knote *, long);
567 static int timeelapsed(const struct timeval *, int, int,
568 const struct timeval *);
569 static void dropqueue(struct psm_softc *);
570 static void flushpackets(struct psm_softc *);
571 static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
572 mousestatus_t *, int *, int *, int *);
573 static int proc_synaptics(struct psm_softc *, packetbuf_t *,
574 mousestatus_t *, int *, int *, int *);
575 static void proc_versapad(struct psm_softc *, packetbuf_t *,
576 mousestatus_t *, int *, int *, int *);
577 static int proc_elantech(struct psm_softc *, packetbuf_t *,
578 mousestatus_t *, int *, int *, int *);
579 static int psmpalmdetect(struct psm_softc *, finger_t *, int);
580 static void psmgestures(struct psm_softc *, finger_t *, int,
582 static void psmsmoother(struct psm_softc *, finger_t *, int,
583 mousestatus_t *, int *, int *);
584 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
587 /* vendor specific features */
588 enum probearg { PROBE, REINIT };
589 typedef int probefunc_t(struct psm_softc *, enum probearg);
591 static int mouse_id_proc1(KBDC, int, int, int *);
592 static int mouse_ext_command(KBDC, int);
594 static int enable_groller(struct psm_softc *sc, enum probearg arg);
595 static int enable_gmouse(struct psm_softc *sc, enum probearg arg);
596 static int enable_aglide(struct psm_softc *sc, enum probearg arg);
597 static int enable_kmouse(struct psm_softc *sc, enum probearg arg);
598 static int enable_msexplorer(struct psm_softc *sc, enum probearg arg);
599 static int enable_msintelli(struct psm_softc *sc, enum probearg arg);
600 static int enable_4dmouse(struct psm_softc *sc, enum probearg arg);
601 static int enable_4dplus(struct psm_softc *sc, enum probearg arg);
602 static int enable_mmanplus(struct psm_softc *sc, enum probearg arg);
603 static int enable_synaptics(struct psm_softc *sc, enum probearg arg);
604 static int enable_trackpoint(struct psm_softc *sc, enum probearg arg);
605 static int enable_versapad(struct psm_softc *sc, enum probearg arg);
606 static int enable_elantech(struct psm_softc *sc, enum probearg arg);
608 static void set_trackpoint_parameters(struct psm_softc *sc);
609 static void synaptics_passthrough_on(struct psm_softc *sc);
610 static void synaptics_passthrough_off(struct psm_softc *sc);
611 static int synaptics_preferred_mode(struct psm_softc *sc);
612 static void synaptics_set_mode(struct psm_softc *sc, int mode_byte);
620 probefunc_t *probefunc;
623 * WARNING: the order of probe is very important. Don't mess it
624 * unless you know what you are doing.
626 { MOUSE_MODEL_NET, /* Genius NetMouse */
627 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
628 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
629 0xc8, 6, enable_groller },
630 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
631 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
632 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
633 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
634 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
635 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
636 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
637 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
638 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
639 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
640 { MOUSE_MODEL_ELANTECH, /* Elantech Touchpad */
641 0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech },
642 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
643 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
644 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
645 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
646 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
647 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
648 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
649 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
650 { MOUSE_MODEL_TRACKPOINT, /* IBM/Lenovo TrackPoint */
651 0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
652 { MOUSE_MODEL_GENERIC,
653 0xc0, MOUSE_PS2_PACKETSIZE, NULL },
655 #define GENERIC_MOUSE_ENTRY (NELEM(vendortype) - 1)
657 /* device driver declaration */
658 static device_method_t psm_methods[] = {
659 /* Device interface */
660 DEVMETHOD(device_identify, psmidentify),
661 DEVMETHOD(device_probe, psmprobe),
662 DEVMETHOD(device_attach, psmattach),
663 DEVMETHOD(device_detach, psmdetach),
664 DEVMETHOD(device_resume, psmresume),
669 static driver_t psm_driver = {
672 sizeof(struct psm_softc)
675 static struct dev_ops psm_ops = {
676 { PSM_DRIVER_NAME, 0, 0 },
681 .d_kqfilter = psmkqfilter
685 /* device I/O routines */
687 enable_aux_dev(KBDC kbdc)
692 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
693 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
695 return (res == PSM_ACK);
699 disable_aux_dev(KBDC kbdc)
704 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
705 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
707 return (res == PSM_ACK);
711 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
721 cmd = PSMC_SEND_DEV_STATUS;
724 cmd = PSMC_SEND_DEV_DATA;
727 empty_aux_buffer(kbdc, 5);
728 res = send_aux_command(kbdc, cmd);
729 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
730 (flag == 1) ? "DATA" : "STATUS", res));
734 for (i = 0; i < len; ++i) {
735 status[i] = read_aux_data(kbdc);
740 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
741 (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
747 get_aux_id(KBDC kbdc)
752 empty_aux_buffer(kbdc, 5);
753 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
754 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
761 id = read_aux_data(kbdc);
762 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
768 set_mouse_sampling_rate(KBDC kbdc, int rate)
773 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
774 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
776 return ((res == PSM_ACK) ? rate : -1);
780 set_mouse_scaling(KBDC kbdc, int scale)
788 scale = PSMC_SET_SCALING11;
791 scale = PSMC_SET_SCALING21;
794 res = send_aux_command(kbdc, scale);
795 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
796 (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
798 return (res == PSM_ACK);
801 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
803 set_mouse_resolution(KBDC kbdc, int val)
808 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
809 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
811 return ((res == PSM_ACK) ? val : -1);
815 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
816 * re-enabled by calling `enable_aux_dev()'
819 set_mouse_mode(KBDC kbdc)
824 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
825 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
827 return (res == PSM_ACK);
831 get_mouse_buttons(KBDC kbdc)
834 int c = 2; /* assume two buttons by default */
838 * NOTE: a special sequence to obtain Logitech Mouse specific
839 * information: set resolution to 25 ppi, set scaling to 1:1, set
840 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
841 * mouse status bytes is the number of available buttons.
842 * Some manufactures also support this sequence.
844 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
846 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
847 set_mouse_scaling(kbdc, 1) &&
848 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
853 /* misc subroutines */
855 model_name(int model)
862 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
863 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
864 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
865 { MOUSE_MODEL_THINK, "ThinkingMouse" },
866 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
867 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
868 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
869 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
870 { MOUSE_MODEL_4D, "4D Mouse" },
871 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
872 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
873 { MOUSE_MODEL_TRACKPOINT, "IBM/Lenovo TrackPoint" },
874 { MOUSE_MODEL_ELANTECH, "Elantech Touchpad" },
875 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
876 { MOUSE_MODEL_UNKNOWN, "Unknown" },
880 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
881 if (models[i].model_code == model)
883 return (models[i].model_name);
887 recover_from_error(KBDC kbdc)
890 /* discard anything left in the output buffer */
891 empty_both_buffers(kbdc, 10);
895 * NOTE: KBDC_RESET_KBD may not restore the communication between the
896 * keyboard and the controller.
901 * NOTE: somehow diagnostic and keyboard port test commands bring the
904 if (!test_controller(kbdc))
905 log(LOG_ERR, "psm: keyboard controller failed.\n");
906 /* if there isn't a keyboard in the system, the following error is OK */
907 if (test_kbd_port(kbdc) != 0)
908 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
913 restore_controller(KBDC kbdc, int command_byte)
916 empty_both_buffers(kbdc, 10);
918 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
919 log(LOG_ERR, "psm: failed to restore the keyboard controller "
921 empty_both_buffers(kbdc, 10);
924 empty_both_buffers(kbdc, 10);
930 * Re-initialize the aux port and device. The aux port must be enabled
931 * and its interrupt must be disabled before calling this routine.
932 * The aux device will be disabled before returning.
933 * The keyboard controller must be locked via `kbdc_lock()' before
934 * calling this routine.
937 doinitialize(struct psm_softc *sc, mousemode_t *mode)
940 KBDC kbdc = sc->kbdc;
944 switch((i = test_aux_port(kbdc))) {
945 case 1: /* ignore these errors */
951 "psm%d: strange result for test aux port (%d).\n",
954 case 0: /* no error */
956 case -1: /* time out */
958 recover_from_error(kbdc);
959 if (sc->config & PSM_CONFIG_IGNPORTERROR)
961 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
966 if (sc->config & PSM_CONFIG_NORESET) {
968 * Don't try to reset the pointing device. It may possibly
969 * be left in the unknown state, though...
973 * NOTE: some controllers appears to hang the `keyboard' when
974 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
976 if (!reset_aux_dev(kbdc)) {
977 recover_from_error(kbdc);
978 log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
985 * both the aux port and the aux device is functioning, see
986 * if the device can be enabled.
988 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
989 log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
993 empty_both_buffers(kbdc, 10); /* remove stray data if any */
995 /* Re-enable the mouse. */
996 for (i = 0; vendortype[i].probefunc != NULL; ++i)
997 if (vendortype[i].model == sc->hw.model)
998 (*vendortype[i].probefunc)(sc, REINIT);
1000 /* set mouse parameters */
1001 if (mode != (mousemode_t *)NULL) {
1003 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
1004 if (mode->resolution >= 0)
1006 set_mouse_resolution(kbdc, mode->resolution);
1007 set_mouse_scaling(kbdc, 1);
1008 set_mouse_mode(kbdc);
1011 /* Record sync on the next data packet we see. */
1012 sc->flags |= PSM_NEED_SYNCBITS;
1014 /* just check the status of the mouse */
1015 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
1016 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
1023 doopen(struct psm_softc *sc, int command_byte)
1029 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
1030 * no obvious reason. Thus we check the current mode and restore the
1031 * Absolute Mode if it was cleared.
1033 * The previous hack at the end of psmprobe() wasn't efficient when
1034 * moused(8) was restarted.
1036 * A Reset (FF) or Set Defaults (F6) command would clear the
1037 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
1038 * doesn't show any evidence of such a command.
1040 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
1041 mouse_ext_command(sc->kbdc, 1);
1042 get_mouse_status(sc->kbdc, stat, 0, 3);
1043 if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
1046 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1047 VLOG(5, (LOG_DEBUG, "psm%d: Synaptics Absolute Mode "
1048 "hopefully restored\n",
1055 * A user may want to disable tap and drag gestures on a Synaptics
1056 * TouchPad when it operates in Relative Mode.
1058 if (sc->hw.model == MOUSE_MODEL_GENERIC) {
1059 if (tap_enabled > 0) {
1061 * Enable tap & drag gestures. We use a Mode Byte
1062 * and clear the DisGest bit (see §2.5 of Synaptics
1063 * TouchPad Interfacing Guide).
1066 "psm%d: enable tap and drag gestures\n",
1068 mouse_ext_command(sc->kbdc, 0x00);
1069 set_mouse_sampling_rate(sc->kbdc, 20);
1070 } else if (tap_enabled == 0) {
1072 * Disable tap & drag gestures. We use a Mode Byte
1073 * and set the DisGest bit (see §2.5 of Synaptics
1074 * TouchPad Interfacing Guide).
1077 "psm%d: disable tap and drag gestures\n",
1079 mouse_ext_command(sc->kbdc, 0x04);
1080 set_mouse_sampling_rate(sc->kbdc, 20);
1084 /* enable the mouse device */
1085 if (!enable_aux_dev(sc->kbdc)) {
1086 /* MOUSE ERROR: failed to enable the mouse because:
1087 * 1) the mouse is faulty,
1088 * 2) the mouse has been removed(!?)
1089 * In the latter case, the keyboard may have hung, and need
1090 * recovery procedure...
1092 recover_from_error(sc->kbdc);
1094 /* FIXME: we could reset the mouse here and try to enable
1095 * it again. But it will take long time and it's not a good
1096 * idea to disable the keyboard that long...
1098 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
1099 recover_from_error(sc->kbdc);
1103 restore_controller(sc->kbdc, command_byte);
1104 /* mark this device is no longer available */
1105 sc->state &= ~PSM_VALID;
1107 "psm%d: failed to enable the device (doopen).\n",
1113 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1114 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
1117 /* enable the aux port and interrupt */
1118 if (!set_controller_command_byte(sc->kbdc,
1119 KBD_AUX_CONTROL_BITS,
1120 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
1121 /* CONTROLLER ERROR */
1122 disable_aux_dev(sc->kbdc);
1123 restore_controller(sc->kbdc, command_byte);
1125 "psm%d: failed to enable the aux interrupt (doopen).\n",
1130 /* start the watchdog timer */
1131 sc->watchdog = FALSE;
1132 callout_reset(&sc->callout, hz * 2, psmtimeout, (void *)(uintptr_t)sc);
1138 reinitialize(struct psm_softc *sc, int doinit)
1144 /* don't let anybody mess with the aux device */
1145 if (!kbdc_lock(sc->kbdc, TRUE))
1148 lockmgr(&sc->lock, LK_EXCLUSIVE);
1150 /* block our watchdog timer */
1151 sc->watchdog = FALSE;
1152 callout_stop(&sc->callout);
1154 /* save the current controller command byte */
1155 empty_both_buffers(sc->kbdc, 10);
1156 c = get_controller_command_byte(sc->kbdc);
1158 "psm%d: current command byte: %04x (reinitialize).\n",
1161 /* enable the aux port but disable the aux interrupt and the keyboard */
1163 !set_controller_command_byte(sc->kbdc,
1164 KBD_AUX_CONTROL_BITS,
1165 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1166 /* CONTROLLER ERROR */
1167 lockmgr(&sc->lock, LK_RELEASE);
1168 kbdc_lock(sc->kbdc, FALSE);
1170 "psm%d: unable to set the command byte (reinitialize).\n",
1175 /* flush any data */
1176 if (sc->state & PSM_VALID) {
1177 /* this may fail; but never mind... */
1178 disable_aux_dev(sc->kbdc);
1179 empty_aux_buffer(sc->kbdc, 10);
1184 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1186 /* try to detect the aux device; are you still there? */
1189 if (doinitialize(sc, &sc->mode)) {
1191 sc->state |= PSM_VALID;
1193 /* the device has gone! */
1194 restore_controller(sc->kbdc, c);
1195 sc->state &= ~PSM_VALID;
1197 "psm%d: the aux device has gone! (reinitialize).\n",
1202 lockmgr(&sc->lock, LK_RELEASE);
1204 /* restore the driver state */
1205 if ((sc->state & PSM_OPEN) && (err == 0)) {
1206 /* enable the aux device and the port again */
1207 err = doopen(sc, c);
1209 log(LOG_ERR, "psm%d: failed to enable the device "
1210 "(reinitialize).\n", sc->unit);
1212 /* restore the keyboard port and disable the aux port */
1213 if (!set_controller_command_byte(sc->kbdc,
1214 KBD_AUX_CONTROL_BITS,
1215 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1216 /* CONTROLLER ERROR */
1217 log(LOG_ERR, "psm%d: failed to disable the aux port "
1218 "(reinitialize).\n", sc->unit);
1223 kbdc_lock(sc->kbdc, FALSE);
1227 /* psm driver entry points */
1230 psmidentify(driver_t *driver, device_t parent)
1238 unit = device_get_unit(parent);
1240 /* always add at least one child */
1241 psm = BUS_ADD_CHILD(parent, parent, KBDC_RID_AUX, driver->name, unit);
1247 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1254 * If the PS/2 mouse device has already been reported by ACPI or
1255 * PnP BIOS, obtain the IRQ resource from it.
1256 * (See psmcpnp_attach() below.)
1258 psmc = device_find_child(device_get_parent(parent),
1259 PSMCPNP_DRIVER_NAME, unit);
1263 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1269 bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1270 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1,
1271 machintr_legacy_intr_cpuid(irq));
1274 #define endprobe(v) do { \
1277 set_controller_command_byte(sc->kbdc, \
1278 KBD_AUX_CONTROL_BITS, KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT); \
1279 kbdc_lock(sc->kbdc, FALSE); \
1284 psmprobe(device_t dev)
1287 int unit = device_get_unit(dev);
1288 struct psm_softc *sc = device_get_softc(dev);
1300 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1301 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
1304 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1305 sc->config = flags & PSM_CONFIG_FLAGS;
1306 /* XXX: for backward compatibility */
1307 #if defined(PSM_HOOKRESUME)
1309 #ifdef PSM_RESETAFTERSUSPEND
1310 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
1312 PSM_CONFIG_HOOKRESUME;
1314 #endif /* PSM_HOOKRESUME */
1319 device_set_desc(dev, "PS/2 Mouse");
1321 if (!kbdc_lock(sc->kbdc, TRUE)) {
1322 kprintf("psm%d: unable to lock the controller.\n", unit);
1330 * NOTE: two bits in the command byte controls the operation of the
1331 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1332 * port interrupt (IRQ 12) enable bit (bit 2).
1335 /* discard anything left after the keyboard initialization */
1336 empty_both_buffers(sc->kbdc, 10);
1338 /* save the current command byte; it will be used later */
1339 command_byte = get_controller_command_byte(sc->kbdc);
1341 kprintf("psm%d: current command byte:%04x\n", unit,
1343 if (command_byte == -1) {
1344 /* CONTROLLER ERROR */
1345 kprintf("psm%d: unable to get the current "
1346 "command byte value.\n",
1351 /* XXX: this note is of the previous code, kept intact in case any
1352 * breakage occurs. Since both comments contradict each other.
1353 * Previous condition used was:
1355 * if (!set_controller_command_byte(sc->kbdc,
1356 * KBD_AUX_CONTROL_BITS,
1357 * KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT))
1359 * .. <slightly different code>
1360 * NOTE: We cannot mess with the keyboard port, do NOT disable it
1361 * while we are probing the aux port during this routine.
1362 * Disabling the keyboard port will break some things
1363 * (Acer c720)... probably related to BIOS emulation of the
1367 * disable the keyboard port while probing the aux port, which must be
1368 * enabled during this routine
1370 if (!set_controller_command_byte(sc->kbdc,
1371 KBD_AUX_CONTROL_BITS,
1372 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1374 * this is CONTROLLER ERROR; I don't know how to recover
1375 * from this error...
1377 kprintf("psm%d: unable to set the command byte.\n", unit);
1382 * NOTE: Linux doesn't send discrete aux port enablement commands,
1383 * it is unclear whether this is needed or helps or hinders
1386 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1389 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1390 * port exists and is functioning. However, some controllers appears
1391 * to respond with zero even when the aux port doesn't exist. (It may
1392 * be that this is only the case when the controller DOES have the aux
1393 * port but the port is not wired on the motherboard.) The keyboard
1394 * controllers without the port, such as the original AT, are
1395 * supporsed to return with an error code or simply time out. In any
1396 * case, we have to continue probing the port even when the controller
1399 * XXX: some controllers erroneously return the error code 1, 2 or 3
1400 * when it has the perfectly functional aux port. We have to ignore
1401 * this error code. Even if the controller HAS error with the aux
1402 * port, it will be detected later...
1403 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1406 switch ((i = test_aux_port(sc->kbdc))) {
1407 case 1: /* ignore these errors */
1412 kprintf("psm%d: strange result for test aux port "
1413 "(%d).\n", unit, i);
1415 case 0: /* no error */
1417 case -1: /* time out */
1418 default: /* error */
1419 recover_from_error(sc->kbdc);
1420 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1424 kprintf("psm%d: the aux port is not "
1425 "functioning (%d).\n",
1430 if (sc->config & PSM_CONFIG_NORESET) {
1432 * Don't try to reset the pointing device. It may possibly be
1433 * left in the unknown state, though...
1437 * NOTE: some controllers appears to hang the `keyboard' when
1438 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1440 * Attempt to reset the controller twice -- this helps
1441 * pierce through some KVM switches. The second reset
1444 if (!reset_aux_dev(sc->kbdc)) {
1445 recover_from_error(sc->kbdc);
1447 kprintf("psm%d: failed to reset the aux "
1450 } else if (!reset_aux_dev(sc->kbdc)) {
1451 recover_from_error(sc->kbdc);
1453 kprintf("psm%d: failed to reset the aux device "
1459 * both the aux port and the aux device are functioning, see if the
1460 * device can be enabled. NOTE: when enabled, the device will start
1461 * sending data; we shall immediately disable the device once we know
1462 * the device can be enabled.
1464 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1466 recover_from_error(sc->kbdc);
1468 kprintf("psm%d: failed to enable the aux device.\n",
1473 /* save the default values after reset */
1474 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1475 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1476 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1478 sc->dflt_mode.rate = sc->mode.rate = -1;
1479 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1482 /* hardware information */
1483 sc->hw.iftype = MOUSE_IF_PS2;
1486 /* verify the device is a mouse */
1487 sc->hw.hwid = get_aux_id(sc->kbdc);
1488 /* XXX Check against a list of PS2 device types. */
1491 switch (sc->hw.hwid) {
1492 case PSM_BALLPOINT_ID:
1493 sc->hw.type = MOUSE_TRACKBALL;
1496 case PSM_INTELLI_ID:
1497 case PSM_EXPLORER_ID:
1498 case PSM_4DMOUSE_ID:
1500 sc->hw.type = MOUSE_MOUSE;
1503 sc->hw.type = MOUSE_UNKNOWN;
1507 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1509 i = GENERIC_MOUSE_ENTRY;
1512 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1514 /* other parameters */
1515 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1516 if ((*vendortype[i].probefunc)(sc, PROBE)) {
1518 kprintf("psm%d: found %s\n", unit,
1519 model_name(vendortype[i].model));
1524 sc->hw.model = vendortype[i].model;
1526 sc->dflt_mode.level = PSM_LEVEL_BASE;
1527 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1528 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1529 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1530 sc->dflt_mode.syncmask[0] = 0;
1532 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1533 if (sc->config & PSM_CONFIG_FORCETAP)
1534 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1535 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1536 sc->mode = sc->dflt_mode;
1537 sc->mode.packetsize = vendortype[i].packetsize;
1539 /* set mouse parameters */
1542 * A version of Logitech FirstMouse+ won't report wheel movement,
1543 * if SET_DEFAULTS is sent... Don't use this command.
1544 * This f}}}ix was found by Takashi Nishida.
1546 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1548 kprintf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1550 if (sc->config & PSM_CONFIG_RESOLUTION)
1551 sc->mode.resolution =
1552 set_mouse_resolution(sc->kbdc,
1553 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1554 else if (sc->mode.resolution >= 0)
1555 sc->mode.resolution =
1556 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1557 if (sc->mode.rate > 0)
1559 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1560 set_mouse_scaling(sc->kbdc, 1);
1562 /* Record sync on the next data packet we see. */
1563 sc->flags |= PSM_NEED_SYNCBITS;
1565 /* just check the status of the mouse */
1567 * NOTE: XXX there are some arcane controller/mouse combinations out
1568 * there, which hung the controller unless there is data transmission
1569 * after ACK from the mouse.
1571 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1572 kprintf("psm%d: failed to get status.\n", unit);
1575 * When in its native mode, some mice operate with different
1576 * default parameters than in the PS/2 compatible mode.
1578 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1579 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1582 /* disable the aux port for now... */
1583 if (!set_controller_command_byte(sc->kbdc,
1584 KBD_AUX_CONTROL_BITS,
1585 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1587 * this is CONTROLLER ERROR; I don't know the proper way to
1588 * recover from this error...
1590 kprintf("psm%d: unable to set the command byte.\n", unit);
1595 kbdc_lock(sc->kbdc, FALSE);
1600 psmattach(device_t dev)
1603 int unit = device_get_unit(dev);
1604 struct psm_softc *sc = device_get_softc(dev);
1609 /* Setup initial state */
1610 sc->state = PSM_VALID;
1611 callout_init_lk(&sc->callout, &sc->lock);
1612 callout_init_lk(&sc->softcallout, &sc->lock);
1613 lockinit(&sc->lock, "psmlk", 0, LK_CANRECURSE);
1615 /* Setup our interrupt handler */
1617 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1618 sc->intr = bus_alloc_legacy_irq_resource(dev, &rid, irq, RF_ACTIVE);
1619 if (sc->intr == NULL) {
1623 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
1624 INTR_NOPOLL, psmintr, sc, &sc->ih, NULL, NULL);
1626 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1631 sc->dev = make_dev(&psm_ops, unit, 0, 0, 0666, "psm%d", unit);
1632 sc->dev->si_drv1 = sc;
1634 /* Some touchpad devices need full reinitialization after suspend. */
1635 switch (sc->hw.model) {
1636 case MOUSE_MODEL_SYNAPTICS:
1637 case MOUSE_MODEL_GLIDEPOINT:
1638 case MOUSE_MODEL_VERSAPAD:
1639 case MOUSE_MODEL_ELANTECH:
1640 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1643 if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
1644 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1648 /* Elantech trackpad`s sync bit differs from touchpad`s one */
1649 if (sc->hw.model == MOUSE_MODEL_ELANTECH &&
1650 (sc->elanhw.hascrc || sc->elanhw.hastrackpoint))
1651 sc->config |= PSM_CONFIG_NOCHECKSYNC;
1655 kprintf("psm%d: model %s, device ID %d\n",
1656 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1658 kprintf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1659 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1660 sc->hw.hwid >> 8, sc->hw.buttons);
1661 kprintf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1662 unit, sc->config, sc->flags, sc->mode.packetsize);
1663 kprintf("psm%d: syncmask:%02x, syncbits:%02x\n",
1664 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1674 psmdetach(device_t dev)
1677 struct psm_softc *sc;
1680 sc = device_get_softc(dev);
1681 if (sc->state & PSM_OPEN)
1685 bus_teardown_intr(dev, sc->intr, sc->ih);
1686 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1688 destroy_dev(sc->dev);
1690 /* XXX: callout_drain in original freebsd11 code */
1691 callout_terminate(&sc->callout);
1692 callout_terminate(&sc->softcallout);
1698 psmopen(struct dev_open_args *ap)
1701 cdev_t dev = ap->a_head.a_dev;
1702 struct psm_softc *sc;
1706 /* Get device data */
1708 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
1709 /* the device is no longer valid/functioning */
1713 /* Disallow multiple opens */
1714 if (sc->state & PSM_OPEN)
1717 device_busy(devclass_get_device(psm_devclass, sc->unit));
1719 /* Initialize state */
1720 sc->mode.level = sc->dflt_mode.level;
1721 sc->mode.protocol = sc->dflt_mode.protocol;
1722 sc->watchdog = FALSE;
1724 /* flush the event queue */
1725 sc->queue.count = 0;
1728 sc->status.flags = 0;
1729 sc->status.button = 0;
1730 sc->status.obutton = 0;
1735 sc->pqueue_start = 0;
1738 /* empty input buffer */
1743 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1744 if (!kbdc_lock(sc->kbdc, TRUE))
1747 /* save the current controller command byte */
1748 lockmgr(&sc->lock, LK_EXCLUSIVE);
1749 command_byte = get_controller_command_byte(sc->kbdc);
1751 /* enable the aux port and temporalily disable the keyboard */
1752 if (command_byte == -1 ||
1753 !set_controller_command_byte(sc->kbdc,
1754 KBD_AUX_CONTROL_BITS,
1755 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1756 /* CONTROLLER ERROR; do you know how to get out of this? */
1757 kbdc_lock(sc->kbdc, FALSE);
1758 lockmgr(&sc->lock, LK_RELEASE);
1760 "psm%d: unable to set the command byte (psmopen).\n",
1765 * Now that the keyboard controller is told not to generate
1766 * the keyboard and mouse interrupts, allow
1767 * the other tty interrupts. The clock interrupt may also occur,
1768 * but timeout routines will be blocked by the poll flag set
1771 lockmgr(&sc->lock, LK_RELEASE);
1773 /* enable the mouse device */
1774 err = doopen(sc, command_byte);
1778 sc->state |= PSM_OPEN;
1779 kbdc_lock(sc->kbdc, FALSE);
1784 psmclose(struct dev_close_args *ap)
1787 cdev_t dev = ap->a_head.a_dev;
1788 struct psm_softc *sc = dev->si_drv1;
1792 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1793 if (!kbdc_lock(sc->kbdc, TRUE))
1796 /* save the current controller command byte */
1797 lockmgr(&sc->lock, LK_EXCLUSIVE);
1798 command_byte = get_controller_command_byte(sc->kbdc);
1799 if (command_byte == -1) {
1800 kbdc_lock(sc->kbdc, FALSE);
1801 lockmgr(&sc->lock, LK_RELEASE);
1805 /* disable the aux interrupt and temporalily disable the keyboard */
1806 if (!set_controller_command_byte(sc->kbdc,
1807 KBD_AUX_CONTROL_BITS,
1808 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1810 "psm%d: failed to disable the aux int (psmclose).\n",
1812 /* CONTROLLER ERROR;
1813 * NOTE: we shall force our way through. Because the only
1814 * ill effect we shall see is that we may not be able
1815 * to read ACK from the mouse, and it doesn't matter much
1816 * so long as the mouse will accept the DISABLE command.
1819 lockmgr(&sc->lock, LK_RELEASE);
1821 /* stop the watchdog timer */
1822 callout_stop(&sc->callout);
1824 /* remove anything left in the output buffer */
1825 empty_aux_buffer(sc->kbdc, 10);
1827 /* disable the aux device, port and interrupt */
1828 if (sc->state & PSM_VALID) {
1829 if (!disable_aux_dev(sc->kbdc)) {
1831 * NOTE: we don't return (error) and continue,
1832 * pretending we have successfully disabled the device.
1833 * It's OK because the interrupt routine will discard
1834 * any data from the mouse hereafter.
1837 "psm%d: failed to disable the device (psmclose).\n",
1841 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1843 "psm%d: failed to get status (psmclose).\n",
1847 if (!set_controller_command_byte(sc->kbdc,
1848 KBD_AUX_CONTROL_BITS,
1849 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1852 * we shall ignore this error; see the above comment.
1855 "psm%d: failed to disable the aux port (psmclose).\n",
1859 /* remove anything left in the output buffer */
1860 empty_aux_buffer(sc->kbdc, 10);
1862 /* close is almost always successful */
1863 sc->state &= ~PSM_OPEN;
1864 kbdc_lock(sc->kbdc, FALSE);
1865 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
1870 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
1874 static u_char butmapps2[8] = {
1876 MOUSE_PS2_BUTTON1DOWN,
1877 MOUSE_PS2_BUTTON2DOWN,
1878 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1879 MOUSE_PS2_BUTTON3DOWN,
1880 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1881 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1882 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
1883 MOUSE_PS2_BUTTON3DOWN,
1885 static u_char butmapmsc[8] = {
1886 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
1887 MOUSE_MSC_BUTTON3UP,
1888 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1889 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1890 MOUSE_MSC_BUTTON3UP,
1891 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1892 MOUSE_MSC_BUTTON2UP,
1893 MOUSE_MSC_BUTTON1UP,
1899 if (sc->mode.level == PSM_LEVEL_BASE) {
1900 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1901 if (status->button & MOUSE_BUTTON4DOWN)
1902 mapped |= MOUSE_BUTTON1DOWN;
1903 status->button = mapped;
1904 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1905 i = imax(imin(status->dx, 255), -256);
1907 buf[0] |= MOUSE_PS2_XNEG;
1909 i = imax(imin(status->dy, 255), -256);
1911 buf[0] |= MOUSE_PS2_YNEG;
1913 return (MOUSE_PS2_PACKETSIZE);
1914 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1915 buf[0] = MOUSE_MSC_SYNC |
1916 butmapmsc[status->button & MOUSE_STDBUTTONS];
1917 i = imax(imin(status->dx, 255), -256);
1919 buf[3] = i - buf[1];
1920 i = imax(imin(status->dy, 255), -256);
1922 buf[4] = i - buf[2];
1923 i = imax(imin(status->dz, 127), -128);
1924 buf[5] = (i >> 1) & 0x7f;
1925 buf[6] = (i - (i >> 1)) & 0x7f;
1926 buf[7] = (~status->button >> 3) & 0x7f;
1927 return (MOUSE_SYS_PACKETSIZE);
1929 return (pb->inputbytes);
1933 psmread(struct dev_read_args *ap)
1936 cdev_t dev = ap->a_head.a_dev;
1937 struct uio *uio = ap->a_uio;
1938 struct psm_softc *sc = dev->si_drv1;
1939 u_char buf[PSM_SMALLBUFSIZE];
1943 if ((sc->state & PSM_VALID) == 0)
1946 /* block until mouse activity occurred */
1947 lockmgr(&sc->lock, LK_EXCLUSIVE);
1948 while (sc->queue.count <= 0) {
1949 if (ap->a_ioflag & IO_NDELAY) {
1950 lockmgr(&sc->lock, LK_RELEASE);
1951 return (EWOULDBLOCK);
1953 sc->state |= PSM_ASLP;
1954 error = lksleep(sc, &sc->lock, PCATCH, "psmrea", 0);
1955 sc->state &= ~PSM_ASLP;
1957 lockmgr(&sc->lock, LK_RELEASE);
1959 } else if ((sc->state & PSM_VALID) == 0) {
1960 /* the device disappeared! */
1961 lockmgr(&sc->lock, LK_RELEASE);
1965 lockmgr(&sc->lock, LK_RELEASE);
1967 /* copy data to the user land */
1968 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1969 lockmgr(&sc->lock, LK_EXCLUSIVE);
1970 l = imin(sc->queue.count, uio->uio_resid);
1971 if (l > sizeof(buf))
1973 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1974 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1975 sizeof(sc->queue.buf) - sc->queue.head);
1976 bcopy(&sc->queue.buf[0],
1977 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1978 l - (sizeof(sc->queue.buf) - sc->queue.head));
1980 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1981 sc->queue.count -= l;
1982 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1983 lockmgr(&sc->lock, LK_RELEASE);
1984 error = uiomove(buf, l, uio);
1993 block_mouse_data(struct psm_softc *sc, int *c)
1996 if (!kbdc_lock(sc->kbdc, TRUE))
1999 lockmgr(&sc->lock, LK_EXCLUSIVE);
2000 *c = get_controller_command_byte(sc->kbdc);
2002 !set_controller_command_byte(sc->kbdc,
2003 KBD_AUX_CONTROL_BITS,
2004 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2005 /* this is CONTROLLER ERROR */
2006 lockmgr(&sc->lock, LK_RELEASE);
2007 kbdc_lock(sc->kbdc, FALSE);
2012 * The device may be in the middle of status data transmission.
2013 * The transmission will be interrupted, thus, incomplete status
2014 * data must be discarded. Although the aux interrupt is disabled
2015 * at the keyboard controller level, at most one aux interrupt
2016 * may have already been pending and a data byte is in the
2017 * output buffer; throw it away. Note that the second argument
2018 * to `empty_aux_buffer()' is zero, so that the call will just
2019 * flush the internal queue.
2020 * `psmintr()' will be invoked after the lock is released if an
2021 * interrupt is pending; it will see no data and returns immediately.
2023 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
2024 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
2026 lockmgr(&sc->lock, LK_RELEASE);
2032 dropqueue(struct psm_softc *sc)
2035 sc->queue.count = 0;
2038 if ((sc->state & PSM_SOFTARMED) != 0) {
2039 sc->state &= ~PSM_SOFTARMED;
2040 callout_stop(&sc->softcallout);
2042 sc->pqueue_start = sc->pqueue_end;
2046 flushpackets(struct psm_softc *sc)
2050 bzero(&sc->pqueue, sizeof(sc->pqueue));
2054 unblock_mouse_data(struct psm_softc *sc, int c)
2060 * We may have seen a part of status data during `set_mouse_XXX()'.
2061 * they have been queued; flush it.
2063 empty_aux_buffer(sc->kbdc, 0);
2065 /* restore ports and interrupt */
2066 if (!set_controller_command_byte(sc->kbdc,
2067 KBD_AUX_CONTROL_BITS, c & KBD_AUX_CONTROL_BITS)) {
2069 * CONTROLLER ERROR; this is serious, we may have
2070 * been left with the inaccessible keyboard and
2071 * the disabled mouse interrupt.
2076 kbdc_lock(sc->kbdc, FALSE);
2082 psmioctl(struct dev_ioctl_args *ap)
2084 cdev_t dev = ap->a_head.a_dev;
2085 caddr_t addr= ap->a_data;
2086 struct psm_softc *sc = dev->si_drv1;
2088 mousestatus_t status;
2089 #if (defined(MOUSE_GETVARS))
2097 /* Perform IOCTL command */
2098 switch (ap->a_cmd) {
2100 case OLD_MOUSE_GETHWINFO:
2101 lockmgr(&sc->lock, LK_EXCLUSIVE);
2102 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
2103 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
2104 ((old_mousehw_t *)addr)->type = sc->hw.type;
2105 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
2106 lockmgr(&sc->lock, LK_RELEASE);
2109 case MOUSE_GETHWINFO:
2110 lockmgr(&sc->lock, LK_EXCLUSIVE);
2111 *(mousehw_t *)addr = sc->hw;
2112 if (sc->mode.level == PSM_LEVEL_BASE)
2113 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
2114 lockmgr(&sc->lock, LK_RELEASE);
2117 case MOUSE_SYN_GETHWINFO:
2118 lockmgr(&sc->lock, LK_EXCLUSIVE);
2119 if (sc->synhw.infoMajor >= 4)
2120 *(synapticshw_t *)addr = sc->synhw;
2123 lockmgr(&sc->lock, LK_RELEASE);
2126 case OLD_MOUSE_GETMODE:
2127 lockmgr(&sc->lock, LK_EXCLUSIVE);
2128 switch (sc->mode.level) {
2129 case PSM_LEVEL_BASE:
2130 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2132 case PSM_LEVEL_STANDARD:
2133 ((old_mousemode_t *)addr)->protocol =
2134 MOUSE_PROTO_SYSMOUSE;
2137 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
2138 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2139 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2140 lockmgr(&sc->lock, LK_RELEASE);
2144 lockmgr(&sc->lock, LK_EXCLUSIVE);
2145 *(mousemode_t *)addr = sc->mode;
2146 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2147 ((mousemode_t *)addr)->syncmask[0] = 0;
2148 ((mousemode_t *)addr)->syncmask[1] = 0;
2150 ((mousemode_t *)addr)->resolution =
2151 MOUSE_RES_LOW - sc->mode.resolution;
2152 switch (sc->mode.level) {
2153 case PSM_LEVEL_BASE:
2154 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2155 ((mousemode_t *)addr)->packetsize =
2156 MOUSE_PS2_PACKETSIZE;
2158 case PSM_LEVEL_STANDARD:
2159 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2160 ((mousemode_t *)addr)->packetsize =
2161 MOUSE_SYS_PACKETSIZE;
2162 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2163 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2166 lockmgr(&sc->lock, LK_RELEASE);
2169 case OLD_MOUSE_SETMODE:
2171 if (ap->a_cmd == OLD_MOUSE_SETMODE) {
2172 mode.rate = ((old_mousemode_t *)addr)->rate;
2174 * resolution old I/F new I/F
2181 if (((old_mousemode_t *)addr)->resolution > 0)
2183 -((old_mousemode_t *)addr)->resolution - 1;
2185 mode.resolution = 0;
2187 ((old_mousemode_t *)addr)->accelfactor;
2190 mode = *(mousemode_t *)addr;
2192 /* adjust and validate parameters. */
2193 if (mode.rate > UCHAR_MAX)
2196 mode.rate = sc->dflt_mode.rate;
2197 else if (mode.rate == -1)
2198 /* don't change the current setting */
2200 else if (mode.rate < 0)
2202 if (mode.resolution >= UCHAR_MAX)
2204 if (mode.resolution >= 200)
2205 mode.resolution = MOUSE_RES_HIGH;
2206 else if (mode.resolution >= 100)
2207 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2208 else if (mode.resolution >= 50)
2209 mode.resolution = MOUSE_RES_MEDIUMLOW;
2210 else if (mode.resolution > 0)
2211 mode.resolution = MOUSE_RES_LOW;
2212 if (mode.resolution == MOUSE_RES_DEFAULT)
2213 mode.resolution = sc->dflt_mode.resolution;
2214 else if (mode.resolution == -1)
2215 /* don't change the current setting */
2217 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2218 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2219 if (mode.level == -1)
2220 /* don't change the current setting */
2221 mode.level = sc->mode.level;
2222 else if ((mode.level < PSM_LEVEL_MIN) ||
2223 (mode.level > PSM_LEVEL_MAX))
2225 if (mode.accelfactor == -1)
2226 /* don't change the current setting */
2227 mode.accelfactor = sc->mode.accelfactor;
2228 else if (mode.accelfactor < 0)
2231 /* don't allow anybody to poll the keyboard controller */
2232 error = block_mouse_data(sc, &command_byte);
2236 /* set mouse parameters */
2238 mode.rate = set_mouse_sampling_rate(sc->kbdc,
2240 if (mode.resolution >= 0)
2242 set_mouse_resolution(sc->kbdc, mode.resolution);
2243 set_mouse_scaling(sc->kbdc, 1);
2244 get_mouse_status(sc->kbdc, stat, 0, 3);
2246 lockmgr(&sc->lock, LK_EXCLUSIVE);
2247 sc->mode.rate = mode.rate;
2248 sc->mode.resolution = mode.resolution;
2249 sc->mode.accelfactor = mode.accelfactor;
2250 sc->mode.level = mode.level;
2251 lockmgr(&sc->lock, LK_RELEASE);
2253 unblock_mouse_data(sc, command_byte);
2256 case MOUSE_GETLEVEL:
2257 *(int *)addr = sc->mode.level;
2260 case MOUSE_SETLEVEL:
2261 if ((*(int *)addr < PSM_LEVEL_MIN) ||
2262 (*(int *)addr > PSM_LEVEL_MAX))
2264 sc->mode.level = *(int *)addr;
2267 case MOUSE_GETSTATUS:
2268 lockmgr(&sc->lock, LK_EXCLUSIVE);
2269 status = sc->status;
2270 sc->status.flags = 0;
2271 sc->status.obutton = sc->status.button;
2272 sc->status.button = 0;
2276 lockmgr(&sc->lock, LK_RELEASE);
2277 *(mousestatus_t *)addr = status;
2280 #if (defined(MOUSE_GETVARS))
2282 var = (mousevar_t *)addr;
2283 bzero(var, sizeof(*var));
2284 lockmgr(&sc->lock, LK_EXCLUSIVE);
2285 var->var[0] = MOUSE_VARS_PS2_SIG;
2286 var->var[1] = sc->config;
2287 var->var[2] = sc->flags;
2288 lockmgr(&sc->lock, LK_RELEASE);
2293 #endif /* MOUSE_GETVARS */
2295 case MOUSE_READSTATE:
2296 case MOUSE_READDATA:
2297 data = (mousedata_t *)addr;
2298 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2301 error = block_mouse_data(sc, &command_byte);
2304 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2305 (ap->a_cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2307 unblock_mouse_data(sc, command_byte);
2310 #if (defined(MOUSE_SETRESOLUTION))
2311 case MOUSE_SETRESOLUTION:
2312 mode.resolution = *(int *)addr;
2313 if (mode.resolution >= UCHAR_MAX)
2315 else if (mode.resolution >= 200)
2316 mode.resolution = MOUSE_RES_HIGH;
2317 else if (mode.resolution >= 100)
2318 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2319 else if (mode.resolution >= 50)
2320 mode.resolution = MOUSE_RES_MEDIUMLOW;
2321 else if (mode.resolution > 0)
2322 mode.resolution = MOUSE_RES_LOW;
2323 if (mode.resolution == MOUSE_RES_DEFAULT)
2324 mode.resolution = sc->dflt_mode.resolution;
2325 else if (mode.resolution == -1)
2326 mode.resolution = sc->mode.resolution;
2327 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2328 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2330 error = block_mouse_data(sc, &command_byte);
2333 sc->mode.resolution =
2334 set_mouse_resolution(sc->kbdc, mode.resolution);
2335 if (sc->mode.resolution != mode.resolution)
2337 unblock_mouse_data(sc, command_byte);
2339 #endif /* MOUSE_SETRESOLUTION */
2341 #if (defined(MOUSE_SETRATE))
2343 mode.rate = *(int *)addr;
2344 if (mode.rate > UCHAR_MAX)
2347 mode.rate = sc->dflt_mode.rate;
2348 else if (mode.rate < 0)
2349 mode.rate = sc->mode.rate;
2351 error = block_mouse_data(sc, &command_byte);
2354 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2355 if (sc->mode.rate != mode.rate)
2357 unblock_mouse_data(sc, command_byte);
2359 #endif /* MOUSE_SETRATE */
2361 #if (defined(MOUSE_SETSCALING))
2362 case MOUSE_SETSCALING:
2363 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2366 error = block_mouse_data(sc, &command_byte);
2369 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2371 unblock_mouse_data(sc, command_byte);
2373 #endif /* MOUSE_SETSCALING */
2375 #if (defined(MOUSE_GETHWID))
2377 error = block_mouse_data(sc, &command_byte);
2380 sc->hw.hwid &= ~0x00ff;
2381 sc->hw.hwid |= get_aux_id(sc->kbdc);
2382 *(int *)addr = sc->hw.hwid & 0x00ff;
2383 unblock_mouse_data(sc, command_byte);
2385 #endif /* MOUSE_GETHWID */
2402 psmtimeout(void *arg)
2405 struct psm_softc *sc;
2407 sc = (struct psm_softc *)arg;
2408 lockmgr(&sc->lock, LK_EXCLUSIVE);
2409 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2410 VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2412 kbdc_lock(sc->kbdc, FALSE);
2414 sc->watchdog = TRUE;
2415 lockmgr(&sc->lock, LK_RELEASE);
2416 callout_reset(&sc->callout, hz, psmtimeout, (void *)(uintptr_t)sc);
2419 /* Add all sysctls under the debug.psm and hw.psm nodes */
2420 SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2421 SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2423 SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RW, &verbose, 0,
2426 static int psmhz = 20;
2427 SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2428 "Frequency of the softcallout (in hz)");
2429 static int psmerrsecs = 2;
2430 SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2431 "Number of seconds during which packets will dropped after a sync error");
2432 static int psmerrusecs = 0;
2433 SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2434 "Microseconds to add to psmerrsecs");
2435 static int psmsecs = 0;
2436 SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2437 "Max number of seconds between soft interrupts");
2438 static int psmusecs = 500000;
2439 SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2440 "Microseconds to add to psmsecs");
2441 static int pkterrthresh = 2;
2442 SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2443 "Number of error packets allowed before reinitializing the mouse");
2445 SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RW, &tap_enabled, 0,
2446 "Enable tap and drag gestures");
2447 static int tap_threshold = PSM_TAP_THRESHOLD;
2448 SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2449 "Button tap threshold");
2450 static int tap_timeout = PSM_TAP_TIMEOUT;
2451 SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2452 "Tap timeout for touchpads");
2454 /* Tunables; defined near top of file */
2455 SYSCTL_INT(_hw_psm, OID_AUTO, synaptics_support, CTLFLAG_RD,
2456 &synaptics_support, 0, "Enable support for Synaptics touchpads");
2458 SYSCTL_INT(_hw_psm, OID_AUTO, trackpoint_support, CTLFLAG_RD,
2459 &trackpoint_support, 0, "Enable support for IBM/Lenovo TrackPoint");
2461 SYSCTL_INT(_hw_psm, OID_AUTO, elantech_support, CTLFLAG_RD,
2462 &elantech_support, 0, "Enable support for Elantech touchpads");
2467 struct psm_softc *sc = arg;
2473 /* read until there is nothing to read */
2474 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2475 pb = &sc->pqueue[sc->pqueue_end];
2477 /* discard the byte if the device is not open */
2478 if ((sc->state & PSM_OPEN) == 0)
2481 getmicrouptime(&now);
2482 if ((pb->inputbytes > 0) &&
2483 timevalcmp(&now, &sc->inputtimeout, >)) {
2484 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2485 "resetting byte count\n"));
2490 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2491 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2492 timevaladd(&sc->inputtimeout, &now);
2494 pb->ipacket[pb->inputbytes++] = c;
2496 if (pb->inputbytes < sc->mode.packetsize)
2500 "psmintr: %02x %02x %02x %02x %02x %02x\n",
2501 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2502 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2506 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2507 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2508 sc->flags &= ~PSM_NEED_SYNCBITS;
2510 "psmintr: Sync bytes now %04x,%04x\n",
2511 sc->mode.syncmask[0], sc->mode.syncmask[0]));
2512 } else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2513 VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
2514 "(%04x != %04x) %d cmds since last error.\n",
2515 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2516 sc->cmdcount - sc->lasterr));
2517 sc->lasterr = sc->cmdcount;
2519 * The sync byte test is a weak measure of packet
2520 * validity. Conservatively discard any input yet
2521 * to be seen by userland when we detect a sync
2522 * error since there is a good chance some of
2523 * the queued packets have undetected errors.
2526 if (sc->syncerrors == 0)
2529 sc->lastinputerr = now;
2530 if (sc->syncerrors >= sc->mode.packetsize * 2 ||
2531 sc->pkterrors >= pkterrthresh) {
2533 * If we've failed to find a single sync byte
2534 * in 2 packets worth of data, or we've seen
2535 * persistent packet errors during the
2536 * validation period, reinitialize the mouse
2537 * in hopes of returning it to the expected
2541 "psmintr: reset the mouse.\n"));
2542 reinitialize(sc, TRUE);
2543 } else if (sc->syncerrors == sc->mode.packetsize) {
2545 * Try a soft reset after searching for a sync
2546 * byte through a packet length of bytes.
2549 "psmintr: re-enable the mouse.\n"));
2551 disable_aux_dev(sc->kbdc);
2552 enable_aux_dev(sc->kbdc);
2555 "psmintr: discard a byte (%d)\n",
2558 bcopy(&pb->ipacket[1], &pb->ipacket[0],
2565 * We have what appears to be a valid packet.
2566 * Reset the error counters.
2571 * Drop even good packets if they occur within a timeout
2572 * period of a sync error. This allows the detection of
2573 * a change in the mouse's packet mode without exposing
2574 * erratic mouse behavior to the user. Some KVMs forget
2575 * enhanced mouse modes during switch events.
2577 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
2584 * Now that we're out of the validation period, reset
2585 * the packet error count.
2590 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2593 * If we've filled the queue then call the softintr ourselves,
2594 * otherwise schedule the interrupt for later.
2596 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2597 (sc->pqueue_end == sc->pqueue_start)) {
2598 if ((sc->state & PSM_SOFTARMED) != 0) {
2599 sc->state &= ~PSM_SOFTARMED;
2600 callout_stop(&sc->softcallout);
2603 } else if ((sc->state & PSM_SOFTARMED) == 0) {
2604 sc->state |= PSM_SOFTARMED;
2605 callout_reset(&sc->softcallout, psmhz < 1 ? 1 : (hz/psmhz),
2612 proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2613 int *x, int *y, int *z)
2617 * PS2++ protocl packet
2619 * b7 b6 b5 b4 b3 b2 b1 b0
2620 * byte 1: * 1 p3 p2 1 * * *
2621 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2623 * p3-p0: packet type
2624 * c1, c2: c1 & c2 == 1, if p2 == 0
2625 * c1 & c2 == 0, if p2 == 1
2627 * packet type: 0 (device type)
2628 * See comments in enable_mmanplus() below.
2630 * packet type: 1 (wheel data)
2632 * b7 b6 b5 b4 b3 b2 b1 b0
2633 * byte 3: h * B5 B4 s d2 d1 d0
2635 * h: 1, if horizontal roller data
2636 * 0, if vertical roller data
2637 * B4, B5: button 4 and 5
2639 * d2-d0: roller data
2641 * packet type: 2 (reserved)
2643 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
2644 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2646 * the extended data packet encodes button
2649 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
2651 /* wheel data packet */
2653 if (pb->ipacket[2] & 0x80) {
2654 /* XXX horizontal roller count - ignore it */
2657 /* vertical roller count */
2658 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
2659 (pb->ipacket[2] & 0x0f) - 16 :
2660 (pb->ipacket[2] & 0x0f);
2662 ms->button |= (pb->ipacket[2] &
2663 MOUSE_PS2PLUS_BUTTON4DOWN) ?
2664 MOUSE_BUTTON4DOWN : 0;
2665 ms->button |= (pb->ipacket[2] &
2666 MOUSE_PS2PLUS_BUTTON5DOWN) ?
2667 MOUSE_BUTTON5DOWN : 0;
2671 * this packet type is reserved by
2675 * IBM ScrollPoint Mouse uses this
2676 * packet type to encode both vertical
2677 * and horizontal scroll movement.
2680 /* horizontal count */
2681 if (pb->ipacket[2] & 0x0f)
2682 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
2684 /* vertical count */
2685 if (pb->ipacket[2] & 0xf0)
2686 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
2690 /* device type packet - shouldn't happen
2692 * XXX: if this shouldn't happen, shouldn't we at least
2693 * add a kprintf indiciating to the user something bad
2694 * happened or is it that prevalent that it'd spam? -htse
2700 ms->button = ms->obutton;
2701 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
2702 "type %d: 0x%02x 0x%02x 0x%02x\n",
2703 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2704 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
2708 /* preserve button states */
2709 ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
2714 proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2715 int *x, int *y, int *z)
2717 static int touchpad_buttons;
2718 static int guest_buttons;
2719 static finger_t f[PSM_FINGERS];
2720 int w, id, nfingers, ewcode, extended_buttons;
2722 extended_buttons = 0;
2724 /* TouchPad PS/2 absolute mode message format with capFourButtons:
2726 * Bits: 7 6 5 4 3 2 1 0 (LSB)
2727 * ------------------------------------------------
2728 * ipacket[0]: 1 0 W3 W2 0 W1 R L
2729 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
2730 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
2731 * ipacket[3]: 1 1 Yc Xc 0 W0 D U
2732 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
2733 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
2736 * L: left physical mouse button
2737 * R: right physical mouse button
2746 * Without capFourButtons but with nExtendeButtons and/or capMiddle
2748 * Bits: 7 6 5 4 3 2 1 0 (LSB)
2749 * ------------------------------------------------------
2750 * ipacket[3]: 1 1 Yc Xc 0 W0 E^R M^L
2751 * ipacket[4]: X7 X6 X5 X4 X3|b7 X2|b5 X1|b3 X0|b1
2752 * ipacket[5]: Y7 Y6 Y5 Y4 Y3|b8 Y2|b6 Y1|b4 Y0|b2
2755 * M: Middle physical mouse button
2756 * E: Extended mouse buttons reported instead of low bits of X and Y
2757 * b1-b8: Extended mouse buttons
2758 * Only ((nExtendedButtons + 1) >> 1) bits are used in packet
2759 * 4 and 5, for reading X and Y value they should be zeroed.
2761 * Absolute reportable limits: 0 - 6143.
2762 * Typical bezel limits: 1472 - 5472.
2763 * Typical edge marings: 1632 - 5312.
2765 * w = 3 Passthrough Packet
2767 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2770 if (!synaptics_support)
2773 /* Sanity check for out of sync packets. */
2774 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2775 (pb->ipacket[3] & 0xc8) != 0xc0)
2779 ms->button = ms->obutton;
2784 * z = 0 No finger contact
2785 * z = 10 Finger hovering near the pad
2786 * z = 30 Very light finger contact
2787 * z = 80 Normal finger contact
2788 * z = 110 Very heavy finger contact
2789 * z = 200 Finger lying flat on pad surface
2790 * z = 255 Maximum reportable Z
2792 *z = pb->ipacket[2];
2795 * Finger width value
2797 * w = 0 Two finger on the pad (capMultiFinger needed)
2798 * w = 1 Three or more fingers (capMultiFinger needed)
2799 * w = 2 Pen (instead of finger) (capPen needed)
2800 * w = 3 Reserved (passthrough?)
2801 * w = 4-7 Finger of normal width (capPalmDetect needed)
2802 * w = 8-14 Very wide finger or palm (capPalmDetect needed)
2803 * w = 15 Maximum reportable width (capPalmDetect needed)
2805 /* XXX Is checking capExtended enough? */
2806 if (sc->synhw.capExtended) {
2807 w = ((pb->ipacket[0] & 0x30) >> 2) |
2808 ((pb->ipacket[0] & 0x04) >> 1) |
2809 ((pb->ipacket[3] & 0x04) >> 2);
2812 /* Assume a finger of regular width. */
2819 * Handle packets from the guest device. See:
2820 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
2822 if (sc->synhw.capPassthrough) {
2823 *x = ((pb->ipacket[1] & 0x10) ?
2824 pb->ipacket[4] - 256 : pb->ipacket[4]);
2825 *y = ((pb->ipacket[1] & 0x20) ?
2826 pb->ipacket[5] - 256 : pb->ipacket[5]);
2830 if (pb->ipacket[1] & 0x01)
2831 guest_buttons |= MOUSE_BUTTON1DOWN;
2832 if (pb->ipacket[1] & 0x04)
2833 guest_buttons |= MOUSE_BUTTON2DOWN;
2834 if (pb->ipacket[1] & 0x02)
2835 guest_buttons |= MOUSE_BUTTON3DOWN;
2837 ms->button = touchpad_buttons | guest_buttons |
2838 sc->extended_buttons;
2843 /* Handle Extended W mode packets */
2844 ewcode = (pb->ipacket[5] & 0xf0) >> 4;
2848 /* Secondary finger */
2849 if (sc->synhw.capAdvancedGestures)
2851 .x = (((pb->ipacket[4] & 0x0f) << 8) |
2852 pb->ipacket[1]) << 1,
2853 .y = (((pb->ipacket[4] & 0xf0) << 4) |
2854 pb->ipacket[2]) << 1,
2855 .p = ((pb->ipacket[3] & 0x30) |
2856 (pb->ipacket[5] & 0x0f)) << 1,
2857 .w = PSM_FINGER_DEFAULT_W,
2858 .flags = PSM_FINGER_FUZZY,
2860 else if (sc->synhw.capReportsV)
2862 .x = (((pb->ipacket[4] & 0x0f) << 8) |
2863 (pb->ipacket[1] & 0xfe)) << 1,
2864 .y = (((pb->ipacket[4] & 0xf0) << 4) |
2865 (pb->ipacket[2] & 0xfe)) << 1,
2866 .p = ((pb->ipacket[3] & 0x30) |
2867 (pb->ipacket[5] & 0x0e)) << 1,
2868 .w = (((pb->ipacket[5] & 0x01) << 2) |
2869 ((pb->ipacket[2] & 0x01) << 1) |
2870 (pb->ipacket[1] & 0x01)) + 8,
2871 .flags = PSM_FINGER_FUZZY,
2888 if (sc->syninfo.touchpad_off)
2891 /* Button presses */
2892 touchpad_buttons = 0;
2893 if (pb->ipacket[0] & 0x01)
2894 touchpad_buttons |= MOUSE_BUTTON1DOWN;
2895 if (pb->ipacket[0] & 0x02)
2896 touchpad_buttons |= MOUSE_BUTTON3DOWN;
2898 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2899 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
2900 touchpad_buttons |= MOUSE_BUTTON4DOWN;
2901 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
2902 touchpad_buttons |= MOUSE_BUTTON5DOWN;
2903 } else if (sc->synhw.capExtended && sc->synhw.capMiddle &&
2904 !sc->synhw.capClickPad) {
2906 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
2907 touchpad_buttons |= MOUSE_BUTTON2DOWN;
2908 } else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
2909 /* Extended Buttons */
2910 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
2911 if (sc->syninfo.directional_scrolls) {
2912 if (pb->ipacket[4] & 0x01)
2913 extended_buttons |= MOUSE_BUTTON4DOWN;
2914 if (pb->ipacket[5] & 0x01)
2915 extended_buttons |= MOUSE_BUTTON5DOWN;
2916 if (pb->ipacket[4] & 0x02)
2917 extended_buttons |= MOUSE_BUTTON6DOWN;
2918 if (pb->ipacket[5] & 0x02)
2919 extended_buttons |= MOUSE_BUTTON7DOWN;
2921 if (pb->ipacket[4] & 0x01)
2922 extended_buttons |= MOUSE_BUTTON1DOWN;
2923 if (pb->ipacket[5] & 0x01)
2924 extended_buttons |= MOUSE_BUTTON3DOWN;
2925 if (pb->ipacket[4] & 0x02)
2926 extended_buttons |= MOUSE_BUTTON2DOWN;
2927 sc->extended_buttons = extended_buttons;
2931 * Zero out bits used by extended buttons to avoid
2932 * misinterpretation of the data absolute position.
2934 * The bits represented by
2936 * (nExtendedButtons + 1) >> 1
2938 * will be masked out in both bytes.
2939 * The mask for n bits is computed with the formula
2945 maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
2946 mask = (1 << maskedbits) - 1;
2947 pb->ipacket[4] &= ~(mask);
2948 pb->ipacket[5] &= ~(mask);
2949 } else if (!sc->syninfo.directional_scrolls &&
2950 !sc->gesture.in_vscroll) {
2952 * Keep reporting MOUSE DOWN until we get a new packet
2953 * indicating otherwise.
2955 extended_buttons |= sc->extended_buttons;
2958 /* Handle ClickPad */
2959 if (sc->synhw.capClickPad &&
2960 ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01))
2961 touchpad_buttons |= MOUSE_BUTTON1DOWN;
2963 if (sc->synhw.capReportsV && nfingers > 1)
2965 .x = ((pb->ipacket[3] & 0x10) << 8) |
2966 ((pb->ipacket[1] & 0x0f) << 8) |
2967 (pb->ipacket[4] & 0xfd),
2968 .y = ((pb->ipacket[3] & 0x20) << 7) |
2969 ((pb->ipacket[1] & 0xf0) << 4) |
2970 (pb->ipacket[5] & 0xfd),
2972 .w = (((pb->ipacket[2] & 0x01) << 2) |
2973 (pb->ipacket[5] & 0x02) |
2974 ((pb->ipacket[4] & 0x02) >> 1)) + 8,
2975 .flags = PSM_FINGER_FUZZY,
2979 .x = ((pb->ipacket[3] & 0x10) << 8) |
2980 ((pb->ipacket[1] & 0x0f) << 8) |
2982 .y = ((pb->ipacket[3] & 0x20) << 7) |
2983 ((pb->ipacket[1] & 0xf0) << 4) |
2987 .flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0,
2990 /* Ignore hovering and unmeasurable touches */
2991 if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2)
2994 for (id = 0; id < PSM_FINGERS; id++)
2996 PSM_FINGER_RESET(f[id]);
2998 ms->button = touchpad_buttons;
3000 /* Palm detection doesn't terminate the current action. */
3001 if (!psmpalmdetect(sc, &f[0], nfingers)) {
3002 psmgestures(sc, &f[0], nfingers, ms);
3003 for (id = 0; id < PSM_FINGERS; id++)
3004 psmsmoother(sc, &f[id], id, ms, x, y);
3006 VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f[0].w));
3009 ms->button |= extended_buttons | guest_buttons;
3015 * Use the extra buttons as a scrollwheel
3017 * XXX X.Org uses the Z axis for vertical wheel only,
3018 * whereas moused(8) understands special values to differ
3019 * vertical and horizontal wheels.
3021 * xf86-input-mouse needs therefore a small patch to
3022 * understand these special values. Without it, the
3023 * horizontal wheel acts as a vertical wheel in X.Org.
3025 * That's why the horizontal wheel is disabled by
3028 if (ms->button & MOUSE_BUTTON4DOWN) {
3030 } else if (ms->button & MOUSE_BUTTON5DOWN) {
3032 } else if (ms->button & MOUSE_BUTTON6DOWN) {
3034 } else if (ms->button & MOUSE_BUTTON7DOWN) {
3038 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
3039 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
3045 psmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers)
3048 ((sc->synhw.capMultiFinger ||
3049 sc->synhw.capAdvancedGestures) && nfingers > 1) ||
3050 (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) ||
3051 (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) ||
3052 (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) {
3054 * We consider the packet irrelevant for the current
3056 * - the width isn't comprised in:
3058 * - the pressure isn't comprised in:
3059 * [min_pressure; max_pressure]
3060 * - pen aren't supported but PSM_FINGER_IS_PEN is set
3068 psmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers,
3071 smoother_t *smoother;
3074 int y_ok, center_button, center_x, right_button, right_x, i;
3077 smoother = &sc->smoother[0];
3078 gest = &sc->gesture;
3080 /* Find first active finger. */
3082 for (i = 0; i < PSM_FINGERS; i++) {
3083 if (PSM_FINGER_IS_SET(fingers[i])) {
3085 smoother = &sc->smoother[i];
3092 * Check pressure to detect a real wanted action on the
3095 if (f->p >= sc->syninfo.min_pressure) {
3098 int start_x, start_y;
3100 int margin_top, margin_right, margin_bottom, margin_left;
3101 int window_min, window_max;
3102 int vscroll_hor_area, vscroll_ver_area;
3103 int two_finger_scroll;
3106 /* silence gcc warnings for last VLOG call */
3110 /* XXX Verify values? */
3111 margin_top = sc->syninfo.margin_top;
3112 margin_right = sc->syninfo.margin_right;
3113 margin_bottom = sc->syninfo.margin_bottom;
3114 margin_left = sc->syninfo.margin_left;
3115 window_min = sc->syninfo.window_min;
3116 window_max = sc->syninfo.window_max;
3117 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
3118 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
3119 two_finger_scroll = sc->syninfo.two_finger_scroll;
3120 max_x = sc->syninfo.max_x;
3121 max_y = sc->syninfo.max_y;
3123 /* Read current absolute position. */
3128 * Limit the coordinates to the specified margins because
3129 * this area isn't very reliable.
3131 if (x0 <= margin_left)
3133 else if (x0 >= max_x - margin_right)
3134 x0 = max_x - margin_right;
3135 if (y0 <= margin_bottom)
3137 else if (y0 >= max_y - margin_top)
3138 y0 = max_y - margin_top;
3140 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
3141 x0, y0, f->p, f->w));
3144 * If the action is just beginning, init the structure and
3145 * compute tap timeout.
3147 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
3148 VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
3150 /* Initialize queue. */
3151 gest->window_min = window_min;
3153 /* Reset pressure peak. */
3156 /* Reset fingers count. */
3157 gest->fingers_nb = 0;
3159 /* Reset virtual scrolling state. */
3160 gest->in_vscroll = 0;
3162 /* Compute tap timeout. */
3163 gest->taptimeout.tv_sec = tap_timeout / 1000000;
3164 gest->taptimeout.tv_usec = tap_timeout % 1000000;
3165 timevaladd(&gest->taptimeout, &sc->lastsoftintr);
3167 sc->flags |= PSM_FLAGS_FINGERDOWN;
3169 /* Smoother has not been reset yet */
3174 queue_len = smoother->queue_len + 1;
3175 start_x = smoother->start_x;
3176 start_y = smoother->start_y;
3179 /* Process ClickPad softbuttons */
3180 if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) {
3181 y_ok = sc->syninfo.softbuttons_y >= 0 ?
3182 start_y < sc->syninfo.softbuttons_y :
3183 start_y > max_y - sc->syninfo.softbuttons_y;
3185 center_button = MOUSE_BUTTON2DOWN;
3186 center_x = sc->syninfo.softbutton2_x;
3187 right_button = MOUSE_BUTTON3DOWN;
3188 right_x = sc->syninfo.softbutton3_x;
3190 if (center_x > 0 && right_x > 0 && center_x > right_x) {
3191 center_button = MOUSE_BUTTON3DOWN;
3192 center_x = sc->syninfo.softbutton3_x;
3193 right_button = MOUSE_BUTTON2DOWN;
3194 right_x = sc->syninfo.softbutton2_x;
3197 if (right_x > 0 && start_x > right_x && y_ok)
3198 ms->button = (ms->button &
3199 ~MOUSE_BUTTON1DOWN) | right_button;
3200 else if (center_x > 0 && start_x > center_x && y_ok)
3201 ms->button = (ms->button &
3202 ~MOUSE_BUTTON1DOWN) | center_button;
3205 /* If in tap-hold, add the recorded button. */
3206 if (gest->in_taphold)
3207 ms->button |= gest->tap_button;
3210 * For tap, we keep the maximum number of fingers and the
3211 * pressure peak. Also with multiple fingers, we increase
3212 * the minimum window.
3215 gest->window_min = window_max;
3216 gest->fingers_nb = imax(nfingers, gest->fingers_nb);
3217 gest->zmax = imax(f->p, gest->zmax);
3219 /* Do we have enough packets to consider this a gesture? */
3220 if (queue_len < gest->window_min)
3223 /* Is a scrolling action occurring? */
3224 if (!gest->in_taphold && !ms->button &&
3225 (!gest->in_vscroll || two_finger_scroll)) {
3227 * A scrolling action must not conflict with a tap
3228 * action. Here are the conditions to consider a
3230 * - the action in a configurable area
3231 * - one of the following:
3232 * . the distance between the last packet and the
3233 * first should be above a configurable minimum
3236 dxp = abs(x0 - start_x);
3237 dyp = abs(y0 - start_y);
3239 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) ||
3240 dxp >= sc->syninfo.vscroll_min_delta ||
3241 dyp >= sc->syninfo.vscroll_min_delta) {
3243 * Handle two finger scrolling.
3244 * Note that we don't rely on fingers_nb
3245 * as that keeps the maximum number of fingers.
3247 if (two_finger_scroll) {
3248 if (nfingers == 2) {
3255 /* Check for horizontal scrolling. */
3256 if ((vscroll_hor_area > 0 &&
3257 start_y <= vscroll_hor_area) ||
3258 (vscroll_hor_area < 0 &&
3260 max_y + vscroll_hor_area))
3261 gest->in_vscroll += 2;
3263 /* Check for vertical scrolling. */
3264 if ((vscroll_ver_area > 0 &&
3265 start_x <= vscroll_ver_area) ||
3266 (vscroll_ver_area < 0 &&
3268 max_x + vscroll_ver_area))
3269 gest->in_vscroll += 1;
3272 /* Avoid conflicts if area overlaps. */
3273 if (gest->in_vscroll >= 3)
3275 (dxp > dyp) ? 2 : 1;
3279 * Reset two finger scrolling when the number of fingers
3280 * is different from two or any button is pressed.
3282 if (two_finger_scroll && gest->in_vscroll != 0 &&
3283 (nfingers != 2 || ms->button))
3284 gest->in_vscroll = 0;
3287 "synaptics: virtual scrolling: %s "
3288 "(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n",
3289 gest->in_vscroll ? "YES" : "NO",
3290 gest->in_vscroll, dxp, dyp,
3293 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3295 * An action is currently taking place but the pressure
3296 * dropped under the minimum, putting an end to it.
3298 int taphold_timeout, dx, dy, tap_max_delta;
3300 dx = abs(smoother->queue[smoother->queue_cursor].x -
3302 dy = abs(smoother->queue[smoother->queue_cursor].y -
3305 /* Max delta is disabled for multi-fingers tap. */
3306 if (gest->fingers_nb > 1)
3307 tap_max_delta = imax(dx, dy);
3309 tap_max_delta = sc->syninfo.tap_max_delta;
3311 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3313 /* Check for tap. */
3315 "synaptics: zmax=%d, dx=%d, dy=%d, "
3316 "delta=%d, fingers=%d, queue=%d\n",
3317 gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb,
3318 smoother->queue_len));
3319 if (!gest->in_vscroll && gest->zmax >= tap_threshold &&
3320 timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) &&
3321 dx <= tap_max_delta && dy <= tap_max_delta &&
3322 smoother->queue_len >= sc->syninfo.tap_min_queue) {
3325 * - the maximum pressure went over tap_threshold
3326 * - the action ended before tap_timeout
3328 * To handle tap-hold, we must delay any button push to
3331 if (gest->in_taphold) {
3333 * This is the second and last tap of a
3334 * double tap action, not a tap-hold.
3336 gest->in_taphold = 0;
3339 * For double-tap to work:
3340 * - no button press is emitted (to
3341 * simulate a button release)
3342 * - PSM_FLAGS_FINGERDOWN is set to
3343 * force the next packet to emit a
3347 "synaptics: button RELEASE: %d\n",
3349 sc->flags |= PSM_FLAGS_FINGERDOWN;
3351 /* Schedule button press on next interrupt */
3352 sc->idletimeout.tv_sec = psmhz > 1 ?
3354 sc->idletimeout.tv_usec = psmhz > 1 ?
3355 1000000 / psmhz : 0;
3358 * This is the first tap: we set the
3359 * tap-hold state and notify the button
3362 gest->in_taphold = 1;
3363 taphold_timeout = sc->syninfo.taphold_timeout;
3364 gest->taptimeout.tv_sec = taphold_timeout /
3366 gest->taptimeout.tv_usec = taphold_timeout %
3368 sc->idletimeout = gest->taptimeout;
3369 timevaladd(&gest->taptimeout,
3372 switch (gest->fingers_nb) {
3386 "synaptics: button PRESS: %d\n",
3388 ms->button |= gest->tap_button;
3392 * Not enough pressure or timeout: reset
3395 if (gest->in_taphold) {
3397 "synaptics: button RELEASE: %d\n",
3399 gest->in_taphold = 0;
3402 "synaptics: not a tap-hold\n"));
3405 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) {
3407 * For a tap-hold to work, the button must remain down at
3408 * least until timeout (where the in_taphold flags will be
3409 * cleared) or during the next action.
3411 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) {
3412 ms->button |= gest->tap_button;
3414 VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n",
3416 gest->in_taphold = 0;
3424 psmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id,
3425 mousestatus_t *ms, int *x, int *y)
3427 smoother_t *smoother = &sc->smoother[smoother_id];
3428 gesture_t *gest = &(sc->gesture);
3431 * Check pressure to detect a real wanted action on the
3434 if (f->p >= sc->syninfo.min_pressure) {
3436 int cursor, peer, window;
3437 int dx, dy, dxp, dyp;
3438 int max_width, max_pressure;
3439 int margin_top, margin_right, margin_bottom, margin_left;
3440 int na_top, na_right, na_bottom, na_left;
3441 int window_min, window_max;
3443 int weight_current, weight_previous, weight_len_squared;
3444 int div_min, div_max, div_len;
3445 int vscroll_hor_area, vscroll_ver_area;
3446 int two_finger_scroll;
3448 int len, weight_prev_x, weight_prev_y;
3449 int div_max_x, div_max_y, div_x, div_y;
3453 /* XXX Verify values? */
3454 max_width = sc->syninfo.max_width;
3455 max_pressure = sc->syninfo.max_pressure;
3456 margin_top = sc->syninfo.margin_top;
3457 margin_right = sc->syninfo.margin_right;
3458 margin_bottom = sc->syninfo.margin_bottom;
3459 margin_left = sc->syninfo.margin_left;
3460 na_top = sc->syninfo.na_top;
3461 na_right = sc->syninfo.na_right;
3462 na_bottom = sc->syninfo.na_bottom;
3463 na_left = sc->syninfo.na_left;
3464 window_min = sc->syninfo.window_min;
3465 window_max = sc->syninfo.window_max;
3466 multiplicator = sc->syninfo.multiplicator;
3467 weight_current = sc->syninfo.weight_current;
3468 weight_previous = sc->syninfo.weight_previous;
3469 weight_len_squared = sc->syninfo.weight_len_squared;
3470 div_min = sc->syninfo.div_min;
3471 div_max = sc->syninfo.div_max;
3472 div_len = sc->syninfo.div_len;
3473 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
3474 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
3475 two_finger_scroll = sc->syninfo.two_finger_scroll;
3476 max_x = sc->syninfo.max_x;
3477 max_y = sc->syninfo.max_y;
3479 is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0;
3481 /* Read current absolute position. */
3486 * Limit the coordinates to the specified margins because
3487 * this area isn't very reliable.
3489 if (x0 <= margin_left)
3491 else if (x0 >= max_x - margin_right)
3492 x0 = max_x - margin_right;
3493 if (y0 <= margin_bottom)
3495 else if (y0 >= max_y - margin_top)
3496 y0 = max_y - margin_top;
3498 /* If the action is just beginning, init the structure. */
3499 if (smoother->active == 0) {
3500 VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id));
3502 /* Store the first point of this action. */
3503 smoother->start_x = x0;
3504 smoother->start_y = y0;
3507 /* Initialize queue. */
3508 smoother->queue_cursor = SYNAPTICS_PACKETQUEUE;
3509 smoother->queue_len = 0;
3511 /* Reset average. */
3512 smoother->avg_dx = 0;
3513 smoother->avg_dy = 0;
3515 /* Reset squelch. */
3516 smoother->squelch_x = 0;
3517 smoother->squelch_y = 0;
3519 /* Activate queue */
3520 smoother->active = 1;
3522 /* Calculate the current delta. */
3523 cursor = smoother->queue_cursor;
3524 dx = x0 - smoother->queue[cursor].x;
3525 dy = y0 - smoother->queue[cursor].y;
3528 VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n",
3529 smoother_id, x0, y0, f->p, f->w));
3531 /* Queue this new packet. */
3532 cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1);
3533 smoother->queue[cursor].x = x0;
3534 smoother->queue[cursor].y = y0;
3535 smoother->queue_cursor = cursor;
3536 if (smoother->queue_len < SYNAPTICS_PACKETQUEUE)
3537 smoother->queue_len++;
3539 "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
3540 smoother_id, cursor, x0, y0, dx, dy));
3542 /* Do we have enough packets to consider this a movement? */
3543 if (smoother->queue_len < gest->window_min)
3546 weight_prev_x = weight_prev_y = weight_previous;
3547 div_max_x = div_max_y = div_max;
3549 if (gest->in_vscroll) {
3550 /* Dividers are different with virtual scrolling. */
3551 div_min = sc->syninfo.vscroll_div_min;
3552 div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
3555 * There's a lot of noise in coordinates when
3556 * the finger is on the touchpad's borders. When
3557 * using this area, we apply a special weight and
3560 if (x0 <= na_left || x0 >= max_x - na_right) {
3561 weight_prev_x = sc->syninfo.weight_previous_na;
3562 div_max_x = sc->syninfo.div_max_na;
3565 if (y0 <= na_bottom || y0 >= max_y - na_top) {
3566 weight_prev_y = sc->syninfo.weight_previous_na;
3567 div_max_y = sc->syninfo.div_max_na;
3572 * Calculate weights for the average operands and
3573 * the divisor. Both depend on the distance between
3574 * the current packet and a previous one (based on the
3577 window = imin(smoother->queue_len, window_max);
3578 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
3579 dxp = abs(x0 - smoother->queue[peer].x) + 1;
3580 dyp = abs(y0 - smoother->queue[peer].y) + 1;
3581 len = (dxp * dxp) + (dyp * dyp);
3582 weight_prev_x = imin(weight_prev_x,
3583 weight_len_squared * weight_prev_x / len);
3584 weight_prev_y = imin(weight_prev_y,
3585 weight_len_squared * weight_prev_y / len);
3587 len = (dxp + dyp) / 2;
3588 div_x = div_len * div_max_x / len;
3589 div_x = imin(div_max_x, div_x);
3590 div_x = imax(div_min, div_x);
3591 div_y = div_len * div_max_y / len;
3592 div_y = imin(div_max_y, div_y);
3593 div_y = imax(div_min, div_y);
3596 "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
3597 smoother_id, peer, len, weight_prev_x, weight_prev_y,
3600 /* Compute averages. */
3602 (weight_current * dx * multiplicator +
3603 weight_prev_x * smoother->avg_dx) /
3604 (weight_current + weight_prev_x);
3607 (weight_current * dy * multiplicator +
3608 weight_prev_y * smoother->avg_dy) /
3609 (weight_current + weight_prev_y);
3612 "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id,
3613 smoother->avg_dx / multiplicator,
3614 smoother->avg_dy / multiplicator));
3616 /* Use these averages to calculate x & y. */
3617 smoother->squelch_x += smoother->avg_dx;
3618 dxp = smoother->squelch_x / (div_x * multiplicator);
3619 smoother->squelch_x = smoother->squelch_x %
3620 (div_x * multiplicator);
3622 smoother->squelch_y += smoother->avg_dy;
3623 dyp = smoother->squelch_y / (div_y * multiplicator);
3624 smoother->squelch_y = smoother->squelch_y %
3625 (div_y * multiplicator);
3627 switch(gest->in_vscroll) {
3628 case 0: /* Pointer movement. */
3629 /* On real<->fuzzy finger switch the x/y pos jumps */
3630 if (is_fuzzy == smoother->is_fuzzy) {
3635 VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n",
3636 smoother_id, dx, dy, dxp, dyp));
3638 case 1: /* Vertical scrolling. */
3640 ms->button |= (dyp > 0) ?
3641 MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN;
3643 case 2: /* Horizontal scrolling. */
3645 ms->button |= (dxp > 0) ?
3646 MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN;
3650 smoother->is_fuzzy = is_fuzzy;
3654 * Deactivate queue. Note: We can not just reset queue here
3655 * as these values are still used by gesture processor.
3656 * So postpone reset till next touch.
3658 smoother->active = 0;
3663 proc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3664 int *x, int *y, int *z)
3666 static int touchpad_button, trackpoint_button;
3667 finger_t fn, f[ELANTECH_MAX_FINGERS];
3668 int pkt, id, scale, i, nfingers, mask;
3670 if (!elantech_support)
3673 /* Determine packet format and do a sanity check for out of sync packets. */
3674 if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion))
3675 pkt = ELANTECH_PKT_NOP;
3676 else if (ELANTECH_PKT_IS_TRACKPOINT(pb))
3677 pkt = ELANTECH_PKT_TRACKPOINT;
3679 switch (sc->elanhw.hwversion) {
3681 if (!ELANTECH_PKT_IS_V2(pb))
3684 pkt = (pb->ipacket[0] & 0xc0) == 0x80 ?
3685 ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON;
3688 if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) &&
3689 !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc))
3692 pkt = ELANTECH_PKT_V3;
3695 if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc))
3698 switch (pb->ipacket[3] & 0x03) {
3700 pkt = ELANTECH_PKT_V4_STATUS;
3703 pkt = ELANTECH_PKT_V4_HEAD;
3706 pkt = ELANTECH_PKT_V4_MOTION;
3716 VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt));
3718 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
3719 PSM_FINGER_RESET(f[id]);
3722 ms->button = ms->obutton;
3724 if (sc->syninfo.touchpad_off)
3728 * L: Left mouse button pressed
3729 * R: Right mouse button pressed
3730 * N: number of fingers on touchpad
3731 * X: absolute x value (horizontal)
3732 * Y: absolute y value (vertical)
3733 * W; width of the finger touch
3737 case ELANTECH_PKT_V2_COMMON: /* HW V2. One/Three finger touch */
3738 /* 7 6 5 4 3 2 1 0 (LSB)
3739 * -------------------------------------------
3740 * ipacket[0]: N1 N0 W3 W2 . . R L
3741 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
3742 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
3743 * ipacket[3]: N4 VF W1 W0 . . . B2
3744 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
3745 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3746 * -------------------------------------------
3747 * N4: set if more than 3 fingers (only in 3 fingers mode)
3748 * VF: a kind of flag? (only on EF123, 0 when finger
3749 * is over one of the buttons, 1 otherwise)
3750 * B2: (on EF113 only, 0 otherwise), one button pressed
3751 * P & W is not reported on EF113 touchpads
3753 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
3754 if (nfingers == 3 && (pb->ipacket[3] & 0x80))
3756 mask = (1 << nfingers) - 1;
3758 fn = ELANTECH_FINGER_SET_XYP(pb);
3759 if (sc->elanhw.haspressure) {
3760 fn.w = ((pb->ipacket[0] & 0x30) >> 2) |
3761 ((pb->ipacket[3] & 0x30) >> 4);
3763 fn.p = PSM_FINGER_DEFAULT_P;
3764 fn.w = PSM_FINGER_DEFAULT_W;
3768 * HW v2 dont report exact finger positions when 3 or more
3769 * fingers are on touchpad. Use reported value as fingers
3770 * position as it is required for tap detection
3773 fn.flags = PSM_FINGER_FUZZY;
3775 for (id = 0; id < imin(nfingers, ELANTECH_MAX_FINGERS); id++)
3779 case ELANTECH_PKT_V2_2FINGER: /*HW V2. Two finger touch */
3780 /* 7 6 5 4 3 2 1 0 (LSB)
3781 * -------------------------------------------
3782 * ipacket[0]: N1 N0 AY8 AX8 . . R L
3783 * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0
3784 * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0
3785 * ipacket[3]: . . BY8 BX8 . . . .
3786 * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0
3787 * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0
3788 * -------------------------------------------
3789 * AX: lower-left finger absolute x value
3790 * AY: lower-left finger absolute y value
3791 * BX: upper-right finger absolute x value
3792 * BY: upper-right finger absolute y value
3795 mask = (1 << nfingers) - 1;
3797 for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++)
3798 f[id] = (finger_t) {
3799 .x = (((pb->ipacket[id * 3] & 0x10) << 4) |
3800 pb->ipacket[id * 3 + 1]) << 2,
3801 .y = (((pb->ipacket[id * 3] & 0x20) << 3) |
3802 pb->ipacket[id * 3 + 2]) << 2,
3803 .p = PSM_FINGER_DEFAULT_P,
3804 .w = PSM_FINGER_DEFAULT_W,
3805 /* HW ver.2 sends bounding box */
3806 .flags = PSM_FINGER_FUZZY
3810 case ELANTECH_PKT_V3: /* HW Version 3 */
3811 /* 7 6 5 4 3 2 1 0 (LSB)
3812 * -------------------------------------------
3813 * ipacket[0]: N1 N0 W3 W2 0 1 R L
3814 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
3815 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
3816 * ipacket[3]: 0 0 W1 W0 0 0 1 0
3817 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
3818 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3819 * -------------------------------------------
3821 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
3822 mask = (1 << nfingers) - 1;
3825 fn = ELANTECH_FINGER_SET_XYP(pb);
3826 fn.w = ((pb->ipacket[0] & 0x30) >> 2) |
3827 ((pb->ipacket[3] & 0x30) >> 4);
3830 * HW v3 dont report exact finger positions when 3 or more
3831 * fingers are on touchpad. Use reported value as fingers
3832 * position as it is required for tap detection
3835 fn.flags = PSM_FINGER_FUZZY;
3837 for (id = 0; id < imin(nfingers, ELANTECH_MAX_FINGERS); id++)
3840 if (nfingers == 2) {
3841 if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) {
3842 sc->elanaction.fingers[0] = fn;
3845 f[0] = sc->elanaction.fingers[0];
3849 case ELANTECH_PKT_V4_STATUS: /* HW Version 4. Status packet */
3850 /* 7 6 5 4 3 2 1 0 (LSB)
3851 * -------------------------------------------
3852 * ipacket[0]: . . . . 0 1 R L
3853 * ipacket[1]: . . . F4 F3 F2 F1 F0
3854 * ipacket[2]: . . . . . . . .
3855 * ipacket[3]: . . . 1 0 0 0 0
3856 * ipacket[4]: PL . . . . . . .
3857 * ipacket[5]: . . . . . . . .
3858 * -------------------------------------------
3859 * Fn: finger n is on touchpad
3861 * HV ver4 sends a status packet to indicate that the numbers
3862 * or identities of the fingers has been changed
3865 mask = pb->ipacket[1] & 0x1f;
3866 nfingers = bitcount32(mask);
3868 /* Skip "new finger is on touchpad" packets */
3869 if ((sc->elanaction.mask & mask) == sc->elanaction.mask &&
3870 (mask & ~sc->elanaction.mask)) {
3871 sc->elanaction.mask = mask;
3877 case ELANTECH_PKT_V4_HEAD: /* HW Version 4. Head packet */
3878 /* 7 6 5 4 3 2 1 0 (LSB)
3879 * -------------------------------------------
3880 * ipacket[0]: W3 W2 W1 W0 0 1 R L
3881 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
3882 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
3883 * ipacket[3]: ID2 ID1 ID0 1 0 0 0 1
3884 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
3885 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3886 * -------------------------------------------
3888 * HW ver 4 sends head packets in two cases:
3889 * 1. One finger touch and movement.
3890 * 2. Next after status packet to tell new finger positions.
3892 mask = sc->elanaction.mask;
3893 nfingers = bitcount32(mask);
3894 id = ((pb->ipacket[3] & 0xe0) >> 5) - 1;
3896 if (id >= 0 && id < ELANTECH_MAX_FINGERS) {
3897 f[id] = ELANTECH_FINGER_SET_XYP(pb);
3898 f[id].w = (pb->ipacket[0] & 0xf0) >> 4;
3902 case ELANTECH_PKT_V4_MOTION: /* HW Version 4. Motion packet */
3903 /* 7 6 5 4 3 2 1 0 (LSB)
3904 * -------------------------------------------
3905 * ipacket[0]: ID2 ID1 ID0 OF 0 1 R L
3906 * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
3907 * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
3908 * ipacket[3]: ID2 ID1 ID0 1 0 0 1 0
3909 * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
3910 * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
3911 * -------------------------------------------
3912 * OF: delta overflows (> 127 or < -128), in this case
3913 * firmware sends us (delta x / 5) and (delta y / 5)
3915 * DX: delta x (two's complement)
3916 * XY: delta y (two's complement)
3917 * byte 0 ~ 2 for one finger
3918 * byte 3 ~ 5 for another finger
3920 mask = sc->elanaction.mask;
3921 nfingers = bitcount32(mask);
3923 scale = (pb->ipacket[0] & 0x10) ? 5 : 1;
3924 for (i = 0; i <= 3; i += 3) {
3925 id = ((pb->ipacket[i] & 0xe0) >> 5) - 1;
3926 if (id < 0 || id >= ELANTECH_MAX_FINGERS)
3929 if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) {
3930 f[id] = sc->elanaction.fingers[id];
3931 f[id].x += imax(-f[id].x,
3932 (signed char)pb->ipacket[i+1] * scale);
3933 f[id].y += imax(-f[id].y,
3934 (signed char)pb->ipacket[i+2] * scale);
3936 VLOG(3, (LOG_DEBUG, "elantech: "
3937 "HW v4 motion packet skipped\n"));
3943 case ELANTECH_PKT_TRACKPOINT:
3944 /* 7 6 5 4 3 2 1 0 (LSB)
3945 * -------------------------------------------
3946 * ipacket[0]: 0 0 SX SY 0 M R L
3947 * ipacket[1]: ~SX 0 0 0 0 0 0 0
3948 * ipacket[2]: ~SY 0 0 0 0 0 0 0
3949 * ipacket[3]: 0 0 ~SY ~SX 0 1 1 0
3950 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
3951 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3952 * -------------------------------------------
3953 * X and Y are written in two's complement spread
3954 * over 9 bits with SX/SY the relative top bit and
3955 * X7..X0 and Y7..Y0 the lower bits.
3957 *x = (pb->ipacket[0] & 0x20) ?
3958 pb->ipacket[4] - 256 : pb->ipacket[4];
3959 *y = (pb->ipacket[0] & 0x10) ?
3960 pb->ipacket[5] - 256 : pb->ipacket[5];
3963 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
3964 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) |
3965 ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0);
3967 ms->button = touchpad_button | trackpoint_button;
3970 case ELANTECH_PKT_NOP:
3977 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
3978 if (PSM_FINGER_IS_SET(f[id]))
3979 VLOG(2, (LOG_DEBUG, "elantech: "
3980 "finger %d: down [%d, %d], %d, %d, %d\n", id + 1,
3981 f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags));
3983 /* Touchpad button presses */
3984 if (sc->elanhw.isclickpad) {
3986 ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0);
3989 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
3990 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0);
3993 ms->button = touchpad_button | trackpoint_button;
3995 /* Palm detection doesn't terminate the current action. */
3996 if (!psmpalmdetect(sc, &f[0], nfingers)) {
3997 /* Send finger 1 position to gesture processor */
3998 if (PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) ||
4000 psmgestures(sc, &f[0], imin(nfingers, 3), ms);
4001 /* Send fingers positions to movement smoothers */
4002 for (id = 0; id < PSM_FINGERS; id++)
4003 if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id)))
4004 psmsmoother(sc, &f[id], id, ms, x, y);
4006 VLOG(2, (LOG_DEBUG, "elantech: palm detected! (%d)\n",
4010 /* Store current finger positions in action context */
4011 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4012 if (PSM_FINGER_IS_SET(f[id]))
4013 sc->elanaction.fingers[id] = f[id];
4014 if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id)))
4015 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
4017 sc->elanaction.mask = mask;
4019 /* Use the extra buttons as a scrollwheel */
4020 if (ms->button & MOUSE_BUTTON4DOWN)
4022 else if (ms->button & MOUSE_BUTTON5DOWN)
4024 else if (ms->button & MOUSE_BUTTON6DOWN)
4026 else if (ms->button & MOUSE_BUTTON7DOWN)
4030 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
4031 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
4037 proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4038 int *x, int *y, int *z)
4040 static int butmap_versapad[8] = {
4046 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4048 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
4052 /* VersaPad PS/2 absolute mode message format
4054 * [packet1] 7 6 5 4 3 2 1 0(LSB)
4055 * ipacket[0]: 1 1 0 A 1 L T R
4056 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
4057 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
4058 * ipacket[3]: 1 1 1 A 1 L T R
4059 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
4060 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
4063 * R: right physical mouse button (1=on)
4064 * T: touch pad virtual button (1=tapping)
4065 * L: left physical mouse button (1=on)
4066 * A: position data is valid (1=valid)
4067 * H: horizontal data (12bit signed integer. H11 is sign bit.)
4068 * V: vertical data (12bit signed integer. V11 is sign bit.)
4071 * Tapping is mapped to MOUSE_BUTTON4.
4075 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
4076 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
4077 if (c & MOUSE_PS2VERSA_IN_USE) {
4078 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
4079 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
4084 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
4087 if (*x < 0) /* XXX */
4096 sc->flags |= PSM_FLAGS_FINGERDOWN;
4100 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
4104 psmsoftintridle(void *arg)
4106 struct psm_softc *sc = arg;
4109 /* Invoke soft handler only when pqueue is empty. Otherwise it will be
4110 * invoked from psmintr soon with pqueue filled with real data */
4111 if (sc->pqueue_start == sc->pqueue_end &&
4112 sc->idlepacket.inputbytes > 0) {
4113 /* Grow circular queue backwards to avoid race with psmintr */
4114 if (--sc->pqueue_start < 0)
4115 sc->pqueue_start = PSM_PACKETQUEUE - 1;
4117 pb = &sc->pqueue[sc->pqueue_start];
4118 memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t));
4120 "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n",
4121 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
4122 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
4130 psmsoftintr(void *arg)
4133 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
4134 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
4136 static int butmap[8] = {
4140 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4142 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
4143 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
4144 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
4146 struct psm_softc *sc = arg;
4151 getmicrouptime(&sc->lastsoftintr);
4153 lockmgr(&sc->lock, LK_EXCLUSIVE);
4156 pb = &sc->pqueue[sc->pqueue_start];
4160 * A kludge for Kensington device!
4161 * The MSB of the horizontal count appears to be stored in
4164 if (sc->hw.model == MOUSE_MODEL_THINK)
4165 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
4167 /* ignore the overflow bits... */
4168 x = (c & MOUSE_PS2_XNEG) ?
4169 pb->ipacket[1] - 256 : pb->ipacket[1];
4170 y = (c & MOUSE_PS2_YNEG) ?
4171 pb->ipacket[2] - 256 : pb->ipacket[2];
4173 ms.obutton = sc->button; /* previous button state */
4174 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
4175 /* `tapping' action */
4176 if (sc->config & PSM_CONFIG_FORCETAP)
4177 ms.button |= ((c & MOUSE_PS2_TAP)) ?
4178 0 : MOUSE_BUTTON4DOWN;
4179 timevalclear(&sc->idletimeout);
4180 sc->idlepacket.inputbytes = 0;
4182 switch (sc->hw.model) {
4184 case MOUSE_MODEL_EXPLORER:
4186 * b7 b6 b5 b4 b3 b2 b1 b0
4187 * byte 1: oy ox sy sx 1 M R L
4188 * byte 2: x x x x x x x x
4189 * byte 3: y y y y y y y y
4190 * byte 4: * * S2 S1 s d2 d1 d0
4192 * L, M, R, S1, S2: left, middle, right and side buttons
4193 * s: wheel data sign bit
4196 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
4197 (pb->ipacket[3] & 0x0f) - 16 :
4198 (pb->ipacket[3] & 0x0f);
4200 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
4201 MOUSE_BUTTON4DOWN : 0;
4203 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
4204 MOUSE_BUTTON5DOWN : 0;
4207 case MOUSE_MODEL_INTELLI:
4208 case MOUSE_MODEL_NET:
4209 /* wheel data is in the fourth byte */
4210 z = (char)pb->ipacket[3];
4212 * XXX some mice may send 7 when there is no Z movement? */
4213 if ((z >= 7) || (z <= -7))
4215 /* some compatible mice have additional buttons */
4216 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
4217 MOUSE_BUTTON4DOWN : 0;
4218 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
4219 MOUSE_BUTTON5DOWN : 0;
4222 case MOUSE_MODEL_MOUSEMANPLUS:
4223 proc_mmanplus(sc, pb, &ms, &x, &y, &z);
4226 case MOUSE_MODEL_GLIDEPOINT:
4227 /* `tapping' action */
4228 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
4232 case MOUSE_MODEL_NETSCROLL:
4234 * three addtional bytes encode buttons and
4237 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
4238 MOUSE_BUTTON4DOWN : 0;
4239 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
4240 MOUSE_BUTTON5DOWN : 0;
4241 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
4242 pb->ipacket[4] - 256 : pb->ipacket[4];
4245 case MOUSE_MODEL_THINK:
4246 /* the fourth button state in the first byte */
4247 ms.button |= (c & MOUSE_PS2_TAP) ?
4248 MOUSE_BUTTON4DOWN : 0;
4251 case MOUSE_MODEL_VERSAPAD:
4252 proc_versapad(sc, pb, &ms, &x, &y, &z);
4253 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
4254 ((y < 0) ? MOUSE_PS2_YNEG : 0);
4257 case MOUSE_MODEL_4D:
4259 * b7 b6 b5 b4 b3 b2 b1 b0
4260 * byte 1: s2 d2 s1 d1 1 M R L
4261 * byte 2: sx x x x x x x x
4262 * byte 3: sy y y y y y y y
4264 * s1: wheel 1 direction
4266 * s2: wheel 2 direction
4269 x = (pb->ipacket[1] & 0x80) ?
4270 pb->ipacket[1] - 256 : pb->ipacket[1];
4271 y = (pb->ipacket[2] & 0x80) ?
4272 pb->ipacket[2] - 256 : pb->ipacket[2];
4273 switch (c & MOUSE_4D_WHEELBITS) {
4280 case 0x40: /* XXX 2nd wheel turning right */
4283 case 0xc0: /* XXX 2nd wheel turning left */
4289 case MOUSE_MODEL_4DPLUS:
4290 if ((x < 16 - 256) && (y < 16 - 256)) {
4292 * b7 b6 b5 b4 b3 b2 b1 b0
4293 * byte 1: 0 0 1 1 1 M R L
4294 * byte 2: 0 0 0 0 1 0 0 0
4295 * byte 3: 0 0 0 0 S s d1 d0
4297 * L, M, R, S: left, middle, right,
4299 * s: wheel data sign bit
4303 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
4304 ms.button |= MOUSE_BUTTON4DOWN;
4305 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
4306 ((pb->ipacket[2] & 0x07) - 8) :
4307 (pb->ipacket[2] & 0x07) ;
4309 /* preserve previous button states */
4310 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
4314 case MOUSE_MODEL_SYNAPTICS:
4315 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0)
4319 case MOUSE_MODEL_ELANTECH:
4320 if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0)
4324 case MOUSE_MODEL_TRACKPOINT:
4325 case MOUSE_MODEL_GENERIC:
4331 if (sc->mode.accelfactor >= 1) {
4333 x = x * x / sc->mode.accelfactor;
4336 if (c & MOUSE_PS2_XNEG)
4340 y = y * y / sc->mode.accelfactor;
4343 if (c & MOUSE_PS2_YNEG)
4347 /* Store last packet for reinjection if it has not been set already */
4348 if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0)
4349 sc->idlepacket = *pb;
4354 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
4355 (ms.obutton ^ ms.button);
4357 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
4359 sc->status.flags |= ms.flags;
4360 sc->status.dx += ms.dx;
4361 sc->status.dy += ms.dy;
4362 sc->status.dz += ms.dz;
4363 sc->status.button = ms.button;
4364 sc->button = ms.button;
4366 sc->watchdog = FALSE;
4369 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
4370 l = imin(pb->inputbytes,
4371 sizeof(sc->queue.buf) - sc->queue.tail);
4372 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
4373 if (pb->inputbytes > l)
4374 bcopy(&pb->ipacket[l], &sc->queue.buf[0],
4375 pb->inputbytes - l);
4376 sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
4377 sizeof(sc->queue.buf);
4378 sc->queue.count += pb->inputbytes;
4383 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
4384 sc->pqueue_start = 0;
4385 } while (sc->pqueue_start != sc->pqueue_end);
4387 if (sc->state & PSM_ASLP) {
4388 sc->state &= ~PSM_ASLP;
4392 KNOTE(&sc->rkq.ki_note, 0);
4393 sc->state &= ~PSM_SOFTARMED;
4395 /* schedule injection of predefined packet after idletimeout
4396 * if no data packets have been received from psmintr */
4397 if (timevalisset(&sc->idletimeout)) {
4398 sc->state |= PSM_SOFTARMED;
4399 callout_reset(&sc->softcallout, tvtohz_high(&sc->idletimeout),
4400 psmsoftintridle, sc);
4401 VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n",
4402 tvtohz_high(&sc->idletimeout)));
4404 lockmgr(&sc->lock, LK_RELEASE);
4407 static struct filterops psmfiltops =
4408 { FILTEROP_ISFD, NULL, psmfilter_detach, psmfilter };
4411 psmkqfilter(struct dev_kqfilter_args *ap)
4413 cdev_t dev = ap->a_head.a_dev;
4414 struct psm_softc *sc = dev->si_drv1;
4415 struct knote *kn = ap->a_kn;
4416 struct klist *klist;
4420 switch (kn->kn_filter) {
4422 kn->kn_fop = &psmfiltops;
4423 kn->kn_hook = (caddr_t)sc;
4426 ap->a_result = EOPNOTSUPP;
4430 klist = &sc->rkq.ki_note;
4431 knote_insert(klist, kn);
4437 psmfilter_detach(struct knote *kn)
4439 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook;
4440 struct klist *klist;
4442 klist = &sc->rkq.ki_note;
4443 knote_remove(klist, kn);
4447 psmfilter(struct knote *kn, long hint)
4449 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook;
4452 lockmgr(&sc->lock, LK_EXCLUSIVE);
4453 if (sc->queue.count > 0)
4455 lockmgr(&sc->lock, LK_RELEASE);
4461 /* vendor/model specific routines */
4463 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
4466 if (set_mouse_resolution(kbdc, res) != res)
4468 if (set_mouse_scaling(kbdc, scale) &&
4469 set_mouse_scaling(kbdc, scale) &&
4470 set_mouse_scaling(kbdc, scale) &&
4471 (get_mouse_status(kbdc, status, 0, 3) >= 3))
4477 mouse_ext_command(KBDC kbdc, int command)
4482 c = (command >> 6) & 0x03;
4483 if (set_mouse_resolution(kbdc, c) != c)
4485 c = (command >> 4) & 0x03;
4486 if (set_mouse_resolution(kbdc, c) != c)
4488 c = (command >> 2) & 0x03;
4489 if (set_mouse_resolution(kbdc, c) != c)
4491 c = (command >> 0) & 0x03;
4492 if (set_mouse_resolution(kbdc, c) != c)
4497 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
4499 enable_groller(struct psm_softc *sc, enum probearg arg)
4501 KBDC kbdc = sc->kbdc;
4505 * The special sequence to enable the fourth button and the
4506 * roller. Immediately after this sequence check status bytes.
4507 * if the mouse is NetScroll, the second and the third bytes are
4512 * If the mouse is an ordinary PS/2 mouse, the status bytes should
4513 * look like the following.
4515 * byte 1 bit 7 always 0
4516 * bit 6 stream mode (0)
4517 * bit 5 disabled (0)
4518 * bit 4 1:1 scaling (0)
4520 * bit 0-2 button status
4521 * byte 2 resolution (PSMD_RES_HIGH)
4522 * byte 3 report rate (?)
4525 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
4527 if ((status[1] != '3') || (status[2] != 'D'))
4529 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
4535 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
4537 enable_gmouse(struct psm_softc *sc, enum probearg arg)
4539 KBDC kbdc = sc->kbdc;
4543 * The special sequence to enable the middle, "rubber" button.
4544 * Immediately after this sequence check status bytes.
4545 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
4546 * the second and the third bytes are '3' and 'U'.
4547 * NOTE: NetMouse reports that it has three buttons although it has
4548 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
4549 * say they have three buttons too and they do have a button on the
4552 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
4554 if ((status[1] != '3') || (status[2] != 'U'))
4559 /* ALPS GlidePoint */
4561 enable_aglide(struct psm_softc *sc, enum probearg arg)
4563 KBDC kbdc = sc->kbdc;
4567 * The special sequence to obtain ALPS GlidePoint specific
4568 * information. Immediately after this sequence, status bytes will
4569 * contain something interesting.
4570 * NOTE: ALPS produces several models of GlidePoint. Some of those
4571 * do not respond to this sequence, thus, cannot be detected this way.
4573 if (set_mouse_sampling_rate(kbdc, 100) != 100)
4575 if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
4577 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
4582 /* Kensington ThinkingMouse/Trackball */
4584 enable_kmouse(struct psm_softc *sc, enum probearg arg)
4586 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
4587 KBDC kbdc = sc->kbdc;
4593 id1 = get_aux_id(kbdc);
4594 if (set_mouse_sampling_rate(kbdc, 10) != 10)
4597 * The device is now in the native mode? It returns a different
4600 id2 = get_aux_id(kbdc);
4601 if ((id1 == id2) || (id2 != 2))
4604 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
4607 /* at this point, resolution is LOW, sampling rate is 10/sec */
4608 if (get_mouse_status(kbdc, status, 0, 3) < 3)
4613 * The special sequence to enable the third and fourth buttons.
4614 * Otherwise they behave like the first and second buttons.
4616 for (i = 0; i < nitems(rate); ++i)
4617 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4621 * At this point, the device is using default resolution and
4622 * sampling rate for the native mode.
4624 if (get_mouse_status(kbdc, status, 0, 3) < 3)
4626 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
4629 /* the device appears be enabled by this sequence, diable it for now */
4630 disable_aux_dev(kbdc);
4631 empty_aux_buffer(kbdc, 5);
4636 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
4638 enable_mmanplus(struct psm_softc *sc, enum probearg arg)
4640 KBDC kbdc = sc->kbdc;
4643 /* the special sequence to enable the fourth button and the roller. */
4645 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
4646 * must be called exactly three times since the last RESET command
4647 * before this sequence. XXX
4649 if (!set_mouse_scaling(kbdc, 1))
4651 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
4653 if (get_mouse_status(kbdc, data, 1, 3) < 3)
4657 * PS2++ protocol, packet type 0
4659 * b7 b6 b5 b4 b3 b2 b1 b0
4660 * byte 1: * 1 p3 p2 1 * * *
4661 * byte 2: 1 1 p1 p0 m1 m0 1 0
4662 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
4664 * p3-p0: packet type: 0
4665 * m7-m0: model ID: MouseMan+:0x50,
4667 * ScrollPoint:0x58...
4669 /* check constant bits */
4670 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
4672 if ((data[1] & 0xc3) != 0xc2)
4674 /* check d3-d0 in byte 2 */
4675 if (!MOUSE_PS2PLUS_CHECKBITS(data))
4678 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
4682 sc->hw.hwid &= 0x00ff;
4683 sc->hw.hwid |= data[2] << 8; /* save model ID */
4687 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
4688 * the wheel and the fourth button events are encoded in the
4689 * special data packet. The mouse may be put in the IntelliMouse mode
4690 * if it is initialized by the IntelliMouse's method.
4695 /* MS IntelliMouse Explorer */
4697 enable_msexplorer(struct psm_softc *sc, enum probearg arg)
4699 KBDC kbdc = sc->kbdc;
4700 static u_char rate0[] = { 200, 100, 80, };
4701 static u_char rate1[] = { 200, 200, 80, };
4706 * This is needed for at least A4Tech X-7xx mice - they do not go
4707 * straight to Explorer mode, but need to be set to Intelli mode
4710 enable_msintelli(sc, arg);
4712 /* the special sequence to enable the extra buttons and the roller. */
4713 for (i = 0; i < nitems(rate1); ++i)
4714 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
4716 /* the device will give the genuine ID only after the above sequence */
4717 id = get_aux_id(kbdc);
4718 if (id != PSM_EXPLORER_ID)
4722 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
4727 * XXX: this is a kludge to fool some KVM switch products
4728 * which think they are clever enough to know the 4-byte IntelliMouse
4729 * protocol, and assume any other protocols use 3-byte packets.
4730 * They don't convey 4-byte data packets from the IntelliMouse Explorer
4731 * correctly to the host computer because of this!
4732 * The following sequence is actually IntelliMouse's "wake up"
4733 * sequence; it will make the KVM think the mouse is IntelliMouse
4734 * when it is in fact IntelliMouse Explorer.
4736 for (i = 0; i < nitems(rate0); ++i)
4737 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
4746 * Logitech MouseMan+ and FirstMouse+ will also respond to this
4747 * probe routine and act like IntelliMouse.
4750 enable_msintelli(struct psm_softc *sc, enum probearg arg)
4752 KBDC kbdc = sc->kbdc;
4753 static u_char rate[] = { 200, 100, 80, };
4757 /* the special sequence to enable the third button and the roller. */
4758 for (i = 0; i < nitems(rate); ++i)
4759 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4761 /* the device will give the genuine ID only after the above sequence */
4762 id = get_aux_id(kbdc);
4763 if (id != PSM_INTELLI_ID)
4776 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
4779 enable_4dmouse(struct psm_softc *sc, enum probearg arg)
4781 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
4782 KBDC kbdc = sc->kbdc;
4786 for (i = 0; i < nitems(rate); ++i)
4787 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4789 id = get_aux_id(kbdc);
4791 * WinEasy 4D, 4 Way Scroll 4D: 6
4792 * Cable-Free 4D: 8 (4DPLUS)
4793 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
4795 if (id != PSM_4DMOUSE_ID)
4799 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
4808 * Newer wheel mice from A4 Tech seem to use this protocol.
4809 * Older models are recognized as either 4D Mouse or IntelliMouse.
4812 enable_4dplus(struct psm_softc *sc, enum probearg arg)
4814 KBDC kbdc = sc->kbdc;
4818 * enable_4dmouse() already issued the following ID sequence...
4819 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
4822 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
4823 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
4827 id = get_aux_id(kbdc);
4831 case PSM_4DPLUS_RFSW35_ID:
4838 sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
4845 /* Synaptics Touchpad */
4847 synaptics_sysctl(SYSCTL_HANDLER_ARGS)
4849 struct psm_softc *sc;
4852 if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 ||
4853 oidp->oid_arg2 > SYNAPTICS_SYSCTL_SOFTBUTTON3_X)
4856 sc = oidp->oid_arg1;
4858 /* Read the current value. */
4859 arg = *(int *)((char *)sc + oidp->oid_arg2);
4860 error = sysctl_handle_int(oidp, &arg, 0, req);
4863 if (error || !req->newptr)
4867 * Check that the new value is in the concerned node's range
4870 switch (oidp->oid_arg2) {
4871 case SYNAPTICS_SYSCTL_MIN_PRESSURE:
4872 case SYNAPTICS_SYSCTL_MAX_PRESSURE:
4873 if (arg < 0 || arg > 255)
4876 case SYNAPTICS_SYSCTL_MAX_WIDTH:
4877 if (arg < 4 || arg > 15)
4880 case SYNAPTICS_SYSCTL_MARGIN_TOP:
4881 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
4882 case SYNAPTICS_SYSCTL_NA_TOP:
4883 case SYNAPTICS_SYSCTL_NA_BOTTOM:
4884 if (arg < 0 || arg > sc->synhw.maximumYCoord)
4887 case SYNAPTICS_SYSCTL_SOFTBUTTON2_X:
4888 case SYNAPTICS_SYSCTL_SOFTBUTTON3_X:
4889 /* Softbuttons is clickpad only feature */
4890 if (!sc->synhw.capClickPad && arg != 0)
4893 case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
4894 case SYNAPTICS_SYSCTL_MARGIN_LEFT:
4895 case SYNAPTICS_SYSCTL_NA_RIGHT:
4896 case SYNAPTICS_SYSCTL_NA_LEFT:
4897 if (arg < 0 || arg > sc->synhw.maximumXCoord)
4900 case SYNAPTICS_SYSCTL_WINDOW_MIN:
4901 case SYNAPTICS_SYSCTL_WINDOW_MAX:
4902 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
4903 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
4906 case SYNAPTICS_SYSCTL_MULTIPLICATOR:
4907 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
4908 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
4909 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
4910 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
4911 case SYNAPTICS_SYSCTL_DIV_MIN:
4912 case SYNAPTICS_SYSCTL_DIV_MAX:
4913 case SYNAPTICS_SYSCTL_DIV_MAX_NA:
4914 case SYNAPTICS_SYSCTL_DIV_LEN:
4915 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
4916 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
4920 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
4921 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
4922 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
4926 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
4927 if (arg < -sc->synhw.maximumXCoord ||
4928 arg > sc->synhw.maximumXCoord)
4931 case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y:
4932 /* Softbuttons is clickpad only feature */
4933 if (!sc->synhw.capClickPad && arg != 0)
4936 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
4937 if (arg < -sc->synhw.maximumYCoord ||
4938 arg > sc->synhw.maximumYCoord)
4941 case SYNAPTICS_SYSCTL_TOUCHPAD_OFF:
4942 if (arg < 0 || arg > 1)
4950 *(int *)((char *)sc + oidp->oid_arg2) = arg;
4956 synaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc)
4959 * Set predefined sizes for softbuttons.
4960 * Values are taken to match HP Pavilion dv6 clickpad drawings
4961 * with thin middle softbutton placed on separator
4964 /* hw.psm.synaptics.softbuttons_y */
4965 sc->syninfo.softbuttons_y = 1700;
4966 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4967 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4968 "softbuttons_y", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4969 sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y,
4970 synaptics_sysctl, "I",
4971 "Vertical size of softbuttons area");
4973 /* hw.psm.synaptics.softbutton2_x */
4974 sc->syninfo.softbutton2_x = 3100;
4975 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4976 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4977 "softbutton2_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4978 sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X,
4979 synaptics_sysctl, "I",
4980 "Horisontal position of 2-nd softbutton left edge (0-disable)");
4982 /* hw.psm.synaptics.softbutton3_x */
4983 sc->syninfo.softbutton3_x = 3900;
4984 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4985 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4986 "softbutton3_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4987 sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X,
4988 synaptics_sysctl, "I",
4989 "Horisontal position of 3-rd softbutton left edge (0-disable)");
4993 synaptics_sysctl_create_tree(struct psm_softc *sc, const char *name,
4997 if (sc->syninfo.sysctl_tree != NULL)
5000 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
5001 sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
5002 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
5003 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name, CTLFLAG_RD,
5006 /* hw.psm.synaptics.directional_scrolls. */
5007 sc->syninfo.directional_scrolls = 0;
5008 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5009 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5010 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
5011 &sc->syninfo.directional_scrolls, 0,
5012 "Enable hardware scrolling pad (if non-zero) or register it as "
5013 "extended buttons (if 0)");
5015 /* hw.psm.synaptics.max_x. */
5016 sc->syninfo.max_x = 6143;
5017 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5018 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5019 "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY,
5020 &sc->syninfo.max_x, 0,
5021 "Horizontal reporting range");
5023 /* hw.psm.synaptics.max_y. */
5024 sc->syninfo.max_y = 6143;
5025 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5026 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5027 "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY,
5028 &sc->syninfo.max_y, 0,
5029 "Vertical reporting range");
5032 * Turn off two finger scroll if we have a
5033 * physical area reserved for scrolling or when
5034 * there's no multi finger support.
5036 if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 &&
5037 sc->synhw.capAdvancedGestures == 0))
5038 sc->syninfo.two_finger_scroll = 0;
5040 sc->syninfo.two_finger_scroll = 1;
5041 /* hw.psm.synaptics.two_finger_scroll. */
5042 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5043 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5044 "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY,
5045 &sc->syninfo.two_finger_scroll, 0,
5046 "Enable two finger scrolling");
5048 /* hw.psm.synaptics.min_pressure. */
5049 sc->syninfo.min_pressure = 16;
5050 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5051 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5052 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5053 sc, SYNAPTICS_SYSCTL_MIN_PRESSURE,
5054 synaptics_sysctl, "I",
5055 "Minimum pressure required to start an action");
5057 /* hw.psm.synaptics.max_pressure. */
5058 sc->syninfo.max_pressure = 220;
5059 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5060 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5061 "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5062 sc, SYNAPTICS_SYSCTL_MAX_PRESSURE,
5063 synaptics_sysctl, "I",
5064 "Maximum pressure to detect palm");
5066 /* hw.psm.synaptics.max_width. */
5067 sc->syninfo.max_width = 10;
5068 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5069 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5070 "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5071 sc, SYNAPTICS_SYSCTL_MAX_WIDTH,
5072 synaptics_sysctl, "I",
5073 "Maximum finger width to detect palm");
5075 /* hw.psm.synaptics.top_margin. */
5076 sc->syninfo.margin_top = 200;
5077 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5078 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5079 "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5080 sc, SYNAPTICS_SYSCTL_MARGIN_TOP,
5081 synaptics_sysctl, "I",
5084 /* hw.psm.synaptics.right_margin. */
5085 sc->syninfo.margin_right = 200;
5086 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5087 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5088 "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5089 sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
5090 synaptics_sysctl, "I",
5093 /* hw.psm.synaptics.bottom_margin. */
5094 sc->syninfo.margin_bottom = 200;
5095 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5096 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5097 "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5098 sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
5099 synaptics_sysctl, "I",
5102 /* hw.psm.synaptics.left_margin. */
5103 sc->syninfo.margin_left = 200;
5104 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5105 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5106 "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5107 sc, SYNAPTICS_SYSCTL_MARGIN_LEFT,
5108 synaptics_sysctl, "I",
5111 /* hw.psm.synaptics.na_top. */
5112 sc->syninfo.na_top = 1783;
5113 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5114 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5115 "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5116 sc, SYNAPTICS_SYSCTL_NA_TOP,
5117 synaptics_sysctl, "I",
5118 "Top noisy area, where weight_previous_na is used instead "
5119 "of weight_previous");
5121 /* hw.psm.synaptics.na_right. */
5122 sc->syninfo.na_right = 563;
5123 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5124 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5125 "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5126 sc, SYNAPTICS_SYSCTL_NA_RIGHT,
5127 synaptics_sysctl, "I",
5128 "Right noisy area, where weight_previous_na is used instead "
5129 "of weight_previous");
5131 /* hw.psm.synaptics.na_bottom. */
5132 sc->syninfo.na_bottom = 1408;
5133 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5134 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5135 "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5136 sc, SYNAPTICS_SYSCTL_NA_BOTTOM,
5137 synaptics_sysctl, "I",
5138 "Bottom noisy area, where weight_previous_na is used instead "
5139 "of weight_previous");
5141 /* hw.psm.synaptics.na_left. */
5142 sc->syninfo.na_left = 1600;
5143 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5144 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5145 "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5146 sc, SYNAPTICS_SYSCTL_NA_LEFT,
5147 synaptics_sysctl, "I",
5148 "Left noisy area, where weight_previous_na is used instead "
5149 "of weight_previous");
5151 /* hw.psm.synaptics.window_min. */
5152 sc->syninfo.window_min = 4;
5153 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5154 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5155 "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5156 sc, SYNAPTICS_SYSCTL_WINDOW_MIN,
5157 synaptics_sysctl, "I",
5158 "Minimum window size to start an action");
5160 /* hw.psm.synaptics.window_max. */
5161 sc->syninfo.window_max = 10;
5162 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5163 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5164 "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5165 sc, SYNAPTICS_SYSCTL_WINDOW_MAX,
5166 synaptics_sysctl, "I",
5167 "Maximum window size");
5169 /* hw.psm.synaptics.multiplicator. */
5170 sc->syninfo.multiplicator = 10000;
5171 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5172 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5173 "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5174 sc, SYNAPTICS_SYSCTL_MULTIPLICATOR,
5175 synaptics_sysctl, "I",
5176 "Multiplicator to increase precision in averages and divisions");
5178 /* hw.psm.synaptics.weight_current. */
5179 sc->syninfo.weight_current = 3;
5180 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5181 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5182 "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5183 sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
5184 synaptics_sysctl, "I",
5185 "Weight of the current movement in the new average");
5187 /* hw.psm.synaptics.weight_previous. */
5188 sc->syninfo.weight_previous = 6;
5189 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5190 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5191 "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5192 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
5193 synaptics_sysctl, "I",
5194 "Weight of the previous average");
5196 /* hw.psm.synaptics.weight_previous_na. */
5197 sc->syninfo.weight_previous_na = 20;
5198 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5199 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5200 "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5201 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
5202 synaptics_sysctl, "I",
5203 "Weight of the previous average (inside the noisy area)");
5205 /* hw.psm.synaptics.weight_len_squared. */
5206 sc->syninfo.weight_len_squared = 2000;
5207 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5208 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5209 "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5210 sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
5211 synaptics_sysctl, "I",
5212 "Length (squared) of segments where weight_previous "
5213 "starts to decrease");
5215 /* hw.psm.synaptics.div_min. */
5216 sc->syninfo.div_min = 9;
5217 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5218 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5219 "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5220 sc, SYNAPTICS_SYSCTL_DIV_MIN,
5221 synaptics_sysctl, "I",
5222 "Divisor for fast movements");
5224 /* hw.psm.synaptics.div_max. */
5225 sc->syninfo.div_max = 17;
5226 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5227 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5228 "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5229 sc, SYNAPTICS_SYSCTL_DIV_MAX,
5230 synaptics_sysctl, "I",
5231 "Divisor for slow movements");
5233 /* hw.psm.synaptics.div_max_na. */
5234 sc->syninfo.div_max_na = 30;
5235 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5236 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5237 "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5238 sc, SYNAPTICS_SYSCTL_DIV_MAX_NA,
5239 synaptics_sysctl, "I",
5240 "Divisor with slow movements (inside the noisy area)");
5242 /* hw.psm.synaptics.div_len. */
5243 sc->syninfo.div_len = 100;
5244 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5245 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5246 "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5247 sc, SYNAPTICS_SYSCTL_DIV_LEN,
5248 synaptics_sysctl, "I",
5249 "Length of segments where div_max starts to decrease");
5251 /* hw.psm.synaptics.tap_max_delta. */
5252 sc->syninfo.tap_max_delta = 80;
5253 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5254 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5255 "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5256 sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
5257 synaptics_sysctl, "I",
5258 "Length of segments above which a tap is ignored");
5260 /* hw.psm.synaptics.tap_min_queue. */
5261 sc->syninfo.tap_min_queue = 2;
5262 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5263 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5264 "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5265 sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
5266 synaptics_sysctl, "I",
5267 "Number of packets required to consider a tap");
5269 /* hw.psm.synaptics.taphold_timeout. */
5270 sc->gesture.in_taphold = 0;
5271 sc->syninfo.taphold_timeout = tap_timeout;
5272 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5273 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5274 "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5275 sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
5276 synaptics_sysctl, "I",
5277 "Maximum elapsed time between two taps to consider a tap-hold "
5280 /* hw.psm.synaptics.vscroll_hor_area. */
5281 sc->syninfo.vscroll_hor_area = 0; /* 1300 */
5282 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5283 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5284 "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5285 sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
5286 synaptics_sysctl, "I",
5287 "Area reserved for horizontal virtual scrolling");
5289 /* hw.psm.synaptics.vscroll_ver_area. */
5290 sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right;
5291 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5292 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5293 "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5294 sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
5295 synaptics_sysctl, "I",
5296 "Area reserved for vertical virtual scrolling");
5298 /* hw.psm.synaptics.vscroll_min_delta. */
5299 sc->syninfo.vscroll_min_delta = 50;
5300 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5301 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5302 "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5303 sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
5304 synaptics_sysctl, "I",
5305 "Minimum movement to consider virtual scrolling");
5307 /* hw.psm.synaptics.vscroll_div_min. */
5308 sc->syninfo.vscroll_div_min = 100;
5309 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5310 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5311 "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5312 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
5313 synaptics_sysctl, "I",
5314 "Divisor for fast scrolling");
5316 /* hw.psm.synaptics.vscroll_div_min. */
5317 sc->syninfo.vscroll_div_max = 150;
5318 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5319 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5320 "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5321 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
5322 synaptics_sysctl, "I",
5323 "Divisor for slow scrolling");
5325 /* hw.psm.synaptics.touchpad_off. */
5326 sc->syninfo.touchpad_off = 0;
5327 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5328 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5329 "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5330 sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF,
5331 synaptics_sysctl, "I",
5332 "Turn off touchpad");
5334 sc->syninfo.softbuttons_y = 0;
5335 sc->syninfo.softbutton2_x = 0;
5336 sc->syninfo.softbutton3_x = 0;
5338 /* skip softbuttons sysctl on not clickpads */
5339 if (sc->synhw.capClickPad)
5340 synaptics_sysctl_create_softbuttons_tree(sc);
5345 synaptics_preferred_mode(struct psm_softc *sc) {
5350 /* request wmode where available */
5351 if (sc->synhw.capExtended)
5358 synaptics_set_mode(struct psm_softc *sc, int mode_byte) {
5359 mouse_ext_command(sc->kbdc, mode_byte);
5361 /* "Commit" the Set Mode Byte command sent above. */
5362 set_mouse_sampling_rate(sc->kbdc, 20);
5365 * Enable advanced gestures mode if supported and we are not entering
5368 if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) &&
5369 !(mode_byte & (1 << 5))) {
5370 mouse_ext_command(sc->kbdc, 3);
5371 set_mouse_sampling_rate(sc->kbdc, 0xc8);
5376 enable_synaptics(struct psm_softc *sc, enum probearg arg)
5378 KBDC kbdc = sc->kbdc;
5379 synapticshw_t synhw;
5383 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
5386 * Just to be on the safe side: this avoids troubles with
5387 * following mouse_ext_command() when the previous command
5388 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
5389 * Synaptics Touchpad behaviour.
5391 set_mouse_scaling(kbdc, 1);
5393 /* Identify the Touchpad version. */
5394 if (mouse_ext_command(kbdc, 0) == 0)
5396 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5398 if (status[1] != 0x47)
5401 bzero(&synhw, sizeof(synhw));
5402 synhw.infoMinor = status[0];
5403 synhw.infoMajor = status[2] & 0x0f;
5406 kprintf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
5409 if (synhw.infoMajor < 4) {
5410 kprintf(" Unsupported (pre-v4) Touchpad detected\n");
5414 /* Get the Touchpad model information. */
5415 if (mouse_ext_command(kbdc, 3) == 0)
5417 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5419 if ((status[1] & 0x01) != 0) {
5420 kprintf(" Failed to read model information\n");
5424 synhw.infoRot180 = (status[0] & 0x80) != 0;
5425 synhw.infoPortrait = (status[0] & 0x40) != 0;
5426 synhw.infoSensor = status[0] & 0x3f;
5427 synhw.infoHardware = (status[1] & 0xfe) >> 1;
5428 synhw.infoNewAbs = (status[2] & 0x80) != 0;
5429 synhw.capPen = (status[2] & 0x40) != 0;
5430 synhw.infoSimplC = (status[2] & 0x20) != 0;
5431 synhw.infoGeometry = status[2] & 0x0f;
5434 kprintf(" Model information:\n");
5435 kprintf(" infoRot180: %d\n", synhw.infoRot180);
5436 kprintf(" infoPortrait: %d\n", synhw.infoPortrait);
5437 kprintf(" infoSensor: %d\n", synhw.infoSensor);
5438 kprintf(" infoHardware: %d\n", synhw.infoHardware);
5439 kprintf(" infoNewAbs: %d\n", synhw.infoNewAbs);
5440 kprintf(" capPen: %d\n", synhw.capPen);
5441 kprintf(" infoSimplC: %d\n", synhw.infoSimplC);
5442 kprintf(" infoGeometry: %d\n", synhw.infoGeometry);
5445 /* Read the extended capability bits. */
5446 if (mouse_ext_command(kbdc, 2) == 0)
5448 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5450 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
5451 kprintf(" Failed to read extended capability bits\n");
5455 /* Set the different capabilities when they exist. */
5457 synhw.capExtended = (status[0] & 0x80) != 0;
5458 if (synhw.capExtended) {
5459 synhw.nExtendedQueries = (status[0] & 0x70) >> 4;
5460 synhw.capMiddle = (status[0] & 0x04) != 0;
5461 synhw.capPassthrough = (status[2] & 0x80) != 0;
5462 synhw.capLowPower = (status[2] & 0x40) != 0;
5463 synhw.capMultiFingerReport =
5464 (status[2] & 0x20) != 0;
5465 synhw.capSleep = (status[2] & 0x10) != 0;
5466 synhw.capFourButtons = (status[2] & 0x08) != 0;
5467 synhw.capBallistics = (status[2] & 0x04) != 0;
5468 synhw.capMultiFinger = (status[2] & 0x02) != 0;
5469 synhw.capPalmDetect = (status[2] & 0x01) != 0;
5471 if (!set_mouse_scaling(kbdc, 1))
5473 if (mouse_ext_command(kbdc, 0x08) == 0)
5475 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5478 synhw.infoXupmm = status[0];
5479 synhw.infoYupmm = status[2];
5482 kprintf(" Extended capabilities:\n");
5483 kprintf(" capExtended: %d\n", synhw.capExtended);
5484 kprintf(" capMiddle: %d\n", synhw.capMiddle);
5485 kprintf(" nExtendedQueries: %d\n",
5486 synhw.nExtendedQueries);
5487 kprintf(" capPassthrough: %d\n", synhw.capPassthrough);
5488 kprintf(" capLowPower: %d\n", synhw.capLowPower);
5489 kprintf(" capMultiFingerReport: %d\n",
5490 synhw.capMultiFingerReport);
5491 kprintf(" capSleep: %d\n", synhw.capSleep);
5492 kprintf(" capFourButtons: %d\n", synhw.capFourButtons);
5493 kprintf(" capBallistics: %d\n", synhw.capBallistics);
5494 kprintf(" capMultiFinger: %d\n", synhw.capMultiFinger);
5495 kprintf(" capPalmDetect: %d\n", synhw.capPalmDetect);
5496 kprintf(" infoXupmm: %d\n", synhw.infoXupmm);
5497 kprintf(" infoYupmm: %d\n", synhw.infoYupmm);
5501 * If nExtendedQueries is 1 or greater, then the TouchPad
5502 * supports this number of extended queries. We can load
5503 * more information about buttons using query 0x09.
5505 if (synhw.nExtendedQueries >= 1) {
5506 if (!set_mouse_scaling(kbdc, 1))
5508 if (mouse_ext_command(kbdc, 0x09) == 0)
5510 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5512 synhw.verticalScroll = (status[0] & 0x01) != 0;
5513 synhw.horizontalScroll = (status[0] & 0x02) != 0;
5514 synhw.verticalWheel = (status[0] & 0x08) != 0;
5515 synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
5516 synhw.capEWmode = (status[0] & 0x04) != 0;
5518 kprintf(" Extended model ID:\n");
5519 kprintf(" verticalScroll: %d\n",
5520 synhw.verticalScroll);
5521 kprintf(" horizontalScroll: %d\n",
5522 synhw.horizontalScroll);
5523 kprintf(" verticalWheel: %d\n",
5524 synhw.verticalWheel);
5525 kprintf(" nExtendedButtons: %d\n",
5526 synhw.nExtendedButtons);
5527 kprintf(" capEWmode: %d\n",
5531 * Add the number of extended buttons to the total
5532 * button support count, including the middle button
5533 * if capMiddle support bit is set.
5535 buttons = synhw.nExtendedButtons + synhw.capMiddle;
5538 * If the capFourButtons support bit is set,
5539 * add a fourth button to the total button count.
5541 buttons = synhw.capFourButtons ? 1 : 0;
5543 /* Read the continued capabilities bits. */
5544 if (synhw.nExtendedQueries >= 4) {
5545 if (!set_mouse_scaling(kbdc, 1))
5547 if (mouse_ext_command(kbdc, 0x0c) == 0)
5549 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5552 synhw.capClickPad = (status[1] & 0x01) << 1;
5553 synhw.capClickPad |= (status[0] & 0x10) != 0;
5554 synhw.capDeluxeLEDs = (status[1] & 0x02) != 0;
5555 synhw.noAbsoluteFilter = (status[1] & 0x04) != 0;
5556 synhw.capReportsV = (status[1] & 0x08) != 0;
5557 synhw.capUniformClickPad = (status[1] & 0x10) != 0;
5558 synhw.capReportsMin = (status[1] & 0x20) != 0;
5559 synhw.capInterTouch = (status[1] & 0x40) != 0;
5560 synhw.capReportsMax = (status[0] & 0x02) != 0;
5561 synhw.capClearPad = (status[0] & 0x04) != 0;
5562 synhw.capAdvancedGestures = (status[0] & 0x08) != 0;
5563 synhw.capCoveredPad = (status[0] & 0x80) != 0;
5565 if (synhw.capReportsMax) {
5566 if (!set_mouse_scaling(kbdc, 1))
5568 if (mouse_ext_command(kbdc, 0x0d) == 0)
5570 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5573 synhw.maximumXCoord = (status[0] << 5) |
5574 ((status[1] & 0x0f) << 1);
5575 synhw.maximumYCoord = (status[2] << 5) |
5576 ((status[1] & 0xf0) >> 3);
5579 * Typical bezel limits. Taken from 'Synaptics
5580 * PS/2 * TouchPad Interfacing Guide' p.3.2.3.
5582 synhw.maximumXCoord = 5472;
5583 synhw.maximumYCoord = 4448;
5586 if (synhw.capReportsMin) {
5587 if (!set_mouse_scaling(kbdc, 1))
5589 if (mouse_ext_command(kbdc, 0x0f) == 0)
5591 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5594 synhw.minimumXCoord = (status[0] << 5) |
5595 ((status[1] & 0x0f) << 1);
5596 synhw.minimumYCoord = (status[2] << 5) |
5597 ((status[1] & 0xf0) >> 3);
5600 * Typical bezel limits. Taken from 'Synaptics
5601 * PS/2 * TouchPad Interfacing Guide' p.3.2.3.
5603 synhw.minimumXCoord = 1472;
5604 synhw.minimumYCoord = 1408;
5608 kprintf(" Continued capabilities:\n");
5609 kprintf(" capClickPad: %d\n",
5611 kprintf(" capDeluxeLEDs: %d\n",
5612 synhw.capDeluxeLEDs);
5613 kprintf(" noAbsoluteFilter: %d\n",
5614 synhw.noAbsoluteFilter);
5615 kprintf(" capReportsV: %d\n",
5617 kprintf(" capUniformClickPad: %d\n",
5618 synhw.capUniformClickPad);
5619 kprintf(" capReportsMin: %d\n",
5620 synhw.capReportsMin);
5621 kprintf(" capInterTouch: %d\n",
5622 synhw.capInterTouch);
5623 kprintf(" capReportsMax: %d\n",
5624 synhw.capReportsMax);
5625 kprintf(" capClearPad: %d\n",
5627 kprintf(" capAdvancedGestures: %d\n",
5628 synhw.capAdvancedGestures);
5629 kprintf(" capCoveredPad: %d\n",
5630 synhw.capCoveredPad);
5631 if (synhw.capReportsMax) {
5632 kprintf(" maximumXCoord: %d\n",
5633 synhw.maximumXCoord);
5634 kprintf(" maximumYCoord: %d\n",
5635 synhw.maximumYCoord);
5637 if (synhw.capReportsMin) {
5638 kprintf(" minimumXCoord: %d\n",
5639 synhw.minimumXCoord);
5640 kprintf(" minimumYCoord: %d\n",
5641 synhw.minimumYCoord);
5644 buttons += synhw.capClickPad;
5649 if (synhw.capExtended)
5650 kprintf(" Additional Buttons: %d\n", buttons);
5652 kprintf(" No extended capabilities\n");
5656 * Add the default number of 3 buttons to the total
5657 * count of supported buttons reported above.
5662 * Read the mode byte.
5664 * XXX: Note the Synaptics documentation also defines the first
5665 * byte of the response to this query to be a constant 0x3b, this
5666 * does not appear to be true for Touchpads with guest devices.
5668 if (mouse_ext_command(kbdc, 1) == 0)
5670 if (get_mouse_status(kbdc, status, 0, 3) != 3)
5672 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
5673 kprintf(" Failed to read mode byte\n");
5679 if (!synaptics_support)
5682 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
5684 if (trackpoint_support && synhw.capPassthrough) {
5685 enable_trackpoint(sc, arg);
5688 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
5691 /* Create sysctl tree. */
5692 synaptics_sysctl_create_tree(sc, "synaptics",
5693 "Synaptics TouchPad");
5694 sc->hw.buttons = buttons;
5700 synaptics_passthrough_on(struct psm_softc *sc)
5702 VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n"));
5703 synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5));
5707 synaptics_passthrough_off(struct psm_softc *sc)
5709 VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n"));
5710 set_mouse_scaling(sc->kbdc, 2);
5711 set_mouse_scaling(sc->kbdc, 1);
5712 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
5715 /* IBM/Lenovo TrackPoint */
5717 trackpoint_command(struct psm_softc *sc, int cmd, int loc, int val)
5719 const int seq[] = { 0xe2, cmd, loc, val };
5722 if (sc->synhw.capPassthrough)
5723 synaptics_passthrough_on(sc);
5725 for (i = 0; i < nitems(seq); i++) {
5726 if (sc->synhw.capPassthrough &&
5727 (seq[i] == 0xff || seq[i] == 0xe7))
5728 if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) {
5729 synaptics_passthrough_off(sc);
5732 if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) {
5733 if (sc->synhw.capPassthrough)
5734 synaptics_passthrough_off(sc);
5739 if (sc->synhw.capPassthrough)
5740 synaptics_passthrough_off(sc);
5745 #define PSM_TPINFO(x) offsetof(struct psm_softc, tpinfo.x)
5751 trackpoint_sysctl(SYSCTL_HANDLER_ARGS)
5753 static const int data[][3] = {
5754 { 0x00, 0x4a, PSM_TPINFO(sensitivity) },
5755 { 0x00, 0x4d, PSM_TPINFO(inertia) },
5756 { 0x00, 0x60, PSM_TPINFO(uplateau) },
5757 { 0x00, 0x57, PSM_TPINFO(reach) },
5758 { 0x00, 0x58, PSM_TPINFO(draghys) },
5759 { 0x00, 0x59, PSM_TPINFO(mindrag) },
5760 { 0x00, 0x5a, PSM_TPINFO(upthresh) },
5761 { 0x00, 0x5c, PSM_TPINFO(threshold) },
5762 { 0x00, 0x5d, PSM_TPINFO(jenks) },
5763 { 0x00, 0x5e, PSM_TPINFO(ztime) },
5764 { 0x01, 0x2c, PSM_TPINFO(pts) },
5765 { 0x08, 0x2d, PSM_TPINFO(skipback) }
5767 struct psm_softc *sc;
5768 int error, newval, *oldvalp;
5771 if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
5775 oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
5777 error = sysctl_handle_int(oidp, &newval, 0, req);
5780 if (newval == *oldvalp)
5782 if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
5784 error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47,
5785 tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
5794 trackpoint_sysctl_create_tree(struct psm_softc *sc)
5797 if (sc->tpinfo.sysctl_tree != NULL)
5800 /* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
5801 sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
5802 sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
5803 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD,
5804 0, "IBM/Lenovo TrackPoint");
5806 /* hw.psm.trackpoint.sensitivity */
5807 sc->tpinfo.sensitivity = 0x80;
5808 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5809 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5810 "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5811 sc, TRACKPOINT_SYSCTL_SENSITIVITY,
5812 trackpoint_sysctl, "I",
5815 /* hw.psm.trackpoint.negative_inertia */
5816 sc->tpinfo.inertia = 0x06;
5817 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5818 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5819 "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5820 sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
5821 trackpoint_sysctl, "I",
5822 "Negative inertia factor");
5824 /* hw.psm.trackpoint.upper_plateau */
5825 sc->tpinfo.uplateau = 0x61;
5826 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5827 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5828 "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5829 sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
5830 trackpoint_sysctl, "I",
5831 "Transfer function upper plateau speed");
5833 /* hw.psm.trackpoint.backup_range */
5834 sc->tpinfo.reach = 0x0a;
5835 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5836 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5837 "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5838 sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
5839 trackpoint_sysctl, "I",
5842 /* hw.psm.trackpoint.drag_hysteresis */
5843 sc->tpinfo.draghys = 0xff;
5844 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5845 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5846 "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5847 sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
5848 trackpoint_sysctl, "I",
5851 /* hw.psm.trackpoint.minimum_drag */
5852 sc->tpinfo.mindrag = 0x14;
5853 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5854 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5855 "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5856 sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
5857 trackpoint_sysctl, "I",
5860 /* hw.psm.trackpoint.up_threshold */
5861 sc->tpinfo.upthresh = 0xff;
5862 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5863 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5864 "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5865 sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
5866 trackpoint_sysctl, "I",
5867 "Up threshold for release");
5869 /* hw.psm.trackpoint.threshold */
5870 sc->tpinfo.threshold = 0x08;
5871 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5872 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5873 "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5874 sc, TRACKPOINT_SYSCTL_THRESHOLD,
5875 trackpoint_sysctl, "I",
5878 /* hw.psm.trackpoint.jenks_curvature */
5879 sc->tpinfo.jenks = 0x87;
5880 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5881 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5882 "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5883 sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
5884 trackpoint_sysctl, "I",
5887 /* hw.psm.trackpoint.z_time */
5888 sc->tpinfo.ztime = 0x26;
5889 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5890 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5891 "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5892 sc, TRACKPOINT_SYSCTL_Z_TIME,
5893 trackpoint_sysctl, "I",
5896 /* hw.psm.trackpoint.press_to_select */
5897 sc->tpinfo.pts = 0x00;
5898 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5899 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5900 "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5901 sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
5902 trackpoint_sysctl, "I",
5905 /* hw.psm.trackpoint.skip_backups */
5906 sc->tpinfo.skipback = 0x00;
5907 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
5908 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
5909 "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5910 sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
5911 trackpoint_sysctl, "I",
5912 "Skip backups from drags");
5916 set_trackpoint_parameters(struct psm_softc *sc)
5918 trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity);
5919 trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau);
5920 trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia);
5921 trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach);
5922 trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys);
5923 trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag);
5924 trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh);
5925 trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold);
5926 trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks);
5927 trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime);
5928 if (sc->tpinfo.pts == 0x01)
5929 trackpoint_command(sc, 0x47, 0x2c, 0x01);
5930 if (sc->tpinfo.skipback == 0x01)
5931 trackpoint_command(sc, 0x47, 0x2d, 0x08);
5935 enable_trackpoint(struct psm_softc *sc, enum probearg arg)
5937 KBDC kbdc = sc->kbdc;
5941 * If called from enable_synaptics(), make sure that passthrough
5942 * mode is enabled so we can reach the trackpoint.
5943 * However, passthrough mode must be disabled before setting the
5944 * trackpoint parameters, as rackpoint_command() enables and disables
5945 * passthrough mode on its own.
5947 if (sc->synhw.capPassthrough)
5948 synaptics_passthrough_on(sc);
5950 if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
5951 read_aux_data(kbdc) != 0x01)
5953 id = read_aux_data(kbdc);
5958 if (!trackpoint_support)
5961 if (sc->synhw.capPassthrough)
5962 synaptics_passthrough_off(sc);
5965 trackpoint_sysctl_create_tree(sc);
5967 * Don't overwrite hwid and buttons when we are
5970 if (!sc->synhw.capPassthrough) {
5976 set_trackpoint_parameters(sc);
5981 if (sc->synhw.capPassthrough)
5982 synaptics_passthrough_off(sc);
5987 /* Interlink electronics VersaPad */
5989 enable_versapad(struct psm_softc *sc, enum probearg arg)
5991 KBDC kbdc = sc->kbdc;
5994 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
5995 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
5996 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
5997 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
5998 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
5999 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6000 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
6002 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
6004 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6006 return (TRUE); /* PS/2 absolute mode */
6009 /* Elantech Touchpad */
6011 elantech_read_1(KBDC kbdc, int hwversion, int reg, int *val)
6013 int res, readcmd, retidx;
6016 readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR;
6017 retidx = hwversion == 4 ? 1 : 0;
6019 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6020 res |= send_aux_command(kbdc, readcmd) != PSM_ACK;
6021 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6022 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6023 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6026 *val = resp[retidx];
6032 elantech_write_1(KBDC kbdc, int hwversion, int reg, int val)
6036 writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR;
6038 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6039 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6040 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6041 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6042 if (hwversion == 4) {
6043 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6044 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6046 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6047 res |= send_aux_command(kbdc, val) != PSM_ACK;
6048 res |= set_mouse_scaling(kbdc, 1) == 0;
6054 elantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp)
6058 if (hwversion == 2) {
6059 res = set_mouse_scaling(kbdc, 1) == 0;
6060 res |= mouse_ext_command(kbdc, cmd) == 0;
6062 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6063 res |= send_aux_command(kbdc, cmd) != PSM_ACK;
6065 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6071 elantech_init(KBDC kbdc, elantechhw_t *elanhw)
6073 int i, val, res, hwversion, reg10;
6075 /* set absolute mode */
6076 hwversion = elanhw->hwversion;
6078 switch (hwversion) {
6080 reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4;
6081 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6084 res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A);
6088 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6091 res = elantech_write_1(kbdc, hwversion, 0x07, 0x01);
6097 /* Read back reg 0x10 to ensure hardware is ready. */
6098 if (res == 0 && reg10 >= 0) {
6099 for (i = 0; i < 5; i++) {
6100 if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0)
6109 kprintf("couldn't set absolute mode\n");
6115 elantech_init_synaptics(struct psm_softc *sc)
6118 /* Set capabilites required by movement smother */
6119 sc->synhw.infoMajor = sc->elanhw.hwversion;
6120 sc->synhw.infoMinor = sc->elanhw.fwversion;
6121 sc->synhw.infoXupmm = sc->elanhw.dpmmx;
6122 sc->synhw.infoYupmm = sc->elanhw.dpmmy;
6123 sc->synhw.verticalScroll = 0;
6124 sc->synhw.nExtendedQueries = 4;
6125 sc->synhw.capExtended = 1;
6126 sc->synhw.capPassthrough = sc->elanhw.hastrackpoint;
6127 sc->synhw.capClickPad = sc->elanhw.isclickpad;
6128 sc->synhw.capMultiFinger = 1;
6129 sc->synhw.capAdvancedGestures = 1;
6130 sc->synhw.capPalmDetect = 1;
6131 sc->synhw.capPen = 0;
6132 sc->synhw.capReportsMax = 1;
6133 sc->synhw.maximumXCoord = sc->elanhw.sizex;
6134 sc->synhw.maximumYCoord = sc->elanhw.sizey;
6135 sc->synhw.capReportsMin = 1;
6136 sc->synhw.minimumXCoord = 0;
6137 sc->synhw.minimumYCoord = 0;
6139 if (sc->syninfo.sysctl_tree == NULL) {
6140 synaptics_sysctl_create_tree(sc, "elantech",
6141 "Elantech Touchpad");
6144 * Adjust synaptic smoother tunables
6145 * 1. Disable finger detection pressure threshold. Unlike
6146 * synaptics we assume the finger is acting when packet with
6147 * its X&Y arrives not when pressure exceedes some threshold
6148 * 2. Disable unrelated features like margins and noisy areas
6149 * 3. Disable virtual scroll areas as 2nd finger is preferable
6150 * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized
6152 * 5. Scale down divisors and movement lengths by a factor of 3
6153 * where 3 is Synaptics to Elantech (~2200/800) dpi ratio
6156 /* Set reporting range to be equal touchpad size */
6157 sc->syninfo.max_x = sc->elanhw.sizex;
6158 sc->syninfo.max_y = sc->elanhw.sizey;
6160 /* Disable finger detection pressure threshold */
6161 sc->syninfo.min_pressure = 1;
6163 /* Use full area of touchpad */
6164 sc->syninfo.margin_top = 0;
6165 sc->syninfo.margin_right = 0;
6166 sc->syninfo.margin_bottom = 0;
6167 sc->syninfo.margin_left = 0;
6169 /* Disable noisy area */
6170 sc->syninfo.na_top = 0;
6171 sc->syninfo.na_right = 0;
6172 sc->syninfo.na_bottom = 0;
6173 sc->syninfo.na_left = 0;
6175 /* Tune divisors and movement lengths */
6176 sc->syninfo.weight_len_squared = 200;
6177 sc->syninfo.div_min = 3;
6178 sc->syninfo.div_max = 6;
6179 sc->syninfo.div_max_na = 10;
6180 sc->syninfo.div_len = 30;
6181 sc->syninfo.tap_max_delta = 25;
6183 /* Disable virtual scrolling areas and tune its divisors */
6184 sc->syninfo.vscroll_hor_area = 0;
6185 sc->syninfo.vscroll_ver_area = 0;
6186 sc->syninfo.vscroll_min_delta = 15;
6187 sc->syninfo.vscroll_div_min = 30;
6188 sc->syninfo.vscroll_div_max = 50;
6190 /* Set bottom quarter as 42% - 16% - 42% sized softbuttons */
6191 if (sc->elanhw.isclickpad) {
6192 sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4;
6193 sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25;
6194 sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25;
6202 enable_elantech(struct psm_softc *sc, enum probearg arg)
6204 static const int ic2hw[] =
6205 /*IC: 0 1 2 3 4 5 6 7 8 9 a b c d e f */
6206 { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0 };
6207 elantechhw_t elanhw;
6208 int icversion, hwversion, dptracex, dptracey, id, resp[3], dpix, dpiy;
6209 KBDC kbdc = sc->kbdc;
6211 VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n"));
6213 set_mouse_scaling(kbdc, 1);
6214 set_mouse_scaling(kbdc, 1);
6215 set_mouse_scaling(kbdc, 1);
6216 if (get_mouse_status(kbdc, resp, 0, 3) != 3)
6219 if (!ELANTECH_MAGIC(resp))
6222 /* Identify the Touchpad version. */
6223 if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp))
6226 bzero(&elanhw, sizeof(elanhw));
6228 elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2];
6229 icversion = resp[0] & 0x0f;
6230 hwversion = ic2hw[icversion];
6233 kprintf("Elantech touchpad hardware v.%d firmware v.0x%06x\n",
6234 hwversion, elanhw.fwversion);
6236 if (ELANTECH_HW_IS_V1(elanhw.fwversion)) {
6237 kprintf (" Unsupported touchpad hardware (v1)\n");
6240 if (hwversion == 0) {
6241 kprintf (" Unknown touchpad hardware (firmware v.0x%06x)\n",
6246 /* Get the Touchpad model information. */
6247 elanhw.hwversion = hwversion;
6248 elanhw.issemimt = hwversion == 2;
6249 elanhw.isclickpad = (resp[1] & 0x10) != 0;
6250 elanhw.hascrc = (resp[1] & 0x40) != 0;
6251 elanhw.haspressure = elanhw.fwversion >= 0x020800;
6253 /* Read the capability bits. */
6254 if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) {
6255 kprintf(" Failed to read capability bits\n");
6259 elanhw.ntracesx = resp[1] - 1;
6260 elanhw.ntracesy = resp[2] - 1;
6261 elanhw.hastrackpoint = (resp[0] & 0x80) != 0;
6263 /* Get the touchpad resolution */
6264 switch (hwversion) {
6266 if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp)
6268 dpix = (resp[1] & 0x0f) * 10 + 790;
6269 dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790;
6270 elanhw.dpmmx = (dpix * 10 + 5) / 254;
6271 elanhw.dpmmy = (dpiy * 10 + 5) / 254;
6277 elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */
6281 if (!elantech_support)
6284 if (elantech_init(kbdc, &elanhw)) {
6285 kprintf("couldn't initialize elantech touchpad\n");
6290 * Get the touchpad reporting range.
6291 * On HW v.3 touchpads it should be done after switching hardware
6292 * to real resolution mode (by setting bit 3 of reg10)
6294 if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) {
6295 kprintf(" Failed to read touchpad size\n");
6296 elanhw.sizex = 10000; /* Arbitrary high values to */
6297 elanhw.sizey = 10000; /* prevent clipping in smoother */
6298 } else if (hwversion == 2) {
6299 dptracex = dptracey = 64;
6300 if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) &&
6301 !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) {
6302 dptracex = resp[1] / 2;
6303 dptracey = resp[2] / 2;
6305 elanhw.sizex = (elanhw.ntracesx - 1) * dptracex;
6306 elanhw.sizey = (elanhw.ntracesy - 1) * dptracey;
6308 elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1];
6309 elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2];
6313 kprintf(" Model information:\n");
6314 kprintf(" MaxX: %d\n", elanhw.sizex);
6315 kprintf(" MaxY: %d\n", elanhw.sizey);
6316 kprintf(" DpmmX: %d\n", elanhw.dpmmx);
6317 kprintf(" DpmmY: %d\n", elanhw.dpmmy);
6318 kprintf(" TracesX: %d\n", elanhw.ntracesx);
6319 kprintf(" TracesY: %d\n", elanhw.ntracesy);
6320 kprintf(" SemiMT: %d\n", elanhw.issemimt);
6321 kprintf(" Clickpad: %d\n", elanhw.isclickpad);
6322 kprintf(" Trackpoint: %d\n", elanhw.hastrackpoint);
6323 kprintf(" CRC: %d\n", elanhw.hascrc);
6324 kprintf(" Pressure: %d\n", elanhw.haspressure);
6327 VLOG(3, (LOG_DEBUG, "elantech: END init\n"));
6330 sc->elanhw = elanhw;
6333 /* Initialize synaptics movement smoother */
6334 elantech_init_synaptics(sc);
6336 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
6337 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
6343 * Return true if 'now' is earlier than (start + (secs.usecs)).
6344 * Now may be NULL and the function will fetch the current time from
6345 * getmicrouptime(), or a cached 'now' can be passed in.
6346 * All values should be numbers derived from getmicrouptime().
6349 timeelapsed(const struct timeval *start, int secs, int usecs,
6350 const struct timeval *now) {
6351 struct timeval snow, tv;
6353 /* if there is no 'now' passed in, the get it as a convenience. */
6355 getmicrouptime(&snow);
6361 timevaladd(&tv, start);
6362 return (timevalcmp(&tv, now, <));
6366 psmresume(device_t dev)
6369 struct psm_softc *sc = device_get_softc(dev);
6370 int unit = device_get_unit(dev);
6373 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
6376 (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
6379 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
6381 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
6383 * Release the blocked process; it must be notified that
6384 * the device cannot be accessed anymore.
6386 sc->state &= ~PSM_ASLP;
6390 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
6395 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, NULL, NULL);
6400 * This sucks up assignments from PNPBIOS and ACPI.
6404 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
6405 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
6406 * can be probed and attached only after the AT keyboard controller is
6407 * attached, we shall quietly reserve the IRQ resource for later use.
6408 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
6409 * copy the IRQ resource to the PS/2 mouse device instance hanging
6410 * under the keyboard controller, then probe and attach it.
6413 static devclass_t psmcpnp_devclass;
6415 static device_probe_t psmcpnp_probe;
6416 static device_attach_t psmcpnp_attach;
6418 static device_method_t psmcpnp_methods[] = {
6419 DEVMETHOD(device_probe, psmcpnp_probe),
6420 DEVMETHOD(device_attach, psmcpnp_attach),
6425 static driver_t psmcpnp_driver = {
6426 PSMCPNP_DRIVER_NAME,
6431 static struct isa_pnp_id psmcpnp_ids[] = {
6432 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
6433 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */
6434 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */
6435 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
6436 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
6437 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
6438 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */
6439 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
6440 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
6441 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
6442 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
6443 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
6444 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
6449 create_a_copy(device_t atkbdc, device_t me)
6455 /* find the PS/2 mouse device instance under the keyboard controller */
6456 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
6457 device_get_unit(atkbdc));
6460 if (device_get_state(psm) != DS_NOTPRESENT)
6463 /* move our resource to the found device */
6464 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
6465 bus_delete_resource(me, SYS_RES_IRQ, 0);
6466 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1,
6467 machintr_legacy_intr_cpuid(irq));
6469 /* ...then probe and attach it */
6470 return (device_probe_and_attach(psm));
6474 psmcpnp_probe(device_t dev)
6476 struct resource *res;
6480 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
6484 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
6485 * to the PS/2 mouse device node. But, some buggy PnP BIOS
6486 * declares the PS/2 mouse device node without an IRQ resource!
6487 * If this happens, we shall refer to device hints.
6488 * If we still don't find it there, use a hardcoded value... XXX
6491 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
6493 if (resource_long_value(PSM_DRIVER_NAME,
6494 device_get_unit(dev),"irq", &irq) != 0)
6496 device_printf(dev, "irq resource info is missing; "
6497 "assuming irq %ld\n", irq);
6498 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1,
6499 machintr_legacy_intr_cpuid(irq));
6501 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
6502 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
6508 return ((res == NULL) ? ENXIO : 0);
6512 psmcpnp_attach(device_t dev)
6516 /* find the keyboard controller, which may be on acpi* or isa* bus */
6517 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
6518 device_get_unit(dev));
6519 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
6520 create_a_copy(atkbdc, dev);
6523 * If we don't have the AT keyboard controller yet,
6524 * just reserve the IRQ for later use...
6525 * (See psmidentify() above.)
6528 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
6534 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, NULL, NULL);
6535 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, NULL, NULL);
6538 #endif /* DEV_ISA */