2 * Copyright (c) 1992, 1993 Erik Forsberg.
3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
15 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 * Ported to 386bsd Oct 17, 1992
25 * Sandi Donno, Computer Science, University of Cape Town, South Africa
26 * Please send bug reports to sandi@cs.uct.ac.za
28 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
29 * although I was only partially successful in getting the alpha release
30 * of his "driver for the Logitech and ATI Inport Bus mice for use with
31 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
32 * found his code to be an invaluable reference when porting this driver
35 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
36 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
38 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
39 * Andrew Herbert - 12 June 1993
41 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
44 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
47 * Hardware access routines and probe logic rewritten by
48 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
49 * - 3, 14, 22 October 1996.
50 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
51 * - 14, 30 November 1996. Uses `kbdio.c'.
52 * - 13 December 1996. Uses queuing version of `kbdio.c'.
53 * - January/February 1997. Tweaked probe logic for
54 * HiNote UltraII/Latitude/Armada laptops.
55 * - 30 July 1997. Added APM support.
56 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
57 * Improved sync check logic.
58 * Vendor specific support routines.
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
66 #include "opt_evdev.h"
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/module.h>
74 #include <sys/filio.h>
76 #include <sys/sigio.h>
77 #include <sys/signalvar.h>
78 #include <sys/syslog.h>
79 #include <machine/bus.h>
81 #include <sys/selinfo.h>
82 #include <sys/sysctl.h>
86 #include <sys/limits.h>
87 #include <sys/mouse.h>
88 #include <machine/resource.h>
91 #include <isa/isavar.h>
95 #include <dev/evdev/evdev.h>
96 #include <dev/evdev/input.h>
99 #include <dev/atkbdc/atkbdcreg.h>
100 #include <dev/atkbdc/psm.h>
103 * Driver specific options: the following options may be set by
104 * `options' statements in the kernel configuration file.
109 #define PSM_DEBUG 0 /*
110 * logging: 0: none, 1: brief, 2: verbose
111 * 3: sync errors, 4: all packets
114 #define VLOG(level, args) do { \
115 if (verbose >= level) \
119 #ifndef PSM_INPUT_TIMEOUT
120 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
123 #ifndef PSM_TAP_TIMEOUT
124 #define PSM_TAP_TIMEOUT 125000
127 #ifndef PSM_TAP_THRESHOLD
128 #define PSM_TAP_THRESHOLD 25
131 /* end of driver specific options */
133 #define PSMCPNP_DRIVER_NAME "psmcpnp"
135 struct psmcpnp_softc {
140 } type; /* Based on PnP ID */
144 #define PSM_BUFSIZE 960
145 #define PSM_SMALLBUFSIZE 240
147 /* operation levels */
148 #define PSM_LEVEL_BASE 0
149 #define PSM_LEVEL_STANDARD 1
150 #define PSM_LEVEL_NATIVE 2
151 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
152 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
154 /* Logitech PS2++ protocol */
155 #define MOUSE_PS2PLUS_CHECKBITS(b) \
156 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
157 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
158 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
161 typedef struct ringbuf {
162 int count; /* # of valid elements in the buffer */
163 int head; /* head pointer */
164 int tail; /* tail poiner */
165 u_char buf[PSM_BUFSIZE];
169 typedef struct packetbuf {
170 u_char ipacket[16]; /* interim input buffer */
171 int inputbytes; /* # of bytes in the input buffer */
174 #ifndef PSM_PACKETQUEUE
175 #define PSM_PACKETQUEUE 128
179 * Typical bezel limits. Taken from 'Synaptics
180 * PS/2 TouchPad Interfacing Guide' p.3.2.3.
182 #define SYNAPTICS_DEFAULT_MAX_X 5472
183 #define SYNAPTICS_DEFAULT_MAX_Y 4448
184 #define SYNAPTICS_DEFAULT_MIN_X 1472
185 #define SYNAPTICS_DEFAULT_MIN_Y 1408
187 typedef struct synapticsinfo {
188 struct sysctl_ctx_list sysctl_ctx;
189 struct sysctl_oid *sysctl_tree;
190 int directional_scrolls;
191 int two_finger_scroll;
208 int weight_previous_na;
209 int weight_len_squared;
217 int vscroll_ver_area;
218 int vscroll_hor_area;
219 int vscroll_min_delta;
230 typedef struct synapticspacket {
235 #define SYNAPTICS_PACKETQUEUE 10
236 #define SYNAPTICS_QUEUE_CURSOR(x) \
237 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
239 #define SYNAPTICS_VERSION_GE(synhw, major, minor) \
240 ((synhw).infoMajor > (major) || \
241 ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
243 typedef struct smoother {
244 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
257 typedef struct gesture {
263 int zmax; /* maximum pressure value */
264 struct timeval taptimeout; /* tap timeout for touchpads */
268 TRACKPOINT_SYSCTL_SENSITIVITY,
269 TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
270 TRACKPOINT_SYSCTL_UPPER_PLATEAU,
271 TRACKPOINT_SYSCTL_BACKUP_RANGE,
272 TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
273 TRACKPOINT_SYSCTL_MINIMUM_DRAG,
274 TRACKPOINT_SYSCTL_UP_THRESHOLD,
275 TRACKPOINT_SYSCTL_THRESHOLD,
276 TRACKPOINT_SYSCTL_JENKS_CURVATURE,
277 TRACKPOINT_SYSCTL_Z_TIME,
278 TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
279 TRACKPOINT_SYSCTL_SKIP_BACKUPS
282 typedef struct trackpointinfo {
283 struct sysctl_ctx_list sysctl_ctx;
284 struct sysctl_oid *sysctl_tree;
299 typedef struct finger {
306 #define PSM_FINGERS 2 /* # of processed fingers */
307 #define PSM_FINGER_IS_PEN (1<<0)
308 #define PSM_FINGER_FUZZY (1<<1)
309 #define PSM_FINGER_DEFAULT_P tap_threshold
310 #define PSM_FINGER_DEFAULT_W 1
311 #define PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0)
312 #define PSM_FINGER_RESET(f) do { \
313 (f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \
316 typedef struct elantechhw {
334 /* minimum versions supported by this driver */
335 #define ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600)
337 #define ELANTECH_MAGIC(magic) \
338 ((magic)[0] == 0x3c && (magic)[1] == 0x03 && \
339 ((magic)[2] == 0xc8 || (magic)[2] == 0x00))
341 #define ELANTECH_FW_ID 0x00
342 #define ELANTECH_FW_VERSION 0x01
343 #define ELANTECH_CAPABILITIES 0x02
344 #define ELANTECH_SAMPLE 0x03
345 #define ELANTECH_RESOLUTION 0x04
346 #define ELANTECH_REG_READ 0x10
347 #define ELANTECH_REG_WRITE 0x11
348 #define ELANTECH_REG_RDWR 0x00
349 #define ELANTECH_CUSTOM_CMD 0xf8
352 #define ELANTECH_MAX_FINGERS 5
354 #define ELANTECH_MAX_FINGERS PSM_FINGERS
357 #define ELANTECH_FINGER_MAX_P 255
358 #define ELANTECH_FINGER_MAX_W 15
359 #define ELANTECH_FINGER_SET_XYP(pb) (finger_t) { \
360 .x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2], \
361 .y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5], \
362 .p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f), \
363 .w = PSM_FINGER_DEFAULT_W, \
369 ELANTECH_PKT_TRACKPOINT,
370 ELANTECH_PKT_V2_COMMON,
371 ELANTECH_PKT_V2_2FINGER,
373 ELANTECH_PKT_V4_STATUS,
374 ELANTECH_PKT_V4_HEAD,
375 ELANTECH_PKT_V4_MOTION
378 #define ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06)
379 #define ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 : \
380 (pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) && \
381 (pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff && \
382 (pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff && \
383 (pb)->ipacket[5] == 0xff)
384 #define ELANTECH_PKT_IS_V2(pb) \
385 (((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02)
386 #define ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? \
387 ((pb)->ipacket[3] & 0x09) == 0x08 : \
388 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02)
389 #define ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? \
390 ((pb)->ipacket[3] & 0x09) == 0x09 : \
391 ((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c)
392 #define ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? \
393 ((pb)->ipacket[3] & 0x08) == 0x00 : \
394 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10)
396 typedef struct elantechaction {
397 finger_t fingers[ELANTECH_MAX_FINGERS];
402 /* driver control block */
403 struct psm_softc { /* Driver status information */
405 struct selinfo rsel; /* Process selecting for Input */
406 u_char state; /* Mouse driver state */
407 int config; /* driver configuration flags */
408 int flags; /* other flags */
409 KBDC kbdc; /* handle to access kbd controller */
410 struct resource *intr; /* IRQ resource */
411 void *ih; /* interrupt handle */
412 mousehw_t hw; /* hardware information */
413 synapticshw_t synhw; /* Synaptics hardware information */
414 synapticsinfo_t syninfo; /* Synaptics configuration */
415 smoother_t smoother[PSM_FINGERS]; /* Motion smoothing */
416 gesture_t gesture; /* Gesture context */
417 elantechhw_t elanhw; /* Elantech hardware information */
418 elantechaction_t elanaction; /* Elantech action context */
419 int tphw; /* TrackPoint hardware information */
420 trackpointinfo_t tpinfo; /* TrackPoint configuration */
421 mousemode_t mode; /* operation mode */
422 mousemode_t dflt_mode; /* default operation mode */
423 mousestatus_t status; /* accumulated mouse movement */
424 ringbuf_t queue; /* mouse status queue */
425 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
426 int pqueue_start; /* start of data in queue */
427 int pqueue_end; /* end of data in queue */
428 int button; /* the latest button state */
429 int xold; /* previous absolute X position */
430 int yold; /* previous absolute Y position */
431 int xaverage; /* average X position */
432 int yaverage; /* average Y position */
433 int squelch; /* level to filter movement at low speed */
434 int syncerrors; /* # of bytes discarded to synchronize */
435 int pkterrors; /* # of packets failed during quaranteen. */
436 int fpcount; /* forcePad valid packet counter */
437 struct timeval inputtimeout;
438 struct timeval lastsoftintr; /* time of last soft interrupt */
439 struct timeval lastinputerr; /* time last sync error happened */
440 struct timeval idletimeout;
441 packetbuf_t idlepacket; /* packet to send after idle timeout */
442 int watchdog; /* watchdog timer flag */
443 struct callout callout; /* watchdog timer call out */
444 struct callout softcallout; /* buffer timer call out */
449 struct sigio *async; /* Processes waiting for SIGIO */
450 int extended_buttons;
452 struct evdev_dev *evdev_a; /* Absolute reporting device */
453 struct evdev_dev *evdev_r; /* Relative reporting device */
456 static devclass_t psm_devclass;
458 /* driver state flags (state) */
459 #define PSM_VALID 0x80
460 #define PSM_OPEN 1 /* Device is open */
461 #define PSM_ASLP 2 /* Waiting for mouse data */
462 #define PSM_SOFTARMED 4 /* Software interrupt armed */
463 #define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
464 #define PSM_EV_OPEN_R 0x10 /* Relative evdev device is open */
465 #define PSM_EV_OPEN_A 0x20 /* Absolute evdev device is open */
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))
493 #define ALWAYS_RESTORE_CONTROLLER(kbdc) !(kbdcp(kbdc)->quirks \
494 & KBDC_QUIRK_KEEP_ACTIVATED)
497 static int tap_enabled = -1;
498 static int verbose = PSM_DEBUG;
499 static int synaptics_support = 0;
500 static int trackpoint_support = 0;
501 static int elantech_support = 0;
503 /* for backward compatibility */
504 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
505 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
506 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
508 typedef struct old_mousehw {
515 typedef struct old_mousemode {
522 #define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field)
524 SYNAPTICS_SYSCTL_MIN_PRESSURE = SYN_OFFSET(min_pressure),
525 SYNAPTICS_SYSCTL_MAX_PRESSURE = SYN_OFFSET(max_pressure),
526 SYNAPTICS_SYSCTL_MAX_WIDTH = SYN_OFFSET(max_width),
527 SYNAPTICS_SYSCTL_MARGIN_TOP = SYN_OFFSET(margin_top),
528 SYNAPTICS_SYSCTL_MARGIN_RIGHT = SYN_OFFSET(margin_right),
529 SYNAPTICS_SYSCTL_MARGIN_BOTTOM = SYN_OFFSET(margin_bottom),
530 SYNAPTICS_SYSCTL_MARGIN_LEFT = SYN_OFFSET(margin_left),
531 SYNAPTICS_SYSCTL_NA_TOP = SYN_OFFSET(na_top),
532 SYNAPTICS_SYSCTL_NA_RIGHT = SYN_OFFSET(na_right),
533 SYNAPTICS_SYSCTL_NA_BOTTOM = SYN_OFFSET(na_bottom),
534 SYNAPTICS_SYSCTL_NA_LEFT = SYN_OFFSET(na_left),
535 SYNAPTICS_SYSCTL_WINDOW_MIN = SYN_OFFSET(window_min),
536 SYNAPTICS_SYSCTL_WINDOW_MAX = SYN_OFFSET(window_max),
537 SYNAPTICS_SYSCTL_MULTIPLICATOR = SYN_OFFSET(multiplicator),
538 SYNAPTICS_SYSCTL_WEIGHT_CURRENT = SYN_OFFSET(weight_current),
539 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS = SYN_OFFSET(weight_previous),
540 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA = SYN_OFFSET(weight_previous_na),
541 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED = SYN_OFFSET(weight_len_squared),
542 SYNAPTICS_SYSCTL_DIV_MIN = SYN_OFFSET(div_min),
543 SYNAPTICS_SYSCTL_DIV_MAX = SYN_OFFSET(div_max),
544 SYNAPTICS_SYSCTL_DIV_MAX_NA = SYN_OFFSET(div_max_na),
545 SYNAPTICS_SYSCTL_DIV_LEN = SYN_OFFSET(div_len),
546 SYNAPTICS_SYSCTL_TAP_MAX_DELTA = SYN_OFFSET(tap_max_delta),
547 SYNAPTICS_SYSCTL_TAP_MIN_QUEUE = SYN_OFFSET(tap_min_queue),
548 SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT = SYN_OFFSET(taphold_timeout),
549 SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA = SYN_OFFSET(vscroll_hor_area),
550 SYNAPTICS_SYSCTL_VSCROLL_VER_AREA = SYN_OFFSET(vscroll_ver_area),
551 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA = SYN_OFFSET(vscroll_min_delta),
552 SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN = SYN_OFFSET(vscroll_div_min),
553 SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX = SYN_OFFSET(vscroll_div_max),
554 SYNAPTICS_SYSCTL_TOUCHPAD_OFF = SYN_OFFSET(touchpad_off),
555 SYNAPTICS_SYSCTL_SOFTBUTTONS_Y = SYN_OFFSET(softbuttons_y),
556 SYNAPTICS_SYSCTL_SOFTBUTTON2_X = SYN_OFFSET(softbutton2_x),
557 SYNAPTICS_SYSCTL_SOFTBUTTON3_X = SYN_OFFSET(softbutton3_x),
560 /* packet formatting function */
561 typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
564 /* function prototypes */
565 static void psmidentify(driver_t *, device_t);
566 static int psmprobe(device_t);
567 static int psmattach(device_t);
568 static int psmdetach(device_t);
569 static int psmresume(device_t);
571 static d_open_t psm_cdev_open;
572 static d_close_t psm_cdev_close;
573 static d_read_t psmread;
574 static d_write_t psmwrite;
575 static d_ioctl_t psmioctl;
576 static d_poll_t psmpoll;
578 static int psmopen(struct psm_softc *);
579 static int psmclose(struct psm_softc *);
582 static evdev_open_t psm_ev_open_r;
583 static evdev_close_t psm_ev_close_r;
584 static evdev_open_t psm_ev_open_a;
585 static evdev_close_t psm_ev_close_a;
588 static int enable_aux_dev(KBDC);
589 static int disable_aux_dev(KBDC);
590 static int get_mouse_status(KBDC, int *, int, int);
591 static int get_aux_id(KBDC);
592 static int set_mouse_sampling_rate(KBDC, int);
593 static int set_mouse_scaling(KBDC, int);
594 static int set_mouse_resolution(KBDC, int);
595 static int set_mouse_mode(KBDC);
596 static int get_mouse_buttons(KBDC);
597 static int is_a_mouse(int);
598 static void recover_from_error(KBDC);
599 static int restore_controller(KBDC, int);
600 static int doinitialize(struct psm_softc *, mousemode_t *);
601 static int doopen(struct psm_softc *, int);
602 static int reinitialize(struct psm_softc *, int);
603 static char *model_name(int);
604 static void psmsoftintr(void *);
605 static void psmsoftintridle(void *);
606 static void psmintr(void *);
607 static void psmtimeout(void *);
608 static int timeelapsed(const struct timeval *, int, int,
609 const struct timeval *);
610 static void dropqueue(struct psm_softc *);
611 static void flushpackets(struct psm_softc *);
612 static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
613 mousestatus_t *, int *, int *, int *);
614 static int proc_synaptics(struct psm_softc *, packetbuf_t *,
615 mousestatus_t *, int *, int *, int *);
616 static void proc_versapad(struct psm_softc *, packetbuf_t *,
617 mousestatus_t *, int *, int *, int *);
618 static int proc_elantech(struct psm_softc *, packetbuf_t *,
619 mousestatus_t *, int *, int *, int *);
620 static int psmpalmdetect(struct psm_softc *, finger_t *, int);
621 static void psmgestures(struct psm_softc *, finger_t *, int,
623 static void psmsmoother(struct psm_softc *, finger_t *, int,
624 mousestatus_t *, int *, int *);
625 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
628 /* vendor specific features */
629 enum probearg { PROBE, REINIT };
630 typedef int probefunc_t(struct psm_softc *, enum probearg);
632 static int mouse_id_proc1(KBDC, int, int, int *);
633 static int mouse_ext_command(KBDC, int);
635 static probefunc_t enable_groller;
636 static probefunc_t enable_gmouse;
637 static probefunc_t enable_aglide;
638 static probefunc_t enable_kmouse;
639 static probefunc_t enable_msexplorer;
640 static probefunc_t enable_msintelli;
641 static probefunc_t enable_4dmouse;
642 static probefunc_t enable_4dplus;
643 static probefunc_t enable_mmanplus;
644 static probefunc_t enable_synaptics;
645 static probefunc_t enable_trackpoint;
646 static probefunc_t enable_versapad;
647 static probefunc_t enable_elantech;
649 static void set_trackpoint_parameters(struct psm_softc *sc);
650 static void synaptics_passthrough_on(struct psm_softc *sc);
651 static void synaptics_passthrough_off(struct psm_softc *sc);
652 static int synaptics_preferred_mode(struct psm_softc *sc);
653 static void synaptics_set_mode(struct psm_softc *sc, int mode_byte);
659 probefunc_t *probefunc;
662 * WARNING: the order of probe is very important. Don't mess it
663 * unless you know what you are doing.
665 { MOUSE_MODEL_NET, /* Genius NetMouse */
666 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
667 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
668 0xc8, 6, enable_groller },
669 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
670 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
671 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
672 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
673 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
674 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
675 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
676 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
677 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
678 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
679 { MOUSE_MODEL_ELANTECH, /* Elantech Touchpad */
680 0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech },
681 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
682 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
683 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
684 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
685 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
686 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
687 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
688 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
689 { MOUSE_MODEL_TRACKPOINT, /* IBM/Lenovo TrackPoint */
690 0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
691 { MOUSE_MODEL_GENERIC,
692 0xc0, MOUSE_PS2_PACKETSIZE, NULL },
694 #define GENERIC_MOUSE_ENTRY (nitems(vendortype) - 1)
696 /* device driver declarateion */
697 static device_method_t psm_methods[] = {
698 /* Device interface */
699 DEVMETHOD(device_identify, psmidentify),
700 DEVMETHOD(device_probe, psmprobe),
701 DEVMETHOD(device_attach, psmattach),
702 DEVMETHOD(device_detach, psmdetach),
703 DEVMETHOD(device_resume, psmresume),
708 static driver_t psm_driver = {
711 sizeof(struct psm_softc),
714 static struct cdevsw psm_cdevsw = {
715 .d_version = D_VERSION,
716 .d_flags = D_NEEDGIANT,
717 .d_open = psm_cdev_open,
718 .d_close = psm_cdev_close,
723 .d_name = PSM_DRIVER_NAME,
727 static const struct evdev_methods psm_ev_methods_r = {
728 .ev_open = psm_ev_open_r,
729 .ev_close = psm_ev_close_r,
731 static const struct evdev_methods psm_ev_methods_a = {
732 .ev_open = psm_ev_open_a,
733 .ev_close = psm_ev_close_a,
737 /* device I/O routines */
739 enable_aux_dev(KBDC kbdc)
743 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
744 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
746 return (res == PSM_ACK);
750 disable_aux_dev(KBDC kbdc)
754 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
755 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
757 return (res == PSM_ACK);
761 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
770 cmd = PSMC_SEND_DEV_STATUS;
773 cmd = PSMC_SEND_DEV_DATA;
776 empty_aux_buffer(kbdc, 5);
777 res = send_aux_command(kbdc, cmd);
778 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
779 (flag == 1) ? "DATA" : "STATUS", res));
783 for (i = 0; i < len; ++i) {
784 status[i] = read_aux_data(kbdc);
791 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
792 (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
799 get_aux_id(KBDC kbdc)
804 empty_aux_buffer(kbdc, 5);
805 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
806 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
813 id = read_aux_data(kbdc);
814 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
820 set_mouse_sampling_rate(KBDC kbdc, int rate)
824 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
825 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
827 return ((res == PSM_ACK) ? rate : -1);
831 set_mouse_scaling(KBDC kbdc, int scale)
838 scale = PSMC_SET_SCALING11;
841 scale = PSMC_SET_SCALING21;
844 res = send_aux_command(kbdc, scale);
845 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
846 (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
848 return (res == PSM_ACK);
851 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
853 set_mouse_resolution(KBDC kbdc, int val)
857 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
858 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
860 return ((res == PSM_ACK) ? val : -1);
864 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
865 * re-enabled by calling `enable_aux_dev()'
868 set_mouse_mode(KBDC kbdc)
872 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
873 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
875 return (res == PSM_ACK);
879 get_mouse_buttons(KBDC kbdc)
881 int c = 2; /* assume two buttons by default */
885 * NOTE: a special sequence to obtain Logitech Mouse specific
886 * information: set resolution to 25 ppi, set scaling to 1:1, set
887 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
888 * mouse status bytes is the number of available buttons.
889 * Some manufactures also support this sequence.
891 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
893 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
894 set_mouse_scaling(kbdc, 1) &&
895 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
900 /* misc subroutines */
902 * Someday, I will get the complete list of valid pointing devices and
909 static int valid_ids[] = {
910 PSM_MOUSE_ID, /* mouse */
911 PSM_BALLPOINT_ID, /* ballpoint device */
912 PSM_INTELLI_ID, /* Intellimouse */
913 PSM_EXPLORER_ID, /* Intellimouse Explorer */
914 -1 /* end of table */
918 for (i = 0; valid_ids[i] >= 0; ++i)
919 if (valid_ids[i] == id)
928 model_name(int model)
934 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
935 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
936 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
937 { MOUSE_MODEL_THINK, "ThinkingMouse" },
938 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
939 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
940 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
941 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
942 { MOUSE_MODEL_4D, "4D Mouse" },
943 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
944 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
945 { MOUSE_MODEL_TRACKPOINT, "IBM/Lenovo TrackPoint" },
946 { MOUSE_MODEL_ELANTECH, "Elantech Touchpad" },
947 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
948 { MOUSE_MODEL_UNKNOWN, "Unknown" },
952 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
953 if (models[i].model_code == model)
955 return (models[i].model_name);
959 recover_from_error(KBDC kbdc)
961 /* discard anything left in the output buffer */
962 empty_both_buffers(kbdc, 10);
966 * NOTE: KBDC_RESET_KBD may not restore the communication between the
967 * keyboard and the controller.
972 * NOTE: somehow diagnostic and keyboard port test commands bring the
975 if (!test_controller(kbdc))
976 log(LOG_ERR, "psm: keyboard controller failed.\n");
977 /* if there isn't a keyboard in the system, the following error is OK */
978 if (test_kbd_port(kbdc) != 0)
979 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
984 restore_controller(KBDC kbdc, int command_byte)
986 empty_both_buffers(kbdc, 10);
988 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
989 log(LOG_ERR, "psm: failed to restore the keyboard controller "
991 empty_both_buffers(kbdc, 10);
994 empty_both_buffers(kbdc, 10);
1000 * Re-initialize the aux port and device. The aux port must be enabled
1001 * and its interrupt must be disabled before calling this routine.
1002 * The aux device will be disabled before returning.
1003 * The keyboard controller must be locked via `kbdc_lock()' before
1004 * calling this routine.
1007 doinitialize(struct psm_softc *sc, mousemode_t *mode)
1009 KBDC kbdc = sc->kbdc;
1013 switch((i = test_aux_port(kbdc))) {
1014 case 1: /* ignore these errors */
1020 "psm%d: strange result for test aux port (%d).\n",
1023 case 0: /* no error */
1025 case -1: /* time out */
1026 default: /* error */
1027 recover_from_error(kbdc);
1028 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1030 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
1035 if (sc->config & PSM_CONFIG_NORESET) {
1037 * Don't try to reset the pointing device. It may possibly
1038 * be left in the unknown state, though...
1042 * NOTE: some controllers appears to hang the `keyboard' when
1043 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1045 if (!reset_aux_dev(kbdc)) {
1046 recover_from_error(kbdc);
1047 log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
1054 * both the aux port and the aux device is functioning, see
1055 * if the device can be enabled.
1057 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
1058 log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
1062 empty_both_buffers(kbdc, 10); /* remove stray data if any */
1064 /* Re-enable the mouse. */
1065 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1066 if (vendortype[i].model == sc->hw.model)
1067 (*vendortype[i].probefunc)(sc, REINIT);
1069 /* set mouse parameters */
1070 if (mode != (mousemode_t *)NULL) {
1072 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
1073 if (mode->resolution >= 0)
1075 set_mouse_resolution(kbdc, mode->resolution);
1076 set_mouse_scaling(kbdc, 1);
1077 set_mouse_mode(kbdc);
1080 /* Record sync on the next data packet we see. */
1081 sc->flags |= PSM_NEED_SYNCBITS;
1083 /* just check the status of the mouse */
1084 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
1085 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
1092 doopen(struct psm_softc *sc, int command_byte)
1097 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
1098 * no obvious reason. Thus we check the current mode and restore the
1099 * Absolute Mode if it was cleared.
1101 * The previous hack at the end of psmprobe() wasn't efficient when
1102 * moused(8) was restarted.
1104 * A Reset (FF) or Set Defaults (F6) command would clear the
1105 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
1106 * doesn't show any evidence of such a command.
1108 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
1109 mouse_ext_command(sc->kbdc, 1);
1110 get_mouse_status(sc->kbdc, stat, 0, 3);
1111 if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
1112 stat[1] == 0x46 || stat[1] == 0x47) &&
1114 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1115 VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
1116 "hopefully restored\n",
1122 * A user may want to disable tap and drag gestures on a Synaptics
1123 * TouchPad when it operates in Relative Mode.
1125 if (sc->hw.model == MOUSE_MODEL_GENERIC) {
1126 if (tap_enabled > 0) {
1128 "psm%d: enable tap and drag gestures\n",
1130 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1131 } else if (tap_enabled == 0) {
1133 "psm%d: disable tap and drag gestures\n",
1135 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1139 /* enable the mouse device */
1140 if (!enable_aux_dev(sc->kbdc)) {
1141 /* MOUSE ERROR: failed to enable the mouse because:
1142 * 1) the mouse is faulty,
1143 * 2) the mouse has been removed(!?)
1144 * In the latter case, the keyboard may have hung, and need
1145 * recovery procedure...
1147 recover_from_error(sc->kbdc);
1149 /* FIXME: we could reset the mouse here and try to enable
1150 * it again. But it will take long time and it's not a good
1151 * idea to disable the keyboard that long...
1153 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
1154 recover_from_error(sc->kbdc);
1158 restore_controller(sc->kbdc, command_byte);
1159 /* mark this device is no longer available */
1160 sc->state &= ~PSM_VALID;
1162 "psm%d: failed to enable the device (doopen).\n",
1168 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1169 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
1172 /* enable the aux port and interrupt */
1173 if (!set_controller_command_byte(sc->kbdc,
1174 kbdc_get_device_mask(sc->kbdc),
1175 (command_byte & KBD_KBD_CONTROL_BITS) |
1176 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
1177 /* CONTROLLER ERROR */
1178 disable_aux_dev(sc->kbdc);
1179 restore_controller(sc->kbdc, command_byte);
1181 "psm%d: failed to enable the aux interrupt (doopen).\n",
1186 /* start the watchdog timer */
1187 sc->watchdog = FALSE;
1188 callout_reset(&sc->callout, hz * 2, psmtimeout, sc);
1194 reinitialize(struct psm_softc *sc, int doinit)
1200 /* don't let anybody mess with the aux device */
1201 if (!kbdc_lock(sc->kbdc, TRUE))
1205 /* block our watchdog timer */
1206 sc->watchdog = FALSE;
1207 callout_stop(&sc->callout);
1209 /* save the current controller command byte */
1210 empty_both_buffers(sc->kbdc, 10);
1211 c = get_controller_command_byte(sc->kbdc);
1213 "psm%d: current command byte: %04x (reinitialize).\n",
1216 /* enable the aux port but disable the aux interrupt and the keyboard */
1217 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
1218 kbdc_get_device_mask(sc->kbdc),
1219 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1220 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1221 /* CONTROLLER ERROR */
1223 kbdc_lock(sc->kbdc, FALSE);
1225 "psm%d: unable to set the command byte (reinitialize).\n",
1230 /* flush any data */
1231 if (sc->state & PSM_VALID) {
1232 /* this may fail; but never mind... */
1233 disable_aux_dev(sc->kbdc);
1234 empty_aux_buffer(sc->kbdc, 10);
1239 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1241 /* try to detect the aux device; are you still there? */
1244 if (doinitialize(sc, &sc->mode)) {
1246 sc->state |= PSM_VALID;
1248 /* the device has gone! */
1249 restore_controller(sc->kbdc, c);
1250 sc->state &= ~PSM_VALID;
1252 "psm%d: the aux device has gone! (reinitialize).\n",
1259 /* restore the driver state */
1260 if ((sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)) &&
1262 /* enable the aux device and the port again */
1263 err = doopen(sc, c);
1265 log(LOG_ERR, "psm%d: failed to enable the device "
1266 "(reinitialize).\n", sc->unit);
1268 /* restore the keyboard port and disable the aux port */
1269 if (!set_controller_command_byte(sc->kbdc,
1270 kbdc_get_device_mask(sc->kbdc),
1271 (c & KBD_KBD_CONTROL_BITS) |
1272 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1273 /* CONTROLLER ERROR */
1274 log(LOG_ERR, "psm%d: failed to disable the aux port "
1275 "(reinitialize).\n", sc->unit);
1280 kbdc_lock(sc->kbdc, FALSE);
1284 /* psm driver entry points */
1287 psmidentify(driver_t *driver, device_t parent)
1294 unit = device_get_unit(parent);
1296 /* always add at least one child */
1297 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1301 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1306 * If the PS/2 mouse device has already been reported by ACPI or
1307 * PnP BIOS, obtain the IRQ resource from it.
1308 * (See psmcpnp_attach() below.)
1310 psmc = device_find_child(device_get_parent(parent),
1311 PSMCPNP_DRIVER_NAME, unit);
1314 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1317 bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1318 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
1321 #define endprobe(v) do { \
1324 kbdc_set_device_mask(sc->kbdc, mask); \
1325 kbdc_lock(sc->kbdc, FALSE); \
1330 psmprobe(device_t dev)
1332 int unit = device_get_unit(dev);
1333 struct psm_softc *sc = device_get_softc(dev);
1344 /* see if IRQ is available */
1346 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1347 if (sc->intr == NULL) {
1349 device_printf(dev, "unable to allocate IRQ\n");
1352 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1355 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1356 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1357 /* XXX: for backward compatibility */
1358 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1360 #ifdef PSM_RESETAFTERSUSPEND
1361 PSM_CONFIG_INITAFTERSUSPEND;
1363 PSM_CONFIG_HOOKRESUME;
1365 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1370 device_set_desc(dev, "PS/2 Mouse");
1372 if (!kbdc_lock(sc->kbdc, TRUE)) {
1373 printf("psm%d: unable to lock the controller.\n", unit);
1380 * NOTE: two bits in the command byte controls the operation of the
1381 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1382 * port interrupt (IRQ 12) enable bit (bit 2).
1385 /* discard anything left after the keyboard initialization */
1386 empty_both_buffers(sc->kbdc, 10);
1388 /* save the current command byte; it will be used later */
1389 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1390 command_byte = get_controller_command_byte(sc->kbdc);
1392 printf("psm%d: current command byte:%04x\n", unit,
1394 if (command_byte == -1) {
1395 /* CONTROLLER ERROR */
1396 printf("psm%d: unable to get the current command byte value.\n",
1402 * disable the keyboard port while probing the aux port, which must be
1403 * enabled during this routine
1405 if (!set_controller_command_byte(sc->kbdc,
1406 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1407 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1408 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1410 * this is CONTROLLER ERROR; I don't know how to recover
1411 * from this error...
1413 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1414 restore_controller(sc->kbdc, command_byte);
1415 printf("psm%d: unable to set the command byte.\n", unit);
1418 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1421 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1422 * port exists and is functioning. However, some controllers appears
1423 * to respond with zero even when the aux port doesn't exist. (It may
1424 * be that this is only the case when the controller DOES have the aux
1425 * port but the port is not wired on the motherboard.) The keyboard
1426 * controllers without the port, such as the original AT, are
1427 * supposed to return with an error code or simply time out. In any
1428 * case, we have to continue probing the port even when the controller
1431 * XXX: some controllers erroneously return the error code 1, 2 or 3
1432 * when it has a perfectly functional aux port. We have to ignore
1433 * this error code. Even if the controller HAS error with the aux
1434 * port, it will be detected later...
1435 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1437 switch ((i = test_aux_port(sc->kbdc))) {
1438 case 1: /* ignore these errors */
1443 printf("psm%d: strange result for test aux port "
1444 "(%d).\n", unit, i);
1446 case 0: /* no error */
1448 case -1: /* time out */
1449 default: /* error */
1450 recover_from_error(sc->kbdc);
1451 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1453 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1454 restore_controller(sc->kbdc, command_byte);
1456 printf("psm%d: the aux port is not functioning (%d).\n",
1461 if (sc->config & PSM_CONFIG_NORESET) {
1463 * Don't try to reset the pointing device. It may possibly be
1464 * left in an unknown state, though...
1468 * NOTE: some controllers appears to hang the `keyboard' when
1469 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1471 * Attempt to reset the controller twice -- this helps
1472 * pierce through some KVM switches. The second reset
1475 if (!reset_aux_dev(sc->kbdc)) {
1476 recover_from_error(sc->kbdc);
1477 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1478 restore_controller(sc->kbdc, command_byte);
1480 printf("psm%d: failed to reset the aux "
1483 } else if (!reset_aux_dev(sc->kbdc)) {
1484 recover_from_error(sc->kbdc);
1486 printf("psm%d: failed to reset the aux device "
1492 * both the aux port and the aux device are functioning, see if the
1493 * device can be enabled. NOTE: when enabled, the device will start
1494 * sending data; we shall immediately disable the device once we know
1495 * the device can be enabled.
1497 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1499 recover_from_error(sc->kbdc);
1500 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1501 restore_controller(sc->kbdc, command_byte);
1503 printf("psm%d: failed to enable the aux device.\n",
1508 /* save the default values after reset */
1509 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1510 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1511 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1513 sc->dflt_mode.rate = sc->mode.rate = -1;
1514 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1517 /* hardware information */
1518 sc->hw.iftype = MOUSE_IF_PS2;
1520 /* verify the device is a mouse */
1521 sc->hw.hwid = get_aux_id(sc->kbdc);
1522 if (!is_a_mouse(sc->hw.hwid)) {
1523 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1524 restore_controller(sc->kbdc, command_byte);
1526 printf("psm%d: unknown device type (%d).\n", unit,
1530 switch (sc->hw.hwid) {
1531 case PSM_BALLPOINT_ID:
1532 sc->hw.type = MOUSE_TRACKBALL;
1535 case PSM_INTELLI_ID:
1536 case PSM_EXPLORER_ID:
1537 case PSM_4DMOUSE_ID:
1539 sc->hw.type = MOUSE_MOUSE;
1542 sc->hw.type = MOUSE_UNKNOWN;
1546 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1548 i = GENERIC_MOUSE_ENTRY;
1551 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1553 /* other parameters */
1554 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1555 if ((*vendortype[i].probefunc)(sc, PROBE)) {
1557 printf("psm%d: found %s\n", unit,
1558 model_name(vendortype[i].model));
1563 sc->hw.model = vendortype[i].model;
1565 sc->dflt_mode.level = PSM_LEVEL_BASE;
1566 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1567 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1568 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1569 sc->dflt_mode.syncmask[0] = 0;
1571 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1572 if (sc->config & PSM_CONFIG_FORCETAP)
1573 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1574 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1575 sc->mode = sc->dflt_mode;
1576 sc->mode.packetsize = vendortype[i].packetsize;
1578 /* set mouse parameters */
1581 * A version of Logitech FirstMouse+ won't report wheel movement,
1582 * if SET_DEFAULTS is sent... Don't use this command.
1583 * This fix was found by Takashi Nishida.
1585 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1587 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1589 if (sc->config & PSM_CONFIG_RESOLUTION)
1590 sc->mode.resolution =
1591 set_mouse_resolution(sc->kbdc,
1592 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1593 else if (sc->mode.resolution >= 0)
1594 sc->mode.resolution =
1595 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1596 if (sc->mode.rate > 0)
1598 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1599 set_mouse_scaling(sc->kbdc, 1);
1601 /* Record sync on the next data packet we see. */
1602 sc->flags |= PSM_NEED_SYNCBITS;
1604 /* just check the status of the mouse */
1606 * NOTE: XXX there are some arcane controller/mouse combinations out
1607 * there, which hung the controller unless there is data transmission
1608 * after ACK from the mouse.
1610 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1611 printf("psm%d: failed to get status.\n", unit);
1614 * When in its native mode, some mice operate with different
1615 * default parameters than in the PS/2 compatible mode.
1617 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1618 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1621 /* disable the aux port for now... */
1622 if (!set_controller_command_byte(sc->kbdc,
1623 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1624 (command_byte & KBD_KBD_CONTROL_BITS) |
1625 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1627 * this is CONTROLLER ERROR; I don't know the proper way to
1628 * recover from this error...
1630 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1631 restore_controller(sc->kbdc, command_byte);
1632 printf("psm%d: unable to set the command byte.\n", unit);
1637 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1638 kbdc_lock(sc->kbdc, FALSE);
1642 #ifdef EVDEV_SUPPORT
1643 /* Values are taken from Linux drivers for userland software compatibility */
1644 #define PS2_MOUSE_VENDOR 0x0002
1645 #define PS2_MOUSE_GENERIC_PRODUCT 0x0001
1646 #define PS2_MOUSE_SYNAPTICS_NAME "SynPS/2 Synaptics TouchPad"
1647 #define PS2_MOUSE_SYNAPTICS_PRODUCT 0x0007
1648 #define PS2_MOUSE_TRACKPOINT_NAME "TPPS/2 IBM TrackPoint"
1649 #define PS2_MOUSE_TRACKPOINT_PRODUCT 0x000A
1650 #define PS2_MOUSE_ELANTECH_NAME "ETPS/2 Elantech Touchpad"
1651 #define PS2_MOUSE_ELANTECH_ST_NAME "ETPS/2 Elantech TrackPoint"
1652 #define PS2_MOUSE_ELANTECH_PRODUCT 0x000E
1654 #define ABSINFO_END { ABS_CNT, 0, 0, 0 }
1657 psm_support_abs_bulk(struct evdev_dev *evdev, const uint16_t info[][4])
1661 for (i = 0; info[i][0] != ABS_CNT; i++)
1662 evdev_support_abs(evdev, info[i][0], 0, info[i][1], info[i][2],
1667 psm_push_mt_finger(struct psm_softc *sc, int id, const finger_t *f)
1669 int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1671 evdev_push_abs(sc->evdev_a, ABS_MT_SLOT, id);
1672 evdev_push_abs(sc->evdev_a, ABS_MT_TRACKING_ID, id);
1673 evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_X, f->x);
1674 evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_Y, y);
1675 evdev_push_abs(sc->evdev_a, ABS_MT_PRESSURE, f->p);
1679 psm_push_st_finger(struct psm_softc *sc, const finger_t *f)
1681 int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1683 evdev_push_abs(sc->evdev_a, ABS_X, f->x);
1684 evdev_push_abs(sc->evdev_a, ABS_Y, y);
1685 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, f->p);
1686 if (sc->synhw.capPalmDetect)
1687 evdev_push_abs(sc->evdev_a, ABS_TOOL_WIDTH, f->w);
1691 psm_release_mt_slot(struct evdev_dev *evdev, int32_t slot)
1694 evdev_push_abs(evdev, ABS_MT_SLOT, slot);
1695 evdev_push_abs(evdev, ABS_MT_TRACKING_ID, -1);
1699 psm_register(device_t dev, int model_code)
1701 struct psm_softc *sc = device_get_softc(dev);
1702 struct evdev_dev *evdev_r;
1703 int error, i, nbuttons, nwheels, product;
1704 bool is_pointing_stick;
1707 name = model_name(model_code);
1708 nbuttons = sc->hw.buttons;
1709 product = PS2_MOUSE_GENERIC_PRODUCT;
1711 is_pointing_stick = false;
1713 switch (model_code) {
1714 case MOUSE_MODEL_TRACKPOINT:
1715 name = PS2_MOUSE_TRACKPOINT_NAME;
1716 product = PS2_MOUSE_TRACKPOINT_PRODUCT;
1718 is_pointing_stick = true;
1721 case MOUSE_MODEL_ELANTECH:
1722 name = PS2_MOUSE_ELANTECH_ST_NAME;
1723 product = PS2_MOUSE_ELANTECH_PRODUCT;
1725 is_pointing_stick = true;
1728 case MOUSE_MODEL_MOUSEMANPLUS:
1729 case MOUSE_MODEL_4D:
1733 case MOUSE_MODEL_EXPLORER:
1734 case MOUSE_MODEL_INTELLI:
1735 case MOUSE_MODEL_NET:
1736 case MOUSE_MODEL_NETSCROLL:
1737 case MOUSE_MODEL_4DPLUS:
1742 evdev_r = evdev_alloc();
1743 evdev_set_name(evdev_r, name);
1744 evdev_set_phys(evdev_r, device_get_nameunit(dev));
1745 evdev_set_id(evdev_r, BUS_I8042, PS2_MOUSE_VENDOR, product, 0);
1746 evdev_set_methods(evdev_r, sc, &psm_ev_methods_r);
1748 evdev_support_prop(evdev_r, INPUT_PROP_POINTER);
1749 if (is_pointing_stick)
1750 evdev_support_prop(evdev_r, INPUT_PROP_POINTING_STICK);
1751 evdev_support_event(evdev_r, EV_SYN);
1752 evdev_support_event(evdev_r, EV_KEY);
1753 evdev_support_event(evdev_r, EV_REL);
1754 evdev_support_rel(evdev_r, REL_X);
1755 evdev_support_rel(evdev_r, REL_Y);
1758 evdev_support_rel(evdev_r, REL_HWHEEL);
1761 evdev_support_rel(evdev_r, REL_WHEEL);
1763 for (i = 0; i < nbuttons; i++)
1764 evdev_support_key(evdev_r, BTN_MOUSE + i);
1766 error = evdev_register_mtx(evdev_r, &Giant);
1768 evdev_free(evdev_r);
1770 sc->evdev_r = evdev_r;
1775 psm_register_synaptics(device_t dev)
1777 struct psm_softc *sc = device_get_softc(dev);
1778 const uint16_t synaptics_absinfo_st[][4] = {
1779 { ABS_X, sc->synhw.minimumXCoord,
1780 sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1781 { ABS_Y, sc->synhw.minimumYCoord,
1782 sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1783 { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1786 const uint16_t synaptics_absinfo_mt[][4] = {
1787 { ABS_MT_SLOT, 0, PSM_FINGERS-1, 0},
1788 { ABS_MT_TRACKING_ID, -1, PSM_FINGERS-1, 0},
1789 { ABS_MT_POSITION_X, sc->synhw.minimumXCoord,
1790 sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1791 { ABS_MT_POSITION_Y, sc->synhw.minimumYCoord,
1792 sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1793 { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1796 struct evdev_dev *evdev_a;
1797 int error, i, guest_model;
1799 evdev_a = evdev_alloc();
1800 evdev_set_name(evdev_a, PS2_MOUSE_SYNAPTICS_NAME);
1801 evdev_set_phys(evdev_a, device_get_nameunit(dev));
1802 evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1803 PS2_MOUSE_SYNAPTICS_PRODUCT, 0);
1804 evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1806 evdev_support_event(evdev_a, EV_SYN);
1807 evdev_support_event(evdev_a, EV_KEY);
1808 evdev_support_event(evdev_a, EV_ABS);
1809 evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1810 if (sc->synhw.capAdvancedGestures)
1811 evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1812 if (sc->synhw.capClickPad)
1813 evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1814 evdev_support_key(evdev_a, BTN_TOUCH);
1815 evdev_support_nfingers(evdev_a, 3);
1816 psm_support_abs_bulk(evdev_a, synaptics_absinfo_st);
1817 if (sc->synhw.capAdvancedGestures || sc->synhw.capReportsV)
1818 psm_support_abs_bulk(evdev_a, synaptics_absinfo_mt);
1819 if (sc->synhw.capPalmDetect)
1820 evdev_support_abs(evdev_a, ABS_TOOL_WIDTH, 0, 0, 15, 0, 0, 0);
1821 evdev_support_key(evdev_a, BTN_LEFT);
1822 if (!sc->synhw.capClickPad) {
1823 evdev_support_key(evdev_a, BTN_RIGHT);
1824 if (sc->synhw.capExtended && sc->synhw.capMiddle)
1825 evdev_support_key(evdev_a, BTN_MIDDLE);
1827 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
1828 evdev_support_key(evdev_a, BTN_BACK);
1829 evdev_support_key(evdev_a, BTN_FORWARD);
1831 if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0))
1832 for (i = 0; i < sc->synhw.nExtendedButtons; i++)
1833 evdev_support_key(evdev_a, BTN_0 + i);
1835 error = evdev_register_mtx(evdev_a, &Giant);
1836 if (!error && sc->synhw.capPassthrough) {
1837 guest_model = sc->tpinfo.sysctl_tree != NULL ?
1838 MOUSE_MODEL_TRACKPOINT : MOUSE_MODEL_GENERIC;
1839 error = psm_register(dev, guest_model);
1842 evdev_free(evdev_a);
1844 sc->evdev_a = evdev_a;
1849 psm_register_elantech(device_t dev)
1851 struct psm_softc *sc = device_get_softc(dev);
1852 const uint16_t elantech_absinfo[][4] = {
1853 { ABS_X, 0, sc->elanhw.sizex,
1855 { ABS_Y, 0, sc->elanhw.sizey,
1857 { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1858 { ABS_TOOL_WIDTH, 0, ELANTECH_FINGER_MAX_W, 0 },
1859 { ABS_MT_SLOT, 0, ELANTECH_MAX_FINGERS - 1, 0 },
1860 { ABS_MT_TRACKING_ID, -1, ELANTECH_MAX_FINGERS - 1, 0 },
1861 { ABS_MT_POSITION_X, 0, sc->elanhw.sizex,
1863 { ABS_MT_POSITION_Y, 0, sc->elanhw.sizey,
1865 { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1866 { ABS_MT_TOUCH_MAJOR, 0, ELANTECH_FINGER_MAX_W *
1867 sc->elanhw.dptracex, 0 },
1870 struct evdev_dev *evdev_a;
1873 evdev_a = evdev_alloc();
1874 evdev_set_name(evdev_a, PS2_MOUSE_ELANTECH_NAME);
1875 evdev_set_phys(evdev_a, device_get_nameunit(dev));
1876 evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1877 PS2_MOUSE_ELANTECH_PRODUCT, 0);
1878 evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1880 evdev_support_event(evdev_a, EV_SYN);
1881 evdev_support_event(evdev_a, EV_KEY);
1882 evdev_support_event(evdev_a, EV_ABS);
1883 evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1884 if (sc->elanhw.issemimt)
1885 evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1886 if (sc->elanhw.isclickpad)
1887 evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1888 evdev_support_key(evdev_a, BTN_TOUCH);
1889 evdev_support_nfingers(evdev_a, ELANTECH_MAX_FINGERS);
1890 evdev_support_key(evdev_a, BTN_LEFT);
1891 if (!sc->elanhw.isclickpad)
1892 evdev_support_key(evdev_a, BTN_RIGHT);
1893 psm_support_abs_bulk(evdev_a, elantech_absinfo);
1895 error = evdev_register_mtx(evdev_a, &Giant);
1896 if (!error && sc->elanhw.hastrackpoint)
1897 error = psm_register(dev, MOUSE_MODEL_ELANTECH);
1899 evdev_free(evdev_a);
1901 sc->evdev_a = evdev_a;
1907 psmattach(device_t dev)
1909 int unit = device_get_unit(dev);
1910 struct psm_softc *sc = device_get_softc(dev);
1914 /* Setup initial state */
1915 sc->state = PSM_VALID;
1916 callout_init(&sc->callout, 0);
1917 callout_init(&sc->softcallout, 0);
1919 /* Setup our interrupt handler */
1921 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1922 if (sc->intr == NULL)
1924 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1927 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1932 sc->dev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "psm%d", unit);
1933 sc->dev->si_drv1 = sc;
1934 sc->bdev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "bpsm%d", unit);
1935 sc->bdev->si_drv1 = sc;
1937 #ifdef EVDEV_SUPPORT
1938 switch (sc->hw.model) {
1939 case MOUSE_MODEL_SYNAPTICS:
1940 error = psm_register_synaptics(dev);
1943 case MOUSE_MODEL_ELANTECH:
1944 error = psm_register_elantech(dev);
1948 error = psm_register(dev, sc->hw.model);
1955 /* Some touchpad devices need full reinitialization after suspend. */
1956 switch (sc->hw.model) {
1957 case MOUSE_MODEL_SYNAPTICS:
1958 case MOUSE_MODEL_GLIDEPOINT:
1959 case MOUSE_MODEL_VERSAPAD:
1960 case MOUSE_MODEL_ELANTECH:
1961 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1964 if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
1965 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1969 /* Elantech trackpad`s sync bit differs from touchpad`s one */
1970 if (sc->hw.model == MOUSE_MODEL_ELANTECH &&
1971 (sc->elanhw.hascrc || sc->elanhw.hastrackpoint)) {
1972 sc->config |= PSM_CONFIG_NOCHECKSYNC;
1973 sc->flags &= ~PSM_NEED_SYNCBITS;
1977 printf("psm%d: model %s, device ID %d\n",
1978 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1980 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1981 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1982 sc->hw.hwid >> 8, sc->hw.buttons);
1983 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1984 unit, sc->config, sc->flags, sc->mode.packetsize);
1985 printf("psm%d: syncmask:%02x, syncbits:%02x%s\n",
1986 unit, sc->mode.syncmask[0], sc->mode.syncmask[1],
1987 sc->config & PSM_CONFIG_NOCHECKSYNC ? " (sync not checked)" : "");
1997 psmdetach(device_t dev)
1999 struct psm_softc *sc;
2002 sc = device_get_softc(dev);
2003 if (sc->state & PSM_OPEN)
2006 #ifdef EVDEV_SUPPORT
2007 evdev_free(sc->evdev_r);
2008 evdev_free(sc->evdev_a);
2012 bus_teardown_intr(dev, sc->intr, sc->ih);
2013 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
2015 destroy_dev(sc->dev);
2016 destroy_dev(sc->bdev);
2018 callout_drain(&sc->callout);
2019 callout_drain(&sc->softcallout);
2024 #ifdef EVDEV_SUPPORT
2026 psm_ev_open_r(struct evdev_dev *evdev, void *ev_softc)
2028 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2031 /* Get device data */
2032 if ((sc->state & PSM_VALID) == 0) {
2033 /* the device is no longer valid/functioning */
2037 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_A)))
2041 sc->state |= PSM_EV_OPEN_R;
2047 psm_ev_close_r(struct evdev_dev *evdev, void *ev_softc)
2049 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2051 sc->state &= ~PSM_EV_OPEN_R;
2053 if (sc->state & (PSM_OPEN | PSM_EV_OPEN_A))
2056 if (sc->state & PSM_VALID)
2061 psm_ev_open_a(struct evdev_dev *evdev, void *ev_softc)
2063 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2066 /* Get device data */
2067 if ((sc->state & PSM_VALID) == 0) {
2068 /* the device is no longer valid/functioning */
2072 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R)))
2076 sc->state |= PSM_EV_OPEN_A;
2082 psm_ev_close_a(struct evdev_dev *evdev, void *ev_softc)
2084 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2086 sc->state &= ~PSM_EV_OPEN_A;
2088 if (sc->state & (PSM_OPEN | PSM_EV_OPEN_R))
2091 if (sc->state & PSM_VALID)
2097 psm_cdev_open(struct cdev *dev, int flag, int fmt, struct thread *td)
2099 struct psm_softc *sc;
2102 /* Get device data */
2104 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2105 /* the device is no longer valid/functioning */
2109 /* Disallow multiple opens */
2110 if (sc->state & PSM_OPEN)
2113 device_busy(devclass_get_device(psm_devclass, sc->unit));
2115 #ifdef EVDEV_SUPPORT
2116 /* Already opened by evdev */
2117 if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2122 sc->state |= PSM_OPEN;
2124 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2130 psm_cdev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
2132 struct psm_softc *sc;
2135 /* Get device data */
2137 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2138 /* the device is no longer valid/functioning */
2142 #ifdef EVDEV_SUPPORT
2143 /* Still opened by evdev */
2144 if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2149 sc->state &= ~PSM_OPEN;
2150 /* clean up and sigio requests */
2151 if (sc->async != NULL) {
2152 funsetown(&sc->async);
2155 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2162 psmopen(struct psm_softc *sc)
2168 /* Initialize state */
2169 sc->mode.level = sc->dflt_mode.level;
2170 sc->mode.protocol = sc->dflt_mode.protocol;
2171 sc->watchdog = FALSE;
2174 /* flush the event queue */
2175 sc->queue.count = 0;
2178 sc->status.flags = 0;
2179 sc->status.button = 0;
2180 sc->status.obutton = 0;
2185 sc->pqueue_start = 0;
2188 /* empty input buffer */
2193 /* don't let timeout routines in the keyboard driver to poll the kbdc */
2194 if (!kbdc_lock(sc->kbdc, TRUE))
2197 /* save the current controller command byte */
2199 command_byte = get_controller_command_byte(sc->kbdc);
2201 /* enable the aux port and temporalily disable the keyboard */
2202 if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
2203 kbdc_get_device_mask(sc->kbdc),
2204 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2205 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2206 /* CONTROLLER ERROR; do you know how to get out of this? */
2207 kbdc_lock(sc->kbdc, FALSE);
2210 "psm%d: unable to set the command byte (psmopen).\n",
2215 * Now that the keyboard controller is told not to generate
2216 * the keyboard and mouse interrupts, call `splx()' to allow
2217 * the other tty interrupts. The clock interrupt may also occur,
2218 * but timeout routines will be blocked by the poll flag set
2223 /* enable the mouse device */
2224 err = doopen(sc, command_byte);
2227 kbdc_lock(sc->kbdc, FALSE);
2232 psmclose(struct psm_softc *sc)
2238 /* don't let timeout routines in the keyboard driver to poll the kbdc */
2239 if (!kbdc_lock(sc->kbdc, TRUE))
2242 /* save the current controller command byte */
2244 command_byte = get_controller_command_byte(sc->kbdc);
2245 if (command_byte == -1) {
2246 kbdc_lock(sc->kbdc, FALSE);
2251 /* disable the aux interrupt and temporalily disable the keyboard */
2252 if (!set_controller_command_byte(sc->kbdc,
2253 kbdc_get_device_mask(sc->kbdc),
2254 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2255 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2257 "psm%d: failed to disable the aux int (psmclose).\n",
2259 /* CONTROLLER ERROR;
2260 * NOTE: we shall force our way through. Because the only
2261 * ill effect we shall see is that we may not be able
2262 * to read ACK from the mouse, and it doesn't matter much
2263 * so long as the mouse will accept the DISABLE command.
2268 /* stop the watchdog timer */
2269 callout_stop(&sc->callout);
2271 /* remove anything left in the output buffer */
2272 empty_aux_buffer(sc->kbdc, 10);
2274 /* disable the aux device, port and interrupt */
2275 if (sc->state & PSM_VALID) {
2276 if (!disable_aux_dev(sc->kbdc)) {
2278 * NOTE: we don't return (error) and continue,
2279 * pretending we have successfully disabled the device.
2280 * It's OK because the interrupt routine will discard
2281 * any data from the mouse hereafter.
2284 "psm%d: failed to disable the device (psmclose).\n",
2288 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
2290 "psm%d: failed to get status (psmclose).\n",
2294 if (!set_controller_command_byte(sc->kbdc,
2295 kbdc_get_device_mask(sc->kbdc),
2296 (command_byte & KBD_KBD_CONTROL_BITS) |
2297 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2300 * we shall ignore this error; see the above comment.
2303 "psm%d: failed to disable the aux port (psmclose).\n",
2307 /* remove anything left in the output buffer */
2308 empty_aux_buffer(sc->kbdc, 10);
2310 /* close is almost always successful */
2311 kbdc_lock(sc->kbdc, FALSE);
2316 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
2319 static u_char butmapps2[8] = {
2321 MOUSE_PS2_BUTTON1DOWN,
2322 MOUSE_PS2_BUTTON2DOWN,
2323 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
2324 MOUSE_PS2_BUTTON3DOWN,
2325 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
2326 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
2327 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
2328 MOUSE_PS2_BUTTON3DOWN,
2330 static u_char butmapmsc[8] = {
2331 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
2332 MOUSE_MSC_BUTTON3UP,
2333 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
2334 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
2335 MOUSE_MSC_BUTTON3UP,
2336 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
2337 MOUSE_MSC_BUTTON2UP,
2338 MOUSE_MSC_BUTTON1UP,
2344 if (sc->mode.level == PSM_LEVEL_BASE) {
2345 mapped = status->button & ~MOUSE_BUTTON4DOWN;
2346 if (status->button & MOUSE_BUTTON4DOWN)
2347 mapped |= MOUSE_BUTTON1DOWN;
2348 status->button = mapped;
2349 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
2350 i = imax(imin(status->dx, 255), -256);
2352 buf[0] |= MOUSE_PS2_XNEG;
2354 i = imax(imin(status->dy, 255), -256);
2356 buf[0] |= MOUSE_PS2_YNEG;
2358 return (MOUSE_PS2_PACKETSIZE);
2359 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
2360 buf[0] = MOUSE_MSC_SYNC |
2361 butmapmsc[status->button & MOUSE_STDBUTTONS];
2362 i = imax(imin(status->dx, 255), -256);
2364 buf[3] = i - buf[1];
2365 i = imax(imin(status->dy, 255), -256);
2367 buf[4] = i - buf[2];
2368 i = imax(imin(status->dz, 127), -128);
2369 buf[5] = (i >> 1) & 0x7f;
2370 buf[6] = (i - (i >> 1)) & 0x7f;
2371 buf[7] = (~status->button >> 3) & 0x7f;
2372 return (MOUSE_SYS_PACKETSIZE);
2374 return (pb->inputbytes);
2378 psmread(struct cdev *dev, struct uio *uio, int flag)
2380 struct psm_softc *sc = dev->si_drv1;
2381 u_char buf[PSM_SMALLBUFSIZE];
2386 if ((sc->state & PSM_VALID) == 0)
2389 /* block until mouse activity occurred */
2391 while (sc->queue.count <= 0) {
2392 if (dev != sc->bdev) {
2394 return (EWOULDBLOCK);
2396 sc->state |= PSM_ASLP;
2397 error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
2398 sc->state &= ~PSM_ASLP;
2402 } else if ((sc->state & PSM_VALID) == 0) {
2403 /* the device disappeared! */
2410 /* copy data to the user land */
2411 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
2413 l = imin(sc->queue.count, uio->uio_resid);
2414 if (l > sizeof(buf))
2416 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
2417 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
2418 sizeof(sc->queue.buf) - sc->queue.head);
2419 bcopy(&sc->queue.buf[0],
2420 &buf[sizeof(sc->queue.buf) - sc->queue.head],
2421 l - (sizeof(sc->queue.buf) - sc->queue.head));
2423 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
2424 sc->queue.count -= l;
2425 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
2427 error = uiomove(buf, l, uio);
2436 block_mouse_data(struct psm_softc *sc, int *c)
2440 if (!kbdc_lock(sc->kbdc, TRUE))
2444 *c = get_controller_command_byte(sc->kbdc);
2445 if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
2446 kbdc_get_device_mask(sc->kbdc),
2447 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2448 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2449 /* this is CONTROLLER ERROR */
2451 kbdc_lock(sc->kbdc, FALSE);
2456 * The device may be in the middle of status data transmission.
2457 * The transmission will be interrupted, thus, incomplete status
2458 * data must be discarded. Although the aux interrupt is disabled
2459 * at the keyboard controller level, at most one aux interrupt
2460 * may have already been pending and a data byte is in the
2461 * output buffer; throw it away. Note that the second argument
2462 * to `empty_aux_buffer()' is zero, so that the call will just
2463 * flush the internal queue.
2464 * `psmintr()' will be invoked after `splx()' if an interrupt is
2465 * pending; it will see no data and returns immediately.
2467 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
2468 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
2476 dropqueue(struct psm_softc *sc)
2479 sc->queue.count = 0;
2482 if ((sc->state & PSM_SOFTARMED) != 0) {
2483 sc->state &= ~PSM_SOFTARMED;
2484 callout_stop(&sc->softcallout);
2486 sc->pqueue_start = sc->pqueue_end;
2490 flushpackets(struct psm_softc *sc)
2494 bzero(&sc->pqueue, sizeof(sc->pqueue));
2498 unblock_mouse_data(struct psm_softc *sc, int c)
2503 * We may have seen a part of status data during `set_mouse_XXX()'.
2504 * they have been queued; flush it.
2506 empty_aux_buffer(sc->kbdc, 0);
2508 /* restore ports and interrupt */
2509 if (!set_controller_command_byte(sc->kbdc,
2510 kbdc_get_device_mask(sc->kbdc),
2511 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
2513 * CONTROLLER ERROR; this is serious, we may have
2514 * been left with the inaccessible keyboard and
2515 * the disabled mouse interrupt.
2520 kbdc_lock(sc->kbdc, FALSE);
2525 psmwrite(struct cdev *dev, struct uio *uio, int flag)
2527 struct psm_softc *sc = dev->si_drv1;
2528 u_char buf[PSM_SMALLBUFSIZE];
2529 int error = 0, i, l;
2531 if ((sc->state & PSM_VALID) == 0)
2534 if (sc->mode.level < PSM_LEVEL_NATIVE)
2537 /* copy data from the user land */
2538 while (uio->uio_resid > 0) {
2539 l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
2540 error = uiomove(buf, l, uio);
2543 for (i = 0; i < l; i++) {
2544 VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
2545 if (!write_aux_command(sc->kbdc, buf[i])) {
2547 "psm: cmd 0x%x failed.\n", buf[i]));
2548 return (reinitialize(sc, FALSE));
2557 psmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2560 struct psm_softc *sc = dev->si_drv1;
2562 mousestatus_t status;
2563 #if (defined(MOUSE_GETVARS))
2572 /* Perform IOCTL command */
2575 case OLD_MOUSE_GETHWINFO:
2577 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
2578 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
2579 ((old_mousehw_t *)addr)->type = sc->hw.type;
2580 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
2584 case MOUSE_GETHWINFO:
2586 *(mousehw_t *)addr = sc->hw;
2587 if (sc->mode.level == PSM_LEVEL_BASE)
2588 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
2592 case MOUSE_SYN_GETHWINFO:
2594 if (sc->synhw.infoMajor >= 4)
2595 *(synapticshw_t *)addr = sc->synhw;
2601 case OLD_MOUSE_GETMODE:
2603 switch (sc->mode.level) {
2604 case PSM_LEVEL_BASE:
2605 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2607 case PSM_LEVEL_STANDARD:
2608 ((old_mousemode_t *)addr)->protocol =
2609 MOUSE_PROTO_SYSMOUSE;
2611 case PSM_LEVEL_NATIVE:
2612 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2615 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
2616 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2617 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2623 *(mousemode_t *)addr = sc->mode;
2624 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2625 ((mousemode_t *)addr)->syncmask[0] = 0;
2626 ((mousemode_t *)addr)->syncmask[1] = 0;
2628 ((mousemode_t *)addr)->resolution =
2629 MOUSE_RES_LOW - sc->mode.resolution;
2630 switch (sc->mode.level) {
2631 case PSM_LEVEL_BASE:
2632 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2633 ((mousemode_t *)addr)->packetsize =
2634 MOUSE_PS2_PACKETSIZE;
2636 case PSM_LEVEL_STANDARD:
2637 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2638 ((mousemode_t *)addr)->packetsize =
2639 MOUSE_SYS_PACKETSIZE;
2640 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2641 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2643 case PSM_LEVEL_NATIVE:
2644 /* FIXME: this isn't quite correct... XXX */
2645 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2651 case OLD_MOUSE_SETMODE:
2653 if (cmd == OLD_MOUSE_SETMODE) {
2654 mode.rate = ((old_mousemode_t *)addr)->rate;
2656 * resolution old I/F new I/F
2663 if (((old_mousemode_t *)addr)->resolution > 0)
2665 -((old_mousemode_t *)addr)->resolution - 1;
2667 mode.resolution = 0;
2669 ((old_mousemode_t *)addr)->accelfactor;
2672 mode = *(mousemode_t *)addr;
2674 /* adjust and validate parameters. */
2675 if (mode.rate > UCHAR_MAX)
2678 mode.rate = sc->dflt_mode.rate;
2679 else if (mode.rate == -1)
2680 /* don't change the current setting */
2682 else if (mode.rate < 0)
2684 if (mode.resolution >= UCHAR_MAX)
2686 if (mode.resolution >= 200)
2687 mode.resolution = MOUSE_RES_HIGH;
2688 else if (mode.resolution >= 100)
2689 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2690 else if (mode.resolution >= 50)
2691 mode.resolution = MOUSE_RES_MEDIUMLOW;
2692 else if (mode.resolution > 0)
2693 mode.resolution = MOUSE_RES_LOW;
2694 if (mode.resolution == MOUSE_RES_DEFAULT)
2695 mode.resolution = sc->dflt_mode.resolution;
2696 else if (mode.resolution == -1)
2697 /* don't change the current setting */
2699 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2700 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2701 if (mode.level == -1)
2702 /* don't change the current setting */
2703 mode.level = sc->mode.level;
2704 else if ((mode.level < PSM_LEVEL_MIN) ||
2705 (mode.level > PSM_LEVEL_MAX))
2707 if (mode.accelfactor == -1)
2708 /* don't change the current setting */
2709 mode.accelfactor = sc->mode.accelfactor;
2710 else if (mode.accelfactor < 0)
2713 /* don't allow anybody to poll the keyboard controller */
2714 error = block_mouse_data(sc, &command_byte);
2718 /* set mouse parameters */
2720 mode.rate = set_mouse_sampling_rate(sc->kbdc,
2722 if (mode.resolution >= 0)
2724 set_mouse_resolution(sc->kbdc, mode.resolution);
2725 set_mouse_scaling(sc->kbdc, 1);
2726 get_mouse_status(sc->kbdc, stat, 0, 3);
2729 sc->mode.rate = mode.rate;
2730 sc->mode.resolution = mode.resolution;
2731 sc->mode.accelfactor = mode.accelfactor;
2732 sc->mode.level = mode.level;
2735 unblock_mouse_data(sc, command_byte);
2738 case MOUSE_GETLEVEL:
2739 *(int *)addr = sc->mode.level;
2742 case MOUSE_SETLEVEL:
2743 if ((*(int *)addr < PSM_LEVEL_MIN) ||
2744 (*(int *)addr > PSM_LEVEL_MAX))
2746 sc->mode.level = *(int *)addr;
2749 case MOUSE_GETSTATUS:
2751 status = sc->status;
2752 sc->status.flags = 0;
2753 sc->status.obutton = sc->status.button;
2754 sc->status.button = 0;
2759 *(mousestatus_t *)addr = status;
2762 #if (defined(MOUSE_GETVARS))
2764 var = (mousevar_t *)addr;
2765 bzero(var, sizeof(*var));
2767 var->var[0] = MOUSE_VARS_PS2_SIG;
2768 var->var[1] = sc->config;
2769 var->var[2] = sc->flags;
2775 #endif /* MOUSE_GETVARS */
2777 case MOUSE_READSTATE:
2778 case MOUSE_READDATA:
2779 data = (mousedata_t *)addr;
2780 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2783 error = block_mouse_data(sc, &command_byte);
2786 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2787 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2789 unblock_mouse_data(sc, command_byte);
2792 #if (defined(MOUSE_SETRESOLUTION))
2793 case MOUSE_SETRESOLUTION:
2794 mode.resolution = *(int *)addr;
2795 if (mode.resolution >= UCHAR_MAX)
2797 else if (mode.resolution >= 200)
2798 mode.resolution = MOUSE_RES_HIGH;
2799 else if (mode.resolution >= 100)
2800 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2801 else if (mode.resolution >= 50)
2802 mode.resolution = MOUSE_RES_MEDIUMLOW;
2803 else if (mode.resolution > 0)
2804 mode.resolution = MOUSE_RES_LOW;
2805 if (mode.resolution == MOUSE_RES_DEFAULT)
2806 mode.resolution = sc->dflt_mode.resolution;
2807 else if (mode.resolution == -1)
2808 mode.resolution = sc->mode.resolution;
2809 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2810 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2812 error = block_mouse_data(sc, &command_byte);
2815 sc->mode.resolution =
2816 set_mouse_resolution(sc->kbdc, mode.resolution);
2817 if (sc->mode.resolution != mode.resolution)
2819 unblock_mouse_data(sc, command_byte);
2821 #endif /* MOUSE_SETRESOLUTION */
2823 #if (defined(MOUSE_SETRATE))
2825 mode.rate = *(int *)addr;
2826 if (mode.rate > UCHAR_MAX)
2829 mode.rate = sc->dflt_mode.rate;
2830 else if (mode.rate < 0)
2831 mode.rate = sc->mode.rate;
2833 error = block_mouse_data(sc, &command_byte);
2836 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2837 if (sc->mode.rate != mode.rate)
2839 unblock_mouse_data(sc, command_byte);
2841 #endif /* MOUSE_SETRATE */
2843 #if (defined(MOUSE_SETSCALING))
2844 case MOUSE_SETSCALING:
2845 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2848 error = block_mouse_data(sc, &command_byte);
2851 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2853 unblock_mouse_data(sc, command_byte);
2855 #endif /* MOUSE_SETSCALING */
2857 #if (defined(MOUSE_GETHWID))
2859 error = block_mouse_data(sc, &command_byte);
2862 sc->hw.hwid &= ~0x00ff;
2863 sc->hw.hwid |= get_aux_id(sc->kbdc);
2864 *(int *)addr = sc->hw.hwid & 0x00ff;
2865 unblock_mouse_data(sc, command_byte);
2867 #endif /* MOUSE_GETHWID */
2873 error = fsetown(*(int *)addr, &sc->async);
2876 *(int *) addr = fgetown(&sc->async);
2886 psmtimeout(void *arg)
2888 struct psm_softc *sc;
2891 sc = (struct psm_softc *)arg;
2893 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2894 VLOG(6, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2896 kbdc_lock(sc->kbdc, FALSE);
2898 sc->watchdog = TRUE;
2900 callout_reset(&sc->callout, hz, psmtimeout, sc);
2903 /* Add all sysctls under the debug.psm and hw.psm nodes */
2904 static SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2905 static SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2907 SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RWTUN, &verbose, 0,
2910 static int psmhz = 20;
2911 SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2912 "Frequency of the softcallout (in hz)");
2913 static int psmerrsecs = 2;
2914 SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2915 "Number of seconds during which packets will dropped after a sync error");
2916 static int psmerrusecs = 0;
2917 SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2918 "Microseconds to add to psmerrsecs");
2919 static int psmsecs = 0;
2920 SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2921 "Max number of seconds between soft interrupts");
2922 static int psmusecs = 500000;
2923 SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2924 "Microseconds to add to psmsecs");
2925 static int pkterrthresh = 2;
2926 SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2927 "Number of error packets allowed before reinitializing the mouse");
2929 SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RWTUN, &tap_enabled, 0,
2930 "Enable tap and drag gestures");
2931 static int tap_threshold = PSM_TAP_THRESHOLD;
2932 SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2933 "Button tap threshold");
2934 static int tap_timeout = PSM_TAP_TIMEOUT;
2935 SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2936 "Tap timeout for touchpads");
2939 SYSCTL_INT(_hw_psm, OID_AUTO, synaptics_support, CTLFLAG_RDTUN,
2940 &synaptics_support, 0, "Enable support for Synaptics touchpads");
2942 SYSCTL_INT(_hw_psm, OID_AUTO, trackpoint_support, CTLFLAG_RDTUN,
2943 &trackpoint_support, 0, "Enable support for IBM/Lenovo TrackPoint");
2945 SYSCTL_INT(_hw_psm, OID_AUTO, elantech_support, CTLFLAG_RDTUN,
2946 &elantech_support, 0, "Enable support for Elantech touchpads");
2951 struct psm_softc *sc = arg;
2957 /* read until there is nothing to read */
2958 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2959 pb = &sc->pqueue[sc->pqueue_end];
2961 /* discard the byte if the device is not open */
2962 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2965 getmicrouptime(&now);
2966 if ((pb->inputbytes > 0) &&
2967 timevalcmp(&now, &sc->inputtimeout, >)) {
2968 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2969 "resetting byte count\n"));
2974 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2975 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2976 timevaladd(&sc->inputtimeout, &now);
2978 pb->ipacket[pb->inputbytes++] = c;
2980 if (sc->mode.level == PSM_LEVEL_NATIVE) {
2981 VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2986 if (pb->inputbytes < sc->mode.packetsize)
2990 "psmintr: %02x %02x %02x %02x %02x %02x\n",
2991 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2992 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2997 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2998 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2999 sc->flags &= ~PSM_NEED_SYNCBITS;
3001 "psmintr: Sync bytes now %04x,%04x\n",
3002 sc->mode.syncmask[0], sc->mode.syncmask[1]));
3003 } else if ((sc->config & PSM_CONFIG_NOCHECKSYNC) == 0 &&
3004 (c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
3005 VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
3006 "(%04x != %04x) %d cmds since last error.\n",
3007 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
3008 sc->cmdcount - sc->lasterr));
3009 sc->lasterr = sc->cmdcount;
3011 * The sync byte test is a weak measure of packet
3012 * validity. Conservatively discard any input yet
3013 * to be seen by userland when we detect a sync
3014 * error since there is a good chance some of
3015 * the queued packets have undetected errors.
3018 if (sc->syncerrors == 0)
3021 sc->lastinputerr = now;
3022 if (sc->syncerrors >= sc->mode.packetsize * 2 ||
3023 sc->pkterrors >= pkterrthresh) {
3025 * If we've failed to find a single sync byte
3026 * in 2 packets worth of data, or we've seen
3027 * persistent packet errors during the
3028 * validation period, reinitialize the mouse
3029 * in hopes of returning it to the expected
3033 "psmintr: reset the mouse.\n"));
3034 reinitialize(sc, TRUE);
3035 } else if (sc->syncerrors == sc->mode.packetsize) {
3037 * Try a soft reset after searching for a sync
3038 * byte through a packet length of bytes.
3041 "psmintr: re-enable the mouse.\n"));
3043 disable_aux_dev(sc->kbdc);
3044 enable_aux_dev(sc->kbdc);
3047 "psmintr: discard a byte (%d)\n",
3050 bcopy(&pb->ipacket[1], &pb->ipacket[0],
3057 * We have what appears to be a valid packet.
3058 * Reset the error counters.
3063 * Drop even good packets if they occur within a timeout
3064 * period of a sync error. This allows the detection of
3065 * a change in the mouse's packet mode without exposing
3066 * erratic mouse behavior to the user. Some KVMs forget
3067 * enhanced mouse modes during switch events.
3069 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
3076 * Now that we're out of the validation period, reset
3077 * the packet error count.
3083 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
3086 * If we've filled the queue then call the softintr ourselves,
3087 * otherwise schedule the interrupt for later.
3089 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
3090 (sc->pqueue_end == sc->pqueue_start)) {
3091 if ((sc->state & PSM_SOFTARMED) != 0) {
3092 sc->state &= ~PSM_SOFTARMED;
3093 callout_stop(&sc->softcallout);
3096 } else if ((sc->state & PSM_SOFTARMED) == 0) {
3097 sc->state |= PSM_SOFTARMED;
3098 callout_reset(&sc->softcallout,
3099 psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg);
3105 proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3106 int *x, int *y, int *z)
3110 * PS2++ protocol packet
3112 * b7 b6 b5 b4 b3 b2 b1 b0
3113 * byte 1: * 1 p3 p2 1 * * *
3114 * byte 2: c1 c2 p1 p0 d1 d0 1 0
3116 * p3-p0: packet type
3117 * c1, c2: c1 & c2 == 1, if p2 == 0
3118 * c1 & c2 == 0, if p2 == 1
3120 * packet type: 0 (device type)
3121 * See comments in enable_mmanplus() below.
3123 * packet type: 1 (wheel data)
3125 * b7 b6 b5 b4 b3 b2 b1 b0
3126 * byte 3: h * B5 B4 s d2 d1 d0
3128 * h: 1, if horizontal roller data
3129 * 0, if vertical roller data
3130 * B4, B5: button 4 and 5
3132 * d2-d0: roller data
3134 * packet type: 2 (reserved)
3136 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
3137 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
3139 * the extended data packet encodes button
3142 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
3144 /* wheel data packet */
3146 if (pb->ipacket[2] & 0x80) {
3147 /* XXX horizontal roller count - ignore it */
3150 /* vertical roller count */
3151 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
3152 (pb->ipacket[2] & 0x0f) - 16 :
3153 (pb->ipacket[2] & 0x0f);
3155 ms->button |= (pb->ipacket[2] &
3156 MOUSE_PS2PLUS_BUTTON4DOWN) ?
3157 MOUSE_BUTTON4DOWN : 0;
3158 ms->button |= (pb->ipacket[2] &
3159 MOUSE_PS2PLUS_BUTTON5DOWN) ?
3160 MOUSE_BUTTON5DOWN : 0;
3164 * this packet type is reserved by
3168 * IBM ScrollPoint Mouse uses this
3169 * packet type to encode both vertical
3170 * and horizontal scroll movement.
3173 /* horizontal count */
3174 if (pb->ipacket[2] & 0x0f)
3175 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
3177 /* vertical count */
3178 if (pb->ipacket[2] & 0xf0)
3179 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
3183 /* device type packet - shouldn't happen */
3187 ms->button = ms->obutton;
3188 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
3189 "type %d: 0x%02x 0x%02x 0x%02x\n",
3190 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
3191 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
3195 /* preserve button states */
3196 ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
3201 proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3202 int *x, int *y, int *z)
3204 static int touchpad_buttons;
3205 static int guest_buttons;
3206 static finger_t f[PSM_FINGERS];
3207 int w, id, nfingers, ewcode, extended_buttons, clickpad_pressed;
3209 extended_buttons = 0;
3211 /* TouchPad PS/2 absolute mode message format with capFourButtons:
3213 * Bits: 7 6 5 4 3 2 1 0 (LSB)
3214 * ------------------------------------------------
3215 * ipacket[0]: 1 0 W3 W2 0 W1 R L
3216 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
3217 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
3218 * ipacket[3]: 1 1 Yc Xc 0 W0 D^R U^L
3219 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
3220 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3223 * L: left physical mouse button
3224 * R: right physical mouse button
3232 * Without capFourButtons but with nExtendeButtons and/or capMiddle
3234 * Bits: 7 6 5 4 3 2 1 0 (LSB)
3235 * ------------------------------------------------------
3236 * ipacket[3]: 1 1 Yc Xc 0 W0 E^R M^L
3237 * ipacket[4]: X7 X6 X5 X4 X3|b7 X2|b5 X1|b3 X0|b1
3238 * ipacket[5]: Y7 Y6 Y5 Y4 Y3|b8 Y2|b6 Y1|b4 Y0|b2
3241 * M: Middle physical mouse button
3242 * E: Extended mouse buttons reported instead of low bits of X and Y
3243 * b1-b8: Extended mouse buttons
3244 * Only ((nExtendedButtons + 1) >> 1) bits are used in packet
3245 * 4 and 5, for reading X and Y value they should be zeroed.
3247 * Absolute reportable limits: 0 - 6143.
3248 * Typical bezel limits: 1472 - 5472.
3249 * Typical edge marings: 1632 - 5312.
3251 * w = 3 Passthrough Packet
3253 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
3256 if (!synaptics_support)
3259 /* Sanity check for out of sync packets. */
3260 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
3261 (pb->ipacket[3] & 0xc8) != 0xc0)
3265 ms->button = ms->obutton;
3270 * z = 0 No finger contact
3271 * z = 10 Finger hovering near the pad
3272 * z = 30 Very light finger contact
3273 * z = 80 Normal finger contact
3274 * z = 110 Very heavy finger contact
3275 * z = 200 Finger lying flat on pad surface
3276 * z = 255 Maximum reportable Z
3278 *z = pb->ipacket[2];
3281 * Finger width value
3283 * w = 0 Two finger on the pad (capMultiFinger needed)
3284 * w = 1 Three or more fingers (capMultiFinger needed)
3285 * w = 2 Pen (instead of finger) (capPen needed)
3286 * w = 3 Reserved (passthrough?)
3287 * w = 4-7 Finger of normal width (capPalmDetect needed)
3288 * w = 8-14 Very wide finger or palm (capPalmDetect needed)
3289 * w = 15 Maximum reportable width (capPalmDetect needed)
3291 /* XXX Is checking capExtended enough? */
3292 if (sc->synhw.capExtended)
3293 w = ((pb->ipacket[0] & 0x30) >> 2) |
3294 ((pb->ipacket[0] & 0x04) >> 1) |
3295 ((pb->ipacket[3] & 0x04) >> 2);
3297 /* Assume a finger of regular width. */
3304 * Handle packets from the guest device. See:
3305 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
3307 if (sc->synhw.capPassthrough) {
3308 *x = ((pb->ipacket[1] & 0x10) ?
3309 pb->ipacket[4] - 256 : pb->ipacket[4]);
3310 *y = ((pb->ipacket[1] & 0x20) ?
3311 pb->ipacket[5] - 256 : pb->ipacket[5]);
3315 if (pb->ipacket[1] & 0x01)
3316 guest_buttons |= MOUSE_BUTTON1DOWN;
3317 if (pb->ipacket[1] & 0x04)
3318 guest_buttons |= MOUSE_BUTTON2DOWN;
3319 if (pb->ipacket[1] & 0x02)
3320 guest_buttons |= MOUSE_BUTTON3DOWN;
3321 #ifdef EVDEV_SUPPORT
3322 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3323 evdev_push_rel(sc->evdev_r, REL_X, *x);
3324 evdev_push_rel(sc->evdev_r, REL_Y, -*y);
3325 evdev_push_mouse_btn(sc->evdev_r,
3327 evdev_sync(sc->evdev_r);
3330 ms->button = touchpad_buttons | guest_buttons |
3331 sc->extended_buttons;
3336 /* Handle Extended W mode packets */
3337 ewcode = (pb->ipacket[5] & 0xf0) >> 4;
3341 /* Secondary finger */
3342 if (sc->synhw.capAdvancedGestures)
3344 .x = (((pb->ipacket[4] & 0x0f) << 8) |
3345 pb->ipacket[1]) << 1,
3346 .y = (((pb->ipacket[4] & 0xf0) << 4) |
3347 pb->ipacket[2]) << 1,
3348 .p = ((pb->ipacket[3] & 0x30) |
3349 (pb->ipacket[5] & 0x0f)) << 1,
3350 .w = PSM_FINGER_DEFAULT_W,
3351 .flags = PSM_FINGER_FUZZY,
3353 else if (sc->synhw.capReportsV)
3355 .x = (((pb->ipacket[4] & 0x0f) << 8) |
3356 (pb->ipacket[1] & 0xfe)) << 1,
3357 .y = (((pb->ipacket[4] & 0xf0) << 4) |
3358 (pb->ipacket[2] & 0xfe)) << 1,
3359 .p = ((pb->ipacket[3] & 0x30) |
3360 (pb->ipacket[5] & 0x0e)) << 1,
3361 .w = (((pb->ipacket[5] & 0x01) << 2) |
3362 ((pb->ipacket[2] & 0x01) << 1) |
3363 (pb->ipacket[1] & 0x01)) + 8,
3364 .flags = PSM_FINGER_FUZZY,
3381 if (sc->syninfo.touchpad_off)
3384 /* Button presses */
3385 touchpad_buttons = 0;
3386 if (pb->ipacket[0] & 0x01)
3387 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3388 if (pb->ipacket[0] & 0x02)
3389 touchpad_buttons |= MOUSE_BUTTON3DOWN;
3391 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3392 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
3393 touchpad_buttons |= MOUSE_BUTTON4DOWN;
3394 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
3395 touchpad_buttons |= MOUSE_BUTTON5DOWN;
3396 } else if (sc->synhw.capExtended && sc->synhw.capMiddle &&
3397 !sc->synhw.capClickPad) {
3399 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
3400 touchpad_buttons |= MOUSE_BUTTON2DOWN;
3401 } else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
3402 /* Extended Buttons */
3403 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
3404 if (sc->syninfo.directional_scrolls) {
3405 if (pb->ipacket[4] & 0x01)
3406 extended_buttons |= MOUSE_BUTTON4DOWN;
3407 if (pb->ipacket[5] & 0x01)
3408 extended_buttons |= MOUSE_BUTTON5DOWN;
3409 if (pb->ipacket[4] & 0x02)
3410 extended_buttons |= MOUSE_BUTTON6DOWN;
3411 if (pb->ipacket[5] & 0x02)
3412 extended_buttons |= MOUSE_BUTTON7DOWN;
3414 if (pb->ipacket[4] & 0x01)
3415 extended_buttons |= MOUSE_BUTTON1DOWN;
3416 if (pb->ipacket[5] & 0x01)
3417 extended_buttons |= MOUSE_BUTTON3DOWN;
3418 if (pb->ipacket[4] & 0x02)
3419 extended_buttons |= MOUSE_BUTTON2DOWN;
3420 sc->extended_buttons = extended_buttons;
3424 * Zero out bits used by extended buttons to avoid
3425 * misinterpretation of the data absolute position.
3427 * The bits represented by
3429 * (nExtendedButtons + 1) >> 1
3431 * will be masked out in both bytes.
3432 * The mask for n bits is computed with the formula
3438 maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
3439 mask = (1 << maskedbits) - 1;
3440 #ifdef EVDEV_SUPPORT
3442 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3443 if (sc->synhw.capPassthrough) {
3444 evdev_push_mouse_btn(sc->evdev_r,
3446 evdev_sync(sc->evdev_r);
3448 for (i = 0; i < maskedbits; i++) {
3449 evdev_push_key(sc->evdev_a,
3451 pb->ipacket[4] & (1 << i));
3452 evdev_push_key(sc->evdev_a,
3454 pb->ipacket[5] & (1 << i));
3458 pb->ipacket[4] &= ~(mask);
3459 pb->ipacket[5] &= ~(mask);
3460 } else if (!sc->syninfo.directional_scrolls &&
3461 !sc->gesture.in_vscroll) {
3463 * Keep reporting MOUSE DOWN until we get a new packet
3464 * indicating otherwise.
3466 extended_buttons |= sc->extended_buttons;
3470 if (sc->synhw.capReportsV && nfingers > 1)
3472 .x = ((pb->ipacket[3] & 0x10) << 8) |
3473 ((pb->ipacket[1] & 0x0f) << 8) |
3474 (pb->ipacket[4] & 0xfd),
3475 .y = ((pb->ipacket[3] & 0x20) << 7) |
3476 ((pb->ipacket[1] & 0xf0) << 4) |
3477 (pb->ipacket[5] & 0xfd),
3479 .w = (((pb->ipacket[2] & 0x01) << 2) |
3480 (pb->ipacket[5] & 0x02) |
3481 ((pb->ipacket[4] & 0x02) >> 1)) + 8,
3482 .flags = PSM_FINGER_FUZZY,
3486 .x = ((pb->ipacket[3] & 0x10) << 8) |
3487 ((pb->ipacket[1] & 0x0f) << 8) |
3489 .y = ((pb->ipacket[3] & 0x20) << 7) |
3490 ((pb->ipacket[1] & 0xf0) << 4) |
3494 .flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0,
3497 /* Ignore hovering and unmeasurable touches */
3498 if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2)
3501 /* Handle ClickPad */
3502 if (sc->synhw.capClickPad) {
3503 clickpad_pressed = (pb->ipacket[0] ^ pb->ipacket[3]) & 0x01;
3504 if (sc->synhw.forcePad) {
3506 * Forcepads erroneously report button click if there
3507 * are 2 or more fingers on the touchpad breaking
3508 * multifinger gestures. To workaround this start
3509 * reporting a click only after 4 consecutive single
3510 * touch packets has been received.
3511 * Skip these packets in case more contacts appear.
3518 if (clickpad_pressed && sc->fpcount < INT_MAX)
3522 if (!clickpad_pressed)
3524 if (sc->fpcount >= sc->syninfo.window_min)
3525 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3527 } else if (clickpad_pressed)
3528 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3531 for (id = 0; id < PSM_FINGERS; id++)
3533 PSM_FINGER_RESET(f[id]);
3535 #ifdef EVDEV_SUPPORT
3536 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3537 for (id = 0; id < PSM_FINGERS; id++) {
3538 if (PSM_FINGER_IS_SET(f[id]))
3539 psm_push_mt_finger(sc, id, &f[id]);
3541 psm_release_mt_slot(sc->evdev_a, id);
3543 evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
3544 evdev_push_nfingers(sc->evdev_a, nfingers);
3546 psm_push_st_finger(sc, &f[0]);
3548 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
3549 evdev_push_mouse_btn(sc->evdev_a, touchpad_buttons);
3550 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3551 evdev_push_key(sc->evdev_a, BTN_FORWARD,
3552 touchpad_buttons & MOUSE_BUTTON4DOWN);
3553 evdev_push_key(sc->evdev_a, BTN_BACK,
3554 touchpad_buttons & MOUSE_BUTTON5DOWN);
3556 evdev_sync(sc->evdev_a);
3560 ms->button = touchpad_buttons;
3562 psmgestures(sc, &f[0], nfingers, ms);
3563 for (id = 0; id < PSM_FINGERS; id++)
3564 psmsmoother(sc, &f[id], id, ms, x, y);
3566 /* Palm detection doesn't terminate the current action. */
3567 if (psmpalmdetect(sc, &f[0], nfingers)) {
3569 ms->button = ms->obutton;
3573 ms->button |= extended_buttons | guest_buttons;
3577 * Use the extra buttons as a scrollwheel
3579 * XXX X.Org uses the Z axis for vertical wheel only,
3580 * whereas moused(8) understands special values to differ
3581 * vertical and horizontal wheels.
3583 * xf86-input-mouse needs therefore a small patch to
3584 * understand these special values. Without it, the
3585 * horizontal wheel acts as a vertical wheel in X.Org.
3587 * That's why the horizontal wheel is disabled by
3590 if (ms->button & MOUSE_BUTTON4DOWN)
3592 else if (ms->button & MOUSE_BUTTON5DOWN)
3594 else if (ms->button & MOUSE_BUTTON6DOWN)
3596 else if (ms->button & MOUSE_BUTTON7DOWN)
3600 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
3601 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
3607 psmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers)
3610 ((sc->synhw.capMultiFinger || sc->synhw.capAdvancedGestures) &&
3611 !sc->synhw.capReportsV && nfingers > 1) ||
3612 (sc->synhw.capReportsV && nfingers > 2) ||
3613 (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) ||
3614 (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) ||
3615 (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) {
3617 * We consider the packet irrelevant for the current
3619 * - the width isn't comprised in:
3621 * - the pressure isn't comprised in:
3622 * [min_pressure; max_pressure]
3623 * - pen aren't supported but PSM_FINGER_IS_PEN is set
3625 VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f->w));
3632 psmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers,
3635 smoother_t *smoother;
3638 int y_ok, center_button, center_x, right_button, right_x, i;
3641 smoother = &sc->smoother[0];
3642 gest = &sc->gesture;
3644 /* Find first active finger. */
3646 for (i = 0; i < PSM_FINGERS; i++) {
3647 if (PSM_FINGER_IS_SET(fingers[i])) {
3649 smoother = &sc->smoother[i];
3656 * Check pressure to detect a real wanted action on the
3659 if (f->p >= sc->syninfo.min_pressure) {
3662 int start_x, start_y;
3664 int margin_top, margin_right, margin_bottom, margin_left;
3665 int window_min, window_max;
3666 int vscroll_hor_area, vscroll_ver_area;
3667 int two_finger_scroll;
3671 /* XXX Verify values? */
3672 margin_top = sc->syninfo.margin_top;
3673 margin_right = sc->syninfo.margin_right;
3674 margin_bottom = sc->syninfo.margin_bottom;
3675 margin_left = sc->syninfo.margin_left;
3676 window_min = sc->syninfo.window_min;
3677 window_max = sc->syninfo.window_max;
3678 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
3679 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
3680 two_finger_scroll = sc->syninfo.two_finger_scroll;
3681 max_x = sc->syninfo.max_x;
3682 max_y = sc->syninfo.max_y;
3684 /* Read current absolute position. */
3689 * Limit the coordinates to the specified margins because
3690 * this area isn't very reliable.
3692 if (x0 <= margin_left)
3694 else if (x0 >= max_x - margin_right)
3695 x0 = max_x - margin_right;
3696 if (y0 <= margin_bottom)
3698 else if (y0 >= max_y - margin_top)
3699 y0 = max_y - margin_top;
3701 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
3702 x0, y0, f->p, f->w));
3705 * If the action is just beginning, init the structure and
3706 * compute tap timeout.
3708 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
3709 VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
3711 /* Initialize queue. */
3712 gest->window_min = window_min;
3714 /* Reset pressure peak. */
3717 /* Reset fingers count. */
3718 gest->fingers_nb = 0;
3720 /* Reset virtual scrolling state. */
3721 gest->in_vscroll = 0;
3723 /* Compute tap timeout. */
3724 gest->taptimeout.tv_sec = tap_timeout / 1000000;
3725 gest->taptimeout.tv_usec = tap_timeout % 1000000;
3726 timevaladd(&gest->taptimeout, &sc->lastsoftintr);
3728 sc->flags |= PSM_FLAGS_FINGERDOWN;
3730 /* Smoother has not been reset yet */
3735 queue_len = smoother->queue_len + 1;
3736 start_x = smoother->start_x;
3737 start_y = smoother->start_y;
3740 /* Process ClickPad softbuttons */
3741 if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) {
3742 y_ok = sc->syninfo.softbuttons_y >= 0 ?
3743 start_y < sc->syninfo.softbuttons_y :
3744 start_y > max_y + sc->syninfo.softbuttons_y;
3746 center_button = MOUSE_BUTTON2DOWN;
3747 center_x = sc->syninfo.softbutton2_x;
3748 right_button = MOUSE_BUTTON3DOWN;
3749 right_x = sc->syninfo.softbutton3_x;
3751 if (center_x > 0 && right_x > 0 && center_x > right_x) {
3752 center_button = MOUSE_BUTTON3DOWN;
3753 center_x = sc->syninfo.softbutton3_x;
3754 right_button = MOUSE_BUTTON2DOWN;
3755 right_x = sc->syninfo.softbutton2_x;
3758 if (right_x > 0 && start_x > right_x && y_ok)
3759 ms->button = (ms->button &
3760 ~MOUSE_BUTTON1DOWN) | right_button;
3761 else if (center_x > 0 && start_x > center_x && y_ok)
3762 ms->button = (ms->button &
3763 ~MOUSE_BUTTON1DOWN) | center_button;
3766 /* If in tap-hold, add the recorded button. */
3767 if (gest->in_taphold)
3768 ms->button |= gest->tap_button;
3771 * For tap, we keep the maximum number of fingers and the
3772 * pressure peak. Also with multiple fingers, we increase
3773 * the minimum window.
3776 gest->window_min = window_max;
3777 gest->fingers_nb = imax(nfingers, gest->fingers_nb);
3778 gest->zmax = imax(f->p, gest->zmax);
3780 /* Do we have enough packets to consider this a gesture? */
3781 if (queue_len < gest->window_min)
3787 /* Is a scrolling action occurring? */
3788 if (!gest->in_taphold && !ms->button &&
3789 (!gest->in_vscroll || two_finger_scroll)) {
3791 * A scrolling action must not conflict with a tap
3792 * action. Here are the conditions to consider a
3794 * - the action in a configurable area
3795 * - one of the following:
3796 * . the distance between the last packet and the
3797 * first should be above a configurable minimum
3800 dxp = abs(x0 - start_x);
3801 dyp = abs(y0 - start_y);
3803 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) ||
3804 dxp >= sc->syninfo.vscroll_min_delta ||
3805 dyp >= sc->syninfo.vscroll_min_delta) {
3807 * Handle two finger scrolling.
3808 * Note that we don't rely on fingers_nb
3809 * as that keeps the maximum number of fingers.
3811 if (two_finger_scroll) {
3812 if (nfingers == 2) {
3819 /* Check for horizontal scrolling. */
3820 if ((vscroll_hor_area > 0 &&
3821 start_y <= vscroll_hor_area) ||
3822 (vscroll_hor_area < 0 &&
3824 max_y + vscroll_hor_area))
3825 gest->in_vscroll += 2;
3827 /* Check for vertical scrolling. */
3828 if ((vscroll_ver_area > 0 &&
3829 start_x <= vscroll_ver_area) ||
3830 (vscroll_ver_area < 0 &&
3832 max_x + vscroll_ver_area))
3833 gest->in_vscroll += 1;
3836 /* Avoid conflicts if area overlaps. */
3837 if (gest->in_vscroll >= 3)
3839 (dxp > dyp) ? 2 : 1;
3843 * Reset two finger scrolling when the number of fingers
3844 * is different from two or any button is pressed.
3846 if (two_finger_scroll && gest->in_vscroll != 0 &&
3847 (nfingers != 2 || ms->button))
3848 gest->in_vscroll = 0;
3851 "synaptics: virtual scrolling: %s "
3852 "(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n",
3853 gest->in_vscroll ? "YES" : "NO",
3854 gest->in_vscroll, dxp, dyp,
3857 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3859 * An action is currently taking place but the pressure
3860 * dropped under the minimum, putting an end to it.
3862 int taphold_timeout, dx, dy, tap_max_delta;
3864 dx = abs(smoother->queue[smoother->queue_cursor].x -
3866 dy = abs(smoother->queue[smoother->queue_cursor].y -
3869 /* Max delta is disabled for multi-fingers tap. */
3870 if (gest->fingers_nb > 1)
3871 tap_max_delta = imax(dx, dy);
3873 tap_max_delta = sc->syninfo.tap_max_delta;
3875 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3877 /* Check for tap. */
3879 "synaptics: zmax=%d, dx=%d, dy=%d, "
3880 "delta=%d, fingers=%d, queue=%d\n",
3881 gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb,
3882 smoother->queue_len));
3883 if (!gest->in_vscroll && gest->zmax >= tap_threshold &&
3884 timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) &&
3885 dx <= tap_max_delta && dy <= tap_max_delta &&
3886 smoother->queue_len >= sc->syninfo.tap_min_queue) {
3889 * - the maximum pressure went over tap_threshold
3890 * - the action ended before tap_timeout
3892 * To handle tap-hold, we must delay any button push to
3895 if (gest->in_taphold) {
3897 * This is the second and last tap of a
3898 * double tap action, not a tap-hold.
3900 gest->in_taphold = 0;
3903 * For double-tap to work:
3904 * - no button press is emitted (to
3905 * simulate a button release)
3906 * - PSM_FLAGS_FINGERDOWN is set to
3907 * force the next packet to emit a
3911 "synaptics: button RELEASE: %d\n",
3913 sc->flags |= PSM_FLAGS_FINGERDOWN;
3915 /* Schedule button press on next interrupt */
3916 sc->idletimeout.tv_sec = psmhz > 1 ?
3918 sc->idletimeout.tv_usec = psmhz > 1 ?
3919 1000000 / psmhz : 0;
3922 * This is the first tap: we set the
3923 * tap-hold state and notify the button
3926 gest->in_taphold = 1;
3927 taphold_timeout = sc->syninfo.taphold_timeout;
3928 gest->taptimeout.tv_sec = taphold_timeout /
3930 gest->taptimeout.tv_usec = taphold_timeout %
3932 sc->idletimeout = gest->taptimeout;
3933 timevaladd(&gest->taptimeout,
3936 switch (gest->fingers_nb) {
3950 "synaptics: button PRESS: %d\n",
3952 ms->button |= gest->tap_button;
3956 * Not enough pressure or timeout: reset
3959 if (gest->in_taphold) {
3961 "synaptics: button RELEASE: %d\n",
3963 gest->in_taphold = 0;
3966 "synaptics: not a tap-hold\n"));
3969 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) {
3971 * For a tap-hold to work, the button must remain down at
3972 * least until timeout (where the in_taphold flags will be
3973 * cleared) or during the next action.
3975 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) {
3976 ms->button |= gest->tap_button;
3978 VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n",
3980 gest->in_taphold = 0;
3988 psmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id,
3989 mousestatus_t *ms, int *x, int *y)
3991 smoother_t *smoother = &sc->smoother[smoother_id];
3992 gesture_t *gest = &(sc->gesture);
3995 * Check pressure to detect a real wanted action on the
3998 if (f->p >= sc->syninfo.min_pressure) {
4000 int cursor, peer, window;
4001 int dx, dy, dxp, dyp;
4002 int max_width, max_pressure;
4003 int margin_top, margin_right, margin_bottom, margin_left;
4004 int na_top, na_right, na_bottom, na_left;
4005 int window_min, window_max;
4007 int weight_current, weight_previous, weight_len_squared;
4008 int div_min, div_max, div_len;
4009 int vscroll_hor_area, vscroll_ver_area;
4010 int two_finger_scroll;
4012 int len, weight_prev_x, weight_prev_y;
4013 int div_max_x, div_max_y, div_x, div_y;
4017 /* XXX Verify values? */
4018 max_width = sc->syninfo.max_width;
4019 max_pressure = sc->syninfo.max_pressure;
4020 margin_top = sc->syninfo.margin_top;
4021 margin_right = sc->syninfo.margin_right;
4022 margin_bottom = sc->syninfo.margin_bottom;
4023 margin_left = sc->syninfo.margin_left;
4024 na_top = sc->syninfo.na_top;
4025 na_right = sc->syninfo.na_right;
4026 na_bottom = sc->syninfo.na_bottom;
4027 na_left = sc->syninfo.na_left;
4028 window_min = sc->syninfo.window_min;
4029 window_max = sc->syninfo.window_max;
4030 multiplicator = sc->syninfo.multiplicator;
4031 weight_current = sc->syninfo.weight_current;
4032 weight_previous = sc->syninfo.weight_previous;
4033 weight_len_squared = sc->syninfo.weight_len_squared;
4034 div_min = sc->syninfo.div_min;
4035 div_max = sc->syninfo.div_max;
4036 div_len = sc->syninfo.div_len;
4037 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
4038 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
4039 two_finger_scroll = sc->syninfo.two_finger_scroll;
4040 max_x = sc->syninfo.max_x;
4041 max_y = sc->syninfo.max_y;
4043 is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0;
4045 /* Read current absolute position. */
4050 * Limit the coordinates to the specified margins because
4051 * this area isn't very reliable.
4053 if (x0 <= margin_left)
4055 else if (x0 >= max_x - margin_right)
4056 x0 = max_x - margin_right;
4057 if (y0 <= margin_bottom)
4059 else if (y0 >= max_y - margin_top)
4060 y0 = max_y - margin_top;
4062 /* If the action is just beginning, init the structure. */
4063 if (smoother->active == 0) {
4064 VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id));
4066 /* Store the first point of this action. */
4067 smoother->start_x = x0;
4068 smoother->start_y = y0;
4071 /* Initialize queue. */
4072 smoother->queue_cursor = SYNAPTICS_PACKETQUEUE;
4073 smoother->queue_len = 0;
4075 /* Reset average. */
4076 smoother->avg_dx = 0;
4077 smoother->avg_dy = 0;
4079 /* Reset squelch. */
4080 smoother->squelch_x = 0;
4081 smoother->squelch_y = 0;
4083 /* Activate queue */
4084 smoother->active = 1;
4086 /* Calculate the current delta. */
4087 cursor = smoother->queue_cursor;
4088 dx = x0 - smoother->queue[cursor].x;
4089 dy = y0 - smoother->queue[cursor].y;
4092 VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n",
4093 smoother_id, x0, y0, f->p, f->w));
4095 /* Queue this new packet. */
4096 cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1);
4097 smoother->queue[cursor].x = x0;
4098 smoother->queue[cursor].y = y0;
4099 smoother->queue_cursor = cursor;
4100 if (smoother->queue_len < SYNAPTICS_PACKETQUEUE)
4101 smoother->queue_len++;
4103 "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
4104 smoother_id, cursor, x0, y0, dx, dy));
4106 /* Do we have enough packets to consider this a movement? */
4107 if (smoother->queue_len < gest->window_min)
4110 weight_prev_x = weight_prev_y = weight_previous;
4111 div_max_x = div_max_y = div_max;
4113 if (gest->in_vscroll) {
4114 /* Dividers are different with virtual scrolling. */
4115 div_min = sc->syninfo.vscroll_div_min;
4116 div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
4119 * There's a lot of noise in coordinates when
4120 * the finger is on the touchpad's borders. When
4121 * using this area, we apply a special weight and
4124 if (x0 <= na_left || x0 >= max_x - na_right) {
4125 weight_prev_x = sc->syninfo.weight_previous_na;
4126 div_max_x = sc->syninfo.div_max_na;
4129 if (y0 <= na_bottom || y0 >= max_y - na_top) {
4130 weight_prev_y = sc->syninfo.weight_previous_na;
4131 div_max_y = sc->syninfo.div_max_na;
4136 * Calculate weights for the average operands and
4137 * the divisor. Both depend on the distance between
4138 * the current packet and a previous one (based on the
4141 window = imin(smoother->queue_len, window_max);
4142 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
4143 dxp = abs(x0 - smoother->queue[peer].x) + 1;
4144 dyp = abs(y0 - smoother->queue[peer].y) + 1;
4145 len = (dxp * dxp) + (dyp * dyp);
4146 weight_prev_x = imin(weight_prev_x,
4147 weight_len_squared * weight_prev_x / len);
4148 weight_prev_y = imin(weight_prev_y,
4149 weight_len_squared * weight_prev_y / len);
4151 len = (dxp + dyp) / 2;
4152 div_x = div_len * div_max_x / len;
4153 div_x = imin(div_max_x, div_x);
4154 div_x = imax(div_min, div_x);
4155 div_y = div_len * div_max_y / len;
4156 div_y = imin(div_max_y, div_y);
4157 div_y = imax(div_min, div_y);
4160 "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
4161 smoother_id, peer, len, weight_prev_x, weight_prev_y,
4164 /* Compute averages. */
4166 (weight_current * dx * multiplicator +
4167 weight_prev_x * smoother->avg_dx) /
4168 (weight_current + weight_prev_x);
4171 (weight_current * dy * multiplicator +
4172 weight_prev_y * smoother->avg_dy) /
4173 (weight_current + weight_prev_y);
4176 "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id,
4177 smoother->avg_dx / multiplicator,
4178 smoother->avg_dy / multiplicator));
4180 /* Use these averages to calculate x & y. */
4181 smoother->squelch_x += smoother->avg_dx;
4182 dxp = smoother->squelch_x / (div_x * multiplicator);
4183 smoother->squelch_x = smoother->squelch_x %
4184 (div_x * multiplicator);
4186 smoother->squelch_y += smoother->avg_dy;
4187 dyp = smoother->squelch_y / (div_y * multiplicator);
4188 smoother->squelch_y = smoother->squelch_y %
4189 (div_y * multiplicator);
4191 switch(gest->in_vscroll) {
4192 case 0: /* Pointer movement. */
4193 /* On real<->fuzzy finger switch the x/y pos jumps */
4194 if (is_fuzzy == smoother->is_fuzzy) {
4199 VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n",
4200 smoother_id, dx, dy, dxp, dyp));
4202 case 1: /* Vertical scrolling. */
4204 ms->button |= (dyp > 0) ?
4205 MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN;
4207 case 2: /* Horizontal scrolling. */
4209 ms->button |= (dxp > 0) ?
4210 MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN;
4214 smoother->is_fuzzy = is_fuzzy;
4218 * Deactivate queue. Note: We can not just reset queue here
4219 * as these values are still used by gesture processor.
4220 * So postpone reset till next touch.
4222 smoother->active = 0;
4227 proc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4228 int *x, int *y, int *z)
4230 static int touchpad_button, trackpoint_button;
4231 finger_t fn, f[ELANTECH_MAX_FINGERS];
4232 int pkt, id, scale, i, nfingers, mask;
4234 if (!elantech_support)
4237 /* Determine packet format and do a sanity check for out of sync packets. */
4238 if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion))
4239 pkt = ELANTECH_PKT_NOP;
4240 else if (sc->elanhw.hastrackpoint && ELANTECH_PKT_IS_TRACKPOINT(pb))
4241 pkt = ELANTECH_PKT_TRACKPOINT;
4243 switch (sc->elanhw.hwversion) {
4245 if (!ELANTECH_PKT_IS_V2(pb))
4248 pkt = (pb->ipacket[0] & 0xc0) == 0x80 ?
4249 ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON;
4252 if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) &&
4253 !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc))
4256 pkt = ELANTECH_PKT_V3;
4259 if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc))
4262 switch (pb->ipacket[3] & 0x03) {
4264 pkt = ELANTECH_PKT_V4_STATUS;
4267 pkt = ELANTECH_PKT_V4_HEAD;
4270 pkt = ELANTECH_PKT_V4_MOTION;
4280 VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt));
4282 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4283 PSM_FINGER_RESET(f[id]);
4286 ms->button = ms->obutton;
4288 if (sc->syninfo.touchpad_off)
4292 * L: Left mouse button pressed
4293 * R: Right mouse button pressed
4294 * N: number of fingers on touchpad
4295 * X: absolute x value (horizontal)
4296 * Y: absolute y value (vertical)
4297 * W; width of the finger touch
4301 case ELANTECH_PKT_V2_COMMON: /* HW V2. One/Three finger touch */
4302 /* 7 6 5 4 3 2 1 0 (LSB)
4303 * -------------------------------------------
4304 * ipacket[0]: N1 N0 W3 W2 . . R L
4305 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4306 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4307 * ipacket[3]: N4 VF W1 W0 . . . B2
4308 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4309 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4310 * -------------------------------------------
4311 * N4: set if more than 3 fingers (only in 3 fingers mode)
4312 * VF: a kind of flag? (only on EF123, 0 when finger
4313 * is over one of the buttons, 1 otherwise)
4314 * B2: (on EF113 only, 0 otherwise), one button pressed
4315 * P & W is not reported on EF113 touchpads
4317 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4318 if (nfingers == 3 && (pb->ipacket[3] & 0x80))
4321 if (nfingers == 0) {
4322 mask = (1 << nfingers) - 1; /* = 0x00 */
4326 /* Map 3-rd and 4-th fingers to first finger */
4327 mask = (1 << 1) - 1; /* = 0x01 */
4328 f[0] = ELANTECH_FINGER_SET_XYP(pb);
4329 if (sc->elanhw.haspressure) {
4330 f[0].w = ((pb->ipacket[0] & 0x30) >> 2) |
4331 ((pb->ipacket[3] & 0x30) >> 4);
4333 f[0].p = PSM_FINGER_DEFAULT_P;
4334 f[0].w = PSM_FINGER_DEFAULT_W;
4338 * HW v2 dont report exact finger positions when 3 or more
4339 * fingers are on touchpad.
4342 f[0].flags = PSM_FINGER_FUZZY;
4346 case ELANTECH_PKT_V2_2FINGER: /*HW V2. Two finger touch */
4347 /* 7 6 5 4 3 2 1 0 (LSB)
4348 * -------------------------------------------
4349 * ipacket[0]: N1 N0 AY8 AX8 . . R L
4350 * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0
4351 * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0
4352 * ipacket[3]: . . BY8 BX8 . . . .
4353 * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0
4354 * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0
4355 * -------------------------------------------
4356 * AX: lower-left finger absolute x value
4357 * AY: lower-left finger absolute y value
4358 * BX: upper-right finger absolute x value
4359 * BY: upper-right finger absolute y value
4362 mask = (1 << nfingers) - 1;
4364 for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++)
4365 f[id] = (finger_t) {
4366 .x = (((pb->ipacket[id * 3] & 0x10) << 4) |
4367 pb->ipacket[id * 3 + 1]) << 2,
4368 .y = (((pb->ipacket[id * 3] & 0x20) << 3) |
4369 pb->ipacket[id * 3 + 2]) << 2,
4370 .p = PSM_FINGER_DEFAULT_P,
4371 .w = PSM_FINGER_DEFAULT_W,
4372 /* HW ver.2 sends bounding box */
4373 .flags = PSM_FINGER_FUZZY
4377 case ELANTECH_PKT_V3: /* HW Version 3 */
4378 /* 7 6 5 4 3 2 1 0 (LSB)
4379 * -------------------------------------------
4380 * ipacket[0]: N1 N0 W3 W2 0 1 R L
4381 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4382 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4383 * ipacket[3]: 0 0 W1 W0 0 0 1 0
4384 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4385 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4386 * -------------------------------------------
4388 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4389 /* Map 3-rd finger to first finger */
4390 id = nfingers > 2 ? 0 : nfingers - 1;
4391 mask = (1 << (id + 1)) - 1;
4396 fn = ELANTECH_FINGER_SET_XYP(pb);
4397 fn.w = ((pb->ipacket[0] & 0x30) >> 2) |
4398 ((pb->ipacket[3] & 0x30) >> 4);
4401 * HW v3 dont report exact finger positions when 3 or more
4402 * fingers are on touchpad.
4405 fn.flags = PSM_FINGER_FUZZY;
4407 if (nfingers == 2) {
4408 if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) {
4409 sc->elanaction.fingers[0] = fn;
4412 f[0] = sc->elanaction.fingers[0];
4417 case ELANTECH_PKT_V4_STATUS: /* HW Version 4. Status packet */
4418 /* 7 6 5 4 3 2 1 0 (LSB)
4419 * -------------------------------------------
4420 * ipacket[0]: . . . . 0 1 R L
4421 * ipacket[1]: . . . F4 F3 F2 F1 F0
4422 * ipacket[2]: . . . . . . . .
4423 * ipacket[3]: . . . 1 0 0 0 0
4424 * ipacket[4]: PL . . . . . . .
4425 * ipacket[5]: . . . . . . . .
4426 * -------------------------------------------
4427 * Fn: finger n is on touchpad
4429 * HV ver4 sends a status packet to indicate that the numbers
4430 * or identities of the fingers has been changed
4433 mask = pb->ipacket[1] & 0x1f;
4434 nfingers = bitcount(mask);
4436 if (sc->elanaction.mask_v4wait != 0)
4437 VLOG(3, (LOG_DEBUG, "elantech: HW v4 status packet"
4438 " when not all previous head packets received\n"));
4440 /* Bitmap of fingers to receive before gesture processing */
4441 sc->elanaction.mask_v4wait = mask & ~sc->elanaction.mask;
4443 /* Skip "new finger is on touchpad" packets */
4444 if (sc->elanaction.mask_v4wait) {
4445 sc->elanaction.mask = mask;
4451 case ELANTECH_PKT_V4_HEAD: /* HW Version 4. Head packet */
4452 /* 7 6 5 4 3 2 1 0 (LSB)
4453 * -------------------------------------------
4454 * ipacket[0]: W3 W2 W1 W0 0 1 R L
4455 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4456 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4457 * ipacket[3]: ID2 ID1 ID0 1 0 0 0 1
4458 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4459 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4460 * -------------------------------------------
4462 * HW ver 4 sends head packets in two cases:
4463 * 1. One finger touch and movement.
4464 * 2. Next after status packet to tell new finger positions.
4466 mask = sc->elanaction.mask;
4467 nfingers = bitcount(mask);
4468 id = ((pb->ipacket[3] & 0xe0) >> 5) - 1;
4469 fn = ELANTECH_FINGER_SET_XYP(pb);
4470 fn.w =(pb->ipacket[0] & 0xf0) >> 4;
4475 /* Packet is finger position update. Report it */
4476 if (sc->elanaction.mask_v4wait == 0) {
4477 if (id < ELANTECH_MAX_FINGERS)
4482 /* Remove finger from waiting bitmap and store into context */
4483 sc->elanaction.mask_v4wait &= ~(1 << id);
4484 if (id < ELANTECH_MAX_FINGERS)
4485 sc->elanaction.fingers[id] = fn;
4487 /* Wait for other fingers if needed */
4488 if (sc->elanaction.mask_v4wait != 0)
4491 /* All new fingers are received. Report them from context */
4492 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4493 if (sc->elanaction.mask & (1 << id))
4494 f[id] = sc->elanaction.fingers[id];
4498 case ELANTECH_PKT_V4_MOTION: /* HW Version 4. Motion packet */
4499 /* 7 6 5 4 3 2 1 0 (LSB)
4500 * -------------------------------------------
4501 * ipacket[0]: ID2 ID1 ID0 OF 0 1 R L
4502 * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4503 * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4504 * ipacket[3]: ID2 ID1 ID0 1 0 0 1 0
4505 * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4506 * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4507 * -------------------------------------------
4508 * OF: delta overflows (> 127 or < -128), in this case
4509 * firmware sends us (delta x / 5) and (delta y / 5)
4511 * DX: delta x (two's complement)
4512 * XY: delta y (two's complement)
4513 * byte 0 ~ 2 for one finger
4514 * byte 3 ~ 5 for another finger
4516 mask = sc->elanaction.mask;
4517 nfingers = bitcount(mask);
4519 scale = (pb->ipacket[0] & 0x10) ? 5 : 1;
4520 for (i = 0; i <= 3; i += 3) {
4521 id = ((pb->ipacket[i] & 0xe0) >> 5) - 1;
4522 if (id < 0 || id >= ELANTECH_MAX_FINGERS)
4525 if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) {
4526 f[id] = sc->elanaction.fingers[id];
4527 f[id].x += imax(-f[id].x,
4528 (signed char)pb->ipacket[i+1] * scale);
4529 f[id].y += imax(-f[id].y,
4530 (signed char)pb->ipacket[i+2] * scale);
4532 VLOG(3, (LOG_DEBUG, "elantech: "
4533 "HW v4 motion packet skipped\n"));
4539 case ELANTECH_PKT_TRACKPOINT:
4540 /* 7 6 5 4 3 2 1 0 (LSB)
4541 * -------------------------------------------
4542 * ipacket[0]: 0 0 SX SY 0 M R L
4543 * ipacket[1]: ~SX 0 0 0 0 0 0 0
4544 * ipacket[2]: ~SY 0 0 0 0 0 0 0
4545 * ipacket[3]: 0 0 ~SY ~SX 0 1 1 0
4546 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
4547 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4548 * -------------------------------------------
4549 * X and Y are written in two's complement spread
4550 * over 9 bits with SX/SY the relative top bit and
4551 * X7..X0 and Y7..Y0 the lower bits.
4553 *x = (pb->ipacket[0] & 0x20) ?
4554 pb->ipacket[4] - 256 : pb->ipacket[4];
4555 *y = (pb->ipacket[0] & 0x10) ?
4556 pb->ipacket[5] - 256 : pb->ipacket[5];
4559 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4560 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) |
4561 ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0);
4562 #ifdef EVDEV_SUPPORT
4563 evdev_push_rel(sc->evdev_r, REL_X, *x);
4564 evdev_push_rel(sc->evdev_r, REL_Y, -*y);
4565 evdev_push_mouse_btn(sc->evdev_r, trackpoint_button);
4566 evdev_sync(sc->evdev_r);
4568 ms->button = touchpad_button | trackpoint_button;
4571 case ELANTECH_PKT_NOP:
4578 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4579 if (PSM_FINGER_IS_SET(f[id]))
4580 VLOG(2, (LOG_DEBUG, "elantech: "
4581 "finger %d: down [%d, %d], %d, %d, %d\n", id + 1,
4582 f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags));
4584 /* Touchpad button presses */
4585 if (sc->elanhw.isclickpad) {
4587 ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0);
4590 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4591 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0);
4594 #ifdef EVDEV_SUPPORT
4595 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
4596 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4597 if (PSM_FINGER_IS_SET(f[id])) {
4598 psm_push_mt_finger(sc, id, &f[id]);
4599 /* Convert touch width to surface units */
4600 evdev_push_abs(sc->evdev_a, ABS_MT_TOUCH_MAJOR,
4601 f[id].w * sc->elanhw.dptracex);
4603 if (sc->elanaction.mask & (1 << id) &&
4604 !(mask & (1 << id)))
4605 psm_release_mt_slot(sc->evdev_a, id);
4607 evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
4608 evdev_push_nfingers(sc->evdev_a, nfingers);
4610 if (PSM_FINGER_IS_SET(f[0]))
4611 psm_push_st_finger(sc, &f[0]);
4613 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
4614 evdev_push_mouse_btn(sc->evdev_a, touchpad_button);
4615 evdev_sync(sc->evdev_a);
4619 ms->button = touchpad_button | trackpoint_button;
4621 /* Send finger 1 position to gesture processor */
4622 if (PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) ||
4624 psmgestures(sc, &f[0], imin(nfingers, 3), ms);
4625 /* Send fingers positions to movement smoothers */
4626 for (id = 0; id < PSM_FINGERS; id++)
4627 if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id)))
4628 psmsmoother(sc, &f[id], id, ms, x, y);
4630 /* Store current finger positions in action context */
4631 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4632 if (PSM_FINGER_IS_SET(f[id]))
4633 sc->elanaction.fingers[id] = f[id];
4634 if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id)))
4635 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
4637 sc->elanaction.mask = mask;
4639 /* Palm detection doesn't terminate the current action. */
4640 if (psmpalmdetect(sc, &f[0], nfingers)) {
4642 ms->button = ms->obutton;
4646 /* Use the extra buttons as a scrollwheel */
4647 if (ms->button & MOUSE_BUTTON4DOWN)
4649 else if (ms->button & MOUSE_BUTTON5DOWN)
4651 else if (ms->button & MOUSE_BUTTON6DOWN)
4653 else if (ms->button & MOUSE_BUTTON7DOWN)
4657 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
4658 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
4664 proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4665 int *x, int *y, int *z)
4667 static int butmap_versapad[8] = {
4673 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4675 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
4679 /* VersaPad PS/2 absolute mode message format
4681 * [packet1] 7 6 5 4 3 2 1 0(LSB)
4682 * ipacket[0]: 1 1 0 A 1 L T R
4683 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
4684 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
4685 * ipacket[3]: 1 1 1 A 1 L T R
4686 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
4687 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
4690 * R: right physical mouse button (1=on)
4691 * T: touch pad virtual button (1=tapping)
4692 * L: left physical mouse button (1=on)
4693 * A: position data is valid (1=valid)
4694 * H: horizontal data (12bit signed integer. H11 is sign bit.)
4695 * V: vertical data (12bit signed integer. V11 is sign bit.)
4698 * Tapping is mapped to MOUSE_BUTTON4.
4702 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
4703 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
4704 if (c & MOUSE_PS2VERSA_IN_USE) {
4705 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
4706 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
4711 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
4714 if (*x < 0) /* XXX */
4723 sc->flags |= PSM_FLAGS_FINGERDOWN;
4727 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
4731 psmsoftintridle(void *arg)
4733 struct psm_softc *sc = arg;
4736 /* Invoke soft handler only when pqueue is empty. Otherwise it will be
4737 * invoked from psmintr soon with pqueue filled with real data */
4738 if (sc->pqueue_start == sc->pqueue_end &&
4739 sc->idlepacket.inputbytes > 0) {
4740 /* Grow circular queue backwards to avoid race with psmintr */
4741 if (--sc->pqueue_start < 0)
4742 sc->pqueue_start = PSM_PACKETQUEUE - 1;
4744 pb = &sc->pqueue[sc->pqueue_start];
4745 memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t));
4747 "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n",
4748 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
4749 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
4756 psmsoftintr(void *arg)
4759 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
4760 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
4762 static int butmap[8] = {
4766 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4768 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
4769 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
4770 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
4772 struct psm_softc *sc = arg;
4775 int x, y, z, c, l, s;
4777 getmicrouptime(&sc->lastsoftintr);
4782 pb = &sc->pqueue[sc->pqueue_start];
4784 if (sc->mode.level == PSM_LEVEL_NATIVE)
4789 * A kludge for Kensington device!
4790 * The MSB of the horizontal count appears to be stored in
4793 if (sc->hw.model == MOUSE_MODEL_THINK)
4794 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
4796 /* ignore the overflow bits... */
4797 x = (c & MOUSE_PS2_XNEG) ?
4798 pb->ipacket[1] - 256 : pb->ipacket[1];
4799 y = (c & MOUSE_PS2_YNEG) ?
4800 pb->ipacket[2] - 256 : pb->ipacket[2];
4802 ms.obutton = sc->button; /* previous button state */
4803 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
4804 /* `tapping' action */
4805 if (sc->config & PSM_CONFIG_FORCETAP)
4806 ms.button |= ((c & MOUSE_PS2_TAP)) ?
4807 0 : MOUSE_BUTTON4DOWN;
4808 timevalclear(&sc->idletimeout);
4809 sc->idlepacket.inputbytes = 0;
4811 switch (sc->hw.model) {
4813 case MOUSE_MODEL_EXPLORER:
4815 * b7 b6 b5 b4 b3 b2 b1 b0
4816 * byte 1: oy ox sy sx 1 M R L
4817 * byte 2: x x x x x x x x
4818 * byte 3: y y y y y y y y
4819 * byte 4: * * S2 S1 s d2 d1 d0
4821 * L, M, R, S1, S2: left, middle, right and side buttons
4822 * s: wheel data sign bit
4825 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
4826 (pb->ipacket[3] & 0x0f) - 16 :
4827 (pb->ipacket[3] & 0x0f);
4829 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
4830 MOUSE_BUTTON4DOWN : 0;
4832 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
4833 MOUSE_BUTTON5DOWN : 0;
4836 case MOUSE_MODEL_INTELLI:
4837 case MOUSE_MODEL_NET:
4838 /* wheel data is in the fourth byte */
4839 z = (char)pb->ipacket[3];
4841 * XXX some mice may send 7 when there is no Z movement? */
4842 if ((z >= 7) || (z <= -7))
4844 /* some compatible mice have additional buttons */
4845 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
4846 MOUSE_BUTTON4DOWN : 0;
4847 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
4848 MOUSE_BUTTON5DOWN : 0;
4851 case MOUSE_MODEL_MOUSEMANPLUS:
4852 proc_mmanplus(sc, pb, &ms, &x, &y, &z);
4855 case MOUSE_MODEL_GLIDEPOINT:
4856 /* `tapping' action */
4857 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
4861 case MOUSE_MODEL_NETSCROLL:
4863 * three additional bytes encode buttons and
4866 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
4867 MOUSE_BUTTON4DOWN : 0;
4868 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
4869 MOUSE_BUTTON5DOWN : 0;
4870 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
4871 pb->ipacket[4] - 256 : pb->ipacket[4];
4874 case MOUSE_MODEL_THINK:
4875 /* the fourth button state in the first byte */
4876 ms.button |= (c & MOUSE_PS2_TAP) ?
4877 MOUSE_BUTTON4DOWN : 0;
4880 case MOUSE_MODEL_VERSAPAD:
4881 proc_versapad(sc, pb, &ms, &x, &y, &z);
4882 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
4883 ((y < 0) ? MOUSE_PS2_YNEG : 0);
4886 case MOUSE_MODEL_4D:
4888 * b7 b6 b5 b4 b3 b2 b1 b0
4889 * byte 1: s2 d2 s1 d1 1 M R L
4890 * byte 2: sx x x x x x x x
4891 * byte 3: sy y y y y y y y
4893 * s1: wheel 1 direction
4895 * s2: wheel 2 direction
4898 x = (pb->ipacket[1] & 0x80) ?
4899 pb->ipacket[1] - 256 : pb->ipacket[1];
4900 y = (pb->ipacket[2] & 0x80) ?
4901 pb->ipacket[2] - 256 : pb->ipacket[2];
4902 switch (c & MOUSE_4D_WHEELBITS) {
4909 case 0x40: /* XXX 2nd wheel turning right */
4912 case 0xc0: /* XXX 2nd wheel turning left */
4918 case MOUSE_MODEL_4DPLUS:
4919 if ((x < 16 - 256) && (y < 16 - 256)) {
4921 * b7 b6 b5 b4 b3 b2 b1 b0
4922 * byte 1: 0 0 1 1 1 M R L
4923 * byte 2: 0 0 0 0 1 0 0 0
4924 * byte 3: 0 0 0 0 S s d1 d0
4926 * L, M, R, S: left, middle, right,
4928 * s: wheel data sign bit
4932 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
4933 ms.button |= MOUSE_BUTTON4DOWN;
4934 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
4935 ((pb->ipacket[2] & 0x07) - 8) :
4936 (pb->ipacket[2] & 0x07) ;
4938 /* preserve previous button states */
4939 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
4943 case MOUSE_MODEL_SYNAPTICS:
4944 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0) {
4945 VLOG(3, (LOG_DEBUG, "synaptics: "
4946 "packet rejected\n"));
4951 case MOUSE_MODEL_ELANTECH:
4952 if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0) {
4953 VLOG(3, (LOG_DEBUG, "elantech: "
4954 "packet rejected\n"));
4959 case MOUSE_MODEL_TRACKPOINT:
4960 case MOUSE_MODEL_GENERIC:
4965 #ifdef EVDEV_SUPPORT
4966 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE &&
4967 sc->hw.model != MOUSE_MODEL_ELANTECH &&
4968 sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
4969 evdev_push_rel(sc->evdev_r, EV_REL, x);
4970 evdev_push_rel(sc->evdev_r, EV_REL, -y);
4972 switch (sc->hw.model) {
4973 case MOUSE_MODEL_EXPLORER:
4974 case MOUSE_MODEL_INTELLI:
4975 case MOUSE_MODEL_NET:
4976 case MOUSE_MODEL_NETSCROLL:
4977 case MOUSE_MODEL_4DPLUS:
4978 evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
4980 case MOUSE_MODEL_MOUSEMANPLUS:
4981 case MOUSE_MODEL_4D:
4985 evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
4989 evdev_push_rel(sc->evdev_r, REL_HWHEEL, z / 2);
4995 evdev_push_mouse_btn(sc->evdev_r, ms.button);
4996 evdev_sync(sc->evdev_r);
5001 if (sc->mode.accelfactor >= 1) {
5003 x = x * x / sc->mode.accelfactor;
5006 if (c & MOUSE_PS2_XNEG)
5010 y = y * y / sc->mode.accelfactor;
5013 if (c & MOUSE_PS2_YNEG)
5018 /* Store last packet for reinjection if it has not been set already */
5019 if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0)
5020 sc->idlepacket = *pb;
5025 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
5026 (ms.obutton ^ ms.button);
5028 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
5030 sc->status.flags |= ms.flags;
5031 sc->status.dx += ms.dx;
5032 sc->status.dy += ms.dy;
5033 sc->status.dz += ms.dz;
5034 sc->status.button = ms.button;
5035 sc->button = ms.button;
5038 sc->watchdog = FALSE;
5041 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
5042 l = imin(pb->inputbytes,
5043 sizeof(sc->queue.buf) - sc->queue.tail);
5044 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
5045 if (pb->inputbytes > l)
5046 bcopy(&pb->ipacket[l], &sc->queue.buf[0],
5047 pb->inputbytes - l);
5048 sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
5049 sizeof(sc->queue.buf);
5050 sc->queue.count += pb->inputbytes;
5055 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
5056 sc->pqueue_start = 0;
5057 } while (sc->pqueue_start != sc->pqueue_end);
5059 if (sc->state & PSM_ASLP) {
5060 sc->state &= ~PSM_ASLP;
5063 selwakeuppri(&sc->rsel, PZERO);
5064 if (sc->async != NULL) {
5065 pgsigio(&sc->async, SIGIO, 0);
5067 sc->state &= ~PSM_SOFTARMED;
5069 /* schedule injection of predefined packet after idletimeout
5070 * if no data packets have been received from psmintr */
5071 if (timevalisset(&sc->idletimeout)) {
5072 sc->state |= PSM_SOFTARMED;
5073 callout_reset(&sc->softcallout, tvtohz(&sc->idletimeout),
5074 psmsoftintridle, sc);
5075 VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n",
5076 tvtohz(&sc->idletimeout)));
5082 psmpoll(struct cdev *dev, int events, struct thread *td)
5084 struct psm_softc *sc = dev->si_drv1;
5088 /* Return true if a mouse event available */
5090 if (events & (POLLIN | POLLRDNORM)) {
5091 if (sc->queue.count > 0)
5092 revents |= events & (POLLIN | POLLRDNORM);
5094 selrecord(td, &sc->rsel);
5101 /* vendor/model specific routines */
5103 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
5105 if (set_mouse_resolution(kbdc, res) != res)
5107 if (set_mouse_scaling(kbdc, scale) &&
5108 set_mouse_scaling(kbdc, scale) &&
5109 set_mouse_scaling(kbdc, scale) &&
5110 (get_mouse_status(kbdc, status, 0, 3) >= 3))
5116 mouse_ext_command(KBDC kbdc, int command)
5120 c = (command >> 6) & 0x03;
5121 if (set_mouse_resolution(kbdc, c) != c)
5123 c = (command >> 4) & 0x03;
5124 if (set_mouse_resolution(kbdc, c) != c)
5126 c = (command >> 2) & 0x03;
5127 if (set_mouse_resolution(kbdc, c) != c)
5129 c = (command >> 0) & 0x03;
5130 if (set_mouse_resolution(kbdc, c) != c)
5136 /* Logitech MouseMan Cordless II */
5138 enable_lcordless(struct psm_softc *sc, enum probearg arg)
5140 KBDC kbdc = sc->kbdc;
5144 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status))
5146 if (status[1] == PSMD_RES_HIGH)
5148 ch = (status[0] & 0x07) - 1; /* channel # */
5149 if ((ch <= 0) || (ch > 4))
5152 * status[1]: always one?
5153 * status[2]: battery status? (0-100)
5159 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
5161 enable_groller(struct psm_softc *sc, enum probearg arg)
5163 KBDC kbdc = sc->kbdc;
5167 * The special sequence to enable the fourth button and the
5168 * roller. Immediately after this sequence check status bytes.
5169 * if the mouse is NetScroll, the second and the third bytes are
5174 * If the mouse is an ordinary PS/2 mouse, the status bytes should
5175 * look like the following.
5177 * byte 1 bit 7 always 0
5178 * bit 6 stream mode (0)
5179 * bit 5 disabled (0)
5180 * bit 4 1:1 scaling (0)
5182 * bit 0-2 button status
5183 * byte 2 resolution (PSMD_RES_HIGH)
5184 * byte 3 report rate (?)
5187 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5189 if ((status[1] != '3') || (status[2] != 'D'))
5191 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
5197 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
5199 enable_gmouse(struct psm_softc *sc, enum probearg arg)
5201 KBDC kbdc = sc->kbdc;
5205 * The special sequence to enable the middle, "rubber" button.
5206 * Immediately after this sequence check status bytes.
5207 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
5208 * the second and the third bytes are '3' and 'U'.
5209 * NOTE: NetMouse reports that it has three buttons although it has
5210 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
5211 * say they have three buttons too and they do have a button on the
5214 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5216 if ((status[1] != '3') || (status[2] != 'U'))
5221 /* ALPS GlidePoint */
5223 enable_aglide(struct psm_softc *sc, enum probearg arg)
5225 KBDC kbdc = sc->kbdc;
5229 * The special sequence to obtain ALPS GlidePoint specific
5230 * information. Immediately after this sequence, status bytes will
5231 * contain something interesting.
5232 * NOTE: ALPS produces several models of GlidePoint. Some of those
5233 * do not respond to this sequence, thus, cannot be detected this way.
5235 if (set_mouse_sampling_rate(kbdc, 100) != 100)
5237 if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
5239 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
5244 /* Kensington ThinkingMouse/Trackball */
5246 enable_kmouse(struct psm_softc *sc, enum probearg arg)
5248 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
5249 KBDC kbdc = sc->kbdc;
5255 id1 = get_aux_id(kbdc);
5256 if (set_mouse_sampling_rate(kbdc, 10) != 10)
5259 * The device is now in the native mode? It returns a different
5262 id2 = get_aux_id(kbdc);
5263 if ((id1 == id2) || (id2 != 2))
5266 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
5269 /* at this point, resolution is LOW, sampling rate is 10/sec */
5270 if (get_mouse_status(kbdc, status, 0, 3) < 3)
5275 * The special sequence to enable the third and fourth buttons.
5276 * Otherwise they behave like the first and second buttons.
5278 for (i = 0; i < nitems(rate); ++i)
5279 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5283 * At this point, the device is using default resolution and
5284 * sampling rate for the native mode.
5286 if (get_mouse_status(kbdc, status, 0, 3) < 3)
5288 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
5291 /* the device appears be enabled by this sequence, diable it for now */
5292 disable_aux_dev(kbdc);
5293 empty_aux_buffer(kbdc, 5);
5298 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
5300 enable_mmanplus(struct psm_softc *sc, enum probearg arg)
5302 KBDC kbdc = sc->kbdc;
5305 /* the special sequence to enable the fourth button and the roller. */
5307 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
5308 * must be called exactly three times since the last RESET command
5309 * before this sequence. XXX
5311 if (!set_mouse_scaling(kbdc, 1))
5313 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
5315 if (get_mouse_status(kbdc, data, 1, 3) < 3)
5319 * PS2++ protocol, packet type 0
5321 * b7 b6 b5 b4 b3 b2 b1 b0
5322 * byte 1: * 1 p3 p2 1 * * *
5323 * byte 2: 1 1 p1 p0 m1 m0 1 0
5324 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
5326 * p3-p0: packet type: 0
5327 * m7-m0: model ID: MouseMan+:0x50,
5329 * ScrollPoint:0x58...
5331 /* check constant bits */
5332 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
5334 if ((data[1] & 0xc3) != 0xc2)
5336 /* check d3-d0 in byte 2 */
5337 if (!MOUSE_PS2PLUS_CHECKBITS(data))
5340 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
5344 sc->hw.hwid &= 0x00ff;
5345 sc->hw.hwid |= data[2] << 8; /* save model ID */
5349 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
5350 * the wheel and the fourth button events are encoded in the
5351 * special data packet. The mouse may be put in the IntelliMouse mode
5352 * if it is initialized by the IntelliMouse's method.
5357 /* MS IntelliMouse Explorer */
5359 enable_msexplorer(struct psm_softc *sc, enum probearg arg)
5361 KBDC kbdc = sc->kbdc;
5362 static u_char rate0[] = { 200, 100, 80, };
5363 static u_char rate1[] = { 200, 200, 80, };
5368 * This is needed for at least A4Tech X-7xx mice - they do not go
5369 * straight to Explorer mode, but need to be set to Intelli mode
5372 enable_msintelli(sc, arg);
5374 /* the special sequence to enable the extra buttons and the roller. */
5375 for (i = 0; i < nitems(rate1); ++i)
5376 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
5378 /* the device will give the genuine ID only after the above sequence */
5379 id = get_aux_id(kbdc);
5380 if (id != PSM_EXPLORER_ID)
5384 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
5389 * XXX: this is a kludge to fool some KVM switch products
5390 * which think they are clever enough to know the 4-byte IntelliMouse
5391 * protocol, and assume any other protocols use 3-byte packets.
5392 * They don't convey 4-byte data packets from the IntelliMouse Explorer
5393 * correctly to the host computer because of this!
5394 * The following sequence is actually IntelliMouse's "wake up"
5395 * sequence; it will make the KVM think the mouse is IntelliMouse
5396 * when it is in fact IntelliMouse Explorer.
5398 for (i = 0; i < nitems(rate0); ++i)
5399 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
5408 * Logitech MouseMan+ and FirstMouse+ will also respond to this
5409 * probe routine and act like IntelliMouse.
5412 enable_msintelli(struct psm_softc *sc, enum probearg arg)
5414 KBDC kbdc = sc->kbdc;
5415 static u_char rate[] = { 200, 100, 80, };
5419 /* the special sequence to enable the third button and the roller. */
5420 for (i = 0; i < nitems(rate); ++i)
5421 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5423 /* the device will give the genuine ID only after the above sequence */
5424 id = get_aux_id(kbdc);
5425 if (id != PSM_INTELLI_ID)
5438 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
5441 enable_4dmouse(struct psm_softc *sc, enum probearg arg)
5443 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5444 KBDC kbdc = sc->kbdc;
5448 for (i = 0; i < nitems(rate); ++i)
5449 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5451 id = get_aux_id(kbdc);
5453 * WinEasy 4D, 4 Way Scroll 4D: 6
5454 * Cable-Free 4D: 8 (4DPLUS)
5455 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
5457 if (id != PSM_4DMOUSE_ID)
5461 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
5470 * Newer wheel mice from A4 Tech seem to use this protocol.
5471 * Older models are recognized as either 4D Mouse or IntelliMouse.
5474 enable_4dplus(struct psm_softc *sc, enum probearg arg)
5476 KBDC kbdc = sc->kbdc;
5480 * enable_4dmouse() already issued the following ID sequence...
5481 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5484 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
5485 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5489 id = get_aux_id(kbdc);
5493 case PSM_4DPLUS_RFSW35_ID:
5500 sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
5507 /* Synaptics Touchpad */
5509 synaptics_sysctl(SYSCTL_HANDLER_ARGS)
5511 struct psm_softc *sc;
5514 if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 ||
5515 oidp->oid_arg2 > SYNAPTICS_SYSCTL_SOFTBUTTON3_X)
5518 sc = oidp->oid_arg1;
5520 /* Read the current value. */
5521 arg = *(int *)((char *)sc + oidp->oid_arg2);
5522 error = sysctl_handle_int(oidp, &arg, 0, req);
5525 if (error || !req->newptr)
5529 * Check that the new value is in the concerned node's range
5532 switch (oidp->oid_arg2) {
5533 case SYNAPTICS_SYSCTL_MIN_PRESSURE:
5534 case SYNAPTICS_SYSCTL_MAX_PRESSURE:
5535 if (arg < 0 || arg > 255)
5538 case SYNAPTICS_SYSCTL_MAX_WIDTH:
5539 if (arg < 4 || arg > 15)
5542 case SYNAPTICS_SYSCTL_MARGIN_TOP:
5543 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
5544 case SYNAPTICS_SYSCTL_NA_TOP:
5545 case SYNAPTICS_SYSCTL_NA_BOTTOM:
5546 if (arg < 0 || arg > sc->synhw.maximumYCoord)
5549 case SYNAPTICS_SYSCTL_SOFTBUTTON2_X:
5550 case SYNAPTICS_SYSCTL_SOFTBUTTON3_X:
5551 /* Softbuttons is clickpad only feature */
5552 if (!sc->synhw.capClickPad && arg != 0)
5555 case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
5556 case SYNAPTICS_SYSCTL_MARGIN_LEFT:
5557 case SYNAPTICS_SYSCTL_NA_RIGHT:
5558 case SYNAPTICS_SYSCTL_NA_LEFT:
5559 if (arg < 0 || arg > sc->synhw.maximumXCoord)
5562 case SYNAPTICS_SYSCTL_WINDOW_MIN:
5563 case SYNAPTICS_SYSCTL_WINDOW_MAX:
5564 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
5565 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
5568 case SYNAPTICS_SYSCTL_MULTIPLICATOR:
5569 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
5570 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
5571 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
5572 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
5573 case SYNAPTICS_SYSCTL_DIV_MIN:
5574 case SYNAPTICS_SYSCTL_DIV_MAX:
5575 case SYNAPTICS_SYSCTL_DIV_MAX_NA:
5576 case SYNAPTICS_SYSCTL_DIV_LEN:
5577 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
5578 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
5582 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
5583 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
5584 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
5588 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
5589 if (arg < -sc->synhw.maximumXCoord ||
5590 arg > sc->synhw.maximumXCoord)
5593 case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y:
5594 /* Softbuttons is clickpad only feature */
5595 if (!sc->synhw.capClickPad && arg != 0)
5598 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
5599 if (arg < -sc->synhw.maximumYCoord ||
5600 arg > sc->synhw.maximumYCoord)
5603 case SYNAPTICS_SYSCTL_TOUCHPAD_OFF:
5604 if (arg < 0 || arg > 1)
5612 *(int *)((char *)sc + oidp->oid_arg2) = arg;
5618 synaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc)
5621 * Set predefined sizes for softbuttons.
5622 * Values are taken to match HP Pavilion dv6 clickpad drawings
5623 * with thin middle softbutton placed on separator
5626 /* hw.psm.synaptics.softbuttons_y */
5627 sc->syninfo.softbuttons_y = 1700;
5628 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5629 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5630 "softbuttons_y", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5631 sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y,
5632 synaptics_sysctl, "I",
5633 "Vertical size of softbuttons area");
5635 /* hw.psm.synaptics.softbutton2_x */
5636 sc->syninfo.softbutton2_x = 3100;
5637 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5638 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5639 "softbutton2_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5640 sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X,
5641 synaptics_sysctl, "I",
5642 "Horisontal position of 2-nd softbutton left edge (0-disable)");
5644 /* hw.psm.synaptics.softbutton3_x */
5645 sc->syninfo.softbutton3_x = 3900;
5646 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5647 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5648 "softbutton3_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5649 sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X,
5650 synaptics_sysctl, "I",
5651 "Horisontal position of 3-rd softbutton left edge (0-disable)");
5655 synaptics_sysctl_create_tree(struct psm_softc *sc, const char *name,
5659 if (sc->syninfo.sysctl_tree != NULL)
5662 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
5663 sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
5664 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
5665 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name, CTLFLAG_RD,
5668 /* hw.psm.synaptics.directional_scrolls. */
5669 sc->syninfo.directional_scrolls = 0;
5670 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5671 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5672 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
5673 &sc->syninfo.directional_scrolls, 0,
5674 "Enable hardware scrolling pad (if non-zero) or register it as "
5675 "extended buttons (if 0)");
5677 /* hw.psm.synaptics.max_x. */
5678 sc->syninfo.max_x = 6143;
5679 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5680 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5681 "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY,
5682 &sc->syninfo.max_x, 0,
5683 "Horizontal reporting range");
5685 /* hw.psm.synaptics.max_y. */
5686 sc->syninfo.max_y = 6143;
5687 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5688 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5689 "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY,
5690 &sc->syninfo.max_y, 0,
5691 "Vertical reporting range");
5694 * Turn off two finger scroll if we have a
5695 * physical area reserved for scrolling or when
5696 * there's no multi finger support.
5698 if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 &&
5699 sc->synhw.capAdvancedGestures == 0))
5700 sc->syninfo.two_finger_scroll = 0;
5702 sc->syninfo.two_finger_scroll = 1;
5703 /* hw.psm.synaptics.two_finger_scroll. */
5704 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5705 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5706 "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY,
5707 &sc->syninfo.two_finger_scroll, 0,
5708 "Enable two finger scrolling");
5710 /* hw.psm.synaptics.min_pressure. */
5711 sc->syninfo.min_pressure = 32;
5712 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5713 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5714 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5715 sc, SYNAPTICS_SYSCTL_MIN_PRESSURE,
5716 synaptics_sysctl, "I",
5717 "Minimum pressure required to start an action");
5719 /* hw.psm.synaptics.max_pressure. */
5720 sc->syninfo.max_pressure = 220;
5721 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5722 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5723 "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5724 sc, SYNAPTICS_SYSCTL_MAX_PRESSURE,
5725 synaptics_sysctl, "I",
5726 "Maximum pressure to detect palm");
5728 /* hw.psm.synaptics.max_width. */
5729 sc->syninfo.max_width = 10;
5730 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5731 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5732 "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5733 sc, SYNAPTICS_SYSCTL_MAX_WIDTH,
5734 synaptics_sysctl, "I",
5735 "Maximum finger width to detect palm");
5737 /* hw.psm.synaptics.top_margin. */
5738 sc->syninfo.margin_top = 200;
5739 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5740 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5741 "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5742 sc, SYNAPTICS_SYSCTL_MARGIN_TOP,
5743 synaptics_sysctl, "I",
5746 /* hw.psm.synaptics.right_margin. */
5747 sc->syninfo.margin_right = 200;
5748 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5749 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5750 "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5751 sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
5752 synaptics_sysctl, "I",
5755 /* hw.psm.synaptics.bottom_margin. */
5756 sc->syninfo.margin_bottom = 200;
5757 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5758 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5759 "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5760 sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
5761 synaptics_sysctl, "I",
5764 /* hw.psm.synaptics.left_margin. */
5765 sc->syninfo.margin_left = 200;
5766 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5767 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5768 "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5769 sc, SYNAPTICS_SYSCTL_MARGIN_LEFT,
5770 synaptics_sysctl, "I",
5773 /* hw.psm.synaptics.na_top. */
5774 sc->syninfo.na_top = 1783;
5775 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5776 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5777 "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5778 sc, SYNAPTICS_SYSCTL_NA_TOP,
5779 synaptics_sysctl, "I",
5780 "Top noisy area, where weight_previous_na is used instead "
5781 "of weight_previous");
5783 /* hw.psm.synaptics.na_right. */
5784 sc->syninfo.na_right = 563;
5785 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5786 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5787 "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5788 sc, SYNAPTICS_SYSCTL_NA_RIGHT,
5789 synaptics_sysctl, "I",
5790 "Right noisy area, where weight_previous_na is used instead "
5791 "of weight_previous");
5793 /* hw.psm.synaptics.na_bottom. */
5794 sc->syninfo.na_bottom = 1408;
5795 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5796 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5797 "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5798 sc, SYNAPTICS_SYSCTL_NA_BOTTOM,
5799 synaptics_sysctl, "I",
5800 "Bottom noisy area, where weight_previous_na is used instead "
5801 "of weight_previous");
5803 /* hw.psm.synaptics.na_left. */
5804 sc->syninfo.na_left = 1600;
5805 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5806 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5807 "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5808 sc, SYNAPTICS_SYSCTL_NA_LEFT,
5809 synaptics_sysctl, "I",
5810 "Left noisy area, where weight_previous_na is used instead "
5811 "of weight_previous");
5813 /* hw.psm.synaptics.window_min. */
5814 sc->syninfo.window_min = 4;
5815 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5816 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5817 "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5818 sc, SYNAPTICS_SYSCTL_WINDOW_MIN,
5819 synaptics_sysctl, "I",
5820 "Minimum window size to start an action");
5822 /* hw.psm.synaptics.window_max. */
5823 sc->syninfo.window_max = 10;
5824 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5825 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5826 "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5827 sc, SYNAPTICS_SYSCTL_WINDOW_MAX,
5828 synaptics_sysctl, "I",
5829 "Maximum window size");
5831 /* hw.psm.synaptics.multiplicator. */
5832 sc->syninfo.multiplicator = 10000;
5833 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5834 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5835 "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5836 sc, SYNAPTICS_SYSCTL_MULTIPLICATOR,
5837 synaptics_sysctl, "I",
5838 "Multiplicator to increase precision in averages and divisions");
5840 /* hw.psm.synaptics.weight_current. */
5841 sc->syninfo.weight_current = 3;
5842 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5843 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5844 "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5845 sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
5846 synaptics_sysctl, "I",
5847 "Weight of the current movement in the new average");
5849 /* hw.psm.synaptics.weight_previous. */
5850 sc->syninfo.weight_previous = 6;
5851 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5852 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5853 "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5854 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
5855 synaptics_sysctl, "I",
5856 "Weight of the previous average");
5858 /* hw.psm.synaptics.weight_previous_na. */
5859 sc->syninfo.weight_previous_na = 20;
5860 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5861 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5862 "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5863 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
5864 synaptics_sysctl, "I",
5865 "Weight of the previous average (inside the noisy area)");
5867 /* hw.psm.synaptics.weight_len_squared. */
5868 sc->syninfo.weight_len_squared = 2000;
5869 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5870 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5871 "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5872 sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
5873 synaptics_sysctl, "I",
5874 "Length (squared) of segments where weight_previous "
5875 "starts to decrease");
5877 /* hw.psm.synaptics.div_min. */
5878 sc->syninfo.div_min = 9;
5879 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5880 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5881 "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5882 sc, SYNAPTICS_SYSCTL_DIV_MIN,
5883 synaptics_sysctl, "I",
5884 "Divisor for fast movements");
5886 /* hw.psm.synaptics.div_max. */
5887 sc->syninfo.div_max = 17;
5888 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5889 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5890 "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5891 sc, SYNAPTICS_SYSCTL_DIV_MAX,
5892 synaptics_sysctl, "I",
5893 "Divisor for slow movements");
5895 /* hw.psm.synaptics.div_max_na. */
5896 sc->syninfo.div_max_na = 30;
5897 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5898 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5899 "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5900 sc, SYNAPTICS_SYSCTL_DIV_MAX_NA,
5901 synaptics_sysctl, "I",
5902 "Divisor with slow movements (inside the noisy area)");
5904 /* hw.psm.synaptics.div_len. */
5905 sc->syninfo.div_len = 100;
5906 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5907 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5908 "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5909 sc, SYNAPTICS_SYSCTL_DIV_LEN,
5910 synaptics_sysctl, "I",
5911 "Length of segments where div_max starts to decrease");
5913 /* hw.psm.synaptics.tap_max_delta. */
5914 sc->syninfo.tap_max_delta = 80;
5915 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5916 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5917 "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5918 sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
5919 synaptics_sysctl, "I",
5920 "Length of segments above which a tap is ignored");
5922 /* hw.psm.synaptics.tap_min_queue. */
5923 sc->syninfo.tap_min_queue = 2;
5924 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5925 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5926 "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5927 sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
5928 synaptics_sysctl, "I",
5929 "Number of packets required to consider a tap");
5931 /* hw.psm.synaptics.taphold_timeout. */
5932 sc->gesture.in_taphold = 0;
5933 sc->syninfo.taphold_timeout = tap_timeout;
5934 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5935 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5936 "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5937 sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
5938 synaptics_sysctl, "I",
5939 "Maximum elapsed time between two taps to consider a tap-hold "
5942 /* hw.psm.synaptics.vscroll_hor_area. */
5943 sc->syninfo.vscroll_hor_area = 0; /* 1300 */
5944 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5945 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5946 "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5947 sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
5948 synaptics_sysctl, "I",
5949 "Area reserved for horizontal virtual scrolling");
5951 /* hw.psm.synaptics.vscroll_ver_area. */
5952 sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right;
5953 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5954 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5955 "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5956 sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
5957 synaptics_sysctl, "I",
5958 "Area reserved for vertical virtual scrolling");
5960 /* hw.psm.synaptics.vscroll_min_delta. */
5961 sc->syninfo.vscroll_min_delta = 50;
5962 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5963 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5964 "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5965 sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
5966 synaptics_sysctl, "I",
5967 "Minimum movement to consider virtual scrolling");
5969 /* hw.psm.synaptics.vscroll_div_min. */
5970 sc->syninfo.vscroll_div_min = 100;
5971 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5972 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5973 "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5974 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
5975 synaptics_sysctl, "I",
5976 "Divisor for fast scrolling");
5978 /* hw.psm.synaptics.vscroll_div_min. */
5979 sc->syninfo.vscroll_div_max = 150;
5980 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5981 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5982 "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5983 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
5984 synaptics_sysctl, "I",
5985 "Divisor for slow scrolling");
5987 /* hw.psm.synaptics.touchpad_off. */
5988 sc->syninfo.touchpad_off = 0;
5989 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5990 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5991 "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5992 sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF,
5993 synaptics_sysctl, "I",
5994 "Turn off touchpad");
5996 sc->syninfo.softbuttons_y = 0;
5997 sc->syninfo.softbutton2_x = 0;
5998 sc->syninfo.softbutton3_x = 0;
6000 /* skip softbuttons sysctl on not clickpads */
6001 if (sc->synhw.capClickPad)
6002 synaptics_sysctl_create_softbuttons_tree(sc);
6006 synaptics_preferred_mode(struct psm_softc *sc) {
6009 /* Check if we are in relative mode */
6010 if (sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
6011 if (tap_enabled == 0)
6013 * Disable tap & drag gestures. We use a Mode Byte
6014 * and set the DisGest bit (see §2.5 of Synaptics
6015 * TouchPad Interfacing Guide).
6020 * Enable tap & drag gestures. We use a Mode Byte
6021 * and clear the DisGest bit (see §2.5 of Synaptics
6022 * TouchPad Interfacing Guide).
6029 /* request wmode where available */
6030 if (sc->synhw.capExtended)
6037 synaptics_set_mode(struct psm_softc *sc, int mode_byte) {
6038 mouse_ext_command(sc->kbdc, mode_byte);
6040 /* "Commit" the Set Mode Byte command sent above. */
6041 set_mouse_sampling_rate(sc->kbdc, 20);
6044 * Enable advanced gestures mode if supported and we are not entering
6045 * passthrough or relative mode.
6047 if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) &&
6048 sc->hw.model == MOUSE_MODEL_SYNAPTICS && !(mode_byte & (1 << 5))) {
6049 mouse_ext_command(sc->kbdc, 3);
6050 set_mouse_sampling_rate(sc->kbdc, 0xc8);
6055 enable_synaptics(struct psm_softc *sc, enum probearg arg)
6058 struct psmcpnp_softc *psmcpnp_sc;
6059 KBDC kbdc = sc->kbdc;
6060 synapticshw_t synhw;
6062 int buttons, middle_byte;
6064 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
6067 * Just to be on the safe side: this avoids troubles with
6068 * following mouse_ext_command() when the previous command
6069 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
6070 * Synaptics Touchpad behaviour.
6072 set_mouse_scaling(kbdc, 1);
6074 /* Identify the Touchpad version. */
6075 if (mouse_ext_command(kbdc, 0) == 0)
6077 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6079 middle_byte = status[1];
6080 if (middle_byte != 0x46 && middle_byte != 0x47)
6083 bzero(&synhw, sizeof(synhw));
6084 synhw.infoMinor = status[0];
6085 synhw.infoMajor = status[2] & 0x0f;
6088 printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
6092 * Most synaptics touchpads return 0x47 in middle byte in responce to
6093 * identify command as stated in p.4.4 of "Synaptics PS/2 TouchPad
6094 * Interfacing Guide" and we only support v4.0 or better. But some
6095 * devices return 0x46 here and have a different numbering scheme.
6096 * In the case of 0x46, we allow versions as low as v2.0
6098 if ((middle_byte == 0x47 && synhw.infoMajor < 4) ||
6099 (middle_byte == 0x46 && synhw.infoMajor < 2)) {
6100 printf(" Unsupported (pre-v4) Touchpad detected\n");
6104 /* Get the Touchpad model information. */
6105 if (mouse_ext_command(kbdc, 3) == 0)
6107 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6109 if ((status[1] & 0x01) != 0) {
6110 printf(" Failed to read model information\n");
6114 synhw.infoRot180 = (status[0] & 0x80) != 0;
6115 synhw.infoPortrait = (status[0] & 0x40) != 0;
6116 synhw.infoSensor = status[0] & 0x3f;
6117 synhw.infoHardware = (status[1] & 0xfe) >> 1;
6118 synhw.infoNewAbs = (status[2] & 0x80) != 0;
6119 synhw.capPen = (status[2] & 0x40) != 0;
6120 synhw.infoSimplC = (status[2] & 0x20) != 0;
6121 synhw.infoGeometry = status[2] & 0x0f;
6124 printf(" Model information:\n");
6125 printf(" infoRot180: %d\n", synhw.infoRot180);
6126 printf(" infoPortrait: %d\n", synhw.infoPortrait);
6127 printf(" infoSensor: %d\n", synhw.infoSensor);
6128 printf(" infoHardware: %d\n", synhw.infoHardware);
6129 printf(" infoNewAbs: %d\n", synhw.infoNewAbs);
6130 printf(" capPen: %d\n", synhw.capPen);
6131 printf(" infoSimplC: %d\n", synhw.infoSimplC);
6132 printf(" infoGeometry: %d\n", synhw.infoGeometry);
6135 /* Read the extended capability bits. */
6136 if (mouse_ext_command(kbdc, 2) == 0)
6138 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6140 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != middle_byte) {
6141 printf(" Failed to read extended capability bits\n");
6145 psmcpnp = devclass_get_device(devclass_find(PSMCPNP_DRIVER_NAME),
6147 psmcpnp_sc = (psmcpnp != NULL) ? device_get_softc(psmcpnp) : NULL;
6150 * Set conservative defaults for 0x46 middle byte touchpads
6151 * as ExtendedQueries return bogus data.
6153 if (middle_byte == 0x46) {
6154 synhw.capExtended = 1;
6155 synhw.capPalmDetect = 1;
6156 synhw.capPassthrough = 1;
6157 synhw.capMultiFinger = 1;
6158 synhw.maximumXCoord = SYNAPTICS_DEFAULT_MAX_X;
6159 synhw.maximumYCoord = SYNAPTICS_DEFAULT_MAX_Y;
6160 synhw.minimumXCoord = SYNAPTICS_DEFAULT_MIN_X;
6161 synhw.minimumYCoord = SYNAPTICS_DEFAULT_MIN_Y;
6162 /* Enable multitouch mode for HW v8.1 devices */
6163 if (psmcpnp_sc != NULL &&
6164 psmcpnp_sc->type == PSMCPNP_HPSYN81)
6165 synhw.capReportsV = 1;
6167 synhw.capExtended = (status[0] & 0x80) != 0;
6169 /* Set the different capabilities when they exist. */
6171 if (synhw.capExtended && middle_byte == 0x47) {
6172 synhw.nExtendedQueries = (status[0] & 0x70) >> 4;
6173 synhw.capMiddle = (status[0] & 0x04) != 0;
6174 synhw.capPassthrough = (status[2] & 0x80) != 0;
6175 synhw.capLowPower = (status[2] & 0x40) != 0;
6176 synhw.capMultiFingerReport =
6177 (status[2] & 0x20) != 0;
6178 synhw.capSleep = (status[2] & 0x10) != 0;
6179 synhw.capFourButtons = (status[2] & 0x08) != 0;
6180 synhw.capBallistics = (status[2] & 0x04) != 0;
6181 synhw.capMultiFinger = (status[2] & 0x02) != 0;
6182 synhw.capPalmDetect = (status[2] & 0x01) != 0;
6184 if (!set_mouse_scaling(kbdc, 1))
6186 if (mouse_ext_command(kbdc, 0x08) == 0)
6188 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6191 if (status[0] != 0 && (status[1] & 0x80) && status[2] != 0) {
6192 synhw.infoXupmm = status[0];
6193 synhw.infoYupmm = status[2];
6197 printf(" Extended capabilities:\n");
6198 printf(" capExtended: %d\n", synhw.capExtended);
6199 printf(" capMiddle: %d\n", synhw.capMiddle);
6200 printf(" nExtendedQueries: %d\n",
6201 synhw.nExtendedQueries);
6202 printf(" capPassthrough: %d\n", synhw.capPassthrough);
6203 printf(" capLowPower: %d\n", synhw.capLowPower);
6204 printf(" capMultiFingerReport: %d\n",
6205 synhw.capMultiFingerReport);
6206 printf(" capSleep: %d\n", synhw.capSleep);
6207 printf(" capFourButtons: %d\n", synhw.capFourButtons);
6208 printf(" capBallistics: %d\n", synhw.capBallistics);
6209 printf(" capMultiFinger: %d\n", synhw.capMultiFinger);
6210 printf(" capPalmDetect: %d\n", synhw.capPalmDetect);
6211 printf(" infoXupmm: %d\n", synhw.infoXupmm);
6212 printf(" infoYupmm: %d\n", synhw.infoYupmm);
6216 * If nExtendedQueries is 1 or greater, then the TouchPad
6217 * supports this number of extended queries. We can load
6218 * more information about buttons using query 0x09.
6220 if (synhw.nExtendedQueries >= 1) {
6221 if (!set_mouse_scaling(kbdc, 1))
6223 if (mouse_ext_command(kbdc, 0x09) == 0)
6225 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6227 synhw.verticalScroll = (status[0] & 0x01) != 0;
6228 synhw.horizontalScroll = (status[0] & 0x02) != 0;
6229 synhw.verticalWheel = (status[0] & 0x08) != 0;
6230 synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
6231 synhw.capEWmode = (status[0] & 0x04) != 0;
6233 printf(" Extended model ID:\n");
6234 printf(" verticalScroll: %d\n",
6235 synhw.verticalScroll);
6236 printf(" horizontalScroll: %d\n",
6237 synhw.horizontalScroll);
6238 printf(" verticalWheel: %d\n",
6239 synhw.verticalWheel);
6240 printf(" nExtendedButtons: %d\n",
6241 synhw.nExtendedButtons);
6242 printf(" capEWmode: %d\n",
6246 * Add the number of extended buttons to the total
6247 * button support count, including the middle button
6248 * if capMiddle support bit is set.
6250 buttons = synhw.nExtendedButtons + synhw.capMiddle;
6253 * If the capFourButtons support bit is set,
6254 * add a fourth button to the total button count.
6256 buttons = synhw.capFourButtons ? 1 : 0;
6258 /* Read the continued capabilities bits. */
6259 if (synhw.nExtendedQueries >= 4) {
6260 if (!set_mouse_scaling(kbdc, 1))
6262 if (mouse_ext_command(kbdc, 0x0c) == 0)
6264 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6267 synhw.capClickPad = (status[1] & 0x01) << 1;
6268 synhw.capClickPad |= (status[0] & 0x10) != 0;
6269 synhw.capDeluxeLEDs = (status[1] & 0x02) != 0;
6270 synhw.noAbsoluteFilter = (status[1] & 0x04) != 0;
6271 synhw.capReportsV = (status[1] & 0x08) != 0;
6272 synhw.capUniformClickPad = (status[1] & 0x10) != 0;
6273 synhw.capReportsMin = (status[1] & 0x20) != 0;
6274 synhw.capInterTouch = (status[1] & 0x40) != 0;
6275 synhw.capReportsMax = (status[0] & 0x02) != 0;
6276 synhw.capClearPad = (status[0] & 0x04) != 0;
6277 synhw.capAdvancedGestures = (status[0] & 0x08) != 0;
6278 synhw.capCoveredPad = (status[0] & 0x80) != 0;
6280 if (synhw.capReportsMax) {
6281 if (!set_mouse_scaling(kbdc, 1))
6283 if (mouse_ext_command(kbdc, 0x0d) == 0)
6285 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6288 synhw.maximumXCoord = (status[0] << 5) |
6289 ((status[1] & 0x0f) << 1);
6290 synhw.maximumYCoord = (status[2] << 5) |
6291 ((status[1] & 0xf0) >> 3);
6293 synhw.maximumXCoord = SYNAPTICS_DEFAULT_MAX_X;
6294 synhw.maximumYCoord = SYNAPTICS_DEFAULT_MAX_Y;
6297 if (synhw.capReportsMin) {
6298 if (!set_mouse_scaling(kbdc, 1))
6300 if (mouse_ext_command(kbdc, 0x0f) == 0)
6302 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6305 synhw.minimumXCoord = (status[0] << 5) |
6306 ((status[1] & 0x0f) << 1);
6307 synhw.minimumYCoord = (status[2] << 5) |
6308 ((status[1] & 0xf0) >> 3);
6310 synhw.minimumXCoord = SYNAPTICS_DEFAULT_MIN_X;
6311 synhw.minimumYCoord = SYNAPTICS_DEFAULT_MIN_Y;
6315 * ClickPad properties are not exported through PS/2
6316 * protocol. Detection is based on controller's PnP ID.
6318 if (synhw.capClickPad && psmcpnp_sc != NULL) {
6319 switch (psmcpnp_sc->type) {
6320 case PSMCPNP_FORCEPAD:
6329 printf(" Continued capabilities:\n");
6330 printf(" capClickPad: %d\n",
6332 printf(" capDeluxeLEDs: %d\n",
6333 synhw.capDeluxeLEDs);
6334 printf(" noAbsoluteFilter: %d\n",
6335 synhw.noAbsoluteFilter);
6336 printf(" capReportsV: %d\n",
6338 printf(" capUniformClickPad: %d\n",
6339 synhw.capUniformClickPad);
6340 printf(" capReportsMin: %d\n",
6341 synhw.capReportsMin);
6342 printf(" capInterTouch: %d\n",
6343 synhw.capInterTouch);
6344 printf(" capReportsMax: %d\n",
6345 synhw.capReportsMax);
6346 printf(" capClearPad: %d\n",
6348 printf(" capAdvancedGestures: %d\n",
6349 synhw.capAdvancedGestures);
6350 printf(" capCoveredPad: %d\n",
6351 synhw.capCoveredPad);
6352 if (synhw.capReportsMax) {
6353 printf(" maximumXCoord: %d\n",
6354 synhw.maximumXCoord);
6355 printf(" maximumYCoord: %d\n",
6356 synhw.maximumYCoord);
6358 if (synhw.capReportsMin) {
6359 printf(" minimumXCoord: %d\n",
6360 synhw.minimumXCoord);
6361 printf(" minimumYCoord: %d\n",
6362 synhw.minimumYCoord);
6364 if (synhw.capClickPad) {
6365 printf(" forcePad: %d\n",
6369 buttons += synhw.capClickPad;
6374 if (synhw.capExtended)
6375 printf(" Additional Buttons: %d\n", buttons);
6377 printf(" No extended capabilities\n");
6381 * Add the default number of 3 buttons to the total
6382 * count of supported buttons reported above.
6387 * Read the mode byte.
6389 * XXX: Note the Synaptics documentation also defines the first
6390 * byte of the response to this query to be a constant 0x3b, this
6391 * does not appear to be true for Touchpads with guest devices.
6393 if (mouse_ext_command(kbdc, 1) == 0)
6395 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6397 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != middle_byte) {
6398 printf(" Failed to read mode byte\n");
6404 if (!synaptics_support)
6407 /* Set mouse type just now for synaptics_set_mode() */
6408 sc->hw.model = MOUSE_MODEL_SYNAPTICS;
6410 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6412 if (trackpoint_support && synhw.capPassthrough) {
6413 enable_trackpoint(sc, arg);
6416 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
6419 /* Create sysctl tree. */
6420 synaptics_sysctl_create_tree(sc, "synaptics",
6421 "Synaptics TouchPad");
6422 sc->hw.buttons = buttons;
6429 synaptics_passthrough_on(struct psm_softc *sc)
6431 VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n"));
6432 synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5));
6436 synaptics_passthrough_off(struct psm_softc *sc)
6438 VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n"));
6439 set_mouse_scaling(sc->kbdc, 2);
6440 set_mouse_scaling(sc->kbdc, 1);
6441 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6444 /* IBM/Lenovo TrackPoint */
6446 trackpoint_command(struct psm_softc *sc, int cmd, int loc, int val)
6448 const int seq[] = { 0xe2, cmd, loc, val };
6451 if (sc->synhw.capPassthrough)
6452 synaptics_passthrough_on(sc);
6454 for (i = 0; i < nitems(seq); i++) {
6455 if (sc->synhw.capPassthrough &&
6456 (seq[i] == 0xff || seq[i] == 0xe7))
6457 if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) {
6458 synaptics_passthrough_off(sc);
6461 if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) {
6462 if (sc->synhw.capPassthrough)
6463 synaptics_passthrough_off(sc);
6468 if (sc->synhw.capPassthrough)
6469 synaptics_passthrough_off(sc);
6474 #define PSM_TPINFO(x) offsetof(struct psm_softc, tpinfo.x)
6480 trackpoint_sysctl(SYSCTL_HANDLER_ARGS)
6482 static const int data[][3] = {
6483 { 0x00, 0x4a, PSM_TPINFO(sensitivity) },
6484 { 0x00, 0x4d, PSM_TPINFO(inertia) },
6485 { 0x00, 0x60, PSM_TPINFO(uplateau) },
6486 { 0x00, 0x57, PSM_TPINFO(reach) },
6487 { 0x00, 0x58, PSM_TPINFO(draghys) },
6488 { 0x00, 0x59, PSM_TPINFO(mindrag) },
6489 { 0x00, 0x5a, PSM_TPINFO(upthresh) },
6490 { 0x00, 0x5c, PSM_TPINFO(threshold) },
6491 { 0x00, 0x5d, PSM_TPINFO(jenks) },
6492 { 0x00, 0x5e, PSM_TPINFO(ztime) },
6493 { 0x01, 0x2c, PSM_TPINFO(pts) },
6494 { 0x08, 0x2d, PSM_TPINFO(skipback) }
6496 struct psm_softc *sc;
6497 int error, newval, *oldvalp;
6500 if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
6504 oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
6506 error = sysctl_handle_int(oidp, &newval, 0, req);
6509 if (newval == *oldvalp)
6511 if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
6513 error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47,
6514 tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
6523 trackpoint_sysctl_create_tree(struct psm_softc *sc)
6526 if (sc->tpinfo.sysctl_tree != NULL)
6529 /* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
6530 sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
6531 sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
6532 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD,
6533 0, "IBM/Lenovo TrackPoint");
6535 /* hw.psm.trackpoint.sensitivity */
6536 sc->tpinfo.sensitivity = 0x80;
6537 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6538 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6539 "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6540 sc, TRACKPOINT_SYSCTL_SENSITIVITY,
6541 trackpoint_sysctl, "I",
6544 /* hw.psm.trackpoint.negative_inertia */
6545 sc->tpinfo.inertia = 0x06;
6546 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6547 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6548 "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6549 sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
6550 trackpoint_sysctl, "I",
6551 "Negative inertia factor");
6553 /* hw.psm.trackpoint.upper_plateau */
6554 sc->tpinfo.uplateau = 0x61;
6555 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6556 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6557 "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6558 sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
6559 trackpoint_sysctl, "I",
6560 "Transfer function upper plateau speed");
6562 /* hw.psm.trackpoint.backup_range */
6563 sc->tpinfo.reach = 0x0a;
6564 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6565 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6566 "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6567 sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
6568 trackpoint_sysctl, "I",
6571 /* hw.psm.trackpoint.drag_hysteresis */
6572 sc->tpinfo.draghys = 0xff;
6573 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6574 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6575 "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6576 sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
6577 trackpoint_sysctl, "I",
6580 /* hw.psm.trackpoint.minimum_drag */
6581 sc->tpinfo.mindrag = 0x14;
6582 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6583 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6584 "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6585 sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
6586 trackpoint_sysctl, "I",
6589 /* hw.psm.trackpoint.up_threshold */
6590 sc->tpinfo.upthresh = 0xff;
6591 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6592 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6593 "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6594 sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
6595 trackpoint_sysctl, "I",
6596 "Up threshold for release");
6598 /* hw.psm.trackpoint.threshold */
6599 sc->tpinfo.threshold = 0x08;
6600 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6601 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6602 "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6603 sc, TRACKPOINT_SYSCTL_THRESHOLD,
6604 trackpoint_sysctl, "I",
6607 /* hw.psm.trackpoint.jenks_curvature */
6608 sc->tpinfo.jenks = 0x87;
6609 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6610 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6611 "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6612 sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
6613 trackpoint_sysctl, "I",
6616 /* hw.psm.trackpoint.z_time */
6617 sc->tpinfo.ztime = 0x26;
6618 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6619 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6620 "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6621 sc, TRACKPOINT_SYSCTL_Z_TIME,
6622 trackpoint_sysctl, "I",
6625 /* hw.psm.trackpoint.press_to_select */
6626 sc->tpinfo.pts = 0x00;
6627 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6628 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6629 "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6630 sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
6631 trackpoint_sysctl, "I",
6634 /* hw.psm.trackpoint.skip_backups */
6635 sc->tpinfo.skipback = 0x00;
6636 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6637 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6638 "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6639 sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
6640 trackpoint_sysctl, "I",
6641 "Skip backups from drags");
6645 set_trackpoint_parameters(struct psm_softc *sc)
6647 trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity);
6648 trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau);
6649 trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia);
6650 trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach);
6651 trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys);
6652 trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag);
6653 trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh);
6654 trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold);
6655 trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks);
6656 trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime);
6657 if (sc->tpinfo.pts == 0x01)
6658 trackpoint_command(sc, 0x47, 0x2c, 0x01);
6659 if (sc->tpinfo.skipback == 0x01)
6660 trackpoint_command(sc, 0x47, 0x2d, 0x08);
6664 enable_trackpoint(struct psm_softc *sc, enum probearg arg)
6666 KBDC kbdc = sc->kbdc;
6670 * If called from enable_synaptics(), make sure that passthrough
6671 * mode is enabled so we can reach the trackpoint.
6672 * However, passthrough mode must be disabled before setting the
6673 * trackpoint parameters, as rackpoint_command() enables and disables
6674 * passthrough mode on its own.
6676 if (sc->synhw.capPassthrough)
6677 synaptics_passthrough_on(sc);
6679 if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
6680 read_aux_data(kbdc) != 0x01)
6682 id = read_aux_data(kbdc);
6687 if (!trackpoint_support)
6690 if (sc->synhw.capPassthrough)
6691 synaptics_passthrough_off(sc);
6694 trackpoint_sysctl_create_tree(sc);
6696 * Don't overwrite hwid and buttons when we are
6699 if (!sc->synhw.capPassthrough) {
6705 set_trackpoint_parameters(sc);
6710 if (sc->synhw.capPassthrough)
6711 synaptics_passthrough_off(sc);
6716 /* Interlink electronics VersaPad */
6718 enable_versapad(struct psm_softc *sc, enum probearg arg)
6720 KBDC kbdc = sc->kbdc;
6723 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
6724 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
6725 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6726 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6727 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6728 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6729 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
6731 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
6733 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6735 return (TRUE); /* PS/2 absolute mode */
6738 /* Elantech Touchpad */
6740 elantech_read_1(KBDC kbdc, int hwversion, int reg, int *val)
6742 int res, readcmd, retidx;
6745 readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR;
6746 retidx = hwversion == 4 ? 1 : 0;
6748 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6749 res |= send_aux_command(kbdc, readcmd) != PSM_ACK;
6750 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6751 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6752 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6755 *val = resp[retidx];
6761 elantech_write_1(KBDC kbdc, int hwversion, int reg, int val)
6765 writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR;
6767 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6768 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6769 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6770 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6771 if (hwversion == 4) {
6772 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6773 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6775 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6776 res |= send_aux_command(kbdc, val) != PSM_ACK;
6777 res |= set_mouse_scaling(kbdc, 1) == 0;
6783 elantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp)
6787 if (hwversion == 2) {
6788 res = set_mouse_scaling(kbdc, 1) == 0;
6789 res |= mouse_ext_command(kbdc, cmd) == 0;
6791 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6792 res |= send_aux_command(kbdc, cmd) != PSM_ACK;
6794 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6800 elantech_init(KBDC kbdc, elantechhw_t *elanhw)
6802 int i, val, res, hwversion, reg10;
6804 /* set absolute mode */
6805 hwversion = elanhw->hwversion;
6807 switch (hwversion) {
6809 reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4;
6810 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6813 res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A);
6817 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6820 res = elantech_write_1(kbdc, hwversion, 0x07, 0x01);
6826 /* Read back reg 0x10 to ensure hardware is ready. */
6827 if (res == 0 && reg10 >= 0) {
6828 for (i = 0; i < 5; i++) {
6829 if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0)
6838 printf("couldn't set absolute mode\n");
6844 elantech_init_synaptics(struct psm_softc *sc)
6847 /* Set capabilites required by movement smother */
6848 sc->synhw.infoMajor = sc->elanhw.hwversion;
6849 sc->synhw.infoMinor = sc->elanhw.fwversion;
6850 sc->synhw.infoXupmm = sc->elanhw.dpmmx;
6851 sc->synhw.infoYupmm = sc->elanhw.dpmmy;
6852 sc->synhw.verticalScroll = 0;
6853 sc->synhw.nExtendedQueries = 4;
6854 sc->synhw.capExtended = 1;
6855 sc->synhw.capPassthrough = sc->elanhw.hastrackpoint;
6856 sc->synhw.capClickPad = sc->elanhw.isclickpad;
6857 sc->synhw.capMultiFinger = 1;
6858 if (sc->elanhw.issemimt)
6859 sc->synhw.capAdvancedGestures = 1;
6861 sc->synhw.capReportsV = 1;
6862 sc->synhw.capPalmDetect = 1;
6863 sc->synhw.capPen = 0;
6864 sc->synhw.capReportsMax = 1;
6865 sc->synhw.maximumXCoord = sc->elanhw.sizex;
6866 sc->synhw.maximumYCoord = sc->elanhw.sizey;
6867 sc->synhw.capReportsMin = 1;
6868 sc->synhw.minimumXCoord = 0;
6869 sc->synhw.minimumYCoord = 0;
6871 if (sc->syninfo.sysctl_tree == NULL) {
6872 synaptics_sysctl_create_tree(sc, "elantech",
6873 "Elantech Touchpad");
6876 * Adjust synaptic smoother tunables
6877 * 1. Disable finger detection pressure threshold. Unlike
6878 * synaptics we assume the finger is acting when packet with
6879 * its X&Y arrives not when pressure exceedes some threshold
6880 * 2. Disable unrelated features like margins and noisy areas
6881 * 3. Disable virtual scroll areas as 2nd finger is preferable
6882 * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized
6884 * 5. Scale down divisors and movement lengths by a factor of 3
6885 * where 3 is Synaptics to Elantech (~2200/800) dpi ratio
6888 /* Set reporting range to be equal touchpad size */
6889 sc->syninfo.max_x = sc->elanhw.sizex;
6890 sc->syninfo.max_y = sc->elanhw.sizey;
6892 /* Disable finger detection pressure threshold */
6893 sc->syninfo.min_pressure = 1;
6895 /* Adjust palm width to nearly match synaptics w=10 */
6896 sc->syninfo.max_width = 7;
6898 /* Elans often report double & triple taps as single event */
6899 sc->syninfo.tap_min_queue = 1;
6901 /* Use full area of touchpad */
6902 sc->syninfo.margin_top = 0;
6903 sc->syninfo.margin_right = 0;
6904 sc->syninfo.margin_bottom = 0;
6905 sc->syninfo.margin_left = 0;
6907 /* Disable noisy area */
6908 sc->syninfo.na_top = 0;
6909 sc->syninfo.na_right = 0;
6910 sc->syninfo.na_bottom = 0;
6911 sc->syninfo.na_left = 0;
6913 /* Tune divisors and movement lengths */
6914 sc->syninfo.weight_len_squared = 200;
6915 sc->syninfo.div_min = 3;
6916 sc->syninfo.div_max = 6;
6917 sc->syninfo.div_max_na = 10;
6918 sc->syninfo.div_len = 30;
6919 sc->syninfo.tap_max_delta = 25;
6921 /* Disable virtual scrolling areas and tune its divisors */
6922 sc->syninfo.vscroll_hor_area = 0;
6923 sc->syninfo.vscroll_ver_area = 0;
6924 sc->syninfo.vscroll_min_delta = 15;
6925 sc->syninfo.vscroll_div_min = 30;
6926 sc->syninfo.vscroll_div_max = 50;
6928 /* Set bottom quarter as 42% - 16% - 42% sized softbuttons */
6929 if (sc->elanhw.isclickpad) {
6930 sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4;
6931 sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25;
6932 sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25;
6940 enable_elantech(struct psm_softc *sc, enum probearg arg)
6942 static const int ic2hw[] =
6943 /*IC: 0 1 2 3 4 5 6 7 8 9 a b c d e f */
6944 { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0 };
6945 static const int fw_sizes[][3] = {
6946 /* FW.vers MaxX MaxY */
6947 { 0x020030, 1152, 768 },
6948 { 0x020800, 1152, 768 },
6949 { 0x020b00, 1152, 768 },
6950 { 0x040215, 900, 500 },
6951 { 0x040216, 819, 405 },
6952 { 0x040219, 900, 500 },
6954 elantechhw_t elanhw;
6955 int icversion, hwversion, xtr, i, id, resp[3], dpix, dpiy;
6956 KBDC kbdc = sc->kbdc;
6958 VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n"));
6960 set_mouse_scaling(kbdc, 1);
6961 set_mouse_scaling(kbdc, 1);
6962 set_mouse_scaling(kbdc, 1);
6963 if (get_mouse_status(kbdc, resp, 0, 3) != 3)
6966 if (!ELANTECH_MAGIC(resp))
6969 /* Identify the Touchpad version. */
6970 if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp))
6973 bzero(&elanhw, sizeof(elanhw));
6975 elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2];
6976 icversion = resp[0] & 0x0f;
6977 hwversion = ic2hw[icversion];
6980 printf("Elantech touchpad hardware v.%d firmware v.0x%06x\n",
6981 hwversion, elanhw.fwversion);
6983 if (ELANTECH_HW_IS_V1(elanhw.fwversion)) {
6984 printf (" Unsupported touchpad hardware (v1)\n");
6987 if (hwversion == 0) {
6988 printf (" Unknown touchpad hardware (firmware v.0x%06x)\n",
6993 /* Get the Touchpad model information. */
6994 elanhw.hwversion = hwversion;
6995 elanhw.issemimt = hwversion == 2;
6996 elanhw.isclickpad = (resp[1] & 0x10) != 0;
6997 elanhw.hascrc = (resp[1] & 0x40) != 0;
6998 elanhw.haspressure = elanhw.fwversion >= 0x020800;
7000 /* Read the capability bits. */
7001 if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) {
7002 printf(" Failed to read capability bits\n");
7006 elanhw.ntracesx = imax(resp[1], 3);
7007 elanhw.ntracesy = imax(resp[2], 3);
7008 elanhw.hastrackpoint = (resp[0] & 0x80) != 0;
7010 /* Get the touchpad resolution */
7011 switch (hwversion) {
7013 if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp)
7015 dpix = (resp[1] & 0x0f) * 10 + 790;
7016 dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790;
7017 elanhw.dpmmx = (dpix * 10 + 5) / 254;
7018 elanhw.dpmmy = (dpiy * 10 + 5) / 254;
7024 elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */
7028 if (!elantech_support)
7031 if (elantech_init(kbdc, &elanhw)) {
7032 printf("couldn't initialize elantech touchpad\n");
7037 * Get the touchpad reporting range.
7038 * On HW v.3 touchpads it should be done after switching hardware
7039 * to real resolution mode (by setting bit 3 of reg10)
7041 elanhw.dptracex = elanhw.dptracey = 64;
7042 for (i = 0; i < nitems(fw_sizes); i++) {
7043 if (elanhw.fwversion == fw_sizes[i][0]) {
7044 elanhw.sizex = fw_sizes[i][1];
7045 elanhw.sizey = fw_sizes[i][2];
7049 if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) {
7050 printf(" Failed to read touchpad size\n");
7051 elanhw.sizex = 10000; /* Arbitrary high values to */
7052 elanhw.sizey = 10000; /* prevent clipping in smoother */
7053 } else if (hwversion == 2) {
7054 if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) &&
7055 !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) {
7056 elanhw.dptracex = resp[1] / 2;
7057 elanhw.dptracey = resp[2] / 2;
7059 xtr = ((elanhw.fwversion >> 8) == 0x0208) ? 1 : 2;
7060 elanhw.sizex = (elanhw.ntracesx - xtr) * elanhw.dptracex;
7061 elanhw.sizey = (elanhw.ntracesy - xtr) * elanhw.dptracey;
7063 elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1];
7064 elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2];
7065 xtr = (elanhw.sizex % (elanhw.ntracesx - 2) == 0) ? 2 : 1;
7066 elanhw.dptracex = elanhw.sizex / (elanhw.ntracesx - xtr);
7067 elanhw.dptracey = elanhw.sizey / (elanhw.ntracesy - xtr);
7071 printf(" Model information:\n");
7072 printf(" MaxX: %d\n", elanhw.sizex);
7073 printf(" MaxY: %d\n", elanhw.sizey);
7074 printf(" DpmmX: %d\n", elanhw.dpmmx);
7075 printf(" DpmmY: %d\n", elanhw.dpmmy);
7076 printf(" TracesX: %d\n", elanhw.ntracesx);
7077 printf(" TracesY: %d\n", elanhw.ntracesy);
7078 printf(" DptraceX: %d\n", elanhw.dptracex);
7079 printf(" DptraceY: %d\n", elanhw.dptracey);
7080 printf(" SemiMT: %d\n", elanhw.issemimt);
7081 printf(" Clickpad: %d\n", elanhw.isclickpad);
7082 printf(" Trackpoint: %d\n", elanhw.hastrackpoint);
7083 printf(" CRC: %d\n", elanhw.hascrc);
7084 printf(" Pressure: %d\n", elanhw.haspressure);
7087 VLOG(3, (LOG_DEBUG, "elantech: END init\n"));
7090 sc->elanhw = elanhw;
7093 /* Initialize synaptics movement smoother */
7094 elantech_init_synaptics(sc);
7096 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
7097 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
7104 * Return true if 'now' is earlier than (start + (secs.usecs)).
7105 * Now may be NULL and the function will fetch the current time from
7106 * getmicrouptime(), or a cached 'now' can be passed in.
7107 * All values should be numbers derived from getmicrouptime().
7110 timeelapsed(start, secs, usecs, now)
7111 const struct timeval *start, *now;
7114 struct timeval snow, tv;
7116 /* if there is no 'now' passed in, the get it as a convience. */
7118 getmicrouptime(&snow);
7124 timevaladd(&tv, start);
7125 return (timevalcmp(&tv, now, <));
7129 psmresume(device_t dev)
7131 struct psm_softc *sc = device_get_softc(dev);
7132 int unit = device_get_unit(dev);
7135 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
7138 (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
7141 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
7143 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
7145 * Release the blocked process; it must be notified that
7146 * the device cannot be accessed anymore.
7148 sc->state &= ~PSM_ASLP;
7152 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
7157 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
7158 #ifdef EVDEV_SUPPORT
7159 MODULE_DEPEND(psm, evdev, 1, 1, 1);
7165 * This sucks up assignments from PNPBIOS and ACPI.
7169 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
7170 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
7171 * can be probed and attached only after the AT keyboard controller is
7172 * attached, we shall quietly reserve the IRQ resource for later use.
7173 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
7174 * copy the IRQ resource to the PS/2 mouse device instance hanging
7175 * under the keyboard controller, then probe and attach it.
7178 static devclass_t psmcpnp_devclass;
7180 static device_probe_t psmcpnp_probe;
7181 static device_attach_t psmcpnp_attach;
7183 static device_method_t psmcpnp_methods[] = {
7184 DEVMETHOD(device_probe, psmcpnp_probe),
7185 DEVMETHOD(device_attach, psmcpnp_attach),
7190 static driver_t psmcpnp_driver = {
7191 PSMCPNP_DRIVER_NAME,
7193 sizeof(struct psmcpnp_softc),
7196 static struct isa_pnp_id psmcpnp_ids[] = {
7197 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
7198 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */
7199 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */
7200 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
7201 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
7202 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
7203 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */
7204 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
7205 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
7206 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
7207 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
7208 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
7209 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
7213 /* _HID list for quirk detection. Any device below has _CID from psmcpnp_ids */
7214 static struct isa_pnp_id forcepad_ids[] = {
7215 { 0x0d302e4f, "HP PS/2 forcepad port" }, /* SYN300D, EB 1040 */
7216 { 0x14302e4f, "HP PS/2 forcepad port" }, /* SYN3014, EB 1040 */
7220 /* List of HW v8.1 synaptics touchpads erroneously detected as HW v2.0 */
7221 static struct isa_pnp_id hpsyn81_ids[] = {
7222 { 0x9e012e4f, "HP PS/2 trackpad port" }, /* SYN019E, EB 9470 */
7227 create_a_copy(device_t atkbdc, device_t me)
7232 /* find the PS/2 mouse device instance under the keyboard controller */
7233 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
7234 device_get_unit(atkbdc));
7237 if (device_get_state(psm) != DS_NOTPRESENT)
7240 /* move our resource to the found device */
7241 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
7242 bus_delete_resource(me, SYS_RES_IRQ, 0);
7243 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
7245 /* ...then probe and attach it */
7246 return (device_probe_and_attach(psm));
7250 psmcpnp_probe(device_t dev)
7252 struct psmcpnp_softc *sc = device_get_softc(dev);
7253 struct resource *res;
7257 if (ISA_PNP_PROBE(device_get_parent(dev), dev, forcepad_ids) == 0)
7258 sc->type = PSMCPNP_FORCEPAD;
7259 else if(ISA_PNP_PROBE(device_get_parent(dev), dev, hpsyn81_ids) == 0)
7260 sc->type = PSMCPNP_HPSYN81;
7261 else if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids) == 0)
7262 sc->type = PSMCPNP_GENERIC;
7267 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
7268 * to the PS/2 mouse device node. But, some buggy PnP BIOS
7269 * declares the PS/2 mouse device node without an IRQ resource!
7270 * If this happens, we shall refer to device hints.
7271 * If we still don't find it there, use a hardcoded value... XXX
7274 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
7276 if (resource_long_value(PSM_DRIVER_NAME,
7277 device_get_unit(dev),"irq", &irq) != 0)
7279 device_printf(dev, "irq resource info is missing; "
7280 "assuming irq %ld\n", irq);
7281 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
7283 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0);
7284 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
7290 return ((res == NULL) ? ENXIO : 0);
7294 psmcpnp_attach(device_t dev)
7298 /* find the keyboard controller, which may be on acpi* or isa* bus */
7299 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
7300 device_get_unit(dev));
7301 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
7302 create_a_copy(atkbdc, dev);
7307 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
7308 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
7309 ISA_PNP_INFO(psmcpnp_ids);
7310 #endif /* DEV_ISA */