kernel/psm: Support newer Synaptics touchpads
[dragonfly.git] / sys / dev / misc / psm / psm.c
blob3aea8af09f12f9f6ee4f64cd27c6024e2271dd7e
1 /*-
2 * Copyright (c) 1992, 1993 Erik Forsberg.
3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
33 * to 386bsd.
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>
42 * - 13 June 1993
44 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
45 * - 24 October 1993
47 * Hardware access routines and probe logic rewritten by
48 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
49 * - 3, 14, 22 October 1996.
50 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
51 * - 14, 30 November 1996. Uses `kbdio.c'.
52 * - 13 December 1996. Uses queuing version of `kbdio.c'.
53 * - January/February 1997. Tweaked probe logic for
54 * HiNote UltraII/Latitude/Armada laptops.
55 * - 30 July 1997. Added APM support.
56 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
57 * Improved sync check logic.
58 * Vendor specific support routines.
60 * $FreeBSD: src/sys/dev/atkbdc/psm.c,v 1.107 2010/09/09 07:52:15 ed Exp $
62 #include "opt_psm.h"
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/module.h>
68 #include <sys/bus.h>
69 #include <sys/conf.h>
70 #include <sys/device.h>
71 #include <sys/event.h>
72 #include <sys/syslog.h>
73 #include <sys/malloc.h>
74 #include <sys/rman.h>
75 #include <sys/sysctl.h>
76 #include <sys/thread2.h>
77 #include <sys/time.h>
78 #include <sys/uio.h>
79 #include <sys/machintr.h>
81 #include <machine/clock.h>
82 #include <machine/limits.h>
83 #include <sys/mouse.h>
85 #include <bus/isa/isavar.h>
86 #include <dev/misc/kbd/atkbdcreg.h>
89 * Driver specific options: the following options may be set by
90 * `options' statements in the kernel configuration file.
93 /* debugging */
94 #ifndef PSM_DEBUG
95 #define PSM_DEBUG 0 /*
96 * logging: 0: none, 1: brief, 2: verbose
97 * 3: sync errors, 4: all packets
99 #endif
100 #define VLOG(level, args) do { \
101 if (verbose >= level) \
102 log args; \
103 } while (0)
105 #ifndef PSM_INPUT_TIMEOUT
106 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
107 #endif
109 #ifndef PSM_TAP_TIMEOUT
110 #define PSM_TAP_TIMEOUT 125000
111 #endif
113 #ifndef PSM_TAP_THRESHOLD
114 #define PSM_TAP_THRESHOLD 25
115 #endif
117 /* end of driver specific options */
119 #define PSM_DRIVER_NAME "psm"
120 #define PSMCPNP_DRIVER_NAME "psmcpnp"
122 /* input queue */
123 #define PSM_BUFSIZE 960
124 #define PSM_SMALLBUFSIZE 240
126 /* operation levels */
127 #define PSM_LEVEL_BASE 0
128 #define PSM_LEVEL_STANDARD 1
129 #define PSM_LEVEL_NATIVE 2
130 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
131 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
133 /* Logitech PS2++ protocol */
134 #define MOUSE_PS2PLUS_CHECKBITS(b) \
135 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
136 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
137 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
139 /* some macros */
140 #define PSM_UNIT(dev) (minor(dev) >> 1)
141 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
142 #define PSM_MKMINOR(unit,block) ((((unit) & 0xff) << 1) | ((block) ? 0:1))
144 /* ring buffer */
145 typedef struct ringbuf {
146 int count; /* # of valid elements in the buffer */
147 int head; /* head pointer */
148 int tail; /* tail poiner */
149 u_char buf[PSM_BUFSIZE];
150 } ringbuf_t;
152 /* data buffer */
153 typedef struct packetbuf {
154 u_char ipacket[16]; /* interim input buffer */
155 int inputbytes; /* # of bytes in the input buffer */
156 } packetbuf_t;
158 #ifndef PSM_PACKETQUEUE
159 #define PSM_PACKETQUEUE 128
160 #endif
162 enum {
163 SYNAPTICS_SYSCTL_MIN_PRESSURE,
164 SYNAPTICS_SYSCTL_MAX_PRESSURE,
165 SYNAPTICS_SYSCTL_MAX_WIDTH,
166 SYNAPTICS_SYSCTL_MARGIN_TOP,
167 SYNAPTICS_SYSCTL_MARGIN_RIGHT,
168 SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
169 SYNAPTICS_SYSCTL_MARGIN_LEFT,
170 SYNAPTICS_SYSCTL_NA_TOP,
171 SYNAPTICS_SYSCTL_NA_RIGHT,
172 SYNAPTICS_SYSCTL_NA_BOTTOM,
173 SYNAPTICS_SYSCTL_NA_LEFT,
174 SYNAPTICS_SYSCTL_WINDOW_MIN,
175 SYNAPTICS_SYSCTL_WINDOW_MAX,
176 SYNAPTICS_SYSCTL_MULTIPLICATOR,
177 SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
178 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
179 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
180 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
181 SYNAPTICS_SYSCTL_DIV_MIN,
182 SYNAPTICS_SYSCTL_DIV_MAX,
183 SYNAPTICS_SYSCTL_DIV_MAX_NA,
184 SYNAPTICS_SYSCTL_DIV_LEN,
185 SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
186 SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
187 SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
188 SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
189 SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
190 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
191 SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
192 SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX
195 typedef struct synapticsinfo {
196 struct sysctl_ctx_list sysctl_ctx;
197 struct sysctl_oid *sysctl_tree;
198 int directional_scrolls;
199 int min_pressure;
200 int max_pressure;
201 int max_width;
202 int margin_top;
203 int margin_right;
204 int margin_bottom;
205 int margin_left;
206 int na_top;
207 int na_right;
208 int na_bottom;
209 int na_left;
210 int window_min;
211 int window_max;
212 int multiplicator;
213 int weight_current;
214 int weight_previous;
215 int weight_previous_na;
216 int weight_len_squared;
217 int div_min;
218 int div_max;
219 int div_max_na;
220 int div_len;
221 int tap_max_delta;
222 int tap_min_queue;
223 int taphold_timeout;
224 int vscroll_ver_area;
225 int vscroll_hor_area;
226 int vscroll_min_delta;
227 int vscroll_div_min;
228 int vscroll_div_max;
229 } synapticsinfo_t;
231 typedef struct synapticspacket {
232 int x;
233 int y;
234 } synapticspacket_t;
236 #define SYNAPTICS_PACKETQUEUE 10
237 #define SYNAPTICS_QUEUE_CURSOR(x) \
238 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
240 #define SYNAPTICS_VERSION_GE(synhw, major, minor) \
241 ((synhw).infoMajor > (major) || \
242 ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
244 typedef struct synapticsaction {
245 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
246 int queue_len;
247 int queue_cursor;
248 int window_min;
249 int start_x;
250 int start_y;
251 int avg_dx;
252 int avg_dy;
253 int squelch_x;
254 int squelch_y;
255 int fingers_nb;
256 int tap_button;
257 int in_taphold;
258 int in_vscroll;
259 } synapticsaction_t;
261 /* driver control block */
262 struct psm_softc { /* Driver status information */
263 int unit;
264 struct kqinfo rkq; /* Processes with registered kevents */
265 u_char state; /* Mouse driver state */
266 int config; /* driver configuration flags */
267 int flags; /* other flags */
268 KBDC kbdc; /* handle to access kbd controller */
269 struct resource *intr; /* IRQ resource */
270 void *ih; /* interrupt handle */
271 mousehw_t hw; /* hardware information */
272 synapticshw_t synhw; /* Synaptics hardware information */
273 synapticsinfo_t syninfo; /* Synaptics configuration */
274 synapticsaction_t synaction; /* Synaptics action context */
275 mousemode_t mode; /* operation mode */
276 mousemode_t dflt_mode; /* default operation mode */
277 mousestatus_t status; /* accumulated mouse movement */
278 ringbuf_t queue; /* mouse status queue */
279 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
280 int pqueue_start; /* start of data in queue */
281 int pqueue_end; /* end of data in queue */
282 int button; /* the latest button state */
283 int xold; /* previous absolute X position */
284 int yold; /* previous absolute Y position */
285 int xaverage; /* average X position */
286 int yaverage; /* average Y position */
287 int squelch; /* level to filter movement at low speed */
288 int zmax; /* maximum pressure value for touchpads */
289 int syncerrors; /* # of bytes discarded to synchronize */
290 int pkterrors; /* # of packets failed during quaranteen. */
291 struct timeval inputtimeout;
292 struct timeval lastsoftintr; /* time of last soft interrupt */
293 struct timeval lastinputerr; /* time last sync error happened */
294 struct timeval taptimeout; /* tap timeout for touchpads */
295 int watchdog; /* watchdog timer flag */
296 struct callout callout; /* watchdog timer call out */
297 struct callout softcallout; /* buffer timer call out */
298 struct cdev *dev;
299 struct cdev *bdev;
300 int lasterr;
301 int cmdcount;
303 static devclass_t psm_devclass;
304 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
306 /* driver state flags (state) */
307 #define PSM_VALID 0x80
308 #define PSM_OPEN 1 /* Device is open */
309 #define PSM_ASLP 2 /* Waiting for mouse data */
310 #define PSM_SOFTARMED 4 /* Software interrupt armed */
311 #define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
313 /* driver configuration flags (config) */
314 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
315 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
316 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
317 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
318 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
319 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
320 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
321 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
322 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
323 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
325 #define PSM_CONFIG_FLAGS \
326 (PSM_CONFIG_RESOLUTION | \
327 PSM_CONFIG_ACCEL | \
328 PSM_CONFIG_NOCHECKSYNC | \
329 PSM_CONFIG_SYNCHACK | \
330 PSM_CONFIG_NOIDPROBE | \
331 PSM_CONFIG_NORESET | \
332 PSM_CONFIG_FORCETAP | \
333 PSM_CONFIG_IGNPORTERROR | \
334 PSM_CONFIG_HOOKRESUME | \
335 PSM_CONFIG_INITAFTERSUSPEND)
337 /* other flags (flags) */
338 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
340 /* Tunables */
341 static int tap_enabled = -1;
342 TUNABLE_INT("hw.psm.tap_enabled", &tap_enabled);
344 static int synaptics_support = 0;
345 TUNABLE_INT("hw.psm.synaptics_support", &synaptics_support);
347 static int verbose = PSM_DEBUG;
348 TUNABLE_INT("debug.psm.loglevel", &verbose);
350 /* for backward compatibility */
351 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
352 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
353 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
355 typedef struct old_mousehw {
356 int buttons;
357 int iftype;
358 int type;
359 int hwid;
360 } old_mousehw_t;
362 typedef struct old_mousemode {
363 int protocol;
364 int rate;
365 int resolution;
366 int accelfactor;
367 } old_mousemode_t;
369 /* packet formatting function */
370 typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
371 mousestatus_t *);
373 /* function prototypes */
374 static void psmidentify(driver_t *, device_t);
375 static int psmprobe(device_t);
376 static int psmattach(device_t);
377 static int psmdetach(device_t);
378 static int psmresume(device_t);
380 static d_open_t psmopen;
381 static d_close_t psmclose;
382 static d_read_t psmread;
383 static d_write_t psmwrite;
384 static d_ioctl_t psmioctl;
385 static d_kqfilter_t psmkqfilter;
387 static int enable_aux_dev(KBDC);
388 static int disable_aux_dev(KBDC);
389 static int get_mouse_status(KBDC, int *, int, int);
390 static int get_aux_id(KBDC);
391 static int set_mouse_sampling_rate(KBDC, int);
392 static int set_mouse_scaling(KBDC, int);
393 static int set_mouse_resolution(KBDC, int);
394 static int set_mouse_mode(KBDC);
395 static int get_mouse_buttons(KBDC);
396 static int is_a_mouse(int);
397 static void recover_from_error(KBDC);
398 static int restore_controller(KBDC, int);
399 static int doinitialize(struct psm_softc *, mousemode_t *);
400 static int doopen(struct psm_softc *, int);
401 static int reinitialize(struct psm_softc *, int);
402 static char *model_name(int);
403 static void psmsoftintr(void *);
404 static void psmintr(void *);
405 static void psmtimeout(void *);
406 static void psmfilter_detach(struct knote *);
407 static int psmfilter(struct knote *, long);
408 static int timeelapsed(const struct timeval *, int, int,
409 const struct timeval *);
410 static void dropqueue(struct psm_softc *);
411 static void flushpackets(struct psm_softc *);
412 static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
413 mousestatus_t *, int *, int *, int *);
414 static int proc_synaptics(struct psm_softc *, packetbuf_t *,
415 mousestatus_t *, int *, int *, int *);
416 static void proc_versapad(struct psm_softc *, packetbuf_t *,
417 mousestatus_t *, int *, int *, int *);
418 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
419 u_char *);
421 /* vendor specific features */
422 typedef int probefunc_t(struct psm_softc *);
424 static int mouse_id_proc1(KBDC, int, int, int *);
425 static int mouse_ext_command(KBDC, int);
427 static probefunc_t enable_groller;
428 static probefunc_t enable_gmouse;
429 static probefunc_t enable_aglide;
430 static probefunc_t enable_kmouse;
431 static probefunc_t enable_msexplorer;
432 static probefunc_t enable_msintelli;
433 static probefunc_t enable_4dmouse;
434 static probefunc_t enable_4dplus;
435 static probefunc_t enable_mmanplus;
436 static probefunc_t enable_synaptics;
437 static probefunc_t enable_versapad;
439 static struct {
440 int model;
441 u_char syncmask;
442 int packetsize;
443 probefunc_t *probefunc;
444 } vendortype[] = {
446 * WARNING: the order of probe is very important. Don't mess it
447 * unless you know what you are doing.
449 { MOUSE_MODEL_NET, /* Genius NetMouse */
450 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
451 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
452 0xc8, 6, enable_groller },
453 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
454 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
455 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
456 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
457 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
458 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
459 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
460 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
461 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
462 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
463 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
464 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
465 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
466 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
467 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
468 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
469 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
470 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
471 { MOUSE_MODEL_GENERIC,
472 0xc0, MOUSE_PS2_PACKETSIZE, NULL },
474 #define GENERIC_MOUSE_ENTRY (NELEM(vendortype) - 1)
476 /* device driver declarateion */
477 static device_method_t psm_methods[] = {
478 /* Device interface */
479 DEVMETHOD(device_identify, psmidentify),
480 DEVMETHOD(device_probe, psmprobe),
481 DEVMETHOD(device_attach, psmattach),
482 DEVMETHOD(device_detach, psmdetach),
483 DEVMETHOD(device_resume, psmresume),
485 DEVMETHOD_END
488 static driver_t psm_driver = {
489 PSM_DRIVER_NAME,
490 psm_methods,
491 sizeof(struct psm_softc),
494 static struct dev_ops psm_ops = {
495 { PSM_DRIVER_NAME, 0, 0 },
496 .d_open = psmopen,
497 .d_close = psmclose,
498 .d_read = psmread,
499 .d_write = psmwrite,
500 .d_ioctl = psmioctl,
501 .d_kqfilter = psmkqfilter
504 /* device I/O routines */
505 static int
506 enable_aux_dev(KBDC kbdc)
508 int res;
510 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
511 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
513 return (res == PSM_ACK);
516 static int
517 disable_aux_dev(KBDC kbdc)
519 int res;
521 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
522 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
524 return (res == PSM_ACK);
527 static int
528 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
530 int cmd;
531 int res;
532 int i;
534 switch (flag) {
535 case 0:
536 default:
537 cmd = PSMC_SEND_DEV_STATUS;
538 break;
539 case 1:
540 cmd = PSMC_SEND_DEV_DATA;
541 break;
543 empty_aux_buffer(kbdc, 5);
544 res = send_aux_command(kbdc, cmd);
545 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
546 (flag == 1) ? "DATA" : "STATUS", res));
547 if (res != PSM_ACK)
548 return (0);
550 for (i = 0; i < len; ++i) {
551 status[i] = read_aux_data(kbdc);
552 if (status[i] < 0)
553 break;
556 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
557 (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
559 return (i);
562 static int
563 get_aux_id(KBDC kbdc)
565 int res;
566 int id;
568 empty_aux_buffer(kbdc, 5);
569 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
570 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
571 if (res != PSM_ACK)
572 return (-1);
574 /* 10ms delay */
575 DRIVERSLEEP(10000);
577 id = read_aux_data(kbdc);
578 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
580 return (id);
583 static int
584 set_mouse_sampling_rate(KBDC kbdc, int rate)
586 int res;
588 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
589 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
591 return ((res == PSM_ACK) ? rate : -1);
594 static int
595 set_mouse_scaling(KBDC kbdc, int scale)
597 int res;
599 switch (scale) {
600 case 1:
601 default:
602 scale = PSMC_SET_SCALING11;
603 break;
604 case 2:
605 scale = PSMC_SET_SCALING21;
606 break;
608 res = send_aux_command(kbdc, scale);
609 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
610 (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
612 return (res == PSM_ACK);
615 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
616 static int
617 set_mouse_resolution(KBDC kbdc, int val)
619 int res;
621 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
622 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
624 return ((res == PSM_ACK) ? val : -1);
628 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
629 * re-enabled by calling `enable_aux_dev()'
631 static int
632 set_mouse_mode(KBDC kbdc)
634 int res;
636 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
637 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
639 return (res == PSM_ACK);
642 static int
643 get_mouse_buttons(KBDC kbdc)
645 int c = 2; /* assume two buttons by default */
646 int status[3];
649 * NOTE: a special sequence to obtain Logitech Mouse specific
650 * information: set resolution to 25 ppi, set scaling to 1:1, set
651 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
652 * mouse status bytes is the number of available buttons.
653 * Some manufactures also support this sequence.
655 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
656 return (c);
657 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
658 set_mouse_scaling(kbdc, 1) &&
659 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
660 return (status[1]);
661 return (c);
664 /* misc subroutines */
666 * Someday, I will get the complete list of valid pointing devices and
667 * their IDs... XXX
669 static int
670 is_a_mouse(int id)
672 #if 0
673 static int valid_ids[] = {
674 PSM_MOUSE_ID, /* mouse */
675 PSM_BALLPOINT_ID, /* ballpoint device */
676 PSM_INTELLI_ID, /* Intellimouse */
677 PSM_EXPLORER_ID, /* Intellimouse Explorer */
678 -1 /* end of table */
680 int i;
682 for (i = 0; valid_ids[i] >= 0; ++i)
683 if (valid_ids[i] == id)
684 return (TRUE);
685 return (FALSE);
686 #else
687 return (TRUE);
688 #endif
691 static char *
692 model_name(int model)
694 static struct {
695 int model_code;
696 char *model_name;
697 } models[] = {
698 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
699 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
700 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
701 { MOUSE_MODEL_THINK, "ThinkingMouse" },
702 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
703 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
704 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
705 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
706 { MOUSE_MODEL_4D, "4D Mouse" },
707 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
708 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
709 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
710 { MOUSE_MODEL_UNKNOWN, "Unknown" },
712 int i;
714 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
715 if (models[i].model_code == model)
716 break;
717 return (models[i].model_name);
720 static void
721 recover_from_error(KBDC kbdc)
723 /* discard anything left in the output buffer */
724 empty_both_buffers(kbdc, 10);
726 #if 0
728 * NOTE: KBDC_RESET_KBD may not restore the communication between the
729 * keyboard and the controller.
731 reset_kbd(kbdc);
732 #else
734 * NOTE: somehow diagnostic and keyboard port test commands bring the
735 * keyboard back.
737 if (!test_controller(kbdc))
738 log(LOG_ERR, "psm: keyboard controller failed.\n");
739 /* if there isn't a keyboard in the system, the following error is OK */
740 if (test_kbd_port(kbdc) != 0)
741 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
742 #endif
745 static int
746 restore_controller(KBDC kbdc, int command_byte)
748 empty_both_buffers(kbdc, 10);
750 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
751 log(LOG_ERR, "psm: failed to restore the keyboard controller "
752 "command byte.\n");
753 empty_both_buffers(kbdc, 10);
754 return (FALSE);
755 } else {
756 empty_both_buffers(kbdc, 10);
757 return (TRUE);
762 * Re-initialize the aux port and device. The aux port must be enabled
763 * and its interrupt must be disabled before calling this routine.
764 * The aux device will be disabled before returning.
765 * The keyboard controller must be locked via `kbdc_lock()' before
766 * calling this routine.
768 static int
769 doinitialize(struct psm_softc *sc, mousemode_t *mode)
771 KBDC kbdc = sc->kbdc;
772 int stat[3];
773 int i;
775 switch((i = test_aux_port(kbdc))) {
776 case 1: /* ignore these errors */
777 case 2:
778 case 3:
779 case PSM_ACK:
780 if (verbose)
781 log(LOG_DEBUG,
782 "psm%d: strange result for test aux port (%d).\n",
783 sc->unit, i);
784 /* FALLTHROUGH */
785 case 0: /* no error */
786 break;
787 case -1: /* time out */
788 default: /* error */
789 recover_from_error(kbdc);
790 if (sc->config & PSM_CONFIG_IGNPORTERROR)
791 break;
792 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
793 sc->unit, i);
794 return (FALSE);
797 if (sc->config & PSM_CONFIG_NORESET) {
799 * Don't try to reset the pointing device. It may possibly
800 * be left in the unknown state, though...
802 } else {
804 * NOTE: some controllers appears to hang the `keyboard' when
805 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
807 if (!reset_aux_dev(kbdc)) {
808 recover_from_error(kbdc);
809 log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
810 sc->unit);
811 return (FALSE);
816 * both the aux port and the aux device is functioning, see
817 * if the device can be enabled.
819 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
820 log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
821 sc->unit);
822 return (FALSE);
824 empty_both_buffers(kbdc, 10); /* remove stray data if any */
826 if (sc->config & PSM_CONFIG_NOIDPROBE)
827 i = GENERIC_MOUSE_ENTRY;
828 else {
829 /* FIXME: hardware ID, mouse buttons? */
831 /* other parameters */
832 for (i = 0; vendortype[i].probefunc != NULL; ++i)
833 if ((*vendortype[i].probefunc)(sc)) {
834 if (verbose >= 2)
835 log(LOG_ERR, "psm%d: found %s\n",
836 sc->unit,
837 model_name(vendortype[i].model));
838 break;
842 sc->hw.model = vendortype[i].model;
843 sc->mode.packetsize = vendortype[i].packetsize;
845 /* set mouse parameters */
846 if (mode != NULL) {
847 if (mode->rate > 0)
848 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
849 if (mode->resolution >= 0)
850 mode->resolution =
851 set_mouse_resolution(kbdc, mode->resolution);
852 set_mouse_scaling(kbdc, 1);
853 set_mouse_mode(kbdc);
856 /* Record sync on the next data packet we see. */
857 sc->flags |= PSM_NEED_SYNCBITS;
859 /* just check the status of the mouse */
860 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
861 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
862 sc->unit);
864 return (TRUE);
867 static int
868 doopen(struct psm_softc *sc, int command_byte)
870 int stat[3];
873 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
874 * no obvious reason. Thus we check the current mode and restore the
875 * Absolute Mode if it was cleared.
877 * The previous hack at the end of psmprobe() wasn't efficient when
878 * moused(8) was restarted.
880 * A Reset (FF) or Set Defaults (F6) command would clear the
881 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
882 * doesn't show any evidence of such a command.
884 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
885 mouse_ext_command(sc->kbdc, 1);
886 get_mouse_status(sc->kbdc, stat, 0, 3);
887 if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) || stat[1] == 0x47)
888 && stat[2] == 0x40) {
889 /* Set the mode byte -- request wmode where
890 * available */
891 if (sc->synhw.capExtended)
892 mouse_ext_command(sc->kbdc, 0xc1);
893 else
894 mouse_ext_command(sc->kbdc, 0xc0);
895 set_mouse_sampling_rate(sc->kbdc, 20);
896 VLOG(5, (LOG_DEBUG, "psm%d: Synaptics Absolute Mode "
897 "hopefully restored\n",
898 sc->unit));
903 * A user may want to disable tap and drag gestures on a Synaptics
904 * TouchPad when it operates in Relative Mode.
906 if (sc->hw.model == MOUSE_MODEL_GENERIC) {
907 if (tap_enabled > 0) {
909 * Enable tap & drag gestures. We use a Mode Byte
910 * and clear the DisGest bit (see §2.5 of Synaptics
911 * TouchPad Interfacing Guide).
913 VLOG(2, (LOG_DEBUG,
914 "psm%d: enable tap and drag gestures\n",
915 sc->unit));
916 mouse_ext_command(sc->kbdc, 0x00);
917 set_mouse_sampling_rate(sc->kbdc, 20);
918 } else if (tap_enabled == 0) {
920 * Disable tap & drag gestures. We use a Mode Byte
921 * and set the DisGest bit (see §2.5 of Synaptics
922 * TouchPad Interfacing Guide).
924 VLOG(2, (LOG_DEBUG,
925 "psm%d: disable tap and drag gestures\n",
926 sc->unit));
927 mouse_ext_command(sc->kbdc, 0x04);
928 set_mouse_sampling_rate(sc->kbdc, 20);
932 /* enable the mouse device */
933 if (!enable_aux_dev(sc->kbdc)) {
934 /* MOUSE ERROR: failed to enable the mouse because:
935 * 1) the mouse is faulty,
936 * 2) the mouse has been removed(!?)
937 * In the latter case, the keyboard may have hung, and need
938 * recovery procedure...
940 recover_from_error(sc->kbdc);
941 #if 0
942 /* FIXME: we could reset the mouse here and try to enable
943 * it again. But it will take long time and it's not a good
944 * idea to disable the keyboard that long...
946 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
947 recover_from_error(sc->kbdc);
948 #else
950 #endif
951 restore_controller(sc->kbdc, command_byte);
952 /* mark this device is no longer available */
953 sc->state &= ~PSM_VALID;
954 log(LOG_ERR,
955 "psm%d: failed to enable the device (doopen).\n",
956 sc->unit);
957 return (EIO);
961 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
962 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
963 sc->unit);
965 /* enable the aux port and interrupt */
966 if (!set_controller_command_byte(sc->kbdc,
967 KBD_AUX_CONTROL_BITS,
968 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
969 /* CONTROLLER ERROR */
970 disable_aux_dev(sc->kbdc);
971 restore_controller(sc->kbdc, command_byte);
972 log(LOG_ERR,
973 "psm%d: failed to enable the aux interrupt (doopen).\n",
974 sc->unit);
975 return (EIO);
978 /* start the watchdog timer */
979 sc->watchdog = FALSE;
980 callout_reset(&sc->callout, hz * 2, psmtimeout, (void *)(uintptr_t)sc);
982 return (0);
985 static int
986 reinitialize(struct psm_softc *sc, int doinit)
988 int err;
989 int c;
991 /* don't let anybody mess with the aux device */
992 if (!kbdc_lock(sc->kbdc, TRUE))
993 return (EIO);
995 crit_enter();
997 /* block our watchdog timer */
998 sc->watchdog = FALSE;
999 callout_stop(&sc->callout);
1001 /* save the current controller command byte */
1002 empty_both_buffers(sc->kbdc, 10);
1003 c = get_controller_command_byte(sc->kbdc);
1004 VLOG(2, (LOG_DEBUG,
1005 "psm%d: current command byte: %04x (reinitialize).\n",
1006 sc->unit, c));
1008 /* enable the aux port but disable the aux interrupt and the keyboard */
1009 if ((c == -1) ||
1010 !set_controller_command_byte(sc->kbdc,
1011 KBD_AUX_CONTROL_BITS,
1012 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1013 /* CONTROLLER ERROR */
1014 crit_exit();
1015 kbdc_lock(sc->kbdc, FALSE);
1016 log(LOG_ERR,
1017 "psm%d: unable to set the command byte (reinitialize).\n",
1018 sc->unit);
1019 return (EIO);
1022 /* flush any data */
1023 if (sc->state & PSM_VALID) {
1024 /* this may fail; but never mind... */
1025 disable_aux_dev(sc->kbdc);
1026 empty_aux_buffer(sc->kbdc, 10);
1028 flushpackets(sc);
1029 sc->syncerrors = 0;
1030 sc->pkterrors = 0;
1031 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1033 /* try to detect the aux device; are you still there? */
1034 err = 0;
1035 if (doinit) {
1036 if (doinitialize(sc, &sc->mode)) {
1037 /* yes */
1038 sc->state |= PSM_VALID;
1039 } else {
1040 /* the device has gone! */
1041 restore_controller(sc->kbdc, c);
1042 sc->state &= ~PSM_VALID;
1043 log(LOG_ERR,
1044 "psm%d: the aux device has gone! (reinitialize).\n",
1045 sc->unit);
1046 err = ENXIO;
1049 crit_exit();
1051 /* restore the driver state */
1052 if ((sc->state & PSM_OPEN) && (err == 0)) {
1053 /* enable the aux device and the port again */
1054 err = doopen(sc, c);
1055 if (err != 0)
1056 log(LOG_ERR, "psm%d: failed to enable the device "
1057 "(reinitialize).\n", sc->unit);
1058 } else {
1059 /* restore the keyboard port and disable the aux port */
1060 if (!set_controller_command_byte(sc->kbdc,
1061 KBD_AUX_CONTROL_BITS,
1062 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1063 /* CONTROLLER ERROR */
1064 log(LOG_ERR, "psm%d: failed to disable the aux port "
1065 "(reinitialize).\n", sc->unit);
1066 err = EIO;
1070 kbdc_lock(sc->kbdc, FALSE);
1071 return (err);
1074 /* psm driver entry points */
1076 static void
1077 psmidentify(driver_t *driver, device_t parent)
1079 device_t psmc;
1080 device_t psm;
1081 u_long irq;
1082 int unit;
1084 unit = device_get_unit(parent);
1086 /* always add at least one child */
1087 psm = BUS_ADD_CHILD(parent, parent, KBDC_RID_AUX, driver->name, unit);
1088 if (psm == NULL)
1089 return;
1091 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1092 if (irq > 0)
1093 return;
1096 * If the PS/2 mouse device has already been reported by ACPI or
1097 * PnP BIOS, obtain the IRQ resource from it.
1098 * (See psmcpnp_attach() below.)
1100 psmc = device_find_child(device_get_parent(parent),
1101 PSMCPNP_DRIVER_NAME, unit);
1102 if (psmc == NULL)
1103 return;
1104 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1105 if (irq <= 0)
1106 return;
1107 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1,
1108 machintr_legacy_intr_cpuid(irq));
1111 #define endprobe(v) do { \
1112 if (bootverbose) \
1113 --verbose; \
1114 set_controller_command_byte(sc->kbdc, \
1115 KBD_AUX_CONTROL_BITS, KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT); \
1116 kbdc_lock(sc->kbdc, FALSE); \
1117 return (v); \
1118 } while (0)
1120 static int
1121 psmprobe(device_t dev)
1123 int unit = device_get_unit(dev);
1124 struct psm_softc *sc = device_get_softc(dev);
1125 uintptr_t irq;
1126 uintptr_t flags;
1127 int stat[3];
1128 int command_byte;
1129 int i;
1131 #if 0
1132 kbdc_debug(TRUE);
1133 #endif
1135 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1136 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
1138 sc->unit = unit;
1139 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1140 sc->config = flags & PSM_CONFIG_FLAGS;
1141 /* XXX: for backward compatibility */
1142 #if defined(PSM_HOOKRESUME)
1143 sc->config |=
1144 #ifdef PSM_RESETAFTERSUSPEND
1145 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
1146 #else
1147 PSM_CONFIG_HOOKRESUME;
1148 #endif
1149 #endif /* PSM_HOOKRESUME */
1150 sc->flags = 0;
1151 if (bootverbose)
1152 ++verbose;
1154 device_set_desc(dev, "PS/2 Mouse");
1156 if (!kbdc_lock(sc->kbdc, TRUE)) {
1157 kprintf("psm%d: unable to lock the controller.\n", unit);
1158 if (bootverbose)
1159 --verbose;
1160 return (ENXIO);
1164 * NOTE: two bits in the command byte controls the operation of the
1165 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1166 * port interrupt (IRQ 12) enable bit (bit 2).
1169 /* discard anything left after the keyboard initialization */
1170 empty_both_buffers(sc->kbdc, 10);
1172 /* save the current command byte; it will be used later */
1173 command_byte = get_controller_command_byte(sc->kbdc);
1174 if (verbose)
1175 kprintf("psm%d: current command byte:%04x\n", unit,
1176 command_byte);
1177 if (command_byte == -1) {
1178 /* CONTROLLER ERROR */
1179 kprintf("psm%d: unable to get the current "
1180 "command byte value.\n",
1181 unit);
1182 endprobe(ENXIO);
1186 * NOTE: We cannot mess with the keyboard port, do NOT disable it
1187 * while we are probing the aux port during this routine.
1188 * Disabling the keyboard port will break some things
1189 * (Acer c720)... probably related to BIOS emulation of the
1190 * i8042.
1192 if (!set_controller_command_byte(sc->kbdc,
1193 KBD_AUX_CONTROL_BITS, KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1195 * this is CONTROLLER ERROR; I don't know how to recover
1196 * from this error...
1198 kprintf("psm%d: unable to set the command byte.\n", unit);
1199 endprobe(ENXIO);
1203 * NOTE: Linux doesn't send discrete aux port enablement commands,
1204 * it is unclear whether this is needed or helps or hinders
1205 * bios emulators.
1207 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1210 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1211 * port exists and is functioning. However, some controllers appears
1212 * to respond with zero even when the aux port doesn't exist. (It may
1213 * be that this is only the case when the controller DOES have the aux
1214 * port but the port is not wired on the motherboard.) The keyboard
1215 * controllers without the port, such as the original AT, are
1216 * supporsed to return with an error code or simply time out. In any
1217 * case, we have to continue probing the port even when the controller
1218 * passes this test.
1220 * XXX: some controllers erroneously return the error code 1, 2 or 3
1221 * when it has the perfectly functional aux port. We have to ignore
1222 * this error code. Even if the controller HAS error with the aux
1223 * port, it will be detected later...
1224 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1226 switch ((i = test_aux_port(sc->kbdc))) {
1227 case 1: /* ignore these errors */
1228 case 2:
1229 case 3:
1230 case PSM_ACK:
1231 if (verbose)
1232 kprintf("psm%d: strange result for test aux port "
1233 "(%d).\n", unit, i);
1234 /* FALLTHROUGH */
1235 case 0: /* no error */
1236 break;
1237 case -1: /* time out */
1238 default: /* error */
1239 recover_from_error(sc->kbdc);
1240 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1241 break;
1242 if (verbose)
1243 kprintf("psm%d: the aux port is not "
1244 "functioning (%d).\n",
1245 unit, i);
1246 endprobe(ENXIO);
1249 if (sc->config & PSM_CONFIG_NORESET) {
1251 * Don't try to reset the pointing device. It may possibly be
1252 * left in the unknown state, though...
1254 } else {
1256 * NOTE: some controllers appears to hang the `keyboard' when
1257 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1259 * Attempt to reset the controller twice -- this helps
1260 * pierce through some KVM switches. The second reset
1261 * is non-fatal.
1263 if (!reset_aux_dev(sc->kbdc)) {
1264 recover_from_error(sc->kbdc);
1265 if (verbose)
1266 kprintf("psm%d: failed to reset the aux "
1267 "device.\n", unit);
1268 endprobe(ENXIO);
1269 } else if (!reset_aux_dev(sc->kbdc)) {
1270 recover_from_error(sc->kbdc);
1271 if (verbose >= 2)
1272 kprintf("psm%d: failed to reset the aux device "
1273 "(2).\n", unit);
1278 * both the aux port and the aux device is functioning, see if the
1279 * device can be enabled. NOTE: when enabled, the device will start
1280 * sending data; we shall immediately disable the device once we know
1281 * the device can be enabled.
1283 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1284 /* MOUSE ERROR */
1285 recover_from_error(sc->kbdc);
1286 if (verbose)
1287 kprintf("psm%d: failed to enable the aux device.\n",
1288 unit);
1289 endprobe(ENXIO);
1292 /* save the default values after reset */
1293 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1294 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1295 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1296 } else {
1297 sc->dflt_mode.rate = sc->mode.rate = -1;
1298 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1301 /* hardware information */
1302 sc->hw.iftype = MOUSE_IF_PS2;
1304 /* verify the device is a mouse */
1305 sc->hw.hwid = get_aux_id(sc->kbdc);
1306 if (!is_a_mouse(sc->hw.hwid)) {
1307 if (verbose)
1308 kprintf("psm%d: unknown device type (%d).\n", unit,
1309 sc->hw.hwid);
1310 endprobe(ENXIO);
1312 switch (sc->hw.hwid) {
1313 case PSM_BALLPOINT_ID:
1314 sc->hw.type = MOUSE_TRACKBALL;
1315 break;
1316 case PSM_MOUSE_ID:
1317 case PSM_INTELLI_ID:
1318 case PSM_EXPLORER_ID:
1319 case PSM_4DMOUSE_ID:
1320 case PSM_4DPLUS_ID:
1321 sc->hw.type = MOUSE_MOUSE;
1322 break;
1323 default:
1324 sc->hw.type = MOUSE_UNKNOWN;
1325 break;
1328 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1329 sc->hw.buttons = 2;
1330 i = GENERIC_MOUSE_ENTRY;
1331 } else {
1332 /* # of buttons */
1333 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1335 /* other parameters */
1336 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1337 if ((*vendortype[i].probefunc)(sc)) {
1338 if (verbose >= 2)
1339 kprintf("psm%d: found %s\n", unit,
1340 model_name(vendortype[i].model));
1341 break;
1345 sc->hw.model = vendortype[i].model;
1347 sc->dflt_mode.level = PSM_LEVEL_BASE;
1348 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1349 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1350 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1351 sc->dflt_mode.syncmask[0] = 0;
1352 else
1353 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1354 if (sc->config & PSM_CONFIG_FORCETAP)
1355 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1356 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1357 sc->mode = sc->dflt_mode;
1358 sc->mode.packetsize = vendortype[i].packetsize;
1360 /* set mouse parameters */
1361 #if 0
1363 * A version of Logitech FirstMouse+ won't report wheel movement,
1364 * if SET_DEFAULTS is sent... Don't use this command.
1365 * This fix was found by Takashi Nishida.
1367 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1368 if (verbose >= 2)
1369 kprintf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1370 #endif
1371 if (sc->config & PSM_CONFIG_RESOLUTION)
1372 sc->mode.resolution =
1373 set_mouse_resolution(sc->kbdc,
1374 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1375 else if (sc->mode.resolution >= 0)
1376 sc->mode.resolution =
1377 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1378 if (sc->mode.rate > 0)
1379 sc->mode.rate =
1380 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1381 set_mouse_scaling(sc->kbdc, 1);
1383 /* Record sync on the next data packet we see. */
1384 sc->flags |= PSM_NEED_SYNCBITS;
1386 /* just check the status of the mouse */
1388 * NOTE: XXX there are some arcane controller/mouse combinations out
1389 * there, which hung the controller unless there is data transmission
1390 * after ACK from the mouse.
1392 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1393 kprintf("psm%d: failed to get status.\n", unit);
1394 else {
1396 * When in its native mode, some mice operate with different
1397 * default parameters than in the PS/2 compatible mode.
1399 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1400 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1403 /* disable the aux port for now... */
1404 if (!set_controller_command_byte(sc->kbdc,
1405 KBD_AUX_CONTROL_BITS, KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1407 * this is CONTROLLER ERROR; I don't know the proper way to
1408 * recover from this error...
1410 kprintf("psm%d: unable to set the command byte.\n", unit);
1411 endprobe(ENXIO);
1414 /* done */
1415 kbdc_lock(sc->kbdc, FALSE);
1416 return (0);
1419 static int
1420 psmattach(device_t dev)
1422 int unit = device_get_unit(dev);
1423 struct psm_softc *sc = device_get_softc(dev);
1424 int error;
1425 intptr_t irq;
1426 int rid;
1428 /* Setup initial state */
1429 sc->state = PSM_VALID;
1430 callout_init(&sc->callout);
1431 callout_init(&sc->softcallout);
1433 /* Setup our interrupt handler */
1434 rid = KBDC_RID_AUX;
1435 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1436 sc->intr = bus_alloc_legacy_irq_resource(dev, &rid, irq, RF_ACTIVE);
1438 if (sc->intr == NULL)
1439 return (ENXIO);
1440 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
1441 INTR_NOPOLL, psmintr, sc, &sc->ih, NULL, NULL);
1442 if (error) {
1443 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1444 return (error);
1447 /* Done */
1448 make_dev(&psm_ops, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, "psm%d", unit);
1449 make_dev(&psm_ops, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, "bpsm%d", unit);
1451 if (!verbose)
1452 kprintf("psm%d: model %s, device ID %d\n",
1453 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1454 else {
1455 kprintf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1456 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1457 sc->hw.hwid >> 8, sc->hw.buttons);
1458 kprintf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1459 unit, sc->config, sc->flags, sc->mode.packetsize);
1460 kprintf("psm%d: syncmask:%02x, syncbits:%02x\n",
1461 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1464 if (bootverbose)
1465 --verbose;
1467 return (0);
1470 static int
1471 psmdetach(device_t dev)
1473 struct psm_softc *sc;
1474 int rid;
1476 sc = device_get_softc(dev);
1477 if (sc->state & PSM_OPEN)
1478 return (EBUSY);
1480 rid = KBDC_RID_AUX;
1481 bus_teardown_intr(dev, sc->intr, sc->ih);
1482 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1484 destroy_dev(sc->dev);
1485 destroy_dev(sc->bdev);
1487 return (0);
1490 static int
1491 psmopen(struct dev_open_args *ap)
1493 cdev_t dev = ap->a_head.a_dev;
1494 int unit = PSM_UNIT(dev);
1495 struct psm_softc *sc;
1496 int command_byte;
1497 int err;
1499 /* Get device data */
1500 sc = PSM_SOFTC(unit);
1501 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
1502 /* the device is no longer valid/functioning */
1503 return (ENXIO);
1506 /* Disallow multiple opens */
1507 if (sc->state & PSM_OPEN)
1508 return (EBUSY);
1510 #if 0
1511 device_busy(devclass_get_device(psm_devclass, sc->unit));
1512 #endif
1514 /* Initialize state */
1515 sc->mode.level = sc->dflt_mode.level;
1516 sc->mode.protocol = sc->dflt_mode.protocol;
1517 sc->watchdog = FALSE;
1519 /* flush the event queue */
1520 sc->queue.count = 0;
1521 sc->queue.head = 0;
1522 sc->queue.tail = 0;
1523 sc->status.flags = 0;
1524 sc->status.button = 0;
1525 sc->status.obutton = 0;
1526 sc->status.dx = 0;
1527 sc->status.dy = 0;
1528 sc->status.dz = 0;
1529 sc->button = 0;
1530 sc->pqueue_start = 0;
1531 sc->pqueue_end = 0;
1533 /* empty input buffer */
1534 flushpackets(sc);
1535 sc->syncerrors = 0;
1536 sc->pkterrors = 0;
1538 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1539 if (!kbdc_lock(sc->kbdc, TRUE))
1540 return (EIO);
1542 /* save the current controller command byte */
1543 crit_enter();
1544 command_byte = get_controller_command_byte(sc->kbdc);
1546 /* enable the aux port and temporalily disable the keyboard */
1547 if (command_byte == -1 ||
1548 !set_controller_command_byte(sc->kbdc,
1549 KBD_AUX_CONTROL_BITS,
1550 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1551 /* CONTROLLER ERROR; do you know how to get out of this? */
1552 kbdc_lock(sc->kbdc, FALSE);
1553 crit_exit();
1554 log(LOG_ERR,
1555 "psm%d: unable to set the command byte (psmopen).\n",
1556 sc->unit);
1557 return (EIO);
1560 * Now that the keyboard controller is told not to generate
1561 * the keyboard and mouse interrupts, call `splx()' to allow
1562 * the other tty interrupts. The clock interrupt may also occur,
1563 * but timeout routines will be blocked by the poll flag set
1564 * via `kbdc_lock()'
1566 crit_exit();
1568 /* enable the mouse device */
1569 err = doopen(sc, command_byte);
1571 /* done */
1572 if (err == 0)
1573 sc->state |= PSM_OPEN;
1574 kbdc_lock(sc->kbdc, FALSE);
1575 return (err);
1578 static int
1579 psmclose(struct dev_close_args *ap)
1581 cdev_t dev = ap->a_head.a_dev;
1582 int unit = PSM_UNIT(dev);
1583 struct psm_softc *sc = PSM_SOFTC(unit);
1584 int stat[3];
1585 int command_byte;
1587 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1588 if (!kbdc_lock(sc->kbdc, TRUE))
1589 return (EIO);
1591 /* save the current controller command byte */
1592 crit_enter();
1593 command_byte = get_controller_command_byte(sc->kbdc);
1594 if (command_byte == -1) {
1595 kbdc_lock(sc->kbdc, FALSE);
1596 crit_exit();
1597 return (EIO);
1600 /* disable the aux interrupt and temporalily disable the keyboard */
1601 if (!set_controller_command_byte(sc->kbdc,
1602 KBD_AUX_CONTROL_BITS,
1603 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1604 log(LOG_ERR,
1605 "psm%d: failed to disable the aux int (psmclose).\n",
1606 sc->unit);
1607 /* CONTROLLER ERROR;
1608 * NOTE: we shall force our way through. Because the only
1609 * ill effect we shall see is that we may not be able
1610 * to read ACK from the mouse, and it doesn't matter much
1611 * so long as the mouse will accept the DISABLE command.
1614 crit_exit();
1616 /* stop the watchdog timer */
1617 callout_stop(&sc->callout);
1619 /* remove anything left in the output buffer */
1620 empty_aux_buffer(sc->kbdc, 10);
1622 /* disable the aux device, port and interrupt */
1623 if (sc->state & PSM_VALID) {
1624 if (!disable_aux_dev(sc->kbdc)) {
1625 /* MOUSE ERROR;
1626 * NOTE: we don't return (error) and continue,
1627 * pretending we have successfully disabled the device.
1628 * It's OK because the interrupt routine will discard
1629 * any data from the mouse hereafter.
1631 log(LOG_ERR,
1632 "psm%d: failed to disable the device (psmclose).\n",
1633 sc->unit);
1636 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1637 log(LOG_DEBUG,
1638 "psm%d: failed to get status (psmclose).\n",
1639 sc->unit);
1642 if (!set_controller_command_byte(sc->kbdc,
1643 KBD_AUX_CONTROL_BITS, KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1645 * CONTROLLER ERROR;
1646 * we shall ignore this error; see the above comment.
1648 log(LOG_ERR,
1649 "psm%d: failed to disable the aux port (psmclose).\n",
1650 sc->unit);
1653 /* remove anything left in the output buffer */
1654 empty_aux_buffer(sc->kbdc, 10);
1656 /* close is almost always successful */
1657 sc->state &= ~PSM_OPEN;
1658 kbdc_lock(sc->kbdc, FALSE);
1659 #if 0
1660 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
1661 #endif
1662 return (0);
1665 static int
1666 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
1667 u_char *buf)
1669 static u_char butmapps2[8] = {
1671 MOUSE_PS2_BUTTON1DOWN,
1672 MOUSE_PS2_BUTTON2DOWN,
1673 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1674 MOUSE_PS2_BUTTON3DOWN,
1675 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1676 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1677 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
1678 MOUSE_PS2_BUTTON3DOWN,
1680 static u_char butmapmsc[8] = {
1681 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
1682 MOUSE_MSC_BUTTON3UP,
1683 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1684 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1685 MOUSE_MSC_BUTTON3UP,
1686 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1687 MOUSE_MSC_BUTTON2UP,
1688 MOUSE_MSC_BUTTON1UP,
1691 int mapped;
1692 int i;
1694 if (sc->mode.level == PSM_LEVEL_BASE) {
1695 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1696 if (status->button & MOUSE_BUTTON4DOWN)
1697 mapped |= MOUSE_BUTTON1DOWN;
1698 status->button = mapped;
1699 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1700 i = imax(imin(status->dx, 255), -256);
1701 if (i < 0)
1702 buf[0] |= MOUSE_PS2_XNEG;
1703 buf[1] = i;
1704 i = imax(imin(status->dy, 255), -256);
1705 if (i < 0)
1706 buf[0] |= MOUSE_PS2_YNEG;
1707 buf[2] = i;
1708 return (MOUSE_PS2_PACKETSIZE);
1709 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1710 buf[0] = MOUSE_MSC_SYNC |
1711 butmapmsc[status->button & MOUSE_STDBUTTONS];
1712 i = imax(imin(status->dx, 255), -256);
1713 buf[1] = i >> 1;
1714 buf[3] = i - buf[1];
1715 i = imax(imin(status->dy, 255), -256);
1716 buf[2] = i >> 1;
1717 buf[4] = i - buf[2];
1718 i = imax(imin(status->dz, 127), -128);
1719 buf[5] = (i >> 1) & 0x7f;
1720 buf[6] = (i - (i >> 1)) & 0x7f;
1721 buf[7] = (~status->button >> 3) & 0x7f;
1722 return (MOUSE_SYS_PACKETSIZE);
1724 return (pb->inputbytes);
1727 static int
1728 psmread(struct dev_read_args *ap)
1730 cdev_t dev = ap->a_head.a_dev;
1731 struct uio *uio = ap->a_uio;
1732 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1733 u_char buf[PSM_SMALLBUFSIZE];
1734 int error = 0;
1735 int l;
1737 if ((sc->state & PSM_VALID) == 0)
1738 return (EIO);
1740 /* block until mouse activity occured */
1741 crit_enter();
1742 while (sc->queue.count <= 0) {
1743 if (dev != sc->bdev) {
1744 crit_exit();
1745 return (EWOULDBLOCK);
1747 sc->state |= PSM_ASLP;
1748 error = tsleep((caddr_t) sc, PCATCH, "psmrea", 0);
1749 sc->state &= ~PSM_ASLP;
1750 if (error) {
1751 crit_exit();
1752 return (error);
1753 } else if ((sc->state & PSM_VALID) == 0) {
1754 /* the device disappeared! */
1755 crit_exit();
1756 return (EIO);
1759 crit_exit();
1761 /* copy data to the user land */
1762 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1763 crit_enter();
1764 l = imin(sc->queue.count, uio->uio_resid);
1765 if (l > sizeof(buf))
1766 l = sizeof(buf);
1767 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1768 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1769 sizeof(sc->queue.buf) - sc->queue.head);
1770 bcopy(&sc->queue.buf[0],
1771 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1772 l - (sizeof(sc->queue.buf) - sc->queue.head));
1773 } else
1774 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1775 sc->queue.count -= l;
1776 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1777 crit_exit();
1778 error = uiomove(buf, (size_t)l, uio);
1779 if (error)
1780 break;
1783 return (error);
1786 static int
1787 block_mouse_data(struct psm_softc *sc, int *c)
1790 if (!kbdc_lock(sc->kbdc, TRUE))
1791 return (EIO);
1793 crit_enter();
1794 *c = get_controller_command_byte(sc->kbdc);
1795 if ((*c == -1) ||
1796 !set_controller_command_byte(sc->kbdc,
1797 KBD_AUX_CONTROL_BITS,
1798 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1799 /* this is CONTROLLER ERROR */
1800 crit_exit();
1801 kbdc_lock(sc->kbdc, FALSE);
1802 return (EIO);
1806 * The device may be in the middle of status data transmission.
1807 * The transmission will be interrupted, thus, incomplete status
1808 * data must be discarded. Although the aux interrupt is disabled
1809 * at the keyboard controller level, at most one aux interrupt
1810 * may have already been pending and a data byte is in the
1811 * output buffer; throw it away. Note that the second argument
1812 * to `empty_aux_buffer()' is zero, so that the call will just
1813 * flush the internal queue.
1814 * `psmintr()' will be invoked after `splx()' if an interrupt is
1815 * pending; it will see no data and returns immediately.
1817 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1818 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1819 flushpackets(sc);
1820 crit_exit();
1822 return (0);
1825 static void
1826 dropqueue(struct psm_softc *sc)
1829 sc->queue.count = 0;
1830 sc->queue.head = 0;
1831 sc->queue.tail = 0;
1832 if ((sc->state & PSM_SOFTARMED) != 0) {
1833 sc->state &= ~PSM_SOFTARMED;
1834 callout_stop(&sc->softcallout);
1836 sc->pqueue_start = sc->pqueue_end;
1839 static void
1840 flushpackets(struct psm_softc *sc)
1843 dropqueue(sc);
1844 bzero(&sc->pqueue, sizeof(sc->pqueue));
1847 static int
1848 unblock_mouse_data(struct psm_softc *sc, int c)
1850 int error = 0;
1853 * We may have seen a part of status data during `set_mouse_XXX()'.
1854 * they have been queued; flush it.
1856 empty_aux_buffer(sc->kbdc, 0);
1858 /* restore ports and interrupt */
1859 if (!set_controller_command_byte(sc->kbdc,
1860 KBD_AUX_CONTROL_BITS, c & (KBD_AUX_CONTROL_BITS))) {
1862 * CONTROLLER ERROR; this is serious, we may have
1863 * been left with the inaccessible keyboard and
1864 * the disabled mouse interrupt.
1866 error = EIO;
1869 kbdc_lock(sc->kbdc, FALSE);
1870 return (error);
1873 static int
1874 psmwrite(struct dev_write_args *ap)
1876 cdev_t dev = ap->a_head.a_dev;
1877 struct uio *uio = ap->a_uio;
1878 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1879 u_char buf[PSM_SMALLBUFSIZE];
1880 int error = 0, i, l;
1882 if ((sc->state & PSM_VALID) == 0)
1883 return (EIO);
1885 if (sc->mode.level < PSM_LEVEL_NATIVE)
1886 return (ENODEV);
1888 /* copy data from the user land */
1889 while (uio->uio_resid > 0) {
1890 l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
1891 error = uiomove(buf, l, uio);
1892 if (error)
1893 break;
1894 for (i = 0; i < l; i++) {
1895 VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
1896 if (!write_aux_command(sc->kbdc, buf[i])) {
1897 VLOG(2, (LOG_DEBUG,
1898 "psm: cmd 0x%x failed.\n", buf[i]));
1899 return (reinitialize(sc, FALSE));
1904 return (error);
1907 static int
1908 psmioctl(struct dev_ioctl_args *ap)
1910 cdev_t dev = ap->a_head.a_dev;
1911 caddr_t addr= ap->a_data;
1912 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1913 mousemode_t mode;
1914 mousestatus_t status;
1915 #if (defined(MOUSE_GETVARS))
1916 mousevar_t *var;
1917 #endif
1918 mousedata_t *data;
1919 int stat[3];
1920 int command_byte;
1921 int error = 0;
1923 mode.resolution = -1;
1925 /* Perform IOCTL command */
1926 switch (ap->a_cmd) {
1928 case OLD_MOUSE_GETHWINFO:
1929 crit_enter();
1930 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1931 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1932 ((old_mousehw_t *)addr)->type = sc->hw.type;
1933 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1934 crit_exit();
1935 break;
1937 case MOUSE_GETHWINFO:
1938 crit_enter();
1939 *(mousehw_t *)addr = sc->hw;
1940 if (sc->mode.level == PSM_LEVEL_BASE)
1941 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1942 crit_exit();
1943 break;
1945 case MOUSE_SYN_GETHWINFO:
1946 crit_enter();
1947 if (synaptics_support && sc->hw.model == MOUSE_MODEL_SYNAPTICS)
1948 *(synapticshw_t *)addr = sc->synhw;
1949 else
1950 error = EINVAL;
1951 crit_exit();
1952 break;
1954 case OLD_MOUSE_GETMODE:
1955 crit_enter();
1956 switch (sc->mode.level) {
1957 case PSM_LEVEL_BASE:
1958 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1959 break;
1960 case PSM_LEVEL_STANDARD:
1961 ((old_mousemode_t *)addr)->protocol =
1962 MOUSE_PROTO_SYSMOUSE;
1963 break;
1964 case PSM_LEVEL_NATIVE:
1965 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1966 break;
1968 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1969 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1970 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1971 crit_exit();
1972 break;
1974 case MOUSE_GETMODE:
1975 crit_enter();
1976 *(mousemode_t *)addr = sc->mode;
1977 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
1978 ((mousemode_t *)addr)->syncmask[0] = 0;
1979 ((mousemode_t *)addr)->syncmask[1] = 0;
1981 ((mousemode_t *)addr)->resolution =
1982 MOUSE_RES_LOW - sc->mode.resolution;
1983 switch (sc->mode.level) {
1984 case PSM_LEVEL_BASE:
1985 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1986 ((mousemode_t *)addr)->packetsize =
1987 MOUSE_PS2_PACKETSIZE;
1988 break;
1989 case PSM_LEVEL_STANDARD:
1990 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1991 ((mousemode_t *)addr)->packetsize =
1992 MOUSE_SYS_PACKETSIZE;
1993 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1994 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1995 break;
1996 case PSM_LEVEL_NATIVE:
1997 /* FIXME: this isn't quite correct... XXX */
1998 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1999 break;
2001 crit_exit();
2002 break;
2004 case OLD_MOUSE_SETMODE:
2005 case MOUSE_SETMODE:
2006 if (ap->a_cmd == OLD_MOUSE_SETMODE) {
2007 mode.rate = ((old_mousemode_t *)addr)->rate;
2009 * resolution old I/F new I/F
2010 * default 0 0
2011 * low 1 -2
2012 * medium low 2 -3
2013 * medium high 3 -4
2014 * high 4 -5
2016 if (((old_mousemode_t *)addr)->resolution > 0)
2017 mode.resolution =
2018 -((old_mousemode_t *)addr)->resolution - 1;
2019 else
2020 mode.resolution = 0;
2021 mode.accelfactor =
2022 ((old_mousemode_t *)addr)->accelfactor;
2023 mode.level = -1;
2024 } else
2025 mode = *(mousemode_t *)addr;
2027 /* adjust and validate parameters. */
2028 if (mode.rate > UCHAR_MAX)
2029 return (EINVAL);
2030 if (mode.rate == 0)
2031 mode.rate = sc->dflt_mode.rate;
2032 else if (mode.rate == -1)
2033 /* don't change the current setting */
2035 else if (mode.rate < 0)
2036 return (EINVAL);
2037 if (mode.resolution >= UCHAR_MAX)
2038 return (EINVAL);
2039 if (mode.resolution >= 200)
2040 mode.resolution = MOUSE_RES_HIGH;
2041 else if (mode.resolution >= 100)
2042 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2043 else if (mode.resolution >= 50)
2044 mode.resolution = MOUSE_RES_MEDIUMLOW;
2045 else if (mode.resolution > 0)
2046 mode.resolution = MOUSE_RES_LOW;
2047 if (mode.resolution == MOUSE_RES_DEFAULT)
2048 mode.resolution = sc->dflt_mode.resolution;
2049 else if (mode.resolution == -1)
2050 /* don't change the current setting */
2052 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2053 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2054 if (mode.level == -1)
2055 /* don't change the current setting */
2056 mode.level = sc->mode.level;
2057 else if ((mode.level < PSM_LEVEL_MIN) ||
2058 (mode.level > PSM_LEVEL_MAX))
2059 return (EINVAL);
2060 if (mode.accelfactor == -1)
2061 /* don't change the current setting */
2062 mode.accelfactor = sc->mode.accelfactor;
2063 else if (mode.accelfactor < 0)
2064 return (EINVAL);
2066 /* don't allow anybody to poll the keyboard controller */
2067 error = block_mouse_data(sc, &command_byte);
2068 if (error)
2069 return (error);
2071 /* set mouse parameters */
2072 if (mode.rate > 0)
2073 mode.rate = set_mouse_sampling_rate(sc->kbdc,
2074 mode.rate);
2075 if (mode.resolution >= 0)
2076 mode.resolution =
2077 set_mouse_resolution(sc->kbdc, mode.resolution);
2078 set_mouse_scaling(sc->kbdc, 1);
2079 get_mouse_status(sc->kbdc, stat, 0, 3);
2081 crit_enter();
2082 sc->mode.rate = mode.rate;
2083 sc->mode.resolution = mode.resolution;
2084 sc->mode.accelfactor = mode.accelfactor;
2085 sc->mode.level = mode.level;
2086 crit_exit();
2088 unblock_mouse_data(sc, command_byte);
2089 break;
2091 case MOUSE_GETLEVEL:
2092 *(int *)addr = sc->mode.level;
2093 break;
2095 case MOUSE_SETLEVEL:
2096 if ((*(int *)addr < PSM_LEVEL_MIN) ||
2097 (*(int *)addr > PSM_LEVEL_MAX))
2098 return (EINVAL);
2099 sc->mode.level = *(int *)addr;
2100 break;
2102 case MOUSE_GETSTATUS:
2103 crit_enter();
2104 status = sc->status;
2105 sc->status.flags = 0;
2106 sc->status.obutton = sc->status.button;
2107 sc->status.button = 0;
2108 sc->status.dx = 0;
2109 sc->status.dy = 0;
2110 sc->status.dz = 0;
2111 crit_exit();
2112 *(mousestatus_t *)addr = status;
2113 break;
2115 #if (defined(MOUSE_GETVARS))
2116 case MOUSE_GETVARS:
2117 var = (mousevar_t *)addr;
2118 bzero(var, sizeof(*var));
2119 crit_enter();
2120 var->var[0] = MOUSE_VARS_PS2_SIG;
2121 var->var[1] = sc->config;
2122 var->var[2] = sc->flags;
2123 crit_exit();
2124 break;
2126 case MOUSE_SETVARS:
2127 return (ENODEV);
2128 #endif /* MOUSE_GETVARS */
2130 case MOUSE_READSTATE:
2131 case MOUSE_READDATA:
2132 data = (mousedata_t *)addr;
2133 if (data->len > NELEM(data->buf))
2134 return (EINVAL);
2136 error = block_mouse_data(sc, &command_byte);
2137 if (error)
2138 return (error);
2139 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2140 (ap->a_cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2141 error = EIO;
2142 unblock_mouse_data(sc, command_byte);
2143 break;
2145 #if (defined(MOUSE_SETRESOLUTION))
2146 case MOUSE_SETRESOLUTION:
2147 mode.resolution = *(int *)addr;
2148 if (mode.resolution >= UCHAR_MAX)
2149 return (EINVAL);
2150 else if (mode.resolution >= 200)
2151 mode.resolution = MOUSE_RES_HIGH;
2152 else if (mode.resolution >= 100)
2153 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2154 else if (mode.resolution >= 50)
2155 mode.resolution = MOUSE_RES_MEDIUMLOW;
2156 else if (mode.resolution > 0)
2157 mode.resolution = MOUSE_RES_LOW;
2158 if (mode.resolution == MOUSE_RES_DEFAULT)
2159 mode.resolution = sc->dflt_mode.resolution;
2160 else if (mode.resolution == -1)
2161 mode.resolution = sc->mode.resolution;
2162 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2163 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2165 error = block_mouse_data(sc, &command_byte);
2166 if (error)
2167 return (error);
2168 sc->mode.resolution =
2169 set_mouse_resolution(sc->kbdc, mode.resolution);
2170 if (sc->mode.resolution != mode.resolution)
2171 error = EIO;
2172 unblock_mouse_data(sc, command_byte);
2173 break;
2174 #endif /* MOUSE_SETRESOLUTION */
2176 #if (defined(MOUSE_SETRATE))
2177 case MOUSE_SETRATE:
2178 mode.rate = *(int *)addr;
2179 if (mode.rate > UCHAR_MAX)
2180 return (EINVAL);
2181 if (mode.rate == 0)
2182 mode.rate = sc->dflt_mode.rate;
2183 else if (mode.rate < 0)
2184 mode.rate = sc->mode.rate;
2186 error = block_mouse_data(sc, &command_byte);
2187 if (error)
2188 return (error);
2189 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2190 if (sc->mode.rate != mode.rate)
2191 error = EIO;
2192 unblock_mouse_data(sc, command_byte);
2193 break;
2194 #endif /* MOUSE_SETRATE */
2196 #if (defined(MOUSE_SETSCALING))
2197 case MOUSE_SETSCALING:
2198 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2199 return (EINVAL);
2201 error = block_mouse_data(sc, &command_byte);
2202 if (error)
2203 return (error);
2204 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2205 error = EIO;
2206 unblock_mouse_data(sc, command_byte);
2207 break;
2208 #endif /* MOUSE_SETSCALING */
2210 #if (defined(MOUSE_GETHWID))
2211 case MOUSE_GETHWID:
2212 error = block_mouse_data(sc, &command_byte);
2213 if (error)
2214 return (error);
2215 sc->hw.hwid &= ~0x00ff;
2216 sc->hw.hwid |= get_aux_id(sc->kbdc);
2217 *(int *)addr = sc->hw.hwid & 0x00ff;
2218 unblock_mouse_data(sc, command_byte);
2219 break;
2220 #endif /* MOUSE_GETHWID */
2222 default:
2223 return (ENOTTY);
2226 return (error);
2229 static void
2230 psmtimeout(void *arg)
2232 struct psm_softc *sc;
2234 sc = (struct psm_softc *)arg;
2235 crit_enter();
2236 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2237 VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2238 psmintr(sc);
2239 kbdc_lock(sc->kbdc, FALSE);
2241 sc->watchdog = TRUE;
2242 crit_exit();
2243 callout_reset(&sc->callout, hz, psmtimeout, (void *)(uintptr_t)sc);
2246 /* Add all sysctls under the debug.psm and hw.psm nodes */
2247 SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2248 SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2250 SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RW, &verbose, 0,
2251 "Verbosity level");
2253 static int psmhz = 20;
2254 SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2255 "Frequency of the softcallout (in hz)");
2256 static int psmerrsecs = 2;
2257 SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2258 "Number of seconds during which packets will dropped after a sync error");
2259 static int psmerrusecs = 0;
2260 SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2261 "Microseconds to add to psmerrsecs");
2262 static int psmsecs = 0;
2263 SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2264 "Max number of seconds between soft interrupts");
2265 static int psmusecs = 500000;
2266 SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2267 "Microseconds to add to psmsecs");
2268 static int pkterrthresh = 2;
2269 SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2270 "Number of error packets allowed before reinitializing the mouse");
2272 SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RW, &tap_enabled, 0,
2273 "Enable tap and drag gestures");
2274 static int tap_threshold = PSM_TAP_THRESHOLD;
2275 SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2276 "Button tap threshold");
2277 static int tap_timeout = PSM_TAP_TIMEOUT;
2278 SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2279 "Tap timeout for touchpads");
2281 static void
2282 psmintr(void *arg)
2284 struct psm_softc *sc = arg;
2285 struct timeval now;
2286 int c;
2287 packetbuf_t *pb;
2290 /* read until there is nothing to read */
2291 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2292 pb = &sc->pqueue[sc->pqueue_end];
2294 /* discard the byte if the device is not open */
2295 if ((sc->state & PSM_OPEN) == 0)
2296 continue;
2298 getmicrouptime(&now);
2299 if ((pb->inputbytes > 0) &&
2300 timevalcmp(&now, &sc->inputtimeout, >)) {
2301 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2302 "resetting byte count\n"));
2303 pb->inputbytes = 0;
2304 sc->syncerrors = 0;
2305 sc->pkterrors = 0;
2307 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2308 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2309 timevaladd(&sc->inputtimeout, &now);
2311 pb->ipacket[pb->inputbytes++] = c;
2313 if (sc->mode.level == PSM_LEVEL_NATIVE) {
2314 VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2315 sc->syncerrors = 0;
2316 sc->pkterrors = 0;
2317 goto next;
2318 } else {
2319 if (pb->inputbytes < sc->mode.packetsize)
2320 continue;
2322 VLOG(4, (LOG_DEBUG,
2323 "psmintr: %02x %02x %02x %02x %02x %02x\n",
2324 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2325 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2328 c = pb->ipacket[0];
2330 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2331 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2332 sc->flags &= ~PSM_NEED_SYNCBITS;
2333 VLOG(2, (LOG_DEBUG,
2334 "psmintr: Sync bytes now %04x,%04x\n",
2335 sc->mode.syncmask[0], sc->mode.syncmask[0]));
2336 } else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2337 VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
2338 "(%04x != %04x) %d cmds since last error.\n",
2339 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2340 sc->cmdcount - sc->lasterr));
2341 sc->lasterr = sc->cmdcount;
2343 * The sync byte test is a weak measure of packet
2344 * validity. Conservatively discard any input yet
2345 * to be seen by userland when we detect a sync
2346 * error since there is a good chance some of
2347 * the queued packets have undetected errors.
2349 dropqueue(sc);
2350 if (sc->syncerrors == 0)
2351 sc->pkterrors++;
2352 ++sc->syncerrors;
2353 sc->lastinputerr = now;
2354 if (sc->syncerrors >= sc->mode.packetsize * 2 ||
2355 sc->pkterrors >= pkterrthresh) {
2357 * If we've failed to find a single sync byte
2358 * in 2 packets worth of data, or we've seen
2359 * persistent packet errors during the
2360 * validation period, reinitialize the mouse
2361 * in hopes of returning it to the expected
2362 * mode.
2364 VLOG(3, (LOG_DEBUG,
2365 "psmintr: reset the mouse.\n"));
2366 reinitialize(sc, TRUE);
2367 } else if (sc->syncerrors == sc->mode.packetsize) {
2369 * Try a soft reset after searching for a sync
2370 * byte through a packet length of bytes.
2372 VLOG(3, (LOG_DEBUG,
2373 "psmintr: re-enable the mouse.\n"));
2374 pb->inputbytes = 0;
2375 disable_aux_dev(sc->kbdc);
2376 enable_aux_dev(sc->kbdc);
2377 } else {
2378 VLOG(3, (LOG_DEBUG,
2379 "psmintr: discard a byte (%d)\n",
2380 sc->syncerrors));
2381 pb->inputbytes--;
2382 bcopy(&pb->ipacket[1], &pb->ipacket[0],
2383 pb->inputbytes);
2385 continue;
2389 * We have what appears to be a valid packet.
2390 * Reset the error counters.
2392 sc->syncerrors = 0;
2395 * Drop even good packets if they occur within a timeout
2396 * period of a sync error. This allows the detection of
2397 * a change in the mouse's packet mode without exposing
2398 * erratic mouse behavior to the user. Some KVMs forget
2399 * enhanced mouse modes during switch events.
2401 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
2402 &now)) {
2403 pb->inputbytes = 0;
2404 continue;
2408 * Now that we're out of the validation period, reset
2409 * the packet error count.
2411 sc->pkterrors = 0;
2413 sc->cmdcount++;
2414 next:
2415 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2416 sc->pqueue_end = 0;
2418 * If we've filled the queue then call the softintr ourselves,
2419 * otherwise schedule the interrupt for later.
2421 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2422 (sc->pqueue_end == sc->pqueue_start)) {
2423 if ((sc->state & PSM_SOFTARMED) != 0) {
2424 sc->state &= ~PSM_SOFTARMED;
2425 callout_stop(&sc->softcallout);
2427 psmsoftintr(arg);
2428 } else if ((sc->state & PSM_SOFTARMED) == 0) {
2429 sc->state |= PSM_SOFTARMED;
2430 callout_reset(&sc->softcallout, psmhz < 1 ? 1 : (hz/psmhz),
2431 psmsoftintr, arg);
2436 static void
2437 proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2438 int *x, int *y, int *z)
2442 * PS2++ protocl packet
2444 * b7 b6 b5 b4 b3 b2 b1 b0
2445 * byte 1: * 1 p3 p2 1 * * *
2446 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2448 * p3-p0: packet type
2449 * c1, c2: c1 & c2 == 1, if p2 == 0
2450 * c1 & c2 == 0, if p2 == 1
2452 * packet type: 0 (device type)
2453 * See comments in enable_mmanplus() below.
2455 * packet type: 1 (wheel data)
2457 * b7 b6 b5 b4 b3 b2 b1 b0
2458 * byte 3: h * B5 B4 s d2 d1 d0
2460 * h: 1, if horizontal roller data
2461 * 0, if vertical roller data
2462 * B4, B5: button 4 and 5
2463 * s: sign bit
2464 * d2-d0: roller data
2466 * packet type: 2 (reserved)
2468 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
2469 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2471 * the extended data packet encodes button
2472 * and wheel events
2474 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
2475 case 1:
2476 /* wheel data packet */
2477 *x = *y = 0;
2478 if (pb->ipacket[2] & 0x80) {
2479 /* XXX horizontal roller count - ignore it */
2481 } else {
2482 /* vertical roller count */
2483 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
2484 (pb->ipacket[2] & 0x0f) - 16 :
2485 (pb->ipacket[2] & 0x0f);
2487 ms->button |= (pb->ipacket[2] &
2488 MOUSE_PS2PLUS_BUTTON4DOWN) ?
2489 MOUSE_BUTTON4DOWN : 0;
2490 ms->button |= (pb->ipacket[2] &
2491 MOUSE_PS2PLUS_BUTTON5DOWN) ?
2492 MOUSE_BUTTON5DOWN : 0;
2493 break;
2494 case 2:
2496 * this packet type is reserved by
2497 * Logitech...
2500 * IBM ScrollPoint Mouse uses this
2501 * packet type to encode both vertical
2502 * and horizontal scroll movement.
2504 *x = *y = 0;
2505 /* horizontal count */
2506 if (pb->ipacket[2] & 0x0f)
2507 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
2508 -2 : 2;
2509 /* vertical count */
2510 if (pb->ipacket[2] & 0xf0)
2511 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
2512 -1 : 1;
2513 break;
2514 case 0:
2515 /* device type packet - shouldn't happen */
2516 /* FALLTHROUGH */
2517 default:
2518 *x = *y = 0;
2519 ms->button = ms->obutton;
2520 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
2521 "type %d: 0x%02x 0x%02x 0x%02x\n",
2522 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2523 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
2524 break;
2526 } else {
2527 /* preserve button states */
2528 ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
2532 static int
2533 proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2534 int *x, int *y, int *z)
2536 static int touchpad_buttons;
2537 static int guest_buttons;
2538 int w, x0, y0;
2540 /* TouchPad PS/2 absolute mode message format
2542 * Bits: 7 6 5 4 3 2 1 0 (LSB)
2543 * ------------------------------------------------
2544 * ipacket[0]: 1 0 W3 W2 0 W1 R L
2545 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
2546 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
2547 * ipacket[3]: 1 1 Yc Xc 0 W0 D U
2548 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
2549 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
2551 * Legend:
2552 * L: left physical mouse button
2553 * R: right physical mouse button
2554 * D: down button
2555 * U: up button
2556 * W: "wrist" value
2557 * X: x position
2558 * Y: y position
2559 * Z: pressure
2561 * Absolute reportable limits: 0 - 6143.
2562 * Typical bezel limits: 1472 - 5472.
2563 * Typical edge marings: 1632 - 5312.
2565 * w = 3 Passthrough Packet
2567 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2570 if (!synaptics_support)
2571 return (0);
2573 /* Sanity check for out of sync packets. */
2574 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2575 (pb->ipacket[3] & 0xc8) != 0xc0)
2576 return (-1);
2578 *x = *y = 0;
2581 * Pressure value.
2582 * Interpretation:
2583 * z = 0 No finger contact
2584 * z = 10 Finger hovering near the pad
2585 * z = 30 Very light finger contact
2586 * z = 80 Normal finger contact
2587 * z = 110 Very heavy finger contact
2588 * z = 200 Finger lying flat on pad surface
2589 * z = 255 Maximum reportable Z
2591 *z = pb->ipacket[2];
2594 * Finger width value
2595 * Interpretation:
2596 * w = 0 Two finger on the pad (capMultiFinger needed)
2597 * w = 1 Three or more fingers (capMultiFinger needed)
2598 * w = 2 Pen (instead of finger) (capPen needed)
2599 * w = 3 Reserved (passthrough?)
2600 * w = 4-7 Finger of normal width (capPalmDetect needed)
2601 * w = 8-14 Very wide finger or palm (capPalmDetect needed)
2602 * w = 15 Maximum reportable width (capPalmDetect needed)
2604 /* XXX Is checking capExtended enough? */
2605 if (sc->synhw.capExtended)
2606 w = ((pb->ipacket[0] & 0x30) >> 2) |
2607 ((pb->ipacket[0] & 0x04) >> 1) |
2608 ((pb->ipacket[3] & 0x04) >> 2);
2609 else {
2610 /* Assume a finger of regular width. */
2611 w = 4;
2614 /* Handle packets from the guest device */
2615 /* XXX Documentation? */
2616 if (w == 3 && sc->synhw.capPassthrough) {
2617 *x = ((pb->ipacket[1] & 0x10) ?
2618 pb->ipacket[4] - 256 : pb->ipacket[4]);
2619 *y = ((pb->ipacket[1] & 0x20) ?
2620 pb->ipacket[5] - 256 : pb->ipacket[5]);
2621 *z = 0;
2623 guest_buttons = 0;
2624 if (pb->ipacket[1] & 0x01)
2625 guest_buttons |= MOUSE_BUTTON1DOWN;
2626 if (pb->ipacket[1] & 0x04)
2627 guest_buttons |= MOUSE_BUTTON2DOWN;
2628 if (pb->ipacket[1] & 0x02)
2629 guest_buttons |= MOUSE_BUTTON3DOWN;
2631 ms->button = touchpad_buttons | guest_buttons;
2632 goto SYNAPTICS_END;
2635 /* Button presses */
2636 touchpad_buttons = 0;
2637 if (pb->ipacket[0] & 0x01)
2638 touchpad_buttons |= MOUSE_BUTTON1DOWN;
2639 if (pb->ipacket[0] & 0x02)
2640 touchpad_buttons |= MOUSE_BUTTON3DOWN;
2642 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2643 if ((pb->ipacket[3] & 0x01) && (pb->ipacket[0] & 0x01) == 0)
2644 touchpad_buttons |= MOUSE_BUTTON4DOWN;
2645 if ((pb->ipacket[3] & 0x02) && (pb->ipacket[0] & 0x02) == 0)
2646 touchpad_buttons |= MOUSE_BUTTON5DOWN;
2650 * In newer pads - bit 0x02 in the third byte of
2651 * the packet indicates that we have an extended
2652 * button press.
2654 /* XXX Documentation? */
2655 if (pb->ipacket[3] & 0x02) {
2657 * if directional_scrolls is not 1, we treat any of
2658 * the scrolling directions as middle-click.
2660 if (sc->syninfo.directional_scrolls) {
2661 if (pb->ipacket[4] & 0x01)
2662 touchpad_buttons |= MOUSE_BUTTON4DOWN;
2663 if (pb->ipacket[5] & 0x01)
2664 touchpad_buttons |= MOUSE_BUTTON5DOWN;
2665 if (pb->ipacket[4] & 0x02)
2666 touchpad_buttons |= MOUSE_BUTTON6DOWN;
2667 if (pb->ipacket[5] & 0x02)
2668 touchpad_buttons |= MOUSE_BUTTON7DOWN;
2669 } else {
2670 if ((pb->ipacket[4] & 0x0F) ||
2671 (pb->ipacket[5] & 0x0F))
2672 touchpad_buttons |= MOUSE_BUTTON2DOWN;
2676 ms->button = touchpad_buttons | guest_buttons;
2678 /* Check pressure to detect a real wanted action on the
2679 * touchpad. */
2680 if (*z >= sc->syninfo.min_pressure) {
2681 synapticsaction_t *synaction;
2682 int cursor, peer, window;
2683 int dx, dy, dxp, dyp;
2684 int max_width, max_pressure;
2685 int margin_top, margin_right, margin_bottom, margin_left;
2686 int na_top, na_right, na_bottom, na_left;
2687 int window_min, window_max;
2688 int multiplicator;
2689 int weight_current, weight_previous, weight_len_squared;
2690 int div_min, div_max, div_len;
2691 int vscroll_hor_area, vscroll_ver_area;
2693 int len, weight_prev_x, weight_prev_y;
2694 int div_max_x, div_max_y, div_x, div_y;
2696 /* Read sysctl. */
2697 /* XXX Verify values? */
2698 max_width = sc->syninfo.max_width;
2699 max_pressure = sc->syninfo.max_pressure;
2700 margin_top = sc->syninfo.margin_top;
2701 margin_right = sc->syninfo.margin_right;
2702 margin_bottom = sc->syninfo.margin_bottom;
2703 margin_left = sc->syninfo.margin_left;
2704 na_top = sc->syninfo.na_top;
2705 na_right = sc->syninfo.na_right;
2706 na_bottom = sc->syninfo.na_bottom;
2707 na_left = sc->syninfo.na_left;
2708 window_min = sc->syninfo.window_min;
2709 window_max = sc->syninfo.window_max;
2710 multiplicator = sc->syninfo.multiplicator;
2711 weight_current = sc->syninfo.weight_current;
2712 weight_previous = sc->syninfo.weight_previous;
2713 weight_len_squared = sc->syninfo.weight_len_squared;
2714 div_min = sc->syninfo.div_min;
2715 div_max = sc->syninfo.div_max;
2716 div_len = sc->syninfo.div_len;
2717 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
2718 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
2720 /* Palm detection. */
2721 if (!(
2722 (sc->synhw.capMultiFinger && (w == 0 || w == 1)) ||
2723 (sc->synhw.capPalmDetect && w >= 4 && w <= max_width) ||
2724 (!sc->synhw.capPalmDetect && *z <= max_pressure) ||
2725 (sc->synhw.capPen && w == 2))) {
2727 * We consider the packet irrelevant for the current
2728 * action when:
2729 * - the width isn't comprised in:
2730 * [4; max_width]
2731 * - the pressure isn't comprised in:
2732 * [min_pressure; max_pressure]
2733 * - pen aren't supported but w is 2
2735 * Note that this doesn't terminate the current action.
2737 VLOG(2, (LOG_DEBUG,
2738 "synaptics: palm detected! (%d)\n", w));
2739 goto SYNAPTICS_END;
2742 /* Read current absolute position. */
2743 x0 = ((pb->ipacket[3] & 0x10) << 8) |
2744 ((pb->ipacket[1] & 0x0f) << 8) |
2745 pb->ipacket[4];
2746 y0 = ((pb->ipacket[3] & 0x20) << 7) |
2747 ((pb->ipacket[1] & 0xf0) << 4) |
2748 pb->ipacket[5];
2750 synaction = &(sc->synaction);
2753 * If the action is just beginning, init the structure and
2754 * compute tap timeout.
2756 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
2757 VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
2759 /* Store the first point of this action. */
2760 synaction->start_x = x0;
2761 synaction->start_y = y0;
2762 dx = dy = 0;
2764 /* Initialize queue. */
2765 synaction->queue_cursor = SYNAPTICS_PACKETQUEUE;
2766 synaction->queue_len = 0;
2767 synaction->window_min = window_min;
2769 /* Reset average. */
2770 synaction->avg_dx = 0;
2771 synaction->avg_dy = 0;
2773 /* Reset squelch. */
2774 synaction->squelch_x = 0;
2775 synaction->squelch_y = 0;
2777 /* Reset pressure peak. */
2778 sc->zmax = 0;
2780 /* Reset fingers count. */
2781 synaction->fingers_nb = 0;
2783 /* Reset virtual scrolling state. */
2784 synaction->in_vscroll = 0;
2786 /* Compute tap timeout. */
2787 sc->taptimeout.tv_sec = tap_timeout / 1000000;
2788 sc->taptimeout.tv_usec = tap_timeout % 1000000;
2789 timevaladd(&sc->taptimeout, &sc->lastsoftintr);
2791 sc->flags |= PSM_FLAGS_FINGERDOWN;
2792 } else {
2793 /* Calculate the current delta. */
2794 cursor = synaction->queue_cursor;
2795 dx = x0 - synaction->queue[cursor].x;
2796 dy = y0 - synaction->queue[cursor].y;
2799 /* If in tap-hold, add the recorded button. */
2800 if (synaction->in_taphold)
2801 ms->button |= synaction->tap_button;
2804 * From now on, we can use the SYNAPTICS_END label to skip
2805 * the current packet.
2809 * Limit the coordinates to the specified margins because
2810 * this area isn't very reliable.
2812 if (x0 <= margin_left)
2813 x0 = margin_left;
2814 else if (x0 >= 6143 - margin_right)
2815 x0 = 6143 - margin_right;
2816 if (y0 <= margin_bottom)
2817 y0 = margin_bottom;
2818 else if (y0 >= 6143 - margin_top)
2819 y0 = 6143 - margin_top;
2821 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
2822 x0, y0, *z, w));
2824 /* Queue this new packet. */
2825 cursor = SYNAPTICS_QUEUE_CURSOR(synaction->queue_cursor - 1);
2826 synaction->queue[cursor].x = x0;
2827 synaction->queue[cursor].y = y0;
2828 synaction->queue_cursor = cursor;
2829 if (synaction->queue_len < SYNAPTICS_PACKETQUEUE)
2830 synaction->queue_len++;
2831 VLOG(5, (LOG_DEBUG,
2832 "synaptics: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
2833 cursor, x0, y0, dx, dy));
2836 * For tap, we keep the maximum number of fingers and the
2837 * pressure peak. Also with multiple fingers, we increase
2838 * the minimum window.
2840 switch (w) {
2841 case 1: /* Three or more fingers. */
2842 synaction->fingers_nb = imax(3, synaction->fingers_nb);
2843 synaction->window_min = window_max;
2844 break;
2845 case 0: /* Two fingers. */
2846 synaction->fingers_nb = imax(2, synaction->fingers_nb);
2847 synaction->window_min = window_max;
2848 break;
2849 default: /* One finger or undetectable. */
2850 synaction->fingers_nb = imax(1, synaction->fingers_nb);
2852 sc->zmax = imax(*z, sc->zmax);
2854 /* Do we have enough packets to consider this a movement? */
2855 if (synaction->queue_len < synaction->window_min)
2856 goto SYNAPTICS_END;
2858 /* Is a scrolling action occuring? */
2859 if (!synaction->in_taphold && !synaction->in_vscroll) {
2861 * A scrolling action must not conflict with a tap
2862 * action. Here are the conditions to consider a
2863 * scrolling action:
2864 * - the action in a configurable area
2865 * - one of the following:
2866 * . the distance between the last packet and the
2867 * first should be above a configurable minimum
2868 * . tap timed out
2870 dxp = abs(synaction->queue[synaction->queue_cursor].x -
2871 synaction->start_x);
2872 dyp = abs(synaction->queue[synaction->queue_cursor].y -
2873 synaction->start_y);
2875 if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, >) ||
2876 dxp >= sc->syninfo.vscroll_min_delta ||
2877 dyp >= sc->syninfo.vscroll_min_delta) {
2878 /* Check for horizontal scrolling. */
2879 if ((vscroll_hor_area > 0 &&
2880 synaction->start_y <= vscroll_hor_area) ||
2881 (vscroll_hor_area < 0 &&
2882 synaction->start_y >=
2883 6143 + vscroll_hor_area))
2884 synaction->in_vscroll += 2;
2886 /* Check for vertical scrolling. */
2887 if ((vscroll_ver_area > 0 &&
2888 synaction->start_x <= vscroll_ver_area) ||
2889 (vscroll_ver_area < 0 &&
2890 synaction->start_x >=
2891 6143 + vscroll_ver_area))
2892 synaction->in_vscroll += 1;
2894 /* Avoid conflicts if area overlaps. */
2895 if (synaction->in_vscroll == 3)
2896 synaction->in_vscroll =
2897 (dxp > dyp) ? 2 : 1;
2899 VLOG(5, (LOG_DEBUG,
2900 "synaptics: virtual scrolling: %s "
2901 "(direction=%d, dxp=%d, dyp=%d)\n",
2902 synaction->in_vscroll ? "YES" : "NO",
2903 synaction->in_vscroll, dxp, dyp));
2906 weight_prev_x = weight_prev_y = weight_previous;
2907 div_max_x = div_max_y = div_max;
2909 if (synaction->in_vscroll) {
2910 /* Dividers are different with virtual scrolling. */
2911 div_min = sc->syninfo.vscroll_div_min;
2912 div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
2913 } else {
2915 * There's a lot of noise in coordinates when
2916 * the finger is on the touchpad's borders. When
2917 * using this area, we apply a special weight and
2918 * div.
2920 if (x0 <= na_left || x0 >= 6143 - na_right) {
2921 weight_prev_x = sc->syninfo.weight_previous_na;
2922 div_max_x = sc->syninfo.div_max_na;
2925 if (y0 <= na_bottom || y0 >= 6143 - na_top) {
2926 weight_prev_y = sc->syninfo.weight_previous_na;
2927 div_max_y = sc->syninfo.div_max_na;
2932 * Calculate weights for the average operands and
2933 * the divisor. Both depend on the distance between
2934 * the current packet and a previous one (based on the
2935 * window width).
2937 window = imin(synaction->queue_len, window_max);
2938 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
2939 dxp = abs(x0 - synaction->queue[peer].x) + 1;
2940 dyp = abs(y0 - synaction->queue[peer].y) + 1;
2941 len = (dxp * dxp) + (dyp * dyp);
2942 weight_prev_x = imin(weight_prev_x,
2943 weight_len_squared * weight_prev_x / len);
2944 weight_prev_y = imin(weight_prev_y,
2945 weight_len_squared * weight_prev_y / len);
2947 len = (dxp + dyp) / 2;
2948 div_x = div_len * div_max_x / len;
2949 div_x = imin(div_max_x, div_x);
2950 div_x = imax(div_min, div_x);
2951 div_y = div_len * div_max_y / len;
2952 div_y = imin(div_max_y, div_y);
2953 div_y = imax(div_min, div_y);
2955 VLOG(3, (LOG_DEBUG,
2956 "synaptics: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
2957 peer, len, weight_prev_x, weight_prev_y, div_x, div_y));
2959 /* Compute averages. */
2960 synaction->avg_dx =
2961 (weight_current * dx * multiplicator +
2962 weight_prev_x * synaction->avg_dx) /
2963 (weight_current + weight_prev_x);
2965 synaction->avg_dy =
2966 (weight_current * dy * multiplicator +
2967 weight_prev_y * synaction->avg_dy) /
2968 (weight_current + weight_prev_y);
2970 VLOG(5, (LOG_DEBUG,
2971 "synaptics: avg_dx~=%d, avg_dy~=%d\n",
2972 synaction->avg_dx / multiplicator,
2973 synaction->avg_dy / multiplicator));
2975 /* Use these averages to calculate x & y. */
2976 synaction->squelch_x += synaction->avg_dx;
2977 *x = synaction->squelch_x / (div_x * multiplicator);
2978 synaction->squelch_x = synaction->squelch_x %
2979 (div_x * multiplicator);
2981 synaction->squelch_y += synaction->avg_dy;
2982 *y = synaction->squelch_y / (div_y * multiplicator);
2983 synaction->squelch_y = synaction->squelch_y %
2984 (div_y * multiplicator);
2986 if (synaction->in_vscroll) {
2987 switch(synaction->in_vscroll) {
2988 case 1: /* Vertical scrolling. */
2989 if (*y != 0)
2990 ms->button |= (*y > 0) ?
2991 MOUSE_BUTTON4DOWN :
2992 MOUSE_BUTTON5DOWN;
2993 break;
2994 case 2: /* Horizontal scrolling. */
2995 if (*x != 0)
2996 ms->button |= (*x > 0) ?
2997 MOUSE_BUTTON7DOWN :
2998 MOUSE_BUTTON6DOWN;
2999 break;
3002 /* The pointer is not moved. */
3003 *x = *y = 0;
3004 } else {
3005 VLOG(3, (LOG_DEBUG, "synaptics: [%d, %d] -> [%d, %d]\n",
3006 dx, dy, *x, *y));
3008 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3010 * An action is currently taking place but the pressure
3011 * dropped under the minimum, putting an end to it.
3013 synapticsaction_t *synaction;
3014 int taphold_timeout, dx, dy, tap_max_delta;
3016 synaction = &(sc->synaction);
3017 dx = abs(synaction->queue[synaction->queue_cursor].x -
3018 synaction->start_x);
3019 dy = abs(synaction->queue[synaction->queue_cursor].y -
3020 synaction->start_y);
3022 /* Max delta is disabled for multi-fingers tap. */
3023 if (synaction->fingers_nb > 1)
3024 tap_max_delta = imax(dx, dy);
3025 else
3026 tap_max_delta = sc->syninfo.tap_max_delta;
3028 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3030 /* Check for tap. */
3031 VLOG(3, (LOG_DEBUG,
3032 "synaptics: zmax=%d, dx=%d, dy=%d, "
3033 "delta=%d, fingers=%d, queue=%d\n",
3034 sc->zmax, dx, dy, tap_max_delta, synaction->fingers_nb,
3035 synaction->queue_len));
3036 if (!synaction->in_vscroll && sc->zmax >= tap_threshold &&
3037 timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=) &&
3038 dx <= tap_max_delta && dy <= tap_max_delta &&
3039 synaction->queue_len >= sc->syninfo.tap_min_queue) {
3041 * We have a tap if:
3042 * - the maximum pressure went over tap_threshold
3043 * - the action ended before tap_timeout
3045 * To handle tap-hold, we must delay any button push to
3046 * the next action.
3048 if (synaction->in_taphold) {
3050 * This is the second and last tap of a
3051 * double tap action, not a tap-hold.
3053 synaction->in_taphold = 0;
3056 * For double-tap to work:
3057 * - no button press is emitted (to
3058 * simulate a button release)
3059 * - PSM_FLAGS_FINGERDOWN is set to
3060 * force the next packet to emit a
3061 * button press)
3063 VLOG(2, (LOG_DEBUG,
3064 "synaptics: button RELEASE: %d\n",
3065 synaction->tap_button));
3066 sc->flags |= PSM_FLAGS_FINGERDOWN;
3067 } else {
3069 * This is the first tap: we set the
3070 * tap-hold state and notify the button
3071 * down event.
3073 synaction->in_taphold = 1;
3074 taphold_timeout = sc->syninfo.taphold_timeout;
3075 sc->taptimeout.tv_sec = taphold_timeout /
3076 1000000;
3077 sc->taptimeout.tv_usec = taphold_timeout %
3078 1000000;
3079 timevaladd(&sc->taptimeout, &sc->lastsoftintr);
3081 switch (synaction->fingers_nb) {
3082 case 3:
3083 synaction->tap_button =
3084 MOUSE_BUTTON2DOWN;
3085 break;
3086 case 2:
3087 synaction->tap_button =
3088 MOUSE_BUTTON3DOWN;
3089 break;
3090 default:
3091 synaction->tap_button =
3092 MOUSE_BUTTON1DOWN;
3094 VLOG(2, (LOG_DEBUG,
3095 "synaptics: button PRESS: %d\n",
3096 synaction->tap_button));
3097 ms->button |= synaction->tap_button;
3099 } else {
3101 * Not enough pressure or timeout: reset
3102 * tap-hold state.
3104 if (synaction->in_taphold) {
3105 VLOG(2, (LOG_DEBUG,
3106 "synaptics: button RELEASE: %d\n",
3107 synaction->tap_button));
3108 synaction->in_taphold = 0;
3109 } else {
3110 VLOG(2, (LOG_DEBUG,
3111 "synaptics: not a tap-hold\n"));
3114 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) &&
3115 sc->synaction.in_taphold) {
3117 * For a tap-hold to work, the button must remain down at
3118 * least until timeout (where the in_taphold flags will be
3119 * cleared) or during the next action.
3121 if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) {
3122 ms->button |= sc->synaction.tap_button;
3123 } else {
3124 VLOG(2, (LOG_DEBUG,
3125 "synaptics: button RELEASE: %d\n",
3126 sc->synaction.tap_button));
3127 sc->synaction.in_taphold = 0;
3131 SYNAPTICS_END:
3133 * Use the extra buttons as a scrollwheel
3135 * XXX X.Org uses the Z axis for vertical wheel only,
3136 * whereas moused(8) understands special values to differ
3137 * vertical and horizontal wheels.
3139 * xf86-input-mouse needs therefore a small patch to
3140 * understand these special values. Without it, the
3141 * horizontal wheel acts as a vertical wheel in X.Org.
3143 * That's why the horizontal wheel is disabled by
3144 * default for now.
3146 if (ms->button & MOUSE_BUTTON4DOWN) {
3147 *z = -1;
3148 ms->button &= ~MOUSE_BUTTON4DOWN;
3149 } else if (ms->button & MOUSE_BUTTON5DOWN) {
3150 *z = 1;
3151 ms->button &= ~MOUSE_BUTTON5DOWN;
3152 } else if (ms->button & MOUSE_BUTTON6DOWN) {
3153 *z = -2;
3154 ms->button &= ~MOUSE_BUTTON6DOWN;
3155 } else if (ms->button & MOUSE_BUTTON7DOWN) {
3156 *z = 2;
3157 ms->button &= ~MOUSE_BUTTON7DOWN;
3158 } else
3159 *z = 0;
3161 return (0);
3164 static void
3165 proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3166 int *x, int *y, int *z)
3168 static int butmap_versapad[8] = {
3170 MOUSE_BUTTON3DOWN,
3172 MOUSE_BUTTON3DOWN,
3173 MOUSE_BUTTON1DOWN,
3174 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3175 MOUSE_BUTTON1DOWN,
3176 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
3178 int c, x0, y0;
3180 /* VersaPad PS/2 absolute mode message format
3182 * [packet1] 7 6 5 4 3 2 1 0(LSB)
3183 * ipacket[0]: 1 1 0 A 1 L T R
3184 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
3185 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
3186 * ipacket[3]: 1 1 1 A 1 L T R
3187 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
3188 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
3190 * [note]
3191 * R: right physical mouse button (1=on)
3192 * T: touch pad virtual button (1=tapping)
3193 * L: left physical mouse button (1=on)
3194 * A: position data is valid (1=valid)
3195 * H: horizontal data (12bit signed integer. H11 is sign bit.)
3196 * V: vertical data (12bit signed integer. V11 is sign bit.)
3197 * P: pressure data
3199 * Tapping is mapped to MOUSE_BUTTON4.
3201 c = pb->ipacket[0];
3202 *x = *y = 0;
3203 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
3204 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
3205 if (c & MOUSE_PS2VERSA_IN_USE) {
3206 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
3207 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
3208 if (x0 & 0x800)
3209 x0 -= 0x1000;
3210 if (y0 & 0x800)
3211 y0 -= 0x1000;
3212 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3213 *x = sc->xold - x0;
3214 *y = y0 - sc->yold;
3215 if (*x < 0) /* XXX */
3216 ++*x;
3217 else if (*x)
3218 --*x;
3219 if (*y < 0)
3220 ++*y;
3221 else if (*y)
3222 --*y;
3223 } else
3224 sc->flags |= PSM_FLAGS_FINGERDOWN;
3225 sc->xold = x0;
3226 sc->yold = y0;
3227 } else
3228 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3231 static void
3232 psmsoftintr(void *arg)
3235 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
3236 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
3238 static int butmap[8] = {
3240 MOUSE_BUTTON1DOWN,
3241 MOUSE_BUTTON3DOWN,
3242 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3243 MOUSE_BUTTON2DOWN,
3244 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
3245 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
3246 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
3248 struct psm_softc *sc = arg;
3249 mousestatus_t ms;
3250 packetbuf_t *pb;
3251 int x, y, z, c, l;
3253 getmicrouptime(&sc->lastsoftintr);
3255 crit_enter();
3257 do {
3258 pb = &sc->pqueue[sc->pqueue_start];
3260 if (sc->mode.level == PSM_LEVEL_NATIVE)
3261 goto next_native;
3263 c = pb->ipacket[0];
3265 * A kludge for Kensington device!
3266 * The MSB of the horizontal count appears to be stored in
3267 * a strange place.
3269 if (sc->hw.model == MOUSE_MODEL_THINK)
3270 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
3272 /* ignore the overflow bits... */
3273 x = (c & MOUSE_PS2_XNEG) ?
3274 pb->ipacket[1] - 256 : pb->ipacket[1];
3275 y = (c & MOUSE_PS2_YNEG) ?
3276 pb->ipacket[2] - 256 : pb->ipacket[2];
3277 z = 0;
3278 ms.obutton = sc->button; /* previous button state */
3279 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
3280 /* `tapping' action */
3281 if (sc->config & PSM_CONFIG_FORCETAP)
3282 ms.button |= ((c & MOUSE_PS2_TAP)) ?
3283 0 : MOUSE_BUTTON4DOWN;
3285 switch (sc->hw.model) {
3287 case MOUSE_MODEL_EXPLORER:
3289 * b7 b6 b5 b4 b3 b2 b1 b0
3290 * byte 1: oy ox sy sx 1 M R L
3291 * byte 2: x x x x x x x x
3292 * byte 3: y y y y y y y y
3293 * byte 4: * * S2 S1 s d2 d1 d0
3295 * L, M, R, S1, S2: left, middle, right and side buttons
3296 * s: wheel data sign bit
3297 * d2-d0: wheel data
3299 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
3300 (pb->ipacket[3] & 0x0f) - 16 :
3301 (pb->ipacket[3] & 0x0f);
3302 ms.button |=
3303 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
3304 MOUSE_BUTTON4DOWN : 0;
3305 ms.button |=
3306 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
3307 MOUSE_BUTTON5DOWN : 0;
3308 break;
3310 case MOUSE_MODEL_INTELLI:
3311 case MOUSE_MODEL_NET:
3312 /* wheel data is in the fourth byte */
3313 z = (char)pb->ipacket[3];
3315 * XXX some mice may send 7 when there is no Z movement? */
3316 if ((z >= 7) || (z <= -7))
3317 z = 0;
3318 /* some compatible mice have additional buttons */
3319 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
3320 MOUSE_BUTTON4DOWN : 0;
3321 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
3322 MOUSE_BUTTON5DOWN : 0;
3323 break;
3325 case MOUSE_MODEL_MOUSEMANPLUS:
3326 proc_mmanplus(sc, pb, &ms, &x, &y, &z);
3327 break;
3329 case MOUSE_MODEL_GLIDEPOINT:
3330 /* `tapping' action */
3331 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
3332 MOUSE_BUTTON4DOWN;
3333 break;
3335 case MOUSE_MODEL_NETSCROLL:
3337 * three addtional bytes encode buttons and
3338 * wheel events
3340 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
3341 MOUSE_BUTTON4DOWN : 0;
3342 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
3343 MOUSE_BUTTON5DOWN : 0;
3344 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
3345 pb->ipacket[4] - 256 : pb->ipacket[4];
3346 break;
3348 case MOUSE_MODEL_THINK:
3349 /* the fourth button state in the first byte */
3350 ms.button |= (c & MOUSE_PS2_TAP) ?
3351 MOUSE_BUTTON4DOWN : 0;
3352 break;
3354 case MOUSE_MODEL_VERSAPAD:
3355 proc_versapad(sc, pb, &ms, &x, &y, &z);
3356 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
3357 ((y < 0) ? MOUSE_PS2_YNEG : 0);
3358 break;
3360 case MOUSE_MODEL_4D:
3362 * b7 b6 b5 b4 b3 b2 b1 b0
3363 * byte 1: s2 d2 s1 d1 1 M R L
3364 * byte 2: sx x x x x x x x
3365 * byte 3: sy y y y y y y y
3367 * s1: wheel 1 direction
3368 * d1: wheel 1 data
3369 * s2: wheel 2 direction
3370 * d2: wheel 2 data
3372 x = (pb->ipacket[1] & 0x80) ?
3373 pb->ipacket[1] - 256 : pb->ipacket[1];
3374 y = (pb->ipacket[2] & 0x80) ?
3375 pb->ipacket[2] - 256 : pb->ipacket[2];
3376 switch (c & MOUSE_4D_WHEELBITS) {
3377 case 0x10:
3378 z = 1;
3379 break;
3380 case 0x30:
3381 z = -1;
3382 break;
3383 case 0x40: /* XXX 2nd wheel turning right */
3384 z = 2;
3385 break;
3386 case 0xc0: /* XXX 2nd wheel turning left */
3387 z = -2;
3388 break;
3390 break;
3392 case MOUSE_MODEL_4DPLUS:
3393 if ((x < 16 - 256) && (y < 16 - 256)) {
3395 * b7 b6 b5 b4 b3 b2 b1 b0
3396 * byte 1: 0 0 1 1 1 M R L
3397 * byte 2: 0 0 0 0 1 0 0 0
3398 * byte 3: 0 0 0 0 S s d1 d0
3400 * L, M, R, S: left, middle, right,
3401 * and side buttons
3402 * s: wheel data sign bit
3403 * d1-d0: wheel data
3405 x = y = 0;
3406 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
3407 ms.button |= MOUSE_BUTTON4DOWN;
3408 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
3409 ((pb->ipacket[2] & 0x07) - 8) :
3410 (pb->ipacket[2] & 0x07) ;
3411 } else {
3412 /* preserve previous button states */
3413 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
3415 break;
3417 case MOUSE_MODEL_SYNAPTICS:
3418 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0)
3419 goto next;
3420 break;
3422 case MOUSE_MODEL_GENERIC:
3423 default:
3424 break;
3427 /* scale values */
3428 if (sc->mode.accelfactor >= 1) {
3429 if (x != 0) {
3430 x = x * x / sc->mode.accelfactor;
3431 if (x == 0)
3432 x = 1;
3433 if (c & MOUSE_PS2_XNEG)
3434 x = -x;
3436 if (y != 0) {
3437 y = y * y / sc->mode.accelfactor;
3438 if (y == 0)
3439 y = 1;
3440 if (c & MOUSE_PS2_YNEG)
3441 y = -y;
3445 ms.dx = x;
3446 ms.dy = y;
3447 ms.dz = z;
3448 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
3449 (ms.obutton ^ ms.button);
3451 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
3453 sc->status.flags |= ms.flags;
3454 sc->status.dx += ms.dx;
3455 sc->status.dy += ms.dy;
3456 sc->status.dz += ms.dz;
3457 sc->status.button = ms.button;
3458 sc->button = ms.button;
3460 next_native:
3461 sc->watchdog = FALSE;
3463 /* queue data */
3464 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
3465 l = imin(pb->inputbytes,
3466 sizeof(sc->queue.buf) - sc->queue.tail);
3467 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
3468 if (pb->inputbytes > l)
3469 bcopy(&pb->ipacket[l], &sc->queue.buf[0],
3470 pb->inputbytes - l);
3471 sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
3472 sizeof(sc->queue.buf);
3473 sc->queue.count += pb->inputbytes;
3475 pb->inputbytes = 0;
3477 next:
3478 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
3479 sc->pqueue_start = 0;
3480 } while (sc->pqueue_start != sc->pqueue_end);
3482 if (sc->state & PSM_ASLP) {
3483 sc->state &= ~PSM_ASLP;
3484 wakeup(sc);
3486 KNOTE(&sc->rkq.ki_note, 0);
3488 sc->state &= ~PSM_SOFTARMED;
3489 crit_exit();
3492 static struct filterops psmfiltops =
3493 { FILTEROP_ISFD, NULL, psmfilter_detach, psmfilter };
3495 static int
3496 psmkqfilter(struct dev_kqfilter_args *ap)
3498 cdev_t dev = ap->a_head.a_dev;
3499 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
3500 struct knote *kn = ap->a_kn;
3501 struct klist *klist;
3503 ap->a_result = 0;
3505 switch (kn->kn_filter) {
3506 case EVFILT_READ:
3507 kn->kn_fop = &psmfiltops;
3508 kn->kn_hook = (caddr_t)sc;
3509 break;
3510 default:
3511 ap->a_result = EOPNOTSUPP;
3512 return (0);
3515 klist = &sc->rkq.ki_note;
3516 knote_insert(klist, kn);
3518 return (0);
3521 static void
3522 psmfilter_detach(struct knote *kn)
3524 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook;
3525 struct klist *klist;
3527 klist = &sc->rkq.ki_note;
3528 knote_remove(klist, kn);
3531 static int
3532 psmfilter(struct knote *kn, long hint)
3534 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook;
3535 int ready = 0;
3537 crit_enter();
3538 if (sc->queue.count > 0)
3539 ready = 1;
3540 crit_exit();
3542 return (ready);
3545 /* vendor/model specific routines */
3547 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
3549 if (set_mouse_resolution(kbdc, res) != res)
3550 return (FALSE);
3551 if (set_mouse_scaling(kbdc, scale) &&
3552 set_mouse_scaling(kbdc, scale) &&
3553 set_mouse_scaling(kbdc, scale) &&
3554 (get_mouse_status(kbdc, status, 0, 3) >= 3))
3555 return (TRUE);
3556 return (FALSE);
3559 static int
3560 mouse_ext_command(KBDC kbdc, int command)
3562 int c;
3564 c = (command >> 6) & 0x03;
3565 if (set_mouse_resolution(kbdc, c) != c)
3566 return (FALSE);
3567 c = (command >> 4) & 0x03;
3568 if (set_mouse_resolution(kbdc, c) != c)
3569 return (FALSE);
3570 c = (command >> 2) & 0x03;
3571 if (set_mouse_resolution(kbdc, c) != c)
3572 return (FALSE);
3573 c = (command >> 0) & 0x03;
3574 if (set_mouse_resolution(kbdc, c) != c)
3575 return (FALSE);
3576 return (TRUE);
3579 #ifdef notyet
3580 /* Logitech MouseMan Cordless II */
3581 static int
3582 enable_lcordless(struct psm_softc *sc)
3584 int status[3];
3585 int ch;
3587 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
3588 return (FALSE);
3589 if (status[1] == PSMD_RES_HIGH)
3590 return (FALSE);
3591 ch = (status[0] & 0x07) - 1; /* channel # */
3592 if ((ch <= 0) || (ch > 4))
3593 return (FALSE);
3595 * status[1]: always one?
3596 * status[2]: battery status? (0-100)
3598 return (TRUE);
3600 #endif /* notyet */
3602 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
3603 static int
3604 enable_groller(struct psm_softc *sc)
3606 int status[3];
3609 * The special sequence to enable the fourth button and the
3610 * roller. Immediately after this sequence check status bytes.
3611 * if the mouse is NetScroll, the second and the third bytes are
3612 * '3' and 'D'.
3616 * If the mouse is an ordinary PS/2 mouse, the status bytes should
3617 * look like the following.
3619 * byte 1 bit 7 always 0
3620 * bit 6 stream mode (0)
3621 * bit 5 disabled (0)
3622 * bit 4 1:1 scaling (0)
3623 * bit 3 always 0
3624 * bit 0-2 button status
3625 * byte 2 resolution (PSMD_RES_HIGH)
3626 * byte 3 report rate (?)
3629 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
3630 return (FALSE);
3631 if ((status[1] != '3') || (status[2] != 'D'))
3632 return (FALSE);
3633 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
3634 sc->hw.buttons = 4;
3635 return (TRUE);
3638 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
3639 static int
3640 enable_gmouse(struct psm_softc *sc)
3642 int status[3];
3645 * The special sequence to enable the middle, "rubber" button.
3646 * Immediately after this sequence check status bytes.
3647 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
3648 * the second and the third bytes are '3' and 'U'.
3649 * NOTE: NetMouse reports that it has three buttons although it has
3650 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
3651 * say they have three buttons too and they do have a button on the
3652 * side...
3654 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
3655 return (FALSE);
3656 if ((status[1] != '3') || (status[2] != 'U'))
3657 return (FALSE);
3658 return (TRUE);
3661 /* ALPS GlidePoint */
3662 static int
3663 enable_aglide(struct psm_softc *sc)
3665 int status[3];
3668 * The special sequence to obtain ALPS GlidePoint specific
3669 * information. Immediately after this sequence, status bytes will
3670 * contain something interesting.
3671 * NOTE: ALPS produces several models of GlidePoint. Some of those
3672 * do not respond to this sequence, thus, cannot be detected this way.
3674 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
3675 return (FALSE);
3676 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
3677 return (FALSE);
3678 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
3679 return (FALSE);
3680 return (TRUE);
3683 /* Kensington ThinkingMouse/Trackball */
3684 static int
3685 enable_kmouse(struct psm_softc *sc)
3687 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
3688 KBDC kbdc = sc->kbdc;
3689 int status[3];
3690 int id1;
3691 int id2;
3692 int i;
3694 id1 = get_aux_id(kbdc);
3695 if (set_mouse_sampling_rate(kbdc, 10) != 10)
3696 return (FALSE);
3698 * The device is now in the native mode? It returns a different
3699 * ID value...
3701 id2 = get_aux_id(kbdc);
3702 if ((id1 == id2) || (id2 != 2))
3703 return (FALSE);
3705 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
3706 return (FALSE);
3707 #if PSM_DEBUG >= 2
3708 /* at this point, resolution is LOW, sampling rate is 10/sec */
3709 if (get_mouse_status(kbdc, status, 0, 3) < 3)
3710 return (FALSE);
3711 #endif
3714 * The special sequence to enable the third and fourth buttons.
3715 * Otherwise they behave like the first and second buttons.
3717 for (i = 0; i < NELEM(rate); ++i)
3718 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3719 return (FALSE);
3722 * At this point, the device is using default resolution and
3723 * sampling rate for the native mode.
3725 if (get_mouse_status(kbdc, status, 0, 3) < 3)
3726 return (FALSE);
3727 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
3728 return (FALSE);
3730 /* the device appears be enabled by this sequence, diable it for now */
3731 disable_aux_dev(kbdc);
3732 empty_aux_buffer(kbdc, 5);
3734 return (TRUE);
3737 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
3738 static int
3739 enable_mmanplus(struct psm_softc *sc)
3741 KBDC kbdc = sc->kbdc;
3742 int data[3];
3744 /* the special sequence to enable the fourth button and the roller. */
3746 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
3747 * must be called exactly three times since the last RESET command
3748 * before this sequence. XXX
3750 if (!set_mouse_scaling(kbdc, 1))
3751 return (FALSE);
3752 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
3753 return (FALSE);
3754 if (get_mouse_status(kbdc, data, 1, 3) < 3)
3755 return (FALSE);
3758 * PS2++ protocl, packet type 0
3760 * b7 b6 b5 b4 b3 b2 b1 b0
3761 * byte 1: * 1 p3 p2 1 * * *
3762 * byte 2: 1 1 p1 p0 m1 m0 1 0
3763 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
3765 * p3-p0: packet type: 0
3766 * m7-m0: model ID: MouseMan+:0x50,
3767 * FirstMouse+:0x51,
3768 * ScrollPoint:0x58...
3770 /* check constant bits */
3771 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
3772 return (FALSE);
3773 if ((data[1] & 0xc3) != 0xc2)
3774 return (FALSE);
3775 /* check d3-d0 in byte 2 */
3776 if (!MOUSE_PS2PLUS_CHECKBITS(data))
3777 return (FALSE);
3778 /* check p3-p0 */
3779 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
3780 return (FALSE);
3782 sc->hw.hwid &= 0x00ff;
3783 sc->hw.hwid |= data[2] << 8; /* save model ID */
3786 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
3787 * the wheel and the fourth button events are encoded in the
3788 * special data packet. The mouse may be put in the IntelliMouse mode
3789 * if it is initialized by the IntelliMouse's method.
3791 return (TRUE);
3794 /* MS IntelliMouse Explorer */
3795 static int
3796 enable_msexplorer(struct psm_softc *sc)
3798 static u_char rate0[] = { 200, 100, 80, };
3799 static u_char rate1[] = { 200, 200, 80, };
3800 KBDC kbdc = sc->kbdc;
3801 int id;
3802 int i;
3805 * This is needed for at least A4Tech X-7xx mice - they do not go
3806 * straight to Explorer mode, but need to be set to Intelli mode
3807 * first.
3809 enable_msintelli(sc);
3811 /* the special sequence to enable the extra buttons and the roller. */
3812 for (i = 0; i < NELEM(rate1); ++i)
3813 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
3814 return (FALSE);
3815 /* the device will give the genuine ID only after the above sequence */
3816 id = get_aux_id(kbdc);
3817 if (id != PSM_EXPLORER_ID)
3818 return (FALSE);
3820 sc->hw.hwid = id;
3821 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
3824 * XXX: this is a kludge to fool some KVM switch products
3825 * which think they are clever enough to know the 4-byte IntelliMouse
3826 * protocol, and assume any other protocols use 3-byte packets.
3827 * They don't convey 4-byte data packets from the IntelliMouse Explorer
3828 * correctly to the host computer because of this!
3829 * The following sequence is actually IntelliMouse's "wake up"
3830 * sequence; it will make the KVM think the mouse is IntelliMouse
3831 * when it is in fact IntelliMouse Explorer.
3833 for (i = 0; i < NELEM(rate0); ++i)
3834 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
3835 break;
3836 id = get_aux_id(kbdc);
3838 return (TRUE);
3841 /* MS IntelliMouse */
3842 static int
3843 enable_msintelli(struct psm_softc *sc)
3846 * Logitech MouseMan+ and FirstMouse+ will also respond to this
3847 * probe routine and act like IntelliMouse.
3850 static u_char rate[] = { 200, 100, 80, };
3851 KBDC kbdc = sc->kbdc;
3852 int id;
3853 int i;
3855 /* the special sequence to enable the third button and the roller. */
3856 for (i = 0; i < NELEM(rate); ++i)
3857 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3858 return (FALSE);
3859 /* the device will give the genuine ID only after the above sequence */
3860 id = get_aux_id(kbdc);
3861 if (id != PSM_INTELLI_ID)
3862 return (FALSE);
3864 sc->hw.hwid = id;
3865 sc->hw.buttons = 3;
3867 return (TRUE);
3870 /* A4 Tech 4D Mouse */
3871 static int
3872 enable_4dmouse(struct psm_softc *sc)
3875 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
3878 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
3879 KBDC kbdc = sc->kbdc;
3880 int id;
3881 int i;
3883 for (i = 0; i < NELEM(rate); ++i)
3884 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3885 return (FALSE);
3886 id = get_aux_id(kbdc);
3888 * WinEasy 4D, 4 Way Scroll 4D: 6
3889 * Cable-Free 4D: 8 (4DPLUS)
3890 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
3892 if (id != PSM_4DMOUSE_ID)
3893 return (FALSE);
3895 sc->hw.hwid = id;
3896 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
3898 return (TRUE);
3901 /* A4 Tech 4D+ Mouse */
3902 static int
3903 enable_4dplus(struct psm_softc *sc)
3906 * Newer wheel mice from A4 Tech seem to use this protocol.
3907 * Older models are recognized as either 4D Mouse or IntelliMouse.
3909 KBDC kbdc = sc->kbdc;
3910 int id;
3913 * enable_4dmouse() already issued the following ID sequence...
3914 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
3915 int i;
3917 for (i = 0; i < NELEM(rate); ++i)
3918 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3919 return (FALSE);
3922 id = get_aux_id(kbdc);
3923 switch (id) {
3924 case PSM_4DPLUS_ID:
3925 sc->hw.buttons = 4;
3926 break;
3927 case PSM_4DPLUS_RFSW35_ID:
3928 sc->hw.buttons = 3;
3929 break;
3930 default:
3931 return (FALSE);
3934 sc->hw.hwid = id;
3936 return (TRUE);
3939 /* Synaptics Touchpad */
3940 static int
3941 synaptics_sysctl(SYSCTL_HANDLER_ARGS)
3943 int error, arg;
3945 /* Read the current value. */
3946 arg = *(int *)oidp->oid_arg1;
3947 error = sysctl_handle_int(oidp, &arg, 0, req);
3949 /* Sanity check. */
3950 if (error || !req->newptr)
3951 return (error);
3954 * Check that the new value is in the concerned node's range
3955 * of values.
3957 switch (oidp->oid_arg2) {
3958 case SYNAPTICS_SYSCTL_MIN_PRESSURE:
3959 case SYNAPTICS_SYSCTL_MAX_PRESSURE:
3960 if (arg < 0 || arg > 255)
3961 return (EINVAL);
3962 break;
3963 case SYNAPTICS_SYSCTL_MAX_WIDTH:
3964 if (arg < 4 || arg > 15)
3965 return (EINVAL);
3966 break;
3967 case SYNAPTICS_SYSCTL_MARGIN_TOP:
3968 case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
3969 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
3970 case SYNAPTICS_SYSCTL_MARGIN_LEFT:
3971 case SYNAPTICS_SYSCTL_NA_TOP:
3972 case SYNAPTICS_SYSCTL_NA_RIGHT:
3973 case SYNAPTICS_SYSCTL_NA_BOTTOM:
3974 case SYNAPTICS_SYSCTL_NA_LEFT:
3975 if (arg < 0 || arg > 6143)
3976 return (EINVAL);
3977 break;
3978 case SYNAPTICS_SYSCTL_WINDOW_MIN:
3979 case SYNAPTICS_SYSCTL_WINDOW_MAX:
3980 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
3981 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
3982 return (EINVAL);
3983 break;
3984 case SYNAPTICS_SYSCTL_MULTIPLICATOR:
3985 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
3986 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
3987 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
3988 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
3989 case SYNAPTICS_SYSCTL_DIV_MIN:
3990 case SYNAPTICS_SYSCTL_DIV_MAX:
3991 case SYNAPTICS_SYSCTL_DIV_MAX_NA:
3992 case SYNAPTICS_SYSCTL_DIV_LEN:
3993 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
3994 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
3995 if (arg < 1)
3996 return (EINVAL);
3997 break;
3998 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
3999 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
4000 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
4001 if (arg < 0)
4002 return (EINVAL);
4003 break;
4004 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
4005 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
4006 if (arg < -6143 || arg > 6143)
4007 return (EINVAL);
4008 break;
4009 default:
4010 return (EINVAL);
4013 /* Update. */
4014 *(int *)oidp->oid_arg1 = arg;
4016 return (error);
4019 static void
4020 synaptics_sysctl_create_tree(struct psm_softc *sc)
4023 if (sc->syninfo.sysctl_tree != NULL)
4024 return;
4026 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
4027 sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
4028 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
4029 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "synaptics", CTLFLAG_RD,
4030 0, "Synaptics TouchPad");
4032 /* hw.psm.synaptics.directional_scrolls. */
4033 sc->syninfo.directional_scrolls = 1;
4034 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
4035 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4036 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
4037 &sc->syninfo.directional_scrolls, 0,
4038 "Enable hardware scrolling pad (if non-zero) or register it as "
4039 "a middle-click (if 0)");
4041 /* hw.psm.synaptics.min_pressure. */
4042 sc->syninfo.min_pressure = 16;
4043 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4044 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4045 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4046 &sc->syninfo.min_pressure, SYNAPTICS_SYSCTL_MIN_PRESSURE,
4047 synaptics_sysctl, "I",
4048 "Minimum pressure required to start an action");
4050 /* hw.psm.synaptics.max_pressure. */
4051 sc->syninfo.max_pressure = 220;
4052 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4053 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4054 "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4055 &sc->syninfo.max_pressure, SYNAPTICS_SYSCTL_MAX_PRESSURE,
4056 synaptics_sysctl, "I",
4057 "Maximum pressure to detect palm");
4059 /* hw.psm.synaptics.max_width. */
4060 sc->syninfo.max_width = 10;
4061 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4062 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4063 "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4064 &sc->syninfo.max_width, SYNAPTICS_SYSCTL_MAX_WIDTH,
4065 synaptics_sysctl, "I",
4066 "Maximum finger width to detect palm");
4068 /* hw.psm.synaptics.top_margin. */
4069 sc->syninfo.margin_top = 200;
4070 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4071 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4072 "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4073 &sc->syninfo.margin_top, SYNAPTICS_SYSCTL_MARGIN_TOP,
4074 synaptics_sysctl, "I",
4075 "Top margin");
4077 /* hw.psm.synaptics.right_margin. */
4078 sc->syninfo.margin_right = 200;
4079 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4080 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4081 "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4082 &sc->syninfo.margin_right, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
4083 synaptics_sysctl, "I",
4084 "Right margin");
4086 /* hw.psm.synaptics.bottom_margin. */
4087 sc->syninfo.margin_bottom = 200;
4088 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4089 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4090 "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4091 &sc->syninfo.margin_bottom, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
4092 synaptics_sysctl, "I",
4093 "Bottom margin");
4095 /* hw.psm.synaptics.left_margin. */
4096 sc->syninfo.margin_left = 200;
4097 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4098 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4099 "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4100 &sc->syninfo.margin_left, SYNAPTICS_SYSCTL_MARGIN_LEFT,
4101 synaptics_sysctl, "I",
4102 "Left margin");
4104 /* hw.psm.synaptics.na_top. */
4105 sc->syninfo.na_top = 1783;
4106 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4107 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4108 "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4109 &sc->syninfo.na_top, SYNAPTICS_SYSCTL_NA_TOP,
4110 synaptics_sysctl, "I",
4111 "Top noisy area, where weight_previous_na is used instead "
4112 "of weight_previous");
4114 /* hw.psm.synaptics.na_right. */
4115 sc->syninfo.na_right = 563;
4116 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4117 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4118 "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4119 &sc->syninfo.na_right, SYNAPTICS_SYSCTL_NA_RIGHT,
4120 synaptics_sysctl, "I",
4121 "Right noisy area, where weight_previous_na is used instead "
4122 "of weight_previous");
4124 /* hw.psm.synaptics.na_bottom. */
4125 sc->syninfo.na_bottom = 1408;
4126 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4127 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4128 "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4129 &sc->syninfo.na_bottom, SYNAPTICS_SYSCTL_NA_BOTTOM,
4130 synaptics_sysctl, "I",
4131 "Bottom noisy area, where weight_previous_na is used instead "
4132 "of weight_previous");
4134 /* hw.psm.synaptics.na_left. */
4135 sc->syninfo.na_left = 1600;
4136 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4137 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4138 "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4139 &sc->syninfo.na_left, SYNAPTICS_SYSCTL_NA_LEFT,
4140 synaptics_sysctl, "I",
4141 "Left noisy area, where weight_previous_na is used instead "
4142 "of weight_previous");
4144 /* hw.psm.synaptics.window_min. */
4145 sc->syninfo.window_min = 4;
4146 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4147 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4148 "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4149 &sc->syninfo.window_min, SYNAPTICS_SYSCTL_WINDOW_MIN,
4150 synaptics_sysctl, "I",
4151 "Minimum window size to start an action");
4153 /* hw.psm.synaptics.window_max. */
4154 sc->syninfo.window_max = 10;
4155 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4156 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4157 "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4158 &sc->syninfo.window_max, SYNAPTICS_SYSCTL_WINDOW_MAX,
4159 synaptics_sysctl, "I",
4160 "Maximum window size");
4162 /* hw.psm.synaptics.multiplicator. */
4163 sc->syninfo.multiplicator = 10000;
4164 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4165 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4166 "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4167 &sc->syninfo.multiplicator, SYNAPTICS_SYSCTL_MULTIPLICATOR,
4168 synaptics_sysctl, "I",
4169 "Multiplicator to increase precision in averages and divisions");
4171 /* hw.psm.synaptics.weight_current. */
4172 sc->syninfo.weight_current = 3;
4173 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4174 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4175 "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4176 &sc->syninfo.weight_current, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
4177 synaptics_sysctl, "I",
4178 "Weight of the current movement in the new average");
4180 /* hw.psm.synaptics.weight_previous. */
4181 sc->syninfo.weight_previous = 6;
4182 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4183 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4184 "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4185 &sc->syninfo.weight_previous, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
4186 synaptics_sysctl, "I",
4187 "Weight of the previous average");
4189 /* hw.psm.synaptics.weight_previous_na. */
4190 sc->syninfo.weight_previous_na = 20;
4191 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4192 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4193 "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4194 &sc->syninfo.weight_previous_na,
4195 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
4196 synaptics_sysctl, "I",
4197 "Weight of the previous average (inside the noisy area)");
4199 /* hw.psm.synaptics.weight_len_squared. */
4200 sc->syninfo.weight_len_squared = 2000;
4201 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4202 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4203 "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4204 &sc->syninfo.weight_len_squared,
4205 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
4206 synaptics_sysctl, "I",
4207 "Length (squared) of segments where weight_previous "
4208 "starts to decrease");
4210 /* hw.psm.synaptics.div_min. */
4211 sc->syninfo.div_min = 9;
4212 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4213 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4214 "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4215 &sc->syninfo.div_min, SYNAPTICS_SYSCTL_DIV_MIN,
4216 synaptics_sysctl, "I",
4217 "Divisor for fast movements");
4219 /* hw.psm.synaptics.div_max. */
4220 sc->syninfo.div_max = 17;
4221 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4222 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4223 "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4224 &sc->syninfo.div_max, SYNAPTICS_SYSCTL_DIV_MAX,
4225 synaptics_sysctl, "I",
4226 "Divisor for slow movements");
4228 /* hw.psm.synaptics.div_max_na. */
4229 sc->syninfo.div_max_na = 30;
4230 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4231 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4232 "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4233 &sc->syninfo.div_max_na, SYNAPTICS_SYSCTL_DIV_MAX_NA,
4234 synaptics_sysctl, "I",
4235 "Divisor with slow movements (inside the noisy area)");
4237 /* hw.psm.synaptics.div_len. */
4238 sc->syninfo.div_len = 100;
4239 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4240 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4241 "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4242 &sc->syninfo.div_len, SYNAPTICS_SYSCTL_DIV_LEN,
4243 synaptics_sysctl, "I",
4244 "Length of segments where div_max starts to decrease");
4246 /* hw.psm.synaptics.tap_max_delta. */
4247 sc->syninfo.tap_max_delta = 80;
4248 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4249 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4250 "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4251 &sc->syninfo.tap_max_delta, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
4252 synaptics_sysctl, "I",
4253 "Length of segments above which a tap is ignored");
4255 /* hw.psm.synaptics.tap_min_queue. */
4256 sc->syninfo.tap_min_queue = 2;
4257 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4258 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4259 "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4260 &sc->syninfo.tap_min_queue, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
4261 synaptics_sysctl, "I",
4262 "Number of packets required to consider a tap");
4264 /* hw.psm.synaptics.taphold_timeout. */
4265 sc->synaction.in_taphold = 0;
4266 sc->syninfo.taphold_timeout = tap_timeout;
4267 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4268 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4269 "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4270 &sc->syninfo.taphold_timeout, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
4271 synaptics_sysctl, "I",
4272 "Maximum elapsed time between two taps to consider a tap-hold "
4273 "action");
4275 /* hw.psm.synaptics.vscroll_hor_area. */
4276 sc->syninfo.vscroll_hor_area = 0; /* 1300 */
4277 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4278 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4279 "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4280 &sc->syninfo.vscroll_hor_area, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
4281 synaptics_sysctl, "I",
4282 "Area reserved for horizontal virtual scrolling");
4284 /* hw.psm.synaptics.vscroll_ver_area. */
4285 sc->syninfo.vscroll_ver_area = -600;
4286 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4287 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4288 "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4289 &sc->syninfo.vscroll_ver_area, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
4290 synaptics_sysctl, "I",
4291 "Area reserved for vertical virtual scrolling");
4293 /* hw.psm.synaptics.vscroll_min_delta. */
4294 sc->syninfo.vscroll_min_delta = 50;
4295 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4296 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4297 "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4298 &sc->syninfo.vscroll_min_delta,
4299 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
4300 synaptics_sysctl, "I",
4301 "Minimum movement to consider virtual scrolling");
4303 /* hw.psm.synaptics.vscroll_div_min. */
4304 sc->syninfo.vscroll_div_min = 100;
4305 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4306 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4307 "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4308 &sc->syninfo.vscroll_div_min, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
4309 synaptics_sysctl, "I",
4310 "Divisor for fast scrolling");
4312 /* hw.psm.synaptics.vscroll_div_min. */
4313 sc->syninfo.vscroll_div_max = 150;
4314 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4315 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4316 "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4317 &sc->syninfo.vscroll_div_max, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
4318 synaptics_sysctl, "I",
4319 "Divisor for slow scrolling");
4322 static int
4323 enable_synaptics(struct psm_softc *sc)
4325 int status[3];
4326 KBDC kbdc;
4328 if (!synaptics_support)
4329 return (FALSE);
4331 kbdc = sc->kbdc;
4332 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
4333 sc->hw.buttons = 3;
4334 sc->squelch = 0;
4337 * Just to be on the safe side: this avoids troubles with
4338 * following mouse_ext_command() when the previous command
4339 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
4340 * Synaptics Touchpad behaviour.
4342 set_mouse_scaling(kbdc, 1);
4344 /* Identify the Touchpad version. */
4345 if (mouse_ext_command(kbdc, 0) == 0)
4346 return (FALSE);
4347 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4348 return (FALSE);
4349 if (status[1] != 0x47)
4350 return (FALSE);
4352 sc->synhw.infoMinor = status[0];
4353 sc->synhw.infoMajor = status[2] & 0x0f;
4355 if (verbose >= 2)
4356 kprintf("Synaptics Touchpad v%d.%d\n", sc->synhw.infoMajor,
4357 sc->synhw.infoMinor);
4359 if (sc->synhw.infoMajor < 4) {
4360 kprintf(" Unsupported (pre-v4) Touchpad detected\n");
4361 return (FALSE);
4364 /* Get the Touchpad model information. */
4365 if (mouse_ext_command(kbdc, 3) == 0)
4366 return (FALSE);
4367 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4368 return (FALSE);
4369 if ((status[1] & 0x01) != 0) {
4370 kprintf(" Failed to read model information\n");
4371 return (FALSE);
4374 sc->synhw.infoRot180 = (status[0] & 0x80) >> 7;
4375 sc->synhw.infoPortrait = (status[0] & 0x40) >> 6;
4376 sc->synhw.infoSensor = status[0] & 0x3f;
4377 sc->synhw.infoHardware = (status[1] & 0xfe) >> 1;
4378 sc->synhw.infoNewAbs = (status[2] & 0x80) >> 7;
4379 sc->synhw.capPen = (status[2] & 0x40) >> 6;
4380 sc->synhw.infoSimplC = (status[2] & 0x20) >> 5;
4381 sc->synhw.infoGeometry = status[2] & 0x0f;
4383 if (verbose >= 2) {
4384 kprintf(" Model information:\n");
4385 kprintf(" infoRot180: %d\n", sc->synhw.infoRot180);
4386 kprintf(" infoPortrait: %d\n", sc->synhw.infoPortrait);
4387 kprintf(" infoSensor: %d\n", sc->synhw.infoSensor);
4388 kprintf(" infoHardware: %d\n", sc->synhw.infoHardware);
4389 kprintf(" infoNewAbs: %d\n", sc->synhw.infoNewAbs);
4390 kprintf(" capPen: %d\n", sc->synhw.capPen);
4391 kprintf(" infoSimplC: %d\n", sc->synhw.infoSimplC);
4392 kprintf(" infoGeometry: %d\n", sc->synhw.infoGeometry);
4395 /* Read the extended capability bits. */
4396 if (mouse_ext_command(kbdc, 2) == 0)
4397 return (FALSE);
4398 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4399 return (FALSE);
4400 if (!SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) && status[1] != 0x47) {
4401 kprintf(" Failed to read extended capability bits\n");
4402 return (FALSE);
4405 /* Set the different capabilities when they exist. */
4406 if ((status[0] & 0x80) >> 7) {
4407 sc->synhw.capExtended = (status[0] & 0x80) >> 7;
4408 sc->synhw.capPassthrough = (status[2] & 0x80) >> 7;
4409 sc->synhw.capSleep = (status[2] & 0x10) >> 4;
4410 sc->synhw.capFourButtons = (status[2] & 0x08) >> 3;
4411 sc->synhw.capMultiFinger = (status[2] & 0x02) >> 1;
4412 sc->synhw.capPalmDetect = (status[2] & 0x01);
4414 if (verbose >= 2) {
4415 kprintf(" Extended capabilities:\n");
4416 kprintf(" capExtended: %d\n", sc->synhw.capExtended);
4417 kprintf(" capPassthrough: %d\n",
4418 sc->synhw.capPassthrough);
4419 kprintf(" capSleep: %d\n", sc->synhw.capSleep);
4420 kprintf(" capFourButtons: %d\n",
4421 sc->synhw.capFourButtons);
4422 kprintf(" capMultiFinger: %d\n",
4423 sc->synhw.capMultiFinger);
4424 kprintf(" capPalmDetect: %d\n",
4425 sc->synhw.capPalmDetect);
4429 * If we have bits set in status[0] & 0x70, then we can load
4430 * more information about buttons using query 0x09.
4432 if (status[0] & 0x70) {
4433 if (mouse_ext_command(kbdc, 0x09) == 0)
4434 return (FALSE);
4435 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4436 return (FALSE);
4437 sc->hw.buttons = ((status[1] & 0xf0) >> 4) + 3;
4438 if (verbose >= 2)
4439 kprintf(" Additional Buttons: %d\n",
4440 sc->hw.buttons -3);
4442 } else {
4443 sc->synhw.capExtended = 0;
4445 if (verbose >= 2)
4446 kprintf(" No extended capabilities\n");
4450 * Read the mode byte.
4452 * XXX: Note the Synaptics documentation also defines the first
4453 * byte of the response to this query to be a constant 0x3b, this
4454 * does not appear to be true for Touchpads with guest devices.
4456 if (mouse_ext_command(kbdc, 1) == 0)
4457 return (FALSE);
4458 if (get_mouse_status(kbdc, status, 0, 3) != 3)
4459 return (FALSE);
4460 if (!SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) && status[1] != 0x47) {
4461 kprintf(" Failed to read mode byte\n");
4462 return (FALSE);
4465 /* Set the mode byte; request wmode where available. */
4466 if (sc->synhw.capExtended)
4467 mouse_ext_command(kbdc, 0xc1);
4468 else
4469 mouse_ext_command(kbdc, 0xc0);
4471 /* "Commit" the Set Mode Byte command sent above. */
4472 set_mouse_sampling_rate(kbdc, 20);
4475 * Report the correct number of buttons
4477 * XXX: I'm not sure this is used anywhere.
4479 if (sc->synhw.capExtended && sc->synhw.capFourButtons)
4480 sc->hw.buttons = 4;
4482 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n",
4483 sc->hw.buttons));
4485 /* Create sysctl tree. */
4486 synaptics_sysctl_create_tree(sc);
4489 * The touchpad will have to be reinitialized after
4490 * suspend/resume.
4492 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
4494 return (TRUE);
4497 /* Interlink electronics VersaPad */
4498 static int
4499 enable_versapad(struct psm_softc *sc)
4501 KBDC kbdc = sc->kbdc;
4502 int data[3];
4504 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
4505 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
4506 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4507 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4508 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4509 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4510 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
4511 return (FALSE);
4512 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
4513 return (FALSE);
4514 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
4516 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
4518 return (TRUE); /* PS/2 absolute mode */
4522 * Return true if 'now' is earlier than (start + (secs.usecs)).
4523 * Now may be NULL and the function will fetch the current time from
4524 * getmicrouptime(), or a cached 'now' can be passed in.
4525 * All values should be numbers derived from getmicrouptime().
4527 static int
4528 timeelapsed(const struct timeval *start, int secs, int usecs,
4529 const struct timeval *now)
4531 struct timeval snow, tv;
4533 /* if there is no 'now' passed in, the get it as a convience. */
4534 if (now == NULL) {
4535 getmicrouptime(&snow);
4536 now = &snow;
4539 tv.tv_sec = secs;
4540 tv.tv_usec = usecs;
4541 timevaladd(&tv, start);
4542 return (timevalcmp(&tv, now, <));
4545 static int
4546 psmresume(device_t dev)
4548 struct psm_softc *sc = device_get_softc(dev);
4549 int unit = device_get_unit(dev);
4550 int err;
4552 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
4554 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
4555 return (0);
4557 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
4559 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
4561 * Release the blocked process; it must be notified that
4562 * the device cannot be accessed anymore.
4564 sc->state &= ~PSM_ASLP;
4565 wakeup(sc);
4568 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
4570 return (err);
4573 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, NULL, NULL);
4576 #if 0
4578 * This sucks up assignments from PNPBIOS and ACPI.
4582 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
4583 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
4584 * can be probed and attached only after the AT keyboard controller is
4585 * attached, we shall quietly reserve the IRQ resource for later use.
4586 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
4587 * copy the IRQ resource to the PS/2 mouse device instance hanging
4588 * under the keyboard controller, then probe and attach it.
4591 static devclass_t psmcpnp_devclass;
4593 static device_probe_t psmcpnp_probe;
4594 static device_attach_t psmcpnp_attach;
4596 static device_method_t psmcpnp_methods[] = {
4597 DEVMETHOD(device_probe, psmcpnp_probe),
4598 DEVMETHOD(device_attach, psmcpnp_attach),
4600 DEVMETHOD_END
4603 static driver_t psmcpnp_driver = {
4604 PSMCPNP_DRIVER_NAME,
4605 psmcpnp_methods,
4606 1, /* no softc */
4609 static struct isa_pnp_id psmcpnp_ids[] = {
4610 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
4611 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */
4612 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */
4613 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
4614 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
4615 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
4616 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */
4617 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
4618 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
4619 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
4620 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
4621 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
4622 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
4623 { 0 }
4626 static int
4627 create_a_copy(device_t atkbdc, device_t me)
4629 device_t psm;
4630 u_long irq;
4632 /* find the PS/2 mouse device instance under the keyboard controller */
4633 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
4634 device_get_unit(atkbdc));
4635 if (psm == NULL)
4636 return (ENXIO);
4637 if (device_get_state(psm) != DS_NOTPRESENT)
4638 return (0);
4640 /* move our resource to the found device */
4641 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
4642 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1,
4643 machintr_legacy_intr_cpuid(irq));
4645 /* ...then probe and attach it */
4646 return (device_probe_and_attach(psm));
4649 static int
4650 psmcpnp_probe(device_t dev)
4652 struct resource *res;
4653 u_long irq;
4654 int rid;
4656 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
4657 return (ENXIO);
4660 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
4661 * to the PS/2 mouse device node. But, some buggy PnP BIOS
4662 * declares the PS/2 mouse device node without an IRQ resource!
4663 * If this happens, we shall refer to device hints.
4664 * If we still don't find it there, use a hardcoded value... XXX
4666 rid = 0;
4667 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
4668 if (irq <= 0) {
4669 if (resource_long_value(PSM_DRIVER_NAME,
4670 device_get_unit(dev),"irq", &irq) != 0)
4671 irq = 12; /* XXX */
4672 device_printf(dev, "irq resource info is missing; "
4673 "assuming irq %ld\n", irq);
4674 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1,
4675 machintr_legacy_intr_cpuid(irq));
4677 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
4678 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
4680 /* keep quiet */
4681 if (!bootverbose)
4682 device_quiet(dev);
4684 return ((res == NULL) ? ENXIO : 0);
4687 static int
4688 psmcpnp_attach(device_t dev)
4690 device_t atkbdc;
4691 int rid;
4693 /* find the keyboard controller, which may be on acpi* or isa* bus */
4694 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
4695 device_get_unit(dev));
4696 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
4697 create_a_copy(atkbdc, dev);
4698 else {
4700 * If we don't have the AT keyboard controller yet,
4701 * just reserve the IRQ for later use...
4702 * (See psmidentify() above.)
4704 rid = 0;
4705 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
4708 return (0);
4711 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, NULL, NULL);
4712 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, NULL, NULL);
4713 #endif