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>
75 #include <sys/mutex.h>
77 #include <sys/sigio.h>
78 #include <sys/signalvar.h>
79 #include <sys/syslog.h>
80 #include <machine/bus.h>
82 #include <sys/selinfo.h>
83 #include <sys/sysctl.h>
87 #include <sys/limits.h>
88 #include <sys/mouse.h>
89 #include <machine/resource.h>
92 #include <isa/isavar.h>
96 #include <dev/evdev/evdev.h>
97 #include <dev/evdev/input.h>
100 #include <dev/atkbdc/atkbdcreg.h>
101 #include <dev/atkbdc/psm.h>
104 * Driver specific options: the following options may be set by
105 * `options' statements in the kernel configuration file.
110 #define PSM_DEBUG 0 /*
111 * logging: 0: none, 1: brief, 2: verbose
112 * 3: sync errors, 4: all packets
115 #define VLOG(level, args) do { \
116 if (verbose >= level) \
120 #ifndef PSM_INPUT_TIMEOUT
121 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
124 #ifndef PSM_TAP_TIMEOUT
125 #define PSM_TAP_TIMEOUT 125000
128 #ifndef PSM_TAP_THRESHOLD
129 #define PSM_TAP_THRESHOLD 25
132 /* end of driver specific options */
134 #define PSMCPNP_DRIVER_NAME "psmcpnp"
136 struct psmcpnp_softc {
140 PSMCPNP_TOPBUTTONPAD,
141 } type; /* Based on PnP ID */
145 #define PSM_BUFSIZE 960
146 #define PSM_SMALLBUFSIZE 240
148 /* operation levels */
149 #define PSM_LEVEL_BASE 0
150 #define PSM_LEVEL_STANDARD 1
151 #define PSM_LEVEL_NATIVE 2
152 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
153 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
155 /* Active PS/2 multiplexing */
156 #define PSM_NOMUX (-1)
158 /* Logitech PS2++ protocol */
159 #define MOUSE_PS2PLUS_CHECKBITS(b) \
160 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
161 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
162 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
165 typedef struct ringbuf {
166 int count; /* # of valid elements in the buffer */
167 int head; /* head pointer */
168 int tail; /* tail poiner */
169 u_char buf[PSM_BUFSIZE];
173 typedef struct packetbuf {
174 u_char ipacket[16]; /* interim input buffer */
175 int inputbytes; /* # of bytes in the input buffer */
178 #ifndef PSM_PACKETQUEUE
179 #define PSM_PACKETQUEUE 128
183 * Synaptics command definitions.
185 #define SYNAPTICS_READ_IDENTITY 0x00
186 #define SYNAPTICS_READ_MODES 0x01
187 #define SYNAPTICS_READ_CAPABILITIES 0x02
188 #define SYNAPTICS_READ_MODEL_ID 0x03
189 #define SYNAPTICS_READ_SERIAL_PREFIX 0x06
190 #define SYNAPTICS_READ_SERIAL_SUFFIX 0x07
191 #define SYNAPTICS_READ_RESOLUTIONS 0x08
192 #define SYNAPTICS_READ_EXTENDED 0x09
193 #define SYNAPTICS_READ_CAPABILITIES_CONT 0x0c
194 #define SYNAPTICS_READ_MAX_COORDS 0x0d
195 #define SYNAPTICS_READ_DELUXE_LED 0x0e
196 #define SYNAPTICS_READ_MIN_COORDS 0x0f
198 typedef struct synapticsinfo {
199 struct sysctl_ctx_list sysctl_ctx;
200 struct sysctl_oid *sysctl_tree;
201 int directional_scrolls;
202 int two_finger_scroll;
219 int weight_previous_na;
220 int weight_len_squared;
228 int vscroll_ver_area;
229 int vscroll_hor_area;
230 int vscroll_min_delta;
239 int three_finger_drag;
243 typedef struct synapticspacket {
248 #define SYNAPTICS_PACKETQUEUE 10
249 #define SYNAPTICS_QUEUE_CURSOR(x) \
250 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
252 #define SYNAPTICS_VERSION_GE(synhw, major, minor) \
253 ((synhw).infoMajor > (major) || \
254 ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
256 typedef struct smoother {
257 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
270 typedef struct gesture {
276 int zmax; /* maximum pressure value */
277 struct timeval taptimeout; /* tap timeout for touchpads */
281 TRACKPOINT_SYSCTL_SENSITIVITY,
282 TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
283 TRACKPOINT_SYSCTL_UPPER_PLATEAU,
284 TRACKPOINT_SYSCTL_BACKUP_RANGE,
285 TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
286 TRACKPOINT_SYSCTL_MINIMUM_DRAG,
287 TRACKPOINT_SYSCTL_UP_THRESHOLD,
288 TRACKPOINT_SYSCTL_THRESHOLD,
289 TRACKPOINT_SYSCTL_JENKS_CURVATURE,
290 TRACKPOINT_SYSCTL_Z_TIME,
291 TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
292 TRACKPOINT_SYSCTL_SKIP_BACKUPS
295 typedef struct trackpointinfo {
296 struct sysctl_ctx_list sysctl_ctx;
297 struct sysctl_oid *sysctl_tree;
312 typedef struct finger {
319 #define PSM_FINGERS 2 /* # of processed fingers */
320 #define PSM_FINGER_IS_PEN (1<<0)
321 #define PSM_FINGER_FUZZY (1<<1)
322 #define PSM_FINGER_DEFAULT_P tap_threshold
323 #define PSM_FINGER_DEFAULT_W 1
324 #define PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0)
325 #define PSM_FINGER_RESET(f) do { \
326 (f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \
329 typedef struct elantechhw {
347 /* minimum versions supported by this driver */
348 #define ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600)
350 #define ELANTECH_MAGIC(magic) \
351 ((magic)[0] == 0x3c && (magic)[1] == 0x03 && \
352 ((magic)[2] == 0xc8 || (magic)[2] == 0x00))
354 #define ELANTECH_FW_ID 0x00
355 #define ELANTECH_FW_VERSION 0x01
356 #define ELANTECH_CAPABILITIES 0x02
357 #define ELANTECH_SAMPLE 0x03
358 #define ELANTECH_RESOLUTION 0x04
359 #define ELANTECH_REG_READ 0x10
360 #define ELANTECH_REG_WRITE 0x11
361 #define ELANTECH_REG_RDWR 0x00
362 #define ELANTECH_CUSTOM_CMD 0xf8
365 #define ELANTECH_MAX_FINGERS 5
367 #define ELANTECH_MAX_FINGERS PSM_FINGERS
370 #define ELANTECH_FINGER_MAX_P 255
371 #define ELANTECH_FINGER_MAX_W 15
372 #define ELANTECH_FINGER_SET_XYP(pb) (finger_t) { \
373 .x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2], \
374 .y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5], \
375 .p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f), \
376 .w = PSM_FINGER_DEFAULT_W, \
382 ELANTECH_PKT_TRACKPOINT,
383 ELANTECH_PKT_V2_COMMON,
384 ELANTECH_PKT_V2_2FINGER,
386 ELANTECH_PKT_V4_STATUS,
387 ELANTECH_PKT_V4_HEAD,
388 ELANTECH_PKT_V4_MOTION
391 #define ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06)
392 #define ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 : \
393 (pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) && \
394 (pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff && \
395 (pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff && \
396 (pb)->ipacket[5] == 0xff)
397 #define ELANTECH_PKT_IS_V2(pb) \
398 (((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02)
399 #define ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? \
400 ((pb)->ipacket[3] & 0x09) == 0x08 : \
401 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02)
402 #define ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? \
403 ((pb)->ipacket[3] & 0x09) == 0x09 : \
404 ((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c)
405 #define ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? \
406 ((pb)->ipacket[3] & 0x08) == 0x00 : \
407 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10)
409 typedef struct elantechaction {
410 finger_t fingers[ELANTECH_MAX_FINGERS];
415 /* driver control block */
416 struct psm_softc { /* Driver status information */
418 struct selinfo rsel; /* Process selecting for Input */
419 u_char state; /* Mouse driver state */
420 int config; /* driver configuration flags */
421 int flags; /* other flags */
422 KBDC kbdc; /* handle to access kbd controller */
423 struct resource *intr; /* IRQ resource */
424 void *ih; /* interrupt handle */
425 mousehw_t hw; /* hardware information */
426 synapticshw_t synhw; /* Synaptics hardware information */
427 synapticsinfo_t syninfo; /* Synaptics configuration */
428 smoother_t smoother[PSM_FINGERS]; /* Motion smoothing */
429 gesture_t gesture; /* Gesture context */
430 elantechhw_t elanhw; /* Elantech hardware information */
431 elantechaction_t elanaction; /* Elantech action context */
432 int tphw; /* TrackPoint hardware information */
433 trackpointinfo_t tpinfo; /* TrackPoint configuration */
434 mousemode_t mode; /* operation mode */
435 mousemode_t dflt_mode; /* default operation mode */
436 mousestatus_t status; /* accumulated mouse movement */
437 ringbuf_t queue; /* mouse status queue */
438 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
439 int pqueue_start; /* start of data in queue */
440 int pqueue_end; /* end of data in queue */
441 int button; /* the latest button state */
442 int xold; /* previous absolute X position */
443 int yold; /* previous absolute Y position */
444 int xaverage; /* average X position */
445 int yaverage; /* average Y position */
446 int squelch; /* level to filter movement at low speed */
447 int syncerrors; /* # of bytes discarded to synchronize */
448 int pkterrors; /* # of packets failed during quaranteen. */
449 int fpcount; /* forcePad valid packet counter */
450 struct timeval inputtimeout;
451 struct timeval lastsoftintr; /* time of last soft interrupt */
452 struct timeval lastinputerr; /* time last sync error happened */
453 struct timeval idletimeout;
454 packetbuf_t idlepacket; /* packet to send after idle timeout */
455 int watchdog; /* watchdog timer flag */
456 struct callout callout; /* watchdog timer call out */
457 struct callout softcallout; /* buffer timer call out */
462 struct sigio *async; /* Processes waiting for SIGIO */
463 int extended_buttons;
464 int muxport; /* MUX port with attached Synaptics */
465 u_char muxsave[3]; /* 3->6 byte proto conversion buffer */
466 int muxtpbuttons; /* Touchpad button state */
467 int muxmsbuttons; /* Mouse (trackpoint) button state */
468 struct timeval muxmidtimeout; /* middle button supression timeout */
469 int muxsinglesyna; /* Probe result of single Synaptics */
471 struct evdev_dev *evdev_a; /* Absolute reporting device */
472 struct evdev_dev *evdev_r; /* Relative reporting device */
475 static devclass_t psm_devclass;
477 /* driver state flags (state) */
478 #define PSM_VALID 0x80
479 #define PSM_OPEN 1 /* Device is open */
480 #define PSM_ASLP 2 /* Waiting for mouse data */
481 #define PSM_SOFTARMED 4 /* Software interrupt armed */
482 #define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
483 #define PSM_EV_OPEN_R 0x10 /* Relative evdev device is open */
484 #define PSM_EV_OPEN_A 0x20 /* Absolute evdev device is open */
486 /* driver configuration flags (config) */
487 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
488 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
489 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
490 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
491 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
492 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
493 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
494 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
495 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
497 #define PSM_CONFIG_FLAGS \
498 (PSM_CONFIG_RESOLUTION | \
500 PSM_CONFIG_NOCHECKSYNC | \
501 PSM_CONFIG_NOIDPROBE | \
502 PSM_CONFIG_NORESET | \
503 PSM_CONFIG_FORCETAP | \
504 PSM_CONFIG_IGNPORTERROR | \
505 PSM_CONFIG_HOOKRESUME | \
506 PSM_CONFIG_INITAFTERSUSPEND)
508 /* other flags (flags) */
509 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
511 #define kbdcp(p) ((atkbdc_softc_t *)(p))
512 #define ALWAYS_RESTORE_CONTROLLER(kbdc) !(kbdcp(kbdc)->quirks \
513 & KBDC_QUIRK_KEEP_ACTIVATED)
516 static int tap_enabled = -1;
517 static int verbose = PSM_DEBUG;
518 static int synaptics_support = 1;
519 static int trackpoint_support = 1;
520 static int elantech_support = 1;
521 static int mux_disabled = -1;
523 /* for backward compatibility */
524 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
525 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
526 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
528 typedef struct old_mousehw {
535 typedef struct old_mousemode {
542 #define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field)
544 SYNAPTICS_SYSCTL_MIN_PRESSURE = SYN_OFFSET(min_pressure),
545 SYNAPTICS_SYSCTL_MAX_PRESSURE = SYN_OFFSET(max_pressure),
546 SYNAPTICS_SYSCTL_MAX_WIDTH = SYN_OFFSET(max_width),
547 SYNAPTICS_SYSCTL_MARGIN_TOP = SYN_OFFSET(margin_top),
548 SYNAPTICS_SYSCTL_MARGIN_RIGHT = SYN_OFFSET(margin_right),
549 SYNAPTICS_SYSCTL_MARGIN_BOTTOM = SYN_OFFSET(margin_bottom),
550 SYNAPTICS_SYSCTL_MARGIN_LEFT = SYN_OFFSET(margin_left),
551 SYNAPTICS_SYSCTL_NA_TOP = SYN_OFFSET(na_top),
552 SYNAPTICS_SYSCTL_NA_RIGHT = SYN_OFFSET(na_right),
553 SYNAPTICS_SYSCTL_NA_BOTTOM = SYN_OFFSET(na_bottom),
554 SYNAPTICS_SYSCTL_NA_LEFT = SYN_OFFSET(na_left),
555 SYNAPTICS_SYSCTL_WINDOW_MIN = SYN_OFFSET(window_min),
556 SYNAPTICS_SYSCTL_WINDOW_MAX = SYN_OFFSET(window_max),
557 SYNAPTICS_SYSCTL_MULTIPLICATOR = SYN_OFFSET(multiplicator),
558 SYNAPTICS_SYSCTL_WEIGHT_CURRENT = SYN_OFFSET(weight_current),
559 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS = SYN_OFFSET(weight_previous),
560 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA = SYN_OFFSET(weight_previous_na),
561 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED = SYN_OFFSET(weight_len_squared),
562 SYNAPTICS_SYSCTL_DIV_MIN = SYN_OFFSET(div_min),
563 SYNAPTICS_SYSCTL_DIV_MAX = SYN_OFFSET(div_max),
564 SYNAPTICS_SYSCTL_DIV_MAX_NA = SYN_OFFSET(div_max_na),
565 SYNAPTICS_SYSCTL_DIV_LEN = SYN_OFFSET(div_len),
566 SYNAPTICS_SYSCTL_TAP_MAX_DELTA = SYN_OFFSET(tap_max_delta),
567 SYNAPTICS_SYSCTL_TAP_MIN_QUEUE = SYN_OFFSET(tap_min_queue),
568 SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT = SYN_OFFSET(taphold_timeout),
569 SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA = SYN_OFFSET(vscroll_hor_area),
570 SYNAPTICS_SYSCTL_VSCROLL_VER_AREA = SYN_OFFSET(vscroll_ver_area),
571 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA = SYN_OFFSET(vscroll_min_delta),
572 SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN = SYN_OFFSET(vscroll_div_min),
573 SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX = SYN_OFFSET(vscroll_div_max),
574 SYNAPTICS_SYSCTL_TOUCHPAD_OFF = SYN_OFFSET(touchpad_off),
575 SYNAPTICS_SYSCTL_SOFTBUTTONS_Y = SYN_OFFSET(softbuttons_y),
576 SYNAPTICS_SYSCTL_SOFTBUTTON2_X = SYN_OFFSET(softbutton2_x),
577 SYNAPTICS_SYSCTL_SOFTBUTTON3_X = SYN_OFFSET(softbutton3_x),
578 SYNAPTICS_SYSCTL_THREE_FINGER_DRAG = SYN_OFFSET(three_finger_drag),
579 SYNAPTICS_SYSCTL_NATURAL_SCROLL = SYN_OFFSET(natural_scroll),
580 #define SYNAPTICS_SYSCTL_LAST SYNAPTICS_SYSCTL_NATURAL_SCROLL
583 /* packet formatting function */
584 typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
587 /* function prototypes */
588 static void psmidentify(driver_t *, device_t);
589 static int psmprobe(device_t);
590 static int psmattach(device_t);
591 static int psmdetach(device_t);
592 static int psmresume(device_t);
594 static d_open_t psm_cdev_open;
595 static d_close_t psm_cdev_close;
596 static d_read_t psmread;
597 static d_write_t psmwrite;
598 static d_ioctl_t psmioctl;
599 static d_poll_t psmpoll;
601 static int psmopen(struct psm_softc *);
602 static int psmclose(struct psm_softc *);
605 static evdev_open_t psm_ev_open_r;
606 static evdev_close_t psm_ev_close_r;
607 static evdev_open_t psm_ev_open_a;
608 static evdev_close_t psm_ev_close_a;
611 static int enable_aux_dev(KBDC);
612 static int disable_aux_dev(KBDC);
613 static int get_mouse_status(KBDC, int *, int, int);
614 static int get_aux_id(KBDC);
615 static int set_mouse_sampling_rate(KBDC, int);
616 static int set_mouse_scaling(KBDC, int);
617 static int set_mouse_resolution(KBDC, int);
618 static int set_mouse_mode(KBDC);
619 static int get_mouse_buttons(KBDC);
620 static int is_a_mouse(int);
621 static void recover_from_error(KBDC);
622 static int restore_controller(KBDC, int);
623 static int doinitialize(struct psm_softc *, mousemode_t *);
624 static int doopen(struct psm_softc *, int);
625 static int reinitialize(struct psm_softc *, int);
626 static char *model_name(int);
627 static void psmsoftintr(void *);
628 static void psmsoftintridle(void *);
629 static void psmintr(void *);
630 static void psmtimeout(void *);
631 static int timeelapsed(const struct timeval *, int, int,
632 const struct timeval *);
633 static void dropqueue(struct psm_softc *);
634 static void flushpackets(struct psm_softc *);
635 static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
636 mousestatus_t *, int *, int *, int *);
637 static int proc_synaptics(struct psm_softc *, packetbuf_t *,
638 mousestatus_t *, int *, int *, int *);
639 static int proc_synaptics_mux(struct psm_softc *, packetbuf_t *);
640 static void proc_versapad(struct psm_softc *, packetbuf_t *,
641 mousestatus_t *, int *, int *, int *);
642 static int proc_elantech(struct psm_softc *, packetbuf_t *,
643 mousestatus_t *, int *, int *, int *);
644 static int psmpalmdetect(struct psm_softc *, finger_t *, int);
645 static void psmgestures(struct psm_softc *, finger_t *, int,
647 static void psmsmoother(struct psm_softc *, finger_t *, int,
648 mousestatus_t *, int *, int *);
649 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
652 /* vendor specific features */
653 enum probearg { PROBE, REINIT };
654 typedef int probefunc_t(struct psm_softc *, enum probearg);
656 static int mouse_id_proc1(KBDC, int, int, int *);
657 static int mouse_ext_command(KBDC, int);
659 static probefunc_t enable_groller;
660 static probefunc_t enable_gmouse;
661 static probefunc_t enable_aglide;
662 static probefunc_t enable_kmouse;
663 static probefunc_t enable_msexplorer;
664 static probefunc_t enable_msintelli;
665 static probefunc_t enable_4dmouse;
666 static probefunc_t enable_4dplus;
667 static probefunc_t enable_mmanplus;
668 static probefunc_t enable_synaptics;
669 static probefunc_t enable_synaptics_mux;
670 static probefunc_t enable_single_synaptics_mux;
671 static probefunc_t enable_trackpoint;
672 static probefunc_t enable_versapad;
673 static probefunc_t enable_elantech;
675 static void set_trackpoint_parameters(struct psm_softc *sc);
676 static void synaptics_passthrough_on(struct psm_softc *sc);
677 static void synaptics_passthrough_off(struct psm_softc *sc);
678 static int synaptics_preferred_mode(struct psm_softc *sc);
679 static void synaptics_set_mode(struct psm_softc *sc, int mode_byte);
685 probefunc_t *probefunc;
688 * WARNING: the order of probe is very important. Don't mess it
689 * unless you know what you are doing.
691 { MOUSE_MODEL_SYNAPTICS, /* Synaptics + mouse on Active Mux */
692 0x00, MOUSE_PS2_PACKETSIZE, enable_synaptics_mux },
693 { MOUSE_MODEL_SYNAPTICS, /* Single Synaptics on Active Mux */
694 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_single_synaptics_mux },
695 { MOUSE_MODEL_NET, /* Genius NetMouse */
696 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
697 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
698 0xc8, 6, enable_groller },
699 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
700 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
701 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
702 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
703 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
704 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
705 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
706 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
707 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
708 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
709 { MOUSE_MODEL_ELANTECH, /* Elantech Touchpad */
710 0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech },
711 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
712 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
713 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
714 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
715 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
716 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
717 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
718 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
719 { MOUSE_MODEL_TRACKPOINT, /* IBM/Lenovo TrackPoint */
720 0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
721 { MOUSE_MODEL_GENERIC,
722 0xc0, MOUSE_PS2_PACKETSIZE, NULL },
724 #define GENERIC_MOUSE_ENTRY (nitems(vendortype) - 1)
726 /* device driver declarateion */
727 static device_method_t psm_methods[] = {
728 /* Device interface */
729 DEVMETHOD(device_identify, psmidentify),
730 DEVMETHOD(device_probe, psmprobe),
731 DEVMETHOD(device_attach, psmattach),
732 DEVMETHOD(device_detach, psmdetach),
733 DEVMETHOD(device_resume, psmresume),
737 static driver_t psm_driver = {
740 sizeof(struct psm_softc),
743 static struct cdevsw psm_cdevsw = {
744 .d_version = D_VERSION,
745 .d_flags = D_NEEDGIANT,
746 .d_open = psm_cdev_open,
747 .d_close = psm_cdev_close,
752 .d_name = PSM_DRIVER_NAME,
756 static const struct evdev_methods psm_ev_methods_r = {
757 .ev_open = psm_ev_open_r,
758 .ev_close = psm_ev_close_r,
760 static const struct evdev_methods psm_ev_methods_a = {
761 .ev_open = psm_ev_open_a,
762 .ev_close = psm_ev_close_a,
766 /* device I/O routines */
768 enable_aux_dev(KBDC kbdc)
772 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
773 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
775 return (res == PSM_ACK);
779 disable_aux_dev(KBDC kbdc)
783 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
784 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
786 return (res == PSM_ACK);
790 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
799 cmd = PSMC_SEND_DEV_STATUS;
802 cmd = PSMC_SEND_DEV_DATA;
805 empty_aux_buffer(kbdc, 5);
806 res = send_aux_command(kbdc, cmd);
807 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
808 (flag == 1) ? "DATA" : "STATUS", res));
812 for (i = 0; i < len; ++i) {
813 status[i] = read_aux_data(kbdc);
820 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
821 (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
828 get_aux_id(KBDC kbdc)
833 empty_aux_buffer(kbdc, 5);
834 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
835 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
842 id = read_aux_data(kbdc);
843 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
849 set_mouse_sampling_rate(KBDC kbdc, int rate)
853 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
854 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
856 return ((res == PSM_ACK) ? rate : -1);
860 set_mouse_scaling(KBDC kbdc, int scale)
867 scale = PSMC_SET_SCALING11;
870 scale = PSMC_SET_SCALING21;
873 res = send_aux_command(kbdc, scale);
874 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
875 (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
877 return (res == PSM_ACK);
880 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
882 set_mouse_resolution(KBDC kbdc, int val)
886 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
887 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
889 return ((res == PSM_ACK) ? val : -1);
893 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
894 * re-enabled by calling `enable_aux_dev()'
897 set_mouse_mode(KBDC kbdc)
901 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
902 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
904 return (res == PSM_ACK);
908 get_mouse_buttons(KBDC kbdc)
910 int c = 2; /* assume two buttons by default */
914 * NOTE: a special sequence to obtain Logitech Mouse specific
915 * information: set resolution to 25 ppi, set scaling to 1:1, set
916 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
917 * mouse status bytes is the number of available buttons.
918 * Some manufactures also support this sequence.
920 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
922 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
923 set_mouse_scaling(kbdc, 1) &&
924 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
929 /* misc subroutines */
931 * Someday, I will get the complete list of valid pointing devices and
938 static int valid_ids[] = {
939 PSM_MOUSE_ID, /* mouse */
940 PSM_BALLPOINT_ID, /* ballpoint device */
941 PSM_INTELLI_ID, /* Intellimouse */
942 PSM_EXPLORER_ID, /* Intellimouse Explorer */
943 -1 /* end of table */
947 for (i = 0; valid_ids[i] >= 0; ++i)
948 if (valid_ids[i] == id)
957 model_name(int model)
963 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
964 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
965 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
966 { MOUSE_MODEL_THINK, "ThinkingMouse" },
967 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
968 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
969 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
970 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
971 { MOUSE_MODEL_4D, "4D Mouse" },
972 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
973 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
974 { MOUSE_MODEL_TRACKPOINT, "IBM/Lenovo TrackPoint" },
975 { MOUSE_MODEL_ELANTECH, "Elantech Touchpad" },
976 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
977 { MOUSE_MODEL_UNKNOWN, "Unknown" },
981 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
982 if (models[i].model_code == model)
984 return (models[i].model_name);
988 recover_from_error(KBDC kbdc)
990 /* discard anything left in the output buffer */
991 empty_both_buffers(kbdc, 10);
995 * NOTE: KBDC_RESET_KBD may not restore the communication between the
996 * keyboard and the controller.
1001 * NOTE: somehow diagnostic and keyboard port test commands bring the
1004 if (!test_controller(kbdc))
1005 log(LOG_ERR, "psm: keyboard controller failed.\n");
1006 /* if there isn't a keyboard in the system, the following error is OK */
1007 if (test_kbd_port(kbdc) != 0)
1008 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
1013 restore_controller(KBDC kbdc, int command_byte)
1015 empty_both_buffers(kbdc, 10);
1017 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
1018 log(LOG_ERR, "psm: failed to restore the keyboard controller "
1020 empty_both_buffers(kbdc, 10);
1023 empty_both_buffers(kbdc, 10);
1029 * Re-initialize the aux port and device. The aux port must be enabled
1030 * and its interrupt must be disabled before calling this routine.
1031 * The aux device will be disabled before returning.
1032 * The keyboard controller must be locked via `kbdc_lock()' before
1033 * calling this routine.
1036 doinitialize(struct psm_softc *sc, mousemode_t *mode)
1038 KBDC kbdc = sc->kbdc;
1042 switch((i = test_aux_port(kbdc))) {
1043 case 1: /* ignore these errors */
1049 "psm%d: strange result for test aux port (%d).\n",
1052 case 0: /* no error */
1054 case -1: /* time out */
1055 default: /* error */
1056 recover_from_error(kbdc);
1057 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1059 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
1064 if (sc->config & PSM_CONFIG_NORESET) {
1066 * Don't try to reset the pointing device. It may possibly
1067 * be left in the unknown state, though...
1071 * NOTE: some controllers appears to hang the `keyboard' when
1072 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1074 if (!reset_aux_dev(kbdc)) {
1075 recover_from_error(kbdc);
1076 log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
1083 * both the aux port and the aux device is functioning, see
1084 * if the device can be enabled.
1086 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
1087 log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
1091 empty_both_buffers(kbdc, 10); /* remove stray data if any */
1093 /* Re-enable the mouse. */
1094 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1095 if (vendortype[i].model == sc->hw.model)
1096 (*vendortype[i].probefunc)(sc, REINIT);
1098 /* set mouse parameters */
1099 if (mode != (mousemode_t *)NULL) {
1101 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
1102 if (mode->resolution >= 0)
1104 set_mouse_resolution(kbdc, mode->resolution);
1105 set_mouse_scaling(kbdc, 1);
1106 set_mouse_mode(kbdc);
1109 /* Record sync on the next data packet we see. */
1110 sc->flags |= PSM_NEED_SYNCBITS;
1112 /* just check the status of the mouse */
1113 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
1114 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
1121 doopen(struct psm_softc *sc, int command_byte)
1124 int mux_enabled = FALSE;
1127 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
1128 * no obvious reason. Thus we check the current mode and restore the
1129 * Absolute Mode if it was cleared.
1131 * The previous hack at the end of psmprobe() wasn't efficient when
1132 * moused(8) was restarted.
1134 * A Reset (FF) or Set Defaults (F6) command would clear the
1135 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
1136 * doesn't show any evidence of such a command.
1138 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
1139 if (sc->muxport != PSM_NOMUX) {
1140 mux_enabled = enable_aux_mux(sc->kbdc) >= 0;
1142 set_active_aux_mux_port(sc->kbdc, sc->muxport);
1144 log(LOG_ERR, "psm%d: failed to enable "
1145 "active multiplexing mode.\n",
1148 mouse_ext_command(sc->kbdc, SYNAPTICS_READ_MODES);
1149 get_mouse_status(sc->kbdc, stat, 0, 3);
1150 if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
1153 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1154 VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
1155 "hopefully restored\n",
1159 disable_aux_mux(sc->kbdc);
1163 * A user may want to disable tap and drag gestures on a Synaptics
1164 * TouchPad when it operates in Relative Mode.
1166 if (sc->hw.model == MOUSE_MODEL_GENERIC) {
1167 if (tap_enabled > 0) {
1169 "psm%d: enable tap and drag gestures\n",
1171 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1172 } else if (tap_enabled == 0) {
1174 "psm%d: disable tap and drag gestures\n",
1176 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1180 /* enable the mouse device */
1181 if (!enable_aux_dev(sc->kbdc)) {
1182 /* MOUSE ERROR: failed to enable the mouse because:
1183 * 1) the mouse is faulty,
1184 * 2) the mouse has been removed(!?)
1185 * In the latter case, the keyboard may have hung, and need
1186 * recovery procedure...
1188 recover_from_error(sc->kbdc);
1190 /* FIXME: we could reset the mouse here and try to enable
1191 * it again. But it will take long time and it's not a good
1192 * idea to disable the keyboard that long...
1194 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
1195 recover_from_error(sc->kbdc);
1199 restore_controller(sc->kbdc, command_byte);
1200 /* mark this device is no longer available */
1201 sc->state &= ~PSM_VALID;
1203 "psm%d: failed to enable the device (doopen).\n",
1209 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1210 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
1213 /* enable the aux port and interrupt */
1214 if (!set_controller_command_byte(sc->kbdc,
1215 kbdc_get_device_mask(sc->kbdc),
1216 (command_byte & KBD_KBD_CONTROL_BITS) |
1217 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
1218 /* CONTROLLER ERROR */
1219 disable_aux_dev(sc->kbdc);
1220 restore_controller(sc->kbdc, command_byte);
1222 "psm%d: failed to enable the aux interrupt (doopen).\n",
1227 /* start the watchdog timer */
1228 sc->watchdog = FALSE;
1229 callout_reset(&sc->callout, hz * 2, psmtimeout, sc);
1235 reinitialize(struct psm_softc *sc, int doinit)
1241 /* don't let anybody mess with the aux device */
1242 if (!kbdc_lock(sc->kbdc, TRUE))
1246 /* block our watchdog timer */
1247 sc->watchdog = FALSE;
1248 callout_stop(&sc->callout);
1250 /* save the current controller command byte */
1251 empty_both_buffers(sc->kbdc, 10);
1252 c = get_controller_command_byte(sc->kbdc);
1254 "psm%d: current command byte: %04x (reinitialize).\n",
1257 /* enable the aux port but disable the aux interrupt and the keyboard */
1258 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
1259 kbdc_get_device_mask(sc->kbdc),
1260 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1261 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1262 /* CONTROLLER ERROR */
1264 kbdc_lock(sc->kbdc, FALSE);
1266 "psm%d: unable to set the command byte (reinitialize).\n",
1271 /* flush any data */
1272 if (sc->state & PSM_VALID) {
1273 /* this may fail; but never mind... */
1274 disable_aux_dev(sc->kbdc);
1275 empty_aux_buffer(sc->kbdc, 10);
1280 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1282 /* try to detect the aux device; are you still there? */
1285 if (doinitialize(sc, &sc->mode)) {
1287 sc->state |= PSM_VALID;
1289 /* the device has gone! */
1290 restore_controller(sc->kbdc, c);
1291 sc->state &= ~PSM_VALID;
1293 "psm%d: the aux device has gone! (reinitialize).\n",
1300 /* restore the driver state */
1301 if ((sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)) &&
1303 /* enable the aux device and the port again */
1304 err = doopen(sc, c);
1306 log(LOG_ERR, "psm%d: failed to enable the device "
1307 "(reinitialize).\n", sc->unit);
1309 /* restore the keyboard port and disable the aux port */
1310 if (!set_controller_command_byte(sc->kbdc,
1311 kbdc_get_device_mask(sc->kbdc),
1312 (c & KBD_KBD_CONTROL_BITS) |
1313 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1314 /* CONTROLLER ERROR */
1315 log(LOG_ERR, "psm%d: failed to disable the aux port "
1316 "(reinitialize).\n", sc->unit);
1321 kbdc_lock(sc->kbdc, FALSE);
1325 /* psm driver entry points */
1328 psmidentify(driver_t *driver, device_t parent)
1335 unit = device_get_unit(parent);
1337 /* always add at least one child */
1338 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1342 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1347 * If the PS/2 mouse device has already been reported by ACPI or
1348 * PnP BIOS, obtain the IRQ resource from it.
1349 * (See psmcpnp_attach() below.)
1351 psmc = device_find_child(device_get_parent(parent),
1352 PSMCPNP_DRIVER_NAME, unit);
1355 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1358 bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1359 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
1362 #define endprobe(v) do { \
1365 kbdc_set_device_mask(sc->kbdc, mask); \
1366 kbdc_lock(sc->kbdc, FALSE); \
1371 psmprobe(device_t dev)
1373 int unit = device_get_unit(dev);
1374 struct psm_softc *sc = device_get_softc(dev);
1385 /* see if IRQ is available */
1387 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1388 if (sc->intr == NULL) {
1390 device_printf(dev, "unable to allocate IRQ\n");
1393 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1396 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1397 if (sc->kbdc == NULL)
1399 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1400 /* XXX: for backward compatibility */
1401 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1403 #ifdef PSM_RESETAFTERSUSPEND
1404 PSM_CONFIG_INITAFTERSUSPEND;
1406 PSM_CONFIG_HOOKRESUME;
1408 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1410 sc->muxport = PSM_NOMUX;
1414 device_set_desc(dev, "PS/2 Mouse");
1416 if (!kbdc_lock(sc->kbdc, TRUE)) {
1417 printf("psm%d: unable to lock the controller.\n", unit);
1424 * NOTE: two bits in the command byte controls the operation of the
1425 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1426 * port interrupt (IRQ 12) enable bit (bit 2).
1429 /* discard anything left after the keyboard initialization */
1430 empty_both_buffers(sc->kbdc, 10);
1432 /* save the current command byte; it will be used later */
1433 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1434 command_byte = get_controller_command_byte(sc->kbdc);
1436 printf("psm%d: current command byte:%04x\n", unit,
1438 if (command_byte == -1) {
1439 /* CONTROLLER ERROR */
1440 printf("psm%d: unable to get the current command byte value.\n",
1446 * disable the keyboard port while probing the aux port, which must be
1447 * enabled during this routine
1449 if (!set_controller_command_byte(sc->kbdc,
1450 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1451 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1452 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1454 * this is CONTROLLER ERROR; I don't know how to recover
1455 * from this error...
1457 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1458 restore_controller(sc->kbdc, command_byte);
1459 printf("psm%d: unable to set the command byte.\n", unit);
1462 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1465 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1466 * port exists and is functioning. However, some controllers appears
1467 * to respond with zero even when the aux port doesn't exist. (It may
1468 * be that this is only the case when the controller DOES have the aux
1469 * port but the port is not wired on the motherboard.) The keyboard
1470 * controllers without the port, such as the original AT, are
1471 * supposed to return with an error code or simply time out. In any
1472 * case, we have to continue probing the port even when the controller
1475 * XXX: some controllers erroneously return the error code 1, 2 or 3
1476 * when it has a perfectly functional aux port. We have to ignore
1477 * this error code. Even if the controller HAS error with the aux
1478 * port, it will be detected later...
1479 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1481 switch ((i = test_aux_port(sc->kbdc))) {
1482 case 1: /* ignore these errors */
1487 printf("psm%d: strange result for test aux port "
1488 "(%d).\n", unit, i);
1490 case 0: /* no error */
1492 case -1: /* time out */
1493 default: /* error */
1494 recover_from_error(sc->kbdc);
1495 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1497 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1498 restore_controller(sc->kbdc, command_byte);
1500 printf("psm%d: the aux port is not functioning (%d).\n",
1505 if (sc->config & PSM_CONFIG_NORESET) {
1507 * Don't try to reset the pointing device. It may possibly be
1508 * left in an unknown state, though...
1512 * NOTE: some controllers appears to hang the `keyboard' when
1513 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1515 * Attempt to reset the controller twice -- this helps
1516 * pierce through some KVM switches. The second reset
1519 if (!reset_aux_dev(sc->kbdc)) {
1520 recover_from_error(sc->kbdc);
1521 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1522 restore_controller(sc->kbdc, command_byte);
1524 printf("psm%d: failed to reset the aux "
1527 } else if (!reset_aux_dev(sc->kbdc)) {
1528 recover_from_error(sc->kbdc);
1530 printf("psm%d: failed to reset the aux device "
1536 * both the aux port and the aux device are functioning, see if the
1537 * device can be enabled. NOTE: when enabled, the device will start
1538 * sending data; we shall immediately disable the device once we know
1539 * the device can be enabled.
1541 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1543 recover_from_error(sc->kbdc);
1544 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1545 restore_controller(sc->kbdc, command_byte);
1547 printf("psm%d: failed to enable the aux device.\n",
1552 /* save the default values after reset */
1553 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1554 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1555 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1557 sc->dflt_mode.rate = sc->mode.rate = -1;
1558 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1561 /* hardware information */
1562 sc->hw.iftype = MOUSE_IF_PS2;
1564 /* verify the device is a mouse */
1565 sc->hw.hwid = get_aux_id(sc->kbdc);
1566 if (!is_a_mouse(sc->hw.hwid)) {
1567 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1568 restore_controller(sc->kbdc, command_byte);
1570 printf("psm%d: unknown device type (%d).\n", unit,
1574 switch (sc->hw.hwid) {
1575 case PSM_BALLPOINT_ID:
1576 sc->hw.type = MOUSE_TRACKBALL;
1579 case PSM_INTELLI_ID:
1580 case PSM_EXPLORER_ID:
1581 case PSM_4DMOUSE_ID:
1583 sc->hw.type = MOUSE_MOUSE;
1586 sc->hw.type = MOUSE_UNKNOWN;
1590 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1592 i = GENERIC_MOUSE_ENTRY;
1595 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1597 /* other parameters */
1598 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1599 if ((*vendortype[i].probefunc)(sc, PROBE)) {
1601 printf("psm%d: found %s\n", unit,
1602 model_name(vendortype[i].model));
1607 sc->hw.model = vendortype[i].model;
1609 sc->dflt_mode.level = PSM_LEVEL_BASE;
1610 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1611 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1612 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1613 sc->dflt_mode.syncmask[0] = 0;
1615 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1616 if (sc->config & PSM_CONFIG_FORCETAP)
1617 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1618 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1619 sc->mode = sc->dflt_mode;
1620 sc->mode.packetsize = vendortype[i].packetsize;
1622 /* set mouse parameters */
1625 * A version of Logitech FirstMouse+ won't report wheel movement,
1626 * if SET_DEFAULTS is sent... Don't use this command.
1627 * This fix was found by Takashi Nishida.
1629 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1631 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1633 if (sc->config & PSM_CONFIG_RESOLUTION)
1634 sc->mode.resolution =
1635 set_mouse_resolution(sc->kbdc,
1636 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1637 else if (sc->mode.resolution >= 0)
1638 sc->mode.resolution =
1639 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1640 if (sc->mode.rate > 0)
1642 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1643 set_mouse_scaling(sc->kbdc, 1);
1645 /* Record sync on the next data packet we see. */
1646 sc->flags |= PSM_NEED_SYNCBITS;
1648 /* just check the status of the mouse */
1650 * NOTE: XXX there are some arcane controller/mouse combinations out
1651 * there, which hung the controller unless there is data transmission
1652 * after ACK from the mouse.
1654 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1655 printf("psm%d: failed to get status.\n", unit);
1658 * When in its native mode, some mice operate with different
1659 * default parameters than in the PS/2 compatible mode.
1661 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1662 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1665 /* disable the aux port for now... */
1666 if (!set_controller_command_byte(sc->kbdc,
1667 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1668 (command_byte & KBD_KBD_CONTROL_BITS) |
1669 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1671 * this is CONTROLLER ERROR; I don't know the proper way to
1672 * recover from this error...
1674 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1675 restore_controller(sc->kbdc, command_byte);
1676 printf("psm%d: unable to set the command byte.\n", unit);
1681 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1682 kbdc_lock(sc->kbdc, FALSE);
1686 #ifdef EVDEV_SUPPORT
1687 /* Values are taken from Linux drivers for userland software compatibility */
1688 #define PS2_MOUSE_VENDOR 0x0002
1689 #define PS2_MOUSE_GENERIC_PRODUCT 0x0001
1690 #define PS2_MOUSE_SYNAPTICS_NAME "SynPS/2 Synaptics TouchPad"
1691 #define PS2_MOUSE_SYNAPTICS_PRODUCT 0x0007
1692 #define PS2_MOUSE_TRACKPOINT_NAME "TPPS/2 IBM TrackPoint"
1693 #define PS2_MOUSE_TRACKPOINT_PRODUCT 0x000A
1694 #define PS2_MOUSE_ELANTECH_NAME "ETPS/2 Elantech Touchpad"
1695 #define PS2_MOUSE_ELANTECH_ST_NAME "ETPS/2 Elantech TrackPoint"
1696 #define PS2_MOUSE_ELANTECH_PRODUCT 0x000E
1697 #define ABSINFO_END { ABS_CNT, 0, 0, 0 }
1700 psm_support_abs_bulk(struct evdev_dev *evdev, const uint16_t info[][4])
1704 for (i = 0; info[i][0] != ABS_CNT; i++)
1705 evdev_support_abs(evdev, info[i][0], info[i][1], info[i][2],
1710 psm_push_mt_finger(struct psm_softc *sc, int id, const finger_t *f)
1712 int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1714 evdev_push_abs(sc->evdev_a, ABS_MT_SLOT, id);
1715 evdev_push_abs(sc->evdev_a, ABS_MT_TRACKING_ID, id);
1716 evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_X, f->x);
1717 evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_Y, y);
1718 evdev_push_abs(sc->evdev_a, ABS_MT_PRESSURE, f->p);
1722 psm_push_st_finger(struct psm_softc *sc, const finger_t *f)
1724 int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1726 evdev_push_abs(sc->evdev_a, ABS_X, f->x);
1727 evdev_push_abs(sc->evdev_a, ABS_Y, y);
1728 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, f->p);
1729 if (sc->synhw.capPalmDetect)
1730 evdev_push_abs(sc->evdev_a, ABS_TOOL_WIDTH, f->w);
1734 psm_register(device_t dev, int model_code)
1736 struct psm_softc *sc = device_get_softc(dev);
1737 struct evdev_dev *evdev_r;
1738 int error, i, nbuttons, nwheels, product;
1739 bool is_pointing_stick;
1742 name = model_name(model_code);
1743 nbuttons = sc->hw.buttons;
1744 product = PS2_MOUSE_GENERIC_PRODUCT;
1746 is_pointing_stick = false;
1748 switch (model_code) {
1749 case MOUSE_MODEL_TRACKPOINT:
1750 name = PS2_MOUSE_TRACKPOINT_NAME;
1751 product = PS2_MOUSE_TRACKPOINT_PRODUCT;
1753 is_pointing_stick = true;
1756 case MOUSE_MODEL_ELANTECH:
1757 name = PS2_MOUSE_ELANTECH_ST_NAME;
1758 product = PS2_MOUSE_ELANTECH_PRODUCT;
1760 is_pointing_stick = true;
1763 case MOUSE_MODEL_MOUSEMANPLUS:
1764 case MOUSE_MODEL_4D:
1768 case MOUSE_MODEL_EXPLORER:
1769 case MOUSE_MODEL_INTELLI:
1770 case MOUSE_MODEL_NET:
1771 case MOUSE_MODEL_NETSCROLL:
1772 case MOUSE_MODEL_4DPLUS:
1777 evdev_r = evdev_alloc();
1778 evdev_set_name(evdev_r, name);
1779 evdev_set_phys(evdev_r, device_get_nameunit(dev));
1780 evdev_set_id(evdev_r, BUS_I8042, PS2_MOUSE_VENDOR, product, 0);
1781 evdev_set_methods(evdev_r, sc, &psm_ev_methods_r);
1783 evdev_support_prop(evdev_r, INPUT_PROP_POINTER);
1784 if (is_pointing_stick)
1785 evdev_support_prop(evdev_r, INPUT_PROP_POINTING_STICK);
1786 evdev_support_event(evdev_r, EV_SYN);
1787 evdev_support_event(evdev_r, EV_KEY);
1788 evdev_support_event(evdev_r, EV_REL);
1789 evdev_support_rel(evdev_r, REL_X);
1790 evdev_support_rel(evdev_r, REL_Y);
1793 evdev_support_rel(evdev_r, REL_HWHEEL);
1796 evdev_support_rel(evdev_r, REL_WHEEL);
1798 for (i = 0; i < nbuttons; i++)
1799 evdev_support_key(evdev_r, BTN_MOUSE + i);
1801 error = evdev_register_mtx(evdev_r, &Giant);
1803 evdev_free(evdev_r);
1805 sc->evdev_r = evdev_r;
1810 psm_register_synaptics(device_t dev)
1812 struct psm_softc *sc = device_get_softc(dev);
1813 const uint16_t synaptics_absinfo_st[][4] = {
1814 { ABS_X, sc->synhw.minimumXCoord,
1815 sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1816 { ABS_Y, sc->synhw.minimumYCoord,
1817 sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1818 { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1821 const uint16_t synaptics_absinfo_mt[][4] = {
1822 { ABS_MT_SLOT, 0, PSM_FINGERS-1, 0},
1823 { ABS_MT_TRACKING_ID, -1, PSM_FINGERS-1, 0},
1824 { ABS_MT_POSITION_X, sc->synhw.minimumXCoord,
1825 sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1826 { ABS_MT_POSITION_Y, sc->synhw.minimumYCoord,
1827 sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1828 { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1831 struct evdev_dev *evdev_a;
1832 int error, i, guest_model;
1834 evdev_a = evdev_alloc();
1835 evdev_set_name(evdev_a, PS2_MOUSE_SYNAPTICS_NAME);
1836 evdev_set_phys(evdev_a, device_get_nameunit(dev));
1837 evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1838 PS2_MOUSE_SYNAPTICS_PRODUCT, 0);
1839 evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1840 if (sc->synhw.capAdvancedGestures || sc->synhw.capReportsV)
1841 evdev_set_flag(evdev_a, EVDEV_FLAG_MT_AUTOREL);
1842 if (sc->synhw.capReportsV)
1843 evdev_set_flag(evdev_a, EVDEV_FLAG_MT_TRACK);
1845 evdev_support_event(evdev_a, EV_SYN);
1846 evdev_support_event(evdev_a, EV_KEY);
1847 evdev_support_event(evdev_a, EV_ABS);
1848 evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1849 if (sc->synhw.capAdvancedGestures)
1850 evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1851 if (sc->synhw.capClickPad)
1852 evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1853 if (sc->synhw.capClickPad && sc->synhw.topButtonPad)
1854 evdev_support_prop(evdev_a, INPUT_PROP_TOPBUTTONPAD);
1855 evdev_support_key(evdev_a, BTN_TOUCH);
1856 evdev_support_nfingers(evdev_a, sc->synhw.capReportsV ? 5 : 3);
1857 psm_support_abs_bulk(evdev_a, synaptics_absinfo_st);
1858 if (sc->synhw.capAdvancedGestures || sc->synhw.capReportsV)
1859 psm_support_abs_bulk(evdev_a, synaptics_absinfo_mt);
1860 if (sc->synhw.capPalmDetect)
1861 evdev_support_abs(evdev_a, ABS_TOOL_WIDTH, 0, 15, 0, 0, 0);
1862 evdev_support_key(evdev_a, BTN_LEFT);
1863 if (!sc->synhw.capClickPad) {
1864 evdev_support_key(evdev_a, BTN_RIGHT);
1865 if (sc->synhw.capExtended && sc->synhw.capMiddle)
1866 evdev_support_key(evdev_a, BTN_MIDDLE);
1868 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
1869 evdev_support_key(evdev_a, BTN_BACK);
1870 evdev_support_key(evdev_a, BTN_FORWARD);
1872 if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0))
1873 for (i = 0; i < sc->synhw.nExtendedButtons; i++)
1874 evdev_support_key(evdev_a, BTN_0 + i);
1876 error = evdev_register_mtx(evdev_a, &Giant);
1877 if (!error && (sc->synhw.capPassthrough || sc->muxport != PSM_NOMUX)) {
1878 guest_model = sc->tpinfo.sysctl_tree != NULL ?
1879 MOUSE_MODEL_TRACKPOINT : MOUSE_MODEL_GENERIC;
1880 error = psm_register(dev, guest_model);
1883 evdev_free(evdev_a);
1885 sc->evdev_a = evdev_a;
1890 psm_register_elantech(device_t dev)
1892 struct psm_softc *sc = device_get_softc(dev);
1893 const uint16_t elantech_absinfo[][4] = {
1894 { ABS_X, 0, sc->elanhw.sizex,
1896 { ABS_Y, 0, sc->elanhw.sizey,
1898 { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1899 { ABS_TOOL_WIDTH, 0, ELANTECH_FINGER_MAX_W, 0 },
1900 { ABS_MT_SLOT, 0, ELANTECH_MAX_FINGERS - 1, 0 },
1901 { ABS_MT_TRACKING_ID, -1, ELANTECH_MAX_FINGERS - 1, 0 },
1902 { ABS_MT_POSITION_X, 0, sc->elanhw.sizex,
1904 { ABS_MT_POSITION_Y, 0, sc->elanhw.sizey,
1906 { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1907 { ABS_MT_TOUCH_MAJOR, 0, ELANTECH_FINGER_MAX_W *
1908 sc->elanhw.dptracex, 0 },
1911 struct evdev_dev *evdev_a;
1914 evdev_a = evdev_alloc();
1915 evdev_set_name(evdev_a, PS2_MOUSE_ELANTECH_NAME);
1916 evdev_set_phys(evdev_a, device_get_nameunit(dev));
1917 evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1918 PS2_MOUSE_ELANTECH_PRODUCT, 0);
1919 evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1920 evdev_set_flag(evdev_a, EVDEV_FLAG_MT_AUTOREL);
1922 evdev_support_event(evdev_a, EV_SYN);
1923 evdev_support_event(evdev_a, EV_KEY);
1924 evdev_support_event(evdev_a, EV_ABS);
1925 evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1926 if (sc->elanhw.issemimt)
1927 evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1928 if (sc->elanhw.isclickpad)
1929 evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1930 evdev_support_key(evdev_a, BTN_TOUCH);
1931 evdev_support_nfingers(evdev_a, ELANTECH_MAX_FINGERS);
1932 evdev_support_key(evdev_a, BTN_LEFT);
1933 if (!sc->elanhw.isclickpad)
1934 evdev_support_key(evdev_a, BTN_RIGHT);
1935 psm_support_abs_bulk(evdev_a, elantech_absinfo);
1937 error = evdev_register_mtx(evdev_a, &Giant);
1938 if (!error && sc->elanhw.hastrackpoint)
1939 error = psm_register(dev, MOUSE_MODEL_ELANTECH);
1941 evdev_free(evdev_a);
1943 sc->evdev_a = evdev_a;
1949 psmattach(device_t dev)
1951 struct make_dev_args mda;
1952 int unit = device_get_unit(dev);
1953 struct psm_softc *sc = device_get_softc(dev);
1957 /* Setup initial state */
1958 sc->state = PSM_VALID;
1959 callout_init(&sc->callout, 0);
1960 callout_init(&sc->softcallout, 0);
1962 /* Setup our interrupt handler */
1964 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1965 if (sc->intr == NULL)
1967 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1973 make_dev_args_init(&mda);
1974 mda.mda_devsw = &psm_cdevsw;
1975 mda.mda_mode = 0666;
1976 mda.mda_si_drv1 = sc;
1978 if ((error = make_dev_s(&mda, &sc->dev, "psm%d", unit)) != 0)
1980 if ((error = make_dev_s(&mda, &sc->bdev, "bpsm%d", unit)) != 0)
1983 #ifdef EVDEV_SUPPORT
1984 switch (sc->hw.model) {
1985 case MOUSE_MODEL_SYNAPTICS:
1986 error = psm_register_synaptics(dev);
1989 case MOUSE_MODEL_ELANTECH:
1990 error = psm_register_elantech(dev);
1994 error = psm_register(dev, sc->hw.model);
2001 /* Some touchpad devices need full reinitialization after suspend. */
2002 switch (sc->hw.model) {
2003 case MOUSE_MODEL_SYNAPTICS:
2004 case MOUSE_MODEL_GLIDEPOINT:
2005 case MOUSE_MODEL_VERSAPAD:
2006 case MOUSE_MODEL_ELANTECH:
2007 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
2010 if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
2011 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
2015 /* Elantech trackpad`s sync bit differs from touchpad`s one */
2016 if (sc->hw.model == MOUSE_MODEL_ELANTECH &&
2017 (sc->elanhw.hascrc || sc->elanhw.hastrackpoint)) {
2018 sc->config |= PSM_CONFIG_NOCHECKSYNC;
2019 sc->flags &= ~PSM_NEED_SYNCBITS;
2023 printf("psm%d: model %s, device ID %d\n",
2024 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
2026 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
2027 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
2028 sc->hw.hwid >> 8, sc->hw.buttons);
2029 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
2030 unit, sc->config, sc->flags, sc->mode.packetsize);
2031 printf("psm%d: syncmask:%02x, syncbits:%02x%s\n",
2032 unit, sc->mode.syncmask[0], sc->mode.syncmask[1],
2033 sc->config & PSM_CONFIG_NOCHECKSYNC ? " (sync not checked)" : "");
2041 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
2042 if (sc->dev != NULL)
2043 destroy_dev(sc->dev);
2044 if (sc->bdev != NULL)
2045 destroy_dev(sc->bdev);
2051 psmdetach(device_t dev)
2053 struct psm_softc *sc;
2056 sc = device_get_softc(dev);
2057 if (sc->state & PSM_OPEN)
2060 #ifdef EVDEV_SUPPORT
2061 evdev_free(sc->evdev_r);
2062 evdev_free(sc->evdev_a);
2066 bus_teardown_intr(dev, sc->intr, sc->ih);
2067 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
2069 destroy_dev(sc->dev);
2070 destroy_dev(sc->bdev);
2072 callout_drain(&sc->callout);
2073 callout_drain(&sc->softcallout);
2078 #ifdef EVDEV_SUPPORT
2080 psm_ev_open_r(struct evdev_dev *evdev)
2082 struct psm_softc *sc = evdev_get_softc(evdev);
2085 /* Get device data */
2086 if ((sc->state & PSM_VALID) == 0) {
2087 /* the device is no longer valid/functioning */
2091 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_A)))
2095 sc->state |= PSM_EV_OPEN_R;
2101 psm_ev_close_r(struct evdev_dev *evdev)
2103 struct psm_softc *sc = evdev_get_softc(evdev);
2106 sc->state &= ~PSM_EV_OPEN_R;
2108 if (sc->state & (PSM_OPEN | PSM_EV_OPEN_A))
2111 if (sc->state & PSM_VALID)
2118 psm_ev_open_a(struct evdev_dev *evdev)
2120 struct psm_softc *sc = evdev_get_softc(evdev);
2123 /* Get device data */
2124 if ((sc->state & PSM_VALID) == 0) {
2125 /* the device is no longer valid/functioning */
2129 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R)))
2133 sc->state |= PSM_EV_OPEN_A;
2139 psm_ev_close_a(struct evdev_dev *evdev)
2141 struct psm_softc *sc = evdev_get_softc(evdev);
2144 sc->state &= ~PSM_EV_OPEN_A;
2146 if (sc->state & (PSM_OPEN | PSM_EV_OPEN_R))
2149 if (sc->state & PSM_VALID)
2157 psm_cdev_open(struct cdev *dev, int flag, int fmt, struct thread *td)
2159 struct psm_softc *sc;
2162 /* Get device data */
2164 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2165 /* the device is no longer valid/functioning */
2169 /* Disallow multiple opens */
2170 if (sc->state & PSM_OPEN)
2173 device_busy(devclass_get_device(psm_devclass, sc->unit));
2175 #ifdef EVDEV_SUPPORT
2176 /* Already opened by evdev */
2177 if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2182 sc->state |= PSM_OPEN;
2184 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2190 psm_cdev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
2192 struct psm_softc *sc;
2195 /* Get device data */
2197 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2198 /* the device is no longer valid/functioning */
2202 #ifdef EVDEV_SUPPORT
2203 /* Still opened by evdev */
2204 if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2209 sc->state &= ~PSM_OPEN;
2210 /* clean up and sigio requests */
2211 if (sc->async != NULL) {
2212 funsetown(&sc->async);
2215 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2222 psmopen(struct psm_softc *sc)
2228 /* Initialize state */
2229 sc->mode.level = sc->dflt_mode.level;
2230 sc->mode.protocol = sc->dflt_mode.protocol;
2231 sc->watchdog = FALSE;
2234 /* flush the event queue */
2235 sc->queue.count = 0;
2238 sc->status.flags = 0;
2239 sc->status.button = 0;
2240 sc->status.obutton = 0;
2245 sc->pqueue_start = 0;
2248 /* empty input buffer */
2253 /* don't let timeout routines in the keyboard driver to poll the kbdc */
2254 if (!kbdc_lock(sc->kbdc, TRUE))
2257 /* save the current controller command byte */
2259 command_byte = get_controller_command_byte(sc->kbdc);
2261 /* enable the aux port and temporalily disable the keyboard */
2262 if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
2263 kbdc_get_device_mask(sc->kbdc),
2264 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2265 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2266 /* CONTROLLER ERROR; do you know how to get out of this? */
2267 kbdc_lock(sc->kbdc, FALSE);
2270 "psm%d: unable to set the command byte (psmopen).\n",
2275 * Now that the keyboard controller is told not to generate
2276 * the keyboard and mouse interrupts, call `splx()' to allow
2277 * the other tty interrupts. The clock interrupt may also occur,
2278 * but timeout routines will be blocked by the poll flag set
2283 /* enable the mouse device */
2284 err = doopen(sc, command_byte);
2287 kbdc_lock(sc->kbdc, FALSE);
2292 psmclose(struct psm_softc *sc)
2298 /* don't let timeout routines in the keyboard driver to poll the kbdc */
2299 if (!kbdc_lock(sc->kbdc, TRUE))
2302 /* save the current controller command byte */
2304 command_byte = get_controller_command_byte(sc->kbdc);
2305 if (command_byte == -1) {
2306 kbdc_lock(sc->kbdc, FALSE);
2311 /* disable the aux interrupt and temporalily disable the keyboard */
2312 if (!set_controller_command_byte(sc->kbdc,
2313 kbdc_get_device_mask(sc->kbdc),
2314 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2315 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2317 "psm%d: failed to disable the aux int (psmclose).\n",
2319 /* CONTROLLER ERROR;
2320 * NOTE: we shall force our way through. Because the only
2321 * ill effect we shall see is that we may not be able
2322 * to read ACK from the mouse, and it doesn't matter much
2323 * so long as the mouse will accept the DISABLE command.
2328 /* stop the watchdog timer */
2329 callout_stop(&sc->callout);
2331 /* remove anything left in the output buffer */
2332 empty_aux_buffer(sc->kbdc, 10);
2334 /* disable the aux device, port and interrupt */
2335 if (sc->state & PSM_VALID) {
2336 if (!disable_aux_dev(sc->kbdc)) {
2338 * NOTE: we don't return (error) and continue,
2339 * pretending we have successfully disabled the device.
2340 * It's OK because the interrupt routine will discard
2341 * any data from the mouse hereafter.
2344 "psm%d: failed to disable the device (psmclose).\n",
2348 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
2350 "psm%d: failed to get status (psmclose).\n",
2354 if (!set_controller_command_byte(sc->kbdc,
2355 kbdc_get_device_mask(sc->kbdc),
2356 (command_byte & KBD_KBD_CONTROL_BITS) |
2357 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2360 * we shall ignore this error; see the above comment.
2363 "psm%d: failed to disable the aux port (psmclose).\n",
2367 /* remove anything left in the output buffer */
2368 empty_aux_buffer(sc->kbdc, 10);
2370 /* close is almost always successful */
2371 kbdc_lock(sc->kbdc, FALSE);
2376 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
2379 static u_char butmapps2[8] = {
2381 MOUSE_PS2_BUTTON1DOWN,
2382 MOUSE_PS2_BUTTON2DOWN,
2383 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
2384 MOUSE_PS2_BUTTON3DOWN,
2385 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
2386 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
2387 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
2388 MOUSE_PS2_BUTTON3DOWN,
2390 static u_char butmapmsc[8] = {
2391 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
2392 MOUSE_MSC_BUTTON3UP,
2393 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
2394 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
2395 MOUSE_MSC_BUTTON3UP,
2396 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
2397 MOUSE_MSC_BUTTON2UP,
2398 MOUSE_MSC_BUTTON1UP,
2404 if (sc->mode.level == PSM_LEVEL_BASE) {
2405 mapped = status->button & ~MOUSE_BUTTON4DOWN;
2406 if (status->button & MOUSE_BUTTON4DOWN)
2407 mapped |= MOUSE_BUTTON1DOWN;
2408 status->button = mapped;
2409 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
2410 i = imax(imin(status->dx, 255), -256);
2412 buf[0] |= MOUSE_PS2_XNEG;
2414 i = imax(imin(status->dy, 255), -256);
2416 buf[0] |= MOUSE_PS2_YNEG;
2418 return (MOUSE_PS2_PACKETSIZE);
2419 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
2420 buf[0] = MOUSE_MSC_SYNC |
2421 butmapmsc[status->button & MOUSE_STDBUTTONS];
2422 i = imax(imin(status->dx, 255), -256);
2424 buf[3] = i - buf[1];
2425 i = imax(imin(status->dy, 255), -256);
2427 buf[4] = i - buf[2];
2428 i = imax(imin(status->dz, 127), -128);
2429 buf[5] = (i >> 1) & 0x7f;
2430 buf[6] = (i - (i >> 1)) & 0x7f;
2431 buf[7] = (~status->button >> 3) & 0x7f;
2432 return (MOUSE_SYS_PACKETSIZE);
2434 return (pb->inputbytes);
2438 psmread(struct cdev *dev, struct uio *uio, int flag)
2440 struct psm_softc *sc = dev->si_drv1;
2441 u_char buf[PSM_SMALLBUFSIZE];
2446 if ((sc->state & PSM_VALID) == 0)
2449 /* block until mouse activity occurred */
2451 while (sc->queue.count <= 0) {
2452 if (dev != sc->bdev) {
2454 return (EWOULDBLOCK);
2456 sc->state |= PSM_ASLP;
2457 error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
2458 sc->state &= ~PSM_ASLP;
2462 } else if ((sc->state & PSM_VALID) == 0) {
2463 /* the device disappeared! */
2470 /* copy data to the user land */
2471 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
2473 l = imin(sc->queue.count, uio->uio_resid);
2474 if (l > sizeof(buf))
2476 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
2477 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
2478 sizeof(sc->queue.buf) - sc->queue.head);
2479 bcopy(&sc->queue.buf[0],
2480 &buf[sizeof(sc->queue.buf) - sc->queue.head],
2481 l - (sizeof(sc->queue.buf) - sc->queue.head));
2483 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
2484 sc->queue.count -= l;
2485 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
2487 error = uiomove(buf, l, uio);
2496 block_mouse_data(struct psm_softc *sc, int *c)
2500 if (!kbdc_lock(sc->kbdc, TRUE))
2504 *c = get_controller_command_byte(sc->kbdc);
2505 if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
2506 kbdc_get_device_mask(sc->kbdc),
2507 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2508 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2509 /* this is CONTROLLER ERROR */
2511 kbdc_lock(sc->kbdc, FALSE);
2516 * The device may be in the middle of status data transmission.
2517 * The transmission will be interrupted, thus, incomplete status
2518 * data must be discarded. Although the aux interrupt is disabled
2519 * at the keyboard controller level, at most one aux interrupt
2520 * may have already been pending and a data byte is in the
2521 * output buffer; throw it away. Note that the second argument
2522 * to `empty_aux_buffer()' is zero, so that the call will just
2523 * flush the internal queue.
2524 * `psmintr()' will be invoked after `splx()' if an interrupt is
2525 * pending; it will see no data and returns immediately.
2527 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
2528 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
2536 dropqueue(struct psm_softc *sc)
2539 sc->queue.count = 0;
2542 if ((sc->state & PSM_SOFTARMED) != 0) {
2543 sc->state &= ~PSM_SOFTARMED;
2544 callout_stop(&sc->softcallout);
2546 sc->pqueue_start = sc->pqueue_end;
2550 flushpackets(struct psm_softc *sc)
2554 bzero(&sc->pqueue, sizeof(sc->pqueue));
2558 unblock_mouse_data(struct psm_softc *sc, int c)
2563 * We may have seen a part of status data during `set_mouse_XXX()'.
2564 * they have been queued; flush it.
2566 empty_aux_buffer(sc->kbdc, 0);
2568 /* restore ports and interrupt */
2569 if (!set_controller_command_byte(sc->kbdc,
2570 kbdc_get_device_mask(sc->kbdc),
2571 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
2573 * CONTROLLER ERROR; this is serious, we may have
2574 * been left with the inaccessible keyboard and
2575 * the disabled mouse interrupt.
2580 kbdc_lock(sc->kbdc, FALSE);
2585 psmwrite(struct cdev *dev, struct uio *uio, int flag)
2587 struct psm_softc *sc = dev->si_drv1;
2588 u_char buf[PSM_SMALLBUFSIZE];
2589 int error = 0, i, l;
2591 if ((sc->state & PSM_VALID) == 0)
2594 if (sc->mode.level < PSM_LEVEL_NATIVE)
2597 /* copy data from the user land */
2598 while (uio->uio_resid > 0) {
2599 l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
2600 error = uiomove(buf, l, uio);
2603 for (i = 0; i < l; i++) {
2604 VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
2605 if (!write_aux_command(sc->kbdc, buf[i])) {
2607 "psm: cmd 0x%x failed.\n", buf[i]));
2608 return (reinitialize(sc, FALSE));
2617 psmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2620 struct psm_softc *sc = dev->si_drv1;
2622 mousestatus_t status;
2629 /* Perform IOCTL command */
2631 case OLD_MOUSE_GETHWINFO:
2633 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
2634 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
2635 ((old_mousehw_t *)addr)->type = sc->hw.type;
2636 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
2640 case MOUSE_GETHWINFO:
2642 *(mousehw_t *)addr = sc->hw;
2643 if (sc->mode.level == PSM_LEVEL_BASE)
2644 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
2648 case MOUSE_SYN_GETHWINFO:
2650 if (sc->synhw.infoMajor >= 4)
2651 *(synapticshw_t *)addr = sc->synhw;
2657 case OLD_MOUSE_GETMODE:
2659 switch (sc->mode.level) {
2660 case PSM_LEVEL_BASE:
2661 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2663 case PSM_LEVEL_STANDARD:
2664 ((old_mousemode_t *)addr)->protocol =
2665 MOUSE_PROTO_SYSMOUSE;
2667 case PSM_LEVEL_NATIVE:
2668 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2671 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
2672 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2673 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2679 *(mousemode_t *)addr = sc->mode;
2680 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2681 ((mousemode_t *)addr)->syncmask[0] = 0;
2682 ((mousemode_t *)addr)->syncmask[1] = 0;
2684 ((mousemode_t *)addr)->resolution =
2685 MOUSE_RES_LOW - sc->mode.resolution;
2686 switch (sc->mode.level) {
2687 case PSM_LEVEL_BASE:
2688 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2689 ((mousemode_t *)addr)->packetsize =
2690 MOUSE_PS2_PACKETSIZE;
2692 case PSM_LEVEL_STANDARD:
2693 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2694 ((mousemode_t *)addr)->packetsize =
2695 MOUSE_SYS_PACKETSIZE;
2696 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2697 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2699 case PSM_LEVEL_NATIVE:
2700 /* FIXME: this isn't quite correct... XXX */
2701 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2707 case OLD_MOUSE_SETMODE:
2709 if (cmd == OLD_MOUSE_SETMODE) {
2710 mode.rate = ((old_mousemode_t *)addr)->rate;
2712 * resolution old I/F new I/F
2719 if (((old_mousemode_t *)addr)->resolution > 0)
2721 -((old_mousemode_t *)addr)->resolution - 1;
2723 mode.resolution = 0;
2725 ((old_mousemode_t *)addr)->accelfactor;
2728 mode = *(mousemode_t *)addr;
2730 /* adjust and validate parameters. */
2731 if (mode.rate > UCHAR_MAX)
2734 mode.rate = sc->dflt_mode.rate;
2735 else if (mode.rate == -1)
2736 /* don't change the current setting */
2738 else if (mode.rate < 0)
2740 if (mode.resolution >= UCHAR_MAX)
2742 if (mode.resolution >= 200)
2743 mode.resolution = MOUSE_RES_HIGH;
2744 else if (mode.resolution >= 100)
2745 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2746 else if (mode.resolution >= 50)
2747 mode.resolution = MOUSE_RES_MEDIUMLOW;
2748 else if (mode.resolution > 0)
2749 mode.resolution = MOUSE_RES_LOW;
2750 if (mode.resolution == MOUSE_RES_DEFAULT)
2751 mode.resolution = sc->dflt_mode.resolution;
2752 else if (mode.resolution == -1)
2753 /* don't change the current setting */
2755 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2756 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2757 if (mode.level == -1)
2758 /* don't change the current setting */
2759 mode.level = sc->mode.level;
2760 else if ((mode.level < PSM_LEVEL_MIN) ||
2761 (mode.level > PSM_LEVEL_MAX))
2763 if (mode.accelfactor == -1)
2764 /* don't change the current setting */
2765 mode.accelfactor = sc->mode.accelfactor;
2766 else if (mode.accelfactor < 0)
2769 /* don't allow anybody to poll the keyboard controller */
2770 error = block_mouse_data(sc, &command_byte);
2774 /* set mouse parameters */
2776 mode.rate = set_mouse_sampling_rate(sc->kbdc,
2778 if (mode.resolution >= 0)
2780 set_mouse_resolution(sc->kbdc, mode.resolution);
2781 set_mouse_scaling(sc->kbdc, 1);
2782 get_mouse_status(sc->kbdc, stat, 0, 3);
2785 sc->mode.rate = mode.rate;
2786 sc->mode.resolution = mode.resolution;
2787 sc->mode.accelfactor = mode.accelfactor;
2788 sc->mode.level = mode.level;
2791 unblock_mouse_data(sc, command_byte);
2794 case MOUSE_GETLEVEL:
2795 *(int *)addr = sc->mode.level;
2798 case MOUSE_SETLEVEL:
2799 if ((*(int *)addr < PSM_LEVEL_MIN) ||
2800 (*(int *)addr > PSM_LEVEL_MAX))
2802 sc->mode.level = *(int *)addr;
2805 case MOUSE_GETSTATUS:
2807 status = sc->status;
2808 sc->status.flags = 0;
2809 sc->status.obutton = sc->status.button;
2810 sc->status.button = 0;
2815 *(mousestatus_t *)addr = status;
2818 case MOUSE_READSTATE:
2819 case MOUSE_READDATA:
2820 data = (mousedata_t *)addr;
2821 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2824 error = block_mouse_data(sc, &command_byte);
2827 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2828 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2830 unblock_mouse_data(sc, command_byte);
2833 #if (defined(MOUSE_SETRESOLUTION))
2834 case MOUSE_SETRESOLUTION:
2835 mode.resolution = *(int *)addr;
2836 if (mode.resolution >= UCHAR_MAX)
2838 else if (mode.resolution >= 200)
2839 mode.resolution = MOUSE_RES_HIGH;
2840 else if (mode.resolution >= 100)
2841 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2842 else if (mode.resolution >= 50)
2843 mode.resolution = MOUSE_RES_MEDIUMLOW;
2844 else if (mode.resolution > 0)
2845 mode.resolution = MOUSE_RES_LOW;
2846 if (mode.resolution == MOUSE_RES_DEFAULT)
2847 mode.resolution = sc->dflt_mode.resolution;
2848 else if (mode.resolution == -1)
2849 mode.resolution = sc->mode.resolution;
2850 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2851 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2853 error = block_mouse_data(sc, &command_byte);
2856 sc->mode.resolution =
2857 set_mouse_resolution(sc->kbdc, mode.resolution);
2858 if (sc->mode.resolution != mode.resolution)
2860 unblock_mouse_data(sc, command_byte);
2862 #endif /* MOUSE_SETRESOLUTION */
2864 #if (defined(MOUSE_SETRATE))
2866 mode.rate = *(int *)addr;
2867 if (mode.rate > UCHAR_MAX)
2870 mode.rate = sc->dflt_mode.rate;
2871 else if (mode.rate < 0)
2872 mode.rate = sc->mode.rate;
2874 error = block_mouse_data(sc, &command_byte);
2877 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2878 if (sc->mode.rate != mode.rate)
2880 unblock_mouse_data(sc, command_byte);
2882 #endif /* MOUSE_SETRATE */
2884 #if (defined(MOUSE_SETSCALING))
2885 case MOUSE_SETSCALING:
2886 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2889 error = block_mouse_data(sc, &command_byte);
2892 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2894 unblock_mouse_data(sc, command_byte);
2896 #endif /* MOUSE_SETSCALING */
2898 #if (defined(MOUSE_GETHWID))
2900 error = block_mouse_data(sc, &command_byte);
2903 sc->hw.hwid &= ~0x00ff;
2904 sc->hw.hwid |= get_aux_id(sc->kbdc);
2905 *(int *)addr = sc->hw.hwid & 0x00ff;
2906 unblock_mouse_data(sc, command_byte);
2908 #endif /* MOUSE_GETHWID */
2914 error = fsetown(*(int *)addr, &sc->async);
2917 *(int *) addr = fgetown(&sc->async);
2927 psmtimeout(void *arg)
2929 struct psm_softc *sc;
2932 sc = (struct psm_softc *)arg;
2934 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2935 VLOG(6, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2937 kbdc_lock(sc->kbdc, FALSE);
2939 sc->watchdog = TRUE;
2941 callout_reset(&sc->callout, hz, psmtimeout, sc);
2944 /* Add all sysctls under the debug.psm and hw.psm nodes */
2945 static SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
2947 static SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
2950 SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RWTUN, &verbose, 0,
2953 static int psmhz = 20;
2954 SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2955 "Frequency of the softcallout (in hz)");
2956 static int psmerrsecs = 2;
2957 SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2958 "Number of seconds during which packets will dropped after a sync error");
2959 static int psmerrusecs = 0;
2960 SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2961 "Microseconds to add to psmerrsecs");
2962 static int psmsecs = 0;
2963 SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2964 "Max number of seconds between soft interrupts");
2965 static int psmusecs = 500000;
2966 SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2967 "Microseconds to add to psmsecs");
2968 static int pkterrthresh = 2;
2969 SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2970 "Number of error packets allowed before reinitializing the mouse");
2972 SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RWTUN, &tap_enabled, 0,
2973 "Enable tap and drag gestures");
2974 static int tap_threshold = PSM_TAP_THRESHOLD;
2975 SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2976 "Button tap threshold");
2977 static int tap_timeout = PSM_TAP_TIMEOUT;
2978 SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2979 "Tap timeout for touchpads");
2982 SYSCTL_INT(_hw_psm, OID_AUTO, synaptics_support, CTLFLAG_RDTUN,
2983 &synaptics_support, 0, "Enable support for Synaptics touchpads");
2985 SYSCTL_INT(_hw_psm, OID_AUTO, trackpoint_support, CTLFLAG_RDTUN,
2986 &trackpoint_support, 0, "Enable support for IBM/Lenovo TrackPoint");
2988 SYSCTL_INT(_hw_psm, OID_AUTO, elantech_support, CTLFLAG_RDTUN,
2989 &elantech_support, 0, "Enable support for Elantech touchpads");
2991 SYSCTL_INT(_hw_psm, OID_AUTO, mux_disabled, CTLFLAG_RDTUN,
2992 &mux_disabled, 0, "Disable active multiplexing");
2997 struct psm_softc *sc = arg;
3002 if (aux_mux_is_enabled(sc->kbdc))
3003 VLOG(2, (LOG_DEBUG, "psmintr: active multiplexing mode is not "
3006 /* read until there is nothing to read */
3007 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
3008 pb = &sc->pqueue[sc->pqueue_end];
3010 /* discard the byte if the device is not open */
3011 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
3014 getmicrouptime(&now);
3015 if ((pb->inputbytes > 0) &&
3016 timevalcmp(&now, &sc->inputtimeout, >)) {
3017 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
3018 "resetting byte count\n"));
3023 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
3024 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
3025 timevaladd(&sc->inputtimeout, &now);
3027 pb->ipacket[pb->inputbytes++] = c;
3029 if (sc->mode.level == PSM_LEVEL_NATIVE) {
3030 VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
3035 if (pb->inputbytes < sc->mode.packetsize)
3039 "psmintr: %02x %02x %02x %02x %02x %02x\n",
3040 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
3041 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
3046 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
3047 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
3048 sc->flags &= ~PSM_NEED_SYNCBITS;
3050 "psmintr: Sync bytes now %04x,%04x\n",
3051 sc->mode.syncmask[0], sc->mode.syncmask[1]));
3052 } else if ((sc->config & PSM_CONFIG_NOCHECKSYNC) == 0 &&
3053 (c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
3054 VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
3055 "(%04x != %04x) %d cmds since last error.\n",
3056 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
3057 sc->cmdcount - sc->lasterr));
3058 sc->lasterr = sc->cmdcount;
3060 * The sync byte test is a weak measure of packet
3061 * validity. Conservatively discard any input yet
3062 * to be seen by userland when we detect a sync
3063 * error since there is a good chance some of
3064 * the queued packets have undetected errors.
3067 if (sc->syncerrors == 0)
3070 sc->lastinputerr = now;
3071 if (sc->syncerrors >= sc->mode.packetsize * 2 ||
3072 sc->pkterrors >= pkterrthresh) {
3074 * If we've failed to find a single sync byte
3075 * in 2 packets worth of data, or we've seen
3076 * persistent packet errors during the
3077 * validation period, reinitialize the mouse
3078 * in hopes of returning it to the expected
3082 "psmintr: reset the mouse.\n"));
3083 reinitialize(sc, TRUE);
3084 } else if (sc->syncerrors == sc->mode.packetsize) {
3086 * Try a soft reset after searching for a sync
3087 * byte through a packet length of bytes.
3090 "psmintr: re-enable the mouse.\n"));
3092 disable_aux_dev(sc->kbdc);
3093 enable_aux_dev(sc->kbdc);
3096 "psmintr: discard a byte (%d)\n",
3099 bcopy(&pb->ipacket[1], &pb->ipacket[0],
3106 * We have what appears to be a valid packet.
3107 * Reset the error counters.
3112 * Drop even good packets if they occur within a timeout
3113 * period of a sync error. This allows the detection of
3114 * a change in the mouse's packet mode without exposing
3115 * erratic mouse behavior to the user. Some KVMs forget
3116 * enhanced mouse modes during switch events.
3118 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
3125 * Now that we're out of the validation period, reset
3126 * the packet error count.
3132 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
3135 * If we've filled the queue then call the softintr ourselves,
3136 * otherwise schedule the interrupt for later.
3137 * Do not postpone interrupts for absolute devices as it
3138 * affects tap detection timings.
3140 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS ||
3141 sc->hw.model == MOUSE_MODEL_ELANTECH ||
3142 !timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
3143 (sc->pqueue_end == sc->pqueue_start)) {
3144 if ((sc->state & PSM_SOFTARMED) != 0) {
3145 sc->state &= ~PSM_SOFTARMED;
3146 callout_stop(&sc->softcallout);
3149 } else if ((sc->state & PSM_SOFTARMED) == 0) {
3150 sc->state |= PSM_SOFTARMED;
3151 callout_reset(&sc->softcallout,
3152 psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg);
3158 proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3159 int *x, int *y, int *z)
3163 * PS2++ protocol packet
3165 * b7 b6 b5 b4 b3 b2 b1 b0
3166 * byte 1: * 1 p3 p2 1 * * *
3167 * byte 2: c1 c2 p1 p0 d1 d0 1 0
3169 * p3-p0: packet type
3170 * c1, c2: c1 & c2 == 1, if p2 == 0
3171 * c1 & c2 == 0, if p2 == 1
3173 * packet type: 0 (device type)
3174 * See comments in enable_mmanplus() below.
3176 * packet type: 1 (wheel data)
3178 * b7 b6 b5 b4 b3 b2 b1 b0
3179 * byte 3: h * B5 B4 s d2 d1 d0
3181 * h: 1, if horizontal roller data
3182 * 0, if vertical roller data
3183 * B4, B5: button 4 and 5
3185 * d2-d0: roller data
3187 * packet type: 2 (reserved)
3189 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
3190 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
3192 * the extended data packet encodes button
3195 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
3197 /* wheel data packet */
3199 if (pb->ipacket[2] & 0x80) {
3200 /* XXX horizontal roller count - ignore it */
3203 /* vertical roller count */
3204 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
3205 (pb->ipacket[2] & 0x0f) - 16 :
3206 (pb->ipacket[2] & 0x0f);
3208 ms->button |= (pb->ipacket[2] &
3209 MOUSE_PS2PLUS_BUTTON4DOWN) ?
3210 MOUSE_BUTTON4DOWN : 0;
3211 ms->button |= (pb->ipacket[2] &
3212 MOUSE_PS2PLUS_BUTTON5DOWN) ?
3213 MOUSE_BUTTON5DOWN : 0;
3217 * this packet type is reserved by
3221 * IBM ScrollPoint Mouse uses this
3222 * packet type to encode both vertical
3223 * and horizontal scroll movement.
3226 /* horizontal count */
3227 if (pb->ipacket[2] & 0x0f)
3228 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
3230 /* vertical count */
3231 if (pb->ipacket[2] & 0xf0)
3232 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
3236 /* device type packet - shouldn't happen */
3240 ms->button = ms->obutton;
3241 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
3242 "type %d: 0x%02x 0x%02x 0x%02x\n",
3243 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
3244 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
3248 /* preserve button states */
3249 ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
3254 proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3255 int *x, int *y, int *z)
3257 static int touchpad_buttons;
3258 static int guest_buttons;
3259 static int ew_finger_count;
3260 static finger_t f[PSM_FINGERS];
3261 int w, id, nfingers, palm, ewcode, extended_buttons, clickpad_pressed;
3263 extended_buttons = 0;
3265 /* TouchPad PS/2 absolute mode message format with capFourButtons:
3267 * Bits: 7 6 5 4 3 2 1 0 (LSB)
3268 * ------------------------------------------------
3269 * ipacket[0]: 1 0 W3 W2 0 W1 R L
3270 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
3271 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
3272 * ipacket[3]: 1 1 Yc Xc 0 W0 D^R U^L
3273 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
3274 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3277 * L: left physical mouse button
3278 * R: right physical mouse button
3286 * Without capFourButtons but with nExtendeButtons and/or capMiddle
3288 * Bits: 7 6 5 4 3 2 1 0 (LSB)
3289 * ------------------------------------------------------
3290 * ipacket[3]: 1 1 Yc Xc 0 W0 E^R M^L
3291 * ipacket[4]: X7 X6 X5 X4 X3|b7 X2|b5 X1|b3 X0|b1
3292 * ipacket[5]: Y7 Y6 Y5 Y4 Y3|b8 Y2|b6 Y1|b4 Y0|b2
3295 * M: Middle physical mouse button
3296 * E: Extended mouse buttons reported instead of low bits of X and Y
3297 * b1-b8: Extended mouse buttons
3298 * Only ((nExtendedButtons + 1) >> 1) bits are used in packet
3299 * 4 and 5, for reading X and Y value they should be zeroed.
3301 * Absolute reportable limits: 0 - 6143.
3302 * Typical bezel limits: 1472 - 5472.
3303 * Typical edge marings: 1632 - 5312.
3305 * w = 3 Passthrough Packet
3307 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
3310 if (!synaptics_support)
3313 /* Sanity check for out of sync packets. */
3314 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
3315 (pb->ipacket[3] & 0xc8) != 0xc0)
3319 ms->button = ms->obutton;
3324 * z = 0 No finger contact
3325 * z = 10 Finger hovering near the pad
3326 * z = 30 Very light finger contact
3327 * z = 80 Normal finger contact
3328 * z = 110 Very heavy finger contact
3329 * z = 200 Finger lying flat on pad surface
3330 * z = 255 Maximum reportable Z
3332 *z = pb->ipacket[2];
3335 * Finger width value
3337 * w = 0 Two finger on the pad (capMultiFinger needed)
3338 * w = 1 Three or more fingers (capMultiFinger needed)
3339 * w = 2 Pen (instead of finger) (capPen needed)
3340 * w = 3 Reserved (passthrough?)
3341 * w = 4-7 Finger of normal width (capPalmDetect needed)
3342 * w = 8-14 Very wide finger or palm (capPalmDetect needed)
3343 * w = 15 Maximum reportable width (capPalmDetect needed)
3345 /* XXX Is checking capExtended enough? */
3346 if (sc->synhw.capExtended)
3347 w = ((pb->ipacket[0] & 0x30) >> 2) |
3348 ((pb->ipacket[0] & 0x04) >> 1) |
3349 ((pb->ipacket[3] & 0x04) >> 2);
3351 /* Assume a finger of regular width. */
3358 * Handle packets from the guest device. See:
3359 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
3361 if (sc->synhw.capPassthrough || sc->muxport != PSM_NOMUX) {
3362 *x = ((pb->ipacket[1] & 0x10) ?
3363 pb->ipacket[4] - 256 : pb->ipacket[4]);
3364 *y = ((pb->ipacket[1] & 0x20) ?
3365 pb->ipacket[5] - 256 : pb->ipacket[5]);
3369 if (pb->ipacket[1] & 0x01)
3370 guest_buttons |= MOUSE_BUTTON1DOWN;
3371 if (pb->ipacket[1] & 0x04)
3372 guest_buttons |= MOUSE_BUTTON2DOWN;
3373 if (pb->ipacket[1] & 0x02)
3374 guest_buttons |= MOUSE_BUTTON3DOWN;
3375 #ifdef EVDEV_SUPPORT
3376 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3377 evdev_push_rel(sc->evdev_r, REL_X, *x);
3378 evdev_push_rel(sc->evdev_r, REL_Y, -*y);
3379 evdev_push_mouse_btn(sc->evdev_r,
3380 guest_buttons | sc->extended_buttons);
3381 evdev_sync(sc->evdev_r);
3384 ms->button = touchpad_buttons | guest_buttons |
3385 sc->extended_buttons;
3390 /* Handle Extended W mode packets */
3391 ewcode = (pb->ipacket[5] & 0xf0) >> 4;
3395 /* Secondary finger */
3396 if (sc->synhw.capAdvancedGestures)
3398 .x = (((pb->ipacket[4] & 0x0f) << 8) |
3399 pb->ipacket[1]) << 1,
3400 .y = (((pb->ipacket[4] & 0xf0) << 4) |
3401 pb->ipacket[2]) << 1,
3402 .p = ((pb->ipacket[3] & 0x30) |
3403 (pb->ipacket[5] & 0x0f)) << 1,
3404 .w = PSM_FINGER_DEFAULT_W,
3405 .flags = PSM_FINGER_FUZZY,
3407 else if (sc->synhw.capReportsV)
3409 .x = (((pb->ipacket[4] & 0x0f) << 8) |
3410 (pb->ipacket[1] & 0xfe)) << 1,
3411 .y = (((pb->ipacket[4] & 0xf0) << 4) |
3412 (pb->ipacket[2] & 0xfe)) << 1,
3413 .p = ((pb->ipacket[3] & 0x30) |
3414 (pb->ipacket[5] & 0x0e)) << 1,
3415 .w = (((pb->ipacket[5] & 0x01) << 2) |
3416 ((pb->ipacket[2] & 0x01) << 1) |
3417 (pb->ipacket[1] & 0x01)) + 8,
3418 .flags = PSM_FINGER_FUZZY,
3422 ew_finger_count = pb->ipacket[1] & 0x0f;
3430 if (sc->synhw.capReportsV && ew_finger_count > 3) {
3431 nfingers = ew_finger_count;
3443 if (sc->syninfo.touchpad_off)
3446 /* Button presses */
3447 touchpad_buttons = 0;
3448 if (pb->ipacket[0] & 0x01)
3449 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3450 if (pb->ipacket[0] & 0x02)
3451 touchpad_buttons |= MOUSE_BUTTON3DOWN;
3453 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3454 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
3455 touchpad_buttons |= MOUSE_BUTTON4DOWN;
3456 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
3457 touchpad_buttons |= MOUSE_BUTTON5DOWN;
3458 } else if (sc->synhw.capExtended && sc->synhw.capMiddle &&
3459 !sc->synhw.capClickPad) {
3461 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
3462 touchpad_buttons |= MOUSE_BUTTON2DOWN;
3463 } else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
3464 /* Extended Buttons */
3465 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
3466 if (sc->syninfo.directional_scrolls) {
3467 if (pb->ipacket[4] & 0x01)
3468 extended_buttons |= MOUSE_BUTTON4DOWN;
3469 if (pb->ipacket[5] & 0x01)
3470 extended_buttons |= MOUSE_BUTTON5DOWN;
3471 if (pb->ipacket[4] & 0x02)
3472 extended_buttons |= MOUSE_BUTTON6DOWN;
3473 if (pb->ipacket[5] & 0x02)
3474 extended_buttons |= MOUSE_BUTTON7DOWN;
3476 if (pb->ipacket[4] & 0x01)
3477 extended_buttons |= MOUSE_BUTTON1DOWN;
3478 if (pb->ipacket[5] & 0x01)
3479 extended_buttons |= MOUSE_BUTTON3DOWN;
3480 if (pb->ipacket[4] & 0x02)
3481 extended_buttons |= MOUSE_BUTTON2DOWN;
3482 sc->extended_buttons = extended_buttons;
3486 * Zero out bits used by extended buttons to avoid
3487 * misinterpretation of the data absolute position.
3489 * The bits represented by
3491 * (nExtendedButtons + 1) >> 1
3493 * will be masked out in both bytes.
3494 * The mask for n bits is computed with the formula
3500 maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
3501 mask = (1 << maskedbits) - 1;
3502 #ifdef EVDEV_SUPPORT
3504 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3505 if (sc->synhw.capPassthrough) {
3506 evdev_push_mouse_btn(sc->evdev_r,
3508 evdev_sync(sc->evdev_r);
3510 for (i = 0; i < maskedbits; i++) {
3511 evdev_push_key(sc->evdev_a,
3513 pb->ipacket[4] & (1 << i));
3514 evdev_push_key(sc->evdev_a,
3516 pb->ipacket[5] & (1 << i));
3520 pb->ipacket[4] &= ~(mask);
3521 pb->ipacket[5] &= ~(mask);
3522 } else if (!sc->syninfo.directional_scrolls &&
3523 !sc->gesture.in_vscroll) {
3525 * Keep reporting MOUSE DOWN until we get a new packet
3526 * indicating otherwise.
3528 extended_buttons |= sc->extended_buttons;
3532 if (sc->synhw.capReportsV && nfingers > 1)
3534 .x = ((pb->ipacket[3] & 0x10) << 8) |
3535 ((pb->ipacket[1] & 0x0f) << 8) |
3536 (pb->ipacket[4] & 0xfd),
3537 .y = ((pb->ipacket[3] & 0x20) << 7) |
3538 ((pb->ipacket[1] & 0xf0) << 4) |
3539 (pb->ipacket[5] & 0xfd),
3541 .w = (((pb->ipacket[2] & 0x01) << 2) |
3542 (pb->ipacket[5] & 0x02) |
3543 ((pb->ipacket[4] & 0x02) >> 1)) + 8,
3544 .flags = PSM_FINGER_FUZZY,
3548 .x = ((pb->ipacket[3] & 0x10) << 8) |
3549 ((pb->ipacket[1] & 0x0f) << 8) |
3551 .y = ((pb->ipacket[3] & 0x20) << 7) |
3552 ((pb->ipacket[1] & 0xf0) << 4) |
3556 .flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0,
3559 /* Ignore hovering and unmeasurable touches */
3560 if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2)
3563 /* Handle ClickPad */
3564 if (sc->synhw.capClickPad) {
3565 clickpad_pressed = (pb->ipacket[0] ^ pb->ipacket[3]) & 0x01;
3566 if (sc->synhw.forcePad) {
3568 * Forcepads erroneously report button click if there
3569 * are 2 or more fingers on the touchpad breaking
3570 * multifinger gestures. To workaround this start
3571 * reporting a click only after 4 consecutive single
3572 * touch packets has been received.
3573 * Skip these packets in case more contacts appear.
3580 if (clickpad_pressed && sc->fpcount < INT_MAX)
3584 if (!clickpad_pressed)
3586 if (sc->fpcount >= sc->syninfo.window_min)
3587 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3589 } else if (clickpad_pressed)
3590 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3593 for (id = 0; id < PSM_FINGERS; id++)
3595 PSM_FINGER_RESET(f[id]);
3597 #ifdef EVDEV_SUPPORT
3598 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3599 for (id = 0; id < PSM_FINGERS; id++)
3600 if (PSM_FINGER_IS_SET(f[id]))
3601 psm_push_mt_finger(sc, id, &f[id]);
3602 evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
3603 evdev_push_nfingers(sc->evdev_a, nfingers);
3605 psm_push_st_finger(sc, &f[0]);
3607 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
3608 evdev_push_mouse_btn(sc->evdev_a, touchpad_buttons);
3609 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3610 evdev_push_key(sc->evdev_a, BTN_FORWARD,
3611 touchpad_buttons & MOUSE_BUTTON4DOWN);
3612 evdev_push_key(sc->evdev_a, BTN_BACK,
3613 touchpad_buttons & MOUSE_BUTTON5DOWN);
3615 evdev_sync(sc->evdev_a);
3619 ms->button = touchpad_buttons;
3621 palm = psmpalmdetect(sc, &f[0], nfingers);
3623 /* Palm detection doesn't terminate the current action. */
3625 psmgestures(sc, &f[0], nfingers, ms);
3627 for (id = 0; id < PSM_FINGERS; id++)
3628 psmsmoother(sc, &f[id], id, ms, x, y);
3632 ms->button = ms->obutton;
3636 ms->button |= extended_buttons | guest_buttons;
3640 * Use the extra buttons as a scrollwheel
3642 * XXX X.Org uses the Z axis for vertical wheel only,
3643 * whereas moused(8) understands special values to differ
3644 * vertical and horizontal wheels.
3646 * xf86-input-mouse needs therefore a small patch to
3647 * understand these special values. Without it, the
3648 * horizontal wheel acts as a vertical wheel in X.Org.
3650 * That's why the horizontal wheel is disabled by
3653 if (ms->button & MOUSE_BUTTON4DOWN)
3655 else if (ms->button & MOUSE_BUTTON5DOWN)
3657 else if (ms->button & MOUSE_BUTTON6DOWN)
3659 else if (ms->button & MOUSE_BUTTON7DOWN)
3663 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
3664 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
3670 proc_synaptics_mux(struct psm_softc *sc, packetbuf_t *pb)
3675 * Convert 3-byte interleaved mixture of Synaptics and generic mouse
3676 * packets into plain 6-byte Synaptics packet protocol.
3677 * While in hidden multiplexing mode KBC does some editing of the
3678 * packet stream. It remembers the button bits from the last packet
3679 * received from each device, and replaces the button bits of every
3680 * packet with the logical OR of all devices’ most recent button bits.
3681 * This button crosstalk should be filtered out as Synaptics and
3682 * generic mouse encode middle button presses in a different way.
3684 switch (pb->ipacket[0] & 0xc0) {
3685 case 0x80: /* First 3 bytes of Synaptics packet */
3686 bcopy(pb->ipacket, sc->muxsave, 3);
3687 /* Compute middle mouse button supression timeout. */
3688 sc->muxmidtimeout.tv_sec = 0;
3689 sc->muxmidtimeout.tv_usec = 50000; /* ~2-3 ints */
3690 timevaladd(&sc->muxmidtimeout, &sc->lastsoftintr);
3693 case 0xc0: /* Second 3 bytes of Synaptics packet */
3694 /* Join two 3-bytes absolute packets */
3695 bcopy(pb->ipacket, pb->ipacket + 3, 3);
3696 bcopy(sc->muxsave, pb->ipacket, 3);
3697 /* Prefer trackpoint buttons over touchpad's */
3698 pb->ipacket[0] &= ~(0x08 | sc->muxmsbuttons);
3699 pb->ipacket[3] &= ~(0x08 | sc->muxmsbuttons);
3700 butt = (pb->ipacket[3] & 0x03) << 2 | (pb->ipacket[0] & 0x03);
3701 /* Add hysteresis to remove spurious middle button events */
3702 if (butt != sc->muxtpbuttons && sc->fpcount < 1) {
3703 pb->ipacket[0] &= 0xfc;
3704 pb->ipacket[0] |= sc->muxtpbuttons & 0x03;
3705 pb->ipacket[3] &= 0xfc;
3706 pb->ipacket[3] |= sc->muxtpbuttons >> 2 & 0x03;
3710 sc->muxtpbuttons = butt;
3712 /* Filter out impossible w induced by middle trackpoint btn */
3713 if (sc->synhw.capExtended && !sc->synhw.capPassthrough &&
3714 (pb->ipacket[0] & 0x34) == 0x04 &&
3715 (pb->ipacket[3] & 0x04) == 0x04) {
3716 pb->ipacket[0] &= 0xfb;
3717 pb->ipacket[3] &= 0xfb;
3719 sc->muxsave[0] &= 0x30;
3722 default: /* Generic mouse (Trackpoint) packet */
3723 /* Filter out middle button events induced by some w values */
3724 if (sc->muxmsbuttons & 0x03 || pb->ipacket[0] & 0x03 ||
3725 (timevalcmp(&sc->lastsoftintr, &sc->muxmidtimeout, <=) &&
3726 (sc->muxsave[0] & 0x30 || sc->muxsave[2] > 8)))
3727 pb->ipacket[0] &= 0xfb;
3728 sc->muxmsbuttons = pb->ipacket[0] & 0x07;
3729 /* Convert to Synaptics pass-through protocol */
3730 pb->ipacket[4] = pb->ipacket[1];
3731 pb->ipacket[5] = pb->ipacket[2];
3732 pb->ipacket[1] = pb->ipacket[0];
3734 pb->ipacket[0] = 0x84 | (sc->muxtpbuttons & 0x03);
3735 pb->ipacket[3] = 0xc4 | (sc->muxtpbuttons >> 2 & 0x03);
3738 VLOG(4, (LOG_DEBUG, "synaptics: %02x %02x %02x %02x %02x %02x\n",
3739 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
3740 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
3742 pb->inputbytes = MOUSE_SYNAPTICS_PACKETSIZE;
3747 psmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers)
3750 ((sc->synhw.capMultiFinger || sc->synhw.capAdvancedGestures) &&
3751 !sc->synhw.capReportsV && nfingers > 1) ||
3752 (sc->synhw.capReportsV && nfingers > 2) ||
3753 (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) ||
3754 (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) ||
3755 (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) {
3757 * We consider the packet irrelevant for the current
3759 * - the width isn't comprised in:
3761 * - the pressure isn't comprised in:
3762 * [min_pressure; max_pressure]
3763 * - pen aren't supported but PSM_FINGER_IS_PEN is set
3765 VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f->w));
3772 psmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers,
3775 smoother_t *smoother;
3778 int y_ok, center_button, center_x, right_button, right_x, i;
3781 smoother = &sc->smoother[0];
3782 gest = &sc->gesture;
3784 /* Find first active finger. */
3786 for (i = 0; i < PSM_FINGERS; i++) {
3787 if (PSM_FINGER_IS_SET(fingers[i])) {
3789 smoother = &sc->smoother[i];
3796 * Check pressure to detect a real wanted action on the
3799 if (f->p >= sc->syninfo.min_pressure) {
3802 int start_x, start_y;
3804 int margin_top, margin_right, margin_bottom, margin_left;
3805 int window_min, window_max;
3806 int vscroll_hor_area, vscroll_ver_area;
3807 int two_finger_scroll;
3809 int three_finger_drag;
3812 /* XXX Verify values? */
3813 margin_top = sc->syninfo.margin_top;
3814 margin_right = sc->syninfo.margin_right;
3815 margin_bottom = sc->syninfo.margin_bottom;
3816 margin_left = sc->syninfo.margin_left;
3817 window_min = sc->syninfo.window_min;
3818 window_max = sc->syninfo.window_max;
3819 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
3820 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
3821 two_finger_scroll = sc->syninfo.two_finger_scroll;
3822 max_x = sc->syninfo.max_x;
3823 max_y = sc->syninfo.max_y;
3824 three_finger_drag = sc->syninfo.three_finger_drag;
3825 /* Read current absolute position. */
3830 * Limit the coordinates to the specified margins because
3831 * this area isn't very reliable.
3833 if (x0 <= margin_left)
3835 else if (x0 >= max_x - margin_right)
3836 x0 = max_x - margin_right;
3837 if (y0 <= margin_bottom)
3839 else if (y0 >= max_y - margin_top)
3840 y0 = max_y - margin_top;
3842 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
3843 x0, y0, f->p, f->w));
3846 * If the action is just beginning, init the structure and
3847 * compute tap timeout.
3849 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
3850 VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
3852 /* Initialize queue. */
3853 gest->window_min = window_min;
3855 /* Reset pressure peak. */
3858 /* Reset fingers count. */
3859 gest->fingers_nb = 0;
3861 /* Reset virtual scrolling state. */
3862 gest->in_vscroll = 0;
3864 /* Compute tap timeout. */
3865 if (tap_enabled != 0) {
3866 gest->taptimeout = (struct timeval) {
3867 .tv_sec = tap_timeout / 1000000,
3868 .tv_usec = tap_timeout % 1000000,
3871 &gest->taptimeout, &sc->lastsoftintr);
3873 timevalclear(&gest->taptimeout);
3875 sc->flags |= PSM_FLAGS_FINGERDOWN;
3877 /* Smoother has not been reset yet */
3882 queue_len = smoother->queue_len + 1;
3883 start_x = smoother->start_x;
3884 start_y = smoother->start_y;
3887 /* Process ClickPad softbuttons */
3888 if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) {
3889 y_ok = sc->syninfo.softbuttons_y >= 0 ?
3890 start_y < sc->syninfo.softbuttons_y :
3891 start_y > max_y + sc->syninfo.softbuttons_y;
3893 center_button = MOUSE_BUTTON2DOWN;
3894 center_x = sc->syninfo.softbutton2_x;
3895 right_button = MOUSE_BUTTON3DOWN;
3896 right_x = sc->syninfo.softbutton3_x;
3898 if (center_x > 0 && right_x > 0 && center_x > right_x) {
3899 center_button = MOUSE_BUTTON3DOWN;
3900 center_x = sc->syninfo.softbutton3_x;
3901 right_button = MOUSE_BUTTON2DOWN;
3902 right_x = sc->syninfo.softbutton2_x;
3905 if (right_x > 0 && start_x > right_x && y_ok)
3906 ms->button = (ms->button &
3907 ~MOUSE_BUTTON1DOWN) | right_button;
3908 else if (center_x > 0 && start_x > center_x && y_ok)
3909 ms->button = (ms->button &
3910 ~MOUSE_BUTTON1DOWN) | center_button;
3913 /* If in tap-hold or three fingers, add the recorded button. */
3914 if (gest->in_taphold || (nfingers == 3 && three_finger_drag))
3915 ms->button |= gest->tap_button;
3918 * For tap, we keep the maximum number of fingers and the
3919 * pressure peak. Also with multiple fingers, we increase
3920 * the minimum window.
3923 gest->window_min = window_max;
3924 gest->fingers_nb = imax(nfingers, gest->fingers_nb);
3925 gest->zmax = imax(f->p, gest->zmax);
3927 /* Do we have enough packets to consider this a gesture? */
3928 if (queue_len < gest->window_min)
3934 /* Is a scrolling action occurring? */
3935 if (!gest->in_taphold && !ms->button &&
3936 (!gest->in_vscroll || two_finger_scroll)) {
3938 * A scrolling action must not conflict with a tap
3939 * action. Here are the conditions to consider a
3941 * - the action in a configurable area
3942 * - one of the following:
3943 * . the distance between the last packet and the
3944 * first should be above a configurable minimum
3947 dxp = abs(x0 - start_x);
3948 dyp = abs(y0 - start_y);
3950 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) ||
3951 dxp >= sc->syninfo.vscroll_min_delta ||
3952 dyp >= sc->syninfo.vscroll_min_delta) {
3954 * Handle two finger scrolling.
3955 * Note that we don't rely on fingers_nb
3956 * as that keeps the maximum number of fingers.
3958 if (two_finger_scroll) {
3959 if (nfingers == 2) {
3966 /* Check for horizontal scrolling. */
3967 if ((vscroll_hor_area > 0 &&
3968 start_y <= vscroll_hor_area) ||
3969 (vscroll_hor_area < 0 &&
3971 max_y + vscroll_hor_area))
3972 gest->in_vscroll += 2;
3974 /* Check for vertical scrolling. */
3975 if ((vscroll_ver_area > 0 &&
3976 start_x <= vscroll_ver_area) ||
3977 (vscroll_ver_area < 0 &&
3979 max_x + vscroll_ver_area))
3980 gest->in_vscroll += 1;
3983 /* Avoid conflicts if area overlaps. */
3984 if (gest->in_vscroll >= 3)
3986 (dxp > dyp) ? 2 : 1;
3990 * Reset two finger scrolling when the number of fingers
3991 * is different from two or any button is pressed.
3993 if (two_finger_scroll && gest->in_vscroll != 0 &&
3994 (nfingers != 2 || ms->button))
3995 gest->in_vscroll = 0;
3998 "synaptics: virtual scrolling: %s "
3999 "(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n",
4000 gest->in_vscroll ? "YES" : "NO",
4001 gest->in_vscroll, dxp, dyp,
4004 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
4006 * An action is currently taking place but the pressure
4007 * dropped under the minimum, putting an end to it.
4009 int taphold_timeout, dx, dy, tap_max_delta;
4011 dx = abs(smoother->queue[smoother->queue_cursor].x -
4013 dy = abs(smoother->queue[smoother->queue_cursor].y -
4016 /* Max delta is disabled for multi-fingers tap. */
4017 if (gest->fingers_nb > 1)
4018 tap_max_delta = imax(dx, dy);
4020 tap_max_delta = sc->syninfo.tap_max_delta;
4022 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
4024 /* Check for tap. */
4026 "synaptics: zmax=%d, dx=%d, dy=%d, "
4027 "delta=%d, fingers=%d, queue=%d\n",
4028 gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb,
4029 smoother->queue_len));
4030 if (!gest->in_vscroll && gest->zmax >= tap_threshold &&
4031 timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) &&
4032 dx <= tap_max_delta && dy <= tap_max_delta &&
4033 smoother->queue_len >= sc->syninfo.tap_min_queue) {
4036 * - the maximum pressure went over tap_threshold
4037 * - the action ended before tap_timeout
4039 * To handle tap-hold, we must delay any button push to
4042 if (gest->in_taphold) {
4044 * This is the second and last tap of a
4045 * double tap action, not a tap-hold.
4047 gest->in_taphold = 0;
4050 * For double-tap to work:
4051 * - no button press is emitted (to
4052 * simulate a button release)
4053 * - PSM_FLAGS_FINGERDOWN is set to
4054 * force the next packet to emit a
4058 "synaptics: button RELEASE: %d\n",
4060 sc->flags |= PSM_FLAGS_FINGERDOWN;
4062 /* Schedule button press on next interrupt */
4063 sc->idletimeout.tv_sec = psmhz > 1 ?
4065 sc->idletimeout.tv_usec = psmhz > 1 ?
4066 1000000 / psmhz : 0;
4069 * This is the first tap: we set the
4070 * tap-hold state and notify the button
4073 gest->in_taphold = 1;
4074 taphold_timeout = sc->syninfo.taphold_timeout;
4075 gest->taptimeout.tv_sec = taphold_timeout /
4077 gest->taptimeout.tv_usec = taphold_timeout %
4079 sc->idletimeout = gest->taptimeout;
4080 timevaladd(&gest->taptimeout,
4083 switch (gest->fingers_nb) {
4097 "synaptics: button PRESS: %d\n",
4099 ms->button |= gest->tap_button;
4103 * Not enough pressure or timeout: reset
4106 if (gest->in_taphold) {
4108 "synaptics: button RELEASE: %d\n",
4110 gest->in_taphold = 0;
4113 "synaptics: not a tap-hold\n"));
4116 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) {
4118 * For a tap-hold to work, the button must remain down at
4119 * least until timeout (where the in_taphold flags will be
4120 * cleared) or during the next action.
4122 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) {
4123 ms->button |= gest->tap_button;
4125 VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n",
4127 gest->in_taphold = 0;
4135 psmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id,
4136 mousestatus_t *ms, int *x, int *y)
4138 smoother_t *smoother = &sc->smoother[smoother_id];
4139 gesture_t *gest = &(sc->gesture);
4142 * Check pressure to detect a real wanted action on the
4145 if (f->p >= sc->syninfo.min_pressure) {
4147 int cursor, peer, window;
4148 int dx, dy, dxp, dyp;
4149 int max_width, max_pressure;
4150 int margin_top, margin_right, margin_bottom, margin_left;
4151 int na_top, na_right, na_bottom, na_left;
4152 int window_min, window_max;
4154 int weight_current, weight_previous, weight_len_squared;
4155 int div_min, div_max, div_len;
4156 int vscroll_hor_area, vscroll_ver_area;
4157 int two_finger_scroll;
4159 int len, weight_prev_x, weight_prev_y;
4160 int div_max_x, div_max_y, div_x, div_y;
4165 /* XXX Verify values? */
4166 max_width = sc->syninfo.max_width;
4167 max_pressure = sc->syninfo.max_pressure;
4168 margin_top = sc->syninfo.margin_top;
4169 margin_right = sc->syninfo.margin_right;
4170 margin_bottom = sc->syninfo.margin_bottom;
4171 margin_left = sc->syninfo.margin_left;
4172 na_top = sc->syninfo.na_top;
4173 na_right = sc->syninfo.na_right;
4174 na_bottom = sc->syninfo.na_bottom;
4175 na_left = sc->syninfo.na_left;
4176 window_min = sc->syninfo.window_min;
4177 window_max = sc->syninfo.window_max;
4178 multiplicator = sc->syninfo.multiplicator;
4179 weight_current = sc->syninfo.weight_current;
4180 weight_previous = sc->syninfo.weight_previous;
4181 weight_len_squared = sc->syninfo.weight_len_squared;
4182 div_min = sc->syninfo.div_min;
4183 div_max = sc->syninfo.div_max;
4184 div_len = sc->syninfo.div_len;
4185 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
4186 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
4187 two_finger_scroll = sc->syninfo.two_finger_scroll;
4188 max_x = sc->syninfo.max_x;
4189 max_y = sc->syninfo.max_y;
4190 natural_scroll = sc->syninfo.natural_scroll;
4192 is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0;
4194 /* Read current absolute position. */
4199 * Limit the coordinates to the specified margins because
4200 * this area isn't very reliable.
4202 if (x0 <= margin_left)
4204 else if (x0 >= max_x - margin_right)
4205 x0 = max_x - margin_right;
4206 if (y0 <= margin_bottom)
4208 else if (y0 >= max_y - margin_top)
4209 y0 = max_y - margin_top;
4211 /* If the action is just beginning, init the structure. */
4212 if (smoother->active == 0) {
4213 VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id));
4215 /* Store the first point of this action. */
4216 smoother->start_x = x0;
4217 smoother->start_y = y0;
4220 /* Initialize queue. */
4221 smoother->queue_cursor = SYNAPTICS_PACKETQUEUE;
4222 smoother->queue_len = 0;
4224 /* Reset average. */
4225 smoother->avg_dx = 0;
4226 smoother->avg_dy = 0;
4228 /* Reset squelch. */
4229 smoother->squelch_x = 0;
4230 smoother->squelch_y = 0;
4232 /* Activate queue */
4233 smoother->active = 1;
4235 /* Calculate the current delta. */
4236 cursor = smoother->queue_cursor;
4237 dx = x0 - smoother->queue[cursor].x;
4238 dy = y0 - smoother->queue[cursor].y;
4241 VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n",
4242 smoother_id, x0, y0, f->p, f->w));
4244 /* Queue this new packet. */
4245 cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1);
4246 smoother->queue[cursor].x = x0;
4247 smoother->queue[cursor].y = y0;
4248 smoother->queue_cursor = cursor;
4249 if (smoother->queue_len < SYNAPTICS_PACKETQUEUE)
4250 smoother->queue_len++;
4252 "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
4253 smoother_id, cursor, x0, y0, dx, dy));
4255 /* Do we have enough packets to consider this a movement? */
4256 if (smoother->queue_len < gest->window_min)
4259 weight_prev_x = weight_prev_y = weight_previous;
4260 div_max_x = div_max_y = div_max;
4262 if (gest->in_vscroll) {
4263 /* Dividers are different with virtual scrolling. */
4264 div_min = sc->syninfo.vscroll_div_min;
4265 div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
4268 * There's a lot of noise in coordinates when
4269 * the finger is on the touchpad's borders. When
4270 * using this area, we apply a special weight and
4273 if (x0 <= na_left || x0 >= max_x - na_right) {
4274 weight_prev_x = sc->syninfo.weight_previous_na;
4275 div_max_x = sc->syninfo.div_max_na;
4278 if (y0 <= na_bottom || y0 >= max_y - na_top) {
4279 weight_prev_y = sc->syninfo.weight_previous_na;
4280 div_max_y = sc->syninfo.div_max_na;
4285 * Calculate weights for the average operands and
4286 * the divisor. Both depend on the distance between
4287 * the current packet and a previous one (based on the
4290 window = imin(smoother->queue_len, window_max);
4291 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
4292 dxp = abs(x0 - smoother->queue[peer].x) + 1;
4293 dyp = abs(y0 - smoother->queue[peer].y) + 1;
4294 len = (dxp * dxp) + (dyp * dyp);
4295 weight_prev_x = imin(weight_prev_x,
4296 weight_len_squared * weight_prev_x / len);
4297 weight_prev_y = imin(weight_prev_y,
4298 weight_len_squared * weight_prev_y / len);
4300 len = (dxp + dyp) / 2;
4301 div_x = div_len * div_max_x / len;
4302 div_x = imin(div_max_x, div_x);
4303 div_x = imax(div_min, div_x);
4304 div_y = div_len * div_max_y / len;
4305 div_y = imin(div_max_y, div_y);
4306 div_y = imax(div_min, div_y);
4309 "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
4310 smoother_id, peer, len, weight_prev_x, weight_prev_y,
4313 /* Compute averages. */
4315 (weight_current * dx * multiplicator +
4316 weight_prev_x * smoother->avg_dx) /
4317 (weight_current + weight_prev_x);
4320 (weight_current * dy * multiplicator +
4321 weight_prev_y * smoother->avg_dy) /
4322 (weight_current + weight_prev_y);
4325 "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id,
4326 smoother->avg_dx / multiplicator,
4327 smoother->avg_dy / multiplicator));
4329 /* Use these averages to calculate x & y. */
4330 smoother->squelch_x += smoother->avg_dx;
4331 dxp = smoother->squelch_x / (div_x * multiplicator);
4332 smoother->squelch_x = smoother->squelch_x %
4333 (div_x * multiplicator);
4335 smoother->squelch_y += smoother->avg_dy;
4336 dyp = smoother->squelch_y / (div_y * multiplicator);
4337 smoother->squelch_y = smoother->squelch_y %
4338 (div_y * multiplicator);
4340 switch(gest->in_vscroll) {
4341 case 0: /* Pointer movement. */
4342 /* On real<->fuzzy finger switch the x/y pos jumps */
4343 if (is_fuzzy == smoother->is_fuzzy) {
4348 VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n",
4349 smoother_id, dx, dy, dxp, dyp));
4351 case 1: /* Vertical scrolling. */
4353 if (two_finger_scroll && natural_scroll)
4354 ms->button |= (dyp > 0) ?
4355 MOUSE_BUTTON5DOWN : MOUSE_BUTTON4DOWN;
4357 ms->button |= (dyp > 0) ?
4358 MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN;
4361 case 2: /* Horizontal scrolling. */
4363 if (two_finger_scroll && natural_scroll)
4364 ms->button |= (dxp > 0) ?
4365 MOUSE_BUTTON6DOWN : MOUSE_BUTTON7DOWN;
4367 ms->button |= (dxp > 0) ?
4368 MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN;
4373 smoother->is_fuzzy = is_fuzzy;
4377 * Deactivate queue. Note: We can not just reset queue here
4378 * as these values are still used by gesture processor.
4379 * So postpone reset till next touch.
4381 smoother->active = 0;
4386 proc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4387 int *x, int *y, int *z)
4389 static int touchpad_button, trackpoint_button;
4390 finger_t fn, f[ELANTECH_MAX_FINGERS];
4391 int pkt, id, scale, i, nfingers, mask, palm;
4393 if (!elantech_support)
4396 /* Determine packet format and do a sanity check for out of sync packets. */
4397 if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion))
4398 pkt = ELANTECH_PKT_NOP;
4399 else if (sc->elanhw.hastrackpoint && ELANTECH_PKT_IS_TRACKPOINT(pb))
4400 pkt = ELANTECH_PKT_TRACKPOINT;
4402 switch (sc->elanhw.hwversion) {
4404 if (!ELANTECH_PKT_IS_V2(pb))
4407 pkt = (pb->ipacket[0] & 0xc0) == 0x80 ?
4408 ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON;
4411 if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) &&
4412 !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc))
4415 pkt = ELANTECH_PKT_V3;
4418 if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc))
4421 switch (pb->ipacket[3] & 0x03) {
4423 pkt = ELANTECH_PKT_V4_STATUS;
4426 pkt = ELANTECH_PKT_V4_HEAD;
4429 pkt = ELANTECH_PKT_V4_MOTION;
4439 VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt));
4441 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4442 PSM_FINGER_RESET(f[id]);
4445 ms->button = ms->obutton;
4447 if (sc->syninfo.touchpad_off && pkt != ELANTECH_PKT_TRACKPOINT)
4451 * L: Left mouse button pressed
4452 * R: Right mouse button pressed
4453 * N: number of fingers on touchpad
4454 * X: absolute x value (horizontal)
4455 * Y: absolute y value (vertical)
4456 * W; width of the finger touch
4460 case ELANTECH_PKT_V2_COMMON: /* HW V2. One/Three finger touch */
4461 /* 7 6 5 4 3 2 1 0 (LSB)
4462 * -------------------------------------------
4463 * ipacket[0]: N1 N0 W3 W2 . . R L
4464 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4465 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4466 * ipacket[3]: N4 VF W1 W0 . . . B2
4467 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4468 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4469 * -------------------------------------------
4470 * N4: set if more than 3 fingers (only in 3 fingers mode)
4471 * VF: a kind of flag? (only on EF123, 0 when finger
4472 * is over one of the buttons, 1 otherwise)
4473 * B2: (on EF113 only, 0 otherwise), one button pressed
4474 * P & W is not reported on EF113 touchpads
4476 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4477 if (nfingers == 3 && (pb->ipacket[3] & 0x80))
4480 if (nfingers == 0) {
4481 mask = (1 << nfingers) - 1; /* = 0x00 */
4485 /* Map 3-rd and 4-th fingers to first finger */
4486 mask = (1 << 1) - 1; /* = 0x01 */
4487 f[0] = ELANTECH_FINGER_SET_XYP(pb);
4488 if (sc->elanhw.haspressure) {
4489 f[0].w = ((pb->ipacket[0] & 0x30) >> 2) |
4490 ((pb->ipacket[3] & 0x30) >> 4);
4492 f[0].p = PSM_FINGER_DEFAULT_P;
4493 f[0].w = PSM_FINGER_DEFAULT_W;
4497 * HW v2 dont report exact finger positions when 3 or more
4498 * fingers are on touchpad.
4501 f[0].flags = PSM_FINGER_FUZZY;
4505 case ELANTECH_PKT_V2_2FINGER: /*HW V2. Two finger touch */
4506 /* 7 6 5 4 3 2 1 0 (LSB)
4507 * -------------------------------------------
4508 * ipacket[0]: N1 N0 AY8 AX8 . . R L
4509 * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0
4510 * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0
4511 * ipacket[3]: . . BY8 BX8 . . . .
4512 * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0
4513 * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0
4514 * -------------------------------------------
4515 * AX: lower-left finger absolute x value
4516 * AY: lower-left finger absolute y value
4517 * BX: upper-right finger absolute x value
4518 * BY: upper-right finger absolute y value
4521 mask = (1 << nfingers) - 1;
4523 for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++)
4524 f[id] = (finger_t) {
4525 .x = (((pb->ipacket[id * 3] & 0x10) << 4) |
4526 pb->ipacket[id * 3 + 1]) << 2,
4527 .y = (((pb->ipacket[id * 3] & 0x20) << 3) |
4528 pb->ipacket[id * 3 + 2]) << 2,
4529 .p = PSM_FINGER_DEFAULT_P,
4530 .w = PSM_FINGER_DEFAULT_W,
4531 /* HW ver.2 sends bounding box */
4532 .flags = PSM_FINGER_FUZZY
4536 case ELANTECH_PKT_V3: /* HW Version 3 */
4537 /* 7 6 5 4 3 2 1 0 (LSB)
4538 * -------------------------------------------
4539 * ipacket[0]: N1 N0 W3 W2 0 1 R L
4540 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4541 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4542 * ipacket[3]: 0 0 W1 W0 0 0 1 0
4543 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4544 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4545 * -------------------------------------------
4547 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4548 /* Map 3-rd finger to first finger */
4549 id = nfingers > 2 ? 0 : nfingers - 1;
4550 mask = (1 << (id + 1)) - 1;
4555 fn = ELANTECH_FINGER_SET_XYP(pb);
4556 fn.w = ((pb->ipacket[0] & 0x30) >> 2) |
4557 ((pb->ipacket[3] & 0x30) >> 4);
4560 * HW v3 dont report exact finger positions when 3 or more
4561 * fingers are on touchpad.
4564 fn.flags = PSM_FINGER_FUZZY;
4566 if (nfingers == 2) {
4567 if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) {
4568 sc->elanaction.fingers[0] = fn;
4571 f[0] = sc->elanaction.fingers[0];
4576 case ELANTECH_PKT_V4_STATUS: /* HW Version 4. Status packet */
4577 /* 7 6 5 4 3 2 1 0 (LSB)
4578 * -------------------------------------------
4579 * ipacket[0]: . . . . 0 1 R L
4580 * ipacket[1]: . . . F4 F3 F2 F1 F0
4581 * ipacket[2]: . . . . . . . .
4582 * ipacket[3]: . . . 1 0 0 0 0
4583 * ipacket[4]: PL . . . . . . .
4584 * ipacket[5]: . . . . . . . .
4585 * -------------------------------------------
4586 * Fn: finger n is on touchpad
4588 * HV ver4 sends a status packet to indicate that the numbers
4589 * or identities of the fingers has been changed
4592 mask = pb->ipacket[1] & 0x1f;
4593 nfingers = bitcount(mask);
4595 if (sc->elanaction.mask_v4wait != 0)
4596 VLOG(3, (LOG_DEBUG, "elantech: HW v4 status packet"
4597 " when not all previous head packets received\n"));
4599 /* Bitmap of fingers to receive before gesture processing */
4600 sc->elanaction.mask_v4wait = mask & ~sc->elanaction.mask;
4602 /* Skip "new finger is on touchpad" packets */
4603 if (sc->elanaction.mask_v4wait) {
4604 sc->elanaction.mask = mask;
4610 case ELANTECH_PKT_V4_HEAD: /* HW Version 4. Head packet */
4611 /* 7 6 5 4 3 2 1 0 (LSB)
4612 * -------------------------------------------
4613 * ipacket[0]: W3 W2 W1 W0 0 1 R L
4614 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4615 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4616 * ipacket[3]: ID2 ID1 ID0 1 0 0 0 1
4617 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4618 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4619 * -------------------------------------------
4621 * HW ver 4 sends head packets in two cases:
4622 * 1. One finger touch and movement.
4623 * 2. Next after status packet to tell new finger positions.
4625 mask = sc->elanaction.mask;
4626 nfingers = bitcount(mask);
4627 id = ((pb->ipacket[3] & 0xe0) >> 5) - 1;
4628 fn = ELANTECH_FINGER_SET_XYP(pb);
4629 fn.w =(pb->ipacket[0] & 0xf0) >> 4;
4634 /* Packet is finger position update. Report it */
4635 if (sc->elanaction.mask_v4wait == 0) {
4636 if (id < ELANTECH_MAX_FINGERS)
4641 /* Remove finger from waiting bitmap and store into context */
4642 sc->elanaction.mask_v4wait &= ~(1 << id);
4643 if (id < ELANTECH_MAX_FINGERS)
4644 sc->elanaction.fingers[id] = fn;
4646 /* Wait for other fingers if needed */
4647 if (sc->elanaction.mask_v4wait != 0)
4650 /* All new fingers are received. Report them from context */
4651 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4652 if (sc->elanaction.mask & (1 << id))
4653 f[id] = sc->elanaction.fingers[id];
4657 case ELANTECH_PKT_V4_MOTION: /* HW Version 4. Motion packet */
4658 /* 7 6 5 4 3 2 1 0 (LSB)
4659 * -------------------------------------------
4660 * ipacket[0]: ID2 ID1 ID0 OF 0 1 R L
4661 * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4662 * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4663 * ipacket[3]: ID2 ID1 ID0 1 0 0 1 0
4664 * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4665 * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4666 * -------------------------------------------
4667 * OF: delta overflows (> 127 or < -128), in this case
4668 * firmware sends us (delta x / 5) and (delta y / 5)
4670 * DX: delta x (two's complement)
4671 * XY: delta y (two's complement)
4672 * byte 0 ~ 2 for one finger
4673 * byte 3 ~ 5 for another finger
4675 mask = sc->elanaction.mask;
4676 nfingers = bitcount(mask);
4678 scale = (pb->ipacket[0] & 0x10) ? 5 : 1;
4679 for (i = 0; i <= 3; i += 3) {
4680 id = ((pb->ipacket[i] & 0xe0) >> 5) - 1;
4681 if (id < 0 || id >= ELANTECH_MAX_FINGERS)
4684 if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) {
4685 f[id] = sc->elanaction.fingers[id];
4686 f[id].x += imax(-f[id].x,
4687 (signed char)pb->ipacket[i+1] * scale);
4688 f[id].y += imax(-f[id].y,
4689 (signed char)pb->ipacket[i+2] * scale);
4691 VLOG(3, (LOG_DEBUG, "elantech: "
4692 "HW v4 motion packet skipped\n"));
4698 case ELANTECH_PKT_TRACKPOINT:
4699 /* 7 6 5 4 3 2 1 0 (LSB)
4700 * -------------------------------------------
4701 * ipacket[0]: 0 0 SY SX 0 M R L
4702 * ipacket[1]: ~SX 0 0 0 0 0 0 0
4703 * ipacket[2]: ~SY 0 0 0 0 0 0 0
4704 * ipacket[3]: 0 0 ~SY ~SX 0 1 1 0
4705 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
4706 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4707 * -------------------------------------------
4708 * X and Y are written in two's complement spread
4709 * over 9 bits with SX/SY the relative top bit and
4710 * X7..X0 and Y7..Y0 the lower bits.
4712 if (!(pb->ipacket[0] & 0xC8) && !(pb->ipacket[1] & 0x7F) &&
4713 !(pb->ipacket[2] & 0x7F) && !(pb->ipacket[3] & 0xC9) &&
4714 !(pb->ipacket[0] & 0x10) != !(pb->ipacket[1] & 0x80) &&
4715 !(pb->ipacket[0] & 0x10) != !(pb->ipacket[3] & 0x10) &&
4716 !(pb->ipacket[0] & 0x20) != !(pb->ipacket[2] & 0x80) &&
4717 !(pb->ipacket[0] & 0x20) != !(pb->ipacket[3] & 0x20)) {
4718 *x = (pb->ipacket[0] & MOUSE_PS2_XNEG) ?
4719 pb->ipacket[4] - 256 : pb->ipacket[4];
4720 *y = (pb->ipacket[0] & MOUSE_PS2_YNEG) ?
4721 pb->ipacket[5] - 256 : pb->ipacket[5];
4724 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4725 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) |
4726 ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0);
4727 #ifdef EVDEV_SUPPORT
4728 evdev_push_rel(sc->evdev_r, REL_X, *x);
4729 evdev_push_rel(sc->evdev_r, REL_Y, -*y);
4730 evdev_push_mouse_btn(sc->evdev_r, trackpoint_button);
4731 evdev_sync(sc->evdev_r);
4733 ms->button = touchpad_button | trackpoint_button;
4735 VLOG(3, (LOG_DEBUG, "elantech: "
4736 "unexpected trackpoint packet skipped\n"));
4739 case ELANTECH_PKT_NOP:
4746 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4747 if (PSM_FINGER_IS_SET(f[id]))
4748 VLOG(2, (LOG_DEBUG, "elantech: "
4749 "finger %d: down [%d, %d], %d, %d, %d\n", id + 1,
4750 f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags));
4752 /* Touchpad button presses */
4753 if (sc->elanhw.isclickpad) {
4755 ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0);
4758 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4759 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0);
4762 #ifdef EVDEV_SUPPORT
4763 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
4764 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4765 if (PSM_FINGER_IS_SET(f[id])) {
4766 psm_push_mt_finger(sc, id, &f[id]);
4767 /* Convert touch width to surface units */
4768 evdev_push_abs(sc->evdev_a, ABS_MT_TOUCH_MAJOR,
4769 f[id].w * sc->elanhw.dptracex);
4772 evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
4773 evdev_push_nfingers(sc->evdev_a, nfingers);
4775 if (PSM_FINGER_IS_SET(f[0]))
4776 psm_push_st_finger(sc, &f[0]);
4778 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
4779 evdev_push_mouse_btn(sc->evdev_a, touchpad_button);
4780 evdev_sync(sc->evdev_a);
4784 ms->button = touchpad_button | trackpoint_button;
4786 /* Palm detection doesn't terminate the current action. */
4787 palm = psmpalmdetect(sc, &f[0], nfingers);
4789 /* Send finger 1 position to gesture processor */
4790 if ((PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) ||
4791 nfingers == 0) && !palm)
4792 psmgestures(sc, &f[0], imin(nfingers, 3), ms);
4794 /* Send fingers positions to movement smoothers */
4795 for (id = 0; id < PSM_FINGERS; id++)
4796 if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id)))
4797 psmsmoother(sc, &f[id], id, ms, x, y);
4799 /* Store current finger positions in action context */
4800 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4801 if (PSM_FINGER_IS_SET(f[id]))
4802 sc->elanaction.fingers[id] = f[id];
4803 if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id)))
4804 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
4806 sc->elanaction.mask = mask;
4810 ms->button = ms->obutton;
4814 /* Use the extra buttons as a scrollwheel */
4815 if (ms->button & MOUSE_BUTTON4DOWN)
4817 else if (ms->button & MOUSE_BUTTON5DOWN)
4819 else if (ms->button & MOUSE_BUTTON6DOWN)
4821 else if (ms->button & MOUSE_BUTTON7DOWN)
4825 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
4826 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
4832 proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4833 int *x, int *y, int *z)
4835 static int butmap_versapad[8] = {
4841 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4843 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
4847 /* VersaPad PS/2 absolute mode message format
4849 * [packet1] 7 6 5 4 3 2 1 0(LSB)
4850 * ipacket[0]: 1 1 0 A 1 L T R
4851 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
4852 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
4853 * ipacket[3]: 1 1 1 A 1 L T R
4854 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
4855 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
4858 * R: right physical mouse button (1=on)
4859 * T: touch pad virtual button (1=tapping)
4860 * L: left physical mouse button (1=on)
4861 * A: position data is valid (1=valid)
4862 * H: horizontal data (12bit signed integer. H11 is sign bit.)
4863 * V: vertical data (12bit signed integer. V11 is sign bit.)
4866 * Tapping is mapped to MOUSE_BUTTON4.
4870 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
4871 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
4872 if (c & MOUSE_PS2VERSA_IN_USE) {
4873 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
4874 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
4879 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
4882 if (*x < 0) /* XXX */
4891 sc->flags |= PSM_FLAGS_FINGERDOWN;
4895 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
4899 psmsoftintridle(void *arg)
4901 struct psm_softc *sc = arg;
4904 /* Invoke soft handler only when pqueue is empty. Otherwise it will be
4905 * invoked from psmintr soon with pqueue filled with real data */
4906 if (sc->pqueue_start == sc->pqueue_end &&
4907 sc->idlepacket.inputbytes > 0) {
4908 /* Grow circular queue backwards to avoid race with psmintr */
4909 if (--sc->pqueue_start < 0)
4910 sc->pqueue_start = PSM_PACKETQUEUE - 1;
4912 pb = &sc->pqueue[sc->pqueue_start];
4913 memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t));
4915 "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n",
4916 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
4917 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
4924 psmsoftintr(void *arg)
4927 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
4928 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
4930 static int butmap[8] = {
4934 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4936 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
4937 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
4938 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
4940 struct psm_softc *sc = arg;
4943 int x, y, z, c, l, s;
4945 getmicrouptime(&sc->lastsoftintr);
4950 pb = &sc->pqueue[sc->pqueue_start];
4952 if (sc->mode.level == PSM_LEVEL_NATIVE)
4957 * A kludge for Kensington device!
4958 * The MSB of the horizontal count appears to be stored in
4961 if (sc->hw.model == MOUSE_MODEL_THINK)
4962 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
4964 /* ignore the overflow bits... */
4965 x = (c & MOUSE_PS2_XNEG) ?
4966 pb->ipacket[1] - 256 : pb->ipacket[1];
4967 y = (c & MOUSE_PS2_YNEG) ?
4968 pb->ipacket[2] - 256 : pb->ipacket[2];
4970 ms.obutton = sc->button; /* previous button state */
4971 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
4972 /* `tapping' action */
4973 if (sc->config & PSM_CONFIG_FORCETAP)
4974 ms.button |= ((c & MOUSE_PS2_TAP)) ?
4975 0 : MOUSE_BUTTON4DOWN;
4976 timevalclear(&sc->idletimeout);
4977 sc->idlepacket.inputbytes = 0;
4979 switch (sc->hw.model) {
4980 case MOUSE_MODEL_EXPLORER:
4982 * b7 b6 b5 b4 b3 b2 b1 b0
4983 * byte 1: oy ox sy sx 1 M R L
4984 * byte 2: x x x x x x x x
4985 * byte 3: y y y y y y y y
4986 * byte 4: * * S2 S1 s d2 d1 d0
4988 * L, M, R, S1, S2: left, middle, right and side buttons
4989 * s: wheel data sign bit
4992 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
4993 (pb->ipacket[3] & 0x0f) - 16 :
4994 (pb->ipacket[3] & 0x0f);
4996 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
4997 MOUSE_BUTTON4DOWN : 0;
4999 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
5000 MOUSE_BUTTON5DOWN : 0;
5003 case MOUSE_MODEL_INTELLI:
5004 case MOUSE_MODEL_NET:
5005 /* wheel data is in the fourth byte */
5006 z = (char)pb->ipacket[3];
5008 * XXX some mice may send 7 when there is no Z movement? */
5009 if ((z >= 7) || (z <= -7))
5011 /* some compatible mice have additional buttons */
5012 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
5013 MOUSE_BUTTON4DOWN : 0;
5014 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
5015 MOUSE_BUTTON5DOWN : 0;
5018 case MOUSE_MODEL_MOUSEMANPLUS:
5019 proc_mmanplus(sc, pb, &ms, &x, &y, &z);
5022 case MOUSE_MODEL_GLIDEPOINT:
5023 /* `tapping' action */
5024 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
5028 case MOUSE_MODEL_NETSCROLL:
5030 * three additional bytes encode buttons and
5033 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
5034 MOUSE_BUTTON4DOWN : 0;
5035 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
5036 MOUSE_BUTTON5DOWN : 0;
5037 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
5038 pb->ipacket[4] - 256 : pb->ipacket[4];
5041 case MOUSE_MODEL_THINK:
5042 /* the fourth button state in the first byte */
5043 ms.button |= (c & MOUSE_PS2_TAP) ?
5044 MOUSE_BUTTON4DOWN : 0;
5047 case MOUSE_MODEL_VERSAPAD:
5048 proc_versapad(sc, pb, &ms, &x, &y, &z);
5049 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
5050 ((y < 0) ? MOUSE_PS2_YNEG : 0);
5053 case MOUSE_MODEL_4D:
5055 * b7 b6 b5 b4 b3 b2 b1 b0
5056 * byte 1: s2 d2 s1 d1 1 M R L
5057 * byte 2: sx x x x x x x x
5058 * byte 3: sy y y y y y y y
5060 * s1: wheel 1 direction
5062 * s2: wheel 2 direction
5065 x = (pb->ipacket[1] & 0x80) ?
5066 pb->ipacket[1] - 256 : pb->ipacket[1];
5067 y = (pb->ipacket[2] & 0x80) ?
5068 pb->ipacket[2] - 256 : pb->ipacket[2];
5069 switch (c & MOUSE_4D_WHEELBITS) {
5076 case 0x40: /* XXX 2nd wheel turning right */
5079 case 0xc0: /* XXX 2nd wheel turning left */
5085 case MOUSE_MODEL_4DPLUS:
5086 if ((x < 16 - 256) && (y < 16 - 256)) {
5088 * b7 b6 b5 b4 b3 b2 b1 b0
5089 * byte 1: 0 0 1 1 1 M R L
5090 * byte 2: 0 0 0 0 1 0 0 0
5091 * byte 3: 0 0 0 0 S s d1 d0
5093 * L, M, R, S: left, middle, right,
5095 * s: wheel data sign bit
5099 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
5100 ms.button |= MOUSE_BUTTON4DOWN;
5101 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
5102 ((pb->ipacket[2] & 0x07) - 8) :
5103 (pb->ipacket[2] & 0x07) ;
5105 /* preserve previous button states */
5106 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
5110 case MOUSE_MODEL_SYNAPTICS:
5111 if (pb->inputbytes == MOUSE_PS2_PACKETSIZE)
5112 if (proc_synaptics_mux(sc, pb))
5115 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0) {
5116 VLOG(3, (LOG_DEBUG, "synaptics: "
5117 "packet rejected\n"));
5122 case MOUSE_MODEL_ELANTECH:
5123 if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0) {
5124 VLOG(3, (LOG_DEBUG, "elantech: "
5125 "packet rejected\n"));
5130 case MOUSE_MODEL_TRACKPOINT:
5131 case MOUSE_MODEL_GENERIC:
5136 #ifdef EVDEV_SUPPORT
5137 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE &&
5138 sc->hw.model != MOUSE_MODEL_ELANTECH &&
5139 sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
5140 evdev_push_rel(sc->evdev_r, REL_X, x);
5141 evdev_push_rel(sc->evdev_r, REL_Y, -y);
5143 switch (sc->hw.model) {
5144 case MOUSE_MODEL_EXPLORER:
5145 case MOUSE_MODEL_INTELLI:
5146 case MOUSE_MODEL_NET:
5147 case MOUSE_MODEL_NETSCROLL:
5148 case MOUSE_MODEL_4DPLUS:
5149 evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
5151 case MOUSE_MODEL_MOUSEMANPLUS:
5152 case MOUSE_MODEL_4D:
5156 evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
5160 evdev_push_rel(sc->evdev_r, REL_HWHEEL, z / 2);
5166 evdev_push_mouse_btn(sc->evdev_r, ms.button);
5167 evdev_sync(sc->evdev_r);
5172 if (sc->mode.accelfactor >= 1) {
5174 x = x * x / sc->mode.accelfactor;
5177 if (c & MOUSE_PS2_XNEG)
5181 y = y * y / sc->mode.accelfactor;
5184 if (c & MOUSE_PS2_YNEG)
5189 /* Store last packet for reinjection if it has not been set already */
5190 if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0)
5191 sc->idlepacket = *pb;
5196 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
5197 (ms.obutton ^ ms.button);
5199 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
5201 sc->status.flags |= ms.flags;
5202 sc->status.dx += ms.dx;
5203 sc->status.dy += ms.dy;
5204 sc->status.dz += ms.dz;
5205 sc->status.button = ms.button;
5206 sc->button = ms.button;
5209 sc->watchdog = FALSE;
5212 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
5213 l = imin(pb->inputbytes,
5214 sizeof(sc->queue.buf) - sc->queue.tail);
5215 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
5216 if (pb->inputbytes > l)
5217 bcopy(&pb->ipacket[l], &sc->queue.buf[0],
5218 pb->inputbytes - l);
5219 sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
5220 sizeof(sc->queue.buf);
5221 sc->queue.count += pb->inputbytes;
5226 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
5227 sc->pqueue_start = 0;
5228 } while (sc->pqueue_start != sc->pqueue_end);
5230 if (sc->state & PSM_ASLP) {
5231 sc->state &= ~PSM_ASLP;
5234 selwakeuppri(&sc->rsel, PZERO);
5235 if (sc->async != NULL) {
5236 pgsigio(&sc->async, SIGIO, 0);
5238 sc->state &= ~PSM_SOFTARMED;
5240 /* schedule injection of predefined packet after idletimeout
5241 * if no data packets have been received from psmintr */
5242 if (timevalisset(&sc->idletimeout)) {
5243 sc->state |= PSM_SOFTARMED;
5244 callout_reset(&sc->softcallout, tvtohz(&sc->idletimeout),
5245 psmsoftintridle, sc);
5246 VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n",
5247 tvtohz(&sc->idletimeout)));
5253 psmpoll(struct cdev *dev, int events, struct thread *td)
5255 struct psm_softc *sc = dev->si_drv1;
5259 /* Return true if a mouse event available */
5261 if (events & (POLLIN | POLLRDNORM)) {
5262 if (sc->queue.count > 0)
5263 revents |= events & (POLLIN | POLLRDNORM);
5265 selrecord(td, &sc->rsel);
5272 /* vendor/model specific routines */
5274 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
5276 if (set_mouse_resolution(kbdc, res) != res)
5278 if (set_mouse_scaling(kbdc, scale) &&
5279 set_mouse_scaling(kbdc, scale) &&
5280 set_mouse_scaling(kbdc, scale) &&
5281 (get_mouse_status(kbdc, status, 0, 3) >= 3))
5287 mouse_ext_command(KBDC kbdc, int command)
5291 c = (command >> 6) & 0x03;
5292 if (set_mouse_resolution(kbdc, c) != c)
5294 c = (command >> 4) & 0x03;
5295 if (set_mouse_resolution(kbdc, c) != c)
5297 c = (command >> 2) & 0x03;
5298 if (set_mouse_resolution(kbdc, c) != c)
5300 c = (command >> 0) & 0x03;
5301 if (set_mouse_resolution(kbdc, c) != c)
5307 /* Logitech MouseMan Cordless II */
5309 enable_lcordless(struct psm_softc *sc, enum probearg arg)
5311 KBDC kbdc = sc->kbdc;
5315 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status))
5317 if (status[1] == PSMD_RES_HIGH)
5319 ch = (status[0] & 0x07) - 1; /* channel # */
5320 if ((ch <= 0) || (ch > 4))
5323 * status[1]: always one?
5324 * status[2]: battery status? (0-100)
5330 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
5332 enable_groller(struct psm_softc *sc, enum probearg arg)
5334 KBDC kbdc = sc->kbdc;
5338 * The special sequence to enable the fourth button and the
5339 * roller. Immediately after this sequence check status bytes.
5340 * if the mouse is NetScroll, the second and the third bytes are
5345 * If the mouse is an ordinary PS/2 mouse, the status bytes should
5346 * look like the following.
5348 * byte 1 bit 7 always 0
5349 * bit 6 stream mode (0)
5350 * bit 5 disabled (0)
5351 * bit 4 1:1 scaling (0)
5353 * bit 0-2 button status
5354 * byte 2 resolution (PSMD_RES_HIGH)
5355 * byte 3 report rate (?)
5358 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5360 if ((status[1] != '3') || (status[2] != 'D'))
5362 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
5368 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
5370 enable_gmouse(struct psm_softc *sc, enum probearg arg)
5372 KBDC kbdc = sc->kbdc;
5376 * The special sequence to enable the middle, "rubber" button.
5377 * Immediately after this sequence check status bytes.
5378 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
5379 * the second and the third bytes are '3' and 'U'.
5380 * NOTE: NetMouse reports that it has three buttons although it has
5381 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
5382 * say they have three buttons too and they do have a button on the
5385 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5387 if ((status[1] != '3') || (status[2] != 'U'))
5392 /* ALPS GlidePoint */
5394 enable_aglide(struct psm_softc *sc, enum probearg arg)
5396 KBDC kbdc = sc->kbdc;
5400 * The special sequence to obtain ALPS GlidePoint specific
5401 * information. Immediately after this sequence, status bytes will
5402 * contain something interesting.
5403 * NOTE: ALPS produces several models of GlidePoint. Some of those
5404 * do not respond to this sequence, thus, cannot be detected this way.
5406 if (set_mouse_sampling_rate(kbdc, 100) != 100)
5408 if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
5410 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
5415 /* Kensington ThinkingMouse/Trackball */
5417 enable_kmouse(struct psm_softc *sc, enum probearg arg)
5419 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
5420 KBDC kbdc = sc->kbdc;
5426 id1 = get_aux_id(kbdc);
5427 if (set_mouse_sampling_rate(kbdc, 10) != 10)
5430 * The device is now in the native mode? It returns a different
5433 id2 = get_aux_id(kbdc);
5434 if ((id1 == id2) || (id2 != 2))
5437 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
5440 /* at this point, resolution is LOW, sampling rate is 10/sec */
5441 if (get_mouse_status(kbdc, status, 0, 3) < 3)
5446 * The special sequence to enable the third and fourth buttons.
5447 * Otherwise they behave like the first and second buttons.
5449 for (i = 0; i < nitems(rate); ++i)
5450 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5454 * At this point, the device is using default resolution and
5455 * sampling rate for the native mode.
5457 if (get_mouse_status(kbdc, status, 0, 3) < 3)
5459 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
5462 /* the device appears be enabled by this sequence, diable it for now */
5463 disable_aux_dev(kbdc);
5464 empty_aux_buffer(kbdc, 5);
5469 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
5471 enable_mmanplus(struct psm_softc *sc, enum probearg arg)
5473 KBDC kbdc = sc->kbdc;
5476 /* the special sequence to enable the fourth button and the roller. */
5478 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
5479 * must be called exactly three times since the last RESET command
5480 * before this sequence. XXX
5482 if (!set_mouse_scaling(kbdc, 1))
5484 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
5486 if (get_mouse_status(kbdc, data, 1, 3) < 3)
5490 * PS2++ protocol, packet type 0
5492 * b7 b6 b5 b4 b3 b2 b1 b0
5493 * byte 1: * 1 p3 p2 1 * * *
5494 * byte 2: 1 1 p1 p0 m1 m0 1 0
5495 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
5497 * p3-p0: packet type: 0
5498 * m7-m0: model ID: MouseMan+:0x50,
5500 * ScrollPoint:0x58...
5502 /* check constant bits */
5503 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
5505 if ((data[1] & 0xc3) != 0xc2)
5507 /* check d3-d0 in byte 2 */
5508 if (!MOUSE_PS2PLUS_CHECKBITS(data))
5511 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
5515 sc->hw.hwid &= 0x00ff;
5516 sc->hw.hwid |= data[2] << 8; /* save model ID */
5520 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
5521 * the wheel and the fourth button events are encoded in the
5522 * special data packet. The mouse may be put in the IntelliMouse mode
5523 * if it is initialized by the IntelliMouse's method.
5528 /* MS IntelliMouse Explorer */
5530 enable_msexplorer(struct psm_softc *sc, enum probearg arg)
5532 KBDC kbdc = sc->kbdc;
5533 static u_char rate0[] = { 200, 100, 80, };
5534 static u_char rate1[] = { 200, 200, 80, };
5539 * This is needed for at least A4Tech X-7xx mice - they do not go
5540 * straight to Explorer mode, but need to be set to Intelli mode
5543 enable_msintelli(sc, arg);
5545 /* the special sequence to enable the extra buttons and the roller. */
5546 for (i = 0; i < nitems(rate1); ++i)
5547 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
5549 /* the device will give the genuine ID only after the above sequence */
5550 id = get_aux_id(kbdc);
5551 if (id != PSM_EXPLORER_ID)
5555 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
5560 * XXX: this is a kludge to fool some KVM switch products
5561 * which think they are clever enough to know the 4-byte IntelliMouse
5562 * protocol, and assume any other protocols use 3-byte packets.
5563 * They don't convey 4-byte data packets from the IntelliMouse Explorer
5564 * correctly to the host computer because of this!
5565 * The following sequence is actually IntelliMouse's "wake up"
5566 * sequence; it will make the KVM think the mouse is IntelliMouse
5567 * when it is in fact IntelliMouse Explorer.
5569 for (i = 0; i < nitems(rate0); ++i)
5570 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
5579 * Logitech MouseMan+ and FirstMouse+ will also respond to this
5580 * probe routine and act like IntelliMouse.
5583 enable_msintelli(struct psm_softc *sc, enum probearg arg)
5585 KBDC kbdc = sc->kbdc;
5586 static u_char rate[] = { 200, 100, 80, };
5590 /* the special sequence to enable the third button and the roller. */
5591 for (i = 0; i < nitems(rate); ++i)
5592 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5594 /* the device will give the genuine ID only after the above sequence */
5595 id = get_aux_id(kbdc);
5596 if (id != PSM_INTELLI_ID)
5609 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
5612 enable_4dmouse(struct psm_softc *sc, enum probearg arg)
5614 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5615 KBDC kbdc = sc->kbdc;
5619 for (i = 0; i < nitems(rate); ++i)
5620 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5622 id = get_aux_id(kbdc);
5624 * WinEasy 4D, 4 Way Scroll 4D: 6
5625 * Cable-Free 4D: 8 (4DPLUS)
5626 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
5628 if (id != PSM_4DMOUSE_ID)
5632 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
5641 * Newer wheel mice from A4 Tech seem to use this protocol.
5642 * Older models are recognized as either 4D Mouse or IntelliMouse.
5645 enable_4dplus(struct psm_softc *sc, enum probearg arg)
5647 KBDC kbdc = sc->kbdc;
5651 * enable_4dmouse() already issued the following ID sequence...
5652 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5655 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
5656 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5660 id = get_aux_id(kbdc);
5664 case PSM_4DPLUS_RFSW35_ID:
5671 sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
5678 /* Synaptics Touchpad */
5680 synaptics_sysctl(SYSCTL_HANDLER_ARGS)
5682 struct psm_softc *sc;
5685 if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 ||
5686 oidp->oid_arg2 > SYNAPTICS_SYSCTL_LAST)
5689 sc = oidp->oid_arg1;
5691 /* Read the current value. */
5692 arg = *(int *)((char *)sc + oidp->oid_arg2);
5693 error = sysctl_handle_int(oidp, &arg, 0, req);
5696 if (error || !req->newptr)
5700 * Check that the new value is in the concerned node's range
5703 switch (oidp->oid_arg2) {
5704 case SYNAPTICS_SYSCTL_MIN_PRESSURE:
5705 case SYNAPTICS_SYSCTL_MAX_PRESSURE:
5706 if (arg < 0 || arg > 255)
5709 case SYNAPTICS_SYSCTL_MAX_WIDTH:
5710 if (arg < 4 || arg > 15)
5713 case SYNAPTICS_SYSCTL_MARGIN_TOP:
5714 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
5715 case SYNAPTICS_SYSCTL_NA_TOP:
5716 case SYNAPTICS_SYSCTL_NA_BOTTOM:
5717 if (arg < 0 || arg > sc->synhw.maximumYCoord)
5720 case SYNAPTICS_SYSCTL_SOFTBUTTON2_X:
5721 case SYNAPTICS_SYSCTL_SOFTBUTTON3_X:
5722 /* Softbuttons is clickpad only feature */
5723 if (!sc->synhw.capClickPad && arg != 0)
5726 case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
5727 case SYNAPTICS_SYSCTL_MARGIN_LEFT:
5728 case SYNAPTICS_SYSCTL_NA_RIGHT:
5729 case SYNAPTICS_SYSCTL_NA_LEFT:
5730 if (arg < 0 || arg > sc->synhw.maximumXCoord)
5733 case SYNAPTICS_SYSCTL_WINDOW_MIN:
5734 case SYNAPTICS_SYSCTL_WINDOW_MAX:
5735 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
5736 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
5739 case SYNAPTICS_SYSCTL_MULTIPLICATOR:
5740 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
5741 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
5742 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
5743 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
5744 case SYNAPTICS_SYSCTL_DIV_MIN:
5745 case SYNAPTICS_SYSCTL_DIV_MAX:
5746 case SYNAPTICS_SYSCTL_DIV_MAX_NA:
5747 case SYNAPTICS_SYSCTL_DIV_LEN:
5748 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
5749 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
5753 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
5754 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
5755 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
5759 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
5760 if (arg < -sc->synhw.maximumXCoord ||
5761 arg > sc->synhw.maximumXCoord)
5764 case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y:
5765 /* Softbuttons is clickpad only feature */
5766 if (!sc->synhw.capClickPad && arg != 0)
5769 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
5770 if (arg < -sc->synhw.maximumYCoord ||
5771 arg > sc->synhw.maximumYCoord)
5774 case SYNAPTICS_SYSCTL_TOUCHPAD_OFF:
5775 case SYNAPTICS_SYSCTL_THREE_FINGER_DRAG:
5776 case SYNAPTICS_SYSCTL_NATURAL_SCROLL:
5777 if (arg < 0 || arg > 1)
5785 *(int *)((char *)sc + oidp->oid_arg2) = arg;
5791 synaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc)
5794 * Set predefined sizes for softbuttons.
5795 * Values are taken to match HP Pavilion dv6 clickpad drawings
5796 * with thin middle softbutton placed on separator
5799 /* hw.psm.synaptics.softbuttons_y */
5800 sc->syninfo.softbuttons_y = sc->synhw.topButtonPad ? -1700 : 1700;
5801 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5802 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5804 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5805 sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y,
5806 synaptics_sysctl, "I",
5807 "Vertical size of softbuttons area");
5809 /* hw.psm.synaptics.softbutton2_x */
5810 sc->syninfo.softbutton2_x = 3100;
5811 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5812 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5814 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5815 sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X,
5816 synaptics_sysctl, "I",
5817 "Horisontal position of 2-nd softbutton left edge (0-disable)");
5819 /* hw.psm.synaptics.softbutton3_x */
5820 sc->syninfo.softbutton3_x = 3900;
5821 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5822 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5824 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5825 sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X,
5826 synaptics_sysctl, "I",
5827 "Horisontal position of 3-rd softbutton left edge (0-disable)");
5831 synaptics_sysctl_create_tree(struct psm_softc *sc, const char *name,
5835 if (sc->syninfo.sysctl_tree != NULL)
5838 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
5839 sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
5840 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
5841 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name,
5842 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, descr);
5844 /* hw.psm.synaptics.directional_scrolls. */
5845 sc->syninfo.directional_scrolls = 0;
5846 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5847 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5848 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
5849 &sc->syninfo.directional_scrolls, 0,
5850 "Enable hardware scrolling pad (if non-zero) or register it as "
5851 "extended buttons (if 0)");
5853 /* hw.psm.synaptics.max_x. */
5854 sc->syninfo.max_x = 6143;
5855 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5856 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5857 "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY,
5858 &sc->syninfo.max_x, 0,
5859 "Horizontal reporting range");
5861 /* hw.psm.synaptics.max_y. */
5862 sc->syninfo.max_y = 6143;
5863 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5864 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5865 "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY,
5866 &sc->syninfo.max_y, 0,
5867 "Vertical reporting range");
5870 * Turn off two finger scroll if we have a
5871 * physical area reserved for scrolling or when
5872 * there's no multi finger support.
5874 if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 &&
5875 sc->synhw.capAdvancedGestures == 0))
5876 sc->syninfo.two_finger_scroll = 0;
5878 sc->syninfo.two_finger_scroll = 1;
5879 /* hw.psm.synaptics.two_finger_scroll. */
5880 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5881 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5882 "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY,
5883 &sc->syninfo.two_finger_scroll, 0,
5884 "Enable two finger scrolling");
5886 /* hw.psm.synaptics.min_pressure. */
5887 sc->syninfo.min_pressure = 32;
5888 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5889 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5891 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5892 sc, SYNAPTICS_SYSCTL_MIN_PRESSURE,
5893 synaptics_sysctl, "I",
5894 "Minimum pressure required to start an action");
5896 /* hw.psm.synaptics.max_pressure. */
5897 sc->syninfo.max_pressure = 220;
5898 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5899 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5901 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5902 sc, SYNAPTICS_SYSCTL_MAX_PRESSURE,
5903 synaptics_sysctl, "I",
5904 "Maximum pressure to detect palm");
5906 /* hw.psm.synaptics.max_width. */
5907 sc->syninfo.max_width = 10;
5908 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5909 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5911 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5912 sc, SYNAPTICS_SYSCTL_MAX_WIDTH,
5913 synaptics_sysctl, "I",
5914 "Maximum finger width to detect palm");
5916 /* hw.psm.synaptics.top_margin. */
5917 sc->syninfo.margin_top = 200;
5918 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5919 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5921 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5922 sc, SYNAPTICS_SYSCTL_MARGIN_TOP,
5923 synaptics_sysctl, "I",
5926 /* hw.psm.synaptics.right_margin. */
5927 sc->syninfo.margin_right = 200;
5928 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5929 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5931 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5932 sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
5933 synaptics_sysctl, "I",
5936 /* hw.psm.synaptics.bottom_margin. */
5937 sc->syninfo.margin_bottom = 200;
5938 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5939 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5941 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5942 sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
5943 synaptics_sysctl, "I",
5946 /* hw.psm.synaptics.left_margin. */
5947 sc->syninfo.margin_left = 200;
5948 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5949 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5951 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5952 sc, SYNAPTICS_SYSCTL_MARGIN_LEFT,
5953 synaptics_sysctl, "I",
5956 /* hw.psm.synaptics.na_top. */
5957 sc->syninfo.na_top = 1783;
5958 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5959 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5961 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5962 sc, SYNAPTICS_SYSCTL_NA_TOP,
5963 synaptics_sysctl, "I",
5964 "Top noisy area, where weight_previous_na is used instead "
5965 "of weight_previous");
5967 /* hw.psm.synaptics.na_right. */
5968 sc->syninfo.na_right = 563;
5969 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5970 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5972 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5973 sc, SYNAPTICS_SYSCTL_NA_RIGHT,
5974 synaptics_sysctl, "I",
5975 "Right noisy area, where weight_previous_na is used instead "
5976 "of weight_previous");
5978 /* hw.psm.synaptics.na_bottom. */
5979 sc->syninfo.na_bottom = 1408;
5980 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5981 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5983 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5984 sc, SYNAPTICS_SYSCTL_NA_BOTTOM,
5985 synaptics_sysctl, "I",
5986 "Bottom noisy area, where weight_previous_na is used instead "
5987 "of weight_previous");
5989 /* hw.psm.synaptics.na_left. */
5990 sc->syninfo.na_left = 1600;
5991 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5992 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5994 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
5995 sc, SYNAPTICS_SYSCTL_NA_LEFT,
5996 synaptics_sysctl, "I",
5997 "Left noisy area, where weight_previous_na is used instead "
5998 "of weight_previous");
6000 /* hw.psm.synaptics.window_min. */
6001 sc->syninfo.window_min = 4;
6002 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6003 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6005 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6006 sc, SYNAPTICS_SYSCTL_WINDOW_MIN,
6007 synaptics_sysctl, "I",
6008 "Minimum window size to start an action");
6010 /* hw.psm.synaptics.window_max. */
6011 sc->syninfo.window_max = 10;
6012 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6013 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6015 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6016 sc, SYNAPTICS_SYSCTL_WINDOW_MAX,
6017 synaptics_sysctl, "I",
6018 "Maximum window size");
6020 /* hw.psm.synaptics.multiplicator. */
6021 sc->syninfo.multiplicator = 10000;
6022 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6023 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6025 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6026 sc, SYNAPTICS_SYSCTL_MULTIPLICATOR,
6027 synaptics_sysctl, "I",
6028 "Multiplicator to increase precision in averages and divisions");
6030 /* hw.psm.synaptics.weight_current. */
6031 sc->syninfo.weight_current = 3;
6032 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6033 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6035 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6036 sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
6037 synaptics_sysctl, "I",
6038 "Weight of the current movement in the new average");
6040 /* hw.psm.synaptics.weight_previous. */
6041 sc->syninfo.weight_previous = 6;
6042 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6043 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6045 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6046 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
6047 synaptics_sysctl, "I",
6048 "Weight of the previous average");
6050 /* hw.psm.synaptics.weight_previous_na. */
6051 sc->syninfo.weight_previous_na = 20;
6052 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6053 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6054 "weight_previous_na",
6055 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6056 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
6057 synaptics_sysctl, "I",
6058 "Weight of the previous average (inside the noisy area)");
6060 /* hw.psm.synaptics.weight_len_squared. */
6061 sc->syninfo.weight_len_squared = 2000;
6062 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6063 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6064 "weight_len_squared",
6065 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6066 sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
6067 synaptics_sysctl, "I",
6068 "Length (squared) of segments where weight_previous "
6069 "starts to decrease");
6071 /* hw.psm.synaptics.div_min. */
6072 sc->syninfo.div_min = 9;
6073 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6074 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6076 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6077 sc, SYNAPTICS_SYSCTL_DIV_MIN,
6078 synaptics_sysctl, "I",
6079 "Divisor for fast movements");
6081 /* hw.psm.synaptics.div_max. */
6082 sc->syninfo.div_max = 17;
6083 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6084 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6086 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6087 sc, SYNAPTICS_SYSCTL_DIV_MAX,
6088 synaptics_sysctl, "I",
6089 "Divisor for slow movements");
6091 /* hw.psm.synaptics.div_max_na. */
6092 sc->syninfo.div_max_na = 30;
6093 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6094 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6096 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6097 sc, SYNAPTICS_SYSCTL_DIV_MAX_NA,
6098 synaptics_sysctl, "I",
6099 "Divisor with slow movements (inside the noisy area)");
6101 /* hw.psm.synaptics.div_len. */
6102 sc->syninfo.div_len = 100;
6103 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6104 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6106 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6107 sc, SYNAPTICS_SYSCTL_DIV_LEN,
6108 synaptics_sysctl, "I",
6109 "Length of segments where div_max starts to decrease");
6111 /* hw.psm.synaptics.tap_max_delta. */
6112 sc->syninfo.tap_max_delta = 80;
6113 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6114 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6116 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6117 sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
6118 synaptics_sysctl, "I",
6119 "Length of segments above which a tap is ignored");
6121 /* hw.psm.synaptics.tap_min_queue. */
6122 sc->syninfo.tap_min_queue = 2;
6123 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6124 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6126 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6127 sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
6128 synaptics_sysctl, "I",
6129 "Number of packets required to consider a tap");
6131 /* hw.psm.synaptics.taphold_timeout. */
6132 sc->gesture.in_taphold = 0;
6133 sc->syninfo.taphold_timeout = tap_timeout;
6134 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6135 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6137 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6138 sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
6139 synaptics_sysctl, "I",
6140 "Maximum elapsed time between two taps to consider a tap-hold "
6143 /* hw.psm.synaptics.vscroll_hor_area. */
6144 sc->syninfo.vscroll_hor_area = 0; /* 1300 */
6145 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6146 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6148 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6149 sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
6150 synaptics_sysctl, "I",
6151 "Area reserved for horizontal virtual scrolling");
6153 /* hw.psm.synaptics.vscroll_ver_area. */
6154 sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right;
6155 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6156 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6158 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6159 sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
6160 synaptics_sysctl, "I",
6161 "Area reserved for vertical virtual scrolling");
6163 /* hw.psm.synaptics.vscroll_min_delta. */
6164 sc->syninfo.vscroll_min_delta = 50;
6165 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6166 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6167 "vscroll_min_delta",
6168 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6169 sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
6170 synaptics_sysctl, "I",
6171 "Minimum movement to consider virtual scrolling");
6173 /* hw.psm.synaptics.vscroll_div_min. */
6174 sc->syninfo.vscroll_div_min = 100;
6175 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6176 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6178 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6179 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
6180 synaptics_sysctl, "I",
6181 "Divisor for fast scrolling");
6183 /* hw.psm.synaptics.vscroll_div_min. */
6184 sc->syninfo.vscroll_div_max = 150;
6185 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6186 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6188 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6189 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
6190 synaptics_sysctl, "I",
6191 "Divisor for slow scrolling");
6193 /* hw.psm.synaptics.touchpad_off. */
6194 sc->syninfo.touchpad_off = 0;
6195 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6196 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6198 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6199 sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF,
6200 synaptics_sysctl, "I",
6201 "Turn off touchpad");
6203 sc->syninfo.three_finger_drag = 0;
6204 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6205 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6206 "three_finger_drag",
6207 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6208 sc, SYNAPTICS_SYSCTL_THREE_FINGER_DRAG,
6209 synaptics_sysctl, "I",
6210 "Enable dragging with three fingers");
6212 /* hw.psm.synaptics.natural_scroll. */
6213 sc->syninfo.natural_scroll = 0;
6214 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6215 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6217 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6218 sc, SYNAPTICS_SYSCTL_NATURAL_SCROLL,
6219 synaptics_sysctl, "I",
6220 "Enable natural scrolling");
6222 sc->syninfo.softbuttons_y = 0;
6223 sc->syninfo.softbutton2_x = 0;
6224 sc->syninfo.softbutton3_x = 0;
6226 /* skip softbuttons sysctl on not clickpads */
6227 if (sc->synhw.capClickPad)
6228 synaptics_sysctl_create_softbuttons_tree(sc);
6232 synaptics_preferred_mode(struct psm_softc *sc) {
6235 /* Check if we are in relative mode */
6236 if (sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
6237 if (tap_enabled == 0)
6239 * Disable tap & drag gestures. We use a Mode Byte
6240 * and set the DisGest bit (see §2.5 of Synaptics
6241 * TouchPad Interfacing Guide).
6246 * Enable tap & drag gestures. We use a Mode Byte
6247 * and clear the DisGest bit (see §2.5 of Synaptics
6248 * TouchPad Interfacing Guide).
6255 /* request wmode where available */
6256 if (sc->synhw.capExtended)
6263 synaptics_set_mode(struct psm_softc *sc, int mode_byte) {
6264 mouse_ext_command(sc->kbdc, mode_byte);
6266 /* "Commit" the Set Mode Byte command sent above. */
6267 set_mouse_sampling_rate(sc->kbdc, 20);
6270 * Enable advanced gestures mode if supported and we are not entering
6271 * passthrough or relative mode.
6273 if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) &&
6274 sc->hw.model == MOUSE_MODEL_SYNAPTICS && !(mode_byte & (1 << 5))) {
6275 mouse_ext_command(sc->kbdc, SYNAPTICS_READ_MODEL_ID);
6276 set_mouse_sampling_rate(sc->kbdc, 0xc8);
6281 * AUX MUX detection code should be placed at very beginning of probe sequence
6282 * at least before 4-byte protocol mouse probes e.g. MS IntelliMouse probe as
6283 * latter can trigger switching the MUX to incompatible state.
6286 enable_synaptics_mux(struct psm_softc *sc, enum probearg arg)
6288 KBDC kbdc = sc->kbdc;
6291 int active_ports_count = 0;
6292 int active_ports_mask = 0;
6294 sc->muxsinglesyna = FALSE;
6296 if (mux_disabled == 1 || (mux_disabled == -1 &&
6297 (kbdc->quirks & KBDC_QUIRK_DISABLE_MUX_PROBE) != 0))
6300 version = enable_aux_mux(kbdc);
6304 for (port = 0; port < KBDC_AUX_MUX_NUM_PORTS; port++) {
6305 VLOG(3, (LOG_DEBUG, "aux_mux: ping port %d\n", port));
6306 set_active_aux_mux_port(kbdc, port);
6307 if (enable_aux_dev(kbdc) && disable_aux_dev(kbdc)) {
6308 active_ports_count++;
6309 active_ports_mask |= 1 << port;
6314 printf("Active Multiplexing PS/2 controller v%d.%d with %d "
6315 "active port(s)\n", version >> 4 & 0x0f, version & 0x0f,
6316 active_ports_count);
6318 /* psm has a special support for GenMouse + SynTouchpad combination */
6319 for (port = 0; port < KBDC_AUX_MUX_NUM_PORTS; port++) {
6320 if ((active_ports_mask & 1 << port) == 0)
6322 VLOG(3, (LOG_DEBUG, "aux_mux: probe port %d\n", port));
6323 set_active_aux_mux_port(kbdc, port);
6324 probe = enable_synaptics(sc, arg);
6332 /* IRQ handler does not support active multiplexing mode */
6333 disable_aux_mux(kbdc);
6335 /* Is MUX still alive after switching back to legacy mode? */
6336 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
6338 * On some laptops e.g. Lenovo X121e dead AUX MUX can be
6339 * brought back to life with resetting of keyboard.
6342 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
6343 printf("psm%d: AUX MUX hang detected!\n", sc->unit);
6344 printf("Consider adding hw.psm.mux_disabled=1 to "
6345 "loader tunables\n");
6348 empty_both_buffers(kbdc, 10); /* remove stray data if any */
6350 /* Don't disable syncbit checks if Synaptics is only device on MUX */
6351 if (active_ports_count == 1)
6352 sc->muxsinglesyna = probe;
6353 return (active_ports_count != 1 ? probe : FALSE);
6357 enable_single_synaptics_mux(struct psm_softc *sc, enum probearg arg)
6359 /* Synaptics device is already initialized in enable_synaptics_mux */
6360 return (sc->muxsinglesyna);
6364 enable_synaptics(struct psm_softc *sc, enum probearg arg)
6367 struct psmcpnp_softc *psmcpnp_sc;
6368 KBDC kbdc = sc->kbdc;
6369 synapticshw_t synhw;
6373 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
6376 * Just to be on the safe side: this avoids troubles with
6377 * following mouse_ext_command() when the previous command
6378 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
6379 * Synaptics Touchpad behaviour.
6381 set_mouse_scaling(kbdc, 1);
6383 /* Identify the Touchpad version. */
6384 if (mouse_ext_command(kbdc, SYNAPTICS_READ_IDENTITY) == 0)
6386 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6388 if (status[1] != 0x47)
6391 bzero(&synhw, sizeof(synhw));
6392 synhw.infoMinor = status[0];
6393 synhw.infoMajor = status[2] & 0x0f;
6396 printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
6399 if (synhw.infoMajor < 4) {
6400 printf(" Unsupported (pre-v4) Touchpad detected\n");
6404 /* Get the Touchpad model information. */
6405 if (mouse_ext_command(kbdc, SYNAPTICS_READ_MODEL_ID) == 0)
6407 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6409 if ((status[1] & 0x01) != 0) {
6410 printf(" Failed to read model information\n");
6414 synhw.infoRot180 = (status[0] & 0x80) != 0;
6415 synhw.infoPortrait = (status[0] & 0x40) != 0;
6416 synhw.infoSensor = status[0] & 0x3f;
6417 synhw.infoHardware = (status[1] & 0xfe) >> 1;
6418 synhw.infoNewAbs = (status[2] & 0x80) != 0;
6419 synhw.capPen = (status[2] & 0x40) != 0;
6420 synhw.infoSimplC = (status[2] & 0x20) != 0;
6421 synhw.infoGeometry = status[2] & 0x0f;
6424 printf(" Model information:\n");
6425 printf(" infoRot180: %d\n", synhw.infoRot180);
6426 printf(" infoPortrait: %d\n", synhw.infoPortrait);
6427 printf(" infoSensor: %d\n", synhw.infoSensor);
6428 printf(" infoHardware: %d\n", synhw.infoHardware);
6429 printf(" infoNewAbs: %d\n", synhw.infoNewAbs);
6430 printf(" capPen: %d\n", synhw.capPen);
6431 printf(" infoSimplC: %d\n", synhw.infoSimplC);
6432 printf(" infoGeometry: %d\n", synhw.infoGeometry);
6436 * Typical bezel limits. Taken from 'Synaptics
6437 * PS/2 * TouchPad Interfacing Guide' p.3.2.3.
6439 synhw.maximumXCoord = 5472;
6440 synhw.maximumYCoord = 4448;
6441 synhw.minimumXCoord = 1472;
6442 synhw.minimumYCoord = 1408;
6444 /* Read the extended capability bits. */
6445 if (mouse_ext_command(kbdc, SYNAPTICS_READ_CAPABILITIES) == 0)
6447 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6449 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
6450 printf(" Failed to read extended capability bits\n");
6454 psmcpnp = devclass_get_device(devclass_find(PSMCPNP_DRIVER_NAME),
6456 psmcpnp_sc = (psmcpnp != NULL) ? device_get_softc(psmcpnp) : NULL;
6458 /* Set the different capabilities when they exist. */
6460 synhw.capExtended = (status[0] & 0x80) != 0;
6461 if (synhw.capExtended) {
6462 synhw.nExtendedQueries = (status[0] & 0x70) >> 4;
6463 synhw.capMiddle = (status[0] & 0x04) != 0;
6464 synhw.capPassthrough = (status[2] & 0x80) != 0;
6465 synhw.capLowPower = (status[2] & 0x40) != 0;
6466 synhw.capMultiFingerReport =
6467 (status[2] & 0x20) != 0;
6468 synhw.capSleep = (status[2] & 0x10) != 0;
6469 synhw.capFourButtons = (status[2] & 0x08) != 0;
6470 synhw.capBallistics = (status[2] & 0x04) != 0;
6471 synhw.capMultiFinger = (status[2] & 0x02) != 0;
6472 synhw.capPalmDetect = (status[2] & 0x01) != 0;
6474 if (!set_mouse_scaling(kbdc, 1))
6476 if (mouse_ext_command(kbdc, SYNAPTICS_READ_RESOLUTIONS) == 0)
6478 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6481 if (status[0] != 0 && (status[1] & 0x80) && status[2] != 0) {
6482 synhw.infoXupmm = status[0];
6483 synhw.infoYupmm = status[2];
6487 printf(" Extended capabilities:\n");
6488 printf(" capExtended: %d\n", synhw.capExtended);
6489 printf(" capMiddle: %d\n", synhw.capMiddle);
6490 printf(" nExtendedQueries: %d\n",
6491 synhw.nExtendedQueries);
6492 printf(" capPassthrough: %d\n", synhw.capPassthrough);
6493 printf(" capLowPower: %d\n", synhw.capLowPower);
6494 printf(" capMultiFingerReport: %d\n",
6495 synhw.capMultiFingerReport);
6496 printf(" capSleep: %d\n", synhw.capSleep);
6497 printf(" capFourButtons: %d\n", synhw.capFourButtons);
6498 printf(" capBallistics: %d\n", synhw.capBallistics);
6499 printf(" capMultiFinger: %d\n", synhw.capMultiFinger);
6500 printf(" capPalmDetect: %d\n", synhw.capPalmDetect);
6501 printf(" infoXupmm: %d\n", synhw.infoXupmm);
6502 printf(" infoYupmm: %d\n", synhw.infoYupmm);
6506 * If nExtendedQueries is 1 or greater, then the TouchPad
6507 * supports this number of extended queries. We can load
6508 * more information about buttons using query 0x09.
6510 if (synhw.nExtendedQueries >= 1) {
6511 if (!set_mouse_scaling(kbdc, 1))
6513 if (mouse_ext_command(kbdc,
6514 SYNAPTICS_READ_EXTENDED) == 0)
6516 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6518 synhw.verticalScroll = (status[0] & 0x01) != 0;
6519 synhw.horizontalScroll = (status[0] & 0x02) != 0;
6520 synhw.verticalWheel = (status[0] & 0x08) != 0;
6521 synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
6522 synhw.capEWmode = (status[0] & 0x04) != 0;
6524 printf(" Extended model ID:\n");
6525 printf(" verticalScroll: %d\n",
6526 synhw.verticalScroll);
6527 printf(" horizontalScroll: %d\n",
6528 synhw.horizontalScroll);
6529 printf(" verticalWheel: %d\n",
6530 synhw.verticalWheel);
6531 printf(" nExtendedButtons: %d\n",
6532 synhw.nExtendedButtons);
6533 printf(" capEWmode: %d\n",
6537 * Add the number of extended buttons to the total
6538 * button support count, including the middle button
6539 * if capMiddle support bit is set.
6541 buttons = synhw.nExtendedButtons + synhw.capMiddle;
6544 * If the capFourButtons support bit is set,
6545 * add a fourth button to the total button count.
6547 buttons = synhw.capFourButtons ? 1 : 0;
6549 /* Read the continued capabilities bits. */
6550 if (synhw.nExtendedQueries >= 4) {
6551 if (!set_mouse_scaling(kbdc, 1))
6553 if (mouse_ext_command(kbdc,
6554 SYNAPTICS_READ_CAPABILITIES_CONT) == 0)
6556 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6559 synhw.capClickPad = (status[1] & 0x01) << 1;
6560 synhw.capClickPad |= (status[0] & 0x10) != 0;
6561 synhw.capDeluxeLEDs = (status[1] & 0x02) != 0;
6562 synhw.noAbsoluteFilter = (status[1] & 0x04) != 0;
6563 synhw.capReportsV = (status[1] & 0x08) != 0;
6564 synhw.capUniformClickPad = (status[1] & 0x10) != 0;
6565 synhw.capReportsMin = (status[1] & 0x20) != 0;
6566 synhw.capInterTouch = (status[1] & 0x40) != 0;
6567 synhw.capReportsMax = (status[0] & 0x02) != 0;
6568 synhw.capClearPad = (status[0] & 0x04) != 0;
6569 synhw.capAdvancedGestures = (status[0] & 0x08) != 0;
6570 synhw.capCoveredPad = (status[0] & 0x80) != 0;
6572 if (synhw.capReportsMax) {
6573 if (!set_mouse_scaling(kbdc, 1))
6575 if (mouse_ext_command(kbdc,
6576 SYNAPTICS_READ_MAX_COORDS) == 0)
6578 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6581 synhw.maximumXCoord = (status[0] << 5) |
6582 ((status[1] & 0x0f) << 1);
6583 synhw.maximumYCoord = (status[2] << 5) |
6584 ((status[1] & 0xf0) >> 3);
6587 if (synhw.capReportsMin) {
6588 if (!set_mouse_scaling(kbdc, 1))
6590 if (mouse_ext_command(kbdc,
6591 SYNAPTICS_READ_MIN_COORDS) == 0)
6593 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6596 synhw.minimumXCoord = (status[0] << 5) |
6597 ((status[1] & 0x0f) << 1);
6598 synhw.minimumYCoord = (status[2] << 5) |
6599 ((status[1] & 0xf0) >> 3);
6603 * ClickPad properties are not exported through PS/2
6604 * protocol. Detection is based on controller's PnP ID.
6606 if (synhw.capClickPad && psmcpnp_sc != NULL) {
6607 switch (psmcpnp_sc->type) {
6608 case PSMCPNP_FORCEPAD:
6611 case PSMCPNP_TOPBUTTONPAD:
6612 synhw.topButtonPad = 1;
6620 printf(" Continued capabilities:\n");
6621 printf(" capClickPad: %d\n",
6623 printf(" capDeluxeLEDs: %d\n",
6624 synhw.capDeluxeLEDs);
6625 printf(" noAbsoluteFilter: %d\n",
6626 synhw.noAbsoluteFilter);
6627 printf(" capReportsV: %d\n",
6629 printf(" capUniformClickPad: %d\n",
6630 synhw.capUniformClickPad);
6631 printf(" capReportsMin: %d\n",
6632 synhw.capReportsMin);
6633 printf(" capInterTouch: %d\n",
6634 synhw.capInterTouch);
6635 printf(" capReportsMax: %d\n",
6636 synhw.capReportsMax);
6637 printf(" capClearPad: %d\n",
6639 printf(" capAdvancedGestures: %d\n",
6640 synhw.capAdvancedGestures);
6641 printf(" capCoveredPad: %d\n",
6642 synhw.capCoveredPad);
6643 if (synhw.capReportsMax) {
6644 printf(" maximumXCoord: %d\n",
6645 synhw.maximumXCoord);
6646 printf(" maximumYCoord: %d\n",
6647 synhw.maximumYCoord);
6649 if (synhw.capReportsMin) {
6650 printf(" minimumXCoord: %d\n",
6651 synhw.minimumXCoord);
6652 printf(" minimumYCoord: %d\n",
6653 synhw.minimumYCoord);
6655 if (synhw.capClickPad) {
6656 printf(" Clickpad capabilities:\n");
6657 printf(" forcePad: %d\n",
6659 printf(" topButtonPad: %d\n",
6660 synhw.topButtonPad);
6663 buttons += synhw.capClickPad;
6668 if (synhw.capExtended)
6669 printf(" Additional Buttons: %d\n", buttons);
6671 printf(" No extended capabilities\n");
6675 * Add the default number of 3 buttons to the total
6676 * count of supported buttons reported above.
6681 * Read the mode byte.
6683 * XXX: Note the Synaptics documentation also defines the first
6684 * byte of the response to this query to be a constant 0x3b, this
6685 * does not appear to be true for Touchpads with guest devices.
6687 if (mouse_ext_command(kbdc, SYNAPTICS_READ_MODES) == 0)
6689 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6691 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
6692 printf(" Failed to read mode byte\n");
6698 if (!synaptics_support)
6701 /* Set mouse type just now for synaptics_set_mode() */
6702 sc->hw.model = MOUSE_MODEL_SYNAPTICS;
6704 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6706 if (trackpoint_support && synhw.capPassthrough) {
6707 enable_trackpoint(sc, arg);
6710 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
6713 /* Create sysctl tree. */
6714 synaptics_sysctl_create_tree(sc, "synaptics",
6715 "Synaptics TouchPad");
6716 sc->hw.buttons = buttons;
6723 synaptics_passthrough_on(struct psm_softc *sc)
6725 VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n"));
6726 synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5));
6730 synaptics_passthrough_off(struct psm_softc *sc)
6732 VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n"));
6733 set_mouse_scaling(sc->kbdc, 2);
6734 set_mouse_scaling(sc->kbdc, 1);
6735 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6738 /* IBM/Lenovo TrackPoint */
6740 trackpoint_command(struct psm_softc *sc, int cmd, int loc, int val)
6742 const int seq[] = { 0xe2, cmd, loc, val };
6745 if (sc->synhw.capPassthrough)
6746 synaptics_passthrough_on(sc);
6748 for (i = 0; i < nitems(seq); i++) {
6749 if (sc->synhw.capPassthrough &&
6750 (seq[i] == 0xff || seq[i] == 0xe7))
6751 if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) {
6752 synaptics_passthrough_off(sc);
6755 if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) {
6756 if (sc->synhw.capPassthrough)
6757 synaptics_passthrough_off(sc);
6762 if (sc->synhw.capPassthrough)
6763 synaptics_passthrough_off(sc);
6768 #define PSM_TPINFO(x) offsetof(struct psm_softc, tpinfo.x)
6774 trackpoint_sysctl(SYSCTL_HANDLER_ARGS)
6776 static const int data[][3] = {
6777 { 0x00, 0x4a, PSM_TPINFO(sensitivity) },
6778 { 0x00, 0x4d, PSM_TPINFO(inertia) },
6779 { 0x00, 0x60, PSM_TPINFO(uplateau) },
6780 { 0x00, 0x57, PSM_TPINFO(reach) },
6781 { 0x00, 0x58, PSM_TPINFO(draghys) },
6782 { 0x00, 0x59, PSM_TPINFO(mindrag) },
6783 { 0x00, 0x5a, PSM_TPINFO(upthresh) },
6784 { 0x00, 0x5c, PSM_TPINFO(threshold) },
6785 { 0x00, 0x5d, PSM_TPINFO(jenks) },
6786 { 0x00, 0x5e, PSM_TPINFO(ztime) },
6787 { 0x01, 0x2c, PSM_TPINFO(pts) },
6788 { 0x08, 0x2d, PSM_TPINFO(skipback) }
6790 struct psm_softc *sc;
6791 int error, newval, *oldvalp;
6794 if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
6798 oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
6800 error = sysctl_handle_int(oidp, &newval, 0, req);
6803 if (newval == *oldvalp)
6805 if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
6807 error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47,
6808 tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
6817 trackpoint_sysctl_create_tree(struct psm_softc *sc)
6820 if (sc->tpinfo.sysctl_tree != NULL)
6823 /* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
6824 sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
6825 sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
6826 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint",
6827 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "IBM/Lenovo TrackPoint");
6829 /* hw.psm.trackpoint.sensitivity */
6830 sc->tpinfo.sensitivity = 0x80;
6831 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6832 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6834 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6835 sc, TRACKPOINT_SYSCTL_SENSITIVITY,
6836 trackpoint_sysctl, "I",
6839 /* hw.psm.trackpoint.negative_inertia */
6840 sc->tpinfo.inertia = 0x06;
6841 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6842 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6844 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6845 sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
6846 trackpoint_sysctl, "I",
6847 "Negative inertia factor");
6849 /* hw.psm.trackpoint.upper_plateau */
6850 sc->tpinfo.uplateau = 0x61;
6851 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6852 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6854 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6855 sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
6856 trackpoint_sysctl, "I",
6857 "Transfer function upper plateau speed");
6859 /* hw.psm.trackpoint.backup_range */
6860 sc->tpinfo.reach = 0x0a;
6861 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6862 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6864 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6865 sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
6866 trackpoint_sysctl, "I",
6869 /* hw.psm.trackpoint.drag_hysteresis */
6870 sc->tpinfo.draghys = 0xff;
6871 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6872 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6874 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6875 sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
6876 trackpoint_sysctl, "I",
6879 /* hw.psm.trackpoint.minimum_drag */
6880 sc->tpinfo.mindrag = 0x14;
6881 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6882 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6884 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6885 sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
6886 trackpoint_sysctl, "I",
6889 /* hw.psm.trackpoint.up_threshold */
6890 sc->tpinfo.upthresh = 0xff;
6891 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6892 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6894 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6895 sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
6896 trackpoint_sysctl, "I",
6897 "Up threshold for release");
6899 /* hw.psm.trackpoint.threshold */
6900 sc->tpinfo.threshold = 0x08;
6901 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6902 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6904 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6905 sc, TRACKPOINT_SYSCTL_THRESHOLD,
6906 trackpoint_sysctl, "I",
6909 /* hw.psm.trackpoint.jenks_curvature */
6910 sc->tpinfo.jenks = 0x87;
6911 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6912 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6914 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6915 sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
6916 trackpoint_sysctl, "I",
6919 /* hw.psm.trackpoint.z_time */
6920 sc->tpinfo.ztime = 0x26;
6921 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6922 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6924 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6925 sc, TRACKPOINT_SYSCTL_Z_TIME,
6926 trackpoint_sysctl, "I",
6929 /* hw.psm.trackpoint.press_to_select */
6930 sc->tpinfo.pts = 0x00;
6931 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6932 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6934 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6935 sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
6936 trackpoint_sysctl, "I",
6939 /* hw.psm.trackpoint.skip_backups */
6940 sc->tpinfo.skipback = 0x00;
6941 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6942 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6944 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT,
6945 sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
6946 trackpoint_sysctl, "I",
6947 "Skip backups from drags");
6951 set_trackpoint_parameters(struct psm_softc *sc)
6953 trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity);
6954 trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau);
6955 trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia);
6956 trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach);
6957 trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys);
6958 trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag);
6959 trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh);
6960 trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold);
6961 trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks);
6962 trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime);
6963 if (sc->tpinfo.pts == 0x01)
6964 trackpoint_command(sc, 0x47, 0x2c, 0x01);
6965 if (sc->tpinfo.skipback == 0x01)
6966 trackpoint_command(sc, 0x47, 0x2d, 0x08);
6970 enable_trackpoint(struct psm_softc *sc, enum probearg arg)
6972 KBDC kbdc = sc->kbdc;
6976 * If called from enable_synaptics(), make sure that passthrough
6977 * mode is enabled so we can reach the trackpoint.
6978 * However, passthrough mode must be disabled before setting the
6979 * trackpoint parameters, as rackpoint_command() enables and disables
6980 * passthrough mode on its own.
6982 if (sc->synhw.capPassthrough)
6983 synaptics_passthrough_on(sc);
6985 if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
6986 read_aux_data(kbdc) != 0x01)
6988 id = read_aux_data(kbdc);
6993 if (!trackpoint_support)
6996 if (sc->synhw.capPassthrough)
6997 synaptics_passthrough_off(sc);
7000 trackpoint_sysctl_create_tree(sc);
7002 * Don't overwrite hwid and buttons when we are
7005 if (!sc->synhw.capPassthrough) {
7011 set_trackpoint_parameters(sc);
7016 if (sc->synhw.capPassthrough)
7017 synaptics_passthrough_off(sc);
7022 /* Interlink electronics VersaPad */
7024 enable_versapad(struct psm_softc *sc, enum probearg arg)
7026 KBDC kbdc = sc->kbdc;
7029 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
7030 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
7031 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
7032 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
7033 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
7034 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
7035 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
7037 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
7039 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
7041 return (TRUE); /* PS/2 absolute mode */
7044 /* Elantech Touchpad */
7046 elantech_read_1(KBDC kbdc, int hwversion, int reg, int *val)
7048 int res, readcmd, retidx;
7051 readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR;
7052 retidx = hwversion == 4 ? 1 : 0;
7054 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7055 res |= send_aux_command(kbdc, readcmd) != PSM_ACK;
7056 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7057 res |= send_aux_command(kbdc, reg) != PSM_ACK;
7058 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
7061 *val = resp[retidx];
7067 elantech_write_1(KBDC kbdc, int hwversion, int reg, int val)
7071 writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR;
7073 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7074 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
7075 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7076 res |= send_aux_command(kbdc, reg) != PSM_ACK;
7077 if (hwversion == 4) {
7078 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7079 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
7081 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7082 res |= send_aux_command(kbdc, val) != PSM_ACK;
7083 res |= set_mouse_scaling(kbdc, 1) == 0;
7089 elantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp)
7093 if (hwversion == 2) {
7094 res = set_mouse_scaling(kbdc, 1) == 0;
7095 res |= mouse_ext_command(kbdc, cmd) == 0;
7097 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
7098 res |= send_aux_command(kbdc, cmd) != PSM_ACK;
7100 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
7106 elantech_init(KBDC kbdc, elantechhw_t *elanhw)
7108 int i, val, res, hwversion, reg10;
7110 /* set absolute mode */
7111 hwversion = elanhw->hwversion;
7113 switch (hwversion) {
7115 reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4;
7116 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
7119 res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A);
7123 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
7126 res = elantech_write_1(kbdc, hwversion, 0x07, 0x01);
7132 /* Read back reg 0x10 to ensure hardware is ready. */
7133 if (res == 0 && reg10 >= 0) {
7134 for (i = 0; i < 5; i++) {
7135 if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0)
7144 printf("couldn't set absolute mode\n");
7150 elantech_init_synaptics(struct psm_softc *sc)
7153 /* Set capabilites required by movement smother */
7154 sc->synhw.infoMajor = sc->elanhw.hwversion;
7155 sc->synhw.infoMinor = sc->elanhw.fwversion;
7156 sc->synhw.infoXupmm = sc->elanhw.dpmmx;
7157 sc->synhw.infoYupmm = sc->elanhw.dpmmy;
7158 sc->synhw.verticalScroll = 0;
7159 sc->synhw.nExtendedQueries = 4;
7160 sc->synhw.capExtended = 1;
7161 sc->synhw.capPassthrough = sc->elanhw.hastrackpoint;
7162 sc->synhw.capClickPad = sc->elanhw.isclickpad;
7163 sc->synhw.capMultiFinger = 1;
7164 if (sc->elanhw.issemimt)
7165 sc->synhw.capAdvancedGestures = 1;
7167 sc->synhw.capReportsV = 1;
7168 sc->synhw.capPalmDetect = 1;
7169 sc->synhw.capPen = 0;
7170 sc->synhw.capReportsMax = 1;
7171 sc->synhw.maximumXCoord = sc->elanhw.sizex;
7172 sc->synhw.maximumYCoord = sc->elanhw.sizey;
7173 sc->synhw.capReportsMin = 1;
7174 sc->synhw.minimumXCoord = 0;
7175 sc->synhw.minimumYCoord = 0;
7177 if (sc->syninfo.sysctl_tree == NULL) {
7178 synaptics_sysctl_create_tree(sc, "elantech",
7179 "Elantech Touchpad");
7182 * Adjust synaptic smoother tunables
7183 * 1. Disable finger detection pressure threshold. Unlike
7184 * synaptics we assume the finger is acting when packet with
7185 * its X&Y arrives not when pressure exceedes some threshold
7186 * 2. Disable unrelated features like margins and noisy areas
7187 * 3. Disable virtual scroll areas as 2nd finger is preferable
7188 * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized
7190 * 5. Scale down divisors and movement lengths by a factor of 3
7191 * where 3 is Synaptics to Elantech (~2200/800) dpi ratio
7194 /* Set reporting range to be equal touchpad size */
7195 sc->syninfo.max_x = sc->elanhw.sizex;
7196 sc->syninfo.max_y = sc->elanhw.sizey;
7198 /* Disable finger detection pressure threshold */
7199 sc->syninfo.min_pressure = 1;
7201 /* Adjust palm width to nearly match synaptics w=10 */
7202 sc->syninfo.max_width = 7;
7204 /* Elans often report double & triple taps as single event */
7205 sc->syninfo.tap_min_queue = 1;
7207 /* Use full area of touchpad */
7208 sc->syninfo.margin_top = 0;
7209 sc->syninfo.margin_right = 0;
7210 sc->syninfo.margin_bottom = 0;
7211 sc->syninfo.margin_left = 0;
7213 /* Disable noisy area */
7214 sc->syninfo.na_top = 0;
7215 sc->syninfo.na_right = 0;
7216 sc->syninfo.na_bottom = 0;
7217 sc->syninfo.na_left = 0;
7219 /* Tune divisors and movement lengths */
7220 sc->syninfo.weight_len_squared = 200;
7221 sc->syninfo.div_min = 3;
7222 sc->syninfo.div_max = 6;
7223 sc->syninfo.div_max_na = 10;
7224 sc->syninfo.div_len = 30;
7225 sc->syninfo.tap_max_delta = 25;
7227 /* Disable virtual scrolling areas and tune its divisors */
7228 sc->syninfo.vscroll_hor_area = 0;
7229 sc->syninfo.vscroll_ver_area = 0;
7230 sc->syninfo.vscroll_min_delta = 15;
7231 sc->syninfo.vscroll_div_min = 30;
7232 sc->syninfo.vscroll_div_max = 50;
7234 /* Set bottom quarter as 42% - 16% - 42% sized softbuttons */
7235 if (sc->elanhw.isclickpad) {
7236 sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4;
7237 sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25;
7238 sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25;
7246 enable_elantech(struct psm_softc *sc, enum probearg arg)
7248 static const int ic2hw[] =
7249 /*IC: 0 1 2 3 4 5 6 7 8 9 a b c d e f */
7250 { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
7251 static const int fw_sizes[][3] = {
7252 /* FW.vers MaxX MaxY */
7253 { 0x020030, 1152, 768 },
7254 { 0x020800, 1152, 768 },
7255 { 0x020b00, 1152, 768 },
7256 { 0x040215, 900, 500 },
7257 { 0x040216, 819, 405 },
7258 { 0x040219, 900, 500 },
7260 elantechhw_t elanhw;
7261 int icversion, hwversion, xtr, i, id, resp[3], dpix, dpiy;
7262 KBDC kbdc = sc->kbdc;
7264 VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n"));
7266 set_mouse_scaling(kbdc, 1);
7267 set_mouse_scaling(kbdc, 1);
7268 set_mouse_scaling(kbdc, 1);
7269 if (get_mouse_status(kbdc, resp, 0, 3) != 3)
7272 if (!ELANTECH_MAGIC(resp))
7275 /* Identify the Touchpad version. */
7276 if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp))
7279 bzero(&elanhw, sizeof(elanhw));
7281 elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2];
7282 icversion = resp[0] & 0x0f;
7283 hwversion = ic2hw[icversion];
7286 printf("Elantech touchpad hardware v.%d firmware v.0x%06x\n",
7287 hwversion, elanhw.fwversion);
7289 if (ELANTECH_HW_IS_V1(elanhw.fwversion)) {
7290 printf (" Unsupported touchpad hardware (v1)\n");
7293 if (hwversion == 0) {
7294 printf (" Unknown touchpad hardware (firmware v.0x%06x)\n",
7299 /* Get the Touchpad model information. */
7300 elanhw.hwversion = hwversion;
7301 elanhw.issemimt = hwversion == 2;
7302 elanhw.isclickpad = (resp[1] & 0x10) != 0;
7303 elanhw.hascrc = (resp[1] & 0x40) != 0;
7304 elanhw.haspressure = elanhw.fwversion >= 0x020800;
7306 /* Read the capability bits. */
7307 if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) {
7308 printf(" Failed to read capability bits\n");
7312 elanhw.ntracesx = imax(resp[1], 3);
7313 elanhw.ntracesy = imax(resp[2], 3);
7314 elanhw.hastrackpoint = (resp[0] & 0x80) != 0;
7316 /* Get the touchpad resolution */
7317 switch (hwversion) {
7319 if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp)
7321 dpix = (resp[1] & 0x0f) * 10 + 790;
7322 dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790;
7323 elanhw.dpmmx = (dpix * 10 + 5) / 254;
7324 elanhw.dpmmy = (dpiy * 10 + 5) / 254;
7330 elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */
7334 if (!elantech_support)
7337 if (elantech_init(kbdc, &elanhw)) {
7338 printf("couldn't initialize elantech touchpad\n");
7343 * Get the touchpad reporting range.
7344 * On HW v.3 touchpads it should be done after switching hardware
7345 * to real resolution mode (by setting bit 3 of reg10)
7347 elanhw.dptracex = elanhw.dptracey = 64;
7348 for (i = 0; i < nitems(fw_sizes); i++) {
7349 if (elanhw.fwversion == fw_sizes[i][0]) {
7350 elanhw.sizex = fw_sizes[i][1];
7351 elanhw.sizey = fw_sizes[i][2];
7355 if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) {
7356 printf(" Failed to read touchpad size\n");
7357 elanhw.sizex = 10000; /* Arbitrary high values to */
7358 elanhw.sizey = 10000; /* prevent clipping in smoother */
7359 } else if (hwversion == 2) {
7360 if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) &&
7361 !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) {
7362 elanhw.dptracex = resp[1] / 2;
7363 elanhw.dptracey = resp[2] / 2;
7365 xtr = ((elanhw.fwversion >> 8) == 0x0208) ? 1 : 2;
7366 elanhw.sizex = (elanhw.ntracesx - xtr) * elanhw.dptracex;
7367 elanhw.sizey = (elanhw.ntracesy - xtr) * elanhw.dptracey;
7369 elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1];
7370 elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2];
7371 xtr = (elanhw.sizex % (elanhw.ntracesx - 2) == 0) ? 2 : 1;
7372 elanhw.dptracex = elanhw.sizex / (elanhw.ntracesx - xtr);
7373 elanhw.dptracey = elanhw.sizey / (elanhw.ntracesy - xtr);
7377 printf(" Model information:\n");
7378 printf(" MaxX: %d\n", elanhw.sizex);
7379 printf(" MaxY: %d\n", elanhw.sizey);
7380 printf(" DpmmX: %d\n", elanhw.dpmmx);
7381 printf(" DpmmY: %d\n", elanhw.dpmmy);
7382 printf(" TracesX: %d\n", elanhw.ntracesx);
7383 printf(" TracesY: %d\n", elanhw.ntracesy);
7384 printf(" DptraceX: %d\n", elanhw.dptracex);
7385 printf(" DptraceY: %d\n", elanhw.dptracey);
7386 printf(" SemiMT: %d\n", elanhw.issemimt);
7387 printf(" Clickpad: %d\n", elanhw.isclickpad);
7388 printf(" Trackpoint: %d\n", elanhw.hastrackpoint);
7389 printf(" CRC: %d\n", elanhw.hascrc);
7390 printf(" Pressure: %d\n", elanhw.haspressure);
7393 VLOG(3, (LOG_DEBUG, "elantech: END init\n"));
7396 sc->elanhw = elanhw;
7399 /* Initialize synaptics movement smoother */
7400 elantech_init_synaptics(sc);
7402 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
7403 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
7410 * Return true if 'now' is earlier than (start + (secs.usecs)).
7411 * Now may be NULL and the function will fetch the current time from
7412 * getmicrouptime(), or a cached 'now' can be passed in.
7413 * All values should be numbers derived from getmicrouptime().
7416 timeelapsed(start, secs, usecs, now)
7417 const struct timeval *start, *now;
7420 struct timeval snow, tv;
7422 /* if there is no 'now' passed in, the get it as a convience. */
7424 getmicrouptime(&snow);
7430 timevaladd(&tv, start);
7431 return (timevalcmp(&tv, now, <));
7435 psmresume(device_t dev)
7437 struct psm_softc *sc = device_get_softc(dev);
7438 int unit = device_get_unit(dev);
7441 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
7444 (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
7447 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
7449 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
7451 * Release the blocked process; it must be notified that
7452 * the device cannot be accessed anymore.
7454 sc->state &= ~PSM_ASLP;
7458 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
7463 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
7464 #ifdef EVDEV_SUPPORT
7465 MODULE_DEPEND(psm, evdev, 1, 1, 1);
7471 * This sucks up assignments from PNPBIOS and ACPI.
7475 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
7476 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
7477 * can be probed and attached only after the AT keyboard controller is
7478 * attached, we shall quietly reserve the IRQ resource for later use.
7479 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
7480 * copy the IRQ resource to the PS/2 mouse device instance hanging
7481 * under the keyboard controller, then probe and attach it.
7484 static devclass_t psmcpnp_devclass;
7486 static device_probe_t psmcpnp_probe;
7487 static device_attach_t psmcpnp_attach;
7489 static device_method_t psmcpnp_methods[] = {
7490 DEVMETHOD(device_probe, psmcpnp_probe),
7491 DEVMETHOD(device_attach, psmcpnp_attach),
7495 static driver_t psmcpnp_driver = {
7496 PSMCPNP_DRIVER_NAME,
7498 sizeof(struct psmcpnp_softc),
7501 static struct isa_pnp_id psmcpnp_ids[] = {
7502 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
7503 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */
7504 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */
7505 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
7506 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
7507 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
7508 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */
7509 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
7510 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
7511 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
7512 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
7513 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
7514 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
7518 /* _HID list for quirk detection. Any device below has _CID from psmcpnp_ids */
7519 static struct isa_pnp_id topbtpad_ids[] = {
7520 { 0x1700ae30, "Lenovo PS/2 clickpad port" }, /* LEN0017, ThinkPad */
7521 { 0x1800ae30, "Lenovo PS/2 clickpad port" }, /* LEN0018, ThinkPad */
7522 { 0x1900ae30, "Lenovo PS/2 clickpad port" }, /* LEN0019, ThinkPad */
7523 { 0x2300ae30, "Lenovo PS/2 clickpad port" }, /* LEN0023, ThinkPad */
7524 { 0x2a00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002a, ThinkPad */
7525 { 0x2b00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002b, ThinkPad */
7526 { 0x2c00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002c, ThinkPad */
7527 { 0x2d00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002d, ThinkPad */
7528 { 0x2e00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002e, ThinkPad */
7529 { 0x3300ae30, "Lenovo PS/2 clickpad port" }, /* LEN0033, ThinkPad */
7530 { 0x3400ae30, "Lenovo PS/2 clickpad port" }, /* LEN0034, ThinkPad */
7531 { 0x3500ae30, "Lenovo PS/2 clickpad port" }, /* LEN0035, ThinkPad */
7532 { 0x3600ae30, "Lenovo PS/2 clickpad port" }, /* LEN0036, ThinkPad */
7533 { 0x3700ae30, "Lenovo PS/2 clickpad port" }, /* LEN0037, ThinkPad */
7534 { 0x3800ae30, "Lenovo PS/2 clickpad port" }, /* LEN0038, ThinkPad */
7535 { 0x3900ae30, "Lenovo PS/2 clickpad port" }, /* LEN0039, ThinkPad */
7536 { 0x4100ae30, "Lenovo PS/2 clickpad port" }, /* LEN0041, ThinkPad */
7537 { 0x4200ae30, "Lenovo PS/2 clickpad port" }, /* LEN0042, ThinkPad */
7538 { 0x4500ae30, "Lenovo PS/2 clickpad port" }, /* LEN0045, ThinkPad */
7539 { 0x4700ae30, "Lenovo PS/2 clickpad port" }, /* LEN0047, ThinkPad */
7540 { 0x4900ae30, "Lenovo PS/2 clickpad port" }, /* LEN0049, ThinkPad */
7541 { 0x0020ae30, "Lenovo PS/2 clickpad port" }, /* LEN2000, ThinkPad */
7542 { 0x0120ae30, "Lenovo PS/2 clickpad port" }, /* LEN2001, ThinkPad */
7543 { 0x0220ae30, "Lenovo PS/2 clickpad port" }, /* LEN2002, ThinkPad */
7544 { 0x0320ae30, "Lenovo PS/2 clickpad port" }, /* LEN2003, ThinkPad */
7545 { 0x0420ae30, "Lenovo PS/2 clickpad port" }, /* LEN2004, ThinkPad */
7546 { 0x0520ae30, "Lenovo PS/2 clickpad port" }, /* LEN2005, ThinkPad */
7547 { 0x0620ae30, "Lenovo PS/2 clickpad port" }, /* LEN2006, ThinkPad */
7548 { 0x0720ae30, "Lenovo PS/2 clickpad port" }, /* LEN2007, ThinkPad */
7549 { 0x0820ae30, "Lenovo PS/2 clickpad port" }, /* LEN2008, ThinkPad */
7550 { 0x0920ae30, "Lenovo PS/2 clickpad port" }, /* LEN2009, ThinkPad */
7551 { 0x0a20ae30, "Lenovo PS/2 clickpad port" }, /* LEN200a, ThinkPad */
7552 { 0x0b20ae30, "Lenovo PS/2 clickpad port" }, /* LEN200b, ThinkPad */
7556 /* _HID list for quirk detection. Any device below has _CID from psmcpnp_ids */
7557 static struct isa_pnp_id forcepad_ids[] = {
7558 { 0x0d302e4f, "HP PS/2 forcepad port" }, /* SYN300D, EB 1040 */
7559 { 0x14302e4f, "HP PS/2 forcepad port" }, /* SYN3014, EB 1040 */
7564 create_a_copy(device_t atkbdc, device_t me)
7569 /* find the PS/2 mouse device instance under the keyboard controller */
7570 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
7571 device_get_unit(atkbdc));
7574 if (device_get_state(psm) != DS_NOTPRESENT)
7577 /* move our resource to the found device */
7578 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
7579 bus_delete_resource(me, SYS_RES_IRQ, 0);
7580 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
7582 /* ...then probe and attach it */
7583 return (device_probe_and_attach(psm));
7587 psmcpnp_probe(device_t dev)
7589 struct psmcpnp_softc *sc = device_get_softc(dev);
7590 struct resource *res;
7594 if (ISA_PNP_PROBE(device_get_parent(dev), dev, forcepad_ids) == 0)
7595 sc->type = PSMCPNP_FORCEPAD;
7596 else if (ISA_PNP_PROBE(device_get_parent(dev), dev, topbtpad_ids) == 0)
7597 sc->type = PSMCPNP_TOPBUTTONPAD;
7598 else if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids) == 0)
7599 sc->type = PSMCPNP_GENERIC;
7604 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
7605 * to the PS/2 mouse device node. But, some buggy PnP BIOS
7606 * declares the PS/2 mouse device node without an IRQ resource!
7607 * If this happens, we shall refer to device hints.
7608 * If we still don't find it there, use a hardcoded value... XXX
7611 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
7613 if (resource_long_value(PSM_DRIVER_NAME,
7614 device_get_unit(dev),"irq", &irq) != 0)
7616 device_printf(dev, "irq resource info is missing; "
7617 "assuming irq %ld\n", irq);
7618 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
7620 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0);
7621 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
7627 return ((res == NULL) ? ENXIO : 0);
7631 psmcpnp_attach(device_t dev)
7635 /* find the keyboard controller, which may be on acpi* or isa* bus */
7636 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
7637 device_get_unit(dev));
7638 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
7639 create_a_copy(atkbdc, dev);
7644 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
7645 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
7646 ISA_PNP_INFO(psmcpnp_ids);
7647 #endif /* DEV_ISA */